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 */
101 const struct Conf_Channel *conf_chan;
103 size_t i, channel_count = array_length(&Conf_Channels, sizeof(*conf_chan));
105 conf_chan = array_start(&Conf_Channels);
107 assert(channel_count == 0 || conf_chan != NULL);
109 for (i = 0; i < channel_count; i++, conf_chan++) {
110 if (!conf_chan->name[0] || !Channel_IsValidName(conf_chan->name)) {
111 Log(LOG_ERR, "Can't create pre-defined channel: invalid name: \"%s\"",
116 new_chan = Channel_Search(conf_chan->name);
118 Log(LOG_INFO, "Can't create pre-defined channel \"%s\": name already in use.",
123 new_chan = Channel_Create(conf_chan->name);
125 Log(LOG_ERR, "Can't create pre-defined channel \"%s\"",
130 Channel_ModeAdd(new_chan, 'P');
132 if (conf_chan->topic[0])
133 Channel_SetTopic(new_chan, NULL, conf_chan->topic);
135 c = conf_chan->modes;
137 Channel_ModeAdd(new_chan, *c++);
139 Channel_SetKey(new_chan, conf_chan->key);
140 Channel_SetMaxUsers(new_chan, conf_chan->maxusers);
141 Log(LOG_INFO, "Created pre-defined channel \"%s\"",
145 array_free(&Conf_Channels);
146 } /* Channel_InitPredefined */
153 CL2CHAN *cl2chan, *cl2chan_next;
155 /* free struct Channel */
160 array_free(&c->topic);
165 /* Free Channel allocation table */
166 cl2chan = My_Cl2Chan;
169 cl2chan_next = cl2chan->next;
171 cl2chan = cl2chan_next;
178 * This function lets a client join a channel. First, the function
179 * checks that the specified channel name is valid and that the client
180 * isn't already a member. If the specified channel doesn't exist,
181 * a new channel is created. Client is added to channel by function
185 Channel_Join( CLIENT *Client, char *Name )
189 assert(Client != NULL);
190 assert(Name != NULL);
192 /* Check that the channel name is valid */
193 if (! Channel_IsValidName(Name)) {
194 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
195 Client_ID(Client), Name);
199 chan = Channel_Search(Name);
201 /* Check if the client is already in the channel */
202 if (Get_Cl2Chan(chan, Client))
205 /* If the specified channel does not exist, the channel
207 chan = Channel_Create(Name);
212 /* Add user to Channel */
213 if (! Add_Client(chan, Client))
221 * Part client from channel.
222 * This function lets a client part from a channel. First, the function checks
223 * if the channel exists and the client is a member of it and sends out
224 * appropriate error messages if not. The real work is done by the function
228 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
232 assert(Client != NULL);
233 assert(Name != NULL);
234 assert(Reason != NULL);
236 /* Check that specified channel exists */
237 chan = Channel_Search(Name);
239 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
240 Client_ID(Client), Name);
244 /* Check that the client is in the channel */
245 if (!Get_Cl2Chan(chan, Client)) {
246 IRC_WriteStrClient(Client, ERR_NOTONCHANNEL_MSG,
247 Client_ID(Client), Name);
251 /* Part client from channel */
252 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
260 * Kick user from Channel
263 Channel_Kick(CLIENT *Peer, CLIENT *Target, CLIENT *Origin, const char *Name,
268 assert(Peer != NULL);
269 assert(Target != NULL);
270 assert(Origin != NULL);
271 assert(Name != NULL);
272 assert(Reason != NULL);
274 /* Check that channel exists */
275 chan = Channel_Search( Name );
278 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
282 if (Client_Type(Peer) != CLIENT_SERVER &&
283 Client_Type(Origin) != CLIENT_SERVICE) {
284 /* Check that user is on the specified channel */
285 if (!Channel_IsMemberOf(chan, Origin)) {
286 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG,
287 Client_ID(Origin), Name);
291 /* Check if user has operator status */
292 if (!strchr(Channel_UserModes(chan, Origin), 'o')) {
293 IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG,
294 Client_ID(Origin), Name);
299 /* Check that the client to be kicked is on the specified channel */
300 if (!Channel_IsMemberOf(chan, Target)) {
301 IRC_WriteStrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
302 Client_ID(Origin), Client_ID(Target), Name );
306 /* Kick Client from channel */
307 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
312 Channel_Quit( CLIENT *Client, char *Reason )
316 assert( Client != NULL );
317 assert( Reason != NULL );
319 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
325 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
332 Channel_Count( void )
335 unsigned long count = 0;
344 } /* Channel_Count */
348 Channel_MemberCount( CHANNEL *Chan )
351 unsigned long count = 0;
353 assert( Chan != NULL );
355 cl2chan = My_Cl2Chan;
358 if( cl2chan->channel == Chan ) count++;
359 cl2chan = cl2chan->next;
362 } /* Channel_MemberCount */
366 Channel_CountForUser( CLIENT *Client )
368 /* Count number of channels a user is member of. */
373 assert( Client != NULL );
375 cl2chan = My_Cl2Chan;
378 if( cl2chan->client == Client ) count++;
379 cl2chan = cl2chan->next;
383 } /* Channel_CountForUser */
387 Channel_Name( const CHANNEL *Chan )
389 assert( Chan != NULL );
395 Channel_Modes( CHANNEL *Chan )
397 assert( Chan != NULL );
399 } /* Channel_Modes */
403 Channel_Key( CHANNEL *Chan )
405 assert( Chan != NULL );
411 Channel_MaxUsers( CHANNEL *Chan )
413 assert( Chan != NULL );
414 return Chan->maxusers;
415 } /* Channel_MaxUsers */
419 Channel_First( void )
422 } /* Channel_First */
426 Channel_Next( CHANNEL *Chan )
428 assert( Chan != NULL );
434 Channel_Search( const char *Name )
436 /* Search channel structure */
441 assert( Name != NULL );
443 search_hash = Hash( Name );
447 if( search_hash == c->hash )
450 if( strcasecmp( Name, c->name ) == 0 ) return c;
455 } /* Channel_Search */
459 Channel_FirstMember( CHANNEL *Chan )
461 assert( Chan != NULL );
462 return Get_First_Cl2Chan( NULL, Chan );
463 } /* Channel_FirstMember */
467 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
469 assert( Chan != NULL );
470 assert( Cl2Chan != NULL );
471 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
472 } /* Channel_NextMember */
476 Channel_FirstChannelOf( CLIENT *Client )
478 assert( Client != NULL );
479 return Get_First_Cl2Chan( Client, NULL );
480 } /* Channel_FirstChannelOf */
484 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
486 assert( Client != NULL );
487 assert( Cl2Chan != NULL );
488 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
489 } /* Channel_NextChannelOf */
493 Channel_GetClient( CL2CHAN *Cl2Chan )
495 assert( Cl2Chan != NULL );
496 return Cl2Chan->client;
497 } /* Channel_GetClient */
501 Channel_GetChannel( CL2CHAN *Cl2Chan )
503 assert( Cl2Chan != NULL );
504 return Cl2Chan->channel;
505 } /* Channel_GetChannel */
509 Channel_IsValidName( const char *Name )
511 assert( Name != NULL );
514 if (strlen(Name) <= 1)
517 if (strchr("#&+", Name[0]) == NULL)
519 if (strlen(Name) >= CHANNEL_NAME_LEN)
522 return Name[strcspn(Name, " ,:\007")] == 0;
523 } /* Channel_IsValidName */
527 Channel_ModeAdd( CHANNEL *Chan, char Mode )
530 * If the channel already had this mode, return false.
531 * If the channel mode was newly set return true.
536 assert( Chan != NULL );
538 x[0] = Mode; x[1] = '\0';
539 if( ! strchr( Chan->modes, x[0] ))
541 /* Channel does not have this mode yet, set it */
542 strlcat( Chan->modes, x, sizeof( Chan->modes ));
546 } /* Channel_ModeAdd */
550 Channel_ModeDel( CHANNEL *Chan, char Mode )
553 * if the mode was removed return true.
554 * if the channel did not have the mode, return false.
558 assert( Chan != NULL );
560 p = strchr( Chan->modes, Mode );
561 if( ! p ) return false;
563 /* Channel has mode -> delete */
570 } /* Channel_ModeDel */
574 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
576 /* Set Channel-User-Mode.
577 * if mode was newly set, return true.
578 * if the User already had this channel-mode, return false.
584 assert( Chan != NULL );
585 assert( Client != NULL );
587 cl2chan = Get_Cl2Chan( Chan, Client );
588 assert( cl2chan != NULL );
590 x[0] = Mode; x[1] = '\0';
591 if( ! strchr( cl2chan->modes, x[0] ))
593 /* mode not set, -> set it */
594 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
598 } /* Channel_UserModeAdd */
602 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
604 /* Delete Channel-User-Mode.
605 * If Mode was removed, return true.
606 * If User did not have the Channel-Mode, return false.
612 assert( Chan != NULL );
613 assert( Client != NULL );
615 cl2chan = Get_Cl2Chan( Chan, Client );
616 assert( cl2chan != NULL );
618 p = strchr( cl2chan->modes, Mode );
619 if( ! p ) return false;
621 /* Client has Mode -> delete */
628 } /* Channel_UserModeDel */
632 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
634 /* return Users' Channel-Modes */
638 assert( Chan != NULL );
639 assert( Client != NULL );
641 cl2chan = Get_Cl2Chan( Chan, Client );
642 assert( cl2chan != NULL );
644 return cl2chan->modes;
645 } /* Channel_UserModes */
649 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
651 /* Test if Client is on Channel Chan */
653 assert( Chan != NULL );
654 assert( Client != NULL );
655 return Get_Cl2Chan(Chan, Client) != NULL;
656 } /* Channel_IsMemberOf */
660 Channel_Topic( CHANNEL *Chan )
663 assert( Chan != NULL );
664 ret = array_start(&Chan->topic);
665 return ret ? ret : "";
666 } /* Channel_Topic */
672 Channel_TopicTime(CHANNEL *Chan)
674 assert(Chan != NULL);
675 return (unsigned int) Chan->topic_time;
676 } /* Channel_TopicTime */
680 Channel_TopicWho(CHANNEL *Chan)
682 assert(Chan != NULL);
683 return Chan->topic_who;
684 } /* Channel_TopicWho */
690 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
693 assert( Chan != NULL );
694 assert( Topic != NULL );
697 if (len < array_bytes(&Chan->topic))
698 array_free(&Chan->topic);
700 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
701 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
702 Topic, Chan->name, strerror(errno));
704 Chan->topic_time = time(NULL);
705 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
706 strlcpy(Chan->topic_who, Client_ID(Client),
707 sizeof Chan->topic_who);
709 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
710 sizeof Chan->topic_who);
714 } /* Channel_SetTopic */
718 Channel_SetModes( CHANNEL *Chan, char *Modes )
720 assert( Chan != NULL );
721 assert( Modes != NULL );
723 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
724 } /* Channel_SetModes */
728 Channel_SetKey( CHANNEL *Chan, const char *Key )
730 assert( Chan != NULL );
731 assert( Key != NULL );
733 strlcpy( Chan->key, Key, sizeof( Chan->key ));
734 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
735 } /* Channel_SetKey */
739 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
741 assert( Chan != NULL );
743 Chan->maxusers = Count;
744 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
745 } /* Channel_SetMaxUsers */
749 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
751 bool is_member, has_voice, is_op;
753 is_member = has_voice = is_op = false;
755 /* The server itself always can send messages :-) */
756 if (Client_ThisServer() == From)
759 if (Channel_IsMemberOf(Chan, From)) {
761 if (strchr(Channel_UserModes(Chan, From), 'v'))
763 if (strchr(Channel_UserModes(Chan, From), 'o'))
768 * Is the client allowed to write to channel?
770 * If channel mode n set: non-members cannot send to channel.
771 * If channel mode m set: need voice.
773 if (strchr(Channel_Modes(Chan), 'n') && !is_member)
776 if (is_op || has_voice)
779 if (strchr(Channel_Modes(Chan), 'm'))
782 return !Lists_Check(&Chan->list_bans, From);
787 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
788 bool SendErrors, const char *Text)
790 if (!Can_Send_To_Channel(Chan, From)) {
792 return CONNECTED; /* no error, see RFC 2812 */
793 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
794 Client_ID(From), Channel_Name(Chan));
797 if (Client_Conn(From) > NONE)
798 Conn_UpdateIdle(Client_Conn(From));
800 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
801 "%s %s :%s", Command, Channel_Name(Chan), Text);
806 Channel_Create( const char *Name )
808 /* Create new CHANNEL structure and add it to linked list */
811 assert( Name != NULL );
813 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
816 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
819 memset( c, 0, sizeof( CHANNEL ));
820 strlcpy( c->name, Name, sizeof( c->name ));
821 c->hash = Hash( c->name );
822 c->next = My_Channels;
824 LogDebug("Created new channel structure for \"%s\".", Name);
826 } /* Channel_Create */
830 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
834 assert( Chan != NULL );
835 assert( Client != NULL );
837 cl2chan = My_Cl2Chan;
840 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
841 cl2chan = cl2chan->next;
848 Add_Client( CHANNEL *Chan, CLIENT *Client )
852 assert( Chan != NULL );
853 assert( Client != NULL );
855 /* Create new CL2CHAN structure */
856 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
859 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
862 cl2chan->channel = Chan;
863 cl2chan->client = Client;
864 strcpy( cl2chan->modes, "" );
867 cl2chan->next = My_Cl2Chan;
868 My_Cl2Chan = cl2chan;
870 Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
877 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
879 CL2CHAN *cl2chan, *last_cl2chan;
882 assert( Chan != NULL );
883 assert( Client != NULL );
884 assert( Origin != NULL );
885 assert( Reason != NULL );
887 /* Do not inform other servers if the channel is local to this server,
888 * regardless of what the caller requested! */
890 InformServer = !Channel_IsLocal(Chan);
893 cl2chan = My_Cl2Chan;
896 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
897 last_cl2chan = cl2chan;
898 cl2chan = cl2chan->next;
900 if( ! cl2chan ) return false;
902 c = cl2chan->channel;
905 /* maintain cl2chan list */
906 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
907 else My_Cl2Chan = cl2chan->next;
913 /* QUIT: other servers have already been notified,
914 * see Client_Destroy(); so only inform other clients
915 * in same channel. */
916 assert( InformServer == false );
917 LogDebug("User \"%s\" left channel \"%s\" (%s).",
918 Client_Mask( Client ), c->name, Reason );
921 /* User was KICKed: inform other servers (public
922 * channels) and all users in the channel */
924 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
925 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
926 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
927 c->name, Client_ID( Client ), Reason );
928 if ((Client_Conn(Client) > NONE) &&
929 (Client_Type(Client) == CLIENT_USER))
931 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
932 c->name, Client_ID( Client ), Reason);
934 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
935 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
939 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
941 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
944 if ((Client_Conn(Origin) > NONE) &&
945 (Client_Type(Origin) == CLIENT_USER))
947 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
948 LogDebug("User \"%s\" left channel \"%s\" (%s).",
949 Client_Mask(Client), c->name, Reason);
953 /* When channel is empty and is not pre-defined, delete */
954 if( ! strchr( Channel_Modes( Chan ), 'P' ))
956 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
960 } /* Remove_Client */
964 Channel_AddBan(CHANNEL *c, const char *mask )
966 struct list_head *h = Channel_GetListBans(c);
967 return Lists_Add(h, mask, false);
972 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
974 struct list_head *h = Channel_GetListInvites(c);
975 return Lists_Add(h, mask, onlyonce);
980 ShowInvitesBans(struct list_head *head, CLIENT *Client, CHANNEL *Channel, bool invite)
983 char *msg = invite ? RPL_INVITELIST_MSG : RPL_BANLIST_MSG;
986 assert( Client != NULL );
987 assert( Channel != NULL );
989 e = Lists_GetFirst(head);
991 if( ! IRC_WriteStrClient( Client, msg, Client_ID( Client ),
992 Channel_Name( Channel ), Lists_GetMask(e) )) return DISCONNECTED;
993 e = Lists_GetNext(e);
996 msg_end = invite ? RPL_ENDOFINVITELIST_MSG : RPL_ENDOFBANLIST_MSG;
997 return IRC_WriteStrClient( Client, msg_end, Client_ID( Client ), Channel_Name( Channel ));
1002 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1004 struct list_head *h;
1006 assert( Channel != NULL );
1008 h = Channel_GetListBans(Channel);
1009 return ShowInvitesBans(h, Client, Channel, false);
1014 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1016 struct list_head *h;
1018 assert( Channel != NULL );
1020 h = Channel_GetListInvites(Channel);
1021 return ShowInvitesBans(h, Client, Channel, true);
1026 * Log a message to the local &SERVER channel, if it exists.
1029 Channel_LogServer(char *msg)
1034 assert(msg != NULL);
1036 sc = Channel_Search("&SERVER");
1040 c = Client_ThisServer();
1041 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1042 } /* Channel_LogServer */
1046 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1048 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1049 } /* Get_First_Cl2Chan */
1053 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1057 assert( Client != NULL || Channel != NULL );
1062 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1063 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1064 cl2chan = cl2chan->next;
1067 } /* Get_Next_Cl2Chan */
1071 Delete_Channel( CHANNEL *Chan )
1073 /* delete channel structure */
1075 CHANNEL *chan, *last_chan;
1081 if( chan == Chan ) break;
1085 if( ! chan ) return false;
1087 Log( LOG_DEBUG, "Freed channel structure for \"%s\".", Chan->name );
1089 /* free invite and ban lists */
1090 Lists_Free( &chan->list_bans );
1091 Lists_Free( &chan->list_invites );
1093 /* maintain channel list */
1094 if( last_chan ) last_chan->next = chan->next;
1095 else My_Channels = chan->next;
1099 } /* Delete_Channel */