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