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