-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.js
76 lines (52 loc) · 1.51 KB
/
commands.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
var cmd = exports,
fs = require('fs'),
irc = require('./irc.js'),
log = require('./log.js'),
authd = {}; // People authentified
cmd.prefix = '.';
function tokenize(msg) {
var ret = msg.split(/\s/),
fst = ret.shift();
return (fst === cmd.prefix) ? ret : undefined;
}
cmd.load = function (dispatcherName, context) {
log.debug({ message: 'Loading dispatcher ' + dispatcherName + '.' });
var dispatcherFile = context.options.dispatcherDir +
'/' + dispatcherName + '.js',
absolutePath = require.resolve(dispatcherFile);
delete require.cache[absolutePath];
cmd[dispatcherName] = require(dispatcherFile);
}
cmd.unload = function (dispatcherName) {
log.debug({ message: 'Unloading dispatcher ' + dispatcherName + '.' });
return delete cmd[dispatcherName];
};
cmd.dispatch = function (context) {
var dispatcher = context.dispatcher,
tokens = tokenize(context.message),
name,
commandFunc;
if (tokens) {
name = tokens[0];
}
else {
return;
}
context.authd = authd;
context.cmd = cmd;
if (typeof cmd[dispatcher] === 'undefined') {
cmd.load(dispatcher, context);
}
commandFunc = cmd[dispatcher][name];
if (typeof commandFunc === 'undefined') {
log.debug({
message: 'Command "' + name +
'" not found on dispatcher ' + dispatcher
});
return;
}
if (commandFunc.restricted === false ||
typeof authd[context.hostmask] !== 'undefined') {
commandFunc(tokens, context);
}
};