Blob


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 */
15 #include "portab.h"
17 static char UNUSED id[] = "$Id: lists.c,v 1.17 2005/03/19 18:43:49 fw Exp $";
19 #include "imp.h"
20 #include <assert.h>
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"
31 #include <stdlib.h>
32 #include <string.h>
33 #include <strings.h>
35 #include "exp.h"
36 #include "lists.h"
39 #define MASK_LEN 2*CLIENT_HOST_LEN
42 typedef struct _C2C
43 {
44 struct _C2C *next;
45 char mask[MASK_LEN];
46 CHANNEL *channel;
47 bool onlyonce;
48 } C2C;
51 LOCAL C2C *My_Invites, *My_Bans;
54 LOCAL C2C *New_C2C PARAMS(( char *Mask, CHANNEL *Chan, bool OnlyOnce ));
56 LOCAL bool Check_List PARAMS(( C2C **Cl2Chan, CLIENT *Client, CHANNEL *Chan ));
57 LOCAL bool Already_Registered PARAMS(( C2C *Cl2Chan, char *Mask, CHANNEL *Chan ));
61 GLOBAL void
62 Lists_Init( void )
63 {
64 /* Modul initialisieren */
66 My_Invites = My_Bans = NULL;
67 } /* Lists_Init */
70 GLOBAL void
71 Lists_Exit( void )
72 {
73 /* Modul abmelden */
75 C2C *c2c, *next;
77 /* Invite-Lists freigeben */
78 c2c = My_Invites;
79 while( c2c )
80 {
81 next = c2c->next;
82 free( c2c );
83 c2c = next;
84 }
86 /* Ban-Lists freigeben */
87 c2c = My_Bans;
88 while( c2c )
89 {
90 next = c2c->next;
91 free( c2c );
92 c2c = next;
93 }
94 } /* Lists_Exit */
97 GLOBAL bool
98 Lists_CheckInvited( CLIENT *Client, CHANNEL *Chan )
99 {
100 return Check_List( &My_Invites, Client, Chan );
101 } /* Lists_CheckInvited */
104 GLOBAL bool
105 Lists_IsInviteEntry( char *Mask, CHANNEL *Chan )
107 assert( Mask != NULL );
108 assert( Chan != NULL );
110 return Already_Registered( My_Invites, Mask, Chan );
111 } /* Lists_IsInviteEntry */
114 GLOBAL bool
115 Lists_AddInvited( char *Mask, CHANNEL *Chan, bool OnlyOnce )
117 C2C *c2c;
119 assert( Mask != NULL );
120 assert( Chan != NULL );
122 if( Already_Registered( My_Invites, Mask, Chan )) return true;
124 c2c = New_C2C( Mask, Chan, OnlyOnce );
125 if( ! c2c )
127 Log( LOG_ERR, "Can't add new invite list entry!" );
128 return false;
131 /* verketten */
132 c2c->next = My_Invites;
133 My_Invites = c2c;
135 Log( LOG_DEBUG, "Added \"%s\" to invite list for \"%s\".", Mask, Channel_Name( Chan ));
136 return true;
137 } /* Lists_AddInvited */
140 GLOBAL void
141 Lists_DelInvited( char *Mask, CHANNEL *Chan )
143 C2C *c2c, *last, *next;
145 assert( Mask != NULL );
146 assert( Chan != NULL );
148 last = NULL;
149 c2c = My_Invites;
150 while( c2c )
152 next = c2c->next;
153 if(( c2c->channel == Chan ) && ( strcasecmp( c2c->mask, Mask ) == 0 ))
155 /* dieser Eintrag muss geloescht werden */
156 Log( LOG_DEBUG, "Deleted \"%s\" from invite list for \"%s\"." , c2c->mask, Channel_Name( Chan ));
157 if( last ) last->next = next;
158 else My_Invites = next;
159 free( c2c );
161 else last = c2c;
162 c2c = next;
164 } /* Lists_DelInvited */
167 GLOBAL bool
168 Lists_ShowInvites( CLIENT *Client, CHANNEL *Channel )
170 C2C *c2c;
172 assert( Client != NULL );
173 assert( Channel != NULL );
175 c2c = My_Invites;
176 while( c2c )
178 if( c2c->channel == Channel )
180 /* Eintrag fuer Channel gefunden; ausgeben: */
181 if( ! IRC_WriteStrClient( Client, RPL_INVITELIST_MSG, Client_ID( Client ), Channel_Name( Channel ), c2c->mask )) return DISCONNECTED;
183 c2c = c2c->next;
185 return IRC_WriteStrClient( Client, RPL_ENDOFINVITELIST_MSG, Client_ID( Client ), Channel_Name( Channel ));
186 } /* Lists_ShowInvites */
189 GLOBAL bool
190 Lists_SendInvites( CLIENT *Client )
192 C2C *c2c;
194 assert( Client != NULL );
196 c2c = My_Invites;
197 while( c2c )
199 if( ! IRC_WriteStrClient( Client, "MODE %s +I %s", Channel_Name( c2c->channel ), c2c->mask )) return DISCONNECTED;
200 c2c = c2c->next;
202 return CONNECTED;
203 } /* Lists_SendInvites */
206 GLOBAL bool
207 Lists_SendBans( CLIENT *Client )
209 C2C *c2c;
211 assert( Client != NULL );
213 c2c = My_Bans;
214 while( c2c )
216 if( ! IRC_WriteStrClient( Client, "MODE %s +b %s", Channel_Name( c2c->channel ), c2c->mask )) return DISCONNECTED;
217 c2c = c2c->next;
219 return CONNECTED;
220 } /* Lists_SendBans */
223 GLOBAL bool
224 Lists_CheckBanned( CLIENT *Client, CHANNEL *Chan )
226 return Check_List( &My_Bans, Client, Chan );
227 } /* Lists_CheckBanned */
230 GLOBAL bool
231 Lists_IsBanEntry( char *Mask, CHANNEL *Chan )
233 assert( Mask != NULL );
234 assert( Chan != NULL );
236 return Already_Registered( My_Bans, Mask, Chan );
237 } /* Lists_IsBanEntry */
240 GLOBAL bool
241 Lists_AddBanned( char *Mask, CHANNEL *Chan )
243 C2C *c2c;
245 assert( Mask != NULL );
246 assert( Chan != NULL );
248 if( Already_Registered( My_Bans, Mask, Chan )) return true;
250 c2c = New_C2C( Mask, Chan, false );
251 if( ! c2c )
253 Log( LOG_ERR, "Can't add new ban list entry!" );
254 return false;
257 /* verketten */
258 c2c->next = My_Bans;
259 My_Bans = c2c;
261 Log( LOG_DEBUG, "Added \"%s\" to ban list for \"%s\".", Mask, Channel_Name( Chan ));
262 return true;
263 } /* Lists_AddBanned */
266 GLOBAL void
267 Lists_DelBanned( char *Mask, CHANNEL *Chan )
269 C2C *c2c, *last, *next;
271 assert( Mask != NULL );
272 assert( Chan != NULL );
274 last = NULL;
275 c2c = My_Bans;
276 while( c2c )
278 next = c2c->next;
279 if(( c2c->channel == Chan ) && ( strcasecmp( c2c->mask, Mask ) == 0 ))
281 /* dieser Eintrag muss geloescht werden */
282 Log( LOG_DEBUG, "Deleted \"%s\" from ban list for \"%s\"." , c2c->mask, Channel_Name( Chan ));
283 if( last ) last->next = next;
284 else My_Bans = next;
285 free( c2c );
287 else last = c2c;
288 c2c = next;
290 } /* Lists_DelBanned */
293 GLOBAL bool
294 Lists_ShowBans( CLIENT *Client, CHANNEL *Channel )
296 C2C *c2c;
298 assert( Client != NULL );
299 assert( Channel != NULL );
301 c2c = My_Bans;
302 while( c2c )
304 if( c2c->channel == Channel )
306 /* Eintrag fuer Channel gefunden; ausgeben: */
307 if( ! IRC_WriteStrClient( Client, RPL_BANLIST_MSG, Client_ID( Client ), Channel_Name( Channel ), c2c->mask )) return DISCONNECTED;
309 c2c = c2c->next;
311 return IRC_WriteStrClient( Client, RPL_ENDOFBANLIST_MSG, Client_ID( Client ), Channel_Name( Channel ));
312 } /* Lists_ShowBans */
315 GLOBAL void
316 Lists_DeleteChannel( CHANNEL *Chan )
318 /* Channel wurde geloescht, Invite- und Ban-Lists aufraeumen */
320 C2C *c2c, *last, *next;
322 /* Invite-List */
323 last = NULL;
324 c2c = My_Invites;
325 while( c2c )
327 next = c2c->next;
328 if( c2c->channel == Chan )
330 /* dieser Eintrag muss geloescht werden */
331 Log( LOG_DEBUG, "Deleted \"%s\" from invite list for \"%s\"." , c2c->mask, Channel_Name( Chan ));
332 if( last ) last->next = next;
333 else My_Invites = next;
334 free( c2c );
336 else last = c2c;
337 c2c = next;
340 /* Ban-List */
341 last = NULL;
342 c2c = My_Bans;
343 while( c2c )
345 next = c2c->next;
346 if( c2c->channel == Chan )
348 /* dieser Eintrag muss geloescht werden */
349 Log( LOG_DEBUG, "Deleted \"%s\" from ban list for \"%s\"." , c2c->mask, Channel_Name( Chan ));
350 if( last ) last->next = next;
351 else My_Bans = next;
352 free( c2c );
354 else last = c2c;
355 c2c = next;
357 } /* Lists_DeleteChannel */
360 GLOBAL char *
361 Lists_MakeMask( char *Pattern )
363 /* This function generats a valid IRC mask of "any" string. This
364 * mask is only valid until the next call to Lists_MakeMask(),
365 * because a single global buffer is used. You have to copy the
366 * generated mask to some sane location yourself! */
368 static char TheMask[MASK_LEN];
369 char *excl, *at;
371 assert( Pattern != NULL );
373 excl = strchr( Pattern, '!' );
374 at = strchr( Pattern, '@' );
376 if(( at ) && ( at < excl )) excl = NULL;
378 if(( ! at ) && ( ! excl ))
380 /* Neither "!" nor "@" found: use string as nick name */
381 strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
382 strlcat( TheMask, "!*@*", sizeof( TheMask ));
383 return TheMask;
386 if(( ! at ) && ( excl ))
388 /* Domain part is missing */
389 strlcpy( TheMask, Pattern, sizeof( TheMask ) - 3 );
390 strlcat( TheMask, "@*", sizeof( TheMask ));
391 return TheMask;
394 if(( at ) && ( ! excl ))
396 /* User name is missing */
397 *at = '\0'; at++;
398 strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
399 strlcat( TheMask, "!*@", sizeof( TheMask ));
400 strlcat( TheMask, at, sizeof( TheMask ));
401 return TheMask;
404 /* All parts (nick, user and domain name) are given */
405 strlcpy( TheMask, Pattern, sizeof( TheMask ));
406 return TheMask;
407 } /* Lists_MakeMask */
410 LOCAL C2C *
411 New_C2C( char *Mask, CHANNEL *Chan, bool OnlyOnce )
413 C2C *c2c;
415 assert( Mask != NULL );
416 assert( Chan != NULL );
418 /* Speicher fuer Eintrag anfordern */
419 c2c = (C2C *)malloc( sizeof( C2C ));
420 if( ! c2c )
422 Log( LOG_EMERG, "Can't allocate memory! [New_C2C]" );
423 return NULL;
426 strlcpy( c2c->mask, Mask, sizeof( c2c->mask ));
427 c2c->channel = Chan;
428 c2c->onlyonce = OnlyOnce;
430 return c2c;
431 } /* New_C2C */
434 LOCAL bool
435 Check_List( C2C **Cl2Chan, CLIENT *Client, CHANNEL *Chan )
437 C2C *c2c, *last;
439 assert( Cl2Chan != NULL );
440 assert( Client != NULL );
441 assert( Chan != NULL );
443 c2c = *Cl2Chan;
444 last = NULL;
446 while( c2c )
448 if( c2c->channel == Chan )
450 /* Ok, richtiger Channel. Passt die Maske? */
451 if( Match( c2c->mask, Client_Mask( Client )))
453 /* Treffer! */
454 if( c2c->onlyonce )
456 /* Eintrag loeschen */
457 Log( LOG_DEBUG, "Deleted \"%s\" from %s list for \"%s\".", c2c->mask, *Cl2Chan == My_Invites ? "invite" : "ban", Channel_Name( Chan ));
458 if( last ) last->next = c2c->next;
459 else *Cl2Chan = c2c->next;
460 free( c2c );
462 return true;
465 last = c2c;
466 c2c = c2c->next;
469 return false;
470 } /* Check_List */
473 LOCAL bool
474 Already_Registered( C2C *List, char *Mask, CHANNEL *Chan )
476 C2C *c2c;
478 c2c = List;
479 while( c2c )
481 if(( c2c->channel == Chan ) && ( strcasecmp( c2c->mask, Mask ) == 0 )) return true;
482 c2c = c2c->next;
484 return false;
485 } /* Already_Registered */
488 /* -eof- */