core/ChatTriggers.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Mon Feb 08 19:57:20 2010 -0800 (2010-02-08)
changeset 2914 bf5f9aa53abe
parent 2617 253d6b4f15ab
child 3004 e3ac0dc6d9a4
permissions -rw-r--r--
Fixed FindEntityByClassname crash in Synergy (bug 4235, r=dvander).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 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 #include "Translator.h"
    38 #include "HalfLife2.h"
    39 
    40 /* :HACKHACK: We can't SH_DECL here because ConCmdManager.cpp does.
    41  * While the OB build only runs on MM:S 1.6.0+ (SH 5+), the older one 
    42  * can technically be compiled against any MM:S version after 1.4.2.
    43  */
    44 #if SOURCE_ENGINE >= SE_ORANGEBOX
    45 extern bool __SourceHook_FHRemoveConCommandDispatch(void *, bool, class fastdelegate::FastDelegate1<const CCommand &, void>);
    46 extern int __SourceHook_FHAddConCommandDispatch(void *, ISourceHook::AddHookMode, bool, class fastdelegate::FastDelegate1<const CCommand &, void>);
    47 #else
    48 extern bool __SourceHook_FHRemoveConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
    49 #if SH_IMPL_VERSION >= 5
    50 extern int __SourceHook_FHAddConCommandDispatch(void *, ISourceHook::AddHookMode, bool, class fastdelegate::FastDelegate0<void>);
    51 #elif SH_IMPL_VERSION == 4
    52 extern int __SourceHook_FHAddConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
    53 #elif SH_IMPL_VERSION == 3
    54 extern bool __SourceHook_FHAddConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
    55 #endif //SH_IMPL_VERSION
    56 #endif //SE_ORANGEBOX
    57 
    58 ChatTriggers g_ChatTriggers;
    59 bool g_bSupressSilentFails = false;
    60 
    61 ChatTriggers::ChatTriggers() : m_pSayCmd(NULL), m_bWillProcessInPost(false), 
    62 	m_bTriggerWasSilent(false), m_ReplyTo(SM_REPLY_CONSOLE)
    63 {
    64 	m_PubTrigger = sm_strdup("!");
    65 	m_PrivTrigger = sm_strdup("/");
    66 	m_PubTriggerSize = 1;
    67 	m_PrivTriggerSize = 1;
    68 	m_bIsChatTrigger = false;
    69 }
    70 
    71 ChatTriggers::~ChatTriggers()
    72 {
    73 	delete [] m_PubTrigger;
    74 	m_PubTrigger = NULL;
    75 	delete [] m_PrivTrigger;
    76 	m_PrivTrigger = NULL;
    77 }
    78 
    79 ConfigResult ChatTriggers::OnSourceModConfigChanged(const char *key, 
    80 													const char *value, 
    81 													ConfigSource source,
    82 													char *error, 
    83 													size_t maxlength)
    84 {
    85 	if (strcmp(key, "PublicChatTrigger") == 0)
    86 	{
    87 		delete [] m_PubTrigger;
    88 		m_PubTrigger = sm_strdup(value);
    89 		m_PubTriggerSize = strlen(m_PubTrigger);
    90 		return ConfigResult_Accept;
    91 	}
    92 	else if (strcmp(key, "SilentChatTrigger") == 0)
    93 	{
    94 		delete [] m_PrivTrigger;
    95 		m_PrivTrigger = sm_strdup(value);
    96 		m_PrivTriggerSize = strlen(m_PrivTrigger);
    97 		return ConfigResult_Accept;
    98 	}
    99 	else if (strcmp(key, "SilentFailSuppress") == 0)
   100 	{
   101 		g_bSupressSilentFails = strcmp(value, "yes") == 0;
   102 		return ConfigResult_Accept;
   103 	}
   104 
   105 	return ConfigResult_Ignore;
   106 }
   107 
   108 void ChatTriggers::OnSourceModAllInitialized()
   109 {
   110 	m_pShouldFloodBlock = g_Forwards.CreateForward("OnClientFloodCheck", ET_Event, 1, NULL, Param_Cell);
   111 	m_pDidFloodBlock = g_Forwards.CreateForward("OnClientFloodResult", ET_Event, 2, NULL, Param_Cell, Param_Cell);
   112 }
   113 
   114 void ChatTriggers::OnSourceModAllInitialized_Post()
   115 {
   116 	g_pCorePhrases->AddPhraseFile("antiflood.phrases");
   117 }
   118 
   119 void ChatTriggers::OnSourceModGameInitialized()
   120 {
   121 	m_pSayCmd = FindCommand("say");
   122 	m_pSayTeamCmd = FindCommand("say_team");
   123 
   124 	if (m_pSayCmd)
   125 	{
   126 		SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
   127 		SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Post, true);
   128 	}
   129 	if (m_pSayTeamCmd)
   130 	{
   131 		SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
   132 		SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Post, true);
   133 	}
   134 }
   135 
   136 void ChatTriggers::OnSourceModShutdown()
   137 {
   138 	if (m_pSayTeamCmd)
   139 	{
   140 		SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Post, true);
   141 		SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
   142 	}
   143 	if (m_pSayCmd)
   144 	{
   145 		SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Post, true);
   146 		SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
   147 	}
   148 
   149 	g_Forwards.ReleaseForward(m_pShouldFloodBlock);
   150 	g_Forwards.ReleaseForward(m_pDidFloodBlock);
   151 }
   152 
   153 #if SOURCE_ENGINE >= SE_ORANGEBOX
   154 void ChatTriggers::OnSayCommand_Pre(const CCommand &command)
   155 {
   156 #else
   157 void ChatTriggers::OnSayCommand_Pre()
   158 {
   159 	CCommand command;
   160 #endif
   161 	int client;
   162 	CPlayer *pPlayer;
   163 	
   164 	client = g_ConCmds.GetCommandClient();
   165 	m_bIsChatTrigger = false;
   166 	m_bWasFloodedMessage = false;
   167 
   168 	/* The server console cannot do this */
   169 	if (client == 0 || (pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
   170 	{
   171 		RETURN_META(MRES_IGNORED);
   172 	}
   173 
   174 	/* We guarantee the client is connected */
   175 	if (!pPlayer->IsConnected())
   176 	{
   177 		RETURN_META(MRES_IGNORED);
   178 	}
   179 
   180 	const char *args = command.ArgS();
   181 	
   182 	if (!args)
   183 	{
   184 		RETURN_META(MRES_IGNORED);
   185 	}
   186 
   187 	/* Check if we need to block this message from being sent */
   188 	if (ClientIsFlooding(client))
   189 	{
   190 		char buffer[128];
   191 
   192 		if (!CoreTranslate(
   193 			buffer,
   194 			sizeof(buffer),
   195 			"%T",
   196 			2,
   197 			NULL,
   198 			"Flooding the server",
   199 			&client))
   200 		{
   201 			UTIL_Format(buffer, sizeof(buffer), "You are flooding the server!");
   202 		}
   203 
   204 		/* :TODO: we should probably kick people who spam too much. */
   205 
   206 		char fullbuffer[192];
   207 		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s", buffer);
   208 		g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
   209 
   210 		m_bWasFloodedMessage = true;
   211 
   212 		RETURN_META(MRES_SUPERCEDE);
   213 	}
   214 
   215 	/* Handle quoted string sets */
   216 	bool is_quoted = false;
   217 	if (args[0] == '"')
   218 	{
   219 		args++;
   220 		is_quoted = true;
   221 	}
   222 
   223 	bool is_trigger = false;
   224 	bool is_silent = false;
   225 
   226 	/* Check for either trigger */
   227 	if (m_PubTriggerSize && strncmp(args, m_PubTrigger, m_PubTriggerSize) == 0)
   228 	{
   229 		is_trigger = true;
   230 		args = &args[m_PubTriggerSize];
   231 	} 
   232 	else if (m_PrivTriggerSize && strncmp(args, m_PrivTrigger, m_PrivTriggerSize) == 0) 
   233 	{
   234 		is_trigger = true;
   235 		is_silent = true;
   236 		args = &args[m_PrivTriggerSize];
   237 	}
   238 
   239 	if (!is_trigger)
   240 	{
   241 		RETURN_META(MRES_IGNORED);
   242 	}
   243 
   244 	/**
   245 	 * Test if this is actually a command!
   246 	 */
   247 	if (!PreProcessTrigger(PEntityOfEntIndex(client), args, is_quoted))
   248 	{
   249 		CPlayer *pPlayer;
   250 		if (is_silent 
   251 			&& g_bSupressSilentFails 
   252 			&& client != 0
   253 			&& (pPlayer = g_Players.GetPlayerByIndex(client)) != NULL
   254 			&& pPlayer->GetAdminId() != INVALID_ADMIN_ID)
   255 		{
   256 			RETURN_META(MRES_SUPERCEDE);
   257 		}
   258 		RETURN_META(MRES_IGNORED);
   259 	}
   260 
   261 	m_bIsChatTrigger = true;
   262 
   263 	/**
   264 	 * We'll execute it in post.
   265 	 */
   266 	m_bWillProcessInPost = true;
   267 	m_bTriggerWasSilent = is_silent;
   268 
   269 	/* If we're silent, block */
   270 	if (is_silent)
   271 	{
   272 		RETURN_META(MRES_SUPERCEDE);
   273 	}
   274 
   275 	/* Otherwise, let the command continue */
   276 	RETURN_META(MRES_IGNORED);
   277 }
   278 
   279 #if SOURCE_ENGINE >= SE_ORANGEBOX
   280 void ChatTriggers::OnSayCommand_Post(const CCommand &command)
   281 #else
   282 void ChatTriggers::OnSayCommand_Post()
   283 #endif
   284 {
   285 	m_bIsChatTrigger = false;
   286 	m_bWasFloodedMessage = false;
   287 	if (m_bWillProcessInPost)
   288 	{
   289 		/* Reset this for re-entrancy */
   290 		m_bWillProcessInPost = false;
   291 		
   292 		/* Execute the cached command */
   293 		int client = g_ConCmds.GetCommandClient();
   294 		unsigned int old = SetReplyTo(SM_REPLY_CHAT);
   295 		serverpluginhelpers->ClientCommand(PEntityOfEntIndex(client), m_ToExecute);
   296 		SetReplyTo(old);
   297 	}
   298 }
   299 
   300 bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args, bool is_quoted)
   301 {
   302 	/* Extract a command.  This is kind of sloppy. */
   303 	char cmd_buf[64];
   304 	size_t cmd_len = 0;
   305 	const char *inptr = args;
   306 	while (*inptr != '\0' 
   307 			&& !textparsers->IsWhitespace(inptr) 
   308 			&& *inptr != '"'
   309 			&& cmd_len < sizeof(cmd_buf) - 1)
   310 	{
   311 		cmd_buf[cmd_len++] = *inptr++;
   312 	}
   313 	cmd_buf[cmd_len] = '\0';
   314 
   315 	if (cmd_len == 0)
   316 	{
   317 		return false;
   318 	}
   319 
   320 	/* See if we have this registered */
   321 	bool prepended = false;
   322 	if (!g_ConCmds.LookForSourceModCommand(cmd_buf))
   323 	{
   324 		/* Check if we had an "sm_" prefix */
   325 		if (strncmp(cmd_buf, "sm_", 3) == 0)
   326 		{
   327 			return false;
   328 		}
   329 
   330 		/* Now, prepend.  Don't worry about the buffers.  This will 
   331 		 * work because the sizes are limited from earlier.
   332 		 */
   333 		char new_buf[80];
   334 		strcpy(new_buf, "sm_");
   335 		strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3);
   336 
   337 		/* Recheck */
   338 		if (!g_ConCmds.LookForSourceModCommand(new_buf))
   339 		{
   340 			return false;
   341 		}
   342 		
   343 		prepended = true;
   344 	}
   345 
   346 	/* See if we need to do extra string manipulation */
   347 	if (is_quoted || prepended)
   348 	{
   349 		size_t len;
   350 
   351 		/* Check if we need to prepend sm_ */
   352 		if (prepended)
   353 		{
   354 			len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
   355 		} else {
   356 			len = strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   357 		}
   358 		
   359 		/* Check if we need to strip a quote */
   360 		if (is_quoted)
   361 		{
   362 			if (m_ToExecute[len-1] == '"')
   363 			{
   364 				m_ToExecute[--len] = '\0';
   365 			}
   366 		}
   367 	} else {
   368 		strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   369 	}
   370 
   371 	return true;
   372 }
   373 
   374 unsigned int ChatTriggers::SetReplyTo(unsigned int reply)
   375 {
   376 	unsigned int old = m_ReplyTo;
   377 
   378 	m_ReplyTo = reply;
   379 
   380 	return old;
   381 }
   382 
   383 unsigned int ChatTriggers::GetReplyTo()
   384 {
   385 	return m_ReplyTo;
   386 }
   387 
   388 bool ChatTriggers::IsChatTrigger()
   389 {
   390 	return m_bIsChatTrigger;
   391 }
   392 
   393 bool ChatTriggers::ClientIsFlooding(int client)
   394 {
   395 	bool is_flooding = false;
   396 
   397 	if (m_pShouldFloodBlock->GetFunctionCount() != 0)
   398 	{
   399 		cell_t res = 0;
   400 
   401 		m_pShouldFloodBlock->PushCell(client);
   402 		m_pShouldFloodBlock->Execute(&res);
   403 
   404 		if (res != 0)
   405 		{
   406 			is_flooding = true;
   407 		}
   408 	}
   409 
   410 	if (m_pDidFloodBlock->GetFunctionCount() != 0)
   411 	{
   412 		m_pDidFloodBlock->PushCell(client);
   413 		m_pDidFloodBlock->PushCell(is_flooding ? 1 : 0);
   414 		m_pDidFloodBlock->Execute(NULL);
   415 	}
   416 
   417 	return is_flooding;
   418 }
   419 
   420 bool ChatTriggers::WasFloodedMessage()
   421 {
   422 	return m_bWasFloodedMessage;
   423 }