2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2008 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.
28 #include "conn-func.h"
35 #include "irc-write.h"
51 static CHANNEL *My_Channels;
52 static CL2CHAN *My_Cl2Chan;
55 static CL2CHAN *Get_Cl2Chan PARAMS(( CHANNEL *Chan, CLIENT *Client ));
56 static CL2CHAN *Add_Client PARAMS(( CHANNEL *Chan, CLIENT *Client ));
57 static bool Remove_Client PARAMS(( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer ));
58 static CL2CHAN *Get_First_Cl2Chan PARAMS(( CLIENT *Client, CHANNEL *Chan ));
59 static CL2CHAN *Get_Next_Cl2Chan PARAMS(( CL2CHAN *Start, CLIENT *Client, CHANNEL *Chan ));
60 static void Delete_Channel PARAMS(( CHANNEL *Chan ));
61 static void Channel_Free PARAMS(( CHANNEL *Chan ));
72 sc = Channel_Create("&SERVER");
74 Channel_SetModes(sc, "mnPt");
75 Channel_SetTopic(sc, Client_ThisServer(), "Server Messages");
80 GLOBAL struct list_head *
81 Channel_GetListBans(CHANNEL *c)
88 GLOBAL struct list_head *
89 Channel_GetListInvites(CHANNEL *c)
92 return &c->list_invites;
97 Channel_InitPredefined( void )
99 /* Generate predefined persistent channels */
102 const struct Conf_Channel *conf_chan;
104 size_t i, channel_count = array_length(&Conf_Channels, sizeof(*conf_chan));
106 conf_chan = array_start(&Conf_Channels);
108 assert(channel_count == 0 || conf_chan != NULL);
110 for (i = 0; i < channel_count; i++, conf_chan++) {
111 if (!conf_chan->name[0] || !Channel_IsValidName(conf_chan->name)) {
112 Log(LOG_ERR, "Can't create pre-defined channel: invalid name: \"%s\"",
117 new_chan = Channel_Search(conf_chan->name);
119 Log(LOG_INFO, "Can't create pre-defined channel \"%s\": name already in use.",
124 new_chan = Channel_Create(conf_chan->name);
126 Log(LOG_ERR, "Can't create pre-defined channel \"%s\"",
131 Channel_ModeAdd(new_chan, 'P');
133 if (conf_chan->topic[0])
134 Channel_SetTopic(new_chan, NULL, conf_chan->topic);
136 c = conf_chan->modes;
138 Channel_ModeAdd(new_chan, *c++);
140 Channel_SetKey(new_chan, conf_chan->key);
141 Channel_SetMaxUsers(new_chan, conf_chan->maxusers);
142 Log(LOG_INFO, "Created pre-defined channel \"%s\"",
146 array_free(&Conf_Channels);
147 } /* Channel_InitPredefined */
151 Channel_Free(CHANNEL *chan)
153 array_free(&chan->topic);
154 Lists_Free(&chan->list_bans);
155 Lists_Free(&chan->list_invites);
165 CL2CHAN *cl2chan, *cl2chan_next;
167 /* free struct Channel */
175 /* Free Channel allocation table */
176 cl2chan = My_Cl2Chan;
179 cl2chan_next = cl2chan->next;
181 cl2chan = cl2chan_next;
188 * This function lets a client join a channel. First, the function
189 * checks that the specified channel name is valid and that the client
190 * isn't already a member. If the specified channel doesn't exist,
191 * a new channel is created. Client is added to channel by function
195 Channel_Join( CLIENT *Client, char *Name )
199 assert(Client != NULL);
200 assert(Name != NULL);
202 /* Check that the channel name is valid */
203 if (! Channel_IsValidName(Name)) {
204 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
205 Client_ID(Client), Name);
209 chan = Channel_Search(Name);
211 /* Check if the client is already in the channel */
212 if (Get_Cl2Chan(chan, Client))
215 /* If the specified channel does not exist, the channel
217 chan = Channel_Create(Name);
222 /* Add user to Channel */
223 if (! Add_Client(chan, Client))
231 * Part client from channel.
232 * This function lets a client part from a channel. First, the function checks
233 * if the channel exists and the client is a member of it and sends out
234 * appropriate error messages if not. The real work is done by the function
238 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
242 assert(Client != NULL);
243 assert(Name != NULL);
244 assert(Reason != NULL);
246 /* Check that specified channel exists */
247 chan = Channel_Search(Name);
249 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
250 Client_ID(Client), Name);
254 /* Check that the client is in the channel */
255 if (!Get_Cl2Chan(chan, Client)) {
256 IRC_WriteStrClient(Client, ERR_NOTONCHANNEL_MSG,
257 Client_ID(Client), Name);
261 /* Part client from channel */
262 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
270 * Kick user from Channel
273 Channel_Kick(CLIENT *Peer, CLIENT *Target, CLIENT *Origin, const char *Name,
278 assert(Peer != NULL);
279 assert(Target != NULL);
280 assert(Origin != NULL);
281 assert(Name != NULL);
282 assert(Reason != NULL);
284 /* Check that channel exists */
285 chan = Channel_Search( Name );
288 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
292 if (Client_Type(Peer) != CLIENT_SERVER &&
293 Client_Type(Origin) != CLIENT_SERVICE) {
294 /* Check that user is on the specified channel */
295 if (!Channel_IsMemberOf(chan, Origin)) {
296 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG,
297 Client_ID(Origin), Name);
301 /* Check if user has operator status */
302 if (!strchr(Channel_UserModes(chan, Origin), 'o')) {
303 IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG,
304 Client_ID(Origin), Name);
309 /* Check that the client to be kicked is on the specified channel */
310 if (!Channel_IsMemberOf(chan, Target)) {
311 IRC_WriteStrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
312 Client_ID(Origin), Client_ID(Target), Name );
316 /* Kick Client from channel */
317 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
322 Channel_Quit( CLIENT *Client, char *Reason )
326 assert( Client != NULL );
327 assert( Reason != NULL );
329 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
335 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
342 Channel_Count( void )
345 unsigned long count = 0;
354 } /* Channel_Count */
358 Channel_MemberCount( CHANNEL *Chan )
361 unsigned long count = 0;
363 assert( Chan != NULL );
365 cl2chan = My_Cl2Chan;
368 if( cl2chan->channel == Chan ) count++;
369 cl2chan = cl2chan->next;
372 } /* Channel_MemberCount */
376 Channel_CountForUser( CLIENT *Client )
378 /* Count number of channels a user is member of. */
383 assert( Client != NULL );
385 cl2chan = My_Cl2Chan;
388 if( cl2chan->client == Client ) count++;
389 cl2chan = cl2chan->next;
393 } /* Channel_CountForUser */
397 Channel_Name( const CHANNEL *Chan )
399 assert( Chan != NULL );
405 Channel_Modes( CHANNEL *Chan )
407 assert( Chan != NULL );
409 } /* Channel_Modes */
413 Channel_Key( CHANNEL *Chan )
415 assert( Chan != NULL );
421 Channel_MaxUsers( CHANNEL *Chan )
423 assert( Chan != NULL );
424 return Chan->maxusers;
425 } /* Channel_MaxUsers */
429 Channel_First( void )
432 } /* Channel_First */
436 Channel_Next( CHANNEL *Chan )
438 assert( Chan != NULL );
444 Channel_Search( const char *Name )
446 /* Search channel structure */
451 assert( Name != NULL );
453 search_hash = Hash( Name );
457 if( search_hash == c->hash )
460 if( strcasecmp( Name, c->name ) == 0 ) return c;
465 } /* Channel_Search */
469 Channel_FirstMember( CHANNEL *Chan )
471 assert( Chan != NULL );
472 return Get_First_Cl2Chan( NULL, Chan );
473 } /* Channel_FirstMember */
477 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
479 assert( Chan != NULL );
480 assert( Cl2Chan != NULL );
481 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
482 } /* Channel_NextMember */
486 Channel_FirstChannelOf( CLIENT *Client )
488 assert( Client != NULL );
489 return Get_First_Cl2Chan( Client, NULL );
490 } /* Channel_FirstChannelOf */
494 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
496 assert( Client != NULL );
497 assert( Cl2Chan != NULL );
498 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
499 } /* Channel_NextChannelOf */
503 Channel_GetClient( CL2CHAN *Cl2Chan )
505 assert( Cl2Chan != NULL );
506 return Cl2Chan->client;
507 } /* Channel_GetClient */
511 Channel_GetChannel( CL2CHAN *Cl2Chan )
513 assert( Cl2Chan != NULL );
514 return Cl2Chan->channel;
515 } /* Channel_GetChannel */
519 Channel_IsValidName( const char *Name )
521 assert( Name != NULL );
524 if (strlen(Name) <= 1)
527 if (strchr("#&+", Name[0]) == NULL)
529 if (strlen(Name) >= CHANNEL_NAME_LEN)
532 return Name[strcspn(Name, " ,:\007")] == 0;
533 } /* Channel_IsValidName */
537 Channel_ModeAdd( CHANNEL *Chan, char Mode )
540 * If the channel already had this mode, return false.
541 * If the channel mode was newly set return true.
546 assert( Chan != NULL );
548 x[0] = Mode; x[1] = '\0';
549 if( ! strchr( Chan->modes, x[0] ))
551 /* Channel does not have this mode yet, set it */
552 strlcat( Chan->modes, x, sizeof( Chan->modes ));
556 } /* Channel_ModeAdd */
560 Channel_ModeDel( CHANNEL *Chan, char Mode )
563 * if the mode was removed return true.
564 * if the channel did not have the mode, return false.
568 assert( Chan != NULL );
570 p = strchr( Chan->modes, Mode );
571 if( ! p ) return false;
573 /* Channel has mode -> delete */
580 } /* Channel_ModeDel */
584 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
586 /* Set Channel-User-Mode.
587 * if mode was newly set, return true.
588 * if the User already had this channel-mode, return false.
594 assert( Chan != NULL );
595 assert( Client != NULL );
597 cl2chan = Get_Cl2Chan( Chan, Client );
598 assert( cl2chan != NULL );
600 x[0] = Mode; x[1] = '\0';
601 if( ! strchr( cl2chan->modes, x[0] ))
603 /* mode not set, -> set it */
604 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
608 } /* Channel_UserModeAdd */
612 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
614 /* Delete Channel-User-Mode.
615 * If Mode was removed, return true.
616 * If User did not have the Channel-Mode, return false.
622 assert( Chan != NULL );
623 assert( Client != NULL );
625 cl2chan = Get_Cl2Chan( Chan, Client );
626 assert( cl2chan != NULL );
628 p = strchr( cl2chan->modes, Mode );
629 if( ! p ) return false;
631 /* Client has Mode -> delete */
638 } /* Channel_UserModeDel */
642 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
644 /* return Users' Channel-Modes */
648 assert( Chan != NULL );
649 assert( Client != NULL );
651 cl2chan = Get_Cl2Chan( Chan, Client );
652 assert( cl2chan != NULL );
654 return cl2chan->modes;
655 } /* Channel_UserModes */
659 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
661 /* Test if Client is on Channel Chan */
663 assert( Chan != NULL );
664 assert( Client != NULL );
665 return Get_Cl2Chan(Chan, Client) != NULL;
666 } /* Channel_IsMemberOf */
670 Channel_Topic( CHANNEL *Chan )
673 assert( Chan != NULL );
674 ret = array_start(&Chan->topic);
675 return ret ? ret : "";
676 } /* Channel_Topic */
682 Channel_TopicTime(CHANNEL *Chan)
684 assert(Chan != NULL);
685 return (unsigned int) Chan->topic_time;
686 } /* Channel_TopicTime */
690 Channel_TopicWho(CHANNEL *Chan)
692 assert(Chan != NULL);
693 return Chan->topic_who;
694 } /* Channel_TopicWho */
700 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
703 assert( Chan != NULL );
704 assert( Topic != NULL );
707 if (len < array_bytes(&Chan->topic))
708 array_free(&Chan->topic);
710 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
711 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
712 Topic, Chan->name, strerror(errno));
714 Chan->topic_time = time(NULL);
715 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
716 strlcpy(Chan->topic_who, Client_ID(Client),
717 sizeof Chan->topic_who);
719 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
720 sizeof Chan->topic_who);
724 } /* Channel_SetTopic */
728 Channel_SetModes( CHANNEL *Chan, char *Modes )
730 assert( Chan != NULL );
731 assert( Modes != NULL );
733 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
734 } /* Channel_SetModes */
738 Channel_SetKey( CHANNEL *Chan, const char *Key )
740 assert( Chan != NULL );
741 assert( Key != NULL );
743 strlcpy( Chan->key, Key, sizeof( Chan->key ));
744 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
745 } /* Channel_SetKey */
749 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
751 assert( Chan != NULL );
753 Chan->maxusers = Count;
754 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
755 } /* Channel_SetMaxUsers */
759 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
761 bool is_member, has_voice, is_op;
763 is_member = has_voice = is_op = false;
765 /* The server itself always can send messages :-) */
766 if (Client_ThisServer() == From)
769 if (Channel_IsMemberOf(Chan, From)) {
771 if (strchr(Channel_UserModes(Chan, From), 'v'))
773 if (strchr(Channel_UserModes(Chan, From), 'o'))
778 * Is the client allowed to write to channel?
780 * If channel mode n set: non-members cannot send to channel.
781 * If channel mode m set: need voice.
783 if (strchr(Channel_Modes(Chan), 'n') && !is_member)
786 if (is_op || has_voice)
789 if (strchr(Channel_Modes(Chan), 'm'))
792 return !Lists_Check(&Chan->list_bans, From);
797 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
798 bool SendErrors, const char *Text)
800 if (!Can_Send_To_Channel(Chan, From)) {
802 return CONNECTED; /* no error, see RFC 2812 */
803 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
804 Client_ID(From), Channel_Name(Chan));
807 if (Client_Conn(From) > NONE)
808 Conn_UpdateIdle(Client_Conn(From));
810 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
811 "%s %s :%s", Command, Channel_Name(Chan), Text);
816 Channel_Create( const char *Name )
818 /* Create new CHANNEL structure and add it to linked list */
821 assert( Name != NULL );
823 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
826 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
829 memset( c, 0, sizeof( CHANNEL ));
830 strlcpy( c->name, Name, sizeof( c->name ));
831 c->hash = Hash( c->name );
832 c->next = My_Channels;
834 LogDebug("Created new channel structure for \"%s\".", Name);
836 } /* Channel_Create */
840 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
844 assert( Chan != NULL );
845 assert( Client != NULL );
847 cl2chan = My_Cl2Chan;
850 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
851 cl2chan = cl2chan->next;
858 Add_Client( CHANNEL *Chan, CLIENT *Client )
862 assert( Chan != NULL );
863 assert( Client != NULL );
865 /* Create new CL2CHAN structure */
866 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
869 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
872 cl2chan->channel = Chan;
873 cl2chan->client = Client;
874 strcpy( cl2chan->modes, "" );
877 cl2chan->next = My_Cl2Chan;
878 My_Cl2Chan = cl2chan;
880 Log( LOG_DEBUG, "User \"%s\" joined channel \"%s\".", Client_Mask( Client ), Chan->name );
887 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
889 CL2CHAN *cl2chan, *last_cl2chan;
892 assert( Chan != NULL );
893 assert( Client != NULL );
894 assert( Origin != NULL );
895 assert( Reason != NULL );
897 /* Do not inform other servers if the channel is local to this server,
898 * regardless of what the caller requested! */
900 InformServer = !Channel_IsLocal(Chan);
903 cl2chan = My_Cl2Chan;
906 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
907 last_cl2chan = cl2chan;
908 cl2chan = cl2chan->next;
910 if( ! cl2chan ) return false;
912 c = cl2chan->channel;
915 /* maintain cl2chan list */
916 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
917 else My_Cl2Chan = cl2chan->next;
923 /* QUIT: other servers have already been notified,
924 * see Client_Destroy(); so only inform other clients
925 * in same channel. */
926 assert( InformServer == false );
927 LogDebug("User \"%s\" left channel \"%s\" (%s).",
928 Client_Mask( Client ), c->name, Reason );
931 /* User was KICKed: inform other servers (public
932 * channels) and all users in the channel */
934 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
935 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
936 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
937 c->name, Client_ID( Client ), Reason );
938 if ((Client_Conn(Client) > NONE) &&
939 (Client_Type(Client) == CLIENT_USER))
941 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
942 c->name, Client_ID( Client ), Reason);
944 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
945 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
949 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
951 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
954 if ((Client_Conn(Origin) > NONE) &&
955 (Client_Type(Origin) == CLIENT_USER))
957 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
958 LogDebug("User \"%s\" left channel \"%s\" (%s).",
959 Client_Mask(Client), c->name, Reason);
963 /* When channel is empty and is not pre-defined, delete */
964 if( ! strchr( Channel_Modes( Chan ), 'P' ))
966 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
970 } /* Remove_Client */
974 Channel_AddBan(CHANNEL *c, const char *mask )
976 struct list_head *h = Channel_GetListBans(c);
977 return Lists_Add(h, mask, false);
982 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
984 struct list_head *h = Channel_GetListInvites(c);
985 return Lists_Add(h, mask, onlyonce);
990 ShowInvitesBans(struct list_head *head, CLIENT *Client, CHANNEL *Channel, bool invite)
993 char *msg = invite ? RPL_INVITELIST_MSG : RPL_BANLIST_MSG;
996 assert( Client != NULL );
997 assert( Channel != NULL );
999 e = Lists_GetFirst(head);
1001 if( ! IRC_WriteStrClient( Client, msg, Client_ID( Client ),
1002 Channel_Name( Channel ), Lists_GetMask(e) )) return DISCONNECTED;
1003 e = Lists_GetNext(e);
1006 msg_end = invite ? RPL_ENDOFINVITELIST_MSG : RPL_ENDOFBANLIST_MSG;
1007 return IRC_WriteStrClient( Client, msg_end, Client_ID( Client ), Channel_Name( Channel ));
1012 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1014 struct list_head *h;
1016 assert( Channel != NULL );
1018 h = Channel_GetListBans(Channel);
1019 return ShowInvitesBans(h, Client, Channel, false);
1024 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1026 struct list_head *h;
1028 assert( Channel != NULL );
1030 h = Channel_GetListInvites(Channel);
1031 return ShowInvitesBans(h, Client, Channel, true);
1036 * Log a message to the local &SERVER channel, if it exists.
1039 Channel_LogServer(char *msg)
1044 assert(msg != NULL);
1046 sc = Channel_Search("&SERVER");
1050 c = Client_ThisServer();
1051 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1052 } /* Channel_LogServer */
1056 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1058 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1059 } /* Get_First_Cl2Chan */
1063 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1067 assert( Client != NULL || Channel != NULL );
1072 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1073 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1074 cl2chan = cl2chan->next;
1077 } /* Get_Next_Cl2Chan */
1081 * Remove a channel and free all of its data structures.
1084 Delete_Channel(CHANNEL *Chan)
1086 CHANNEL *chan, *last_chan;
1097 assert(chan != NULL);
1101 /* maintain channel list */
1103 last_chan->next = chan->next;
1105 My_Channels = chan->next;
1107 LogDebug("Freed channel structure for \"%s\".", Chan->name);
1109 } /* Delete_Channel */