2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2009 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 void Delete_Channel PARAMS(( CHANNEL *Chan ));
61 static void Free_Channel PARAMS(( CHANNEL *Chan ));
72 sc = Channel_Create("&SERVER");
74 Channel_SetModes(sc, "mnPt");
75 Channel_SetTopic(sc, Client_ThisServer(), "Server Messages");
80 GLOBAL struct list_head *
81 Channel_GetListBans(CHANNEL *c)
88 GLOBAL struct list_head *
89 Channel_GetListInvites(CHANNEL *c)
92 return &c->list_invites;
97 Channel_InitPredefined( void )
99 /* Generate predefined persistent channels */
102 const struct Conf_Channel *conf_chan;
104 size_t i, channel_count = array_length(&Conf_Channels, sizeof(*conf_chan));
106 conf_chan = array_start(&Conf_Channels);
108 assert(channel_count == 0 || conf_chan != NULL);
110 for (i = 0; i < channel_count; i++, conf_chan++) {
111 if (!conf_chan->name[0] || !Channel_IsValidName(conf_chan->name)) {
112 Log(LOG_ERR, "Can't create pre-defined channel: invalid name: \"%s\"",
117 new_chan = Channel_Search(conf_chan->name);
119 Log(LOG_INFO, "Can't create pre-defined channel \"%s\": name already in use.",
124 new_chan = Channel_Create(conf_chan->name);
126 Log(LOG_ERR, "Can't create pre-defined channel \"%s\"",
131 Channel_ModeAdd(new_chan, 'P');
133 if (conf_chan->topic[0])
134 Channel_SetTopic(new_chan, NULL, conf_chan->topic);
136 c = conf_chan->modes;
138 Channel_ModeAdd(new_chan, *c++);
140 Channel_SetKey(new_chan, conf_chan->key);
141 Channel_SetMaxUsers(new_chan, conf_chan->maxusers);
142 Log(LOG_INFO, "Created pre-defined channel \"%s\"",
146 array_free(&Conf_Channels);
147 } /* Channel_InitPredefined */
151 Free_Channel(CHANNEL *chan)
153 array_free(&chan->topic);
154 Lists_Free(&chan->list_bans);
155 Lists_Free(&chan->list_invites);
165 CL2CHAN *cl2chan, *cl2chan_next;
167 /* free struct Channel */
175 /* Free Channel allocation table */
176 cl2chan = My_Cl2Chan;
178 cl2chan_next = cl2chan->next;
180 cl2chan = cl2chan_next;
187 * This function lets a client join a channel. First, the function
188 * checks that the specified channel name is valid and that the client
189 * isn't already a member. If the specified channel doesn't exist,
190 * a new channel is created. Client is added to channel by function
194 Channel_Join( CLIENT *Client, char *Name )
198 assert(Client != NULL);
199 assert(Name != NULL);
201 /* Check that the channel name is valid */
202 if (! Channel_IsValidName(Name)) {
203 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
204 Client_ID(Client), Name);
208 chan = Channel_Search(Name);
210 /* Check if the client is already in the channel */
211 if (Get_Cl2Chan(chan, Client))
214 /* If the specified channel does not exist, the channel
216 chan = Channel_Create(Name);
221 /* Add user to Channel */
222 if (! Add_Client(chan, Client))
230 * Part client from channel.
231 * This function lets a client part from a channel. First, the function checks
232 * if the channel exists and the client is a member of it and sends out
233 * appropriate error messages if not. The real work is done by the function
237 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
241 assert(Client != NULL);
242 assert(Name != NULL);
243 assert(Reason != NULL);
245 /* Check that specified channel exists */
246 chan = Channel_Search(Name);
248 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
249 Client_ID(Client), Name);
253 /* Check that the client is in the channel */
254 if (!Get_Cl2Chan(chan, Client)) {
255 IRC_WriteStrClient(Client, ERR_NOTONCHANNEL_MSG,
256 Client_ID(Client), Name);
260 /* Part client from channel */
261 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
269 * Kick user from Channel
272 Channel_Kick(CLIENT *Peer, CLIENT *Target, CLIENT *Origin, const char *Name,
277 assert(Peer != NULL);
278 assert(Target != NULL);
279 assert(Origin != NULL);
280 assert(Name != NULL);
281 assert(Reason != NULL);
283 /* Check that channel exists */
284 chan = Channel_Search( Name );
287 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
291 if (Client_Type(Peer) != CLIENT_SERVER &&
292 Client_Type(Origin) != CLIENT_SERVICE) {
293 /* Check that user is on the specified channel */
294 if (!Channel_IsMemberOf(chan, Origin)) {
295 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG,
296 Client_ID(Origin), Name);
300 /* Check if user has operator status */
301 if (!strchr(Channel_UserModes(chan, Origin), 'o')) {
302 IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG,
303 Client_ID(Origin), Name);
308 /* Check that the client to be kicked is on the specified channel */
309 if (!Channel_IsMemberOf(chan, Target)) {
310 IRC_WriteStrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
311 Client_ID(Origin), Client_ID(Target), Name );
315 /* Kick Client from channel */
316 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
321 Channel_Quit( CLIENT *Client, char *Reason )
325 assert( Client != NULL );
326 assert( Reason != NULL );
328 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
334 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
341 Channel_Count( void )
344 unsigned long count = 0;
353 } /* Channel_Count */
357 Channel_MemberCount( CHANNEL *Chan )
360 unsigned long count = 0;
362 assert( Chan != NULL );
364 cl2chan = My_Cl2Chan;
367 if( cl2chan->channel == Chan ) count++;
368 cl2chan = cl2chan->next;
371 } /* Channel_MemberCount */
375 Channel_CountForUser( CLIENT *Client )
377 /* Count number of channels a user is member of. */
382 assert( Client != NULL );
384 cl2chan = My_Cl2Chan;
387 if( cl2chan->client == Client ) count++;
388 cl2chan = cl2chan->next;
392 } /* Channel_CountForUser */
396 Channel_Name( const CHANNEL *Chan )
398 assert( Chan != NULL );
404 Channel_Modes( CHANNEL *Chan )
406 assert( Chan != NULL );
408 } /* Channel_Modes */
412 Channel_Key( CHANNEL *Chan )
414 assert( Chan != NULL );
420 Channel_MaxUsers( CHANNEL *Chan )
422 assert( Chan != NULL );
423 return Chan->maxusers;
424 } /* Channel_MaxUsers */
428 Channel_First( void )
431 } /* Channel_First */
435 Channel_Next( CHANNEL *Chan )
437 assert( Chan != NULL );
443 Channel_Search( const char *Name )
445 /* Search channel structure */
450 assert( Name != NULL );
452 search_hash = Hash( Name );
456 if( search_hash == c->hash )
459 if( strcasecmp( Name, c->name ) == 0 ) return c;
464 } /* Channel_Search */
468 Channel_FirstMember( CHANNEL *Chan )
470 assert( Chan != NULL );
471 return Get_First_Cl2Chan( NULL, Chan );
472 } /* Channel_FirstMember */
476 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
478 assert( Chan != NULL );
479 assert( Cl2Chan != NULL );
480 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
481 } /* Channel_NextMember */
485 Channel_FirstChannelOf( CLIENT *Client )
487 assert( Client != NULL );
488 return Get_First_Cl2Chan( Client, NULL );
489 } /* Channel_FirstChannelOf */
493 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
495 assert( Client != NULL );
496 assert( Cl2Chan != NULL );
497 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
498 } /* Channel_NextChannelOf */
502 Channel_GetClient( CL2CHAN *Cl2Chan )
504 assert( Cl2Chan != NULL );
505 return Cl2Chan->client;
506 } /* Channel_GetClient */
510 Channel_GetChannel( CL2CHAN *Cl2Chan )
512 assert( Cl2Chan != NULL );
513 return Cl2Chan->channel;
514 } /* Channel_GetChannel */
518 Channel_IsValidName( const char *Name )
520 assert( Name != NULL );
523 if (strlen(Name) <= 1)
526 if (strchr("#&+", Name[0]) == NULL)
528 if (strlen(Name) >= CHANNEL_NAME_LEN)
531 return Name[strcspn(Name, " ,:\007")] == 0;
532 } /* Channel_IsValidName */
536 Channel_ModeAdd( CHANNEL *Chan, char Mode )
539 * If the channel already had this mode, return false.
540 * If the channel mode was newly set return true.
545 assert( Chan != NULL );
547 x[0] = Mode; x[1] = '\0';
548 if( ! strchr( Chan->modes, x[0] ))
550 /* Channel does not have this mode yet, set it */
551 strlcat( Chan->modes, x, sizeof( Chan->modes ));
555 } /* Channel_ModeAdd */
559 Channel_ModeDel( CHANNEL *Chan, char Mode )
562 * if the mode was removed return true.
563 * if the channel did not have the mode, return false.
567 assert( Chan != NULL );
569 p = strchr( Chan->modes, Mode );
570 if( ! p ) return false;
572 /* Channel has mode -> delete */
579 } /* Channel_ModeDel */
583 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
585 /* Set Channel-User-Mode.
586 * if mode was newly set, return true.
587 * if the User already had this channel-mode, return false.
593 assert( Chan != NULL );
594 assert( Client != NULL );
596 cl2chan = Get_Cl2Chan( Chan, Client );
597 assert( cl2chan != NULL );
599 x[0] = Mode; x[1] = '\0';
600 if( ! strchr( cl2chan->modes, x[0] ))
602 /* mode not set, -> set it */
603 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
607 } /* Channel_UserModeAdd */
611 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
613 /* Delete Channel-User-Mode.
614 * If Mode was removed, return true.
615 * If User did not have the Channel-Mode, return false.
621 assert( Chan != NULL );
622 assert( Client != NULL );
624 cl2chan = Get_Cl2Chan( Chan, Client );
625 assert( cl2chan != NULL );
627 p = strchr( cl2chan->modes, Mode );
628 if( ! p ) return false;
630 /* Client has Mode -> delete */
637 } /* Channel_UserModeDel */
641 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
643 /* return Users' Channel-Modes */
647 assert( Chan != NULL );
648 assert( Client != NULL );
650 cl2chan = Get_Cl2Chan( Chan, Client );
651 assert( cl2chan != NULL );
653 return cl2chan->modes;
654 } /* Channel_UserModes */
658 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
660 /* Test if Client is on Channel Chan */
662 assert( Chan != NULL );
663 assert( Client != NULL );
664 return Get_Cl2Chan(Chan, Client) != NULL;
665 } /* Channel_IsMemberOf */
669 Channel_Topic( CHANNEL *Chan )
672 assert( Chan != NULL );
673 ret = array_start(&Chan->topic);
674 return ret ? ret : "";
675 } /* Channel_Topic */
681 Channel_TopicTime(CHANNEL *Chan)
683 assert(Chan != NULL);
684 return (unsigned int) Chan->topic_time;
685 } /* Channel_TopicTime */
689 Channel_TopicWho(CHANNEL *Chan)
691 assert(Chan != NULL);
692 return Chan->topic_who;
693 } /* Channel_TopicWho */
699 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
702 assert( Chan != NULL );
703 assert( Topic != NULL );
706 if (len < array_bytes(&Chan->topic))
707 array_free(&Chan->topic);
709 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
710 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
711 Topic, Chan->name, strerror(errno));
713 Chan->topic_time = time(NULL);
714 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
715 strlcpy(Chan->topic_who, Client_ID(Client),
716 sizeof Chan->topic_who);
718 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
719 sizeof Chan->topic_who);
723 } /* Channel_SetTopic */
727 Channel_SetModes( CHANNEL *Chan, char *Modes )
729 assert( Chan != NULL );
730 assert( Modes != NULL );
732 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
733 } /* Channel_SetModes */
737 Channel_SetKey( CHANNEL *Chan, const char *Key )
739 assert( Chan != NULL );
740 assert( Key != NULL );
742 strlcpy( Chan->key, Key, sizeof( Chan->key ));
743 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
744 } /* Channel_SetKey */
748 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
750 assert( Chan != NULL );
752 Chan->maxusers = Count;
753 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
754 } /* Channel_SetMaxUsers */
758 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
760 bool is_member, has_voice, is_op;
762 is_member = has_voice = is_op = false;
764 /* The server itself always can send messages :-) */
765 if (Client_ThisServer() == From)
768 if (Channel_IsMemberOf(Chan, From)) {
770 if (strchr(Channel_UserModes(Chan, From), 'v'))
772 if (strchr(Channel_UserModes(Chan, From), 'o'))
777 * Is the client allowed to write to channel?
779 * If channel mode n set: non-members cannot send to channel.
780 * If channel mode m set: need voice.
782 if (strchr(Channel_Modes(Chan), 'n') && !is_member)
785 if (is_op || has_voice)
788 if (strchr(Channel_Modes(Chan), 'm'))
791 return !Lists_Check(&Chan->list_bans, From);
796 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
797 bool SendErrors, const char *Text)
799 if (!Can_Send_To_Channel(Chan, From)) {
801 return CONNECTED; /* no error, see RFC 2812 */
802 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
803 Client_ID(From), Channel_Name(Chan));
806 if (Client_Conn(From) > NONE)
807 Conn_UpdateIdle(Client_Conn(From));
809 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
810 "%s %s :%s", Command, Channel_Name(Chan), Text);
815 Channel_Create( const char *Name )
817 /* Create new CHANNEL structure and add it to linked list */
820 assert( Name != NULL );
822 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
825 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
828 memset( c, 0, sizeof( CHANNEL ));
829 strlcpy( c->name, Name, sizeof( c->name ));
830 c->hash = Hash( c->name );
831 c->next = My_Channels;
833 LogDebug("Created new channel structure for \"%s\".", Name);
835 } /* Channel_Create */
839 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
843 assert( Chan != NULL );
844 assert( Client != NULL );
846 cl2chan = My_Cl2Chan;
849 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
850 cl2chan = cl2chan->next;
857 Add_Client( CHANNEL *Chan, CLIENT *Client )
861 assert( Chan != NULL );
862 assert( Client != NULL );
864 /* Create new CL2CHAN structure */
865 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
868 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
871 cl2chan->channel = Chan;
872 cl2chan->client = Client;
873 strcpy( cl2chan->modes, "" );
876 cl2chan->next = My_Cl2Chan;
877 My_Cl2Chan = cl2chan;
879 Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
886 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
888 CL2CHAN *cl2chan, *last_cl2chan;
891 assert( Chan != NULL );
892 assert( Client != NULL );
893 assert( Origin != NULL );
894 assert( Reason != NULL );
896 /* Do not inform other servers if the channel is local to this server,
897 * regardless of what the caller requested! */
899 InformServer = !Channel_IsLocal(Chan);
902 cl2chan = My_Cl2Chan;
905 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
906 last_cl2chan = cl2chan;
907 cl2chan = cl2chan->next;
909 if( ! cl2chan ) return false;
911 c = cl2chan->channel;
914 /* maintain cl2chan list */
915 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
916 else My_Cl2Chan = cl2chan->next;
922 /* QUIT: other servers have already been notified,
923 * see Client_Destroy(); so only inform other clients
924 * in same channel. */
925 assert( InformServer == false );
926 LogDebug("User \"%s\" left channel \"%s\" (%s).",
927 Client_Mask( Client ), c->name, Reason );
930 /* User was KICKed: inform other servers (public
931 * channels) and all users in the channel */
933 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
934 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
935 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
936 c->name, Client_ID( Client ), Reason );
937 if ((Client_Conn(Client) > NONE) &&
938 (Client_Type(Client) == CLIENT_USER))
940 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
941 c->name, Client_ID( Client ), Reason);
943 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
944 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
948 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
950 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
953 if ((Client_Conn(Origin) > NONE) &&
954 (Client_Type(Origin) == CLIENT_USER))
956 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
957 LogDebug("User \"%s\" left channel \"%s\" (%s).",
958 Client_Mask(Client), c->name, Reason);
962 /* When channel is empty and is not pre-defined, delete */
963 if( ! strchr( Channel_Modes( Chan ), 'P' ))
965 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
969 } /* Remove_Client */
973 Channel_AddBan(CHANNEL *c, const char *mask )
975 struct list_head *h = Channel_GetListBans(c);
976 return Lists_Add(h, mask, false);
981 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
983 struct list_head *h = Channel_GetListInvites(c);
984 return Lists_Add(h, mask, onlyonce);
989 ShowInvitesBans(struct list_head *head, CLIENT *Client, CHANNEL *Channel, bool invite)
992 char *msg = invite ? RPL_INVITELIST_MSG : RPL_BANLIST_MSG;
995 assert( Client != NULL );
996 assert( Channel != NULL );
998 e = Lists_GetFirst(head);
1000 if( ! IRC_WriteStrClient( Client, msg, Client_ID( Client ),
1001 Channel_Name( Channel ), Lists_GetMask(e) )) return DISCONNECTED;
1002 e = Lists_GetNext(e);
1005 msg_end = invite ? RPL_ENDOFINVITELIST_MSG : RPL_ENDOFBANLIST_MSG;
1006 return IRC_WriteStrClient( Client, msg_end, Client_ID( Client ), Channel_Name( Channel ));
1011 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1013 struct list_head *h;
1015 assert( Channel != NULL );
1017 h = Channel_GetListBans(Channel);
1018 return ShowInvitesBans(h, Client, Channel, false);
1023 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1025 struct list_head *h;
1027 assert( Channel != NULL );
1029 h = Channel_GetListInvites(Channel);
1030 return ShowInvitesBans(h, Client, Channel, true);
1035 * Log a message to the local &SERVER channel, if it exists.
1038 Channel_LogServer(char *msg)
1043 assert(msg != NULL);
1045 sc = Channel_Search("&SERVER");
1049 c = Client_ThisServer();
1050 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1051 } /* Channel_LogServer */
1055 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1057 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1058 } /* Get_First_Cl2Chan */
1062 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1066 assert( Client != NULL || Channel != NULL );
1071 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1072 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1073 cl2chan = cl2chan->next;
1076 } /* Get_Next_Cl2Chan */
1080 * Remove a channel and free all of its data structures.
1083 Delete_Channel(CHANNEL *Chan)
1085 CHANNEL *chan, *last_chan;
1096 assert(chan != NULL);
1100 /* maintain channel list */
1102 last_chan->next = chan->next;
1104 My_Channels = chan->next;
1106 LogDebug("Freed channel structure for \"%s\".", Chan->name);
1108 } /* Delete_Channel */