-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugins.go
159 lines (138 loc) · 3.74 KB
/
plugins.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"errors"
"io/ioutil"
"os"
"path"
"plugin"
"github.com/zechenturm/yahas/item"
"github.com/zechenturm/yahas/logging"
"github.com/zechenturm/yahas/yahasplugin"
"github.com/gorilla/mux"
)
var plugins map[string]*yahasplugin.Plugin
var yPlugins map[string]*yPlugin
var pManager pluginManager
type pluginManager struct {
}
func (pm *pluginManager) Load(name string) error {
if _, ok := plugins[name]; ok {
return errors.New("Plugin alreeady loaded")
}
return loadPlugin(name + ".so")
}
func (pm *pluginManager) Unload(name string) error {
plug, ok := yPlugins[name]
if !ok {
return errors.New("Plugin not loaded")
}
coreLogger.DebugLn("unlading " + name)
*(plug.router) = mux.Router{}
mainRouter.PathPrefix("/" + name).Subrouter()
pl := plugins[name]
delete(yPlugins, name)
delete(plugins, name)
return (*pl).DeInit()
}
func (pm *pluginManager) Map() *map[string]*yahasplugin.Plugin {
return &plugins
}
func checkPermission(plugin string, name string, defaultAllow bool) bool {
perm := false
permMap, ok := coreconf.Permissions[plugin]
if ok {
perm, ok = permMap[name]
}
if defaultAllow && !ok {
return true
}
return perm && ok
}
type yPlugin struct {
Name string
router *mux.Router
}
func (yp *yPlugin) RequestRouter() (*mux.Router, error) {
if !checkPermission(yp.Name, "router", true) {
return nil, errors.New("Permission denied")
}
if yp.router == nil {
yp.router = mainRouter.PathPrefix("/" + yp.Name).Subrouter()
}
return yp.router, nil
}
func (yp *yPlugin) Items() (*item.NamespaceMap, error) {
if !checkPermission(yp.Name, "items", true) {
return nil, errors.New("Permission denied")
}
return &Items, nil
}
func (yp *yPlugin) RequestPlugins() (yahasplugin.Manager, error) {
if !checkPermission(yp.Name, "plugins", false) {
return nil, errors.New("Permission denied")
}
return &pManager, nil
}
func (yp *yPlugin) BindingManager() (item.BindingManager, error) {
if !checkPermission(yp.Name, "bindings", false) {
return nil, errors.New("Permission denied")
}
return loader.BindingManager, nil
}
func loadPlugins(ignore []string) error {
plugins = make(map[string]*yahasplugin.Plugin)
yPlugins = make(map[string]*yPlugin)
files, err := ioutil.ReadDir("./plugins")
if err != nil {
return err
}
coreLogger.DebugLn("plugin ignore list:", ignore)
shouldIgnore := func(name string) bool {
for _, elem := range ignore {
if name == elem {
return true
}
}
return false
}
for _, file := range files {
baseName := file.Name()[:len(file.Name())-len(".so")]
if path.Ext(file.Name()) == ".so" && !shouldIgnore(baseName) {
err := loadPlugin(file.Name())
if err != nil {
coreLogger.ErrorLn("Error loading plugin", file.Name(), ":", err)
}
}
}
coreLogger.DebugLn("plugins:", plugins)
coreLogger.DebugLn("yplugins:", yPlugins)
return nil
}
func loadPlugin(name string) error {
coreLogger.InfoLn("loading plugin", name)
var ypi yahasplugin.Plugin
p, err := plugin.Open("./plugins/" + name)
if err != nil {
return err
}
sb, err := p.Lookup("Plugin")
if err != nil {
return err
}
ypi, ok := sb.(yahasplugin.Plugin)
if !ok {
return errors.New("loaded symbol of wrong type, yahasplugin.Plugin interface not implemented fully?")
}
yp := yPlugin{Name: name[:len(name)-3]}
plugins[yp.Name] = &ypi
yPlugins[yp.Name] = &yp
coreLogger.InfoLn("loaded plugin", yp.Name)
configFile, err := os.Open("./config/plugins/" + yp.Name + ".json")
if err != nil {
coreLogger.DebugLn("Could not open config file for "+yp.Name+":", err)
} else {
coreLogger.DebugLn("Opened config file for", name)
}
coreLogger.DebugLn("plugin permissions: ", coreconf.Permissions[name])
return ypi.Init(&yp, logging.New(yp.Name, logging.StrToLvl(coreconf.Loglevels.Plugins[yp.Name])), configFile)
}