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 GLOBAL struct list_head *
72 Channel_GetListBans(CHANNEL *c)
79 GLOBAL struct list_head *
80 Channel_GetListInvites(CHANNEL *c)
83 return &c->list_invites;
88 Channel_InitPredefined( void )
90 /* Generate predefined persistent channels */
96 for( i = 0; i < Conf_Channel_Count; i++ )
98 /* Check for Name configuration */
99 if( ! Conf_Channel[i].name[0] ) continue;
101 /* Check for invalid channel name */
102 if( ! Channel_IsValidName( Conf_Channel[i].name ))
104 Log( LOG_ERR, "Can't create pre-defined channel: invalid name: \"%s\"!", Conf_Channel[i].name );
105 array_free(&Conf_Channel[i].topic);
109 /* Check if the channel name is already in use */
110 chan = Channel_Search( Conf_Channel[i].name );
113 Log( LOG_INFO, "Can't create pre-defined channel \"%s\": name already in use.", Conf_Channel[i].name );
114 array_free(&Conf_Channel[i].topic);
119 chan = Channel_Create(Conf_Channel[i].name);
121 Channel_ModeAdd(chan, 'P');
123 if (array_start(&Conf_Channel[i].topic) != NULL)
124 Channel_SetTopic(chan, NULL,
125 array_start(&Conf_Channel[i].topic));
126 array_free(&Conf_Channel[i].topic);
128 c = Conf_Channel[i].modes;
130 Channel_ModeAdd(chan, *c++);
132 Channel_SetKey(chan, Conf_Channel[i].key);
133 Channel_SetMaxUsers(chan, Conf_Channel[i].maxusers);
135 Log(LOG_INFO, "Created pre-defined channel \"%s\".",
136 Conf_Channel[i].name );
138 else Log(LOG_ERR, "Can't create pre-defined channel \"%s\"!",
139 Conf_Channel[i].name );
141 } /* Channel_InitPredefined */
148 CL2CHAN *cl2chan, *cl2chan_next;
150 /* free struct Channel */
155 array_free(&c->topic);
160 /* Free Channel allocation table */
161 cl2chan = My_Cl2Chan;
164 cl2chan_next = cl2chan->next;
166 cl2chan = cl2chan_next;
173 * This function lets a client join a channel. First, the function
174 * checks that the specified channel name is valid and that the client
175 * isn't already a member. If the specified channel doesn't exist,
176 * a new channel is created. Client is added to channel by function
180 Channel_Join( CLIENT *Client, char *Name )
184 assert(Client != NULL);
185 assert(Name != NULL);
187 /* Check that the channel name is valid */
188 if (! Channel_IsValidName(Name)) {
189 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
190 Client_ID(Client), Name);
194 chan = Channel_Search(Name);
196 /* Check if the client is already in the channel */
197 if (Get_Cl2Chan(chan, Client))
200 /* If the specified channel does not exist, the channel
202 chan = Channel_Create(Name);
207 /* Add user to Channel */
208 if (! Add_Client(chan, Client))
216 * Part client from channel.
217 * This function lets a client part from a channel. First, the function checks
218 * if the channel exists and the client is a member of it and sends out
219 * appropriate error messages if not. The real work is done by the function
223 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
227 assert(Client != NULL);
228 assert(Name != NULL);
229 assert(Reason != NULL);
231 /* Check that specified channel exists */
232 chan = Channel_Search(Name);
234 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
235 Client_ID(Client), Name);
239 /* Check that the client is in the channel */
240 if (!Get_Cl2Chan(chan, Client)) {
241 IRC_WriteStrClient(Client, ERR_NOTONCHANNEL_MSG,
242 Client_ID(Client), Name);
246 /* Part client from channel */
247 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
254 /* Kick user from Channel */
256 Channel_Kick( CLIENT *Client, CLIENT *Origin, const char *Name, const char *Reason )
260 assert( Client != NULL );
261 assert( Origin != NULL );
262 assert( Name != NULL );
263 assert( Reason != NULL );
265 /* Check that channel exists */
266 chan = Channel_Search( Name );
269 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
273 /* Check that user is on the specified channel */
274 if( ! Channel_IsMemberOf( chan, Origin ))
276 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Name );
280 /* Check if user has operator status */
281 if( ! strchr( Channel_UserModes( chan, Origin ), 'o' ))
283 IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Name);
287 /* Check that the client to be kicked is on the specified channel */
288 if( ! Channel_IsMemberOf( chan, Client ))
290 IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( Client ), Name );
294 /* Kick Client from channel */
295 Remove_Client( REMOVE_KICK, chan, Client, Origin, Reason, true);
300 Channel_Quit( CLIENT *Client, char *Reason )
304 assert( Client != NULL );
305 assert( Reason != NULL );
307 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
313 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
320 Channel_Count( void )
323 unsigned long count = 0;
332 } /* Channel_Count */
336 Channel_MemberCount( CHANNEL *Chan )
339 unsigned long count = 0;
341 assert( Chan != NULL );
343 cl2chan = My_Cl2Chan;
346 if( cl2chan->channel == Chan ) count++;
347 cl2chan = cl2chan->next;
350 } /* Channel_MemberCount */
354 Channel_CountForUser( CLIENT *Client )
356 /* Count number of channels a user is member of. */
361 assert( Client != NULL );
363 cl2chan = My_Cl2Chan;
366 if( cl2chan->client == Client ) count++;
367 cl2chan = cl2chan->next;
371 } /* Channel_CountForUser */
375 Channel_Name( const CHANNEL *Chan )
377 assert( Chan != NULL );
383 Channel_Modes( CHANNEL *Chan )
385 assert( Chan != NULL );
387 } /* Channel_Modes */
391 Channel_Key( CHANNEL *Chan )
393 assert( Chan != NULL );
399 Channel_MaxUsers( CHANNEL *Chan )
401 assert( Chan != NULL );
402 return Chan->maxusers;
403 } /* Channel_MaxUsers */
407 Channel_First( void )
410 } /* Channel_First */
414 Channel_Next( CHANNEL *Chan )
416 assert( Chan != NULL );
422 Channel_Search( const char *Name )
424 /* Search channel structure */
429 assert( Name != NULL );
431 search_hash = Hash( Name );
435 if( search_hash == c->hash )
438 if( strcasecmp( Name, c->name ) == 0 ) return c;
443 } /* Channel_Search */
447 Channel_FirstMember( CHANNEL *Chan )
449 assert( Chan != NULL );
450 return Get_First_Cl2Chan( NULL, Chan );
451 } /* Channel_FirstMember */
455 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
457 assert( Chan != NULL );
458 assert( Cl2Chan != NULL );
459 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
460 } /* Channel_NextMember */
464 Channel_FirstChannelOf( CLIENT *Client )
466 assert( Client != NULL );
467 return Get_First_Cl2Chan( Client, NULL );
468 } /* Channel_FirstChannelOf */
472 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
474 assert( Client != NULL );
475 assert( Cl2Chan != NULL );
476 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
477 } /* Channel_NextChannelOf */
481 Channel_GetClient( CL2CHAN *Cl2Chan )
483 assert( Cl2Chan != NULL );
484 return Cl2Chan->client;
485 } /* Channel_GetClient */
489 Channel_GetChannel( CL2CHAN *Cl2Chan )
491 assert( Cl2Chan != NULL );
492 return Cl2Chan->channel;
493 } /* Channel_GetChannel */
497 Channel_IsValidName( const char *Name )
499 assert( Name != NULL );
502 if (strlen(Name) <= 1)
505 if (strchr("+#", Name[0]) == NULL)
507 if (strlen(Name) >= CHANNEL_NAME_LEN)
510 return Name[strcspn(Name, " ,:\007")] == 0;
511 } /* Channel_IsValidName */
515 Channel_ModeAdd( CHANNEL *Chan, char Mode )
518 * If the channel already had this mode, return false.
519 * If the channel mode was newly set return true.
524 assert( Chan != NULL );
526 x[0] = Mode; x[1] = '\0';
527 if( ! strchr( Chan->modes, x[0] ))
529 /* Channel does not have this mode yet, set it */
530 strlcat( Chan->modes, x, sizeof( Chan->modes ));
534 } /* Channel_ModeAdd */
538 Channel_ModeDel( CHANNEL *Chan, char Mode )
541 * if the mode was removed return true.
542 * if the channel did not have the mode, return false.
546 assert( Chan != NULL );
548 p = strchr( Chan->modes, Mode );
549 if( ! p ) return false;
551 /* Channel has mode -> delete */
558 } /* Channel_ModeDel */
562 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
564 /* Set Channel-User-Mode.
565 * if mode was newly set, return true.
566 * if the User already had this channel-mode, return false.
572 assert( Chan != NULL );
573 assert( Client != NULL );
575 cl2chan = Get_Cl2Chan( Chan, Client );
576 assert( cl2chan != NULL );
578 x[0] = Mode; x[1] = '\0';
579 if( ! strchr( cl2chan->modes, x[0] ))
581 /* mode not set, -> set it */
582 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
586 } /* Channel_UserModeAdd */
590 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
592 /* Delete Channel-User-Mode.
593 * If Mode was removed, return true.
594 * If User did not have the Channel-Mode, return false.
600 assert( Chan != NULL );
601 assert( Client != NULL );
603 cl2chan = Get_Cl2Chan( Chan, Client );
604 assert( cl2chan != NULL );
606 p = strchr( cl2chan->modes, Mode );
607 if( ! p ) return false;
609 /* Client has Mode -> delete */
616 } /* Channel_UserModeDel */
620 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
622 /* return Users' Channel-Modes */
626 assert( Chan != NULL );
627 assert( Client != NULL );
629 cl2chan = Get_Cl2Chan( Chan, Client );
630 assert( cl2chan != NULL );
632 return cl2chan->modes;
633 } /* Channel_UserModes */
637 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
639 /* Test if Client is on Channel Chan */
641 assert( Chan != NULL );
642 assert( Client != NULL );
643 return Get_Cl2Chan(Chan, Client) != NULL;
644 } /* Channel_IsMemberOf */
648 Channel_Topic( CHANNEL *Chan )
651 assert( Chan != NULL );
652 ret = array_start(&Chan->topic);
653 return ret ? ret : "";
654 } /* Channel_Topic */
660 Channel_TopicTime(CHANNEL *Chan)
662 assert(Chan != NULL);
663 return (unsigned int) Chan->topic_time;
664 } /* Channel_TopicTime */
668 Channel_TopicWho(CHANNEL *Chan)
670 assert(Chan != NULL);
671 return Chan->topic_who;
672 } /* Channel_TopicWho */
678 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, char *Topic)
681 assert( Chan != NULL );
682 assert( Topic != NULL );
685 if (len < array_bytes(&Chan->topic))
686 array_free(&Chan->topic);
688 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
689 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
690 Topic, Chan->name, strerror(errno));
692 Chan->topic_time = time(NULL);
693 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
694 strlcpy(Chan->topic_who, Client_ID(Client),
695 sizeof Chan->topic_who);
697 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
698 sizeof Chan->topic_who);
702 } /* Channel_SetTopic */
706 Channel_SetModes( CHANNEL *Chan, char *Modes )
708 assert( Chan != NULL );
709 assert( Modes != NULL );
711 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
712 } /* Channel_SetModes */
716 Channel_SetKey( CHANNEL *Chan, char *Key )
718 assert( Chan != NULL );
719 assert( Key != NULL );
721 strlcpy( Chan->key, Key, sizeof( Chan->key ));
722 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
723 } /* Channel_SetKey */
727 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
729 assert( Chan != NULL );
731 Chan->maxusers = Count;
732 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
733 } /* Channel_SetMaxUsers */
737 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
739 bool is_member, has_voice, is_op;
741 is_member = has_voice = is_op = false;
743 if (Channel_IsMemberOf(Chan, From)) {
745 if (strchr(Channel_UserModes(Chan, From), 'v'))
747 if (strchr(Channel_UserModes(Chan, From), 'o'))
752 * Is the client allowed to write to channel?
754 * If channel mode n set: non-members cannot send to channel.
755 * If channel mode m set: need voice.
757 if (strchr(Channel_Modes(Chan), 'n') && !is_member)
760 if (is_op || has_voice)
763 if (strchr(Channel_Modes(Chan), 'm'))
766 return !Lists_Check(&Chan->list_bans, From);
771 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
772 bool SendErrors, const char *Text)
774 if (!Can_Send_To_Channel(Chan, From)) {
776 return CONNECTED; /* no error, see RFC 2812 */
777 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
778 Client_ID(From), Channel_Name(Chan));
781 if (Client_Conn(From) > NONE)
782 Conn_UpdateIdle(Client_Conn(From));
784 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
785 "%s %s :%s", Command, Channel_Name(Chan), Text);
790 Channel_Create( char *Name )
792 /* Create new CHANNEL structure and add it to linked list */
795 assert( Name != NULL );
797 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
800 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
803 memset( c, 0, sizeof( CHANNEL ));
804 strlcpy( c->name, Name, sizeof( c->name ));
805 c->hash = Hash( c->name );
806 c->next = My_Channels;
808 LogDebug("Created new channel structure for \"%s\".", Name);
810 } /* Channel_Create */
814 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
818 assert( Chan != NULL );
819 assert( Client != NULL );
821 cl2chan = My_Cl2Chan;
824 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
825 cl2chan = cl2chan->next;
832 Add_Client( CHANNEL *Chan, CLIENT *Client )
836 assert( Chan != NULL );
837 assert( Client != NULL );
839 /* Create new CL2CHAN structure */
840 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
843 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
846 cl2chan->channel = Chan;
847 cl2chan->client = Client;
848 strcpy( cl2chan->modes, "" );
851 cl2chan->next = My_Cl2Chan;
852 My_Cl2Chan = cl2chan;
854 Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
861 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
863 CL2CHAN *cl2chan, *last_cl2chan;
866 assert( Chan != NULL );
867 assert( Client != NULL );
868 assert( Origin != NULL );
869 assert( Reason != NULL );
872 cl2chan = My_Cl2Chan;
875 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
876 last_cl2chan = cl2chan;
877 cl2chan = cl2chan->next;
879 if( ! cl2chan ) return false;
881 c = cl2chan->channel;
884 /* maintain cl2chan list */
885 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
886 else My_Cl2Chan = cl2chan->next;
892 /* QUIT: other servers have already been notified, see Client_Destroy();
893 * so only inform other clients in same channel. */
894 assert( InformServer == false );
895 LogDebug("User \"%s\" left channel \"%s\" (%s).",
896 Client_Mask( Client ), c->name, Reason );
899 /* User was KICKed: inform other servers and all users in channel */
901 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
902 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
903 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
904 c->name, Client_ID( Client ), Reason );
905 if ((Client_Conn(Client) > NONE) &&
906 (Client_Type(Client) == CLIENT_USER))
908 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
909 c->name, Client_ID( Client ), Reason);
911 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
912 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
916 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
918 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
921 if ((Client_Conn(Origin) > NONE) &&
922 (Client_Type(Origin) == CLIENT_USER))
924 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
925 LogDebug("User \"%s\" left channel \"%s\" (%s).",
926 Client_Mask(Client), c->name, Reason);
930 /* When channel is empty and is not pre-defined, delete */
931 if( ! strchr( Channel_Modes( Chan ), 'P' ))
933 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
937 } /* Remove_Client */
941 Channel_AddBan(CHANNEL *c, const char *mask )
943 struct list_head *h = Channel_GetListBans(c);
944 return Lists_Add(h, mask, false);
949 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
951 struct list_head *h = Channel_GetListInvites(c);
952 return Lists_Add(h, mask, onlyonce);
957 ShowInvitesBans(struct list_head *head, CLIENT *Client, CHANNEL *Channel, bool invite)
960 char *msg = invite ? RPL_INVITELIST_MSG : RPL_BANLIST_MSG;
963 assert( Client != NULL );
964 assert( Channel != NULL );
966 e = Lists_GetFirst(head);
968 if( ! IRC_WriteStrClient( Client, msg, Client_ID( Client ),
969 Channel_Name( Channel ), Lists_GetMask(e) )) return DISCONNECTED;
970 e = Lists_GetNext(e);
973 msg_end = invite ? RPL_ENDOFINVITELIST_MSG : RPL_ENDOFBANLIST_MSG;
974 return IRC_WriteStrClient( Client, msg_end, Client_ID( Client ), Channel_Name( Channel ));
979 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
983 assert( Channel != NULL );
985 h = Channel_GetListBans(Channel);
986 return ShowInvitesBans(h, Client, Channel, false);
991 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
995 assert( Channel != NULL );
997 h = Channel_GetListInvites(Channel);
998 return ShowInvitesBans(h, Client, Channel, true);
1003 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1005 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1006 } /* Get_First_Cl2Chan */
1010 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1014 assert( Client != NULL || Channel != NULL );
1019 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1020 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1021 cl2chan = cl2chan->next;
1024 } /* Get_Next_Cl2Chan */
1028 Delete_Channel( CHANNEL *Chan )
1030 /* delete channel structure */
1032 CHANNEL *chan, *last_chan;
1038 if( chan == Chan ) break;
1042 if( ! chan ) return false;
1044 Log( LOG_DEBUG, "Freed channel structure for \"%s\".", Chan->name );
1046 /* free invite and ban lists */
1047 Lists_Free( &chan->list_bans );
1048 Lists_Free( &chan->list_invites );
1050 /* maintain channel list */
1051 if( last_chan ) last_chan->next = chan->next;
1052 else My_Channels = chan->next;
1056 } /* Delete_Channel */