core/ChatTriggers.cpp
author Matt Woodrow <pred@alliedmods.net>
Fri Aug 08 00:17:37 2008 +0000 (2008-08-08)
changeset 2224 676c3b826f13
parent 2042 4a2faafca477
child 2276 50d05c18e62a
permissions -rw-r--r--
Initial Import of beta structs extension.
     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 defined ORANGEBOX_BUILD
    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 //ORANGEBOX_BUILD
    57 
    58 ChatTriggers g_ChatTriggers;
    59 bool g_bSupressSilentFails = false;
    60 CPhraseFile *g_pFloodPhrases = NULL;
    61 
    62 ChatTriggers::ChatTriggers() : m_pSayCmd(NULL), m_bWillProcessInPost(false), 
    63 	m_bTriggerWasSilent(false), m_ReplyTo(SM_REPLY_CONSOLE)
    64 {
    65 	m_PubTrigger = sm_strdup("!");
    66 	m_PrivTrigger = sm_strdup("/");
    67 	m_PubTriggerSize = 1;
    68 	m_PrivTriggerSize = 1;
    69 	m_bIsChatTrigger = false;
    70 }
    71 
    72 ChatTriggers::~ChatTriggers()
    73 {
    74 	delete [] m_PubTrigger;
    75 	m_PubTrigger = NULL;
    76 	delete [] m_PrivTrigger;
    77 	m_PrivTrigger = NULL;
    78 }
    79 
    80 ConfigResult ChatTriggers::OnSourceModConfigChanged(const char *key, 
    81 													const char *value, 
    82 													ConfigSource source,
    83 													char *error, 
    84 													size_t maxlength)
    85 {
    86 	if (strcmp(key, "PublicChatTrigger") == 0)
    87 	{
    88 		delete [] m_PubTrigger;
    89 		m_PubTrigger = sm_strdup(value);
    90 		m_PubTriggerSize = strlen(m_PubTrigger);
    91 		return ConfigResult_Accept;
    92 	}
    93 	else if (strcmp(key, "SilentChatTrigger") == 0)
    94 	{
    95 		delete [] m_PrivTrigger;
    96 		m_PrivTrigger = sm_strdup(value);
    97 		m_PrivTriggerSize = strlen(m_PrivTrigger);
    98 		return ConfigResult_Accept;
    99 	}
   100 	else if (strcmp(key, "SilentFailSuppress") == 0)
   101 	{
   102 		g_bSupressSilentFails = strcmp(value, "yes") == 0;
   103 		return ConfigResult_Accept;
   104 	}
   105 
   106 	return ConfigResult_Ignore;
   107 }
   108 
   109 void ChatTriggers::OnSourceModAllInitialized()
   110 {
   111 	m_pShouldFloodBlock = g_Forwards.CreateForward("OnClientFloodCheck", ET_Event, 1, NULL, Param_Cell);
   112 	m_pDidFloodBlock = g_Forwards.CreateForward("OnClientFloodResult", ET_Event, 2, NULL, Param_Cell, Param_Cell);
   113 }
   114 
   115 void ChatTriggers::OnSourceModAllInitialized_Post()
   116 {
   117 	unsigned int file_id;
   118 	
   119 	file_id = g_Translator.FindOrAddPhraseFile("antiflood.phrases.txt");
   120 	g_pFloodPhrases = g_Translator.GetFileByIndex(file_id);
   121 }
   122 
   123 void ChatTriggers::OnSourceModGameInitialized()
   124 {
   125 	unsigned int total = 2;
   126 	ConCommandBase *pCmd = icvar->GetCommands();
   127 	const char *name;
   128 	while (pCmd)
   129 	{
   130 		if (pCmd->IsCommand())
   131 		{
   132 			name = pCmd->GetName();
   133 			if (!m_pSayCmd && strcmp(name, "say") == 0)
   134 			{
   135 				m_pSayCmd = (ConCommand *)pCmd;
   136 				if (--total == 0)
   137 				{
   138 					break;
   139 				}
   140 			} else if (!m_pSayTeamCmd && strcmp(name, "say_team") == 0) {
   141 				m_pSayTeamCmd = (ConCommand *)pCmd;
   142 				if (--total == 0)
   143 				{
   144 					break;
   145 				}
   146 			}
   147 		}
   148 		pCmd = const_cast<ConCommandBase *>(pCmd->GetNext());
   149 	}
   150 
   151 	if (m_pSayCmd)
   152 	{
   153 		SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
   154 		SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Post, true);
   155 	}
   156 	if (m_pSayTeamCmd)
   157 	{
   158 		SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
   159 		SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Post, true);
   160 	}
   161 }
   162 
   163 void ChatTriggers::OnSourceModShutdown()
   164 {
   165 	if (m_pSayTeamCmd)
   166 	{
   167 		SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Post, true);
   168 		SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
   169 	}
   170 	if (m_pSayCmd)
   171 	{
   172 		SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Post, true);
   173 		SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
   174 	}
   175 
   176 	g_Forwards.ReleaseForward(m_pShouldFloodBlock);
   177 	g_Forwards.ReleaseForward(m_pDidFloodBlock);
   178 }
   179 
   180 #if defined ORANGEBOX_BUILD
   181 void ChatTriggers::OnSayCommand_Pre(const CCommand &command)
   182 {
   183 #else
   184 void ChatTriggers::OnSayCommand_Pre()
   185 {
   186 	CCommand command;
   187 #endif
   188 	int client;
   189 	CPlayer *pPlayer;
   190 	
   191 	client = g_ConCmds.GetCommandClient();
   192 	m_bIsChatTrigger = false;
   193 	m_bWasFloodedMessage = false;
   194 
   195 	/* The server console cannot do this */
   196 	if (client == 0 || (pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
   197 	{
   198 		RETURN_META(MRES_IGNORED);
   199 	}
   200 
   201 	/* We guarantee the client is connected */
   202 	if (!pPlayer->IsConnected())
   203 	{
   204 		RETURN_META(MRES_IGNORED);
   205 	}
   206 
   207 	const char *args = command.ArgS();
   208 	
   209 	if (!args)
   210 	{
   211 		RETURN_META(MRES_IGNORED);
   212 	}
   213 
   214 	/* Check if we need to block this message from being sent */
   215 	if (ClientIsFlooding(client))
   216 	{
   217 		char buffer[128];
   218 
   219 		if (!CoreTranslate(
   220 			buffer,
   221 			sizeof(buffer),
   222 			"%T",
   223 			2,
   224 			NULL,
   225 			"Flooding the server",
   226 			&client))
   227 		{
   228 			UTIL_Format(buffer, sizeof(buffer), "You are flooding the server!");
   229 		}
   230 
   231 		/* :TODO: we should probably kick people who spam too much. */
   232 
   233 		char fullbuffer[192];
   234 		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s", buffer);
   235 		g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
   236 
   237 		m_bWasFloodedMessage = true;
   238 
   239 		RETURN_META(MRES_SUPERCEDE);
   240 	}
   241 
   242 	/* Handle quoted string sets */
   243 	bool is_quoted = false;
   244 	if (args[0] == '"')
   245 	{
   246 		args++;
   247 		is_quoted = true;
   248 	}
   249 
   250 	bool is_trigger = false;
   251 	bool is_silent = false;
   252 
   253 	/* Check for either trigger */
   254 	if (m_PubTriggerSize && strncmp(args, m_PubTrigger, m_PubTriggerSize) == 0)
   255 	{
   256 		is_trigger = true;
   257 		args = &args[m_PubTriggerSize];
   258 	} 
   259 	else if (m_PrivTriggerSize && strncmp(args, m_PrivTrigger, m_PrivTriggerSize) == 0) 
   260 	{
   261 		is_trigger = true;
   262 		is_silent = true;
   263 		args = &args[m_PrivTriggerSize];
   264 	}
   265 
   266 	if (!is_trigger)
   267 	{
   268 		RETURN_META(MRES_IGNORED);
   269 	}
   270 
   271 	/**
   272 	 * Test if this is actually a command!
   273 	 */
   274 	if (!PreProcessTrigger(engine->PEntityOfEntIndex(client), args, is_quoted))
   275 	{
   276 		CPlayer *pPlayer;
   277 		if (is_silent 
   278 			&& g_bSupressSilentFails 
   279 			&& client != 0
   280 			&& (pPlayer = g_Players.GetPlayerByIndex(client)) != NULL
   281 			&& pPlayer->GetAdminId() != INVALID_ADMIN_ID)
   282 		{
   283 			RETURN_META(MRES_SUPERCEDE);
   284 		}
   285 		RETURN_META(MRES_IGNORED);
   286 	}
   287 
   288 	m_bIsChatTrigger = true;
   289 
   290 	/**
   291 	 * We'll execute it in post.
   292 	 */
   293 	m_bWillProcessInPost = true;
   294 	m_bTriggerWasSilent = is_silent;
   295 
   296 	/* If we're silent, block */
   297 	if (is_silent)
   298 	{
   299 		RETURN_META(MRES_SUPERCEDE);
   300 	}
   301 
   302 	/* Otherwise, let the command continue */
   303 	RETURN_META(MRES_IGNORED);
   304 }
   305 
   306 #if defined ORANGEBOX_BUILD
   307 void ChatTriggers::OnSayCommand_Post(const CCommand &command)
   308 #else
   309 void ChatTriggers::OnSayCommand_Post()
   310 #endif
   311 {
   312 	m_bIsChatTrigger = false;
   313 	m_bWasFloodedMessage = false;
   314 	if (m_bWillProcessInPost)
   315 	{
   316 		/* Reset this for re-entrancy */
   317 		m_bWillProcessInPost = false;
   318 		
   319 		/* Execute the cached command */
   320 		int client = g_ConCmds.GetCommandClient();
   321 		unsigned int old = SetReplyTo(SM_REPLY_CHAT);
   322 		serverpluginhelpers->ClientCommand(engine->PEntityOfEntIndex(client), m_ToExecute);
   323 		SetReplyTo(old);
   324 	}
   325 }
   326 
   327 bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args, bool is_quoted)
   328 {
   329 	/* Extract a command.  This is kind of sloppy. */
   330 	char cmd_buf[64];
   331 	size_t cmd_len = 0;
   332 	const char *inptr = args;
   333 	while (*inptr != '\0' 
   334 			&& !textparsers->IsWhitespace(inptr) 
   335 			&& *inptr != '"'
   336 			&& cmd_len < sizeof(cmd_buf) - 1)
   337 	{
   338 		cmd_buf[cmd_len++] = *inptr++;
   339 	}
   340 	cmd_buf[cmd_len] = '\0';
   341 
   342 	if (cmd_len == 0)
   343 	{
   344 		return false;
   345 	}
   346 
   347 	/* See if we have this registered */
   348 	bool prepended = false;
   349 	if (!g_ConCmds.LookForSourceModCommand(cmd_buf))
   350 	{
   351 		/* Check if we had an "sm_" prefix */
   352 		if (strncmp(cmd_buf, "sm_", 3) == 0)
   353 		{
   354 			return false;
   355 		}
   356 
   357 		/* Now, prepend.  Don't worry about the buffers.  This will 
   358 		 * work because the sizes are limited from earlier.
   359 		 */
   360 		char new_buf[80];
   361 		strcpy(new_buf, "sm_");
   362 		strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3);
   363 
   364 		/* Recheck */
   365 		if (!g_ConCmds.LookForSourceModCommand(new_buf))
   366 		{
   367 			return false;
   368 		}
   369 		
   370 		prepended = true;
   371 	}
   372 
   373 	/* See if we need to do extra string manipulation */
   374 	if (is_quoted || prepended)
   375 	{
   376 		size_t len;
   377 
   378 		/* Check if we need to prepend sm_ */
   379 		if (prepended)
   380 		{
   381 			len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
   382 		} else {
   383 			len = strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   384 		}
   385 		
   386 		/* Check if we need to strip a quote */
   387 		if (is_quoted)
   388 		{
   389 			if (m_ToExecute[len-1] == '"')
   390 			{
   391 				m_ToExecute[--len] = '\0';
   392 			}
   393 		}
   394 	} else {
   395 		strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   396 	}
   397 
   398 	return true;
   399 }
   400 
   401 unsigned int ChatTriggers::SetReplyTo(unsigned int reply)
   402 {
   403 	unsigned int old = m_ReplyTo;
   404 
   405 	m_ReplyTo = reply;
   406 
   407 	return old;
   408 }
   409 
   410 unsigned int ChatTriggers::GetReplyTo()
   411 {
   412 	return m_ReplyTo;
   413 }
   414 
   415 bool ChatTriggers::IsChatTrigger()
   416 {
   417 	return m_bIsChatTrigger;
   418 }
   419 
   420 bool ChatTriggers::ClientIsFlooding(int client)
   421 {
   422 	bool is_flooding = false;
   423 
   424 	if (m_pShouldFloodBlock->GetFunctionCount() != 0)
   425 	{
   426 		cell_t res = 0;
   427 
   428 		m_pShouldFloodBlock->PushCell(client);
   429 		m_pShouldFloodBlock->Execute(&res);
   430 
   431 		if (res != 0)
   432 		{
   433 			is_flooding = true;
   434 		}
   435 	}
   436 
   437 	if (m_pDidFloodBlock->GetFunctionCount() != 0)
   438 	{
   439 		m_pDidFloodBlock->PushCell(client);
   440 		m_pDidFloodBlock->PushCell(is_flooding ? 1 : 0);
   441 		m_pDidFloodBlock->Execute(NULL);
   442 	}
   443 
   444 	return is_flooding;
   445 }
   446 
   447 bool ChatTriggers::WasFloodedMessage()
   448 {
   449 	return m_bWasFloodedMessage;
   450 }