2016-12-11 16:06:17 +00:00
|
|
|
/* Copyright Azareal 2016 - 2017 */
|
|
|
|
package main
|
2017-01-01 15:45:43 +00:00
|
|
|
import "log"
|
2016-12-11 16:06:17 +00:00
|
|
|
|
2017-01-05 14:41:14 +00:00
|
|
|
var plugins map[string]*Plugin = make(map[string]*Plugin)
|
|
|
|
var hooks map[string][]func(interface{})interface{} = make(map[string][]func(interface{})interface{})
|
2016-12-13 02:14:14 +00:00
|
|
|
var vhooks map[string]func(...interface{})interface{} = make(map[string]func(...interface{})interface{})
|
2016-12-11 16:06:17 +00:00
|
|
|
|
|
|
|
type Plugin struct
|
|
|
|
{
|
|
|
|
UName string
|
|
|
|
Name string
|
|
|
|
Author string
|
|
|
|
URL string
|
|
|
|
Settings string
|
|
|
|
Active bool
|
2016-12-16 10:37:42 +00:00
|
|
|
Tag string
|
2016-12-11 16:06:17 +00:00
|
|
|
Type string
|
|
|
|
Init func()
|
2016-12-16 10:37:42 +00:00
|
|
|
Activate func()error
|
2016-12-13 02:14:14 +00:00
|
|
|
Deactivate func()
|
2017-01-05 14:41:14 +00:00
|
|
|
|
|
|
|
Hooks map[string]int
|
2016-12-11 16:06:17 +00:00
|
|
|
}
|
|
|
|
|
2017-01-05 14:41:14 +00:00
|
|
|
func NewPlugin(uname string, name string, author string, url string, settings string, tag string, ptype string, init func(), activate func()error, deactivate func()) *Plugin {
|
|
|
|
return &Plugin{
|
|
|
|
UName: uname,
|
|
|
|
Name: name,
|
|
|
|
Author: author,
|
|
|
|
URL: url,
|
|
|
|
Settings: settings,
|
|
|
|
Tag: tag,
|
|
|
|
Type: ptype,
|
|
|
|
Init: init,
|
|
|
|
Activate: activate,
|
|
|
|
Deactivate: deactivate,
|
|
|
|
|
|
|
|
/*
|
|
|
|
The Active field should never be altered by a plugin. It's used internally by the software to determine whether an admin has enabled a plugin or not and whether to run it. This will be overwritten by the user's preference.
|
|
|
|
*/
|
|
|
|
Active: false,
|
|
|
|
Hooks: make(map[string]int),
|
2017-01-01 15:45:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-05 14:41:14 +00:00
|
|
|
func (plugin *Plugin) AddHook(name string, handler interface{}) {
|
2016-12-18 12:56:06 +00:00
|
|
|
switch h := handler.(type) {
|
|
|
|
case func(interface{})interface{}:
|
2017-01-05 14:41:14 +00:00
|
|
|
if len(hooks[name]) == 0 {
|
|
|
|
var hookSlice []func(interface{})interface{}
|
|
|
|
hookSlice = append(hookSlice, h)
|
|
|
|
hooks[name] = hookSlice
|
|
|
|
} else {
|
|
|
|
hooks[name] = append(hooks[name], h)
|
|
|
|
}
|
|
|
|
plugin.Hooks[name] = len(hooks[name])
|
2016-12-18 12:56:06 +00:00
|
|
|
case func(...interface{}) interface{}:
|
|
|
|
vhooks[name] = h
|
2017-01-05 14:41:14 +00:00
|
|
|
plugin.Hooks[name] = 0
|
2016-12-18 12:56:06 +00:00
|
|
|
default:
|
|
|
|
panic("I don't recognise this kind of handler!") // Should this be an error for the plugin instead of a panic()?
|
|
|
|
}
|
2016-12-11 16:06:17 +00:00
|
|
|
}
|
|
|
|
|
2017-01-05 14:41:14 +00:00
|
|
|
func (plugin *Plugin) RemoveHook(name string, handler interface{}) {
|
|
|
|
switch handler.(type) {
|
|
|
|
case func(interface{})interface{}:
|
|
|
|
key := plugin.Hooks[name]
|
|
|
|
hook := hooks[name]
|
2017-04-13 15:01:30 +00:00
|
|
|
if len(hook) == 1 {
|
|
|
|
hook = []func(interface{})interface{}{}
|
|
|
|
} else {
|
|
|
|
hook = append(hook[:key], hook[key + 1:]...)
|
|
|
|
}
|
2017-01-05 14:41:14 +00:00
|
|
|
hooks[name] = hook
|
|
|
|
case func(...interface{}) interface{}:
|
|
|
|
delete(vhooks, name)
|
|
|
|
default:
|
|
|
|
panic("I don't recognise this kind of handler!") // Should this be an error for the plugin instead of a panic()?
|
|
|
|
}
|
|
|
|
delete(plugin.Hooks, name)
|
2016-12-13 02:14:14 +00:00
|
|
|
}
|
|
|
|
|
2017-01-17 07:55:46 +00:00
|
|
|
var plugins_inited bool = false
|
2017-01-05 14:41:14 +00:00
|
|
|
func init_plugins() {
|
|
|
|
for name, body := range plugins {
|
|
|
|
log.Print("Added plugin " + name)
|
|
|
|
if body.Active {
|
|
|
|
log.Print("Initialised plugin " + name)
|
|
|
|
plugins[name].Init()
|
|
|
|
}
|
|
|
|
}
|
2017-01-17 07:55:46 +00:00
|
|
|
plugins_inited = true
|
2016-12-13 02:14:14 +00:00
|
|
|
}
|
|
|
|
|
2017-01-05 14:41:14 +00:00
|
|
|
func run_hook(name string, data interface{}) interface{} {
|
|
|
|
for _, hook := range hooks[name] {
|
|
|
|
data = hook(data)
|
|
|
|
}
|
|
|
|
return data
|
2016-12-16 10:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func run_vhook(name string, data ...interface{}) interface{} {
|
|
|
|
return vhooks[name](data...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func run_vhook_noreturn(name string, data ...interface{}) {
|
|
|
|
_ = vhooks[name](data...)
|
2016-12-11 16:06:17 +00:00
|
|
|
}
|