-
Notifications
You must be signed in to change notification settings - Fork 0
/
calladmin_block.sp
834 lines (493 loc) · 17.5 KB
/
calladmin_block.sp
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
/* Dependencies */
#include <sourcemod>
#include <regex>
#include "include/calladmin"
#pragma semicolon 1
#pragma newdecls required
#define PLUGIN_VERSION "2.3"
#define PREFIX "[SM]"
#define PREFIXN "[SM]"
/* Plugin Info */
public Plugin myinfo = {
name = "CallAdmin - Block",
author = "ampere",
description = "CallAdmin module to block people from reporting.",
version = PLUGIN_VERSION,
url = "https://github.com/maxijabase/calladmin-block"
}
/* Globals */
Database g_Database;
Regex g_rSteamIdRegex;
int g_iCurrentUserInPanel;
int g_iUserUnbanTime[MAXPLAYERS + 1];
char g_cCurrentPanelSteamID[32];
/* Plugin Start */
public void OnPluginStart() {
CreateConVar("sm_calladmin_block_version", PLUGIN_VERSION, "Plugin Version.", FCVAR_DONTRECORD | FCVAR_NOTIFY);
RegAdminCmd("sm_calladmin_block_add", CMD_Add, ADMFLAG_GENERIC, "Add people to the block list of CallAdmin.");
RegAdminCmd("sm_cb_add", CMD_Add, ADMFLAG_GENERIC, "Add people to the block list of CallAdmin.");
RegAdminCmd("sm_calladmin_block_remove", CMD_Remove, ADMFLAG_GENERIC, "Remove people from the block list of CallAdmin.");
RegAdminCmd("sm_cb_remove", CMD_Remove, ADMFLAG_GENERIC, "Remove people from the block list of CallAdmin.");
RegAdminCmd("sm_calladmin_block_list", CMD_List, ADMFLAG_GENERIC, "List all people in CallAdmin's block list.");
RegAdminCmd("sm_cb_list", CMD_List, ADMFLAG_GENERIC, "List all people in CallAdmin's block list.");
g_rSteamIdRegex = CompileRegex("^STEAM_[\\d{1}]:[\\d{1}]:[\\d]+$");
LoadTranslations("calladmin_block.phrases");
LoadTranslations("common.phrases");
Database.Connect(SQL_ConnectCallback, "calladmin_block");
}
/* Database */
public void SQL_ConnectCallback(Database db, const char[] error, any data) {
if (db == null) {
SetFailState("%s %s", PREFIXN, error);
return;
}
g_Database = db;
CreateTables();
}
void CreateTables() {
char query[512];
Format(query, sizeof(query), "CREATE TABLE IF NOT EXISTS calladmin_block "...
"(id INT NOT NULL AUTO_INCREMENT, "...
"steam_id VARCHAR(64) UNIQUE, "...
"time_start BIGINT, time_end BIGINT, alias VARCHAR(128), "...
"PRIMARY KEY(id));");
g_Database.Query(SQL_TablesCallback, query);
}
public void SQL_TablesCallback(Database db, DBResultSet results, const char[] error, any data) {
if (db == null || results == null) {
SetFailState("%s %s", PREFIXN, error);
delete results;
return;
}
else {
for (int i = 1; i <= MaxClients; i++) {
if (IsClientInGame(i) && IsClientAuthorized(i)) {
OnClientAuthorized(i);
}
}
}
delete results;
}
/* Cache User Ban Times */
public void OnClientAuthorized(int client) {
if (!g_Database) {
return;
}
char steamID[32];
if (GetClientAuthId(client, AuthId_Steam2, steamID, sizeof(steamID))) {
char query[512];
Format(query, sizeof(query), "SELECT time_end FROM calladmin_block WHERE steam_id = '%s'", steamID);
g_Database.Query(SQL_OnClientConnectedCacheCallback, query, GetClientUserId(client));
}
}
public void SQL_OnClientConnectedCacheCallback(Database db, DBResultSet results, const char[] error, int userid) {
if (db == null || results == null) {
ThrowError("%s %s", PREFIXN, error);
delete results;
return;
}
int client = GetClientOfUserId(userid);
int time_end;
if (!results.FetchRow()) {
g_iUserUnbanTime[client] = -1;
delete results;
return;
} else {
results.FieldNameToNum("time_end", time_end);
g_iUserUnbanTime[client] = results.FetchInt(time_end);
}
delete results;
}
/* Add Command */
public Action CMD_Add(int client, int args) {
if (g_Database == null) {
ReplyToCommand(client, "%s Database failed to connect! Try reloading plugin.");
return Plugin_Handled;
}
char arg[128], arg1[64], arg2[64], arg3[64];
GetCmdArgString(arg, sizeof(arg));
char buf[3][32];
ExplodeString(arg, " ", buf, sizeof(buf), sizeof(buf[]));
strcopy(arg1, sizeof(arg1), buf[0]);
strcopy(arg2, sizeof(arg1), buf[1]);
strcopy(arg3, sizeof(arg1), buf[2]);
char targetSteamID[64];
bool isTarget;
int target;
if (args < 2 || arg2[0] == '\0') {
ReplyToCommand(client, "%s Usage: sm_calladmin_block_add <user | STEAM_0:X:XXXXX> <time> [alias]", PREFIX);
return Plugin_Handled;
}
if (!SimpleRegexMatch(arg2, "^[0-9]*$")) {
ReplyToCommand(client, "%s %t", PREFIX, "Incorrect Time");
return Plugin_Handled;
}
int banTime = ProcessBanTime(StringToInt(arg2));
if (banTime < 0) {
ReplyToCommand(client, "%s %t", PREFIX, "Time Too Long");
return Plugin_Handled;
}
if (MatchRegex(g_rSteamIdRegex, arg1)) {
strcopy(targetSteamID, sizeof(targetSteamID), arg1);
isTarget = false;
} else {
target = FindTarget(client, arg1, true, false);
if (target == -1) {
return Plugin_Handled;
} else {
if (!GetClientAuthId(target, AuthId_Steam2, targetSteamID, sizeof(targetSteamID))) {
ReplyToCommand(client, "%s %t", PREFIX, "Error Get Auth ID");
return Plugin_Handled;
}
isTarget = true;
}
}
char query[512], dbAlias[65];
if (isTarget) {
if (arg3[0] != '\0') {
g_Database.Escape(arg3, dbAlias, sizeof(dbAlias));
} else {
char name[MAX_NAME_LENGTH];
GetClientName(target, name, sizeof(name));
g_Database.Escape(name, dbAlias, sizeof(dbAlias));
}
g_Database.Format(query, sizeof(query), "INSERT INTO calladmin_block (steam_id, time_start, time_end, alias) "...
"VALUES ('%s', %d, %d, '%s') "...
"ON DUPLICATE KEY UPDATE "...
"steam_id = '%s', "...
"time_start = %d, "...
"time_end = %d, "...
"alias = '%s';", targetSteamID, GetTime(), banTime, dbAlias, targetSteamID, GetTime(), banTime, dbAlias);
} else {
if (arg3[0] != '\0') {
g_Database.Escape(arg3, dbAlias, sizeof(dbAlias));
g_Database.Format(query, sizeof(query), "INSERT INTO calladmin_block (steam_id, time_start, time_end, alias) "...
"VALUES ('%s', %d, %d, '%s') "...
"ON DUPLICATE KEY UPDATE "...
"steam_id = '%s', "...
"time_start = %d, "...
"time_end = %d, "...
"alias = '%s';", targetSteamID, GetTime(), banTime, dbAlias, targetSteamID, GetTime(), banTime, dbAlias);
} else {
g_Database.Format(query, sizeof(query), "INSERT INTO calladmin_block (steam_id, time_start, time_end) "...
"VALUES ('%s', %d, %d) "...
"ON DUPLICATE KEY UPDATE "...
"steam_id = '%s', "...
"time_start = %d, "...
"time_end = %d;", targetSteamID, GetTime(), banTime, targetSteamID, GetTime(), banTime);
}
}
DataPack pack = new DataPack();
pack.WriteCell(!client ? client : GetClientUserId(client));
pack.WriteCell(isTarget);
if (isTarget)
pack.WriteCell(GetClientUserId(target));
g_Database.Query(SQL_AddCallback, query, pack);
InGameAdd(target, banTime, targetSteamID, isTarget);
return Plugin_Handled;
}
public void SQL_AddCallback(Database db, DBResultSet results, const char[] error, DataPack pack) {
pack.Reset();
int client = pack.ReadCell();
bool isTarget = pack.ReadCell();
int target;
if (isTarget) {
target = GetClientOfUserId(pack.ReadCell());
}
if (client != 0) {
client = GetClientOfUserId(client);
}
delete pack;
if (db == null || results == null) {
ThrowError("%s %s", PREFIXN, error);
delete results;
return;
}
if (isTarget) {
!client ? PrintToServer("%s %t", PREFIX, "Steam ID Added Target", target) : PrintToChat(client, "%s %t", PREFIX, "Steam ID Added Target", target);
} else {
!client ? PrintToServer("%s %t", PREFIX, "Steam ID Added") : PrintToChat(client, "%s %t", PREFIX, "Steam ID Added");
}
delete results;
}
/* Remove Command */
public Action CMD_Remove(int client, int args) {
if (g_Database == null) {
ReplyToCommand(client, "%s Database failed to connect! Try reloading plugin.");
return Plugin_Handled;
}
if (!args) {
ReplyToCommand(client, "%s Usage: sm_calladmin_block_remove <user | STEAM_0:X:XXXXX>", PREFIX);
return Plugin_Handled;
}
char arg1[64];
GetCmdArgString(arg1, sizeof(arg1));
char targetSteamID[32];
bool isTarget;
int target;
if (MatchRegex(g_rSteamIdRegex, arg1)) {
strcopy(targetSteamID, sizeof(targetSteamID), arg1);
isTarget = false;
} else {
target = FindTarget(client, arg1, true, false);
if (target == -1) {
return Plugin_Handled;
} else {
if (!GetClientAuthId(target, AuthId_Steam2, targetSteamID, sizeof(targetSteamID))) {
ReplyToCommand(client, "%s %t", PREFIX, "Error Get Auth ID");
return Plugin_Handled;
}
isTarget = true;
}
}
char query[512];
Format(query, sizeof(query), "DELETE FROM calladmin_block WHERE steam_id = '%s'", targetSteamID);
DataPack pack = new DataPack();
pack.WriteCell(!client ? client : GetClientUserId(client));
pack.WriteCell(isTarget);
if (isTarget) {
pack.WriteCell(GetClientUserId(target));
}
g_Database.Query(SQL_RemoveCallback, query, pack);
InGameRemove(target, targetSteamID, isTarget);
return Plugin_Handled;
}
public void SQL_RemoveCallback(Database db, DBResultSet results, const char[] error, DataPack pack) {
pack.Reset();
int client = pack.ReadCell();
bool isTarget = pack.ReadCell();
int target;
if (isTarget) {
target = GetClientOfUserId(pack.ReadCell());
}
if (client != 0) {
client = GetClientOfUserId(client);
}
delete pack;
if (db == null || results == null) {
ThrowError("%s %s", PREFIXN, error);
delete results;
return;
}
if (results.AffectedRows) {
if (isTarget) {
!client ? PrintToServer("%s %t", PREFIX, "Steam ID Removed Target", target) : PrintToChat(client, "%s %t", PREFIX, "Steam ID Removed Target", target);
} else {
!client ? PrintToServer("%s %t", PREFIX, "Steam ID Removed") : PrintToChat(client, "%s %t", PREFIX, "Steam ID Removed");
}
} else {
if (isTarget) {
!client ? PrintToServer("%s %t", PREFIX, "Steam ID Not In Blocklist Target", target) : PrintToChat(client, "%s %t", PREFIX, "Steam ID Not In Blocklist Target", target);
} else {
!client ? PrintToServer("%s %t", PREFIX, "Steam ID Not In Blocklist") : PrintToChat(client, "%s %t", PREFIX, "Steam ID Not In Blocklist");
}
}
delete results;
}
/* List Command */
public Action CMD_List(int client, int args) {
if (g_Database == null) {
ReplyToCommand(client, "%s Database failed to connect! Try reloading plugin.");
return Plugin_Handled;
}
if (!client) {
ReplyToCommand(client, "%s This command cannot be executed from the console.", PREFIX);
return Plugin_Handled;
}
ShowBanList(client);
return Plugin_Handled;
}
void ShowBanList(int client) {
char query[512];
Format(query, sizeof(query), "SELECT steam_id, alias FROM calladmin_block");
g_Database.Query(SQL_ListCallback, query, GetClientUserId(client));
}
public void SQL_ListCallback(Database db, DBResultSet results, const char[] error, int userid) {
if (db == null || results == null) {
ThrowError("%s %s", PREFIXN, error);
delete results;
return;
}
int client = GetClientOfUserId(userid);
if (!results.FetchRow()) {
PrintToChat(client, "%s %t", PREFIX, "No Information");
delete results;
return;
}
int steamidCol, aliasCol, count;
char steamID[32], alias[64], menuLine[128], idx[2];
results.FieldNameToNum("steam_id", steamidCol);
results.FieldNameToNum("alias", aliasCol);
Menu menu = new Menu(menuHandler, MENU_ACTIONS_ALL);
menu.SetTitle("%t", "List of Steam IDs");
do {
count++;
IntToString(count, idx, sizeof(idx));
results.FetchString(steamidCol, steamID, sizeof(steamID));
results.FetchString(aliasCol, alias, sizeof(alias));
alias[0] ? Format(menuLine, sizeof(menuLine), "%s (%s)", steamID, alias) : Format(menuLine, sizeof(menuLine), "%s", steamID);
menu.AddItem(idx, menuLine);
} while (results.FetchRow());
menu.ExitButton = true;
menu.Display(client, 20);
delete results;
}
public int menuHandler(Menu menu, MenuAction action, int param1, int param2) {
switch (action) {
case MenuAction_Select: {
g_iCurrentUserInPanel = param2;
ShowBannedClientPanel(GetClientUserId(param1), param2);
}
case MenuAction_End: {
delete menu;
}
}
}
void ShowBannedClientPanel(int userid, int offset) {
char query[512];
Format(query, sizeof(query), "SELECT * FROM calladmin_block LIMIT %i,1", offset);
g_Database.Query(SQL_BannedClientPanelCallback, query, userid);
}
public void SQL_BannedClientPanelCallback(Database db, DBResultSet results, const char[] error, int userid) {
if (db == null || results == null) {
ThrowError("%s %s", PREFIXN, error);
delete results;
return;
}
if (!results.FetchRow()) {
delete results;
return;
}
int client = GetClientOfUserId(userid);
int steamIdCol, timeStartCol, timeEndCol, aliasCol;
char panelTitle[64];
char steamID[32], timeStart[32], timeEnd[32], alias[32];
char timeStartFormatted[64], timeEndFormatted[64];
char line1[32], line2[32], line3[32], line4[32];
char exitString[16], backString[16], removeUser[32];
Panel banPanel = new Panel();
results.FieldNameToNum("steam_id", steamIdCol);
results.FieldNameToNum("time_start", timeStartCol);
results.FieldNameToNum("time_end", timeEndCol);
results.FieldNameToNum("alias", aliasCol);
results.FetchString(steamIdCol, steamID, sizeof(steamID));
results.FetchString(timeStartCol, timeStart, sizeof(timeStart));
results.FetchString(timeEndCol, timeEnd, sizeof(timeEnd));
results.FetchString(aliasCol, alias, sizeof(alias));
strcopy(g_cCurrentPanelSteamID, sizeof(g_cCurrentPanelSteamID), steamID);
FormatTime(timeStartFormatted, sizeof(timeStartFormatted), "%F %R", StringToInt(timeStart));
if (StringToInt(timeEnd) == 0) {
Format(timeEndFormatted, sizeof(timeEndFormatted), "%t", "Permaban");
} else {
FormatTime(timeEndFormatted, sizeof(timeEndFormatted), "%F %R", StringToInt(timeEnd));
}
Format(line1, sizeof(line1), "Steam ID: %s", steamID);
if (alias[0]) {
Format(line2, sizeof(line2), "%t: %s", "Alias", alias);
} else {
Format(line2, sizeof(line2), "%t: %t", "Alias", "No Alias");
}
Format(line3, sizeof(line3), "%t: %s", "Date Of Issue", timeStartFormatted);
Format(line4, sizeof(line4), "%t: %s", "Date Of End", timeEndFormatted);
Format(panelTitle, sizeof(panelTitle), "%t", "Showing Ban Status", steamID);
banPanel.SetTitle(panelTitle);
Format(exitString, sizeof(exitString), "%t", "Exit");
Format(backString, sizeof(backString), "%t", "Back");
Format(removeUser, sizeof(removeUser), "%t", "Remove User");
banPanel.DrawText(" ");
banPanel.DrawText(line1);
banPanel.DrawText(line2);
banPanel.DrawText(line3);
banPanel.DrawText(line4);
banPanel.DrawText(" ");
banPanel.DrawItem(removeUser);
banPanel.DrawItem(backString);
banPanel.CurrentKey = 10;
banPanel.DrawItem(exitString);
banPanel.Send(client, banPanelHandler, 20);
delete results;
delete banPanel;
}
public int banPanelHandler(Menu menu, MenuAction action, int param1, int param2) {
switch (action) {
case MenuAction_Select: {
switch (param2) {
case 1: {
DeleteMenuEntry(param1);
InGameRemove(_, g_cCurrentPanelSteamID, false);
ShowBanList(param1);
}
case 2: {
ShowBanList(param1);
}
case 10: {
delete menu;
}
}
}
case MenuAction_End: {
delete menu;
}
}
}
void DeleteMenuEntry(int client) {
char query[512];
Format(query, sizeof(query), "DELETE c1 FROM calladmin_block c1 "...
"INNER JOIN (SELECT steam_id FROM calladmin_block ORDER BY id LIMIT %i,1) "...
"AS c2 ON c2.steam_id = c1.steam_id;", g_iCurrentUserInPanel);
g_Database.Query(SQL_DeleteMenuEntryCallback, query, GetClientUserId(client));
}
public void SQL_DeleteMenuEntryCallback(Database db, DBResultSet results, const char[] error, int userid) {
if (db == null || results == null) {
ThrowError("%s %s", PREFIXN, error);
delete results;
return;
}
int client = GetClientOfUserId(userid);
PrintToChat(client, "%s %t", PREFIX, "Steam ID Removed");
delete results;
}
/* CallAdmin Menu Draw Forward */
public Action CallAdmin_OnDrawMenu(int client) {
if ((GetTime() < g_iUserUnbanTime[client] || g_iUserUnbanTime[client] == 0) && g_iUserUnbanTime[client] != -1) {
PrintToChat(client, "%s %t", PREFIX, "Not Allowed");
return Plugin_Handled;
}
return Plugin_Continue;
}
/* In Game Stocks (to update user ban time caches on the go and return ban times) */
void InGameAdd(int target, int banEnd, const char[] targetSteamID, bool isTarget) {
if (isTarget) {
g_iUserUnbanTime[target] = banEnd;
return;
} else {
char steamID[32];
for (int i = 1; i <= MaxClients; i++) {
if (IsClientInGame(i) && !IsFakeClient(i) && GetClientAuthId(i, AuthId_Steam2, steamID, sizeof(steamID))) {
if (StrEqual(targetSteamID, steamID)) {
g_iUserUnbanTime[i] = banEnd;
return;
}
}
}
}
}
void InGameRemove(int target = 1, const char[] targetSteamID = "", bool isTarget = false) {
if (isTarget) {
g_iUserUnbanTime[target] = -1;
return;
} else {
char steamID[32];
for (int i = 1; i <= MaxClients; i++) {
if (IsClientInGame(i) && !IsFakeClient(i) && GetClientAuthId(i, AuthId_Steam2, steamID, sizeof(steamID))) {
if (StrEqual(targetSteamID, steamID)) {
g_iUserUnbanTime[i] = -1;
return;
}
}
}
}
}
stock int ProcessBanTime(int time) {
return time == 0 ? 0 : (GetTime() + (time * 60));
}