core/ChatTriggers.cpp
changeset 1103 043baf2d8c96
parent 935 287169a89759
child 1139 5e2ace825428
     1.1 --- a/core/ChatTriggers.cpp	Fri Jul 13 21:18:03 2007 +0000
     1.2 +++ b/core/ChatTriggers.cpp	Sat Jul 14 00:35:08 2007 +0000
     1.3 @@ -15,7 +15,7 @@
     1.4  ChatTriggers g_ChatTriggers;
     1.5  
     1.6  ChatTriggers::ChatTriggers() : m_pSayCmd(NULL), m_bWillProcessInPost(false), 
     1.7 -	m_ReplyTo(SM_REPLY_CONSOLE)
     1.8 +	m_bTriggerWasSilent(false), m_ReplyTo(SM_REPLY_CONSOLE)
     1.9  {
    1.10  	m_PubTrigger = sm_strdup("!");
    1.11  	m_PrivTrigger = sm_strdup("/");
    1.12 @@ -134,9 +134,11 @@
    1.13  	if (m_PubTriggerSize && strncmp(args, m_PubTrigger, m_PubTriggerSize) == 0)
    1.14  	{
    1.15  		is_trigger = true;
    1.16 +		args = &args[m_PubTriggerSize];
    1.17  	} else if (m_PrivTriggerSize && strncmp(args, m_PrivTrigger, m_PrivTriggerSize) == 0) {
    1.18  		is_trigger = true;
    1.19  		is_silent = true;
    1.20 +		args = &args[m_PrivTriggerSize];
    1.21  	}
    1.22  
    1.23  	if (!is_trigger)
    1.24 @@ -144,21 +146,27 @@
    1.25  		RETURN_META(MRES_IGNORED);
    1.26  	}
    1.27  
    1.28 -	/* If we're a public command, process later */
    1.29 -	if (!is_silent)
    1.30 +	/**
    1.31 +	 * Test if this is actually a command!
    1.32 +	 */
    1.33 +	if (!PreProcessTrigger(engine->PEntityOfEntIndex(client), args, is_quoted))
    1.34  	{
    1.35 -		/* We have to process this in _post_ instead.  Darn. */
    1.36 -		m_bWillProcessInPost = true;
    1.37  		RETURN_META(MRES_IGNORED);
    1.38  	}
    1.39  
    1.40 -	/* Otherwise, process now */
    1.41 -	if (ProcessTrigger(engine->PEntityOfEntIndex(client), &args[m_PrivTriggerSize], is_quoted))
    1.42 +	/**
    1.43 +	 * We'll execute it in post.
    1.44 +	 */
    1.45 +	m_bWillProcessInPost = true;
    1.46 +	m_bTriggerWasSilent = is_silent;
    1.47 +
    1.48 +	/* If we're silent, block */
    1.49 +	if (is_silent)
    1.50  	{
    1.51 -		/* If we succeed, block the original say! */
    1.52  		RETURN_META(MRES_SUPERCEDE);
    1.53  	}
    1.54  
    1.55 +	/* Otherwise, let the command continue */
    1.56  	RETURN_META(MRES_IGNORED);
    1.57  }
    1.58  
    1.59 @@ -168,25 +176,16 @@
    1.60  	{
    1.61  		/* Reset this for re-entrancy */
    1.62  		m_bWillProcessInPost = false;
    1.63 -
    1.64 -		/* Get our arguments */
    1.65 -		const char *args = engine->Cmd_Args();
    1.66 -
    1.67 -		/* Handle quotations */
    1.68 -		bool is_quoted = false;
    1.69 -		if (args[0] == '"')
    1.70 -		{
    1.71 -			args++;
    1.72 -			is_quoted = true;
    1.73 -		}
    1.74 -
    1.75 +		
    1.76 +		/* Execute the cached command */
    1.77  		int client = g_ConCmds.GetCommandClient();
    1.78 -
    1.79 -		ProcessTrigger(engine->PEntityOfEntIndex(client), &args[m_PubTriggerSize], is_quoted);
    1.80 +		unsigned int old = SetReplyTo(SM_REPLY_CHAT);
    1.81 +		serverpluginhelpers->ClientCommand(engine->PEntityOfEntIndex(client), m_ToExecute);
    1.82 +		SetReplyTo(old);
    1.83  	}
    1.84  }
    1.85  
    1.86 -bool ChatTriggers::ProcessTrigger(edict_t *pEdict, const char *args, bool is_quoted)
    1.87 +bool ChatTriggers::PreProcessTrigger(edict_t *pEdict, const char *args, bool is_quoted)
    1.88  {
    1.89  	/* Eat up whitespace */
    1.90  	while (*args != '\0' && IsWhitespace(args))
    1.91 @@ -242,34 +241,28 @@
    1.92  	/* See if we need to do extra string manipulation */
    1.93  	if (is_quoted || prepended)
    1.94  	{
    1.95 -		static char buffer[300];
    1.96  		size_t len;
    1.97  
    1.98  		/* Check if we need to prepend sm_ */
    1.99  		if (prepended)
   1.100  		{
   1.101 -			len = UTIL_Format(buffer, sizeof(buffer), "sm_%s", args);
   1.102 +			len = UTIL_Format(m_ToExecute, sizeof(m_ToExecute), "sm_%s", args);
   1.103  		} else {
   1.104 -			len = strncopy(buffer, args, sizeof(buffer));
   1.105 +			len = strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   1.106  		}
   1.107  		
   1.108  		/* Check if we need to strip a quote */
   1.109  		if (is_quoted)
   1.110  		{
   1.111 -			if (buffer[len-1] == '"')
   1.112 +			if (m_ToExecute[len-1] == '"')
   1.113  			{
   1.114 -				buffer[--len] = '\0';
   1.115 +				m_ToExecute[--len] = '\0';
   1.116  			}
   1.117  		}
   1.118 -
   1.119 -		args = buffer;
   1.120 +	} else {
   1.121 +		strncopy(m_ToExecute, args, sizeof(m_ToExecute));
   1.122  	}
   1.123  
   1.124 -	/* Finally, execute! */
   1.125 -	unsigned int old = SetReplyTo(SM_REPLY_CHAT);
   1.126 -	serverpluginhelpers->ClientCommand(pEdict, args);
   1.127 -	SetReplyTo(old);
   1.128 -
   1.129  	return true;
   1.130  }
   1.131