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.65 2008/02/05 16:31:35 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, const 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 /* Generate predefined persistent channels */
98 for( i = 0; i < Conf_Channel_Count; i++ )
100 /* Check for Name configuration */
101 if( ! Conf_Channel[i].name[0] ) continue;
103 /* Check for invalid 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 /* Check if the channel name is already in use */
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 Channel_SetKey(chan, Conf_Channel[i].key);
135 Channel_SetMaxUsers(chan, Conf_Channel[i].maxusers);
137 Log(LOG_INFO, "Created pre-defined channel \"%s\".",
138 Conf_Channel[i].name );
140 else Log(LOG_ERR, "Can't create pre-defined channel \"%s\"!",
141 Conf_Channel[i].name );
143 } /* Channel_InitPredefined */
150 CL2CHAN *cl2chan, *cl2chan_next;
152 /* free struct Channel */
157 array_free(&c->topic);
162 /* Free Channel allocation table */
163 cl2chan = My_Cl2Chan;
166 cl2chan_next = cl2chan->next;
168 cl2chan = cl2chan_next;
175 * This function lets a client join a channel. First, the function
176 * checks that the specified channel name is valid and that the client
177 * isn't already a member. If the specified channel doesn't exist,
178 * a new channel is created. Client is added to channel by function
182 Channel_Join( CLIENT *Client, char *Name )
186 assert(Client != NULL);
187 assert(Name != NULL);
189 /* Check that the channel name is valid */
190 if (! Channel_IsValidName(Name)) {
191 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
192 Client_ID(Client), Name);
196 chan = Channel_Search(Name);
198 /* Check if the client is already in the channel */
199 if (Get_Cl2Chan(chan, Client))
202 /* If the specified channel does not exist, the channel
204 chan = Channel_Create(Name);
209 /* Add user to Channel */
210 if (! Add_Client(chan, Client))
218 * Part client from channel.
219 * This function lets a client part from a channel. First, the function checks
220 * if the channel exists and the client is a member of it and sends out
221 * appropriate error messages if not. The real work is done by the function
225 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
229 assert(Client != NULL);
230 assert(Name != NULL);
231 assert(Reason != NULL);
233 /* Check that specified channel exists */
234 chan = Channel_Search(Name);
236 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
237 Client_ID(Client), Name);
241 /* Check that the client is in the channel */
242 if (!Get_Cl2Chan(chan, Client)) {
243 IRC_WriteStrClient(Client, ERR_NOTONCHANNEL_MSG,
244 Client_ID(Client), Name);
248 /* Part client from channel */
249 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
256 /* Kick user from Channel */
258 Channel_Kick( CLIENT *Client, CLIENT *Origin, const char *Name, const char *Reason )
262 assert( Client != NULL );
263 assert( Origin != NULL );
264 assert( Name != NULL );
265 assert( Reason != NULL );
267 /* Check that channel exists */
268 chan = Channel_Search( Name );
271 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
275 /* Check that user is on the specified channel */
276 if( ! Channel_IsMemberOf( chan, Origin ))
278 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Name );
282 /* Check if user has operator status */
283 if( ! strchr( Channel_UserModes( chan, Origin ), 'o' ))
285 IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Name);
289 /* Check that the client to be kicked is on the specified channel */
290 if( ! Channel_IsMemberOf( chan, Client ))
292 IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( Client ), Name );
296 /* Kick Client from channel */
297 Remove_Client( REMOVE_KICK, chan, Client, Origin, Reason, true);
302 Channel_Quit( CLIENT *Client, char *Reason )
306 assert( Client != NULL );
307 assert( Reason != NULL );
309 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
315 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
322 Channel_Count( void )
325 unsigned long count = 0;
334 } /* Channel_Count */
338 Channel_MemberCount( CHANNEL *Chan )
341 unsigned long count = 0;
343 assert( Chan != NULL );
345 cl2chan = My_Cl2Chan;
348 if( cl2chan->channel == Chan ) count++;
349 cl2chan = cl2chan->next;
352 } /* Channel_MemberCount */
356 Channel_CountForUser( CLIENT *Client )
358 /* Count number of channels a user is member of. */
363 assert( Client != NULL );
365 cl2chan = My_Cl2Chan;
368 if( cl2chan->client == Client ) count++;
369 cl2chan = cl2chan->next;
373 } /* Channel_CountForUser */
377 Channel_Name( const CHANNEL *Chan )
379 assert( Chan != NULL );
385 Channel_Modes( CHANNEL *Chan )
387 assert( Chan != NULL );
389 } /* Channel_Modes */
393 Channel_Key( CHANNEL *Chan )
395 assert( Chan != NULL );
401 Channel_MaxUsers( CHANNEL *Chan )
403 assert( Chan != NULL );
404 return Chan->maxusers;
405 } /* Channel_MaxUsers */
409 Channel_First( void )
412 } /* Channel_First */
416 Channel_Next( CHANNEL *Chan )
418 assert( Chan != NULL );
424 Channel_Search( const char *Name )
426 /* Search channel structure */
431 assert( Name != NULL );
433 search_hash = Hash( Name );
437 if( search_hash == c->hash )
440 if( strcasecmp( Name, c->name ) == 0 ) return c;
445 } /* Channel_Search */
449 Channel_FirstMember( CHANNEL *Chan )
451 assert( Chan != NULL );
452 return Get_First_Cl2Chan( NULL, Chan );
453 } /* Channel_FirstMember */
457 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
459 assert( Chan != NULL );
460 assert( Cl2Chan != NULL );
461 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
462 } /* Channel_NextMember */
466 Channel_FirstChannelOf( CLIENT *Client )
468 assert( Client != NULL );
469 return Get_First_Cl2Chan( Client, NULL );
470 } /* Channel_FirstChannelOf */
474 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
476 assert( Client != NULL );
477 assert( Cl2Chan != NULL );
478 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
479 } /* Channel_NextChannelOf */
483 Channel_GetClient( CL2CHAN *Cl2Chan )
485 assert( Cl2Chan != NULL );
486 return Cl2Chan->client;
487 } /* Channel_GetClient */
491 Channel_GetChannel( CL2CHAN *Cl2Chan )
493 assert( Cl2Chan != NULL );
494 return Cl2Chan->channel;
495 } /* Channel_GetChannel */
499 Channel_IsValidName( const char *Name )
501 assert( Name != NULL );
504 if (strlen(Name) <= 1)
507 if (strchr("+#", Name[0]) == NULL)
509 if (strlen(Name) >= CHANNEL_NAME_LEN)
512 return Name[strcspn(Name, " ,:\007")] == 0;
513 } /* Channel_IsValidName */
517 Channel_ModeAdd( CHANNEL *Chan, char Mode )
520 * If the channel already had this mode, return false.
521 * If the channel mode was newly set return true.
526 assert( Chan != NULL );
528 x[0] = Mode; x[1] = '\0';
529 if( ! strchr( Chan->modes, x[0] ))
531 /* Channel does not have this mode yet, set it */
532 strlcat( Chan->modes, x, sizeof( Chan->modes ));
536 } /* Channel_ModeAdd */
540 Channel_ModeDel( CHANNEL *Chan, char Mode )
543 * if the mode was removed return true.
544 * if the channel did not have the mode, return false.
548 assert( Chan != NULL );
550 p = strchr( Chan->modes, Mode );
551 if( ! p ) return false;
553 /* Channel has mode -> delete */
560 } /* Channel_ModeDel */
564 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
566 /* Set Channel-User-Mode.
567 * if mode was newly set, return true.
568 * if the User already had this channel-mode, return false.
574 assert( Chan != NULL );
575 assert( Client != NULL );
577 cl2chan = Get_Cl2Chan( Chan, Client );
578 assert( cl2chan != NULL );
580 x[0] = Mode; x[1] = '\0';
581 if( ! strchr( cl2chan->modes, x[0] ))
583 /* mode not set, -> set it */
584 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
588 } /* Channel_UserModeAdd */
592 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
594 /* Delete Channel-User-Mode.
595 * If Mode was removed, return true.
596 * If User did not have the Channel-Mode, return false.
602 assert( Chan != NULL );
603 assert( Client != NULL );
605 cl2chan = Get_Cl2Chan( Chan, Client );
606 assert( cl2chan != NULL );
608 p = strchr( cl2chan->modes, Mode );
609 if( ! p ) return false;
611 /* Client has Mode -> delete */
618 } /* Channel_UserModeDel */
622 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
624 /* return Users' Channel-Modes */
628 assert( Chan != NULL );
629 assert( Client != NULL );
631 cl2chan = Get_Cl2Chan( Chan, Client );
632 assert( cl2chan != NULL );
634 return cl2chan->modes;
635 } /* Channel_UserModes */
639 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
641 /* Test if Client is on Channel Chan */
643 assert( Chan != NULL );
644 assert( Client != NULL );
645 return Get_Cl2Chan(Chan, Client) != NULL;
646 } /* Channel_IsMemberOf */
650 Channel_Topic( CHANNEL *Chan )
653 assert( Chan != NULL );
654 ret = array_start(&Chan->topic);
655 return ret ? ret : "";
656 } /* Channel_Topic */
662 Channel_TopicTime(CHANNEL *Chan)
664 assert(Chan != NULL);
665 return (unsigned int) Chan->topic_time;
666 } /* Channel_TopicTime */
670 Channel_TopicWho(CHANNEL *Chan)
672 assert(Chan != NULL);
673 return Chan->topic_who;
674 } /* Channel_TopicWho */
680 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, char *Topic)
683 assert( Chan != NULL );
684 assert( Topic != NULL );
687 if (len < array_bytes(&Chan->topic))
688 array_free(&Chan->topic);
690 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
691 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
692 Topic, Chan->name, strerror(errno));
694 Chan->topic_time = time(NULL);
695 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
696 strlcpy(Chan->topic_who, Client_ID(Client),
697 sizeof Chan->topic_who);
699 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
700 sizeof Chan->topic_who);
704 } /* Channel_SetTopic */
708 Channel_SetModes( CHANNEL *Chan, char *Modes )
710 assert( Chan != NULL );
711 assert( Modes != NULL );
713 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
714 } /* Channel_SetModes */
718 Channel_SetKey( CHANNEL *Chan, char *Key )
720 assert( Chan != NULL );
721 assert( Key != NULL );
723 strlcpy( Chan->key, Key, sizeof( Chan->key ));
724 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
725 } /* Channel_SetKey */
729 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
731 assert( Chan != NULL );
733 Chan->maxusers = Count;
734 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
735 } /* Channel_SetMaxUsers */
739 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
741 bool is_member, has_voice, is_op;
743 is_member = has_voice = is_op = false;
745 if (Channel_IsMemberOf(Chan, From)) {
747 if (strchr(Channel_UserModes(Chan, From), 'v'))
749 if (strchr(Channel_UserModes(Chan, From), 'o'))
754 * Is the client allowed to write to channel?
756 * If channel mode n set: non-members cannot send to channel.
757 * If channel mode m set: need voice.
759 if (strchr(Channel_Modes(Chan), 'n') && !is_member)
762 if (is_op || has_voice)
765 if (strchr(Channel_Modes(Chan), 'm'))
768 return !Lists_Check(&Chan->list_bans, From);
773 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Text)
775 if (!Can_Send_To_Channel(Chan, From))
776 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG, Client_ID(From), Channel_Name(Chan));
778 if (Client_Conn(From) > NONE)
779 Conn_UpdateIdle(Client_Conn(From));
781 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
782 "PRIVMSG %s :%s", Channel_Name(Chan), Text);
787 Channel_Notice(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Text)
789 if (!Can_Send_To_Channel(Chan, From))
790 return true; /* no error, see RFC 2812 */
792 if (Client_Conn(From) > NONE)
793 Conn_UpdateIdle(Client_Conn(From));
795 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
796 "NOTICE %s :%s", Channel_Name(Chan), Text);
801 Channel_Create( char *Name )
803 /* Create new CHANNEL structure and add it to linked list */
806 assert( Name != NULL );
808 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
811 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
814 memset( c, 0, sizeof( CHANNEL ));
815 strlcpy( c->name, Name, sizeof( c->name ));
816 c->hash = Hash( c->name );
817 c->next = My_Channels;
819 LogDebug("Created new channel structure for \"%s\".", Name);
821 } /* Channel_Create */
825 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
829 assert( Chan != NULL );
830 assert( Client != NULL );
832 cl2chan = My_Cl2Chan;
835 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
836 cl2chan = cl2chan->next;
843 Add_Client( CHANNEL *Chan, CLIENT *Client )
847 assert( Chan != NULL );
848 assert( Client != NULL );
850 /* Create new CL2CHAN structure */
851 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
854 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
857 cl2chan->channel = Chan;
858 cl2chan->client = Client;
859 strcpy( cl2chan->modes, "" );
862 cl2chan->next = My_Cl2Chan;
863 My_Cl2Chan = cl2chan;
865 Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
872 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
874 CL2CHAN *cl2chan, *last_cl2chan;
877 assert( Chan != NULL );
878 assert( Client != NULL );
879 assert( Origin != NULL );
880 assert( Reason != NULL );
883 cl2chan = My_Cl2Chan;
886 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
887 last_cl2chan = cl2chan;
888 cl2chan = cl2chan->next;
890 if( ! cl2chan ) return false;
892 c = cl2chan->channel;
895 /* maintain cl2chan list */
896 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
897 else My_Cl2Chan = cl2chan->next;
903 /* QUIT: other servers have already been notified, see Client_Destroy();
904 * so only inform other clients in same channel. */
905 assert( InformServer == false );
906 LogDebug("User \"%s\" left channel \"%s\" (%s).",
907 Client_Mask( Client ), c->name, Reason );
910 /* User was KICKed: inform other servers and all users in channel */
912 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
913 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
914 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
915 c->name, Client_ID( Client ), Reason );
916 if ((Client_Conn(Client) > NONE) &&
917 (Client_Type(Client) == CLIENT_USER))
919 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
920 c->name, Client_ID( Client ), Reason);
922 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
923 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
927 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
929 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
932 if ((Client_Conn(Origin) > NONE) &&
933 (Client_Type(Origin) == CLIENT_USER))
935 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
936 LogDebug("User \"%s\" left channel \"%s\" (%s).",
937 Client_Mask(Client), c->name, Reason);
941 /* When channel is empty and is not pre-defined, delete */
942 if( ! strchr( Channel_Modes( Chan ), 'P' ))
944 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
948 } /* Remove_Client */
952 Channel_AddBan(CHANNEL *c, const char *mask )
954 struct list_head *h = Channel_GetListBans(c);
955 return Lists_Add(h, mask, false);
960 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
962 struct list_head *h = Channel_GetListInvites(c);
963 return Lists_Add(h, mask, onlyonce);
968 ShowInvitesBans(struct list_head *head, CLIENT *Client, CHANNEL *Channel, bool invite)
971 char *msg = invite ? RPL_INVITELIST_MSG : RPL_BANLIST_MSG;
974 assert( Client != NULL );
975 assert( Channel != NULL );
977 e = Lists_GetFirst(head);
979 if( ! IRC_WriteStrClient( Client, msg, Client_ID( Client ),
980 Channel_Name( Channel ), Lists_GetMask(e) )) return DISCONNECTED;
981 e = Lists_GetNext(e);
984 msg_end = invite ? RPL_ENDOFINVITELIST_MSG : RPL_ENDOFBANLIST_MSG;
985 return IRC_WriteStrClient( Client, msg_end, Client_ID( Client ), Channel_Name( Channel ));
990 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
994 assert( Channel != NULL );
996 h = Channel_GetListBans(Channel);
997 return ShowInvitesBans(h, Client, Channel, false);
1002 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1004 struct list_head *h;
1006 assert( Channel != NULL );
1008 h = Channel_GetListInvites(Channel);
1009 return ShowInvitesBans(h, Client, Channel, true);
1014 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1016 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1017 } /* Get_First_Cl2Chan */
1021 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1025 assert( Client != NULL || Channel != NULL );
1030 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1031 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1032 cl2chan = cl2chan->next;
1035 } /* Get_Next_Cl2Chan */
1039 Delete_Channel( CHANNEL *Chan )
1041 /* delete channel structure */
1043 CHANNEL *chan, *last_chan;
1049 if( chan == Chan ) break;
1053 if( ! chan ) return false;
1055 Log( LOG_DEBUG, "Freed channel structure for \"%s\".", Chan->name );
1057 /* free invite and ban lists */
1058 Lists_Free( &chan->list_bans );
1059 Lists_Free( &chan->list_invites );
1061 /* maintain channel list */
1062 if( last_chan ) last_chan->next = chan->next;
1063 else My_Channels = chan->next;
1067 } /* Delete_Channel */