des trucs pas termine qui fonctionne plus ou moins

This commit is contained in:
KLIPFEL Arthur 2018-07-24 14:04:08 +02:00
parent 99a6cd2a14
commit 8301e1dfaa
7 changed files with 233 additions and 2 deletions

View File

@ -8,6 +8,7 @@
package main
import (
"./plugin_manager"
"./settings"
"./shared"
tb "gopkg.in/tucnak/telebot.v2"
@ -63,6 +64,9 @@ func main() {
b.Send(m.Chat, "Venez participer à mon développement en posant vos idées ici : https://mensuel.framapad.org/p/ALFRED2LERETOUR.")
})
plugin_manager.Init("plugin", b)
b.Handle(tb.OnText, plugin_manager.HandleMessage)
plugin_manager.StartPlugins()
log.Println("Starting bot")
b.Start()
}

22
plugin/test.go Normal file
View File

@ -0,0 +1,22 @@
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

22
plugin/test2.go Normal file
View File

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

View File

@ -0,0 +1,9 @@
package plugin_manager
import (
tb "gopkg.in/tucnak/telebot.v2"
)
type Context struct {
bot *tb.Bot
}

117
plugin_manager/manager.go Normal file
View File

@ -0,0 +1,117 @@
/**
* @Author: KLIPFEL Arthur
* @Date: 2018-08-24 12:17:17
*/
package plugin_manager
import (
tb "gopkg.in/tucnak/telebot.v2"
"log"
"sync"
)
type PluginCtrl struct {
plugin Plugin
mux sync.Mutex
running bool
enable bool
}
var pluginDir string
var pluginsRunning bool
var plugins map[string]PluginCtrl
var context Context
func Init(_pluginDir string, bot *tb.Bot) {
pluginDir = _pluginDir
pluginsRunning = false
plugins = make(map[string]PluginCtrl)
context.bot = bot
for _, fileName := range GetSoFiles(pluginDir) {
var p PluginCtrl
p.plugin = LoadSoFile(pluginDir + "/" + fileName)
if p.plugin != nil {
p.running = false
p.enable = true
plugins[fileName[:len(fileName)-3]] = p
}
}
}
func EnablePlugin(name string, enable bool) {
if p, ok := plugins[name]; ok {
if enable != p.enable {
p.enable = enable
plugins[name] = p
if pluginsRunning {
if enable {
if !p.running {
startPlugin(name)
}
} else {
if p.running {
stopPlugin(name)
}
}
}
}
} else {
log.Fatal("error: plugin " + name + " not founded")
}
}
func StopPlugins() {
for k, _ := range plugins {
stopPlugin(k)
}
pluginsRunning = false
}
func HandleMessage(msg *tb.Message) {
for _, val := range plugins {
if val.enable {
val.plugin.HandleMessage(context.bot, msg.Text)
}
}
}
func StartPlugins() {
for k, val := range plugins {
if val.enable {
startPlugin(k)
}
}
pluginsRunning = true
}
func startPlugin(name string) {
if p, ok := plugins[name]; ok {
//p.mux.Lock()
if !p.running && p.enable {
p.running = true
plugins[name] = p
p.plugin.Load()
}
//p.mux.Unlock()
}
}
func stopPlugin(name string) {
if p, ok := plugins[name]; ok {
//p.mux.Lock()
if p.running {
p.running = false
plugins[name] = p
p.plugin.Unload()
}
//p.mux.Unlock()
}
}
func Exit() {
for _, p := range plugins {
if p.running {
p.plugin.Unload()
}
}
}

57
plugin_manager/tools.go Normal file
View File

@ -0,0 +1,57 @@
/**
* @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 Plugin interface {
Load()
HandleMessage(bot *tb.Bot, msg string)
Unload()
}
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, ok := symPlugin.(Plugin)
if !ok {
log.Fatal(file + ": unexpected type from module symbol")
return nil
}
return plugin
}

View File

@ -1,4 +1,4 @@
{
"token": "INSERT TOKEN HERE",
"token": "646721001:AAHuCB0uqW1u94GD_BGTUrz-ECaGwGJRm2E",
"history size": 10
}
}