2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
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.
29 #include "conn-func.h"
33 #include "irc-write.h"
44 static CHANNEL *My_Channels;
45 static CL2CHAN *My_Cl2Chan;
47 static CL2CHAN *Get_Cl2Chan PARAMS(( CHANNEL *Chan, CLIENT *Client ));
48 static CL2CHAN *Add_Client PARAMS(( CHANNEL *Chan, CLIENT *Client ));
49 static bool Remove_Client PARAMS(( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer ));
50 static CL2CHAN *Get_First_Cl2Chan PARAMS(( CLIENT *Client, CHANNEL *Chan ));
51 static CL2CHAN *Get_Next_Cl2Chan PARAMS(( CL2CHAN *Start, CLIENT *Client, CHANNEL *Chan ));
52 static void Delete_Channel PARAMS(( CHANNEL *Chan ));
53 static void Free_Channel PARAMS(( CHANNEL *Chan ));
54 static void Set_KeyFile PARAMS((CHANNEL *Chan, const char *KeyFile));
65 GLOBAL struct list_head *
66 Channel_GetListBans(CHANNEL *c)
73 GLOBAL struct list_head *
74 Channel_GetListExcepts(CHANNEL *c)
77 return &c->list_excepts;
81 GLOBAL struct list_head *
82 Channel_GetListInvites(CHANNEL *c)
85 return &c->list_invites;
90 * Generate predefined persistent channels and &SERVER
93 Channel_InitPredefined( void )
96 const struct Conf_Channel *conf_chan;
98 size_t i, channel_count = array_length(&Conf_Channels, sizeof(*conf_chan));
100 conf_chan = array_start(&Conf_Channels);
102 assert(channel_count == 0 || conf_chan != NULL);
104 for (i = 0; i < channel_count; i++, conf_chan++) {
105 if (!conf_chan->name[0])
107 if (!Channel_IsValidName(conf_chan->name)) {
109 "Can't create pre-defined channel: invalid name: \"%s\"",
114 new_chan = Channel_Search(conf_chan->name);
117 "Can't create pre-defined channel \"%s\": name already in use.",
119 Set_KeyFile(new_chan, conf_chan->keyfile);
123 new_chan = Channel_Create(conf_chan->name);
125 Log(LOG_ERR, "Can't create pre-defined channel \"%s\"!",
129 Log(LOG_INFO, "Created pre-defined channel \"%s\".",
132 Channel_ModeAdd(new_chan, 'P');
134 if (conf_chan->topic[0])
135 Channel_SetTopic(new_chan, NULL, conf_chan->topic);
137 c = conf_chan->modes;
139 Channel_ModeAdd(new_chan, *c++);
141 Channel_SetKey(new_chan, conf_chan->key);
142 Channel_SetMaxUsers(new_chan, conf_chan->maxusers);
143 Set_KeyFile(new_chan, conf_chan->keyfile);
146 array_free(&Conf_Channels);
148 /* Make sure the local &SERVER channel exists */
149 if (!Channel_Search("&SERVER")) {
150 new_chan = Channel_Create("&SERVER");
152 Channel_SetModes(new_chan, "mnPt");
153 Channel_SetTopic(new_chan, Client_ThisServer(),
156 Log(LOG_ERR, "Failed to create \"&SERVER\" channel!");
158 LogDebug("Required channel \"&SERVER\" already exists, ok.");
159 } /* Channel_InitPredefined */
163 Free_Channel(CHANNEL *chan)
165 array_free(&chan->topic);
166 array_free(&chan->keyfile);
167 Lists_Free(&chan->list_bans);
168 Lists_Free(&chan->list_excepts);
169 Lists_Free(&chan->list_invites);
179 CL2CHAN *cl2chan, *cl2chan_next;
181 /* free struct Channel */
189 /* Free Channel allocation table */
190 cl2chan = My_Cl2Chan;
192 cl2chan_next = cl2chan->next;
194 cl2chan = cl2chan_next;
201 * This function lets a client join a channel. First, the function
202 * checks that the specified channel name is valid and that the client
203 * isn't already a member. If the specified channel doesn't exist,
204 * a new channel is created. Client is added to channel by function
208 Channel_Join( CLIENT *Client, const char *Name )
212 assert(Client != NULL);
213 assert(Name != NULL);
215 /* Check that the channel name is valid */
216 if (! Channel_IsValidName(Name)) {
217 IRC_WriteErrClient(Client, ERR_NOSUCHCHANNEL_MSG,
218 Client_ID(Client), Name);
222 chan = Channel_Search(Name);
224 /* Check if the client is already in the channel */
225 if (Get_Cl2Chan(chan, Client))
228 /* If the specified channel does not exist, the channel
230 chan = Channel_Create(Name);
235 /* Add user to Channel */
236 if (! Add_Client(chan, Client))
244 * Part client from channel.
245 * This function lets a client part from a channel. First, the function checks
246 * if the channel exists and the client is a member of it and sends out
247 * appropriate error messages if not. The real work is done by the function
251 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
255 assert(Client != NULL);
256 assert(Name != NULL);
257 assert(Reason != NULL);
259 /* Check that specified channel exists */
260 chan = Channel_Search(Name);
262 IRC_WriteErrClient(Client, ERR_NOSUCHCHANNEL_MSG,
263 Client_ID(Client), Name);
267 /* Check that the client is in the channel */
268 if (!Get_Cl2Chan(chan, Client)) {
269 IRC_WriteErrClient(Client, ERR_NOTONCHANNEL_MSG,
270 Client_ID(Client), Name);
274 if (Conf_MorePrivacy)
277 /* Part client from channel */
278 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
286 * Kick user from Channel
289 Channel_Kick(CLIENT *Peer, CLIENT *Target, CLIENT *Origin, const char *Name,
293 bool can_kick = false;
295 assert(Peer != NULL);
296 assert(Target != NULL);
297 assert(Origin != NULL);
298 assert(Name != NULL);
299 assert(Reason != NULL);
301 /* Check that channel exists */
302 chan = Channel_Search( Name );
304 IRC_WriteErrClient(Origin, ERR_NOSUCHCHANNEL_MSG,
305 Client_ID(Origin), Name);
309 if (Client_Type(Peer) != CLIENT_SERVER &&
310 Client_Type(Origin) != CLIENT_SERVICE) {
311 /* Check that user is on the specified channel */
312 if (!Channel_IsMemberOf(chan, Origin)) {
313 IRC_WriteErrClient(Origin, ERR_NOTONCHANNEL_MSG,
314 Client_ID(Origin), Name);
319 /* Check that the client to be kicked is on the specified channel */
320 if (!Channel_IsMemberOf(chan, Target)) {
321 IRC_WriteErrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
322 Client_ID(Origin), Client_ID(Target), Name );
326 if(Client_Type(Peer) == CLIENT_USER) {
327 /* Channel mode 'Q' and user mode 'q' on target: nobody but
328 * IRC Operators and servers can kick the target user */
329 if ((Channel_HasMode(chan, 'Q')
330 || Client_HasMode(Target, 'q')
331 || Client_Type(Target) == CLIENT_SERVICE)
332 && !Client_HasMode(Origin, 'o')) {
333 IRC_WriteErrClient(Origin, ERR_KICKDENY_MSG,
334 Client_ID(Origin), Name,
339 /* Check if client has the rights to kick target */
341 /* Owner can kick everyone */
342 if (Channel_UserHasMode(chan, Peer, 'q'))
345 /* Admin can't kick owner */
346 else if (Channel_UserHasMode(chan, Peer, 'a') &&
347 !Channel_UserHasMode(chan, Target, 'q'))
350 /* Op can't kick owner | admin */
351 else if (Channel_UserHasMode(chan, Peer, 'o') &&
352 !Channel_UserHasMode(chan, Target, 'q') &&
353 !Channel_UserHasMode(chan, Target, 'a'))
356 /* Half Op can't kick owner | admin | op */
357 else if (Channel_UserHasMode(chan, Peer, 'h') &&
358 !Channel_UserHasMode(chan, Target, 'q') &&
359 !Channel_UserHasMode(chan, Target, 'a') &&
360 !Channel_UserHasMode(chan, Target, 'o'))
363 /* IRC operators & IRCd with OperCanMode enabled
364 * can kick anyways regardless of privilege */
365 else if(Client_HasMode(Origin, 'o') && Conf_OperCanMode)
369 IRC_WriteErrClient(Origin, ERR_CHANOPPRIVTOOLOW_MSG,
370 Client_ID(Origin), Name);
375 /* Kick Client from channel */
376 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
381 Channel_Quit( CLIENT *Client, const char *Reason )
385 assert( Client != NULL );
386 assert( Reason != NULL );
388 if (Conf_MorePrivacy)
391 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
397 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
404 * Get number of channels this server knows and that are "visible" to
405 * the given client. If no client is given, all channels will be counted.
407 * @param Client The client to check or NULL.
408 * @return Number of channels visible to the client.
411 Channel_CountVisible (CLIENT *Client)
414 unsigned long count = 0;
419 if (!Channel_HasMode(c, 's')
420 || Channel_IsMemberOf(c, Client))
431 Channel_MemberCount( CHANNEL *Chan )
434 unsigned long count = 0;
436 assert( Chan != NULL );
438 cl2chan = My_Cl2Chan;
441 if( cl2chan->channel == Chan ) count++;
442 cl2chan = cl2chan->next;
445 } /* Channel_MemberCount */
449 Channel_CountForUser( CLIENT *Client )
451 /* Count number of channels a user is member of. */
456 assert( Client != NULL );
458 cl2chan = My_Cl2Chan;
461 if( cl2chan->client == Client ) count++;
462 cl2chan = cl2chan->next;
466 } /* Channel_CountForUser */
470 Channel_Name( const CHANNEL *Chan )
472 assert( Chan != NULL );
478 Channel_Modes( CHANNEL *Chan )
480 assert( Chan != NULL );
482 } /* Channel_Modes */
486 Channel_HasMode( CHANNEL *Chan, char Mode )
488 assert( Chan != NULL );
489 return strchr( Chan->modes, Mode ) != NULL;
490 } /* Channel_HasMode */
494 Channel_Key( CHANNEL *Chan )
496 assert( Chan != NULL );
502 Channel_MaxUsers( CHANNEL *Chan )
504 assert( Chan != NULL );
505 return Chan->maxusers;
506 } /* Channel_MaxUsers */
510 Channel_First( void )
513 } /* Channel_First */
517 Channel_Next( CHANNEL *Chan )
519 assert( Chan != NULL );
525 Channel_Search( const char *Name )
527 /* Search channel structure */
532 assert( Name != NULL );
534 search_hash = Hash( Name );
538 if( search_hash == c->hash )
541 if( strcasecmp( Name, c->name ) == 0 ) return c;
546 } /* Channel_Search */
550 Channel_FirstMember( CHANNEL *Chan )
552 assert( Chan != NULL );
553 return Get_First_Cl2Chan( NULL, Chan );
554 } /* Channel_FirstMember */
558 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
560 assert( Chan != NULL );
561 assert( Cl2Chan != NULL );
562 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
563 } /* Channel_NextMember */
567 Channel_FirstChannelOf( CLIENT *Client )
569 assert( Client != NULL );
570 return Get_First_Cl2Chan( Client, NULL );
571 } /* Channel_FirstChannelOf */
575 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
577 assert( Client != NULL );
578 assert( Cl2Chan != NULL );
579 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
580 } /* Channel_NextChannelOf */
584 Channel_GetClient( CL2CHAN *Cl2Chan )
586 assert( Cl2Chan != NULL );
587 return Cl2Chan->client;
588 } /* Channel_GetClient */
592 Channel_GetChannel( CL2CHAN *Cl2Chan )
594 assert( Cl2Chan != NULL );
595 return Cl2Chan->channel;
596 } /* Channel_GetChannel */
600 Channel_IsValidName( const char *Name )
602 assert( Name != NULL );
605 if (strlen(Name) <= 1)
608 if (strchr("#&+", Name[0]) == NULL)
610 if (strlen(Name) >= CHANNEL_NAME_LEN)
613 return Name[strcspn(Name, " ,:\007")] == 0;
614 } /* Channel_IsValidName */
618 Channel_ModeAdd( CHANNEL *Chan, char Mode )
621 * If the channel already had this mode, return false.
622 * If the channel mode was newly set return true.
627 assert( Chan != NULL );
629 x[0] = Mode; x[1] = '\0';
630 if( ! Channel_HasMode( Chan, x[0] ))
632 /* Channel does not have this mode yet, set it */
633 strlcat( Chan->modes, x, sizeof( Chan->modes ));
637 } /* Channel_ModeAdd */
641 Channel_ModeDel( CHANNEL *Chan, char Mode )
644 * if the mode was removed return true.
645 * if the channel did not have the mode, return false.
649 assert( Chan != NULL );
651 p = strchr( Chan->modes, Mode );
652 if( ! p ) return false;
654 /* Channel has mode -> delete */
661 } /* Channel_ModeDel */
665 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
667 /* Set Channel-User-Mode.
668 * if mode was newly set, return true.
669 * if the User already had this channel-mode, return false.
675 assert( Chan != NULL );
676 assert( Client != NULL );
678 cl2chan = Get_Cl2Chan( Chan, Client );
679 assert( cl2chan != NULL );
681 x[0] = Mode; x[1] = '\0';
682 if( ! strchr( cl2chan->modes, x[0] ))
684 /* mode not set, -> set it */
685 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
689 } /* Channel_UserModeAdd */
693 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
695 /* Delete Channel-User-Mode.
696 * If Mode was removed, return true.
697 * If User did not have the Channel-Mode, return false.
703 assert( Chan != NULL );
704 assert( Client != NULL );
706 cl2chan = Get_Cl2Chan( Chan, Client );
707 assert( cl2chan != NULL );
709 p = strchr( cl2chan->modes, Mode );
710 if( ! p ) return false;
712 /* Client has Mode -> delete */
719 } /* Channel_UserModeDel */
723 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
725 /* return Users' Channel-Modes */
729 assert( Chan != NULL );
730 assert( Client != NULL );
732 cl2chan = Get_Cl2Chan( Chan, Client );
733 assert( cl2chan != NULL );
735 return cl2chan->modes;
736 } /* Channel_UserModes */
740 Channel_UserHasMode( CHANNEL *Chan, CLIENT *Client, char Mode )
742 return strchr(Channel_UserModes(Chan, Client), Mode) != NULL;
743 } /* Channel_UserHasMode */
747 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
749 /* Test if Client is on Channel Chan */
751 assert( Chan != NULL );
752 assert( Client != NULL );
753 return Get_Cl2Chan(Chan, Client) != NULL;
754 } /* Channel_IsMemberOf */
758 Channel_Topic( CHANNEL *Chan )
761 assert( Chan != NULL );
762 ret = array_start(&Chan->topic);
763 return ret ? ret : "";
764 } /* Channel_Topic */
770 Channel_TopicTime(CHANNEL *Chan)
772 assert(Chan != NULL);
773 return (unsigned int) Chan->topic_time;
774 } /* Channel_TopicTime */
778 Channel_TopicWho(CHANNEL *Chan)
780 assert(Chan != NULL);
781 return Chan->topic_who;
782 } /* Channel_TopicWho */
786 Channel_CreationTime(CHANNEL *Chan)
788 assert(Chan != NULL);
789 return (unsigned int) Chan->creation_time;
790 } /* Channel_CreationTime */
796 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
799 assert( Chan != NULL );
800 assert( Topic != NULL );
803 if (len < array_bytes(&Chan->topic))
804 array_free(&Chan->topic);
806 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
807 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
808 Topic, Chan->name, strerror(errno));
810 Chan->topic_time = time(NULL);
811 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
812 strlcpy(Chan->topic_who, Client_ID(Client),
813 sizeof Chan->topic_who);
815 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
816 sizeof Chan->topic_who);
820 } /* Channel_SetTopic */
824 Channel_SetModes( CHANNEL *Chan, const char *Modes )
826 assert( Chan != NULL );
827 assert( Modes != NULL );
829 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
830 } /* Channel_SetModes */
834 Channel_SetKey( CHANNEL *Chan, const char *Key )
836 assert( Chan != NULL );
837 assert( Key != NULL );
839 strlcpy( Chan->key, Key, sizeof( Chan->key ));
840 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
841 } /* Channel_SetKey */
845 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
847 assert( Chan != NULL );
849 Chan->maxusers = Count;
850 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
851 } /* Channel_SetMaxUsers */
855 * Check if a client is allowed to send to a specific channel.
857 * @param Chan The channel to check.
858 * @param From The client that wants to send.
859 * @return true if the client is allowed to send, false otherwise.
862 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
864 bool is_member, has_voice, is_halfop, is_op, is_chanadmin, is_owner;
866 is_member = has_voice = is_halfop = is_op = is_chanadmin = is_owner = false;
868 /* The server itself always can send messages :-) */
869 if (Client_ThisServer() == From)
872 if (Channel_IsMemberOf(Chan, From)) {
874 if (Channel_UserHasMode(Chan, From, 'v'))
876 if (Channel_UserHasMode(Chan, From, 'h'))
878 if (Channel_UserHasMode(Chan, From, 'o'))
880 if (Channel_UserHasMode(Chan, From, 'a'))
882 if (Channel_UserHasMode(Chan, From, 'q'))
887 * Is the client allowed to write to channel?
889 * If channel mode n set: non-members cannot send to channel.
890 * If channel mode m set: need voice.
892 if (Channel_HasMode(Chan, 'n') && !is_member)
895 if (Channel_HasMode(Chan, 'M') && !Client_HasMode(From, 'R')
896 && !Client_HasMode(From, 'o'))
899 if (has_voice || is_halfop || is_op || is_chanadmin || is_owner)
902 if (Channel_HasMode(Chan, 'm'))
905 if (Lists_Check(&Chan->list_excepts, From))
908 return !Lists_Check(&Chan->list_bans, From);
913 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
914 bool SendErrors, const char *Text)
916 if (!Can_Send_To_Channel(Chan, From)) {
918 return CONNECTED; /* no error, see RFC 2812 */
919 if (Channel_HasMode(Chan, 'M'))
920 return IRC_WriteErrClient(From, ERR_NEEDREGGEDNICK_MSG,
921 Client_ID(From), Channel_Name(Chan));
923 return IRC_WriteErrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
924 Client_ID(From), Channel_Name(Chan));
927 if (Client_Conn(From) > NONE)
928 Conn_UpdateIdle(Client_Conn(From));
930 IRC_WriteStrChannelPrefix(Client, Chan, From, true, "%s %s :%s",
931 Command, Channel_Name(Chan), Text);
937 Channel_Create( const char *Name )
939 /* Create new CHANNEL structure and add it to linked list */
942 assert( Name != NULL );
944 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
947 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
950 memset( c, 0, sizeof( CHANNEL ));
951 strlcpy( c->name, Name, sizeof( c->name ));
952 c->hash = Hash( c->name );
953 c->next = My_Channels;
955 c->creation_time = time(NULL);
958 LogDebug("Created new channel structure for \"%s\".", Name);
960 } /* Channel_Create */
964 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
968 assert( Chan != NULL );
969 assert( Client != NULL );
971 cl2chan = My_Cl2Chan;
974 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
975 cl2chan = cl2chan->next;
982 Add_Client( CHANNEL *Chan, CLIENT *Client )
986 assert( Chan != NULL );
987 assert( Client != NULL );
989 /* Create new CL2CHAN structure */
990 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
993 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
996 cl2chan->channel = Chan;
997 cl2chan->client = Client;
998 strcpy( cl2chan->modes, "" );
1001 cl2chan->next = My_Cl2Chan;
1002 My_Cl2Chan = cl2chan;
1004 LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
1011 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
1013 CL2CHAN *cl2chan, *last_cl2chan;
1016 assert( Chan != NULL );
1017 assert( Client != NULL );
1018 assert( Origin != NULL );
1019 assert( Reason != NULL );
1021 /* Do not inform other servers if the channel is local to this server,
1022 * regardless of what the caller requested! */
1024 InformServer = !Channel_IsLocal(Chan);
1026 last_cl2chan = NULL;
1027 cl2chan = My_Cl2Chan;
1030 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
1031 last_cl2chan = cl2chan;
1032 cl2chan = cl2chan->next;
1034 if( ! cl2chan ) return false;
1036 c = cl2chan->channel;
1037 assert( c != NULL );
1039 /* maintain cl2chan list */
1040 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
1041 else My_Cl2Chan = cl2chan->next;
1047 /* QUIT: other servers have already been notified,
1048 * see Client_Destroy(); so only inform other clients
1049 * in same channel. */
1050 assert( InformServer == false );
1051 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1052 Client_Mask( Client ), c->name, Reason );
1055 /* User was KICKed: inform other servers (public
1056 * channels) and all users in the channel */
1058 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
1059 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
1060 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
1061 c->name, Client_ID( Client ), Reason );
1062 if ((Client_Conn(Client) > NONE) &&
1063 (Client_Type(Client) == CLIENT_USER))
1065 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
1066 c->name, Client_ID( Client ), Reason);
1068 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
1069 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
1072 if (Conf_MorePrivacy)
1076 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
1078 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
1081 if ((Client_Conn(Origin) > NONE) &&
1082 (Client_Type(Origin) == CLIENT_USER))
1084 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
1085 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1086 Client_Mask(Client), c->name, Reason);
1090 /* When channel is empty and is not pre-defined, delete */
1091 if( ! Channel_HasMode( Chan, 'P' ))
1093 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
1097 } /* Remove_Client */
1101 Channel_AddBan(CHANNEL *c, const char *mask, const char *who )
1103 struct list_head *h = Channel_GetListBans(c);
1104 LogDebug("Adding \"%s\" to \"%s\" ban list", mask, Channel_Name(c));
1105 return Lists_Add(h, mask, time(NULL), who, false);
1110 Channel_AddExcept(CHANNEL *c, const char *mask, const char *who )
1112 struct list_head *h = Channel_GetListExcepts(c);
1113 LogDebug("Adding \"%s\" to \"%s\" exception list", mask, Channel_Name(c));
1114 return Lists_Add(h, mask, time(NULL), who, false);
1119 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce, const char *who )
1121 struct list_head *h = Channel_GetListInvites(c);
1122 LogDebug("Adding \"%s\" to \"%s\" invite list", mask, Channel_Name(c));
1123 return Lists_Add(h, mask, time(NULL), who, onlyonce);
1128 ShowChannelList(struct list_head *head, CLIENT *Client, CHANNEL *Channel,
1129 char *msg, char *msg_end)
1131 struct list_elem *e;
1133 assert (Client != NULL);
1134 assert (Channel != NULL);
1136 e = Lists_GetFirst(head);
1138 if (!IRC_WriteStrClient(Client, msg, Client_ID(Client),
1139 Channel_Name(Channel),
1142 Lists_GetValidity(e)))
1143 return DISCONNECTED;
1144 e = Lists_GetNext(e);
1147 return IRC_WriteStrClient(Client, msg_end, Client_ID(Client),
1148 Channel_Name(Channel));
1153 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1155 struct list_head *h;
1157 assert( Channel != NULL );
1159 h = Channel_GetListBans(Channel);
1160 return ShowChannelList(h, Client, Channel, RPL_BANLIST_MSG,
1161 RPL_ENDOFBANLIST_MSG);
1166 Channel_ShowExcepts( CLIENT *Client, CHANNEL *Channel )
1168 struct list_head *h;
1170 assert( Channel != NULL );
1172 h = Channel_GetListExcepts(Channel);
1173 return ShowChannelList(h, Client, Channel, RPL_EXCEPTLIST_MSG,
1174 RPL_ENDOFEXCEPTLIST_MSG);
1179 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1181 struct list_head *h;
1183 assert( Channel != NULL );
1185 h = Channel_GetListInvites(Channel);
1186 return ShowChannelList(h, Client, Channel, RPL_INVITELIST_MSG,
1187 RPL_ENDOFINVITELIST_MSG);
1192 * Log a message to the local &SERVER channel, if it exists.
1195 Channel_LogServer(const char *msg)
1200 assert(msg != NULL);
1202 sc = Channel_Search("&SERVER");
1206 c = Client_ThisServer();
1207 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1208 } /* Channel_LogServer */
1212 Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
1214 char *file_name, line[COMMAND_LEN], *nick, *pass;
1217 assert(Chan != NULL);
1218 assert(Client != NULL);
1219 assert(Key != NULL);
1221 if (!Channel_HasMode(Chan, 'k'))
1225 if (strcmp(Chan->key, Key) == 0)
1228 file_name = array_start(&Chan->keyfile);
1231 fd = fopen(file_name, "r");
1233 Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
1234 file_name, Chan->name, strerror(errno));
1238 while (fgets(line, (int)sizeof(line), fd) != NULL) {
1240 if (! (nick = strchr(line, ':')))
1243 if (!Match(line, Client_User(Client)))
1245 if (! (pass = strchr(nick, ':')))
1248 if (!Match(nick, Client_ID(Client)))
1250 if (strcmp(Key, pass) != 0)
1258 } /* Channel_CheckKey */
1262 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1264 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1265 } /* Get_First_Cl2Chan */
1269 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1273 assert( Client != NULL || Channel != NULL );
1278 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1279 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1280 cl2chan = cl2chan->next;
1283 } /* Get_Next_Cl2Chan */
1287 * Remove a channel and free all of its data structures.
1290 Delete_Channel(CHANNEL *Chan)
1292 CHANNEL *chan, *last_chan;
1303 assert(chan != NULL);
1307 /* maintain channel list */
1309 last_chan->next = chan->next;
1311 My_Channels = chan->next;
1313 LogDebug("Freed channel structure for \"%s\".", Chan->name);
1315 } /* Delete_Channel */
1319 Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
1323 assert(Chan != NULL);
1324 assert(KeyFile != NULL);
1326 len = strlen(KeyFile);
1327 if (len < array_bytes(&Chan->keyfile)) {
1328 Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
1329 array_free(&Chan->keyfile);
1335 if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
1337 "Could not set new channel key file \"%s\" for %s: %s",
1338 KeyFile, Chan->name, strerror(errno));
1340 Log(LOG_INFO|LOG_snotice,
1341 "New local channel key file \"%s\" for %s activated.",
1342 KeyFile, Chan->name);