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.
29 #include "conn-func.h"
33 #include "irc-write.h"
45 static CHANNEL *My_Channels;
46 static CL2CHAN *My_Cl2Chan;
48 static CL2CHAN *Get_Cl2Chan PARAMS(( CHANNEL *Chan, CLIENT *Client ));
49 static CL2CHAN *Add_Client PARAMS(( CHANNEL *Chan, CLIENT *Client ));
50 static bool Remove_Client PARAMS(( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer ));
51 static CL2CHAN *Get_First_Cl2Chan PARAMS(( CLIENT *Client, CHANNEL *Chan ));
52 static CL2CHAN *Get_Next_Cl2Chan PARAMS(( CL2CHAN *Start, CLIENT *Client, CHANNEL *Chan ));
53 static void Delete_Channel PARAMS(( CHANNEL *Chan ));
54 static void Free_Channel PARAMS(( CHANNEL *Chan ));
55 static void Set_KeyFile PARAMS((CHANNEL *Chan, const char *KeyFile));
66 GLOBAL struct list_head *
67 Channel_GetListBans(CHANNEL *c)
74 GLOBAL struct list_head *
75 Channel_GetListExcepts(CHANNEL *c)
78 return &c->list_excepts;
82 GLOBAL struct list_head *
83 Channel_GetListInvites(CHANNEL *c)
86 return &c->list_invites;
91 * Generate predefined persistent channels and &SERVER
94 Channel_InitPredefined( void )
97 const struct Conf_Channel *conf_chan;
99 size_t i, channel_count = array_length(&Conf_Channels, sizeof(*conf_chan));
101 conf_chan = array_start(&Conf_Channels);
103 assert(channel_count == 0 || conf_chan != NULL);
105 for (i = 0; i < channel_count; i++, conf_chan++) {
106 if (!conf_chan->name[0])
108 if (!Channel_IsValidName(conf_chan->name)) {
110 "Can't create pre-defined channel: invalid name: \"%s\"",
115 new_chan = Channel_Search(conf_chan->name);
118 "Can't create pre-defined channel \"%s\": name already in use.",
120 Set_KeyFile(new_chan, conf_chan->keyfile);
124 new_chan = Channel_Create(conf_chan->name);
126 Log(LOG_ERR, "Can't create pre-defined channel \"%s\"!",
130 Log(LOG_INFO, "Created pre-defined channel \"%s\".",
133 Channel_ModeAdd(new_chan, 'P');
135 if (conf_chan->topic[0])
136 Channel_SetTopic(new_chan, NULL, conf_chan->topic);
138 c = conf_chan->modes;
140 Channel_ModeAdd(new_chan, *c++);
142 Channel_SetKey(new_chan, conf_chan->key);
143 Channel_SetMaxUsers(new_chan, conf_chan->maxusers);
144 Set_KeyFile(new_chan, conf_chan->keyfile);
147 array_free(&Conf_Channels);
149 /* Make sure the local &SERVER channel exists */
150 if (!Channel_Search("&SERVER")) {
151 new_chan = Channel_Create("&SERVER");
153 Channel_SetModes(new_chan, "mnPt");
154 Channel_SetTopic(new_chan, Client_ThisServer(),
157 Log(LOG_ERR, "Failed to create \"&SERVER\" channel!");
159 LogDebug("Required channel \"&SERVER\" already exists, ok.");
160 } /* Channel_InitPredefined */
164 Free_Channel(CHANNEL *chan)
166 array_free(&chan->topic);
167 array_free(&chan->keyfile);
168 Lists_Free(&chan->list_bans);
169 Lists_Free(&chan->list_excepts);
170 Lists_Free(&chan->list_invites);
180 CL2CHAN *cl2chan, *cl2chan_next;
182 /* free struct Channel */
190 /* Free Channel allocation table */
191 cl2chan = My_Cl2Chan;
193 cl2chan_next = cl2chan->next;
195 cl2chan = cl2chan_next;
202 * This function lets a client join a channel. First, the function
203 * checks that the specified channel name is valid and that the client
204 * isn't already a member. If the specified channel doesn't exist,
205 * a new channel is created. Client is added to channel by function
209 Channel_Join( CLIENT *Client, const char *Name )
213 assert(Client != NULL);
214 assert(Name != NULL);
216 /* Check that the channel name is valid */
217 if (! Channel_IsValidName(Name)) {
218 IRC_WriteErrClient(Client, ERR_NOSUCHCHANNEL_MSG,
219 Client_ID(Client), Name);
223 chan = Channel_Search(Name);
225 /* Check if the client is already in the channel */
226 if (Get_Cl2Chan(chan, Client))
229 /* If the specified channel does not exist, the channel
231 chan = Channel_Create(Name);
236 /* Add user to Channel */
237 if (! Add_Client(chan, Client))
245 * Part client from channel.
246 * This function lets a client part from a channel. First, the function checks
247 * if the channel exists and the client is a member of it and sends out
248 * appropriate error messages if not. The real work is done by the function
252 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
256 assert(Client != NULL);
257 assert(Name != NULL);
258 assert(Reason != NULL);
260 /* Check that specified channel exists */
261 chan = Channel_Search(Name);
263 IRC_WriteErrClient(Client, ERR_NOSUCHCHANNEL_MSG,
264 Client_ID(Client), Name);
268 /* Check that the client is in the channel */
269 if (!Get_Cl2Chan(chan, Client)) {
270 IRC_WriteErrClient(Client, ERR_NOTONCHANNEL_MSG,
271 Client_ID(Client), Name);
275 if (Conf_MorePrivacy)
278 /* Part client from channel */
279 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
287 * Kick user from Channel
290 Channel_Kick(CLIENT *Peer, CLIENT *Target, CLIENT *Origin, const char *Name,
294 bool can_kick = false;
296 assert(Peer != NULL);
297 assert(Target != NULL);
298 assert(Origin != NULL);
299 assert(Name != NULL);
300 assert(Reason != NULL);
302 /* Check that channel exists */
303 chan = Channel_Search( Name );
305 IRC_WriteErrClient(Origin, ERR_NOSUCHCHANNEL_MSG,
306 Client_ID(Origin), Name);
310 if (Client_Type(Peer) != CLIENT_SERVER &&
311 Client_Type(Origin) != CLIENT_SERVICE) {
312 /* Check that user is on the specified channel */
313 if (!Channel_IsMemberOf(chan, Origin)) {
314 IRC_WriteErrClient(Origin, ERR_NOTONCHANNEL_MSG,
315 Client_ID(Origin), Name);
320 /* Check that the client to be kicked is on the specified channel */
321 if (!Channel_IsMemberOf(chan, Target)) {
322 IRC_WriteErrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
323 Client_ID(Origin), Client_ID(Target), Name );
327 if(Client_Type(Peer) == CLIENT_USER) {
328 /* Channel mode 'Q' and user mode 'q' on target: nobody but
329 * IRC Operators and servers can kick the target user */
330 if ((Channel_HasMode(chan, 'Q')
331 || Client_HasMode(Target, 'q')
332 || Client_Type(Target) == CLIENT_SERVICE)
333 && !Client_HasMode(Origin, 'o')) {
334 IRC_WriteErrClient(Origin, ERR_KICKDENY_MSG,
335 Client_ID(Origin), Name,
340 /* Check if client has the rights to kick target */
342 /* Owner can kick everyone */
343 if (Channel_UserHasMode(chan, Peer, 'q'))
346 /* Admin can't kick owner */
347 else if (Channel_UserHasMode(chan, Peer, 'a') &&
348 !Channel_UserHasMode(chan, Target, 'q'))
351 /* Op can't kick owner | admin */
352 else if (Channel_UserHasMode(chan, Peer, 'o') &&
353 !Channel_UserHasMode(chan, Target, 'q') &&
354 !Channel_UserHasMode(chan, Target, 'a'))
357 /* Half Op can't kick owner | admin | op */
358 else if (Channel_UserHasMode(chan, Peer, 'h') &&
359 !Channel_UserHasMode(chan, Target, 'q') &&
360 !Channel_UserHasMode(chan, Target, 'a') &&
361 !Channel_UserHasMode(chan, Target, 'o'))
365 IRC_WriteErrClient(Origin, ERR_CHANOPPRIVTOOLOW_MSG,
366 Client_ID(Origin), Name);
371 /* Kick Client from channel */
372 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
377 Channel_Quit( CLIENT *Client, const char *Reason )
381 assert( Client != NULL );
382 assert( Reason != NULL );
384 if (Conf_MorePrivacy)
387 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
393 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
400 * Get number of channels this server knows and that are "visible" to
401 * the given client. If no client is given, all channels will be counted.
403 * @param Client The client to check or NULL.
404 * @return Number of channels visible to the client.
407 Channel_CountVisible (CLIENT *Client)
410 unsigned long count = 0;
415 if (!Channel_HasMode(c, 's')
416 || Channel_IsMemberOf(c, Client))
427 Channel_MemberCount( CHANNEL *Chan )
430 unsigned long count = 0;
432 assert( Chan != NULL );
434 cl2chan = My_Cl2Chan;
437 if( cl2chan->channel == Chan ) count++;
438 cl2chan = cl2chan->next;
441 } /* Channel_MemberCount */
445 Channel_CountForUser( CLIENT *Client )
447 /* Count number of channels a user is member of. */
452 assert( Client != NULL );
454 cl2chan = My_Cl2Chan;
457 if( cl2chan->client == Client ) count++;
458 cl2chan = cl2chan->next;
462 } /* Channel_CountForUser */
466 Channel_Name( const CHANNEL *Chan )
468 assert( Chan != NULL );
474 Channel_Modes( CHANNEL *Chan )
476 assert( Chan != NULL );
478 } /* Channel_Modes */
482 Channel_HasMode( CHANNEL *Chan, char Mode )
484 assert( Chan != NULL );
485 return strchr( Chan->modes, Mode ) != NULL;
486 } /* Channel_HasMode */
490 Channel_Key( CHANNEL *Chan )
492 assert( Chan != NULL );
498 Channel_MaxUsers( CHANNEL *Chan )
500 assert( Chan != NULL );
501 return Chan->maxusers;
502 } /* Channel_MaxUsers */
506 Channel_First( void )
509 } /* Channel_First */
513 Channel_Next( CHANNEL *Chan )
515 assert( Chan != NULL );
521 Channel_Search( const char *Name )
523 /* Search channel structure */
528 assert( Name != NULL );
530 search_hash = Hash( Name );
534 if( search_hash == c->hash )
537 if( strcasecmp( Name, c->name ) == 0 ) return c;
542 } /* Channel_Search */
546 Channel_FirstMember( CHANNEL *Chan )
548 assert( Chan != NULL );
549 return Get_First_Cl2Chan( NULL, Chan );
550 } /* Channel_FirstMember */
554 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
556 assert( Chan != NULL );
557 assert( Cl2Chan != NULL );
558 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
559 } /* Channel_NextMember */
563 Channel_FirstChannelOf( CLIENT *Client )
565 assert( Client != NULL );
566 return Get_First_Cl2Chan( Client, NULL );
567 } /* Channel_FirstChannelOf */
571 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
573 assert( Client != NULL );
574 assert( Cl2Chan != NULL );
575 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
576 } /* Channel_NextChannelOf */
580 Channel_GetClient( CL2CHAN *Cl2Chan )
582 assert( Cl2Chan != NULL );
583 return Cl2Chan->client;
584 } /* Channel_GetClient */
588 Channel_GetChannel( CL2CHAN *Cl2Chan )
590 assert( Cl2Chan != NULL );
591 return Cl2Chan->channel;
592 } /* Channel_GetChannel */
596 Channel_IsValidName( const char *Name )
598 assert( Name != NULL );
601 if (strlen(Name) <= 1)
604 if (strchr("#&+", Name[0]) == NULL)
606 if (strlen(Name) >= CHANNEL_NAME_LEN)
609 return Name[strcspn(Name, " ,:\007")] == 0;
610 } /* Channel_IsValidName */
614 Channel_ModeAdd( CHANNEL *Chan, char Mode )
617 * If the channel already had this mode, return false.
618 * If the channel mode was newly set return true.
623 assert( Chan != NULL );
625 x[0] = Mode; x[1] = '\0';
626 if( ! Channel_HasMode( Chan, x[0] ))
628 /* Channel does not have this mode yet, set it */
629 strlcat( Chan->modes, x, sizeof( Chan->modes ));
633 } /* Channel_ModeAdd */
637 Channel_ModeDel( CHANNEL *Chan, char Mode )
640 * if the mode was removed return true.
641 * if the channel did not have the mode, return false.
645 assert( Chan != NULL );
647 p = strchr( Chan->modes, Mode );
648 if( ! p ) return false;
650 /* Channel has mode -> delete */
657 } /* Channel_ModeDel */
661 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
663 /* Set Channel-User-Mode.
664 * if mode was newly set, return true.
665 * if the User already had this channel-mode, return false.
671 assert( Chan != NULL );
672 assert( Client != NULL );
674 cl2chan = Get_Cl2Chan( Chan, Client );
675 assert( cl2chan != NULL );
677 x[0] = Mode; x[1] = '\0';
678 if( ! strchr( cl2chan->modes, x[0] ))
680 /* mode not set, -> set it */
681 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
685 } /* Channel_UserModeAdd */
689 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
691 /* Delete Channel-User-Mode.
692 * If Mode was removed, return true.
693 * If User did not have the Channel-Mode, return false.
699 assert( Chan != NULL );
700 assert( Client != NULL );
702 cl2chan = Get_Cl2Chan( Chan, Client );
703 assert( cl2chan != NULL );
705 p = strchr( cl2chan->modes, Mode );
706 if( ! p ) return false;
708 /* Client has Mode -> delete */
715 } /* Channel_UserModeDel */
719 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
721 /* return Users' Channel-Modes */
725 assert( Chan != NULL );
726 assert( Client != NULL );
728 cl2chan = Get_Cl2Chan( Chan, Client );
729 assert( cl2chan != NULL );
731 return cl2chan->modes;
732 } /* Channel_UserModes */
736 Channel_UserHasMode( CHANNEL *Chan, CLIENT *Client, char Mode )
738 return strchr(Channel_UserModes(Chan, Client), Mode) != NULL;
739 } /* Channel_UserHasMode */
743 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
745 /* Test if Client is on Channel Chan */
747 assert( Chan != NULL );
748 assert( Client != NULL );
749 return Get_Cl2Chan(Chan, Client) != NULL;
750 } /* Channel_IsMemberOf */
754 Channel_Topic( CHANNEL *Chan )
757 assert( Chan != NULL );
758 ret = array_start(&Chan->topic);
759 return ret ? ret : "";
760 } /* Channel_Topic */
766 Channel_TopicTime(CHANNEL *Chan)
768 assert(Chan != NULL);
769 return (unsigned int) Chan->topic_time;
770 } /* Channel_TopicTime */
774 Channel_TopicWho(CHANNEL *Chan)
776 assert(Chan != NULL);
777 return Chan->topic_who;
778 } /* Channel_TopicWho */
782 Channel_CreationTime(CHANNEL *Chan)
784 assert(Chan != NULL);
785 return (unsigned int) Chan->creation_time;
786 } /* Channel_CreationTime */
792 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
795 assert( Chan != NULL );
796 assert( Topic != NULL );
799 if (len < array_bytes(&Chan->topic))
800 array_free(&Chan->topic);
802 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
803 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
804 Topic, Chan->name, strerror(errno));
806 Chan->topic_time = time(NULL);
807 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
808 strlcpy(Chan->topic_who, Client_ID(Client),
809 sizeof Chan->topic_who);
811 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
812 sizeof Chan->topic_who);
816 } /* Channel_SetTopic */
820 Channel_SetModes( CHANNEL *Chan, const char *Modes )
822 assert( Chan != NULL );
823 assert( Modes != NULL );
825 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
826 } /* Channel_SetModes */
830 Channel_SetKey( CHANNEL *Chan, const char *Key )
832 assert( Chan != NULL );
833 assert( Key != NULL );
835 strlcpy( Chan->key, Key, sizeof( Chan->key ));
836 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
837 } /* Channel_SetKey */
841 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
843 assert( Chan != NULL );
845 Chan->maxusers = Count;
846 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
847 } /* Channel_SetMaxUsers */
851 * Check if a client is allowed to send to a specific channel.
853 * @param Chan The channel to check.
854 * @param From The client that wants to send.
855 * @return true if the client is allowed to send, false otherwise.
858 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
860 bool is_member, has_voice, is_halfop, is_op, is_chanadmin, is_owner;
862 is_member = has_voice = is_halfop = is_op = is_chanadmin = is_owner = false;
864 /* The server itself always can send messages :-) */
865 if (Client_ThisServer() == From)
868 if (Channel_IsMemberOf(Chan, From)) {
870 if (Channel_UserHasMode(Chan, From, 'v'))
872 if (Channel_UserHasMode(Chan, From, 'h'))
874 if (Channel_UserHasMode(Chan, From, 'o'))
876 if (Channel_UserHasMode(Chan, From, 'a'))
878 if (Channel_UserHasMode(Chan, From, 'q'))
883 * Is the client allowed to write to channel?
885 * If channel mode n set: non-members cannot send to channel.
886 * If channel mode m set: need voice.
888 if (Channel_HasMode(Chan, 'n') && !is_member)
891 if (Channel_HasMode(Chan, 'M') && !Client_HasMode(From, 'R')
892 && !Client_HasMode(From, 'o'))
895 if (has_voice || is_halfop || is_op || is_chanadmin || is_owner)
898 if (Channel_HasMode(Chan, 'm'))
901 if (Lists_Check(&Chan->list_excepts, From))
904 return !Lists_Check(&Chan->list_bans, From);
909 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
910 bool SendErrors, const char *Text)
912 if (!Can_Send_To_Channel(Chan, From)) {
914 return CONNECTED; /* no error, see RFC 2812 */
915 if (Channel_HasMode(Chan, 'M'))
916 return IRC_WriteErrClient(From, ERR_NEEDREGGEDNICK_MSG,
917 Client_ID(From), Channel_Name(Chan));
919 return IRC_WriteErrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
920 Client_ID(From), Channel_Name(Chan));
923 if (Client_Conn(From) > NONE)
924 Conn_UpdateIdle(Client_Conn(From));
926 IRC_WriteStrChannelPrefix(Client, Chan, From, true, "%s %s :%s",
927 Command, Channel_Name(Chan), Text);
933 Channel_Create( const char *Name )
935 /* Create new CHANNEL structure and add it to linked list */
938 assert( Name != NULL );
940 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
943 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
946 memset( c, 0, sizeof( CHANNEL ));
947 strlcpy( c->name, Name, sizeof( c->name ));
948 c->hash = Hash( c->name );
949 c->next = My_Channels;
951 c->creation_time = time(NULL);
954 LogDebug("Created new channel structure for \"%s\".", Name);
956 } /* Channel_Create */
960 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
964 assert( Chan != NULL );
965 assert( Client != NULL );
967 cl2chan = My_Cl2Chan;
970 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
971 cl2chan = cl2chan->next;
978 Add_Client( CHANNEL *Chan, CLIENT *Client )
982 assert( Chan != NULL );
983 assert( Client != NULL );
985 /* Create new CL2CHAN structure */
986 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
989 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
992 cl2chan->channel = Chan;
993 cl2chan->client = Client;
994 strcpy( cl2chan->modes, "" );
997 cl2chan->next = My_Cl2Chan;
998 My_Cl2Chan = cl2chan;
1000 LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
1007 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
1009 CL2CHAN *cl2chan, *last_cl2chan;
1012 assert( Chan != NULL );
1013 assert( Client != NULL );
1014 assert( Origin != NULL );
1015 assert( Reason != NULL );
1017 /* Do not inform other servers if the channel is local to this server,
1018 * regardless of what the caller requested! */
1020 InformServer = !Channel_IsLocal(Chan);
1022 last_cl2chan = NULL;
1023 cl2chan = My_Cl2Chan;
1026 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
1027 last_cl2chan = cl2chan;
1028 cl2chan = cl2chan->next;
1030 if( ! cl2chan ) return false;
1032 c = cl2chan->channel;
1033 assert( c != NULL );
1035 /* maintain cl2chan list */
1036 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
1037 else My_Cl2Chan = cl2chan->next;
1043 /* QUIT: other servers have already been notified,
1044 * see Client_Destroy(); so only inform other clients
1045 * in same channel. */
1046 assert( InformServer == false );
1047 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1048 Client_Mask( Client ), c->name, Reason );
1051 /* User was KICKed: inform other servers (public
1052 * channels) and all users in the channel */
1054 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
1055 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
1056 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
1057 c->name, Client_ID( Client ), Reason );
1058 if ((Client_Conn(Client) > NONE) &&
1059 (Client_Type(Client) == CLIENT_USER))
1061 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
1062 c->name, Client_ID( Client ), Reason);
1064 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
1065 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
1068 if (Conf_MorePrivacy)
1072 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
1074 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
1077 if ((Client_Conn(Origin) > NONE) &&
1078 (Client_Type(Origin) == CLIENT_USER))
1080 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
1081 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1082 Client_Mask(Client), c->name, Reason);
1086 /* When channel is empty and is not pre-defined, delete */
1087 if( ! Channel_HasMode( Chan, 'P' ))
1089 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
1093 } /* Remove_Client */
1097 Channel_AddBan(CHANNEL *c, const char *mask )
1099 struct list_head *h = Channel_GetListBans(c);
1100 LogDebug("Adding \"%s\" to \"%s\" ban list", mask, Channel_Name(c));
1101 return Lists_Add(h, mask, false, NULL);
1106 Channel_AddExcept(CHANNEL *c, const char *mask )
1108 struct list_head *h = Channel_GetListExcepts(c);
1109 LogDebug("Adding \"%s\" to \"%s\" exception list", mask, Channel_Name(c));
1110 return Lists_Add(h, mask, false, NULL);
1115 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
1117 struct list_head *h = Channel_GetListInvites(c);
1118 LogDebug("Adding \"%s\" to \"%s\" invite list", mask, Channel_Name(c));
1119 return Lists_Add(h, mask, onlyonce, NULL);
1124 ShowChannelList(struct list_head *head, CLIENT *Client, CHANNEL *Channel,
1125 char *msg, char *msg_end)
1127 struct list_elem *e;
1129 assert (Client != NULL);
1130 assert (Channel != NULL);
1132 e = Lists_GetFirst(head);
1134 if (!IRC_WriteStrClient(Client, msg, Client_ID(Client),
1135 Channel_Name(Channel),
1137 return DISCONNECTED;
1138 e = Lists_GetNext(e);
1141 return IRC_WriteStrClient(Client, msg_end, Client_ID(Client),
1142 Channel_Name(Channel));
1147 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1149 struct list_head *h;
1151 assert( Channel != NULL );
1153 h = Channel_GetListBans(Channel);
1154 return ShowChannelList(h, Client, Channel, RPL_BANLIST_MSG,
1155 RPL_ENDOFBANLIST_MSG);
1160 Channel_ShowExcepts( CLIENT *Client, CHANNEL *Channel )
1162 struct list_head *h;
1164 assert( Channel != NULL );
1166 h = Channel_GetListExcepts(Channel);
1167 return ShowChannelList(h, Client, Channel, RPL_EXCEPTLIST_MSG,
1168 RPL_ENDOFEXCEPTLIST_MSG);
1173 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1175 struct list_head *h;
1177 assert( Channel != NULL );
1179 h = Channel_GetListInvites(Channel);
1180 return ShowChannelList(h, Client, Channel, RPL_INVITELIST_MSG,
1181 RPL_ENDOFINVITELIST_MSG);
1186 * Log a message to the local &SERVER channel, if it exists.
1189 Channel_LogServer(const char *msg)
1194 assert(msg != NULL);
1196 sc = Channel_Search("&SERVER");
1200 c = Client_ThisServer();
1201 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1202 } /* Channel_LogServer */
1206 Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
1208 char *file_name, line[COMMAND_LEN], *nick, *pass;
1211 assert(Chan != NULL);
1212 assert(Client != NULL);
1213 assert(Key != NULL);
1215 if (!Channel_HasMode(Chan, 'k'))
1219 if (strcmp(Chan->key, Key) == 0)
1222 file_name = array_start(&Chan->keyfile);
1225 fd = fopen(file_name, "r");
1227 Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
1228 file_name, Chan->name, strerror(errno));
1232 while (fgets(line, (int)sizeof(line), fd) != NULL) {
1234 if (! (nick = strchr(line, ':')))
1237 if (!Match(line, Client_User(Client)))
1239 if (! (pass = strchr(nick, ':')))
1242 if (!Match(nick, Client_ID(Client)))
1244 if (strcmp(Key, pass) != 0)
1252 } /* Channel_CheckKey */
1256 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1258 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1259 } /* Get_First_Cl2Chan */
1263 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1267 assert( Client != NULL || Channel != NULL );
1272 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1273 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1274 cl2chan = cl2chan->next;
1277 } /* Get_Next_Cl2Chan */
1281 * Remove a channel and free all of its data structures.
1284 Delete_Channel(CHANNEL *Chan)
1286 CHANNEL *chan, *last_chan;
1297 assert(chan != NULL);
1301 /* maintain channel list */
1303 last_chan->next = chan->next;
1305 My_Channels = chan->next;
1307 LogDebug("Freed channel structure for \"%s\".", Chan->name);
1309 } /* Delete_Channel */
1313 Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
1317 assert(Chan != NULL);
1318 assert(KeyFile != NULL);
1320 len = strlen(KeyFile);
1321 if (len < array_bytes(&Chan->keyfile)) {
1322 Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
1323 array_free(&Chan->keyfile);
1329 if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
1331 "Could not set new channel key file \"%s\" for %s: %s",
1332 KeyFile, Chan->name, strerror(errno));
1334 Log(LOG_INFO|LOG_snotice,
1335 "New local channel key file \"%s\" for %s activated.",
1336 KeyFile, Chan->name);