-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_manager.js
122 lines (97 loc) · 3.63 KB
/
plugin_manager.js
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
const hooks = require('./hooks.js');
const Plugin = require('./plugin.js');
const vm = require('vm');
class PluginManager {
constructor (plugin) {
this.plugin = plugin;
this.plugins = [];
this.all_plugins = {};
}
register () {
for (var i = 0; i < hooks.length; i++) {
this.plugin.register_hook(hooks[i], 'hook', 0);
}
this.plugin.config.get('plugin_manager.yaml', 'yaml', () => {
this.plugin.loginfo('Reloading Config');
this.load_config();
});
this.load_config();
}
load_config () {
var cfg = this.plugin.config.get('plugin_manager.yaml')
if (!('plugins' in cfg)) {
return this.plugin.logerror('No config found!');
}
// reset plugins before repopulating config
this.plugins = [];
for (var name in cfg.plugins) {
var code = '"use strict"; exports.check = ' + cfg.plugins[name].check;
var context = { console: { log: this.plugin.loginfo.bind(this.plugin) }, exports: { } };
try {
vm.runInNewContext(code, context);
} catch (err) {
this.plugin.logerror('Unable to load queue ' + name + ': ' + err);
continue;
}
this.plugins.push({
name: name,
check: context.exports.check,
plugins: this.load_plugins(cfg.plugins[name].plugins)
});
}
}
load_plugins (plugins) {
var loaded_plugins = [];
for (var i = 0; i < plugins.length; i++) {
var name = plugins[i];
if (!(name in this.all_plugins)) {
this.all_plugins[name] = new Plugin(this, name);
this.all_plugins[name].register();
}
loaded_plugins.push(this.all_plugins[name]);
}
return loaded_plugins;
}
handle_hook (hook, next, connection, params) {
this.plugin.logdebug('Handling hook');
/*
Handles a single plugin collection,
and calls the corresponding plugin callbacks
*/
var run_hooks = function (run_plugins, plugins, idx, return_code, msg) {
if (return_code !== undefined) {
return run_plugins(return_code, msg);
}
if (idx < plugins.length) {
var plugin = plugins[idx];
this.plugin.logdebug('running ' + hook + ' plugin ' + plugin.name);
plugin.run_hook(hook, run_hooks.bind(this, run_plugins, plugins, idx + 1), connection, params);
} else {
run_plugins();
}
}.bind(this);
/*
Handles a each plugin collection
*/
var run_plugin_collection = function (idx, return_code, msg) {
if (return_code !== undefined) {
return next(return_code, msg);
}
if (idx < this.plugins.length) {
var plugins = this.plugins[idx];
if (plugins.check(connection)) {
this.plugin.logdebug('running ' + hook + ' plugin collection ' + plugins.name);
run_hooks(run_plugin_collection.bind(this, idx + 1), plugins.plugins, 0);
} else {
this.plugin.logdebug('skipping ' + hook + ' plugin collection ' + plugins.name);
run_plugin_collection(idx + 1);
}
} else {
next();
}
}.bind(this);
// run through each plugin collection
run_plugin_collection(0);
}
}
module.exports = PluginManager;