3 * ================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * ================================================================
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License,
10 * version 3.0, as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to
21 * link the code of this program (as well as its derivative works) to
22 * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any
23 * Game MODs that run on software by the Valve Corporation. You must
24 * obey the GNU General Public License in all respects for all other
25 * code used. Additionally, AlliedModders LLC grants this exception
26 * to all derivative works. AlliedModders LLC defines further
27 * exceptions, found in LICENSE.txt (as of this writing, version
28 * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
33 #include "ChatTriggers.h"
34 #include "sm_stringutil.h"
35 #include "TextParsers.h"
36 #include "ConCmdManager.h"
38 /* :HACKHACK: We can't SH_DECL here because ConCmdManager.cpp does */
39 extern bool __SourceHook_FHRemoveConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
41 #if SH_IMPL_VERSION >= 4
42 extern int __SourceHook_FHAddConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
44 extern bool __SourceHook_FHAddConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
47 ChatTriggers g_ChatTriggers;
49 ChatTriggers::ChatTriggers() : m_pSayCmd(NULL), m_bWillProcessInPost(false),
50 m_bTriggerWasSilent(false), m_ReplyTo(SM_REPLY_CONSOLE)
52 m_PubTrigger = sm_strdup("!");
53 m_PrivTrigger = sm_strdup("/");
55 m_PrivTriggerSize = 1;
58 ChatTriggers::~ChatTriggers()
60 delete [] m_PubTrigger;
62 delete [] m_PrivTrigger;
66 ConfigResult ChatTriggers::OnSourceModConfigChanged(const char *key,
72 if (strcmp(key, "PublicChatTrigger") == 0)
74 delete [] m_PubTrigger;
75 m_PubTrigger = sm_strdup(value);
76 m_PubTriggerSize = strlen(m_PubTrigger);
77 return ConfigResult_Accept;
78 } else if (strcmp(key, "SilentChatTrigger") == 0) {
79 delete [] m_PrivTrigger;
80 m_PrivTrigger = sm_strdup(value);
81 m_PrivTriggerSize = strlen(m_PrivTrigger);
82 return ConfigResult_Accept;
85 return ConfigResult_Ignore;
88 void ChatTriggers::OnSourceModGameInitialized()
90 unsigned int total = 2;
91 ConCommandBase *pCmd = icvar->GetCommands();
95 if (pCmd->IsCommand())
97 name = pCmd->GetName();
98 if (!m_pSayCmd && strcmp(name, "say") == 0)
100 m_pSayCmd = (ConCommand *)pCmd;
105 } else if (!m_pSayTeamCmd && strcmp(name, "say_team") == 0) {
106 m_pSayTeamCmd = (ConCommand *)pCmd;
113 pCmd = const_cast<ConCommandBase *>(pCmd->GetNext());
118 SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
119 SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Post, true);
123 SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
124 SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Post, true);
128 void ChatTriggers::OnSourceModShutdown()
132 SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Post, true);
133 SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
137 SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Post, true);
138 SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
142 void ChatTriggers::OnSayCommand_Pre()
144 int client = g_ConCmds.GetCommandClient();
146 /* The server console cannot do this */
149 RETURN_META(MRES_IGNORED);
152 const char *args = engine->Cmd_Args();
156 RETURN_META(MRES_IGNORED);
159 /* Handle quoted string sets */
160 bool is_quoted = false;
167 bool is_trigger = false;
168 bool is_silent = false;
170 /* Check for either trigger */
171 if (m_PubTriggerSize && strncmp(args, m_PubTrigger, m_PubTriggerSize) == 0)
174 args = &args[m_PubTriggerSize];
175 } else if (m_PrivTriggerSize && strncmp(args, m_PrivTrigger, m_PrivTriggerSize) == 0) {
178 args = &args[m_PrivTriggerSize];
183 RETURN_META(MRES_IGNORED);
187 * Test if this is actually a command!
189 if (!PreProcessTrigger(engine->PEntityOfEntIndex(client), args, is_quoted))
191 RETURN_META(MRES_IGNORED);
195 * We'll execute it in post.
197 m_bWillProcessInPost = true;
198 m_bTriggerWasSilent = is_silent;
200 /* If we're silent, block */
203 RETURN_META(MRES_SUPERCEDE);
206 /* Otherwise, let the command continue */
207 RETURN_META(MRES_IGNORED);
210 void ChatTriggers::OnSayCommand_Post()
212 if (m_bWillProcessInPost)
214 /* Reset this for re-entrancy */
215 m_bWillProcessInPost = false;
217 /* Execute the cached command */
218 int client = g_ConCmds.GetCommandClient();
219 unsigned int old = SetReplyTo(SM_REPLY_CHAT);
220 serverpluginhelpers->ClientCommand(engine->PEntityOfEntIndex(client), m_ToExecute);
225 bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args, bool is_quoted)
227 /* Eat up whitespace */
228 while (*args != '\0' && IsWhitespace(args))
233 /* Check if we're still valid */
239 /* Extract a command. This is kind of sloppy. */
242 const char *inptr = args;
243 while (*inptr != '\0'
244 && !IsWhitespace(inptr)
246 && cmd_len < sizeof(cmd_buf) - 1)
248 cmd_buf[cmd_len++] = *inptr++;
250 cmd_buf[cmd_len] = '\0';
252 /* See if we have this registered */
253 bool prepended = false;
254 if (!g_ConCmds.LookForSourceModCommand(cmd_buf))
256 /* Check if we had an "sm_" prefix */
257 if (strncmp(cmd_buf, "sm_", 3) == 0)
262 /* Now, prepend. Don't worry about the buffers. This will
263 * work because the sizes are limited from earlier.
266 strcpy(new_buf, "sm_");
267 strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3);
270 if (!g_ConCmds.LookForSourceModCommand(new_buf))
278 /* See if we need to do extra string manipulation */
279 if (is_quoted || prepended)
283 /* Check if we need to prepend sm_ */
286 len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
288 len = strncopy(m_ToExecute, args, sizeof(m_ToExecute));
291 /* Check if we need to strip a quote */
294 if (m_ToExecute[len-1] == '"')
296 m_ToExecute[--len] = '\0';
300 strncopy(m_ToExecute, args, sizeof(m_ToExecute));
306 unsigned int ChatTriggers::SetReplyTo(unsigned int reply)
308 unsigned int old = m_ReplyTo;
315 unsigned int ChatTriggers::GetReplyTo()