]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/lists.c
Limit channel invite and ban lists to 50 entries
[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 empty string ("").
64  */
65 GLOBAL const char *
66 Lists_GetReason(const struct list_elem *e)
67 {
68         assert(e != NULL);
69         return e->reason ? 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         e = Lists_CheckDupeMask(h, Mask);
130         if (e) {
131                 e->valid_until = ValidUntil;
132                 if (Reason) {
133                         free(e->reason);
134                         e->reason = strdup(Reason);
135                 }
136                 return true;
137         }
138
139         e = Lists_GetFirst(h);
140
141         newelem = malloc(sizeof(struct list_elem));
142         if (!newelem) {
143                 Log(LOG_EMERG,
144                     "Can't allocate memory for new list entry!");
145                 return false;
146         }
147
148         strlcpy(newelem->mask, Mask, sizeof(newelem->mask));
149         if (Reason) {
150                 newelem->reason = malloc(strlen(Reason) + 1);
151                 if (newelem->reason)
152                         strlcpy(newelem->reason, Reason, strlen(Reason) + 1);
153                 else
154                         Log(LOG_EMERG,
155                             "Can't allocate memory for new list reason text!");
156         }
157         else
158                 newelem->reason = NULL;
159         newelem->valid_until = ValidUntil;
160         newelem->next = e;
161         h->first = newelem;
162
163         return true;
164 }
165
166 /**
167  * Delete a list element from a list.
168  *
169  * @param h List head.
170  * @param p Pointer to previous list element or NULL, if there is none.
171  * @param victim List element to delete.
172  */
173 static void
174 Lists_Unlink(struct list_head *h, struct list_elem *p, struct list_elem *victim)
175 {
176         assert(victim != NULL);
177         assert(h != NULL);
178
179         if (p)
180                 p->next = victim->next;
181         else
182                 h->first = victim->next;
183
184         if (victim->reason)
185                 free(victim->reason);
186
187         free(victim);
188 }
189
190 /**
191  * Delete a given IRC mask from a list.
192  *
193  * @param h List head.
194  * @param Mask IRC mask to delete from the list.
195  */
196 GLOBAL void
197 Lists_Del(struct list_head *h, const char *Mask)
198 {
199         struct list_elem *e, *last, *victim;
200
201         assert(h != NULL);
202         assert(Mask != NULL);
203
204         last = NULL;
205         e = Lists_GetFirst(h);
206         while (e) {
207                 if (strcasecmp(e->mask, Mask) == 0) {
208                         LogDebug("Deleted \"%s\" from list", e->mask);
209                         victim = e;
210                         e = victim->next;
211                         Lists_Unlink(h, last, victim);
212                         continue;
213                 }
214                 last = e;
215                 e = e->next;
216         }
217 }
218
219 /**
220  * Free a complete list.
221  *
222  * @param head List head.
223  */
224 GLOBAL void
225 Lists_Free(struct list_head *head)
226 {
227         struct list_elem *e, *victim;
228
229         assert(head != NULL);
230
231         e = head->first;
232         head->first = NULL;
233         while (e) {
234                 LogDebug("Deleted \"%s\" from list" , e->mask);
235                 victim = e;
236                 e = e->next;
237                 if (victim->reason)
238                         free(victim->reason);
239                 free(victim);
240         }
241 }
242
243 /**
244  * Check if an IRC mask is already contained in a list.
245  *
246  * @param h List head.
247  * @param Mask IRC mask to test.
248  * @return true if mask is already stored in the list, false otherwise.
249  */
250 GLOBAL struct list_elem *
251 Lists_CheckDupeMask(const struct list_head *h, const char *Mask )
252 {
253         struct list_elem *e;
254         e = h->first;
255         while (e) {
256                 if (strcasecmp(e->mask, Mask) == 0)
257                         return e;
258                 e = e->next;
259         }
260         return NULL;
261 }
262
263 /**
264  * Generate a valid IRC mask from "any" string given.
265  *
266  * Attention: This mask is only valid until the next call to Lists_MakeMask(),
267  * because a single global buffer ist used! You have to copy the generated
268  * mask to some sane location yourself!
269  *
270  * @param Pattern Source string to generate an IRC mask for.
271  * @return Pointer to global result buffer.
272  */
273 GLOBAL const char *
274 Lists_MakeMask(const char *Pattern)
275 {
276         static char TheMask[MASK_LEN];
277         char *excl, *at;
278
279         assert(Pattern != NULL);
280
281         excl = strchr(Pattern, '!');
282         at = strchr(Pattern, '@');
283
284         if (at && at < excl)
285                 excl = NULL;
286
287         if (!at && !excl) {
288                 /* Neither "!" nor "@" found: use string as nick name */
289                 strlcpy(TheMask, Pattern, sizeof(TheMask) - 5);
290                 strlcat(TheMask, "!*@*", sizeof(TheMask));
291                 return TheMask;
292         }
293
294         if (!at && excl) {
295                 /* Domain part is missing */
296                 strlcpy(TheMask, Pattern, sizeof(TheMask) - 3);
297                 strlcat(TheMask, "@*", sizeof(TheMask));
298                 return TheMask;
299         }
300
301         if (at && !excl) {
302                 /* User name is missing */
303                 *at = '\0'; at++;
304                 strlcpy(TheMask, Pattern, sizeof(TheMask) - 5);
305                 strlcat(TheMask, "!*@", sizeof(TheMask));
306                 strlcat(TheMask, at, sizeof(TheMask));
307                 return TheMask;
308         }
309
310         /* All parts (nick, user and domain name) are given */
311         strlcpy(TheMask, Pattern, sizeof(TheMask));
312         return TheMask;
313 } /* Lists_MakeMask */
314
315 /**
316  * Check if a client is listed in a list.
317  *
318  * @param h List head.
319  * @param Client Client to check.
320  * @return true if client is listed, false if not.
321  */
322 bool
323 Lists_Check( struct list_head *h, CLIENT *Client)
324 {
325         struct list_elem *e, *last, *next;
326
327         assert(h != NULL);
328
329         e = h->first;
330         last = NULL;
331
332         while (e) {
333                 next = e->next;
334                 if (Match(e->mask, Client_Mask(Client))) {
335                         if (e->valid_until == 1) {
336                                 /* Entry is valid only once, delete it */
337                                 LogDebug("Deleted \"%s\" from list (used).",
338                                          e->mask);
339                                 Lists_Unlink(h, last, e);
340                         }
341                         return true;
342                 }
343                 last = e;
344                 e = next;
345         }
346
347         return false;
348 }
349
350 /**
351  * Check list and purge expired entries.
352  *
353  * @param h List head.
354  */
355 GLOBAL void
356 Lists_Expire(struct list_head *h, const char *ListName)
357 {
358         struct list_elem *e, *last, *next;
359         time_t now;
360
361         assert(h != NULL);
362
363         e = h->first;
364         last = NULL;
365         now = time(NULL);
366
367         while (e) {
368                 next = e->next;
369                 if (e->valid_until > 1 && e->valid_until < now) {
370                         /* Entry is expired, delete it */
371                         if (e->reason)
372                                 Log(LOG_INFO,
373                                     "Deleted \"%s\" (\"%s\") from %s list (expired).",
374                                     e->mask, e->reason, ListName);
375                         else
376                                 Log(LOG_INFO,
377                                     "Deleted \"%s\" from %s list (expired).",
378                                     e->mask, ListName);
379                         Lists_Unlink(h, last, e);
380                         e = next;
381                         continue;
382                 }
383                 last = e;
384                 e = next;
385         }
386 }
387
388 /**
389  * Return the number of entries of a list.
390  *
391  * @param h List head.
392  * @return Number of items.
393  */
394 GLOBAL unsigned long
395 Lists_Count(struct list_head *h)
396 {
397         struct list_elem *e;
398         unsigned long count = 0;
399
400         assert(h != NULL);
401
402         e = h->first;
403         while (e) {
404                 count++;
405                 e = e->next;
406         }
407         return count;
408 }
409
410 /* -eof- */