core/ChatTriggers.cpp
author David Anderson <dvander@alliedmods.net>
Sun Sep 14 18:25:44 2008 -0500 (2008-09-14)
changeset 2294 9a9f3ed9745d
parent 2289 44045ead61cd
child 2400 76b7b4575e21
child 2603 fa286305bd3f
permissions -rw-r--r--
Triggering build.
     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 
    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 	unsigned int total = 2;
   122 	ConCommandBase *pCmd = icvar->GetCommands();
   123 	const char *name;
   124 	while (pCmd)
   125 	{
   126 		if (pCmd->IsCommand())
   127 		{
   128 			name = pCmd->GetName();
   129 			if (!m_pSayCmd && strcmp(name, "say") == 0)
   130 			{
   131 				m_pSayCmd = (ConCommand *)pCmd;
   132 				if (--total == 0)
   133 				{
   134 					break;
   135 				}
   136 			} else if (!m_pSayTeamCmd && strcmp(name, "say_team") == 0) {
   137 				m_pSayTeamCmd = (ConCommand *)pCmd;
   138 				if (--total == 0)
   139 				{
   140 					break;
   141 				}
   142 			}
   143 		}
   144 		pCmd = const_cast<ConCommandBase *>(pCmd->GetNext());
   145 	}
   146 
   147 	if (m_pSayCmd)
   148 	{
   149 		SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
   150 		SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Post, true);
   151 	}
   152 	if (m_pSayTeamCmd)
   153 	{
   154 		SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
   155 		SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Post, true);
   156 	}
   157 }
   158 
   159 void ChatTriggers::OnSourceModShutdown()
   160 {
   161 	if (m_pSayTeamCmd)
   162 	{
   163 		SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Post, true);
   164 		SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
   165 	}
   166 	if (m_pSayCmd)
   167 	{
   168 		SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Post, true);
   169 		SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
   170 	}
   171 
   172 	g_Forwards.ReleaseForward(m_pShouldFloodBlock);
   173 	g_Forwards.ReleaseForward(m_pDidFloodBlock);
   174 }
   175 
   176 #if defined ORANGEBOX_BUILD
   177 void ChatTriggers::OnSayCommand_Pre(const CCommand &command)
   178 {
   179 #else
   180 void ChatTriggers::OnSayCommand_Pre()
   181 {
   182 	CCommand command;
   183 #endif
   184 	int client;
   185 	CPlayer *pPlayer;
   186 	
   187 	client = g_ConCmds.GetCommandClient();
   188 	m_bIsChatTrigger = false;
   189 	m_bWasFloodedMessage = false;
   190 
   191 	/* The server console cannot do this */
   192 	if (client == 0 || (pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
   193 	{
   194 		RETURN_META(MRES_IGNORED);
   195 	}
   196 
   197 	/* We guarantee the client is connected */
   198 	if (!pPlayer->IsConnected())
   199 	{
   200 		RETURN_META(MRES_IGNORED);
   201 	}
   202 
   203 	const char *args = command.ArgS();
   204 	
   205 	if (!args)
   206 	{
   207 		RETURN_META(MRES_IGNORED);
   208 	}
   209 
   210 	/* Check if we need to block this message from being sent */
   211 	if (ClientIsFlooding(client))
   212 	{
   213 		char buffer[128];
   214 
   215 		if (!CoreTranslate(
   216 			buffer,
   217 			sizeof(buffer),
   218 			"%T",
   219 			2,
   220 			NULL,
   221 			"Flooding the server",
   222 			&client))
   223 		{
   224 			UTIL_Format(buffer, sizeof(buffer), "You are flooding the server!");
   225 		}
   226 
   227 		/* :TODO: we should probably kick people who spam too much. */
   228 
   229 		char fullbuffer[192];
   230 		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s", buffer);
   231 		g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
   232 
   233 		m_bWasFloodedMessage = true;
   234 
   235 		RETURN_META(MRES_SUPERCEDE);
   236 	}
   237 
   238 	/* Handle quoted string sets */
   239 	bool is_quoted = false;
   240 	if (args[0] == '"')
   241 	{
   242 		args++;
   243 		is_quoted = true;
   244 	}
   245 
   246 	bool is_trigger = false;
   247 	bool is_silent = false;
   248 
   249 	/* Check for either trigger */
   250 	if (m_PubTriggerSize && strncmp(args, m_PubTrigger, m_PubTriggerSize) == 0)
   251 	{
   252 		is_trigger = true;
   253 		args = &args[m_PubTriggerSize];
   254 	} 
   255 	else if (m_PrivTriggerSize && strncmp(args, m_PrivTrigger, m_PrivTriggerSize) == 0) 
   256 	{
   257 		is_trigger = true;
   258 		is_silent = true;
   259 		args = &args[m_PrivTriggerSize];
   260 	}
   261 
   262 	if (!is_trigger)
   263 	{
   264 		RETURN_META(MRES_IGNORED);
   265 	}
   266 
   267 	/**
   268 	 * Test if this is actually a command!
   269 	 */
   270 	if (!PreProcessTrigger(engine->PEntityOfEntIndex(client), args, is_quoted))
   271 	{
   272 		CPlayer *pPlayer;
   273 		if (is_silent 
   274 			&& g_bSupressSilentFails 
   275 			&& client != 0
   276 			&& (pPlayer = g_Players.GetPlayerByIndex(client)) != NULL
   277 			&& pPlayer->GetAdminId() != INVALID_ADMIN_ID)
   278 		{
   279 			RETURN_META(MRES_SUPERCEDE);
   280 		}
   281 		RETURN_META(MRES_IGNORED);
   282 	}
   283 
   284 	m_bIsChatTrigger = true;
   285 
   286 	/**
   287 	 * We'll execute it in post.
   288 	 */
   289 	m_bWillProcessInPost = true;
   290 	m_bTriggerWasSilent = is_silent;
   291 
   292 	/* If we're silent, block */
   293 	if (is_silent)
   294 	{
   295 		RETURN_META(MRES_SUPERCEDE);
   296 	}
   297 
   298 	/* Otherwise, let the command continue */
   299 	RETURN_META(MRES_IGNORED);
   300 }
   301 
   302 #if defined ORANGEBOX_BUILD
   303 void ChatTriggers::OnSayCommand_Post(const CCommand &command)
   304 #else
   305 void ChatTriggers::OnSayCommand_Post()
   306 #endif
   307 {
   308 	m_bIsChatTrigger = false;
   309 	m_bWasFloodedMessage = false;
   310 	if (m_bWillProcessInPost)
   311 	{
   312 		/* Reset this for re-entrancy */
   313 		m_bWillProcessInPost = false;
   314 		
   315 		/* Execute the cached command */
   316 		int client = g_ConCmds.GetCommandClient();
   317 		unsigned int old = SetReplyTo(SM_REPLY_CHAT);
   318 		serverpluginhelpers->ClientCommand(engine->PEntityOfEntIndex(client), m_ToExecute);
   319 		SetReplyTo(old);
   320 	}
   321 }
   322 
   323 bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args, bool is_quoted)
   324 {
   325 	/* Extract a command.  This is kind of sloppy. */
   326 	char cmd_buf[64];
   327 	size_t cmd_len = 0;
   328 	const char *inptr = args;
   329 	while (*inptr != '\0' 
   330 			&& !textparsers->IsWhitespace(inptr) 
   331 			&& *inptr != '"'
   332 			&& cmd_len < sizeof(cmd_buf) - 1)
   333 	{
   334 		cmd_buf[cmd_len++] = *inptr++;
   335 	}
   336 	cmd_buf[cmd_len] = '\0';
   337 
   338 	if (cmd_len == 0)
   339 	{
   340 		return false;
   341 	}
   342 
   343 	/* See if we have this registered */
   344 	bool prepended = false;
   345 	if (!g_ConCmds.LookForSourceModCommand(cmd_buf))
   346 	{
   347 		/* Check if we had an "sm_" prefix */
   348 		if (strncmp(cmd_buf, "sm_", 3) == 0)
   349 		{
   350 			return false;
   351 		}
   352 
   353 		/* Now, prepend.  Don't worry about the buffers.  This will 
   354 		 * work because the sizes are limited from earlier.
   355 		 */
   356 		char new_buf[80];
   357 		strcpy(new_buf, "sm_");
   358 		strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3);
   359 
   360 		/* Recheck */
   361 		if (!g_ConCmds.LookForSourceModCommand(new_buf))
   362 		{
   363 			return false;
   364 		}
   365 		
   366 		prepended = true;
   367 	}
   368 
   369 	/* See if we need to do extra string manipulation */
   370 	if (is_quoted || prepended)
   371 	{
   372 		size_t len;
   373 
   374 		/* Check if we need to prepend sm_ */
   375 		if (prepended)
   376 		{
   377 			len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
   378 		} else {
   379 			len = strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   380 		}
   381 		
   382 		/* Check if we need to strip a quote */
   383 		if (is_quoted)
   384 		{
   385 			if (m_ToExecute[len-1] == '"')
   386 			{
   387 				m_ToExecute[--len] = '\0';
   388 			}
   389 		}
   390 	} else {
   391 		strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   392 	}
   393 
   394 	return true;
   395 }
   396 
   397 unsigned int ChatTriggers::SetReplyTo(unsigned int reply)
   398 {
   399 	unsigned int old = m_ReplyTo;
   400 
   401 	m_ReplyTo = reply;
   402 
   403 	return old;
   404 }
   405 
   406 unsigned int ChatTriggers::GetReplyTo()
   407 {
   408 	return m_ReplyTo;
   409 }
   410 
   411 bool ChatTriggers::IsChatTrigger()
   412 {
   413 	return m_bIsChatTrigger;
   414 }
   415 
   416 bool ChatTriggers::ClientIsFlooding(int client)
   417 {
   418 	bool is_flooding = false;
   419 
   420 	if (m_pShouldFloodBlock->GetFunctionCount() != 0)
   421 	{
   422 		cell_t res = 0;
   423 
   424 		m_pShouldFloodBlock->PushCell(client);
   425 		m_pShouldFloodBlock->Execute(&res);
   426 
   427 		if (res != 0)
   428 		{
   429 			is_flooding = true;
   430 		}
   431 	}
   432 
   433 	if (m_pDidFloodBlock->GetFunctionCount() != 0)
   434 	{
   435 		m_pDidFloodBlock->PushCell(client);
   436 		m_pDidFloodBlock->PushCell(is_flooding ? 1 : 0);
   437 		m_pDidFloodBlock->Execute(NULL);
   438 	}
   439 
   440 	return is_flooding;
   441 }
   442 
   443 bool ChatTriggers::WasFloodedMessage()
   444 {
   445 	return m_bWasFloodedMessage;
   446 }