forked from smogon/pokemon-showdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
punishments.js
1360 lines (1211 loc) · 36.9 KB
/
punishments.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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
/**
* Punishments
* Pokemon Showdown - http://pokemonshowdown.com/
*
* Handles the punishing of users on PS.
*
* There are four types of global punishments on PS. Locks, bans, namelocks and rangelocks.
* This file contains the lists of users that have been punished (both IPs and usernames),
* as well as the functions that handle the execution of said punishments.
*
* @license MIT license
*/
let Punishments = module.exports;
const FS = require('./fs');
const PUNISHMENT_FILE = 'config/punishments.tsv';
const ROOM_PUNISHMENT_FILE = 'config/room-punishments.tsv';
const SHAREDIPS_FILE = 'config/sharedips.tsv';
const RANGELOCK_DURATION = 60 * 60 * 1000; // 1 hour
const LOCK_DURATION = 48 * 60 * 60 * 1000; // 48 hours
const BAN_DURATION = 7 * 24 * 60 * 60 * 1000; // 1 week
const ROOMBAN_DURATION = 48 * 60 * 60 * 1000; // 48 hours
const BLACKLIST_DURATION = 365 * 24 * 60 * 60 * 1000; // 1 year
const USERID_REGEX = /^[a-z0-9]+$/;
const PUNISH_TRUSTED = false;
const PUNISHMENT_POINT_VALUES = {MUTE: 2, BLACKLIST: 3, ROOMBAN: 4};
const AUTOLOCK_POINT_THRESHOLD = 8;
/**
* a punishment is an array: [punishType, userid, expireTime, reason]
* @typedef {[string, string, number, string]} Punishment
*/
class PunishmentMap extends Map/*:: <string, Punishment> */ {
get(k) {
const punishment = super.get(k);
if (punishment) {
if (Date.now() < punishment[2]) return punishment;
this.delete(k);
}
return undefined;
}
has(k) {
return !!this.get(k);
}
forEach(callback) {
super.forEach((punishment, k) => {
if (Date.now() < punishment[2]) return callback(punishment, k);
this.delete(k);
});
}
}
/**
* ips is an ip:punishment Map
*/
Punishments.ips = new PunishmentMap();
/**
* userids is a userid:punishment Map
*/
Punishments.userids = new PunishmentMap();
/**
* @augments {Map<string, Punishment>}
*/
class NestedPunishmentMap extends Map {
nestedSet(k1, k2, value) {
if (!this.get(k1)) {
this.set(k1, new Map());
}
this.get(k1).set(k2, value);
}
nestedGet(k1, k2) {
const subMap = this.get(k1);
if (!subMap) return subMap;
const punishment = subMap.get(k2);
if (punishment) {
if (Date.now() < punishment[2]) return punishment;
this.nestedDelete(k1, k2);
}
return undefined;
}
nestedHas(k1, k2) {
return !!this.nestedGet(k1, k2);
}
nestedDelete(k1, k2) {
const subMap = this.get(k1);
if (!subMap) return;
subMap.delete(k2);
if (!subMap.size) this.delete(k1);
}
nestedForEach(callback) {
this.forEach((subMap, k1) => {
subMap.forEach((punishment, k2) => {
if (Date.now() < punishment[2]) return callback(punishment, k1, k2);
this.nestedDelete(k1, k2);
});
});
}
}
/**
* roomUserids is a roomid:userid:punishment nested Map
*/
Punishments.roomUserids = new NestedPunishmentMap();
/**
* roomIps is a roomid:ip:punishment Map
*/
Punishments.roomIps = new NestedPunishmentMap();
/**
* sharedIps is an ip:note Map
*/
Punishments.sharedIps = new Map();
/*********************************************************
* Persistence
*********************************************************/
// punishType is an allcaps string, for global punishments they can be one of the following:
// 'LOCK'
// 'BAN'
// 'NAMELOCK'
// For room punishments, they can be anything in the roomPunishmentTypes map.
// This map can be extended with custom punishments by chat plugins.
// Keys in the map correspond to punishTypes, values signify the way they should be displayed in /alt
// By default, this includes:
// 'ROOMBAN'
// 'BLACKLIST'
// 'MUTE' (used by getRoomPunishments)
Punishments.roomPunishmentTypes = new Map([
['ROOMBAN', 'banned'],
['BLACKLIST', 'blacklisted'],
['MUTE', 'muted'],
]);
// punishments.tsv is in the format:
// punishType, userid, ips/usernames, expiration time
// room-punishments.tsv is in the format:
// punishType, roomid:userid, ips/usernames, expiration time
Punishments.loadPunishments = async function () {
const data = await FS(PUNISHMENT_FILE).readTextIfExists();
if (!data) return;
for (const row of data.split("\n")) {
if (!row || row === '\r') continue;
const [punishType, id, altKeys, expireTimeStr, ...rest] = row.trim().split("\t");
const expireTime = Number(expireTimeStr);
if (punishType === "Punishment") continue;
const keys = altKeys.split(',').concat(id);
const punishment = [punishType, id, expireTime].concat(rest);
if (Date.now() >= expireTime) {
continue;
}
for (const key of keys) {
if (!USERID_REGEX.test(key)) {
Punishments.ips.set(key, punishment);
} else {
Punishments.userids.set(key, punishment);
}
}
}
};
Punishments.loadRoomPunishments = async function () {
const data = await FS(ROOM_PUNISHMENT_FILE).readTextIfExists();
if (!data) return;
for (const row of data.split("\n")) {
if (!row || row === '\r') continue;
const [punishType, id, altKeys, expireTimeStr, ...rest] = row.trim().split("\t");
const expireTime = Number(expireTimeStr);
if (punishType === "Punishment") continue;
const [roomid, userid] = id.split(':');
if (!userid) continue; // invalid format
const keys = altKeys.split(',').concat(userid);
const punishment = [punishType, userid, expireTime].concat(rest);
if (Date.now() >= expireTime) {
continue;
}
for (const key of keys) {
if (!USERID_REGEX.test(key)) {
Punishments.roomIps.nestedSet(roomid, key, punishment);
} else {
Punishments.roomUserids.nestedSet(roomid, key, punishment);
}
}
}
};
Punishments.savePunishments = function () {
FS(PUNISHMENT_FILE).writeUpdate(() => {
const saveTable = new Map();
Punishments.ips.forEach((punishment, ip) => {
const [punishType, id, ...rest] = punishment;
if (id.charAt(0) === '#') return;
let entry = saveTable.get(id);
if (entry) {
entry.keys.push(ip);
return;
}
entry = {
keys: [ip],
punishType: punishType,
rest: rest,
};
saveTable.set(id, entry);
});
Punishments.userids.forEach((punishment, userid) => {
const [punishType, id, ...rest] = punishment;
if (id.charAt(0) === '#') return;
let entry = saveTable.get(id);
if (!entry) {
entry = {
keys: [],
punishType: punishType,
rest: rest,
};
saveTable.set(id, entry);
}
if (userid !== id) entry.keys.push(userid);
});
let buf = 'Punishment\tUser ID\tIPs and alts\tExpires\r\n';
saveTable.forEach((entry, id) => {
buf += Punishments.renderEntry(entry, id);
});
return buf;
});
};
Punishments.saveRoomPunishments = function () {
FS(ROOM_PUNISHMENT_FILE).writeUpdate(() => {
const saveTable = new Map();
Punishments.roomIps.nestedForEach((punishment, roomid, ip) => {
const [punishType, punishUserid, ...rest] = punishment;
const id = roomid + ':' + punishUserid;
if (id.charAt(0) === '#') return;
let entry = saveTable.get(id);
if (entry) {
entry.keys.push(ip);
return;
}
entry = {
keys: [ip],
punishType: punishType,
rest: rest,
};
saveTable.set(id, entry);
});
Punishments.roomUserids.nestedForEach((punishment, roomid, userid) => {
const [punishType, punishUserid, ...rest] = punishment;
const id = roomid + ':' + punishUserid;
let entry = saveTable.get(id);
if (!entry) {
entry = {
keys: [],
punishType: punishType,
rest: rest,
};
saveTable.set(id, entry);
}
if (userid !== punishUserid) entry.keys.push(userid);
});
let buf = 'Punishment\tRoom ID:User ID\tIPs and alts\tExpires\r\n';
saveTable.forEach((entry, id) => {
buf += Punishments.renderEntry(entry, id);
});
return buf;
});
};
/**
* @param {Object} entry
* @param {string} id
*/
Punishments.appendPunishment = function (entry, id, filename) {
if (id.charAt(0) === '#') return;
let buf = Punishments.renderEntry(entry, id);
FS(filename).append(buf);
};
/**
* @param {Object} entry
* @param {string} id
* @return {string}
*/
Punishments.renderEntry = function (entry, id) {
let row = [entry.punishType, id, entry.keys.join(',')].concat(entry.rest);
return row.join('\t') + '\r\n';
};
Punishments.loadBanlist = async function () {
const data = await FS('config/ipbans.txt').readTextIfExists();
if (!data) return;
let rangebans = [];
for (const row of data.split("\n")) {
const ip = row.split('#')[0].trim();
if (!ip) continue;
if (ip.includes('/')) {
rangebans.push(ip);
} else if (!Punishments.ips.has(ip)) {
Punishments.ips.set(ip, ['BAN', '#ipban', Infinity]);
}
}
Punishments.checkRangeBanned = Dnsbl.checker(rangebans);
};
// sharedips.tsv is in the format:
// IP, type (in this case always SHARED), note
Punishments.loadSharedIps = async function () {
const data = await FS(SHAREDIPS_FILE).readTextIfExists();
if (!data) return;
for (const row of data.split("\n")) {
if (!row || row === '\r') continue;
const [ip, type, note] = row.trim().split("\t");
if (!ip.includes('.')) continue;
if (type !== 'SHARED') continue;
Punishments.sharedIps.set(ip, note);
}
};
/**
* @param {string} ip
* @param {string} note
*/
Punishments.appendSharedIp = function (ip, note) {
let buf = `${ip}\tSHARED\t${note}\r\n`;
FS(SHAREDIPS_FILE).append(buf);
};
Punishments.saveSharedIps = function () {
let buf = 'IP\tType\tNote\r\n';
Punishments.sharedIps.forEach((note, ip) => {
buf += `${ip}\tSHARED\t${note}\r\n`;
});
FS(SHAREDIPS_FILE).write(buf);
};
setImmediate(() => {
Punishments.loadPunishments();
Punishments.loadRoomPunishments();
Punishments.loadBanlist();
Punishments.loadSharedIps();
});
/*********************************************************
* Adding and removing
*********************************************************/
/**
* @param {User} user
* @param {Punishment} punishment
* @param {?Set<string>} recursionKeys
* @return {?Array}
*/
Punishments.punish = function (user, punishment, recursionKeys) {
let existingPunishment = Punishments.userids.get(toId(user.name));
if (existingPunishment) {
// don't reduce the duration of an existing punishment
if (existingPunishment[2] > punishment[2]) {
punishment[2] = existingPunishment[2];
}
// don't override stronger punishment types
const types = ['LOCK', 'NAMELOCK', 'BAN'];
if (types.indexOf(existingPunishment[0]) > types.indexOf(punishment[0])) {
punishment[0] = existingPunishment[0];
}
}
let keys = recursionKeys || new Set();
let affected;
if (!recursionKeys) {
affected = user.getAltUsers(PUNISH_TRUSTED, true);
for (let curUser of affected) {
this.punish(curUser, punishment, keys);
}
}
for (let ip in user.ips) {
Punishments.ips.set(ip, punishment);
keys.add(ip);
}
let lastUserId = user.getLastId();
if (!lastUserId.startsWith('guest')) {
Punishments.userids.set(lastUserId, punishment);
}
if (user.autoconfirmed) {
Punishments.userids.set(user.autoconfirmed, punishment);
keys.add(user.autoconfirmed);
}
if (user.trusted) {
Punishments.userids.set(user.trusted, punishment);
keys.add(user.trusted);
if (!PUNISH_TRUSTED) affected.unshift(user);
}
if (!recursionKeys) {
const [punishType, id, ...rest] = punishment;
keys.delete(id);
Punishments.appendPunishment({
keys: Array.from(keys),
punishType: punishType,
rest: rest,
}, id, PUNISHMENT_FILE);
return affected;
}
};
Punishments.punishName = function (userid, punishment) {
let foundKeys = Punishments.search(userid)[0].map(key => key.split(':')[0]);
let userids = new Set([userid]);
let ips = new Set();
for (let key of foundKeys) {
if (key.includes('.')) {
ips.add(key);
} else {
userids.add(key);
}
}
userids.forEach(id => {
Punishments.userids.set(id, punishment);
});
ips.forEach(ip => {
Punishments.ips.set(ip, punishment);
});
const [punishType, id, ...rest] = punishment;
let affected = Users.findUsers(Array.from(userids), Array.from(ips), {includeTrusted: PUNISH_TRUSTED, forPunishment: true});
userids.delete(id);
Punishments.appendPunishment({
keys: Array.from(userids).concat(Array.from(ips)),
punishType: punishType,
rest: rest,
}, id, PUNISHMENT_FILE);
return affected;
};
/**
* @param {string} id
* @param {string} punishType
*/
Punishments.unpunish = function (id, punishType) {
id = toId(id);
let punishment = Punishments.userids.get(id);
if (punishment) {
id = punishment[1];
}
// in theory we can stop here if punishment doesn't exist, but
// in case of inconsistent state, we'll try anyway
let success = false;
Punishments.ips.forEach((punishment, key) => {
if (punishment[1] === id && punishment[0] === punishType) {
Punishments.ips.delete(key);
success = id;
}
});
Punishments.userids.forEach((punishment, key) => {
if (punishment[1] === id && punishment[0] === punishType) {
Punishments.userids.delete(key);
success = id;
}
});
if (success) {
Punishments.savePunishments();
}
return success;
};
/**
* @param {User} user
* @param {Punishment} punishment
* @param {?Set<string>} recursionKeys
* @return {?Array}
*/
Punishments.roomPunish = function (room, user, punishment, recursionKeys) {
let keys = recursionKeys || new Set();
let affected;
if (!recursionKeys) {
affected = user.getAltUsers(PUNISH_TRUSTED, true);
for (let curUser of affected) {
this.roomPunish(room, curUser, punishment, keys);
}
}
for (let ip in user.ips) {
Punishments.roomIps.nestedSet(room.id, ip, punishment);
keys.add(ip);
}
if (!user.userid.startsWith('guest')) {
Punishments.roomUserids.nestedSet(room.id, user.userid, punishment);
}
if (user.autoconfirmed) {
Punishments.roomUserids.nestedSet(room.id, user.autoconfirmed, punishment);
keys.add(user.autoconfirmed);
}
if (user.trusted) {
Punishments.roomUserids.nestedSet(room.id, user.trusted, punishment);
keys.add(user.trusted);
if (!PUNISH_TRUSTED) affected.unshift(user);
}
if (!recursionKeys) {
const [punishType, id, ...rest] = punishment;
keys.delete(id);
Punishments.appendPunishment({
keys: Array.from(keys),
punishType: punishType,
rest: rest,
}, room.id + ':' + id, ROOM_PUNISHMENT_FILE);
if (!(room.isPrivate === true || room.isPersonal || room.battle)) Punishments.monitorRoomPunishments(user);
return affected;
}
};
Punishments.roomPunishName = function (room, userid, punishment) {
let foundKeys = Punishments.search(userid)[0].map(key => key.split(':')[0]);
let userids = new Set([userid]);
let ips = new Set();
for (let key of foundKeys) {
if (key.includes('.')) {
ips.add(key);
} else {
userids.add(key);
}
}
userids.forEach(id => {
Punishments.roomUserids.nestedSet(room.id, id, punishment);
});
ips.forEach(ip => {
Punishments.roomIps.nestedSet(room.id, ip, punishment);
});
const [punishType, id, ...rest] = punishment;
let affected = Users.findUsers(Array.from(userids), Array.from(ips), {includeTrusted: PUNISH_TRUSTED, forPunishment: true});
userids.delete(id);
Punishments.appendPunishment({
keys: Array.from(userids).concat(Array.from(ips)),
punishType: punishType,
rest: rest,
}, room.id + ':' + id, ROOM_PUNISHMENT_FILE);
if (!(room.isPrivate === true || room.isPersonal || room.battle)) Punishments.monitorRoomPunishments(userid);
return affected;
};
/**
* @param {Room} room
* @param {string} userid
* @param {string} punishType
* @param {boolean} ignoreWrite Flag to skip persistent storage.
*/
Punishments.roomUnpunish = function (room, id, punishType, ignoreWrite) {
id = toId(id);
let punishment = Punishments.roomUserids.nestedGet(room, id);
if (punishment) {
id = punishment[1];
}
// in theory we can stop here if punishment doesn't exist, but
// in case of inconsistent state, we'll try anyway
let success;
const ipSubMap = Punishments.roomIps.get(room.id);
if (ipSubMap) {
ipSubMap.forEach((punishment, key) => {
if (punishment[1] === id && punishment[0] === punishType) {
ipSubMap.delete(key);
success = id;
}
});
}
const useridSubMap = Punishments.roomUserids.get(room.id);
if (useridSubMap) {
useridSubMap.forEach((punishment, key) => {
if (punishment[1] === id && punishment[0] === punishType) {
useridSubMap.delete(key);
success = id;
}
});
}
if (success && !ignoreWrite) {
Punishments.saveRoomPunishments();
}
return success;
};
/*********************************************************
* Specific punishments
*********************************************************/
/**
* @param {User} user
* @param {number} expireTime
* @param {string} id
* @param {...string} [reason]
* @return {?Array}
*/
Punishments.ban = function (user, expireTime, id, ...reason) {
if (!id) id = user.getLastId();
if (!expireTime) expireTime = Date.now() + BAN_DURATION;
let punishment = ['BAN', id, expireTime, ...reason];
let affected = Punishments.punish(user, punishment);
for (let curUser of affected) {
curUser.locked = id;
curUser.disconnectAll();
}
return affected;
};
/**
* @param {string} name
*/
Punishments.unban = function (name) {
return Punishments.unpunish(name, 'BAN');
};
/**
* @param {User} user
* @param {number} expireTime
* @param {string} id
* @param {...string} [reason]
* @return {?Array}
*/
Punishments.lock = function (user, expireTime, id, ...reason) {
if (!id && user) id = user.getLastId();
if (!user || typeof user === 'string') user = Users(id);
if (!expireTime) expireTime = Date.now() + LOCK_DURATION;
let punishment = ['LOCK', id, expireTime, ...reason];
let affected = [];
if (user) {
affected = Punishments.punish(user, punishment);
} else {
affected = Punishments.punishName(id, punishment);
}
for (let curUser of affected) {
curUser.locked = id;
curUser.updateIdentity();
}
return affected;
};
/**
* @param {User} user
* @param {Room} room
* @param {string} source
* @param {string} reason
* @param {?string} message
* @param {?boolean} week
*/
Punishments.autolock = function (user, room, source, reason, message, week) {
if (!message) message = reason;
let punishment = `LOCKED`;
let expires = null;
if (week) {
expires = Date.now() + 7 * 24 * 60 * 60 * 1000;
punishment = `WEEKLOCKED`;
}
Punishments.lock(user, expires, toId(user), `Autolock: ${user.name || toId(user)}: ${reason}`);
Monitor.log(`[${source}] ${punishment}: ${message}`);
Rooms.global.modlog(`(${toId(room)}) AUTOLOCK: [${toId(user)}]: ${reason}`);
};
/**
* @param {string} name
*/
Punishments.unlock = function (name) {
let user = Users(name);
let id = toId(name);
let success = [];
if (user && user.locked && !user.namelocked) {
id = user.locked;
user.locked = false;
user.namelocked = false;
user.updateIdentity();
success.push(user.getLastName());
if (id.charAt(0) !== '#') {
Users.users.forEach(curUser => {
if (curUser.locked === id) {
curUser.locked = false;
curUser.namelocked = false;
curUser.updateIdentity();
success.push(curUser.getLastName());
}
});
}
}
if (Punishments.unpunish(name, 'LOCK')) {
if (!success.length) success.push(name);
}
if (!success.length) return undefined;
if (!success.some(v => toId(v) === id)) {
success.push(id);
}
return success;
};
/**
* @param {User} user
* @param {number} expireTime
* @param {string} id
* @param {...string} [reason]
* @return {?Array}
*/
Punishments.namelock = function (user, expireTime, id, ...reason) {
if (!id) id = user.getLastId();
if (!expireTime) expireTime = Date.now() + LOCK_DURATION;
let punishment = ['NAMELOCK', id, expireTime, ...reason];
let affected = Punishments.punish(user, punishment);
for (let curUser of affected) {
curUser.locked = id;
curUser.namelocked = id;
curUser.resetName();
curUser.updateIdentity();
}
return affected;
};
/**
* @param {string} name
*/
Punishments.unnamelock = function (name) {
let user = Users(name);
let id = toId(name);
let success = [];
let unpunished = Punishments.unpunish(name, 'NAMELOCK');
if (user && user.locked) {
id = user.locked;
user.locked = false;
user.namelocked = false;
user.resetName();
success.push(user.getLastName());
if (id.charAt(0) !== '#') {
Users.users.forEach(curUser => {
if (curUser.locked === id) {
curUser.locked = false;
curUser.namelocked = false;
curUser.resetName();
success.push(curUser.getLastName());
}
});
}
}
if (unpunished && !success.length) success.push(name);
if (!success.length) return false;
if (!success.some(v => toId(v) === id)) {
success.push(id);
}
return success;
};
/**
* @param {string} range
* @param {string} reason
*/
Punishments.lockRange = function (range, reason) {
let punishment = ['LOCK', '#rangelock', Date.now() + RANGELOCK_DURATION, reason];
Punishments.ips.set(range, punishment);
};
/**
* @param {string} range
* @param {string} reason
*/
Punishments.banRange = function (range, reason) {
let punishment = ['BAN', '#rangelock', Date.now() + RANGELOCK_DURATION, reason];
Punishments.ips.set(range, punishment);
};
/**
* @param {Room} room
* @param {User} user
* @param {number} expireTime
* @param {string} userId
* @param {...string} [reason]
* @return {?Array}
*/
Punishments.roomBan = function (room, user, expireTime, userId, ...reason) {
if (!userId) userId = user.getLastId();
if (!expireTime) expireTime = Date.now() + ROOMBAN_DURATION;
let punishment = ['ROOMBAN', userId, expireTime].concat(reason);
let affected = Punishments.roomPunish(room, user, punishment);
for (let curUser of affected) {
if (room.game && room.game.removeBannedUser) {
room.game.removeBannedUser(curUser);
}
curUser.leaveRoom(room.id);
}
return affected;
};
/**
* @param {Room} room
* @param {User} user
* @param {number} expireTime
* @param {string} userId
* @param {...string} [reason]
* @return {?Array}
*/
Punishments.roomBlacklist = function (room, user, expireTime, userId, ...reason) {
if (!userId && user) userId = user.getLastId();
if (!user) user = Users(userId);
if (!expireTime) expireTime = Date.now() + BLACKLIST_DURATION;
let punishment = ['BLACKLIST', userId, expireTime].concat(reason);
let affected = [];
if (!user || userId && userId !== user.userid) {
affected = Punishments.roomPunishName(room, userId, punishment);
}
if (user) {
affected = affected.concat(Punishments.roomPunish(room, user, punishment));
}
for (let curUser of affected) {
if (room.game && room.game.removeBannedUser) {
room.game.removeBannedUser(curUser);
}
curUser.leaveRoom(room.id);
}
return affected;
};
/**
* @param {Room} room
* @param {string} userid
*/
Punishments.roomUnban = function (room, userid) {
const user = Users(userid);
if (user) {
let punishment = Punishments.isRoomBanned(user, room.id);
if (punishment) userid = punishment[1];
}
return Punishments.roomUnpunish(room, userid, 'ROOMBAN');
};
/**
* @param {Room} room
* @param {string} userid
* @param {boolean} ignoreWrite Flag to skip persistent storage.
*/
Punishments.roomUnblacklist = function (room, userid, ignoreWrite) {
const user = Users(userid);
if (user) {
let punishment = Punishments.isRoomBanned(user, room.id);
if (punishment) userid = punishment[1];
}
return Punishments.roomUnpunish(room, userid, 'BLACKLIST', ignoreWrite);
};
/**
* @param {Room} room
*/
Punishments.roomUnblacklistAll = function (room) {
const roombans = Punishments.roomUserids.get(room.id);
if (!roombans) return false;
let unblacklisted = [];
roombans.forEach((punishment, userid) => {
if (punishment[0] === 'BLACKLIST') {
Punishments.roomUnblacklist(room, userid, true);
unblacklisted.push(userid);
}
});
if (unblacklisted.length === 0) return false;
Punishments.saveRoomPunishments();
return unblacklisted;
};
/**
* @param {string} ip
* @param {string} note
*/
Punishments.addSharedIp = function (ip, note) {
Punishments.sharedIps.set(ip, note);
Punishments.appendSharedIp(ip, note);
Users.users.forEach(user => {
if (user.locked && user.locked !== user.userid && ip in user.ips) {
if (!user.autoconfirmed) {
user.semilocked = `#sharedip ${user.locked}`;
}
user.locked = false;
user.updateIdentity();
}
});
};
/**
* @param {string} ip
*/
Punishments.removeSharedIp = function (ip) {
Punishments.sharedIps.delete(ip);
Punishments.saveSharedIps();
};
/*********************************************************
* Checking
*********************************************************/
Punishments.search = function (searchId) {
let foundKeys = [];
let foundRest = null;
Punishments.ips.forEach((punishment, ip) => {
const [, id, ...rest] = punishment;
if (searchId === id || searchId === ip) {
foundKeys.push(ip);
foundRest = rest;
}
});
Punishments.userids.forEach((punishment, userid) => {
const [, id, ...rest] = punishment;
if (searchId === id || searchId === userid) {
foundKeys.push(userid);
foundRest = rest;
}
});
Punishments.roomIps.nestedForEach((punishment, roomid, ip) => {
const [, punishUserid, ...rest] = punishment;
if (searchId === punishUserid || searchId === ip) {
foundKeys.push(ip + ':' + roomid);
foundRest = rest;
}
});
Punishments.roomUserids.nestedForEach((punishment, roomid, userid) => {
const [, punishUserid, ...rest] = punishment;
if (searchId === punishUserid || searchId === userid) {
foundKeys.push(userid + ':' + roomid);
foundRest = rest;
}
});
return [foundKeys, foundRest];
};
/**
* @param {string} name
*/
Punishments.getPunishType = function (name) {
let punishment = Punishments.userids.get(toId(name));
if (punishment) return punishment[0];
let user = Users.get(name);
if (!user) return;
punishment = Punishments.ipSearch(user.latestIp);
if (punishment) return punishment[0];
return '';
};
/**
* @param {Room} room
* @param {string} name
*/
Punishments.getRoomPunishType = function (room, name) {
let punishment = Punishments.roomUserids.nestedGet(room.id, toId(name));