2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2014 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.
32 struct list_elem *next; /** pointer to next list element */
33 char mask[MASK_LEN]; /** IRC mask */
34 char *reason; /** Optional "reason" text */
35 time_t valid_until; /** 0: unlimited; 1: once; t(>1): until t */
39 * Get IRC mask stored in list element.
41 * @param list_elem List element.
42 * @return Pointer to IRC mask
45 Lists_GetMask(const struct list_elem *e)
52 * Get optional "reason" text stored in list element.
54 * @param list_elem List element.
55 * @return Pointer to "reason" text or empty string ("").
58 Lists_GetReason(const struct list_elem *e)
61 return e->reason ? e->reason : "";
65 * Get "validity" value stored in list element.
67 * @param list_elem List element.
68 * @return Validity: 0=unlimited, 1=once, >1 until this time stamp.
71 Lists_GetValidity(const struct list_elem *e)
74 return e->valid_until;
78 * Get first list element of a list.
81 * @return Pointer to first list element.
83 GLOBAL struct list_elem*
84 Lists_GetFirst(const struct list_head *h)
91 * Get next list element of a list.
93 * @param e Current list element.
94 * @return Pointer to next list element.
96 GLOBAL struct list_elem*
97 Lists_GetNext(const struct list_elem *e)
104 * Add a new mask to a list.
106 * @param h List head.
107 * @param Mask The IRC mask to add to the list.
108 * @param ValidUntil 0: unlimited, 1: only once, t>1: until given time_t.
109 * @param Reason Reason string or NULL, if no reason should be saved.
110 * @return true on success, false otherwise.
113 Lists_Add(struct list_head *h, const char *Mask, time_t ValidUntil,
116 struct list_elem *e, *newelem;
119 assert(Mask != NULL);
121 e = Lists_CheckDupeMask(h, Mask);
123 e->valid_until = ValidUntil;
127 e->reason = strdup(Reason);
132 e = Lists_GetFirst(h);
134 newelem = malloc(sizeof(struct list_elem));
137 "Can't allocate memory for new list entry!");
141 strlcpy(newelem->mask, Mask, sizeof(newelem->mask));
143 newelem->reason = strdup(Reason);
144 if (!newelem->reason)
146 "Can't allocate memory for new list reason text!");
149 newelem->reason = NULL;
150 newelem->valid_until = ValidUntil;
158 * Delete a list element from a list.
160 * @param h List head.
161 * @param p Pointer to previous list element or NULL, if there is none.
162 * @param victim List element to delete.
165 Lists_Unlink(struct list_head *h, struct list_elem *p, struct list_elem *victim)
167 assert(victim != NULL);
171 p->next = victim->next;
173 h->first = victim->next;
176 free(victim->reason);
182 * Delete a given IRC mask from a list.
184 * @param h List head.
185 * @param Mask IRC mask to delete from the list.
188 Lists_Del(struct list_head *h, const char *Mask)
190 struct list_elem *e, *last, *victim;
193 assert(Mask != NULL);
196 e = Lists_GetFirst(h);
198 if (strcasecmp(e->mask, Mask) == 0) {
199 LogDebug("Deleted \"%s\" from list", e->mask);
202 Lists_Unlink(h, last, victim);
211 * Free a complete list.
213 * @param head List head.
216 Lists_Free(struct list_head *head)
218 struct list_elem *e, *victim;
220 assert(head != NULL);
225 LogDebug("Deleted \"%s\" from list" , e->mask);
229 free(victim->reason);
235 * Check if an IRC mask is already contained in a list.
237 * @param h List head.
238 * @param Mask IRC mask to test.
239 * @return true if mask is already stored in the list, false otherwise.
241 GLOBAL struct list_elem *
242 Lists_CheckDupeMask(const struct list_head *h, const char *Mask )
247 if (strcasecmp(e->mask, Mask) == 0)
255 * Generate a valid IRC mask from "any" string given.
257 * @param Pattern Source string to generate an IRC mask for.
258 * @param mask Buffer to store the mask.
259 * @param len Size of the buffer.
262 Lists_MakeMask(const char *Pattern, char *mask, size_t len)
266 assert(Pattern != NULL);
268 excl = strchr(Pattern, '!');
269 at = strchr(Pattern, '@');
275 /* Neither "!" nor "@" found: use string as nickname */
276 strlcpy(mask, Pattern, len - 5);
277 strlcat(mask, "!*@*", len);
278 } else if (!at && excl) {
279 /* Domain part is missing */
280 strlcpy(mask, Pattern, len - 3);
281 strlcat(mask, "@*", len);
282 } else if (at && !excl) {
283 /* User name is missing */
285 strlcpy(mask, Pattern, len - 5);
286 strlcat(mask, "!*@", len);
287 strlcat(mask, at, len);
290 /* All parts (nick, user and domain name) are given */
291 strlcpy(mask, Pattern, len);
293 } /* Lists_MakeMask */
296 * Check if a client is listed in a list.
298 * @param h List head.
299 * @param Client Client to check.
300 * @return true if client is listed, false if not.
303 Lists_Check(struct list_head *h, CLIENT *Client)
305 return Lists_CheckReason(h, Client, NULL, 0);
309 * Check if a client is listed in a list and store the reason.
311 * @param h List head.
312 * @param Client Client to check.
313 * @param reason Buffer to store the reason.
314 * @param len Size of the buffer if reason should be saved.
315 * @return true if client is listed, false if not.
318 Lists_CheckReason(struct list_head *h, CLIENT *Client, char *reason, size_t len)
320 struct list_elem *e, *last, *next;
329 if (MatchCaseInsensitive(e->mask, Client_MaskCloaked(Client))) {
330 if (len && e->reason)
331 strlcpy(reason, e->reason, len);
332 if (e->valid_until == 1) {
333 /* Entry is valid only once, delete it */
334 LogDebug("Deleted \"%s\" from list (used).",
336 Lists_Unlink(h, last, e);
348 * Check list and purge expired entries.
350 * @param h List head.
353 Lists_Expire(struct list_head *h, const char *ListName)
355 struct list_elem *e, *last, *next;
366 if (e->valid_until > 1 && e->valid_until < now) {
367 /* Entry is expired, delete it */
370 "Deleted \"%s\" (\"%s\") from %s list (expired).",
371 e->mask, e->reason, ListName);
374 "Deleted \"%s\" from %s list (expired).",
376 Lists_Unlink(h, last, e);
386 * Return the number of entries of a list.
388 * @param h List head.
389 * @return Number of items.
392 Lists_Count(struct list_head *h)
395 unsigned long count = 0;