-
Notifications
You must be signed in to change notification settings - Fork 0
/
kiwi-clantags.sp
1090 lines (979 loc) · 29.3 KB
/
kiwi-clantags.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
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
/*
Potential To Do:
MaxTagLength = 12
Add cmd for temporary tag override: sm_forcetag <target> <tag>
*/
#pragma semicolon 1
#define PLUGIN_VERSION "2.2.7"
#define LoopValidPlayers(%1,%2)\
for(int %1 = 1;%1 <= MaxClients; ++%1)\
if(IsValidClient(%1, %2))
#include <sourcemod>
#include <cstrike>
#include <autoexecconfig> //https://github.com/Impact123/AutoExecConfig
#pragma newdecls required
char g_sCfgPath[PLATFORM_MAX_PATH];
ConVar g_hAdminFlag;
char g_sAdminFlag[30];
ConVar g_hIncludeBots;
ConVar g_hEnforceTags;
ConVar g_hUpdateFreq;
ConVar g_hUseMySQL;
ConVar g_hDebug;
char ga_sTag[MAXPLAYERS + 1][50];
char ga_sExtTag[MAXPLAYERS + 1][50];
bool ga_bLoaded[MAXPLAYERS + 1] = {false, ...};
ArrayList g_hValidTags;
ArrayList g_hTags;
ArrayList g_hFlags;
ArrayList g_hIgnored;
Database g_oDatabase;
//bool g_bLateLoad;
char g_sServerIP[64] = "";
int g_iNumSetups = -1;
int g_iDBLoaded = 0;
public Plugin myinfo =
{
name = "[KIWI] Clan Tags",
author = "drop",
description = "Configurable clan tag setups.",
version = PLUGIN_VERSION,
url = "https://kiir.us"
}
public APLRes AskPluginLoad2(Handle hMyself, bool bLate, char[] sError, int err_max)
{
//g_bLateLoad = bLate;
CreateNative("KIWIClanTags_Reload", Native_ReloadPlugin);
CreateNative("KIWIClanTags_ReloadPlayer", Native_ReloadPlayer);
CreateNative("KIWIClanTags_UsingMysql", Native_UsingMysql);
CreateNative("KIWIClanTags_SetExtTag", Native_SetExtTag);
CreateNative("KIWIClanTags_HasAnyTag", Native_HasAnyTag);
CreateNative("KIWIClanTags_HasMainTag", Native_HasMainTag);
CreateNative("KIWIClanTags_HasExtTag", Native_HasExtTag);
RegPluginLibrary("kiwiclantags");
return APLRes_Success;
}
public void OnPluginStart()
{
AutoExecConfig_SetFile("kiwiclantags");
AutoExecConfig_CreateConVar("kiwiclantags_version", PLUGIN_VERSION, "[KIWI] Clan Tags: Version", FCVAR_NOTIFY|FCVAR_DONTRECORD);
g_hAdminFlag = CreateConVar("kiwiclantags_admflag", "z", "Admin flag(s) used for sm_rechecktags command.", FCVAR_NONE);
g_hAdminFlag.GetString(g_sAdminFlag, sizeof(g_sAdminFlag));
g_hAdminFlag.AddChangeHook(OnCVarChange);
g_hIncludeBots = AutoExecConfig_CreateConVar("kiwiclantags_bots", "0", "Do bots get tags? (1 = yes, 0 = no)", FCVAR_NONE, true, 0.0, true, 1.0);
g_hEnforceTags = AutoExecConfig_CreateConVar("kiwiclantags_enforcetags", "2", "If no matching setup is found, should their tag be forced to be blank? (0 = allow players setting any clan tags they want, 1 = if no matching setup found, they can only use tags found in the cfg file, 2 = only get tags by having a matching setup in cfg file or database).", FCVAR_NONE, true, 0.0, true, 2.0);
g_hUpdateFreq = AutoExecConfig_CreateConVar("kiwiclantags_updatefreq", "0", "Frequency to re-load clients from cfg file (0 = only check once). This function is namely used to help interact with other plugins changing admin status late.", FCVAR_NONE, true, 0.0);
g_hUseMySQL = AutoExecConfig_CreateConVar("kiwiclantags_use_mysql", "0", "Use mysql? (1 = Use MySQL to manage setups, 0 = Use cfg file to manage setups)", FCVAR_NONE, true, 0.0, true, 1.0);
g_hDebug = AutoExecConfig_CreateConVar("kiwiclantags_debug", "0", "Enable debug mode? (1 = Yes, produce debug files (note, this can produce large files), 0 = Disable debug mode)", FCVAR_NONE, true, 0.0, true, 1.0);
AutoExecConfig_ExecuteFile();
AutoExecConfig_CleanFile();
RegConsoleCmd("sm_rechecktags", Cmd_ResetTags, "Recheck tags for all players in the server.");
HookEvent("player_spawn", Event_Recheck);
HookEvent("player_team", Event_Recheck);
HookEvent("round_end", Event_RoundEnd, EventHookMode_Pre);
AddCommandListener(Command_Recheck, "jointeam");
AddCommandListener(Command_Recheck, "joinclass");
AddCommandListener(Command_Recheck, "spec_mode");
AddCommandListener(Command_Recheck, "spec_next");
AddCommandListener(Command_Recheck, "spec_player");
AddCommandListener(Command_Recheck, "spec_prev");
BuildPath(Path_SM, g_sCfgPath, sizeof(g_sCfgPath), "configs/kiwiclantags.cfg");
g_hValidTags = new ArrayList(64);
g_hTags = new ArrayList(150);
g_hFlags = new ArrayList(150);
g_hIgnored = new ArrayList();
//LoadSetups();
}
public void OnCVarChange(ConVar hCVar, const char[] sOldValue, const char[] sNewValue)
{
if(hCVar == g_hAdminFlag)
{
GetConVarString(g_hAdminFlag, g_sAdminFlag, sizeof(g_sAdminFlag));
}
}
public void OnConfigsExecuted()
{
GetServerIP();
if(g_hUseMySQL.BoolValue)
{
if(g_oDatabase == null)
{
SetDBHandle();
}
}
/*LoadSetups();
if(g_bLateLoad)
{
LoopValidPlayers_Bots(i)
{
OnClientConnected(i);
OnClientPostAdminCheck(i);
}
}*/
}
void GetServerIP()
{
int a_iArray[4];
int iLongIP = GetConVarInt(FindConVar("hostip"));
a_iArray[0] = (iLongIP >> 24) & 0x000000FF;
a_iArray[1] = (iLongIP >> 16) & 0x000000FF;
a_iArray[2] = (iLongIP >> 8) & 0x000000FF;
a_iArray[3] = iLongIP & 0x000000FF;
Format(g_sServerIP, sizeof(g_sServerIP), "%d.%d.%d.%d:%i", a_iArray[0], a_iArray[1], a_iArray[2], a_iArray[3], GetConVarInt(FindConVar("hostport")));
}
void SetDBHandle()
{
if(g_iDBLoaded != 1) //if connection not in progress (allow if no connection or already connected)
{
g_iDBLoaded = 1;
if(g_oDatabase != null)
{
delete g_oDatabase;
g_oDatabase = null;
}
PrintToServer("Establishing database connection for togsclantags.");
Database.Connect(SQLCallback_Connect, "kiwiclantags");
}
}
public void SQLCallback_Connect(Database oDB, const char[] sError, any data)
{
if(oDB == null)
{
SetFailState("Error connecting to main database. %s", sError);
}
else
{
g_oDatabase = oDB;
char sDriver[64], sQuery[600];
DBDriver oDriver = g_oDatabase.Driver;
oDriver.GetIdentifier(sDriver, sizeof(sDriver));
if(StrEqual(sDriver, "sqlite", false))
{
SetFailState("This plugin cannot use SQLite due to the need for server operators to create setups. Either use a MySQL database (with CVar setting: \"kiwiclantags_use_mysql\" \"1\"), or use the config file (with CVar setting: \"kiwiclantags_use_mysql\" \"0\").");
}
PrintToServer("Database connection established for kiwiclantags!");
/* `id` INT(20) NOT NULL AUTO_INCREMENT,
`setup_type` VARCHAR(150) NOT NULL DEFAULT 'public',
`enable_setup` INT(1) NOT NULL DEFAULT 1,
`exclude_setup` INT(1) NULL DEFAULT 0,
`ignore_setup` INT(1) NULL DEFAULT 0,
`tagtext` VARCHAR(150) NULL DEFAULT '',
`server_ip` VARCHAR(300) NULL DEFAULT '',
`setup_order` INT(10) NULL DEFAULT 0
`dont_remove` INT(2) NULL DEFAULT 1
*/
Format(sQuery, sizeof(sQuery), "CREATE TABLE IF NOT EXISTS `kiwiclantags_setups` (\
`id` INT(20) NOT NULL AUTO_INCREMENT,\
`setup_type` VARCHAR(150) NOT NULL DEFAULT 'public',\
`enable_setup` INT(1) NOT NULL DEFAULT 1,\
`exclude_setup` INT(1) NULL DEFAULT 0,\
`ignore_setup` INT(1) NULL DEFAULT 0,\
`tagtext` VARCHAR(150) NULL DEFAULT '',\
`server_ip` VARCHAR(300) NULL DEFAULT '',\
`setup_order` INT(10) NULL DEFAULT 0,\
`dont_remove` INT(2) NULL DEFAULT 1,\
PRIMARY KEY (`id`)) DEFAULT CHARSET=latin1 AUTO_INCREMENT=1");
g_oDatabase.Query(SQLCallback_Void, sQuery, 1);
}
}
public void SQLCallback_Void(Database oDB, DBResultSet oResultsSet, const char[] sError, any iValue)
{
if(oDB == null || oResultsSet == null)
{
SetFailState("Error (%i): %s", iValue, sError);
}
else if(iValue == 1)
{
GetSetupsCount();
LoadSetups();
}
}
public void OnMapStart()
{
LoadSetups();
}
void GetSetupsCount()
{
if(StrEqual(g_sServerIP, "", false))
{
GetServerIP();
}
if(g_hUseMySQL.BoolValue)
{
if(g_oDatabase != null)
{
char sQuery[150];
Format(sQuery, sizeof(sQuery), "SELECT COUNT(*) \
FROM togsclantags_setups \
WHERE (server_ip LIKE '%%%s%%') OR (server_ip = '') OR (server_ip IS NULL)", g_sServerIP);
g_oDatabase.Query(SQLCallback_SetupsCnt, sQuery, 1);
}
}
}
public void SQLCallback_SetupsCnt(Database oDB, DBResultSet oResultsSet, const char[] sError, any iValue)
{
if(oDB == null || oResultsSet == null)
{
SetFailState("Error (%i): %s", iValue, sError);
}
if(oResultsSet.RowCount > 0)
{
oResultsSet.FetchRow();
int iPrevCnt = g_iNumSetups;
g_iNumSetups = oResultsSet.FetchInt(0);
if((iPrevCnt != -1) && (iPrevCnt != g_iNumSetups))
{
LoadSetups();
}
}
}
void LoadSetups()
{
if(g_iDBLoaded == 2)
{
g_iDBLoaded = 0;
}
g_hValidTags.Clear();
g_hTags.Clear();
g_hFlags.Clear();
g_hIgnored.Clear();
if(g_hUseMySQL.BoolValue)
{
if(g_hDebug.BoolValue)
{
Log("togsclantags_debug.log", "Retrieving Setups from database.");
}
if(g_oDatabase == null)
{
SetDBHandle();
return;
}
else
{
GetMySQLSetups();
return;
}
}
else
{
if(g_hDebug.BoolValue)
{
Log("togsclantags_debug.log", "Retrieving Setups from config file.");
}
if(!FileExists(g_sCfgPath))
{
SetFailState("Configuration file not found: %s", g_sCfgPath);
return;
}
KeyValues oKeyValues = new KeyValues("Setups");
if(!oKeyValues.ImportFromFile(g_sCfgPath))
{
delete oKeyValues;
SetFailState("Improper structure for configuration file: %s", g_sCfgPath);
return;
}
if(oKeyValues.GotoFirstSubKey(true))
{
do
{
char sBuffer2[150];
oKeyValues.GetString("tag", sBuffer2, sizeof(sBuffer2), "");
if(oKeyValues.GetNum("exclude", 0) == 1)
{
g_hValidTags.PushString(sBuffer2);
}
char sSectionName[100];
if(g_hDebug.BoolValue)
{
oKeyValues.GetSectionName(sSectionName, sizeof(sSectionName));
Log("togsclantags_debug.log", "Checking setup '%s' (exclude: %i). Tag: %s", sSectionName, oKeyValues.GetNum("exclude", 0), sBuffer2);
}
if(oKeyValues.GetNum("enabled", 1))
{
g_hTags.PushString(sBuffer2);
oKeyValues.GetString("flag", sBuffer2, sizeof(sBuffer2), "public");
if(g_hDebug.BoolValue)
{
Log("togsclantags_debug.log", "Setup '%s' is being added with flags: %s. Ignore: %i", sSectionName, sBuffer2, oKeyValues.GetNum("ignore", 0));
}
g_hFlags.PushString(sBuffer2);
g_hIgnored.Push(oKeyValues.GetNum("ignore", 0));
}
else
{
if(g_hDebug.BoolValue)
{
Log("togsclantags_debug.log", "Setup '%s' is disabled.", sSectionName);
}
}
}
while(oKeyValues.GotoNextKey(false));
}
else
{
delete oKeyValues;
SetFailState("Can't find subkey in configuration file %s!", g_sCfgPath);
return;
}
delete oKeyValues;
g_iDBLoaded = 2;
LoopValidPlayers(i, true)
{
OnClientConnected(i);
OnClientPostAdminCheck(i);
}
}
}
void GetMySQLSetups()
{
if(g_oDatabase != null)
{
char sQuery[1000];
/* `id` INT(20) NOT NULL AUTO_INCREMENT,
`setup_type` VARCHAR(150) NOT NULL DEFAULT 'public',
`enable_setup` INT(1) NOT NULL DEFAULT 1,
`exclude_setup` INT(1) NULL DEFAULT 0,
`ignore_setup` INT(1) NULL DEFAULT 0,
`tagtext` VARCHAR(150) NULL DEFAULT '',
`server_ip` VARCHAR(300) NOT NULL DEFAULT ''
*/
Format(sQuery, sizeof(sQuery), "SELECT setup_type,ignore_setup,tagtext,exclude_setup,enable_setup \
FROM togsclantags_setups \
WHERE (server_ip = '') OR (server_ip LIKE '%%%s%%') OR (server_ip IS NULL) \
ORDER BY setup_order", g_sServerIP);
if(g_hDebug.BoolValue)
{
Log("togsclantags_debug.log", "Querying database for setups with query: %s", sQuery);
}
g_oDatabase.Query(SQLCallback_GetSetups, sQuery, 1);
}
else
{
CreateTimer(3.0, TimerCB_RetryConn, _, TIMER_FLAG_NO_MAPCHANGE);
}
}
public Action TimerCB_RetryConn(Handle hTimer)
{
if(g_oDatabase != null)
{
char sQuery[1000];
Format(sQuery, sizeof(sQuery), "SELECT setup_type,ignore_setup,tagtext,exclude_setup,enable_setup \
FROM togsclantags_setups \
WHERE (server_ip = '') OR (server_ip LIKE '%%%s%%') OR (server_ip IS NULL) \
ORDER BY setup_order", g_sServerIP);
if(g_hDebug.BoolValue)
{
Log("togsclantags_debug.log", "Querying database for setups with query: %s", sQuery);
}
g_oDatabase.Query(SQLCallback_GetSetups, sQuery, 1);
}
else
{
CreateTimer(5.0, TimerCB_RetryConn, _, TIMER_FLAG_NO_MAPCHANGE);
}
}
public void SQLCallback_GetSetups(Database oDB, DBResultSet oResultsSet, const char[] sError, any iValue)
{
if(oDB == null || oResultsSet == null)
{
SetFailState("Error (%i): %s", iValue, sError);
}
ClearExistingSetups();
int iRowCnt = oResultsSet.RowCount;
if(g_hDebug.BoolValue)
{
Log("togsclantags_debug.log", "%i setup rows returned from database.", iRowCnt);
}
if(iRowCnt > 0)
{
while(oResultsSet.FetchRow())
{
/* `id` INT(20) NOT NULL AUTO_INCREMENT,
`setup_type` VARCHAR(150) NOT NULL DEFAULT 'public',
`enable_setup` INT(1) NOT NULL DEFAULT 1,
`exclude_setup` INT(1) NULL DEFAULT 0,
`ignore_setup` INT(1) NULL DEFAULT 0,
`tagtext` VARCHAR(150) NULL DEFAULT '',
`server_ip` VARCHAR(300) NOT NULL DEFAULT '',
setup_order INT(10) NULL DEFAULT 0
`dont_remove` INT(2) NULL DEFAULT 1
0 1 2 3 4
setup_type,ignore_setup,tagtext,exclude_setup,enable_setup
*/
if(g_hDebug.BoolValue)
{
Log("togsclantags_debug.log", "Starting new setup row.");
}
char sBuffer[150];
int iEnabled = oResultsSet.FetchInt(4);
if(iEnabled)
{
oResultsSet.FetchString(0, sBuffer, sizeof(sBuffer));
g_hFlags.PushString(sBuffer);
g_hIgnored.Push(oResultsSet.FetchInt(1));
if(g_hDebug.BoolValue)
{
Log("togsclantags_debug.log", "Adding setup flag: %s. Ignored: %i", sBuffer, oResultsSet.FetchInt(1));
}
oResultsSet.FetchString(2, sBuffer, sizeof(sBuffer));
g_hTags.PushString(sBuffer);
if(g_hDebug.BoolValue)
{
Log("togsclantags_debug.log", "Adding setup tag: %s", sBuffer);
}
}
else
{
if(g_hDebug.BoolValue)
{
Log("togsclantags_debug.log", "setup row is disabled.");
}
}
if(oResultsSet.FetchInt(3) == 1)
{
g_hValidTags.PushString(sBuffer);
}
}
}
g_iDBLoaded = 2;
LoopValidPlayers(i, true)
{
OnClientConnected(i);
OnClientPostAdminCheck(i);
}
}
void ClearExistingSetups()
{
/*for(int i = 0; i < GetArraySize(g_hTags); i++)
{
DoStuff();
}*/
g_hTags.Clear();
}
public int Native_ReloadPlugin(Handle hPlugin, int iNumParams)
{
LoadSetups();
ReRetrieveAllTags();
}
public int Native_UsingMysql(Handle hPlugin, int iNumParams)
{
if(g_hUseMySQL.BoolValue)
{
return 1;
}
return 0;
}
public int Native_ReloadPlayer(Handle hPlugin, int iNumParams)
{
int client = GetNativeCell(1);
if(IsValidClient(client))
{
ReRetrieveTags(client);
return true;
}
return ThrowNativeError(SP_ERROR_NATIVE, "Invalid client index (%i) or client not connected (add check before using native).", client);
}
public int Native_HasAnyTag(Handle hPlugin, int iNumParams) //pull request by Hexer10
{
int client = GetNativeCell(1);
if(IsValidClient(client))
{
return ((strlen(ga_sTag[client]) > 0) || (strlen(ga_sExtTag[client]) > 0));
}
return ThrowNativeError(SP_ERROR_NATIVE, "Invalid client index (%i) or client not connected (add check before using native).", client);
}
public int Native_HasMainTag(Handle hPlugin, int iNumParams)
{
int client = GetNativeCell(1);
if(IsValidClient(client))
{
return (strlen(ga_sTag[client]) > 0);
}
return ThrowNativeError(SP_ERROR_NATIVE, "Invalid client index (%i) or client not connected (add check before using native).", client);
}
public int Native_HasExtTag(Handle hPlugin, int iNumParams)
{
int client = GetNativeCell(1);
if(IsValidClient(client))
{
return (strlen(ga_sExtTag[client]) > 0);
}
return ThrowNativeError(SP_ERROR_NATIVE, "Invalid client index (%i) or client not connected (add check before using native).", client);
}
public int Native_SetExtTag(Handle hPlugin, int iNumParams)
{
int client = GetNativeCell(1);
if(IsValidClient(client))
{
GetNativeString(2, ga_sExtTag[client], sizeof(ga_sExtTag[]));
ga_bLoaded[client] = false;
GetTags(client);
return true;
}
return ThrowNativeError(SP_ERROR_NATIVE, "Invalid client index (%i) or client not connected (add check before using native).", client);
}
public Action Cmd_ResetTags(int client, int iArgs)
{
if(IsValidClient(client))
{
if(!HasFlags(client, g_sAdminFlag))
{
ReplyToCommand(client, "\x04You do not have access to this command!");
return Plugin_Handled;
}
}
LoadSetups();
ReRetrieveAllTags();
return Plugin_Handled;
}
void ReRetrieveAllTags()
{
for(int i = 1; i <= MaxClients; i++)
{
ReRetrieveTags(i);
}
}
void ReRetrieveTags(int client)
{
if(IsValidClient(client, g_hIncludeBots.BoolValue))
{
ga_bLoaded[client] = false;
GetTags(client);
}
}
public void OnClientConnected(int client)
{
ga_sTag[client] = "";
ga_sExtTag[client] = "";
ga_bLoaded[client] = false;
}
public void OnClientDisconnect(int client)
{
ga_sTag[client] = "";
ga_sExtTag[client] = "";
ga_bLoaded[client] = false;
}
public void OnClientPostAdminCheck(int client)
{
if(g_iDBLoaded == 2)
{
GetTags(client);
if(g_hUpdateFreq.FloatValue)
{
CreateTimer(g_hUpdateFreq.FloatValue, TimerCB_ReCheckCfg, GetClientUserId(client), TIMER_FLAG_NO_MAPCHANGE);
}
}
else
{
CreateTimer(5.0, TimerCB_RetryLoadClient, GetClientUserId(client), TIMER_FLAG_NO_MAPCHANGE);
}
}
public Action TimerCB_RetryLoadClient(Handle hTimer, any iUserID)
{
int client = GetClientOfUserId(iUserID);
if(!IsValidClient(client, g_hIncludeBots.BoolValue))
{
return Plugin_Stop;
}
if(g_iDBLoaded == 2)
{
GetTags(client);
if(g_hUpdateFreq.FloatValue)
{
CreateTimer(g_hUpdateFreq.FloatValue, TimerCB_ReCheckCfg, GetClientUserId(client), TIMER_FLAG_NO_MAPCHANGE);
}
}
else
{
CreateTimer(5.0, TimerCB_RetryLoadClient, GetClientUserId(client), TIMER_FLAG_NO_MAPCHANGE);
}
return Plugin_Continue;
}
public Action TimerCB_ReCheckCfg(Handle hTimer, any iUserID)
{
int client = GetClientOfUserId(iUserID);
if(!IsValidClient(client, g_hIncludeBots.BoolValue))
{
return Plugin_Stop;
}
ga_bLoaded[client] = false;
GetTags(client);
return Plugin_Continue;
}
public void OnClientSettingsChanged(int client) //hooked in case they change their clan tag
{
if(ga_bLoaded[client]) //dont want them to try loading before steam id loads - could also lead to multiple timers
{
CheckTags(client);
}
}
public Action Event_RoundEnd(Handle hEvent, const char[] sName, bool bDontBroadcast)
{
GetSetupsCount();
}
public Action Event_Recheck(Handle hEvent, const char[] sName, bool bDontBroadcast)
{
int client = GetClientOfUserId(GetEventInt(hEvent, "userid"));
if(IsValidClient(client, g_hIncludeBots.BoolValue))
{
if(!ga_bLoaded[client])
{
GetTags(client);
}
else
{
CheckTags(client);
}
}
return Plugin_Continue;
}
public Action Command_Recheck(int client, char[] sCommand, int iArgs)
{
if(IsValidClient(client, g_hIncludeBots.BoolValue))
{
if(!ga_bLoaded[client])
{
GetTags(client);
}
else
{
CheckTags(client);
}
}
return Plugin_Continue;
}
void GetTags(int client)
{
if(!IsValidClient(client, true))
{
return;
}
if(g_iDBLoaded != 2)
{
CreateTimer(5.0, TimerCB_RetryLoadClient, GetClientUserId(client), TIMER_FLAG_NO_MAPCHANGE);
}
if(g_hDebug.BoolValue)
{
Log("togsclantags_debug.log", "%L is starting checks for tags", client);
}
ga_sTag[client] = "";
if(StrEqual(ga_sExtTag[client], "", false))
{
char sBuffer[150], a_sSteamIDs[4][65];
if(!IsValidClient(client))
{
Format(a_sSteamIDs[0], sizeof(a_sSteamIDs[]), "BOT");
Format(a_sSteamIDs[1], sizeof(a_sSteamIDs[]), "BOT");
Format(a_sSteamIDs[2], sizeof(a_sSteamIDs[]), "BOT");
Format(a_sSteamIDs[3], sizeof(a_sSteamIDs[]), "BOT");
}
else
{
if(IsClientAuthorized(client))
{
GetClientAuthId(client, AuthId_Steam2, a_sSteamIDs[0], sizeof(a_sSteamIDs[]));
ReplaceString(a_sSteamIDs[0], sizeof(a_sSteamIDs[]), "STEAM_1", "STEAM_0", false);
GetClientAuthId(client, AuthId_Steam2, a_sSteamIDs[1], sizeof(a_sSteamIDs[]));
ReplaceString(a_sSteamIDs[1], sizeof(a_sSteamIDs[]), "STEAM_0", "STEAM_1", false);
GetClientAuthId(client, AuthId_Steam3, a_sSteamIDs[2], sizeof(a_sSteamIDs[]));
GetClientAuthId(client, AuthId_SteamID64, a_sSteamIDs[3], sizeof(a_sSteamIDs[]));
}
else
{
CreateTimer(5.0, TimerCB_RetryLoadClient, GetClientUserId(client), TIMER_FLAG_NO_MAPCHANGE);
}
}
if(g_hDebug.BoolValue)
{
Log("togsclantags_debug.log", "Auth IDs for %L: %s ; %s ; %s ; %s", client, a_sSteamIDs[0], a_sSteamIDs[1], a_sSteamIDs[2], a_sSteamIDs[3]);
}
int iSetupCnt = g_hFlags.Length;
for(int i = 0; i < iSetupCnt; i++)
{
g_hFlags.GetString(i, sBuffer, sizeof(sBuffer));
if(g_hDebug.BoolValue)
{
Log("togsclantags_debug.log", "Checking %L against flag (%i/%i): %s", client, i+1, iSetupCnt, sBuffer);
}
if(StrEqual("BOT", sBuffer, false)) //check if BOT config
{
if(g_hDebug.BoolValue)
{
Log("togsclantags_debug.log", "Checking %L against flag (%i/%i): %s. Setup is in 'BOT' category.", client, i+1, iSetupCnt, sBuffer);
}
if(StrEqual("BOT", a_sSteamIDs[0], false)) //check if player is BOT
{
g_hTags.GetString(i, ga_sTag[client], sizeof(ga_sTag[]));
break;
}
}
else if(HasNumbers(sBuffer) || (StrContains(sBuffer, ":", false) != -1)) //if steam ID
{
if(StrEqual(sBuffer, a_sSteamIDs[0], false) || StrEqual(sBuffer, a_sSteamIDs[1], false) || StrEqual(sBuffer, a_sSteamIDs[2], false) || StrEqual(sBuffer, a_sSteamIDs[3], false))
{
if(g_hDebug.BoolValue)
{
Log("togsclantags_debug.log", "Checking %L against flag (%i/%i): %s. Matching setup found in Steam IDs!", client, i+1, iSetupCnt, sBuffer);
}
if(!g_hIgnored.Get(i))
{
g_hTags.GetString(i, ga_sTag[client], sizeof(ga_sTag[]));
}
break;
}
else if(g_hDebug.BoolValue)
{
Log("togsclantags_debug.log", "Checking %L against flag (%i/%i): %s. Setup is in Steam ID category, but does not match.", client, i+1, iSetupCnt, sBuffer);
}
}
else if(HasFlags(client, sBuffer)) //check if player has defined flags
{
if(g_hDebug.BoolValue)
{
Log("togsclantags_debug.log", "Checking %L against flag (%i/%i): %s. Matching setup found in flags category!", client, i+1, iSetupCnt, sBuffer);
}
if(!g_hIgnored.Get(i))
{
g_hTags.GetString(i, ga_sTag[client], sizeof(ga_sTag[]));
}
break;
}
else
{
if(g_hDebug.BoolValue)
{
Log("togsclantags_debug.log", "No matches found for %L against flag (%i/%i): %s.", client, i+1, iSetupCnt, sBuffer);
}
}
}
}
else
{
strcopy(ga_sTag[client], sizeof(ga_sTag[]), ga_sExtTag[client]);
if(g_hDebug.BoolValue)
{
Log("togsclantags_debug.log", "Tag for %L set by external plugin: %s.", client, ga_sTag[client]);
}
}
ga_bLoaded[client] = true;
CheckTags(client);
}
stock bool HasNumbers(char[] sString)
{
for(int i = 0; i < strlen(sString); i++)
{
if(IsCharNumeric(sString[i]))
{
return true;
}
}
return false;
}
stock bool IsNumeric(char[] sString)
{
for(int i = 0; i < strlen(sString); i++)
{
if(!IsCharNumeric(sString[i]))
{
return false;
}
}
return true;
}
void CheckTags(int client)
{
if(!ga_bLoaded[client])
{
GetTags(client);
return;
}
//PrintToChatAll("TEAM: %d", GetClientTeam(client));
if(!StrEqual(ga_sTag[client], "", true))
{
if(GetClientTeam(client) != CS_TEAM_SPECTATOR && GetClientTeam(client) != CS_TEAM_NONE) {
CS_SetClientClanTag(client, ga_sTag[client]);
//Log("togsclantags_debug.log", "Set tag to OWNER.");
} else {
CS_SetClientClanTag(client, "");
//Log("togsclantags_debug.log", "Set tag to blank.");
}
}
else if(g_hEnforceTags.IntValue == 1)
{
char sTag[50];
CS_GetClientClanTag(client, sTag, sizeof(sTag));
if(g_hValidTags.FindString(sTag) == -1 )
{
CS_SetClientClanTag(client, "");
}
}
else if(g_hEnforceTags.IntValue == 2)
{
CS_SetClientClanTag(client, "");
}
}
bool HasFlags(int client, char[] sFlags)
{
if(StrEqual(sFlags, "public", false) || StrEqual(sFlags, "", false))
{
return true;
}
else if(StrEqual(sFlags, "none", false)) //useful for some plugins
{
return false;
}
else if(!client) //if rcon
{
return true;
}
else if(CheckCommandAccess(client, "sm_not_a_command", ADMFLAG_ROOT, true))
{
return true;
}
AdminId id = GetUserAdmin(client);
if(id == INVALID_ADMIN_ID)
{
return false;
}
int flags, clientflags;
clientflags = GetUserFlagBits(client);
if(StrContains(sFlags, ";", false) != -1) //check if multiple strings
{
int i = 0, iStrCount = 0;
while(sFlags[i] != '\0')
{
if(sFlags[i++] == ';')
{
iStrCount++;
}
}
iStrCount++; //add one more for stuff after last comma
char[][] a_sTempArray = new char[iStrCount][30];
ExplodeString(sFlags, ";", a_sTempArray, iStrCount, 30);
bool bMatching = true;
for(i = 0; i < iStrCount; i++)
{
bMatching = true;
flags = ReadFlagString(a_sTempArray[i]);
for(int j = 0; j <= 20; j++)
{
if(bMatching) //if still matching, continue loop
{
if(flags & (1<<j))
{
if(!(clientflags & (1<<j)))
{
bMatching = false;
}
}
}
}
if(bMatching)
{
return true;
}
}
return false;
}
else
{
flags = ReadFlagString(sFlags);
for(int i = 0; i <= 20; i++)
{
if(flags & (1<<i))
{
if(!(clientflags & (1<<i)))
{
return false;
}
}
}
return true;
}
}