]> arthur.barton.de Git - ngircd.git/blobdiff - src/ngircd/channel.c
cleaned up Channel_IsValidName (now uses strcspn())
[ngircd.git] / src / ngircd / channel.c
index 56029195a21c2bd02e1d1d03c4f3d30282542039..5e3b9a802dc1d4ad28a8d7e6753ea792066675ca 100644 (file)
 
 #include "portab.h"
 
-static char UNUSED id[] = "$Id: channel.c,v 1.48 2005/06/04 11:53:25 fw Exp $";
+static char UNUSED id[] = "$Id: channel.c,v 1.58 2006/10/02 21:55:49 fw Exp $";
 
 #include "imp.h"
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 #include <strings.h>
 
 #include "defines.h"
@@ -49,16 +50,16 @@ static char UNUSED id[] = "$Id: channel.c,v 1.48 2005/06/04 11:53:25 fw Exp $";
 #define REMOVE_KICK 2
 
 
-LOCAL CHANNEL *My_Channels;
-LOCAL CL2CHAN *My_Cl2Chan;
+static CHANNEL *My_Channels;
+static CL2CHAN *My_Cl2Chan;
 
 
-LOCAL CL2CHAN *Get_Cl2Chan PARAMS(( CHANNEL *Chan, CLIENT *Client ));
-LOCAL CL2CHAN *Add_Client PARAMS(( CHANNEL *Chan, CLIENT *Client ));
-LOCAL bool Remove_Client PARAMS(( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, char *Reason, bool InformServer ));
-LOCAL CL2CHAN *Get_First_Cl2Chan PARAMS(( CLIENT *Client, CHANNEL *Chan ));
-LOCAL CL2CHAN *Get_Next_Cl2Chan PARAMS(( CL2CHAN *Start, CLIENT *Client, CHANNEL *Chan ));
-LOCAL bool Delete_Channel PARAMS(( CHANNEL *Chan ));
+static CL2CHAN *Get_Cl2Chan PARAMS(( CHANNEL *Chan, CLIENT *Client ));
+static CL2CHAN *Add_Client PARAMS(( CHANNEL *Chan, CLIENT *Client ));
+static bool Remove_Client PARAMS(( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, char *Reason, bool InformServer ));
+static CL2CHAN *Get_First_Cl2Chan PARAMS(( CLIENT *Client, CHANNEL *Chan ));
+static CL2CHAN *Get_Next_Cl2Chan PARAMS(( CL2CHAN *Start, CLIENT *Client, CHANNEL *Chan ));
+static bool Delete_Channel PARAMS(( CHANNEL *Chan ));
 
 
 GLOBAL void
@@ -76,7 +77,7 @@ Channel_InitPredefined( void )
 
        CHANNEL *chan;
        char *c;
-       int i;
+       unsigned int i;
        
        for( i = 0; i < Conf_Channel_Count; i++ )
        {
@@ -87,6 +88,7 @@ Channel_InitPredefined( void )
                if( ! Channel_IsValidName( Conf_Channel[i].name ))
                {
                        Log( LOG_ERR, "Can't create pre-defined channel: invalid name: \"%s\"!", Conf_Channel[i].name );
+                       array_free(&Conf_Channel[i].topic);
                        continue;
                }
 
@@ -95,20 +97,29 @@ Channel_InitPredefined( void )
                if( chan )
                {
                        Log( LOG_INFO, "Can't create pre-defined channel \"%s\": name already in use.", Conf_Channel[i].name );
+                       array_free(&Conf_Channel[i].topic);
                        continue;
                }
-               
-               /* Channel anlegen */
-               chan = Channel_Create( Conf_Channel[i].name );
-               if( chan )
-               {
-                       Channel_ModeAdd( chan, 'P' );
-                       Channel_SetTopic( chan, Conf_Channel[i].topic );
+
+               /* Create channel */
+               chan = Channel_Create(Conf_Channel[i].name);
+               if (chan) {
+                       Channel_ModeAdd(chan, 'P');
+
+                       if (array_start(&Conf_Channel[i].topic) != NULL)
+                               Channel_SetTopic(chan, NULL,
+                                        array_start(&Conf_Channel[i].topic));
+                       array_free(&Conf_Channel[i].topic);
+
                        c = Conf_Channel[i].modes;
-                       while( *c ) Channel_ModeAdd( chan, *c++ );
-                       Log( LOG_INFO, "Created pre-defined channel \"%s\".", Conf_Channel[i].name );
+                       while (*c)
+                               Channel_ModeAdd(chan, *c++);
+
+                       Log(LOG_INFO, "Created pre-defined channel \"%s\".",
+                                                       Conf_Channel[i].name );
                }
-               else Log( LOG_ERR, "Can't create pre-defined channel \"%s\"!", Conf_Channel[i].name );
+               else Log(LOG_ERR, "Can't create pre-defined channel \"%s\"!",
+                                                       Conf_Channel[i].name );
        }
 } /* Channel_InitPredefined */
 
@@ -124,6 +135,7 @@ Channel_Exit( void )
        while( c )
        {
                c_next = c->next;
+               array_free(&c->topic);
                free( c );
                c = c_next;
        }
@@ -147,17 +159,13 @@ Channel_Join( CLIENT *Client, char *Name )
        assert( Client != NULL );
        assert( Name != NULL );
 
-       /* Valider Channel-Name? */
-       if( ! Channel_IsValidName( Name ))
-       {
+       if( ! Channel_IsValidName( Name )) {
                IRC_WriteStrClient( Client, ERR_NOSUCHCHANNEL_MSG, Client_ID( Client ), Name );
                return false;
        }
 
-       /* Channel suchen */
        chan = Channel_Search( Name );
-       if( chan )
-       {
+       if( chan ) {
                /* Ist der Client bereits Mitglied? */
                if( Get_Cl2Chan( chan, Client )) return false;
        }
@@ -165,7 +173,7 @@ Channel_Join( CLIENT *Client, char *Name )
        {
                /* Gibt es noch nicht? Dann neu anlegen: */
                chan = Channel_Create( Name );
-               if( ! chan ) return false;
+               if (!chan) return false;
        }
 
        /* User dem Channel hinzufuegen */
@@ -183,7 +191,6 @@ Channel_Part( CLIENT *Client, CLIENT *Origin, char *Name, char *Reason )
        assert( Name != NULL );
        assert( Reason != NULL );
 
-       /* Channel suchen */
        chan = Channel_Search( Name );
        if(( ! chan ) || ( ! Get_Cl2Chan( chan, Client )))
        {
@@ -207,7 +214,6 @@ Channel_Kick( CLIENT *Client, CLIENT *Origin, char *Name, char *Reason )
        assert( Name != NULL );
        assert( Reason != NULL );
 
-       /* Channel suchen */
        chan = Channel_Search( Name );
        if( ! chan )
        {
@@ -215,21 +221,20 @@ Channel_Kick( CLIENT *Client, CLIENT *Origin, char *Name, char *Reason )
                return;
        }
 
-       /* Ist der User Mitglied in dem Channel? */
        if( ! Channel_IsMemberOf( chan, Origin ))
        {
                IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Name );
                return;
        }
 
-       /* Ist der User Channel-Operator? */
+       /* Is User Channel-Operator? */
        if( ! strchr( Channel_UserModes( chan, Origin ), 'o' ))
        {
                IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Name);
                return;
        }
 
-       /* Ist der Ziel-User Mitglied im Channel? */
+       /* Ist the kickED User member of channel? */
        if( ! Channel_IsMemberOf( chan, Client ))
        {
                IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( Client ), Name );
@@ -457,25 +462,13 @@ Channel_GetChannel( CL2CHAN *Cl2Chan )
 
 
 GLOBAL bool
-Channel_IsValidName( char *Name )
+Channel_IsValidName( const char *Name )
 {
-       /* Pruefen, ob Name als Channelname gueltig */
-
-       char *ptr, badchars[10];
-       
        assert( Name != NULL );
 
        if(( Name[0] != '#' ) || ( strlen( Name ) >= CHANNEL_NAME_LEN )) return false;
 
-       ptr = Name;
-       strcpy( badchars, " ,:\007" );
-       while( *ptr )
-       {
-               if( strchr( badchars, *ptr )) return false;
-               ptr++;
-       }
-       
-       return true;
+       return Name[strcspn(Name, " ,:\007")] == 0;
 } /* Channel_IsValidName */
 
 
@@ -509,13 +502,11 @@ Channel_ModeDel( CHANNEL *Chan, char Mode )
         * if the mode was removed return true.
         * if the channel did not have the mode, return false.
        */
-       char x[2], *p;
+       char *p;
 
        assert( Chan != NULL );
 
-       x[0] = Mode; x[1] = '\0';
-
-       p = strchr( Chan->modes, x[0] );
+       p = strchr( Chan->modes, Mode );
        if( ! p ) return false;
 
        /* Channel has mode -> delete */
@@ -544,7 +535,7 @@ Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
 
        cl2chan = Get_Cl2Chan( Chan, Client );
        assert( cl2chan != NULL );
-       
+
        x[0] = Mode; x[1] = '\0';
        if( ! strchr( cl2chan->modes, x[0] ))
        {
@@ -565,7 +556,7 @@ Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
         */
 
        CL2CHAN *cl2chan;
-       char x[2], *p;
+       char *p;
 
        assert( Chan != NULL );
        assert( Client != NULL );
@@ -573,9 +564,7 @@ Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
        cl2chan = Get_Cl2Chan( Chan, Client );
        assert( cl2chan != NULL );
 
-       x[0] = Mode; x[1] = '\0';
-
-       p = strchr( cl2chan->modes, x[0] );
+       p = strchr( cl2chan->modes, Mode );
        if( ! p ) return false;
 
        /* Client has Mode -> delete */
@@ -592,7 +581,7 @@ GLOBAL char *
 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
 {
        /* return Users' Channel-Modes */
-       
+
        CL2CHAN *cl2chan;
 
        assert( Chan != NULL );
@@ -621,18 +610,58 @@ Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
 GLOBAL char *
 Channel_Topic( CHANNEL *Chan )
 {
+       char *ret;
        assert( Chan != NULL );
-       return Chan->topic;
+       ret = array_start(&Chan->topic);
+       return ret ? ret : "";
 } /* Channel_Topic */
 
+       
+#ifndef STRICT_RFC
+
+GLOBAL unsigned int
+Channel_TopicTime(CHANNEL *Chan)
+{
+       assert(Chan != NULL);
+       return (unsigned int) Chan->topic_time;
+} /* Channel_TopicTime */
+
+
+GLOBAL char *
+Channel_TopicWho(CHANNEL *Chan)
+{
+       assert(Chan != NULL);
+       return Chan->topic_who;
+} /* Channel_TopicWho */
+
+#endif
+
 
 GLOBAL void
-Channel_SetTopic( CHANNEL *Chan, char *Topic )
+Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, char *Topic)
 {
+       size_t len;
        assert( Chan != NULL );
        assert( Topic != NULL );
-       
-       strlcpy( Chan->topic, Topic, sizeof( Chan->topic ));
+
+       len = strlen(Topic);
+       if (len < array_bytes(&Chan->topic))
+               array_free(&Chan->topic);
+
+       if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
+               Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
+                                       Topic, Chan->name, strerror(errno));
+#ifndef STRICT_RFC
+       Chan->topic_time = time(NULL);
+       if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
+               strlcpy(Chan->topic_who, Client_ID(Client),
+                       sizeof Chan->topic_who);
+       else
+               strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
+                       sizeof Chan->topic_who);
+#else
+       (void) Client;
+#endif
 } /* Channel_SetTopic */
 
 
@@ -653,7 +682,7 @@ Channel_SetKey( CHANNEL *Chan, char *Key )
        assert( Key != NULL );
 
        strlcpy( Chan->key, Key, sizeof( Chan->key ));
-       Log( LOG_DEBUG, "Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
+       LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
 } /* Channel_SetKey */
 
 
@@ -663,7 +692,7 @@ Channel_SetMaxUsers( CHANNEL *Chan, long Count )
        assert( Chan != NULL );
 
        Chan->maxusers = Count;
-       Log( LOG_DEBUG, "Channel %s: Member limit is now %ld.", Chan->name, Chan->maxusers );
+       LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
 } /* Channel_SetMaxUsers */
 
 
@@ -689,7 +718,7 @@ Channel_Write( CHANNEL *Chan, CLIENT *From, CLIENT *Client, char *Text )
        /* Is the client banned? */
        if( Lists_CheckBanned( From, Chan ))
        {
-               /* Client is banned, bus is he channel operator or has voice? */
+               /* Client is banned, but is he channel operator or has voice? */
                if(( ! has_voice ) && ( ! is_op )) ok = false;
        }
 
@@ -708,7 +737,7 @@ Channel_Create( char *Name )
        CHANNEL *c;
 
        assert( Name != NULL );
-       
+
        c = (CHANNEL *)malloc( sizeof( CHANNEL ));
        if( ! c )
        {
@@ -720,14 +749,12 @@ Channel_Create( char *Name )
        c->hash = Hash( c->name );
        c->next = My_Channels;
        My_Channels = c;
-       
-       Log( LOG_DEBUG, "Created new channel structure for \"%s\".", Name );
-       
+       LogDebug("Created new channel structure for \"%s\".", Name);
        return c;
 } /* Channel_Create */
 
 
-LOCAL CL2CHAN *
+static CL2CHAN *
 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
 {
        CL2CHAN *cl2chan;
@@ -745,7 +772,7 @@ Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
 } /* Get_Cl2Chan */
 
 
-LOCAL CL2CHAN *
+static CL2CHAN *
 Add_Client( CHANNEL *Chan, CLIENT *Client )
 {
        CL2CHAN *cl2chan;
@@ -774,7 +801,7 @@ Add_Client( CHANNEL *Chan, CLIENT *Client )
 } /* Add_Client */
 
 
-LOCAL bool
+static bool
 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, char *Reason, bool InformServer )
 {
        CL2CHAN *cl2chan, *last_cl2chan;
@@ -806,25 +833,42 @@ Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, char *Re
        switch( Type )
        {
                case REMOVE_QUIT:
-                       /* QUIT: andere Server wurden bereits informiert, vgl. Client_Destroy();
-                        * hier also "nur" noch alle User in betroffenen Channeln infomieren */
+                       /* QUIT: other servers have already been notified, see Client_Destroy();
+                        * so only inform other clients in same channel. */
                        assert( InformServer == false );
-                       Log( LOG_DEBUG, "User \"%s\" left channel \"%s\" (%s).", Client_Mask( Client ), c->name, Reason );
+                       LogDebug("User \"%s\" left channel \"%s\" (%s).",
+                                       Client_Mask( Client ), c->name, Reason );
                        break;
                case REMOVE_KICK:
-                       /* User wurde geKICKed: ggf. andere Server sowie alle betroffenen User
-                        * im entsprechenden Channel informieren */
-                       if( InformServer ) IRC_WriteStrServersPrefix( Client_NextHop( Origin ), Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason );
-                       IRC_WriteStrChannelPrefix( Client, c, Origin, false, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason );
-                       if(( Client_Conn( Client ) > NONE ) && ( Client_Type( Client ) == CLIENT_USER )) IRC_WriteStrClientPrefix( Client, Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason );
-                       Log( LOG_DEBUG, "User \"%s\" has been kicked of \"%s\" by \"%s\": %s.", Client_Mask( Client ), c->name, Client_ID( Origin ), Reason );
+                       /* User was KICKed: inform other servers and all users in channel */
+                       if( InformServer )
+                               IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
+                                       Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
+                       IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
+                                                       c->name, Client_ID( Client ), Reason );
+                       if ((Client_Conn(Client) > NONE) &&
+                                       (Client_Type(Client) == CLIENT_USER))
+                       {
+                               IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
+                                                               c->name, Client_ID( Client ), Reason);
+                       }
+                       LogDebug("User \"%s\" has been kicked of \"%s\" by \"%s\": %s.",
+                               Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
                        break;
-               default:
-                       /* PART */
-                       if( InformServer ) IRC_WriteStrServersPrefix( Origin, Client, "PART %s :%s", c->name, Reason );
-                       IRC_WriteStrChannelPrefix( Origin, c, Client, false, "PART %s :%s", c->name, Reason );
-                       if(( Client_Conn( Origin ) > NONE ) && ( Client_Type( Origin ) == CLIENT_USER )) IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason );
-                       Log( LOG_DEBUG, "User \"%s\" left channel \"%s\" (%s).", Client_Mask( Client ), c->name, Reason );
+               default: /* PART */
+                       if (InformServer)
+                               IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
+
+                       IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
+                                                                       c->name, Reason);
+
+                       if ((Client_Conn(Origin) > NONE) &&
+                                       (Client_Type(Origin) == CLIENT_USER))
+                       {
+                               IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
+                               LogDebug("User \"%s\" left channel \"%s\" (%s).",
+                                       Client_Mask(Client), c->name, Reason);
+                       }
        }
 
        /* Wenn Channel nun leer und nicht pre-defined: loeschen */
@@ -837,14 +881,14 @@ Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, char *Re
 } /* Remove_Client */
 
 
-LOCAL CL2CHAN *
+static CL2CHAN *
 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
 {
        return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
 } /* Get_First_Cl2Chan */
 
 
-LOCAL CL2CHAN *
+static CL2CHAN *
 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
 {
        CL2CHAN *cl2chan;
@@ -862,7 +906,7 @@ Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
 } /* Get_Next_Cl2Chan */
 
 
-LOCAL bool
+static bool
 Delete_Channel( CHANNEL *Chan )
 {
        /* Channel-Struktur loeschen */