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 #if SOURCE_ENGINE == SE_DOTA
41 SH_DECL_EXTERN2_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommandContext &, const CCommand &);
42 #elif SOURCE_ENGINE >= SE_ORANGEBOX
43 SH_DECL_EXTERN1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
44 #elif SOURCE_ENGINE == SE_DARKMESSIAH
45 SH_DECL_EXTERN0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
47 # if SH_IMPL_VERSION >= 4
48 extern int __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
50 extern bool __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
52 extern bool __SourceHook_FHRemoveConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
55 ChatTriggers g_ChatTriggers;
56 bool g_bSupressSilentFails = false;
58 ChatTriggers::ChatTriggers() : m_pSayCmd(NULL), m_bWillProcessInPost(false),
59 m_ReplyTo(SM_REPLY_CONSOLE)
61 m_PubTrigger = sm_strdup("!");
62 m_PrivTrigger = sm_strdup("/");
64 m_PrivTriggerSize = 1;
65 m_bIsChatTrigger = false;
66 m_bPluginIgnored = false;
67 #if SOURCE_ENGINE == SE_EPISODEONE
72 ChatTriggers::~ChatTriggers()
74 delete [] m_PubTrigger;
76 delete [] m_PrivTrigger;
80 ConfigResult ChatTriggers::OnSourceModConfigChanged(const char *key,
86 if (strcmp(key, "PublicChatTrigger") == 0)
88 delete [] m_PubTrigger;
89 m_PubTrigger = sm_strdup(value);
90 m_PubTriggerSize = strlen(m_PubTrigger);
91 return ConfigResult_Accept;
93 else if (strcmp(key, "SilentChatTrigger") == 0)
95 delete [] m_PrivTrigger;
96 m_PrivTrigger = sm_strdup(value);
97 m_PrivTriggerSize = strlen(m_PrivTrigger);
98 return ConfigResult_Accept;
100 else if (strcmp(key, "SilentFailSuppress") == 0)
102 g_bSupressSilentFails = strcmp(value, "yes") == 0;
103 return ConfigResult_Accept;
106 return ConfigResult_Ignore;
109 void ChatTriggers::OnSourceModAllInitialized()
111 m_pShouldFloodBlock = g_Forwards.CreateForward("OnClientFloodCheck", ET_Event, 1, NULL, Param_Cell);
112 m_pDidFloodBlock = g_Forwards.CreateForward("OnClientFloodResult", ET_Event, 2, NULL, Param_Cell, Param_Cell);
113 m_pOnClientSayCmd = g_Forwards.CreateForward("OnClientSayCommand", ET_Event, 3, NULL, Param_Cell, Param_String, Param_String);
114 m_pOnClientSayCmd_Post = g_Forwards.CreateForward("OnClientSayCommand_Post", ET_Ignore, 3, NULL, Param_Cell, Param_String, Param_String);
117 void ChatTriggers::OnSourceModAllInitialized_Post()
119 logicore.AddCorePhraseFile("antiflood.phrases");
122 void ChatTriggers::OnSourceModGameInitialized()
124 m_pSayCmd = FindCommand("say");
125 m_pSayTeamCmd = FindCommand("say_team");
129 SH_ADD_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
130 SH_ADD_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
134 SH_ADD_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
135 SH_ADD_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
138 #if SOURCE_ENGINE == SE_EPISODEONE
139 m_bIsINS = (strcmp(g_SourceMod.GetGameFolderName(), "insurgency") == 0);
143 m_pSay2Cmd = FindCommand("say2");
146 SH_ADD_HOOK(ConCommand, Dispatch, m_pSay2Cmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
147 SH_ADD_HOOK(ConCommand, Dispatch, m_pSay2Cmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
150 #elif SOURCE_ENGINE == SE_NUCLEARDAWN
151 m_pSaySquadCmd = FindCommand("say_squad");
155 SH_ADD_HOOK(ConCommand, Dispatch, m_pSaySquadCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
156 SH_ADD_HOOK(ConCommand, Dispatch, m_pSaySquadCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
161 void ChatTriggers::OnSourceModShutdown()
165 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
166 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
171 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
172 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
175 #if SOURCE_ENGINE == SE_EPISODEONE
176 if (m_bIsINS && m_pSay2Cmd)
178 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSay2Cmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
179 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSay2Cmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
181 #elif SOURCE_ENGINE == SE_NUCLEARDAWN
184 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSaySquadCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
185 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSaySquadCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
189 g_Forwards.ReleaseForward(m_pShouldFloodBlock);
190 g_Forwards.ReleaseForward(m_pDidFloodBlock);
191 g_Forwards.ReleaseForward(m_pOnClientSayCmd);
192 g_Forwards.ReleaseForward(m_pOnClientSayCmd_Post);
195 #if SOURCE_ENGINE == SE_DOTA
196 void ChatTriggers::OnSayCommand_Pre(const CCommandContext &context, const CCommand &command)
198 #elif SOURCE_ENGINE >= SE_ORANGEBOX
199 void ChatTriggers::OnSayCommand_Pre(const CCommand &command)
202 void ChatTriggers::OnSayCommand_Pre()
206 int client = g_ConCmds.GetCommandClient();
207 m_bIsChatTrigger = false;
208 m_bWasFloodedMessage = false;
209 m_bPluginIgnored = false;
211 const char *args = command.ArgS();
215 RETURN_META(MRES_IGNORED);
218 /* Save these off for post hook as the command data returned from the engine in older engine versions
219 * can be NULL, despite the data still being there and valid. */
220 m_Arg0Backup = command.Arg(0);
221 m_ArgSBackup = command.ArgS();
223 /* The server console cannot do this */
226 cell_t res = CallOnClientSayCommand(client);
227 if (res >= Pl_Handled)
229 m_bPluginIgnored = (res >= Pl_Stop);
230 RETURN_META(MRES_SUPERCEDE);
232 RETURN_META(MRES_IGNORED);
235 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
237 /* We guarantee the client is connected */
238 if (!pPlayer || !pPlayer->IsConnected())
240 RETURN_META(MRES_IGNORED);
243 /* Check if we need to block this message from being sent */
244 if (ClientIsFlooding(client))
248 if (!logicore.CoreTranslate(buffer, sizeof(buffer), "%T", 2, NULL, "Flooding the server", &client))
249 UTIL_Format(buffer, sizeof(buffer), "You are flooding the server!");
251 /* :TODO: we should probably kick people who spam too much. */
253 char fullbuffer[192];
254 UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s", buffer);
255 g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
257 m_bWasFloodedMessage = true;
259 RETURN_META(MRES_SUPERCEDE);
262 /* Handle quoted string sets */
263 bool is_quoted = false;
270 #if SOURCE_ENGINE == SE_EPISODEONE
271 if (m_bIsINS && strcmp(m_Arg0Backup, "say2") == 0 && strlen(args) >= 4)
277 bool is_trigger = false;
278 bool is_silent = false;
280 /* Check for either trigger */
281 if (m_PubTriggerSize && strncmp(args, m_PubTrigger, m_PubTriggerSize) == 0)
284 args = &args[m_PubTriggerSize];
286 else if (m_PrivTriggerSize && strncmp(args, m_PrivTrigger, m_PrivTriggerSize) == 0)
290 args = &args[m_PrivTriggerSize];
294 * Test if this is actually a command!
296 if (is_trigger && PreProcessTrigger(PEntityOfEntIndex(client), args, is_quoted))
298 m_bIsChatTrigger = true;
301 * We'll execute it in post.
303 m_bWillProcessInPost = true;
306 cell_t res = CallOnClientSayCommand(client);
308 if (res >= Pl_Handled)
310 m_bPluginIgnored = (res >= Pl_Stop);
311 RETURN_META(MRES_SUPERCEDE);
314 if (is_silent && (m_bIsChatTrigger || (g_bSupressSilentFails && pPlayer->GetAdminId() != INVALID_ADMIN_ID)))
316 RETURN_META(MRES_SUPERCEDE);
319 /* Otherwise, let the command continue */
320 RETURN_META(MRES_IGNORED);
323 #if SOURCE_ENGINE == SE_DOTA
324 void ChatTriggers::OnSayCommand_Post(const CCommandContext &context, const CCommand &command)
325 #elif SOURCE_ENGINE >= SE_ORANGEBOX
326 void ChatTriggers::OnSayCommand_Post(const CCommand &command)
328 void ChatTriggers::OnSayCommand_Post()
331 int client = g_ConCmds.GetCommandClient();
333 if (m_bWillProcessInPost)
335 /* Reset this for re-entrancy */
336 m_bWillProcessInPost = false;
338 /* Execute the cached command */
339 unsigned int old = SetReplyTo(SM_REPLY_CHAT);
340 #if SOURCE_ENGINE == SE_DOTA
341 engine->ClientCommand(client, "%s", m_ToExecute);
343 serverpluginhelpers->ClientCommand(PEntityOfEntIndex(client), m_ToExecute);
348 if (m_bPluginIgnored)
350 m_bPluginIgnored = false;
352 else if (!m_bWasFloodedMessage && !m_bIsChatTrigger && m_pOnClientSayCmd_Post->GetFunctionCount() != 0)
354 m_pOnClientSayCmd_Post->PushCell(client);
355 m_pOnClientSayCmd_Post->PushString(m_Arg0Backup);
356 m_pOnClientSayCmd_Post->PushString(m_ArgSBackup);
357 m_pOnClientSayCmd_Post->Execute(NULL);
360 m_bIsChatTrigger = false;
361 m_bWasFloodedMessage = false;
364 bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args, bool is_quoted)
366 /* Extract a command. This is kind of sloppy. */
369 const char *inptr = args;
370 while (*inptr != '\0'
371 && !textparsers->IsWhitespace(inptr)
373 && cmd_len < sizeof(cmd_buf) - 1)
375 cmd_buf[cmd_len++] = *inptr++;
377 cmd_buf[cmd_len] = '\0';
384 /* See if we have this registered */
385 bool prepended = false;
386 if (!g_ConCmds.LookForSourceModCommand(cmd_buf))
388 /* Check if we had an "sm_" prefix */
389 if (strncmp(cmd_buf, "sm_", 3) == 0)
394 /* Now, prepend. Don't worry about the buffers. This will
395 * work because the sizes are limited from earlier.
398 strcpy(new_buf, "sm_");
399 strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3);
402 if (!g_ConCmds.LookForSourceModCommand(new_buf))
410 /* See if we need to do extra string manipulation */
411 if (is_quoted || prepended)
415 /* Check if we need to prepend sm_ */
418 len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
420 len = strncopy(m_ToExecute, args, sizeof(m_ToExecute));
423 /* Check if we need to strip a quote */
426 if (m_ToExecute[len-1] == '"')
428 m_ToExecute[--len] = '\0';
432 strncopy(m_ToExecute, args, sizeof(m_ToExecute));
438 cell_t ChatTriggers::CallOnClientSayCommand(int client)
440 cell_t res = Pl_Continue;
441 if (!m_bIsChatTrigger && m_pOnClientSayCmd->GetFunctionCount() != 0)
443 m_pOnClientSayCmd->PushCell(client);
444 m_pOnClientSayCmd->PushString(m_Arg0Backup);
445 m_pOnClientSayCmd->PushString(m_ArgSBackup);
446 m_pOnClientSayCmd->Execute(&res);
451 unsigned int ChatTriggers::SetReplyTo(unsigned int reply)
453 unsigned int old = m_ReplyTo;
460 unsigned int ChatTriggers::GetReplyTo()
465 bool ChatTriggers::IsChatTrigger()
467 return m_bIsChatTrigger;
470 bool ChatTriggers::ClientIsFlooding(int client)
472 bool is_flooding = false;
474 if (m_pShouldFloodBlock->GetFunctionCount() != 0)
478 m_pShouldFloodBlock->PushCell(client);
479 m_pShouldFloodBlock->Execute(&res);
487 if (m_pDidFloodBlock->GetFunctionCount() != 0)
489 m_pDidFloodBlock->PushCell(client);
490 m_pDidFloodBlock->PushCell(is_flooding ? 1 : 0);
491 m_pDidFloodBlock->Execute(NULL);
497 bool ChatTriggers::WasFloodedMessage()
499 return m_bWasFloodedMessage;