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.59 2006/10/06 21:32:58 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 )
272 unsigned long count = 0;
281 } /* Channel_Count */
285 Channel_MemberCount( CHANNEL *Chan )
288 unsigned long count = 0;
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( const 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( const char *Name )
467 assert( Name != NULL );
469 if(( Name[0] != '#' ) || ( strlen( Name ) >= CHANNEL_NAME_LEN )) return false;
471 return Name[strcspn(Name, " ,:\007")] == 0;
472 } /* Channel_IsValidName */
476 Channel_ModeAdd( CHANNEL *Chan, char Mode )
479 * If the channel already had this mode, return false.
480 * If the channel mode was newly set return true.
485 assert( Chan != NULL );
487 x[0] = Mode; x[1] = '\0';
488 if( ! strchr( Chan->modes, x[0] ))
490 /* Channel does not have this mode yet, set it */
491 strlcat( Chan->modes, x, sizeof( Chan->modes ));
495 } /* Channel_ModeAdd */
499 Channel_ModeDel( CHANNEL *Chan, char Mode )
502 * if the mode was removed return true.
503 * if the channel did not have the mode, return false.
507 assert( Chan != NULL );
509 p = strchr( Chan->modes, Mode );
510 if( ! p ) return false;
512 /* Channel has mode -> delete */
519 } /* Channel_ModeDel */
523 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
525 /* Set Channel-User-Mode.
526 * if mode was newly set, return true.
527 * if the User already had this channel-mode, return false.
533 assert( Chan != NULL );
534 assert( Client != NULL );
536 cl2chan = Get_Cl2Chan( Chan, Client );
537 assert( cl2chan != NULL );
539 x[0] = Mode; x[1] = '\0';
540 if( ! strchr( cl2chan->modes, x[0] ))
542 /* mode not set, -> set it */
543 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
547 } /* Channel_UserModeAdd */
551 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
553 /* Delete Channel-User-Mode.
554 * If Mode was removed, return true.
555 * If User did not have the Channel-Mode, return false.
561 assert( Chan != NULL );
562 assert( Client != NULL );
564 cl2chan = Get_Cl2Chan( Chan, Client );
565 assert( cl2chan != NULL );
567 p = strchr( cl2chan->modes, Mode );
568 if( ! p ) return false;
570 /* Client has Mode -> delete */
577 } /* Channel_UserModeDel */
581 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
583 /* return Users' Channel-Modes */
587 assert( Chan != NULL );
588 assert( Client != NULL );
590 cl2chan = Get_Cl2Chan( Chan, Client );
591 assert( cl2chan != NULL );
593 return cl2chan->modes;
594 } /* Channel_UserModes */
598 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
600 /* Test if Client is on Channel Chan */
602 assert( Chan != NULL );
603 assert( Client != NULL );
605 if( Get_Cl2Chan( Chan, Client )) return true;
607 } /* Channel_IsMemberOf */
611 Channel_Topic( CHANNEL *Chan )
614 assert( Chan != NULL );
615 ret = array_start(&Chan->topic);
616 return ret ? ret : "";
617 } /* Channel_Topic */
623 Channel_TopicTime(CHANNEL *Chan)
625 assert(Chan != NULL);
626 return (unsigned int) Chan->topic_time;
627 } /* Channel_TopicTime */
631 Channel_TopicWho(CHANNEL *Chan)
633 assert(Chan != NULL);
634 return Chan->topic_who;
635 } /* Channel_TopicWho */
641 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, char *Topic)
644 assert( Chan != NULL );
645 assert( Topic != NULL );
648 if (len < array_bytes(&Chan->topic))
649 array_free(&Chan->topic);
651 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
652 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
653 Topic, Chan->name, strerror(errno));
655 Chan->topic_time = time(NULL);
656 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
657 strlcpy(Chan->topic_who, Client_ID(Client),
658 sizeof Chan->topic_who);
660 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
661 sizeof Chan->topic_who);
665 } /* Channel_SetTopic */
669 Channel_SetModes( CHANNEL *Chan, char *Modes )
671 assert( Chan != NULL );
672 assert( Modes != NULL );
674 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
675 } /* Channel_SetModes */
679 Channel_SetKey( CHANNEL *Chan, char *Key )
681 assert( Chan != NULL );
682 assert( Key != NULL );
684 strlcpy( Chan->key, Key, sizeof( Chan->key ));
685 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
686 } /* Channel_SetKey */
690 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
692 assert( Chan != NULL );
694 Chan->maxusers = Count;
695 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
696 } /* Channel_SetMaxUsers */
700 Channel_Write( CHANNEL *Chan, CLIENT *From, CLIENT *Client, char *Text )
702 bool is_member, has_voice, is_op, ok;
704 /* Okay, target is a channel */
705 is_member = has_voice = is_op = false;
706 if( Channel_IsMemberOf( Chan, From ))
709 if( strchr( Channel_UserModes( Chan, From ), 'v' )) has_voice = true;
710 if( strchr( Channel_UserModes( Chan, From ), 'o' )) is_op = true;
713 /* Is the client allowed to write to channel? */
715 if( strchr( Channel_Modes( Chan ), 'n' ) && ( ! is_member )) ok = false;
716 if( strchr( Channel_Modes( Chan ), 'm' ) && ( ! is_op ) && ( ! has_voice )) ok = false;
718 /* Is the client banned? */
719 if( Lists_CheckBanned( From, Chan ))
721 /* Client is banned, but is he channel operator or has voice? */
722 if(( ! has_voice ) && ( ! is_op )) ok = false;
725 if( ! ok ) return IRC_WriteStrClient( From, ERR_CANNOTSENDTOCHAN_MSG, Client_ID( From ), Channel_Name( Chan ));
728 if( Client_Conn( From ) > NONE ) Conn_UpdateIdle( Client_Conn( From ));
729 return IRC_WriteStrChannelPrefix( Client, Chan, From, true, "PRIVMSG %s :%s", Channel_Name( Chan ), Text );
730 } /* Channel_Write */
734 Channel_Create( char *Name )
736 /* Create new CHANNEL structure and add it to linked list */
739 assert( Name != NULL );
741 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
744 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
747 memset( c, 0, sizeof( CHANNEL ));
748 strlcpy( c->name, Name, sizeof( c->name ));
749 c->hash = Hash( c->name );
750 c->next = My_Channels;
752 LogDebug("Created new channel structure for \"%s\".", Name);
754 } /* Channel_Create */
758 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
762 assert( Chan != NULL );
763 assert( Client != NULL );
765 cl2chan = My_Cl2Chan;
768 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
769 cl2chan = cl2chan->next;
776 Add_Client( CHANNEL *Chan, CLIENT *Client )
780 assert( Chan != NULL );
781 assert( Client != NULL );
783 /* neue CL2CHAN-Struktur anlegen */
784 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
787 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
790 cl2chan->channel = Chan;
791 cl2chan->client = Client;
792 strcpy( cl2chan->modes, "" );
795 cl2chan->next = My_Cl2Chan;
796 My_Cl2Chan = cl2chan;
798 Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
805 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, char *Reason, bool InformServer )
807 CL2CHAN *cl2chan, *last_cl2chan;
810 assert( Chan != NULL );
811 assert( Client != NULL );
812 assert( Origin != NULL );
813 assert( Reason != NULL );
816 cl2chan = My_Cl2Chan;
819 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
820 last_cl2chan = cl2chan;
821 cl2chan = cl2chan->next;
823 if( ! cl2chan ) return false;
825 c = cl2chan->channel;
828 /* Aus Verkettung loesen und freigeben */
829 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
830 else My_Cl2Chan = cl2chan->next;
836 /* QUIT: other servers have already been notified, see Client_Destroy();
837 * so only inform other clients in same channel. */
838 assert( InformServer == false );
839 LogDebug("User \"%s\" left channel \"%s\" (%s).",
840 Client_Mask( Client ), c->name, Reason );
843 /* User was KICKed: inform other servers and all users in channel */
845 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
846 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
847 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
848 c->name, Client_ID( Client ), Reason );
849 if ((Client_Conn(Client) > NONE) &&
850 (Client_Type(Client) == CLIENT_USER))
852 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
853 c->name, Client_ID( Client ), Reason);
855 LogDebug("User \"%s\" has been kicked of \"%s\" by \"%s\": %s.",
856 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
860 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
862 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
865 if ((Client_Conn(Origin) > NONE) &&
866 (Client_Type(Origin) == CLIENT_USER))
868 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
869 LogDebug("User \"%s\" left channel \"%s\" (%s).",
870 Client_Mask(Client), c->name, Reason);
874 /* Wenn Channel nun leer und nicht pre-defined: loeschen */
875 if( ! strchr( Channel_Modes( Chan ), 'P' ))
877 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
881 } /* Remove_Client */
885 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
887 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
888 } /* Get_First_Cl2Chan */
892 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
896 assert( Client != NULL || Channel != NULL );
901 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
902 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
903 cl2chan = cl2chan->next;
906 } /* Get_Next_Cl2Chan */
910 Delete_Channel( CHANNEL *Chan )
912 /* Channel-Struktur loeschen */
914 CHANNEL *chan, *last_chan;
920 if( chan == Chan ) break;
924 if( ! chan ) return false;
926 Log( LOG_DEBUG, "Freed channel structure for \"%s\".", Chan->name );
928 /* Invite- und Ban-Lists aufraeumen */
929 Lists_DeleteChannel( chan );
931 /* Neu verketten und freigeben */
932 if( last_chan ) last_chan->next = chan->next;
933 else My_Channels = chan->next;
937 } /* Delete_Channel */