2018-07-24 14:04:08 +02:00
|
|
|
/**
|
|
|
|
* @Author: KLIPFEL Arthur
|
|
|
|
* @Date: 2018-08-24 12:17:17
|
|
|
|
*/
|
|
|
|
package plugin_manager
|
|
|
|
|
|
|
|
import (
|
|
|
|
tb "gopkg.in/tucnak/telebot.v2"
|
|
|
|
"log"
|
2018-07-25 14:12:52 +02:00
|
|
|
"path/filepath"
|
2018-07-25 11:23:11 +02:00
|
|
|
"strings"
|
2018-07-24 14:04:08 +02:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PluginCtrl struct {
|
|
|
|
plugin Plugin
|
|
|
|
mux sync.Mutex
|
|
|
|
running bool
|
|
|
|
enable bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var pluginDir string
|
|
|
|
var pluginsRunning bool
|
|
|
|
var plugins map[string]PluginCtrl
|
2018-07-25 12:59:22 +02:00
|
|
|
var context *tb.Bot
|
2018-07-24 14:04:08 +02:00
|
|
|
|
|
|
|
func Init(_pluginDir string, bot *tb.Bot) {
|
|
|
|
pluginDir = _pluginDir
|
|
|
|
pluginsRunning = false
|
|
|
|
plugins = make(map[string]PluginCtrl)
|
2018-07-25 12:59:22 +02:00
|
|
|
context = bot
|
2018-07-24 14:04:08 +02:00
|
|
|
for _, fileName := range GetSoFiles(pluginDir) {
|
|
|
|
var p PluginCtrl
|
2018-07-25 14:12:52 +02:00
|
|
|
p.plugin = LoadSoFile(filepath.Join(pluginDir, fileName))
|
2018-07-24 14:04:08 +02:00
|
|
|
if p.plugin != nil {
|
|
|
|
p.running = false
|
|
|
|
p.enable = true
|
|
|
|
plugins[fileName[:len(fileName)-3]] = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-25 11:23:11 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:04:08 +02:00
|
|
|
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 {
|
2018-07-25 11:23:11 +02:00
|
|
|
if val.enable && val.running {
|
|
|
|
if strings.HasPrefix(msg.Text, "/") {
|
|
|
|
split := strings.Split(msg.Text, " ")
|
|
|
|
split[0] = split[0][1:]
|
|
|
|
if Contains(split[0], ExecGetCommands(val.plugin)) {
|
2018-07-25 12:59:22 +02:00
|
|
|
ExecHandleCommand(val.plugin, context, msg, split[0], split[1:])
|
2018-07-25 11:23:11 +02:00
|
|
|
}
|
|
|
|
} else {
|
2018-07-25 12:59:22 +02:00
|
|
|
ExecHandleMessage(val.plugin, context, msg)
|
2018-07-25 11:23:11 +02:00
|
|
|
}
|
2018-07-24 14:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2018-07-25 11:23:11 +02:00
|
|
|
p.running = ExecLoad(p.plugin)
|
2018-07-24 14:04:08 +02:00
|
|
|
plugins[name] = p
|
|
|
|
}
|
|
|
|
//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
|
2018-07-25 11:23:11 +02:00
|
|
|
ExecUnload(p.plugin)
|
2018-07-24 14:04:08 +02:00
|
|
|
}
|
|
|
|
//p.mux.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Exit() {
|
|
|
|
for _, p := range plugins {
|
|
|
|
if p.running {
|
2018-07-25 11:23:11 +02:00
|
|
|
ExecUnload(p.plugin)
|
2018-07-24 14:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|