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.
11 * Management of IRC lists: ban, invite, ...
26 #include "irc-write.h"
35 #define MASK_LEN (2*CLIENT_HOST_LEN)
38 struct list_elem *next;
45 Lists_GetMask(const struct list_elem *e)
51 GLOBAL struct list_elem*
52 Lists_GetFirst(const struct list_head *h)
58 GLOBAL struct list_elem*
59 Lists_GetNext(const struct list_elem *e)
66 Lists_Add(struct list_head *header, const char *Mask, bool OnlyOnce )
68 struct list_elem *e, *newelem;
70 assert( header != NULL );
71 assert( Mask != NULL );
73 if (Lists_CheckDupeMask(header, Mask )) return true;
75 e = Lists_GetFirst(header);
77 newelem = malloc(sizeof(struct list_elem));
79 Log( LOG_EMERG, "Can't allocate memory for new Ban/Invite entry!" );
83 strlcpy( newelem->mask, Mask, sizeof( newelem->mask ));
84 newelem->onlyonce = OnlyOnce;
86 header->first = newelem;
93 Lists_Unlink(struct list_head *header, struct list_elem *p, struct list_elem *victim)
95 assert(victim != NULL);
96 assert(header != NULL);
98 if (p) p->next = victim->next;
99 else header->first = victim->next;
106 Lists_Del(struct list_head *header, const char *Mask)
108 struct list_elem *e, *last, *victim;
110 assert( header != NULL );
111 assert( Mask != NULL );
114 e = Lists_GetFirst(header);
116 if(strcasecmp( e->mask, Mask ) == 0 ) {
117 LogDebug("Deleted \"%s\" from list", e->mask);
120 Lists_Unlink(header, last, victim);
130 Lists_Free(struct list_head *head)
132 struct list_elem *e, *victim;
134 assert(head != NULL);
139 LogDebug("Deleted \"%s\" from invite list" , e->mask);
148 Lists_CheckDupeMask(const struct list_head *h, const char *Mask )
153 if (strcasecmp( e->mask, Mask ) == 0 )
162 Lists_MakeMask(const char *Pattern)
164 /* This function generats a valid IRC mask of "any" string. This
165 * mask is only valid until the next call to Lists_MakeMask(),
166 * because a single global buffer is used. You have to copy the
167 * generated mask to some sane location yourself! */
169 static char TheMask[MASK_LEN];
172 assert( Pattern != NULL );
174 excl = strchr( Pattern, '!' );
175 at = strchr( Pattern, '@' );
177 if(( at ) && ( at < excl )) excl = NULL;
179 if(( ! at ) && ( ! excl ))
181 /* Neither "!" nor "@" found: use string as nick name */
182 strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
183 strlcat( TheMask, "!*@*", sizeof( TheMask ));
187 if(( ! at ) && ( excl ))
189 /* Domain part is missing */
190 strlcpy( TheMask, Pattern, sizeof( TheMask ) - 3 );
191 strlcat( TheMask, "@*", sizeof( TheMask ));
195 if(( at ) && ( ! excl ))
197 /* User name is missing */
199 strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
200 strlcat( TheMask, "!*@", sizeof( TheMask ));
201 strlcat( TheMask, at, sizeof( TheMask ));
205 /* All parts (nick, user and domain name) are given */
206 strlcpy( TheMask, Pattern, sizeof( TheMask ));
208 } /* Lists_MakeMask */
213 Lists_Check( struct list_head *header, CLIENT *Client)
215 struct list_elem *e, *last;
217 assert( header != NULL );
223 if( Match( e->mask, Client_Mask( Client ))) {
224 if( e->onlyonce ) { /* delete entry */
225 LogDebug("Deleted \"%s\" from list", e->mask);
226 Lists_Unlink(header, last, e);