ALFRED/plugin_manager/tools.go

128 lines
2.2 KiB
Go

/**
* @Author: KLIPFEL Arthur
* @Date: 2018-08-24 12:17:17
*/
package plugin_manager
import (
tb "gopkg.in/tucnak/telebot.v2"
"io/ioutil"
"log"
"plugin"
"strings"
)
type TestGetCommands interface {
GetCommands() []string
}
type TestLoad interface {
Load() bool
}
type TestHandleMessage interface {
HandleMessage(bot *tb.Bot, msg *tb.Message)
}
type TestHandleCommand interface {
HandleCommand(bot *tb.Bot, msg *tb.Message, cmd string, args []string)
}
type TestUnload interface {
Unload() bool
}
type Plugin interface { /*
GetCommandes() []string
Load() bool
HandleMessage(bot *tb.Bot, msg *tb.Message)
HandleCommand(bot *tb.Bot, msg *tb.Message, cmd string, args []string)
Unload() bool*/
}
func GetSoFiles(dir string) []string {
var slice []string
files, err := ioutil.ReadDir(dir)
if err != nil {
log.Fatal(err)
} else {
for _, f := range files {
if strings.HasSuffix(f.Name(), ".so") {
slice = append(slice, f.Name())
}
}
}
return slice
}
func LoadSoFile(file string) Plugin {
plug, err := plugin.Open(file)
if err != nil {
log.Fatal(err)
return nil
}
symPlugin, err := plug.Lookup("Plugin")
if err != nil {
log.Fatal(err)
return nil
}
var plugin Plugin
//plugin, _ = symPlugin.(Plugin)
plugin, ok := symPlugin.(Plugin)
if !ok {
log.Fatal(file + ": unexpected type from module symbol")
return nil
}
return plugin
}
func ExecGetCommands(plugin Plugin) []string {
p, ok := plugin.(TestGetCommands)
if ok {
return p.GetCommands()
}
return []string{}
}
func ExecLoad(plugin Plugin) bool {
p, ok := plugin.(TestLoad)
if ok {
return p.Load()
}
return true
}
func ExecHandleMessage(plugin Plugin, bot *tb.Bot, msg *tb.Message) {
p, ok := plugin.(TestHandleMessage)
if ok {
p.HandleMessage(bot, msg)
}
}
func ExecHandleCommand(plugin Plugin, bot *tb.Bot, msg *tb.Message, cmd string, args []string) {
p, ok := plugin.(TestHandleCommand)
if ok {
p.HandleCommand(bot, msg, cmd, args)
}
}
func ExecUnload(plugin Plugin) bool {
p, ok := plugin.(TestUnload)
if ok {
return p.Unload()
}
return true
}
func Contains(e string, s []string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}