This repository has been archived by the owner on Sep 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
server.ChatServerBase.js
executable file
·529 lines (471 loc) · 19.9 KB
/
server.ChatServerBase.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
/* jshint asi: true */
/* jshint esnext: true */
var crypto = require('crypto');
var tor = require('tor-exits');
var triplist = require("./data/trips.json");
var donatorlist = require("./data/donators.json");
// Keeps multiple connections for a client
function MetaClient() {
this.clients = [];
}
MetaClient.prototype.push = function(client) {
this.clients.push(client);
};
MetaClient.prototype.remove = function(client) {
this.clients.splice(this.clients.indexOf(client), 1);
};
MetaClient.prototype.send = function() {
var args = arguments;
this.clients.forEach(function(c) {
c.send.apply(c, args);
});
};
MetaClient.prototype.setClientConfigurationData = function(channel, nick, trip) {
this.channel = channel;
this.nick = nick;
this.trip = trip;
this.score = 0;
this.timeoutTime = 0;
};
function ChatServerBase() {
this._connectedClients = { '': 0 }; // map: channel -> (lowerCaseNick -> ChatClientBase)
this.bot = new(require('./bot.js'))(this);
this.bot.restrictCommandToChannels('ascii', ['ascii']);
}
module.exports = function() {
return new ChatServerBase();
};
ChatServerBase.prototype.initialize = function(config, version, AutoMod) {
var that = this;
that.AutoMod = AutoMod;
that.config = config;
that.version = version;
that.nodes = [];
tor.fetch(function(err, data) {
if (err) return console.error(err);
that.nodes = tor.parse(data);
});
};
ChatServerBase.prototype.getEventQueue = function() {
return this.eventQueue;
};
ChatServerBase.prototype.getChannelNames = function() {
var channelNames = [];
for (var i in this._connectedClients)
if (i !== '') channelNames.push(i);
return channelNames;
};
ChatServerBase.prototype.getClientsOfChannel = function(channel) {
var clientsOfChannel = this._connectedClients[channel];
if (clientsOfChannel !== void 0) return clientsOfChannel;
return { '': 0 }; // return empty list
};
ChatServerBase.prototype.getClientOfChannel = function(nick, channel) {
var clientsOfChannel = this._connectedClients[channel];
if (clientsOfChannel !== void 0)
for (var i in clientsOfChannel)
if (clientsOfChannel[i].nick == nick)
return clientsOfChannel[i];
return;
};
ChatServerBase.prototype.addClientToChannel = function(channel, client, nick, trip) {
var clientsOfChannel = this._connectedClients[channel];
if (clientsOfChannel === void 0) {
this._connectedClients[''] += 1; // increase channel count
clientsOfChannel = this._connectedClients[channel] = { '': 0 }; // '' keeps the count of users
}
var existingMetaClient = clientsOfChannel[nick.toLowerCase()];
if (existingMetaClient !== void 0) {
if (existingMetaClient.trip !== trip)
return false; // not allowed to log in
} else {
clientsOfChannel[''] += 1;
existingMetaClient = clientsOfChannel[nick.toLowerCase()] = new MetaClient();
existingMetaClient.setClientConfigurationData(channel, nick, trip); // everything is ok, set data
this.broadcast(client, { cmd: 'onlineAdd', nick: nick, trip: trip, donator: donatorlist.hasOwnProperty(trip) }, channel);
}
client.setClientConfigurationData(channel, nick, trip); // everything is ok, set data
existingMetaClient.push(client);
return true;
};
ChatServerBase.prototype.removeClientFromChannel = function(channel, client) {
var clientsOfChannel = this._connectedClients[channel];
if (clientsOfChannel === void 0) return; // empty - nothing to do
var metaClient = clientsOfChannel[client.nick.toLowerCase()];
if (metaClient !== void 0) {
metaClient.remove(client);
if (metaClient.clients.length <= 0) {
this.broadcast(client, { cmd: 'onlineRemove', nick: client.nick }, client.channel);
delete clientsOfChannel[client.nick.toLowerCase()];
clientsOfChannel[''] -= 1; // decrease channel count
if (clientsOfChannel[''] <= 0) {
this._connectedClients[''] -= 1; // decrease user count
delete this._connectedClients[channel];
}
}
}
};
ChatServerBase.prototype.onClose = function(client) {
this.removeClientFromChannel(client.channel, client);
};
ChatServerBase.prototype.onMessage = function(client, data) {
try {
if (this.AutoMod.frisk(client.getIpAddress(), 0)) { // probe for rate limit
client.send(client, { cmd: 'warn', errCode: 'E001', text: "Your IP is being rate-limited or blocked." });
return;
}
this.AutoMod.frisk(client.getIpAddress(), 1); // Penalize here, but don't do anything about it
if (data.length > 65536) return; // ignore large packets
var args;
if ((typeof data) === 'string' || data instanceof String)
args = JSON.parse(data);
else
args = data;
var cmd = args.cmd;
this.handleCommand(cmd, client, args);
} catch (e) {
console.warn(e.stack)
}
};
ChatServerBase.prototype.broadcast = function(causingClient, data, channel) {
var clientsOfChannel = this.getClientsOfChannel(channel);
for (var i in clientsOfChannel) {
if (i !== '')
clientsOfChannel[i].send(causingClient, data);
}
};
ChatServerBase.prototype.broadcastAll = function(causingClient, data) {
var channelNames = this.getChannelNames();
for (var i in channelNames) {
var clientsOfChannel = this.getClientsOfChannel(channelNames[i]);
for (var k in clientsOfChannel) {
if (k !== '')
clientsOfChannel[k].send(causingClient, data);
}
}
};
ChatServerBase.prototype.validateNickName = function(nick) {
return /^[a-zA-Z0-9_]{1,24}$/.test(nick); // Allow letters, numbers, and underscores
};
ChatServerBase.prototype.hashPassword = function(password) {
var sha = crypto.createHash('sha256');
sha.update(password + this.config.salt);
return sha.digest('base64').substr(0, 10);
};
ChatServerBase.prototype.generatePassword = function(nick) {
var gPass = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 21 + nick.length; i++)
gPass += possible.charAt(Math.floor(Math.random() * possible.length));
return gPass + "NEW";
};
ChatServerBase.prototype.handleCommand = function(command, client, args) {
var self = this;
// Commands usable by all users
switch (command) {
case 'ping':
client.send(null, { cmd: 'pong' });
return;
case 'verify':
if (self.config.allowTor)
return client.send(null, { cmd: 'verify', valid: (args.version == this.version) });
if (self.nodes) {
if (self.nodes.indexOf(client.getIpAddress()) == -1)
client.send(null, { cmd: 'verify', valid: (args.version == this.version) });
else {
console.log("Tor client detected")
client.send(null, { cmd: 'warn', text: "Connection could not be established" });
}
} else {
client.send(null, { cmd: 'warn', errCode: '666', text: "Something went wrong trying again." });
}
return;
case 'join':
var channel = String(args.channel).trim();
var nick = String(args.nick).trim();
var lowerCaseNick = nick.toLowerCase();
// dont allow invalid or empty passwords
if ((typeof args.pass) !== 'string' || args.pass === '' || args.pass == 'undefined')
args.pass = this.generatePassword(nick);
var trip = this.hashPassword(args.pass);
// if (this.AutoMod.frisk(client.getIpAddress(), 2) && !this.) {
// send(client, {cmd: 'warn', errCode: 'E002', text: "You are joining channels too fast. Wait a moment and try again."}, this)
// return
// }
if (client.nick) return; // Already joined
if (channel === "") return; // Must join a non-blank channel
// Process nickname
if (!this.validateNickName(nick)) {
client.send(null, {
cmd: 'warn',
errCode: 'E003',
text: "Nickname must consist of up to 24 letters, numbers, and underscores"
});
return;
}
if (lowerCaseNick === this.config.admin.toLowerCase()) {
if (args.pass !== this.config.password) {
client.send(null, { cmd: 'warn', errCode: 'E004', text: "Cannot impersonate the admin" });
return;
}
}
if (!this.addClientToChannel(channel, client, nick, trip)) {
client.send(null, { cmd: 'warn', errCode: 'E005', text: "Nickname taken" });
return;
}
// Set the online users for new user
var nicks = [];
var trips = [];
var clientsOfChannel = this.getClientsOfChannel(channel);
for (var i in clientsOfChannel) {
someClient = clientsOfChannel[i];
if (i === '' || someClient == client) continue;
nicks.push(someClient.nick);
trips.push(someClient.trip);
}
client.send(null, { cmd: 'onlineSet', nicks: nicks, trips: trips });
return;
case 'chat':
var text = String(args.text);
args.channel = String(client.channel);
if (!client.channel) return;
// strip newlines from beginning and end
text = text.replace(/^\s*\n|^\s+$|\n\s*$/g, '');
// replace 3+ newlines with just 2 newlines
text = text.replace(/\n{3,}/g, "\n\n");
if (!text) return;
if (self.AutoMod.isMuted(client.getIpAddress()))
return;
var score = text.length / 83 / 4;
if (self.AutoMod.frisk(client.getIpAddress(), score) && !client.admin) {
client.send(null, {
cmd: 'warn',
errCode: 'E006',
text: "You are sending too much text. Wait a moment and try again.\nPress the up arrow key to restore your last message."
});
return;
}
var data = {
cmd: 'chat',
nick: client.nick,
trip: client.trip,
text: text,
admin: self.AutoMod.isAdmin(client),
donator: donatorlist.hasOwnProperty(client.trip),
llama: (Math.floor(Math.random() * 20) == 0 || client.nick.toLowerCase() == "llama")
};
if (client.timeoutTime != 0) { //if this user hasn't recieved a chat timeout
var warningData = { cmd: 'chat', text: "You have to wait: " + client.timeoutTime + " seconds, before you can chat again.", nick: 'AutoMod' };
return client.send(null, warningData);
}
if (self.AutoMod && self.AutoMod.ScanMessage(data, client))
return;
if (typeof triplist[data.trip] != 'undefined')
data.trip = triplist[data.trip];
else
data.trip = data.trip.substr(0, 6);
// check for '/me' actions
if (text[0] == '/' && text.substr(0, 4) == '/me ')
data.cmd = 'action';
if (text[0] != '.') {
self.broadcast(client, data, client.channel);
}
if (text[0] == '!' || text[0] == '.') {
try {
self.bot.parseCmd(data, client);
} catch (e) {
if (text[0] == '.') {
data.text = e.toString();
self.broadcast(client, data, client.channel);
}
data.text = e.toString();
self.broadcast(client, data, client.channel);
}
}
return;
case 'whisper':
var text = String(args.text);
var nickToWhisper = String(args.target);
var friend = this.getClientsOfChannel(client.channel)[nickToWhisper.toLowerCase()];
if (!friend) {
client.send(null, { cmd: 'warn', errCode: 'E008', text: "Could not find user in channel" });
return
}
if (friend.nick === client.nick) return; // Ignore silently
client.send(null, {
cmd: 'whisper',
channel: channel,
target: friend.nick,
nick: "to " + friend.nick,
text: text,
whisper: true
});
friend.send(null, {
cmd: 'whisper',
channel: channel,
owner: client.nick,
nick: "from " + client.nick,
text: text,
whisper: true
});
return;
case 'invite':
var nickToInvite = String(args.nick);
if (!client.channel) return;
if (this.AutoMod.frisk(client.getIpAddress(), 2)) {
client.send(null, {
cmd: 'warn',
errCode: 'E007',
text: "You are sending invites too fast. Wait a moment before trying again."
});
return;
}
var friend = this.getClientsOfChannel(client.channel)[nickToInvite.toLowerCase()];
if (!friend) {
client.send(null, { cmd: 'warn', errCode: 'E008', text: "Could not find user in channel" });
return
}
if (friend.nicks === client.nick) return; // Ignore silently
var channel = Math.random().toString(36).substr(2, 8);
client.send(null, {
cmd: 'info',
infoCode: 'I001',
channel: channel,
text: "You invited " + friend.nick + " to ?" + channel
});
friend.send(null, {
cmd: 'info',
infoCode: 'I002',
channel: channel,
text: client.nick + " invited you to ?" + channel
});
return;
case 'stats':
var channelNames = this.getChannelNames();
var ips = {};
var numberOfIps = 0;
channelNames.forEach(function(channel) {
var clientsOfChannel = self.getClientsOfChannel(channel);
for (var i in clientsOfChannel) {
var someClient = clientsOfChannel[i];
if (ips[someClient.getIpAddress()] !== true) {
ips[someClient.getIpAddress()] = true;
numberOfIps += 1;
}
}
});
client.send(null, {
cmd: 'info',
infoCode: 'I003',
text: numberOfIps + " unique IPs in " + channelNames.length + " channels"
});
return;
}
// Commands usable by all mods
if (this.AutoMod.isMod(client)) {
switch (command) {
case 'kick':
var nick = String(args);
if (!client.channel) return;
var badClient = this.getClientsOfChannel(client.channel)[nick.toLowerCase()];
if (!badClient) {
client.send(null, { cmd: 'warn', errCode: 'E009', text: "Could not find " + nick });
return;
}
if (this.AutoMod.isMod(badClient)) {
client.send(null, { cmd: 'warn', errCode: 'E010', text: "Cannot kick moderator" });
return;
}
console.log(client.nick + " [" + client.trip + "] kicked " + nick + " in " + client.channel);
this.broadcast(client, {
cmd: 'info',
infoCode: 'I004',
nick: nick,
text: "Kicked " + nick
}, client.channel);
//Kick the client
badClient.clients.forEach(function(c) {
c.send(null, { cmd: 'close' });
self.AutoMod.kick(c.getIpAddress(), args.time);
console.log("IP: " + c.getIpAddress());
});
return;
case 'ban':
var nick = String(args);
if (!client.channel) return;
var badClient = this.getClientsOfChannel(client.channel)[nick.toLowerCase()];
if (!badClient) {
client.send(null, { cmd: 'warn', errCode: 'E009', text: "Could not find " + nick });
return;
}
if (this.AutoMod.isMod(badClient)) {
client.send(null, { cmd: 'warn', errCode: 'E010', text: "Cannot ban moderator" });
return;
}
console.log(client.nick + " [" + client.trip + "] banned " + nick + " in " + client.channel);
this.broadcast(client, {
cmd: 'info',
infoCode: 'I004',
nick: nick,
text: "Banned " + nick
}, client.channel);
//Ban the client
badClient.clients.forEach(function(c) {
c.send(null, { cmd: 'dataSet', bSet: true });
c.send(null, { cmd: 'close' });
self.AutoMod.ban(c.getIpAddress(), args.time);
});
return;
case 'mute':
var nick = String(args);
if (!client.channel) return;
var badClient = this.getClientsOfChannel(client.channel)[nick.toLowerCase()];
if (!badClient) {
client.send(null, { cmd: 'warn', errCode: 'E009', text: "Could not find " + nick });
return;
}
if (this.AutoMod.isMod(badClient)) {
client.send(null, { cmd: 'warn', errCode: 'E010', text: "Cannot mute moderator" });
return;
}
badClient.clients.forEach(function(c) {
c.send(null, { cmd: 'dataSet', mSet: true });
self.AutoMod.mute(c.getIpAddress(), args.time);
console.log("IP: " + c.getIpAddress());
});
console.log(client.nick + " [" + client.trip + "] muted " + nick + " in " + client.channel);
}
}
// Commands usable by all admins
if (this.AutoMod.isAdmin(client)) {
switch (command) {
case 'play':
var url = args.url.trim();
this.broadcast(client, { cmd: 'play', nick: client.nick, trip: client.trip, url: url }, client.channel);
return;
case 'listUsers':
var channelNames = this.getChannelNames();
var clientCount = 0;
var lines = [];
channelNames.sort();
channelNames.forEach(function(channel) {
var clientNicks = [];
var clientsOfChannel = self.getClientsOfChannel(channel);
for (var i in clientsOfChannel) {
if (i === '') continue;
clientNicks.push(clientsOfChannel[i].nick);
}
lines.push("?" + channel + " " + clientNicks.join(", "));
clientCount += clientNicks.length;
});
var text = clientCount + " users online:\n\n";
text += lines.join("\n");
client.send(null, { cmd: 'info', text: text });
return;
case 'broadcast':
var text = args.join(' ');
this.broadcastAll(client, { cmd: 'shout', infoCode: 'S001', text: "Server broadcast: " + text });
return;
}
}
};