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