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