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 /* Vordefinierte persistente Channels erzeugen */
98 for( i = 0; i < Conf_Channel_Count; i++ )
100 /* Ist ein Name konfiguriert? */
101 if( ! Conf_Channel[i].name[0] ) continue;
103 /* Gueltiger 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 /* Gibt es den Channel bereits? */
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 /* Channel-Strukturen freigeben */
157 array_free(&c->topic);
162 /* Channel-Zuordnungstabelle freigeben */
163 cl2chan = My_Cl2Chan;
166 cl2chan_next = cl2chan->next;
168 cl2chan = cl2chan_next;
174 Channel_Join( CLIENT *Client, char *Name )
178 assert( Client != NULL );
179 assert( Name != NULL );
181 if( ! Channel_IsValidName( Name )) {
182 IRC_WriteStrClient( Client, ERR_NOSUCHCHANNEL_MSG, Client_ID( Client ), Name );
186 chan = Channel_Search( Name );
188 /* Ist der Client bereits Mitglied? */
189 if( Get_Cl2Chan( chan, Client )) return false;
193 /* Gibt es noch nicht? Dann neu anlegen: */
194 chan = Channel_Create( Name );
195 if (!chan) return false;
198 /* User dem Channel hinzufuegen */
199 if( ! Add_Client( chan, Client )) return false;
205 * Remove client from channel.
206 * This function lets a client lead a channel. First, the function checks
207 * if the channel exists and the client is a member of it and sends out
208 * appropriate error messages if not. The real work is done by the function
212 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
216 assert(Client != NULL);
217 assert(Name != NULL);
218 assert(Reason != NULL);
220 chan = Channel_Search(Name);
221 if ((!chan) || (!Get_Cl2Chan(chan, Client))) {
222 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
223 Client_ID(Client), Name);
227 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
235 Channel_Kick( CLIENT *Client, CLIENT *Origin, char *Name, char *Reason )
239 assert( Client != NULL );
240 assert( Origin != NULL );
241 assert( Name != NULL );
242 assert( Reason != NULL );
244 chan = Channel_Search( Name );
247 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
251 if( ! Channel_IsMemberOf( chan, Origin ))
253 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Name );
257 /* Is User Channel-Operator? */
258 if( ! strchr( Channel_UserModes( chan, Origin ), 'o' ))
260 IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Name);
264 /* Ist the kickED User member of channel? */
265 if( ! Channel_IsMemberOf( chan, Client ))
267 IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( Client ), Name );
271 Remove_Client( REMOVE_KICK, chan, Client, Origin, Reason, true);
276 Channel_Quit( CLIENT *Client, char *Reason )
280 assert( Client != NULL );
281 assert( Reason != NULL );
283 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
289 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
296 Channel_Count( void )
299 unsigned long count = 0;
308 } /* Channel_Count */
312 Channel_MemberCount( CHANNEL *Chan )
315 unsigned long count = 0;
317 assert( Chan != NULL );
319 cl2chan = My_Cl2Chan;
322 if( cl2chan->channel == Chan ) count++;
323 cl2chan = cl2chan->next;
326 } /* Channel_MemberCount */
330 Channel_CountForUser( CLIENT *Client )
332 /* Count number of channels a user is member of. */
337 assert( Client != NULL );
339 cl2chan = My_Cl2Chan;
342 if( cl2chan->client == Client ) count++;
343 cl2chan = cl2chan->next;
347 } /* Channel_CountForUser */
352 Channel_Name( const CHANNEL *Chan )
354 assert( Chan != NULL );
360 Channel_Modes( CHANNEL *Chan )
362 assert( Chan != NULL );
364 } /* Channel_Modes */
368 Channel_Key( CHANNEL *Chan )
370 assert( Chan != NULL );
376 Channel_MaxUsers( CHANNEL *Chan )
378 assert( Chan != NULL );
379 return Chan->maxusers;
380 } /* Channel_MaxUsers */
384 Channel_First( void )
387 } /* Channel_First */
391 Channel_Next( CHANNEL *Chan )
393 assert( Chan != NULL );
399 Channel_Search( const char *Name )
401 /* Channel-Struktur suchen */
406 assert( Name != NULL );
408 search_hash = Hash( Name );
412 if( search_hash == c->hash )
414 /* lt. Hash-Wert: Treffer! */
415 if( strcasecmp( Name, c->name ) == 0 ) return c;
420 } /* Channel_Search */
424 Channel_FirstMember( CHANNEL *Chan )
426 assert( Chan != NULL );
427 return Get_First_Cl2Chan( NULL, Chan );
428 } /* Channel_FirstMember */
432 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
434 assert( Chan != NULL );
435 assert( Cl2Chan != NULL );
436 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
437 } /* Channel_NextMember */
441 Channel_FirstChannelOf( CLIENT *Client )
443 assert( Client != NULL );
444 return Get_First_Cl2Chan( Client, NULL );
445 } /* Channel_FirstChannelOf */
449 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
451 assert( Client != NULL );
452 assert( Cl2Chan != NULL );
453 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
454 } /* Channel_NextChannelOf */
458 Channel_GetClient( CL2CHAN *Cl2Chan )
460 assert( Cl2Chan != NULL );
461 return Cl2Chan->client;
462 } /* Channel_GetClient */
466 Channel_GetChannel( CL2CHAN *Cl2Chan )
468 assert( Cl2Chan != NULL );
469 return Cl2Chan->channel;
470 } /* Channel_GetChannel */
474 Channel_IsValidName( const char *Name )
476 assert( Name != NULL );
478 if(( Name[0] != '#' ) || ( strlen( Name ) >= CHANNEL_NAME_LEN )) return false;
480 return Name[strcspn(Name, " ,:\007")] == 0;
481 } /* Channel_IsValidName */
485 Channel_ModeAdd( CHANNEL *Chan, char Mode )
488 * If the channel already had this mode, return false.
489 * If the channel mode was newly set return true.
494 assert( Chan != NULL );
496 x[0] = Mode; x[1] = '\0';
497 if( ! strchr( Chan->modes, x[0] ))
499 /* Channel does not have this mode yet, set it */
500 strlcat( Chan->modes, x, sizeof( Chan->modes ));
504 } /* Channel_ModeAdd */
508 Channel_ModeDel( CHANNEL *Chan, char Mode )
511 * if the mode was removed return true.
512 * if the channel did not have the mode, return false.
516 assert( Chan != NULL );
518 p = strchr( Chan->modes, Mode );
519 if( ! p ) return false;
521 /* Channel has mode -> delete */
528 } /* Channel_ModeDel */
532 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
534 /* Set Channel-User-Mode.
535 * if mode was newly set, return true.
536 * if the User already had this channel-mode, return false.
542 assert( Chan != NULL );
543 assert( Client != NULL );
545 cl2chan = Get_Cl2Chan( Chan, Client );
546 assert( cl2chan != NULL );
548 x[0] = Mode; x[1] = '\0';
549 if( ! strchr( cl2chan->modes, x[0] ))
551 /* mode not set, -> set it */
552 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
556 } /* Channel_UserModeAdd */
560 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
562 /* Delete Channel-User-Mode.
563 * If Mode was removed, return true.
564 * If User did not have the Channel-Mode, return false.
570 assert( Chan != NULL );
571 assert( Client != NULL );
573 cl2chan = Get_Cl2Chan( Chan, Client );
574 assert( cl2chan != NULL );
576 p = strchr( cl2chan->modes, Mode );
577 if( ! p ) return false;
579 /* Client has Mode -> delete */
586 } /* Channel_UserModeDel */
590 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
592 /* return Users' Channel-Modes */
596 assert( Chan != NULL );
597 assert( Client != NULL );
599 cl2chan = Get_Cl2Chan( Chan, Client );
600 assert( cl2chan != NULL );
602 return cl2chan->modes;
603 } /* Channel_UserModes */
607 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
609 /* Test if Client is on Channel Chan */
611 assert( Chan != NULL );
612 assert( Client != NULL );
613 return Get_Cl2Chan(Chan, Client) != NULL;
614 } /* Channel_IsMemberOf */
618 Channel_Topic( CHANNEL *Chan )
621 assert( Chan != NULL );
622 ret = array_start(&Chan->topic);
623 return ret ? ret : "";
624 } /* Channel_Topic */
630 Channel_TopicTime(CHANNEL *Chan)
632 assert(Chan != NULL);
633 return (unsigned int) Chan->topic_time;
634 } /* Channel_TopicTime */
638 Channel_TopicWho(CHANNEL *Chan)
640 assert(Chan != NULL);
641 return Chan->topic_who;
642 } /* Channel_TopicWho */
648 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, char *Topic)
651 assert( Chan != NULL );
652 assert( Topic != NULL );
655 if (len < array_bytes(&Chan->topic))
656 array_free(&Chan->topic);
658 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
659 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
660 Topic, Chan->name, strerror(errno));
662 Chan->topic_time = time(NULL);
663 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
664 strlcpy(Chan->topic_who, Client_ID(Client),
665 sizeof Chan->topic_who);
667 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
668 sizeof Chan->topic_who);
672 } /* Channel_SetTopic */
676 Channel_SetModes( CHANNEL *Chan, char *Modes )
678 assert( Chan != NULL );
679 assert( Modes != NULL );
681 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
682 } /* Channel_SetModes */
686 Channel_SetKey( CHANNEL *Chan, char *Key )
688 assert( Chan != NULL );
689 assert( Key != NULL );
691 strlcpy( Chan->key, Key, sizeof( Chan->key ));
692 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
693 } /* Channel_SetKey */
697 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
699 assert( Chan != NULL );
701 Chan->maxusers = Count;
702 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
703 } /* Channel_SetMaxUsers */
707 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
709 bool is_member, has_voice, is_op;
711 is_member = has_voice = is_op = false;
713 if (Channel_IsMemberOf(Chan, From)) {
715 if (strchr(Channel_UserModes(Chan, From), 'v'))
717 if (strchr(Channel_UserModes(Chan, From), 'o'))
722 * Is the client allowed to write to channel?
724 * If channel mode n set: non-members cannot send to channel.
725 * If channel mode m set: need voice.
727 if (strchr(Channel_Modes(Chan), 'n') && !is_member)
730 if (is_op || has_voice)
733 if (strchr(Channel_Modes(Chan), 'm'))
736 return !Lists_Check(&Chan->list_bans, From);
741 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Text)
743 if (!Can_Send_To_Channel(Chan, From))
744 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG, Client_ID(From), Channel_Name(Chan));
746 if (Client_Conn(From) > NONE)
747 Conn_UpdateIdle(Client_Conn(From));
749 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
750 "PRIVMSG %s :%s", Channel_Name(Chan), Text);
755 Channel_Notice(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Text)
757 if (!Can_Send_To_Channel(Chan, From))
758 return true; /* no error, see RFC 2812 */
760 if (Client_Conn(From) > NONE)
761 Conn_UpdateIdle(Client_Conn(From));
763 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
764 "NOTICE %s :%s", Channel_Name(Chan), Text);
769 Channel_Create( char *Name )
771 /* Create new CHANNEL structure and add it to linked list */
774 assert( Name != NULL );
776 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
779 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
782 memset( c, 0, sizeof( CHANNEL ));
783 strlcpy( c->name, Name, sizeof( c->name ));
784 c->hash = Hash( c->name );
785 c->next = My_Channels;
787 LogDebug("Created new channel structure for \"%s\".", Name);
789 } /* Channel_Create */
793 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
797 assert( Chan != NULL );
798 assert( Client != NULL );
800 cl2chan = My_Cl2Chan;
803 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
804 cl2chan = cl2chan->next;
811 Add_Client( CHANNEL *Chan, CLIENT *Client )
815 assert( Chan != NULL );
816 assert( Client != NULL );
818 /* neue CL2CHAN-Struktur anlegen */
819 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
822 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
825 cl2chan->channel = Chan;
826 cl2chan->client = Client;
827 strcpy( cl2chan->modes, "" );
830 cl2chan->next = My_Cl2Chan;
831 My_Cl2Chan = cl2chan;
833 Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
840 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
842 CL2CHAN *cl2chan, *last_cl2chan;
845 assert( Chan != NULL );
846 assert( Client != NULL );
847 assert( Origin != NULL );
848 assert( Reason != NULL );
851 cl2chan = My_Cl2Chan;
854 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
855 last_cl2chan = cl2chan;
856 cl2chan = cl2chan->next;
858 if( ! cl2chan ) return false;
860 c = cl2chan->channel;
863 /* Aus Verkettung loesen und freigeben */
864 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
865 else My_Cl2Chan = cl2chan->next;
871 /* QUIT: other servers have already been notified, see Client_Destroy();
872 * so only inform other clients in same channel. */
873 assert( InformServer == false );
874 LogDebug("User \"%s\" left channel \"%s\" (%s).",
875 Client_Mask( Client ), c->name, Reason );
878 /* User was KICKed: inform other servers and all users in channel */
880 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
881 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
882 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
883 c->name, Client_ID( Client ), Reason );
884 if ((Client_Conn(Client) > NONE) &&
885 (Client_Type(Client) == CLIENT_USER))
887 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
888 c->name, Client_ID( Client ), Reason);
890 LogDebug("User \"%s\" has been kicked of \"%s\" by \"%s\": %s.",
891 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
895 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
897 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
900 if ((Client_Conn(Origin) > NONE) &&
901 (Client_Type(Origin) == CLIENT_USER))
903 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
904 LogDebug("User \"%s\" left channel \"%s\" (%s).",
905 Client_Mask(Client), c->name, Reason);
909 /* Wenn Channel nun leer und nicht pre-defined: loeschen */
910 if( ! strchr( Channel_Modes( Chan ), 'P' ))
912 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
916 } /* Remove_Client */
920 Channel_AddBan(CHANNEL *c, const char *mask )
922 struct list_head *h = Channel_GetListBans(c);
923 return Lists_Add(h, mask, false);
928 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
930 struct list_head *h = Channel_GetListInvites(c);
931 return Lists_Add(h, mask, onlyonce);
936 ShowInvitesBans(struct list_head *head, CLIENT *Client, CHANNEL *Channel, bool invite)
939 char *msg = invite ? RPL_INVITELIST_MSG : RPL_BANLIST_MSG;
942 assert( Client != NULL );
943 assert( Channel != NULL );
945 e = Lists_GetFirst(head);
947 if( ! IRC_WriteStrClient( Client, msg, Client_ID( Client ),
948 Channel_Name( Channel ), Lists_GetMask(e) )) return DISCONNECTED;
949 e = Lists_GetNext(e);
952 msg_end = invite ? RPL_ENDOFINVITELIST_MSG : RPL_ENDOFBANLIST_MSG;
953 return IRC_WriteStrClient( Client, msg_end, Client_ID( Client ), Channel_Name( Channel ));
958 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
962 assert( Channel != NULL );
964 h = Channel_GetListBans(Channel);
965 return ShowInvitesBans(h, Client, Channel, false);
970 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
974 assert( Channel != NULL );
976 h = Channel_GetListInvites(Channel);
977 return ShowInvitesBans(h, Client, Channel, true);
982 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
984 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
985 } /* Get_First_Cl2Chan */
989 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
993 assert( Client != NULL || Channel != NULL );
998 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
999 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1000 cl2chan = cl2chan->next;
1003 } /* Get_Next_Cl2Chan */
1007 Delete_Channel( CHANNEL *Chan )
1009 /* Channel-Struktur loeschen */
1011 CHANNEL *chan, *last_chan;
1017 if( chan == Chan ) break;
1021 if( ! chan ) return false;
1023 Log( LOG_DEBUG, "Freed channel structure for \"%s\".", Chan->name );
1025 /* Invite- und Ban-Lists aufraeumen */
1026 Lists_Free( &chan->list_bans );
1027 Lists_Free( &chan->list_invites );
1029 /* Neu verketten und freigeben */
1030 if( last_chan ) last_chan->next = chan->next;
1031 else My_Channels = chan->next;
1035 } /* Delete_Channel */