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 empty string ("").
66 Lists_GetReason(const struct list_elem *e)
69 return e->reason ? e->reason : "";
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 = strdup(Reason);
139 e = Lists_GetFirst(h);
141 newelem = malloc(sizeof(struct list_elem));
144 "Can't allocate memory for new list entry!");
148 strlcpy(newelem->mask, Mask, sizeof(newelem->mask));
150 newelem->reason = strdup(Reason);
151 if (!newelem->reason)
153 "Can't allocate memory for new list reason text!");
156 newelem->reason = NULL;
157 newelem->valid_until = ValidUntil;
165 * Delete a list element from a list.
167 * @param h List head.
168 * @param p Pointer to previous list element or NULL, if there is none.
169 * @param victim List element to delete.
172 Lists_Unlink(struct list_head *h, struct list_elem *p, struct list_elem *victim)
174 assert(victim != NULL);
178 p->next = victim->next;
180 h->first = victim->next;
183 free(victim->reason);
189 * Delete a given IRC mask from a list.
191 * @param h List head.
192 * @param Mask IRC mask to delete from the list.
195 Lists_Del(struct list_head *h, const char *Mask)
197 struct list_elem *e, *last, *victim;
200 assert(Mask != NULL);
203 e = Lists_GetFirst(h);
205 if (strcasecmp(e->mask, Mask) == 0) {
206 LogDebug("Deleted \"%s\" from list", e->mask);
209 Lists_Unlink(h, last, victim);
218 * Free a complete list.
220 * @param head List head.
223 Lists_Free(struct list_head *head)
225 struct list_elem *e, *victim;
227 assert(head != NULL);
232 LogDebug("Deleted \"%s\" from list" , e->mask);
236 free(victim->reason);
242 * Check if an IRC mask is already contained in a list.
244 * @param h List head.
245 * @param Mask IRC mask to test.
246 * @return true if mask is already stored in the list, false otherwise.
248 GLOBAL struct list_elem *
249 Lists_CheckDupeMask(const struct list_head *h, const char *Mask )
254 if (strcasecmp(e->mask, Mask) == 0)
262 * Generate a valid IRC mask from "any" string given.
264 * Attention: This mask is only valid until the next call to Lists_MakeMask(),
265 * because a single global buffer ist used! You have to copy the generated
266 * mask to some sane location yourself!
268 * @param Pattern Source string to generate an IRC mask for.
269 * @return Pointer to global result buffer.
272 Lists_MakeMask(const char *Pattern)
274 static char TheMask[MASK_LEN];
277 assert(Pattern != NULL);
279 excl = strchr(Pattern, '!');
280 at = strchr(Pattern, '@');
286 /* Neither "!" nor "@" found: use string as nick name */
287 strlcpy(TheMask, Pattern, sizeof(TheMask) - 5);
288 strlcat(TheMask, "!*@*", sizeof(TheMask));
293 /* Domain part is missing */
294 strlcpy(TheMask, Pattern, sizeof(TheMask) - 3);
295 strlcat(TheMask, "@*", sizeof(TheMask));
300 /* User name is missing */
302 strlcpy(TheMask, Pattern, sizeof(TheMask) - 5);
303 strlcat(TheMask, "!*@", sizeof(TheMask));
304 strlcat(TheMask, at, sizeof(TheMask));
308 /* All parts (nick, user and domain name) are given */
309 strlcpy(TheMask, Pattern, sizeof(TheMask));
311 } /* Lists_MakeMask */
314 * Check if a client is listed in a list.
316 * @param h List head.
317 * @param Client Client to check.
318 * @return true if client is listed, false if not.
321 Lists_Check(struct list_head *h, CLIENT *Client)
323 return Lists_CheckReason(h, Client) != NULL;
327 * Check if a client is listed in a list and return the "reason".
329 * @param h List head.
330 * @param Client Client to check.
331 * @return true if client is listed, false if not.
334 Lists_CheckReason(struct list_head *h, CLIENT *Client)
336 struct list_elem *e, *last, *next;
345 if (Match(e->mask, Client_Mask(Client))) {
346 if (e->valid_until == 1) {
347 /* Entry is valid only once, delete it */
348 LogDebug("Deleted \"%s\" from list (used).",
350 Lists_Unlink(h, last, e);
352 return e->reason ? e->reason : "";
362 * Check list and purge expired entries.
364 * @param h List head.
367 Lists_Expire(struct list_head *h, const char *ListName)
369 struct list_elem *e, *last, *next;
380 if (e->valid_until > 1 && e->valid_until < now) {
381 /* Entry is expired, delete it */
384 "Deleted \"%s\" (\"%s\") from %s list (expired).",
385 e->mask, e->reason, ListName);
388 "Deleted \"%s\" from %s list (expired).",
390 Lists_Unlink(h, last, e);
400 * Return the number of entries of a list.
402 * @param h List head.
403 * @return Number of items.
406 Lists_Count(struct list_head *h)
409 unsigned long count = 0;