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, ...
27 #include "irc-write.h"
36 #define MASK_LEN (2*CLIENT_HOST_LEN)
39 struct list_elem *next;
46 Lists_GetMask(const struct list_elem *e)
52 GLOBAL struct list_elem*
53 Lists_GetFirst(const struct list_head *h)
59 GLOBAL struct list_elem*
60 Lists_GetNext(const struct list_elem *e)
67 Lists_Add(struct list_head *header, const char *Mask, bool OnlyOnce )
69 struct list_elem *e, *newelem;
71 assert( header != NULL );
72 assert( Mask != NULL );
74 if (Lists_CheckDupeMask(header, Mask )) return true;
76 e = Lists_GetFirst(header);
78 newelem = malloc(sizeof(struct list_elem));
80 Log( LOG_EMERG, "Can't allocate memory for new Ban/Invite entry!" );
84 strlcpy( newelem->mask, Mask, sizeof( newelem->mask ));
85 newelem->onlyonce = OnlyOnce;
87 header->first = newelem;
89 LogDebug("Added \"%s\" to invite list", Mask);
95 Lists_Unlink(struct list_head *header, struct list_elem *p, struct list_elem *victim)
97 assert(victim != NULL);
98 assert(header != NULL);
100 if (p) p->next = victim->next;
101 else header->first = victim->next;
108 Lists_Del(struct list_head *header, const char *Mask)
110 struct list_elem *e, *last, *victim;
112 assert( header != NULL );
113 assert( Mask != NULL );
116 e = Lists_GetFirst(header);
118 if(strcasecmp( e->mask, Mask ) == 0 ) {
119 LogDebug("Deleted \"%s\" from list", e->mask);
122 Lists_Unlink(header, last, victim);
132 Lists_Free(struct list_head *head)
134 struct list_elem *e, *victim;
136 assert(head != NULL);
141 LogDebug("Deleted \"%s\" from invite list" , e->mask);
150 Lists_CheckDupeMask(const struct list_head *h, const char *Mask )
155 if (strcasecmp( e->mask, Mask ) == 0 )
164 Lists_MakeMask(const char *Pattern)
166 /* This function generats a valid IRC mask of "any" string. This
167 * mask is only valid until the next call to Lists_MakeMask(),
168 * because a single global buffer is used. You have to copy the
169 * generated mask to some sane location yourself! */
171 static char TheMask[MASK_LEN];
174 assert( Pattern != NULL );
176 excl = strchr( Pattern, '!' );
177 at = strchr( Pattern, '@' );
179 if(( at ) && ( at < excl )) excl = NULL;
181 if(( ! at ) && ( ! excl ))
183 /* Neither "!" nor "@" found: use string as nick name */
184 strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
185 strlcat( TheMask, "!*@*", sizeof( TheMask ));
189 if(( ! at ) && ( excl ))
191 /* Domain part is missing */
192 strlcpy( TheMask, Pattern, sizeof( TheMask ) - 3 );
193 strlcat( TheMask, "@*", sizeof( TheMask ));
197 if(( at ) && ( ! excl ))
199 /* User name is missing */
201 strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
202 strlcat( TheMask, "!*@", sizeof( TheMask ));
203 strlcat( TheMask, at, sizeof( TheMask ));
207 /* All parts (nick, user and domain name) are given */
208 strlcpy( TheMask, Pattern, sizeof( TheMask ));
210 } /* Lists_MakeMask */
215 Lists_Check( struct list_head *header, CLIENT *Client)
217 struct list_elem *e, *last;
219 assert( header != NULL );
225 if( Match( e->mask, Client_Mask( Client ))) {
226 if( e->onlyonce ) { /* delete entry */
227 LogDebug("Deleted \"%s\" from list", e->mask);
228 Lists_Unlink(header, last, e);