3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #include <ITextParsers.h>
33 #include "ChatTriggers.h"
34 #include "sm_stringutil.h"
35 #include "ConCmdManager.h"
36 #include "PlayerManager.h"
37 #include "HalfLife2.h"
38 #include "logic_bridge.h"
40 /* :HACKHACK: We can't SH_DECL here because ConCmdManager.cpp does.
41 * While the OB build only runs on MM:S 1.6.0+ (SH 5+), the older one
42 * can technically be compiled against any MM:S version after 1.4.2.
44 #if SOURCE_ENGINE >= SE_ORANGEBOX
45 extern bool __SourceHook_FHRemoveConCommandDispatch(void *, bool, class fastdelegate::FastDelegate1<const CCommand &, void>);
46 extern int __SourceHook_FHAddConCommandDispatch(void *, ISourceHook::AddHookMode, bool, class fastdelegate::FastDelegate1<const CCommand &, void>);
48 extern bool __SourceHook_FHRemoveConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
49 #if SH_IMPL_VERSION >= 5
50 extern int __SourceHook_FHAddConCommandDispatch(void *, ISourceHook::AddHookMode, bool, class fastdelegate::FastDelegate0<void>);
51 #elif SH_IMPL_VERSION == 4
52 extern int __SourceHook_FHAddConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
53 #elif SH_IMPL_VERSION == 3
54 extern bool __SourceHook_FHAddConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
55 #endif //SH_IMPL_VERSION
58 ChatTriggers g_ChatTriggers;
59 bool g_bSupressSilentFails = false;
61 ChatTriggers::ChatTriggers() : m_pSayCmd(NULL), m_bWillProcessInPost(false),
62 m_bTriggerWasSilent(false), m_ReplyTo(SM_REPLY_CONSOLE)
64 m_PubTrigger = sm_strdup("!");
65 m_PrivTrigger = sm_strdup("/");
67 m_PrivTriggerSize = 1;
68 m_bIsChatTrigger = false;
71 ChatTriggers::~ChatTriggers()
73 delete [] m_PubTrigger;
75 delete [] m_PrivTrigger;
79 ConfigResult ChatTriggers::OnSourceModConfigChanged(const char *key,
85 if (strcmp(key, "PublicChatTrigger") == 0)
87 delete [] m_PubTrigger;
88 m_PubTrigger = sm_strdup(value);
89 m_PubTriggerSize = strlen(m_PubTrigger);
90 return ConfigResult_Accept;
92 else if (strcmp(key, "SilentChatTrigger") == 0)
94 delete [] m_PrivTrigger;
95 m_PrivTrigger = sm_strdup(value);
96 m_PrivTriggerSize = strlen(m_PrivTrigger);
97 return ConfigResult_Accept;
99 else if (strcmp(key, "SilentFailSuppress") == 0)
101 g_bSupressSilentFails = strcmp(value, "yes") == 0;
102 return ConfigResult_Accept;
105 return ConfigResult_Ignore;
108 void ChatTriggers::OnSourceModAllInitialized()
110 m_pShouldFloodBlock = g_Forwards.CreateForward("OnClientFloodCheck", ET_Event, 1, NULL, Param_Cell);
111 m_pDidFloodBlock = g_Forwards.CreateForward("OnClientFloodResult", ET_Event, 2, NULL, Param_Cell, Param_Cell);
114 void ChatTriggers::OnSourceModAllInitialized_Post()
116 logicore.AddCorePhraseFile("antiflood.phrases");
119 void ChatTriggers::OnSourceModGameInitialized()
121 m_pSayCmd = FindCommand("say");
122 m_pSayTeamCmd = FindCommand("say_team");
126 SH_ADD_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
127 SH_ADD_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
131 SH_ADD_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
132 SH_ADD_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
136 void ChatTriggers::OnSourceModShutdown()
140 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
141 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
145 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
146 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
149 g_Forwards.ReleaseForward(m_pShouldFloodBlock);
150 g_Forwards.ReleaseForward(m_pDidFloodBlock);
153 #if SOURCE_ENGINE >= SE_ORANGEBOX
154 void ChatTriggers::OnSayCommand_Pre(const CCommand &command)
157 void ChatTriggers::OnSayCommand_Pre()
164 client = g_ConCmds.GetCommandClient();
165 m_bIsChatTrigger = false;
166 m_bWasFloodedMessage = false;
168 /* The server console cannot do this */
169 if (client == 0 || (pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
171 RETURN_META(MRES_IGNORED);
174 /* We guarantee the client is connected */
175 if (!pPlayer->IsConnected())
177 RETURN_META(MRES_IGNORED);
180 const char *args = command.ArgS();
184 RETURN_META(MRES_IGNORED);
187 /* Check if we need to block this message from being sent */
188 if (ClientIsFlooding(client))
192 if (!logicore.CoreTranslate(buffer, sizeof(buffer), "%T", 2, NULL, "Flooding the server", &client))
193 UTIL_Format(buffer, sizeof(buffer), "You are flooding the server!");
195 /* :TODO: we should probably kick people who spam too much. */
197 char fullbuffer[192];
198 UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s", buffer);
199 g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
201 m_bWasFloodedMessage = true;
203 RETURN_META(MRES_SUPERCEDE);
206 /* Handle quoted string sets */
207 bool is_quoted = false;
214 bool is_trigger = false;
215 bool is_silent = false;
217 /* Check for either trigger */
218 if (m_PubTriggerSize && strncmp(args, m_PubTrigger, m_PubTriggerSize) == 0)
221 args = &args[m_PubTriggerSize];
223 else if (m_PrivTriggerSize && strncmp(args, m_PrivTrigger, m_PrivTriggerSize) == 0)
227 args = &args[m_PrivTriggerSize];
232 RETURN_META(MRES_IGNORED);
236 * Test if this is actually a command!
238 if (!PreProcessTrigger(PEntityOfEntIndex(client), args, is_quoted))
242 && g_bSupressSilentFails
244 && (pPlayer = g_Players.GetPlayerByIndex(client)) != NULL
245 && pPlayer->GetAdminId() != INVALID_ADMIN_ID)
247 RETURN_META(MRES_SUPERCEDE);
249 RETURN_META(MRES_IGNORED);
252 m_bIsChatTrigger = true;
255 * We'll execute it in post.
257 m_bWillProcessInPost = true;
258 m_bTriggerWasSilent = is_silent;
260 /* If we're silent, block */
263 RETURN_META(MRES_SUPERCEDE);
266 /* Otherwise, let the command continue */
267 RETURN_META(MRES_IGNORED);
270 #if SOURCE_ENGINE >= SE_ORANGEBOX
271 void ChatTriggers::OnSayCommand_Post(const CCommand &command)
273 void ChatTriggers::OnSayCommand_Post()
276 m_bIsChatTrigger = false;
277 m_bWasFloodedMessage = false;
278 if (m_bWillProcessInPost)
280 /* Reset this for re-entrancy */
281 m_bWillProcessInPost = false;
283 /* Execute the cached command */
284 int client = g_ConCmds.GetCommandClient();
285 unsigned int old = SetReplyTo(SM_REPLY_CHAT);
286 serverpluginhelpers->ClientCommand(PEntityOfEntIndex(client), m_ToExecute);
291 bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args, bool is_quoted)
293 /* Extract a command. This is kind of sloppy. */
296 const char *inptr = args;
297 while (*inptr != '\0'
298 && !textparsers->IsWhitespace(inptr)
300 && cmd_len < sizeof(cmd_buf) - 1)
302 cmd_buf[cmd_len++] = *inptr++;
304 cmd_buf[cmd_len] = '\0';
311 /* See if we have this registered */
312 bool prepended = false;
313 if (!g_ConCmds.LookForSourceModCommand(cmd_buf))
315 /* Check if we had an "sm_" prefix */
316 if (strncmp(cmd_buf, "sm_", 3) == 0)
321 /* Now, prepend. Don't worry about the buffers. This will
322 * work because the sizes are limited from earlier.
325 strcpy(new_buf, "sm_");
326 strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3);
329 if (!g_ConCmds.LookForSourceModCommand(new_buf))
337 /* See if we need to do extra string manipulation */
338 if (is_quoted || prepended)
342 /* Check if we need to prepend sm_ */
345 len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
347 len = strncopy(m_ToExecute, args, sizeof(m_ToExecute));
350 /* Check if we need to strip a quote */
353 if (m_ToExecute[len-1] == '"')
355 m_ToExecute[--len] = '\0';
359 strncopy(m_ToExecute, args, sizeof(m_ToExecute));
365 unsigned int ChatTriggers::SetReplyTo(unsigned int reply)
367 unsigned int old = m_ReplyTo;
374 unsigned int ChatTriggers::GetReplyTo()
379 bool ChatTriggers::IsChatTrigger()
381 return m_bIsChatTrigger;
384 bool ChatTriggers::ClientIsFlooding(int client)
386 bool is_flooding = false;
388 if (m_pShouldFloodBlock->GetFunctionCount() != 0)
392 m_pShouldFloodBlock->PushCell(client);
393 m_pShouldFloodBlock->Execute(&res);
401 if (m_pDidFloodBlock->GetFunctionCount() != 0)
403 m_pDidFloodBlock->PushCell(client);
404 m_pDidFloodBlock->PushCell(is_flooding ? 1 : 0);
405 m_pDidFloodBlock->Execute(NULL);
411 bool ChatTriggers::WasFloodedMessage()
413 return m_bWasFloodedMessage;