2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2005 by Alexander Barton (alex@barton.de)
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.
20 static char UNUSED id[] = "$Id: channel.c,v 1.48 2005/06/04 11:53:25 fw Exp $";
29 #include "conn-func.h"
36 #include "irc-write.h"
52 LOCAL CHANNEL *My_Channels;
53 LOCAL CL2CHAN *My_Cl2Chan;
56 LOCAL CL2CHAN *Get_Cl2Chan PARAMS(( CHANNEL *Chan, CLIENT *Client ));
57 LOCAL CL2CHAN *Add_Client PARAMS(( CHANNEL *Chan, CLIENT *Client ));
58 LOCAL bool Remove_Client PARAMS(( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, char *Reason, bool InformServer ));
59 LOCAL CL2CHAN *Get_First_Cl2Chan PARAMS(( CLIENT *Client, CHANNEL *Chan ));
60 LOCAL CL2CHAN *Get_Next_Cl2Chan PARAMS(( CL2CHAN *Start, CLIENT *Client, CHANNEL *Chan ));
61 LOCAL bool Delete_Channel PARAMS(( CHANNEL *Chan ));
73 Channel_InitPredefined( void )
75 /* Vordefinierte persistente Channels erzeugen */
81 for( i = 0; i < Conf_Channel_Count; i++ )
83 /* Ist ein Name konfiguriert? */
84 if( ! Conf_Channel[i].name[0] ) continue;
86 /* Gueltiger Channel-Name? */
87 if( ! Channel_IsValidName( Conf_Channel[i].name ))
89 Log( LOG_ERR, "Can't create pre-defined channel: invalid name: \"%s\"!", Conf_Channel[i].name );
93 /* Gibt es den Channel bereits? */
94 chan = Channel_Search( Conf_Channel[i].name );
97 Log( LOG_INFO, "Can't create pre-defined channel \"%s\": name already in use.", Conf_Channel[i].name );
101 /* Channel anlegen */
102 chan = Channel_Create( Conf_Channel[i].name );
105 Channel_ModeAdd( chan, 'P' );
106 Channel_SetTopic( chan, Conf_Channel[i].topic );
107 c = Conf_Channel[i].modes;
108 while( *c ) Channel_ModeAdd( chan, *c++ );
109 Log( LOG_INFO, "Created pre-defined channel \"%s\".", Conf_Channel[i].name );
111 else Log( LOG_ERR, "Can't create pre-defined channel \"%s\"!", Conf_Channel[i].name );
113 } /* Channel_InitPredefined */
120 CL2CHAN *cl2chan, *cl2chan_next;
122 /* Channel-Strukturen freigeben */
131 /* Channel-Zuordnungstabelle freigeben */
132 cl2chan = My_Cl2Chan;
135 cl2chan_next = cl2chan->next;
137 cl2chan = cl2chan_next;
143 Channel_Join( CLIENT *Client, char *Name )
147 assert( Client != NULL );
148 assert( Name != NULL );
150 /* Valider Channel-Name? */
151 if( ! Channel_IsValidName( Name ))
153 IRC_WriteStrClient( Client, ERR_NOSUCHCHANNEL_MSG, Client_ID( Client ), Name );
158 chan = Channel_Search( Name );
161 /* Ist der Client bereits Mitglied? */
162 if( Get_Cl2Chan( chan, Client )) return false;
166 /* Gibt es noch nicht? Dann neu anlegen: */
167 chan = Channel_Create( Name );
168 if( ! chan ) return false;
171 /* User dem Channel hinzufuegen */
172 if( ! Add_Client( chan, Client )) return false;
178 Channel_Part( CLIENT *Client, CLIENT *Origin, char *Name, char *Reason )
182 assert( Client != NULL );
183 assert( Name != NULL );
184 assert( Reason != NULL );
187 chan = Channel_Search( Name );
188 if(( ! chan ) || ( ! Get_Cl2Chan( chan, Client )))
190 IRC_WriteStrClient( Client, ERR_NOSUCHCHANNEL_MSG, Client_ID( Client ), Name );
194 /* User aus Channel entfernen */
195 if( ! Remove_Client( REMOVE_PART, chan, Client, Origin, Reason, true)) return false;
201 Channel_Kick( CLIENT *Client, CLIENT *Origin, char *Name, char *Reason )
205 assert( Client != NULL );
206 assert( Origin != NULL );
207 assert( Name != NULL );
208 assert( Reason != NULL );
211 chan = Channel_Search( Name );
214 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
218 /* Ist der User Mitglied in dem Channel? */
219 if( ! Channel_IsMemberOf( chan, Origin ))
221 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Name );
225 /* Ist der User Channel-Operator? */
226 if( ! strchr( Channel_UserModes( chan, Origin ), 'o' ))
228 IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Name);
232 /* Ist der Ziel-User Mitglied im Channel? */
233 if( ! Channel_IsMemberOf( chan, Client ))
235 IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( Client ), Name );
239 Remove_Client( REMOVE_KICK, chan, Client, Origin, Reason, true);
244 Channel_Quit( CLIENT *Client, char *Reason )
248 assert( Client != NULL );
249 assert( Reason != NULL );
251 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
257 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
264 Channel_Count( void )
276 } /* Channel_Count */
280 Channel_MemberCount( CHANNEL *Chan )
285 assert( Chan != NULL );
287 cl2chan = My_Cl2Chan;
290 if( cl2chan->channel == Chan ) count++;
291 cl2chan = cl2chan->next;
294 } /* Channel_MemberCount */
298 Channel_CountForUser( CLIENT *Client )
300 /* Count number of channels a user is member of. */
305 assert( Client != NULL );
307 cl2chan = My_Cl2Chan;
310 if( cl2chan->client == Client ) count++;
311 cl2chan = cl2chan->next;
315 } /* Channel_CountForUser */
319 Channel_PCount( void )
321 /* Count the number of persistent (mode 'P') channels */
329 if( strchr( chan->modes, 'P' )) count++;
334 } /* Channel_PCount */
338 Channel_Name( CHANNEL *Chan )
340 assert( Chan != NULL );
346 Channel_Modes( CHANNEL *Chan )
348 assert( Chan != NULL );
350 } /* Channel_Modes */
354 Channel_Key( CHANNEL *Chan )
356 assert( Chan != NULL );
362 Channel_MaxUsers( CHANNEL *Chan )
364 assert( Chan != NULL );
365 return Chan->maxusers;
366 } /* Channel_MaxUsers */
370 Channel_First( void )
373 } /* Channel_First */
377 Channel_Next( CHANNEL *Chan )
379 assert( Chan != NULL );
385 Channel_Search( char *Name )
387 /* Channel-Struktur suchen */
392 assert( Name != NULL );
394 search_hash = Hash( Name );
398 if( search_hash == c->hash )
400 /* lt. Hash-Wert: Treffer! */
401 if( strcasecmp( Name, c->name ) == 0 ) return c;
406 } /* Channel_Search */
410 Channel_FirstMember( CHANNEL *Chan )
412 assert( Chan != NULL );
413 return Get_First_Cl2Chan( NULL, Chan );
414 } /* Channel_FirstMember */
418 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
420 assert( Chan != NULL );
421 assert( Cl2Chan != NULL );
422 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
423 } /* Channel_NextMember */
427 Channel_FirstChannelOf( CLIENT *Client )
429 assert( Client != NULL );
430 return Get_First_Cl2Chan( Client, NULL );
431 } /* Channel_FirstChannelOf */
435 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
437 assert( Client != NULL );
438 assert( Cl2Chan != NULL );
439 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
440 } /* Channel_NextChannelOf */
444 Channel_GetClient( CL2CHAN *Cl2Chan )
446 assert( Cl2Chan != NULL );
447 return Cl2Chan->client;
448 } /* Channel_GetClient */
452 Channel_GetChannel( CL2CHAN *Cl2Chan )
454 assert( Cl2Chan != NULL );
455 return Cl2Chan->channel;
456 } /* Channel_GetChannel */
460 Channel_IsValidName( char *Name )
462 /* Pruefen, ob Name als Channelname gueltig */
464 char *ptr, badchars[10];
466 assert( Name != NULL );
468 if(( Name[0] != '#' ) || ( strlen( Name ) >= CHANNEL_NAME_LEN )) return false;
471 strcpy( badchars, " ,:\007" );
474 if( strchr( badchars, *ptr )) return false;
479 } /* Channel_IsValidName */
483 Channel_ModeAdd( CHANNEL *Chan, char Mode )
486 * If the channel already had this mode, return false.
487 * If the channel mode was newly set return true.
492 assert( Chan != NULL );
494 x[0] = Mode; x[1] = '\0';
495 if( ! strchr( Chan->modes, x[0] ))
497 /* Channel does not have this mode yet, set it */
498 strlcat( Chan->modes, x, sizeof( Chan->modes ));
502 } /* Channel_ModeAdd */
506 Channel_ModeDel( CHANNEL *Chan, char Mode )
509 * if the mode was removed return true.
510 * if the channel did not have the mode, return false.
514 assert( Chan != NULL );
516 x[0] = Mode; x[1] = '\0';
518 p = strchr( Chan->modes, x[0] );
519 if( ! p ) return false;
521 /* Channel has mode -> delete */
528 } /* Channel_ModeDel */
532 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
534 /* Set Channel-User-Mode.
535 * if mode was newly set, return true.
536 * if the User already had this channel-mode, return false.
542 assert( Chan != NULL );
543 assert( Client != NULL );
545 cl2chan = Get_Cl2Chan( Chan, Client );
546 assert( cl2chan != NULL );
548 x[0] = Mode; x[1] = '\0';
549 if( ! strchr( cl2chan->modes, x[0] ))
551 /* mode not set, -> set it */
552 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
556 } /* Channel_UserModeAdd */
560 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
562 /* Delete Channel-User-Mode.
563 * If Mode was removed, return true.
564 * If User did not have the Channel-Mode, return false.
570 assert( Chan != NULL );
571 assert( Client != NULL );
573 cl2chan = Get_Cl2Chan( Chan, Client );
574 assert( cl2chan != NULL );
576 x[0] = Mode; x[1] = '\0';
578 p = strchr( cl2chan->modes, x[0] );
579 if( ! p ) return false;
581 /* Client has Mode -> delete */
588 } /* Channel_UserModeDel */
592 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
594 /* return Users' Channel-Modes */
598 assert( Chan != NULL );
599 assert( Client != NULL );
601 cl2chan = Get_Cl2Chan( Chan, Client );
602 assert( cl2chan != NULL );
604 return cl2chan->modes;
605 } /* Channel_UserModes */
609 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
611 /* Test if Client is on Channel Chan */
613 assert( Chan != NULL );
614 assert( Client != NULL );
616 if( Get_Cl2Chan( Chan, Client )) return true;
618 } /* Channel_IsMemberOf */
622 Channel_Topic( CHANNEL *Chan )
624 assert( Chan != NULL );
626 } /* Channel_Topic */
630 Channel_SetTopic( CHANNEL *Chan, char *Topic )
632 assert( Chan != NULL );
633 assert( Topic != NULL );
635 strlcpy( Chan->topic, Topic, sizeof( Chan->topic ));
636 } /* Channel_SetTopic */
640 Channel_SetModes( CHANNEL *Chan, char *Modes )
642 assert( Chan != NULL );
643 assert( Modes != NULL );
645 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
646 } /* Channel_SetModes */
650 Channel_SetKey( CHANNEL *Chan, char *Key )
652 assert( Chan != NULL );
653 assert( Key != NULL );
655 strlcpy( Chan->key, Key, sizeof( Chan->key ));
656 Log( LOG_DEBUG, "Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
657 } /* Channel_SetKey */
661 Channel_SetMaxUsers( CHANNEL *Chan, long Count )
663 assert( Chan != NULL );
665 Chan->maxusers = Count;
666 Log( LOG_DEBUG, "Channel %s: Member limit is now %ld.", Chan->name, Chan->maxusers );
667 } /* Channel_SetMaxUsers */
671 Channel_Write( CHANNEL *Chan, CLIENT *From, CLIENT *Client, char *Text )
673 bool is_member, has_voice, is_op, ok;
675 /* Okay, target is a channel */
676 is_member = has_voice = is_op = false;
677 if( Channel_IsMemberOf( Chan, From ))
680 if( strchr( Channel_UserModes( Chan, From ), 'v' )) has_voice = true;
681 if( strchr( Channel_UserModes( Chan, From ), 'o' )) is_op = true;
684 /* Is the client allowed to write to channel? */
686 if( strchr( Channel_Modes( Chan ), 'n' ) && ( ! is_member )) ok = false;
687 if( strchr( Channel_Modes( Chan ), 'm' ) && ( ! is_op ) && ( ! has_voice )) ok = false;
689 /* Is the client banned? */
690 if( Lists_CheckBanned( From, Chan ))
692 /* Client is banned, bus is he channel operator or has voice? */
693 if(( ! has_voice ) && ( ! is_op )) ok = false;
696 if( ! ok ) return IRC_WriteStrClient( From, ERR_CANNOTSENDTOCHAN_MSG, Client_ID( From ), Channel_Name( Chan ));
699 if( Client_Conn( From ) > NONE ) Conn_UpdateIdle( Client_Conn( From ));
700 return IRC_WriteStrChannelPrefix( Client, Chan, From, true, "PRIVMSG %s :%s", Channel_Name( Chan ), Text );
701 } /* Channel_Write */
705 Channel_Create( char *Name )
707 /* Create new CHANNEL structure and add it to linked list */
710 assert( Name != NULL );
712 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
715 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
718 memset( c, 0, sizeof( CHANNEL ));
719 strlcpy( c->name, Name, sizeof( c->name ));
720 c->hash = Hash( c->name );
721 c->next = My_Channels;
724 Log( LOG_DEBUG, "Created new channel structure for \"%s\".", Name );
727 } /* Channel_Create */
731 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
735 assert( Chan != NULL );
736 assert( Client != NULL );
738 cl2chan = My_Cl2Chan;
741 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
742 cl2chan = cl2chan->next;
749 Add_Client( CHANNEL *Chan, CLIENT *Client )
753 assert( Chan != NULL );
754 assert( Client != NULL );
756 /* neue CL2CHAN-Struktur anlegen */
757 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
760 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
763 cl2chan->channel = Chan;
764 cl2chan->client = Client;
765 strcpy( cl2chan->modes, "" );
768 cl2chan->next = My_Cl2Chan;
769 My_Cl2Chan = cl2chan;
771 Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
778 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, char *Reason, bool InformServer )
780 CL2CHAN *cl2chan, *last_cl2chan;
783 assert( Chan != NULL );
784 assert( Client != NULL );
785 assert( Origin != NULL );
786 assert( Reason != NULL );
789 cl2chan = My_Cl2Chan;
792 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
793 last_cl2chan = cl2chan;
794 cl2chan = cl2chan->next;
796 if( ! cl2chan ) return false;
798 c = cl2chan->channel;
801 /* Aus Verkettung loesen und freigeben */
802 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
803 else My_Cl2Chan = cl2chan->next;
809 /* QUIT: andere Server wurden bereits informiert, vgl. Client_Destroy();
810 * hier also "nur" noch alle User in betroffenen Channeln infomieren */
811 assert( InformServer == false );
812 Log( LOG_DEBUG, "User \"%s\" left channel \"%s\" (%s).", Client_Mask( Client ), c->name, Reason );
815 /* User wurde geKICKed: ggf. andere Server sowie alle betroffenen User
816 * im entsprechenden Channel informieren */
817 if( InformServer ) IRC_WriteStrServersPrefix( Client_NextHop( Origin ), Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason );
818 IRC_WriteStrChannelPrefix( Client, c, Origin, false, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason );
819 if(( Client_Conn( Client ) > NONE ) && ( Client_Type( Client ) == CLIENT_USER )) IRC_WriteStrClientPrefix( Client, Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason );
820 Log( LOG_DEBUG, "User \"%s\" has been kicked of \"%s\" by \"%s\": %s.", Client_Mask( Client ), c->name, Client_ID( Origin ), Reason );
824 if( InformServer ) IRC_WriteStrServersPrefix( Origin, Client, "PART %s :%s", c->name, Reason );
825 IRC_WriteStrChannelPrefix( Origin, c, Client, false, "PART %s :%s", c->name, Reason );
826 if(( Client_Conn( Origin ) > NONE ) && ( Client_Type( Origin ) == CLIENT_USER )) IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason );
827 Log( LOG_DEBUG, "User \"%s\" left channel \"%s\" (%s).", Client_Mask( Client ), c->name, Reason );
830 /* Wenn Channel nun leer und nicht pre-defined: loeschen */
831 if( ! strchr( Channel_Modes( Chan ), 'P' ))
833 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
837 } /* Remove_Client */
841 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
843 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
844 } /* Get_First_Cl2Chan */
848 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
852 assert( Client != NULL || Channel != NULL );
857 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
858 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
859 cl2chan = cl2chan->next;
862 } /* Get_Next_Cl2Chan */
866 Delete_Channel( CHANNEL *Chan )
868 /* Channel-Struktur loeschen */
870 CHANNEL *chan, *last_chan;
876 if( chan == Chan ) break;
880 if( ! chan ) return false;
882 Log( LOG_DEBUG, "Freed channel structure for \"%s\".", Chan->name );
884 /* Invite- und Ban-Lists aufraeumen */
885 Lists_DeleteChannel( chan );
887 /* Neu verketten und freigeben */
888 if( last_chan ) last_chan->next = chan->next;
889 else My_Channels = chan->next;
893 } /* Delete_Channel */