]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/lists.c
Don't #include client.h when conn.h/conn-func.h is already included
[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  * 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         LogDebug("Added \"%s\" to invite list", Mask);
89         return true;
90 }
91
92
93 static void
94 Lists_Unlink(struct list_head *header, struct list_elem *p, struct list_elem *victim)
95 {
96         assert(victim != NULL);
97         assert(header != NULL);
98
99         if (p) p->next = victim->next;
100         else header->first = victim->next;
101
102         free(victim);
103 }
104
105
106 GLOBAL void
107 Lists_Del(struct list_head *header, const char *Mask)
108 {
109         struct list_elem *e, *last, *victim;
110
111         assert( header != NULL );
112         assert( Mask != NULL );
113
114         last = NULL;
115         e = Lists_GetFirst(header);
116         while( e ) {
117                 if(strcasecmp( e->mask, Mask ) == 0 ) {
118                         LogDebug("Deleted \"%s\" from list", e->mask);
119                         victim = e;
120                         e = victim->next;
121                         Lists_Unlink(header, last, victim);
122                         continue;
123                 }
124                 last = e;
125                 e = e->next;
126         }
127 }
128
129
130 GLOBAL void
131 Lists_Free(struct list_head *head)
132 {
133         struct list_elem *e, *victim;
134
135         assert(head != NULL);
136
137         e = head->first;
138         head->first = NULL;
139         while (e) {
140                 LogDebug("Deleted \"%s\" from invite list" , e->mask);
141                 victim = e;
142                 e = e->next;
143                 free( victim );
144         }
145 }
146
147
148 GLOBAL bool
149 Lists_CheckDupeMask(const struct list_head *h, const char *Mask )
150 {
151         struct list_elem *e;
152         e = h->first;
153         while (e) {
154                 if (strcasecmp( e->mask, Mask ) == 0 )
155                         return true;
156                 e = e->next;
157         }
158         return false;
159 }
160
161
162 GLOBAL const char *
163 Lists_MakeMask(const char *Pattern)
164 {
165         /* This function generats a valid IRC mask of "any" string. This
166          * mask is only valid until the next call to Lists_MakeMask(),
167          * because a single global buffer is used. You have to copy the
168          * generated mask to some sane location yourself! */
169
170         static char TheMask[MASK_LEN];
171         char *excl, *at;
172
173         assert( Pattern != NULL );
174
175         excl = strchr( Pattern, '!' );
176         at = strchr( Pattern, '@' );
177
178         if(( at ) && ( at < excl )) excl = NULL;
179
180         if(( ! at ) && ( ! excl ))
181         {
182                 /* Neither "!" nor "@" found: use string as nick name */
183                 strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
184                 strlcat( TheMask, "!*@*", sizeof( TheMask ));
185                 return TheMask;
186         }
187
188         if(( ! at ) && ( excl ))
189         {
190                 /* Domain part is missing */
191                 strlcpy( TheMask, Pattern, sizeof( TheMask ) - 3 );
192                 strlcat( TheMask, "@*", sizeof( TheMask ));
193                 return TheMask;
194         }
195
196         if(( at ) && ( ! excl ))
197         {
198                 /* User name is missing */
199                 *at = '\0'; at++;
200                 strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
201                 strlcat( TheMask, "!*@", sizeof( TheMask ));
202                 strlcat( TheMask, at, sizeof( TheMask ));
203                 return TheMask;
204         }
205
206         /* All parts (nick, user and domain name) are given */
207         strlcpy( TheMask, Pattern, sizeof( TheMask ));
208         return TheMask;
209 } /* Lists_MakeMask */
210
211
212
213 bool
214 Lists_Check( struct list_head *header, CLIENT *Client)
215 {
216         struct list_elem *e, *last;
217
218         assert( header != NULL );
219
220         e = header->first;
221         last = NULL;
222
223         while( e ) {
224                 if( Match( e->mask, Client_Mask( Client ))) {
225                         if( e->onlyonce ) { /* delete entry */
226                                 LogDebug("Deleted \"%s\" from list", e->mask);
227                                 Lists_Unlink(header, last, e);
228                         }
229                         return true;
230                 }
231                 last = e;
232                 e = e->next;
233         }
234
235         return false;
236 }
237
238 /* -eof- */