/
chat.js
627 lines (451 loc) · 18.3 KB
/
chat.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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*jslint regexp: true */
(function () {
'use strict';
/*
* WGlads.com | Powerfull light-weight chat system ^___^
* Copyright (c) Yuri Borunov, 2015
*/
var fs = require('fs'),
io = require('socket.io'),
wrap = require('wordwrap'),
memcache = require('memcache'),
redis = require('redis'),
notify = require('./notification.js'),
chatSockets,
mclient,
mclient_reconnect_id,
rclient,
secret_key = 'abcd',
users = {},
users_id = {},
users_gags = {},
libs = require('./libs.js')(mclient, users_gags),
sendMessage;
fs.writeFile(__dirname + "/chat.pid", process.pid, function (err) {
if (err) {
console.log(err);
} else {
console.log('The chat process pid was saved!');
}
});
// Run Socket.IO to listen on specific port
io.listen(9045, {'log level': 1});
// Selects all connection to specific channel
chatSockets = io.of('/chat');
notify.roomsModuleInit(chatSockets);
// Connect to Memcache
mclient = new memcache.Client(11312, 'localhost');
mclient.connect();
// Connect to Redis
rclient = redis.createClient(6379, 'localhost');
rclient.on('connect', function () {
// Listen system messages 30 seconds after start
setTimeout(function () {
rclient.subscribe('chat_system');
rclient.subscribe('sys_cmd');
}, 1000 * 30);
});
// Socket IO events
chatSockets.on('connection', function (socket) {
// Accept new connection
socket.emit('auth', { hello: 'Welcome to WGlads brand-new chat' });
// Accept authorization data from client
socket.on('save_auth', function (data) {
var user_md5,
got_key,
comms,
comms_arr,
commn_key,
count_users,
i;
console.log("Accept authorization data from client");
if (!data.login || !data.auth_key) {
console.log("Client sent wrong authorization data");
return false;
}
// create MD5 to verify secret_key
user_md5 = libs.make_md5(libs.convert_tocp1251(data.login));
got_key = libs.make_md5(secret_key + user_md5);
if (got_key !== data.auth_key) {
console.log("login: " + data.login + " = wrong md5");
socket.emit('error', { text: 'wrong auth key' });
return false;
}
// add user to system
users[data.login] = socket.id;
users_id[data.uid] = data.login;
// save extra data to socket
socket.uid = data.uid;
socket.login = data.login;
socket.level = data.level;
socket.locate = data.locate;
socket.color = data.color;
socket.join('global');
socket.join('loc_' + data.locate);
notify.addUser(data.uid, data.login, socket.id);
if (data.clan) {
socket.join('clan_' + data.clan);
if (data.clan === 'Magistratus') {
socket.join('clan_Senate');
}
}
if (data.communities !== 'underfined' && data.communities !== '') {
comms = data.communities.split(",");
comms_arr = [];
for (i = 0; i <= comms.length - 1; i += 1) {
commn_key = libs.make_md5(comms[i]);
socket.join('comms_' + commn_key);
console.log('join comm: "' + comms[i] + '", comms_' + commn_key);
comms_arr[comms_arr.length] = 'comms_' + commn_key;
}
socket.communities = comms_arr;
} else {
socket.communities = [];
}
count_users = users.length;
console.log('It\'s ' + count_users + ' users online for now');
});
// Accept authorization data from client
socket.on('switch_location', function (data) {
if (!socket.login) {
return;
}
console.log('User ' + socket.login + ' wish to switch location: ' + data.location);
socket.leave('loc_' + socket.locate);
socket.join('loc_' + data.location);
socket.locate = data.location;
//console.log(chatSockets.manager.roomClients[socket.id]);
});
// Accept authorization data from client
socket.on('save_communities', function (data) {
var i,
comms,
comms_new,
comms_arr,
commn_key;
if (!socket.login) {
return;
}
console.log('User ' + socket.login + ' save communities: ' + data.communities);
comms = socket.communities;
for (i = 0; i <= comms.length - 1; i += 1) {
socket.leave(comms[i]);
}
comms_new = data.communities.split(",");
comms_arr = [];
for (i = 0; i <= comms_new.length - 1; i += 1) {
commn_key = libs.make_md5(comms_new[i]);
socket.join('comms_' + commn_key);
comms_arr[comms_arr.length] = 'comms_' + commn_key;
}
socket.communities = comms_arr;
});
// Accept authorization data from client
socket.on('save_level', function (data) {
if (!socket.login) {
return;
}
console.log('User ' + socket.login + ' save level: ' + data.level);
socket.level = data.level;
});
// Accept new messages
socket.on('add_msg', function (data) {
var ts,
txt,
text,
words,
pattern,
pattern_txt,
logins_constr,
private_login,
communities,
comm_name,
commn_key,
clan_row,
i;
if (!socket.login) {
return;
}
// check if user got gag in mouth
ts = Math.round((new Date()).getTime() / 1000);
if (users_gags[socket.uid] && (users_gags[socket.uid] === 0 || users_gags[socket.uid] > ts)) {
txt = 'You have been blocked in chat, you will be able to chat only after ' + Math.ceil((users_gags[socket.uid] - ts) / 60) + ' min.';
sendMessage({
type: 'private',
to: socket.login,
from: 'Senator Gracchus',
text: 'private (' + socket.login + ') ' + txt,
color: 'black'
});
return true;
}
console.log('Accepted new message from user ' + socket.login);
text = data.text;
// filtering accepted message
text = text.replace(/^\s+|\s+$/g, ''); // trim
text = libs.escapeHtml(text); // remove html
// wrap big strings
words = text.split(' ');
for (i = 0; i <= words.length - 1; i += 1) {
words[i] = wrap.hard(70)(words[i]).split('\n').join(' ');
}
text = words.join(' ');
// replace internal links with text
text = text.replace(/http:\/\/wglads\.(com|ru)\/game\/[\-a-zA-Z0-9_\.?=\/&]+/i, '(prohibited link)');
// check if the message is advertising
pattern_txt = text.replace(/[^a-zA-Z]/i, "");
if (pattern_txt.match(/(gladiatorsru|fdworldsnet|vk\.com\/app)/i)) {
txt = 'This is prohibited link, don\'t add it to chat, please :thanks:';
sendMessage({
type: 'private',
to: socket.login,
from: 'Senator Gracchus',
text: 'private (' + socket.login + ') ' + txt,
color: 'black'
});
return true;
}
// if user's level less than 20 and he's writing in private chat
if (socket.level < 20 && (text.indexOf('private (') >= 0 || text.indexOf('communmity (') >= 0 || text.match(/clan \([a-zA-Z]+\)/i))) {
txt = 'it\'s too sad, however until 20 level you allowed to chat exclusively in common chat. We all once were in the same circumstances :zdarov: ';
sendMessage({
type: 'private',
to: socket.login,
from: 'Senator Gracchus',
text: 'private (' + socket.login + ') ' + txt,
color: 'black'
});
return true;
}
// if message contains private notation
if (text.indexOf('private (') >= 0) {
pattern = /private \(([\-a-zA-Zа-яА-Я0-9_\s]+)\)/g;
logins_constr = text.match(pattern);
console.log('- sending to private section');
if (logins_constr !== null) {
for (i = 0; i < logins_constr.length; i += 1) {
private_login = logins_constr[i].replace(pattern, "$1");
if (private_login !== socket.login) {
//console.log('login - '+private_login);
sendMessage({
type: 'private',
to: private_login,
from: socket.login,
text: text,
color: socket.color,
is_hidden: data.is_hidden
});
}
}
sendMessage({
type: 'private',
to: socket.login,
from: socket.login,
text: text,
color: socket.color,
is_hidden: data.is_hidden
});
return true;
}
return false;
}
// if message contain community address
if (text.indexOf('community (') >= 0) {
pattern = /community \(([\-a-zA-Zа-яА-Я\s\.]+)\)/g;
communities = text.match(pattern);
console.log('- sending to community section');
if (communities !== null) {
if (communities.length > 0) {
comm_name = communities[0].replace(pattern, "$1");
commn_key = libs.make_md5(comm_name);
sendMessage({
type: 'community',
to: 'comms_' + commn_key,
from: socket.login,
text: text,
color: socket.color,
is_hidden: data.is_hidden
});
}
return true;
}
return false;
}
// if message contain clan issue
if (text.indexOf('clan (') >= 0) {
console.log('clan section');
pattern = /clan \(([a-zA-Z]+)\)/i;
clan_row = pattern.exec(text);
//console.log(clan_row);
if (clan_row !== null) {
sendMessage({
type: 'clan',
to: 'clan_' + clan_row[1],
from: socket.login,
text: text,
color: socket.color,
is_hidden: socket.is_hidden
});
return true;
}
return false;
}
// if a message to a global chat
if (data.channel === 'global') {
sendMessage({
type: 'global',
to: 'global',
from: socket.login,
text: text,
color: socket.color,
is_hidden: data.is_hidden
});
return true;
}
// if just a regular message to current location chat
sendMessage({
type: 'locate',
to: 'loc_' + socket.locate,
from: socket.login,
text: text,
color: socket.color,
is_hidden: data.is_hidden
});
});
socket.on('disconnect', function () {
console.log('user ' + socket.login + ' disconnected :(');
notify.delUser(socket.uid);
//uid_todel[socket.uid] = Math.round((new Date()).getTime() / 1000);
//console.log(chatSockets.clients())
//delete users[socket.login];
//delete users_id[socket.uid];
});
});
sendMessage = function (data) {
var now = new Date(),
hours = (now.getHours() < 10) ? '0' + now.getHours() : now.getHours(),
minutes = (now.getMinutes() < 10) ? '0' + now.getMinutes() : now.getMinutes(),
time = hours + ':' + minutes, //+':'+now.getSeconds()
from = data.from,
to = data.to,
text = data.text,
color = data.color,
type = data.type,
is_hidden = data.is_hidden,
ready_data;
if (is_hidden === '1') {
from = 'unkown';
}
ready_data = {
'type': type,
'time': time,
'login': from,
'text': text,
'color': color
};
if (type === 'global' || type === 'locate' || type === 'clan' || type === 'community') {
console.log('send to: ' + type + ' - ' + data.to);
chatSockets.in(data.to).emit('message', ready_data);
return true;
}
if (type === 'private' || type === 'system') {
//console.log(users);
//console.log('to'+data.to);
//console.log('user '+users[data.to]);
if (!users[to]) {
return;
}
console.log('send to: private - ' + to);
chatSockets.socket(users[to]).emit('message', ready_data);
return true;
}
};
// Redis events
rclient.on('message', function (channel, rawMsgData) {
//console.log('get redis msg');
var msgData = JSON.parse(rawMsgData),
data,
id_list,
socket_id,
i;
if (channel === 'chat_system') {
if (msgData.type === 'system') {
//console.log('system msg');
if (!users[msgData.to]) {
return;
}
data = {
'type': 'system',
'from': 'system',
'to': msgData.to,
'text': msgData.text,
'color': 'black'
};
sendMessage(data);
//console.log('send redis system msg to chat');
}
if (msgData.type === 'locate') {
//console.log('locate msg');
data = {
'type': 'locate',
'from': msgData.from,
'to': 'loc_' + msgData.to,
'text': msgData.text,
'color': 'black'
};
sendMessage(data);
//console.log('send redis locate msg to chat: '+msgData.to);
}
if (msgData.type === 'private') {
//console.log('private msg');
data = {
'type': 'private',
'to': msgData.to,
'from': msgData.from,
'text': 'private (' + msgData.to + ') ' + msgData.text,
'color': 'black'
};
sendMessage(data);
//console.log('send redis private msg to chat: '+msgData.to);
}
if (msgData.type === 'refresh') {
console.log('refresh msg');
id_list = msgData.uid.split(',');
for (i = 0; i < id_list.length; i += 1) {
if (users_id[id_list[i]]) {
//console.log('send refresh to: '+id_list[i]);
socket_id = users[users_id[id_list[i]]];
chatSockets.socket(socket_id).emit('message', {
"type": "refresh"
});
}
}
}
return true;
}
if (channel === 'sys_cmd') {
if (msgData.cmd === 'gags_reload') {
libs.getUsersGags();
}
}
});
// Memcache events
mclient_reconnect_id = -1;
mclient.on('connect', function () {
console.log('Memcache connected');
libs.getUsersGags();
});
mclient.on('close', function () {
console.log('memcache disconnected');
if (mclient_reconnect_id > -1) {
clearTimeout(mclient_reconnect_id);
}
mclient_reconnect_id = setTimeout(function () {
console.log("trying to reconnect Memcache..");
mclient.connect();
}, 5000);
});
mclient.on('error', function () {
console.log('Memcache error');
});
}());