2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2013 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"
51 static CHANNEL *My_Channels;
52 static CL2CHAN *My_Cl2Chan;
54 static CL2CHAN *Get_Cl2Chan PARAMS(( CHANNEL *Chan, CLIENT *Client ));
55 static CL2CHAN *Add_Client PARAMS(( CHANNEL *Chan, CLIENT *Client ));
56 static bool Remove_Client PARAMS(( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer ));
57 static CL2CHAN *Get_First_Cl2Chan PARAMS(( CLIENT *Client, CHANNEL *Chan ));
58 static CL2CHAN *Get_Next_Cl2Chan PARAMS(( CL2CHAN *Start, CLIENT *Client, CHANNEL *Chan ));
59 static void Delete_Channel PARAMS(( CHANNEL *Chan ));
60 static void Free_Channel PARAMS(( CHANNEL *Chan ));
61 static void Set_KeyFile PARAMS((CHANNEL *Chan, const char *KeyFile));
72 GLOBAL struct list_head *
73 Channel_GetListBans(CHANNEL *c)
80 GLOBAL struct list_head *
81 Channel_GetListExcepts(CHANNEL *c)
84 return &c->list_excepts;
88 GLOBAL struct list_head *
89 Channel_GetListInvites(CHANNEL *c)
92 return &c->list_invites;
97 * Generate predefined persistent channels and &SERVER
100 Channel_InitPredefined( void )
103 const struct Conf_Channel *conf_chan;
105 size_t i, channel_count = array_length(&Conf_Channels, sizeof(*conf_chan));
107 conf_chan = array_start(&Conf_Channels);
109 assert(channel_count == 0 || conf_chan != NULL);
111 for (i = 0; i < channel_count; i++, conf_chan++) {
112 if (!conf_chan->name[0])
114 if (!Channel_IsValidName(conf_chan->name)) {
116 "Can't create pre-defined channel: invalid name: \"%s\"",
121 new_chan = Channel_Search(conf_chan->name);
124 "Can't create pre-defined channel \"%s\": name already in use.",
126 Set_KeyFile(new_chan, conf_chan->keyfile);
130 new_chan = Channel_Create(conf_chan->name);
132 Log(LOG_ERR, "Can't create pre-defined channel \"%s\"!",
136 Log(LOG_INFO, "Created pre-defined channel \"%s\".",
139 Channel_ModeAdd(new_chan, 'P');
141 if (conf_chan->topic[0])
142 Channel_SetTopic(new_chan, NULL, conf_chan->topic);
144 c = conf_chan->modes;
146 Channel_ModeAdd(new_chan, *c++);
148 Channel_SetKey(new_chan, conf_chan->key);
149 Channel_SetMaxUsers(new_chan, conf_chan->maxusers);
150 Set_KeyFile(new_chan, conf_chan->keyfile);
153 array_free(&Conf_Channels);
155 /* Make sure the local &SERVER channel exists */
156 if (!Channel_Search("&SERVER")) {
157 new_chan = Channel_Create("&SERVER");
159 Channel_SetModes(new_chan, "mnPt");
160 Channel_SetTopic(new_chan, Client_ThisServer(),
163 Log(LOG_ERR, "Failed to create \"&SERVER\" channel!");
165 LogDebug("Required channel \"&SERVER\" already exists, ok.");
166 } /* Channel_InitPredefined */
170 Free_Channel(CHANNEL *chan)
172 array_free(&chan->topic);
173 array_free(&chan->keyfile);
174 Lists_Free(&chan->list_bans);
175 Lists_Free(&chan->list_excepts);
176 Lists_Free(&chan->list_invites);
186 CL2CHAN *cl2chan, *cl2chan_next;
188 /* free struct Channel */
196 /* Free Channel allocation table */
197 cl2chan = My_Cl2Chan;
199 cl2chan_next = cl2chan->next;
201 cl2chan = cl2chan_next;
208 * This function lets a client join a channel. First, the function
209 * checks that the specified channel name is valid and that the client
210 * isn't already a member. If the specified channel doesn't exist,
211 * a new channel is created. Client is added to channel by function
215 Channel_Join( CLIENT *Client, const char *Name )
219 assert(Client != NULL);
220 assert(Name != NULL);
222 /* Check that the channel name is valid */
223 if (! Channel_IsValidName(Name)) {
224 IRC_WriteErrClient(Client, ERR_NOSUCHCHANNEL_MSG,
225 Client_ID(Client), Name);
229 chan = Channel_Search(Name);
231 /* Check if the client is already in the channel */
232 if (Get_Cl2Chan(chan, Client))
235 /* If the specified channel does not exist, the channel
237 chan = Channel_Create(Name);
242 /* Add user to Channel */
243 if (! Add_Client(chan, Client))
251 * Part client from channel.
252 * This function lets a client part from a channel. First, the function checks
253 * if the channel exists and the client is a member of it and sends out
254 * appropriate error messages if not. The real work is done by the function
258 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
262 assert(Client != NULL);
263 assert(Name != NULL);
264 assert(Reason != NULL);
266 /* Check that specified channel exists */
267 chan = Channel_Search(Name);
269 IRC_WriteErrClient(Client, ERR_NOSUCHCHANNEL_MSG,
270 Client_ID(Client), Name);
274 /* Check that the client is in the channel */
275 if (!Get_Cl2Chan(chan, Client)) {
276 IRC_WriteErrClient(Client, ERR_NOTONCHANNEL_MSG,
277 Client_ID(Client), Name);
281 if (Conf_MorePrivacy)
284 /* Part client from channel */
285 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
293 * Kick user from Channel
296 Channel_Kick(CLIENT *Peer, CLIENT *Target, CLIENT *Origin, const char *Name,
300 bool can_kick = false;
302 assert(Peer != NULL);
303 assert(Target != NULL);
304 assert(Origin != NULL);
305 assert(Name != NULL);
306 assert(Reason != NULL);
308 /* Check that channel exists */
309 chan = Channel_Search( Name );
311 IRC_WriteErrClient(Origin, ERR_NOSUCHCHANNEL_MSG,
312 Client_ID(Origin), Name);
316 if (Client_Type(Peer) != CLIENT_SERVER &&
317 Client_Type(Origin) != CLIENT_SERVICE) {
318 /* Check that user is on the specified channel */
319 if (!Channel_IsMemberOf(chan, Origin)) {
320 IRC_WriteErrClient(Origin, ERR_NOTONCHANNEL_MSG,
321 Client_ID(Origin), Name);
326 /* Check that the client to be kicked is on the specified channel */
327 if (!Channel_IsMemberOf(chan, Target)) {
328 IRC_WriteErrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
329 Client_ID(Origin), Client_ID(Target), Name );
333 if(Client_Type(Peer) == CLIENT_USER) {
334 /* Channel mode 'Q' and user mode 'q' on target: nobody but
335 * IRC Operators and servers can kick the target user */
336 if ((Channel_HasMode(chan, 'Q')
337 || Client_HasMode(Target, 'q')
338 || Client_Type(Target) == CLIENT_SERVICE)
339 && !Client_HasMode(Origin, 'o')) {
340 IRC_WriteErrClient(Origin, ERR_KICKDENY_MSG,
341 Client_ID(Origin), Name,
346 /* Check if client has the rights to kick target */
348 /* Owner can kick everyone */
349 if (Channel_UserHasMode(chan, Peer, 'q'))
352 /* Admin can't kick owner */
353 else if (Channel_UserHasMode(chan, Peer, 'a') &&
354 !Channel_UserHasMode(chan, Target, 'q'))
357 /* Op can't kick owner | admin */
358 else if (Channel_UserHasMode(chan, Peer, 'o') &&
359 !Channel_UserHasMode(chan, Target, 'q') &&
360 !Channel_UserHasMode(chan, Target, 'a'))
363 /* Half Op can't kick owner | admin | op */
364 else if (Channel_UserHasMode(chan, Peer, 'h') &&
365 !Channel_UserHasMode(chan, Target, 'q') &&
366 !Channel_UserHasMode(chan, Target, 'a') &&
367 !Channel_UserHasMode(chan, Target, 'o'))
371 IRC_WriteErrClient(Origin, ERR_CHANOPPRIVTOOLOW_MSG,
372 Client_ID(Origin), Name);
377 /* Kick Client from channel */
378 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
383 Channel_Quit( CLIENT *Client, const char *Reason )
387 assert( Client != NULL );
388 assert( Reason != NULL );
390 if (Conf_MorePrivacy)
393 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
399 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
406 * Get number of channels this server knows and that are "visible" to
407 * the given client. If no client is given, all channels will be counted.
409 * @param Client The client to check or NULL.
410 * @return Number of channels visible to the client.
413 Channel_CountVisible (CLIENT *Client)
416 unsigned long count = 0;
421 if (!Channel_HasMode(c, 's')
422 || Channel_IsMemberOf(c, Client))
433 Channel_MemberCount( CHANNEL *Chan )
436 unsigned long count = 0;
438 assert( Chan != NULL );
440 cl2chan = My_Cl2Chan;
443 if( cl2chan->channel == Chan ) count++;
444 cl2chan = cl2chan->next;
447 } /* Channel_MemberCount */
451 Channel_CountForUser( CLIENT *Client )
453 /* Count number of channels a user is member of. */
458 assert( Client != NULL );
460 cl2chan = My_Cl2Chan;
463 if( cl2chan->client == Client ) count++;
464 cl2chan = cl2chan->next;
468 } /* Channel_CountForUser */
472 Channel_Name( const CHANNEL *Chan )
474 assert( Chan != NULL );
480 Channel_Modes( CHANNEL *Chan )
482 assert( Chan != NULL );
484 } /* Channel_Modes */
488 Channel_HasMode( CHANNEL *Chan, char Mode )
490 assert( Chan != NULL );
491 return strchr( Chan->modes, Mode ) != NULL;
492 } /* Channel_HasMode */
496 Channel_Key( CHANNEL *Chan )
498 assert( Chan != NULL );
504 Channel_MaxUsers( CHANNEL *Chan )
506 assert( Chan != NULL );
507 return Chan->maxusers;
508 } /* Channel_MaxUsers */
512 Channel_First( void )
515 } /* Channel_First */
519 Channel_Next( CHANNEL *Chan )
521 assert( Chan != NULL );
527 Channel_Search( const char *Name )
529 /* Search channel structure */
534 assert( Name != NULL );
536 search_hash = Hash( Name );
540 if( search_hash == c->hash )
543 if( strcasecmp( Name, c->name ) == 0 ) return c;
548 } /* Channel_Search */
552 Channel_FirstMember( CHANNEL *Chan )
554 assert( Chan != NULL );
555 return Get_First_Cl2Chan( NULL, Chan );
556 } /* Channel_FirstMember */
560 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
562 assert( Chan != NULL );
563 assert( Cl2Chan != NULL );
564 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
565 } /* Channel_NextMember */
569 Channel_FirstChannelOf( CLIENT *Client )
571 assert( Client != NULL );
572 return Get_First_Cl2Chan( Client, NULL );
573 } /* Channel_FirstChannelOf */
577 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
579 assert( Client != NULL );
580 assert( Cl2Chan != NULL );
581 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
582 } /* Channel_NextChannelOf */
586 Channel_GetClient( CL2CHAN *Cl2Chan )
588 assert( Cl2Chan != NULL );
589 return Cl2Chan->client;
590 } /* Channel_GetClient */
594 Channel_GetChannel( CL2CHAN *Cl2Chan )
596 assert( Cl2Chan != NULL );
597 return Cl2Chan->channel;
598 } /* Channel_GetChannel */
602 Channel_IsValidName( const char *Name )
604 assert( Name != NULL );
607 if (strlen(Name) <= 1)
610 if (strchr("#&+", Name[0]) == NULL)
612 if (strlen(Name) >= CHANNEL_NAME_LEN)
615 return Name[strcspn(Name, " ,:\007")] == 0;
616 } /* Channel_IsValidName */
620 Channel_ModeAdd( CHANNEL *Chan, char Mode )
623 * If the channel already had this mode, return false.
624 * If the channel mode was newly set return true.
629 assert( Chan != NULL );
631 x[0] = Mode; x[1] = '\0';
632 if( ! Channel_HasMode( Chan, x[0] ))
634 /* Channel does not have this mode yet, set it */
635 strlcat( Chan->modes, x, sizeof( Chan->modes ));
639 } /* Channel_ModeAdd */
643 Channel_ModeDel( CHANNEL *Chan, char Mode )
646 * if the mode was removed return true.
647 * if the channel did not have the mode, return false.
651 assert( Chan != NULL );
653 p = strchr( Chan->modes, Mode );
654 if( ! p ) return false;
656 /* Channel has mode -> delete */
663 } /* Channel_ModeDel */
667 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
669 /* Set Channel-User-Mode.
670 * if mode was newly set, return true.
671 * if the User already had this channel-mode, return false.
677 assert( Chan != NULL );
678 assert( Client != NULL );
680 cl2chan = Get_Cl2Chan( Chan, Client );
681 assert( cl2chan != NULL );
683 x[0] = Mode; x[1] = '\0';
684 if( ! strchr( cl2chan->modes, x[0] ))
686 /* mode not set, -> set it */
687 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
691 } /* Channel_UserModeAdd */
695 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
697 /* Delete Channel-User-Mode.
698 * If Mode was removed, return true.
699 * If User did not have the Channel-Mode, return false.
705 assert( Chan != NULL );
706 assert( Client != NULL );
708 cl2chan = Get_Cl2Chan( Chan, Client );
709 assert( cl2chan != NULL );
711 p = strchr( cl2chan->modes, Mode );
712 if( ! p ) return false;
714 /* Client has Mode -> delete */
721 } /* Channel_UserModeDel */
725 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
727 /* return Users' Channel-Modes */
731 assert( Chan != NULL );
732 assert( Client != NULL );
734 cl2chan = Get_Cl2Chan( Chan, Client );
735 assert( cl2chan != NULL );
737 return cl2chan->modes;
738 } /* Channel_UserModes */
742 Channel_UserHasMode( CHANNEL *Chan, CLIENT *Client, char Mode )
744 return strchr(Channel_UserModes(Chan, Client), Mode) != NULL;
745 } /* Channel_UserHasMode */
749 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
751 /* Test if Client is on Channel Chan */
753 assert( Chan != NULL );
754 assert( Client != NULL );
755 return Get_Cl2Chan(Chan, Client) != NULL;
756 } /* Channel_IsMemberOf */
760 Channel_Topic( CHANNEL *Chan )
763 assert( Chan != NULL );
764 ret = array_start(&Chan->topic);
765 return ret ? ret : "";
766 } /* Channel_Topic */
772 Channel_TopicTime(CHANNEL *Chan)
774 assert(Chan != NULL);
775 return (unsigned int) Chan->topic_time;
776 } /* Channel_TopicTime */
780 Channel_TopicWho(CHANNEL *Chan)
782 assert(Chan != NULL);
783 return Chan->topic_who;
784 } /* Channel_TopicWho */
788 Channel_CreationTime(CHANNEL *Chan)
790 assert(Chan != NULL);
791 return (unsigned int) Chan->creation_time;
792 } /* Channel_CreationTime */
798 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
801 assert( Chan != NULL );
802 assert( Topic != NULL );
805 if (len < array_bytes(&Chan->topic))
806 array_free(&Chan->topic);
808 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
809 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
810 Topic, Chan->name, strerror(errno));
812 Chan->topic_time = time(NULL);
813 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
814 strlcpy(Chan->topic_who, Client_ID(Client),
815 sizeof Chan->topic_who);
817 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
818 sizeof Chan->topic_who);
822 } /* Channel_SetTopic */
826 Channel_SetModes( CHANNEL *Chan, const char *Modes )
828 assert( Chan != NULL );
829 assert( Modes != NULL );
831 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
832 } /* Channel_SetModes */
836 Channel_SetKey( CHANNEL *Chan, const char *Key )
838 assert( Chan != NULL );
839 assert( Key != NULL );
841 strlcpy( Chan->key, Key, sizeof( Chan->key ));
842 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
843 } /* Channel_SetKey */
847 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
849 assert( Chan != NULL );
851 Chan->maxusers = Count;
852 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
853 } /* Channel_SetMaxUsers */
857 * Check if a client is allowed to send to a specific channel.
859 * @param Chan The channel to check.
860 * @param From The client that wants to send.
861 * @return true if the client is allowed to send, false otherwise.
864 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
866 bool is_member, has_voice, is_halfop, is_op, is_chanadmin, is_owner;
868 is_member = has_voice = is_halfop = is_op = is_chanadmin = is_owner = false;
870 /* The server itself always can send messages :-) */
871 if (Client_ThisServer() == From)
874 if (Channel_IsMemberOf(Chan, From)) {
876 if (Channel_UserHasMode(Chan, From, 'v'))
878 if (Channel_UserHasMode(Chan, From, 'h'))
880 if (Channel_UserHasMode(Chan, From, 'o'))
882 if (Channel_UserHasMode(Chan, From, 'a'))
884 if (Channel_UserHasMode(Chan, From, 'q'))
889 * Is the client allowed to write to channel?
891 * If channel mode n set: non-members cannot send to channel.
892 * If channel mode m set: need voice.
894 if (Channel_HasMode(Chan, 'n') && !is_member)
897 if (Channel_HasMode(Chan, 'M') && !Client_HasMode(From, 'R')
898 && !Client_HasMode(From, 'o'))
901 if (has_voice || is_halfop || is_op || is_chanadmin || is_owner)
904 if (Channel_HasMode(Chan, 'm'))
907 if (Lists_Check(&Chan->list_excepts, From))
910 return !Lists_Check(&Chan->list_bans, From);
915 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
916 bool SendErrors, const char *Text)
918 if (!Can_Send_To_Channel(Chan, From)) {
920 return CONNECTED; /* no error, see RFC 2812 */
921 if (Channel_HasMode(Chan, 'M'))
922 return IRC_WriteErrClient(From, ERR_NEEDREGGEDNICK_MSG,
923 Client_ID(From), Channel_Name(Chan));
925 return IRC_WriteErrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
926 Client_ID(From), Channel_Name(Chan));
929 if (Client_Conn(From) > NONE)
930 Conn_UpdateIdle(Client_Conn(From));
932 IRC_WriteStrChannelPrefix(Client, Chan, From, true, "%s %s :%s",
933 Command, Channel_Name(Chan), Text);
939 Channel_Create( const char *Name )
941 /* Create new CHANNEL structure and add it to linked list */
944 assert( Name != NULL );
946 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
949 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
952 memset( c, 0, sizeof( CHANNEL ));
953 strlcpy( c->name, Name, sizeof( c->name ));
954 c->hash = Hash( c->name );
955 c->next = My_Channels;
957 c->creation_time = time(NULL);
960 LogDebug("Created new channel structure for \"%s\".", Name);
962 } /* Channel_Create */
966 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
970 assert( Chan != NULL );
971 assert( Client != NULL );
973 cl2chan = My_Cl2Chan;
976 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
977 cl2chan = cl2chan->next;
984 Add_Client( CHANNEL *Chan, CLIENT *Client )
988 assert( Chan != NULL );
989 assert( Client != NULL );
991 /* Create new CL2CHAN structure */
992 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
995 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
998 cl2chan->channel = Chan;
999 cl2chan->client = Client;
1000 strcpy( cl2chan->modes, "" );
1003 cl2chan->next = My_Cl2Chan;
1004 My_Cl2Chan = cl2chan;
1006 LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
1013 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
1015 CL2CHAN *cl2chan, *last_cl2chan;
1018 assert( Chan != NULL );
1019 assert( Client != NULL );
1020 assert( Origin != NULL );
1021 assert( Reason != NULL );
1023 /* Do not inform other servers if the channel is local to this server,
1024 * regardless of what the caller requested! */
1026 InformServer = !Channel_IsLocal(Chan);
1028 last_cl2chan = NULL;
1029 cl2chan = My_Cl2Chan;
1032 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
1033 last_cl2chan = cl2chan;
1034 cl2chan = cl2chan->next;
1036 if( ! cl2chan ) return false;
1038 c = cl2chan->channel;
1039 assert( c != NULL );
1041 /* maintain cl2chan list */
1042 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
1043 else My_Cl2Chan = cl2chan->next;
1049 /* QUIT: other servers have already been notified,
1050 * see Client_Destroy(); so only inform other clients
1051 * in same channel. */
1052 assert( InformServer == false );
1053 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1054 Client_Mask( Client ), c->name, Reason );
1057 /* User was KICKed: inform other servers (public
1058 * channels) and all users in the channel */
1060 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
1061 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
1062 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
1063 c->name, Client_ID( Client ), Reason );
1064 if ((Client_Conn(Client) > NONE) &&
1065 (Client_Type(Client) == CLIENT_USER))
1067 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
1068 c->name, Client_ID( Client ), Reason);
1070 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
1071 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
1074 if (Conf_MorePrivacy)
1078 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
1080 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
1083 if ((Client_Conn(Origin) > NONE) &&
1084 (Client_Type(Origin) == CLIENT_USER))
1086 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
1087 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1088 Client_Mask(Client), c->name, Reason);
1092 /* When channel is empty and is not pre-defined, delete */
1093 if( ! Channel_HasMode( Chan, 'P' ))
1095 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
1099 } /* Remove_Client */
1103 Channel_AddBan(CHANNEL *c, const char *mask )
1105 struct list_head *h = Channel_GetListBans(c);
1106 LogDebug("Adding \"%s\" to \"%s\" ban list", mask, Channel_Name(c));
1107 return Lists_Add(h, mask, false, NULL);
1112 Channel_AddExcept(CHANNEL *c, const char *mask )
1114 struct list_head *h = Channel_GetListExcepts(c);
1115 LogDebug("Adding \"%s\" to \"%s\" exception list", mask, Channel_Name(c));
1116 return Lists_Add(h, mask, false, NULL);
1121 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
1123 struct list_head *h = Channel_GetListInvites(c);
1124 LogDebug("Adding \"%s\" to \"%s\" invite list", mask, Channel_Name(c));
1125 return Lists_Add(h, mask, onlyonce, NULL);
1130 ShowChannelList(struct list_head *head, CLIENT *Client, CHANNEL *Channel,
1131 char *msg, char *msg_end)
1133 struct list_elem *e;
1135 assert (Client != NULL);
1136 assert (Channel != NULL);
1138 e = Lists_GetFirst(head);
1140 if (!IRC_WriteStrClient(Client, msg, Client_ID(Client),
1141 Channel_Name(Channel),
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);