2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2012 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.
30 #include "conn-func.h"
36 #include "irc-write.h"
52 static CHANNEL *My_Channels;
53 static CL2CHAN *My_Cl2Chan;
56 static CL2CHAN *Get_Cl2Chan PARAMS(( CHANNEL *Chan, CLIENT *Client ));
57 static CL2CHAN *Add_Client PARAMS(( CHANNEL *Chan, CLIENT *Client ));
58 static bool Remove_Client PARAMS(( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer ));
59 static CL2CHAN *Get_First_Cl2Chan PARAMS(( CLIENT *Client, CHANNEL *Chan ));
60 static CL2CHAN *Get_Next_Cl2Chan PARAMS(( CL2CHAN *Start, CLIENT *Client, CHANNEL *Chan ));
61 static void Delete_Channel PARAMS(( CHANNEL *Chan ));
62 static void Free_Channel PARAMS(( CHANNEL *Chan ));
63 static void Set_KeyFile PARAMS((CHANNEL *Chan, const char *KeyFile));
74 GLOBAL struct list_head *
75 Channel_GetListBans(CHANNEL *c)
82 GLOBAL struct list_head *
83 Channel_GetListExcepts(CHANNEL *c)
86 return &c->list_excepts;
90 GLOBAL struct list_head *
91 Channel_GetListInvites(CHANNEL *c)
94 return &c->list_invites;
99 * Generate predefined persistent channels and &SERVER
102 Channel_InitPredefined( void )
105 const struct Conf_Channel *conf_chan;
107 size_t i, channel_count = array_length(&Conf_Channels, sizeof(*conf_chan));
109 conf_chan = array_start(&Conf_Channels);
111 assert(channel_count == 0 || conf_chan != NULL);
113 for (i = 0; i < channel_count; i++, conf_chan++) {
114 if (!conf_chan->name[0])
116 if (!Channel_IsValidName(conf_chan->name)) {
118 "Can't create pre-defined channel: invalid name: \"%s\"",
123 new_chan = Channel_Search(conf_chan->name);
126 "Can't create pre-defined channel \"%s\": name already in use.",
128 Set_KeyFile(new_chan, conf_chan->keyfile);
132 new_chan = Channel_Create(conf_chan->name);
134 Log(LOG_ERR, "Can't create pre-defined channel \"%s\"!",
138 Log(LOG_INFO, "Created pre-defined channel \"%s\".",
141 Channel_ModeAdd(new_chan, 'P');
143 if (conf_chan->topic[0])
144 Channel_SetTopic(new_chan, NULL, conf_chan->topic);
146 c = conf_chan->modes;
148 Channel_ModeAdd(new_chan, *c++);
150 Channel_SetKey(new_chan, conf_chan->key);
151 Channel_SetMaxUsers(new_chan, conf_chan->maxusers);
152 Set_KeyFile(new_chan, conf_chan->keyfile);
155 array_free(&Conf_Channels);
157 /* Make sure the local &SERVER channel exists */
158 if (!Channel_Search("&SERVER")) {
159 new_chan = Channel_Create("&SERVER");
161 Channel_SetModes(new_chan, "mnPt");
162 Channel_SetTopic(new_chan, Client_ThisServer(),
165 Log(LOG_ERR, "Failed to create \"&SERVER\" channel!");
167 LogDebug("Required channel \"&SERVER\" already exists, ok.");
168 } /* Channel_InitPredefined */
172 Free_Channel(CHANNEL *chan)
174 array_free(&chan->topic);
175 array_free(&chan->keyfile);
176 Lists_Free(&chan->list_bans);
177 Lists_Free(&chan->list_excepts);
178 Lists_Free(&chan->list_invites);
188 CL2CHAN *cl2chan, *cl2chan_next;
190 /* free struct Channel */
198 /* Free Channel allocation table */
199 cl2chan = My_Cl2Chan;
201 cl2chan_next = cl2chan->next;
203 cl2chan = cl2chan_next;
210 * This function lets a client join a channel. First, the function
211 * checks that the specified channel name is valid and that the client
212 * isn't already a member. If the specified channel doesn't exist,
213 * a new channel is created. Client is added to channel by function
217 Channel_Join( CLIENT *Client, const char *Name )
221 assert(Client != NULL);
222 assert(Name != NULL);
224 /* Check that the channel name is valid */
225 if (! Channel_IsValidName(Name)) {
226 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
227 Client_ID(Client), Name);
231 chan = Channel_Search(Name);
233 /* Check if the client is already in the channel */
234 if (Get_Cl2Chan(chan, Client))
237 /* If the specified channel does not exist, the channel
239 chan = Channel_Create(Name);
244 /* Add user to Channel */
245 if (! Add_Client(chan, Client))
253 * Part client from channel.
254 * This function lets a client part from a channel. First, the function checks
255 * if the channel exists and the client is a member of it and sends out
256 * appropriate error messages if not. The real work is done by the function
260 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
264 assert(Client != NULL);
265 assert(Name != NULL);
266 assert(Reason != NULL);
268 /* Check that specified channel exists */
269 chan = Channel_Search(Name);
271 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
272 Client_ID(Client), Name);
276 /* Check that the client is in the channel */
277 if (!Get_Cl2Chan(chan, Client)) {
278 IRC_WriteStrClient(Client, ERR_NOTONCHANNEL_MSG,
279 Client_ID(Client), Name);
283 if (Conf_MorePrivacy)
286 /* Part client from channel */
287 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
295 * Kick user from Channel
298 Channel_Kick(CLIENT *Peer, CLIENT *Target, CLIENT *Origin, const char *Name,
302 bool can_kick = false;
304 assert(Peer != NULL);
305 assert(Target != NULL);
306 assert(Origin != NULL);
307 assert(Name != NULL);
308 assert(Reason != NULL);
310 /* Check that channel exists */
311 chan = Channel_Search( Name );
314 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
318 if (Client_Type(Peer) != CLIENT_SERVER &&
319 Client_Type(Origin) != CLIENT_SERVICE) {
320 /* Check that user is on the specified channel */
321 if (!Channel_IsMemberOf(chan, Origin)) {
322 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG,
323 Client_ID(Origin), Name);
328 /* Check that the client to be kicked is on the specified channel */
329 if (!Channel_IsMemberOf(chan, Target)) {
330 IRC_WriteStrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
331 Client_ID(Origin), Client_ID(Target), Name );
335 if(Client_Type(Peer) == CLIENT_USER) {
336 /* Channel mode 'Q' and user mode 'q' on target: nobody but
337 * IRC Operators and servers can kick the target user */
338 if ((Channel_HasMode(chan, 'Q')
339 || Client_HasMode(Target, 'q')
340 || Client_Type(Target) == CLIENT_SERVICE)
341 && !Client_HasMode(Origin, 'o')) {
342 IRC_WriteStrClient(Origin, ERR_KICKDENY_MSG,
343 Client_ID(Origin), Name,
348 /* Check if client has the rights to kick target */
350 /* Owner can kick everyone */
351 if (Channel_UserHasMode(chan, Peer, 'q'))
354 /* Admin can't kick owner */
355 else if (Channel_UserHasMode(chan, Peer, 'a') &&
356 !Channel_UserHasMode(chan, Target, 'q'))
359 /* Op can't kick owner | admin */
360 else if (Channel_UserHasMode(chan, Peer, 'o') &&
361 !Channel_UserHasMode(chan, Target, 'q') &&
362 !Channel_UserHasMode(chan, Target, 'a'))
365 /* Half Op can't kick owner | admin | op */
366 else if (Channel_UserHasMode(chan, Peer, 'h') &&
367 !Channel_UserHasMode(chan, Target, 'q') &&
368 !Channel_UserHasMode(chan, Target, 'a') &&
369 !Channel_UserHasMode(chan, Target, 'o'))
373 IRC_WriteStrClient(Origin, ERR_CHANOPPRIVTOOLOW_MSG,
374 Client_ID(Origin), Name);
379 /* Kick Client from channel */
380 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
385 Channel_Quit( CLIENT *Client, const char *Reason )
389 assert( Client != NULL );
390 assert( Reason != NULL );
392 if (Conf_MorePrivacy)
395 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
401 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
408 * Get number of channels this server knows and that are "visible" to
409 * the given client. If no client is given, all channels will be counted.
411 * @param Client The client to check or NULL.
412 * @return Number of channels visible to the client.
415 Channel_CountVisible (CLIENT *Client)
418 unsigned long count = 0;
423 if (!Channel_HasMode(c, 's')
424 || Channel_IsMemberOf(c, Client))
435 Channel_MemberCount( CHANNEL *Chan )
438 unsigned long count = 0;
440 assert( Chan != NULL );
442 cl2chan = My_Cl2Chan;
445 if( cl2chan->channel == Chan ) count++;
446 cl2chan = cl2chan->next;
449 } /* Channel_MemberCount */
453 Channel_CountForUser( CLIENT *Client )
455 /* Count number of channels a user is member of. */
460 assert( Client != NULL );
462 cl2chan = My_Cl2Chan;
465 if( cl2chan->client == Client ) count++;
466 cl2chan = cl2chan->next;
470 } /* Channel_CountForUser */
474 Channel_Name( const CHANNEL *Chan )
476 assert( Chan != NULL );
482 Channel_Modes( CHANNEL *Chan )
484 assert( Chan != NULL );
486 } /* Channel_Modes */
490 Channel_HasMode( CHANNEL *Chan, char Mode )
492 assert( Chan != NULL );
493 return strchr( Chan->modes, Mode ) != NULL;
494 } /* Channel_HasMode */
498 Channel_Key( CHANNEL *Chan )
500 assert( Chan != NULL );
506 Channel_MaxUsers( CHANNEL *Chan )
508 assert( Chan != NULL );
509 return Chan->maxusers;
510 } /* Channel_MaxUsers */
514 Channel_First( void )
517 } /* Channel_First */
521 Channel_Next( CHANNEL *Chan )
523 assert( Chan != NULL );
529 Channel_Search( const char *Name )
531 /* Search channel structure */
536 assert( Name != NULL );
538 search_hash = Hash( Name );
542 if( search_hash == c->hash )
545 if( strcasecmp( Name, c->name ) == 0 ) return c;
550 } /* Channel_Search */
554 Channel_FirstMember( CHANNEL *Chan )
556 assert( Chan != NULL );
557 return Get_First_Cl2Chan( NULL, Chan );
558 } /* Channel_FirstMember */
562 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
564 assert( Chan != NULL );
565 assert( Cl2Chan != NULL );
566 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
567 } /* Channel_NextMember */
571 Channel_FirstChannelOf( CLIENT *Client )
573 assert( Client != NULL );
574 return Get_First_Cl2Chan( Client, NULL );
575 } /* Channel_FirstChannelOf */
579 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
581 assert( Client != NULL );
582 assert( Cl2Chan != NULL );
583 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
584 } /* Channel_NextChannelOf */
588 Channel_GetClient( CL2CHAN *Cl2Chan )
590 assert( Cl2Chan != NULL );
591 return Cl2Chan->client;
592 } /* Channel_GetClient */
596 Channel_GetChannel( CL2CHAN *Cl2Chan )
598 assert( Cl2Chan != NULL );
599 return Cl2Chan->channel;
600 } /* Channel_GetChannel */
604 Channel_IsValidName( const char *Name )
606 assert( Name != NULL );
609 if (strlen(Name) <= 1)
612 if (strchr("#&+", Name[0]) == NULL)
614 if (strlen(Name) >= CHANNEL_NAME_LEN)
617 return Name[strcspn(Name, " ,:\007")] == 0;
618 } /* Channel_IsValidName */
622 Channel_ModeAdd( CHANNEL *Chan, char Mode )
625 * If the channel already had this mode, return false.
626 * If the channel mode was newly set return true.
631 assert( Chan != NULL );
633 x[0] = Mode; x[1] = '\0';
634 if( ! Channel_HasMode( Chan, x[0] ))
636 /* Channel does not have this mode yet, set it */
637 strlcat( Chan->modes, x, sizeof( Chan->modes ));
641 } /* Channel_ModeAdd */
645 Channel_ModeDel( CHANNEL *Chan, char Mode )
648 * if the mode was removed return true.
649 * if the channel did not have the mode, return false.
653 assert( Chan != NULL );
655 p = strchr( Chan->modes, Mode );
656 if( ! p ) return false;
658 /* Channel has mode -> delete */
665 } /* Channel_ModeDel */
669 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
671 /* Set Channel-User-Mode.
672 * if mode was newly set, return true.
673 * if the User already had this channel-mode, return false.
679 assert( Chan != NULL );
680 assert( Client != NULL );
682 cl2chan = Get_Cl2Chan( Chan, Client );
683 assert( cl2chan != NULL );
685 x[0] = Mode; x[1] = '\0';
686 if( ! strchr( cl2chan->modes, x[0] ))
688 /* mode not set, -> set it */
689 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
693 } /* Channel_UserModeAdd */
697 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
699 /* Delete Channel-User-Mode.
700 * If Mode was removed, return true.
701 * If User did not have the Channel-Mode, return false.
707 assert( Chan != NULL );
708 assert( Client != NULL );
710 cl2chan = Get_Cl2Chan( Chan, Client );
711 assert( cl2chan != NULL );
713 p = strchr( cl2chan->modes, Mode );
714 if( ! p ) return false;
716 /* Client has Mode -> delete */
723 } /* Channel_UserModeDel */
727 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
729 /* return Users' Channel-Modes */
733 assert( Chan != NULL );
734 assert( Client != NULL );
736 cl2chan = Get_Cl2Chan( Chan, Client );
737 assert( cl2chan != NULL );
739 return cl2chan->modes;
740 } /* Channel_UserModes */
744 Channel_UserHasMode( CHANNEL *Chan, CLIENT *Client, char Mode )
746 return strchr(Channel_UserModes(Chan, Client), Mode) != NULL;
747 } /* Channel_UserHasMode */
751 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
753 /* Test if Client is on Channel Chan */
755 assert( Chan != NULL );
756 assert( Client != NULL );
757 return Get_Cl2Chan(Chan, Client) != NULL;
758 } /* Channel_IsMemberOf */
762 Channel_Topic( CHANNEL *Chan )
765 assert( Chan != NULL );
766 ret = array_start(&Chan->topic);
767 return ret ? ret : "";
768 } /* Channel_Topic */
774 Channel_TopicTime(CHANNEL *Chan)
776 assert(Chan != NULL);
777 return (unsigned int) Chan->topic_time;
778 } /* Channel_TopicTime */
782 Channel_TopicWho(CHANNEL *Chan)
784 assert(Chan != NULL);
785 return Chan->topic_who;
786 } /* Channel_TopicWho */
790 Channel_CreationTime(CHANNEL *Chan)
792 assert(Chan != NULL);
793 return (unsigned int) Chan->creation_time;
794 } /* Channel_CreationTime */
800 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
803 assert( Chan != NULL );
804 assert( Topic != NULL );
807 if (len < array_bytes(&Chan->topic))
808 array_free(&Chan->topic);
810 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
811 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
812 Topic, Chan->name, strerror(errno));
814 Chan->topic_time = time(NULL);
815 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
816 strlcpy(Chan->topic_who, Client_ID(Client),
817 sizeof Chan->topic_who);
819 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
820 sizeof Chan->topic_who);
824 } /* Channel_SetTopic */
828 Channel_SetModes( CHANNEL *Chan, const char *Modes )
830 assert( Chan != NULL );
831 assert( Modes != NULL );
833 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
834 } /* Channel_SetModes */
838 Channel_SetKey( CHANNEL *Chan, const char *Key )
840 assert( Chan != NULL );
841 assert( Key != NULL );
843 strlcpy( Chan->key, Key, sizeof( Chan->key ));
844 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
845 } /* Channel_SetKey */
849 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
851 assert( Chan != NULL );
853 Chan->maxusers = Count;
854 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
855 } /* Channel_SetMaxUsers */
859 * Check if a client is allowed to send to a specific channel.
861 * @param Chan The channel to check.
862 * @param From The client that wants to send.
863 * @return true if the client is allowed to send, false otherwise.
866 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
868 bool is_member, has_voice, is_halfop, is_op, is_chanadmin, is_owner;
870 is_member = has_voice = is_halfop = is_op = is_chanadmin = is_owner = false;
872 /* The server itself always can send messages :-) */
873 if (Client_ThisServer() == From)
876 if (Channel_IsMemberOf(Chan, From)) {
878 if (Channel_UserHasMode(Chan, From, 'v'))
880 if (Channel_UserHasMode(Chan, From, 'h'))
882 if (Channel_UserHasMode(Chan, From, 'o'))
884 if (Channel_UserHasMode(Chan, From, 'a'))
886 if (Channel_UserHasMode(Chan, From, 'q'))
891 * Is the client allowed to write to channel?
893 * If channel mode n set: non-members cannot send to channel.
894 * If channel mode m set: need voice.
896 if (Channel_HasMode(Chan, 'n') && !is_member)
899 if (Channel_HasMode(Chan, 'M') && !Client_HasMode(From, 'R')
900 && !Client_HasMode(From, 'o'))
903 if (has_voice || is_halfop || is_op || is_chanadmin || is_owner)
906 if (Channel_HasMode(Chan, 'm'))
909 if (Lists_Check(&Chan->list_excepts, From))
912 return !Lists_Check(&Chan->list_bans, From);
917 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
918 bool SendErrors, const char *Text)
920 if (!Can_Send_To_Channel(Chan, From)) {
922 return CONNECTED; /* no error, see RFC 2812 */
923 if (Channel_HasMode(Chan, 'M'))
924 return IRC_WriteStrClient(From, ERR_NEEDREGGEDNICK_MSG,
925 Client_ID(From), Channel_Name(Chan));
927 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
928 Client_ID(From), Channel_Name(Chan));
931 if (Client_Conn(From) > NONE)
932 Conn_UpdateIdle(Client_Conn(From));
934 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
935 "%s %s :%s", Command, Channel_Name(Chan), Text);
940 Channel_Create( const char *Name )
942 /* Create new CHANNEL structure and add it to linked list */
945 assert( Name != NULL );
947 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
950 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
953 memset( c, 0, sizeof( CHANNEL ));
954 strlcpy( c->name, Name, sizeof( c->name ));
955 c->hash = Hash( c->name );
956 c->next = My_Channels;
958 c->creation_time = time(NULL);
961 LogDebug("Created new channel structure for \"%s\".", Name);
963 } /* Channel_Create */
967 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
971 assert( Chan != NULL );
972 assert( Client != NULL );
974 cl2chan = My_Cl2Chan;
977 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
978 cl2chan = cl2chan->next;
985 Add_Client( CHANNEL *Chan, CLIENT *Client )
989 assert( Chan != NULL );
990 assert( Client != NULL );
992 /* Create new CL2CHAN structure */
993 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
996 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
999 cl2chan->channel = Chan;
1000 cl2chan->client = Client;
1001 strcpy( cl2chan->modes, "" );
1004 cl2chan->next = My_Cl2Chan;
1005 My_Cl2Chan = cl2chan;
1007 LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
1014 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
1016 CL2CHAN *cl2chan, *last_cl2chan;
1019 assert( Chan != NULL );
1020 assert( Client != NULL );
1021 assert( Origin != NULL );
1022 assert( Reason != NULL );
1024 /* Do not inform other servers if the channel is local to this server,
1025 * regardless of what the caller requested! */
1027 InformServer = !Channel_IsLocal(Chan);
1029 last_cl2chan = NULL;
1030 cl2chan = My_Cl2Chan;
1033 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
1034 last_cl2chan = cl2chan;
1035 cl2chan = cl2chan->next;
1037 if( ! cl2chan ) return false;
1039 c = cl2chan->channel;
1040 assert( c != NULL );
1042 /* maintain cl2chan list */
1043 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
1044 else My_Cl2Chan = cl2chan->next;
1050 /* QUIT: other servers have already been notified,
1051 * see Client_Destroy(); so only inform other clients
1052 * in same channel. */
1053 assert( InformServer == false );
1054 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1055 Client_Mask( Client ), c->name, Reason );
1058 /* User was KICKed: inform other servers (public
1059 * channels) and all users in the channel */
1061 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
1062 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
1063 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
1064 c->name, Client_ID( Client ), Reason );
1065 if ((Client_Conn(Client) > NONE) &&
1066 (Client_Type(Client) == CLIENT_USER))
1068 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
1069 c->name, Client_ID( Client ), Reason);
1071 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
1072 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
1075 if (Conf_MorePrivacy)
1079 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
1081 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
1084 if ((Client_Conn(Origin) > NONE) &&
1085 (Client_Type(Origin) == CLIENT_USER))
1087 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
1088 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1089 Client_Mask(Client), c->name, Reason);
1093 /* When channel is empty and is not pre-defined, delete */
1094 if( ! Channel_HasMode( Chan, 'P' ))
1096 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
1100 } /* Remove_Client */
1104 Channel_AddBan(CHANNEL *c, const char *mask )
1106 struct list_head *h = Channel_GetListBans(c);
1107 LogDebug("Adding \"%s\" to \"%s\" ban list", mask, Channel_Name(c));
1108 return Lists_Add(h, mask, false, NULL);
1113 Channel_AddExcept(CHANNEL *c, const char *mask )
1115 struct list_head *h = Channel_GetListExcepts(c);
1116 LogDebug("Adding \"%s\" to \"%s\" exception list", mask, Channel_Name(c));
1117 return Lists_Add(h, mask, false, NULL);
1122 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
1124 struct list_head *h = Channel_GetListInvites(c);
1125 LogDebug("Adding \"%s\" to \"%s\" invite list", mask, Channel_Name(c));
1126 return Lists_Add(h, mask, onlyonce, NULL);
1131 ShowChannelList(struct list_head *head, CLIENT *Client, CHANNEL *Channel,
1132 char *msg, char *msg_end)
1134 struct list_elem *e;
1136 assert (Client != NULL);
1137 assert (Channel != NULL);
1139 e = Lists_GetFirst(head);
1141 if (!IRC_WriteStrClient(Client, msg, Client_ID(Client),
1142 Channel_Name(Channel),
1144 return DISCONNECTED;
1145 e = Lists_GetNext(e);
1148 return IRC_WriteStrClient(Client, msg_end, Client_ID(Client),
1149 Channel_Name(Channel));
1154 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1156 struct list_head *h;
1158 assert( Channel != NULL );
1160 h = Channel_GetListBans(Channel);
1161 return ShowChannelList(h, Client, Channel, RPL_BANLIST_MSG,
1162 RPL_ENDOFBANLIST_MSG);
1167 Channel_ShowExcepts( CLIENT *Client, CHANNEL *Channel )
1169 struct list_head *h;
1171 assert( Channel != NULL );
1173 h = Channel_GetListExcepts(Channel);
1174 return ShowChannelList(h, Client, Channel, RPL_EXCEPTLIST_MSG,
1175 RPL_ENDOFEXCEPTLIST_MSG);
1180 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1182 struct list_head *h;
1184 assert( Channel != NULL );
1186 h = Channel_GetListInvites(Channel);
1187 return ShowChannelList(h, Client, Channel, RPL_INVITELIST_MSG,
1188 RPL_ENDOFINVITELIST_MSG);
1193 * Log a message to the local &SERVER channel, if it exists.
1196 Channel_LogServer(const char *msg)
1201 assert(msg != NULL);
1203 sc = Channel_Search("&SERVER");
1207 c = Client_ThisServer();
1208 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1209 } /* Channel_LogServer */
1213 Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
1215 char *file_name, line[COMMAND_LEN], *nick, *pass;
1218 assert(Chan != NULL);
1219 assert(Client != NULL);
1220 assert(Key != NULL);
1222 if (!Channel_HasMode(Chan, 'k'))
1226 if (strcmp(Chan->key, Key) == 0)
1229 file_name = array_start(&Chan->keyfile);
1232 fd = fopen(file_name, "r");
1234 Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
1235 file_name, Chan->name, strerror(errno));
1239 while (fgets(line, (int)sizeof(line), fd) != NULL) {
1241 if (! (nick = strchr(line, ':')))
1244 if (!Match(line, Client_User(Client)))
1246 if (! (pass = strchr(nick, ':')))
1249 if (!Match(nick, Client_ID(Client)))
1251 if (strcmp(Key, pass) != 0)
1259 } /* Channel_CheckKey */
1263 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1265 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1266 } /* Get_First_Cl2Chan */
1270 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1274 assert( Client != NULL || Channel != NULL );
1279 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1280 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1281 cl2chan = cl2chan->next;
1284 } /* Get_Next_Cl2Chan */
1288 * Remove a channel and free all of its data structures.
1291 Delete_Channel(CHANNEL *Chan)
1293 CHANNEL *chan, *last_chan;
1304 assert(chan != NULL);
1308 /* maintain channel list */
1310 last_chan->next = chan->next;
1312 My_Channels = chan->next;
1314 LogDebug("Freed channel structure for \"%s\".", Chan->name);
1316 } /* Delete_Channel */
1320 Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
1324 assert(Chan != NULL);
1325 assert(KeyFile != NULL);
1327 len = strlen(KeyFile);
1328 if (len < array_bytes(&Chan->keyfile)) {
1329 Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
1330 array_free(&Chan->keyfile);
1336 if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
1338 "Could not set new channel key file \"%s\" for %s: %s",
1339 KeyFile, Chan->name, strerror(errno));
1341 Log(LOG_INFO|LOG_snotice,
1342 "New local channel key file \"%s\" for %s activated.",
1343 KeyFile, Chan->name);