2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2011 Alexander Barton (alex@barton.de) and Contributors.
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.
16 * Management of IRC lists: ban, invite, etc.
28 #include "irc-write.h"
37 #define MASK_LEN (2*CLIENT_HOST_LEN)
40 struct list_elem *next; /** pointer to next list element */
41 char mask[MASK_LEN]; /** IRC mask */
42 char *reason; /** Optional "reason" text */
43 time_t valid_until; /** 0: unlimited; 1: once; t(>1): until t */
47 * Get IRC mask stored in list element.
49 * @param list_elem List element.
50 * @return Pointer to IRC mask
53 Lists_GetMask(const struct list_elem *e)
60 * Get optional "reason" text stored in list element.
62 * @param list_elem List element.
63 * @return Pointer to "reason" text or NULL.
66 Lists_GetReason(const struct list_elem *e)
73 * Get "validity" value stored in list element.
75 * @param list_elem List element.
76 * @return Validity: 0=unlimited, 1=once, >1 until this time stamp.
79 Lists_GetValidity(const struct list_elem *e)
82 return e->valid_until;
86 * Get first list element of a list.
89 * @return Pointer to first list element.
91 GLOBAL struct list_elem*
92 Lists_GetFirst(const struct list_head *h)
99 * Get next list element of a list.
101 * @param e Current list element.
102 * @return Pointer to next list element.
104 GLOBAL struct list_elem*
105 Lists_GetNext(const struct list_elem *e)
112 * Add a new mask to a list.
114 * @param h List head.
115 * @param Mask The IRC mask to add to the list.
116 * @param ValidUntil 0: unlimited, 1: only once, t>1: until given time_t.
117 * @param Reason Reason string or NULL, if no reason should be saved.
118 * @return true on success, false otherwise.
121 Lists_Add(struct list_head *h, const char *Mask, time_t ValidUntil,
124 struct list_elem *e, *newelem;
127 assert(Mask != NULL);
129 e = Lists_CheckDupeMask(h, Mask);
131 e->valid_until = ValidUntil;
134 e->reason = malloc(strlen(Reason) + 1);
136 strlcpy(e->reason, Reason, strlen(Reason) + 1);
139 "Can't allocate memory for new list reason text!");
143 e = Lists_GetFirst(h);
145 newelem = malloc(sizeof(struct list_elem));
148 "Can't allocate memory for new list entry!");
152 strlcpy(newelem->mask, Mask, sizeof(newelem->mask));
154 newelem->reason = malloc(strlen(Reason) + 1);
156 strlcpy(newelem->reason, Reason, strlen(Reason) + 1);
159 "Can't allocate memory for new list reason text!");
162 newelem->reason = NULL;
163 newelem->valid_until = ValidUntil;
171 * Delete a list element from a list.
173 * @param h List head.
174 * @param p Pointer to previous list element or NULL, if there is none.
175 * @param victim List element to delete.
178 Lists_Unlink(struct list_head *h, struct list_elem *p, struct list_elem *victim)
180 assert(victim != NULL);
184 p->next = victim->next;
186 h->first = victim->next;
189 free(victim->reason);
195 * Delete a given IRC mask from a list.
197 * @param h List head.
198 * @param Mask IRC mask to delete from the list.
201 Lists_Del(struct list_head *h, const char *Mask)
203 struct list_elem *e, *last, *victim;
206 assert(Mask != NULL);
209 e = Lists_GetFirst(h);
211 if (strcasecmp(e->mask, Mask) == 0) {
212 LogDebug("Deleted \"%s\" from list", e->mask);
215 Lists_Unlink(h, last, victim);
224 * Free a complete list.
226 * @param head List head.
229 Lists_Free(struct list_head *head)
231 struct list_elem *e, *victim;
233 assert(head != NULL);
238 LogDebug("Deleted \"%s\" from list" , e->mask);
242 free(victim->reason);
248 * Check if an IRC mask is already contained in a list.
250 * @param h List head.
251 * @param Mask IRC mask to test.
252 * @return true if mask is already stored in the list, false otherwise.
254 GLOBAL struct list_elem *
255 Lists_CheckDupeMask(const struct list_head *h, const char *Mask )
260 if (strcasecmp(e->mask, Mask) == 0)
268 * Generate a valid IRC mask from "any" string given.
270 * Attention: This mask is only valid until the next call to Lists_MakeMask(),
271 * because a single global buffer ist used! You have to copy the generated
272 * mask to some sane location yourself!
274 * @param Pattern Source string to generate an IRC mask for.
275 * @return Pointer to global result buffer.
278 Lists_MakeMask(const char *Pattern)
280 static char TheMask[MASK_LEN];
283 assert(Pattern != NULL);
285 excl = strchr(Pattern, '!');
286 at = strchr(Pattern, '@');
292 /* Neither "!" nor "@" found: use string as nick name */
293 strlcpy(TheMask, Pattern, sizeof(TheMask) - 5);
294 strlcat(TheMask, "!*@*", sizeof(TheMask));
299 /* Domain part is missing */
300 strlcpy(TheMask, Pattern, sizeof(TheMask) - 3);
301 strlcat(TheMask, "@*", sizeof(TheMask));
306 /* User name is missing */
308 strlcpy(TheMask, Pattern, sizeof(TheMask) - 5);
309 strlcat(TheMask, "!*@", sizeof(TheMask));
310 strlcat(TheMask, at, sizeof(TheMask));
314 /* All parts (nick, user and domain name) are given */
315 strlcpy(TheMask, Pattern, sizeof(TheMask));
317 } /* Lists_MakeMask */
320 * Check if a client is listed in a list.
322 * @param h List head.
323 * @param Client Client to check.
324 * @return true if client is listed, false if not.
327 Lists_Check( struct list_head *h, CLIENT *Client)
329 struct list_elem *e, *last, *next;
338 if (Match(e->mask, Client_Mask(Client))) {
339 if (e->valid_until == 1) {
340 /* Entry is valid only once, delete it */
341 LogDebug("Deleted \"%s\" from list (used).",
343 Lists_Unlink(h, last, e);
355 * Check list and purge expired entries.
357 * @param h List head.
360 Lists_Expire(struct list_head *h, const char *ListName)
362 struct list_elem *e, *last, *next;
373 if (e->valid_until > 1 && e->valid_until < now) {
374 /* Entry is expired, delete it */
377 "Deleted \"%s\" (\"%s\") from %s list (expired).",
378 e->mask, e->reason, ListName);
381 "Deleted \"%s\" from %s list (expired).",
383 Lists_Unlink(h, last, e);