forked from git.klmp200.net/ALFRED
des trucs pas termine qui fonctionne plus ou moins
This commit is contained in:
parent
99a6cd2a14
commit
8301e1dfaa
7 changed files with 233 additions and 2 deletions
9
plugin_manager/context.go
Normal file
9
plugin_manager/context.go
Normal 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
117
plugin_manager/manager.go
Normal 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
57
plugin_manager/tools.go
Normal 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue