core/ChatTriggers.cpp
author David Anderson <dvander@alliedmods.net>
Tue Sep 11 18:08:49 2007 +0000 (2007-09-11)
changeset 1408 2fd63cc4bd9c
parent 1406 f5c07ec3c052
child 1469 300cc2432356
permissions -rw-r--r--
timelimit function now gets initialized
     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 
    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 	/* Extract a command.  This is kind of sloppy. */
   227 	char cmd_buf[64];
   228 	size_t cmd_len = 0;
   229 	const char *inptr = args;
   230 	while (*inptr != '\0' 
   231 			&& !textparsers->IsWhitespace(inptr) 
   232 			&& *inptr != '"'
   233 			&& cmd_len < sizeof(cmd_buf) - 1)
   234 	{
   235 		cmd_buf[cmd_len++] = *inptr++;
   236 	}
   237 	cmd_buf[cmd_len] = '\0';
   238 
   239 	if (cmd_len == 0)
   240 	{
   241 		return false;
   242 	}
   243 
   244 	/* See if we have this registered */
   245 	bool prepended = false;
   246 	if (!g_ConCmds.LookForSourceModCommand(cmd_buf))
   247 	{
   248 		/* Check if we had an "sm_" prefix */
   249 		if (strncmp(cmd_buf, "sm_", 3) == 0)
   250 		{
   251 			return false;
   252 		}
   253 
   254 		/* Now, prepend.  Don't worry about the buffers.  This will 
   255 		 * work because the sizes are limited from earlier.
   256 		 */
   257 		char new_buf[80];
   258 		strcpy(new_buf, "sm_");
   259 		strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3);
   260 
   261 		/* Recheck */
   262 		if (!g_ConCmds.LookForSourceModCommand(new_buf))
   263 		{
   264 			return false;
   265 		}
   266 		
   267 		prepended = true;
   268 	}
   269 
   270 	/* See if we need to do extra string manipulation */
   271 	if (is_quoted || prepended)
   272 	{
   273 		size_t len;
   274 
   275 		/* Check if we need to prepend sm_ */
   276 		if (prepended)
   277 		{
   278 			len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
   279 		} else {
   280 			len = strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   281 		}
   282 		
   283 		/* Check if we need to strip a quote */
   284 		if (is_quoted)
   285 		{
   286 			if (m_ToExecute[len-1] == '"')
   287 			{
   288 				m_ToExecute[--len] = '\0';
   289 			}
   290 		}
   291 	} else {
   292 		strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   293 	}
   294 
   295 	return true;
   296 }
   297 
   298 unsigned int ChatTriggers::SetReplyTo(unsigned int reply)
   299 {
   300 	unsigned int old = m_ReplyTo;
   301 
   302 	m_ReplyTo = reply;
   303 
   304 	return old;
   305 }
   306 
   307 unsigned int ChatTriggers::GetReplyTo()
   308 {
   309 	return m_ReplyTo;
   310 }