core/ChatTriggers.cpp
author David Anderson <dvander@alliedmods.net>
Sun Jun 17 07:32:31 2007 +0000 (2007-06-17)
changeset 969 0cd7fc27e054
parent 935 287169a89759
child 1103 043baf2d8c96
permissions -rw-r--r--
fixed a state bug in the gameconf parser that would break parsing after a signature section was done
     1 #include "ChatTriggers.h"
     2 #include "sm_stringutil.h"
     3 #include "TextParsers.h"
     4 #include "ConCmdManager.h"
     5 
     6 /* :HACKHACK: We can't SH_DECL here because ConCmdManager.cpp does */
     7 extern bool __SourceHook_FHRemoveConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
     8 
     9 #if SH_IMPL_VERSION >= 4
    10 extern int __SourceHook_FHAddConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
    11 #else
    12 extern bool __SourceHook_FHAddConCommandDispatch(void *, bool, class fastdelegate::FastDelegate0<void>);
    13 #endif
    14 
    15 ChatTriggers g_ChatTriggers;
    16 
    17 ChatTriggers::ChatTriggers() : m_pSayCmd(NULL), m_bWillProcessInPost(false), 
    18 	m_ReplyTo(SM_REPLY_CONSOLE)
    19 {
    20 	m_PubTrigger = sm_strdup("!");
    21 	m_PrivTrigger = sm_strdup("/");
    22 	m_PubTriggerSize = 1;
    23 	m_PrivTriggerSize = 1;
    24 }
    25 
    26 ChatTriggers::~ChatTriggers()
    27 {
    28 	delete [] m_PubTrigger;
    29 	m_PubTrigger = NULL;
    30 	delete [] m_PrivTrigger;
    31 	m_PrivTrigger = NULL;
    32 }
    33 
    34 ConfigResult ChatTriggers::OnSourceModConfigChanged(const char *key, 
    35 													const char *value, 
    36 													ConfigSource source,
    37 													char *error, 
    38 													size_t maxlength)
    39 {
    40 	if (strcmp(key, "PublicChatTrigger") == 0)
    41 	{
    42 		delete [] m_PubTrigger;
    43 		m_PubTrigger = sm_strdup(value);
    44 		m_PubTriggerSize = strlen(m_PubTrigger);
    45 		return ConfigResult_Accept;
    46 	} else if (strcmp(key, "SilentChatTrigger") == 0) {
    47 		delete [] m_PrivTrigger;
    48 		m_PrivTrigger = sm_strdup(value);
    49 		m_PrivTriggerSize = strlen(m_PrivTrigger);
    50 		return ConfigResult_Accept;
    51 	}
    52 
    53 	return ConfigResult_Ignore;
    54 }
    55 
    56 void ChatTriggers::OnSourceModGameInitialized()
    57 {
    58 	unsigned int total = 2;
    59 	ConCommandBase *pCmd = icvar->GetCommands();
    60 	const char *name;
    61 	while (pCmd)
    62 	{
    63 		if (pCmd->IsCommand())
    64 		{
    65 			name = pCmd->GetName();
    66 			if (!m_pSayCmd && strcmp(name, "say") == 0)
    67 			{
    68 				m_pSayCmd = (ConCommand *)pCmd;
    69 				if (--total == 0)
    70 				{
    71 					break;
    72 				}
    73 			} else if (!m_pSayTeamCmd && strcmp(name, "say_team") == 0) {
    74 				m_pSayTeamCmd = (ConCommand *)pCmd;
    75 				if (--total == 0)
    76 				{
    77 					break;
    78 				}
    79 			}
    80 		}
    81 		pCmd = const_cast<ConCommandBase *>(pCmd->GetNext());
    82 	}
    83 
    84 	if (m_pSayCmd)
    85 	{
    86 		SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
    87 		SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Post, true);
    88 	}
    89 	if (m_pSayTeamCmd)
    90 	{
    91 		SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
    92 		SH_ADD_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Post, true);
    93 	}
    94 }
    95 
    96 void ChatTriggers::OnSourceModShutdown()
    97 {
    98 	if (m_pSayTeamCmd)
    99 	{
   100 		SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Post, true);
   101 		SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayTeamCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
   102 	}
   103 	if (m_pSayCmd)
   104 	{
   105 		SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Post, true);
   106 		SH_REMOVE_HOOK_MEMFUNC(ConCommand, Dispatch, m_pSayCmd, this, &ChatTriggers::OnSayCommand_Pre, false);
   107 	}
   108 }
   109 
   110 void ChatTriggers::OnSayCommand_Pre()
   111 {
   112 	int client = g_ConCmds.GetCommandClient();
   113 
   114 	/* The server console cannot do this */
   115 	if (client == 0)
   116 	{
   117 		RETURN_META(MRES_IGNORED);
   118 	}
   119 
   120 	const char *args = engine->Cmd_Args();
   121 
   122 	/* Handle quoted string sets */
   123 	bool is_quoted = false;
   124 	if (args[0] == '"')
   125 	{
   126 		args++;
   127 		is_quoted = true;
   128 	}
   129 
   130 	bool is_trigger = false;
   131 	bool is_silent = false;
   132 
   133 	/* Check for either trigger */
   134 	if (m_PubTriggerSize && strncmp(args, m_PubTrigger, m_PubTriggerSize) == 0)
   135 	{
   136 		is_trigger = true;
   137 	} else if (m_PrivTriggerSize && strncmp(args, m_PrivTrigger, m_PrivTriggerSize) == 0) {
   138 		is_trigger = true;
   139 		is_silent = true;
   140 	}
   141 
   142 	if (!is_trigger)
   143 	{
   144 		RETURN_META(MRES_IGNORED);
   145 	}
   146 
   147 	/* If we're a public command, process later */
   148 	if (!is_silent)
   149 	{
   150 		/* We have to process this in _post_ instead.  Darn. */
   151 		m_bWillProcessInPost = true;
   152 		RETURN_META(MRES_IGNORED);
   153 	}
   154 
   155 	/* Otherwise, process now */
   156 	if (ProcessTrigger(engine->PEntityOfEntIndex(client), &args[m_PrivTriggerSize], is_quoted))
   157 	{
   158 		/* If we succeed, block the original say! */
   159 		RETURN_META(MRES_SUPERCEDE);
   160 	}
   161 
   162 	RETURN_META(MRES_IGNORED);
   163 }
   164 
   165 void ChatTriggers::OnSayCommand_Post()
   166 {
   167 	if (m_bWillProcessInPost)
   168 	{
   169 		/* Reset this for re-entrancy */
   170 		m_bWillProcessInPost = false;
   171 
   172 		/* Get our arguments */
   173 		const char *args = engine->Cmd_Args();
   174 
   175 		/* Handle quotations */
   176 		bool is_quoted = false;
   177 		if (args[0] == '"')
   178 		{
   179 			args++;
   180 			is_quoted = true;
   181 		}
   182 
   183 		int client = g_ConCmds.GetCommandClient();
   184 
   185 		ProcessTrigger(engine->PEntityOfEntIndex(client), &args[m_PubTriggerSize], is_quoted);
   186 	}
   187 }
   188 
   189 bool ChatTriggers::ProcessTrigger(edict_t *pEdict, const char *args, bool is_quoted)
   190 {
   191 	/* Eat up whitespace */
   192 	while (*args != '\0' && IsWhitespace(args))
   193 	{
   194 		args++;
   195 	}
   196 
   197 	/* Check if we're still valid */
   198 	if (*args == '\0')
   199 	{
   200 		return false;
   201 	}
   202 
   203 	/* Extract a command.  This is kind of sloppy. */
   204 	char cmd_buf[64];
   205 	size_t cmd_len = 0;
   206 	const char *inptr = args;
   207 	while (*inptr != '\0' 
   208 			&& !IsWhitespace(inptr) 
   209 			&& *inptr != '"'
   210 			&& cmd_len < sizeof(cmd_buf) - 1)
   211 	{
   212 		cmd_buf[cmd_len++] = *inptr++;
   213 	}
   214 	cmd_buf[cmd_len] = '\0';
   215 
   216 	/* See if we have this registered */
   217 	bool prepended = false;
   218 	if (!g_ConCmds.LookForSourceModCommand(cmd_buf))
   219 	{
   220 		/* Check if we had an "sm_" prefix */
   221 		if (strncmp(cmd_buf, "sm_", 3) == 0)
   222 		{
   223 			return false;
   224 		}
   225 
   226 		/* Now, prepend.  Don't worry about the buffers.  This will 
   227 		 * work because the sizes are limited from earlier.
   228 		 */
   229 		char new_buf[80];
   230 		strcpy(new_buf, "sm_");
   231 		strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3);
   232 
   233 		/* Recheck */
   234 		if (!g_ConCmds.LookForSourceModCommand(new_buf))
   235 		{
   236 			return false;
   237 		}
   238 		
   239 		prepended = true;
   240 	}
   241 
   242 	/* See if we need to do extra string manipulation */
   243 	if (is_quoted || prepended)
   244 	{
   245 		static char buffer[300];
   246 		size_t len;
   247 
   248 		/* Check if we need to prepend sm_ */
   249 		if (prepended)
   250 		{
   251 			len = UTIL_Format(buffer, sizeof(buffer), "sm_%s", args);
   252 		} else {
   253 			len = strncopy(buffer, args, sizeof(buffer));
   254 		}
   255 		
   256 		/* Check if we need to strip a quote */
   257 		if (is_quoted)
   258 		{
   259 			if (buffer[len-1] == '"')
   260 			{
   261 				buffer[--len] = '\0';
   262 			}
   263 		}
   264 
   265 		args = buffer;
   266 	}
   267 
   268 	/* Finally, execute! */
   269 	unsigned int old = SetReplyTo(SM_REPLY_CHAT);
   270 	serverpluginhelpers->ClientCommand(pEdict, args);
   271 	SetReplyTo(old);
   272 
   273 	return true;
   274 }
   275 
   276 unsigned int ChatTriggers::SetReplyTo(unsigned int reply)
   277 {
   278 	unsigned int old = m_ReplyTo;
   279 
   280 	m_ReplyTo = reply;
   281 
   282 	return old;
   283 }
   284 
   285 unsigned int ChatTriggers::GetReplyTo()
   286 {
   287 	return m_ReplyTo;
   288 }