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, Client_ID( Client ), Name );
195 chan = Channel_Search( Name );
197 /* Check if the client is already in the channel */
198 if( Get_Cl2Chan( chan, Client )) return false;
202 /* If the specified channel doesn't exist, the channel is created */
203 chan = Channel_Create( Name );
204 if (!chan) return false;
207 /* Add user to Channel */
208 if( ! Add_Client( chan, Client )) return false;
214 * Part client from channel.
215 * This function lets a client part from a channel. First, the function checks
216 * if the channel exists and the client is a member of it and sends out
217 * appropriate error messages if not. The real work is done by the function
221 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
225 assert(Client != NULL);
226 assert(Name != NULL);
227 assert(Reason != NULL);
229 /* Check that specified channel exists */
230 chan = Channel_Search(Name);
232 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
233 Client_ID(Client), Name);
237 /* Check that the client is in the channel */
238 if (!Get_Cl2Chan(chan, Client)) {
239 IRC_WriteStrClient(Client, ERR_NOTONCHANNEL_MSG,
240 Client_ID(Client), Name);
244 /* Part client from channel */
245 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
252 /* Kick user from Channel */
254 Channel_Kick( CLIENT *Client, CLIENT *Origin, const char *Name, const char *Reason )
258 assert( Client != NULL );
259 assert( Origin != NULL );
260 assert( Name != NULL );
261 assert( Reason != NULL );
263 /* Check that channel exists */
264 chan = Channel_Search( Name );
267 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
271 /* Check that user is on the specified channel */
272 if( ! Channel_IsMemberOf( chan, Origin ))
274 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Name );
278 /* Check if user has operator status */
279 if( ! strchr( Channel_UserModes( chan, Origin ), 'o' ))
281 IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Name);
285 /* Check that the client to be kicked is on the specified channel */
286 if( ! Channel_IsMemberOf( chan, Client ))
288 IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( Client ), Name );
292 /* Kick Client from channel */
293 Remove_Client( REMOVE_KICK, chan, Client, Origin, Reason, true);
298 Channel_Quit( CLIENT *Client, char *Reason )
302 assert( Client != NULL );
303 assert( Reason != NULL );
305 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
311 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
318 Channel_Count( void )
321 unsigned long count = 0;
330 } /* Channel_Count */
334 Channel_MemberCount( CHANNEL *Chan )
337 unsigned long count = 0;
339 assert( Chan != NULL );
341 cl2chan = My_Cl2Chan;
344 if( cl2chan->channel == Chan ) count++;
345 cl2chan = cl2chan->next;
348 } /* Channel_MemberCount */
352 Channel_CountForUser( CLIENT *Client )
354 /* Count number of channels a user is member of. */
359 assert( Client != NULL );
361 cl2chan = My_Cl2Chan;
364 if( cl2chan->client == Client ) count++;
365 cl2chan = cl2chan->next;
369 } /* Channel_CountForUser */
373 Channel_Name( const CHANNEL *Chan )
375 assert( Chan != NULL );
381 Channel_Modes( CHANNEL *Chan )
383 assert( Chan != NULL );
385 } /* Channel_Modes */
389 Channel_Key( CHANNEL *Chan )
391 assert( Chan != NULL );
397 Channel_MaxUsers( CHANNEL *Chan )
399 assert( Chan != NULL );
400 return Chan->maxusers;
401 } /* Channel_MaxUsers */
405 Channel_First( void )
408 } /* Channel_First */
412 Channel_Next( CHANNEL *Chan )
414 assert( Chan != NULL );
420 Channel_Search( const char *Name )
422 /* Channel-Struktur suchen */
427 assert( Name != NULL );
429 search_hash = Hash( Name );
433 if( search_hash == c->hash )
435 /* lt. Hash-Wert: Treffer! */
436 if( strcasecmp( Name, c->name ) == 0 ) return c;
441 } /* Channel_Search */
445 Channel_FirstMember( CHANNEL *Chan )
447 assert( Chan != NULL );
448 return Get_First_Cl2Chan( NULL, Chan );
449 } /* Channel_FirstMember */
453 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
455 assert( Chan != NULL );
456 assert( Cl2Chan != NULL );
457 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
458 } /* Channel_NextMember */
462 Channel_FirstChannelOf( CLIENT *Client )
464 assert( Client != NULL );
465 return Get_First_Cl2Chan( Client, NULL );
466 } /* Channel_FirstChannelOf */
470 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
472 assert( Client != NULL );
473 assert( Cl2Chan != NULL );
474 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
475 } /* Channel_NextChannelOf */
479 Channel_GetClient( CL2CHAN *Cl2Chan )
481 assert( Cl2Chan != NULL );
482 return Cl2Chan->client;
483 } /* Channel_GetClient */
487 Channel_GetChannel( CL2CHAN *Cl2Chan )
489 assert( Cl2Chan != NULL );
490 return Cl2Chan->channel;
491 } /* Channel_GetChannel */
495 Channel_IsValidName( const char *Name )
497 assert( Name != NULL );
499 if (strchr("+#", Name[0]) == NULL)
501 if (strlen(Name) >= CHANNEL_NAME_LEN)
504 return Name[strcspn(Name, " ,:\007")] == 0;
505 } /* Channel_IsValidName */
509 Channel_ModeAdd( CHANNEL *Chan, char Mode )
512 * If the channel already had this mode, return false.
513 * If the channel mode was newly set return true.
518 assert( Chan != NULL );
520 x[0] = Mode; x[1] = '\0';
521 if( ! strchr( Chan->modes, x[0] ))
523 /* Channel does not have this mode yet, set it */
524 strlcat( Chan->modes, x, sizeof( Chan->modes ));
528 } /* Channel_ModeAdd */
532 Channel_ModeDel( CHANNEL *Chan, char Mode )
535 * if the mode was removed return true.
536 * if the channel did not have the mode, return false.
540 assert( Chan != NULL );
542 p = strchr( Chan->modes, Mode );
543 if( ! p ) return false;
545 /* Channel has mode -> delete */
552 } /* Channel_ModeDel */
556 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
558 /* Set Channel-User-Mode.
559 * if mode was newly set, return true.
560 * if the User already had this channel-mode, return false.
566 assert( Chan != NULL );
567 assert( Client != NULL );
569 cl2chan = Get_Cl2Chan( Chan, Client );
570 assert( cl2chan != NULL );
572 x[0] = Mode; x[1] = '\0';
573 if( ! strchr( cl2chan->modes, x[0] ))
575 /* mode not set, -> set it */
576 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
580 } /* Channel_UserModeAdd */
584 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
586 /* Delete Channel-User-Mode.
587 * If Mode was removed, return true.
588 * If User did not have the Channel-Mode, return false.
594 assert( Chan != NULL );
595 assert( Client != NULL );
597 cl2chan = Get_Cl2Chan( Chan, Client );
598 assert( cl2chan != NULL );
600 p = strchr( cl2chan->modes, Mode );
601 if( ! p ) return false;
603 /* Client has Mode -> delete */
610 } /* Channel_UserModeDel */
614 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
616 /* return Users' Channel-Modes */
620 assert( Chan != NULL );
621 assert( Client != NULL );
623 cl2chan = Get_Cl2Chan( Chan, Client );
624 assert( cl2chan != NULL );
626 return cl2chan->modes;
627 } /* Channel_UserModes */
631 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
633 /* Test if Client is on Channel Chan */
635 assert( Chan != NULL );
636 assert( Client != NULL );
637 return Get_Cl2Chan(Chan, Client) != NULL;
638 } /* Channel_IsMemberOf */
642 Channel_Topic( CHANNEL *Chan )
645 assert( Chan != NULL );
646 ret = array_start(&Chan->topic);
647 return ret ? ret : "";
648 } /* Channel_Topic */
654 Channel_TopicTime(CHANNEL *Chan)
656 assert(Chan != NULL);
657 return (unsigned int) Chan->topic_time;
658 } /* Channel_TopicTime */
662 Channel_TopicWho(CHANNEL *Chan)
664 assert(Chan != NULL);
665 return Chan->topic_who;
666 } /* Channel_TopicWho */
672 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, char *Topic)
675 assert( Chan != NULL );
676 assert( Topic != NULL );
679 if (len < array_bytes(&Chan->topic))
680 array_free(&Chan->topic);
682 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
683 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
684 Topic, Chan->name, strerror(errno));
686 Chan->topic_time = time(NULL);
687 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
688 strlcpy(Chan->topic_who, Client_ID(Client),
689 sizeof Chan->topic_who);
691 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
692 sizeof Chan->topic_who);
696 } /* Channel_SetTopic */
700 Channel_SetModes( CHANNEL *Chan, char *Modes )
702 assert( Chan != NULL );
703 assert( Modes != NULL );
705 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
706 } /* Channel_SetModes */
710 Channel_SetKey( CHANNEL *Chan, char *Key )
712 assert( Chan != NULL );
713 assert( Key != NULL );
715 strlcpy( Chan->key, Key, sizeof( Chan->key ));
716 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
717 } /* Channel_SetKey */
721 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
723 assert( Chan != NULL );
725 Chan->maxusers = Count;
726 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
727 } /* Channel_SetMaxUsers */
731 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
733 bool is_member, has_voice, is_op;
735 is_member = has_voice = is_op = false;
737 if (Channel_IsMemberOf(Chan, From)) {
739 if (strchr(Channel_UserModes(Chan, From), 'v'))
741 if (strchr(Channel_UserModes(Chan, From), 'o'))
746 * Is the client allowed to write to channel?
748 * If channel mode n set: non-members cannot send to channel.
749 * If channel mode m set: need voice.
751 if (strchr(Channel_Modes(Chan), 'n') && !is_member)
754 if (is_op || has_voice)
757 if (strchr(Channel_Modes(Chan), 'm'))
760 return !Lists_Check(&Chan->list_bans, From);
765 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Text)
767 if (!Can_Send_To_Channel(Chan, From))
768 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG, Client_ID(From), Channel_Name(Chan));
770 if (Client_Conn(From) > NONE)
771 Conn_UpdateIdle(Client_Conn(From));
773 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
774 "PRIVMSG %s :%s", Channel_Name(Chan), Text);
779 Channel_Notice(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Text)
781 if (!Can_Send_To_Channel(Chan, From))
782 return true; /* no error, see RFC 2812 */
784 if (Client_Conn(From) > NONE)
785 Conn_UpdateIdle(Client_Conn(From));
787 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
788 "NOTICE %s :%s", Channel_Name(Chan), Text);
793 Channel_Create( char *Name )
795 /* Create new CHANNEL structure and add it to linked list */
798 assert( Name != NULL );
800 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
803 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
806 memset( c, 0, sizeof( CHANNEL ));
807 strlcpy( c->name, Name, sizeof( c->name ));
808 c->hash = Hash( c->name );
809 c->next = My_Channels;
811 LogDebug("Created new channel structure for \"%s\".", Name);
813 } /* Channel_Create */
817 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
821 assert( Chan != NULL );
822 assert( Client != NULL );
824 cl2chan = My_Cl2Chan;
827 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
828 cl2chan = cl2chan->next;
835 Add_Client( CHANNEL *Chan, CLIENT *Client )
839 assert( Chan != NULL );
840 assert( Client != NULL );
842 /* neue CL2CHAN-Struktur anlegen */
843 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
846 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
849 cl2chan->channel = Chan;
850 cl2chan->client = Client;
851 strcpy( cl2chan->modes, "" );
854 cl2chan->next = My_Cl2Chan;
855 My_Cl2Chan = cl2chan;
857 Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
864 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
866 CL2CHAN *cl2chan, *last_cl2chan;
869 assert( Chan != NULL );
870 assert( Client != NULL );
871 assert( Origin != NULL );
872 assert( Reason != NULL );
875 cl2chan = My_Cl2Chan;
878 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
879 last_cl2chan = cl2chan;
880 cl2chan = cl2chan->next;
882 if( ! cl2chan ) return false;
884 c = cl2chan->channel;
887 /* Aus Verkettung loesen und freigeben */
888 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
889 else My_Cl2Chan = cl2chan->next;
895 /* QUIT: other servers have already been notified, see Client_Destroy();
896 * so only inform other clients in same channel. */
897 assert( InformServer == false );
898 LogDebug("User \"%s\" left channel \"%s\" (%s).",
899 Client_Mask( Client ), c->name, Reason );
902 /* User was KICKed: inform other servers and all users in channel */
904 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
905 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
906 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
907 c->name, Client_ID( Client ), Reason );
908 if ((Client_Conn(Client) > NONE) &&
909 (Client_Type(Client) == CLIENT_USER))
911 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
912 c->name, Client_ID( Client ), Reason);
914 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
915 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
919 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
921 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
924 if ((Client_Conn(Origin) > NONE) &&
925 (Client_Type(Origin) == CLIENT_USER))
927 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
928 LogDebug("User \"%s\" left channel \"%s\" (%s).",
929 Client_Mask(Client), c->name, Reason);
933 /* Wenn Channel nun leer und nicht pre-defined: loeschen */
934 if( ! strchr( Channel_Modes( Chan ), 'P' ))
936 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
940 } /* Remove_Client */
944 Channel_AddBan(CHANNEL *c, const char *mask )
946 struct list_head *h = Channel_GetListBans(c);
947 return Lists_Add(h, mask, false);
952 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
954 struct list_head *h = Channel_GetListInvites(c);
955 return Lists_Add(h, mask, onlyonce);
960 ShowInvitesBans(struct list_head *head, CLIENT *Client, CHANNEL *Channel, bool invite)
963 char *msg = invite ? RPL_INVITELIST_MSG : RPL_BANLIST_MSG;
966 assert( Client != NULL );
967 assert( Channel != NULL );
969 e = Lists_GetFirst(head);
971 if( ! IRC_WriteStrClient( Client, msg, Client_ID( Client ),
972 Channel_Name( Channel ), Lists_GetMask(e) )) return DISCONNECTED;
973 e = Lists_GetNext(e);
976 msg_end = invite ? RPL_ENDOFINVITELIST_MSG : RPL_ENDOFBANLIST_MSG;
977 return IRC_WriteStrClient( Client, msg_end, Client_ID( Client ), Channel_Name( Channel ));
982 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
986 assert( Channel != NULL );
988 h = Channel_GetListBans(Channel);
989 return ShowInvitesBans(h, Client, Channel, false);
994 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
998 assert( Channel != NULL );
1000 h = Channel_GetListInvites(Channel);
1001 return ShowInvitesBans(h, Client, Channel, true);
1006 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1008 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1009 } /* Get_First_Cl2Chan */
1013 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1017 assert( Client != NULL || Channel != NULL );
1022 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1023 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1024 cl2chan = cl2chan->next;
1027 } /* Get_Next_Cl2Chan */
1031 Delete_Channel( CHANNEL *Chan )
1033 /* Channel-Struktur loeschen */
1035 CHANNEL *chan, *last_chan;
1041 if( chan == Chan ) break;
1045 if( ! chan ) return false;
1047 Log( LOG_DEBUG, "Freed channel structure for \"%s\".", Chan->name );
1049 /* Invite- und Ban-Lists aufraeumen */
1050 Lists_Free( &chan->list_bans );
1051 Lists_Free( &chan->list_invites );
1053 /* Neu verketten und freigeben */
1054 if( last_chan ) last_chan->next = chan->next;
1055 else My_Channels = chan->next;
1059 } /* Delete_Channel */