forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.js
291 lines (255 loc) · 9.52 KB
/
plugins.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
"use strict";
// load all defined plugins
var logger = require('./logger');
var config = require('./config');
var constants = require('./constants');
var path = require('path');
var vm = require('vm');
var fs = require('fs');
var utils = require('./utils');
var util = require('util');
var plugin_paths = [path.join(__dirname, './plugins')];
if (process.env.HARAKA) { plugin_paths.unshift(path.join(process.env.HARAKA, 'plugins')); }
function Plugin(name) {
this.name = name;
this.timeout = config.get(name + '.timeout');
if (this.timeout === null) {
this.timeout = config.get('plugin_timeout') || 30;
}
else {
logger.logdebug("plugin " + name + " set timeout to: " + this.timeout + "s");
}
var full_paths = []
plugin_paths.forEach(function (pp) {
full_paths.push(path.resolve(pp, name) + '.js');
});
this.full_paths = full_paths;
this.config = config;
this.hooks = {};
};
Plugin.prototype.register_hook = function(hook_name, method_name) {
this.hooks[hook_name] = this.hooks[hook_name] || [];
this.hooks[hook_name].push(method_name);
logger.logdebug("registered hook " + hook_name + " to " + this.name + "." + method_name);
}
Plugin.prototype.register = function () {}; // noop
Plugin.prototype.inherits = function (parent_name) {
var parent_plugin = plugins._load_and_compile_plugin(parent_name);
for (var method in parent_plugin) {
if (!this[method]) {
this[method] = parent_plugin[method];
}
}
if (parent_plugin.register) {
parent_plugin.register.call(this);
}
this.base = parent_plugin;
}
// copy logger methods into Plugin:
for (var key in logger) {
if (key.match(/^log\w/)) {
// console.log("adding Plugin." + key + " method");
Plugin.prototype[key] = (function (key) {
return function () {
var args = [this];
for (var i=0, l=arguments.length; i<l; i++) {
args.push(arguments[i]);
}
logger[key].apply(logger, args);
}
})(key);
}
}
var plugins = exports;
plugins.Plugin = Plugin;
plugins.load_plugins = function () {
logger.loginfo("Loading plugins");
var plugin_list = config.get('plugins', 'list');
plugins.plugin_list = plugin_list.map(plugins.load_plugin);
logger.dump_logs(); // now logging plugins are loaded.
};
plugins.load_plugin = function(name) {
logger.loginfo("Loading plugin: " + name);
var plugin = plugins._load_and_compile_plugin(name);
if (plugin) {
plugins._register_plugin(plugin);
}
return plugin;
}
plugins._load_and_compile_plugin = function(name) {
var plugin = new Plugin(name);
var fp = plugin.full_paths,
rf, last_err;
for (var i=0, j=fp.length; i<j; i++) {
try {
rf = fs.readFileSync(fp[i]);
break;
}
catch (err) {
last_err = err;
continue;
}
}
if (!rf) {
if (config.get('smtp.ini').main.ignore_bad_plugins) {
logger.logcrit("Loading plugin " + name + " failed: " + last_err);
return;
}
throw "Loading plugin " + name + " failed: " + last_err;
}
var code = '"use strict";' + rf;
var sandbox = {
require: require,
__filename: fp[i],
__dirname: path.dirname(fp[i]),
exports: plugin,
setTimeout: setTimeout,
clearTimeout: clearTimeout,
setInterval: setInterval,
clearInterval: clearInterval,
process: process,
Buffer: Buffer,
Math: Math,
};
constants.import(sandbox);
try {
vm.runInNewContext(code, sandbox, name);
}
catch (err) {
logger.logcrit("Compiling plugin: " + name + " failed");
if (config.get('smtp.ini').main.ignore_bad_plugins) {
logger.logcrit("Loading plugin " + name + " failed: ", err.message
+ " - will skip this plugin and continue");
return;
}
throw err; // default is to re-throw and stop Haraka
}
return plugin;
}
plugins._register_plugin = function (plugin) {
plugin.register();
// register any hook_blah methods.
for (var method in plugin) {
var result;
if (result = method.match(/^hook_(\w+)\b/)) {
plugin.register_hook(result[1], method);
}
}
return plugin;
}
plugins.run_hooks = function (hook, object, params) {
if (hook != 'log')
object.logdebug("running " + hook + " hooks");
if (hook != 'deny' && hook != 'log' && object.hooks_to_run && object.hooks_to_run.length) {
throw new Error("We are already running hooks! Fatal error!");
}
if (hook === 'deny') {
// Save the hooks_to_run list so that we can run any remaining
// plugins on the previous hook once this hook is complete.
object.saved_hooks_to_run = object.hooks_to_run;
}
object.hooks_to_run = [];
for (var i = 0; i < plugins.plugin_list.length; i++) {
var plugin = plugins.plugin_list[i];
if (plugin && plugin.hooks[hook]) {
var j;
for (j = 0; j < plugin.hooks[hook].length; j++) {
var hook_code_name = plugin.hooks[hook][j];
object.hooks_to_run.push([plugin, hook_code_name]);
}
}
}
plugins.run_next_hook(hook, object, params);
};
plugins.run_next_hook = function(hook, object, params) {
var called_once = 0;
var timeout_id;
var item;
var callback = function(retval, msg) {
if (timeout_id) clearTimeout(timeout_id);
if (called_once) {
if (hook != 'log')
object.logerror(item[0].name + ' plugin ran callback multiple times - ignoring subsequent calls');
// Write a stack trace to the log to aid debugging
object.logerror((new Error).stack);
return;
}
called_once++;
if (!retval) retval = constants.cont;
// Log what is being run
if (item && hook !== 'log') {
var log = 'logdebug';
var is_not_cont = (retval !== constants.cont && logger.would_log(logger.LOGINFO));
if (is_not_cont) log = 'loginfo';
if (is_not_cont || logger.would_log(logger.LOGDEBUG)) {
object[log]([
'hook=' + hook,
'plugin=' + item[0].name,
'function=' + item[1],
'params="' + ((params) ? ((typeof params === 'string') ? params : params[0]) : '') + '"',
'retval=' + constants.translate(retval),
'msg="' + ((msg) ? msg : '') + '"',
].join(' '));
}
}
if (object.hooks_to_run.length == 0 ||
retval !== constants.cont)
{
var respond_method = hook + "_respond";
if (item && utils.in_array(retval, [constants.deny, constants.denysoft, constants.denydisconnect, constants.denysoftdisconnect])) {
object.deny_respond = function (deny_retval, deny_msg) {
switch(deny_retval) {
case constants.ok:
// Override rejection
object.loginfo('deny(soft?) overriden by deny hook' +
(deny_msg ? ': ' + deny_msg : ''));
// Restore hooks_to_run with saved copy so that
// any other plugins on this hook can also run.
if (object.saved_hooks_to_run.length > 0) {
object.hooks_to_run = object.saved_hooks_to_run;
plugins.run_next_hook(hook, object, params);
}
else {
object[respond_method](constants.cont, deny_msg);
}
break;
default:
object.saved_hooks_to_run = [];
object.hooks_to_run = [];
object[respond_method](retval, msg);
}
};
plugins.run_hooks('deny', object, [retval, msg, item[0].name, item[1], params, hook]);
}
else {
object.hooks_to_run = [];
object[respond_method](retval, msg, params);
}
}
else {
plugins.run_next_hook(hook, object, params);
}
}
if (!object.hooks_to_run.length) return callback();
// shift the next one off the stack and run it.
item = object.hooks_to_run.shift();
if (item[0].timeout && hook != 'log') {
timeout_id = setTimeout(function () {
object.logcrit("Plugin " + item[0].name +
" timed out on hook " + hook + " - make sure it calls the callback");
callback(constants.denysoft, "plugin timeout");
}, item[0].timeout * 1000);
}
if (hook != 'log')
object.logdebug("running " + hook + " hook in " + item[0].name + " plugin");
try {
object.current_hook = item;
item[0][ item[1] ].call(item[0], callback, object, params);
}
catch (err) {
if (hook != 'log') {
object.logcrit("Plugin " + item[0].name + " failed: " + (err.stack || err));
}
callback();
}
};