core/ChatTriggers.cpp
author David Anderson <dvander@alliedmods.net>
Wed Sep 26 14:38:40 2007 +0000 (2007-09-26)
changeset 1488 5562086c65c8
parent 1469 300cc2432356
child 1519 c325cd4b060a
permissions -rw-r--r--
initial import of UNTESTED, UNFINISHED topmenus extension
     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 	m_bIsChatTrigger = false;
    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 	m_bIsChatTrigger = false;
   146 
   147 	/* The server console cannot do this */
   148 	if (client == 0)
   149 	{
   150 		RETURN_META(MRES_IGNORED);
   151 	}
   152 
   153 	const char *args = engine->Cmd_Args();
   154 	
   155 	if (!args)
   156 	{
   157 		RETURN_META(MRES_IGNORED);
   158 	}
   159 
   160 	/* Handle quoted string sets */
   161 	bool is_quoted = false;
   162 	if (args[0] == '"')
   163 	{
   164 		args++;
   165 		is_quoted = true;
   166 	}
   167 
   168 	bool is_trigger = false;
   169 	bool is_silent = false;
   170 
   171 	/* Check for either trigger */
   172 	if (m_PubTriggerSize && strncmp(args, m_PubTrigger, m_PubTriggerSize) == 0)
   173 	{
   174 		is_trigger = true;
   175 		args = &args[m_PubTriggerSize];
   176 	} else if (m_PrivTriggerSize && strncmp(args, m_PrivTrigger, m_PrivTriggerSize) == 0) {
   177 		is_trigger = true;
   178 		is_silent = true;
   179 		args = &args[m_PrivTriggerSize];
   180 	}
   181 
   182 	if (!is_trigger)
   183 	{
   184 		RETURN_META(MRES_IGNORED);
   185 	}
   186 
   187 	/**
   188 	 * Test if this is actually a command!
   189 	 */
   190 	if (!PreProcessTrigger(engine->PEntityOfEntIndex(client), args, is_quoted))
   191 	{
   192 		RETURN_META(MRES_IGNORED);
   193 	}
   194 
   195 	m_bIsChatTrigger = true;
   196 
   197 	/**
   198 	 * We'll execute it in post.
   199 	 */
   200 	m_bWillProcessInPost = true;
   201 	m_bTriggerWasSilent = is_silent;
   202 
   203 	/* If we're silent, block */
   204 	if (is_silent)
   205 	{
   206 		RETURN_META(MRES_SUPERCEDE);
   207 	}
   208 
   209 	/* Otherwise, let the command continue */
   210 	RETURN_META(MRES_IGNORED);
   211 }
   212 
   213 void ChatTriggers::OnSayCommand_Post()
   214 {
   215 	m_bIsChatTrigger = false;
   216 	if (m_bWillProcessInPost)
   217 	{
   218 		/* Reset this for re-entrancy */
   219 		m_bWillProcessInPost = false;
   220 		
   221 		/* Execute the cached command */
   222 		int client = g_ConCmds.GetCommandClient();
   223 		unsigned int old = SetReplyTo(SM_REPLY_CHAT);
   224 		serverpluginhelpers->ClientCommand(engine->PEntityOfEntIndex(client), m_ToExecute);
   225 		SetReplyTo(old);
   226 	}
   227 }
   228 
   229 bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args, bool is_quoted)
   230 {
   231 	/* Extract a command.  This is kind of sloppy. */
   232 	char cmd_buf[64];
   233 	size_t cmd_len = 0;
   234 	const char *inptr = args;
   235 	while (*inptr != '\0' 
   236 			&& !textparsers->IsWhitespace(inptr) 
   237 			&& *inptr != '"'
   238 			&& cmd_len < sizeof(cmd_buf) - 1)
   239 	{
   240 		cmd_buf[cmd_len++] = *inptr++;
   241 	}
   242 	cmd_buf[cmd_len] = '\0';
   243 
   244 	if (cmd_len == 0)
   245 	{
   246 		return false;
   247 	}
   248 
   249 	/* See if we have this registered */
   250 	bool prepended = false;
   251 	if (!g_ConCmds.LookForSourceModCommand(cmd_buf))
   252 	{
   253 		/* Check if we had an "sm_" prefix */
   254 		if (strncmp(cmd_buf, "sm_", 3) == 0)
   255 		{
   256 			return false;
   257 		}
   258 
   259 		/* Now, prepend.  Don't worry about the buffers.  This will 
   260 		 * work because the sizes are limited from earlier.
   261 		 */
   262 		char new_buf[80];
   263 		strcpy(new_buf, "sm_");
   264 		strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3);
   265 
   266 		/* Recheck */
   267 		if (!g_ConCmds.LookForSourceModCommand(new_buf))
   268 		{
   269 			return false;
   270 		}
   271 		
   272 		prepended = true;
   273 	}
   274 
   275 	/* See if we need to do extra string manipulation */
   276 	if (is_quoted || prepended)
   277 	{
   278 		size_t len;
   279 
   280 		/* Check if we need to prepend sm_ */
   281 		if (prepended)
   282 		{
   283 			len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
   284 		} else {
   285 			len = strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   286 		}
   287 		
   288 		/* Check if we need to strip a quote */
   289 		if (is_quoted)
   290 		{
   291 			if (m_ToExecute[len-1] == '"')
   292 			{
   293 				m_ToExecute[--len] = '\0';
   294 			}
   295 		}
   296 	} else {
   297 		strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   298 	}
   299 
   300 	return true;
   301 }
   302 
   303 unsigned int ChatTriggers::SetReplyTo(unsigned int reply)
   304 {
   305 	unsigned int old = m_ReplyTo;
   306 
   307 	m_ReplyTo = reply;
   308 
   309 	return old;
   310 }
   311 
   312 unsigned int ChatTriggers::GetReplyTo()
   313 {
   314 	return m_ReplyTo;
   315 }
   316 
   317 bool ChatTriggers::IsChatTrigger()
   318 {
   319 	return m_bIsChatTrigger;
   320 }