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;
88 LogDebug("Added \"%s\" to invite list", Mask);
94 Lists_Unlink(struct list_head *header, struct list_elem *p, struct list_elem *victim)
96 assert(victim != NULL);
97 assert(header != NULL);
99 if (p) p->next = victim->next;
100 else header->first = victim->next;
107 Lists_Del(struct list_head *header, const char *Mask)
109 struct list_elem *e, *last, *victim;
111 assert( header != NULL );
112 assert( Mask != NULL );
115 e = Lists_GetFirst(header);
117 if(strcasecmp( e->mask, Mask ) == 0 ) {
118 LogDebug("Deleted \"%s\" from list", e->mask);
121 Lists_Unlink(header, last, victim);
131 Lists_Free(struct list_head *head)
133 struct list_elem *e, *victim;
135 assert(head != NULL);
140 LogDebug("Deleted \"%s\" from invite list" , e->mask);
149 Lists_CheckDupeMask(const struct list_head *h, const char *Mask )
154 if (strcasecmp( e->mask, Mask ) == 0 )
163 Lists_MakeMask(const char *Pattern)
165 /* This function generats a valid IRC mask of "any" string. This
166 * mask is only valid until the next call to Lists_MakeMask(),
167 * because a single global buffer is used. You have to copy the
168 * generated mask to some sane location yourself! */
170 static char TheMask[MASK_LEN];
173 assert( Pattern != NULL );
175 excl = strchr( Pattern, '!' );
176 at = strchr( Pattern, '@' );
178 if(( at ) && ( at < excl )) excl = NULL;
180 if(( ! at ) && ( ! excl ))
182 /* Neither "!" nor "@" found: use string as nick name */
183 strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
184 strlcat( TheMask, "!*@*", sizeof( TheMask ));
188 if(( ! at ) && ( excl ))
190 /* Domain part is missing */
191 strlcpy( TheMask, Pattern, sizeof( TheMask ) - 3 );
192 strlcat( TheMask, "@*", sizeof( TheMask ));
196 if(( at ) && ( ! excl ))
198 /* User name is missing */
200 strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
201 strlcat( TheMask, "!*@", sizeof( TheMask ));
202 strlcat( TheMask, at, sizeof( TheMask ));
206 /* All parts (nick, user and domain name) are given */
207 strlcpy( TheMask, Pattern, sizeof( TheMask ));
209 } /* Lists_MakeMask */
214 Lists_Check( struct list_head *header, CLIENT *Client)
216 struct list_elem *e, *last;
218 assert( header != NULL );
224 if( Match( e->mask, Client_Mask( Client ))) {
225 if( e->onlyonce ) { /* delete entry */
226 LogDebug("Deleted \"%s\" from list", e->mask);
227 Lists_Unlink(header, last, e);