ds@1952: /**
ds@1952: * vim: set ts=4 :
ds@1952: * =============================================================================
ds@1952: * SourceMod
ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
ds@1952: * =============================================================================
ds@1952: *
ds@1952: * This program is free software; you can redistribute it and/or modify it under
ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952: * Free Software Foundation.
ds@1952: *
ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
ds@1952: * details.
ds@1952: *
ds@1952: * You should have received a copy of the GNU General Public License along with
ds@1952: * this program. If not, see .
ds@1952: *
ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in
ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants
ds@1952: * this exception to all derivative works. AlliedModders LLC defines further
ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952: * or .
ds@1952: *
ds@1952: * Version: $Id$
ds@1952: */
ds@1952:
ds@1952: #include
ds@1952: #include "ChatTriggers.h"
ds@1952: #include "sm_stringutil.h"
ds@1952: #include "ConCmdManager.h"
ds@1952: #include "PlayerManager.h"
ds@1952: #include "Translator.h"
ds@1952: #include "HalfLife2.h"
ds@1952:
ds@1952: /* :HACKHACK: We can't SH_DECL here because ConCmdManager.cpp does.
ds@1952: * While the OB build only runs on MM:S 1.6.0+ (SH 5+), the older one
ds@1952: * can technically be compiled against any MM:S version after 1.4.2.
ds@1952: */
ds@1952: #if defined ORANGEBOX_BUILD
ds@1952: extern bool __SourceHook_FHRemoveConCommandDispatch(void *, bool, class fastdelegate::FastDelegate1);
ds@1952: extern int __SourceHook_FHAddConCommandDispatch(void *, ISourceHook::AddHookMode, bool, class fastdelegate::FastDelegate1);
ds@1952: #else
ds@1952: extern bool __SourceHook_FHRemoveConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0);
ds@1952: #if SH_IMPL_VERSION >= 5
ds@1952: extern int __SourceHook_FHAddConCommandDispatch(void *, ISourceHook::AddHookMode, bool, class fastdelegate::FastDelegate0);
ds@1952: #elif SH_IMPL_VERSION == 4
ds@1952: extern int __SourceHook_FHAddConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0);
ds@1952: #elif SH_IMPL_VERSION == 3
ds@1952: extern bool __SourceHook_FHAddConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0);
ds@1952: #endif //SH_IMPL_VERSION
ds@1952: #endif //ORANGEBOX_BUILD
ds@1952:
ds@1952: ChatTriggers g_ChatTriggers;
ds@1952: bool g_bSupressSilentFails = false;
ds@1952: CPhraseFile *g_pFloodPhrases = NULL;
ds@1952:
ds@1952: ChatTriggers::ChatTriggers() : m_pSayCmd(NULL), m_bWillProcessInPost(false),
ds@1952: m_bTriggerWasSilent(false), m_ReplyTo(SM_REPLY_CONSOLE)
ds@1952: {
ds@1952: m_PubTrigger = sm_strdup("!");
ds@1952: m_PrivTrigger = sm_strdup("/");
ds@1952: m_PubTriggerSize = 1;
ds@1952: m_PrivTriggerSize = 1;
ds@1952: m_bIsChatTrigger = false;
ds@1952: }
ds@1952:
ds@1952: ChatTriggers::~ChatTriggers()
ds@1952: {
ds@1952: delete [] m_PubTrigger;
ds@1952: m_PubTrigger = NULL;
ds@1952: delete [] m_PrivTrigger;
ds@1952: m_PrivTrigger = NULL;
ds@1952: }
ds@1952:
ds@1952: ConfigResult ChatTriggers::OnSourceModConfigChanged(const char *key,
ds@1952: const char *value,
ds@1952: ConfigSource source,
ds@1952: char *error,
ds@1952: size_t maxlength)
ds@1952: {
ds@1952: if (strcmp(key, "PublicChatTrigger") == 0)
ds@1952: {
ds@1952: delete [] m_PubTrigger;
ds@1952: m_PubTrigger = sm_strdup(value);
ds@1952: m_PubTriggerSize = strlen(m_PubTrigger);
ds@1952: return ConfigResult_Accept;
ds@1952: }
ds@1952: else if (strcmp(key, "SilentChatTrigger") == 0)
ds@1952: {
ds@1952: delete [] m_PrivTrigger;
ds@1952: m_PrivTrigger = sm_strdup(value);
ds@1952: m_PrivTriggerSize = strlen(m_PrivTrigger);
ds@1952: return ConfigResult_Accept;
ds@1952: }
ds@1952: else if (strcmp(key, "SilentFailSuppress") == 0)
ds@1952: {
ds@1952: g_bSupressSilentFails = strcmp(value, "yes") == 0;
ds@1952: return ConfigResult_Accept;
ds@1952: }
ds@1952:
ds@1952: return ConfigResult_Ignore;
ds@1952: }
ds@1952:
ds@1952: void ChatTriggers::OnSourceModAllInitialized()
ds@1952: {
ds@1952: m_pShouldFloodBlock = g_Forwards.CreateForward("OnClientFloodCheck", ET_Event, 1, NULL, Param_Cell);
ds@1952: m_pDidFloodBlock = g_Forwards.CreateForward("OnClientFloodResult", ET_Event, 2, NULL, Param_Cell, Param_Cell);
ds@1952: }
ds@1952:
ds@1952: void ChatTriggers::OnSourceModAllInitialized_Post()
ds@1952: {
ds@1952: unsigned int file_id;
ds@1952:
ds@1952: file_id = g_Translator.FindOrAddPhraseFile("antiflood.phrases.txt");
ds@1952: g_pFloodPhrases = g_Translator.GetFileByIndex(file_id);
ds@1952: }
ds@1952:
ds@1952: void ChatTriggers::OnSourceModGameInitialized()
ds@1952: {
ds@1952: unsigned int total = 2;
ds@1952: ConCommandBase *pCmd = icvar->GetCommands();
ds@1952: const char *name;
ds@1952: while (pCmd)
ds@1952: {
ds@1952: if (pCmd->IsCommand())
ds@1952: {
ds@1952: name = pCmd->GetName();
ds@1952: if (!m_pSayCmd && strcmp(name, "say") == 0)
ds@1952: {
ds@1952: m_pSayCmd = (ConCommand *)pCmd;
ds@1952: if (--total == 0)
ds@1952: {
ds@1952: break;
ds@1952: }
ds@1952: } else if (!m_pSayTeamCmd && strcmp(name, "say_team") == 0) {
ds@1952: m_pSayTeamCmd = (ConCommand *)pCmd;
ds@1952: if (--total == 0)
ds@1952: {
ds@1952: break;
ds@1952: }
ds@1952: }
ds@1952: }
ds@1952: pCmd = const_cast(pCmd->GetNext());
ds@1952: }
ds@1952:
ds@1952: if (m_pSayCmd)
ds@1952: {
ds@1952: SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
ds@1952: SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Post, true);
ds@1952: }
ds@1952: if (m_pSayTeamCmd)
ds@1952: {
ds@1952: SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
ds@1952: SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Post, true);
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: void ChatTriggers::OnSourceModShutdown()
ds@1952: {
ds@1952: if (m_pSayTeamCmd)
ds@1952: {
ds@1952: SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Post, true);
ds@1952: SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
ds@1952: }
ds@1952: if (m_pSayCmd)
ds@1952: {
ds@1952: SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Post, true);
ds@1952: SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
ds@1952: }
ds@1952:
ds@1952: g_Forwards.ReleaseForward(m_pShouldFloodBlock);
ds@1952: g_Forwards.ReleaseForward(m_pDidFloodBlock);
ds@1952: }
ds@1952:
ds@1952: #if defined ORANGEBOX_BUILD
ds@1952: void ChatTriggers::OnSayCommand_Pre(const CCommand &command)
ds@1952: {
ds@1952: #else
ds@1952: void ChatTriggers::OnSayCommand_Pre()
ds@1952: {
ds@1952: CCommand command;
ds@1952: #endif
ds@1952: int client;
ds@1952: CPlayer *pPlayer;
ds@1952:
ds@1952: client = g_ConCmds.GetCommandClient();
ds@1952: m_bIsChatTrigger = false;
ds@1952: m_bWasFloodedMessage = false;
ds@1952:
ds@1952: /* The server console cannot do this */
ds@1952: if (client == 0 || (pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
ds@1952: {
ds@1952: RETURN_META(MRES_IGNORED);
ds@1952: }
ds@1952:
ds@1952: /* We guarantee the client is connected */
ds@1952: if (!pPlayer->IsConnected())
ds@1952: {
ds@1952: RETURN_META(MRES_IGNORED);
ds@1952: }
ds@1952:
ds@1952: const char *args = command.ArgS();
ds@1952:
ds@1952: if (!args)
ds@1952: {
ds@1952: RETURN_META(MRES_IGNORED);
ds@1952: }
ds@1952:
ds@1952: /* Check if we need to block this message from being sent */
ds@1952: if (ClientIsFlooding(client))
ds@1952: {
ds@1952: char buffer[128];
ds@1952:
dvander@2042: if (!CoreTranslate(
dvander@2042: buffer,
ds@1952: sizeof(buffer),
dvander@2042: "%T",
dvander@2042: 2,
ds@1952: NULL,
dvander@2042: "Flooding the server",
dvander@2042: &client))
ds@1952: {
ds@1952: UTIL_Format(buffer, sizeof(buffer), "You are flooding the server!");
ds@1952: }
ds@1952:
ds@1952: /* :TODO: we should probably kick people who spam too much. */
ds@1952:
ds@1952: char fullbuffer[192];
ds@1952: UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s", buffer);
ds@1952: g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
ds@1952:
ds@1952: m_bWasFloodedMessage = true;
ds@1952:
ds@1952: RETURN_META(MRES_SUPERCEDE);
ds@1952: }
ds@1952:
ds@1952: /* Handle quoted string sets */
ds@1952: bool is_quoted = false;
ds@1952: if (args[0] == '"')
ds@1952: {
ds@1952: args++;
ds@1952: is_quoted = true;
ds@1952: }
ds@1952:
ds@1952: bool is_trigger = false;
ds@1952: bool is_silent = false;
ds@1952:
ds@1952: /* Check for either trigger */
ds@1952: if (m_PubTriggerSize && strncmp(args, m_PubTrigger, m_PubTriggerSize) == 0)
ds@1952: {
ds@1952: is_trigger = true;
ds@1952: args = &args[m_PubTriggerSize];
ds@1952: }
ds@1952: else if (m_PrivTriggerSize && strncmp(args, m_PrivTrigger, m_PrivTriggerSize) == 0)
ds@1952: {
ds@1952: is_trigger = true;
ds@1952: is_silent = true;
ds@1952: args = &args[m_PrivTriggerSize];
ds@1952: }
ds@1952:
ds@1952: if (!is_trigger)
ds@1952: {
ds@1952: RETURN_META(MRES_IGNORED);
ds@1952: }
ds@1952:
ds@1952: /**
ds@1952: * Test if this is actually a command!
ds@1952: */
ds@1952: if (!PreProcessTrigger(engine->PEntityOfEntIndex(client), args, is_quoted))
ds@1952: {
ds@1952: CPlayer *pPlayer;
ds@1952: if (is_silent
ds@1952: && g_bSupressSilentFails
ds@1952: && client != 0
ds@1952: && (pPlayer = g_Players.GetPlayerByIndex(client)) != NULL
ds@1952: && pPlayer->GetAdminId() != INVALID_ADMIN_ID)
ds@1952: {
ds@1952: RETURN_META(MRES_SUPERCEDE);
ds@1952: }
ds@1952: RETURN_META(MRES_IGNORED);
ds@1952: }
ds@1952:
ds@1952: m_bIsChatTrigger = true;
ds@1952:
ds@1952: /**
ds@1952: * We'll execute it in post.
ds@1952: */
ds@1952: m_bWillProcessInPost = true;
ds@1952: m_bTriggerWasSilent = is_silent;
ds@1952:
ds@1952: /* If we're silent, block */
ds@1952: if (is_silent)
ds@1952: {
ds@1952: RETURN_META(MRES_SUPERCEDE);
ds@1952: }
ds@1952:
ds@1952: /* Otherwise, let the command continue */
ds@1952: RETURN_META(MRES_IGNORED);
ds@1952: }
ds@1952:
ds@1952: #if defined ORANGEBOX_BUILD
ds@1952: void ChatTriggers::OnSayCommand_Post(const CCommand &command)
ds@1952: #else
ds@1952: void ChatTriggers::OnSayCommand_Post()
ds@1952: #endif
ds@1952: {
ds@1952: m_bIsChatTrigger = false;
ds@1952: m_bWasFloodedMessage = false;
ds@1952: if (m_bWillProcessInPost)
ds@1952: {
ds@1952: /* Reset this for re-entrancy */
ds@1952: m_bWillProcessInPost = false;
ds@1952:
ds@1952: /* Execute the cached command */
ds@1952: int client = g_ConCmds.GetCommandClient();
ds@1952: unsigned int old = SetReplyTo(SM_REPLY_CHAT);
ds@1952: serverpluginhelpers->ClientCommand(engine->PEntityOfEntIndex(client), m_ToExecute);
ds@1952: SetReplyTo(old);
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args, bool is_quoted)
ds@1952: {
ds@1952: /* Extract a command. This is kind of sloppy. */
ds@1952: char cmd_buf[64];
ds@1952: size_t cmd_len = 0;
ds@1952: const char *inptr = args;
ds@1952: while (*inptr != '\0'
ds@1952: && !textparsers->IsWhitespace(inptr)
ds@1952: && *inptr != '"'
ds@1952: && cmd_len < sizeof(cmd_buf) - 1)
ds@1952: {
ds@1952: cmd_buf[cmd_len++] = *inptr++;
ds@1952: }
ds@1952: cmd_buf[cmd_len] = '\0';
ds@1952:
ds@1952: if (cmd_len == 0)
ds@1952: {
ds@1952: return false;
ds@1952: }
ds@1952:
ds@1952: /* See if we have this registered */
ds@1952: bool prepended = false;
ds@1952: if (!g_ConCmds.LookForSourceModCommand(cmd_buf))
ds@1952: {
ds@1952: /* Check if we had an "sm_" prefix */
ds@1952: if (strncmp(cmd_buf, "sm_", 3) == 0)
ds@1952: {
ds@1952: return false;
ds@1952: }
ds@1952:
ds@1952: /* Now, prepend. Don't worry about the buffers. This will
ds@1952: * work because the sizes are limited from earlier.
ds@1952: */
ds@1952: char new_buf[80];
ds@1952: strcpy(new_buf, "sm_");
ds@1952: strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3);
ds@1952:
ds@1952: /* Recheck */
ds@1952: if (!g_ConCmds.LookForSourceModCommand(new_buf))
ds@1952: {
ds@1952: return false;
ds@1952: }
ds@1952:
ds@1952: prepended = true;
ds@1952: }
ds@1952:
ds@1952: /* See if we need to do extra string manipulation */
ds@1952: if (is_quoted || prepended)
ds@1952: {
ds@1952: size_t len;
ds@1952:
ds@1952: /* Check if we need to prepend sm_ */
ds@1952: if (prepended)
ds@1952: {
ds@1952: len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
ds@1952: } else {
ds@1952: len = strncopy(m_ToExecute, args, sizeof(m_ToExecute));
ds@1952: }
ds@1952:
ds@1952: /* Check if we need to strip a quote */
ds@1952: if (is_quoted)
ds@1952: {
ds@1952: if (m_ToExecute[len-1] == '"')
ds@1952: {
ds@1952: m_ToExecute[--len] = '\0';
ds@1952: }
ds@1952: }
ds@1952: } else {
ds@1952: strncopy(m_ToExecute, args, sizeof(m_ToExecute));
ds@1952: }
ds@1952:
ds@1952: return true;
ds@1952: }
ds@1952:
ds@1952: unsigned int ChatTriggers::SetReplyTo(unsigned int reply)
ds@1952: {
ds@1952: unsigned int old = m_ReplyTo;
ds@1952:
ds@1952: m_ReplyTo = reply;
ds@1952:
ds@1952: return old;
ds@1952: }
ds@1952:
ds@1952: unsigned int ChatTriggers::GetReplyTo()
ds@1952: {
ds@1952: return m_ReplyTo;
ds@1952: }
ds@1952:
ds@1952: bool ChatTriggers::IsChatTrigger()
ds@1952: {
ds@1952: return m_bIsChatTrigger;
ds@1952: }
ds@1952:
ds@1952: bool ChatTriggers::ClientIsFlooding(int client)
ds@1952: {
ds@1952: bool is_flooding = false;
ds@1952:
ds@1952: if (m_pShouldFloodBlock->GetFunctionCount() != 0)
ds@1952: {
ds@1952: cell_t res = 0;
ds@1952:
ds@1952: m_pShouldFloodBlock->PushCell(client);
ds@1952: m_pShouldFloodBlock->Execute(&res);
ds@1952:
ds@1952: if (res != 0)
ds@1952: {
ds@1952: is_flooding = true;
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: if (m_pDidFloodBlock->GetFunctionCount() != 0)
ds@1952: {
ds@1952: m_pDidFloodBlock->PushCell(client);
ds@1952: m_pDidFloodBlock->PushCell(is_flooding ? 1 : 0);
ds@1952: m_pDidFloodBlock->Execute(NULL);
ds@1952: }
ds@1952:
ds@1952: return is_flooding;
ds@1952: }
ds@1952:
ds@1952: bool ChatTriggers::WasFloodedMessage()
ds@1952: {
ds@1952: return m_bWasFloodedMessage;
ds@1952: }