This repository has been archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbot.js
560 lines (495 loc) · 17.7 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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
// Requires
var irc = require('irc'),
fs = require('fs'),
settings = require('./configuration.js'),
util = require('util');
for (var item in settings) {
util.log(item + ': ' + settings[item]);
}
var ircServer = settings.host,
nick = settings.nick,
options = {
channels: settings.channels.split(','),
autoRejoin: settings.autoRejoin,
port: settings.port,
secure: settings.secure,
autoConnect: false,
floodProtection: false
},
client = new irc.Client(ircServer, nick, options),
testDay = {
active: false,
channel: settings.channels.split(',')[0],
admins: settings.admins.split(','),
helpers: settings.helpers.split(','),
start: new Date(),
end: new Date(2000),
etherpad: "",
topic: "",
topic_backup: "",
advertisement: {
'channels': settings.adChannels.split(','),
'message': settings.adMessage}
},
timerID = 0,
optOut = [],
metrics = {
topic: "",
etherpad: "",
start: new Date(2000),
end: new Date(2000),
activeUsers: {},
hourUTC: {},
firebotBugs: [],
optOutTotal: 0
},
help = { ":help" : "This is Help! :)",
":bug" : "Learn how to report a bug",
":qmo" : "Learn about Quality at Mozilla",
":sumo" : "Learn about Support at Mozilla",
":join" : "Learn about getting involved with Mozilla QA",
":etherpad" : "View the Test Day etherpad",
":helpers" : "View Test Day helpers, and request help with :helpers request",
":schedule" : "View the Test Day schedule",
":optout" : "Opt out from Test Day data collection for your nick",
":optin" : "Opt in (default) to Test Day data collection for your nick"
},
adminhelp = { ":adminhelp" : ":adminhelp: This is Admin Help! :)",
":admin" : ":admin <add|remove> <nickname>: add or remove as a Test Day admin",
":helper": ":helper <add|remove> <nickname>: add or remove as a Test Day helper",
":next" : ":next <start as YYYY-MM-DDThh:mmZ> <end as YYYY-MM-DDThh:mmZ> <etherpad> <topic>: set next Test Day",
":stats" : ":stats: display Test Day stats",
":stop" : ":stop: stop Test Day early"
},
helperhelp = { ":advertise" : "Advertise the Test Day in other appropriate channels."
};
function resetData() {
testDay.admins = settings.admins.split(',');
testDay.helpers = settings.helpers.split(',');
metrics = {
topic: testDay.topic,
etherpad: testDay.etherpad,
start: new Date(testDay.start),
end: new Date(testDay.end),
activeUsers: {},
hourUTC: {},
firebotBugs:[],
optOutTotal: 0
};
saveData("metrics", JSON.stringify(metrics));
}
function updateTestDayData() {
if (testDay.end < Date.now()) {
testDay.active = false;
client.send('TOPIC', testDay.channel, testDay.topic_backup);
client.say(testDay.channel, "The " + testDay.topic + " testday " +
"has ended. Thank you all for your participation!");
if (timerID !== 0) {
clearTimeout(timerID);
timerID = 0;
}
} else {
testDay.active = true;
timerID = setTimeout(updateTestDayData, testDay.end - Date.now());
// if starting a new test day, not restarting
if (testDay.start > metrics.start) {
resetData();
client.send('TOPIC', testDay.channel, "Welcome to the QA channel. " +
"Today we are testing " + testDay.topic + ". " +
"Please read " + testDay.etherpad + " for more information " +
"and ask any questions you have in this channel.");
client.say(testDay.channel, "The " + testDay.topic + " testday starts "+
"now. For details, see " + testDay.etherpad);
}
}
saveData("testDay", JSON.stringify(testDay));
}
client.connect(function () {
client.say("NickServ", "IDENTIFY " + settings.password);
});
restoreTestDayData();
client.addListener('topic', function (aChannel, aChannelTopic, aNick) {
if (!testDay.active && (aChannel === testDay.channel)) {
// save a non-Test Day topic to restore after Test Day
testDay.topic_backup = aChannelTopic;
}
});
client.addListener('message', function(from, to, message) {
var intro;
if (to === nick) { // private message to bot
to = from;
}
if (message.search('[!:]help\\b') >= 0) {
client.say(to, "Hello, " + from + "! I’ve sent you a private message " +
"with more help.");
for (var item in help) {
client.say(from, item + " : " + help[item]);
}
if (testDay.helpers.indexOf(from) >= 0 || testDay.admins.indexOf(from) >= 0) {
for (item in helperhelp) {
client.say(from, item + " : " + helperhelp[item]);
}
}
if (testDay.admins.indexOf(from) >= 0) {
for (item in adminhelp) {
client.say(from, adminhelp[item]);
}
}
}
if (message.search('[!:]bug') >= 0) {
client.say(to, "You can find details on how to raise a bug at https://developer.mozilla.org/en/Bug_writing_guidelines");
}
if (message.search('[!:]sumo') >= 0) {
client.say(to, "SUMO is short for http://support.mozilla.org, the official, community-powered support website for Mozilla Firefox");
}
if (message.search('[!:]qmo') >= 0) {
client.say(to, "QMO is short for http://quality.mozilla.org, the official destination for everything related with Mozilla QA");
}
if (message.search('[!:]join') >= 0) {
client.say(from, "Mozilla QA is a diverse, open community of people " +
"pushing the open web forward by ensuring Mozilla produces " +
"the best technology possible. See " +
"https://wiki.mozilla.org/QA " +
"to find out more about getting involved.");
}
if (message.search('[!:]etherpad') >= 0) {
// bot just started, nothing's happened, nothing's scheduled
if (testDay.start > testDay.end) {
intro = "No Test Day has been scheduled.";
} else {
// default to past Test Day
intro = "No Test Day is currently scheduled. Last Test Day’s etherpad: ";
}
// if today is a Test Day
if (testDay.active) {
intro = "Today’s etherpad is ";
// else if a future Test Day is scheduled
} else if (testDay.start > Date.now()) {
intro = "Next Test Day’s etherpad is ";
}
client.say(to, intro + testDay.etherpad);
}
if (message.search('[!:]helpers') === 0) {
var command = message.split(" ");
if (testDay.active) {
intro = "Today's helpers: ";
switch (command[1]) {
case 'request':
intro = "Help request sent to ";
testDay.helpers.forEach(function (helper) {
client.say(helper, from + " could use some help!");
});
default:
client.say(to, intro + testDay.helpers.join([separator = ', ']));
}
} else {
client.say(to, "There's no Test Day in progress.");
}
}
if (message.search('[!:]schedule') >= 0) {
var scheduleTimes = "";
// bot just started, nothing's happened, nothing's scheduled
if (testDay.start > testDay.end) {
intro = "No Test Day has been scheduled.";
} else {
// default to past Test Day
intro = "No Test Day is currently scheduled. Last";
scheduleTimes = " Test Day: " + testDay.start.toUTCString() + " till " +
testDay.end.toUTCString();
}
// if today is a Test Day
if (testDay.active) {
intro = "This";
// else if a future Test Day is scheduled
} else if (testDay.start > Date.now()) {
intro = "Next";
}
client.say(to, intro + scheduleTimes);
}
if (message.search('[!:]optout') === 0) {
if (optOut.indexOf(from) === -1) {
optOut.push(from);
metrics.optOutTotal += 1;
if (from in metrics.activeUsers) {
delete metrics.activeUsers[from];
}
saveData("optOut", JSON.stringify(optOut));
}
client.say(from, "You’ve opted out of Test Day data collection " +
"for your nick " + from + ". You can opt in again using " +
"the command :optin.");
}
if (message.search('[!:]optin') === 0) {
if (optOut.indexOf(from) >= 0) {
optOut.splice(optOut.indexOf(from), 1);
// on Test Days, add to metrics to avoid second(?) opt out notice
if (testDay.active) {
metrics.activeUsers[from] = 0;
}
saveData("optOut", JSON.stringify(optOut));
}
client.say(from, "You’re opted in to Test Day data collection " +
"for your nick " + from + ".");
}
if (testDay.active) {
if (from === 'firebot') {
var matches = /^(https:\/\/bugzil.la\/|Bug )(\d+)/i.exec(message);
if (matches) {
metrics.firebotBugs.push(matches[2]);
}
}
// if from is not on the opt out list
if (optOut.indexOf(from) === -1) {
if (from in metrics.activeUsers) {
metrics.activeUsers[from] += 1;
} else {
client.say(from, "Welcome to today’s Test Day, " + from + "!");
client.say(from, "To opt out of data collection, use the command :optout.");
metrics.activeUsers[from] = 1;
}
}
var nowHour = new Date().getUTCHours().toString();
if (nowHour in metrics.hourUTC) {
metrics.hourUTC[nowHour] += 1;
} else {
metrics.hourUTC[nowHour] = 1;
}
saveData("metrics", JSON.stringify(metrics));
}
});
client.addListener('pm', function(from, message) { // private messages to bot
var command = message.split(" ");
if (!((command[0] in adminhelp) || (command[0] in helperhelp))) {
// not a privileged command
return;
}
if (!((testDay.admins.indexOf(from) >= 0) || (testDay.helpers.indexOf(from) >= 0))) {
client.say(from, "Sorry! " + from + " is not a Test Day admin or helper.");
return;
}
client.whois(from, function(whoisinfo) {
if (!(whoisinfo.accountinfo && whoisinfo.accountinfo.search('is logged in as') >= 0)) {
client.say(from, "Sorry! You're not logged in with a registered nick.");
return;
}
var cmdLen = command.length;
// :advertise is the only helper command; run it without further check
if (command[0] === ":advertise") {
client.say(from, 'Sending "' + testDay.advertisement.message +
'" to ' + testDay.advertisement.channels.join(", "));
testDay.advertisement.channels.forEach(function (aChannel){
client.join(aChannel, function() {
client.say(aChannel, testDay.advertisement.message);
client.part(aChannel);
});
});
return;
}
// other privileged commands are admin-only; return if not from admin
if (!(testDay.admins.indexOf(from) >= 0)) {
return;
}
// admin commands
switch (command[0]) {
case ":adminhelp":
for (var item in adminhelp) {
client.say(from, adminhelp[item]);
}
break;
case ":admin":
if (cmdLen != 3) {
client.say(from, "Need some help? " + adminhelp[command[0]]);
return;
}
switch (command[1]) {
case "add":
if (testDay.admins.indexOf(command[2]) === -1) {
testDay.admins.push(command[2]);
}
break;
case "remove":
var index = testDay.admins.indexOf(command[2]);
if (index >= 0) {
testDay.admins.splice(index, 1);
}
break;
default:
client.say(from, "Need some help? " + adminhelp[command[0]]);
}
client.say(from, 'Test Day admins are ' + testDay.admins.join(", "));
break;
case ":helper":
if (cmdLen != 3) {
client.say(from, "Need some help? " + adminhelp[command[0]]);
return;
}
switch (command[1]) {
case "add":
if (testDay.helpers.indexOf(command[2]) === -1) {
testDay.helpers.push(command[2]);
}
break;
case "remove":
var index = testDay.helpers.indexOf(command[2]);
if (index >= 0) {
testDay.helpers.splice(index, 1);
}
break;
default:
client.say(from, "Need some help? " + adminhelp[command[0]]);
}
client.say(from, 'Test Day helpers are ' + testDay.helpers.join(", "));
break;
case ":stats":
var stats = new Stats();
stats.generateStats(metrics, from);
break;
case ":stop":
if (testDay.active) {
testDay.end = new Date();
metrics.end = testDay.end.toUTCString();
saveData("metrics", JSON.stringify(metrics));
updateTestDayData();
client.say(from, "Test Day stopped.");
} else {
client.say(from, "No Test Day is in progress.");
}
break;
case ":next":
if (testDay.active) {
client.say(from, "Test Day in progress and scheduled to end " + testDay.end);
} else {
if (cmdLen >= 5) {
var startTime = new Date(command[1]);
var endTime = new Date(command[2]);
var dateErrors = [];
if (endTime < startTime) {
dateErrors.push("Start time is set after end time.");
}
if (startTime < Date.now()) {
dateErrors.push("Start time is set in the past.");
}
// if the start and end dates appear valid, set the test day date
if (dateErrors.length == 0) {
if (timerID !== 0) {
clearTimeout(timerID);
}
testDay.start = startTime;
testDay.end = endTime;
testDay.etherpad = command[3];
testDay.topic = command.slice(4, cmdLen).join(" ");
timerID = setTimeout(updateTestDayData, testDay.start - Date.now());
client.say(from, "Next Test Day's start is " + testDay.start.toUTCString());
client.say(from, "Next Test Day's end is " + testDay.end.toUTCString());
client.say(from, "Next Test Day's etherpad is " + testDay.etherpad);
client.say(from, "Next Test Day's topic is " + testDay.topic);
}
else {
client.say(from, "Please use valid dates:\n" + dateErrors.join("\n"));
}
} else {
client.say(from, "Need some help? " + adminhelp[command[0]]);
}
}
break;
default:
client.say(from, "Oops! I don't really know how to " + message + ".");
}
saveData("testDay", JSON.stringify(testDay), function (error) {
var message = "Saving testDay data was unsuccessful. Please repeat the command!";
message += "\nThe error was: " + error;
client.say(from, message);
});
});
});
client.addListener('error', function(message) {
console.error('ERROR: %s: %s', message.command, message.args.join(' '));
});
var Stats = function() {};
Stats.prototype.generateStats = function(metrcs, from) {
var keys = Object.keys(metrcs);
var what = Object.prototype.toString;
var report ="";
var t = 0;
for (var i = 0; i < keys.length; i++) {
if (what.call(metrcs[keys[i]]).search('Array') > 0) {
report = report + keys[i] + ": " + metrcs[keys[i]].join(", ") + "\n";
} else {
if (keys[i] == "activeUsers") {
var speakers = Object.keys(metrcs.activeUsers).sort(function (a, b) {
return metrcs.activeUsers[b] - metrcs.activeUsers[a];
});
var speakersTotal = speakers.length;
report = report + "The following " + speakersTotal + " people were active in the channel: ";
for (t = 0; t < speakersTotal; t++) {
var sep = (t === speakersTotal - 1) ? "\n" : ", ";
report = report + speakers[t] + ": " + metrcs.activeUsers[speakers[t]] + sep;
}
} else if (keys[i] == "hourUTC") {
report = report + "The following hours (UTC) were active in the channel: ";
speakers = Object.keys(metrcs.hourUTC);
speakersTotal = speakers.length;
for (t = 0; t < speakersTotal; t++) {
sep = (t === speakersTotal - 1) ? "\n" : ", ";
report = report + speakers[t] + ": " + metrcs.hourUTC[speakers[t]] + sep;
}
} else {
report = report + keys[i] + ": " + metrcs[keys[i]] + "\n";
}
}
}
client.say(from, report);
};
function restoreTestDayData() {
var data = readData("optOut");
if (data) {
optOut = data;
}
data = readData("testDay");
if (data) {
testDay = data;
// Date objects don't survive JSON stringify/parse
testDay.start = new Date(testDay.start);
testDay.end = new Date(testDay.end);
}
data = readData("metrics");
if (data) {
metrics = data;
// Date objects don't survive JSON stringify/parse
metrics.start = new Date(metrics.start);
metrics.end = new Date(metrics.end);
}
if (testDay.start > Date.now()) {
// if a future Test Day has been scheduled
timerID = setTimeout(updateTestDayData, testDay.start - Date.now());
} else if (testDay.end > Date.now()) {
// else if Test Day has not ended
updateTestDayData();
}
}
function saveData(datastore, data, callback) {
var filename = "./data/" + datastore + ".json";
if (!fs.existsSync("./data")) {
fs.mkdirSync("./data", "0750");
}
fs.writeFile(filename, data, function(err){
if (err) {
console.error("Error writing " + filename);
if (typeof callback == "function") {
try {
callback(err);
} catch (e) {
// Nothing to do
}
}
}
});
}
function readData(datastore) {
var filename = "./data/" + datastore + ".json";
var data;
if (fs.existsSync(filename)) {
data = fs.readFileSync(filename, 'utf8');
return JSON.parse(data);
}
}