core/ChatTriggers.cpp
author David Anderson <dvander@alliedmods.net>
Sun Aug 26 17:41:02 2007 +0000 (2007-08-26)
changeset 1369 bd9ccced1b1a
parent 1330 ab94316bb52b
child 1377 723de5e67879
permissions -rw-r--r--
fixed amb834 - command callbacks could potentially occur on invalid clients in very rare cases
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "ChatTriggers.h"
    33 #include "sm_stringutil.h"
    34 #include "TextParsers.h"
    35 #include "ConCmdManager.h"
    36 
    37 /* :HACKHACK: We can't SH_DECL here because ConCmdManager.cpp does */
    38 extern bool __SourceHook_FHRemoveConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
    39 
    40 #if SH_IMPL_VERSION >= 4
    41 extern int __SourceHook_FHAddConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
    42 #else
    43 extern bool __SourceHook_FHAddConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
    44 #endif
    45 
    46 ChatTriggers g_ChatTriggers;
    47 
    48 ChatTriggers::ChatTriggers() : m_pSayCmd(NULL), m_bWillProcessInPost(false), 
    49 	m_bTriggerWasSilent(false), m_ReplyTo(SM_REPLY_CONSOLE)
    50 {
    51 	m_PubTrigger = sm_strdup("!");
    52 	m_PrivTrigger = sm_strdup("/");
    53 	m_PubTriggerSize = 1;
    54 	m_PrivTriggerSize = 1;
    55 }
    56 
    57 ChatTriggers::~ChatTriggers()
    58 {
    59 	delete [] m_PubTrigger;
    60 	m_PubTrigger = NULL;
    61 	delete [] m_PrivTrigger;
    62 	m_PrivTrigger = NULL;
    63 }
    64 
    65 ConfigResult ChatTriggers::OnSourceModConfigChanged(const char *key, 
    66 													const char *value, 
    67 													ConfigSource source,
    68 													char *error, 
    69 													size_t maxlength)
    70 {
    71 	if (strcmp(key, "PublicChatTrigger") == 0)
    72 	{
    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;
    82 	}
    83 
    84 	return ConfigResult_Ignore;
    85 }
    86 
    87 void ChatTriggers::OnSourceModGameInitialized()
    88 {
    89 	unsigned int total = 2;
    90 	ConCommandBase *pCmd = icvar->GetCommands();
    91 	const char *name;
    92 	while (pCmd)
    93 	{
    94 		if (pCmd->IsCommand())
    95 		{
    96 			name = pCmd->GetName();
    97 			if (!m_pSayCmd && strcmp(name, "say") == 0)
    98 			{
    99 				m_pSayCmd = (ConCommand *)pCmd;
   100 				if (--total == 0)
   101 				{
   102 					break;
   103 				}
   104 			} else if (!m_pSayTeamCmd && strcmp(name, "say_team") == 0) {
   105 				m_pSayTeamCmd = (ConCommand *)pCmd;
   106 				if (--total == 0)
   107 				{
   108 					break;
   109 				}
   110 			}
   111 		}
   112 		pCmd = const_cast<ConCommandBase *>(pCmd->GetNext());
   113 	}
   114 
   115 	if (m_pSayCmd)
   116 	{
   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);
   119 	}
   120 	if (m_pSayTeamCmd)
   121 	{
   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);
   124 	}
   125 }
   126 
   127 void ChatTriggers::OnSourceModShutdown()
   128 {
   129 	if (m_pSayTeamCmd)
   130 	{
   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);
   133 	}
   134 	if (m_pSayCmd)
   135 	{
   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);
   138 	}
   139 }
   140 
   141 void ChatTriggers::OnSayCommand_Pre()
   142 {
   143 	int client = g_ConCmds.GetCommandClient();
   144 
   145 	/* The server console cannot do this */
   146 	if (client == 0)
   147 	{
   148 		RETURN_META(MRES_IGNORED);
   149 	}
   150 
   151 	const char *args = engine->Cmd_Args();
   152 	
   153 	if (!args)
   154 	{
   155 		RETURN_META(MRES_IGNORED);
   156 	}
   157 
   158 	/* Handle quoted string sets */
   159 	bool is_quoted = false;
   160 	if (args[0] == '"')
   161 	{
   162 		args++;
   163 		is_quoted = true;
   164 	}
   165 
   166 	bool is_trigger = false;
   167 	bool is_silent = false;
   168 
   169 	/* Check for either trigger */
   170 	if (m_PubTriggerSize && strncmp(args, m_PubTrigger, m_PubTriggerSize) == 0)
   171 	{
   172 		is_trigger = true;
   173 		args = &args[m_PubTriggerSize];
   174 	} else if (m_PrivTriggerSize && strncmp(args, m_PrivTrigger, m_PrivTriggerSize) == 0) {
   175 		is_trigger = true;
   176 		is_silent = true;
   177 		args = &args[m_PrivTriggerSize];
   178 	}
   179 
   180 	if (!is_trigger)
   181 	{
   182 		RETURN_META(MRES_IGNORED);
   183 	}
   184 
   185 	/**
   186 	 * Test if this is actually a command!
   187 	 */
   188 	if (!PreProcessTrigger(engine->PEntityOfEntIndex(client), args, is_quoted))
   189 	{
   190 		RETURN_META(MRES_IGNORED);
   191 	}
   192 
   193 	/**
   194 	 * We'll execute it in post.
   195 	 */
   196 	m_bWillProcessInPost = true;
   197 	m_bTriggerWasSilent = is_silent;
   198 
   199 	/* If we're silent, block */
   200 	if (is_silent)
   201 	{
   202 		RETURN_META(MRES_SUPERCEDE);
   203 	}
   204 
   205 	/* Otherwise, let the command continue */
   206 	RETURN_META(MRES_IGNORED);
   207 }
   208 
   209 void ChatTriggers::OnSayCommand_Post()
   210 {
   211 	if (m_bWillProcessInPost)
   212 	{
   213 		/* Reset this for re-entrancy */
   214 		m_bWillProcessInPost = false;
   215 		
   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);
   220 		SetReplyTo(old);
   221 	}
   222 }
   223 
   224 bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args, bool is_quoted)
   225 {
   226 	/* Eat up whitespace */
   227 	while (*args != '\0' && IsWhitespace(args))
   228 	{
   229 		args++;
   230 	}
   231 
   232 	/* Check if we're still valid */
   233 	if (*args == '\0')
   234 	{
   235 		return false;
   236 	}
   237 
   238 	/* Extract a command.  This is kind of sloppy. */
   239 	char cmd_buf[64];
   240 	size_t cmd_len = 0;
   241 	const char *inptr = args;
   242 	while (*inptr != '\0' 
   243 			&& !IsWhitespace(inptr) 
   244 			&& *inptr != '"'
   245 			&& cmd_len < sizeof(cmd_buf) - 1)
   246 	{
   247 		cmd_buf[cmd_len++] = *inptr++;
   248 	}
   249 	cmd_buf[cmd_len] = '\0';
   250 
   251 	/* See if we have this registered */
   252 	bool prepended = false;
   253 	if (!g_ConCmds.LookForSourceModCommand(cmd_buf))
   254 	{
   255 		/* Check if we had an "sm_" prefix */
   256 		if (strncmp(cmd_buf, "sm_", 3) == 0)
   257 		{
   258 			return false;
   259 		}
   260 
   261 		/* Now, prepend.  Don't worry about the buffers.  This will 
   262 		 * work because the sizes are limited from earlier.
   263 		 */
   264 		char new_buf[80];
   265 		strcpy(new_buf, "sm_");
   266 		strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3);
   267 
   268 		/* Recheck */
   269 		if (!g_ConCmds.LookForSourceModCommand(new_buf))
   270 		{
   271 			return false;
   272 		}
   273 		
   274 		prepended = true;
   275 	}
   276 
   277 	/* See if we need to do extra string manipulation */
   278 	if (is_quoted || prepended)
   279 	{
   280 		size_t len;
   281 
   282 		/* Check if we need to prepend sm_ */
   283 		if (prepended)
   284 		{
   285 			len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
   286 		} else {
   287 			len = strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   288 		}
   289 		
   290 		/* Check if we need to strip a quote */
   291 		if (is_quoted)
   292 		{
   293 			if (m_ToExecute[len-1] == '"')
   294 			{
   295 				m_ToExecute[--len] = '\0';
   296 			}
   297 		}
   298 	} else {
   299 		strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   300 	}
   301 
   302 	return true;
   303 }
   304 
   305 unsigned int ChatTriggers::SetReplyTo(unsigned int reply)
   306 {
   307 	unsigned int old = m_ReplyTo;
   308 
   309 	m_ReplyTo = reply;
   310 
   311 	return old;
   312 }
   313 
   314 unsigned int ChatTriggers::GetReplyTo()
   315 {
   316 	return m_ReplyTo;
   317 }