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.57 2006/08/12 11:58:21 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, 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 ));
74 Channel_InitPredefined( void )
76 /* Vordefinierte persistente Channels erzeugen */
82 for( i = 0; i < Conf_Channel_Count; i++ )
84 /* Ist ein Name konfiguriert? */
85 if( ! Conf_Channel[i].name[0] ) continue;
87 /* Gueltiger Channel-Name? */
88 if( ! Channel_IsValidName( Conf_Channel[i].name ))
90 Log( LOG_ERR, "Can't create pre-defined channel: invalid name: \"%s\"!", Conf_Channel[i].name );
91 array_free(&Conf_Channel[i].topic);
95 /* Gibt es den Channel bereits? */
96 chan = Channel_Search( Conf_Channel[i].name );
99 Log( LOG_INFO, "Can't create pre-defined channel \"%s\": name already in use.", Conf_Channel[i].name );
100 array_free(&Conf_Channel[i].topic);
105 chan = Channel_Create(Conf_Channel[i].name);
107 Channel_ModeAdd(chan, 'P');
109 if (array_start(&Conf_Channel[i].topic) != NULL)
110 Channel_SetTopic(chan, NULL,
111 array_start(&Conf_Channel[i].topic));
112 array_free(&Conf_Channel[i].topic);
114 c = Conf_Channel[i].modes;
116 Channel_ModeAdd(chan, *c++);
118 Log(LOG_INFO, "Created pre-defined channel \"%s\".",
119 Conf_Channel[i].name );
121 else Log(LOG_ERR, "Can't create pre-defined channel \"%s\"!",
122 Conf_Channel[i].name );
124 } /* Channel_InitPredefined */
131 CL2CHAN *cl2chan, *cl2chan_next;
133 /* Channel-Strukturen freigeben */
138 array_free(&c->topic);
143 /* Channel-Zuordnungstabelle freigeben */
144 cl2chan = My_Cl2Chan;
147 cl2chan_next = cl2chan->next;
149 cl2chan = cl2chan_next;
155 Channel_Join( CLIENT *Client, char *Name )
159 assert( Client != NULL );
160 assert( Name != NULL );
162 if( ! Channel_IsValidName( Name )) {
163 IRC_WriteStrClient( Client, ERR_NOSUCHCHANNEL_MSG, Client_ID( Client ), Name );
167 chan = Channel_Search( Name );
169 /* Ist der Client bereits Mitglied? */
170 if( Get_Cl2Chan( chan, Client )) return false;
174 /* Gibt es noch nicht? Dann neu anlegen: */
175 chan = Channel_Create( Name );
176 if (!chan) return false;
179 /* User dem Channel hinzufuegen */
180 if( ! Add_Client( chan, Client )) return false;
186 Channel_Part( CLIENT *Client, CLIENT *Origin, char *Name, char *Reason )
190 assert( Client != NULL );
191 assert( Name != NULL );
192 assert( Reason != NULL );
194 chan = Channel_Search( Name );
195 if(( ! chan ) || ( ! Get_Cl2Chan( chan, Client )))
197 IRC_WriteStrClient( Client, ERR_NOSUCHCHANNEL_MSG, Client_ID( Client ), Name );
201 /* User aus Channel entfernen */
202 if( ! Remove_Client( REMOVE_PART, chan, Client, Origin, Reason, true)) return false;
208 Channel_Kick( CLIENT *Client, CLIENT *Origin, char *Name, char *Reason )
212 assert( Client != NULL );
213 assert( Origin != NULL );
214 assert( Name != NULL );
215 assert( Reason != NULL );
217 chan = Channel_Search( Name );
220 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
224 if( ! Channel_IsMemberOf( chan, Origin ))
226 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Name );
230 /* Is User Channel-Operator? */
231 if( ! strchr( Channel_UserModes( chan, Origin ), 'o' ))
233 IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Name);
237 /* Ist the kickED User member of channel? */
238 if( ! Channel_IsMemberOf( chan, Client ))
240 IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( Client ), Name );
244 Remove_Client( REMOVE_KICK, chan, Client, Origin, Reason, true);
249 Channel_Quit( CLIENT *Client, char *Reason )
253 assert( Client != NULL );
254 assert( Reason != NULL );
256 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
262 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
269 Channel_Count( void )
281 } /* Channel_Count */
285 Channel_MemberCount( CHANNEL *Chan )
290 assert( Chan != NULL );
292 cl2chan = My_Cl2Chan;
295 if( cl2chan->channel == Chan ) count++;
296 cl2chan = cl2chan->next;
299 } /* Channel_MemberCount */
303 Channel_CountForUser( CLIENT *Client )
305 /* Count number of channels a user is member of. */
310 assert( Client != NULL );
312 cl2chan = My_Cl2Chan;
315 if( cl2chan->client == Client ) count++;
316 cl2chan = cl2chan->next;
320 } /* Channel_CountForUser */
324 Channel_PCount( void )
326 /* Count the number of persistent (mode 'P') channels */
334 if( strchr( chan->modes, 'P' )) count++;
339 } /* Channel_PCount */
343 Channel_Name( CHANNEL *Chan )
345 assert( Chan != NULL );
351 Channel_Modes( CHANNEL *Chan )
353 assert( Chan != NULL );
355 } /* Channel_Modes */
359 Channel_Key( CHANNEL *Chan )
361 assert( Chan != NULL );
367 Channel_MaxUsers( CHANNEL *Chan )
369 assert( Chan != NULL );
370 return Chan->maxusers;
371 } /* Channel_MaxUsers */
375 Channel_First( void )
378 } /* Channel_First */
382 Channel_Next( CHANNEL *Chan )
384 assert( Chan != NULL );
390 Channel_Search( char *Name )
392 /* Channel-Struktur suchen */
397 assert( Name != NULL );
399 search_hash = Hash( Name );
403 if( search_hash == c->hash )
405 /* lt. Hash-Wert: Treffer! */
406 if( strcasecmp( Name, c->name ) == 0 ) return c;
411 } /* Channel_Search */
415 Channel_FirstMember( CHANNEL *Chan )
417 assert( Chan != NULL );
418 return Get_First_Cl2Chan( NULL, Chan );
419 } /* Channel_FirstMember */
423 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
425 assert( Chan != NULL );
426 assert( Cl2Chan != NULL );
427 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
428 } /* Channel_NextMember */
432 Channel_FirstChannelOf( CLIENT *Client )
434 assert( Client != NULL );
435 return Get_First_Cl2Chan( Client, NULL );
436 } /* Channel_FirstChannelOf */
440 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
442 assert( Client != NULL );
443 assert( Cl2Chan != NULL );
444 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
445 } /* Channel_NextChannelOf */
449 Channel_GetClient( CL2CHAN *Cl2Chan )
451 assert( Cl2Chan != NULL );
452 return Cl2Chan->client;
453 } /* Channel_GetClient */
457 Channel_GetChannel( CL2CHAN *Cl2Chan )
459 assert( Cl2Chan != NULL );
460 return Cl2Chan->channel;
461 } /* Channel_GetChannel */
465 Channel_IsValidName( char *Name )
467 /* Pruefen, ob Name als Channelname gueltig */
469 char *ptr, badchars[10];
471 assert( Name != NULL );
473 if(( Name[0] != '#' ) || ( strlen( Name ) >= CHANNEL_NAME_LEN )) return false;
476 strcpy( badchars, " ,:\007" );
479 if( strchr( badchars, *ptr )) return false;
484 } /* Channel_IsValidName */
488 Channel_ModeAdd( CHANNEL *Chan, char Mode )
491 * If the channel already had this mode, return false.
492 * If the channel mode was newly set return true.
497 assert( Chan != NULL );
499 x[0] = Mode; x[1] = '\0';
500 if( ! strchr( Chan->modes, x[0] ))
502 /* Channel does not have this mode yet, set it */
503 strlcat( Chan->modes, x, sizeof( Chan->modes ));
507 } /* Channel_ModeAdd */
511 Channel_ModeDel( CHANNEL *Chan, char Mode )
514 * if the mode was removed return true.
515 * if the channel did not have the mode, return false.
519 assert( Chan != NULL );
521 p = strchr( Chan->modes, Mode );
522 if( ! p ) return false;
524 /* Channel has mode -> delete */
531 } /* Channel_ModeDel */
535 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
537 /* Set Channel-User-Mode.
538 * if mode was newly set, return true.
539 * if the User already had this channel-mode, return false.
545 assert( Chan != NULL );
546 assert( Client != NULL );
548 cl2chan = Get_Cl2Chan( Chan, Client );
549 assert( cl2chan != NULL );
551 x[0] = Mode; x[1] = '\0';
552 if( ! strchr( cl2chan->modes, x[0] ))
554 /* mode not set, -> set it */
555 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
559 } /* Channel_UserModeAdd */
563 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
565 /* Delete Channel-User-Mode.
566 * If Mode was removed, return true.
567 * If User did not have the Channel-Mode, return false.
573 assert( Chan != NULL );
574 assert( Client != NULL );
576 cl2chan = Get_Cl2Chan( Chan, Client );
577 assert( cl2chan != NULL );
579 p = strchr( cl2chan->modes, Mode );
580 if( ! p ) return false;
582 /* Client has Mode -> delete */
589 } /* Channel_UserModeDel */
593 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
595 /* return Users' Channel-Modes */
599 assert( Chan != NULL );
600 assert( Client != NULL );
602 cl2chan = Get_Cl2Chan( Chan, Client );
603 assert( cl2chan != NULL );
605 return cl2chan->modes;
606 } /* Channel_UserModes */
610 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
612 /* Test if Client is on Channel Chan */
614 assert( Chan != NULL );
615 assert( Client != NULL );
617 if( Get_Cl2Chan( Chan, Client )) return true;
619 } /* Channel_IsMemberOf */
623 Channel_Topic( CHANNEL *Chan )
626 assert( Chan != NULL );
627 ret = array_start(&Chan->topic);
628 return ret ? ret : "";
629 } /* Channel_Topic */
635 Channel_TopicTime(CHANNEL *Chan)
637 assert(Chan != NULL);
638 return (unsigned int) Chan->topic_time;
639 } /* Channel_TopicTime */
643 Channel_TopicWho(CHANNEL *Chan)
645 assert(Chan != NULL);
646 return Chan->topic_who;
647 } /* Channel_TopicWho */
653 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, char *Topic)
656 assert( Chan != NULL );
657 assert( Topic != NULL );
660 if (len < array_bytes(&Chan->topic))
661 array_free(&Chan->topic);
663 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
664 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
665 Topic, Chan->name, strerror(errno));
667 Chan->topic_time = time(NULL);
668 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
669 strlcpy(Chan->topic_who, Client_ID(Client),
670 sizeof Chan->topic_who);
672 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
673 sizeof Chan->topic_who);
677 } /* Channel_SetTopic */
681 Channel_SetModes( CHANNEL *Chan, char *Modes )
683 assert( Chan != NULL );
684 assert( Modes != NULL );
686 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
687 } /* Channel_SetModes */
691 Channel_SetKey( CHANNEL *Chan, char *Key )
693 assert( Chan != NULL );
694 assert( Key != NULL );
696 strlcpy( Chan->key, Key, sizeof( Chan->key ));
697 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
698 } /* Channel_SetKey */
702 Channel_SetMaxUsers( CHANNEL *Chan, long Count )
704 assert( Chan != NULL );
706 Chan->maxusers = Count;
707 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
708 } /* Channel_SetMaxUsers */
712 Channel_Write( CHANNEL *Chan, CLIENT *From, CLIENT *Client, char *Text )
714 bool is_member, has_voice, is_op, ok;
716 /* Okay, target is a channel */
717 is_member = has_voice = is_op = false;
718 if( Channel_IsMemberOf( Chan, From ))
721 if( strchr( Channel_UserModes( Chan, From ), 'v' )) has_voice = true;
722 if( strchr( Channel_UserModes( Chan, From ), 'o' )) is_op = true;
725 /* Is the client allowed to write to channel? */
727 if( strchr( Channel_Modes( Chan ), 'n' ) && ( ! is_member )) ok = false;
728 if( strchr( Channel_Modes( Chan ), 'm' ) && ( ! is_op ) && ( ! has_voice )) ok = false;
730 /* Is the client banned? */
731 if( Lists_CheckBanned( From, Chan ))
733 /* Client is banned, but is he channel operator or has voice? */
734 if(( ! has_voice ) && ( ! is_op )) ok = false;
737 if( ! ok ) return IRC_WriteStrClient( From, ERR_CANNOTSENDTOCHAN_MSG, Client_ID( From ), Channel_Name( Chan ));
740 if( Client_Conn( From ) > NONE ) Conn_UpdateIdle( Client_Conn( From ));
741 return IRC_WriteStrChannelPrefix( Client, Chan, From, true, "PRIVMSG %s :%s", Channel_Name( Chan ), Text );
742 } /* Channel_Write */
746 Channel_Create( char *Name )
748 /* Create new CHANNEL structure and add it to linked list */
751 assert( Name != NULL );
753 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
756 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
759 memset( c, 0, sizeof( CHANNEL ));
760 strlcpy( c->name, Name, sizeof( c->name ));
761 c->hash = Hash( c->name );
762 c->next = My_Channels;
764 LogDebug("Created new channel structure for \"%s\".", Name);
766 } /* Channel_Create */
770 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
774 assert( Chan != NULL );
775 assert( Client != NULL );
777 cl2chan = My_Cl2Chan;
780 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
781 cl2chan = cl2chan->next;
788 Add_Client( CHANNEL *Chan, CLIENT *Client )
792 assert( Chan != NULL );
793 assert( Client != NULL );
795 /* neue CL2CHAN-Struktur anlegen */
796 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
799 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
802 cl2chan->channel = Chan;
803 cl2chan->client = Client;
804 strcpy( cl2chan->modes, "" );
807 cl2chan->next = My_Cl2Chan;
808 My_Cl2Chan = cl2chan;
810 Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
817 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, char *Reason, bool InformServer )
819 CL2CHAN *cl2chan, *last_cl2chan;
822 assert( Chan != NULL );
823 assert( Client != NULL );
824 assert( Origin != NULL );
825 assert( Reason != NULL );
828 cl2chan = My_Cl2Chan;
831 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
832 last_cl2chan = cl2chan;
833 cl2chan = cl2chan->next;
835 if( ! cl2chan ) return false;
837 c = cl2chan->channel;
840 /* Aus Verkettung loesen und freigeben */
841 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
842 else My_Cl2Chan = cl2chan->next;
848 /* QUIT: other servers have already been notified, see Client_Destroy();
849 * so only inform other clients in same channel. */
850 assert( InformServer == false );
851 LogDebug("User \"%s\" left channel \"%s\" (%s).",
852 Client_Mask( Client ), c->name, Reason );
855 /* User was KICKed: inform other servers and all users in channel */
857 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
858 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
859 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
860 c->name, Client_ID( Client ), Reason );
861 if ((Client_Conn(Client) > NONE) &&
862 (Client_Type(Client) == CLIENT_USER))
864 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
865 c->name, Client_ID( Client ), Reason);
867 LogDebug("User \"%s\" has been kicked of \"%s\" by \"%s\": %s.",
868 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
872 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
874 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
877 if ((Client_Conn(Origin) > NONE) &&
878 (Client_Type(Origin) == CLIENT_USER))
880 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
881 LogDebug("User \"%s\" left channel \"%s\" (%s).",
882 Client_Mask(Client), c->name, Reason);
886 /* Wenn Channel nun leer und nicht pre-defined: loeschen */
887 if( ! strchr( Channel_Modes( Chan ), 'P' ))
889 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
893 } /* Remove_Client */
897 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
899 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
900 } /* Get_First_Cl2Chan */
904 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
908 assert( Client != NULL || Channel != NULL );
913 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
914 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
915 cl2chan = cl2chan->next;
918 } /* Get_Next_Cl2Chan */
922 Delete_Channel( CHANNEL *Chan )
924 /* Channel-Struktur loeschen */
926 CHANNEL *chan, *last_chan;
932 if( chan == Chan ) break;
936 if( ! chan ) return false;
938 Log( LOG_DEBUG, "Freed channel structure for \"%s\".", Chan->name );
940 /* Invite- und Ban-Lists aufraeumen */
941 Lists_DeleteChannel( chan );
943 /* Neu verketten und freigeben */
944 if( last_chan ) last_chan->next = chan->next;
945 else My_Channels = chan->next;
949 } /* Delete_Channel */