Update gamedata for CS:S update.
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"
39 #include "sourcemod.h"
41 #if SOURCE_ENGINE == SE_DOTA
42 SH_DECL_EXTERN2_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommandContext &, const CCommand &);
43 #elif SOURCE_ENGINE >= SE_ORANGEBOX
44 SH_DECL_EXTERN1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
45 #elif SOURCE_ENGINE == SE_DARKMESSIAH
46 SH_DECL_EXTERN0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
48 # if SH_IMPL_VERSION >= 4
49 extern int __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
51 extern bool __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
53 extern bool __SourceHook_FHRemoveConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
56 ChatTriggers g_ChatTriggers;
57 bool g_bSupressSilentFails = false;
59 ChatTriggers::ChatTriggers() : m_pSayCmd(NULL), m_bWillProcessInPost(false),
60 m_ReplyTo(SM_REPLY_CONSOLE), m_ArgSBackup(NULL)
62 m_PubTrigger = sm_strdup("!");
63 m_PrivTrigger = sm_strdup("/");
65 m_PrivTriggerSize = 1;
66 m_bIsChatTrigger = false;
67 m_bPluginIgnored = true;
68 #if SOURCE_ENGINE == SE_EPISODEONE
73 ChatTriggers::~ChatTriggers()
75 delete [] m_PubTrigger;
77 delete [] m_PrivTrigger;
79 delete [] m_ArgSBackup;
83 ConfigResult ChatTriggers::OnSourceModConfigChanged(const char *key,
89 if (strcmp(key, "PublicChatTrigger") == 0)
91 delete [] m_PubTrigger;
92 m_PubTrigger = sm_strdup(value);
93 m_PubTriggerSize = strlen(m_PubTrigger);
94 return ConfigResult_Accept;
96 else if (strcmp(key, "SilentChatTrigger") == 0)
98 delete [] m_PrivTrigger;
99 m_PrivTrigger = sm_strdup(value);
100 m_PrivTriggerSize = strlen(m_PrivTrigger);
101 return ConfigResult_Accept;
103 else if (strcmp(key, "SilentFailSuppress") == 0)
105 g_bSupressSilentFails = strcmp(value, "yes") == 0;
106 return ConfigResult_Accept;
109 return ConfigResult_Ignore;
112 void ChatTriggers::OnSourceModAllInitialized()
114 m_pShouldFloodBlock = forwardsys->CreateForward("OnClientFloodCheck", ET_Event, 1, NULL, Param_Cell);
115 m_pDidFloodBlock = forwardsys->CreateForward("OnClientFloodResult", ET_Event, 2, NULL, Param_Cell, Param_Cell);
116 m_pOnClientSayCmd = forwardsys->CreateForward("OnClientSayCommand", ET_Event, 3, NULL, Param_Cell, Param_String, Param_String);
117 m_pOnClientSayCmd_Post = forwardsys->CreateForward("OnClientSayCommand_Post", ET_Ignore, 3, NULL, Param_Cell, Param_String, Param_String);
120 void ChatTriggers::OnSourceModAllInitialized_Post()
122 logicore.AddCorePhraseFile("antiflood.phrases");
125 void ChatTriggers::OnSourceModGameInitialized()
127 m_pSayCmd = FindCommand("say");
128 m_pSayTeamCmd = FindCommand("say_team");
132 SH_ADD_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
133 SH_ADD_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
137 SH_ADD_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
138 SH_ADD_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
141 #if SOURCE_ENGINE == SE_EPISODEONE
142 m_bIsINS = (strcmp(g_SourceMod.GetGameFolderName(), "insurgency") == 0);
146 m_pSay2Cmd = FindCommand("say2");
149 SH_ADD_HOOK(ConCommand, Dispatch, m_pSay2Cmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
150 SH_ADD_HOOK(ConCommand, Dispatch, m_pSay2Cmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
153 #elif SOURCE_ENGINE == SE_NUCLEARDAWN
154 m_pSaySquadCmd = FindCommand("say_squad");
158 SH_ADD_HOOK(ConCommand, Dispatch, m_pSaySquadCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
159 SH_ADD_HOOK(ConCommand, Dispatch, m_pSaySquadCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
164 void ChatTriggers::OnSourceModShutdown()
168 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
169 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
174 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
175 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
178 #if SOURCE_ENGINE == SE_EPISODEONE
179 if (m_bIsINS && m_pSay2Cmd)
181 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSay2Cmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
182 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSay2Cmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
184 #elif SOURCE_ENGINE == SE_NUCLEARDAWN
187 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSaySquadCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
188 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSaySquadCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
192 forwardsys->ReleaseForward(m_pShouldFloodBlock);
193 forwardsys->ReleaseForward(m_pDidFloodBlock);
194 forwardsys->ReleaseForward(m_pOnClientSayCmd);
195 forwardsys->ReleaseForward(m_pOnClientSayCmd_Post);
198 #if SOURCE_ENGINE == SE_DOTA
199 void ChatTriggers::OnSayCommand_Pre(const CCommandContext &context, const CCommand &command)
201 #elif SOURCE_ENGINE >= SE_ORANGEBOX
202 void ChatTriggers::OnSayCommand_Pre(const CCommand &command)
205 void ChatTriggers::OnSayCommand_Pre()
209 int client = g_ConCmds.GetCommandClient();
210 m_bIsChatTrigger = false;
211 m_bWasFloodedMessage = false;
212 m_bPluginIgnored = true;
214 const char *args = command.ArgS();
218 RETURN_META(MRES_IGNORED);
221 /* Save these off for post hook as the command data returned from the engine in older engine versions
222 * can be NULL, despite the data still being there and valid. */
223 m_Arg0Backup = command.Arg(0);
224 size_t len = strlen(args);
226 #if SOURCE_ENGINE == SE_EPISODEONE
229 if (strcmp(m_Arg0Backup, "say2") == 0 && len >= 4)
237 RETURN_META(MRES_SUPERCEDE);
242 /* The first pair of quotes are stripped from client say commands, but not console ones.
243 * We do not want the forwards to differ from what is displayed.
244 * So only strip the first pair of quotes from client say commands. */
245 bool is_quoted = false;
248 #if SOURCE_ENGINE == SE_EPISODEONE
251 client != 0 && args[0] == '"' && args[len-1] == '"')
253 /* The server normally won't display empty say commands, but in this case it does.
254 * I don't think it's desired so let's block it. */
257 RETURN_META(MRES_SUPERCEDE);
265 /* Some? engines strip the last quote when printing the string to chat.
266 * This results in having a double-quoted message passed to the OnClientSayCommand ("message") forward,
267 * but losing the last quote in the OnClientSayCommand_Post ("message) forward.
268 * To compensate this, we copy the args into our own buffer where the engine won't mess with
269 * and strip the quotes. */
270 delete [] m_ArgSBackup;
271 m_ArgSBackup = new char[CCommand::MaxCommandLength()+1];
272 memcpy(m_ArgSBackup, args, len+1);
274 /* Strip the quotes from the argument */
277 if (m_ArgSBackup[len-1] == '"')
279 m_ArgSBackup[--len] = '\0';
283 /* The server console cannot do this */
286 if (CallOnClientSayCommand(client) >= Pl_Handled)
288 RETURN_META(MRES_SUPERCEDE);
291 RETURN_META(MRES_IGNORED);
294 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
296 /* We guarantee the client is connected */
297 if (!pPlayer || !pPlayer->IsConnected())
299 RETURN_META(MRES_IGNORED);
302 /* Check if we need to block this message from being sent */
303 if (ClientIsFlooding(client))
307 if (!logicore.CoreTranslate(buffer, sizeof(buffer), "%T", 2, NULL, "Flooding the server", &client))
308 UTIL_Format(buffer, sizeof(buffer), "You are flooding the server!");
310 /* :TODO: we should probably kick people who spam too much. */
312 char fullbuffer[192];
313 UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s", buffer);
314 g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
316 m_bWasFloodedMessage = true;
318 RETURN_META(MRES_SUPERCEDE);
321 bool is_trigger = false;
322 bool is_silent = false;
324 /* Check for either trigger */
325 if (m_PubTriggerSize && strncmp(m_ArgSBackup, m_PubTrigger, m_PubTriggerSize) == 0)
328 args = &m_ArgSBackup[m_PubTriggerSize];
330 else if (m_PrivTriggerSize && strncmp(m_ArgSBackup, m_PrivTrigger, m_PrivTriggerSize) == 0)
334 args = &m_ArgSBackup[m_PrivTriggerSize];
338 * Test if this is actually a command!
340 if (is_trigger && PreProcessTrigger(PEntityOfEntIndex(client), args))
342 m_bIsChatTrigger = true;
345 * We'll execute it in post.
347 m_bWillProcessInPost = true;
350 if (is_silent && (m_bIsChatTrigger || (g_bSupressSilentFails && pPlayer->GetAdminId() != INVALID_ADMIN_ID)))
352 RETURN_META(MRES_SUPERCEDE);
355 if (CallOnClientSayCommand(client) >= Pl_Handled)
357 RETURN_META(MRES_SUPERCEDE);
360 /* Otherwise, let the command continue */
361 RETURN_META(MRES_IGNORED);
364 #if SOURCE_ENGINE == SE_DOTA
365 void ChatTriggers::OnSayCommand_Post(const CCommandContext &context, const CCommand &command)
366 #elif SOURCE_ENGINE >= SE_ORANGEBOX
367 void ChatTriggers::OnSayCommand_Post(const CCommand &command)
369 void ChatTriggers::OnSayCommand_Post()
372 int client = g_ConCmds.GetCommandClient();
374 if (m_bWillProcessInPost)
376 /* Reset this for re-entrancy */
377 m_bWillProcessInPost = false;
379 /* Execute the cached command */
380 unsigned int old = SetReplyTo(SM_REPLY_CHAT);
381 #if SOURCE_ENGINE == SE_DOTA
382 engine->ClientCommand(client, "%s", m_ToExecute);
384 serverpluginhelpers->ClientCommand(PEntityOfEntIndex(client), m_ToExecute);
389 if (!m_bPluginIgnored && m_pOnClientSayCmd_Post->GetFunctionCount() != 0)
391 m_pOnClientSayCmd_Post->PushCell(client);
392 m_pOnClientSayCmd_Post->PushString(m_Arg0Backup);
393 m_pOnClientSayCmd_Post->PushString(m_ArgSBackup);
394 m_pOnClientSayCmd_Post->Execute(NULL);
397 m_bIsChatTrigger = false;
398 m_bWasFloodedMessage = false;
401 bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args)
403 /* Extract a command. This is kind of sloppy. */
406 const char *inptr = args;
407 while (*inptr != '\0'
408 && !textparsers->IsWhitespace(inptr)
410 && cmd_len < sizeof(cmd_buf) - 1)
412 cmd_buf[cmd_len++] = *inptr++;
414 cmd_buf[cmd_len] = '\0';
421 /* See if we have this registered */
422 bool prepended = false;
423 if (!g_ConCmds.LookForSourceModCommand(cmd_buf))
425 /* Check if we had an "sm_" prefix */
426 if (strncmp(cmd_buf, "sm_", 3) == 0)
431 /* Now, prepend. Don't worry about the buffers. This will
432 * work because the sizes are limited from earlier.
435 strcpy(new_buf, "sm_");
436 strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3);
439 if (!g_ConCmds.LookForSourceModCommand(new_buf))
447 /* See if we need to do extra string manipulation */
452 /* Check if we need to prepend sm_ */
455 len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
457 len = strncopy(m_ToExecute, args, sizeof(m_ToExecute));
460 strncopy(m_ToExecute, args, sizeof(m_ToExecute));
466 cell_t ChatTriggers::CallOnClientSayCommand(int client)
468 cell_t res = Pl_Continue;
469 if (m_pOnClientSayCmd->GetFunctionCount() != 0)
471 m_pOnClientSayCmd->PushCell(client);
472 m_pOnClientSayCmd->PushString(m_Arg0Backup);
473 m_pOnClientSayCmd->PushString(m_ArgSBackup);
474 m_pOnClientSayCmd->Execute(&res);
477 m_bPluginIgnored = (res >= Pl_Stop);
482 unsigned int ChatTriggers::SetReplyTo(unsigned int reply)
484 unsigned int old = m_ReplyTo;
491 unsigned int ChatTriggers::GetReplyTo()
496 bool ChatTriggers::IsChatTrigger()
498 return m_bIsChatTrigger;
501 bool ChatTriggers::ClientIsFlooding(int client)
503 bool is_flooding = false;
505 if (m_pShouldFloodBlock->GetFunctionCount() != 0)
509 m_pShouldFloodBlock->PushCell(client);
510 m_pShouldFloodBlock->Execute(&res);
518 if (m_pDidFloodBlock->GetFunctionCount() != 0)
520 m_pDidFloodBlock->PushCell(client);
521 m_pDidFloodBlock->PushCell(is_flooding ? 1 : 0);
522 m_pDidFloodBlock->Execute(NULL);
528 bool ChatTriggers::WasFloodedMessage()
530 return m_bWasFloodedMessage;