· 8 years ago · Dec 29, 2017, 02:46 AM
1#pragma semicolon 1
2
3// ====[ INCLUDES ]============================================================
4#include <sourcemod>
5#include <regex>
6#include <ccc>
7
8// ====[ DEFINES ]=============================================================
9#define PLUGIN_NAME "Custom Chat Colors Menu"
10#define PLUGIN_VERSION "2.2"
11#define MAX_COLORS 255
12#define TAG 0
13#define NAME 1
14#define CHAT 2
15#define ENABLEFLAG_TAG (1 << TAG)
16#define ENABLEFLAG_NAME (1 << NAME)
17#define ENABLEFLAG_CHAT (1 << CHAT)
18
19// ====[ HANDLES | CVARS ]=====================================================
20new Handle:g_hCvarEnabled, g_iCvarEnabled;
21new Handle:g_hCvarHideTags, bool:g_bCvarHideTags;
22new Handle:g_hRegexHex;
23new Handle:g_hSQL;
24
25// ====[ VARIABLES ]===========================================================
26new g_iColorCount;
27new AdminFlag:g_iColorFlagList[MAX_COLORS][16];
28new bool:g_bColorsLoaded[MAXPLAYERS + 1];
29new bool:g_bColorAdminFlags[MAX_COLORS];
30new bool:g_bHideTag[MAXPLAYERS + 1];
31new bool:g_bAccessColor[MAXPLAYERS + 1][3];
32new bool:g_bAccessHideTags[MAXPLAYERS + 1];
33new String:g_strAuth[MAXPLAYERS + 1][32];
34new String:g_strColor[MAXPLAYERS + 1][3][7];
35new String:g_strColorName[MAX_COLORS][255];
36new String:g_strColorHex[MAX_COLORS][255];
37new String:g_strColorFlags[MAX_COLORS][255];
38new String:g_strConfigFile[PLATFORM_MAX_PATH];
39new String:g_strSQLDriver[16];
40
41// ====[ PLUGIN ]==============================================================
42public Plugin:myinfo =
43{
44 name = PLUGIN_NAME,
45 author = "ReFlexPoison",
46 description = "Change Custom Chat Colors settings through easy to access menus",
47 version = PLUGIN_VERSION,
48 url = "http://www.sourcemod.net"
49}
50
51// ====[ EVENTS ]==============================================================
52public OnPluginStart()
53{
54 CreateConVar("sm_cccm_version", PLUGIN_VERSION, PLUGIN_NAME, FCVAR_PLUGIN | FCVAR_SPONLY | FCVAR_DONTRECORD | FCVAR_NOTIFY);
55
56 g_hCvarEnabled = CreateConVar("sm_cccm_enabled", "7", "Enable Custom Chat Colors Menu (Add up the numbers to choose)\n0 = Disabled\n1 = Tag\n2 = Name\n4 = Chat", FCVAR_PLUGIN, true, 0.0, true, 7.0);
57 g_iCvarEnabled = GetConVarInt(g_hCvarEnabled);
58 HookConVarChange(g_hCvarEnabled, OnConVarChange);
59
60 g_hCvarHideTags = CreateConVar("sm_cccm_hidetags", "1", "Allow players to hide their chat tags\n0 = Disabled\n1 = Enabled", FCVAR_PLUGIN, true, 0.0, true, 1.0);
61 g_bCvarHideTags = GetConVarBool(g_hCvarHideTags);
62 HookConVarChange(g_hCvarHideTags, OnConVarChange);
63
64 AutoExecConfig(true, "plugin.custom-chatcolors-menu");
65
66 RegAdminCmd("sm_tag", Command_Color, ADMFLAG_RESERVATION, "Open Custom Chat Colors Menu");
67 RegAdminCmd("sm_reload_cccm", Command_Reload, ADMFLAG_ROOT, "Reloads Custom Chat Colors Menu config");
68 RegAdminCmd("sm_tagcolor", Command_TagColor, ADMFLAG_RESERVATION, "Change tag color to a specified hexadecimal value");
69 RegAdminCmd("sm_resettag", Command_ResetTagColor, ADMFLAG_RESERVATION, "Reset tag color to default");
70 RegAdminCmd("sm_namecolor", Command_NameColor, ADMFLAG_RESERVATION, "Change name color to a specified hexadecimal value");
71 RegAdminCmd("sm_resetname", Command_ResetNameColor, ADMFLAG_RESERVATION, "Reset name color to default");
72 RegAdminCmd("sm_chatcolor", Command_ChatColor, ADMFLAG_RESERVATION, "Change chat color to a specified hexadecimal value");
73 RegAdminCmd("sm_resetchat", Command_ResetChatColor, ADMFLAG_RESERVATION, "Reset chat color to default");
74
75 LoadTranslations("core.phrases");
76 LoadTranslations("common.phrases");
77 LoadTranslations("custom-chatcolors-menu.phrases");
78
79 g_hRegexHex = CompileRegex("([A-Fa-f0-9]{6})");
80
81 BuildPath(Path_SM, g_strConfigFile, sizeof(g_strConfigFile), "configs/custom-chatcolors-menu.cfg");
82
83 g_hSQL = INVALID_HANDLE;
84 if(SQL_CheckConfig("cccm"))
85 SQL_TConnect(SQLQuery_Connect, "cccm");
86}
87
88public OnConVarChange(Handle:hConvar, const String:strOldValue[], const String:strNewValue[])
89{
90 if(hConvar == g_hCvarEnabled)
91 g_iCvarEnabled = GetConVarInt(g_hCvarEnabled);
92 else if(hConvar == g_hCvarHideTags)
93 g_bCvarHideTags = GetConVarBool(g_hCvarHideTags);
94}
95
96public SQL_LoadColors(iClient)
97{
98 if(!IsClientAuthorized(iClient))
99 return;
100
101 if(g_hSQL != INVALID_HANDLE)
102 {
103 decl String:strAuth[32];
104 GetClientAuthString(iClient, strAuth, sizeof(strAuth));
105 strcopy(g_strAuth[iClient], sizeof(g_strAuth[]), strAuth);
106
107 decl String:strQuery[256];
108 Format(strQuery, sizeof(strQuery), "SELECT hidetag, tagcolor, namecolor, chatcolor FROM cccm_users WHERE auth = '%s'", g_strAuth[iClient]);
109 SQL_TQuery(g_hSQL, SQLQuery_LoadColors, strQuery, GetClientUserId(iClient), DBPrio_High);
110 }
111}
112
113public OnConfigsExecuted()
114{
115 Config_Load();
116}
117
118public OnClientConnected(iClient)
119{
120 g_bColorsLoaded[iClient] = false;
121 g_bHideTag[iClient] = false;
122 g_bAccessColor[iClient][TAG] = false;
123 g_bAccessColor[iClient][NAME] = false;
124 g_bAccessColor[iClient][CHAT] = false;
125 g_bAccessHideTags[iClient] = false;
126 strcopy(g_strAuth[iClient], sizeof(g_strAuth[]), "");
127 strcopy(g_strColor[iClient][TAG], sizeof(g_strColor[][]), "");
128 strcopy(g_strColor[iClient][NAME], sizeof(g_strColor[][]), "");
129 strcopy(g_strColor[iClient][CHAT], sizeof(g_strColor[][]), "");
130}
131
132public CCC_OnUserConfigLoaded(iClient)
133{
134 if(g_bColorsLoaded[iClient])
135 return;
136
137 decl String:strTag[7];
138 IntToString(CCC_GetColor(iClient, CCC_TagColor), strTag, sizeof(strTag));
139 if(IsValidHex(strTag))
140 strcopy(g_strColor[iClient][TAG], sizeof(g_strColor[][]), strTag);
141
142 decl String:strName[7];
143 IntToString(CCC_GetColor(iClient, CCC_TagColor), strName, sizeof(strName));
144 if(IsValidHex(strName))
145 strcopy(g_strColor[iClient][NAME], sizeof(g_strColor[][]), strName);
146
147 decl String:strChat[7];
148 IntToString(CCC_GetColor(iClient, CCC_TagColor), strChat, sizeof(strChat));
149 if(IsValidHex(strChat))
150 strcopy(g_strColor[iClient][CHAT], sizeof(g_strColor[][]), strChat);
151}
152
153public OnClientAuthorized(iClient, const String:strAuth[])
154{
155 strcopy(g_strAuth[iClient], sizeof(g_strAuth[]), strAuth);
156}
157
158public OnRebuildAdminCache(AdminCachePart:iPart)
159{
160 for(new i = 1; i <= MaxClients; i++) if(IsClientInGame(i))
161 {
162 OnClientConnected(i);
163 OnClientPostAdminCheck(i);
164 }
165}
166
167public OnClientPostAdminCheck(iClient)
168{
169 SQL_LoadColors(iClient);
170 if(CheckCommandAccess(iClient, "sm_ccc_tag", ADMFLAG_RESERVATION))
171 g_bAccessColor[iClient][TAG] = true;
172 if(CheckCommandAccess(iClient, "sm_ccc_name", ADMFLAG_RESERVATION))
173 g_bAccessColor[iClient][NAME] = true;
174 if(CheckCommandAccess(iClient, "sm_ccc_chat", ADMFLAG_RESERVATION))
175 g_bAccessColor[iClient][CHAT] = true;
176 if(CheckCommandAccess(iClient, "sm_ccc_hidetags", ADMFLAG_RESERVATION))
177 g_bAccessHideTags[iClient] = true;
178}
179
180public Action:CCC_OnColor(iClient, const String:strMessage[], CCC_ColorType:iType)
181{
182 if(iType == CCC_TagColor)
183 {
184 if(!(g_iCvarEnabled & ENABLEFLAG_TAG))
185 return Plugin_Handled;
186
187 if(g_bCvarHideTags && g_bHideTag[iClient] && g_bAccessHideTags[iClient])
188 return Plugin_Handled;
189
190 if(!StrEqual(g_strColor[iClient][TAG], "-1") && !IsValidHex(g_strColor[iClient][TAG]))
191 return Plugin_Handled;
192 }
193
194 if(iType == CCC_NameColor)
195 {
196 if(!(g_iCvarEnabled & ENABLEFLAG_NAME))
197 return Plugin_Handled;
198
199 if(!IsValidHex(g_strColor[iClient][NAME]))
200 return Plugin_Handled;
201 }
202
203 if(iType == CCC_ChatColor)
204 {
205 if(!(g_iCvarEnabled & ENABLEFLAG_CHAT))
206 return Plugin_Handled;
207
208 if(!IsValidHex(g_strColor[iClient][CHAT]))
209 return Plugin_Handled;
210 }
211
212 return Plugin_Continue;
213}
214
215// ====[ COMMANDS ]============================================================
216public Action:Command_Color(iClient, iArgs)
217{
218 if(!IsValidClient(iClient))
219 return Plugin_Continue;
220
221 Menu_Settings(iClient);
222 return Plugin_Handled;
223}
224
225public Action:Command_Reload(iClient, iArgs)
226{
227 Config_Load();
228 ReplyToCommand(iClient, "[SM] Configuration file %s reloaded.", g_strConfigFile);
229 return Plugin_Handled;
230}
231
232public Action:Command_TagColor(iClient, iArgs)
233{
234 if(!IsValidClient(iClient))
235 return Plugin_Continue;
236
237 if(iArgs != 1)
238 {
239 ReplyToCommand(iClient, "[SM] Usage: sm_tagcolor <hex>");
240 return Plugin_Handled;
241 }
242
243 decl String:strArg[32];
244 GetCmdArgString(strArg, sizeof(strArg));
245 ReplaceString(strArg, sizeof(strArg), "#", "", false);
246
247 if(!IsValidHex(strArg))
248 {
249 ReplyToCommand(iClient, "[SM] Usage: sm_tagcolor <hex>");
250 return Plugin_Handled;
251 }
252
253 PrintToChat(iClient, "\x01[SM] %T \x07%s#%s\x01", "TagSet", iClient, strArg, strArg);
254 strcopy(g_strColor[iClient][TAG], sizeof(g_strColor[][]), strArg);
255 CCC_SetColor(iClient, CCC_TagColor, StringToInt(strArg, 16), false);
256
257 if(g_hSQL != INVALID_HANDLE && IsClientAuthorized(iClient))
258 {
259 decl String:strQuery[256];
260 Format(strQuery, sizeof(strQuery), "SELECT tagcolor FROM cccm_users WHERE auth = '%s'", g_strAuth[iClient]);
261 SQL_TQuery(g_hSQL, SQLQuery_TagColor, strQuery, GetClientUserId(iClient), DBPrio_High);
262 }
263
264 return Plugin_Handled;
265}
266
267public Action:Command_ResetTagColor(iClient, iArgs)
268{
269 if(!IsValidClient(iClient))
270 return Plugin_Continue;
271
272 PrintToChat(iClient, "[SM] %T", "TagReset", iClient);
273 strcopy(g_strColor[iClient][TAG], sizeof(g_strColor[][]), "");
274 CCC_ResetColor(iClient, CCC_TagColor);
275
276 decl String:strTag[32];
277 IntToString(CCC_GetColor(iClient, CCC_TagColor), strTag, sizeof(strTag));
278 strcopy(g_strColor[iClient][TAG], sizeof(g_strColor[][]), strTag);
279
280 if(g_hSQL != INVALID_HANDLE && IsClientAuthorized(iClient))
281 {
282 decl String:strQuery[256];
283 Format(strQuery, sizeof(strQuery), "SELECT tagcolor FROM cccm_users WHERE auth = '%s'", g_strAuth[iClient]);
284 SQL_TQuery(g_hSQL, SQLQuery_TagColor, strQuery, GetClientUserId(iClient), DBPrio_High);
285 }
286
287 return Plugin_Handled;
288}
289
290public Action:Command_NameColor(iClient, iArgs)
291{
292 if(!IsValidClient(iClient))
293 return Plugin_Continue;
294
295 if(iArgs != 1)
296 {
297 ReplyToCommand(iClient, "[SM] Usage: sm_namecolor <hex>");
298 return Plugin_Handled;
299 }
300
301 decl String:strArg[32];
302 GetCmdArgString(strArg, sizeof(strArg));
303 ReplaceString(strArg, sizeof(strArg), "#", "", false);
304
305 if(!IsValidHex(strArg))
306 {
307 ReplyToCommand(iClient, "[SM] Usage: sm_namecolor <hex>");
308 return Plugin_Handled;
309 }
310
311 PrintToChat(iClient, "\x01[SM] %T \x07%s#%s\x01", "NameSet", iClient, strArg, strArg);
312 strcopy(g_strColor[iClient][TAG], sizeof(g_strColor[][]), strArg);
313 CCC_SetColor(iClient, CCC_TagColor, StringToInt(strArg, 16), false);
314
315 if(g_hSQL != INVALID_HANDLE && IsClientAuthorized(iClient))
316 {
317 decl String:strQuery[256];
318 Format(strQuery, sizeof(strQuery), "SELECT namecolor FROM cccm_users WHERE auth = '%s'", g_strAuth[iClient]);
319 SQL_TQuery(g_hSQL, SQLQuery_TagColor, strQuery, GetClientUserId(iClient), DBPrio_High);
320 }
321
322 return Plugin_Handled;
323}
324
325public Action:Command_ResetNameColor(iClient, iArgs)
326{
327 if(!IsValidClient(iClient))
328 return Plugin_Continue;
329
330 PrintToChat(iClient, "[SM] %T", "NameReset", iClient);
331 strcopy(g_strColor[iClient][NAME], sizeof(g_strColor[][]), "");
332 CCC_ResetColor(iClient, CCC_NameColor);
333
334 if(g_hSQL != INVALID_HANDLE && IsClientAuthorized(iClient))
335 {
336 decl String:strQuery[256];
337 Format(strQuery, sizeof(strQuery), "SELECT namecolor FROM cccm_users WHERE auth = '%s'", g_strAuth[iClient]);
338 SQL_TQuery(g_hSQL, SQLQuery_NameColor, strQuery, GetClientUserId(iClient), DBPrio_High);
339 }
340
341 return Plugin_Handled;
342}
343
344public Action:Command_ChatColor(iClient, iArgs)
345{
346 if(!IsValidClient(iClient))
347 return Plugin_Continue;
348
349 if(iArgs != 1)
350 {
351 ReplyToCommand(iClient, "[SM] Usage: sm_chatcolor <hex>");
352 return Plugin_Handled;
353 }
354
355 decl String:strArg[32];
356 GetCmdArgString(strArg, sizeof(strArg));
357 ReplaceString(strArg, sizeof(strArg), "#", "", false);
358
359 if(!IsValidHex(strArg))
360 {
361 ReplyToCommand(iClient, "[SM] Usage: sm_chatcolor <hex>");
362 return Plugin_Handled;
363 }
364
365 PrintToChat(iClient, "\x01[SM] %T \x07%s#%s\x01", "ChatSet", iClient, strArg, strArg);
366 strcopy(g_strColor[iClient][TAG], sizeof(g_strColor[][]), strArg);
367 CCC_SetColor(iClient, CCC_TagColor, StringToInt(strArg, 16), false);
368
369 if(g_hSQL != INVALID_HANDLE && IsClientAuthorized(iClient))
370 {
371 decl String:strQuery[256];
372 Format(strQuery, sizeof(strQuery), "SELECT chatcolor FROM cccm_users WHERE auth = '%s'", g_strAuth[iClient]);
373 SQL_TQuery(g_hSQL, SQLQuery_TagColor, strQuery, GetClientUserId(iClient), DBPrio_High);
374 }
375
376 return Plugin_Handled;
377}
378
379public Action:Command_ResetChatColor(iClient, iArgs)
380{
381 if(!IsValidClient(iClient))
382 return Plugin_Continue;
383
384 PrintToChat(iClient, "[SM] %T", "ChatReset", iClient);
385 strcopy(g_strColor[iClient][CHAT], sizeof(g_strColor[][]), "");
386 CCC_ResetColor(iClient, CCC_ChatColor);
387
388 if(g_hSQL != INVALID_HANDLE && IsClientAuthorized(iClient))
389 {
390 decl String:strQuery[256];
391 Format(strQuery, sizeof(strQuery), "SELECT chatcolor FROM cccm_users WHERE auth = '%s'", g_strAuth[iClient]);
392 SQL_TQuery(g_hSQL, SQLQuery_ChatColor, strQuery, GetClientUserId(iClient), DBPrio_High);
393 }
394
395 return Plugin_Handled;
396}
397
398// ====[ MENUS ]===============================================================
399public Menu_Settings(iClient)
400{
401 if(IsVoteInProgress())
402 return;
403
404 new Handle:hMenu = CreateMenu(MenuHandler_Settings);
405 SetMenuTitle(hMenu, "%T:", "Title", iClient);
406
407 decl String:strBuffer[32];
408 Format(strBuffer, sizeof(strBuffer), "%T", "ChangeTag", iClient);
409 if(g_iCvarEnabled & ENABLEFLAG_TAG && (g_bAccessColor[iClient][TAG] || (g_bCvarHideTags && g_bAccessHideTags[iClient])))
410 AddMenuItem(hMenu, "Tag", strBuffer);
411 else
412 AddMenuItem(hMenu, "", strBuffer, ITEMDRAW_DISABLED);
413
414 Format(strBuffer, sizeof(strBuffer), "%T", "ChangeName", iClient);
415 if(g_iCvarEnabled & ENABLEFLAG_NAME && g_bAccessColor[iClient][NAME])
416 AddMenuItem(hMenu, "Name", strBuffer);
417 else
418 AddMenuItem(hMenu, "", strBuffer, ITEMDRAW_DISABLED);
419
420 Format(strBuffer, sizeof(strBuffer), "%T", "ChangeChat", iClient);
421 if(g_iCvarEnabled & ENABLEFLAG_CHAT && g_bAccessColor[iClient][CHAT])
422 AddMenuItem(hMenu, "Chat", strBuffer);
423 else
424 AddMenuItem(hMenu, "", strBuffer, ITEMDRAW_DISABLED);
425
426 DisplayMenu(hMenu, iClient, MENU_TIME_FOREVER);
427}
428
429public MenuHandler_Settings(Handle:hMenu, MenuAction:iAction, iParam1, iParam2)
430{
431 if(iAction == MenuAction_End)
432 {
433 CloseHandle(hMenu);
434 return;
435 }
436
437 if(iAction == MenuAction_Select)
438 {
439 decl String:strBuffer[32];
440 GetMenuItem(hMenu, iParam2, strBuffer, sizeof(strBuffer));
441
442 if(StrEqual(strBuffer, "Tag"))
443 Menu_TagColor(iParam1);
444 else if(StrEqual(strBuffer, "Name"))
445 Menu_NameColor(iParam1);
446 else if(StrEqual(strBuffer, "Chat"))
447 Menu_ChatColor(iParam1);
448 }
449}
450
451public Menu_TagColor(iClient)
452{
453 if(IsVoteInProgress())
454 return;
455
456 new Handle:hMenu = CreateMenu(MenuHandler_TagColor);
457 SetMenuTitle(hMenu, "%T:", "TagColor", iClient);
458 SetMenuExitBackButton(hMenu, true);
459
460 decl String:strBuffer[32];
461 if(g_bCvarHideTags && g_bAccessHideTags[iClient])
462 {
463 if(!g_bHideTag[iClient])
464 {
465 Format(strBuffer, sizeof(strBuffer), "%T", "HideTag", iClient);
466 AddMenuItem(hMenu, "HideTag", strBuffer);
467 }
468 else
469 {
470 Format(strBuffer, sizeof(strBuffer), "%T", "ShowTag", iClient);
471 AddMenuItem(hMenu, "HideTag", strBuffer);
472 }
473 }
474
475 Format(strBuffer, sizeof(strBuffer), "%T", "Reset", iClient);
476 AddMenuItem(hMenu, "Reset", strBuffer);
477
478 decl String:strColorIndex[4];
479 for(new i = 0; i < g_iColorCount; i++)
480 {
481 if(!g_bColorAdminFlags[i] || (g_bColorAdminFlags[i] && HasAdminFlag(iClient, g_iColorFlagList[i])))
482 {
483 IntToString(i, strColorIndex, sizeof(strColorIndex));
484 AddMenuItem(hMenu, strColorIndex, g_strColorName[i]);
485 }
486 }
487
488 DisplayMenu(hMenu, iClient, MENU_TIME_FOREVER);
489}
490
491public MenuHandler_TagColor(Handle:hMenu, MenuAction:iAction, iParam1, iParam2)
492{
493 if(iAction == MenuAction_End)
494 {
495 CloseHandle(hMenu);
496 return;
497 }
498
499 if(iAction == MenuAction_Cancel && iParam2 == MenuCancel_ExitBack)
500 {
501 Menu_Settings(iParam1);
502 return;
503 }
504
505 if(iAction == MenuAction_Select)
506 {
507 decl String:strBuffer[32];
508 GetMenuItem(hMenu, iParam2, strBuffer, sizeof(strBuffer));
509
510 if(StrEqual(strBuffer, "HideTag"))
511 {
512 if(g_bHideTag[iParam1])
513 {
514 g_bHideTag[iParam1] = false;
515 PrintToChat(iParam1, "[SM] %T", "TagEnabled", iParam1);
516 }
517 else
518 {
519 g_bHideTag[iParam1] = true;
520 PrintToChat(iParam1, "[SM] %T", "TagDisabled", iParam1);
521 }
522
523 if(g_hSQL != INVALID_HANDLE && IsClientAuthorized(iParam1))
524 {
525 decl String:strQuery[256];
526 Format(strQuery, sizeof(strQuery), "SELECT hidetag FROM cccm_users WHERE auth = '%s'", g_strAuth[iParam1]);
527 SQL_TQuery(g_hSQL, SQLQuery_HideTag, strQuery, GetClientUserId(iParam1), DBPrio_High);
528 }
529 }
530 else
531 {
532 if(StrEqual(strBuffer, "Reset"))
533 {
534 PrintToChat(iParam1, "[SM] %T", "TagReset", iParam1);
535 strcopy(g_strColor[iParam1][TAG], sizeof(g_strColor[][]), "");
536 CCC_ResetColor(iParam1, CCC_TagColor);
537
538 decl String:strTag[32];
539 IntToString(CCC_GetColor(iParam1, CCC_TagColor), strTag, sizeof(strTag));
540 strcopy(g_strColor[iParam1][TAG], sizeof(g_strColor[][]), strTag);
541 }
542 else
543 {
544 new iColorIndex = StringToInt(strBuffer);
545 PrintToChat(iParam1, "\x01[SM] %T \x07%s%s\x01", "TagSet", iParam1, g_strColorHex[iColorIndex], g_strColorName[iColorIndex]);
546 strcopy(g_strColor[iParam1][TAG], sizeof(g_strColor[][]), g_strColorHex[iColorIndex]);
547 CCC_SetColor(iParam1, CCC_TagColor, StringToInt(g_strColorHex[iColorIndex], 16), false);
548 }
549
550 if(g_hSQL != INVALID_HANDLE && IsClientAuthorized(iParam1))
551 {
552 decl String:strQuery[256];
553 Format(strQuery, sizeof(strQuery), "SELECT tagcolor FROM cccm_users WHERE auth = '%s'", g_strAuth[iParam1]);
554 SQL_TQuery(g_hSQL, SQLQuery_TagColor, strQuery, GetClientUserId(iParam1), DBPrio_High);
555 }
556 }
557
558 Menu_Settings(iParam1);
559 }
560}
561
562public Menu_NameColor(iClient)
563{
564 if(IsVoteInProgress())
565 return;
566
567 new Handle:hMenu = CreateMenu(MenuHandler_NameColor);
568 SetMenuTitle(hMenu, "%T:", "NameColor", iClient);
569 SetMenuExitBackButton(hMenu, true);
570
571 decl String:strBuffer[32];
572 Format(strBuffer, sizeof(strBuffer), "%T", "Reset", iClient);
573 AddMenuItem(hMenu, "Reset", strBuffer);
574
575 decl String:strColorIndex[4];
576 for(new i = 0; i < g_iColorCount; i++)
577 {
578 if(!g_bColorAdminFlags[i] || (g_bColorAdminFlags[i] && HasAdminFlag(iClient, g_iColorFlagList[i])))
579 {
580 IntToString(i, strColorIndex, sizeof(strColorIndex));
581 AddMenuItem(hMenu, strColorIndex, g_strColorName[i]);
582 }
583 }
584
585 DisplayMenu(hMenu, iClient, MENU_TIME_FOREVER);
586}
587
588public MenuHandler_NameColor(Handle:hMenu, MenuAction:iAction, iParam1, iParam2)
589{
590 if(iAction == MenuAction_End)
591 {
592 CloseHandle(hMenu);
593 return;
594 }
595
596 if(iAction == MenuAction_Cancel && iParam2 == MenuCancel_ExitBack)
597 {
598 Menu_Settings(iParam1);
599 return;
600 }
601
602 if(iAction == MenuAction_Select)
603 {
604 decl String:strBuffer[32];
605 GetMenuItem(hMenu, iParam2, strBuffer, sizeof(strBuffer));
606
607 if(StrEqual(strBuffer, "Reset"))
608 {
609 PrintToChat(iParam1, "[SM] %T", "NameReset", iParam1);
610 strcopy(g_strColor[iParam1][NAME], sizeof(g_strColor[][]), "");
611 CCC_ResetColor(iParam1, CCC_NameColor);
612 }
613 else
614 {
615 new iColorIndex = StringToInt(strBuffer);
616 PrintToChat(iParam1, "\x01[SM] %T \x07%s%s\x01", "NameSet", iParam1, g_strColorHex[iColorIndex], g_strColorName[iColorIndex]);
617 strcopy(g_strColor[iParam1][NAME], sizeof(g_strColor[][]), g_strColorHex[iColorIndex]);
618 CCC_SetColor(iParam1, CCC_NameColor, StringToInt(g_strColorHex[iColorIndex], 16), false);
619 }
620
621 if(g_hSQL != INVALID_HANDLE && IsClientAuthorized(iParam1))
622 {
623 decl String:strQuery[256];
624 Format(strQuery, sizeof(strQuery), "SELECT namecolor FROM cccm_users WHERE auth = '%s'", g_strAuth[iParam1]);
625 SQL_TQuery(g_hSQL, SQLQuery_NameColor, strQuery, GetClientUserId(iParam1), DBPrio_High);
626 }
627
628 Menu_Settings(iParam1);
629 }
630}
631
632public Menu_ChatColor(iClient)
633{
634 if(IsVoteInProgress())
635 return;
636
637 new Handle:hMenu = CreateMenu(MenuHandler_ChatColor);
638 SetMenuTitle(hMenu, "%T:", "ChatColor", iClient);
639 SetMenuExitBackButton(hMenu, true);
640
641 decl String:strBuffer[32];
642 Format(strBuffer, sizeof(strBuffer), "%T", "Reset", iClient);
643 AddMenuItem(hMenu, "Reset", strBuffer);
644
645 decl String:strColorIndex[4];
646 for(new i = 0; i < g_iColorCount; i++)
647 {
648 if(!g_bColorAdminFlags[i] || (g_bColorAdminFlags[i] && HasAdminFlag(iClient, g_iColorFlagList[i])))
649 {
650 IntToString(i, strColorIndex, sizeof(strColorIndex));
651 AddMenuItem(hMenu, strColorIndex, g_strColorName[i]);
652 }
653 }
654
655 DisplayMenu(hMenu, iClient, MENU_TIME_FOREVER);
656}
657
658public MenuHandler_ChatColor(Handle:hMenu, MenuAction:iAction, iParam1, iParam2)
659{
660 if(iAction == MenuAction_End)
661 {
662 CloseHandle(hMenu);
663 return;
664 }
665
666 if(iAction == MenuAction_Cancel && iParam2 == MenuCancel_ExitBack)
667 {
668 Menu_Settings(iParam1);
669 return;
670 }
671
672 if(iAction == MenuAction_Select)
673 {
674 decl String:strBuffer[32];
675 GetMenuItem(hMenu, iParam2, strBuffer, sizeof(strBuffer));
676
677 if(StrEqual(strBuffer, "Reset"))
678 {
679 PrintToChat(iParam1, "[SM] %T", "ChatReset", iParam1);
680 strcopy(g_strColor[iParam1][CHAT], sizeof(g_strColor[][]), "");
681 CCC_ResetColor(iParam1, CCC_ChatColor);
682 }
683 else
684 {
685 new iColorIndex = StringToInt(strBuffer);
686 PrintToChat(iParam1, "\x01[SM] %T \x07%s%s\x01", "ChatSet", iParam1, g_strColorHex[iColorIndex], g_strColorName[iColorIndex]);
687 strcopy(g_strColor[iParam1][CHAT], sizeof(g_strColor[][]), g_strColorHex[iColorIndex]);
688 CCC_SetColor(iParam1, CCC_ChatColor, StringToInt(g_strColorHex[iColorIndex], 16), false);
689 }
690
691 if(g_hSQL != INVALID_HANDLE && IsClientAuthorized(iParam1))
692 {
693 decl String:strQuery[256];
694 Format(strQuery, sizeof(strQuery), "SELECT chatcolor FROM cccm_users WHERE auth = '%s'", g_strAuth[iParam1]);
695 SQL_TQuery(g_hSQL, SQLQuery_ChatColor, strQuery, GetClientUserId(iParam1), DBPrio_High);
696 }
697
698 Menu_Settings(iParam1);
699 }
700}
701
702// ====[ CONFIGURATION ]=======================================================
703public Config_Load()
704{
705 if(!FileExists(g_strConfigFile))
706 {
707 SetFailState("Configuration file %s not found!", g_strConfigFile);
708 return;
709 }
710
711 new Handle:hKeyValues = CreateKeyValues("CCC Menu Colors");
712 if(!FileToKeyValues(hKeyValues, g_strConfigFile))
713 {
714 SetFailState("Improper structure for configuration file %s!", g_strConfigFile);
715 return;
716 }
717
718 if(!KvGotoFirstSubKey(hKeyValues))
719 {
720 SetFailState("Can't find configuration file %s!", g_strConfigFile);
721 return;
722 }
723
724 for(new i = 0; i < MAX_COLORS; i++)
725 {
726 strcopy(g_strColorName[i], sizeof(g_strColorName[]), "");
727 strcopy(g_strColorHex[i], sizeof(g_strColorHex[]), "");
728 strcopy(g_strColorFlags[i], sizeof(g_strColorFlags[]), "");
729 g_bColorAdminFlags[i] = false;
730 for(new i2 = 0; i2 < 16; i2++)
731 g_iColorFlagList[i][i2] = AdminFlag:-1;
732 }
733
734 g_iColorCount = 0;
735 do
736 {
737 KvGetString(hKeyValues, "name", g_strColorName[g_iColorCount], sizeof(g_strColorName[]));
738 KvGetString(hKeyValues, "hex", g_strColorHex[g_iColorCount], sizeof(g_strColorHex[]));
739 ReplaceString(g_strColorHex[g_iColorCount], sizeof(g_strColorHex[]), "#", "", false);
740 KvGetString(hKeyValues, "flags", g_strColorFlags[g_iColorCount], sizeof(g_strColorFlags[]));
741
742 if(!IsValidHex(g_strColorHex[g_iColorCount]))
743 {
744 LogError("Invalid hexadecimal value for color %s.", g_strColorName[g_iColorCount]);
745 strcopy(g_strColorName[g_iColorCount], sizeof(g_strColorName[]), "");
746 strcopy(g_strColorHex[g_iColorCount], sizeof(g_strColorHex[]), "");
747 strcopy(g_strColorFlags[g_iColorCount], sizeof(g_strColorFlags[]), "");
748 }
749
750 if(!StrEqual(g_strColorFlags[g_iColorCount], ""))
751 {
752 g_bColorAdminFlags[g_iColorCount] = true;
753 FlagBitsToArray(ReadFlagString(g_strColorFlags[g_iColorCount]), g_iColorFlagList[g_iColorCount], sizeof(g_iColorFlagList[]));
754 }
755
756 g_iColorCount++;
757 }
758 while(KvGotoNextKey(hKeyValues));
759 CloseHandle(hKeyValues);
760
761 LogMessage("Loaded %i colors from configuration file %s.", g_iColorCount, g_strConfigFile);
762}
763
764// ====[ SQL QUERIES ]=========================================================
765public SQLQuery_Connect(Handle:hOwner, Handle:hQuery, const String:strError[], any:iData)
766{
767 if(hQuery == INVALID_HANDLE)
768 return;
769
770 g_hSQL = hQuery;
771 SQL_GetDriverIdent(hOwner, g_strSQLDriver, sizeof(g_strSQLDriver));
772
773 if(StrEqual(g_strSQLDriver, "mysql", false))
774 {
775 LogMessage("MySQL server configured. Variable saving enabled.");
776 SQL_TQuery(g_hSQL, SQLQuery_Update, "CREATE TABLE IF NOT EXISTS cccm_users (id INT(64) NOT NULL AUTO_INCREMENT, auth varchar(32) UNIQUE, hidetag varchar(1), tagcolor varchar(7), namecolor varchar(7), chatcolor varchar(7), PRIMARY KEY (id))", _, DBPrio_High);
777 }
778 else if(StrEqual(g_strSQLDriver, "sqlite", false))
779 {
780 LogMessage("SQlite server configured. Variable saving enabled.");
781 SQL_TQuery(g_hSQL, SQLQuery_Update, "CREATE TABLE IF NOT EXISTS cccm_users (id INTERGER PRIMARY KEY, auth varchar(32) UNIQUE, hidetag varchar(1), tagcolor varchar(7), namecolor varchar(7), chatcolor varchar(7))", _, DBPrio_High);
782 }
783 else
784 {
785 LogMessage("Saved variable server not configured. Variable saving disabled.");
786 return;
787 }
788
789 for(new i = 1; i <= MaxClients; ++i) if(IsClientInGame(i))
790 SQL_LoadColors(i);
791}
792
793public SQLQuery_LoadColors(Handle:hOwner, Handle:hQuery, const String:strError[], any:iData)
794{
795 new iClient = GetClientOfUserId(iData);
796 if(!IsValidClient(iClient))
797 return;
798
799 if(hOwner == INVALID_HANDLE || hQuery == INVALID_HANDLE)
800 {
801 LogError("SQL Error: %s", strError);
802 return;
803 }
804
805 if(SQL_FetchRow(hQuery) && SQL_GetRowCount(hQuery) != 0)
806 {
807 g_bHideTag[iClient] = bool:SQL_FetchInt(hQuery, 0);
808
809 decl String:strTag[7];
810 SQL_FetchString(hQuery, 1, strTag, sizeof(strTag));
811 if(IsValidHex(strTag))
812 {
813 strcopy(g_strColor[iClient][TAG], sizeof(g_strColor[][]), strTag);
814 CCC_SetColor(iClient, CCC_TagColor, StringToInt(g_strColor[iClient][TAG], 16), false);
815 }
816 else if(StrEqual(strTag, "-1"))
817 strcopy(g_strColor[iClient][TAG], sizeof(g_strColor[][]), "-1");
818
819 decl String:strName[7];
820 SQL_FetchString(hQuery, 2, strName, sizeof(strName));
821 if(IsValidHex(strName))
822 {
823 strcopy(g_strColor[iClient][NAME], sizeof(g_strColor[][]), strName);
824 CCC_SetColor(iClient, CCC_NameColor, StringToInt(g_strColor[iClient][NAME], 16), false);
825 }
826
827 decl String:strChat[7];
828 SQL_FetchString(hQuery, 3, strChat, sizeof(strChat));
829 if(IsValidHex(strChat))
830 {
831 strcopy(g_strColor[iClient][CHAT], sizeof(g_strColor[][]), strChat);
832 CCC_SetColor(iClient, CCC_ChatColor, StringToInt(g_strColor[iClient][CHAT], 16), false);
833 }
834
835 g_bColorsLoaded[iClient] = true;
836 }
837}
838
839public SQLQuery_HideTag(Handle:hOwner, Handle:hQuery, const String:strError[], any:iClientId)
840{
841 new iClient = GetClientOfUserId(iClientId);
842 if(!IsValidClient(iClient))
843 return;
844
845 if(hOwner == INVALID_HANDLE || hQuery == INVALID_HANDLE)
846 {
847 LogError("SQL Error: %s", strError);
848 return;
849 }
850
851 if(SQL_GetRowCount(hQuery) == 0)
852 {
853 decl String:strQuery[256];
854 Format(strQuery, sizeof(strQuery), "INSERT INTO cccm_users (hidetag, auth) VALUES (%i, '%s')", g_bHideTag[iClient], g_strAuth[iClient]);
855 SQL_TQuery(g_hSQL, SQLQuery_Update, strQuery, _, DBPrio_High);
856 }
857 else
858 {
859 decl String:strQuery[256];
860 Format(strQuery, sizeof(strQuery), "UPDATE cccm_users SET hidetag = '%i' WHERE auth = '%s'", g_bHideTag[iClient], g_strAuth[iClient]);
861 SQL_TQuery(g_hSQL, SQLQuery_Update, strQuery, _, DBPrio_Normal);
862 }
863}
864
865public SQLQuery_TagColor(Handle:hOwner, Handle:hQuery, const String:strError[], any:iClientId)
866{
867 new iClient = GetClientOfUserId(iClientId);
868 if(!IsValidClient(iClient))
869 return;
870
871 if(hOwner == INVALID_HANDLE || hQuery == INVALID_HANDLE)
872 {
873 LogError("SQL Error: %s", strError);
874 return;
875 }
876
877 if(SQL_GetRowCount(hQuery) == 0)
878 {
879 decl String:strQuery[256];
880 Format(strQuery, sizeof(strQuery), "INSERT INTO cccm_users (tagcolor, auth) VALUES ('%s', '%s')", g_strColor[iClient][TAG], g_strAuth[iClient]);
881 SQL_TQuery(g_hSQL, SQLQuery_Update, strQuery, _, DBPrio_High);
882 }
883 else
884 {
885 decl String:strQuery[256];
886 Format(strQuery, sizeof(strQuery), "UPDATE cccm_users SET tagcolor = '%s' WHERE auth = '%s'", g_strColor[iClient][TAG], g_strAuth[iClient]);
887 SQL_TQuery(g_hSQL, SQLQuery_Update, strQuery, _, DBPrio_Normal);
888 }
889}
890
891public SQLQuery_NameColor(Handle:hOwner, Handle:hQuery, const String:strError[], any:iClientId)
892{
893 new iClient = GetClientOfUserId(iClientId);
894 if(!IsValidClient(iClient))
895 return;
896
897 if(hOwner == INVALID_HANDLE || hQuery == INVALID_HANDLE)
898 {
899 LogError("SQL Error: %s", strError);
900 return;
901 }
902
903 if(SQL_GetRowCount(hQuery) == 0)
904 {
905 decl String:strQuery[256];
906 Format(strQuery, sizeof(strQuery), "INSERT INTO cccm_users (namecolor, auth) VALUES ('%s', '%s')", g_strColor[iClient][NAME], g_strAuth[iClient]);
907 SQL_TQuery(g_hSQL, SQLQuery_Update, strQuery, _, DBPrio_High);
908 }
909 else
910 {
911 decl String:strQuery[256];
912 Format(strQuery, sizeof(strQuery), "UPDATE cccm_users SET namecolor = '%s' WHERE auth = '%s'", g_strColor[iClient][NAME], g_strAuth[iClient]);
913 SQL_TQuery(g_hSQL, SQLQuery_Update, strQuery, _, DBPrio_Normal);
914 }
915}
916
917public SQLQuery_ChatColor(Handle:hOwner, Handle:hQuery, const String:strError[], any:iClientId)
918{
919 new iClient = GetClientOfUserId(iClientId);
920 if(!IsValidClient(iClient))
921 return;
922
923 if(hOwner == INVALID_HANDLE || hQuery == INVALID_HANDLE)
924 {
925 LogError("SQL Error: %s", strError);
926 return;
927 }
928
929 if(SQL_GetRowCount(hQuery) == 0)
930 {
931 decl String:strQuery[256];
932 Format(strQuery, sizeof(strQuery), "INSERT INTO cccm_users (chatcolor, auth) VALUES ('%s', '%s')", g_strColor[iClient][CHAT], g_strAuth[iClient]);
933 SQL_TQuery(g_hSQL, SQLQuery_Update, strQuery, _, DBPrio_High);
934 }
935 else
936 {
937 decl String:strQuery[256];
938 Format(strQuery, sizeof(strQuery), "UPDATE cccm_users SET chatcolor = '%s' WHERE auth = '%s'", g_strColor[iClient][CHAT], g_strAuth[iClient]);
939 SQL_TQuery(g_hSQL, SQLQuery_Update, strQuery, _, DBPrio_Normal);
940 }
941}
942
943public SQLQuery_Update(Handle:hOwner, Handle:hQuery, const String:strError[], any:iData)
944{
945 if(hQuery == INVALID_HANDLE)
946 LogError("SQL Error: %s", strError);
947}
948
949// ====[ STOCKS ]==============================================================
950stock bool:IsValidClient(iClient)
951{
952 if(iClient <= 0 || iClient > MaxClients || !IsClientInGame(iClient))
953 return false;
954 return true;
955}
956
957stock bool:IsValidHex(const String:strHex[])
958{
959 if(strlen(strHex) == 6 && MatchRegex(g_hRegexHex, strHex))
960 return true;
961 return false;
962}
963
964stock bool:HasAdminFlag(iClient, const AdminFlag:iFlagList[16])
965{
966 new iFlags = GetUserFlagBits(iClient);
967 if(iFlags & ADMFLAG_ROOT)
968 return true;
969
970 for(new i = 0; i < sizeof(iFlagList); i++)
971 {
972 if(iFlags & FlagToBit(iFlagList[i]))
973 return true;
974 }
975 return false;
976}