2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2005 Alexander Barton (alex@barton.de)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
17 * Management of IRC lists: ban, invite, etc.
29 #include "irc-write.h"
38 #define MASK_LEN (2*CLIENT_HOST_LEN)
41 struct list_elem *next;
48 Lists_GetMask(const struct list_elem *e)
54 GLOBAL struct list_elem*
55 Lists_GetFirst(const struct list_head *h)
61 GLOBAL struct list_elem*
62 Lists_GetNext(const struct list_elem *e)
69 Lists_Add(struct list_head *header, const char *Mask, bool OnlyOnce )
71 struct list_elem *e, *newelem;
73 assert( header != NULL );
74 assert( Mask != NULL );
76 if (Lists_CheckDupeMask(header, Mask )) return true;
78 e = Lists_GetFirst(header);
80 newelem = malloc(sizeof(struct list_elem));
82 Log( LOG_EMERG, "Can't allocate memory for new Ban/Invite entry!" );
86 strlcpy( newelem->mask, Mask, sizeof( newelem->mask ));
87 newelem->onlyonce = OnlyOnce;
89 header->first = newelem;
96 Lists_Unlink(struct list_head *header, struct list_elem *p, struct list_elem *victim)
98 assert(victim != NULL);
99 assert(header != NULL);
101 if (p) p->next = victim->next;
102 else header->first = victim->next;
109 Lists_Del(struct list_head *header, const char *Mask)
111 struct list_elem *e, *last, *victim;
113 assert( header != NULL );
114 assert( Mask != NULL );
117 e = Lists_GetFirst(header);
119 if(strcasecmp( e->mask, Mask ) == 0 ) {
120 LogDebug("Deleted \"%s\" from list", e->mask);
123 Lists_Unlink(header, last, victim);
133 Lists_Free(struct list_head *head)
135 struct list_elem *e, *victim;
137 assert(head != NULL);
142 LogDebug("Deleted \"%s\" from invite list" , e->mask);
151 Lists_CheckDupeMask(const struct list_head *h, const char *Mask )
156 if (strcasecmp( e->mask, Mask ) == 0 )
165 Lists_MakeMask(const char *Pattern)
167 /* This function generats a valid IRC mask of "any" string. This
168 * mask is only valid until the next call to Lists_MakeMask(),
169 * because a single global buffer is used. You have to copy the
170 * generated mask to some sane location yourself! */
172 static char TheMask[MASK_LEN];
175 assert( Pattern != NULL );
177 excl = strchr( Pattern, '!' );
178 at = strchr( Pattern, '@' );
180 if(( at ) && ( at < excl )) excl = NULL;
182 if(( ! at ) && ( ! excl ))
184 /* Neither "!" nor "@" found: use string as nick name */
185 strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
186 strlcat( TheMask, "!*@*", sizeof( TheMask ));
190 if(( ! at ) && ( excl ))
192 /* Domain part is missing */
193 strlcpy( TheMask, Pattern, sizeof( TheMask ) - 3 );
194 strlcat( TheMask, "@*", sizeof( TheMask ));
198 if(( at ) && ( ! excl ))
200 /* User name is missing */
202 strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
203 strlcat( TheMask, "!*@", sizeof( TheMask ));
204 strlcat( TheMask, at, sizeof( TheMask ));
208 /* All parts (nick, user and domain name) are given */
209 strlcpy( TheMask, Pattern, sizeof( TheMask ));
211 } /* Lists_MakeMask */
216 Lists_Check( struct list_head *header, CLIENT *Client)
218 struct list_elem *e, *last;
220 assert( header != NULL );
226 if( Match( e->mask, Client_Mask( Client ))) {
227 if( e->onlyonce ) { /* delete entry */
228 LogDebug("Deleted \"%s\" from list", e->mask);
229 Lists_Unlink(header, last, e);