]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/lists.c
Add Doxygen @file documentation to each source and header file
[ngircd-alex.git] / src / ngircd / lists.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2005 Alexander Barton (alex@barton.de)
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
13 #include "portab.h"
14
15 /**
16  * @file
17  * Management of IRC lists: ban, invite, etc.
18  */
19
20 #include "imp.h"
21 #include <assert.h>
22
23 #include "defines.h"
24 #include "conn.h"
25 #include "channel.h"
26 #include "log.h"
27 #include "match.h"
28 #include "messages.h"
29 #include "irc-write.h"
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <strings.h>
34
35 #include "exp.h"
36 #include "lists.h"
37
38 #define MASK_LEN        (2*CLIENT_HOST_LEN)
39
40 struct list_elem {
41         struct list_elem *next;
42         char mask[MASK_LEN];
43         bool onlyonce;
44 };
45
46
47 GLOBAL const char *
48 Lists_GetMask(const struct list_elem *e)
49 {
50         return e->mask;
51 }
52
53
54 GLOBAL struct list_elem*
55 Lists_GetFirst(const struct list_head *h)
56 {
57         return h->first;
58 }
59
60
61 GLOBAL struct list_elem*
62 Lists_GetNext(const struct list_elem *e)
63 {
64         return e->next;
65 }
66
67
68 bool
69 Lists_Add(struct list_head *header, const char *Mask, bool OnlyOnce )
70 {
71         struct list_elem *e, *newelem;
72
73         assert( header != NULL );
74         assert( Mask != NULL );
75
76         if (Lists_CheckDupeMask(header, Mask )) return true;
77
78         e = Lists_GetFirst(header);
79
80         newelem = malloc(sizeof(struct list_elem));
81         if( ! newelem ) {
82                 Log( LOG_EMERG, "Can't allocate memory for new Ban/Invite entry!" );
83                 return false;
84         }
85
86         strlcpy( newelem->mask, Mask, sizeof( newelem->mask ));
87         newelem->onlyonce = OnlyOnce;
88         newelem->next = e;
89         header->first = newelem;
90
91         return true;
92 }
93
94
95 static void
96 Lists_Unlink(struct list_head *header, struct list_elem *p, struct list_elem *victim)
97 {
98         assert(victim != NULL);
99         assert(header != NULL);
100
101         if (p) p->next = victim->next;
102         else header->first = victim->next;
103
104         free(victim);
105 }
106
107
108 GLOBAL void
109 Lists_Del(struct list_head *header, const char *Mask)
110 {
111         struct list_elem *e, *last, *victim;
112
113         assert( header != NULL );
114         assert( Mask != NULL );
115
116         last = NULL;
117         e = Lists_GetFirst(header);
118         while( e ) {
119                 if(strcasecmp( e->mask, Mask ) == 0 ) {
120                         LogDebug("Deleted \"%s\" from list", e->mask);
121                         victim = e;
122                         e = victim->next;
123                         Lists_Unlink(header, last, victim);
124                         continue;
125                 }
126                 last = e;
127                 e = e->next;
128         }
129 }
130
131
132 GLOBAL void
133 Lists_Free(struct list_head *head)
134 {
135         struct list_elem *e, *victim;
136
137         assert(head != NULL);
138
139         e = head->first;
140         head->first = NULL;
141         while (e) {
142                 LogDebug("Deleted \"%s\" from invite list" , e->mask);
143                 victim = e;
144                 e = e->next;
145                 free( victim );
146         }
147 }
148
149
150 GLOBAL bool
151 Lists_CheckDupeMask(const struct list_head *h, const char *Mask )
152 {
153         struct list_elem *e;
154         e = h->first;
155         while (e) {
156                 if (strcasecmp( e->mask, Mask ) == 0 )
157                         return true;
158                 e = e->next;
159         }
160         return false;
161 }
162
163
164 GLOBAL const char *
165 Lists_MakeMask(const char *Pattern)
166 {
167         /* This function generats a valid IRC mask of "any" string. This
168          * mask is only valid until the next call to Lists_MakeMask(),
169          * because a single global buffer is used. You have to copy the
170          * generated mask to some sane location yourself! */
171
172         static char TheMask[MASK_LEN];
173         char *excl, *at;
174
175         assert( Pattern != NULL );
176
177         excl = strchr( Pattern, '!' );
178         at = strchr( Pattern, '@' );
179
180         if(( at ) && ( at < excl )) excl = NULL;
181
182         if(( ! at ) && ( ! excl ))
183         {
184                 /* Neither "!" nor "@" found: use string as nick name */
185                 strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
186                 strlcat( TheMask, "!*@*", sizeof( TheMask ));
187                 return TheMask;
188         }
189
190         if(( ! at ) && ( excl ))
191         {
192                 /* Domain part is missing */
193                 strlcpy( TheMask, Pattern, sizeof( TheMask ) - 3 );
194                 strlcat( TheMask, "@*", sizeof( TheMask ));
195                 return TheMask;
196         }
197
198         if(( at ) && ( ! excl ))
199         {
200                 /* User name is missing */
201                 *at = '\0'; at++;
202                 strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
203                 strlcat( TheMask, "!*@", sizeof( TheMask ));
204                 strlcat( TheMask, at, sizeof( TheMask ));
205                 return TheMask;
206         }
207
208         /* All parts (nick, user and domain name) are given */
209         strlcpy( TheMask, Pattern, sizeof( TheMask ));
210         return TheMask;
211 } /* Lists_MakeMask */
212
213
214
215 bool
216 Lists_Check( struct list_head *header, CLIENT *Client)
217 {
218         struct list_elem *e, *last;
219
220         assert( header != NULL );
221
222         e = header->first;
223         last = NULL;
224
225         while( e ) {
226                 if( Match( e->mask, Client_Mask( Client ))) {
227                         if( e->onlyonce ) { /* delete entry */
228                                 LogDebug("Deleted \"%s\" from list", e->mask);
229                                 Lists_Unlink(header, last, e);
230                         }
231                         return true;
232                 }
233                 last = e;
234                 e = e->next;
235         }
236
237         return false;
238 }
239
240 /* -eof- */