plugin manager qui fonctionne

This commit is contained in:
KLIPFEL Arthur 2018-07-25 11:23:11 +02:00
parent 8301e1dfaa
commit 54c3bc8f59
6 changed files with 232 additions and 32 deletions

68
plugin.go Normal file
View File

@ -0,0 +1,68 @@
package main
import (
"./plugin_manager"
tb "gopkg.in/tucnak/telebot.v2"
)
type plugin string
func (g plugin) GetCommands() []string {
return []string{"plugin"}
}
func (g plugin) HandleCommand(bot *tb.Bot, msg *tb.Message, cmd string, args []string) {
if cmd == "plugin" {
ok := false
if len(args) >= 1 {
if args[0] == "list" {
lst := ""
for _, pName := range plugin_manager.GetPluginList() {
lst = lst + "-" + pName + " (status: "
if plugin_manager.IsPluginEnable(pName) {
lst += "enable, "
} else {
lst += "disable, "
}
if plugin_manager.IsPluginRunning(pName) {
lst += "running"
} else {
lst += "stopped"
}
lst += ")\n"
}
bot.Send(msg.Chat, "liste des plugins disponible:\n"+lst)
ok = true
}
if args[0] == "enable" && len(args) >= 2 {
for _, name := range args[1:] {
if plugin_manager.ExistPlugin(name) {
plugin_manager.EnablePlugin(name, true)
bot.Send(msg.Chat, "enable plugin "+name)
}
}
ok = true
}
if args[0] == "disable" && len(args) >= 2 {
for _, name := range args[1:] {
if plugin_manager.ExistPlugin(name) {
plugin_manager.EnablePlugin(name, false)
bot.Send(msg.Chat, "disable plugin "+name)
}
}
ok = true
}
}
if !ok {
bot.Send(msg.Chat, "command inconnue\n"+
"liste de plugin:\n"+
"/plugin list\n"+
"activer un/des plugins\n"+
"/plugin enable nom_plugin\n"+
"désactiver un/des plugins\n"+
"/plugin disable nom_plugin")
}
}
}
var Plugin plugin

View File

@ -1,22 +0,0 @@
package main
import (
tb "gopkg.in/tucnak/telebot.v2"
"log"
)
type plugin string
func (g plugin) Load() {
log.Println("plugin test loaded!")
}
func (g plugin) HandleMessage(bot *tb.Bot, msg string) {
log.Println("test message: " + msg)
}
func (g plugin) Unload() {
log.Println("plugin test unloaded!")
}
var Plugin plugin

View File

@ -7,6 +7,7 @@ package plugin_manager
import ( import (
tb "gopkg.in/tucnak/telebot.v2" tb "gopkg.in/tucnak/telebot.v2"
"log" "log"
"strings"
"sync" "sync"
) )
@ -38,6 +39,42 @@ func Init(_pluginDir string, bot *tb.Bot) {
} }
} }
func GetPluginList() []string {
var lst []string
for name, _ := range plugins {
lst = append(lst, name)
}
return lst
}
func IsPluginRunning(name string) bool {
if p, ok := plugins[name]; ok {
return p.running
}
return false
}
func ExistPlugin(name string) bool {
if _, ok := plugins[name]; ok {
return true
}
return false
}
func GetPlugin(name string) Plugin {
if p, ok := plugins[name]; ok {
return p
}
return nil
}
func IsPluginEnable(name string) bool {
if p, ok := plugins[name]; ok {
return p.enable
}
return false
}
func EnablePlugin(name string, enable bool) { func EnablePlugin(name string, enable bool) {
if p, ok := plugins[name]; ok { if p, ok := plugins[name]; ok {
if enable != p.enable { if enable != p.enable {
@ -69,8 +106,16 @@ func StopPlugins() {
func HandleMessage(msg *tb.Message) { func HandleMessage(msg *tb.Message) {
for _, val := range plugins { for _, val := range plugins {
if val.enable { if val.enable && val.running {
val.plugin.HandleMessage(context.bot, msg.Text) if strings.HasPrefix(msg.Text, "/") {
split := strings.Split(msg.Text, " ")
split[0] = split[0][1:]
if Contains(split[0], ExecGetCommands(val.plugin)) {
ExecHandleCommand(val.plugin, context.bot, msg, split[0], split[1:])
}
} else {
ExecHandleMessage(val.plugin, context.bot, msg)
}
} }
} }
} }
@ -88,9 +133,8 @@ func startPlugin(name string) {
if p, ok := plugins[name]; ok { if p, ok := plugins[name]; ok {
//p.mux.Lock() //p.mux.Lock()
if !p.running && p.enable { if !p.running && p.enable {
p.running = true p.running = ExecLoad(p.plugin)
plugins[name] = p plugins[name] = p
p.plugin.Load()
} }
//p.mux.Unlock() //p.mux.Unlock()
} }
@ -102,7 +146,7 @@ func stopPlugin(name string) {
if p.running { if p.running {
p.running = false p.running = false
plugins[name] = p plugins[name] = p
p.plugin.Unload() ExecUnload(p.plugin)
} }
//p.mux.Unlock() //p.mux.Unlock()
} }
@ -111,7 +155,7 @@ func stopPlugin(name string) {
func Exit() { func Exit() {
for _, p := range plugins { for _, p := range plugins {
if p.running { if p.running {
p.plugin.Unload() ExecUnload(p.plugin)
} }
} }
} }

View File

@ -12,10 +12,32 @@ import (
"strings" "strings"
) )
type Plugin interface { type TestGetCommands interface {
Load() GetCommands() []string
HandleMessage(bot *tb.Bot, msg string) }
Unload()
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 { func GetSoFiles(dir string) []string {
@ -48,6 +70,7 @@ func LoadSoFile(file string) Plugin {
} }
var plugin Plugin var plugin Plugin
//plugin, _ = symPlugin.(Plugin)
plugin, ok := symPlugin.(Plugin) plugin, ok := symPlugin.(Plugin)
if !ok { if !ok {
log.Fatal(file + ": unexpected type from module symbol") log.Fatal(file + ": unexpected type from module symbol")
@ -55,3 +78,50 @@ func LoadSoFile(file string) Plugin {
} }
return plugin 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
}

40
test.go Normal file
View File

@ -0,0 +1,40 @@
package main
import (
tb "gopkg.in/tucnak/telebot.v2"
"log"
)
type plugin string
func (g plugin) GetCommands() []string {
return []string{"ping", "test"}
}
func (g plugin) Load() bool {
log.Println("plugin test loaded!")
return true
}
func (g plugin) HandleMessage(bot *tb.Bot, msg *tb.Message) {
log.Println("plugin test message: " + msg.Text)
}
func (g plugin) HandleCommand(bot *tb.Bot, msg *tb.Message, cmd string, args []string) {
if cmd == "ping" {
bot.Send(msg.Chat, "pong!")
}
argsS := ""
for _, arg := range args {
argsS = argsS + " " + arg
}
log.Print("plugin test cmd: " + cmd + " (args:" + argsS + ")")
}
func (g plugin) Unload() bool {
log.Println("plugin test unloaded!")
return true
}
var Plugin plugin