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 "ChatTriggers.h"
33 #include "sm_stringutil.h"
34 #include "TextParsers.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;
57 ChatTriggers::~ChatTriggers()
59 delete [] m_PubTrigger;
61 delete [] m_PrivTrigger;
65 ConfigResult ChatTriggers::OnSourceModConfigChanged(const char *key,
71 if (strcmp(key, "PublicChatTrigger") == 0)
73 delete [] m_PubTrigger;
74 m_PubTrigger = sm_strdup(value);
75 m_PubTriggerSize = strlen(m_PubTrigger);
76 return ConfigResult_Accept;
77 } else if (strcmp(key, "SilentChatTrigger") == 0) {
78 delete [] m_PrivTrigger;
79 m_PrivTrigger = sm_strdup(value);
80 m_PrivTriggerSize = strlen(m_PrivTrigger);
81 return ConfigResult_Accept;
84 return ConfigResult_Ignore;
87 void ChatTriggers::OnSourceModGameInitialized()
89 unsigned int total = 2;
90 ConCommandBase *pCmd = icvar->GetCommands();
94 if (pCmd->IsCommand())
96 name = pCmd->GetName();
97 if (!m_pSayCmd && strcmp(name, "say") == 0)
99 m_pSayCmd = (ConCommand *)pCmd;
104 } else if (!m_pSayTeamCmd && strcmp(name, "say_team") == 0) {
105 m_pSayTeamCmd = (ConCommand *)pCmd;
112 pCmd = const_cast<ConCommandBase *>(pCmd->GetNext());
117 SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
118 SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Post, true);
122 SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
123 SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Post, true);
127 void ChatTriggers::OnSourceModShutdown()
131 SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Post, true);
132 SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
136 SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Post, true);
137 SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
141 void ChatTriggers::OnSayCommand_Pre()
143 int client = g_ConCmds.GetCommandClient();
145 /* The server console cannot do this */
148 RETURN_META(MRES_IGNORED);
151 const char *args = engine->Cmd_Args();
155 RETURN_META(MRES_IGNORED);
158 /* Handle quoted string sets */
159 bool is_quoted = false;
166 bool is_trigger = false;
167 bool is_silent = false;
169 /* Check for either trigger */
170 if (m_PubTriggerSize && strncmp(args, m_PubTrigger, m_PubTriggerSize) == 0)
173 args = &args[m_PubTriggerSize];
174 } else if (m_PrivTriggerSize && strncmp(args, m_PrivTrigger, m_PrivTriggerSize) == 0) {
177 args = &args[m_PrivTriggerSize];
182 RETURN_META(MRES_IGNORED);
186 * Test if this is actually a command!
188 if (!PreProcessTrigger(engine->PEntityOfEntIndex(client), args, is_quoted))
190 RETURN_META(MRES_IGNORED);
194 * We'll execute it in post.
196 m_bWillProcessInPost = true;
197 m_bTriggerWasSilent = is_silent;
199 /* If we're silent, block */
202 RETURN_META(MRES_SUPERCEDE);
205 /* Otherwise, let the command continue */
206 RETURN_META(MRES_IGNORED);
209 void ChatTriggers::OnSayCommand_Post()
211 if (m_bWillProcessInPost)
213 /* Reset this for re-entrancy */
214 m_bWillProcessInPost = false;
216 /* Execute the cached command */
217 int client = g_ConCmds.GetCommandClient();
218 unsigned int old = SetReplyTo(SM_REPLY_CHAT);
219 serverpluginhelpers->ClientCommand(engine->PEntityOfEntIndex(client), m_ToExecute);
224 bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args, bool is_quoted)
226 /* Eat up whitespace */
227 while (*args != '\0' && IsWhitespace(args))
232 /* Check if we're still valid */
238 /* Extract a command. This is kind of sloppy. */
241 const char *inptr = args;
242 while (*inptr != '\0'
243 && !IsWhitespace(inptr)
245 && cmd_len < sizeof(cmd_buf) - 1)
247 cmd_buf[cmd_len++] = *inptr++;
249 cmd_buf[cmd_len] = '\0';
251 /* See if we have this registered */
252 bool prepended = false;
253 if (!g_ConCmds.LookForSourceModCommand(cmd_buf))
255 /* Check if we had an "sm_" prefix */
256 if (strncmp(cmd_buf, "sm_", 3) == 0)
261 /* Now, prepend. Don't worry about the buffers. This will
262 * work because the sizes are limited from earlier.
265 strcpy(new_buf, "sm_");
266 strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3);
269 if (!g_ConCmds.LookForSourceModCommand(new_buf))
277 /* See if we need to do extra string manipulation */
278 if (is_quoted || prepended)
282 /* Check if we need to prepend sm_ */
285 len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
287 len = strncopy(m_ToExecute, args, sizeof(m_ToExecute));
290 /* Check if we need to strip a quote */
293 if (m_ToExecute[len-1] == '"')
295 m_ToExecute[--len] = '\0';
299 strncopy(m_ToExecute, args, sizeof(m_ToExecute));
305 unsigned int ChatTriggers::SetReplyTo(unsigned int reply)
307 unsigned int old = m_ReplyTo;
314 unsigned int ChatTriggers::GetReplyTo()