]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/lists.c
Send "fake '*' key" in "MODE -k" replies
[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 struct list_elem {
38         struct list_elem *next; /** pointer to next list element */
39         char mask[MASK_LEN];    /** IRC mask */
40         char *reason;           /** Optional "reason" text */
41         time_t valid_until;     /** 0: unlimited; 1: once; t(>1): until t */
42 };
43
44 /**
45  * Get IRC mask stored in list element.
46  *
47  * @param list_elem List element.
48  * @return Pointer to IRC mask
49  */
50 GLOBAL const char *
51 Lists_GetMask(const struct list_elem *e)
52 {
53         assert(e != NULL);
54         return e->mask;
55 }
56
57 /**
58  * Get optional "reason" text stored in list element.
59  *
60  * @param list_elem List element.
61  * @return Pointer to "reason" text or empty string ("").
62  */
63 GLOBAL const char *
64 Lists_GetReason(const struct list_elem *e)
65 {
66         assert(e != NULL);
67         return e->reason ? e->reason : "";
68 }
69
70 /**
71  * Get "validity" value stored in list element.
72  *
73  * @param list_elem List element.
74  * @return Validity: 0=unlimited, 1=once, >1 until this time stamp.
75  */
76 GLOBAL time_t
77 Lists_GetValidity(const struct list_elem *e)
78 {
79         assert(e != NULL);
80         return e->valid_until;
81 }
82
83 /**
84  * Get first list element of a list.
85  *
86  * @param h List head.
87  * @return Pointer to first list element.
88  */
89 GLOBAL struct list_elem*
90 Lists_GetFirst(const struct list_head *h)
91 {
92         assert(h != NULL);
93         return h->first;
94 }
95
96 /**
97  * Get next list element of a list.
98  *
99  * @param e Current list element.
100  * @return Pointer to next list element.
101  */
102 GLOBAL struct list_elem*
103 Lists_GetNext(const struct list_elem *e)
104 {
105         assert(e != NULL);
106         return e->next;
107 }
108
109 /**
110  * Add a new mask to a list.
111  *
112  * @param h List head.
113  * @param Mask The IRC mask to add to the list.
114  * @param ValidUntil 0: unlimited, 1: only once, t>1: until given time_t.
115  * @param Reason Reason string or NULL, if no reason should be saved.
116  * @return true on success, false otherwise.
117  */
118 bool
119 Lists_Add(struct list_head *h, const char *Mask, time_t ValidUntil,
120           const char *Reason)
121 {
122         struct list_elem *e, *newelem;
123
124         assert(h != NULL);
125         assert(Mask != NULL);
126
127         e = Lists_CheckDupeMask(h, Mask);
128         if (e) {
129                 e->valid_until = ValidUntil;
130                 if (Reason) {
131                         if (e->reason)
132                                 free(e->reason);
133                         e->reason = strdup(Reason);
134                 }
135                 return true;
136         }
137
138         e = Lists_GetFirst(h);
139
140         newelem = malloc(sizeof(struct list_elem));
141         if (!newelem) {
142                 Log(LOG_EMERG,
143                     "Can't allocate memory for new list entry!");
144                 return false;
145         }
146
147         strlcpy(newelem->mask, Mask, sizeof(newelem->mask));
148         if (Reason) {
149                 newelem->reason = strdup(Reason);
150                 if (!newelem->reason)
151                         Log(LOG_EMERG,
152                             "Can't allocate memory for new list reason text!");
153         }
154         else
155                 newelem->reason = NULL;
156         newelem->valid_until = ValidUntil;
157         newelem->next = e;
158         h->first = newelem;
159
160         return true;
161 }
162
163 /**
164  * Delete a list element from a list.
165  *
166  * @param h List head.
167  * @param p Pointer to previous list element or NULL, if there is none.
168  * @param victim List element to delete.
169  */
170 static void
171 Lists_Unlink(struct list_head *h, struct list_elem *p, struct list_elem *victim)
172 {
173         assert(victim != NULL);
174         assert(h != NULL);
175
176         if (p)
177                 p->next = victim->next;
178         else
179                 h->first = victim->next;
180
181         if (victim->reason)
182                 free(victim->reason);
183
184         free(victim);
185 }
186
187 /**
188  * Delete a given IRC mask from a list.
189  *
190  * @param h List head.
191  * @param Mask IRC mask to delete from the list.
192  */
193 GLOBAL void
194 Lists_Del(struct list_head *h, const char *Mask)
195 {
196         struct list_elem *e, *last, *victim;
197
198         assert(h != NULL);
199         assert(Mask != NULL);
200
201         last = NULL;
202         e = Lists_GetFirst(h);
203         while (e) {
204                 if (strcasecmp(e->mask, Mask) == 0) {
205                         LogDebug("Deleted \"%s\" from list", e->mask);
206                         victim = e;
207                         e = victim->next;
208                         Lists_Unlink(h, last, victim);
209                         continue;
210                 }
211                 last = e;
212                 e = e->next;
213         }
214 }
215
216 /**
217  * Free a complete list.
218  *
219  * @param head List head.
220  */
221 GLOBAL void
222 Lists_Free(struct list_head *head)
223 {
224         struct list_elem *e, *victim;
225
226         assert(head != NULL);
227
228         e = head->first;
229         head->first = NULL;
230         while (e) {
231                 LogDebug("Deleted \"%s\" from list" , e->mask);
232                 victim = e;
233                 e = e->next;
234                 if (victim->reason)
235                         free(victim->reason);
236                 free(victim);
237         }
238 }
239
240 /**
241  * Check if an IRC mask is already contained in a list.
242  *
243  * @param h List head.
244  * @param Mask IRC mask to test.
245  * @return true if mask is already stored in the list, false otherwise.
246  */
247 GLOBAL struct list_elem *
248 Lists_CheckDupeMask(const struct list_head *h, const char *Mask )
249 {
250         struct list_elem *e;
251         e = h->first;
252         while (e) {
253                 if (strcasecmp(e->mask, Mask) == 0)
254                         return e;
255                 e = e->next;
256         }
257         return NULL;
258 }
259
260 /**
261  * Generate a valid IRC mask from "any" string given.
262  *
263  * @param Pattern Source string to generate an IRC mask for.
264  * @param mask    Buffer to store the mask.
265  * @param len     Size of the buffer.
266  */
267 GLOBAL void
268 Lists_MakeMask(const char *Pattern, char *mask, size_t len)
269 {
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 nickname */
282                 strlcpy(mask, Pattern, len - 5);
283                 strlcat(mask, "!*@*", len);
284         } else if (!at && excl) {
285                 /* Domain part is missing */
286                 strlcpy(mask, Pattern, len - 3);
287                 strlcat(mask, "@*", len);
288         } else if (at && !excl) {
289                 /* User name is missing */
290                 *at = '\0'; at++;
291                 strlcpy(mask, Pattern, len - 5);
292                 strlcat(mask, "!*@", len);
293                 strlcat(mask, at, len);
294         } else {
295                 /* All parts (nick, user and domain name) are given */
296                 strlcpy(mask, Pattern, len);
297         }
298 } /* Lists_MakeMask */
299
300 /**
301  * Check if a client is listed in a list.
302  *
303  * @param h List head.
304  * @param Client Client to check.
305  * @return true if client is listed, false if not.
306  */
307 bool
308 Lists_Check(struct list_head *h, CLIENT *Client)
309 {
310         return Lists_CheckReason(h, Client, NULL, 0);
311 }
312
313 /**
314  * Check if a client is listed in a list and store the reason.
315  *
316  * @param h      List head.
317  * @param Client Client to check.
318  * @param reason Buffer to store the reason.
319  * @param len    Size of the buffer if reason should be saved.
320  * @return true if client is listed, false if not.
321  */
322 bool
323 Lists_CheckReason(struct list_head *h, CLIENT *Client, char *reason, size_t len)
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_MaskCloaked(Client))) {
335                         if (len && e->reason)
336                                 strlcpy(reason, e->reason, len);
337                         if (e->valid_until == 1) {
338                                 /* Entry is valid only once, delete it */
339                                 LogDebug("Deleted \"%s\" from list (used).",
340                                          e->mask);
341                                 Lists_Unlink(h, last, e);
342                         }
343                         return true;
344                 }
345                 last = e;
346                 e = next;
347         }
348
349         return false;
350 }
351
352 /**
353  * Check list and purge expired entries.
354  *
355  * @param h List head.
356  */
357 GLOBAL void
358 Lists_Expire(struct list_head *h, const char *ListName)
359 {
360         struct list_elem *e, *last, *next;
361         time_t now;
362
363         assert(h != NULL);
364
365         e = h->first;
366         last = NULL;
367         now = time(NULL);
368
369         while (e) {
370                 next = e->next;
371                 if (e->valid_until > 1 && e->valid_until < now) {
372                         /* Entry is expired, delete it */
373                         if (e->reason)
374                                 Log(LOG_INFO,
375                                     "Deleted \"%s\" (\"%s\") from %s list (expired).",
376                                     e->mask, e->reason, ListName);
377                         else
378                                 Log(LOG_INFO,
379                                     "Deleted \"%s\" from %s list (expired).",
380                                     e->mask, ListName);
381                         Lists_Unlink(h, last, e);
382                         e = next;
383                         continue;
384                 }
385                 last = e;
386                 e = next;
387         }
388 }
389
390 /**
391  * Return the number of entries of a list.
392  *
393  * @param h List head.
394  * @return Number of items.
395  */
396 GLOBAL unsigned long
397 Lists_Count(struct list_head *h)
398 {
399         struct list_elem *e;
400         unsigned long count = 0;
401
402         assert(h != NULL);
403
404         e = h->first;
405         while (e) {
406                 count++;
407                 e = e->next;
408         }
409         return count;
410 }
411
412 /* -eof- */