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