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 = malloc(strlen(Reason) + 1);
152 strlcpy(newelem->reason, Reason,
153 sizeof(newelem->reason));
156 "Can't allocate memory for new list reason text!");
159 newelem->reason = NULL;
160 newelem->valid_until = ValidUntil;
168 * Delete a list element from a list.
170 * @param h List head.
171 * @param p Pointer to previous list element or NULL, if there is none.
172 * @param victim List element to delete.
175 Lists_Unlink(struct list_head *h, struct list_elem *p, struct list_elem *victim)
177 assert(victim != NULL);
181 p->next = victim->next;
183 h->first = victim->next;
186 free(victim->reason);
192 * Delete a given IRC mask from a list.
194 * @param h List head.
195 * @param Mask IRC mask to delete from the list.
198 Lists_Del(struct list_head *h, const char *Mask)
200 struct list_elem *e, *last, *victim;
203 assert(Mask != NULL);
206 e = Lists_GetFirst(h);
208 if (strcasecmp(e->mask, Mask) == 0) {
209 LogDebug("Deleted \"%s\" from list", e->mask);
212 Lists_Unlink(h, last, victim);
221 * Free a complete list.
223 * @param head List head.
226 Lists_Free(struct list_head *head)
228 struct list_elem *e, *victim;
230 assert(head != NULL);
235 LogDebug("Deleted \"%s\" from list" , e->mask);
239 free(victim->reason);
245 * Check if an IRC mask is already contained in a list.
247 * @param h List head.
248 * @param Mask IRC mask to test.
249 * @return true if mask is already stored in the list, false otherwise.
251 GLOBAL struct list_elem *
252 Lists_CheckDupeMask(const struct list_head *h, const char *Mask )
257 if (strcasecmp(e->mask, Mask) == 0)
265 * Generate a valid IRC mask from "any" string given.
267 * Attention: This mask is only valid until the next call to Lists_MakeMask(),
268 * because a single global buffer ist used! You have to copy the generated
269 * mask to some sane location yourself!
271 * @param Pattern Source string to generate an IRC mask for.
272 * @return Pointer to global result buffer.
275 Lists_MakeMask(const char *Pattern)
277 static char TheMask[MASK_LEN];
280 assert(Pattern != NULL);
282 excl = strchr(Pattern, '!');
283 at = strchr(Pattern, '@');
289 /* Neither "!" nor "@" found: use string as nick name */
290 strlcpy(TheMask, Pattern, sizeof(TheMask) - 5);
291 strlcat(TheMask, "!*@*", sizeof(TheMask));
296 /* Domain part is missing */
297 strlcpy(TheMask, Pattern, sizeof(TheMask) - 3);
298 strlcat(TheMask, "@*", sizeof(TheMask));
303 /* User name is missing */
305 strlcpy(TheMask, Pattern, sizeof(TheMask) - 5);
306 strlcat(TheMask, "!*@", sizeof(TheMask));
307 strlcat(TheMask, at, sizeof(TheMask));
311 /* All parts (nick, user and domain name) are given */
312 strlcpy(TheMask, Pattern, sizeof(TheMask));
314 } /* Lists_MakeMask */
317 * Check if a client is listed in a list.
319 * @param h List head.
320 * @param Client Client to check.
321 * @return true if client is listed, false if not.
324 Lists_Check(struct list_head *h, CLIENT *Client)
326 return Lists_CheckReason(h, Client) != NULL;
330 * Check if a client is listed in a list and return the "reason".
332 * @param h List head.
333 * @param Client Client to check.
334 * @return true if client is listed, false if not.
337 Lists_CheckReason(struct list_head *h, CLIENT *Client)
339 struct list_elem *e, *last, *next;
348 if (Match(e->mask, Client_Mask(Client))) {
349 if (e->valid_until == 1) {
350 /* Entry is valid only once, delete it */
351 LogDebug("Deleted \"%s\" from list (used).",
353 Lists_Unlink(h, last, e);
355 return e->reason ? e->reason : "";
365 * Check list and purge expired entries.
367 * @param h List head.
370 Lists_Expire(struct list_head *h, const char *ListName)
372 struct list_elem *e, *last, *next;
383 if (e->valid_until > 1 && e->valid_until < now) {
384 /* Entry is expired, delete it */
387 "Deleted \"%s\" (\"%s\") from %s list (expired).",
388 e->mask, e->reason, ListName);
391 "Deleted \"%s\" from %s list (expired).",
393 Lists_Unlink(h, last, e);
403 * Return the number of entries of a list.
405 * @param h List head.
406 * @return Number of items.
409 Lists_Count(struct list_head *h)
412 unsigned long count = 0;