-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.lua
1659 lines (1593 loc) · 62.3 KB
/
commands.lua
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
verbana.commands = {}
local data = verbana.data
local lib_asn = verbana.lib_asn
local lib_ip = verbana.lib_ip
local log = verbana.log
local settings = verbana.settings
local util = verbana.util
local mod_priv = verbana.privs.moderator
local admin_priv = verbana.privs.admin
local kick_priv = verbana.privs.kick
local debug_mode = settings.debug_mode
local parse_timespan = util.parse_timespan
local safe = util.safe
local safe_kick_player = util.safe_kick_player
local table_contains = util.table_contains
local iso_date = util.iso_date
local worldpath = minetest.get_worldpath()
local function chat_send_player(name, message, ...)
message = message:format(...)
minetest.chat_send_player(name, message)
local irc_message = minetest.strip_colors(message)
if minetest.global_exists('irc') and irc.joined_players[name] then
irc.say(name, irc_message)
end
if minetest.global_exists('irc2') and irc2.joined_players[name] then
irc2.say(name, irc_message)
end
end
local function register_chatcommand(name, def)
if debug_mode then name = ('v_%s'):format(name) end
def.func = safe(def.func)
minetest.register_chatcommand(name, def)
end
local function override_chatcommand(name, def)
def.func = safe(def.func)
if debug_mode then
name = ('v_%s'):format(name)
minetest.register_chatcommand(name, def)
else
minetest.override_chatcommand(name, def)
end
end
local function alias_chatcommand(name, existing_name)
if debug_mode then
name = ('v_%s'):format(name)
existing_name = ('v_%s'):format(existing_name)
end
local existing_def = minetest.registered_chatcommands[existing_name]
if not existing_def then
log('error', "Could not alias command %q to %q, because %q doesn't exist", name, existing_name, existing_name)
else
minetest.register_chatcommand(name, existing_def)
end
end
register_chatcommand('sban_import', {
description='Import records from sban into verbana',
params='[<filename>]',
privs={[admin_priv]=true},
func=function (caller, filename)
if not filename or filename == '' then
filename = worldpath .. '/sban.sqlite'
end
if not util.file_exists(filename) then
return false, ('Could not open file %q.'):format(filename)
end
chat_send_player(caller, 'Importing SBAN. This can take a while...')
if data.import_from_sban(filename) then
return true, 'Successfully imported.'
else
return false, 'Error importing SBAN db (see server log)'
end
end
})
register_chatcommand('verification', {
description='Turn universal verification on or off',
params='on | off',
privs={[admin_priv]=true},
func=function(caller, params)
local value
if params == 'on' then
value = true
elseif params == 'off' then
value = false
else
return false, 'Invalid parameters'
end
if settings.universal_verification == value then
return true, ('Universal verification is already %s'):format(params)
end
settings.set_universal_verification(value)
return true, ('Turned universal verification %s'):format(params)
end
})
register_chatcommand('asn', {
description='Get the ASN associated with an IP or player name',
params='<player_name> | <IP>',
privs={[mod_priv]=true},
func = function(_, name_or_ipstr)
local ipstr
if lib_ip.is_valid_ip(name_or_ipstr) then
ipstr = name_or_ipstr
else
ipstr = data.fumble_about_for_an_ip(name_or_ipstr)
end
if not ipstr or ipstr == '' then
return false, ('"%s" is not a valid ip nor a known player'):format(name_or_ipstr)
end
local asn, description = lib_asn.lookup(ipstr)
if not asn or asn == 0 then
return false, ('could not find ASN for "%s"'):format(ipstr)
end
description = description or ''
return true, ('A%u (%s)'):format(asn, description)
end
})
----------------- SET PLAYER STATUS COMMANDS -----------------
local function parse_player_status_params(params)
local name, reason = params:match('^([a-zA-Z0-9_-]+)%s+(.*)$')
if not name then
name = params:match('^([a-zA-Z0-9_-]+)$')
end
if not name or name:len() > 20 then
return nil, nil, nil, ('Invalid argument(s): %q'):format(params)
end
local player_id, player_name = data.get_player_id(name)
if not player_id then
return nil, nil, nil, ('Unknown player: %s'):format(name)
end
local player_status = data.get_player_status(player_id, true)
return player_id, player_name, player_status, reason
end
local function has_suspicious_connection(player_id)
local connection_log = data.get_player_connection_log(player_id, 1)
if not connection_log or #connection_log ~= 1 then
log('warning', 'player %s exists but has no connection log?', player_id)
return true
end
local last_login = connection_log[1]
if last_login.ip_status_id == data.ip_status.trusted.id then
return false
elseif last_login.ip_status_id and last_login.ip_status_id ~= data.ip_status.default.id then
return true
elseif last_login.asn_status_id == data.asn_status.default.id then
return false
end
return true
end
register_chatcommand('verify', {
description='Verify a player',
params='<player_name> [<reason>]',
privs={[mod_priv]=true},
func=function(caller, params)
local player_id, player_name, player_status, reason = parse_player_status_params(params)
if not player_id then
return false, reason
end
if player_status.id ~= data.player_status.unverified.id then
return false, ('Player %s is not unverified'):format(player_name)
end
local executor_id = data.get_player_id(caller)
if not executor_id then
return false, 'ERROR: could not get executor ID?'
end
local status_id
if has_suspicious_connection(player_id) then
status_id = data.player_status.suspicious.id
else
status_id = data.player_status.default.id
end
if not data.set_player_status(player_id, executor_id, status_id, reason) then
return false, 'ERROR setting player status'
end
log('action', 'setting verified privs for %s', player_name)
if not debug_mode then
minetest.set_player_privs(player_name, settings.verified_privs)
end
local player = minetest.get_player_by_name(player_name)
if player then
log('action', 'moving %s to spawn', player_name)
if not debug_mode then
player:set_pos(settings.spawn_pos)
end
end
if reason then
log('action', '%s verified %s because %s', caller, player_name, reason)
else
log('action', '%s verified %s', caller, player_name)
end
return true, ('Verified %s'):format(player_name)
end
})
register_chatcommand('unverify', {
description='Unverify a player, revoking privs and putting them back in jail.',
params='<player_name> [<reason>]',
privs={[mod_priv]=true},
func=function(caller, params)
local player_id, player_name, player_status, reason = parse_player_status_params(params)
if not player_id then
return false, reason
end
if not table_contains({
data.player_status.default.id,
data.player_status.suspicious.id,
}, player_status.id) then
return false, ('Cannot unverify %s w/ status %s'):format(player_name, player_status.name)
end
local executor_id = data.get_player_id(caller)
if not executor_id then
return false, 'ERROR: could not get executor ID?'
end
if not data.set_player_status(player_id, executor_id, data.player_status.unverified.id, reason) then
return false, 'ERROR setting player status'
end
log('action', 'setting unverified privs for %s', player_name)
if not debug_mode then
minetest.set_player_privs(player_name, settings.unverified_privs)
end
local player = minetest.get_player_by_name(player_name)
if player then
log('action', 'moving %s to unverified area', player_name)
if not debug_mode then
player:set_pos(settings.unverified_spawn_pos)
end
end
if reason then
log('action', '%s unverified %s because %s', caller, player_name, reason)
else
log('action', '%s unverified %s', caller, player_name)
end
return true, ('Unverified %s'):format(player_name)
end
})
local kick_privs = {[mod_priv]=true}
if kick_priv and kick_priv ~= mod_priv then
kick_privs = nil
end
override_chatcommand('kick', {
description='Kick a player',
params='<player_name> [<reason>]',
privs=kick_privs or {},
func=function(caller, params)
if not kick_privs then
if not (minetest.check_player_privs(caller, {[mod_priv]=true}) or
minetest.check_player_privs(caller, {[kick_priv]=true})) then
return false, "You lack sufficient privileges to run this command"
end
end
local player_id, player_name, _, reason = parse_player_status_params(params)
if not player_id then
return false, reason
end
local player = minetest.get_player_by_name(player_name)
if not player then
return false, ("Player %s not in game!"):format(player_name)
end
safe_kick_player(caller, player, reason)
local executor_id = data.get_player_id(caller)
if not executor_id then
return false, 'ERROR: could not get executor ID?'
end
if not data.set_player_status(player_id, executor_id, data.player_status.kicked.id, reason, nil, true) then
return false, 'ERROR logging player status'
end
if reason then
log('action', '%s kicked %s because %s', caller, player_name, reason)
else
log('action', '%s kicked %s', caller, player_name)
end
return true, ('Kicked %s'):format(player_name)
end
})
override_chatcommand('ban', {
description='Ban a player. Timespan and reason are optional.',
params='<player_name> [<timespan>] [<reason>]',
privs={[mod_priv]=true},
func=function(caller, params)
local player_id, player_name, player_status, reason = parse_player_status_params(params)
if not player_id then
return false, reason
end
local now = os.time()
local timespan
local expires
if reason then
local first = reason:match('^(%S+)')
timespan = parse_timespan(first)
if timespan then
reason = reason:sub(first:len() + 2)
expires = now + timespan
end
end
if not table_contains({
data.player_status.default.id,
data.player_status.unverified.id,
data.player_status.suspicious.id,
}, player_status.id) then
return false, ('Cannot ban %s w/ status %s'):format(player_name, player_status.name)
end
local player = minetest.get_player_by_name(player_name)
if player then
safe_kick_player(caller, player, reason)
end
local executor_id = data.get_player_id(caller)
if not executor_id then
return false, 'ERROR: could not get executor ID?'
end
if not data.set_player_status(player_id, executor_id, data.player_status.banned.id, reason, expires) then
return false, 'ERROR setting player status'
end
if expires then
if reason then
log('action', '%s banned %s until %s because %s', caller, player_name, iso_date(expires), reason)
else
log('action', '%s banned %s until %s', caller, player_name, iso_date(expires))
end
else
if reason then
log('action', '%s banned %s because %s', caller, player_name, reason)
else
log('action', '%s banned %s', caller, player_name)
end
end
local ipstr = data.fumble_about_for_an_ip(player_name)
if ipstr then
local ipint = lib_ip.ipstr_to_ipint(ipstr)
local ip_status = data.get_ip_status(ipint)
if not ip_status or ip_status.name == data.ip_status.default.name then
-- mark the IP the player is on as suspicious
local ip_suspect_expires
if expires then
ip_suspect_expires = expires
else
ip_suspect_expires = now + parse_timespan('1w')
end
local ip_suspect_reason = ('Marked suspicious while banning %s'):format(player_name)
if not data.set_ip_status(ipint, executor_id, data.ip_status.suspicious.id, ip_suspect_reason, ip_suspect_expires) then
return false, 'ERROR setting ip status'
end
end
end
return true, ('Banned %s'):format(player_name)
end
})
override_chatcommand('unban', {
description='Unban a player',
params='<player_name> [<reason>]',
privs={[mod_priv]=true},
func=function(caller, params)
local player_id, player_name, player_status, reason = parse_player_status_params(params)
if not player_id then
return false, reason
end
if player_status.id ~= data.player_status.banned.id then
return false, ('Player %s is not banned!'):format(player_name)
end
local executor_id = data.get_player_id(caller)
if not executor_id then
return false, 'ERROR: could not get executor ID?'
end
local status_id
if has_suspicious_connection(player_id) then
status_id = data.player_status.suspicious.id
else
status_id = data.player_status.default.id
end
if not data.set_player_status(player_id, executor_id, status_id, reason) then
return false, 'ERROR setting player status'
end
if reason then
log('action', '%s unbanned %s because %s', caller, player_name, reason)
else
log('action', '%s unbanned %s', caller, player_name)
end
return true, ('Unbanned %s'):format(player_name)
end
})
register_chatcommand('whitelist', {
description='Whitelist a player',
params='<player_name> [<reason>]',
privs={[admin_priv]=true},
func=function(caller, params)
local player_id, player_name, player_status, reason = parse_player_status_params(params)
if not player_id then
return false, reason
end
if not table_contains({
data.player_status.default.id,
data.player_status.unverified.id,
data.player_status.suspicious.id,
}, player_status.id) then
return false, ('Cannot whitelist %s w/ status %s'):format(player_name, player_status.name)
end
local executor_id = data.get_player_id(caller)
if not executor_id then
return false, 'ERROR: could not get executor ID?'
end
if not data.set_player_status(player_id, executor_id, data.player_status.whitelisted.id, reason) then
return false, 'ERROR setting player status'
end
if reason then
log('action', '%s whitelisted %s because %s', caller, player_name, reason)
else
log('action', '%s whitelisted %s', caller, player_name)
end
return true, ('Whitelisted %s'):format(player_name)
end
})
register_chatcommand('unwhitelist', {
description='Remove whitelist status from a player',
params='<player_name> [<reason>]',
privs={[admin_priv]=true},
func=function(caller, params)
local player_id, player_name, player_status, reason = parse_player_status_params(params)
if not player_id then
return false, reason
end
if player_status.id ~= data.player_status.whitelisted.id then
return false, ('Player %s is not whitelisted!'):format(player_name)
end
local executor_id = data.get_player_id(caller)
if not executor_id then
return false, 'ERROR: could not get executor ID?'
end
if not data.set_player_status(player_id, executor_id, data.player_status.default.id, reason) then
return false, 'ERROR setting player status'
end
if reason then
log('action', '%s unwhitelisted %s because %s', caller, player_name, reason)
else
log('action', '%s unwhitelisted %s', caller, player_name)
end
return true, ('Unwhitelisted %s'):format(player_name)
end
})
register_chatcommand('suspect', {
description='Mark a player as suspicious',
params='<player_name> [<reason>]',
privs={[mod_priv]=true},
func=function(caller, params)
local player_id, player_name, player_status, reason = parse_player_status_params(params)
if not player_id then
return false, reason
end
if player_status.id ~= data.player_status.default.id then
return false, ('Cannot whitelist %s w/ status %s'):format(player_name, player_status.name)
end
local executor_id = data.get_player_id(caller)
if not executor_id then
return false, 'ERROR: could not get executor ID?'
end
if not data.set_player_status(player_id, executor_id, data.player_status.suspicious.id, reason) then
return false, 'ERROR setting player status'
end
if reason then
log('action', '%s suspected %s because %s', caller, player_name, reason)
else
log('action', '%s suspected %s', caller, player_name)
end
return true, ('Suspected %s'):format(player_name)
end
})
register_chatcommand('unsuspect', {
description='Unmark a player as suspicious',
params='<player_name> [<reason>]',
privs={[mod_priv]=true},
func=function(caller, params)
local player_id, player_name, player_status, reason = parse_player_status_params(params)
if not player_id then
return false, reason
end
if player_status.id ~= data.player_status.suspicious.id then
return false, ('Player %s is not suspicious!'):format(player_name)
end
local executor_id = data.get_player_id(caller)
if not executor_id then
return false, 'ERROR: could not get executor ID?'
end
if not data.set_player_status(player_id, executor_id, data.player_status.default.id, reason) then
return false, 'ERROR setting player status'
end
if reason then
log('action', '%s unsuspected %s because %s', caller, player_name, reason)
else
log('action', '%s unsuspected %s', caller, player_name)
end
return true, ('Unsuspected %s'):format(player_name)
end
})
----------------- SET IP STATUS COMMANDS -----------------
local function parse_ip_status_params(params)
local ipstr, reason = params:match('^(%d+%.%d+%.%d+%.%d+)%s+(.*)$')
if not ipstr then
ipstr = params:match('^(%d+%.%d+%.%d+%.%d+)$')
end
if not ipstr or not lib_ip.is_valid_ip(ipstr) then
return nil, nil, nil, ('Invalid argument(s): %q'):format(params)
end
local ipint = lib_ip.ipstr_to_ipint(ipstr)
data.register_ip(ipint)
local ip_status = data.get_ip_status(ipint, true)
return ipint, ipstr, ip_status, reason
end
register_chatcommand('ip_trust', {
description='Mark an IP as trusted - connections will bypass suspicious network checks',
params='<IP> [<reason>]',
privs={[admin_priv]=true},
func=function(caller, params)
local ipint, ipstr, ip_status, reason = parse_ip_status_params(params)
if not ipint then
return false, reason
end
if not table_contains({
data.ip_status.default.id,
data.ip_status.suspicious.id,
}, ip_status.id) then
return false, ('Cannot trust IP w/ status %s'):format(ip_status.name)
end
local executor_id = data.get_player_id(caller)
if not executor_id then
return false, 'ERROR: could not get executor ID?'
end
if not data.set_ip_status(ipint, executor_id, data.ip_status.trusted.id, reason) then
return false, 'ERROR setting IP status'
end
if reason then
log('action', '%s trusted %s because %s', caller, ipstr, reason)
else
log('action', '%s trusted %s', caller, ipstr)
end
return true, ('Trusted %s'):format(ipstr)
end
})
register_chatcommand('ip_untrust', {
description='Remove trusted status from an IP',
params='<IP> [<reason>]',
privs={[admin_priv]=true},
func=function(caller, params)
local ipint, ipstr, ip_status, reason = parse_ip_status_params(params)
if not ipint then
return false, reason
end
if ip_status.id ~= data.player_status.trusted.id then
return false, ('IP %s is not trusted!'):format(ipstr)
end
local executor_id = data.get_player_id(caller)
if not executor_id then
return false, 'ERROR: could not get executor ID?'
end
if not data.set_ip_status(ipint, executor_id, data.ip_status.default.id, reason) then
return false, 'ERROR setting IP status'
end
if reason then
log('action', '%s untrusted %s because %s', caller, ipstr, reason)
else
log('action', '%s untrusted %s', caller, ipstr)
end
return true, ('Untrusted %s'):format(ipstr)
end
})
register_chatcommand('ip_suspect', {
description='Mark an IP as suspicious.',
params='<IP> [<reason>]',
privs={[mod_priv]=true},
func=function(caller, params)
local ipint, ipstr, ip_status, reason = parse_ip_status_params(params)
if not ipint then
return false, reason
end
if ip_status.id ~= data.ip_status.default.id then
return false, ('Cannot suspect IP w/ status %s'):format(ip_status.name)
end
local executor_id = data.get_player_id(caller)
if not executor_id then
return false, 'ERROR: could not get executor ID?'
end
if not data.set_ip_status(ipint, executor_id, data.ip_status.suspicious.id, reason) then
return false, 'ERROR setting IP status'
end
if reason then
log('action', '%s suspected %s because %s', caller, ipstr, reason)
else
log('action', '%s suspected %s', caller, ipstr)
end
return true, ('Suspected %s'):format(ipstr)
end
})
register_chatcommand('ip_unsuspect', {
description='Unmark an IP as suspicious',
params='<IP> [<reason>]',
privs={[mod_priv]=true},
func=function(caller, params)
local ipint, ipstr, ip_status, reason = parse_ip_status_params(params)
if not ipint then
return false, reason
end
if ip_status.id ~= data.player_status.suspicious.id then
return false, ('IP %s is not suspicious!'):format(ipstr)
end
local executor_id = data.get_player_id(caller)
if not executor_id then
return false, 'ERROR: could not get executor ID?'
end
if not data.set_ip_status(ipint, executor_id, data.ip_status.default.id, reason) then
return false, 'ERROR setting IP status'
end
if reason then
log('action', '%s unsuspected %s because %s', caller, ipstr, reason)
else
log('action', '%s unsuspected %s', caller, ipstr)
end
return true, ('Unsuspected %s'):format(ipstr)
end
})
register_chatcommand('ip_block', {
description='Block an IP from connecting.',
params='<IP> [<reason>]',
privs={[admin_priv]=true},
func=function(caller, params)
local ipint, ipstr, ip_status, reason = parse_ip_status_params(params)
if not ipint then
return false, reason
end
local timespan
local expires
if reason then
local first = reason:match('^(%S+)')
timespan = parse_timespan(first)
if timespan then
reason = reason:sub(first:len() + 2)
expires = os.time() + timespan
end
end
if not table_contains({
data.ip_status.default.id,
data.ip_status.suspicious.id,
}, ip_status.id) then
return false, ('Cannot block IP w/ status %s'):format(ip_status.name)
end
local executor_id = data.get_player_id(caller)
if not executor_id then
return false, 'ERROR: could not get executor ID?'
end
if not data.set_ip_status(ipint, executor_id, data.ip_status.blocked.id, reason, expires) then
return false, 'ERROR setting IP status'
end
util.safe_kick_ip(ipstr)
if expires then
if reason then
log('action', '%s blocked %s until %s because %s', caller, ipstr, iso_date(expires), reason)
else
log('action', '%s blocked %s until %s', caller, ipstr, iso_date(expires))
end
else
if reason then
log('action', '%s blocked %s because %s', caller, ipstr, reason)
else
log('action', '%s blocked %s', caller, ipstr)
end
end
return true, ('Blocked %s'):format(ipstr)
end
})
register_chatcommand('ip_unblock', {
description='Unblock an IP',
params='<IP> [<reason>]',
privs={[admin_priv]=true},
func=function(caller, params)
local ipint, ipstr, ip_status, reason = parse_ip_status_params(params)
if not ipint then
return false, reason
end
if ip_status.id ~= data.ip_status.blocked.id then
return false, 'IP is not blocked!'
end
local executor_id = data.get_player_id(caller)
if not executor_id then
return false, 'ERROR: could not get executor ID?'
end
if not data.set_ip_status(ipint, executor_id, data.ip_status.default.id, reason) then
return false, 'ERROR setting IP status'
end
if reason then
log('action', '%s unblocked %s because %s', caller, ipstr, reason)
else
log('action', '%s unblocked %s', caller, ipstr)
end
return true, ('Unblocked %s'):format(ipstr)
end
})
----------------- SET ASN STATUS COMMANDS -----------------
local function parse_asn_status_params(params)
local asnstr, reason = params:match('^A?(%d+)%s+(.*)$')
if not asnstr then
asnstr = params:match('^A?(%d+)$')
end
if not asnstr then
return nil, nil, nil, ('Invalid argument(s): %q'):format(params)
end
local asn = tonumber(asnstr)
local description = lib_asn.get_description(asn)
if description == lib_asn.invalid_asn_description then
return nil, nil, nil, ('Not a valid ASN: %q'):format(params)
end
data.register_asn(asn)
local asn_status = data.get_asn_status(asn, true)
return asn, description, asn_status, reason
end
register_chatcommand('asn_suspect', {
description='Mark an ASN as suspicious.',
params='<ASN> [<reason>]',
privs={[mod_priv]=true},
func=function(caller, params)
local asn, description, asn_status, reason = parse_asn_status_params(params)
if not asn then
return false, reason
end
if asn_status.id ~= data.asn_status.default.id then
return false, ('Cannot suspect ASN w/ status %s'):format(asn_status.name)
end
local executor_id = data.get_player_id(caller)
if not executor_id then
return false, 'ERROR: could not get executor ID?'
end
if not data.set_asn_status(asn, executor_id, data.asn_status.suspicious.id, reason) then
return false, 'ERROR setting ASN status'
end
if reason then
log('action', '%s suspected A%s because %s', caller, asn, reason)
else
log('action', '%s suspected A%s', caller, asn)
end
return true, ('Suspected A%s (%s)'):format(asn, description)
end
})
register_chatcommand('asn_unsuspect', {
description='Unmark an ASN as suspicious.',
params='<ASN> [<reason>]',
privs={[mod_priv]=true},
func=function(caller, params)
local asn, description, asn_status, reason = parse_asn_status_params(params)
if not asn then
return false, reason
end
if asn_status.id ~= data.asn_status.suspicious.id then
return false, ('A%s is not suspicious!'):format(asn)
end
local executor_id = data.get_player_id(caller)
if not executor_id then
return false, 'ERROR: could not get executor ID?'
end
if not data.set_asn_status(asn, executor_id, data.asn_status.default.id, reason) then
return false, 'ERROR setting ASN status'
end
if reason then
log('action', '%s unsuspected A%s because %s', caller, asn, reason)
else
log('action', '%s unsuspected A%s', caller, asn)
end
return true, ('Unsuspected A%s (%q)'):format(asn, description)
end
})
register_chatcommand('asn_block', {
description='Block an ASN. Duration and reason optional.',
params='<ASN> [<duration>] [<reason>]',
privs={[admin_priv]=true},
func=function(caller, params)
local asn, description, asn_status, reason = parse_asn_status_params(params)
if not asn then
return false, reason
end
local timespan
local expires
if reason then
local first = reason:match('^(%S+)')
timespan = parse_timespan(first)
if timespan then
reason = reason:sub(first:len() + 2)
expires = os.time() + timespan
end
end
if not table_contains({
data.asn_status.default.id,
data.asn_status.suspicious.id,
}, asn_status.id) then
return false, ('Cannot block ASN w/ status %s'):format(asn_status.name)
end
local executor_id = data.get_player_id(caller)
if not executor_id then
return false, 'ERROR: could not get executor ID?'
end
if not data.set_asn_status(asn, executor_id, data.asn_status.blocked.id, reason, expires) then
return false, 'ERROR setting ASN status'
end
util.safe_kick_asn(asn)
if expires then
if reason then
log('action', '%s blocked A%s until %s because %s', caller, asn, iso_date(expires), reason)
else
log('action', '%s blocked A%s until %s ', caller, asn, iso_date(expires))
end
else
if reason then
log('action', '%s blocked A%s because %s', caller, asn, reason)
else
log('action', '%s blocked A%s', caller, asn)
end
end
return true, ('Blocked A%s (%q)'):format(asn, description)
end
})
register_chatcommand('asn_unblock', {
description='Unblock an ASN.',
params='<ASN> [<reason>]',
privs={[admin_priv]=true},
func=function(caller, params)
local asn, description, asn_status, reason = parse_asn_status_params(params)
if not asn then
return false, reason
end
if asn_status.id ~= data.asn_status.blocked.id then
return false, 'ASN is not blocked!'
end
local executor_id = data.get_player_id(caller)
if not executor_id then
return false, 'ERROR: could not get executor ID?'
end
if not data.set_asn_status(asn, executor_id, data.asn_status.default.id, reason) then
return false, 'ERROR setting IP status'
end
if reason then
log('action', '%s unblocked A%s because %s', caller, asn, reason)
else
log('action', '%s unblocked A%s', caller, asn)
end
return true, ('Unblocked A%s (%q)'):format(asn, description)
end
})
---------------- GET LOGS ---------------
register_chatcommand('player_status', {
description='shows the status log of a player',
params='<player_name> [<number>]',
privs={[mod_priv]=true},
func=function(caller, params)
local name, numberstr = string.match(params, '^([%a%d_-]+)%s+(%d+)$')
if not name then
name = string.match(params, '^([%a%d_-]+)$')
end
if not name then
return false, 'invalid arguments'
end
local player_id, name = data.get_player_id(name)
if not player_id then
return false, 'unknown player'
end
local rows = data.get_player_status_log(player_id)
if not rows then
return false, 'An error occurred (see server logs)'
end
if #rows == 0 then
return true, ('No records found for %s.'):format(name)
end
local starti
if numberstr then
local number = tonumber(numberstr)
starti = math.max(1, #rows - number)
else
starti = 1
end
chat_send_player(caller, ('Records for %s'):format(name))
for index = starti,#rows do
local row = rows[index]
local status_name = data.player_status_name[row.status_id] or data.player_status.default.name
local status_color = data.player_status_color[row.status_id] or data.player_status.default.color
local message = ('%s: %s set status to %s.'):format(
iso_date(row.timestamp),
row.executor_name,
minetest.colorize(status_color, status_name)
)
local reason = row.reason
if reason and reason ~= '' then
message = ('%s Reason: %s'):format(message, reason)
end
local expires = row.expires
if expires then
message = ('%s Expires: %s'):format(message, iso_date(expires))
end
chat_send_player(caller, message)
end
return true
end
})
register_chatcommand('ban_record', {
description = 'shows relevant info for a player',
params = '<player_name>',
privs = { [mod_priv] = true },
func = function(caller, params)
local player_name = params:match('^([a-zA-Z0-9_-]+)$')
if not player_name or player_name:len() > 20 then
return false, 'Invalid argument'
end
local player_id
player_id, player_name = data.get_player_id(player_name)
if not player_id then
return false, 'Unknown player'
end
local ipstr = data.fumble_about_for_an_ip(player_name, player_id)
local ipint = lib_ip.ipstr_to_ipint(ipstr)
local asn, asn_description = lib_asn.lookup(ipint)
chat_send_player(caller, "Ban record for %s", player_name)
local rows = data.get_player_cluster(player_id)
if not rows then return false, 'An error occurred (see server logs)' end
if #rows > 0 then
local clustered = {}
for _, row in ipairs(rows) do
local color = data.player_status_color[row.player_status_id] or data.player_status.default.color
table.insert(clustered, minetest.colorize(color, row.player_name))
end
chat_send_player(caller, "Accounts associated by IP:")
chat_send_player(caller, table.concat(clustered, ', '))
end
rows = data.get_asn_associations(asn, os.time() - parse_timespan('1y'))
if not rows then return false, 'An error occurred (see server logs)' end
if #rows > 0 then
local assocs = {}
for _, row in ipairs(rows) do
local color = data.player_status_color[row.player_status_id] or data.player_status.default.color
table.insert(assocs, minetest.colorize(color, row.player_name))
end
chat_send_player(caller, "Flagged accounts on A%s (%s):", asn, asn_description)
chat_send_player(caller, table.concat(assocs, ', '))
end
rows = data.get_player_status_log(player_id)
if not rows then
return false, 'An error occurred (see server logs)'
end
if #rows > 0 then
chat_send_player(caller, "Account status:")
end
for index = 1,#rows do
local row = rows[index]
local status_name = data.player_status_name[row.status_id] or data.player_status.default.name
local status_color = data.player_status_color[row.status_id] or data.player_status.default.color
local message = ('%s: %s set status to %s.'):format(
iso_date(row.timestamp),
row.executor_name,
minetest.colorize(status_color, status_name)
)
local reason = row.reason
if reason and reason ~= '' then
message = ('%s Reason: %s'):format(message, reason)
end
local expires = row.expires
if expires then
message = ('%s Expires: %s'):format(message, iso_date(expires))
end
chat_send_player(caller, message)
end