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 if (Lists_CheckDupeMask(h, Mask))
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 = malloc(strlen(Reason) + 1);
145 strlcpy(newelem->reason, Reason, strlen(Reason) + 1);
148 "Can't allocate memory for new list reason text!");
151 newelem->reason = NULL;
152 newelem->valid_until = ValidUntil;
160 * Delete a list element from a list.
162 * @param h List head.
163 * @param p Pointer to previous list element or NULL, if there is none.
164 * @param victim List element to delete.
167 Lists_Unlink(struct list_head *h, struct list_elem *p, struct list_elem *victim)
169 assert(victim != NULL);
173 p->next = victim->next;
175 h->first = victim->next;
178 free(victim->reason);
184 * Delete a given IRC mask from a list.
186 * @param h List head.
187 * @param Mask IRC mask to delete from the list.
190 Lists_Del(struct list_head *h, const char *Mask)
192 struct list_elem *e, *last, *victim;
195 assert(Mask != NULL);
198 e = Lists_GetFirst(h);
200 if (strcasecmp(e->mask, Mask) == 0) {
201 LogDebug("Deleted \"%s\" from list", e->mask);
204 Lists_Unlink(h, last, victim);
213 * Free a complete list.
215 * @param head List head.
218 Lists_Free(struct list_head *head)
220 struct list_elem *e, *victim;
222 assert(head != NULL);
227 LogDebug("Deleted \"%s\" from list" , e->mask);
231 free(victim->reason);
237 * Check if an IRC mask is already contained in a list.
239 * @param h List head.
240 * @param Mask IRC mask to test.
241 * @return true if mask is already stored in the list, false otherwise.
244 Lists_CheckDupeMask(const struct list_head *h, const char *Mask )
249 if (strcasecmp(e->mask, Mask) == 0)
257 * Generate a valid IRC mask from "any" string given.
259 * Attention: This mask is only valid until the next call to Lists_MakeMask(),
260 * because a single global buffer ist used! You have to copy the generated
261 * mask to some sane location yourself!
263 * @param Pattern Source string to generate an IRC mask for.
264 * @return Pointer to global result buffer.
267 Lists_MakeMask(const char *Pattern)
269 static char TheMask[MASK_LEN];
272 assert(Pattern != NULL);
274 excl = strchr(Pattern, '!');
275 at = strchr(Pattern, '@');
281 /* Neither "!" nor "@" found: use string as nick name */
282 strlcpy(TheMask, Pattern, sizeof(TheMask) - 5);
283 strlcat(TheMask, "!*@*", sizeof(TheMask));
288 /* Domain part is missing */
289 strlcpy(TheMask, Pattern, sizeof(TheMask) - 3);
290 strlcat(TheMask, "@*", sizeof(TheMask));
295 /* User name is missing */
297 strlcpy(TheMask, Pattern, sizeof(TheMask) - 5);
298 strlcat(TheMask, "!*@", sizeof(TheMask));
299 strlcat(TheMask, at, sizeof(TheMask));
303 /* All parts (nick, user and domain name) are given */
304 strlcpy(TheMask, Pattern, sizeof(TheMask));
306 } /* Lists_MakeMask */
309 * Check if a client is listed in a list.
311 * @param h List head.
312 * @param Client Client to check.
313 * @return true if client is listed, false if not.
316 Lists_Check( struct list_head *h, CLIENT *Client)
318 struct list_elem *e, *last, *next;
327 if (e->valid_until > 1 && e->valid_until < time(NULL)) {
328 /* Entry is expired, delete it */
329 LogDebug("Deleted \"%s\" from list (expired).",
331 Lists_Unlink(h, last, e);
335 if (Match(e->mask, Client_Mask(Client))) {
336 if (e->valid_until == 1) {
337 /* Entry is valid only once, delete it */
338 LogDebug("Deleted \"%s\" from list (used).",
340 Lists_Unlink(h, last, e);