core/ChatTriggers.cpp
author David Anderson <dvander@alliedmods.net>
Sun Oct 14 21:31:39 2007 +0000 (2007-10-14)
changeset 1558 95a3d8bf618d
parent 1547 78abc407c8a3
child 1794 4778536abecc
permissions -rw-r--r--
all pdbs are now packaged with windows
     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 <ITextParsers.h>
    33 #include "ChatTriggers.h"
    34 #include "sm_stringutil.h"
    35 #include "ConCmdManager.h"
    36 #include <IPlayerHelpers.h>
    37 
    38 /* :HACKHACK: We can't SH_DECL here because ConCmdManager.cpp does.
    39  * While the OB build only runs on MM:S 1.6.0+ (SH 5+), the older one 
    40  * can technically be compiled against any MM:S version after 1.4.2.
    41  */
    42 #if defined ORANGEBOX_BUILD
    43 extern bool __SourceHook_FHRemoveConCommandDispatch(void *, bool, class fastdelegate::FastDelegate1<const CCommand &, void>);
    44 extern int __SourceHook_FHAddConCommandDispatch(void *, ISourceHook::AddHookMode, bool, class fastdelegate::FastDelegate1<const CCommand &, void>);
    45 #else
    46 extern bool __SourceHook_FHRemoveConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
    47 #if SH_IMPL_VERSION >= 5
    48 extern int __SourceHook_FHAddConCommandDispatch(void *, ISourceHook::AddHookMode, bool, class fastdelegate::FastDelegate0<void>);
    49 #elif SH_IMPL_VERSION == 4
    50 extern int __SourceHook_FHAddConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
    51 #elif SH_IMPL_VERSION == 3
    52 extern bool __SourceHook_FHAddConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
    53 #endif //SH_IMPL_VERSION
    54 #endif //ORANGEBOX_BUILD
    55 
    56 ChatTriggers g_ChatTriggers;
    57 
    58 ChatTriggers::ChatTriggers() : m_pSayCmd(NULL), m_bWillProcessInPost(false), 
    59 	m_bTriggerWasSilent(false), m_ReplyTo(SM_REPLY_CONSOLE)
    60 {
    61 	m_PubTrigger = sm_strdup("!");
    62 	m_PrivTrigger = sm_strdup("/");
    63 	m_PubTriggerSize = 1;
    64 	m_PrivTriggerSize = 1;
    65 	m_bIsChatTrigger = false;
    66 }
    67 
    68 ChatTriggers::~ChatTriggers()
    69 {
    70 	delete [] m_PubTrigger;
    71 	m_PubTrigger = NULL;
    72 	delete [] m_PrivTrigger;
    73 	m_PrivTrigger = NULL;
    74 }
    75 
    76 ConfigResult ChatTriggers::OnSourceModConfigChanged(const char *key, 
    77 													const char *value, 
    78 													ConfigSource source,
    79 													char *error, 
    80 													size_t maxlength)
    81 {
    82 	if (strcmp(key, "PublicChatTrigger") == 0)
    83 	{
    84 		delete [] m_PubTrigger;
    85 		m_PubTrigger = sm_strdup(value);
    86 		m_PubTriggerSize = strlen(m_PubTrigger);
    87 		return ConfigResult_Accept;
    88 	} else if (strcmp(key, "SilentChatTrigger") == 0) {
    89 		delete [] m_PrivTrigger;
    90 		m_PrivTrigger = sm_strdup(value);
    91 		m_PrivTriggerSize = strlen(m_PrivTrigger);
    92 		return ConfigResult_Accept;
    93 	}
    94 
    95 	return ConfigResult_Ignore;
    96 }
    97 
    98 void ChatTriggers::OnSourceModGameInitialized()
    99 {
   100 	unsigned int total = 2;
   101 	ConCommandBase *pCmd = icvar->GetCommands();
   102 	const char *name;
   103 	while (pCmd)
   104 	{
   105 		if (pCmd->IsCommand())
   106 		{
   107 			name = pCmd->GetName();
   108 			if (!m_pSayCmd && strcmp(name, "say") == 0)
   109 			{
   110 				m_pSayCmd = (ConCommand *)pCmd;
   111 				if (--total == 0)
   112 				{
   113 					break;
   114 				}
   115 			} else if (!m_pSayTeamCmd && strcmp(name, "say_team") == 0) {
   116 				m_pSayTeamCmd = (ConCommand *)pCmd;
   117 				if (--total == 0)
   118 				{
   119 					break;
   120 				}
   121 			}
   122 		}
   123 		pCmd = const_cast<ConCommandBase *>(pCmd->GetNext());
   124 	}
   125 
   126 	if (m_pSayCmd)
   127 	{
   128 		SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
   129 		SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Post, true);
   130 	}
   131 	if (m_pSayTeamCmd)
   132 	{
   133 		SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
   134 		SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Post, true);
   135 	}
   136 }
   137 
   138 void ChatTriggers::OnSourceModShutdown()
   139 {
   140 	if (m_pSayTeamCmd)
   141 	{
   142 		SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Post, true);
   143 		SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
   144 	}
   145 	if (m_pSayCmd)
   146 	{
   147 		SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Post, true);
   148 		SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
   149 	}
   150 }
   151 
   152 #if defined ORANGEBOX_BUILD
   153 void ChatTriggers::OnSayCommand_Pre(const CCommand &command)
   154 {
   155 #else
   156 void ChatTriggers::OnSayCommand_Pre()
   157 {
   158 	CCommand command;
   159 #endif
   160 	int client = g_ConCmds.GetCommandClient();
   161 	m_bIsChatTrigger = false;
   162 
   163 	/* The server console cannot do this */
   164 	if (client == 0)
   165 	{
   166 		RETURN_META(MRES_IGNORED);
   167 	}
   168 
   169 	const char *args = command.ArgS();
   170 	
   171 	if (!args)
   172 	{
   173 		RETURN_META(MRES_IGNORED);
   174 	}
   175 
   176 	/* Handle quoted string sets */
   177 	bool is_quoted = false;
   178 	if (args[0] == '"')
   179 	{
   180 		args++;
   181 		is_quoted = true;
   182 	}
   183 
   184 	bool is_trigger = false;
   185 	bool is_silent = false;
   186 
   187 	/* Check for either trigger */
   188 	if (m_PubTriggerSize && strncmp(args, m_PubTrigger, m_PubTriggerSize) == 0)
   189 	{
   190 		is_trigger = true;
   191 		args = &args[m_PubTriggerSize];
   192 	} else if (m_PrivTriggerSize && strncmp(args, m_PrivTrigger, m_PrivTriggerSize) == 0) {
   193 		is_trigger = true;
   194 		is_silent = true;
   195 		args = &args[m_PrivTriggerSize];
   196 	}
   197 
   198 	if (!is_trigger)
   199 	{
   200 		RETURN_META(MRES_IGNORED);
   201 	}
   202 
   203 	/**
   204 	 * Test if this is actually a command!
   205 	 */
   206 	if (!PreProcessTrigger(engine->PEntityOfEntIndex(client), args, is_quoted))
   207 	{
   208 		RETURN_META(MRES_IGNORED);
   209 	}
   210 
   211 	m_bIsChatTrigger = true;
   212 
   213 	/**
   214 	 * We'll execute it in post.
   215 	 */
   216 	m_bWillProcessInPost = true;
   217 	m_bTriggerWasSilent = is_silent;
   218 
   219 	/* If we're silent, block */
   220 	if (is_silent)
   221 	{
   222 		RETURN_META(MRES_SUPERCEDE);
   223 	}
   224 
   225 	/* Otherwise, let the command continue */
   226 	RETURN_META(MRES_IGNORED);
   227 }
   228 
   229 #if defined ORANGEBOX_BUILD
   230 void ChatTriggers::OnSayCommand_Post(const CCommand &command)
   231 #else
   232 void ChatTriggers::OnSayCommand_Post()
   233 #endif
   234 {
   235 	m_bIsChatTrigger = false;
   236 	if (m_bWillProcessInPost)
   237 	{
   238 		/* Reset this for re-entrancy */
   239 		m_bWillProcessInPost = false;
   240 		
   241 		/* Execute the cached command */
   242 		int client = g_ConCmds.GetCommandClient();
   243 		unsigned int old = SetReplyTo(SM_REPLY_CHAT);
   244 		serverpluginhelpers->ClientCommand(engine->PEntityOfEntIndex(client), m_ToExecute);
   245 		SetReplyTo(old);
   246 	}
   247 }
   248 
   249 bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args, bool is_quoted)
   250 {
   251 	/* Extract a command.  This is kind of sloppy. */
   252 	char cmd_buf[64];
   253 	size_t cmd_len = 0;
   254 	const char *inptr = args;
   255 	while (*inptr != '\0' 
   256 			&& !textparsers->IsWhitespace(inptr) 
   257 			&& *inptr != '"'
   258 			&& cmd_len < sizeof(cmd_buf) - 1)
   259 	{
   260 		cmd_buf[cmd_len++] = *inptr++;
   261 	}
   262 	cmd_buf[cmd_len] = '\0';
   263 
   264 	if (cmd_len == 0)
   265 	{
   266 		return false;
   267 	}
   268 
   269 	/* See if we have this registered */
   270 	bool prepended = false;
   271 	if (!g_ConCmds.LookForSourceModCommand(cmd_buf))
   272 	{
   273 		/* Check if we had an "sm_" prefix */
   274 		if (strncmp(cmd_buf, "sm_", 3) == 0)
   275 		{
   276 			return false;
   277 		}
   278 
   279 		/* Now, prepend.  Don't worry about the buffers.  This will 
   280 		 * work because the sizes are limited from earlier.
   281 		 */
   282 		char new_buf[80];
   283 		strcpy(new_buf, "sm_");
   284 		strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3);
   285 
   286 		/* Recheck */
   287 		if (!g_ConCmds.LookForSourceModCommand(new_buf))
   288 		{
   289 			return false;
   290 		}
   291 		
   292 		prepended = true;
   293 	}
   294 
   295 	/* See if we need to do extra string manipulation */
   296 	if (is_quoted || prepended)
   297 	{
   298 		size_t len;
   299 
   300 		/* Check if we need to prepend sm_ */
   301 		if (prepended)
   302 		{
   303 			len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
   304 		} else {
   305 			len = strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   306 		}
   307 		
   308 		/* Check if we need to strip a quote */
   309 		if (is_quoted)
   310 		{
   311 			if (m_ToExecute[len-1] == '"')
   312 			{
   313 				m_ToExecute[--len] = '\0';
   314 			}
   315 		}
   316 	} else {
   317 		strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   318 	}
   319 
   320 	return true;
   321 }
   322 
   323 unsigned int ChatTriggers::SetReplyTo(unsigned int reply)
   324 {
   325 	unsigned int old = m_ReplyTo;
   326 
   327 	m_ReplyTo = reply;
   328 
   329 	return old;
   330 }
   331 
   332 unsigned int ChatTriggers::GetReplyTo()
   333 {
   334 	return m_ReplyTo;
   335 }
   336 
   337 bool ChatTriggers::IsChatTrigger()
   338 {
   339 	return m_bIsChatTrigger;
   340 }