-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
323 lines (264 loc) · 9.22 KB
/
plugin.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
const fs = require('fs');
const path = require('path');
const vm = require('vm');
const hooks = require('./hooks.js');
const constants = require('haraka-constants');
class CommandBag {
constructor (plugin) {
this.plugin = plugin;
this.register_hook = plugin.register_hook.bind(plugin);
}
inspect () {
return '[' + this.plugin.name + '] ';
}
get name () {
return this.plugin.name;
}
}
class Plugin {
constructor (manager, name) {
this.name = name;
this.manager = manager;
this.plugin = manager.plugin;
this.commands = new CommandBag(this);
this.hooks = {};
this.hasPackageJson = false;
this.plugin_path = '';
this.load_plugin();
}
_make_custom_require () {
const plugin = this;
let require_paths;
if (plugin.hasPackageJson) {
// external plugin, ./ references to local plugin dir
require_paths = [
path.resolve(path.dirname(this.plugin_path), 'node_modules'),
path.resolve(path.dirname(this.plugin_path)),
path.resolve('node_modules'),
path.resolve('.'),
path.resolve('..'),
];
} else {
// internal plugin, means that ./ references to HARAKA_ENV,
require_paths = [
path.resolve(path.dirname(this.plugin_path), 'node_modules'),
path.resolve('node_modules'),
path.resolve('.'),
path.resolve('..'),
];
}
return function (module) {
for (var i = 0; i < require_paths.length; i++) {
var test_path = path.resolve(require_paths[i], module);
if (fs.existsSync(test_path + '.js')) {
return require(test_path + '.js');
}
if (fs.existsSync(test_path)) {
return require(test_path);
}
if (fs.existsSync(path.join(test_path, 'package.json'))) {
return require(test_path);
}
}
if (plugin.hasPackageJson) {
try {
const mod = require(module);
constants.import(global);
global.server = plugin.plugin.server;
return mod;
} catch (err) {
plugin.plugin.logerror(err);
throw err;
}
}
if (module === './config') {
return plugin.plugin.config;
}
if (!/^\./.test(module)) {
return require(module);
}
if (fs.existsSync(path.join(__dirname, `${module}.js`)) ||
fs.existsSync(path.join(__dirname, module))) {
return require(module);
}
return require(path.join(path.dirname(plugin.plugin.plugin_path), module));
};
}
/*
Searches for the plugin in different paths
*/
find_plugin_path () {
var name = this.name;
if (/^haraka-plugin-/.test(name)) {
name = name.replace(/^haraka-plugin-/, '');
}
var paths = [
path.resolve('plugins', name + '.js'),
path.resolve('node_modules', 'haraka-plugin-' + name, 'package.json'),
path.resolve('..', 'haraka-plugin-' + name, 'package.json'),
];
for (var i = 0; i < paths.length; i++) {
var ppath = paths[i];
try {
if (fs.statSync(ppath)) {
if (path.basename(ppath) === 'package.json') {
this.hasPackageJson = true;
}
return ppath;
}
}
catch (ignore) {}
}
return null;
}
/*
Retrieves a plugin code
*/
get_code () {
if (this.hasPackageJson) {
let packageDir = path.dirname(this.plugin_path);
//return 'var _p = require("' + packageDir + '"); for (var k in _p) { exports[k] = _p[k] }';
return '"use strict"; ' + fs.readFileSync(packageDir + '/index.js');
}
return '"use strict"; ' + fs.readFileSync(this.plugin_path);
}
/*
Loads a plugin based on the initialized name
*/
load_plugin () {
this.plugin_path = this.find_plugin_path();
if (!this.plugin_path) {
this.plugin.logerror('[' + this.name + '] Unable to find plugin code - is the plugin installed?');
return;
}
var code = this.get_code();
var plugin = this;
const sandbox = {
require: this._make_custom_require(),
__filename: this.plugin_path,
__dirname: path.dirname(this.plugin_path),
exports: this.commands,
setTimeout: setTimeout,
clearTimeout: clearTimeout,
setInterval: setInterval,
clearInterval: clearInterval,
process: process,
Buffer: Buffer,
Math: Math,
console: console,
server: server,
setImmediate: setImmediate
};
if (this.hasPackageJson) {
delete sandbox.__filename;
}
constants.import(sandbox);
try {
vm.runInNewContext(code, sandbox, this.plugin_path);
} catch (err) {
console.log(err);
this.plugin.logerror('[' + this.name + '] Unable to execute plugin code: ' + err);
}
for (var name in Object.getPrototypeOf(this.plugin)) {
this.commands[name] = this.forward_log.bind(this, name);
}
this.commands.config = this.plugin.config;
this.commands.inherits = this.inherits.bind(this);
this.commands.haraka_require = this.plugin.haraka_require;
}
/*
Inherit function used in some plugins
*/
inherits (parent_name) {
this.forward_log('loginfo', 'Loading parent ' + parent_name);
const parent_plugin = new Plugin(this.manager, parent_name);
for (const method in parent_plugin.commands) {
if (!this.commands[method]) {
this.commands[method] = parent_plugin.commands[method];
}
}
if (parent_plugin.commands.register) {
parent_plugin.commands.register.call(this.commands);
}
// this.base[parent_name] = parent_plugin;
}
/*
Forwards logs to the real plugin
*/
forward_log (func, text) {
var prepend = [];
var args = Array.prototype.slice.call(arguments, 1)
if (!(arguments[1] instanceof CommandBag)) {
prepend.push(this.commands);
}
this.plugin[func].apply(this.plugin, prepend.concat(args));
}
/*
Registers the plugin with the plugin manager
*/
register () {
if ('register' in this.commands) {
this.commands.register.call(this.commands);
} else {
this.forward_log('logerror', this.commands, 'Unable to find register function - fine if you don\'t need one!');
}
// register any hook_blah methods.
for (const method in this.commands) {
const result = method.match(/^hook_(\w+)\b/);
if (result) {
this.commands.register_hook(result[1], method);
}
}
}
/*
Register a hook for this plugin
*/
register_hook (hook, callback, priority) {
if (hooks.indexOf(hook) === -1) {
this.plugin.logerror(this.commands, 'Unable to register hook ' + hook + ' - not available');
return;
}
if (typeof callback !== 'function') {
if (!(callback in this.commands)) {
this.plugin.logerror('Unable to register callback for hook ' + hook + ' - not a function');
return;
}
callback = this.commands[callback].bind(this.commands);
}
if (!(hook in this.hooks)) {
this.hooks[hook] = [];
}
this.hooks[hook].push(callback);
this.commands.loginfo(this.commands, 'Hook ' + hook + ' registered');
// this.manager.register_plugin_hook(this, hook, callback, priority);
}
/*
Runs a specific hook for this plugin
*/
run_hook (hook, next, connection, params) {
var run_hooks = function (idx, return_code, msg) {
if (return_code !== undefined) {
return next(return_code, msg);
}
if (idx < this.hooks[hook].length) {
var hook_callback = this.hooks[hook][idx];
this.commands.loginfo('Handling hook ' + hook);
// handle the plugin callback
hook_callback(run_hooks.bind(this, idx + 1), connection, params);
} else {
// fallback in case no hook says "OK"
next();
}
}.bind(this);
// if we got hooks registered call them
if (hook in this.hooks) {
return run_hooks(0);
}
// fallback in case we don't have this hook
next();
}
toString () {
return this.name;
}
}
module.exports = Plugin;