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.
27 #include "irc-write.h"
36 struct list_elem *next; /** pointer to next list element */
37 char mask[MASK_LEN]; /** IRC mask */
38 char *reason; /** Optional "reason" text */
39 time_t valid_until; /** 0: unlimited; 1: once; t(>1): until t */
43 * Get IRC mask stored in list element.
45 * @param list_elem List element.
46 * @return Pointer to IRC mask
49 Lists_GetMask(const struct list_elem *e)
56 * Get optional "reason" text stored in list element.
58 * @param list_elem List element.
59 * @return Pointer to "reason" text or empty string ("").
62 Lists_GetReason(const struct list_elem *e)
65 return e->reason ? e->reason : "";
69 * Get "validity" value stored in list element.
71 * @param list_elem List element.
72 * @return Validity: 0=unlimited, 1=once, >1 until this time stamp.
75 Lists_GetValidity(const struct list_elem *e)
78 return e->valid_until;
82 * Get first list element of a list.
85 * @return Pointer to first list element.
87 GLOBAL struct list_elem*
88 Lists_GetFirst(const struct list_head *h)
95 * Get next list element of a list.
97 * @param e Current list element.
98 * @return Pointer to next list element.
100 GLOBAL struct list_elem*
101 Lists_GetNext(const struct list_elem *e)
108 * Add a new mask to a list.
110 * @param h List head.
111 * @param Mask The IRC mask to add to the list.
112 * @param ValidUntil 0: unlimited, 1: only once, t>1: until given time_t.
113 * @param Reason Reason string or NULL, if no reason should be saved.
114 * @return true on success, false otherwise.
117 Lists_Add(struct list_head *h, const char *Mask, time_t ValidUntil,
120 struct list_elem *e, *newelem;
123 assert(Mask != NULL);
125 e = Lists_CheckDupeMask(h, Mask);
127 e->valid_until = ValidUntil;
131 e->reason = strdup(Reason);
136 e = Lists_GetFirst(h);
138 newelem = malloc(sizeof(struct list_elem));
141 "Can't allocate memory for new list entry!");
145 strlcpy(newelem->mask, Mask, sizeof(newelem->mask));
147 newelem->reason = strdup(Reason);
148 if (!newelem->reason)
150 "Can't allocate memory for new list reason text!");
153 newelem->reason = NULL;
154 newelem->valid_until = ValidUntil;
162 * Delete a list element from a list.
164 * @param h List head.
165 * @param p Pointer to previous list element or NULL, if there is none.
166 * @param victim List element to delete.
169 Lists_Unlink(struct list_head *h, struct list_elem *p, struct list_elem *victim)
171 assert(victim != NULL);
175 p->next = victim->next;
177 h->first = victim->next;
180 free(victim->reason);
186 * Delete a given IRC mask from a list.
188 * @param h List head.
189 * @param Mask IRC mask to delete from the list.
192 Lists_Del(struct list_head *h, const char *Mask)
194 struct list_elem *e, *last, *victim;
197 assert(Mask != NULL);
200 e = Lists_GetFirst(h);
202 if (strcasecmp(e->mask, Mask) == 0) {
203 LogDebug("Deleted \"%s\" from list", e->mask);
206 Lists_Unlink(h, last, victim);
215 * Free a complete list.
217 * @param head List head.
220 Lists_Free(struct list_head *head)
222 struct list_elem *e, *victim;
224 assert(head != NULL);
229 LogDebug("Deleted \"%s\" from list" , e->mask);
233 free(victim->reason);
239 * Check if an IRC mask is already contained in a list.
241 * @param h List head.
242 * @param Mask IRC mask to test.
243 * @return true if mask is already stored in the list, false otherwise.
245 GLOBAL struct list_elem *
246 Lists_CheckDupeMask(const struct list_head *h, const char *Mask )
251 if (strcasecmp(e->mask, Mask) == 0)
259 * Generate a valid IRC mask from "any" string given.
261 * @param Pattern Source string to generate an IRC mask for.
262 * @param mask Buffer to store the mask.
263 * @param len Size of the buffer.
266 Lists_MakeMask(const char *Pattern, char *mask, size_t len)
270 assert(Pattern != NULL);
272 excl = strchr(Pattern, '!');
273 at = strchr(Pattern, '@');
279 /* Neither "!" nor "@" found: use string as nickname */
280 strlcpy(mask, Pattern, len - 5);
281 strlcat(mask, "!*@*", len);
282 } else if (!at && excl) {
283 /* Domain part is missing */
284 strlcpy(mask, Pattern, len - 3);
285 strlcat(mask, "@*", len);
286 } else if (at && !excl) {
287 /* User name is missing */
289 strlcpy(mask, Pattern, len - 5);
290 strlcat(mask, "!*@", len);
291 strlcat(mask, at, len);
293 /* All parts (nick, user and domain name) are given */
294 strlcpy(mask, Pattern, len);
296 } /* Lists_MakeMask */
299 * Check if a client is listed in a list.
301 * @param h List head.
302 * @param Client Client to check.
303 * @return true if client is listed, false if not.
306 Lists_Check(struct list_head *h, CLIENT *Client)
308 return Lists_CheckReason(h, Client, NULL, 0);
312 * Check if a client is listed in a list and store the reason.
314 * @param h List head.
315 * @param Client Client to check.
316 * @param reason Buffer to store the reason.
317 * @param len Size of the buffer if reason should be saved.
318 * @return true if client is listed, false if not.
321 Lists_CheckReason(struct list_head *h, CLIENT *Client, char *reason, size_t len)
323 struct list_elem *e, *last, *next;
332 if (Match(e->mask, Client_MaskCloaked(Client))) {
333 if (len && e->reason)
334 strlcpy(reason, e->reason, len);
335 if (e->valid_until == 1) {
336 /* Entry is valid only once, delete it */
337 LogDebug("Deleted \"%s\" from list (used).",
339 Lists_Unlink(h, last, e);
351 * Check list and purge expired entries.
353 * @param h List head.
356 Lists_Expire(struct list_head *h, const char *ListName)
358 struct list_elem *e, *last, *next;
369 if (e->valid_until > 1 && e->valid_until < now) {
370 /* Entry is expired, delete it */
373 "Deleted \"%s\" (\"%s\") from %s list (expired).",
374 e->mask, e->reason, ListName);
377 "Deleted \"%s\" from %s list (expired).",
379 Lists_Unlink(h, last, e);
389 * Return the number of entries of a list.
391 * @param h List head.
392 * @return Number of items.
395 Lists_Count(struct list_head *h)
398 unsigned long count = 0;