]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/lists.c
New functions Lists_Expire() and Class_Expire()
[ngircd-alex.git] / src / ngircd / lists.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2011 Alexander Barton (alex@barton.de) and Contributors.
4  *
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.
10  */
11
12 #include "portab.h"
13
14 /**
15  * @file
16  * Management of IRC lists: ban, invite, etc.
17  */
18
19 #include "imp.h"
20 #include <assert.h>
21
22 #include "defines.h"
23 #include "conn.h"
24 #include "channel.h"
25 #include "log.h"
26 #include "match.h"
27 #include "messages.h"
28 #include "irc-write.h"
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <strings.h>
33
34 #include "exp.h"
35 #include "lists.h"
36
37 #define MASK_LEN        (2*CLIENT_HOST_LEN)
38
39 struct list_elem {
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 */
44 };
45
46 /**
47  * Get IRC mask stored in list element.
48  *
49  * @param list_elem List element.
50  * @return Pointer to IRC mask
51  */
52 GLOBAL const char *
53 Lists_GetMask(const struct list_elem *e)
54 {
55         assert(e != NULL);
56         return e->mask;
57 }
58
59 /**
60  * Get optional "reason" text stored in list element.
61  *
62  * @param list_elem List element.
63  * @return Pointer to "reason" text or NULL.
64  */
65 GLOBAL const char *
66 Lists_GetReason(const struct list_elem *e)
67 {
68         assert(e != NULL);
69         return e->reason;
70 }
71
72 /**
73  * Get "validity" value stored in list element.
74  *
75  * @param list_elem List element.
76  * @return Validity: 0=unlimited, 1=once, >1 until this time stamp.
77  */
78 GLOBAL time_t
79 Lists_GetValidity(const struct list_elem *e)
80 {
81         assert(e != NULL);
82         return e->valid_until;
83 }
84
85 /**
86  * Get first list element of a list.
87  *
88  * @param h List head.
89  * @return Pointer to first list element.
90  */
91 GLOBAL struct list_elem*
92 Lists_GetFirst(const struct list_head *h)
93 {
94         assert(h != NULL);
95         return h->first;
96 }
97
98 /**
99  * Get next list element of a list.
100  *
101  * @param e Current list element.
102  * @return Pointer to next list element.
103  */
104 GLOBAL struct list_elem*
105 Lists_GetNext(const struct list_elem *e)
106 {
107         assert(e != NULL);
108         return e->next;
109 }
110
111 /**
112  * Add a new mask to a list.
113  *
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.
119  */
120 bool
121 Lists_Add(struct list_head *h, const char *Mask, time_t ValidUntil,
122           const char *Reason)
123 {
124         struct list_elem *e, *newelem;
125
126         assert(h != NULL);
127         assert(Mask != NULL);
128
129         if (Lists_CheckDupeMask(h, Mask))
130                 return true;
131
132         e = Lists_GetFirst(h);
133
134         newelem = malloc(sizeof(struct list_elem));
135         if (!newelem) {
136                 Log(LOG_EMERG,
137                     "Can't allocate memory for new list entry!");
138                 return false;
139         }
140
141         strlcpy(newelem->mask, Mask, sizeof(newelem->mask));
142         if (Reason) {
143                 newelem->reason = malloc(strlen(Reason) + 1);
144                 if (newelem->reason)
145                         strlcpy(newelem->reason, Reason, strlen(Reason) + 1);
146                 else
147                         Log(LOG_EMERG,
148                             "Can't allocate memory for new list reason text!");
149         }
150         else
151                 newelem->reason = NULL;
152         newelem->valid_until = ValidUntil;
153         newelem->next = e;
154         h->first = newelem;
155
156         return true;
157 }
158
159 /**
160  * Delete a list element from a list.
161  *
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.
165  */
166 static void
167 Lists_Unlink(struct list_head *h, struct list_elem *p, struct list_elem *victim)
168 {
169         assert(victim != NULL);
170         assert(h != NULL);
171
172         if (p)
173                 p->next = victim->next;
174         else
175                 h->first = victim->next;
176
177         if (victim->reason)
178                 free(victim->reason);
179
180         free(victim);
181 }
182
183 /**
184  * Delete a given IRC mask from a list.
185  *
186  * @param h List head.
187  * @param Mask IRC mask to delete from the list.
188  */
189 GLOBAL void
190 Lists_Del(struct list_head *h, const char *Mask)
191 {
192         struct list_elem *e, *last, *victim;
193
194         assert(h != NULL);
195         assert(Mask != NULL);
196
197         last = NULL;
198         e = Lists_GetFirst(h);
199         while (e) {
200                 if (strcasecmp(e->mask, Mask) == 0) {
201                         LogDebug("Deleted \"%s\" from list", e->mask);
202                         victim = e;
203                         e = victim->next;
204                         Lists_Unlink(h, last, victim);
205                         continue;
206                 }
207                 last = e;
208                 e = e->next;
209         }
210 }
211
212 /**
213  * Free a complete list.
214  *
215  * @param head List head.
216  */
217 GLOBAL void
218 Lists_Free(struct list_head *head)
219 {
220         struct list_elem *e, *victim;
221
222         assert(head != NULL);
223
224         e = head->first;
225         head->first = NULL;
226         while (e) {
227                 LogDebug("Deleted \"%s\" from list" , e->mask);
228                 victim = e;
229                 e = e->next;
230                 if (victim->reason)
231                         free(victim->reason);
232                 free(victim);
233         }
234 }
235
236 /**
237  * Check if an IRC mask is already contained in a list.
238  *
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.
242  */
243 GLOBAL bool
244 Lists_CheckDupeMask(const struct list_head *h, const char *Mask )
245 {
246         struct list_elem *e;
247         e = h->first;
248         while (e) {
249                 if (strcasecmp(e->mask, Mask) == 0)
250                         return true;
251                 e = e->next;
252         }
253         return false;
254 }
255
256 /**
257  * Generate a valid IRC mask from "any" string given.
258  *
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!
262  *
263  * @param Pattern Source string to generate an IRC mask for.
264  * @return Pointer to global result buffer.
265  */
266 GLOBAL const char *
267 Lists_MakeMask(const char *Pattern)
268 {
269         static char TheMask[MASK_LEN];
270         char *excl, *at;
271
272         assert(Pattern != NULL);
273
274         excl = strchr(Pattern, '!');
275         at = strchr(Pattern, '@');
276
277         if (at && at < excl)
278                 excl = NULL;
279
280         if (!at && !excl) {
281                 /* Neither "!" nor "@" found: use string as nick name */
282                 strlcpy(TheMask, Pattern, sizeof(TheMask) - 5);
283                 strlcat(TheMask, "!*@*", sizeof(TheMask));
284                 return TheMask;
285         }
286
287         if (!at && excl) {
288                 /* Domain part is missing */
289                 strlcpy(TheMask, Pattern, sizeof(TheMask) - 3);
290                 strlcat(TheMask, "@*", sizeof(TheMask));
291                 return TheMask;
292         }
293
294         if (at && !excl) {
295                 /* User name is missing */
296                 *at = '\0'; at++;
297                 strlcpy(TheMask, Pattern, sizeof(TheMask) - 5);
298                 strlcat(TheMask, "!*@", sizeof(TheMask));
299                 strlcat(TheMask, at, sizeof(TheMask));
300                 return TheMask;
301         }
302
303         /* All parts (nick, user and domain name) are given */
304         strlcpy(TheMask, Pattern, sizeof(TheMask));
305         return TheMask;
306 } /* Lists_MakeMask */
307
308 /**
309  * Check if a client is listed in a list.
310  *
311  * @param h List head.
312  * @param Client Client to check.
313  * @return true if client is listed, false if not.
314  */
315 bool
316 Lists_Check( struct list_head *h, CLIENT *Client)
317 {
318         struct list_elem *e, *last, *next;
319         time_t now;
320
321         assert(h != NULL);
322
323         e = h->first;
324         last = NULL;
325         now = time(NULL);
326
327         while (e) {
328                 next = e->next;
329                 if (Match(e->mask, Client_Mask(Client))) {
330                         if (e->valid_until == 1) {
331                                 /* Entry is valid only once, delete it */
332                                 LogDebug("Deleted \"%s\" from list (used).",
333                                          e->mask);
334                                 Lists_Unlink(h, last, e);
335                         }
336                         return true;
337                 }
338                 last = e;
339                 e = next;
340         }
341
342         return false;
343 }
344
345 /**
346  * Check list and purge expired entries.
347  *
348  * @param h List head.
349  */
350 GLOBAL void
351 Lists_Expire(struct list_head *h, const char *ListName)
352 {
353         struct list_elem *e, *last, *next;
354         time_t now;
355
356         assert(h != NULL);
357
358         e = h->first;
359         last = NULL;
360         now = time(NULL);
361
362         while (e) {
363                 next = e->next;
364                 if (e->valid_until > 1 && e->valid_until < now) {
365                         /* Entry is expired, delete it */
366                         if (e->reason)
367                                 Log(LOG_INFO,
368                                     "Deleted \"%s\" (\"%s\") from %s list (expired).",
369                                     e->mask, e->reason, ListName);
370                         else
371                                 Log(LOG_INFO,
372                                     "Deleted \"%s\" from %s list (expired).",
373                                     e->mask, ListName);
374                         Lists_Unlink(h, last, e);
375                         e = next;
376                         continue;
377                 }
378                 last = e;
379                 e = next;
380         }
381 }
382
383 /* -eof- */