IRC_MODE(), Client_Mode(): code cleanup [1/2]

This commit is contained in:
Alexander Barton 2011-08-01 09:07:32 +02:00
parent 9f3690c39c
commit d692286d7a
1 changed files with 90 additions and 58 deletions

View File

@ -50,25 +50,39 @@ static bool Send_ListChange PARAMS((const char *Mode, CLIENT *Prefix,
CLIENT *Client, CHANNEL *Channel, const char *Mask)); CLIENT *Client, CHANNEL *Channel, const char *Mask));
/**
* Handler for the IRC "MODE" command.
*
* See RFC 2812 section 3.1.5 ("user mode message") and section 3.2.3
* ("channel mode message"), and RFC 2811 section 4 ("channel modes").
*
* @param Client The client from which this command has been received.
* @param Req Request structure with prefix and all parameters.
* @returns CONNECTED or DISCONNECTED.
*/
GLOBAL bool GLOBAL bool
IRC_MODE( CLIENT *Client, REQUEST *Req ) IRC_MODE( CLIENT *Client, REQUEST *Req )
{ {
CLIENT *cl, *origin; CLIENT *cl, *origin;
CHANNEL *chan; CHANNEL *chan;
assert( Client != NULL ); assert(Client != NULL);
assert( Req != NULL ); assert(Req != NULL);
/* No parameters? */ /* No parameters? */
if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command ); if (Req->argc < 1)
return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
Client_ID(Client), Req->command);
/* Origin for answers */ /* Origin for answers */
if( Client_Type( Client ) == CLIENT_SERVER ) if (Client_Type(Client) == CLIENT_SERVER) {
{ origin = Client_Search(Req->prefix);
origin = Client_Search( Req->prefix ); if (!origin)
if( ! origin ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix ); return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
} Client_ID(Client),
else origin = Client; Req->prefix);
} else
origin = Client;
/* Channel or user mode? */ /* Channel or user mode? */
cl = NULL; chan = NULL; cl = NULL; chan = NULL;
@ -88,49 +102,63 @@ IRC_MODE( CLIENT *Client, REQUEST *Req )
} /* IRC_MODE */ } /* IRC_MODE */
/**
* Handle client mode requests
*
* @param Client The client from which this command has been received.
* @param Req Request structure with prefix and all parameters.
* @param Origin The originator of the MODE command (prefix).
* @param Target The target (client) of this MODE command.
* @returns CONNECTED or DISCONNECTED.
*/
static bool static bool
Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target ) Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target )
{ {
/* Handle client mode requests */
char the_modes[COMMAND_LEN], x[2], *mode_ptr; char the_modes[COMMAND_LEN], x[2], *mode_ptr;
bool ok, set; bool ok, set;
int mode_arg; int mode_arg;
size_t len; size_t len;
/* Is the client allowed to request or change the modes? */ /* Is the client allowed to request or change the modes? */
if( Client_Type( Client ) == CLIENT_USER ) if (Client_Type(Client) == CLIENT_USER) {
{
/* Users are only allowed to manipulate their own modes! */ /* Users are only allowed to manipulate their own modes! */
if( Target != Client ) return IRC_WriteStrClient( Client, ERR_USERSDONTMATCH_MSG, Client_ID( Client )); if (Target != Client)
return IRC_WriteStrClient(Client,
ERR_USERSDONTMATCH_MSG,
Client_ID(Client));
} }
/* Mode request: let's answer it :-) */ /* Mode request: let's answer it :-) */
if( Req->argc == 1 ) return IRC_WriteStrClient( Origin, RPL_UMODEIS_MSG, Client_ID( Origin ), Client_Modes( Target )); if (Req->argc == 1)
return IRC_WriteStrClient(Origin, RPL_UMODEIS_MSG,
Client_ID(Origin),
Client_Modes(Target));
mode_arg = 1; mode_arg = 1;
mode_ptr = Req->argv[mode_arg]; mode_ptr = Req->argv[mode_arg];
/* Initial state: set or unset modes? */ /* Initial state: set or unset modes? */
if( *mode_ptr == '+' ) set = true; if (*mode_ptr == '+') {
else if( *mode_ptr == '-' ) set = false; set = true;
else return IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Origin )); strcpy(the_modes, "+");
} else if (*mode_ptr == '-') {
/* Prepare reply string */ set = false;
if( set ) strcpy( the_modes, "+" ); strcpy(the_modes, "-");
else strcpy( the_modes, "-" ); } else
return IRC_WriteStrClient(Origin, ERR_UMODEUNKNOWNFLAG_MSG,
Client_ID(Origin));
x[1] = '\0'; x[1] = '\0';
ok = CONNECTED; ok = CONNECTED;
while( mode_ptr ) while (mode_ptr) {
{
mode_ptr++; mode_ptr++;
if( ! *mode_ptr ) if (!*mode_ptr) {
{
/* Try next argument if there's any */ /* Try next argument if there's any */
mode_arg++; mode_arg++;
if( mode_arg < Req->argc ) mode_ptr = Req->argv[mode_arg]; if (mode_arg < Req->argc)
else break; mode_ptr = Req->argv[mode_arg];
else
break;
} }
switch( *mode_ptr ) switch( *mode_ptr )
@ -217,49 +245,53 @@ Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target )
x[0] = '\0'; x[0] = '\0';
goto client_exit; goto client_exit;
} }
if( ! ok ) break; if (!ok)
break;
/* Is there a valid mode change? */ /* Is there a valid mode change? */
if( ! x[0] ) continue; if (!x[0])
continue;
if( set ) if (set) {
{ if (Client_ModeAdd(Target, x[0]))
/* Set mode */ strlcat(the_modes, x, sizeof(the_modes));
if( Client_ModeAdd( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes )); } else {
if (Client_ModeDel(Target, x[0]))
} strlcat(the_modes, x, sizeof(the_modes));
else
{
/* Unset mode */
if( Client_ModeDel( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
} }
} }
client_exit: client_exit:
/* Are there changed modes? */ /* Are there changed modes? */
if( the_modes[1] ) if (the_modes[1]) {
{
/* Remoce needless action modifier characters */ /* Remoce needless action modifier characters */
len = strlen( the_modes ) - 1; len = strlen(the_modes) - 1;
if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' )) the_modes[len] = '\0'; if (the_modes[len] == '+' || the_modes[len] == '-')
the_modes[len] = '\0';
if( Client_Type( Client ) == CLIENT_SERVER ) if (Client_Type(Client) == CLIENT_SERVER) {
{
/* Forward modes to other servers */ /* Forward modes to other servers */
IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes ); IRC_WriteStrServersPrefix(Client, Origin,
} "MODE %s :%s",
else Client_ID(Target),
{ the_modes);
} else {
/* Send reply to client and inform other servers */ /* Send reply to client and inform other servers */
ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes ); ok = IRC_WriteStrClientPrefix(Client, Origin,
IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes ); "MODE %s :%s",
Client_ID(Target),
the_modes);
IRC_WriteStrServersPrefix(Client, Origin,
"MODE %s :%s",
Client_ID(Target),
the_modes);
} }
LogDebug("%s \"%s\": Mode change, now \"%s\".", LogDebug("%s \"%s\": Mode change, now \"%s\".",
Client_TypeText(Target), Client_Mask(Target), Client_TypeText(Target), Client_Mask(Target),
Client_Modes(Target)); Client_Modes(Target));
} }
IRC_SetPenalty( Client, 1 ); IRC_SetPenalty(Client, 1);
return ok; return ok;
} /* Client_Mode */ } /* Client_Mode */