-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
301 lines (278 loc) · 8.7 KB
/
index.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
var irc = require('irc'), // The base irc-bot module.
fs = require('fs'), // Required to navigate file systems.
path = require('path'); // Used for fast path creation.
var config = require('./config');
var client = new irc.Client(config.network, config.handle, config.params);
// Some client additions
// We put the config inside the client for easy grabbing should it come up in a plugin
client.config = config;
// We add our own speaking function to whisper if the sender is whispering.
client.respond = function (message, content) {
if (message.to === config.handle) {
// Whispering.
client.say(message.from, content);
}
else {
//Speaking.
client.say(message.to, content);
}
};
var requires = {};
var functions = {
loadPlugins: function () {
plugins = [];
var startups = [];
// Include all of the plugins.
fs.readdirSync('./lib/plugins').forEach(function (name) {
var filename = path.resolve('./lib/plugins/' + name);
delete require.cache[filename];
if (config.disable.indexOf(name) < 0 ) {
var plugin = require('./lib/plugins/' + name);
if (!plugin.weight) {
plugin.weight = 0;
}
plugins.push(plugin);
if (plugin.functions) {
for (var func in plugin.functions) {
functions[func] = plugin.functions[func];
}
}
if (plugin.startup) {
plugin.startup.forEach(function(startfunc) {
startups.push(startfunc);
});
}
if (plugin.requires) {
requires[plugin.name] = {};
plugin.requires.forEach(function (required) {
if (required.name && required.file) {
fs.exists(required.file, function(exists) {
if (exists) {
requires[plugin.name][required.name] = require(required.file);
}
else if (required.type) {
var newfilecontent;
switch (required.type) {
case 'object':
newfilecontent = "{}";
break;
case 'array':
newfilecontent = "[]";
break;
}
fs.writeFile(required.file, newfilecontent, function (e) {
if (e) {
console.log(e);
}
});
requires[plugin.name][required.name] = JSON.parse(newfilecontent);
}
else {
// It's probably a node-module then.
requires[plugin.name][required.name] = require(required.file);
}
});
}
});
}
}
});
plugins.sort(function (a, b) {
return a.weight - b.weight;
});
// We wait until now to run the startups, so that they go in order.
startups.sort(function (a, b) {
return a.weight - b.weight;
});
if (startups.length > 0) {
startups.forEach(function(startfunc) {
if (startfunc.function) {
startfunc.function(client);
}
});
}
},
updateFile: function (file, data) {
var string = JSON.stringify(data);
fs.writeFile(file, string, function (e) {
if (e) {
console.log(e);
}
});
},
randInt: function (min, max) {
return Math.floor(Math.random() * (max - min + 1));
}
};
requires.functions = functions;
var builtins = [
{
name: 'reload',
help: [
{
usage: '!reload',
description: 'Reloads all definition files'
}
],
run: {
onmessage: function (client, message) {
var result = /^!reload$/.exec(message.content);
if (result) {
functions.loadPlugins();
client.say(message.from, 'Reloaded definition files.');
return {status:"success"};
}
return {status:"fail"};
}
}
},
{
name: 'help',
help: [
{
usage: '!help',
description: 'Lists all possible help topics.'
},
{
usage: '!help [command]',
description: 'Gives more information about a specific help topic.'
}
],
run: {
onmessage: function (client, message) {
var allbuiltinnames = builtins.map(function (elem) {
if (elem.hasOwnProperty('help')) {
return elem.name;
}
}).filter(function(value) {
return value !== false;
}).join(", ")
, allpluginnames = plugins.map(function (elem) {
if (elem.hasOwnProperty('help')) {
return elem.name;
}
else {
return false;
}
}).filter(function (value) {
return value !== false;
}).join(", ");
var helpfunctions;
var command;
var result = /^!help\s?(\S*)?$/.exec(message.content);
if (result) {
if (result[1]) {
command = result[1];
builtins.forEach(function (builtin) {
if (builtin.name === command && builtin.hasOwnProperty('help')) {
helpfunctions = builtin.help;
}
});
if (!helpfunctions) {
plugins.forEach(function (plugin) {
if ((plugin.name === command || (typeof(plugin.aliases) != 'undefined' && plugin.aliases.indexOf(command) > -1)) && plugin.hasOwnProperty('help')) {
helpfunctions = plugin.help;
}
});
}
if (helpfunctions) {
helpfunctions.forEach(function (helpfunction) {
client.say(message.from, 'Usage: ' + helpfunction.usage + ', ' + helpfunction.description);
});
}
else {
client.say(message.from, 'Sorry, function not found or has no help info.');
}
}
else {
client.say(message.from, 'Possible help topics: ' + allbuiltinnames + ', ' + allpluginnames);
}
return {status:"success"};
}
return {status:"fail"};
}
}
}
];
var checkCommand = function (command, event, from, to, content) {
var message = {
to: to,
from: from,
content: content
};
var result = {
status: 'fail'
};
if (command.run.hasOwnProperty(event)) {
result = command.run[event](client, message, requires);
}
return result;
};
var plugins = [];
functions.loadPlugins();
client.addListener('error', function (content) {
console.log('error: ', content);
});
// When a message is received, look through our functions for 'message' function
// and run it. Stops looking for for functions when one returns 'success'.
client.addListener('message', function (from, to, content) {
var builtin_found = false,
plugin_found = false;
console.log(from + ' said ' + content + ' to ' + to);
builtin_found = builtins.some(function (command) {
if (checkCommand(command, 'onmessage', from, to, content).status === 'success') {
return true;
}
return false;
});
if (!builtin_found) {
plugin_found = plugins.some(function (command) {
var result = checkCommand(command, 'onmessage', from, to, content);
switch (result.status) {
case 'fail':
break
case 'update':
if (result.hasOwnProperty('file') && result.hasOwnProperty('data')) {
functions.updateFile(result.file, result.data);
console.log(result.file + ' updated.');
} else {
console.log('Missing update file or data.');
}
case 'success':
return true;
}
return false;
});
}
});
// When a user joins the channel, look through all plugins and builtins for an
// onjoin function, and run any of them that exist. Note that all onjoin functions
// get run, unlike with onmessage, which stops at the first success.
client.addListener('join', function (channel, user, content) {
var builtin_found = false,
plugin_found = false;
console.log(user + ' joined ' + channel);
builtin_found = builtins.some(function (command) {
if (checkCommand(command, 'onjoin', user, channel, content).status === 'success') {
return true;
}
return false;
});
if (!builtin_found) {
plugin_found = plugins.some(function (command) {
var result = checkCommand(command, 'onjoin', user, channel, content);
switch (result.status) {
case 'fail':
break
case 'update':
if (result.hasOwnProperty('file') && result.hasOwnProperty('data')) {
functions.updateFile(result.file, result.data);
console.log(result.file + ' updated.');
}
case 'success':
return true;
}
return false;
});
}
});