Update linux dep script (no bug, r=NPOTB).
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)
62 m_PubTrigger = sm_strdup("!");
63 m_PrivTrigger = sm_strdup("/");
65 m_PrivTriggerSize = 1;
66 m_bIsChatTrigger = false;
67 m_bPluginIgnored = false;
68 #if SOURCE_ENGINE == SE_EPISODEONE
73 ChatTriggers::~ChatTriggers()
75 delete [] m_PubTrigger;
77 delete [] m_PrivTrigger;
81 ConfigResult ChatTriggers::OnSourceModConfigChanged(const char *key,
87 if (strcmp(key, "PublicChatTrigger") == 0)
89 delete [] m_PubTrigger;
90 m_PubTrigger = sm_strdup(value);
91 m_PubTriggerSize = strlen(m_PubTrigger);
92 return ConfigResult_Accept;
94 else if (strcmp(key, "SilentChatTrigger") == 0)
96 delete [] m_PrivTrigger;
97 m_PrivTrigger = sm_strdup(value);
98 m_PrivTriggerSize = strlen(m_PrivTrigger);
99 return ConfigResult_Accept;
101 else if (strcmp(key, "SilentFailSuppress") == 0)
103 g_bSupressSilentFails = strcmp(value, "yes") == 0;
104 return ConfigResult_Accept;
107 return ConfigResult_Ignore;
110 void ChatTriggers::OnSourceModAllInitialized()
112 m_pShouldFloodBlock = forwardsys->CreateForward("OnClientFloodCheck", ET_Event, 1, NULL, Param_Cell);
113 m_pDidFloodBlock = forwardsys->CreateForward("OnClientFloodResult", ET_Event, 2, NULL, Param_Cell, Param_Cell);
114 m_pOnClientSayCmd = forwardsys->CreateForward("OnClientSayCommand", ET_Event, 3, NULL, Param_Cell, Param_String, Param_String);
115 m_pOnClientSayCmd_Post = forwardsys->CreateForward("OnClientSayCommand_Post", ET_Ignore, 3, NULL, Param_Cell, Param_String, Param_String);
118 void ChatTriggers::OnSourceModAllInitialized_Post()
120 logicore.AddCorePhraseFile("antiflood.phrases");
123 void ChatTriggers::OnSourceModGameInitialized()
125 m_pSayCmd = FindCommand("say");
126 m_pSayTeamCmd = FindCommand("say_team");
130 SH_ADD_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
131 SH_ADD_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
135 SH_ADD_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
136 SH_ADD_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
139 #if SOURCE_ENGINE == SE_EPISODEONE
140 m_bIsINS = (strcmp(g_SourceMod.GetGameFolderName(), "insurgency") == 0);
144 m_pSay2Cmd = FindCommand("say2");
147 SH_ADD_HOOK(ConCommand, Dispatch, m_pSay2Cmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
148 SH_ADD_HOOK(ConCommand, Dispatch, m_pSay2Cmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
151 #elif SOURCE_ENGINE == SE_NUCLEARDAWN
152 m_pSaySquadCmd = FindCommand("say_squad");
156 SH_ADD_HOOK(ConCommand, Dispatch, m_pSaySquadCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
157 SH_ADD_HOOK(ConCommand, Dispatch, m_pSaySquadCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
162 void ChatTriggers::OnSourceModShutdown()
166 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
167 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
172 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
173 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
176 #if SOURCE_ENGINE == SE_EPISODEONE
177 if (m_bIsINS && m_pSay2Cmd)
179 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSay2Cmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
180 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSay2Cmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
182 #elif SOURCE_ENGINE == SE_NUCLEARDAWN
185 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSaySquadCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
186 SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSaySquadCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
190 forwardsys->ReleaseForward(m_pShouldFloodBlock);
191 forwardsys->ReleaseForward(m_pDidFloodBlock);
192 forwardsys->ReleaseForward(m_pOnClientSayCmd);
193 forwardsys->ReleaseForward(m_pOnClientSayCmd_Post);
196 #if SOURCE_ENGINE == SE_DOTA
197 void ChatTriggers::OnSayCommand_Pre(const CCommandContext &context, const CCommand &command)
199 #elif SOURCE_ENGINE >= SE_ORANGEBOX
200 void ChatTriggers::OnSayCommand_Pre(const CCommand &command)
203 void ChatTriggers::OnSayCommand_Pre()
207 int client = g_ConCmds.GetCommandClient();
208 m_bIsChatTrigger = false;
209 m_bWasFloodedMessage = false;
210 m_bPluginIgnored = false;
212 const char *args = command.ArgS();
216 RETURN_META(MRES_IGNORED);
219 /* Save these off for post hook as the command data returned from the engine in older engine versions
220 * can be NULL, despite the data still being there and valid. */
221 m_Arg0Backup = command.Arg(0);
222 m_ArgSBackup = command.ArgS();
224 /* The server console cannot do this */
227 cell_t res = CallOnClientSayCommand(client);
228 if (res >= Pl_Handled)
230 m_bPluginIgnored = (res >= Pl_Stop);
231 RETURN_META(MRES_SUPERCEDE);
233 RETURN_META(MRES_IGNORED);
236 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
238 /* We guarantee the client is connected */
239 if (!pPlayer || !pPlayer->IsConnected())
241 RETURN_META(MRES_IGNORED);
244 /* Check if we need to block this message from being sent */
245 if (ClientIsFlooding(client))
249 if (!logicore.CoreTranslate(buffer, sizeof(buffer), "%T", 2, NULL, "Flooding the server", &client))
250 UTIL_Format(buffer, sizeof(buffer), "You are flooding the server!");
252 /* :TODO: we should probably kick people who spam too much. */
254 char fullbuffer[192];
255 UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s", buffer);
256 g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
258 m_bWasFloodedMessage = true;
260 RETURN_META(MRES_SUPERCEDE);
263 /* Handle quoted string sets */
264 bool is_quoted = false;
271 #if SOURCE_ENGINE == SE_EPISODEONE
272 if (m_bIsINS && strcmp(m_Arg0Backup, "say2") == 0 && strlen(args) >= 4)
278 bool is_trigger = false;
279 bool is_silent = false;
281 /* Check for either trigger */
282 if (m_PubTriggerSize && strncmp(args, m_PubTrigger, m_PubTriggerSize) == 0)
285 args = &args[m_PubTriggerSize];
287 else if (m_PrivTriggerSize && strncmp(args, m_PrivTrigger, m_PrivTriggerSize) == 0)
291 args = &args[m_PrivTriggerSize];
295 * Test if this is actually a command!
297 if (is_trigger && PreProcessTrigger(PEntityOfEntIndex(client), args, is_quoted))
299 m_bIsChatTrigger = true;
302 * We'll execute it in post.
304 m_bWillProcessInPost = true;
307 cell_t res = CallOnClientSayCommand(client);
309 if (res >= Pl_Handled)
311 m_bPluginIgnored = (res >= Pl_Stop);
312 RETURN_META(MRES_SUPERCEDE);
315 if (is_silent && (m_bIsChatTrigger || (g_bSupressSilentFails && pPlayer->GetAdminId() != INVALID_ADMIN_ID)))
317 RETURN_META(MRES_SUPERCEDE);
320 /* Otherwise, let the command continue */
321 RETURN_META(MRES_IGNORED);
324 #if SOURCE_ENGINE == SE_DOTA
325 void ChatTriggers::OnSayCommand_Post(const CCommandContext &context, const CCommand &command)
326 #elif SOURCE_ENGINE >= SE_ORANGEBOX
327 void ChatTriggers::OnSayCommand_Post(const CCommand &command)
329 void ChatTriggers::OnSayCommand_Post()
332 int client = g_ConCmds.GetCommandClient();
334 if (m_bWillProcessInPost)
336 /* Reset this for re-entrancy */
337 m_bWillProcessInPost = false;
339 /* Execute the cached command */
340 unsigned int old = SetReplyTo(SM_REPLY_CHAT);
341 #if SOURCE_ENGINE == SE_DOTA
342 engine->ClientCommand(client, "%s", m_ToExecute);
344 serverpluginhelpers->ClientCommand(PEntityOfEntIndex(client), m_ToExecute);
349 if (m_bPluginIgnored)
351 m_bPluginIgnored = false;
353 else if (!m_bWasFloodedMessage && !m_bIsChatTrigger && m_pOnClientSayCmd_Post->GetFunctionCount() != 0)
355 m_pOnClientSayCmd_Post->PushCell(client);
356 m_pOnClientSayCmd_Post->PushString(m_Arg0Backup);
357 m_pOnClientSayCmd_Post->PushString(m_ArgSBackup);
358 m_pOnClientSayCmd_Post->Execute(NULL);
361 m_bIsChatTrigger = false;
362 m_bWasFloodedMessage = false;
365 bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args, bool is_quoted)
367 /* Extract a command. This is kind of sloppy. */
370 const char *inptr = args;
371 while (*inptr != '\0'
372 && !textparsers->IsWhitespace(inptr)
374 && cmd_len < sizeof(cmd_buf) - 1)
376 cmd_buf[cmd_len++] = *inptr++;
378 cmd_buf[cmd_len] = '\0';
385 /* See if we have this registered */
386 bool prepended = false;
387 if (!g_ConCmds.LookForSourceModCommand(cmd_buf))
389 /* Check if we had an "sm_" prefix */
390 if (strncmp(cmd_buf, "sm_", 3) == 0)
395 /* Now, prepend. Don't worry about the buffers. This will
396 * work because the sizes are limited from earlier.
399 strcpy(new_buf, "sm_");
400 strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3);
403 if (!g_ConCmds.LookForSourceModCommand(new_buf))
411 /* See if we need to do extra string manipulation */
412 if (is_quoted || prepended)
416 /* Check if we need to prepend sm_ */
419 len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
421 len = strncopy(m_ToExecute, args, sizeof(m_ToExecute));
424 /* Check if we need to strip a quote */
427 if (m_ToExecute[len-1] == '"')
429 m_ToExecute[--len] = '\0';
433 strncopy(m_ToExecute, args, sizeof(m_ToExecute));
439 cell_t ChatTriggers::CallOnClientSayCommand(int client)
441 cell_t res = Pl_Continue;
442 if (!m_bIsChatTrigger && m_pOnClientSayCmd->GetFunctionCount() != 0)
444 m_pOnClientSayCmd->PushCell(client);
445 m_pOnClientSayCmd->PushString(m_Arg0Backup);
446 m_pOnClientSayCmd->PushString(m_ArgSBackup);
447 m_pOnClientSayCmd->Execute(&res);
452 unsigned int ChatTriggers::SetReplyTo(unsigned int reply)
454 unsigned int old = m_ReplyTo;
461 unsigned int ChatTriggers::GetReplyTo()
466 bool ChatTriggers::IsChatTrigger()
468 return m_bIsChatTrigger;
471 bool ChatTriggers::ClientIsFlooding(int client)
473 bool is_flooding = false;
475 if (m_pShouldFloodBlock->GetFunctionCount() != 0)
479 m_pShouldFloodBlock->PushCell(client);
480 m_pShouldFloodBlock->Execute(&res);
488 if (m_pDidFloodBlock->GetFunctionCount() != 0)
490 m_pDidFloodBlock->PushCell(client);
491 m_pDidFloodBlock->PushCell(is_flooding ? 1 : 0);
492 m_pDidFloodBlock->Execute(NULL);
498 bool ChatTriggers::WasFloodedMessage()
500 return m_bWasFloodedMessage;