core/ChatTriggers.cpp
author David Anderson <danderson@mozilla.com>
Thu Aug 08 09:40:23 2013 -0700 (2013-08-08)
changeset 3976 bc77cfde7c5c
parent 3975 835659a15ce3
child 4015 5f1fcac17b60
permissions -rw-r--r--
Add -Wno-narrowing to support building on GCC-4.7 (no bug, r=me).
     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 "HalfLife2.h"
    38 #include "logic_bridge.h"
    39 
    40 #if SOURCE_ENGINE == SE_DOTA
    41 SH_DECL_EXTERN2_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommandContext &, const CCommand &);
    42 #elif SOURCE_ENGINE >= SE_ORANGEBOX
    43 SH_DECL_EXTERN1_void(ConCommand, Dispatch, SH_NOATTRIB, false, const CCommand &);
    44 #elif SOURCE_ENGINE == SE_DARKMESSIAH
    45 SH_DECL_EXTERN0_void(ConCommand, Dispatch, SH_NOATTRIB, false);
    46 #else
    47 # if SH_IMPL_VERSION >= 4
    48  extern int __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
    49 # else
    50  extern bool __SourceHook_FHAddConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
    51 # endif
    52 extern bool __SourceHook_FHRemoveConCommandDispatch(void *,bool,class fastdelegate::FastDelegate0<void>);
    53 #endif
    54 
    55 ChatTriggers g_ChatTriggers;
    56 bool g_bSupressSilentFails = false;
    57 
    58 ChatTriggers::ChatTriggers() : m_pSayCmd(NULL), m_bWillProcessInPost(false), 
    59 	m_bTriggerWasSilent(false), m_ReplyTo(SM_REPLY_CONSOLE)
    60 {
    61 	m_PubTrigger = sm_strdup("!");
    62 	m_PrivTrigger = sm_strdup("/");
    63 	m_PubTriggerSize = 1;
    64 	m_PrivTriggerSize = 1;
    65 	m_bIsChatTrigger = false;
    66 	m_bPluginIgnored = false;
    67 #if SOURCE_ENGINE == SE_EPISODEONE
    68 	m_bIsINS = false;
    69 #endif
    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 	m_pOnClientSayCmd = g_Forwards.CreateForward("OnClientSayCommand", ET_Event, 3, NULL, Param_Cell, Param_String, Param_String);
   114 	m_pOnClientSayCmd_Post = g_Forwards.CreateForward("OnClientSayCommand_Post", ET_Ignore, 3, NULL, Param_Cell, Param_String, Param_String);
   115 }
   116 
   117 void ChatTriggers::OnSourceModAllInitialized_Post()
   118 {
   119 	logicore.AddCorePhraseFile("antiflood.phrases");
   120 }
   121 
   122 void ChatTriggers::OnSourceModGameInitialized()
   123 {
   124 	m_pSayCmd = FindCommand("say");
   125 	m_pSayTeamCmd = FindCommand("say_team");
   126 
   127 	if (m_pSayCmd)
   128 	{
   129 		SH_ADD_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
   130 		SH_ADD_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
   131 	}
   132 	if (m_pSayTeamCmd)
   133 	{
   134 		SH_ADD_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
   135 		SH_ADD_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
   136 	}
   137 
   138 #if SOURCE_ENGINE == SE_EPISODEONE
   139 	m_bIsINS = (strcmp(g_SourceMod.GetGameFolderName(), "insurgency") == 0);
   140 
   141 	if (m_bIsINS)
   142 	{
   143 		m_pSay2Cmd = FindCommand("say2");
   144 		if (m_pSay2Cmd)
   145 		{
   146 			SH_ADD_HOOK(ConCommand, Dispatch, m_pSay2Cmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
   147 			SH_ADD_HOOK(ConCommand, Dispatch, m_pSay2Cmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
   148 		}
   149 	}
   150 #elif SOURCE_ENGINE == SE_NUCLEARDAWN
   151 	m_pSaySquadCmd = FindCommand("say_squad");
   152 
   153 	if (m_pSaySquadCmd)
   154 	{
   155 		SH_ADD_HOOK(ConCommand, Dispatch, m_pSaySquadCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
   156 		SH_ADD_HOOK(ConCommand, Dispatch, m_pSaySquadCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
   157 	}
   158 #endif
   159 }
   160 
   161 void ChatTriggers::OnSourceModShutdown()
   162 {
   163 	if (m_pSayCmd)
   164 	{
   165 		SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
   166 		SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
   167 	}
   168 
   169 	if (m_pSayTeamCmd)
   170 	{
   171 		SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
   172 		SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
   173 	}
   174 
   175 #if SOURCE_ENGINE == SE_EPISODEONE
   176 	if (m_bIsINS && m_pSay2Cmd)
   177 	{
   178 		SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSay2Cmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
   179 		SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSay2Cmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
   180 	}
   181 #elif SOURCE_ENGINE == SE_NUCLEARDAWN
   182 	if (m_pSaySquadCmd)
   183 	{
   184 		SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSaySquadCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
   185 		SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSaySquadCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
   186 	}
   187 #endif
   188 
   189 	g_Forwards.ReleaseForward(m_pShouldFloodBlock);
   190 	g_Forwards.ReleaseForward(m_pDidFloodBlock);
   191 	g_Forwards.ReleaseForward(m_pOnClientSayCmd);
   192 	g_Forwards.ReleaseForward(m_pOnClientSayCmd_Post);
   193 }
   194 
   195 #if SOURCE_ENGINE == SE_DOTA
   196 void ChatTriggers::OnSayCommand_Pre(const CCommandContext &context, const CCommand &command)
   197 {
   198 #elif SOURCE_ENGINE >= SE_ORANGEBOX
   199 void ChatTriggers::OnSayCommand_Pre(const CCommand &command)
   200 {
   201 #else
   202 void ChatTriggers::OnSayCommand_Pre()
   203 {
   204 	CCommand command;
   205 #endif
   206 	int client = g_ConCmds.GetCommandClient();
   207 	m_bIsChatTrigger = false;
   208 	m_bWasFloodedMessage = false;
   209 	m_bPluginIgnored = false;
   210 
   211 	/* The server console cannot do this */
   212 	if (client == 0)
   213 	{
   214 		RETURN_META(MRES_IGNORED);
   215 	}
   216 
   217 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   218 
   219 	/* We guarantee the client is connected */
   220 	if (!pPlayer || !pPlayer->IsConnected())
   221 	{
   222 		RETURN_META(MRES_IGNORED);
   223 	}
   224 
   225 	const char *args = command.ArgS();
   226 	
   227 	if (!args)
   228 	{
   229 		RETURN_META(MRES_IGNORED);
   230 	}
   231 
   232 	/* Check if we need to block this message from being sent */
   233 	if (ClientIsFlooding(client))
   234 	{
   235 		char buffer[128];
   236 
   237 		if (!logicore.CoreTranslate(buffer, sizeof(buffer), "%T", 2, NULL, "Flooding the server", &client))
   238 			UTIL_Format(buffer, sizeof(buffer), "You are flooding the server!");
   239 
   240 		/* :TODO: we should probably kick people who spam too much. */
   241 
   242 		char fullbuffer[192];
   243 		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s", buffer);
   244 		g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
   245 
   246 		m_bWasFloodedMessage = true;
   247 
   248 		RETURN_META(MRES_SUPERCEDE);
   249 	}
   250 
   251 	/* Handle quoted string sets */
   252 	bool is_quoted = false;
   253 	if (args[0] == '"')
   254 	{
   255 		args++;
   256 		is_quoted = true;
   257 	}
   258 
   259 	const char * pCommandName = command.Arg(0);
   260 #if SOURCE_ENGINE == SE_EPISODEONE
   261 	if (m_bIsINS && strcmp(pCommandName, "say2") == 0 && strlen(args) >= 4)
   262 	{
   263 		args += 4;
   264 	}
   265 #endif
   266 
   267 	bool is_trigger = false;
   268 	bool is_silent = false;
   269 
   270 	/* Check for either trigger */
   271 	if (m_PubTriggerSize && strncmp(args, m_PubTrigger, m_PubTriggerSize) == 0)
   272 	{
   273 		is_trigger = true;
   274 		args = &args[m_PubTriggerSize];
   275 	} 
   276 	else if (m_PrivTriggerSize && strncmp(args, m_PrivTrigger, m_PrivTriggerSize) == 0) 
   277 	{
   278 		is_trigger = true;
   279 		is_silent = true;
   280 		args = &args[m_PrivTriggerSize];
   281 	}
   282 
   283 	/**
   284 	 * Test if this is actually a command!
   285 	 */
   286 	if (is_trigger && PreProcessTrigger(PEntityOfEntIndex(client), args, is_quoted))
   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 
   297 	if (m_pOnClientSayCmd->GetFunctionCount() != 0)
   298 	{
   299 		cell_t res = Pl_Continue;
   300 		m_pOnClientSayCmd->PushCell(client);
   301 		m_pOnClientSayCmd->PushString(pCommandName);
   302 		m_pOnClientSayCmd->PushString(command.ArgS());
   303 		m_pOnClientSayCmd->Execute(&res);
   304 
   305 		if (res >= Pl_Handled)
   306 		{
   307 			m_bPluginIgnored = (res >= Pl_Stop);
   308 			RETURN_META(MRES_SUPERCEDE);
   309 		}
   310 	}
   311 
   312 	if (m_bWillProcessInPost || \
   313 		(is_silent && g_bSupressSilentFails && pPlayer->GetAdminId() != INVALID_ADMIN_ID))
   314 	{
   315 		RETURN_META(MRES_SUPERCEDE);
   316 	}
   317 
   318 	/* Otherwise, let the command continue */
   319 	RETURN_META(MRES_IGNORED);
   320 }
   321 
   322 #if SOURCE_ENGINE == SE_DOTA
   323 void ChatTriggers::OnSayCommand_Post(const CCommandContext &context, const CCommand &command)
   324 {
   325 #elif SOURCE_ENGINE >= SE_ORANGEBOX
   326 void ChatTriggers::OnSayCommand_Post(const CCommand &command)
   327 {
   328 #else
   329 void ChatTriggers::OnSayCommand_Post()
   330 {
   331 	CCommand command;
   332 #endif
   333 	int client = g_ConCmds.GetCommandClient();
   334 
   335 	if (m_bWillProcessInPost)
   336 	{
   337 		/* Reset this for re-entrancy */
   338 		m_bWillProcessInPost = false;
   339 		
   340 		/* Execute the cached command */
   341 		unsigned int old = SetReplyTo(SM_REPLY_CHAT);
   342 #if SOURCE_ENGINE == SE_DOTA
   343 		engine->ClientCommand(client, "%s", m_ToExecute);
   344 #else
   345 		serverpluginhelpers->ClientCommand(PEntityOfEntIndex(client), m_ToExecute);
   346 #endif
   347 		SetReplyTo(old);
   348 	}
   349 
   350 	if (m_bPluginIgnored)
   351 	{
   352 		m_bPluginIgnored = false;
   353 	}
   354 	else if (!m_bWasFloodedMessage && m_pOnClientSayCmd_Post->GetFunctionCount() != 0)
   355 	{	
   356 		m_pOnClientSayCmd_Post->PushCell(client);
   357 		m_pOnClientSayCmd_Post->PushString(command.Arg(0));
   358 		m_pOnClientSayCmd_Post->PushString(command.ArgS());
   359 		m_pOnClientSayCmd_Post->Execute(NULL);
   360 	}
   361 
   362 	m_bIsChatTrigger = false;
   363 	m_bWasFloodedMessage = false;
   364 }
   365 
   366 bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args, bool is_quoted)
   367 {
   368 	/* Extract a command.  This is kind of sloppy. */
   369 	char cmd_buf[64];
   370 	size_t cmd_len = 0;
   371 	const char *inptr = args;
   372 	while (*inptr != '\0' 
   373 			&& !textparsers->IsWhitespace(inptr) 
   374 			&& *inptr != '"'
   375 			&& cmd_len < sizeof(cmd_buf) - 1)
   376 	{
   377 		cmd_buf[cmd_len++] = *inptr++;
   378 	}
   379 	cmd_buf[cmd_len] = '\0';
   380 
   381 	if (cmd_len == 0)
   382 	{
   383 		return false;
   384 	}
   385 
   386 	/* See if we have this registered */
   387 	bool prepended = false;
   388 	if (!g_ConCmds.LookForSourceModCommand(cmd_buf))
   389 	{
   390 		/* Check if we had an "sm_" prefix */
   391 		if (strncmp(cmd_buf, "sm_", 3) == 0)
   392 		{
   393 			return false;
   394 		}
   395 
   396 		/* Now, prepend.  Don't worry about the buffers.  This will 
   397 		 * work because the sizes are limited from earlier.
   398 		 */
   399 		char new_buf[80];
   400 		strcpy(new_buf, "sm_");
   401 		strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3);
   402 
   403 		/* Recheck */
   404 		if (!g_ConCmds.LookForSourceModCommand(new_buf))
   405 		{
   406 			return false;
   407 		}
   408 		
   409 		prepended = true;
   410 	}
   411 
   412 	/* See if we need to do extra string manipulation */
   413 	if (is_quoted || prepended)
   414 	{
   415 		size_t len;
   416 
   417 		/* Check if we need to prepend sm_ */
   418 		if (prepended)
   419 		{
   420 			len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
   421 		} else {
   422 			len = strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   423 		}
   424 		
   425 		/* Check if we need to strip a quote */
   426 		if (is_quoted)
   427 		{
   428 			if (m_ToExecute[len-1] == '"')
   429 			{
   430 				m_ToExecute[--len] = '\0';
   431 			}
   432 		}
   433 	} else {
   434 		strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   435 	}
   436 
   437 	return true;
   438 }
   439 
   440 unsigned int ChatTriggers::SetReplyTo(unsigned int reply)
   441 {
   442 	unsigned int old = m_ReplyTo;
   443 
   444 	m_ReplyTo = reply;
   445 
   446 	return old;
   447 }
   448 
   449 unsigned int ChatTriggers::GetReplyTo()
   450 {
   451 	return m_ReplyTo;
   452 }
   453 
   454 bool ChatTriggers::IsChatTrigger()
   455 {
   456 	return m_bIsChatTrigger;
   457 }
   458 
   459 bool ChatTriggers::ClientIsFlooding(int client)
   460 {
   461 	bool is_flooding = false;
   462 
   463 	if (m_pShouldFloodBlock->GetFunctionCount() != 0)
   464 	{
   465 		cell_t res = 0;
   466 
   467 		m_pShouldFloodBlock->PushCell(client);
   468 		m_pShouldFloodBlock->Execute(&res);
   469 
   470 		if (res != 0)
   471 		{
   472 			is_flooding = true;
   473 		}
   474 	}
   475 
   476 	if (m_pDidFloodBlock->GetFunctionCount() != 0)
   477 	{
   478 		m_pDidFloodBlock->PushCell(client);
   479 		m_pDidFloodBlock->PushCell(is_flooding ? 1 : 0);
   480 		m_pDidFloodBlock->Execute(NULL);
   481 	}
   482 
   483 	return is_flooding;
   484 }
   485 
   486 bool ChatTriggers::WasFloodedMessage()
   487 {
   488 	return m_bWasFloodedMessage;
   489 }