-
Notifications
You must be signed in to change notification settings - Fork 4
/
bot.js
executable file
·480 lines (462 loc) · 14.3 KB
/
bot.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Let's invite people to the party !
var irc = require('irc'),
fs = require('fs'),
twitter = require('twitter'),
Sandbox = require('sandbox'),
http = require('http');
var util=require('util');
// vars
var linesBuffer = [],
bufferTimeout,
watchs = [],
twittLastDate = new Date(),
twittTimer,
votingTweets = [],
votingIds = 0;
// consts
const IRC_SRV = 'irc.freenode.net',
IRC_PORT = 8002,
BOT_NAME = 'Marionnette',
BOT_REAL_NAME = 'Robot FranceJS',
MAIN_CHANNEL = '#francejs',
ADMINS = ['nfroidure', 'naholyr', '_kud'],
BUFFER_TIMEOUT = 6, // seconds
BUFFER_SIZE = 10, // lines
LOG_DIR = 'logs', // rel to log path
LOG_NAME = 'irc', // log filename
TWITTER_TIMEOUT = 300, // seconds
IRC_EVENT_MSG = 1,
IRC_EVENT_JOIN = 2,
IRC_EVENT_PART = 4,
IRC_EVENT_TOPIC = 8,
IRC_EVENT_BOT = 16,
IRC_EVENT_QUIT = 32,
IRC_EVENT_KICK = 64,
IRC_EVENT_KILL = 128,
IRC_EVENT_NICK = 256,
IRC_EVENT_TWITT = 512,
IRC_DEST_SELECT = 0,
IRC_DEST_CHAN = 1,
IRC_DEST_NICK = 2,
IRC_DEST_SILENT = 4;
// Init twitter api
var twit = new twitter(JSON.parse(fs.readFileSync(__dirname
+ '/twitter-conf.json')));
// Listening for MAIN_CHANNEL twitts
function getTwitts() {
clearTimeout(twittTimer);
twit.search(MAIN_CHANNEL, function (data) {
var i;
if (data && data.statuses && data.statuses.length) {
// Throwing old tweets away
i = data.statuses.length - 1;
while (i >= 0 && Date.parse(data.statuses[i].created_at)
<= twittLastDate.getTime()) {
i--;
}
// Displaying left twitts
if (i >= 0) {
console.log(twittLastDate + " : Caught some tweets(" + (i + 1)
+ "), displaying!");
// Renew the last twitt date
twittLastDate = new Date(data.statuses[0].created_at);
console.log(twittLastDate + " : Updated the last twitt date!");
} else {
console.log(twittLastDate + " : No new tweet to display.");
}
while (i >= 0) {
if (0 !== data.statuses[i].text.indexOf('RT')||i--<-1) {
client.say(MAIN_CHANNEL,
logMessage(IRC_EVENT_TWITT | IRC_EVENT_BOT,
[BOT_NAME, 'Twitter.com' + MAIN_CHANNEL + ' - '
+ '@' + data.statuses[i].user.screen_name
.replace('<', '<').replace('>', '>')
.replace('"', '"').replace('&', '&')
+ ': ' + data.statuses[i--].text
.replace('<', '<').replace('>', '>')
.replace('"', '"').replace('&', '&')
]
)
);
}
}
} else {
console.log(twittLastDate + " : Couldn\'t get tweets!");
}
});
twittTimer=setTimeout(getTwitts, TWITTER_TIMEOUT * 1000);
}
// Write messages to the log when timeout is fired
function writeMessages() {
var curDate = new Date();
fs.appendFile(__dirname + '/' + LOG_DIR + '/' + LOG_NAME + '-'
+ curDate.getFullYear() + '-'
+ (('' + curDate.getMonth()).length < 2 ? '0' : '')
+ (curDate.getMonth() + 1) + '-'
+ (('' + curDate.getDate()).length < 2 ? '0' : '')
+ curDate.getDate() + '.log',
linesBuffer.join('\n') + '\n', function (err) {
console.log(err || curDate + " : Message buffer saved!");
});
linesBuffer.length = 0;
}
// Add message to buffer
function logMessage(type, fields) {
var message;
if (linesBuffer.length < BUFFER_SIZE && bufferTimeout)
clearTimeout(bufferTimeout);
if (fields.length !== 2)
throw RangeError('Not enougth fields sent');
// Saving message
message = fields[1];
// Escaping double quotes
fields.forEach(function (item, i) {
fields[i] = '"' + item.replace(/"/g, '\\"') + '"';
});
// Adding log type and date
fields.unshift(type, Date.now()); // unshift to keep CSV format extendable
// Pushing to the buffer
linesBuffer.push(fields.join(','));
bufferTimeout = setTimeout(writeMessages, BUFFER_TIMEOUT * 1000);
return message;
}
// Commands
function executeCommand(command, nick, origin) {
var messages, dest;
switch (command.split(' ')[0].toLowerCase()) {
case 'ls':
case 'help':
case 'commands':
dest = IRC_DEST_NICK;
messages = ['I understand the following commands :',
'- ls/commands : list commands',
'- hello/lo : kinda cool to say hello!',
'- watch <nickname> : tells you when <nickname> talk',
'- unwatch <nickname> : stops telling you when <nickname> talk',
'- dice <faces> <num> : Lets hazard comes',
'- eval <code> : Life is dangerous',
'- wannatweet <theTweet> : Launch a poll to tweet',
'- vote : List all tweets that await votes',
'- vote <voteId> ++|-- : Vote for a tweet',
'- seen <nickname> : last connection of <nickname> (not implemented)',
'- diffuse <message> : diffuse a message to each js chan (#parisjs,'
+' #francejs) (not implemented)',
'- log <n> <start> <date> : give the <n> messages nick <start> on <date>'
+' (not implemented)',
'- todo : adds items todo (not implemented)'
];
break;
case 'lo':
case 'hello':
case 'hi':
dest = IRC_DEST_SELECT;
messages = ['Hi! Nice to see you!'];
break;
case 'd':
case 'roll':
case 'dice':
dest = IRC_DEST_SELECT;
var args = command.split(' ');
if (args.length < 2) {
messages = ['Not enought args given for the d command.'];
break;
}
args[1] = parseInt(args[1]);
args[2] = parseInt(args[2]);
if (isNaN(args[1]) || args[1] < 2) {
messages = ['Invalid face count for d command (numFaces >= 2).'];
break;
}
if (args[2] && (isNaN(args[2]) || args[2] < 1 || args[2] > 11)) {
messages = ['Invalid dice count for d command (11 < numDices >= 2).'];
break;
}
var result = '';
for (var i = 0, j = (args[2] ? args[2] : 1); i < j; i++)
result += (result ? ' ' : '')
+ Math.round((Math.random() * (args[1] - 1)) + 1);
messages = [result];
break;
case 'say':
if (-1 === ADMINS.indexOf(nick)) {
dest = IRC_DEST_NICK;
messages = ['Not allowed to say that.'];
break;
}
dest = IRC_DEST_CHAN | IRC_DEST_SILENT;
messages = [command.split(' ').splice(1).join(' ')];
break;
case 'eval':
var s = new Sandbox();
s.run(command.split(' ').splice(1).join(' '), function (output) {
client.say(MAIN_CHANNEL, logMessage(
IRC_EVENT_MSG | IRC_EVENT_BOT, [BOT_NAME, nick + ': '
+ output.result.substr(0, 50)]));
});
messages = ['Running...'];
break;
case 'twittime':
dest = IRC_DEST_NICK;
if (-1 === ADMINS.indexOf(nick)) {
messages = ['Not allowed to do that.'];
break;
}
var args = command.split(' ');
if (args.length < 2) {
messages = ['Not enought args given for the twittime command.'];
break;
}
twittLastDate = new Date(parseInt(args[1])),
messages = ['You mastered the time, doc.'];
break;
case 'wannatweet':
dest = IRC_DEST_SELECT;
var tweet = command.split(' ').splice(1).join(' ');
if (tweet.length < 5) {
messages = ['No tweet or tweet too small.'];
break;
}
votingTweets.push({
'tweet': tweet,
'votes': 1,
'voters': [nick],
'id': (++votingIds)
});
messages = ['Tweet #' + votingIds + ' added send "vote ' + votingIds
+ ' ++|--" to give your opinion.'];
break;
case 'vote':
dest = IRC_DEST_SELECT;
var args = command.split(' ');
if (args.length < 2) {
messages = votingTweets.map(function (v) {
return '#' + v.id + ': ' + v.tweet + ' (votes: ' + v.votes + ').'
});
break;
}
args[1] = parseInt(args[1]);
if (isNaN(args[1]) || !votingTweets.some(function (votingTweet, index) {
if (votingTweet.id != args[1])
return false;
if ((!args[2]) || (args[2] != '--' && args[2] != '++')) {
messages = ['Bad arg #2, use --|++.'];
return true;
}
if (-1 !== votingTweet.voters.indexOf(nick)) {
messages = ['It seems you already voted for this tweet.'];
} else {
votingTweet.voters.push(nick);
votingTweet.votes += ('--' == args[2] ? -1 : 1);
messages = ['Voted ! Current vote sum is ' + votingTweet.votes + '.'];
if (votingTweet.votes <= -3) {
messages = ['Too many negative votes, tweet removed.'];
votingTweets.splice(index, 1);
} else if (votingTweet.votes >= 5) {
twit.updateStatus(votingTweet.tweet, function (data) {
console.log(JSON.stringify(data));
});
messages = ['So, let\'s tweet it guys ;).'];
votingTweets.splice(index, 1);
}
}
return true;
})) {
messages = ['Invalid voting tweet number (format: vote [tweetid] ++|--).'];
break;
}
break;
case 'tweet':
dest = IRC_DEST_NICK;
if (-1 === ADMINS.indexOf(nick)) {
messages = ['You aren\'t allowed to tweet.'];
break;
}
var tweet = command.split(' ').splice(1).join(' ');
if (tweet.length < 5) {
messages = ['No tweet or tweet too small.'];
break;
}
twit.updateStatus(tweet, function (data) {
console.log(JSON.stringify(data));
});
messages = ['Your tweet has been sent (' + tweet + ').'];
break;
case 'quote':
var req = http.request({
host: 'www.iheartquotes.com',
port: 80,
path: '/api/v1/random?source=esr+humorix_misc+humorix_stories+'
+'joel_on_software+macintosh+math+mav_flame+osp_rules+paul_graham'
+'+prog_style+subversion' + '&max_lines=4&max_characters=256'
+'&format=json',
method: 'GET'
}, function (res) {
var body = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function (chunk) {
var result = JSON.parse(body);
client.say(MAIN_CHANNEL, logMessage(
IRC_EVENT_MSG | IRC_EVENT_BOT, [BOT_NAME, result.quote]));
client.say(MAIN_CHANNEL, logMessage(
IRC_EVENT_MSG | IRC_EVENT_BOT, [BOT_NAME, 'Source: ' + result.link]));
});
});
req.on('error', function (e) {
console.log('Couldn\'t retrieve the fortune: ' + e.message);
});
req.write('');
req.end();
break;
case 'bitch':
case 'bastard':
case 'motherfucker':
case 'fuck':
case 'fucker':
case 'idiot':
case 'git':
dest = IRC_DEST_SELECT;
messages = [IRC_DEST_SELECT, 'Nice to meet you "' + command.split(' ')[0]
+ '", I\'m ' + BOT_NAME + ', waiting for commands!'];
break;
case 'watch':
dest = IRC_DEST_NICK;
var args = command.split(' ');
if (args.length < 2) {
messages = ['Not enought args given for the watch command.'];
break;
}
if (args[1] == BOT_NAME) {
messages = ['Bots have private life too.'];
break;
}
if (watchs[args[1]] && -1 !== watchs[args[1]].indexOf(nick)) {
messages = ['You\'re already watching ' + args[1] + '.'];
break;
}
(undefined !== watchs[args[1]] && watchs[args[1]].push(nick)
|| (watchs[args[1]] = [nick + '']));
messages = ['Now you\'re watching ' + args[1] + '.'];
break;
case 'unwatch':
dest = IRC_DEST_NICK;
var args = command.split(' ');
if (args.length < 2) {
messages = ['Not enought args given for the unwatch command'];
break;
}
var index = watchs[args[1]].indexOf(nick);
if (watchs[args[1]] && -1 !== index && watchs[args[1]].splice(index, 1)); {
messages = ['You unwatched ' + args[1] + '.'];
break;
}
messages = ['You wasn\'t watching ' + args[1] + '.'];
break;
case 'seen':
case 'diffuse':
case 'log':
case 'todo':
dest = IRC_DEST_SELECT;
messages = ['Not implemented, but feel free to:'
+' https://github.com/francejs/irc-bot'];
break;
default:
dest = IRC_DEST_NICK;
messages = ["You\'re talking to me ?? Try ls."];
break;
}
if (dest === IRC_DEST_SELECT)
dest |= origin;
(dest & IRC_DEST_CHAN) && messages.forEach(function (msg, i) {
client.say(MAIN_CHANNEL, logMessage(IRC_EVENT_MSG | IRC_EVENT_BOT,
[BOT_NAME, (i === 0 && !(dest & IRC_DEST_SILENT) ? nick + ': ' : '')
+ msg]));
});
(dest & IRC_DEST_NICK) && messages.forEach(function (msg) {
client.say(nick, msg);
});
}
// Starting IRC client
var client = new irc.Client(IRC_SRV, BOT_NAME, {
realName: BOT_REAL_NAME,
port: IRC_PORT,
autoRejoin: true,
autoConnect: false,
debug: true,
showErrors: true,
floodProtection: true,
floodProtectionDelay: 2000
});
// Connecting to IRC
client.connect(Infinity, function () {
client.join(MAIN_CHANNEL);
});
// Listening for messages
var botRegExp = new RegExp(BOT_NAME + '([ ,:]+)');
client.addListener('message' + MAIN_CHANNEL, function (nick, message) {
// Logging message
logMessage(IRC_EVENT_MSG, [nick, message]);
// Looking for a command to execute
if (-1 !== message.indexOf(BOT_NAME)) {
executeCommand(message.replace(botRegExp, ''), nick, IRC_DEST_CHAN);
}
// Telling watchers
(watchs[nick] || []).forEach(function (watcher, i) {
client.say(watcher, nick + ' said : ' + message);
});
});
client.addListener('pm', function (nick, message) {
executeCommand(message, nick, IRC_DEST_NICK);
});
// Listening for incoming people
client.addListener('join' + MAIN_CHANNEL, function (nick, message) {
if (-1 !== nick.indexOf(BOT_NAME)) {
logMessage(IRC_EVENT_JOIN | IRC_EVENT_BOT, [nick, nick + ' join the chan.']);
getTwitts();
client.say(MAIN_CHANNEL, logMessage(IRC_EVENT_MSG|IRC_EVENT_BOT,
[BOT_NAME,'Pouah! This chan is filled with humans.']));
} else {
logMessage(IRC_EVENT_JOIN, [nick, nick + ' join the chan.']);
// Enable this when someone connects for the first time only
//client.say(MAIN_CHANNEL, logMessage(IRC_EVENT_MSG|IRC_EVENT_BOT,
// [BOT_NAME,'Welcome '+nick+'. I obey to commands, not to humans.']));
}
});
// Listening for topic changes
client.addListener('topic', function (chan, topic, nick, message) {
nick && logMessage(IRC_EVENT_TOPIC, [nick, nick + ' change topic to "'
+ topic + '"']);
});
// Listening for leaving people
client.addListener('part' + MAIN_CHANNEL, function (nick, message) {
logMessage(IRC_EVENT_PART, [nick, nick + ' leave the chan.']);
});
client.addListener('quit', function (nick, reason, channels, message) {
if (-1 !== channels.indexOf(MAIN_CHANNEL))
logMessage(IRC_EVENT_QUIT, [nick, nick + ' leave the IRC (' + reason
+ ').']);
});
// Listening for killed people
client.addListener('kill', function (nick, reason, channels, message) {
if (-1 !== channels.indexOf(MAIN_CHANNEL))
logMessage(IRC_EVENT_KILL, [nick, nick + ' has been killed from IRC ('
+ reason + ').']);
});
// Listening for kicked people
client.addListener('kick' + MAIN_CHANNEL, function (nick, by, reason, message) {
logMessage(IRC_EVENT_KICK, [nick, nick + ' has been kicked by ' + by + ' ('
+ reason + ').']);
});
// Listening for nick changes
client.addListener('nick', function (oldNick, newNick, channels, message) {
if (-1 !== channels.indexOf(MAIN_CHANNEL))
logMessage(IRC_EVENT_NICK, [oldNick, oldNick + ' changed his nick for '
+ newNick + '.']);
});
// Shoud listen for disconnections to discard watchs
// or not, people will assume that watchs are disconnect safe
client.addListener('error', function (message) {
console.log('error: ', message);
});