core/ChatTriggers.cpp
author David Anderson <dvander@alliedmods.net>
Thu Jul 19 03:22:10 2007 +0000 (2007-07-19)
changeset 1134 68e8b8d386ff
parent 1103 043baf2d8c96
child 1139 5e2ace825428
permissions -rw-r--r--
fixed another translator cache issue
     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_bTriggerWasSilent(false), 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 		args = &args[m_PubTriggerSize];
   138 	} else if (m_PrivTriggerSize && strncmp(args, m_PrivTrigger, m_PrivTriggerSize) == 0) {
   139 		is_trigger = true;
   140 		is_silent = true;
   141 		args = &args[m_PrivTriggerSize];
   142 	}
   143 
   144 	if (!is_trigger)
   145 	{
   146 		RETURN_META(MRES_IGNORED);
   147 	}
   148 
   149 	/**
   150 	 * Test if this is actually a command!
   151 	 */
   152 	if (!PreProcessTrigger(engine->PEntityOfEntIndex(client), args, is_quoted))
   153 	{
   154 		RETURN_META(MRES_IGNORED);
   155 	}
   156 
   157 	/**
   158 	 * We'll execute it in post.
   159 	 */
   160 	m_bWillProcessInPost = true;
   161 	m_bTriggerWasSilent = is_silent;
   162 
   163 	/* If we're silent, block */
   164 	if (is_silent)
   165 	{
   166 		RETURN_META(MRES_SUPERCEDE);
   167 	}
   168 
   169 	/* Otherwise, let the command continue */
   170 	RETURN_META(MRES_IGNORED);
   171 }
   172 
   173 void ChatTriggers::OnSayCommand_Post()
   174 {
   175 	if (m_bWillProcessInPost)
   176 	{
   177 		/* Reset this for re-entrancy */
   178 		m_bWillProcessInPost = false;
   179 		
   180 		/* Execute the cached command */
   181 		int client = g_ConCmds.GetCommandClient();
   182 		unsigned int old = SetReplyTo(SM_REPLY_CHAT);
   183 		serverpluginhelpers->ClientCommand(engine->PEntityOfEntIndex(client), m_ToExecute);
   184 		SetReplyTo(old);
   185 	}
   186 }
   187 
   188 bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args, bool is_quoted)
   189 {
   190 	/* Eat up whitespace */
   191 	while (*args != '\0' && IsWhitespace(args))
   192 	{
   193 		args++;
   194 	}
   195 
   196 	/* Check if we're still valid */
   197 	if (*args == '\0')
   198 	{
   199 		return false;
   200 	}
   201 
   202 	/* Extract a command.  This is kind of sloppy. */
   203 	char cmd_buf[64];
   204 	size_t cmd_len = 0;
   205 	const char *inptr = args;
   206 	while (*inptr != '\0' 
   207 			&& !IsWhitespace(inptr) 
   208 			&& *inptr != '"'
   209 			&& cmd_len < sizeof(cmd_buf) - 1)
   210 	{
   211 		cmd_buf[cmd_len++] = *inptr++;
   212 	}
   213 	cmd_buf[cmd_len] = '\0';
   214 
   215 	/* See if we have this registered */
   216 	bool prepended = false;
   217 	if (!g_ConCmds.LookForSourceModCommand(cmd_buf))
   218 	{
   219 		/* Check if we had an "sm_" prefix */
   220 		if (strncmp(cmd_buf, "sm_", 3) == 0)
   221 		{
   222 			return false;
   223 		}
   224 
   225 		/* Now, prepend.  Don't worry about the buffers.  This will 
   226 		 * work because the sizes are limited from earlier.
   227 		 */
   228 		char new_buf[80];
   229 		strcpy(new_buf, "sm_");
   230 		strncopy(&new_buf[3], cmd_buf, sizeof(new_buf)-3);
   231 
   232 		/* Recheck */
   233 		if (!g_ConCmds.LookForSourceModCommand(new_buf))
   234 		{
   235 			return false;
   236 		}
   237 		
   238 		prepended = true;
   239 	}
   240 
   241 	/* See if we need to do extra string manipulation */
   242 	if (is_quoted || prepended)
   243 	{
   244 		size_t len;
   245 
   246 		/* Check if we need to prepend sm_ */
   247 		if (prepended)
   248 		{
   249 			len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
   250 		} else {
   251 			len = strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   252 		}
   253 		
   254 		/* Check if we need to strip a quote */
   255 		if (is_quoted)
   256 		{
   257 			if (m_ToExecute[len-1] == '"')
   258 			{
   259 				m_ToExecute[--len] = '\0';
   260 			}
   261 		}
   262 	} else {
   263 		strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   264 	}
   265 
   266 	return true;
   267 }
   268 
   269 unsigned int ChatTriggers::SetReplyTo(unsigned int reply)
   270 {
   271 	unsigned int old = m_ReplyTo;
   272 
   273 	m_ReplyTo = reply;
   274 
   275 	return old;
   276 }
   277 
   278 unsigned int ChatTriggers::GetReplyTo()
   279 {
   280 	return m_ReplyTo;
   281 }