]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/irc-mode.c
Enlarge client user mode buffer, reduce client flags buffer
[ngircd-alex.git] / src / ngircd / irc-mode.c
index a4c1d89b330ed175bb02ea158c595693a22be11e..1222bc5cd67a357e2452c122856bbfbf02c33221 100644 (file)
@@ -1,19 +1,21 @@
 /*
  * ngIRCd -- The Next Generation IRC Daemon
- * Copyright (c)2001-2010 Alexander Barton (alex@barton.de)
+ * Copyright (c)2001-2011 Alexander Barton (alex@barton.de) and Contributors.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
  * Please read the file COPYING, README and AUTHORS for more information.
- *
- * IRC commands for mode changes (MODE, AWAY, ...)
  */
 
-
 #include "portab.h"
 
+/**
+ * @file
+ * IRC commands for mode changes (like MODE, AWAY, etc.)
+ */
+
 #include "imp.h"
 #include <assert.h>
 #include <stdio.h>
@@ -48,25 +50,39 @@ static bool Send_ListChange PARAMS((const char *Mode, CLIENT *Prefix,
        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
 IRC_MODE( CLIENT *Client, REQUEST *Req )
 {
        CLIENT *cl, *origin;
        CHANNEL *chan;
 
-       assert( Client != NULL );
-       assert( Req != NULL );
+       assert(Client != NULL);
+       assert(Req != NULL);
 
        /* 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 */
-       if( Client_Type( Client ) == CLIENT_SERVER )
-       {
-               origin = Client_Search( Req->prefix );
-               if( ! origin ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
-       }
-       else origin = Client;
+       if (Client_Type(Client) == CLIENT_SERVER) {
+               origin = Client_Search(Req->prefix);
+               if (!origin)
+                       return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
+                                                 Client_ID(Client),
+                                                 Req->prefix);
+       } else
+               origin = Client;
 
        /* Channel or user mode? */
        cl = NULL; chan = NULL;
@@ -86,51 +102,65 @@ IRC_MODE( CLIENT *Client, REQUEST *Req )
 } /* 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
 Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target )
 {
-       /* Handle client mode requests */
-
        char the_modes[COMMAND_LEN], x[2], *mode_ptr;
        bool ok, set;
        int mode_arg;
        size_t len;
 
        /* 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! */
-               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 :-) */
-       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_ptr = Req->argv[mode_arg];
 
        /* Initial state: set or unset modes? */
-       if( *mode_ptr == '+' ) set = true;
-       else if( *mode_ptr == '-' ) set = false;
-       else return IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Origin ));
-
-       /* Prepare reply string */
-       if( set ) strcpy( the_modes, "+" );
-       else strcpy( the_modes, "-" );
+       if (*mode_ptr == '+') {
+               set = true;
+               strcpy(the_modes, "+");
+       } else if (*mode_ptr == '-') {
+               set = false;
+               strcpy(the_modes, "-");
+       } else
+               return IRC_WriteStrClient(Origin, ERR_UMODEUNKNOWNFLAG_MSG,
+                                         Client_ID(Origin));
 
        x[1] = '\0';
        ok = CONNECTED;
-       while( mode_ptr )
-       {
+       while (mode_ptr) {
                mode_ptr++;
-               if( ! *mode_ptr )
-               {
+               if (!*mode_ptr) {
                        /* Try next argument if there's any */
                        mode_arg++;
-                       if( mode_arg < Req->argc ) mode_ptr = Req->argv[mode_arg];
-                       else break;
+                       if (mode_arg < Req->argc)
+                               mode_ptr = Req->argv[mode_arg];
+                       else
+                               break;
                }
-               
+
                switch( *mode_ptr )
                {
                        case '+':
@@ -202,7 +232,7 @@ Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target )
 
                        case 'x': /* Cloak hostname */
                                if (Client_HasMode(Client, 'r'))
-                                       IRC_WriteStrClient(Origin,
+                                       ok = IRC_WriteStrClient(Origin,
                                                           ERR_RESTRICTED_MSG,
                                                           Client_ID(Origin));
                                else
@@ -215,49 +245,61 @@ Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target )
                                x[0] = '\0';
                                goto client_exit;
                }
-               if( ! ok ) break;
+               if (!ok)
+                       break;
 
                /* Is there a valid mode change? */
-               if( ! x[0] ) continue;
-
-               if( set )
-               {
-                       /* Set mode */
-                       if( Client_ModeAdd( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
+               if (!x[0])
+                       continue;
 
+               if (set) {
+                       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:
-       
+
        /* Are there changed modes? */
-       if( the_modes[1] )
-       {
+       if (the_modes[1]) {
                /* Remoce needless action modifier characters */
-               len = strlen( the_modes ) - 1;
-               if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' )) the_modes[len] = '\0';
+               len = strlen(the_modes) - 1;
+               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 */
-                       IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
-               }
-               else
-               {
+                       if (Client_Conn(Target) != NONE) {
+                               /* Remote server (service?) changed modes
+                                * for one of our clients. Inform it! */
+                               IRC_WriteStrClientPrefix(Target, Origin,
+                                                        "MODE %s :%s",
+                                                        Client_ID(Target),
+                                                        the_modes);
+                       }
+                       IRC_WriteStrServersPrefix(Client, Origin,
+                                                 "MODE %s :%s",
+                                                 Client_ID(Target),
+                                                 the_modes);
+               } else {
                        /* Send reply to client and inform other servers */
-                       ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
-                       IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
+                       ok = IRC_WriteStrClientPrefix(Client, Origin,
+                                                     "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\".",
                         Client_TypeText(Target), Client_Mask(Target),
                         Client_Modes(Target));
        }
-       
-       IRC_SetPenalty( Client, 1 );    
+
+       IRC_SetPenalty(Client, 1);
        return ok;
 } /* Client_Mode */
 
@@ -316,8 +358,7 @@ Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
 {
        char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
            argadd[CLIENT_PASS_LEN], *mode_ptr;
-       bool connected, set, skiponce, retval, onchannel;
-       bool modeok = true, use_servermode = false;
+       bool connected, set, skiponce, retval, onchannel, modeok, use_servermode;
        int mode_arg, arg_arg;
        CLIENT *client;
        long l;
@@ -331,28 +372,13 @@ Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
        if (Req->argc <= 1)
                return Channel_Mode_Answer_Request(Origin, Channel);
 
-       /* Is the user allowed to change modes? */
-       if (Client_Type(Client) == CLIENT_USER) {
-               /* Is the originating user on that channel? */
-               onchannel = Channel_IsMemberOf(Channel, Origin);
-               modeok = false;
-               /* channel operator? */
-               if (onchannel &&
-                   strchr(Channel_UserModes(Channel, Origin), 'o')) {
-                       modeok = true;
-               } else if (Conf_OperCanMode) {
-                       /* IRC-Operators can use MODE as well */
-                       if (Client_OperByMe(Origin)) {
-                               modeok = true;
-                               if (Conf_OperServerMode)
-                                       use_servermode = true; /* Change Origin to Server */
-                       }
-               }
+       Channel_CheckAdminRights(Channel, Client, Origin,
+                                &onchannel, &modeok, &use_servermode);
 
-               if (!onchannel && !modeok)
-                       return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
-                               Client_ID(Origin), Channel_Name(Channel));
-       }
+       if (!onchannel && !modeok)
+               return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
+                                         Client_ID(Origin),
+                                         Channel_Name(Channel));
 
        mode_arg = 1;
        mode_ptr = Req->argv[mode_arg];
@@ -515,6 +541,23 @@ Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
                                goto chan_exit;
                        }
                        break;
+               case 'O': /* IRC operators only */
+                       if (modeok) {
+                               /* Only IRC operators are allowed to
+                                * set the 'O' channel mode! */
+                               if (set && !(Client_OperByMe(Client)
+                                   || Client_Type(Client) == CLIENT_SERVER))
+                                       connected = IRC_WriteStrClient(Origin,
+                                               ERR_NOPRIVILEGES_MSG,
+                                               Client_ID(Origin));
+                               else
+                                       x[0] = 'O';
+                       } else
+                               connected = IRC_WriteStrClient(Origin,
+                                               ERR_CHANOPRIVSNEEDED_MSG,
+                                               Client_ID(Origin),
+                                               Channel_Name(Channel));
+                               break;
                case 'P': /* Persistent channel */
                        if (modeok) {
                                /* Only IRC operators are allowed to