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'))
364 IRC_WriteErrClient(Origin, ERR_CHANOPPRIVTOOLOW_MSG,
365 Client_ID(Origin), Name);
370 /* Kick Client from channel */
371 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
376 Channel_Quit( CLIENT *Client, const char *Reason )
380 assert( Client != NULL );
381 assert( Reason != NULL );
383 if (Conf_MorePrivacy)
386 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
392 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
399 * Get number of channels this server knows and that are "visible" to
400 * the given client. If no client is given, all channels will be counted.
402 * @param Client The client to check or NULL.
403 * @return Number of channels visible to the client.
406 Channel_CountVisible (CLIENT *Client)
409 unsigned long count = 0;
414 if (!Channel_HasMode(c, 's')
415 || Channel_IsMemberOf(c, Client))
426 Channel_MemberCount( CHANNEL *Chan )
429 unsigned long count = 0;
431 assert( Chan != NULL );
433 cl2chan = My_Cl2Chan;
436 if( cl2chan->channel == Chan ) count++;
437 cl2chan = cl2chan->next;
440 } /* Channel_MemberCount */
444 Channel_CountForUser( CLIENT *Client )
446 /* Count number of channels a user is member of. */
451 assert( Client != NULL );
453 cl2chan = My_Cl2Chan;
456 if( cl2chan->client == Client ) count++;
457 cl2chan = cl2chan->next;
461 } /* Channel_CountForUser */
465 Channel_Name( const CHANNEL *Chan )
467 assert( Chan != NULL );
473 Channel_Modes( CHANNEL *Chan )
475 assert( Chan != NULL );
477 } /* Channel_Modes */
481 Channel_HasMode( CHANNEL *Chan, char Mode )
483 assert( Chan != NULL );
484 return strchr( Chan->modes, Mode ) != NULL;
485 } /* Channel_HasMode */
489 Channel_Key( CHANNEL *Chan )
491 assert( Chan != NULL );
497 Channel_MaxUsers( CHANNEL *Chan )
499 assert( Chan != NULL );
500 return Chan->maxusers;
501 } /* Channel_MaxUsers */
505 Channel_First( void )
508 } /* Channel_First */
512 Channel_Next( CHANNEL *Chan )
514 assert( Chan != NULL );
520 Channel_Search( const char *Name )
522 /* Search channel structure */
527 assert( Name != NULL );
529 search_hash = Hash( Name );
533 if( search_hash == c->hash )
536 if( strcasecmp( Name, c->name ) == 0 ) return c;
541 } /* Channel_Search */
545 Channel_FirstMember( CHANNEL *Chan )
547 assert( Chan != NULL );
548 return Get_First_Cl2Chan( NULL, Chan );
549 } /* Channel_FirstMember */
553 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
555 assert( Chan != NULL );
556 assert( Cl2Chan != NULL );
557 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
558 } /* Channel_NextMember */
562 Channel_FirstChannelOf( CLIENT *Client )
564 assert( Client != NULL );
565 return Get_First_Cl2Chan( Client, NULL );
566 } /* Channel_FirstChannelOf */
570 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
572 assert( Client != NULL );
573 assert( Cl2Chan != NULL );
574 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
575 } /* Channel_NextChannelOf */
579 Channel_GetClient( CL2CHAN *Cl2Chan )
581 assert( Cl2Chan != NULL );
582 return Cl2Chan->client;
583 } /* Channel_GetClient */
587 Channel_GetChannel( CL2CHAN *Cl2Chan )
589 assert( Cl2Chan != NULL );
590 return Cl2Chan->channel;
591 } /* Channel_GetChannel */
595 Channel_IsValidName( const char *Name )
597 assert( Name != NULL );
600 if (strlen(Name) <= 1)
603 if (strchr("#&+", Name[0]) == NULL)
605 if (strlen(Name) >= CHANNEL_NAME_LEN)
608 return Name[strcspn(Name, " ,:\007")] == 0;
609 } /* Channel_IsValidName */
613 Channel_ModeAdd( CHANNEL *Chan, char Mode )
616 * If the channel already had this mode, return false.
617 * If the channel mode was newly set return true.
622 assert( Chan != NULL );
624 x[0] = Mode; x[1] = '\0';
625 if( ! Channel_HasMode( Chan, x[0] ))
627 /* Channel does not have this mode yet, set it */
628 strlcat( Chan->modes, x, sizeof( Chan->modes ));
632 } /* Channel_ModeAdd */
636 Channel_ModeDel( CHANNEL *Chan, char Mode )
639 * if the mode was removed return true.
640 * if the channel did not have the mode, return false.
644 assert( Chan != NULL );
646 p = strchr( Chan->modes, Mode );
647 if( ! p ) return false;
649 /* Channel has mode -> delete */
656 } /* Channel_ModeDel */
660 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
662 /* Set Channel-User-Mode.
663 * if mode was newly set, return true.
664 * if the User already had this channel-mode, return false.
670 assert( Chan != NULL );
671 assert( Client != NULL );
673 cl2chan = Get_Cl2Chan( Chan, Client );
674 assert( cl2chan != NULL );
676 x[0] = Mode; x[1] = '\0';
677 if( ! strchr( cl2chan->modes, x[0] ))
679 /* mode not set, -> set it */
680 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
684 } /* Channel_UserModeAdd */
688 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
690 /* Delete Channel-User-Mode.
691 * If Mode was removed, return true.
692 * If User did not have the Channel-Mode, return false.
698 assert( Chan != NULL );
699 assert( Client != NULL );
701 cl2chan = Get_Cl2Chan( Chan, Client );
702 assert( cl2chan != NULL );
704 p = strchr( cl2chan->modes, Mode );
705 if( ! p ) return false;
707 /* Client has Mode -> delete */
714 } /* Channel_UserModeDel */
718 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
720 /* return Users' Channel-Modes */
724 assert( Chan != NULL );
725 assert( Client != NULL );
727 cl2chan = Get_Cl2Chan( Chan, Client );
728 assert( cl2chan != NULL );
730 return cl2chan->modes;
731 } /* Channel_UserModes */
735 Channel_UserHasMode( CHANNEL *Chan, CLIENT *Client, char Mode )
737 return strchr(Channel_UserModes(Chan, Client), Mode) != NULL;
738 } /* Channel_UserHasMode */
742 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
744 /* Test if Client is on Channel Chan */
746 assert( Chan != NULL );
747 assert( Client != NULL );
748 return Get_Cl2Chan(Chan, Client) != NULL;
749 } /* Channel_IsMemberOf */
753 Channel_Topic( CHANNEL *Chan )
756 assert( Chan != NULL );
757 ret = array_start(&Chan->topic);
758 return ret ? ret : "";
759 } /* Channel_Topic */
765 Channel_TopicTime(CHANNEL *Chan)
767 assert(Chan != NULL);
768 return (unsigned int) Chan->topic_time;
769 } /* Channel_TopicTime */
773 Channel_TopicWho(CHANNEL *Chan)
775 assert(Chan != NULL);
776 return Chan->topic_who;
777 } /* Channel_TopicWho */
781 Channel_CreationTime(CHANNEL *Chan)
783 assert(Chan != NULL);
784 return (unsigned int) Chan->creation_time;
785 } /* Channel_CreationTime */
791 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
794 assert( Chan != NULL );
795 assert( Topic != NULL );
798 if (len < array_bytes(&Chan->topic))
799 array_free(&Chan->topic);
801 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
802 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
803 Topic, Chan->name, strerror(errno));
805 Chan->topic_time = time(NULL);
806 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
807 strlcpy(Chan->topic_who, Client_ID(Client),
808 sizeof Chan->topic_who);
810 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
811 sizeof Chan->topic_who);
815 } /* Channel_SetTopic */
819 Channel_SetModes( CHANNEL *Chan, const char *Modes )
821 assert( Chan != NULL );
822 assert( Modes != NULL );
824 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
825 } /* Channel_SetModes */
829 Channel_SetKey( CHANNEL *Chan, const char *Key )
831 assert( Chan != NULL );
832 assert( Key != NULL );
834 strlcpy( Chan->key, Key, sizeof( Chan->key ));
835 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
836 } /* Channel_SetKey */
840 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
842 assert( Chan != NULL );
844 Chan->maxusers = Count;
845 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
846 } /* Channel_SetMaxUsers */
850 * Check if a client is allowed to send to a specific channel.
852 * @param Chan The channel to check.
853 * @param From The client that wants to send.
854 * @return true if the client is allowed to send, false otherwise.
857 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
859 bool is_member, has_voice, is_halfop, is_op, is_chanadmin, is_owner;
861 is_member = has_voice = is_halfop = is_op = is_chanadmin = is_owner = false;
863 /* The server itself always can send messages :-) */
864 if (Client_ThisServer() == From)
867 if (Channel_IsMemberOf(Chan, From)) {
869 if (Channel_UserHasMode(Chan, From, 'v'))
871 if (Channel_UserHasMode(Chan, From, 'h'))
873 if (Channel_UserHasMode(Chan, From, 'o'))
875 if (Channel_UserHasMode(Chan, From, 'a'))
877 if (Channel_UserHasMode(Chan, From, 'q'))
882 * Is the client allowed to write to channel?
884 * If channel mode n set: non-members cannot send to channel.
885 * If channel mode m set: need voice.
887 if (Channel_HasMode(Chan, 'n') && !is_member)
890 if (Channel_HasMode(Chan, 'M') && !Client_HasMode(From, 'R')
891 && !Client_HasMode(From, 'o'))
894 if (has_voice || is_halfop || is_op || is_chanadmin || is_owner)
897 if (Channel_HasMode(Chan, 'm'))
900 if (Lists_Check(&Chan->list_excepts, From))
903 return !Lists_Check(&Chan->list_bans, From);
908 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
909 bool SendErrors, const char *Text)
911 if (!Can_Send_To_Channel(Chan, From)) {
913 return CONNECTED; /* no error, see RFC 2812 */
914 if (Channel_HasMode(Chan, 'M'))
915 return IRC_WriteErrClient(From, ERR_NEEDREGGEDNICK_MSG,
916 Client_ID(From), Channel_Name(Chan));
918 return IRC_WriteErrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
919 Client_ID(From), Channel_Name(Chan));
922 if (Client_Conn(From) > NONE)
923 Conn_UpdateIdle(Client_Conn(From));
925 IRC_WriteStrChannelPrefix(Client, Chan, From, true, "%s %s :%s",
926 Command, Channel_Name(Chan), Text);
932 Channel_Create( const char *Name )
934 /* Create new CHANNEL structure and add it to linked list */
937 assert( Name != NULL );
939 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
942 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
945 memset( c, 0, sizeof( CHANNEL ));
946 strlcpy( c->name, Name, sizeof( c->name ));
947 c->hash = Hash( c->name );
948 c->next = My_Channels;
950 c->creation_time = time(NULL);
953 LogDebug("Created new channel structure for \"%s\".", Name);
955 } /* Channel_Create */
959 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
963 assert( Chan != NULL );
964 assert( Client != NULL );
966 cl2chan = My_Cl2Chan;
969 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
970 cl2chan = cl2chan->next;
977 Add_Client( CHANNEL *Chan, CLIENT *Client )
981 assert( Chan != NULL );
982 assert( Client != NULL );
984 /* Create new CL2CHAN structure */
985 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
988 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
991 cl2chan->channel = Chan;
992 cl2chan->client = Client;
993 strcpy( cl2chan->modes, "" );
996 cl2chan->next = My_Cl2Chan;
997 My_Cl2Chan = cl2chan;
999 LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
1006 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
1008 CL2CHAN *cl2chan, *last_cl2chan;
1011 assert( Chan != NULL );
1012 assert( Client != NULL );
1013 assert( Origin != NULL );
1014 assert( Reason != NULL );
1016 /* Do not inform other servers if the channel is local to this server,
1017 * regardless of what the caller requested! */
1019 InformServer = !Channel_IsLocal(Chan);
1021 last_cl2chan = NULL;
1022 cl2chan = My_Cl2Chan;
1025 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
1026 last_cl2chan = cl2chan;
1027 cl2chan = cl2chan->next;
1029 if( ! cl2chan ) return false;
1031 c = cl2chan->channel;
1032 assert( c != NULL );
1034 /* maintain cl2chan list */
1035 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
1036 else My_Cl2Chan = cl2chan->next;
1042 /* QUIT: other servers have already been notified,
1043 * see Client_Destroy(); so only inform other clients
1044 * in same channel. */
1045 assert( InformServer == false );
1046 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1047 Client_Mask( Client ), c->name, Reason );
1050 /* User was KICKed: inform other servers (public
1051 * channels) and all users in the channel */
1053 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
1054 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
1055 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
1056 c->name, Client_ID( Client ), Reason );
1057 if ((Client_Conn(Client) > NONE) &&
1058 (Client_Type(Client) == CLIENT_USER))
1060 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
1061 c->name, Client_ID( Client ), Reason);
1063 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
1064 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
1067 if (Conf_MorePrivacy)
1071 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
1073 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
1076 if ((Client_Conn(Origin) > NONE) &&
1077 (Client_Type(Origin) == CLIENT_USER))
1079 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
1080 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1081 Client_Mask(Client), c->name, Reason);
1085 /* When channel is empty and is not pre-defined, delete */
1086 if( ! Channel_HasMode( Chan, 'P' ))
1088 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
1092 } /* Remove_Client */
1096 Channel_AddBan(CHANNEL *c, const char *mask )
1098 struct list_head *h = Channel_GetListBans(c);
1099 LogDebug("Adding \"%s\" to \"%s\" ban list", mask, Channel_Name(c));
1100 return Lists_Add(h, mask, false, NULL);
1105 Channel_AddExcept(CHANNEL *c, const char *mask )
1107 struct list_head *h = Channel_GetListExcepts(c);
1108 LogDebug("Adding \"%s\" to \"%s\" exception list", mask, Channel_Name(c));
1109 return Lists_Add(h, mask, false, NULL);
1114 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
1116 struct list_head *h = Channel_GetListInvites(c);
1117 LogDebug("Adding \"%s\" to \"%s\" invite list", mask, Channel_Name(c));
1118 return Lists_Add(h, mask, onlyonce, NULL);
1123 ShowChannelList(struct list_head *head, CLIENT *Client, CHANNEL *Channel,
1124 char *msg, char *msg_end)
1126 struct list_elem *e;
1128 assert (Client != NULL);
1129 assert (Channel != NULL);
1131 e = Lists_GetFirst(head);
1133 if (!IRC_WriteStrClient(Client, msg, Client_ID(Client),
1134 Channel_Name(Channel),
1136 return DISCONNECTED;
1137 e = Lists_GetNext(e);
1140 return IRC_WriteStrClient(Client, msg_end, Client_ID(Client),
1141 Channel_Name(Channel));
1146 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1148 struct list_head *h;
1150 assert( Channel != NULL );
1152 h = Channel_GetListBans(Channel);
1153 return ShowChannelList(h, Client, Channel, RPL_BANLIST_MSG,
1154 RPL_ENDOFBANLIST_MSG);
1159 Channel_ShowExcepts( CLIENT *Client, CHANNEL *Channel )
1161 struct list_head *h;
1163 assert( Channel != NULL );
1165 h = Channel_GetListExcepts(Channel);
1166 return ShowChannelList(h, Client, Channel, RPL_EXCEPTLIST_MSG,
1167 RPL_ENDOFEXCEPTLIST_MSG);
1172 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1174 struct list_head *h;
1176 assert( Channel != NULL );
1178 h = Channel_GetListInvites(Channel);
1179 return ShowChannelList(h, Client, Channel, RPL_INVITELIST_MSG,
1180 RPL_ENDOFINVITELIST_MSG);
1185 * Log a message to the local &SERVER channel, if it exists.
1188 Channel_LogServer(const char *msg)
1193 assert(msg != NULL);
1195 sc = Channel_Search("&SERVER");
1199 c = Client_ThisServer();
1200 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1201 } /* Channel_LogServer */
1205 Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
1207 char *file_name, line[COMMAND_LEN], *nick, *pass;
1210 assert(Chan != NULL);
1211 assert(Client != NULL);
1212 assert(Key != NULL);
1214 if (!Channel_HasMode(Chan, 'k'))
1218 if (strcmp(Chan->key, Key) == 0)
1221 file_name = array_start(&Chan->keyfile);
1224 fd = fopen(file_name, "r");
1226 Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
1227 file_name, Chan->name, strerror(errno));
1231 while (fgets(line, (int)sizeof(line), fd) != NULL) {
1233 if (! (nick = strchr(line, ':')))
1236 if (!Match(line, Client_User(Client)))
1238 if (! (pass = strchr(nick, ':')))
1241 if (!Match(nick, Client_ID(Client)))
1243 if (strcmp(Key, pass) != 0)
1251 } /* Channel_CheckKey */
1255 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1257 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1258 } /* Get_First_Cl2Chan */
1262 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1266 assert( Client != NULL || Channel != NULL );
1271 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1272 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1273 cl2chan = cl2chan->next;
1276 } /* Get_Next_Cl2Chan */
1280 * Remove a channel and free all of its data structures.
1283 Delete_Channel(CHANNEL *Chan)
1285 CHANNEL *chan, *last_chan;
1296 assert(chan != NULL);
1300 /* maintain channel list */
1302 last_chan->next = chan->next;
1304 My_Channels = chan->next;
1306 LogDebug("Freed channel structure for \"%s\".", Chan->name);
1308 } /* Delete_Channel */
1312 Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
1316 assert(Chan != NULL);
1317 assert(KeyFile != NULL);
1319 len = strlen(KeyFile);
1320 if (len < array_bytes(&Chan->keyfile)) {
1321 Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
1322 array_free(&Chan->keyfile);
1328 if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
1330 "Could not set new channel key file \"%s\" for %s: %s",
1331 KeyFile, Chan->name, strerror(errno));
1333 Log(LOG_INFO|LOG_snotice,
1334 "New local channel key file \"%s\" for %s activated.",
1335 KeyFile, Chan->name);