2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2008 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.
28 #include "conn-func.h"
35 #include "irc-write.h"
51 static CHANNEL *My_Channels;
52 static CL2CHAN *My_Cl2Chan;
55 static CL2CHAN *Get_Cl2Chan PARAMS(( CHANNEL *Chan, CLIENT *Client ));
56 static CL2CHAN *Add_Client PARAMS(( CHANNEL *Chan, CLIENT *Client ));
57 static bool Remove_Client PARAMS(( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer ));
58 static CL2CHAN *Get_First_Cl2Chan PARAMS(( CLIENT *Client, CHANNEL *Chan ));
59 static CL2CHAN *Get_Next_Cl2Chan PARAMS(( CL2CHAN *Start, CLIENT *Client, CHANNEL *Chan ));
60 static bool Delete_Channel PARAMS(( CHANNEL *Chan ));
71 sc = Channel_Create("&SERVER");
73 Channel_SetModes(sc, "mnPt");
74 Channel_SetTopic(sc, Client_ThisServer(), "Server Messages");
79 GLOBAL struct list_head *
80 Channel_GetListBans(CHANNEL *c)
87 GLOBAL struct list_head *
88 Channel_GetListInvites(CHANNEL *c)
91 return &c->list_invites;
96 Channel_InitPredefined( void )
98 /* Generate predefined persistent channels */
104 for( i = 0; i < Conf_Channel_Count; i++ )
106 /* Check for Name configuration */
107 if( ! Conf_Channel[i].name[0] ) continue;
109 /* Check for invalid channel name */
110 if( ! Channel_IsValidName( Conf_Channel[i].name ))
112 Log( LOG_ERR, "Can't create pre-defined channel: invalid name: \"%s\"!", Conf_Channel[i].name );
113 array_free(&Conf_Channel[i].topic);
117 /* Check if the channel name is already in use */
118 chan = Channel_Search( Conf_Channel[i].name );
121 Log( LOG_INFO, "Can't create pre-defined channel \"%s\": name already in use.", Conf_Channel[i].name );
122 array_free(&Conf_Channel[i].topic);
127 chan = Channel_Create(Conf_Channel[i].name);
129 Channel_ModeAdd(chan, 'P');
131 if (array_start(&Conf_Channel[i].topic) != NULL)
132 Channel_SetTopic(chan, NULL,
133 array_start(&Conf_Channel[i].topic));
134 array_free(&Conf_Channel[i].topic);
136 c = Conf_Channel[i].modes;
138 Channel_ModeAdd(chan, *c++);
140 Channel_SetKey(chan, Conf_Channel[i].key);
141 Channel_SetMaxUsers(chan, Conf_Channel[i].maxusers);
143 Log(LOG_INFO, "Created pre-defined channel \"%s\".",
144 Conf_Channel[i].name );
146 else Log(LOG_ERR, "Can't create pre-defined channel \"%s\"!",
147 Conf_Channel[i].name );
149 } /* Channel_InitPredefined */
156 CL2CHAN *cl2chan, *cl2chan_next;
158 /* free struct Channel */
163 array_free(&c->topic);
168 /* Free Channel allocation table */
169 cl2chan = My_Cl2Chan;
172 cl2chan_next = cl2chan->next;
174 cl2chan = cl2chan_next;
181 * This function lets a client join a channel. First, the function
182 * checks that the specified channel name is valid and that the client
183 * isn't already a member. If the specified channel doesn't exist,
184 * a new channel is created. Client is added to channel by function
188 Channel_Join( CLIENT *Client, char *Name )
192 assert(Client != NULL);
193 assert(Name != NULL);
195 /* Check that the channel name is valid */
196 if (! Channel_IsValidName(Name)) {
197 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
198 Client_ID(Client), Name);
202 chan = Channel_Search(Name);
204 /* Check if the client is already in the channel */
205 if (Get_Cl2Chan(chan, Client))
208 /* If the specified channel does not exist, the channel
210 chan = Channel_Create(Name);
215 /* Add user to Channel */
216 if (! Add_Client(chan, Client))
224 * Part client from channel.
225 * This function lets a client part from a channel. First, the function checks
226 * if the channel exists and the client is a member of it and sends out
227 * appropriate error messages if not. The real work is done by the function
231 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
235 assert(Client != NULL);
236 assert(Name != NULL);
237 assert(Reason != NULL);
239 /* Check that specified channel exists */
240 chan = Channel_Search(Name);
242 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
243 Client_ID(Client), Name);
247 /* Check that the client is in the channel */
248 if (!Get_Cl2Chan(chan, Client)) {
249 IRC_WriteStrClient(Client, ERR_NOTONCHANNEL_MSG,
250 Client_ID(Client), Name);
254 /* Part client from channel */
255 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
263 * Kick user from Channel
266 Channel_Kick(CLIENT *Peer, CLIENT *Target, CLIENT *Origin, const char *Name,
271 assert(Peer != NULL);
272 assert(Target != NULL);
273 assert(Origin != NULL);
274 assert(Name != NULL);
275 assert(Reason != NULL);
277 /* Check that channel exists */
278 chan = Channel_Search( Name );
281 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
285 if (Client_Type(Peer) != CLIENT_SERVER &&
286 Client_Type(Origin) != CLIENT_SERVICE) {
287 /* Check that user is on the specified channel */
288 if (!Channel_IsMemberOf(chan, Origin)) {
289 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG,
290 Client_ID(Origin), Name);
294 /* Check if user has operator status */
295 if (!strchr(Channel_UserModes(chan, Origin), 'o')) {
296 IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG,
297 Client_ID(Origin), Name);
302 /* Check that the client to be kicked is on the specified channel */
303 if (!Channel_IsMemberOf(chan, Target)) {
304 IRC_WriteStrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
305 Client_ID(Origin), Client_ID(Target), Name );
309 /* Kick Client from channel */
310 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
315 Channel_Quit( CLIENT *Client, char *Reason )
319 assert( Client != NULL );
320 assert( Reason != NULL );
322 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
328 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
335 Channel_Count( void )
338 unsigned long count = 0;
347 } /* Channel_Count */
351 Channel_MemberCount( CHANNEL *Chan )
354 unsigned long count = 0;
356 assert( Chan != NULL );
358 cl2chan = My_Cl2Chan;
361 if( cl2chan->channel == Chan ) count++;
362 cl2chan = cl2chan->next;
365 } /* Channel_MemberCount */
369 Channel_CountForUser( CLIENT *Client )
371 /* Count number of channels a user is member of. */
376 assert( Client != NULL );
378 cl2chan = My_Cl2Chan;
381 if( cl2chan->client == Client ) count++;
382 cl2chan = cl2chan->next;
386 } /* Channel_CountForUser */
390 Channel_Name( const CHANNEL *Chan )
392 assert( Chan != NULL );
398 Channel_Modes( CHANNEL *Chan )
400 assert( Chan != NULL );
402 } /* Channel_Modes */
406 Channel_Key( CHANNEL *Chan )
408 assert( Chan != NULL );
414 Channel_MaxUsers( CHANNEL *Chan )
416 assert( Chan != NULL );
417 return Chan->maxusers;
418 } /* Channel_MaxUsers */
422 Channel_First( void )
425 } /* Channel_First */
429 Channel_Next( CHANNEL *Chan )
431 assert( Chan != NULL );
437 Channel_Search( const char *Name )
439 /* Search channel structure */
444 assert( Name != NULL );
446 search_hash = Hash( Name );
450 if( search_hash == c->hash )
453 if( strcasecmp( Name, c->name ) == 0 ) return c;
458 } /* Channel_Search */
462 Channel_FirstMember( CHANNEL *Chan )
464 assert( Chan != NULL );
465 return Get_First_Cl2Chan( NULL, Chan );
466 } /* Channel_FirstMember */
470 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
472 assert( Chan != NULL );
473 assert( Cl2Chan != NULL );
474 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
475 } /* Channel_NextMember */
479 Channel_FirstChannelOf( CLIENT *Client )
481 assert( Client != NULL );
482 return Get_First_Cl2Chan( Client, NULL );
483 } /* Channel_FirstChannelOf */
487 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
489 assert( Client != NULL );
490 assert( Cl2Chan != NULL );
491 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
492 } /* Channel_NextChannelOf */
496 Channel_GetClient( CL2CHAN *Cl2Chan )
498 assert( Cl2Chan != NULL );
499 return Cl2Chan->client;
500 } /* Channel_GetClient */
504 Channel_GetChannel( CL2CHAN *Cl2Chan )
506 assert( Cl2Chan != NULL );
507 return Cl2Chan->channel;
508 } /* Channel_GetChannel */
512 Channel_IsValidName( const char *Name )
514 assert( Name != NULL );
517 if (strlen(Name) <= 1)
520 if (strchr("#&+", Name[0]) == NULL)
522 if (strlen(Name) >= CHANNEL_NAME_LEN)
525 return Name[strcspn(Name, " ,:\007")] == 0;
526 } /* Channel_IsValidName */
530 Channel_ModeAdd( CHANNEL *Chan, char Mode )
533 * If the channel already had this mode, return false.
534 * If the channel mode was newly set return true.
539 assert( Chan != NULL );
541 x[0] = Mode; x[1] = '\0';
542 if( ! strchr( Chan->modes, x[0] ))
544 /* Channel does not have this mode yet, set it */
545 strlcat( Chan->modes, x, sizeof( Chan->modes ));
549 } /* Channel_ModeAdd */
553 Channel_ModeDel( CHANNEL *Chan, char Mode )
556 * if the mode was removed return true.
557 * if the channel did not have the mode, return false.
561 assert( Chan != NULL );
563 p = strchr( Chan->modes, Mode );
564 if( ! p ) return false;
566 /* Channel has mode -> delete */
573 } /* Channel_ModeDel */
577 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
579 /* Set Channel-User-Mode.
580 * if mode was newly set, return true.
581 * if the User already had this channel-mode, return false.
587 assert( Chan != NULL );
588 assert( Client != NULL );
590 cl2chan = Get_Cl2Chan( Chan, Client );
591 assert( cl2chan != NULL );
593 x[0] = Mode; x[1] = '\0';
594 if( ! strchr( cl2chan->modes, x[0] ))
596 /* mode not set, -> set it */
597 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
601 } /* Channel_UserModeAdd */
605 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
607 /* Delete Channel-User-Mode.
608 * If Mode was removed, return true.
609 * If User did not have the Channel-Mode, return false.
615 assert( Chan != NULL );
616 assert( Client != NULL );
618 cl2chan = Get_Cl2Chan( Chan, Client );
619 assert( cl2chan != NULL );
621 p = strchr( cl2chan->modes, Mode );
622 if( ! p ) return false;
624 /* Client has Mode -> delete */
631 } /* Channel_UserModeDel */
635 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
637 /* return Users' Channel-Modes */
641 assert( Chan != NULL );
642 assert( Client != NULL );
644 cl2chan = Get_Cl2Chan( Chan, Client );
645 assert( cl2chan != NULL );
647 return cl2chan->modes;
648 } /* Channel_UserModes */
652 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
654 /* Test if Client is on Channel Chan */
656 assert( Chan != NULL );
657 assert( Client != NULL );
658 return Get_Cl2Chan(Chan, Client) != NULL;
659 } /* Channel_IsMemberOf */
663 Channel_Topic( CHANNEL *Chan )
666 assert( Chan != NULL );
667 ret = array_start(&Chan->topic);
668 return ret ? ret : "";
669 } /* Channel_Topic */
675 Channel_TopicTime(CHANNEL *Chan)
677 assert(Chan != NULL);
678 return (unsigned int) Chan->topic_time;
679 } /* Channel_TopicTime */
683 Channel_TopicWho(CHANNEL *Chan)
685 assert(Chan != NULL);
686 return Chan->topic_who;
687 } /* Channel_TopicWho */
693 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
696 assert( Chan != NULL );
697 assert( Topic != NULL );
700 if (len < array_bytes(&Chan->topic))
701 array_free(&Chan->topic);
703 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
704 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
705 Topic, Chan->name, strerror(errno));
707 Chan->topic_time = time(NULL);
708 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
709 strlcpy(Chan->topic_who, Client_ID(Client),
710 sizeof Chan->topic_who);
712 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
713 sizeof Chan->topic_who);
717 } /* Channel_SetTopic */
721 Channel_SetModes( CHANNEL *Chan, char *Modes )
723 assert( Chan != NULL );
724 assert( Modes != NULL );
726 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
727 } /* Channel_SetModes */
731 Channel_SetKey( CHANNEL *Chan, const char *Key )
733 assert( Chan != NULL );
734 assert( Key != NULL );
736 strlcpy( Chan->key, Key, sizeof( Chan->key ));
737 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
738 } /* Channel_SetKey */
742 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
744 assert( Chan != NULL );
746 Chan->maxusers = Count;
747 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
748 } /* Channel_SetMaxUsers */
752 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
754 bool is_member, has_voice, is_op;
756 is_member = has_voice = is_op = false;
758 /* The server itself always can send messages :-) */
759 if (Client_ThisServer() == From)
762 if (Channel_IsMemberOf(Chan, From)) {
764 if (strchr(Channel_UserModes(Chan, From), 'v'))
766 if (strchr(Channel_UserModes(Chan, From), 'o'))
771 * Is the client allowed to write to channel?
773 * If channel mode n set: non-members cannot send to channel.
774 * If channel mode m set: need voice.
776 if (strchr(Channel_Modes(Chan), 'n') && !is_member)
779 if (is_op || has_voice)
782 if (strchr(Channel_Modes(Chan), 'm'))
785 return !Lists_Check(&Chan->list_bans, From);
790 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
791 bool SendErrors, const char *Text)
793 if (!Can_Send_To_Channel(Chan, From)) {
795 return CONNECTED; /* no error, see RFC 2812 */
796 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
797 Client_ID(From), Channel_Name(Chan));
800 if (Client_Conn(From) > NONE)
801 Conn_UpdateIdle(Client_Conn(From));
803 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
804 "%s %s :%s", Command, Channel_Name(Chan), Text);
809 Channel_Create( const char *Name )
811 /* Create new CHANNEL structure and add it to linked list */
814 assert( Name != NULL );
816 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
819 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
822 memset( c, 0, sizeof( CHANNEL ));
823 strlcpy( c->name, Name, sizeof( c->name ));
824 c->hash = Hash( c->name );
825 c->next = My_Channels;
827 LogDebug("Created new channel structure for \"%s\".", Name);
829 } /* Channel_Create */
833 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
837 assert( Chan != NULL );
838 assert( Client != NULL );
840 cl2chan = My_Cl2Chan;
843 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
844 cl2chan = cl2chan->next;
851 Add_Client( CHANNEL *Chan, CLIENT *Client )
855 assert( Chan != NULL );
856 assert( Client != NULL );
858 /* Create new CL2CHAN structure */
859 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
862 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
865 cl2chan->channel = Chan;
866 cl2chan->client = Client;
867 strcpy( cl2chan->modes, "" );
870 cl2chan->next = My_Cl2Chan;
871 My_Cl2Chan = cl2chan;
873 Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
880 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
882 CL2CHAN *cl2chan, *last_cl2chan;
885 assert( Chan != NULL );
886 assert( Client != NULL );
887 assert( Origin != NULL );
888 assert( Reason != NULL );
890 /* Do not inform other servers if the channel is local to this server,
891 * regardless of what the caller requested! */
893 InformServer = !Channel_IsLocal(Chan);
896 cl2chan = My_Cl2Chan;
899 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
900 last_cl2chan = cl2chan;
901 cl2chan = cl2chan->next;
903 if( ! cl2chan ) return false;
905 c = cl2chan->channel;
908 /* maintain cl2chan list */
909 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
910 else My_Cl2Chan = cl2chan->next;
916 /* QUIT: other servers have already been notified,
917 * see Client_Destroy(); so only inform other clients
918 * in same channel. */
919 assert( InformServer == false );
920 LogDebug("User \"%s\" left channel \"%s\" (%s).",
921 Client_Mask( Client ), c->name, Reason );
924 /* User was KICKed: inform other servers (public
925 * channels) and all users in the channel */
927 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
928 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
929 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
930 c->name, Client_ID( Client ), Reason );
931 if ((Client_Conn(Client) > NONE) &&
932 (Client_Type(Client) == CLIENT_USER))
934 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
935 c->name, Client_ID( Client ), Reason);
937 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
938 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
942 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
944 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
947 if ((Client_Conn(Origin) > NONE) &&
948 (Client_Type(Origin) == CLIENT_USER))
950 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
951 LogDebug("User \"%s\" left channel \"%s\" (%s).",
952 Client_Mask(Client), c->name, Reason);
956 /* When channel is empty and is not pre-defined, delete */
957 if( ! strchr( Channel_Modes( Chan ), 'P' ))
959 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
963 } /* Remove_Client */
967 Channel_AddBan(CHANNEL *c, const char *mask )
969 struct list_head *h = Channel_GetListBans(c);
970 return Lists_Add(h, mask, false);
975 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
977 struct list_head *h = Channel_GetListInvites(c);
978 return Lists_Add(h, mask, onlyonce);
983 ShowInvitesBans(struct list_head *head, CLIENT *Client, CHANNEL *Channel, bool invite)
986 char *msg = invite ? RPL_INVITELIST_MSG : RPL_BANLIST_MSG;
989 assert( Client != NULL );
990 assert( Channel != NULL );
992 e = Lists_GetFirst(head);
994 if( ! IRC_WriteStrClient( Client, msg, Client_ID( Client ),
995 Channel_Name( Channel ), Lists_GetMask(e) )) return DISCONNECTED;
996 e = Lists_GetNext(e);
999 msg_end = invite ? RPL_ENDOFINVITELIST_MSG : RPL_ENDOFBANLIST_MSG;
1000 return IRC_WriteStrClient( Client, msg_end, Client_ID( Client ), Channel_Name( Channel ));
1005 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1007 struct list_head *h;
1009 assert( Channel != NULL );
1011 h = Channel_GetListBans(Channel);
1012 return ShowInvitesBans(h, Client, Channel, false);
1017 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1019 struct list_head *h;
1021 assert( Channel != NULL );
1023 h = Channel_GetListInvites(Channel);
1024 return ShowInvitesBans(h, Client, Channel, true);
1029 * Log a message to the local &SERVER channel, if it exists.
1032 Channel_LogServer(char *msg)
1037 assert(msg != NULL);
1039 sc = Channel_Search("&SERVER");
1043 c = Client_ThisServer();
1044 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1045 } /* Channel_LogServer */
1049 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1051 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1052 } /* Get_First_Cl2Chan */
1056 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1060 assert( Client != NULL || Channel != NULL );
1065 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1066 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1067 cl2chan = cl2chan->next;
1070 } /* Get_Next_Cl2Chan */
1074 Delete_Channel( CHANNEL *Chan )
1076 /* delete channel structure */
1078 CHANNEL *chan, *last_chan;
1084 if( chan == Chan ) break;
1088 if( ! chan ) return false;
1090 Log( LOG_DEBUG, "Freed channel structure for \"%s\".", Chan->name );
1092 /* free invite and ban lists */
1093 Lists_Free( &chan->list_bans );
1094 Lists_Free( &chan->list_invites );
1096 /* maintain channel list */
1097 if( last_chan ) last_chan->next = chan->next;
1098 else My_Channels = chan->next;
1102 } /* Delete_Channel */