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 Channel_Part( CLIENT *Client, CLIENT *Origin, const char *Name, const char *Reason )
209 assert( Client != NULL );
210 assert( Name != NULL );
211 assert( Reason != NULL );
213 chan = Channel_Search( Name );
214 if(( ! chan ) || ( ! Get_Cl2Chan( chan, Client )))
216 IRC_WriteStrClient( Client, ERR_NOSUCHCHANNEL_MSG, Client_ID( Client ), Name );
220 /* User aus Channel entfernen */
221 if( ! Remove_Client( REMOVE_PART, chan, Client, Origin, Reason, true)) return false;
227 Channel_Kick( CLIENT *Client, CLIENT *Origin, char *Name, char *Reason )
231 assert( Client != NULL );
232 assert( Origin != NULL );
233 assert( Name != NULL );
234 assert( Reason != NULL );
236 chan = Channel_Search( Name );
239 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
243 if( ! Channel_IsMemberOf( chan, Origin ))
245 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Name );
249 /* Is User Channel-Operator? */
250 if( ! strchr( Channel_UserModes( chan, Origin ), 'o' ))
252 IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Name);
256 /* Ist the kickED User member of channel? */
257 if( ! Channel_IsMemberOf( chan, Client ))
259 IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( Client ), Name );
263 Remove_Client( REMOVE_KICK, chan, Client, Origin, Reason, true);
268 Channel_Quit( CLIENT *Client, char *Reason )
272 assert( Client != NULL );
273 assert( Reason != NULL );
275 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
281 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
288 Channel_Count( void )
291 unsigned long count = 0;
300 } /* Channel_Count */
304 Channel_MemberCount( CHANNEL *Chan )
307 unsigned long count = 0;
309 assert( Chan != NULL );
311 cl2chan = My_Cl2Chan;
314 if( cl2chan->channel == Chan ) count++;
315 cl2chan = cl2chan->next;
318 } /* Channel_MemberCount */
322 Channel_CountForUser( CLIENT *Client )
324 /* Count number of channels a user is member of. */
329 assert( Client != NULL );
331 cl2chan = My_Cl2Chan;
334 if( cl2chan->client == Client ) count++;
335 cl2chan = cl2chan->next;
339 } /* Channel_CountForUser */
344 Channel_Name( const CHANNEL *Chan )
346 assert( Chan != NULL );
352 Channel_Modes( CHANNEL *Chan )
354 assert( Chan != NULL );
356 } /* Channel_Modes */
360 Channel_Key( CHANNEL *Chan )
362 assert( Chan != NULL );
368 Channel_MaxUsers( CHANNEL *Chan )
370 assert( Chan != NULL );
371 return Chan->maxusers;
372 } /* Channel_MaxUsers */
376 Channel_First( void )
379 } /* Channel_First */
383 Channel_Next( CHANNEL *Chan )
385 assert( Chan != NULL );
391 Channel_Search( const char *Name )
393 /* Channel-Struktur suchen */
398 assert( Name != NULL );
400 search_hash = Hash( Name );
404 if( search_hash == c->hash )
406 /* lt. Hash-Wert: Treffer! */
407 if( strcasecmp( Name, c->name ) == 0 ) return c;
412 } /* Channel_Search */
416 Channel_FirstMember( CHANNEL *Chan )
418 assert( Chan != NULL );
419 return Get_First_Cl2Chan( NULL, Chan );
420 } /* Channel_FirstMember */
424 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
426 assert( Chan != NULL );
427 assert( Cl2Chan != NULL );
428 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
429 } /* Channel_NextMember */
433 Channel_FirstChannelOf( CLIENT *Client )
435 assert( Client != NULL );
436 return Get_First_Cl2Chan( Client, NULL );
437 } /* Channel_FirstChannelOf */
441 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
443 assert( Client != NULL );
444 assert( Cl2Chan != NULL );
445 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
446 } /* Channel_NextChannelOf */
450 Channel_GetClient( CL2CHAN *Cl2Chan )
452 assert( Cl2Chan != NULL );
453 return Cl2Chan->client;
454 } /* Channel_GetClient */
458 Channel_GetChannel( CL2CHAN *Cl2Chan )
460 assert( Cl2Chan != NULL );
461 return Cl2Chan->channel;
462 } /* Channel_GetChannel */
466 Channel_IsValidName( const char *Name )
468 assert( Name != NULL );
470 if(( Name[0] != '#' ) || ( strlen( Name ) >= CHANNEL_NAME_LEN )) return false;
472 return Name[strcspn(Name, " ,:\007")] == 0;
473 } /* Channel_IsValidName */
477 Channel_ModeAdd( CHANNEL *Chan, char Mode )
480 * If the channel already had this mode, return false.
481 * If the channel mode was newly set return true.
486 assert( Chan != NULL );
488 x[0] = Mode; x[1] = '\0';
489 if( ! strchr( Chan->modes, x[0] ))
491 /* Channel does not have this mode yet, set it */
492 strlcat( Chan->modes, x, sizeof( Chan->modes ));
496 } /* Channel_ModeAdd */
500 Channel_ModeDel( CHANNEL *Chan, char Mode )
503 * if the mode was removed return true.
504 * if the channel did not have the mode, return false.
508 assert( Chan != NULL );
510 p = strchr( Chan->modes, Mode );
511 if( ! p ) return false;
513 /* Channel has mode -> delete */
520 } /* Channel_ModeDel */
524 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
526 /* Set Channel-User-Mode.
527 * if mode was newly set, return true.
528 * if the User already had this channel-mode, return false.
534 assert( Chan != NULL );
535 assert( Client != NULL );
537 cl2chan = Get_Cl2Chan( Chan, Client );
538 assert( cl2chan != NULL );
540 x[0] = Mode; x[1] = '\0';
541 if( ! strchr( cl2chan->modes, x[0] ))
543 /* mode not set, -> set it */
544 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
548 } /* Channel_UserModeAdd */
552 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
554 /* Delete Channel-User-Mode.
555 * If Mode was removed, return true.
556 * If User did not have the Channel-Mode, return false.
562 assert( Chan != NULL );
563 assert( Client != NULL );
565 cl2chan = Get_Cl2Chan( Chan, Client );
566 assert( cl2chan != NULL );
568 p = strchr( cl2chan->modes, Mode );
569 if( ! p ) return false;
571 /* Client has Mode -> delete */
578 } /* Channel_UserModeDel */
582 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
584 /* return Users' Channel-Modes */
588 assert( Chan != NULL );
589 assert( Client != NULL );
591 cl2chan = Get_Cl2Chan( Chan, Client );
592 assert( cl2chan != NULL );
594 return cl2chan->modes;
595 } /* Channel_UserModes */
599 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
601 /* Test if Client is on Channel Chan */
603 assert( Chan != NULL );
604 assert( Client != NULL );
605 return Get_Cl2Chan(Chan, Client) != NULL;
606 } /* Channel_IsMemberOf */
610 Channel_Topic( CHANNEL *Chan )
613 assert( Chan != NULL );
614 ret = array_start(&Chan->topic);
615 return ret ? ret : "";
616 } /* Channel_Topic */
622 Channel_TopicTime(CHANNEL *Chan)
624 assert(Chan != NULL);
625 return (unsigned int) Chan->topic_time;
626 } /* Channel_TopicTime */
630 Channel_TopicWho(CHANNEL *Chan)
632 assert(Chan != NULL);
633 return Chan->topic_who;
634 } /* Channel_TopicWho */
640 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, char *Topic)
643 assert( Chan != NULL );
644 assert( Topic != NULL );
647 if (len < array_bytes(&Chan->topic))
648 array_free(&Chan->topic);
650 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
651 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
652 Topic, Chan->name, strerror(errno));
654 Chan->topic_time = time(NULL);
655 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
656 strlcpy(Chan->topic_who, Client_ID(Client),
657 sizeof Chan->topic_who);
659 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
660 sizeof Chan->topic_who);
664 } /* Channel_SetTopic */
668 Channel_SetModes( CHANNEL *Chan, char *Modes )
670 assert( Chan != NULL );
671 assert( Modes != NULL );
673 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
674 } /* Channel_SetModes */
678 Channel_SetKey( CHANNEL *Chan, char *Key )
680 assert( Chan != NULL );
681 assert( Key != NULL );
683 strlcpy( Chan->key, Key, sizeof( Chan->key ));
684 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
685 } /* Channel_SetKey */
689 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
691 assert( Chan != NULL );
693 Chan->maxusers = Count;
694 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
695 } /* Channel_SetMaxUsers */
699 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
701 bool is_member, has_voice, is_op;
703 is_member = has_voice = is_op = false;
705 if (Channel_IsMemberOf(Chan, From)) {
707 if (strchr(Channel_UserModes(Chan, From), 'v'))
709 if (strchr(Channel_UserModes(Chan, From), 'o'))
714 * Is the client allowed to write to channel?
716 * If channel mode n set: non-members cannot send to channel.
717 * If channel mode m set: need voice.
719 if (strchr(Channel_Modes(Chan), 'n') && !is_member)
722 if (is_op || has_voice)
725 if (strchr(Channel_Modes(Chan), 'm'))
728 return !Lists_Check(&Chan->list_bans, From);
733 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Text)
735 if (!Can_Send_To_Channel(Chan, From))
736 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG, Client_ID(From), Channel_Name(Chan));
738 if (Client_Conn(From) > NONE)
739 Conn_UpdateIdle(Client_Conn(From));
741 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
742 "PRIVMSG %s :%s", Channel_Name(Chan), Text);
747 Channel_Notice(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Text)
749 if (!Can_Send_To_Channel(Chan, From))
750 return true; /* no error, see RFC 2812 */
752 if (Client_Conn(From) > NONE)
753 Conn_UpdateIdle(Client_Conn(From));
755 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
756 "NOTICE %s :%s", Channel_Name(Chan), Text);
761 Channel_Create( char *Name )
763 /* Create new CHANNEL structure and add it to linked list */
766 assert( Name != NULL );
768 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
771 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
774 memset( c, 0, sizeof( CHANNEL ));
775 strlcpy( c->name, Name, sizeof( c->name ));
776 c->hash = Hash( c->name );
777 c->next = My_Channels;
779 LogDebug("Created new channel structure for \"%s\".", Name);
781 } /* Channel_Create */
785 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
789 assert( Chan != NULL );
790 assert( Client != NULL );
792 cl2chan = My_Cl2Chan;
795 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
796 cl2chan = cl2chan->next;
803 Add_Client( CHANNEL *Chan, CLIENT *Client )
807 assert( Chan != NULL );
808 assert( Client != NULL );
810 /* neue CL2CHAN-Struktur anlegen */
811 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
814 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
817 cl2chan->channel = Chan;
818 cl2chan->client = Client;
819 strcpy( cl2chan->modes, "" );
822 cl2chan->next = My_Cl2Chan;
823 My_Cl2Chan = cl2chan;
825 Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
832 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
834 CL2CHAN *cl2chan, *last_cl2chan;
837 assert( Chan != NULL );
838 assert( Client != NULL );
839 assert( Origin != NULL );
840 assert( Reason != NULL );
843 cl2chan = My_Cl2Chan;
846 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
847 last_cl2chan = cl2chan;
848 cl2chan = cl2chan->next;
850 if( ! cl2chan ) return false;
852 c = cl2chan->channel;
855 /* Aus Verkettung loesen und freigeben */
856 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
857 else My_Cl2Chan = cl2chan->next;
863 /* QUIT: other servers have already been notified, see Client_Destroy();
864 * so only inform other clients in same channel. */
865 assert( InformServer == false );
866 LogDebug("User \"%s\" left channel \"%s\" (%s).",
867 Client_Mask( Client ), c->name, Reason );
870 /* User was KICKed: inform other servers and all users in channel */
872 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
873 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
874 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
875 c->name, Client_ID( Client ), Reason );
876 if ((Client_Conn(Client) > NONE) &&
877 (Client_Type(Client) == CLIENT_USER))
879 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
880 c->name, Client_ID( Client ), Reason);
882 LogDebug("User \"%s\" has been kicked of \"%s\" by \"%s\": %s.",
883 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
887 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
889 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
892 if ((Client_Conn(Origin) > NONE) &&
893 (Client_Type(Origin) == CLIENT_USER))
895 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
896 LogDebug("User \"%s\" left channel \"%s\" (%s).",
897 Client_Mask(Client), c->name, Reason);
901 /* Wenn Channel nun leer und nicht pre-defined: loeschen */
902 if( ! strchr( Channel_Modes( Chan ), 'P' ))
904 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
908 } /* Remove_Client */
912 Channel_AddBan(CHANNEL *c, const char *mask )
914 struct list_head *h = Channel_GetListBans(c);
915 return Lists_Add(h, mask, false);
920 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
922 struct list_head *h = Channel_GetListInvites(c);
923 return Lists_Add(h, mask, onlyonce);
928 ShowInvitesBans(struct list_head *head, CLIENT *Client, CHANNEL *Channel, bool invite)
931 char *msg = invite ? RPL_INVITELIST_MSG : RPL_BANLIST_MSG;
934 assert( Client != NULL );
935 assert( Channel != NULL );
937 e = Lists_GetFirst(head);
939 if( ! IRC_WriteStrClient( Client, msg, Client_ID( Client ),
940 Channel_Name( Channel ), Lists_GetMask(e) )) return DISCONNECTED;
941 e = Lists_GetNext(e);
944 msg_end = invite ? RPL_ENDOFINVITELIST_MSG : RPL_ENDOFBANLIST_MSG;
945 return IRC_WriteStrClient( Client, msg_end, Client_ID( Client ), Channel_Name( Channel ));
950 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
954 assert( Channel != NULL );
956 h = Channel_GetListBans(Channel);
957 return ShowInvitesBans(h, Client, Channel, false);
962 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
966 assert( Channel != NULL );
968 h = Channel_GetListInvites(Channel);
969 return ShowInvitesBans(h, Client, Channel, true);
974 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
976 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
977 } /* Get_First_Cl2Chan */
981 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
985 assert( Client != NULL || Channel != NULL );
990 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
991 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
992 cl2chan = cl2chan->next;
995 } /* Get_Next_Cl2Chan */
999 Delete_Channel( CHANNEL *Chan )
1001 /* Channel-Struktur loeschen */
1003 CHANNEL *chan, *last_chan;
1009 if( chan == Chan ) break;
1013 if( ! chan ) return false;
1015 Log( LOG_DEBUG, "Freed channel structure for \"%s\".", Chan->name );
1017 /* Invite- und Ban-Lists aufraeumen */
1018 Lists_Free( &chan->list_bans );
1019 Lists_Free( &chan->list_invites );
1021 /* Neu verketten und freigeben */
1022 if( last_chan ) last_chan->next = chan->next;
1023 else My_Channels = chan->next;
1027 } /* Delete_Channel */