-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain.js
306 lines (277 loc) · 10.6 KB
/
main.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
// (c) 2011 Richard Carter
// This code is licensed under the MIT license; see LICENSE.txt for details.
// https://github.com/Ricket/nodebot
require('./config.js');
var util = require('util'),
net = require('net'),
fs = require('fs'),
vm = require('vm'),
repl = require('repl'),
_ = require('lodash');
function uncacheModules() {
// Clear the module cache
var key;
for (key in require.cache) {
delete require.cache[key];
}
}
var irc = global.nodebot = (function () {
var buffer, ignoredb, listeners, socket;
socket = new net.Socket();
socket.setNoDelay(true);
socket.setEncoding('utf8');
buffer = {
b: new Buffer(4096),
size: 0
};
listeners = [];
ignoredb = require('./lib/listdb').getDB('ignore');
var pingServer = _.debounce(function () {
irc.ping();
}, nodebot_prefs.ping_interval || 58000);
function send(data) {
if (!data || data.length == 0) {
console.error("ERROR tried to send no data");
} else if (data.length > 510) {
console.error("ERROR tried to send data > 510 chars in length: " + data);
} else {
socket.write(data + '\r\n', 'utf8', function () {
var sensitiveMatch = require('./regexFactory').password().exec(data);
if (sensitiveMatch) {
console.log("-> " + sensitiveMatch[1] + "***");
} else {
console.log("-> " + data);
}
});
pingServer();
}
}
socket.setTimeout(240 * 1000, function () {
// If the connection is closed, this will fail to send and the 'error'
// and 'close' events will trigger.
send('VERSION');
});
socket.on('close', function () {
process.exit();
});
socket.on('data', function (data) {
var newlineIdx;
data = data.replace('\r', '');
while ((newlineIdx = data.indexOf('\n')) > -1) {
if (buffer.size > 0) {
data = buffer.b.toString('utf8', 0, buffer.size) + data;
newlineIdx += buffer.size;
buffer.size = 0;
}
handle(data.substr(0, newlineIdx));
data = data.slice(newlineIdx + 1);
}
if (data.length > 0) {
buffer.b.write(data, buffer.size, 'utf8');
buffer.size += data.length;
}
});
function handle(data) {
var dest, i, user, replyTo, from;
console.log("<- " + data);
user = (/^:([^!]+)!/i).exec(data);
if (user) {
user = user[1];
if (ignoredb.hasValue(user, true)) {
return;
}
}
replyTo = null;
from = null;
if (data.indexOf('PRIVMSG') > -1) {
dest = (/^:([^!]+)!.*PRIVMSG ([^ ]+) /i).exec(data);
if (dest) {
if (dest[2].toUpperCase() == nodebot_prefs.nickname.toUpperCase()) {
replyTo = from = dest[1];
} else {
replyTo = dest[2];
from = dest[1];
}
}
}
var match, regex, listenerRequestStop;
for (i = 0; i < listeners.length; i++) {
match = listeners[i][0].exec(data);
if (match) {
try {
// TODO move data to the end since it is least commonly needed
// and replyTo to the front, since it is always needed
listenerRequestStop = listeners[i][1](match, data, replyTo, from);
} catch (err) {
console.log("Caught error in script " + listeners[i][4] + ": " + err);
}
if (listeners[i][2] /* once */) {
listeners.splice(i, 1);
i--;
}
if (listenerRequestStop) {
break;
}
}
}
pingServer();
}
function sanitize(data) {
if (!data) {
return data;
}
/* Note:
* 0x00 (null character) is invalid
* 0x01 signals a CTCP message, which we shouldn't ever need to do
* 0x02 is bold in mIRC (and thus other GUI clients)
* 0x03 precedes a color code in mIRC (and thus other GUI clients)
* 0x04 thru 0x19 are invalid control codes, except for:
* 0x16 is "reverse" (swaps fg and bg colors) in mIRC
*/
return data.replace(/\n/g, "\\n").replace(/\r/g, "\\r")
.replace(/[^\x02-\x03|\x16|\x20-\x7e]/g, "");
}
return {
/* The following function gives full power to scripts;
* you may want to no-op this function for security reasons, if you
* don't vet your scripts carefully.
*/
raw: function(stuff) {
send(stuff);
},
sanitize: function (data) {
return sanitize(data);
},
connect: function (host, port, nickname, username, realname, serverpass) {
port = port || 6667;
socket.connect(port, host, function () {
if (serverpass) {
send('PASS ' + sanitize(serverpass));
}
send('NICK ' + sanitize(nickname));
send('USER ' + sanitize(username) + ' localhost * ' + sanitize(realname));
});
},
loadScripts: function () {
var i, k, script, scripts;
socket.pause();
uncacheModules();
listeners = [];
scripts = require('./scripts');
for (i = 0; i < scripts.length; i++) {
console.log("Loading script " + scripts[i] + "...");
script = fs.readFileSync('scripts/' + scripts[i] + '.js');
if (script) {
var scriptName = scripts[i];
var sandbox = {
irc: irc,
nodebot_prefs: nodebot_prefs,
console: console,
setTimeout: setTimeout,
clearTimeout: clearTimeout,
setInterval: setInterval,
clearInterval: clearInterval,
vm: vm,
fs: fs,
process: process,
require: require,
util: util,
Buffer: Buffer,
_: require('lodash'),
regexFactory: require('./regexFactory'),
listen: function (dataRegex, callback, once, prefixed) {
if (!_.isRegExp(dataRegex)) {
console.err("Error in script " + scripts[i] + ": first parameter to listen is not a RegExp object. Use regexFactory.");
return;
}
once = !!once;
if (typeof prefixed === "undefined" || prefixed === null) {
prefixed = true;
}
listeners.push([dataRegex, callback, once, prefixed, scriptName]);
},
listen_admin: function (dataRegex, callback, once, prefixed) {
sandbox.listen(dataRegex, function(match, data, replyTo, from) {
if (require('./lib/admins').is(from)) {
callback(match, data, replyTo, from);
}
}, once, prefixed);
}
};
try {
vm.runInNewContext(script, sandbox, scripts[i]);
} catch (err) {
console.log("Error in script " + scripts[i] + ": " + err);
for (j = 0; j < listeners.length; j++) {
if (listeners[j][3] == scriptName) {
listeners.splice(j, 1);
j--;
}
}
}
} else {
console.log("Error loading script " + scripts[i] + ": readFileSync returned empty/null/undefined");
}
}
try {
socket.resume();
} catch (err) {}
console.log("Scripts loaded.");
},
/* IRC COMMANDS: */
ping: function (server) {
send("PING " + (server || nodebot_prefs.server));
},
pong: function (server) {
send("PONG " + (server || nodebot_prefs.server));
},
join: function (channel, key) {
var cmd = "JOIN :" + sanitize(channel);
if (key) {
cmd += " " + sanitize(key);
}
send(cmd);
},
part: function (channel) {
send("PART :" + sanitize(channel));
},
privmsg: function (user, message, sanitizeMessage) {
if (user && message) {
user = sanitize(user); //avoid sanitizing these more than once
if (sanitizeMessage !== false) {
message = sanitize(message);
}
var privmsg = "PRIVMSG " + user + " :";
var max = 510 - privmsg.length;
var maxmessages = 3;
while (message && (maxmessages--) > 0) {
send(privmsg + message.slice(0, max));
message = message.slice(max);
}
}
},
action: function (channel, action) {
if (channel && action) {
send("PRIVMSG " + sanitize(channel) + " :\x01ACTION " + sanitize(action) + "\x01");
}
},
/* ADDITIONAL GLOBAL IRC FUNCTIONALITY */
ignore: function (user) {
ignoredb.add(user);
},
unignore: function (user) {
ignoredb.remove(user, true);
},
chatignorelist: function (channel) {
irc.chatmsg(channel, "Ignore list: " + sanitize(ignoredb.getAll().join(",")));
}
}
})(); // end of object 'irc'
process.on('uncaughtException', function (err) {
console.log("caught error in script: " + err);
});
irc.loadScripts();
irc.connect(nodebot_prefs.server, nodebot_prefs.port, nodebot_prefs.nickname,
nodebot_prefs.username, nodebot_prefs.realname,
nodebot_prefs.serverpass);
repl.start({ prompt: '> ', ignoreUndefined: true });