123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- /*
- * ngIRCd -- The Next Generation IRC Daemon
- * Copyright (c)2001-2005 Alexander Barton (alex@barton.de)
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- * Please read the file COPYING, README and AUTHORS for more information.
- *
- * Management of IRC lists: ban, invite, ...
- */
- #include "portab.h"
- static char UNUSED id[] = "$Id: lists.c,v 1.17 2005/03/19 18:43:49 fw Exp $";
- #include "imp.h"
- #include <assert.h>
- #include "defines.h"
- #include "conn.h"
- #include "client.h"
- #include "channel.h"
- #include "log.h"
- #include "match.h"
- #include "messages.h"
- #include "irc-write.h"
- #include <stdlib.h>
- #include <string.h>
- #include <strings.h>
- #include "exp.h"
- #include "lists.h"
- #define MASK_LEN 2*CLIENT_HOST_LEN
- typedef struct _C2C
- {
- struct _C2C *next;
- char mask[MASK_LEN];
- CHANNEL *channel;
- bool onlyonce;
- } C2C;
- LOCAL C2C *My_Invites, *My_Bans;
- LOCAL C2C *New_C2C PARAMS(( char *Mask, CHANNEL *Chan, bool OnlyOnce ));
- LOCAL bool Check_List PARAMS(( C2C **Cl2Chan, CLIENT *Client, CHANNEL *Chan ));
- LOCAL bool Already_Registered PARAMS(( C2C *Cl2Chan, char *Mask, CHANNEL *Chan ));
- GLOBAL void
- Lists_Init( void )
- {
- /* Modul initialisieren */
- My_Invites = My_Bans = NULL;
- } /* Lists_Init */
- GLOBAL void
- Lists_Exit( void )
- {
- /* Modul abmelden */
- C2C *c2c, *next;
- /* Invite-Lists freigeben */
- c2c = My_Invites;
- while( c2c )
- {
- next = c2c->next;
- free( c2c );
- c2c = next;
- }
- /* Ban-Lists freigeben */
- c2c = My_Bans;
- while( c2c )
- {
- next = c2c->next;
- free( c2c );
- c2c = next;
- }
- } /* Lists_Exit */
- GLOBAL bool
- Lists_CheckInvited( CLIENT *Client, CHANNEL *Chan )
- {
- return Check_List( &My_Invites, Client, Chan );
- } /* Lists_CheckInvited */
- GLOBAL bool
- Lists_IsInviteEntry( char *Mask, CHANNEL *Chan )
- {
- assert( Mask != NULL );
- assert( Chan != NULL );
-
- return Already_Registered( My_Invites, Mask, Chan );
- } /* Lists_IsInviteEntry */
- GLOBAL bool
- Lists_AddInvited( char *Mask, CHANNEL *Chan, bool OnlyOnce )
- {
- C2C *c2c;
- assert( Mask != NULL );
- assert( Chan != NULL );
- if( Already_Registered( My_Invites, Mask, Chan )) return true;
-
- c2c = New_C2C( Mask, Chan, OnlyOnce );
- if( ! c2c )
- {
- Log( LOG_ERR, "Can't add new invite list entry!" );
- return false;
- }
- /* verketten */
- c2c->next = My_Invites;
- My_Invites = c2c;
- Log( LOG_DEBUG, "Added \"%s\" to invite list for \"%s\".", Mask, Channel_Name( Chan ));
- return true;
- } /* Lists_AddInvited */
- GLOBAL void
- Lists_DelInvited( char *Mask, CHANNEL *Chan )
- {
- C2C *c2c, *last, *next;
- assert( Mask != NULL );
- assert( Chan != NULL );
- last = NULL;
- c2c = My_Invites;
- while( c2c )
- {
- next = c2c->next;
- if(( c2c->channel == Chan ) && ( strcasecmp( c2c->mask, Mask ) == 0 ))
- {
- /* dieser Eintrag muss geloescht werden */
- Log( LOG_DEBUG, "Deleted \"%s\" from invite list for \"%s\"." , c2c->mask, Channel_Name( Chan ));
- if( last ) last->next = next;
- else My_Invites = next;
- free( c2c );
- }
- else last = c2c;
- c2c = next;
- }
- } /* Lists_DelInvited */
- GLOBAL bool
- Lists_ShowInvites( CLIENT *Client, CHANNEL *Channel )
- {
- C2C *c2c;
- assert( Client != NULL );
- assert( Channel != NULL );
- c2c = My_Invites;
- while( c2c )
- {
- if( c2c->channel == Channel )
- {
- /* Eintrag fuer Channel gefunden; ausgeben: */
- if( ! IRC_WriteStrClient( Client, RPL_INVITELIST_MSG, Client_ID( Client ), Channel_Name( Channel ), c2c->mask )) return DISCONNECTED;
- }
- c2c = c2c->next;
- }
- return IRC_WriteStrClient( Client, RPL_ENDOFINVITELIST_MSG, Client_ID( Client ), Channel_Name( Channel ));
- } /* Lists_ShowInvites */
- GLOBAL bool
- Lists_SendInvites( CLIENT *Client )
- {
- C2C *c2c;
-
- assert( Client != NULL );
-
- c2c = My_Invites;
- while( c2c )
- {
- if( ! IRC_WriteStrClient( Client, "MODE %s +I %s", Channel_Name( c2c->channel ), c2c->mask )) return DISCONNECTED;
- c2c = c2c->next;
- }
- return CONNECTED;
- } /* Lists_SendInvites */
- GLOBAL bool
- Lists_SendBans( CLIENT *Client )
- {
- C2C *c2c;
-
- assert( Client != NULL );
-
- c2c = My_Bans;
- while( c2c )
- {
- if( ! IRC_WriteStrClient( Client, "MODE %s +b %s", Channel_Name( c2c->channel ), c2c->mask )) return DISCONNECTED;
- c2c = c2c->next;
- }
- return CONNECTED;
- } /* Lists_SendBans */
- GLOBAL bool
- Lists_CheckBanned( CLIENT *Client, CHANNEL *Chan )
- {
- return Check_List( &My_Bans, Client, Chan );
- } /* Lists_CheckBanned */
- GLOBAL bool
- Lists_IsBanEntry( char *Mask, CHANNEL *Chan )
- {
- assert( Mask != NULL );
- assert( Chan != NULL );
-
- return Already_Registered( My_Bans, Mask, Chan );
- } /* Lists_IsBanEntry */
- GLOBAL bool
- Lists_AddBanned( char *Mask, CHANNEL *Chan )
- {
- C2C *c2c;
- assert( Mask != NULL );
- assert( Chan != NULL );
- if( Already_Registered( My_Bans, Mask, Chan )) return true;
- c2c = New_C2C( Mask, Chan, false );
- if( ! c2c )
- {
- Log( LOG_ERR, "Can't add new ban list entry!" );
- return false;
- }
- /* verketten */
- c2c->next = My_Bans;
- My_Bans = c2c;
- Log( LOG_DEBUG, "Added \"%s\" to ban list for \"%s\".", Mask, Channel_Name( Chan ));
- return true;
- } /* Lists_AddBanned */
- GLOBAL void
- Lists_DelBanned( char *Mask, CHANNEL *Chan )
- {
- C2C *c2c, *last, *next;
- assert( Mask != NULL );
- assert( Chan != NULL );
- last = NULL;
- c2c = My_Bans;
- while( c2c )
- {
- next = c2c->next;
- if(( c2c->channel == Chan ) && ( strcasecmp( c2c->mask, Mask ) == 0 ))
- {
- /* dieser Eintrag muss geloescht werden */
- Log( LOG_DEBUG, "Deleted \"%s\" from ban list for \"%s\"." , c2c->mask, Channel_Name( Chan ));
- if( last ) last->next = next;
- else My_Bans = next;
- free( c2c );
- }
- else last = c2c;
- c2c = next;
- }
- } /* Lists_DelBanned */
- GLOBAL bool
- Lists_ShowBans( CLIENT *Client, CHANNEL *Channel )
- {
- C2C *c2c;
- assert( Client != NULL );
- assert( Channel != NULL );
- c2c = My_Bans;
- while( c2c )
- {
- if( c2c->channel == Channel )
- {
- /* Eintrag fuer Channel gefunden; ausgeben: */
- if( ! IRC_WriteStrClient( Client, RPL_BANLIST_MSG, Client_ID( Client ), Channel_Name( Channel ), c2c->mask )) return DISCONNECTED;
- }
- c2c = c2c->next;
- }
- return IRC_WriteStrClient( Client, RPL_ENDOFBANLIST_MSG, Client_ID( Client ), Channel_Name( Channel ));
- } /* Lists_ShowBans */
- GLOBAL void
- Lists_DeleteChannel( CHANNEL *Chan )
- {
- /* Channel wurde geloescht, Invite- und Ban-Lists aufraeumen */
- C2C *c2c, *last, *next;
- /* Invite-List */
- last = NULL;
- c2c = My_Invites;
- while( c2c )
- {
- next = c2c->next;
- if( c2c->channel == Chan )
- {
- /* dieser Eintrag muss geloescht werden */
- Log( LOG_DEBUG, "Deleted \"%s\" from invite list for \"%s\"." , c2c->mask, Channel_Name( Chan ));
- if( last ) last->next = next;
- else My_Invites = next;
- free( c2c );
- }
- else last = c2c;
- c2c = next;
- }
- /* Ban-List */
- last = NULL;
- c2c = My_Bans;
- while( c2c )
- {
- next = c2c->next;
- if( c2c->channel == Chan )
- {
- /* dieser Eintrag muss geloescht werden */
- Log( LOG_DEBUG, "Deleted \"%s\" from ban list for \"%s\"." , c2c->mask, Channel_Name( Chan ));
- if( last ) last->next = next;
- else My_Bans = next;
- free( c2c );
- }
- else last = c2c;
- c2c = next;
- }
- } /* Lists_DeleteChannel */
- GLOBAL char *
- Lists_MakeMask( char *Pattern )
- {
- /* This function generats a valid IRC mask of "any" string. This
- * mask is only valid until the next call to Lists_MakeMask(),
- * because a single global buffer is used. You have to copy the
- * generated mask to some sane location yourself! */
- static char TheMask[MASK_LEN];
- char *excl, *at;
- assert( Pattern != NULL );
- excl = strchr( Pattern, '!' );
- at = strchr( Pattern, '@' );
- if(( at ) && ( at < excl )) excl = NULL;
- if(( ! at ) && ( ! excl ))
- {
- /* Neither "!" nor "@" found: use string as nick name */
- strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
- strlcat( TheMask, "!*@*", sizeof( TheMask ));
- return TheMask;
- }
- if(( ! at ) && ( excl ))
- {
- /* Domain part is missing */
- strlcpy( TheMask, Pattern, sizeof( TheMask ) - 3 );
- strlcat( TheMask, "@*", sizeof( TheMask ));
- return TheMask;
- }
- if(( at ) && ( ! excl ))
- {
- /* User name is missing */
- *at = '\0'; at++;
- strlcpy( TheMask, Pattern, sizeof( TheMask ) - 5 );
- strlcat( TheMask, "!*@", sizeof( TheMask ));
- strlcat( TheMask, at, sizeof( TheMask ));
- return TheMask;
- }
- /* All parts (nick, user and domain name) are given */
- strlcpy( TheMask, Pattern, sizeof( TheMask ));
- return TheMask;
- } /* Lists_MakeMask */
- LOCAL C2C *
- New_C2C( char *Mask, CHANNEL *Chan, bool OnlyOnce )
- {
- C2C *c2c;
-
- assert( Mask != NULL );
- assert( Chan != NULL );
- /* Speicher fuer Eintrag anfordern */
- c2c = (C2C *)malloc( sizeof( C2C ));
- if( ! c2c )
- {
- Log( LOG_EMERG, "Can't allocate memory! [New_C2C]" );
- return NULL;
- }
- strlcpy( c2c->mask, Mask, sizeof( c2c->mask ));
- c2c->channel = Chan;
- c2c->onlyonce = OnlyOnce;
- return c2c;
- } /* New_C2C */
- LOCAL bool
- Check_List( C2C **Cl2Chan, CLIENT *Client, CHANNEL *Chan )
- {
- C2C *c2c, *last;
- assert( Cl2Chan != NULL );
- assert( Client != NULL );
- assert( Chan != NULL );
- c2c = *Cl2Chan;
- last = NULL;
- while( c2c )
- {
- if( c2c->channel == Chan )
- {
- /* Ok, richtiger Channel. Passt die Maske? */
- if( Match( c2c->mask, Client_Mask( Client )))
- {
- /* Treffer! */
- if( c2c->onlyonce )
- {
- /* Eintrag loeschen */
- Log( LOG_DEBUG, "Deleted \"%s\" from %s list for \"%s\".", c2c->mask, *Cl2Chan == My_Invites ? "invite" : "ban", Channel_Name( Chan ));
- if( last ) last->next = c2c->next;
- else *Cl2Chan = c2c->next;
- free( c2c );
- }
- return true;
- }
- }
- last = c2c;
- c2c = c2c->next;
- }
- return false;
- } /* Check_List */
- LOCAL bool
- Already_Registered( C2C *List, char *Mask, CHANNEL *Chan )
- {
- C2C *c2c;
- c2c = List;
- while( c2c )
- {
- if(( c2c->channel == Chan ) && ( strcasecmp( c2c->mask, Mask ) == 0 )) return true;
- c2c = c2c->next;
- }
- return false;
- } /* Already_Registered */
- /* -eof- */
|