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 );
503 if (strchr("+#", Name[0]) == NULL)
505 if (strlen(Name) >= CHANNEL_NAME_LEN)
508 return Name[strcspn(Name, " ,:\007")] == 0;
509 } /* Channel_IsValidName */
513 Channel_ModeAdd( CHANNEL *Chan, char Mode )
516 * If the channel already had this mode, return false.
517 * If the channel mode was newly set return true.
522 assert( Chan != NULL );
524 x[0] = Mode; x[1] = '\0';
525 if( ! strchr( Chan->modes, x[0] ))
527 /* Channel does not have this mode yet, set it */
528 strlcat( Chan->modes, x, sizeof( Chan->modes ));
532 } /* Channel_ModeAdd */
536 Channel_ModeDel( CHANNEL *Chan, char Mode )
539 * if the mode was removed return true.
540 * if the channel did not have the mode, return false.
544 assert( Chan != NULL );
546 p = strchr( Chan->modes, Mode );
547 if( ! p ) return false;
549 /* Channel has mode -> delete */
556 } /* Channel_ModeDel */
560 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
562 /* Set Channel-User-Mode.
563 * if mode was newly set, return true.
564 * if the User already had this 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';
577 if( ! strchr( cl2chan->modes, x[0] ))
579 /* mode not set, -> set it */
580 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
584 } /* Channel_UserModeAdd */
588 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
590 /* Delete Channel-User-Mode.
591 * If Mode was removed, return true.
592 * If User did not have the Channel-Mode, return false.
598 assert( Chan != NULL );
599 assert( Client != NULL );
601 cl2chan = Get_Cl2Chan( Chan, Client );
602 assert( cl2chan != NULL );
604 p = strchr( cl2chan->modes, Mode );
605 if( ! p ) return false;
607 /* Client has Mode -> delete */
614 } /* Channel_UserModeDel */
618 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
620 /* return Users' Channel-Modes */
624 assert( Chan != NULL );
625 assert( Client != NULL );
627 cl2chan = Get_Cl2Chan( Chan, Client );
628 assert( cl2chan != NULL );
630 return cl2chan->modes;
631 } /* Channel_UserModes */
635 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
637 /* Test if Client is on Channel Chan */
639 assert( Chan != NULL );
640 assert( Client != NULL );
641 return Get_Cl2Chan(Chan, Client) != NULL;
642 } /* Channel_IsMemberOf */
646 Channel_Topic( CHANNEL *Chan )
649 assert( Chan != NULL );
650 ret = array_start(&Chan->topic);
651 return ret ? ret : "";
652 } /* Channel_Topic */
658 Channel_TopicTime(CHANNEL *Chan)
660 assert(Chan != NULL);
661 return (unsigned int) Chan->topic_time;
662 } /* Channel_TopicTime */
666 Channel_TopicWho(CHANNEL *Chan)
668 assert(Chan != NULL);
669 return Chan->topic_who;
670 } /* Channel_TopicWho */
676 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, char *Topic)
679 assert( Chan != NULL );
680 assert( Topic != NULL );
683 if (len < array_bytes(&Chan->topic))
684 array_free(&Chan->topic);
686 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
687 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
688 Topic, Chan->name, strerror(errno));
690 Chan->topic_time = time(NULL);
691 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
692 strlcpy(Chan->topic_who, Client_ID(Client),
693 sizeof Chan->topic_who);
695 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
696 sizeof Chan->topic_who);
700 } /* Channel_SetTopic */
704 Channel_SetModes( CHANNEL *Chan, char *Modes )
706 assert( Chan != NULL );
707 assert( Modes != NULL );
709 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
710 } /* Channel_SetModes */
714 Channel_SetKey( CHANNEL *Chan, char *Key )
716 assert( Chan != NULL );
717 assert( Key != NULL );
719 strlcpy( Chan->key, Key, sizeof( Chan->key ));
720 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
721 } /* Channel_SetKey */
725 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
727 assert( Chan != NULL );
729 Chan->maxusers = Count;
730 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
731 } /* Channel_SetMaxUsers */
735 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
737 bool is_member, has_voice, is_op;
739 is_member = has_voice = is_op = false;
741 if (Channel_IsMemberOf(Chan, From)) {
743 if (strchr(Channel_UserModes(Chan, From), 'v'))
745 if (strchr(Channel_UserModes(Chan, From), 'o'))
750 * Is the client allowed to write to channel?
752 * If channel mode n set: non-members cannot send to channel.
753 * If channel mode m set: need voice.
755 if (strchr(Channel_Modes(Chan), 'n') && !is_member)
758 if (is_op || has_voice)
761 if (strchr(Channel_Modes(Chan), 'm'))
764 return !Lists_Check(&Chan->list_bans, From);
769 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Text)
771 if (!Can_Send_To_Channel(Chan, From))
772 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG, Client_ID(From), Channel_Name(Chan));
774 if (Client_Conn(From) > NONE)
775 Conn_UpdateIdle(Client_Conn(From));
777 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
778 "PRIVMSG %s :%s", Channel_Name(Chan), Text);
783 Channel_Notice(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Text)
785 if (!Can_Send_To_Channel(Chan, From))
786 return true; /* no error, see RFC 2812 */
788 if (Client_Conn(From) > NONE)
789 Conn_UpdateIdle(Client_Conn(From));
791 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
792 "NOTICE %s :%s", Channel_Name(Chan), Text);
797 Channel_Create( char *Name )
799 /* Create new CHANNEL structure and add it to linked list */
802 assert( Name != NULL );
804 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
807 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
810 memset( c, 0, sizeof( CHANNEL ));
811 strlcpy( c->name, Name, sizeof( c->name ));
812 c->hash = Hash( c->name );
813 c->next = My_Channels;
815 LogDebug("Created new channel structure for \"%s\".", Name);
817 } /* Channel_Create */
821 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
825 assert( Chan != NULL );
826 assert( Client != NULL );
828 cl2chan = My_Cl2Chan;
831 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
832 cl2chan = cl2chan->next;
839 Add_Client( CHANNEL *Chan, CLIENT *Client )
843 assert( Chan != NULL );
844 assert( Client != NULL );
846 /* Create new CL2CHAN structure */
847 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
850 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
853 cl2chan->channel = Chan;
854 cl2chan->client = Client;
855 strcpy( cl2chan->modes, "" );
858 cl2chan->next = My_Cl2Chan;
859 My_Cl2Chan = cl2chan;
861 Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
868 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
870 CL2CHAN *cl2chan, *last_cl2chan;
873 assert( Chan != NULL );
874 assert( Client != NULL );
875 assert( Origin != NULL );
876 assert( Reason != NULL );
879 cl2chan = My_Cl2Chan;
882 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
883 last_cl2chan = cl2chan;
884 cl2chan = cl2chan->next;
886 if( ! cl2chan ) return false;
888 c = cl2chan->channel;
891 /* maintain cl2chan list */
892 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
893 else My_Cl2Chan = cl2chan->next;
899 /* QUIT: other servers have already been notified, see Client_Destroy();
900 * so only inform other clients in same channel. */
901 assert( InformServer == false );
902 LogDebug("User \"%s\" left channel \"%s\" (%s).",
903 Client_Mask( Client ), c->name, Reason );
906 /* User was KICKed: inform other servers and all users in channel */
908 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
909 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
910 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
911 c->name, Client_ID( Client ), Reason );
912 if ((Client_Conn(Client) > NONE) &&
913 (Client_Type(Client) == CLIENT_USER))
915 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
916 c->name, Client_ID( Client ), Reason);
918 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
919 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
923 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
925 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
928 if ((Client_Conn(Origin) > NONE) &&
929 (Client_Type(Origin) == CLIENT_USER))
931 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
932 LogDebug("User \"%s\" left channel \"%s\" (%s).",
933 Client_Mask(Client), c->name, Reason);
937 /* When channel is empty and is not pre-defined, delete */
938 if( ! strchr( Channel_Modes( Chan ), 'P' ))
940 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
944 } /* Remove_Client */
948 Channel_AddBan(CHANNEL *c, const char *mask )
950 struct list_head *h = Channel_GetListBans(c);
951 return Lists_Add(h, mask, false);
956 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
958 struct list_head *h = Channel_GetListInvites(c);
959 return Lists_Add(h, mask, onlyonce);
964 ShowInvitesBans(struct list_head *head, CLIENT *Client, CHANNEL *Channel, bool invite)
967 char *msg = invite ? RPL_INVITELIST_MSG : RPL_BANLIST_MSG;
970 assert( Client != NULL );
971 assert( Channel != NULL );
973 e = Lists_GetFirst(head);
975 if( ! IRC_WriteStrClient( Client, msg, Client_ID( Client ),
976 Channel_Name( Channel ), Lists_GetMask(e) )) return DISCONNECTED;
977 e = Lists_GetNext(e);
980 msg_end = invite ? RPL_ENDOFINVITELIST_MSG : RPL_ENDOFBANLIST_MSG;
981 return IRC_WriteStrClient( Client, msg_end, Client_ID( Client ), Channel_Name( Channel ));
986 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
990 assert( Channel != NULL );
992 h = Channel_GetListBans(Channel);
993 return ShowInvitesBans(h, Client, Channel, false);
998 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1000 struct list_head *h;
1002 assert( Channel != NULL );
1004 h = Channel_GetListInvites(Channel);
1005 return ShowInvitesBans(h, Client, Channel, true);
1010 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1012 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1013 } /* Get_First_Cl2Chan */
1017 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1021 assert( Client != NULL || Channel != NULL );
1026 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1027 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1028 cl2chan = cl2chan->next;
1031 } /* Get_Next_Cl2Chan */
1035 Delete_Channel( CHANNEL *Chan )
1037 /* delete channel structure */
1039 CHANNEL *chan, *last_chan;
1045 if( chan == Chan ) break;
1049 if( ! chan ) return false;
1051 Log( LOG_DEBUG, "Freed channel structure for \"%s\".", Chan->name );
1053 /* free invite and ban lists */
1054 Lists_Free( &chan->list_bans );
1055 Lists_Free( &chan->list_invites );
1057 /* maintain channel list */
1058 if( last_chan ) last_chan->next = chan->next;
1059 else My_Channels = chan->next;
1063 } /* Delete_Channel */