This repository has been archived by the owner on Jun 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
342 lines (288 loc) · 13.6 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
(function () {
var mongojs = require('mongojs'),
request = require('request'),
cheerio = require('cheerio'),
irc = require('irc'),
dateCutOff = new Date('Dec 10, 2014').getTime(),
uri = '', // Mongodb connection URL
db = mongojs.connect(uri, ['trusted_users', 'alerted_revisions']),
trustStatus = {},
authorList = [],
revisionCount,
backgroundCheckCount = 0,
revisions = {},
channels = ['#sentinel', '#mdn'],
bot,
mdnRoot = 'http://developer.mozilla.org',
spamTriggerWords = ['watch', 'stream', 'streaming', 'season', 'online', 'movie',
'episode', '2014', 'free', 'full', 'hd', 'premiere'];
console.log('Sentinel is standing watch.');
function checkRevisions(isManuallyInvoked) {
revisionCount = 0;
backgroundCheckCount = 0;
isManuallyInvoked = isManuallyInvoked ? isManuallyInvoked : false;
request('https://developer.mozilla.org/en-US/dashboards/revisions', function (error, response, html) {
if (error) {
console.error('Failed to fetch revisions dashboard.');
return;
}
$ = cheerio.load(html);
$('.dashboard-row').each(function (i, element) {
var revisionId = $(this).data('revision-id');
var author = $(this).children().last().text();
var revisionTitle = $(this).children().eq(1).children().first().text();
var pageLink = $(this).data('view-url');
var thisRevision = {
revisionId: revisionId,
link: mdnRoot + pageLink,
revisionTitle: revisionTitle
};
if (revisions[author]) {
revisions[author].push(thisRevision);
} else {
revisions[author] = [thisRevision];
}
if (authorList.indexOf(author) === -1) {
authorList.push(author);
}
revisionCount++;
if (revisionCount == 50) {
backgroundCheckAuthors(isManuallyInvoked);
}
});
});
}
function backgroundCheckAuthors(isManuallyInvoked) {
authorList.forEach(function (author) {
isAuthorTrusted(author, function (err, status) {
backgroundCheckCount++;
if (!err) {
trustStatus[author] = status;
if (status) {
addAsTrustedUser(author);
}
}
if (backgroundCheckCount == authorList.length) {
processRevisions();
if (isManuallyInvoked) { // isManuallyInvoked = channel name
bot.say(isManuallyInvoked, 'Check completed.');
}
}
});
});
}
function processRevisions() {
for (var user in trustStatus) {
if (!trustStatus[user]) {
alertHumans(user, revisions[user]);
}
}
}
function isAuthorTrusted(username, callback) {
if (trustStatus[username]) {
callback(null, trustStatus[username]);
return;
}
db.trusted_users.find({"username": username}, function (err, records) {
if (!err && records.length > 0) {
callback(null, true);
return;
}
try {
var profileURL = 'https://developer.mozilla.org/en-US/profiles/' + username;
request(profileURL, function (error, response, html) {
if (error) {
console.error('Failed to fetch user profile of ', username);
return;
}
var $ = cheerio.load(html);
var joinDate = new Date($('.memberSince').text().replace('Member since ', '')).getTime();
if (joinDate < dateCutOff) {
// User is old enough to be genuine
callback(null, true);
} else {
callback(null, false);
return;
}
});
} catch (e) {
console.error(e);
callback(null, false); // not trusted by age
}
});
}
function addAsTrustedUser(username, callback) {
callback = typeof callback === 'function' ? callback : function () {};
db.trusted_users.find({"username": username}, function (err, records) {
if (err) {
callback(err, false);
return;
}
// Already exists
if (records.length > 0) {
callback(null, true, 'already existing');
return;
}
db.trusted_users.insert({"username": username});
callback(null, true);
});
}
function markAsAlerted(revisionId, callback) {
callback = typeof callback === 'function' ? callback : function () {};
db.alerted_revisions.find({"revisionId": revisionId}, function (err, records) {
if (err) {
callback(err, false);
return;
}
if (records.length > 0) {
callback(null, true);
return;
}
db.alerted_revisions.insert({"revisionId": revisionId});
callback(null, true);
});
}
function isRevisionAlreadyAlertedAbout(revisionId, callback) {
db.alerted_revisions.find({"revisionId": revisionId}, function (err, records) {
if (err) {
callback(err, false);
return;
}
if (records.length > 0) {
callback(null, true);
} else {
callback(null, false);
}
});
}
function hasAdminAccess(nick) {
var adminNicknames = ['sheppy', 'teoli', 'alispivak', 'fscholz', 'jms', 'hoosteno', 'jsx',
'wbamberg', 'dcamp', 'HBloomer', 'jezdez', 'mars', 'openjck', 'groovecoder'];
return adminNicknames.indexOf(nick) !== -1;
}
function alertHumans(user, edits) {
var isRedFlag = false; // Red flag true for those edits with a high chance of being spam
var revisionsToAlertAbout = [];
var pluralify = edits.length > 1 ? 's' : '',
editedPageLinks = '';
edits.forEach(function (edit, i) {
isRevisionAlreadyAlertedAbout(edit.revisionId, function (err, isAlreadyAlerted) {
if (!isAlreadyAlerted) {
editedPageLinks += edit.link + "\n";
revisionsToAlertAbout.push(edit);
spamTriggerWords.forEach(function (keyword) {
if (edit.title.toLowerCase().indexOf(keyword) !== -1) {
isRedFlag = true;
}
});
markAsAlerted(edit.revisionId);
}
if (i == edits.length - 1) {
if (editedPageLinks) {
if (isRedFlag) {
bot.say(channels[1], 'Suspicious edit'+ pluralify +' from user '+ user +'');
bot.say(channels[1], editedPageLinks);
} else {
bot.say(channels[0], 'Red alert edit'+ pluralify +' from user '+ user +'');
bot.say(channels[0], editedPageLinks);
}
}
}
});
});
}
function sentinelStart() {
bot = new irc.Client('irc.mozilla.org', 'sentinel', {
channels: channels,
realName: 'Sentinel Bot'
});
bot.addListener('message', function (sender, to, text, message) {
var tokens = text.toLowerCase().split(' ');
if (tokens[0] && tokens[0] == 'sentinelx:') {
if (tokens[1]) {
var command = tokens[1];
switch (command) {
case 'trust': {
if (tokens[2]) {
var author = tokens[2];
if (!hasAdminAccess(sender)) {
bot.say(to, 'Sorry! You are not authorized to use this spell :(');
return;
}
addAsTrustedUser(author, function (err, added, alreadyTrusted) {
if (err || !added) {
bot.say(to, 'Oops! Error adding author to trusted list.');
} else if (alreadyTrusted) {
bot.say(to, author + ' is a sweet angel and is already trusted.');
} else {
bot.say(to, 'Yay! ' + author + ' will be trusted from now on.');
}
});
} else {
bot.say(to, 'Invalid message format. Try "sentinel: help');
}
break;
}
case 'checknow': {
if (!hasAdminAccess(sender)) {
bot.say(to, 'Sorry! You are not authorized to use this spell :(');
return;
}
bot.say(to, 'Checking revision list for possible spam...');
checkRevisions(to);
break;
}
case 'help': {
var response;
if (!tokens[2]) { // No topic mentioned for help, so show general help response
response = "---------------------------------------------------------------------\n";
response += "Sentinel is on the lookout for spam edits on MDN\n\n";
response += "You can run Sentinel commands by typing sentinel: command\n";
response += "For more info on a particular command, type sentinel: help <command>\n";
response += "Following are the available botzilla commands\n \n";
response += " trust Adds an MDN username to the list of trusted users.\n";
response += " checknow Manually invoke the periodic checking\n";
response += " help Display this help message.\n";
response += "---------------------------------------------------------------------";
} else {
// topic for help is specified
switch (tokens[2]) {
case 'trust': {
response = "---------------------------------------------------------------------\n";
response += "trust adds an MDN username to the list of trusted users\n \n";
response += "usage: sentinel: trust <username>\n";
response += "If a username is added to the trusted list, future edits from that \n";
response += "particular user won't trigger any alerts. Only a few people are allowed to\n";
response += "run the trust command now. If you need this privilege, make a PR \n";
response += "or ping jsx\n";
response += "---------------------------------------------------------------------";
break;
}
case 'checknow': {
response = "---------------------------------------------------------------------\n";
response += "checknow immediately invokes the spam check\n \n";
response += "usage: sentinel: checknow\n";
response += "Only a few people are allowed to run the trust command now.\n";
response += "If you need this privilege, make a PR or ping jsx\n";
response += "---------------------------------------------------------------------";
break;
}
}
}
bot.say(to, response);
break;
}
default: {
bot.say(to, 'Invalid command. Type "sentinel: help" for list of available commands.');
}
}
}
}
});
}
sentinelStart();
// Set the bot to check revisions every x minutes
setInterval(function () {
checkRevisions();
}, 10 * 60 * 1000);
})();