]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/lists.c
Add Doxygen @file documentation to each source and header file
[ngircd-alex.git] / src / ngircd / lists.c
index cc0538e910707a330c7d1864bfe5b96a5d4970d4..cde928f6bf9c0301b260d5fb25b1cf89b2dbfaca 100644 (file)
@@ -1,27 +1,27 @@
 /*
  * ngIRCd -- The Next Generation IRC Daemon
- * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
+ * Copyright (c)2001-2005 Alexander Barton (alex@barton.de)
  *
  * 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.
- *
- * Management of IRC lists: ban, invite, ...
  */
 
 
 #include "portab.h"
 
-static char UNUSED id[] = "$Id: lists.c,v 1.12 2004/01/17 03:17:49 alex Exp $";
+/**
+ * @file
+ * Management of IRC lists: ban, invite, etc.
+ */
 
 #include "imp.h"
 #include <assert.h>
 
 #include "defines.h"
 #include "conn.h"
-#include "client.h"
 #include "channel.h"
 #include "log.h"
 #include "match.h"
@@ -35,293 +35,142 @@ static char UNUSED id[] = "$Id: lists.c,v 1.12 2004/01/17 03:17:49 alex Exp $";
 #include "exp.h"
 #include "lists.h"
 
+#define MASK_LEN       (2*CLIENT_HOST_LEN)
 
-#define MASK_LEN 2*CLIENT_HOST_LEN
-
-
-typedef struct _C2C
-{
-       struct _C2C *next;
-       CHAR mask[MASK_LEN];
-       CHANNEL *channel;
-       BOOLEAN onlyonce;
-} C2C;
-
-
-LOCAL C2C *My_Invites, *My_Bans;
-
-
-LOCAL C2C *New_C2C PARAMS(( CHAR *Mask, CHANNEL *Chan, BOOLEAN OnlyOnce ));
-
-LOCAL BOOLEAN Check_List PARAMS(( C2C **Cl2Chan, CLIENT *Client, CHANNEL *Chan ));
-LOCAL BOOLEAN Already_Registered PARAMS(( C2C *Cl2Chan, CHAR *Mask, CHANNEL *Chan ));
+struct list_elem {
+       struct list_elem *next;
+       char mask[MASK_LEN];
+       bool onlyonce;
+};
 
 
-
-GLOBAL VOID
-Lists_Init( VOID )
+GLOBAL const char *
+Lists_GetMask(const struct list_elem *e)
 {
-       /* Modul initialisieren */
-
-       My_Invites = My_Bans = NULL;
-} /* Lists_Init */
+       return e->mask;
+}
 
 
-GLOBAL VOID
-Lists_Exit( VOID )
+GLOBAL struct list_elem*
+Lists_GetFirst(const struct list_head *h)
 {
-       /* Modul abmelden */
-
-       C2C *c2c, *next;
-
-       /* Invite-Lists freigeben */
-       c2c = My_Invites;
-       while( c2c )
-       {
-               next = c2c->next;
-               free( c2c );
-               c2c = next;
-       }
+       return h->first;
+}
 
-       /* Ban-Lists freigeben */
-       c2c = My_Bans;
-       while( c2c )
-       {
-               next = c2c->next;
-               free( c2c );
-               c2c = next;
-       }
-} /* Lists_Exit */
 
-
-GLOBAL BOOLEAN
-Lists_CheckInvited( CLIENT *Client, CHANNEL *Chan )
+GLOBAL struct list_elem*
+Lists_GetNext(const struct list_elem *e)
 {
-       return Check_List( &My_Invites, Client, Chan );
-} /* Lists_CheckInvited */
+       return e->next;
+}
 
 
-GLOBAL BOOLEAN
-Lists_AddInvited( CLIENT *From, CHAR *Mask, CHANNEL *Chan, BOOLEAN OnlyOnce )
+bool
+Lists_Add(struct list_head *header, const char *Mask, bool OnlyOnce )
 {
-       C2C *c2c;
+       struct list_elem *e, *newelem;
 
+       assert( header != NULL );
        assert( Mask != NULL );
-       assert( Chan != NULL );
-
-       if( Already_Registered( My_Invites, Mask, Chan ))
-       {
-               /* Eintrag ist bereits vorhanden */
-               IRC_WriteStrClient( From, RPL_INVITELIST_MSG, Client_ID( From ), Channel_Name( Chan ), Mask );
-               return FALSE;
-       }
-       
-       c2c = New_C2C( Mask, Chan, OnlyOnce );
-       if( ! c2c )
-       {
-               Log( LOG_ERR, "Can't add new invite list entry!" );
-               return FALSE;
-       }
-
-       /* verketten */
-       c2c->next = My_Invites;
-       My_Invites = c2c;
 
-       Log( LOG_DEBUG, "Added \"%s\" to invite list for \"%s\".", Mask, Channel_Name( Chan ));
-       return TRUE;
-} /* Lists_AddInvited */
+       if (Lists_CheckDupeMask(header, Mask )) return true;
 
+       e = Lists_GetFirst(header);
 
-GLOBAL VOID
-Lists_DelInvited( CHAR *Mask, CHANNEL *Chan )
-{
-       C2C *c2c, *last, *next;
-
-       assert( Mask != NULL );
-       assert( Chan != NULL );
-
-       last = NULL;
-       c2c = My_Invites;
-       while( c2c )
-       {
-               next = c2c->next;
-               if(( c2c->channel == Chan ) && ( strcasecmp( c2c->mask, Mask ) == 0 ))
-               {
-                       /* dieser Eintrag muss geloescht werden */
-                       Log( LOG_DEBUG, "Deleted \"%s\" from invite list for \"%s\"." , c2c->mask, Channel_Name( Chan ));
-                       if( last ) last->next = next;
-                       else My_Invites = next;
-                       free( c2c );
-               }
-               else last = c2c;
-               c2c = next;
+       newelem = malloc(sizeof(struct list_elem));
+       if( ! newelem ) {
+               Log( LOG_EMERG, "Can't allocate memory for new Ban/Invite entry!" );
+               return false;
        }
-} /* Lists_DelInvited */
-
-
-GLOBAL BOOLEAN
-Lists_ShowInvites( CLIENT *Client, CHANNEL *Channel )
-{
-       C2C *c2c;
 
-       assert( Client != NULL );
-       assert( Channel != NULL );
+       strlcpy( newelem->mask, Mask, sizeof( newelem->mask ));
+       newelem->onlyonce = OnlyOnce;
+       newelem->next = e;
+       header->first = newelem;
 
-       c2c = My_Invites;
-       while( c2c )
-       {
-               if( c2c->channel == Channel )
-               {
-                       /* Eintrag fuer Channel gefunden; ausgeben: */
-                       if( ! IRC_WriteStrClient( Client, RPL_INVITELIST_MSG, Client_ID( Client ), Channel_Name( Channel ), c2c->mask )) return DISCONNECTED;
-               }
-               c2c = c2c->next;
-       }
-       return IRC_WriteStrClient( Client, RPL_ENDOFINVITELIST_MSG, Client_ID( Client ), Channel_Name( Channel ));
-} /* Lists_ShowInvites */
+       return true;
+}
 
 
-GLOBAL BOOLEAN
-Lists_CheckBanned( CLIENT *Client, CHANNEL *Chan )
+static void
+Lists_Unlink(struct list_head *header, struct list_elem *p, struct list_elem *victim)
 {
-       return Check_List( &My_Bans, Client, Chan );
-} /* Lists_CheckBanned */
+       assert(victim != NULL);
+       assert(header != NULL);
 
+       if (p) p->next = victim->next;
+       else header->first = victim->next;
 
-GLOBAL BOOLEAN
-Lists_AddBanned( CLIENT *From, CHAR *Mask, CHANNEL *Chan )
-{
-       C2C *c2c;
+       free(victim);
+}
 
-       assert( Mask != NULL );
-       assert( Chan != NULL );
 
-       if( Already_Registered( My_Bans, Mask, Chan ))
-       {
-               /* Eintrag ist bereits vorhanden */
-               IRC_WriteStrClient( From, RPL_BANLIST_MSG, Client_ID( From ), Channel_Name( Chan ), Mask );
-               return FALSE;
-       }
-
-       c2c = New_C2C( Mask, Chan, FALSE );
-       if( ! c2c )
-       {
-               Log( LOG_ERR, "Can't add new ban list entry!" );
-               return FALSE;
-       }
-
-       /* verketten */
-       c2c->next = My_Bans;
-       My_Bans = c2c;
-
-       Log( LOG_DEBUG, "Added \"%s\" to ban list for \"%s\".", Mask, Channel_Name( Chan ));
-       return TRUE;
-} /* Lists_AddBanned */
-
-
-GLOBAL VOID
-Lists_DelBanned( CHAR *Mask, CHANNEL *Chan )
+GLOBAL void
+Lists_Del(struct list_head *header, const char *Mask)
 {
-       C2C *c2c, *last, *next;
+       struct list_elem *e, *last, *victim;
 
+       assert( header != NULL );
        assert( Mask != NULL );
-       assert( Chan != NULL );
 
        last = NULL;
-       c2c = My_Bans;
-       while( c2c )
-       {
-               next = c2c->next;
-               if(( c2c->channel == Chan ) && ( strcasecmp( c2c->mask, Mask ) == 0 ))
-               {
-                       /* dieser Eintrag muss geloescht werden */
-                       Log( LOG_DEBUG, "Deleted \"%s\" from ban list for \"%s\"." , c2c->mask, Channel_Name( Chan ));
-                       if( last ) last->next = next;
-                       else My_Bans = next;
-                       free( c2c );
+       e = Lists_GetFirst(header);
+       while( e ) {
+               if(strcasecmp( e->mask, Mask ) == 0 ) {
+                       LogDebug("Deleted \"%s\" from list", e->mask);
+                       victim = e;
+                       e = victim->next;
+                       Lists_Unlink(header, last, victim);
+                       continue;
                }
-               else last = c2c;
-               c2c = next;
+               last = e;
+               e = e->next;
        }
-} /* Lists_DelBanned */
+}
 
 
-GLOBAL BOOLEAN
-Lists_ShowBans( CLIENT *Client, CHANNEL *Channel )
+GLOBAL void
+Lists_Free(struct list_head *head)
 {
-       C2C *c2c;
+       struct list_elem *e, *victim;
 
-       assert( Client != NULL );
-       assert( Channel != NULL );
+       assert(head != NULL);
 
-       c2c = My_Bans;
-       while( c2c )
-       {
-               if( c2c->channel == Channel )
-               {
-                       /* Eintrag fuer Channel gefunden; ausgeben: */
-                       if( ! IRC_WriteStrClient( Client, RPL_BANLIST_MSG, Client_ID( Client ), Channel_Name( Channel ), c2c->mask )) return DISCONNECTED;
-               }
-               c2c = c2c->next;
+       e = head->first;
+       head->first = NULL;
+       while (e) {
+               LogDebug("Deleted \"%s\" from invite list" , e->mask);
+               victim = e;
+               e = e->next;
+               free( victim );
        }
-       return IRC_WriteStrClient( Client, RPL_ENDOFBANLIST_MSG, Client_ID( Client ), Channel_Name( Channel ));
-} /* Lists_ShowBans */
+}
 
 
-GLOBAL VOID
-Lists_DeleteChannel( CHANNEL *Chan )
+GLOBAL bool
+Lists_CheckDupeMask(const struct list_head *h, const char *Mask )
 {
-       /* Channel wurde geloescht, Invite- und Ban-Lists aufraeumen */
-
-       C2C *c2c, *last, *next;
-
-       /* Invite-List */
-       last = NULL;
-       c2c = My_Invites;
-       while( c2c )
-       {
-               next = c2c->next;
-               if( c2c->channel == Chan )
-               {
-                       /* dieser Eintrag muss geloescht werden */
-                       Log( LOG_DEBUG, "Deleted \"%s\" from invite list for \"%s\"." , c2c->mask, Channel_Name( Chan ));
-                       if( last ) last->next = next;
-                       else My_Invites = next;
-                       free( c2c );
-               }
-               else last = c2c;
-               c2c = next;
+       struct list_elem *e;
+       e = h->first;
+       while (e) {
+               if (strcasecmp( e->mask, Mask ) == 0 )
+                       return true;
+               e = e->next;
        }
-
-       /* Ban-List */
-       last = NULL;
-       c2c = My_Bans;
-       while( c2c )
-       {
-               next = c2c->next;
-               if( c2c->channel == Chan )
-               {
-                       /* dieser Eintrag muss geloescht werden */
-                       Log( LOG_DEBUG, "Deleted \"%s\" from ban list for \"%s\"." , c2c->mask, Channel_Name( Chan ));
-                       if( last ) last->next = next;
-                       else My_Bans = next;
-                       free( c2c );
-               }
-               else last = c2c;
-               c2c = next;
-       }
-} /* Lists_DeleteChannel */
+       return false;
+}
 
 
-GLOBAL CHAR *
-Lists_MakeMask( CHAR *Pattern )
+GLOBAL const char *
+Lists_MakeMask(const char *Pattern)
 {
-       /* Hier wird aus einem "beliebigen" Pattern eine gueltige IRC-Mask erzeugt.
-        * Diese ist aber nur bis zum naechsten Aufruf von Lists_MakeMask() gueltig,
-        * da ein einziger globaler Puffer verwendet wird. ->Umkopieren!*/
+       /* This function generats a valid IRC mask of "any" string. This
+        * mask is only valid until the next call to Lists_MakeMask(),
+        * because a single global buffer is used. You have to copy the
+        * generated mask to some sane location yourself! */
 
-       STATIC CHAR TheMask[MASK_LEN];
-       CHAR *excl, *at;
+       static char TheMask[MASK_LEN];
+       char *excl, *at;
 
        assert( Pattern != NULL );
 
@@ -332,7 +181,7 @@ Lists_MakeMask( CHAR *Pattern )
 
        if(( ! at ) && ( ! excl ))
        {
-               /* weder ! noch @ vorhanden: als Nick annehmen */
+               /* Neither "!" nor "@" found: use string as nick name */
                strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
                strlcat( TheMask, "!*@*", sizeof( TheMask ));
                return TheMask;
@@ -340,7 +189,7 @@ Lists_MakeMask( CHAR *Pattern )
 
        if(( ! at ) && ( excl ))
        {
-               /* Domain fehlt */
+               /* Domain part is missing */
                strlcpy( TheMask, Pattern, sizeof( TheMask ) - 3 );
                strlcat( TheMask, "@*", sizeof( TheMask ));
                return TheMask;
@@ -348,96 +197,44 @@ Lists_MakeMask( CHAR *Pattern )
 
        if(( at ) && ( ! excl ))
        {
-               /* User fehlt */
+               /* User name is missing */
                *at = '\0'; at++;
-               strlcpy( TheMask, Pattern, sizeof( TheMask ) - strlen( at ) - 4 );
+               strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
                strlcat( TheMask, "!*@", sizeof( TheMask ));
                strlcat( TheMask, at, sizeof( TheMask ));
                return TheMask;
        }
 
-       /* alle Teile vorhanden */
+       /* All parts (nick, user and domain name) are given */
        strlcpy( TheMask, Pattern, sizeof( TheMask ));
        return TheMask;
 } /* Lists_MakeMask */
 
 
-LOCAL C2C *
-New_C2C( CHAR *Mask, CHANNEL *Chan, BOOLEAN OnlyOnce )
-{
-       C2C *c2c;
-       
-       assert( Mask != NULL );
-       assert( Chan != NULL );
-
-       /* Speicher fuer Eintrag anfordern */
-       c2c = malloc( sizeof( C2C ));
-       if( ! c2c )
-       {
-               Log( LOG_EMERG, "Can't allocate memory! [New_C2C]" );
-               return NULL;
-       }
-
-       strlcpy( c2c->mask, Mask, sizeof( c2c->mask ));
-       c2c->channel = Chan;
-       c2c->onlyonce = OnlyOnce;
-
-       return c2c;
-} /* New_C2C */
 
-
-LOCAL BOOLEAN
-Check_List( C2C **Cl2Chan, CLIENT *Client, CHANNEL *Chan )
+bool
+Lists_Check( struct list_head *header, CLIENT *Client)
 {
-       C2C *c2c, *last;
+       struct list_elem *e, *last;
 
-       assert( Cl2Chan != NULL );
-       assert( Client != NULL );
-       assert( Chan != NULL );
+       assert( header != NULL );
 
-       c2c = *Cl2Chan;
+       e = header->first;
        last = NULL;
 
-       while( c2c )
-       {
-               if( c2c->channel == Chan )
-               {
-                       /* Ok, richtiger Channel. Passt die Maske? */
-                       if( Match( c2c->mask, Client_Mask( Client )))
-                       {
-                               /* Treffer! */
-                               if( c2c->onlyonce )
-                               {
-                                       /* Eintrag loeschen */
-                                       Log( LOG_DEBUG, "Deleted \"%s\" from %s list for \"%s\".", c2c->mask, *Cl2Chan == My_Invites ? "invite" : "ban", Channel_Name( Chan ));
-                                       if( last ) last->next = c2c->next;
-                                       else *Cl2Chan = c2c->next;
-                                       free( c2c );
-                               }
-                               return TRUE;
+       while( e ) {
+               if( Match( e->mask, Client_Mask( Client ))) {
+                       if( e->onlyonce ) { /* delete entry */
+                               LogDebug("Deleted \"%s\" from list", e->mask);
+                               Lists_Unlink(header, last, e);
                        }
+                       return true;
                }
-               last = c2c;
-               c2c = c2c->next;
-       }
-
-       return FALSE;
-} /* Check_List */
-
-
-LOCAL BOOLEAN
-Already_Registered( C2C *List, CHAR *Mask, CHANNEL *Chan )
-{
-       C2C *c2c;
-
-       c2c = List;
-       while( c2c )
-       {
-               if(( c2c->channel == Chan ) && ( strcasecmp( c2c->mask, Mask ) == 0 )) return TRUE;
-               c2c = c2c->next;
+               last = e;
+               e = e->next;
        }
-       return FALSE;
-} /* Already_Registered */
 
+       return false;
+}
 
 /* -eof- */