]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/lists.c
Code cleanup: mostly removing empty lines
[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 #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 #define MASK_LEN        (2*CLIENT_HOST_LEN)
38
39 struct list_elem {
40         struct list_elem *next;
41         char mask[MASK_LEN];
42         bool onlyonce;
43 };
44
45
46 GLOBAL const char *
47 Lists_GetMask(const struct list_elem *e)
48 {
49         return e->mask;
50 }
51
52
53 GLOBAL struct list_elem*
54 Lists_GetFirst(const struct list_head *h)
55 {
56         return h->first;
57 }
58
59
60 GLOBAL struct list_elem*
61 Lists_GetNext(const struct list_elem *e)
62 {
63         return e->next;
64 }
65
66
67 bool
68 Lists_Add(struct list_head *header, const char *Mask, bool OnlyOnce )
69 {
70         struct list_elem *e, *newelem;
71
72         assert( header != NULL );
73         assert( Mask != NULL );
74
75         if (Lists_CheckDupeMask(header, Mask )) return true;
76
77         e = Lists_GetFirst(header);
78
79         newelem = malloc(sizeof(struct list_elem));
80         if( ! newelem ) {
81                 Log( LOG_EMERG, "Can't allocate memory for new Ban/Invite entry!" );
82                 return false;
83         }
84
85         strlcpy( newelem->mask, Mask, sizeof( newelem->mask ));
86         newelem->onlyonce = OnlyOnce;
87         newelem->next = e;
88         header->first = newelem;
89
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 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- */