]> 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 4f3c59e0c559ab335f2e978206808e78f21cf6c5..cde928f6bf9c0301b260d5fb25b1cf89b2dbfaca 100644 (file)
@@ -1,28 +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)
  *
- * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
- * der GNU General Public License (GPL), wie von der Free Software Foundation
- * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
- * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
- * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
- * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
- *
- * $Id: lists.c,v 1.9 2002/10/03 21:49:59 alex Exp $
- *
- * lists.c: Verwaltung der "IRC-Listen": Ban, Invite, ...
+ * 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.
  */
 
 
 #include "portab.h"
 
+/**
+ * @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"
 
 #include <stdlib.h>
 #include <string.h>
+#include <strings.h>
 
 #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;
-
+struct list_elem {
+       struct list_elem *next;
+       char mask[MASK_LEN];
+       bool onlyonce;
+};
 
-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 ));
-
-
-
-GLOBAL VOID
-Lists_Init( VOID )
-{
-       /* Modul initialisieren */
-
-       My_Invites = My_Bans = NULL;
-} /* Lists_Init */
-
-
-GLOBAL VOID
-Lists_Exit( VOID )
+GLOBAL const char *
+Lists_GetMask(const struct list_elem *e)
 {
-       /* Modul abmelden */
-
-       C2C *c2c, *next;
-
-       /* Invite-Lists freigeben */
-       c2c = My_Invites;
-       while( c2c )
-       {
-               next = c2c->next;
-               free( c2c );
-               c2c = next;
-       }
+       return e->mask;
+}
 
-       /* 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_GetFirst(const struct list_head *h)
 {
-       return Check_List( &My_Invites, Client, Chan );
-} /* Lists_CheckInvited */
+       return h->first;
+}
 
 
-GLOBAL BOOLEAN
-Lists_AddInvited( CLIENT *From, CHAR *Mask, CHANNEL *Chan, BOOLEAN OnlyOnce )
+GLOBAL struct list_elem*
+Lists_GetNext(const struct list_elem *e)
 {
-       C2C *c2c;
-
-       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 */
+       return e->next;
+}
 
 
-GLOBAL VOID
-Lists_DelInvited( CHAR *Mask, CHANNEL *Chan )
+bool
+Lists_Add(struct list_head *header, const char *Mask, bool OnlyOnce )
 {
-       C2C *c2c, *last, *next;
+       struct list_elem *e, *newelem;
 
+       assert( header != NULL );
        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;
-       }
-} /* Lists_DelInvited */
 
+       if (Lists_CheckDupeMask(header, Mask )) return true;
 
-GLOBAL BOOLEAN
-Lists_ShowInvites( CLIENT *Client, CHANNEL *Channel )
-{
-       C2C *c2c;
-
-       assert( Client != NULL );
-       assert( Channel != NULL );
+       e = Lists_GetFirst(header);
 
-       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;
+       newelem = malloc(sizeof(struct list_elem));
+       if( ! newelem ) {
+               Log( LOG_EMERG, "Can't allocate memory for new Ban/Invite entry!" );
+               return false;
        }
-       return IRC_WriteStrClient( Client, RPL_ENDOFINVITELIST_MSG, Client_ID( Client ), Channel_Name( Channel ));
-} /* Lists_ShowInvites */
 
+       strlcpy( newelem->mask, Mask, sizeof( newelem->mask ));
+       newelem->onlyonce = OnlyOnce;
+       newelem->next = e;
+       header->first = newelem;
 
-GLOBAL BOOLEAN
-Lists_CheckBanned( CLIENT *Client, CHANNEL *Chan )
-{
-       return Check_List( &My_Bans, Client, Chan );
-} /* Lists_CheckBanned */
+       return true;
+}
 
 
-GLOBAL BOOLEAN
-Lists_AddBanned( CLIENT *From, CHAR *Mask, CHANNEL *Chan )
+static void
+Lists_Unlink(struct list_head *header, struct list_elem *p, struct list_elem *victim)
 {
-       C2C *c2c;
-
-       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;
-       }
+       assert(victim != NULL);
+       assert(header != NULL);
 
-       /* verketten */
-       c2c->next = My_Bans;
-       My_Bans = c2c;
+       if (p) p->next = victim->next;
+       else header->first = victim->next;
 
-       Log( LOG_DEBUG, "Added \"%s\" to ban list for \"%s\".", Mask, Channel_Name( Chan ));
-       return TRUE;
-} /* Lists_AddBanned */
+       free(victim);
+}
 
 
-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;
-       }
-
-       /* 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;
+       struct list_elem *e;
+       e = h->first;
+       while (e) {
+               if (strcasecmp( e->mask, Mask ) == 0 )
+                       return true;
+               e = e->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,117 +181,60 @@ Lists_MakeMask( CHAR *Pattern )
 
        if(( ! at ) && ( ! excl ))
        {
-               /* weder ! noch @ĂŠvorhanden: als Nick annehmen */
-               strncpy( TheMask, Pattern, MASK_LEN - 5 );
-               TheMask[MASK_LEN - 5] = '\0';
-               strcat( TheMask, "!*@*" );
+               /* Neither "!" nor "@" found: use string as nick name */
+               strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
+               strlcat( TheMask, "!*@*", sizeof( TheMask ));
                return TheMask;
        }
 
        if(( ! at ) && ( excl ))
        {
-               /* Domain fehlt */
-               strncpy( TheMask, Pattern, MASK_LEN - 3 );
-               TheMask[MASK_LEN - 3] = '\0';
-               strcat( TheMask, "@*" );
+               /* Domain part is missing */
+               strlcpy( TheMask, Pattern, sizeof( TheMask ) - 3 );
+               strlcat( TheMask, "@*", sizeof( TheMask ));
                return TheMask;
        }
 
        if(( at ) && ( ! excl ))
        {
-               /* User fehlt */
+               /* User name is missing */
                *at = '\0'; at++;
-               strncpy( TheMask, Pattern, MASK_LEN - 4 );
-               TheMask[MASK_LEN - 4] = '\0';
-               strcat( TheMask, "!*@" );
-               strncat( TheMask, at, strlen( TheMask ) - MASK_LEN - 1 );
-               TheMask[MASK_LEN - 1] = '\0';
+               strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
+               strlcat( TheMask, "!*@", sizeof( TheMask ));
+               strlcat( TheMask, at, sizeof( TheMask ));
                return TheMask;
        }
 
-       /* alle Teile vorhanden */
-       strncpy( TheMask, Pattern, MASK_LEN - 1 );
-       TheMask[MASK_LEN - 1] = '\0';
+       /* 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;
-       }
-
-       strncpy( c2c->mask, Mask, MASK_LEN );
-       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- */