2018-06-13 22:41:05 +00:00
|
|
|
// maubot - A plugin-based Matrix bot system written in Go.
|
|
|
|
// Copyright (C) 2018 Tulir Asokan
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package maubot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"plugin"
|
|
|
|
|
|
|
|
"maubot.xyz/database"
|
2018-06-14 07:29:37 +00:00
|
|
|
"maubot.xyz/interfaces"
|
2018-06-13 22:41:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PluginWrapper struct {
|
2018-06-14 07:29:37 +00:00
|
|
|
interfaces.Plugin
|
|
|
|
Creator *interfaces.PluginCreator
|
2018-06-13 22:41:05 +00:00
|
|
|
DB *database.Plugin
|
|
|
|
}
|
|
|
|
|
2018-06-14 07:29:37 +00:00
|
|
|
func LoadPlugin(path string) (*interfaces.PluginCreator, error) {
|
2018-06-13 22:41:05 +00:00
|
|
|
rawPlugin, err := plugin.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to open: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginCreatorSymbol, err := rawPlugin.Lookup("Plugin")
|
|
|
|
if err == nil {
|
2018-06-14 07:29:37 +00:00
|
|
|
pluginCreator, ok := pluginCreatorSymbol.(*interfaces.PluginCreator)
|
2018-06-13 22:41:05 +00:00
|
|
|
if ok {
|
|
|
|
pluginCreator.Path = path
|
|
|
|
return pluginCreator, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pluginCreatorFuncSymbol, err := rawPlugin.Lookup("Create")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("symbol \"Create\" not found: %v", err)
|
|
|
|
}
|
2018-06-14 07:29:37 +00:00
|
|
|
pluginCreatorFunc, ok := pluginCreatorFuncSymbol.(interfaces.PluginCreatorFunc)
|
2018-06-13 22:41:05 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("symbol \"Create\" does not implement maubot.PluginCreator")
|
|
|
|
}
|
|
|
|
|
|
|
|
nameSymbol, err := rawPlugin.Lookup("Name")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("symbol \"Name\" not found: %v", err)
|
|
|
|
}
|
|
|
|
name, ok := nameSymbol.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("symbol \"Name\" is not a string")
|
|
|
|
}
|
|
|
|
|
|
|
|
versionSymbol, err := rawPlugin.Lookup("Version")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("symbol \"Version\" not found: %v", err)
|
|
|
|
}
|
|
|
|
version, ok := versionSymbol.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("symbol \"Version\" is not a string")
|
|
|
|
}
|
|
|
|
|
2018-06-14 07:29:37 +00:00
|
|
|
return &interfaces.PluginCreator{
|
2018-06-13 22:41:05 +00:00
|
|
|
Create: pluginCreatorFunc,
|
|
|
|
Name: name,
|
|
|
|
Version: version,
|
|
|
|
Path: path,
|
|
|
|
}, nil
|
|
|
|
}
|