3 * =============================================================================
5 * Copyright (C) 2004-2007 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"
37 /* :HACKHACK: We can't SH_DECL here because ConCmdManager.cpp does */
38 extern bool __SourceHook_FHRemoveConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
40 #if SH_IMPL_VERSION >= 4
41 extern int __SourceHook_FHAddConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
43 extern bool __SourceHook_FHAddConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
46 ChatTriggers g_ChatTriggers;
48 ChatTriggers::ChatTriggers() : m_pSayCmd(NULL), m_bWillProcessInPost(false),
49 m_bTriggerWasSilent(false), m_ReplyTo(SM_REPLY_CONSOLE)
51 m_PubTrigger = sm_strdup("!");
52 m_PrivTrigger = sm_strdup("/");
54 m_PrivTriggerSize = 1;
55 m_bIsChatTrigger = false;
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();
145 m_bIsChatTrigger = false;
147 /* The server console cannot do this */
150 RETURN_META(MRES_IGNORED);
153 const char *args = engine->Cmd_Args();
157 RETURN_META(MRES_IGNORED);
160 /* Handle quoted string sets */
161 bool is_quoted = false;
168 bool is_trigger = false;
169 bool is_silent = false;
171 /* Check for either trigger */
172 if (m_PubTriggerSize && strncmp(args, m_PubTrigger, m_PubTriggerSize) == 0)
175 args = &args[m_PubTriggerSize];
176 } else if (m_PrivTriggerSize && strncmp(args, m_PrivTrigger, m_PrivTriggerSize) == 0) {
179 args = &args[m_PrivTriggerSize];
184 RETURN_META(MRES_IGNORED);
188 * Test if this is actually a command!
190 if (!PreProcessTrigger(engine->PEntityOfEntIndex(client), args, is_quoted))
192 RETURN_META(MRES_IGNORED);
195 m_bIsChatTrigger = true;
198 * We'll execute it in post.
200 m_bWillProcessInPost = true;
201 m_bTriggerWasSilent = is_silent;
203 /* If we're silent, block */
206 RETURN_META(MRES_SUPERCEDE);
209 /* Otherwise, let the command continue */
210 RETURN_META(MRES_IGNORED);
213 void ChatTriggers::OnSayCommand_Post()
215 m_bIsChatTrigger = false;
216 if (m_bWillProcessInPost)
218 /* Reset this for re-entrancy */
219 m_bWillProcessInPost = false;
221 /* Execute the cached command */
222 int client = g_ConCmds.GetCommandClient();
223 unsigned int old = SetReplyTo(SM_REPLY_CHAT);
224 serverpluginhelpers->ClientCommand(engine->PEntityOfEntIndex(client), m_ToExecute);
229 bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args, bool is_quoted)
231 /* Extract a command. This is kind of sloppy. */
234 const char *inptr = args;
235 while (*inptr != '\0'
236 && !textparsers->IsWhitespace(inptr)
238 && cmd_len < sizeof(cmd_buf) - 1)
240 cmd_buf[cmd_len++] = *inptr++;
242 cmd_buf[cmd_len] = '\0';
249 /* See if we have this registered */
250 bool prepended = false;
251 if (!g_ConCmds.LookForSourceModCommand(cmd_buf))
253 /* Check if we had an "sm_" prefix */
254 if (strncmp(cmd_buf, "sm_", 3) == 0)
259 /* Now, prepend. Don't worry about the buffers. This will
260 * work because the sizes are limited from earlier.
263 strcpy(new_buf, "sm_");
264 strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3);
267 if (!g_ConCmds.LookForSourceModCommand(new_buf))
275 /* See if we need to do extra string manipulation */
276 if (is_quoted || prepended)
280 /* Check if we need to prepend sm_ */
283 len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
285 len = strncopy(m_ToExecute, args, sizeof(m_ToExecute));
288 /* Check if we need to strip a quote */
291 if (m_ToExecute[len-1] == '"')
293 m_ToExecute[--len] = '\0';
297 strncopy(m_ToExecute, args, sizeof(m_ToExecute));
303 unsigned int ChatTriggers::SetReplyTo(unsigned int reply)
305 unsigned int old = m_ReplyTo;
312 unsigned int ChatTriggers::GetReplyTo()
317 bool ChatTriggers::IsChatTrigger()
319 return m_bIsChatTrigger;