core/ChatTriggers.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Mon Jul 29 10:03:18 2013 -0400 (2013-07-29)
changeset 3960 d50b36dc6821
parent 3942 7aaa00382627
child 3974 9b3779f472d4
permissions -rw-r--r--
Updated for latest hl2sdk-dota changes.
     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 }
    67 
    68 ChatTriggers::~ChatTriggers()
    69 {
    70 	delete [] m_PubTrigger;
    71 	m_PubTrigger = NULL;
    72 	delete [] m_PrivTrigger;
    73 	m_PrivTrigger = NULL;
    74 }
    75 
    76 ConfigResult ChatTriggers::OnSourceModConfigChanged(const char *key, 
    77 													const char *value, 
    78 													ConfigSource source,
    79 													char *error, 
    80 													size_t maxlength)
    81 {
    82 	if (strcmp(key, "PublicChatTrigger") == 0)
    83 	{
    84 		delete [] m_PubTrigger;
    85 		m_PubTrigger = sm_strdup(value);
    86 		m_PubTriggerSize = strlen(m_PubTrigger);
    87 		return ConfigResult_Accept;
    88 	}
    89 	else if (strcmp(key, "SilentChatTrigger") == 0)
    90 	{
    91 		delete [] m_PrivTrigger;
    92 		m_PrivTrigger = sm_strdup(value);
    93 		m_PrivTriggerSize = strlen(m_PrivTrigger);
    94 		return ConfigResult_Accept;
    95 	}
    96 	else if (strcmp(key, "SilentFailSuppress") == 0)
    97 	{
    98 		g_bSupressSilentFails = strcmp(value, "yes") == 0;
    99 		return ConfigResult_Accept;
   100 	}
   101 
   102 	return ConfigResult_Ignore;
   103 }
   104 
   105 void ChatTriggers::OnSourceModAllInitialized()
   106 {
   107 	m_pShouldFloodBlock = g_Forwards.CreateForward("OnClientFloodCheck", ET_Event, 1, NULL, Param_Cell);
   108 	m_pDidFloodBlock = g_Forwards.CreateForward("OnClientFloodResult", ET_Event, 2, NULL, Param_Cell, Param_Cell);
   109 }
   110 
   111 void ChatTriggers::OnSourceModAllInitialized_Post()
   112 {
   113 	logicore.AddCorePhraseFile("antiflood.phrases");
   114 }
   115 
   116 void ChatTriggers::OnSourceModGameInitialized()
   117 {
   118 	m_pSayCmd = FindCommand("say");
   119 	m_pSayTeamCmd = FindCommand("say_team");
   120 
   121 	if (m_pSayCmd)
   122 	{
   123 		SH_ADD_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
   124 		SH_ADD_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
   125 	}
   126 	if (m_pSayTeamCmd)
   127 	{
   128 		SH_ADD_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
   129 		SH_ADD_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
   130 	}
   131 }
   132 
   133 void ChatTriggers::OnSourceModShutdown()
   134 {
   135 	if (m_pSayTeamCmd)
   136 	{
   137 		SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
   138 		SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayTeamCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
   139 	}
   140 	if (m_pSayCmd)
   141 	{
   142 		SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Post), true);
   143 		SH_REMOVE_HOOK(ConCommand, Dispatch, m_pSayCmd, SH_MEMBER(this, &ChatTriggers::OnSayCommand_Pre), false);
   144 	}
   145 
   146 	g_Forwards.ReleaseForward(m_pShouldFloodBlock);
   147 	g_Forwards.ReleaseForward(m_pDidFloodBlock);
   148 }
   149 
   150 #if SOURCE_ENGINE == SE_DOTA
   151 void ChatTriggers::OnSayCommand_Pre(const CCommandContext &context, const CCommand &command)
   152 {
   153 #elif SOURCE_ENGINE >= SE_ORANGEBOX
   154 void ChatTriggers::OnSayCommand_Pre(const CCommand &command)
   155 {
   156 #else
   157 void ChatTriggers::OnSayCommand_Pre()
   158 {
   159 	CCommand command;
   160 #endif
   161 	int client;
   162 	CPlayer *pPlayer;
   163 	
   164 	client = g_ConCmds.GetCommandClient();
   165 	m_bIsChatTrigger = false;
   166 	m_bWasFloodedMessage = false;
   167 
   168 	/* The server console cannot do this */
   169 	if (client == 0 || (pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
   170 	{
   171 		RETURN_META(MRES_IGNORED);
   172 	}
   173 
   174 	/* We guarantee the client is connected */
   175 	if (!pPlayer->IsConnected())
   176 	{
   177 		RETURN_META(MRES_IGNORED);
   178 	}
   179 
   180 	const char *args = command.ArgS();
   181 	
   182 	if (!args)
   183 	{
   184 		RETURN_META(MRES_IGNORED);
   185 	}
   186 
   187 	/* Check if we need to block this message from being sent */
   188 	if (ClientIsFlooding(client))
   189 	{
   190 		char buffer[128];
   191 
   192 		if (!logicore.CoreTranslate(buffer, sizeof(buffer), "%T", 2, NULL, "Flooding the server", &client))
   193 			UTIL_Format(buffer, sizeof(buffer), "You are flooding the server!");
   194 
   195 		/* :TODO: we should probably kick people who spam too much. */
   196 
   197 		char fullbuffer[192];
   198 		UTIL_Format(fullbuffer, sizeof(fullbuffer), "[SM] %s", buffer);
   199 		g_HL2.TextMsg(client, HUD_PRINTTALK, fullbuffer);
   200 
   201 		m_bWasFloodedMessage = true;
   202 
   203 		RETURN_META(MRES_SUPERCEDE);
   204 	}
   205 
   206 	/* Handle quoted string sets */
   207 	bool is_quoted = false;
   208 	if (args[0] == '"')
   209 	{
   210 		args++;
   211 		is_quoted = true;
   212 	}
   213 
   214 	bool is_trigger = false;
   215 	bool is_silent = false;
   216 
   217 	/* Check for either trigger */
   218 	if (m_PubTriggerSize && strncmp(args, m_PubTrigger, m_PubTriggerSize) == 0)
   219 	{
   220 		is_trigger = true;
   221 		args = &args[m_PubTriggerSize];
   222 	} 
   223 	else if (m_PrivTriggerSize && strncmp(args, m_PrivTrigger, m_PrivTriggerSize) == 0) 
   224 	{
   225 		is_trigger = true;
   226 		is_silent = true;
   227 		args = &args[m_PrivTriggerSize];
   228 	}
   229 
   230 	if (!is_trigger)
   231 	{
   232 		RETURN_META(MRES_IGNORED);
   233 	}
   234 
   235 	/**
   236 	 * Test if this is actually a command!
   237 	 */
   238 	if (!PreProcessTrigger(PEntityOfEntIndex(client), args, is_quoted))
   239 	{
   240 		CPlayer *pPlayer;
   241 		if (is_silent 
   242 			&& g_bSupressSilentFails 
   243 			&& client != 0
   244 			&& (pPlayer = g_Players.GetPlayerByIndex(client)) != NULL
   245 			&& pPlayer->GetAdminId() != INVALID_ADMIN_ID)
   246 		{
   247 			RETURN_META(MRES_SUPERCEDE);
   248 		}
   249 		RETURN_META(MRES_IGNORED);
   250 	}
   251 
   252 	m_bIsChatTrigger = true;
   253 
   254 	/**
   255 	 * We'll execute it in post.
   256 	 */
   257 	m_bWillProcessInPost = true;
   258 	m_bTriggerWasSilent = is_silent;
   259 
   260 	/* If we're silent, block */
   261 	if (is_silent)
   262 	{
   263 		RETURN_META(MRES_SUPERCEDE);
   264 	}
   265 
   266 	/* Otherwise, let the command continue */
   267 	RETURN_META(MRES_IGNORED);
   268 }
   269 
   270 #if SOURCE_ENGINE == SE_DOTA
   271 void ChatTriggers::OnSayCommand_Post(const CCommandContext &context, const CCommand &command)
   272 #elif SOURCE_ENGINE >= SE_ORANGEBOX
   273 void ChatTriggers::OnSayCommand_Post(const CCommand &command)
   274 #else
   275 void ChatTriggers::OnSayCommand_Post()
   276 #endif
   277 {
   278 	m_bIsChatTrigger = false;
   279 	m_bWasFloodedMessage = false;
   280 	if (m_bWillProcessInPost)
   281 	{
   282 		/* Reset this for re-entrancy */
   283 		m_bWillProcessInPost = false;
   284 		
   285 		/* Execute the cached command */
   286 		int client = g_ConCmds.GetCommandClient();
   287 		unsigned int old = SetReplyTo(SM_REPLY_CHAT);
   288 #if SOURCE_ENGINE == SE_DOTA
   289 		engine->ClientCommand(client, "%s", m_ToExecute);
   290 #else
   291 		serverpluginhelpers->ClientCommand(PEntityOfEntIndex(client), m_ToExecute);
   292 #endif
   293 		SetReplyTo(old);
   294 	}
   295 }
   296 
   297 bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args, bool is_quoted)
   298 {
   299 	/* Extract a command.  This is kind of sloppy. */
   300 	char cmd_buf[64];
   301 	size_t cmd_len = 0;
   302 	const char *inptr = args;
   303 	while (*inptr != '\0' 
   304 			&& !textparsers->IsWhitespace(inptr) 
   305 			&& *inptr != '"'
   306 			&& cmd_len < sizeof(cmd_buf) - 1)
   307 	{
   308 		cmd_buf[cmd_len++] = *inptr++;
   309 	}
   310 	cmd_buf[cmd_len] = '\0';
   311 
   312 	if (cmd_len == 0)
   313 	{
   314 		return false;
   315 	}
   316 
   317 	/* See if we have this registered */
   318 	bool prepended = false;
   319 	if (!g_ConCmds.LookForSourceModCommand(cmd_buf))
   320 	{
   321 		/* Check if we had an "sm_" prefix */
   322 		if (strncmp(cmd_buf, "sm_", 3) == 0)
   323 		{
   324 			return false;
   325 		}
   326 
   327 		/* Now, prepend.  Don't worry about the buffers.  This will 
   328 		 * work because the sizes are limited from earlier.
   329 		 */
   330 		char new_buf[80];
   331 		strcpy(new_buf, "sm_");
   332 		strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3);
   333 
   334 		/* Recheck */
   335 		if (!g_ConCmds.LookForSourceModCommand(new_buf))
   336 		{
   337 			return false;
   338 		}
   339 		
   340 		prepended = true;
   341 	}
   342 
   343 	/* See if we need to do extra string manipulation */
   344 	if (is_quoted || prepended)
   345 	{
   346 		size_t len;
   347 
   348 		/* Check if we need to prepend sm_ */
   349 		if (prepended)
   350 		{
   351 			len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
   352 		} else {
   353 			len = strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   354 		}
   355 		
   356 		/* Check if we need to strip a quote */
   357 		if (is_quoted)
   358 		{
   359 			if (m_ToExecute[len-1] == '"')
   360 			{
   361 				m_ToExecute[--len] = '\0';
   362 			}
   363 		}
   364 	} else {
   365 		strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   366 	}
   367 
   368 	return true;
   369 }
   370 
   371 unsigned int ChatTriggers::SetReplyTo(unsigned int reply)
   372 {
   373 	unsigned int old = m_ReplyTo;
   374 
   375 	m_ReplyTo = reply;
   376 
   377 	return old;
   378 }
   379 
   380 unsigned int ChatTriggers::GetReplyTo()
   381 {
   382 	return m_ReplyTo;
   383 }
   384 
   385 bool ChatTriggers::IsChatTrigger()
   386 {
   387 	return m_bIsChatTrigger;
   388 }
   389 
   390 bool ChatTriggers::ClientIsFlooding(int client)
   391 {
   392 	bool is_flooding = false;
   393 
   394 	if (m_pShouldFloodBlock->GetFunctionCount() != 0)
   395 	{
   396 		cell_t res = 0;
   397 
   398 		m_pShouldFloodBlock->PushCell(client);
   399 		m_pShouldFloodBlock->Execute(&res);
   400 
   401 		if (res != 0)
   402 		{
   403 			is_flooding = true;
   404 		}
   405 	}
   406 
   407 	if (m_pDidFloodBlock->GetFunctionCount() != 0)
   408 	{
   409 		m_pDidFloodBlock->PushCell(client);
   410 		m_pDidFloodBlock->PushCell(is_flooding ? 1 : 0);
   411 		m_pDidFloodBlock->Execute(NULL);
   412 	}
   413 
   414 	return is_flooding;
   415 }
   416 
   417 bool ChatTriggers::WasFloodedMessage()
   418 {
   419 	return m_bWasFloodedMessage;
   420 }