X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=ngircd-alex.git;a=blobdiff_plain;f=src%2Fngircd%2Flists.c;h=247344e508a6e3daf3fc164d1e47ddbdc0e98152;hp=e3020634e36e2c90afc529dfa3a0fa33b7aae9db;hb=be887070273c47a07c9bf488630f6fb707976f67;hpb=490f28ffd1b42ee2076ce89c62d0e672ee7e541e diff --git a/src/ngircd/lists.c b/src/ngircd/lists.c index e3020634..247344e5 100644 --- a/src/ngircd/lists.c +++ b/src/ngircd/lists.c @@ -1,447 +1,407 @@ /* * ngIRCd -- The Next Generation IRC Daemon - * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de) + * Copyright (c)2001-2014 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.10 2002/12/12 12:24:18 alex Exp $"; +/** + * @file + * Management of IRC lists: ban, invite, etc. + */ -#include "imp.h" #include +#include +#include +#include +#include -#include "defines.h" #include "conn.h" -#include "client.h" -#include "channel.h" #include "log.h" #include "match.h" -#include "messages.h" -#include "irc-write.h" - -#include -#include -#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 +/** + * 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) { - struct _C2C *next; - CHAR mask[MASK_LEN]; - CHANNEL *channel; - BOOLEAN onlyonce; -} C2C; - - -LOCAL C2C *My_Invites, *My_Bans; + assert(e != NULL); + return e->mask; +} - -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 ) +/** + * 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) { - /* Modul initialisieren */ + assert(e != NULL); + return e->reason ? e->reason : ""; +} - My_Invites = My_Bans = NULL; -} /* Lists_Init */ - - -GLOBAL VOID -Lists_Exit( VOID ) +/** + * 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) { - /* Modul abmelden */ - - C2C *c2c, *next; - - /* Invite-Lists freigeben */ - c2c = My_Invites; - while( c2c ) - { - next = c2c->next; - free( c2c ); - c2c = next; - } + assert(e != NULL); + return e->valid_until; +} - /* 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 ) +/** + * 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) { - return Check_List( &My_Invites, Client, Chan ); -} /* Lists_CheckInvited */ - + assert(h != NULL); + return h->first; +} -GLOBAL BOOLEAN -Lists_AddInvited( CLIENT *From, CHAR *Mask, CHANNEL *Chan, BOOLEAN OnlyOnce ) +/** + * 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; - - 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 */ + assert(e != NULL); + return e->next; +} - -GLOBAL VOID -Lists_DelInvited( CHAR *Mask, CHANNEL *Chan ) +/** + * 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, *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 ); + 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); } - else last = c2c; - c2c = next; + return true; } -} /* Lists_DelInvited */ + e = Lists_GetFirst(h); -GLOBAL BOOLEAN -Lists_ShowInvites( CLIENT *Client, CHANNEL *Channel ) -{ - 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; - } - 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 IRC_WriteStrClient( Client, RPL_ENDOFINVITELIST_MSG, Client_ID( Client ), Channel_Name( Channel )); -} /* Lists_ShowInvites */ - -GLOBAL BOOLEAN -Lists_CheckBanned( CLIENT *Client, CHANNEL *Chan ) -{ - return Check_List( &My_Bans, Client, Chan ); -} /* Lists_CheckBanned */ + 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!"); + } + else + newelem->reason = NULL; + newelem->valid_until = ValidUntil; + newelem->next = e; + h->first = newelem; + return true; +} -GLOBAL BOOLEAN -Lists_AddBanned( CLIENT *From, 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(victim != NULL); + assert(h != NULL); - assert( Mask != NULL ); - assert( Chan != NULL ); + if (p) + p->next = victim->next; + else + h->first = victim->next; - 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 */ + if (victim->reason) + free(victim->reason); + free(victim); +} -GLOBAL VOID -Lists_DelBanned( CHAR *Mask, CHANNEL *Chan ) +/** + * Delete a given IRC mask from a list. + * + * @param h List head. + * @param Mask IRC mask to delete from the list. + */ +GLOBAL void +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 ); + 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; } - else last = c2c; - c2c = next; + last = e; + e = e->next; } -} /* Lists_DelBanned */ - +} -GLOBAL BOOLEAN -Lists_ShowBans( CLIENT *Client, CHANNEL *Channel ) +/** + * Free a complete list. + * + * @param head List head. + */ +GLOBAL void +Lists_Free(struct list_head *head) { - 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; - } - 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); } - return IRC_WriteStrClient( Client, RPL_ENDOFBANLIST_MSG, Client_ID( Client ), Channel_Name( Channel )); -} /* Lists_ShowBans */ - +} -GLOBAL VOID -Lists_DeleteChannel( CHANNEL *Chan ) +/** + * 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 ) { - /* 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 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) { - /* 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!*/ - - STATIC CHAR TheMask[MASK_LEN]; - CHAR *excl, *at; - - assert( Pattern != NULL ); - - excl = strchr( Pattern, '!' ); - at = strchr( Pattern, '@' ); - - if(( at ) && ( at < excl )) excl = NULL; - - if(( ! at ) && ( ! excl )) - { - /* weder ! noch @ĂŠvorhanden: als Nick annehmen */ - strncpy( TheMask, Pattern, MASK_LEN - 5 ); - TheMask[MASK_LEN - 5] = '\0'; - strcat( TheMask, "!*@*" ); - return TheMask; - } - - if(( ! at ) && ( excl )) - { - /* Domain fehlt */ - strncpy( TheMask, Pattern, MASK_LEN - 3 ); - TheMask[MASK_LEN - 3] = '\0'; - strcat( TheMask, "@*" ); - return TheMask; - } - - if(( at ) && ( ! excl )) - { - /* User fehlt */ + char *excl, *at; + + assert(Pattern != NULL); + + excl = strchr(Pattern, '!'); + at = strchr(Pattern, '@'); + + if (at && at < excl) + excl = NULL; + + 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(mask, Pattern, len - 3); + strlcat(mask, "@*", len); + } else if (at && !excl) { + /* 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'; - return TheMask; + strlcpy(mask, Pattern, len - 5); + strlcat(mask, "!*@", len); + strlcat(mask, at, len); + at--; *at = '@'; + } else { + /* All parts (nick, user and domain name) are given */ + strlcpy(mask, Pattern, len); } - - /* alle Teile vorhanden */ - strncpy( TheMask, Pattern, MASK_LEN - 1 ); - TheMask[MASK_LEN - 1] = '\0'; - return TheMask; } /* Lists_MakeMask */ - -LOCAL C2C * -New_C2C( CHAR *Mask, CHANNEL *Chan, BOOLEAN 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 = 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 Lists_CheckReason(h, Client, NULL, 0); +} - return c2c; -} /* New_C2C */ - - -LOCAL BOOLEAN -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 (MatchCaseInsensitive(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 */ - + return false; +} -LOCAL BOOLEAN -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; - c2c = List; - while( c2c ) - { - if(( c2c->channel == Chan ) && ( strcasecmp( c2c->mask, Mask ) == 0 )) return TRUE; - c2c = c2c->next; + assert(h != NULL); + + 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- */