Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sourcemod>
- #include <sdktools>
- #include <chat-processor>
- #include <colorvariables>
- #pragma semicolon 1
- #pragma newdecls required
- #define PLUGIN_VERSION "1.0.1"
- public Plugin myinfo = {
- name = "[Source 2013] Chat-Processor Module: Chat Tags",
- author = "404 (abrandnewday)",
- description = "Processes chat and provides colors and tags for Source 2013 games",
- version = PLUGIN_VERSION,
- url = "http://www.unfgaming.net"
- };
- #define MAXLENGTH_NAME_NEW 256
- Handle hColorForward;
- Handle hNameForward;
- Handle hTagForward;
- Handle hApplicationForward;
- Handle hMessageForward;
- Handle hPreLoadedForward;
- Handle hLoadedForward;
- Handle hConfigReloadedForward;
- char g_strTag[MAXPLAYERS + 1][32];
- char g_strTagColor[MAXPLAYERS + 1][32];
- char g_strUsernameColor[MAXPLAYERS + 1][12];
- char g_strChatColor[MAXPLAYERS + 1][12];
- char g_strDefaultTag[MAXPLAYERS + 1][32];
- char g_strDefaultTagColor[MAXPLAYERS + 1][32];
- char g_strDefaultUsernameColor[MAXPLAYERS + 1][12];
- char g_strDefaultChatColor[MAXPLAYERS + 1][12];
- KeyValues kvConfigFile = null;
- enum CPT_ColorType {
- CPT_TagColor,
- CPT_NameColor,
- CPT_ChatColor
- };
- #define COLOR_NONE -1
- #define COLOR_GREEN -2
- #define COLOR_OLIVE -3
- #define COLOR_TEAM -4
- #define UPDATE_FILE "chatcolors.txt"
- public APLRes AskPluginLoad2(Handle hMyself, bool bLate, char[] strError, int iErrMax)
- {
- CreateNative("CPT_GetColor", Native_GetColor);
- CreateNative("CPT_SetColor", Native_SetColor);
- CreateNative("CPT_GetTag", Native_GetTag);
- CreateNative("CPT_SetTag", Native_SetTag);
- CreateNative("CPT_ResetColor", Native_ResetColor);
- CreateNative("CPT_ResetTag", Native_ResetTag);
- RegPluginLibrary("cpt");
- return APLRes_Success;
- }
- public void OnPluginStart()
- {
- RegAdminCmd("sm_reloadtags", Command_ReloadConfig, ADMFLAG_CONFIG, "Reloads Custom Chat Colors config file");
- hColorForward = CreateGlobalForward("CPT_OnChatColor", ET_Event, Param_Cell);
- hNameForward = CreateGlobalForward("CPT_OnNameColor", ET_Event, Param_Cell);
- hTagForward = CreateGlobalForward("CPT_OnTagApplied", ET_Event, Param_Cell);
- hApplicationForward = CreateGlobalForward("CPT_OnColor", ET_Event, Param_Cell, Param_String, Param_Cell);
- hMessageForward = CreateGlobalForward("CPT_OnChatMessage", ET_Ignore, Param_Cell, Param_String, Param_Cell);
- hPreLoadedForward = CreateGlobalForward("CPT_OnUserConfigPreLoaded", ET_Event, Param_Cell);
- hLoadedForward = CreateGlobalForward("CPT_OnUserConfigLoaded", ET_Ignore, Param_Cell);
- hConfigReloadedForward = CreateGlobalForward("CPT_OnConfigReloaded", ET_Ignore);
- LoadConfig();
- }
- void LoadConfig()
- {
- if(kvConfigFile != null)
- {
- delete kvConfigFile;
- }
- kvConfigFile = new KeyValues("chat_processor_tags");
- char strPath[64];
- BuildPath(Path_SM, strPath, sizeof(strPath), "configs/chat-processor-tags.cfg");
- if(!kvConfigFile.ImportFromFile(strPath))
- {
- SetFailState("Config file missing");
- }
- for(int i = 1; i <= MaxClients; i++)
- {
- if(!IsClientInGame(i) || IsFakeClient(i))
- {
- continue;
- }
- ClearValues(i);
- OnClientPostAdminCheck(i);
- }
- }
- public Action Command_ReloadConfig(int iClient, int iArgs)
- {
- LoadConfig();
- LogAction(iClient, -1, "Reloaded Chat-Processor Tags config file");
- ReplyToCommand(iClient, "[CPT] Reloaded config file.");
- Call_StartForward(hConfigReloadedForward);
- Call_Finish();
- return Plugin_Handled;
- }
- stock void ClearValues(int iClient)
- {
- Format(g_strTag[iClient], sizeof(g_strTag[]), "");
- Format(g_strTagColor[iClient], sizeof(g_strTagColor[]), "");
- Format(g_strUsernameColor[iClient], sizeof(g_strUsernameColor[]), "");
- Format(g_strChatColor[iClient], sizeof(g_strChatColor[]), "");
- Format(g_strDefaultTag[iClient], sizeof(g_strDefaultTag[]), "");
- Format(g_strDefaultTagColor[iClient], sizeof(g_strDefaultTagColor[]), "");
- Format(g_strDefaultUsernameColor[iClient], sizeof(g_strDefaultUsernameColor[]), "");
- Format(g_strDefaultChatColor[iClient], sizeof(g_strDefaultChatColor[]), "");
- }
- public void OnClientConnected(int iClient)
- {
- ClearValues(iClient);
- }
- public void OnClientDisconnect(int iClient)
- {
- ClearValues(iClient); // On connect and on disconnect, just to be safe
- }
- public void OnClientPostAdminCheck(int iClient)
- {
- if(!ConfigForward(iClient))
- {
- return; // Another plugin wants to block this
- }
- // check the Steam ID first
- char strAuthId2[32];
- GetClientAuthId(iClient, AuthId_Steam2, strAuthId2, sizeof(strAuthId2));
- kvConfigFile.Rewind();
- if(!kvConfigFile.JumpToKey(strAuthId2))
- {
- kvConfigFile.Rewind();
- kvConfigFile.GotoFirstSubKey();
- AdminId iAdmin = GetUserAdmin(iClient);
- AdminFlag fFlag;
- char strConfigFlag[2];
- char strSection[32];
- bool bFound = false;
- do
- {
- kvConfigFile.GetSectionName(strSection, sizeof(strSection));
- kvConfigFile.GetString("flag", strConfigFlag, sizeof(strConfigFlag));
- if(strlen(strConfigFlag) > 1)
- {
- LogError("Multiple flags given in section \"%s\", which is not allowed. Using first character.", strSection);
- }
- if(strlen(strConfigFlag) == 0 && StrContains(strSection, "STEAM_", false) == -1 && StrContains(strSection, "[U:1:", false) == -1)
- {
- bFound = true;
- break;
- }
- if(!FindFlagByChar(strConfigFlag[0], fFlag))
- {
- if(strlen(strConfigFlag) > 0)
- {
- LogError("Invalid flag given for section \"%s\", skipping", strSection);
- }
- continue;
- }
- if(GetAdminFlag(iAdmin, fFlag))
- {
- bFound = true;
- break;
- }
- } while(kvConfigFile.GotoNextKey());
- if(!bFound)
- {
- return;
- }
- }
- char strClientTagColor[12];
- char strClientNameColor[12];
- char strClientChatColor[12];
- kvConfigFile.GetString("tag", g_strTag[iClient], sizeof(g_strTag[]));
- kvConfigFile.GetString("tagcolor", strClientTagColor, sizeof(strClientTagColor));
- kvConfigFile.GetString("namecolor", strClientNameColor, sizeof(strClientNameColor));
- kvConfigFile.GetString("textcolor", strClientChatColor, sizeof(strClientChatColor));
- ReplaceString(strClientTagColor, sizeof(strClientTagColor), "#", "");
- ReplaceString(strClientNameColor, sizeof(strClientNameColor), "#", "");
- ReplaceString(strClientChatColor, sizeof(strClientChatColor), "#", "");
- int iTagLength = strlen(strClientTagColor);
- int iNameLength = strlen(strClientNameColor);
- int iChatLength = strlen(strClientChatColor);
- if(iTagLength == 6 || iTagLength == 8 || StrEqual(strClientTagColor, "T", false) || StrEqual(strClientTagColor, "G", false) || StrEqual(strClientTagColor, "O", false))
- {
- strcopy(g_strTagColor[iClient], sizeof(g_strTagColor[]), strClientTagColor);
- }
- if(iNameLength == 6 || iNameLength == 8 || StrEqual(strClientNameColor, "G", false) || StrEqual(strClientNameColor, "O", false))
- {
- strcopy(g_strUsernameColor[iClient], sizeof(g_strUsernameColor[]), strClientNameColor);
- }
- if(iChatLength == 6 || iChatLength == 8 || StrEqual(strClientChatColor, "T", false) || StrEqual(strClientChatColor, "G", false) || StrEqual(strClientChatColor, "O", false))
- {
- strcopy(g_strChatColor[iClient], sizeof(g_strChatColor[]), strClientChatColor);
- }
- strcopy(g_strDefaultTag[iClient], sizeof(g_strDefaultTag[]), g_strTag[iClient]);
- strcopy(g_strDefaultTagColor[iClient], sizeof(g_strDefaultTagColor[]), g_strTagColor[iClient]);
- strcopy(g_strDefaultUsernameColor[iClient], sizeof(g_strDefaultUsernameColor[]), g_strUsernameColor[iClient]);
- strcopy(g_strDefaultChatColor[iClient], sizeof(g_strDefaultChatColor[]), g_strChatColor[iClient]);
- Call_StartForward(hLoadedForward);
- Call_PushCell(iClient);
- Call_Finish();
- }
- forward Action OnChatMessage(int& author, Handle recipients, eChatFlags& flag, char[] name, char[] message, bool& bProcessColors, bool& bRemoveColors);
- {
- return Plugin_Handled;
- }
- forward void OnChatMessagePost(int author, Handle recipients, eChatFlags flag, const char[] name, const char[] message, bool bProcessColors, bool bRemoveColors);
- {
- char strNewName[MAXLENGTH_NAME_NEW];
- strcopy(strNewName, sizeof(strNewName), name);
- char strNewMessage[MAXLENGTH_MESSAGE];
- strcopy(strNewMessage, sizeof(strNewMessage), message);
- if(CheckForward(iAuthor, strNewMessage, CPT_NameColor))
- {
- if(StrEqual(g_strUsernameColor[iAuthor], "G", false))
- {
- Format(strNewName, MAXLENGTH_NAME_NEW, "\x04%s", strName);
- }
- else if(StrEqual(g_strUsernameColor[iAuthor], "O", false))
- {
- Format(strNewName, MAXLENGTH_NAME_NEW, "\x05%s", strName);
- }
- else if(strlen(g_strUsernameColor[iAuthor]) == 6)
- {
- Format(strNewName, MAXLENGTH_NAME_NEW, "\x07%s%s", g_strUsernameColor[iAuthor], strName);
- }
- else if(strlen(g_strUsernameColor[iAuthor]) == 8)
- {
- Format(strNewName, MAXLENGTH_NAME_NEW, "\x08%s%s", g_strUsernameColor[iAuthor], strName);
- }
- else
- {
- Format(strNewName, MAXLENGTH_NAME_NEW, "\x03%s", strName); // team color by default!
- }
- }
- else
- {
- Format(strNewName, MAXLENGTH_NAME_NEW, "\x03%s", strName); // team color by default!
- }
- if(CheckForward(iAuthor, strNewMessage, CPT_TagColor))
- {
- if(strlen(g_strTag[iAuthor]) > 0)
- {
- if(StrEqual(g_strTagColor[iAuthor], "T", false))
- {
- Format(strNewName, MAXLENGTH_NAME_NEW, "\x03%s%s", g_strTag[iAuthor], strName);
- }
- else if(StrEqual(g_strTagColor[iAuthor], "G", false))
- {
- Format(strNewName, MAXLENGTH_NAME_NEW, "\x04%s%s", g_strTag[iAuthor], strName);
- }
- else if(StrEqual(g_strTagColor[iAuthor], "O", false))
- {
- Format(strNewName, MAXLENGTH_NAME_NEW, "\x05%s%s", g_strTag[iAuthor], strName);
- }
- else if(strlen(g_strTagColor[iAuthor]) == 6)
- {
- Format(strNewName, MAXLENGTH_NAME_NEW, "\x07%s%s%s", g_strTagColor[iAuthor], g_strTag[iAuthor], strName);
- }
- else if(strlen(g_strTagColor[iAuthor]) == 8)
- {
- Format(strNewName, MAXLENGTH_NAME_NEW, "\x08%s%s%s", g_strTagColor[iAuthor], g_strTag[iAuthor], strName);
- }
- else
- {
- Format(strNewName, MAXLENGTH_NAME_NEW, "%s\x01%s", g_strTag[iAuthor], strName);
- }
- }
- }
- if(strlen(g_strChatColor[iAuthor]) > 0 && CheckForward(iAuthor, strNewMessage, CPT_ChatColor))
- {
- if(StrEqual(g_strChatColor[iAuthor], "T", false))
- {
- Format(strNewMessage, MAXLENGTH_MESSAGE, "\x03%s", strNewMessage);
- }
- else if(StrEqual(g_strChatColor[iAuthor], "G", false))
- {
- Format(strNewMessage, MAXLENGTH_MESSAGE, "\x04%s", strNewMessage);
- }
- else if(StrEqual(g_strChatColor[iAuthor], "O", false))
- {
- Format(strNewMessage, MAXLENGTH_MESSAGE, "\x05%s", strNewMessage);
- }
- else if(strlen(g_strChatColor[iAuthor]) == 6)
- {
- Format(strNewMessage, MAXLENGTH_MESSAGE, "\x07%s%s", g_strChatColor[iAuthor], strNewMessage);
- }
- else if(strlen(g_strChatColor[iAuthor]) == 8)
- {
- Format(strNewMessage, MAXLENGTH_MESSAGE, "\x08%s%s", g_strChatColor[iAuthor], strNewMessage);
- }
- }
- char strGame[64];
- GetGameFolderName(strGame, sizeof(strGame));
- if(StrEqual(strGame, "csgo"))
- {
- Format(strName, MAXLENGTH_NAME_NEW, "\x01\x0B%s", strName);
- }
- Call_StartForward(hMessageForward);
- Call_PushCell(iAuthor);
- Call_PushStringEx(strNewMessage, MAXLENGTH_MESSAGE, SM_PARAM_STRING_UTF8|SM_PARAM_STRING_COPY, SM_PARAM_COPYBACK);
- Call_PushCell(MAXLENGTH_MESSAGE);
- Call_Finish();
- CPrintToChat(iAuthor, "%s{default} : %s", strNewName, strNewMessage);
- }
- stock bool CheckForward(int iAuthor, const char[] strMessage, CPT_ColorType tType)
- {
- Action aResult = Plugin_Continue;
- Call_StartForward(hApplicationForward);
- Call_PushCell(iAuthor);
- Call_PushString(strMessage);
- Call_PushCell(tType);
- Call_Finish(aResult);
- if(aResult >= Plugin_Handled)
- {
- return false;
- }
- // Compatibility
- switch(tType)
- {
- case CPT_TagColor:
- {
- return TagForward(iAuthor);
- }
- case CPT_NameColor:
- {
- return NameForward(iAuthor);
- }
- case CPT_ChatColor:
- {
- return ColorForward(iAuthor);
- }
- }
- return true;
- }
- stock bool ColorForward(int iAuthor)
- {
- Action aResult = Plugin_Continue;
- Call_StartForward(hColorForward);
- Call_PushCell(iAuthor);
- Call_Finish(aResult);
- if(aResult >= Plugin_Handled)
- {
- return false;
- }
- return true;
- }
- stock bool NameForward(int iAuthor)
- {
- Action aResult = Plugin_Continue;
- Call_StartForward(hNameForward);
- Call_PushCell(iAuthor);
- Call_Finish(aResult);
- if(aResult >= Plugin_Handled)
- {
- return false;
- }
- return true;
- }
- stock bool TagForward(int iAuthor)
- {
- Action aResult = Plugin_Continue;
- Call_StartForward(hTagForward);
- Call_PushCell(iAuthor);
- Call_Finish(aResult);
- if(aResult >= Plugin_Handled)
- {
- return false;
- }
- return true;
- }
- stock bool ConfigForward(int iClient)
- {
- Action aResult = Plugin_Continue;
- Call_StartForward(hPreLoadedForward);
- Call_PushCell(iClient);
- Call_Finish(aResult);
- if(aResult >= Plugin_Handled)
- {
- return false;
- }
- return true;
- }
- public int Native_GetColor(Handle hPlugin, int NumParams)
- {
- int iClient = GetNativeCell(1);
- if(iClient < 1 || iClient > MaxClients || !IsClientInGame(iClient))
- {
- ThrowNativeError(SP_ERROR_PARAM, "Invalid client or client is not in game");
- return COLOR_NONE;
- }
- switch(GetNativeCell(2))
- {
- case CPT_TagColor:
- {
- if(StrEqual(g_strTagColor[iClient], "T", false))
- {
- SetNativeCellRef(3, false);
- return COLOR_TEAM;
- }
- else if(StrEqual(g_strTagColor[iClient], "G", false))
- {
- SetNativeCellRef(3, false);
- return COLOR_GREEN;
- }
- else if(StrEqual(g_strTagColor[iClient], "O", false))
- {
- SetNativeCellRef(3, false);
- return COLOR_OLIVE;
- }
- else if(strlen(g_strTagColor[iClient]) == 6 || strlen(g_strTagColor[iClient]) == 8)
- {
- SetNativeCellRef(3, strlen(g_strTagColor[iClient]) == 8);
- return StringToInt(g_strTagColor[iClient], 16);
- }
- else
- {
- SetNativeCellRef(3, false);
- return COLOR_NONE;
- }
- }
- case CPT_NameColor:
- {
- if(StrEqual(g_strUsernameColor[iClient], "G", false))
- {
- SetNativeCellRef(3, false);
- return COLOR_GREEN;
- }
- else if(StrEqual(g_strUsernameColor[iClient], "O", false))
- {
- SetNativeCellRef(3, false);
- return COLOR_OLIVE;
- }
- else if(strlen(g_strUsernameColor[iClient]) == 6 || strlen(g_strUsernameColor[iClient]) == 8)
- {
- SetNativeCellRef(3, strlen(g_strUsernameColor[iClient]) == 8);
- return StringToInt(g_strUsernameColor[iClient], 16);
- }
- else
- {
- SetNativeCellRef(3, false);
- return COLOR_TEAM;
- }
- }
- case CPT_ChatColor:
- {
- if(StrEqual(g_strChatColor[iClient], "T", false))
- {
- SetNativeCellRef(3, false);
- return COLOR_TEAM;
- }
- else if(StrEqual(g_strChatColor[iClient], "G", false))
- {
- SetNativeCellRef(3, false);
- return COLOR_GREEN;
- }
- else if(StrEqual(g_strChatColor[iClient], "O", false))
- {
- SetNativeCellRef(3, false);
- return COLOR_OLIVE;
- }
- else if(strlen(g_strChatColor[iClient]) == 6 || strlen(g_strChatColor[iClient]) == 8)
- {
- SetNativeCellRef(3, strlen(g_strChatColor[iClient]) == 8);
- return StringToInt(g_strChatColor[iClient], 16);
- }
- else
- {
- SetNativeCellRef(3, false);
- return COLOR_NONE;
- }
- }
- }
- return COLOR_NONE;
- }
- public int Native_SetColor(Handle hPlugin, int iNumParams)
- {
- int iClient = GetNativeCell(1);
- if(iClient < 1 || iClient > MaxClients || !IsClientInGame(iClient))
- {
- ThrowNativeError(SP_ERROR_PARAM, "Invalid client or client is not in game");
- return false;
- }
- char strColor[32];
- if(GetNativeCell(3) < 0)
- {
- switch(GetNativeCell(3))
- {
- case COLOR_GREEN:
- {
- Format(strColor, sizeof(strColor), "G");
- }
- case COLOR_OLIVE:
- {
- Format(strColor, sizeof(strColor), "O");
- }
- case COLOR_TEAM:
- {
- Format(strColor, sizeof(strColor), "T");
- }
- case COLOR_NONE:
- {
- Format(strColor, sizeof(strColor), "");
- }
- }
- }
- else
- {
- if(!GetNativeCell(4))
- {
- // No alpha
- Format(strColor, sizeof(strColor), "%06X", GetNativeCell(3));
- }
- else
- {
- // Alpha specified
- Format(strColor, sizeof(strColor), "%08X", GetNativeCell(3));
- }
- }
- if(strlen(strColor) != 6 && strlen(strColor) != 8 && !StrEqual(strColor, "G", false) && !StrEqual(strColor, "O", false) && !StrEqual(strColor, "T", false))
- {
- return false;
- }
- switch(GetNativeCell(2))
- {
- case CPT_TagColor:
- {
- strcopy(g_strTagColor[iClient], sizeof(g_strTagColor[]), strColor);
- }
- case CPT_NameColor:
- {
- strcopy(g_strUsernameColor[iClient], sizeof(g_strUsernameColor[]), strColor);
- }
- case CPT_ChatColor:
- {
- strcopy(g_strChatColor[iClient], sizeof(g_strChatColor[]), strColor);
- }
- }
- return true;
- }
- public int Native_GetTag(Handle hPlugin, int iNumParams)
- {
- int iClient = GetNativeCell(1);
- if(iClient < 1 || iClient > MaxClients || !IsClientInGame(iClient))
- {
- ThrowNativeError(SP_ERROR_PARAM, "Invalid client or client is not in game");
- return;
- }
- SetNativeString(2, g_strTag[iClient], GetNativeCell(3));
- }
- public int Native_SetTag(Handle hPlugin, int iNumParams)
- {
- int iClient = GetNativeCell(1);
- if(iClient < 1 || iClient > MaxClients || !IsClientInGame(iClient))
- {
- ThrowNativeError(SP_ERROR_PARAM, "Invalid client or client is not in game");
- return;
- }
- GetNativeString(2, g_strTag[iClient], sizeof(g_strTag[]));
- }
- public int Native_ResetColor(Handle hPlugin, int iNumParams)
- {
- int iClient = GetNativeCell(1);
- if(iClient < 1 || iClient > MaxClients || !IsClientInGame(iClient))
- {
- ThrowNativeError(SP_ERROR_PARAM, "Invalid client or client is not in game");
- return;
- }
- switch(GetNativeCell(2))
- {
- case CPT_TagColor:
- {
- strcopy(g_strTagColor[iClient], sizeof(g_strTagColor[]), g_strDefaultTagColor[iClient]);
- }
- case CPT_NameColor:
- {
- strcopy(g_strUsernameColor[iClient], sizeof(g_strUsernameColor[]), g_strDefaultUsernameColor[iClient]);
- }
- case CPT_ChatColor:
- {
- strcopy(g_strChatColor[iClient], sizeof(g_strChatColor[]), g_strDefaultChatColor[iClient]);
- }
- }
- }
- public int Native_ResetTag(Handle hPlugin, int iNumParams)
- {
- int iClient = GetNativeCell(1);
- if(iClient < 1 || iClient > MaxClients || !IsClientInGame(iClient))
- {
- ThrowNativeError(SP_ERROR_PARAM, "Invalid client or client is not in game");
- return;
- }
- strcopy(g_strTag[iClient], sizeof(g_strTag[]), g_strDefaultTag[iClient]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement