core/ChatTriggers.cpp
author David Anderson <dvander@alliedmods.net>
Wed Aug 01 02:12:47 2007 +0000 (2007-08-01)
changeset 1231 17badaf66ce4
parent 1139 5e2ace825428
child 1330 ab94316bb52b
permissions -rw-r--r--
updated more header files (mostly internal now)
     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 
     9  * modify it under the terms of the GNU General Public License, 
    10  * version 3.0, as published by the Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful,
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  * GNU General Public License for more details.
    16  *
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to 
    21  * link the code of this program (as well as its derivative works) to 
    22  * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any 
    23  * Game MODs that run on software by the Valve Corporation.  You must 
    24  * obey the GNU General Public License in all respects for all other 
    25  * code used. Additionally, AlliedModders LLC grants this exception 
    26  * to all derivative works. AlliedModders LLC defines further 
    27  * exceptions, found in LICENSE.txt (as of this writing, version 
    28  * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #include "ChatTriggers.h"
    34 #include "sm_stringutil.h"
    35 #include "TextParsers.h"
    36 #include "ConCmdManager.h"
    37 
    38 /* :HACKHACK: We can't SH_DECL here because ConCmdManager.cpp does */
    39 extern bool __SourceHook_FHRemoveConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
    40 
    41 #if SH_IMPL_VERSION >= 4
    42 extern int __SourceHook_FHAddConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
    43 #else
    44 extern bool __SourceHook_FHAddConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
    45 #endif
    46 
    47 ChatTriggers g_ChatTriggers;
    48 
    49 ChatTriggers::ChatTriggers() : m_pSayCmd(NULL), m_bWillProcessInPost(false), 
    50 	m_bTriggerWasSilent(false), m_ReplyTo(SM_REPLY_CONSOLE)
    51 {
    52 	m_PubTrigger = sm_strdup("!");
    53 	m_PrivTrigger = sm_strdup("/");
    54 	m_PubTriggerSize = 1;
    55 	m_PrivTriggerSize = 1;
    56 }
    57 
    58 ChatTriggers::~ChatTriggers()
    59 {
    60 	delete [] m_PubTrigger;
    61 	m_PubTrigger = NULL;
    62 	delete [] m_PrivTrigger;
    63 	m_PrivTrigger = NULL;
    64 }
    65 
    66 ConfigResult ChatTriggers::OnSourceModConfigChanged(const char *key, 
    67 													const char *value, 
    68 													ConfigSource source,
    69 													char *error, 
    70 													size_t maxlength)
    71 {
    72 	if (strcmp(key, "PublicChatTrigger") == 0)
    73 	{
    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;
    83 	}
    84 
    85 	return ConfigResult_Ignore;
    86 }
    87 
    88 void ChatTriggers::OnSourceModGameInitialized()
    89 {
    90 	unsigned int total = 2;
    91 	ConCommandBase *pCmd = icvar->GetCommands();
    92 	const char *name;
    93 	while (pCmd)
    94 	{
    95 		if (pCmd->IsCommand())
    96 		{
    97 			name = pCmd->GetName();
    98 			if (!m_pSayCmd && strcmp(name, "say") == 0)
    99 			{
   100 				m_pSayCmd = (ConCommand *)pCmd;
   101 				if (--total == 0)
   102 				{
   103 					break;
   104 				}
   105 			} else if (!m_pSayTeamCmd && strcmp(name, "say_team") == 0) {
   106 				m_pSayTeamCmd = (ConCommand *)pCmd;
   107 				if (--total == 0)
   108 				{
   109 					break;
   110 				}
   111 			}
   112 		}
   113 		pCmd = const_cast<ConCommandBase *>(pCmd->GetNext());
   114 	}
   115 
   116 	if (m_pSayCmd)
   117 	{
   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);
   120 	}
   121 	if (m_pSayTeamCmd)
   122 	{
   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);
   125 	}
   126 }
   127 
   128 void ChatTriggers::OnSourceModShutdown()
   129 {
   130 	if (m_pSayTeamCmd)
   131 	{
   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);
   134 	}
   135 	if (m_pSayCmd)
   136 	{
   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);
   139 	}
   140 }
   141 
   142 void ChatTriggers::OnSayCommand_Pre()
   143 {
   144 	int client = g_ConCmds.GetCommandClient();
   145 
   146 	/* The server console cannot do this */
   147 	if (client == 0)
   148 	{
   149 		RETURN_META(MRES_IGNORED);
   150 	}
   151 
   152 	const char *args = engine->Cmd_Args();
   153 	
   154 	if (!args)
   155 	{
   156 		RETURN_META(MRES_IGNORED);
   157 	}
   158 
   159 	/* Handle quoted string sets */
   160 	bool is_quoted = false;
   161 	if (args[0] == '"')
   162 	{
   163 		args++;
   164 		is_quoted = true;
   165 	}
   166 
   167 	bool is_trigger = false;
   168 	bool is_silent = false;
   169 
   170 	/* Check for either trigger */
   171 	if (m_PubTriggerSize && strncmp(args, m_PubTrigger, m_PubTriggerSize) == 0)
   172 	{
   173 		is_trigger = true;
   174 		args = &args[m_PubTriggerSize];
   175 	} else if (m_PrivTriggerSize && strncmp(args, m_PrivTrigger, m_PrivTriggerSize) == 0) {
   176 		is_trigger = true;
   177 		is_silent = true;
   178 		args = &args[m_PrivTriggerSize];
   179 	}
   180 
   181 	if (!is_trigger)
   182 	{
   183 		RETURN_META(MRES_IGNORED);
   184 	}
   185 
   186 	/**
   187 	 * Test if this is actually a command!
   188 	 */
   189 	if (!PreProcessTrigger(engine->PEntityOfEntIndex(client), args, is_quoted))
   190 	{
   191 		RETURN_META(MRES_IGNORED);
   192 	}
   193 
   194 	/**
   195 	 * We'll execute it in post.
   196 	 */
   197 	m_bWillProcessInPost = true;
   198 	m_bTriggerWasSilent = is_silent;
   199 
   200 	/* If we're silent, block */
   201 	if (is_silent)
   202 	{
   203 		RETURN_META(MRES_SUPERCEDE);
   204 	}
   205 
   206 	/* Otherwise, let the command continue */
   207 	RETURN_META(MRES_IGNORED);
   208 }
   209 
   210 void ChatTriggers::OnSayCommand_Post()
   211 {
   212 	if (m_bWillProcessInPost)
   213 	{
   214 		/* Reset this for re-entrancy */
   215 		m_bWillProcessInPost = false;
   216 		
   217 		/* Execute the cached command */
   218 		int client = g_ConCmds.GetCommandClient();
   219 		unsigned int old = SetReplyTo(SM_REPLY_CHAT);
   220 		serverpluginhelpers->ClientCommand(engine->PEntityOfEntIndex(client), m_ToExecute);
   221 		SetReplyTo(old);
   222 	}
   223 }
   224 
   225 bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args, bool is_quoted)
   226 {
   227 	/* Eat up whitespace */
   228 	while (*args != '\0' && IsWhitespace(args))
   229 	{
   230 		args++;
   231 	}
   232 
   233 	/* Check if we're still valid */
   234 	if (*args == '\0')
   235 	{
   236 		return false;
   237 	}
   238 
   239 	/* Extract a command.  This is kind of sloppy. */
   240 	char cmd_buf[64];
   241 	size_t cmd_len = 0;
   242 	const char *inptr = args;
   243 	while (*inptr != '\0' 
   244 			&& !IsWhitespace(inptr) 
   245 			&& *inptr != '"'
   246 			&& cmd_len < sizeof(cmd_buf) - 1)
   247 	{
   248 		cmd_buf[cmd_len++] = *inptr++;
   249 	}
   250 	cmd_buf[cmd_len] = '\0';
   251 
   252 	/* See if we have this registered */
   253 	bool prepended = false;
   254 	if (!g_ConCmds.LookForSourceModCommand(cmd_buf))
   255 	{
   256 		/* Check if we had an "sm_" prefix */
   257 		if (strncmp(cmd_buf, "sm_", 3) == 0)
   258 		{
   259 			return false;
   260 		}
   261 
   262 		/* Now, prepend.  Don't worry about the buffers.  This will 
   263 		 * work because the sizes are limited from earlier.
   264 		 */
   265 		char new_buf[80];
   266 		strcpy(new_buf, "sm_");
   267 		strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3);
   268 
   269 		/* Recheck */
   270 		if (!g_ConCmds.LookForSourceModCommand(new_buf))
   271 		{
   272 			return false;
   273 		}
   274 		
   275 		prepended = true;
   276 	}
   277 
   278 	/* See if we need to do extra string manipulation */
   279 	if (is_quoted || prepended)
   280 	{
   281 		size_t len;
   282 
   283 		/* Check if we need to prepend sm_ */
   284 		if (prepended)
   285 		{
   286 			len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
   287 		} else {
   288 			len = strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   289 		}
   290 		
   291 		/* Check if we need to strip a quote */
   292 		if (is_quoted)
   293 		{
   294 			if (m_ToExecute[len-1] == '"')
   295 			{
   296 				m_ToExecute[--len] = '\0';
   297 			}
   298 		}
   299 	} else {
   300 		strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   301 	}
   302 
   303 	return true;
   304 }
   305 
   306 unsigned int ChatTriggers::SetReplyTo(unsigned int reply)
   307 {
   308 	unsigned int old = m_ReplyTo;
   309 
   310 	m_ReplyTo = reply;
   311 
   312 	return old;
   313 }
   314 
   315 unsigned int ChatTriggers::GetReplyTo()
   316 {
   317 	return m_ReplyTo;
   318 }