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.60 2006/12/07 17:57:20 fw Exp $";
30 #include "conn-func.h"
37 #include "irc-write.h"
53 static CHANNEL *My_Channels;
54 static CL2CHAN *My_Cl2Chan;
57 static CL2CHAN *Get_Cl2Chan PARAMS(( CHANNEL *Chan, CLIENT *Client ));
58 static CL2CHAN *Add_Client PARAMS(( CHANNEL *Chan, CLIENT *Client ));
59 static bool Remove_Client PARAMS(( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, char *Reason, bool InformServer ));
60 static CL2CHAN *Get_First_Cl2Chan PARAMS(( CLIENT *Client, CHANNEL *Chan ));
61 static CL2CHAN *Get_Next_Cl2Chan PARAMS(( CL2CHAN *Start, CLIENT *Client, CHANNEL *Chan ));
62 static bool Delete_Channel PARAMS(( CHANNEL *Chan ));
73 GLOBAL struct list_head *
74 Channel_GetListBans(CHANNEL *c)
81 GLOBAL struct list_head *
82 Channel_GetListInvites(CHANNEL *c)
85 return &c->list_invites;
90 Channel_InitPredefined( void )
92 /* Vordefinierte persistente Channels erzeugen */
98 for( i = 0; i < Conf_Channel_Count; i++ )
100 /* Ist ein Name konfiguriert? */
101 if( ! Conf_Channel[i].name[0] ) continue;
103 /* Gueltiger Channel-Name? */
104 if( ! Channel_IsValidName( Conf_Channel[i].name ))
106 Log( LOG_ERR, "Can't create pre-defined channel: invalid name: \"%s\"!", Conf_Channel[i].name );
107 array_free(&Conf_Channel[i].topic);
111 /* Gibt es den Channel bereits? */
112 chan = Channel_Search( Conf_Channel[i].name );
115 Log( LOG_INFO, "Can't create pre-defined channel \"%s\": name already in use.", Conf_Channel[i].name );
116 array_free(&Conf_Channel[i].topic);
121 chan = Channel_Create(Conf_Channel[i].name);
123 Channel_ModeAdd(chan, 'P');
125 if (array_start(&Conf_Channel[i].topic) != NULL)
126 Channel_SetTopic(chan, NULL,
127 array_start(&Conf_Channel[i].topic));
128 array_free(&Conf_Channel[i].topic);
130 c = Conf_Channel[i].modes;
132 Channel_ModeAdd(chan, *c++);
134 Log(LOG_INFO, "Created pre-defined channel \"%s\".",
135 Conf_Channel[i].name );
137 else Log(LOG_ERR, "Can't create pre-defined channel \"%s\"!",
138 Conf_Channel[i].name );
140 } /* Channel_InitPredefined */
147 CL2CHAN *cl2chan, *cl2chan_next;
149 /* Channel-Strukturen freigeben */
154 array_free(&c->topic);
159 /* Channel-Zuordnungstabelle freigeben */
160 cl2chan = My_Cl2Chan;
163 cl2chan_next = cl2chan->next;
165 cl2chan = cl2chan_next;
171 Channel_Join( CLIENT *Client, char *Name )
175 assert( Client != NULL );
176 assert( Name != NULL );
178 if( ! Channel_IsValidName( Name )) {
179 IRC_WriteStrClient( Client, ERR_NOSUCHCHANNEL_MSG, Client_ID( Client ), Name );
183 chan = Channel_Search( Name );
185 /* Ist der Client bereits Mitglied? */
186 if( Get_Cl2Chan( chan, Client )) return false;
190 /* Gibt es noch nicht? Dann neu anlegen: */
191 chan = Channel_Create( Name );
192 if (!chan) return false;
195 /* User dem Channel hinzufuegen */
196 if( ! Add_Client( chan, Client )) return false;
202 Channel_Part( CLIENT *Client, CLIENT *Origin, char *Name, char *Reason )
206 assert( Client != NULL );
207 assert( Name != NULL );
208 assert( Reason != NULL );
210 chan = Channel_Search( Name );
211 if(( ! chan ) || ( ! Get_Cl2Chan( chan, Client )))
213 IRC_WriteStrClient( Client, ERR_NOSUCHCHANNEL_MSG, Client_ID( Client ), Name );
217 /* User aus Channel entfernen */
218 if( ! Remove_Client( REMOVE_PART, chan, Client, Origin, Reason, true)) return false;
224 Channel_Kick( CLIENT *Client, CLIENT *Origin, char *Name, char *Reason )
228 assert( Client != NULL );
229 assert( Origin != NULL );
230 assert( Name != NULL );
231 assert( Reason != NULL );
233 chan = Channel_Search( Name );
236 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
240 if( ! Channel_IsMemberOf( chan, Origin ))
242 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Name );
246 /* Is User Channel-Operator? */
247 if( ! strchr( Channel_UserModes( chan, Origin ), 'o' ))
249 IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Name);
253 /* Ist the kickED User member of channel? */
254 if( ! Channel_IsMemberOf( chan, Client ))
256 IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( Client ), Name );
260 Remove_Client( REMOVE_KICK, chan, Client, Origin, Reason, true);
265 Channel_Quit( CLIENT *Client, char *Reason )
269 assert( Client != NULL );
270 assert( Reason != NULL );
272 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
278 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
285 Channel_Count( void )
288 unsigned long count = 0;
297 } /* Channel_Count */
301 Channel_MemberCount( CHANNEL *Chan )
304 unsigned long count = 0;
306 assert( Chan != NULL );
308 cl2chan = My_Cl2Chan;
311 if( cl2chan->channel == Chan ) count++;
312 cl2chan = cl2chan->next;
315 } /* Channel_MemberCount */
319 Channel_CountForUser( CLIENT *Client )
321 /* Count number of channels a user is member of. */
326 assert( Client != NULL );
328 cl2chan = My_Cl2Chan;
331 if( cl2chan->client == Client ) count++;
332 cl2chan = cl2chan->next;
336 } /* Channel_CountForUser */
340 Channel_PCount( void )
342 /* Count the number of persistent (mode 'P') channels */
350 if( strchr( chan->modes, 'P' )) count++;
355 } /* Channel_PCount */
359 Channel_Name( const CHANNEL *Chan )
361 assert( Chan != NULL );
367 Channel_Modes( CHANNEL *Chan )
369 assert( Chan != NULL );
371 } /* Channel_Modes */
375 Channel_Key( CHANNEL *Chan )
377 assert( Chan != NULL );
383 Channel_MaxUsers( CHANNEL *Chan )
385 assert( Chan != NULL );
386 return Chan->maxusers;
387 } /* Channel_MaxUsers */
391 Channel_First( void )
394 } /* Channel_First */
398 Channel_Next( CHANNEL *Chan )
400 assert( Chan != NULL );
406 Channel_Search( char *Name )
408 /* Channel-Struktur suchen */
413 assert( Name != NULL );
415 search_hash = Hash( Name );
419 if( search_hash == c->hash )
421 /* lt. Hash-Wert: Treffer! */
422 if( strcasecmp( Name, c->name ) == 0 ) return c;
427 } /* Channel_Search */
431 Channel_FirstMember( CHANNEL *Chan )
433 assert( Chan != NULL );
434 return Get_First_Cl2Chan( NULL, Chan );
435 } /* Channel_FirstMember */
439 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
441 assert( Chan != NULL );
442 assert( Cl2Chan != NULL );
443 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
444 } /* Channel_NextMember */
448 Channel_FirstChannelOf( CLIENT *Client )
450 assert( Client != NULL );
451 return Get_First_Cl2Chan( Client, NULL );
452 } /* Channel_FirstChannelOf */
456 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
458 assert( Client != NULL );
459 assert( Cl2Chan != NULL );
460 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
461 } /* Channel_NextChannelOf */
465 Channel_GetClient( CL2CHAN *Cl2Chan )
467 assert( Cl2Chan != NULL );
468 return Cl2Chan->client;
469 } /* Channel_GetClient */
473 Channel_GetChannel( CL2CHAN *Cl2Chan )
475 assert( Cl2Chan != NULL );
476 return Cl2Chan->channel;
477 } /* Channel_GetChannel */
481 Channel_IsValidName( const char *Name )
483 assert( Name != NULL );
485 if(( Name[0] != '#' ) || ( strlen( Name ) >= CHANNEL_NAME_LEN )) return false;
487 return Name[strcspn(Name, " ,:\007")] == 0;
488 } /* Channel_IsValidName */
492 Channel_ModeAdd( CHANNEL *Chan, char Mode )
495 * If the channel already had this mode, return false.
496 * If the channel mode was newly set return true.
501 assert( Chan != NULL );
503 x[0] = Mode; x[1] = '\0';
504 if( ! strchr( Chan->modes, x[0] ))
506 /* Channel does not have this mode yet, set it */
507 strlcat( Chan->modes, x, sizeof( Chan->modes ));
511 } /* Channel_ModeAdd */
515 Channel_ModeDel( CHANNEL *Chan, char Mode )
518 * if the mode was removed return true.
519 * if the channel did not have the mode, return false.
523 assert( Chan != NULL );
525 p = strchr( Chan->modes, Mode );
526 if( ! p ) return false;
528 /* Channel has mode -> delete */
535 } /* Channel_ModeDel */
539 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
541 /* Set Channel-User-Mode.
542 * if mode was newly set, return true.
543 * if the User already had this channel-mode, return false.
549 assert( Chan != NULL );
550 assert( Client != NULL );
552 cl2chan = Get_Cl2Chan( Chan, Client );
553 assert( cl2chan != NULL );
555 x[0] = Mode; x[1] = '\0';
556 if( ! strchr( cl2chan->modes, x[0] ))
558 /* mode not set, -> set it */
559 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
563 } /* Channel_UserModeAdd */
567 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
569 /* Delete Channel-User-Mode.
570 * If Mode was removed, return true.
571 * If User did not have the Channel-Mode, return false.
577 assert( Chan != NULL );
578 assert( Client != NULL );
580 cl2chan = Get_Cl2Chan( Chan, Client );
581 assert( cl2chan != NULL );
583 p = strchr( cl2chan->modes, Mode );
584 if( ! p ) return false;
586 /* Client has Mode -> delete */
593 } /* Channel_UserModeDel */
597 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
599 /* return Users' Channel-Modes */
603 assert( Chan != NULL );
604 assert( Client != NULL );
606 cl2chan = Get_Cl2Chan( Chan, Client );
607 assert( cl2chan != NULL );
609 return cl2chan->modes;
610 } /* Channel_UserModes */
614 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
616 /* Test if Client is on Channel Chan */
618 assert( Chan != NULL );
619 assert( Client != NULL );
621 if( Get_Cl2Chan( Chan, Client )) return true;
623 } /* Channel_IsMemberOf */
627 Channel_Topic( CHANNEL *Chan )
630 assert( Chan != NULL );
631 ret = array_start(&Chan->topic);
632 return ret ? ret : "";
633 } /* Channel_Topic */
639 Channel_TopicTime(CHANNEL *Chan)
641 assert(Chan != NULL);
642 return (unsigned int) Chan->topic_time;
643 } /* Channel_TopicTime */
647 Channel_TopicWho(CHANNEL *Chan)
649 assert(Chan != NULL);
650 return Chan->topic_who;
651 } /* Channel_TopicWho */
657 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, char *Topic)
660 assert( Chan != NULL );
661 assert( Topic != NULL );
664 if (len < array_bytes(&Chan->topic))
665 array_free(&Chan->topic);
667 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
668 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
669 Topic, Chan->name, strerror(errno));
671 Chan->topic_time = time(NULL);
672 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
673 strlcpy(Chan->topic_who, Client_ID(Client),
674 sizeof Chan->topic_who);
676 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
677 sizeof Chan->topic_who);
681 } /* Channel_SetTopic */
685 Channel_SetModes( CHANNEL *Chan, char *Modes )
687 assert( Chan != NULL );
688 assert( Modes != NULL );
690 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
691 } /* Channel_SetModes */
695 Channel_SetKey( CHANNEL *Chan, char *Key )
697 assert( Chan != NULL );
698 assert( Key != NULL );
700 strlcpy( Chan->key, Key, sizeof( Chan->key ));
701 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
702 } /* Channel_SetKey */
706 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
708 assert( Chan != NULL );
710 Chan->maxusers = Count;
711 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
712 } /* Channel_SetMaxUsers */
716 Channel_Write( CHANNEL *Chan, CLIENT *From, CLIENT *Client, char *Text )
718 bool is_member, has_voice, is_op, ok;
720 /* Okay, target is a channel */
721 is_member = has_voice = is_op = false;
722 if( Channel_IsMemberOf( Chan, From ))
725 if( strchr( Channel_UserModes( Chan, From ), 'v' )) has_voice = true;
726 if( strchr( Channel_UserModes( Chan, From ), 'o' )) is_op = true;
729 /* Is the client allowed to write to channel? */
731 if( strchr( Channel_Modes( Chan ), 'n' ) && ( ! is_member )) ok = false;
732 if( strchr( Channel_Modes( Chan ), 'm' ) && ( ! is_op ) && ( ! has_voice )) ok = false;
734 /* Is the client banned? */
735 if( Lists_Check(&Chan->list_bans, From))
737 /* Client is banned, but is he channel operator or has voice? */
738 if(( ! has_voice ) && ( ! is_op )) ok = false;
741 if( ! ok ) return IRC_WriteStrClient( From, ERR_CANNOTSENDTOCHAN_MSG, Client_ID( From ), Channel_Name( Chan ));
744 if( Client_Conn( From ) > NONE ) Conn_UpdateIdle( Client_Conn( From ));
745 return IRC_WriteStrChannelPrefix( Client, Chan, From, true, "PRIVMSG %s :%s", Channel_Name( Chan ), Text );
746 } /* Channel_Write */
750 Channel_Create( char *Name )
752 /* Create new CHANNEL structure and add it to linked list */
755 assert( Name != NULL );
757 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
760 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
763 memset( c, 0, sizeof( CHANNEL ));
764 strlcpy( c->name, Name, sizeof( c->name ));
765 c->hash = Hash( c->name );
766 c->next = My_Channels;
768 LogDebug("Created new channel structure for \"%s\".", Name);
770 } /* Channel_Create */
774 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
778 assert( Chan != NULL );
779 assert( Client != NULL );
781 cl2chan = My_Cl2Chan;
784 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
785 cl2chan = cl2chan->next;
792 Add_Client( CHANNEL *Chan, CLIENT *Client )
796 assert( Chan != NULL );
797 assert( Client != NULL );
799 /* neue CL2CHAN-Struktur anlegen */
800 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
803 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
806 cl2chan->channel = Chan;
807 cl2chan->client = Client;
808 strcpy( cl2chan->modes, "" );
811 cl2chan->next = My_Cl2Chan;
812 My_Cl2Chan = cl2chan;
814 Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
821 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, char *Reason, bool InformServer )
823 CL2CHAN *cl2chan, *last_cl2chan;
826 assert( Chan != NULL );
827 assert( Client != NULL );
828 assert( Origin != NULL );
829 assert( Reason != NULL );
832 cl2chan = My_Cl2Chan;
835 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
836 last_cl2chan = cl2chan;
837 cl2chan = cl2chan->next;
839 if( ! cl2chan ) return false;
841 c = cl2chan->channel;
844 /* Aus Verkettung loesen und freigeben */
845 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
846 else My_Cl2Chan = cl2chan->next;
852 /* QUIT: other servers have already been notified, see Client_Destroy();
853 * so only inform other clients in same channel. */
854 assert( InformServer == false );
855 LogDebug("User \"%s\" left channel \"%s\" (%s).",
856 Client_Mask( Client ), c->name, Reason );
859 /* User was KICKed: inform other servers and all users in channel */
861 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
862 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
863 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
864 c->name, Client_ID( Client ), Reason );
865 if ((Client_Conn(Client) > NONE) &&
866 (Client_Type(Client) == CLIENT_USER))
868 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
869 c->name, Client_ID( Client ), Reason);
871 LogDebug("User \"%s\" has been kicked of \"%s\" by \"%s\": %s.",
872 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
876 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
878 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
881 if ((Client_Conn(Origin) > NONE) &&
882 (Client_Type(Origin) == CLIENT_USER))
884 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
885 LogDebug("User \"%s\" left channel \"%s\" (%s).",
886 Client_Mask(Client), c->name, Reason);
890 /* Wenn Channel nun leer und nicht pre-defined: loeschen */
891 if( ! strchr( Channel_Modes( Chan ), 'P' ))
893 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
897 } /* Remove_Client */
901 Channel_AddBan(CHANNEL *c, const char *mask )
903 struct list_head *h = Channel_GetListBans(c);
904 return Lists_Add(h, mask, false);
909 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
911 struct list_head *h = Channel_GetListInvites(c);
912 return Lists_Add(h, mask, onlyonce);
917 ShowInvitesBans(struct list_head *head, CLIENT *Client, CHANNEL *Channel, bool invite)
920 char *msg = invite ? RPL_INVITELIST_MSG : RPL_BANLIST_MSG;
923 assert( Client != NULL );
924 assert( Channel != NULL );
926 e = Lists_GetFirst(head);
928 if( ! IRC_WriteStrClient( Client, msg, Client_ID( Client ),
929 Channel_Name( Channel ), Lists_GetMask(e) )) return DISCONNECTED;
930 e = Lists_GetNext(e);
933 msg_end = invite ? RPL_ENDOFINVITELIST_MSG : RPL_ENDOFBANLIST_MSG;
934 return IRC_WriteStrClient( Client, msg_end, Client_ID( Client ), Channel_Name( Channel ));
939 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
943 assert( Channel != NULL );
945 h = Channel_GetListBans(Channel);
946 return ShowInvitesBans(h, Client, Channel, false);
951 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
955 assert( Channel != NULL );
957 h = Channel_GetListInvites(Channel);
958 return ShowInvitesBans(h, Client, Channel, true);
963 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
965 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
966 } /* Get_First_Cl2Chan */
970 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
974 assert( Client != NULL || Channel != NULL );
979 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
980 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
981 cl2chan = cl2chan->next;
984 } /* Get_Next_Cl2Chan */
988 Delete_Channel( CHANNEL *Chan )
990 /* Channel-Struktur loeschen */
992 CHANNEL *chan, *last_chan;
998 if( chan == Chan ) break;
1002 if( ! chan ) return false;
1004 Log( LOG_DEBUG, "Freed channel structure for \"%s\".", Chan->name );
1006 /* Invite- und Ban-Lists aufraeumen */
1007 Lists_Free( &chan->list_bans );
1008 Lists_Free( &chan->list_invites );
1010 /* Neu verketten und freigeben */
1011 if( last_chan ) last_chan->next = chan->next;
1012 else My_Channels = chan->next;
1016 } /* Delete_Channel */