core/ChatTriggers.cpp
author David Anderson <dvander@alliedmods.net>
Fri Aug 30 10:16:28 2013 -0700 (2013-08-30)
changeset 4109 10eb68d668af
parent 4052 c5b6ed34f5e3
child 4139 de7d1cd00703
permissions -rw-r--r--
Use Refcounted to manage to NativeEntry (bug 5852 part 4, r=ds).
     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_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 	const char *args = command.ArgS();
   212 	
   213 	if (!args)
   214 	{
   215 		RETURN_META(MRES_IGNORED);
   216 	}
   217 
   218 	/* Save these off for post hook as the command data returned from the engine in older engine versions 
   219 	 * can be NULL, despite the data still being there and valid. */
   220 	m_Arg0Backup = command.Arg(0);
   221 	m_ArgSBackup = command.ArgS();
   222 
   223 	/* The server console cannot do this */
   224 	if (client == 0)
   225 	{
   226 		cell_t res = CallOnClientSayCommand(client);
   227 		if (res >= Pl_Handled)
   228 		{
   229 			m_bPluginIgnored = (res >= Pl_Stop);
   230 			RETURN_META(MRES_SUPERCEDE);
   231 		}
   232 		RETURN_META(MRES_IGNORED);
   233 	}
   234 
   235 	CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
   236 
   237 	/* We guarantee the client is connected */
   238 	if (!pPlayer || !pPlayer->IsConnected())
   239 	{
   240 		RETURN_META(MRES_IGNORED);
   241 	}
   242 
   243 	/* Check if we need to block this message from being sent */
   244 	if (ClientIsFlooding(client))
   245 	{
   246 		char buffer[128];
   247 
   248 		if (!logicore.CoreTranslate(buffer, sizeof(buffer), "%T", 2, NULL, "Flooding the server", &client))
   249 			UTIL_Format(buffer, sizeof(buffer), "You are flooding the server!");
   250 
   251 		/* :TODO: we should probably kick people who spam too much. */
   252 
   253 		char fullbuffer[192];
   254 		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s", buffer);
   255 		g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
   256 
   257 		m_bWasFloodedMessage = true;
   258 
   259 		RETURN_META(MRES_SUPERCEDE);
   260 	}
   261 
   262 	/* Handle quoted string sets */
   263 	bool is_quoted = false;
   264 	if (args[0] == '"')
   265 	{
   266 		args++;
   267 		is_quoted = true;
   268 	}
   269 
   270 #if SOURCE_ENGINE == SE_EPISODEONE
   271 	if (m_bIsINS && strcmp(m_Arg0Backup, "say2") == 0 && strlen(args) >= 4)
   272 	{
   273 		args += 4;
   274 	}
   275 #endif
   276 
   277 	bool is_trigger = false;
   278 	bool is_silent = false;
   279 
   280 	/* Check for either trigger */
   281 	if (m_PubTriggerSize && strncmp(args, m_PubTrigger, m_PubTriggerSize) == 0)
   282 	{
   283 		is_trigger = true;
   284 		args = &args[m_PubTriggerSize];
   285 	} 
   286 	else if (m_PrivTriggerSize && strncmp(args, m_PrivTrigger, m_PrivTriggerSize) == 0) 
   287 	{
   288 		is_trigger = true;
   289 		is_silent = true;
   290 		args = &args[m_PrivTriggerSize];
   291 	}
   292 
   293 	/**
   294 	 * Test if this is actually a command!
   295 	 */
   296 	if (is_trigger && PreProcessTrigger(PEntityOfEntIndex(client), args, is_quoted))
   297 	{
   298 		m_bIsChatTrigger = true;
   299 
   300 		/**
   301 		 * We'll execute it in post.
   302 		 */
   303 		m_bWillProcessInPost = true;
   304 	}
   305 
   306 	cell_t res = CallOnClientSayCommand(client);
   307 
   308 	if (res >= Pl_Handled)
   309 	{
   310 		m_bPluginIgnored = (res >= Pl_Stop);
   311 		RETURN_META(MRES_SUPERCEDE);
   312 	}
   313 
   314 	if (is_silent && (m_bIsChatTrigger || (g_bSupressSilentFails && pPlayer->GetAdminId() != INVALID_ADMIN_ID)))
   315 	{
   316 		RETURN_META(MRES_SUPERCEDE);
   317 	}
   318 
   319 	/* Otherwise, let the command continue */
   320 	RETURN_META(MRES_IGNORED);
   321 }
   322 
   323 #if SOURCE_ENGINE == SE_DOTA
   324 void ChatTriggers::OnSayCommand_Post(const CCommandContext &context, const CCommand &command)
   325 #elif SOURCE_ENGINE >= SE_ORANGEBOX
   326 void ChatTriggers::OnSayCommand_Post(const CCommand &command)
   327 #else
   328 void ChatTriggers::OnSayCommand_Post()
   329 #endif
   330 {
   331 	int client = g_ConCmds.GetCommandClient();
   332 
   333 	if (m_bWillProcessInPost)
   334 	{
   335 		/* Reset this for re-entrancy */
   336 		m_bWillProcessInPost = false;
   337 		
   338 		/* Execute the cached command */
   339 		unsigned int old = SetReplyTo(SM_REPLY_CHAT);
   340 #if SOURCE_ENGINE == SE_DOTA
   341 		engine->ClientCommand(client, "%s", m_ToExecute);
   342 #else
   343 		serverpluginhelpers->ClientCommand(PEntityOfEntIndex(client), m_ToExecute);
   344 #endif
   345 		SetReplyTo(old);
   346 	}
   347 
   348 	if (m_bPluginIgnored)
   349 	{
   350 		m_bPluginIgnored = false;
   351 	}
   352 	else if (!m_bWasFloodedMessage && !m_bIsChatTrigger && m_pOnClientSayCmd_Post->GetFunctionCount() != 0)
   353 	{	
   354 		m_pOnClientSayCmd_Post->PushCell(client);
   355 		m_pOnClientSayCmd_Post->PushString(m_Arg0Backup);
   356 		m_pOnClientSayCmd_Post->PushString(m_ArgSBackup);
   357 		m_pOnClientSayCmd_Post->Execute(NULL);
   358 	}
   359 
   360 	m_bIsChatTrigger = false;
   361 	m_bWasFloodedMessage = false;
   362 }
   363 
   364 bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args, bool is_quoted)
   365 {
   366 	/* Extract a command.  This is kind of sloppy. */
   367 	char cmd_buf[64];
   368 	size_t cmd_len = 0;
   369 	const char *inptr = args;
   370 	while (*inptr != '\0' 
   371 			&& !textparsers->IsWhitespace(inptr) 
   372 			&& *inptr != '"'
   373 			&& cmd_len < sizeof(cmd_buf) - 1)
   374 	{
   375 		cmd_buf[cmd_len++] = *inptr++;
   376 	}
   377 	cmd_buf[cmd_len] = '\0';
   378 
   379 	if (cmd_len == 0)
   380 	{
   381 		return false;
   382 	}
   383 
   384 	/* See if we have this registered */
   385 	bool prepended = false;
   386 	if (!g_ConCmds.LookForSourceModCommand(cmd_buf))
   387 	{
   388 		/* Check if we had an "sm_" prefix */
   389 		if (strncmp(cmd_buf, "sm_", 3) == 0)
   390 		{
   391 			return false;
   392 		}
   393 
   394 		/* Now, prepend.  Don't worry about the buffers.  This will 
   395 		 * work because the sizes are limited from earlier.
   396 		 */
   397 		char new_buf[80];
   398 		strcpy(new_buf, "sm_");
   399 		strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3);
   400 
   401 		/* Recheck */
   402 		if (!g_ConCmds.LookForSourceModCommand(new_buf))
   403 		{
   404 			return false;
   405 		}
   406 		
   407 		prepended = true;
   408 	}
   409 
   410 	/* See if we need to do extra string manipulation */
   411 	if (is_quoted || prepended)
   412 	{
   413 		size_t len;
   414 
   415 		/* Check if we need to prepend sm_ */
   416 		if (prepended)
   417 		{
   418 			len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
   419 		} else {
   420 			len = strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   421 		}
   422 		
   423 		/* Check if we need to strip a quote */
   424 		if (is_quoted)
   425 		{
   426 			if (m_ToExecute[len-1] == '"')
   427 			{
   428 				m_ToExecute[--len] = '\0';
   429 			}
   430 		}
   431 	} else {
   432 		strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   433 	}
   434 
   435 	return true;
   436 }
   437 
   438 cell_t ChatTriggers::CallOnClientSayCommand(int client)
   439 {
   440 	cell_t res = Pl_Continue;
   441 	if (!m_bIsChatTrigger && m_pOnClientSayCmd->GetFunctionCount() != 0)
   442 	{
   443 		m_pOnClientSayCmd->PushCell(client);
   444 		m_pOnClientSayCmd->PushString(m_Arg0Backup);
   445 		m_pOnClientSayCmd->PushString(m_ArgSBackup);
   446 		m_pOnClientSayCmd->Execute(&res);
   447 	}
   448 	return res;
   449 }
   450 
   451 unsigned int ChatTriggers::SetReplyTo(unsigned int reply)
   452 {
   453 	unsigned int old = m_ReplyTo;
   454 
   455 	m_ReplyTo = reply;
   456 
   457 	return old;
   458 }
   459 
   460 unsigned int ChatTriggers::GetReplyTo()
   461 {
   462 	return m_ReplyTo;
   463 }
   464 
   465 bool ChatTriggers::IsChatTrigger()
   466 {
   467 	return m_bIsChatTrigger;
   468 }
   469 
   470 bool ChatTriggers::ClientIsFlooding(int client)
   471 {
   472 	bool is_flooding = false;
   473 
   474 	if (m_pShouldFloodBlock->GetFunctionCount() != 0)
   475 	{
   476 		cell_t res = 0;
   477 
   478 		m_pShouldFloodBlock->PushCell(client);
   479 		m_pShouldFloodBlock->Execute(&res);
   480 
   481 		if (res != 0)
   482 		{
   483 			is_flooding = true;
   484 		}
   485 	}
   486 
   487 	if (m_pDidFloodBlock->GetFunctionCount() != 0)
   488 	{
   489 		m_pDidFloodBlock->PushCell(client);
   490 		m_pDidFloodBlock->PushCell(is_flooding ? 1 : 0);
   491 		m_pDidFloodBlock->Execute(NULL);
   492 	}
   493 
   494 	return is_flooding;
   495 }
   496 
   497 bool ChatTriggers::WasFloodedMessage()
   498 {
   499 	return m_bWasFloodedMessage;
   500 }