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);
222 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
223 Client_ID(Client), Name);
226 if (!Get_Cl2Chan(chan, Client)) {
227 IRC_WriteStrClient(Client, ERR_NOTONCHANNEL_MSG,
228 Client_ID(Client), Name);
232 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
240 Channel_Kick( CLIENT *Client, CLIENT *Origin, char *Name, char *Reason )
244 assert( Client != NULL );
245 assert( Origin != NULL );
246 assert( Name != NULL );
247 assert( Reason != NULL );
249 chan = Channel_Search( Name );
252 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
256 if( ! Channel_IsMemberOf( chan, Origin ))
258 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Name );
262 /* Is User Channel-Operator? */
263 if( ! strchr( Channel_UserModes( chan, Origin ), 'o' ))
265 IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Name);
269 /* Ist the kickED User member of channel? */
270 if( ! Channel_IsMemberOf( chan, Client ))
272 IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( Client ), Name );
276 Remove_Client( REMOVE_KICK, chan, Client, Origin, Reason, true);
281 Channel_Quit( CLIENT *Client, char *Reason )
285 assert( Client != NULL );
286 assert( Reason != NULL );
288 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
294 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
301 Channel_Count( void )
304 unsigned long count = 0;
313 } /* Channel_Count */
317 Channel_MemberCount( CHANNEL *Chan )
320 unsigned long count = 0;
322 assert( Chan != NULL );
324 cl2chan = My_Cl2Chan;
327 if( cl2chan->channel == Chan ) count++;
328 cl2chan = cl2chan->next;
331 } /* Channel_MemberCount */
335 Channel_CountForUser( CLIENT *Client )
337 /* Count number of channels a user is member of. */
342 assert( Client != NULL );
344 cl2chan = My_Cl2Chan;
347 if( cl2chan->client == Client ) count++;
348 cl2chan = cl2chan->next;
352 } /* Channel_CountForUser */
357 Channel_Name( const CHANNEL *Chan )
359 assert( Chan != NULL );
365 Channel_Modes( CHANNEL *Chan )
367 assert( Chan != NULL );
369 } /* Channel_Modes */
373 Channel_Key( CHANNEL *Chan )
375 assert( Chan != NULL );
381 Channel_MaxUsers( CHANNEL *Chan )
383 assert( Chan != NULL );
384 return Chan->maxusers;
385 } /* Channel_MaxUsers */
389 Channel_First( void )
392 } /* Channel_First */
396 Channel_Next( CHANNEL *Chan )
398 assert( Chan != NULL );
404 Channel_Search( const char *Name )
406 /* Channel-Struktur suchen */
411 assert( Name != NULL );
413 search_hash = Hash( Name );
417 if( search_hash == c->hash )
419 /* lt. Hash-Wert: Treffer! */
420 if( strcasecmp( Name, c->name ) == 0 ) return c;
425 } /* Channel_Search */
429 Channel_FirstMember( CHANNEL *Chan )
431 assert( Chan != NULL );
432 return Get_First_Cl2Chan( NULL, Chan );
433 } /* Channel_FirstMember */
437 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
439 assert( Chan != NULL );
440 assert( Cl2Chan != NULL );
441 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
442 } /* Channel_NextMember */
446 Channel_FirstChannelOf( CLIENT *Client )
448 assert( Client != NULL );
449 return Get_First_Cl2Chan( Client, NULL );
450 } /* Channel_FirstChannelOf */
454 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
456 assert( Client != NULL );
457 assert( Cl2Chan != NULL );
458 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
459 } /* Channel_NextChannelOf */
463 Channel_GetClient( CL2CHAN *Cl2Chan )
465 assert( Cl2Chan != NULL );
466 return Cl2Chan->client;
467 } /* Channel_GetClient */
471 Channel_GetChannel( CL2CHAN *Cl2Chan )
473 assert( Cl2Chan != NULL );
474 return Cl2Chan->channel;
475 } /* Channel_GetChannel */
479 Channel_IsValidName( const char *Name )
481 assert( Name != NULL );
486 case '+': /* modeless channel */
489 default: return false;
491 if (strlen(Name) >= CHANNEL_NAME_LEN)
494 return Name[strcspn(Name, " ,:\007")] == 0;
495 } /* Channel_IsValidName */
499 Channel_ModeAdd( CHANNEL *Chan, char Mode )
502 * If the channel already had this mode, return false.
503 * If the channel mode was newly set return true.
508 assert( Chan != NULL );
510 x[0] = Mode; x[1] = '\0';
511 if( ! strchr( Chan->modes, x[0] ))
513 /* Channel does not have this mode yet, set it */
514 strlcat( Chan->modes, x, sizeof( Chan->modes ));
518 } /* Channel_ModeAdd */
522 Channel_ModeDel( CHANNEL *Chan, char Mode )
525 * if the mode was removed return true.
526 * if the channel did not have the mode, return false.
530 assert( Chan != NULL );
532 p = strchr( Chan->modes, Mode );
533 if( ! p ) return false;
535 /* Channel has mode -> delete */
542 } /* Channel_ModeDel */
546 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
548 /* Set Channel-User-Mode.
549 * if mode was newly set, return true.
550 * if the User already had this channel-mode, return false.
556 assert( Chan != NULL );
557 assert( Client != NULL );
559 cl2chan = Get_Cl2Chan( Chan, Client );
560 assert( cl2chan != NULL );
562 x[0] = Mode; x[1] = '\0';
563 if( ! strchr( cl2chan->modes, x[0] ))
565 /* mode not set, -> set it */
566 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
570 } /* Channel_UserModeAdd */
574 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
576 /* Delete Channel-User-Mode.
577 * If Mode was removed, return true.
578 * If User did not have the Channel-Mode, return false.
584 assert( Chan != NULL );
585 assert( Client != NULL );
587 cl2chan = Get_Cl2Chan( Chan, Client );
588 assert( cl2chan != NULL );
590 p = strchr( cl2chan->modes, Mode );
591 if( ! p ) return false;
593 /* Client has Mode -> delete */
600 } /* Channel_UserModeDel */
604 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
606 /* return Users' Channel-Modes */
610 assert( Chan != NULL );
611 assert( Client != NULL );
613 cl2chan = Get_Cl2Chan( Chan, Client );
614 assert( cl2chan != NULL );
616 return cl2chan->modes;
617 } /* Channel_UserModes */
621 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
623 /* Test if Client is on Channel Chan */
625 assert( Chan != NULL );
626 assert( Client != NULL );
627 return Get_Cl2Chan(Chan, Client) != NULL;
628 } /* Channel_IsMemberOf */
632 Channel_Topic( CHANNEL *Chan )
635 assert( Chan != NULL );
636 ret = array_start(&Chan->topic);
637 return ret ? ret : "";
638 } /* Channel_Topic */
644 Channel_TopicTime(CHANNEL *Chan)
646 assert(Chan != NULL);
647 return (unsigned int) Chan->topic_time;
648 } /* Channel_TopicTime */
652 Channel_TopicWho(CHANNEL *Chan)
654 assert(Chan != NULL);
655 return Chan->topic_who;
656 } /* Channel_TopicWho */
662 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, char *Topic)
665 assert( Chan != NULL );
666 assert( Topic != NULL );
669 if (len < array_bytes(&Chan->topic))
670 array_free(&Chan->topic);
672 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
673 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
674 Topic, Chan->name, strerror(errno));
676 Chan->topic_time = time(NULL);
677 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
678 strlcpy(Chan->topic_who, Client_ID(Client),
679 sizeof Chan->topic_who);
681 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
682 sizeof Chan->topic_who);
686 } /* Channel_SetTopic */
690 Channel_SetModes( CHANNEL *Chan, char *Modes )
692 assert( Chan != NULL );
693 assert( Modes != NULL );
695 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
696 } /* Channel_SetModes */
700 Channel_SetKey( CHANNEL *Chan, char *Key )
702 assert( Chan != NULL );
703 assert( Key != NULL );
705 strlcpy( Chan->key, Key, sizeof( Chan->key ));
706 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
707 } /* Channel_SetKey */
711 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
713 assert( Chan != NULL );
715 Chan->maxusers = Count;
716 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
717 } /* Channel_SetMaxUsers */
721 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
723 bool is_member, has_voice, is_op;
725 is_member = has_voice = is_op = false;
727 if (Channel_IsMemberOf(Chan, From)) {
729 if (strchr(Channel_UserModes(Chan, From), 'v'))
731 if (strchr(Channel_UserModes(Chan, From), 'o'))
736 * Is the client allowed to write to channel?
738 * If channel mode n set: non-members cannot send to channel.
739 * If channel mode m set: need voice.
741 if (strchr(Channel_Modes(Chan), 'n') && !is_member)
744 if (is_op || has_voice)
747 if (strchr(Channel_Modes(Chan), 'm'))
750 return !Lists_Check(&Chan->list_bans, From);
755 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Text)
757 if (!Can_Send_To_Channel(Chan, From))
758 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG, Client_ID(From), Channel_Name(Chan));
760 if (Client_Conn(From) > NONE)
761 Conn_UpdateIdle(Client_Conn(From));
763 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
764 "PRIVMSG %s :%s", Channel_Name(Chan), Text);
769 Channel_Notice(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Text)
771 if (!Can_Send_To_Channel(Chan, From))
772 return true; /* no error, see RFC 2812 */
774 if (Client_Conn(From) > NONE)
775 Conn_UpdateIdle(Client_Conn(From));
777 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
778 "NOTICE %s :%s", Channel_Name(Chan), Text);
783 Channel_Create( char *Name )
785 /* Create new CHANNEL structure and add it to linked list */
788 assert( Name != NULL );
790 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
793 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
796 memset( c, 0, sizeof( CHANNEL ));
797 strlcpy( c->name, Name, sizeof( c->name ));
798 c->hash = Hash( c->name );
799 c->next = My_Channels;
801 LogDebug("Created new channel structure for \"%s\".", Name);
803 } /* Channel_Create */
807 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
811 assert( Chan != NULL );
812 assert( Client != NULL );
814 cl2chan = My_Cl2Chan;
817 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
818 cl2chan = cl2chan->next;
825 Add_Client( CHANNEL *Chan, CLIENT *Client )
829 assert( Chan != NULL );
830 assert( Client != NULL );
832 /* neue CL2CHAN-Struktur anlegen */
833 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
836 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
839 cl2chan->channel = Chan;
840 cl2chan->client = Client;
841 strcpy( cl2chan->modes, "" );
844 cl2chan->next = My_Cl2Chan;
845 My_Cl2Chan = cl2chan;
847 Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
854 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
856 CL2CHAN *cl2chan, *last_cl2chan;
859 assert( Chan != NULL );
860 assert( Client != NULL );
861 assert( Origin != NULL );
862 assert( Reason != NULL );
865 cl2chan = My_Cl2Chan;
868 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
869 last_cl2chan = cl2chan;
870 cl2chan = cl2chan->next;
872 if( ! cl2chan ) return false;
874 c = cl2chan->channel;
877 /* Aus Verkettung loesen und freigeben */
878 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
879 else My_Cl2Chan = cl2chan->next;
885 /* QUIT: other servers have already been notified, see Client_Destroy();
886 * so only inform other clients in same channel. */
887 assert( InformServer == false );
888 LogDebug("User \"%s\" left channel \"%s\" (%s).",
889 Client_Mask( Client ), c->name, Reason );
892 /* User was KICKed: inform other servers and all users in channel */
894 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
895 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
896 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
897 c->name, Client_ID( Client ), Reason );
898 if ((Client_Conn(Client) > NONE) &&
899 (Client_Type(Client) == CLIENT_USER))
901 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
902 c->name, Client_ID( Client ), Reason);
904 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
905 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
909 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
911 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
914 if ((Client_Conn(Origin) > NONE) &&
915 (Client_Type(Origin) == CLIENT_USER))
917 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
918 LogDebug("User \"%s\" left channel \"%s\" (%s).",
919 Client_Mask(Client), c->name, Reason);
923 /* Wenn Channel nun leer und nicht pre-defined: loeschen */
924 if( ! strchr( Channel_Modes( Chan ), 'P' ))
926 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
930 } /* Remove_Client */
934 Channel_AddBan(CHANNEL *c, const char *mask )
936 struct list_head *h = Channel_GetListBans(c);
937 return Lists_Add(h, mask, false);
942 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
944 struct list_head *h = Channel_GetListInvites(c);
945 return Lists_Add(h, mask, onlyonce);
950 ShowInvitesBans(struct list_head *head, CLIENT *Client, CHANNEL *Channel, bool invite)
953 char *msg = invite ? RPL_INVITELIST_MSG : RPL_BANLIST_MSG;
956 assert( Client != NULL );
957 assert( Channel != NULL );
959 e = Lists_GetFirst(head);
961 if( ! IRC_WriteStrClient( Client, msg, Client_ID( Client ),
962 Channel_Name( Channel ), Lists_GetMask(e) )) return DISCONNECTED;
963 e = Lists_GetNext(e);
966 msg_end = invite ? RPL_ENDOFINVITELIST_MSG : RPL_ENDOFBANLIST_MSG;
967 return IRC_WriteStrClient( Client, msg_end, Client_ID( Client ), Channel_Name( Channel ));
972 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
976 assert( Channel != NULL );
978 h = Channel_GetListBans(Channel);
979 return ShowInvitesBans(h, Client, Channel, false);
984 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
988 assert( Channel != NULL );
990 h = Channel_GetListInvites(Channel);
991 return ShowInvitesBans(h, Client, Channel, true);
996 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
998 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
999 } /* Get_First_Cl2Chan */
1003 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1007 assert( Client != NULL || Channel != NULL );
1012 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1013 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1014 cl2chan = cl2chan->next;
1017 } /* Get_Next_Cl2Chan */
1021 Delete_Channel( CHANNEL *Chan )
1023 /* Channel-Struktur loeschen */
1025 CHANNEL *chan, *last_chan;
1031 if( chan == Chan ) break;
1035 if( ! chan ) return false;
1037 Log( LOG_DEBUG, "Freed channel structure for \"%s\".", Chan->name );
1039 /* Invite- und Ban-Lists aufraeumen */
1040 Lists_Free( &chan->list_bans );
1041 Lists_Free( &chan->list_invites );
1043 /* Neu verketten und freigeben */
1044 if( last_chan ) last_chan->next = chan->next;
1045 else My_Channels = chan->next;
1049 } /* Delete_Channel */