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