X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=ngircd-alex.git;a=blobdiff_plain;f=src%2Fngircd%2Flists.c;h=b0accd41edb6476a6d85e27567618b65402906dd;hp=e16cdd7087ad1e3f1e0a23e7853e706cc1b43bc9;hb=0fc822d8c44be42a62d3c26bbab99d5d0bc88346;hpb=8adff5922376676c2eeb49de1cbab86cc345b887 diff --git a/src/ngircd/lists.c b/src/ngircd/lists.c index e16cdd70..b0accd41 100644 --- a/src/ngircd/lists.c +++ b/src/ngircd/lists.c @@ -1,27 +1,26 @@ /* * ngIRCd -- The Next Generation IRC Daemon - * Copyright (c)2001-2005 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. - * - * Management of IRC lists: ban, invite, ... */ - #include "portab.h" -static char UNUSED id[] = "$Id: lists.c,v 1.17 2005/03/19 18:43:49 fw Exp $"; +/** + * @file + * Management of IRC lists: ban, invite, etc. + */ #include "imp.h" #include #include "defines.h" #include "conn.h" -#include "client.h" #include "channel.h" #include "log.h" #include "match.h" @@ -35,454 +34,379 @@ static char UNUSED id[] = "$Id: lists.c,v 1.17 2005/03/19 18:43:49 fw Exp $"; #include "exp.h" #include "lists.h" +struct list_elem { + struct list_elem *next; /** pointer to next list element */ + char mask[MASK_LEN]; /** IRC mask */ + char *reason; /** Optional "reason" text */ + time_t valid_until; /** 0: unlimited; 1: once; t(>1): until t */ +}; -#define MASK_LEN 2*CLIENT_HOST_LEN - - -typedef struct _C2C -{ - struct _C2C *next; - char mask[MASK_LEN]; - CHANNEL *channel; - bool onlyonce; -} C2C; - - -LOCAL C2C *My_Invites, *My_Bans; - - -LOCAL C2C *New_C2C PARAMS(( char *Mask, CHANNEL *Chan, bool OnlyOnce )); - -LOCAL bool Check_List PARAMS(( C2C **Cl2Chan, CLIENT *Client, CHANNEL *Chan )); -LOCAL bool 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 ) +/** + * Get IRC mask stored in list element. + * + * @param list_elem List element. + * @return Pointer to IRC mask + */ +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; - } - - /* Ban-Lists freigeben */ - c2c = My_Bans; - while( c2c ) - { - next = c2c->next; - free( c2c ); - c2c = next; - } -} /* Lists_Exit */ - + assert(e != NULL); + return e->mask; +} -GLOBAL bool -Lists_CheckInvited( CLIENT *Client, CHANNEL *Chan ) +/** + * Get optional "reason" text stored in list element. + * + * @param list_elem List element. + * @return Pointer to "reason" text or empty string (""). + */ +GLOBAL const char * +Lists_GetReason(const struct list_elem *e) { - return Check_List( &My_Invites, Client, Chan ); -} /* Lists_CheckInvited */ + assert(e != NULL); + return e->reason ? e->reason : ""; +} - -GLOBAL bool -Lists_IsInviteEntry( char *Mask, CHANNEL *Chan ) +/** + * Get "validity" value stored in list element. + * + * @param list_elem List element. + * @return Validity: 0=unlimited, 1=once, >1 until this time stamp. + */ +GLOBAL time_t +Lists_GetValidity(const struct list_elem *e) { - assert( Mask != NULL ); - assert( Chan != NULL ); - - return Already_Registered( My_Invites, Mask, Chan ); -} /* Lists_IsInviteEntry */ - + assert(e != NULL); + return e->valid_until; +} -GLOBAL bool -Lists_AddInvited( char *Mask, CHANNEL *Chan, bool OnlyOnce ) +/** + * Get first list element of a list. + * + * @param h List head. + * @return Pointer to first list element. + */ +GLOBAL struct list_elem* +Lists_GetFirst(const struct list_head *h) { - C2C *c2c; - - assert( Mask != NULL ); - assert( Chan != NULL ); - - if( Already_Registered( My_Invites, Mask, Chan )) return true; - - c2c = New_C2C( Mask, Chan, OnlyOnce ); - if( ! c2c ) - { - Log( LOG_ERR, "Can't add new invite list entry!" ); - return false; - } + assert(h != NULL); + return h->first; +} - /* 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 */ - - -GLOBAL void -Lists_DelInvited( char *Mask, CHANNEL *Chan ) +/** + * Get next list element of a list. + * + * @param e Current list element. + * @return Pointer to next list element. + */ +GLOBAL struct list_elem* +Lists_GetNext(const struct list_elem *e) { - C2C *c2c, *last, *next; - - assert( Mask != NULL ); - assert( Chan != NULL ); + assert(e != NULL); + return e->next; +} - 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 */ - - -GLOBAL bool -Lists_ShowInvites( CLIENT *Client, CHANNEL *Channel ) +/** + * Add a new mask to a list. + * + * @param h List head. + * @param Mask The IRC mask to add to the list. + * @param ValidUntil 0: unlimited, 1: only once, t>1: until given time_t. + * @param Reason Reason string or NULL, if no reason should be saved. + * @return true on success, false otherwise. + */ +bool +Lists_Add(struct list_head *h, const char *Mask, time_t ValidUntil, + const char *Reason) { - C2C *c2c; - - assert( Client != NULL ); - assert( Channel != NULL ); - - 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; + struct list_elem *e, *newelem; + + assert(h != NULL); + assert(Mask != NULL); + + e = Lists_CheckDupeMask(h, Mask); + if (e) { + e->valid_until = ValidUntil; + if (Reason) { + if (e->reason) + free(e->reason); + e->reason = strdup(Reason); } - c2c = c2c->next; + return true; } - return IRC_WriteStrClient( Client, RPL_ENDOFINVITELIST_MSG, Client_ID( Client ), Channel_Name( Channel )); -} /* Lists_ShowInvites */ + e = Lists_GetFirst(h); -GLOBAL bool -Lists_SendInvites( CLIENT *Client ) -{ - C2C *c2c; - - assert( Client != NULL ); - - c2c = My_Invites; - while( c2c ) - { - if( ! IRC_WriteStrClient( Client, "MODE %s +I %s", Channel_Name( c2c->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 list entry!"); + return false; } - return CONNECTED; -} /* Lists_SendInvites */ - -GLOBAL bool -Lists_SendBans( CLIENT *Client ) -{ - C2C *c2c; - - assert( Client != NULL ); - - c2c = My_Bans; - while( c2c ) - { - if( ! IRC_WriteStrClient( Client, "MODE %s +b %s", Channel_Name( c2c->channel ), c2c->mask )) return DISCONNECTED; - c2c = c2c->next; + strlcpy(newelem->mask, Mask, sizeof(newelem->mask)); + if (Reason) { + newelem->reason = strdup(Reason); + if (!newelem->reason) + Log(LOG_EMERG, + "Can't allocate memory for new list reason text!"); } - return CONNECTED; -} /* Lists_SendBans */ - - -GLOBAL bool -Lists_CheckBanned( CLIENT *Client, CHANNEL *Chan ) -{ - return Check_List( &My_Bans, Client, Chan ); -} /* Lists_CheckBanned */ - - -GLOBAL bool -Lists_IsBanEntry( char *Mask, CHANNEL *Chan ) -{ - assert( Mask != NULL ); - assert( Chan != NULL ); - - return Already_Registered( My_Bans, Mask, Chan ); -} /* Lists_IsBanEntry */ + else + newelem->reason = NULL; + newelem->valid_until = ValidUntil; + newelem->next = e; + h->first = newelem; + return true; +} -GLOBAL bool -Lists_AddBanned( char *Mask, CHANNEL *Chan ) +/** + * Delete a list element from a list. + * + * @param h List head. + * @param p Pointer to previous list element or NULL, if there is none. + * @param victim List element to delete. + */ +static void +Lists_Unlink(struct list_head *h, struct list_elem *p, struct list_elem *victim) { - C2C *c2c; - - assert( Mask != NULL ); - assert( Chan != NULL ); - - if( Already_Registered( My_Bans, Mask, Chan )) return true; - - c2c = New_C2C( Mask, Chan, false ); - if( ! c2c ) - { - Log( LOG_ERR, "Can't add new ban list entry!" ); - return false; - } + assert(victim != NULL); + assert(h != NULL); - /* verketten */ - c2c->next = My_Bans; - My_Bans = c2c; + if (p) + p->next = victim->next; + else + h->first = victim->next; - Log( LOG_DEBUG, "Added \"%s\" to ban list for \"%s\".", Mask, Channel_Name( Chan )); - return true; -} /* Lists_AddBanned */ + if (victim->reason) + free(victim->reason); + free(victim); +} +/** + * Delete a given IRC mask from a list. + * + * @param h List head. + * @param Mask IRC mask to delete from the list. + */ GLOBAL void -Lists_DelBanned( char *Mask, CHANNEL *Chan ) +Lists_Del(struct list_head *h, const char *Mask) { - C2C *c2c, *last, *next; + struct list_elem *e, *last, *victim; - assert( Mask != NULL ); - assert( Chan != NULL ); + assert(h != NULL); + assert(Mask != 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 ); - } - else last = c2c; - c2c = next; - } -} /* Lists_DelBanned */ - - -GLOBAL bool -Lists_ShowBans( CLIENT *Client, CHANNEL *Channel ) -{ - C2C *c2c; - - assert( Client != NULL ); - assert( Channel != 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; + e = Lists_GetFirst(h); + while (e) { + if (strcasecmp(e->mask, Mask) == 0) { + LogDebug("Deleted \"%s\" from list", e->mask); + victim = e; + e = victim->next; + Lists_Unlink(h, last, victim); + continue; } - c2c = c2c->next; + last = e; + e = e->next; } - return IRC_WriteStrClient( Client, RPL_ENDOFBANLIST_MSG, Client_ID( Client ), Channel_Name( Channel )); -} /* Lists_ShowBans */ - +} +/** + * Free a complete list. + * + * @param head List head. + */ GLOBAL void -Lists_DeleteChannel( CHANNEL *Chan ) +Lists_Free(struct list_head *head) { - /* 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, *victim; + + assert(head != NULL); + + e = head->first; + head->first = NULL; + while (e) { + LogDebug("Deleted \"%s\" from list" , e->mask); + victim = e; + e = e->next; + if (victim->reason) + free(victim->reason); + free(victim); } +} - /* 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; +/** + * Check if an IRC mask is already contained in a list. + * + * @param h List head. + * @param Mask IRC mask to test. + * @return true if mask is already stored in the list, false otherwise. + */ +GLOBAL struct list_elem * +Lists_CheckDupeMask(const struct list_head *h, const char *Mask ) +{ + struct list_elem *e; + e = h->first; + while (e) { + if (strcasecmp(e->mask, Mask) == 0) + return e; + e = e->next; } -} /* Lists_DeleteChannel */ + return NULL; +} - -GLOBAL char * -Lists_MakeMask( char *Pattern ) +/** + * Generate a valid IRC mask from "any" string given. + * + * @param Pattern Source string to generate an IRC mask for. + * @param mask Buffer to store the mask. + * @param len Size of the buffer. + */ +GLOBAL void +Lists_MakeMask(const char *Pattern, char *mask, size_t len) { - /* 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; - assert( Pattern != NULL ); - - excl = strchr( Pattern, '!' ); - at = strchr( Pattern, '@' ); + assert(Pattern != NULL); - if(( at ) && ( at < excl )) excl = NULL; + excl = strchr(Pattern, '!'); + at = strchr(Pattern, '@'); - if(( ! at ) && ( ! excl )) - { - /* Neither "!" nor "@" found: use string as nick name */ - strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 ); - strlcat( TheMask, "!*@*", sizeof( TheMask )); - return TheMask; - } + if (at && at < excl) + excl = NULL; - if(( ! at ) && ( excl )) - { + if (!at && !excl) { + /* Neither "!" nor "@" found: use string as nickname */ + strlcpy(mask, Pattern, len - 5); + strlcat(mask, "!*@*", len); + } else if (!at && excl) { /* Domain part is missing */ - strlcpy( TheMask, Pattern, sizeof( TheMask ) - 3 ); - strlcat( TheMask, "@*", sizeof( TheMask )); - return TheMask; - } - - if(( at ) && ( ! excl )) - { + strlcpy(mask, Pattern, len - 3); + strlcat(mask, "@*", len); + } else if (at && !excl) { /* User name is missing */ *at = '\0'; at++; - strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 ); - strlcat( TheMask, "!*@", sizeof( TheMask )); - strlcat( TheMask, at, sizeof( TheMask )); - return TheMask; + strlcpy(mask, Pattern, len - 5); + strlcat(mask, "!*@", len); + strlcat(mask, at, len); + } else { + /* All parts (nick, user and domain name) are given */ + strlcpy(mask, Pattern, len); } - - /* 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, bool OnlyOnce ) +/** + * Check if a client is listed in a list. + * + * @param h List head. + * @param Client Client to check. + * @return true if client is listed, false if not. + */ +bool +Lists_Check(struct list_head *h, CLIENT *Client) { - C2C *c2c; - - assert( Mask != NULL ); - assert( Chan != NULL ); - - /* Speicher fuer Eintrag anfordern */ - c2c = (C2C *)malloc( sizeof( C2C )); - if( ! c2c ) - { - Log( LOG_EMERG, "Can't allocate memory! [New_C2C]" ); - return NULL; - } + return Lists_CheckReason(h, Client, NULL, 0); +} - strlcpy( c2c->mask, Mask, sizeof( c2c->mask )); - c2c->channel = Chan; - c2c->onlyonce = OnlyOnce; - - return c2c; -} /* New_C2C */ - - -LOCAL bool -Check_List( C2C **Cl2Chan, CLIENT *Client, CHANNEL *Chan ) +/** + * Check if a client is listed in a list and store the reason. + * + * @param h List head. + * @param Client Client to check. + * @param reason Buffer to store the reason. + * @param len Size of the buffer if reason should be saved. + * @return true if client is listed, false if not. + */ +bool +Lists_CheckReason(struct list_head *h, CLIENT *Client, char *reason, size_t len) { - C2C *c2c, *last; + struct list_elem *e, *last, *next; - assert( Cl2Chan != NULL ); - assert( Client != NULL ); - assert( Chan != NULL ); + assert(h != NULL); - c2c = *Cl2Chan; + e = h->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) { + next = e->next; + if (Match(e->mask, Client_MaskCloaked(Client))) { + if (len && e->reason) + strlcpy(reason, e->reason, len); + if (e->valid_until == 1) { + /* Entry is valid only once, delete it */ + LogDebug("Deleted \"%s\" from list (used).", + e->mask); + Lists_Unlink(h, last, e); } + return true; } - last = c2c; - c2c = c2c->next; + last = e; + e = next; } return false; -} /* Check_List */ - +} -LOCAL bool -Already_Registered( C2C *List, char *Mask, CHANNEL *Chan ) +/** + * Check list and purge expired entries. + * + * @param h List head. + */ +GLOBAL void +Lists_Expire(struct list_head *h, const char *ListName) { - C2C *c2c; + struct list_elem *e, *last, *next; + time_t now; + + assert(h != NULL); - c2c = List; - while( c2c ) - { - if(( c2c->channel == Chan ) && ( strcasecmp( c2c->mask, Mask ) == 0 )) return true; - c2c = c2c->next; + e = h->first; + last = NULL; + now = time(NULL); + + while (e) { + next = e->next; + if (e->valid_until > 1 && e->valid_until < now) { + /* Entry is expired, delete it */ + if (e->reason) + Log(LOG_INFO, + "Deleted \"%s\" (\"%s\") from %s list (expired).", + e->mask, e->reason, ListName); + else + Log(LOG_INFO, + "Deleted \"%s\" from %s list (expired).", + e->mask, ListName); + Lists_Unlink(h, last, e); + e = next; + continue; + } + last = e; + e = next; } - return false; -} /* Already_Registered */ +} +/** + * Return the number of entries of a list. + * + * @param h List head. + * @return Number of items. + */ +GLOBAL unsigned long +Lists_Count(struct list_head *h) +{ + struct list_elem *e; + unsigned long count = 0; + + assert(h != NULL); + + e = h->first; + while (e) { + count++; + e = e->next; + } + return count; +} /* -eof- */