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