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"
46 static CHANNEL *My_Channels;
47 static CL2CHAN *My_Cl2Chan;
49 static CL2CHAN *Get_Cl2Chan PARAMS(( CHANNEL *Chan, CLIENT *Client ));
50 static CL2CHAN *Add_Client PARAMS(( CHANNEL *Chan, CLIENT *Client ));
51 static bool Remove_Client PARAMS(( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer ));
52 static CL2CHAN *Get_First_Cl2Chan PARAMS(( CLIENT *Client, CHANNEL *Chan ));
53 static CL2CHAN *Get_Next_Cl2Chan PARAMS(( CL2CHAN *Start, CLIENT *Client, CHANNEL *Chan ));
54 static void Delete_Channel PARAMS(( CHANNEL *Chan ));
55 static void Free_Channel PARAMS(( CHANNEL *Chan ));
56 static void Set_KeyFile PARAMS((CHANNEL *Chan, const char *KeyFile));
67 GLOBAL struct list_head *
68 Channel_GetListBans(CHANNEL *c)
75 GLOBAL struct list_head *
76 Channel_GetListExcepts(CHANNEL *c)
79 return &c->list_excepts;
83 GLOBAL struct list_head *
84 Channel_GetListInvites(CHANNEL *c)
87 return &c->list_invites;
92 * Generate predefined persistent channels and &SERVER
95 Channel_InitPredefined( void )
99 const struct Conf_Channel *conf_chan;
101 char modes[COMMAND_LEN], name[CHANNEL_NAME_LEN];
102 size_t i, n, channel_count = array_length(&Conf_Channels, sizeof(*conf_chan));
104 conf_chan = array_start(&Conf_Channels);
106 assert(channel_count == 0 || conf_chan != NULL);
108 for (i = 0; i < channel_count; i++, conf_chan++) {
109 if (!conf_chan->name[0])
111 if (!Channel_IsValidName(conf_chan->name)) {
113 "Can't create pre-defined channel: invalid name: \"%s\"",
118 new_chan = Channel_Search(conf_chan->name);
121 "Can't create pre-defined channel \"%s\": name already in use.",
123 Set_KeyFile(new_chan, conf_chan->keyfile);
127 new_chan = Channel_Create(conf_chan->name);
129 Log(LOG_ERR, "Can't create 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 /* Evaluate modes strings with fake requests */
139 if (conf_chan->modes_num) {
140 /* Prepare fake request structure */
141 strlcpy(name, conf_chan->name, sizeof(name));
142 Log(LOG_INFO, "Evaluating predefined channel modes for \"%s\".", name);
144 Req.prefix = Client_ID(Client_ThisServer());
145 Req.command = "MODE";
147 /* Iterate over channel modes strings */
148 for (n = 0; n < conf_chan->modes_num; n++) {
150 strlcpy(modes, conf_chan->modes[n], sizeof(modes));
151 LogDebug("Evaluate \"MODE %s %s\".", name, modes);
152 c = strtok(modes, " ");
153 while (c && Req.argc < 15) {
154 Req.argv[Req.argc++] = c;
159 /* Handling of legacy "Key" and "MaxUsers" settings:
160 * Enforce setting the respective mode(s), to support
161 * the legacy "Mode = kl" notation, which was valid but
162 * is an invalid MODE string: key and limit are missing!
163 * So set them manually when "k" or "l" are detected in
164 * the first MODE parameter ... */
165 if (Req.argc > 1 && strchr(Req.argv[1], 'k')) {
166 Channel_SetKey(new_chan, conf_chan->key);
167 Channel_ModeAdd(new_chan, 'k');
169 if (strchr(Req.argv[1], 'l')) {
170 Channel_SetMaxUsers(new_chan, conf_chan->maxusers);
171 Channel_ModeAdd(new_chan, 'l');
174 IRC_MODE(Client_ThisServer(), &Req);
177 /* Original channel modes strings are no longer needed */
178 free(conf_chan->modes[n]);
182 Set_KeyFile(new_chan, conf_chan->keyfile);
185 "Created pre-defined channel \"%s\", mode \"%s\" (key \"%s\", limit %d).",
186 new_chan->name, new_chan->modes, new_chan->key,
190 /* Make sure the local &SERVER channel exists */
191 if (!Channel_Search("&SERVER")) {
192 new_chan = Channel_Create("&SERVER");
194 Channel_SetModes(new_chan, "mnPt");
195 Channel_SetTopic(new_chan, Client_ThisServer(),
198 Log(LOG_ERR, "Failed to create \"&SERVER\" channel!");
200 LogDebug("Required channel \"&SERVER\" already exists, ok.");
201 } /* Channel_InitPredefined */
205 Free_Channel(CHANNEL *chan)
207 array_free(&chan->topic);
208 array_free(&chan->keyfile);
209 Lists_Free(&chan->list_bans);
210 Lists_Free(&chan->list_excepts);
211 Lists_Free(&chan->list_invites);
221 CL2CHAN *cl2chan, *cl2chan_next;
223 /* free struct Channel */
231 /* Free Channel allocation table */
232 cl2chan = My_Cl2Chan;
234 cl2chan_next = cl2chan->next;
236 cl2chan = cl2chan_next;
243 * This function lets a client join a channel. First, the function
244 * checks that the specified channel name is valid and that the client
245 * isn't already a member. If the specified channel doesn't exist,
246 * a new channel is created. Client is added to channel by function
250 Channel_Join( CLIENT *Client, const char *Name )
254 assert(Client != NULL);
255 assert(Name != NULL);
257 /* Check that the channel name is valid */
258 if (! Channel_IsValidName(Name)) {
259 IRC_WriteErrClient(Client, ERR_NOSUCHCHANNEL_MSG,
260 Client_ID(Client), Name);
264 chan = Channel_Search(Name);
266 /* Check if the client is already in the channel */
267 if (Get_Cl2Chan(chan, Client))
270 /* If the specified channel does not exist, the channel
272 chan = Channel_Create(Name);
277 /* Add user to Channel */
278 if (! Add_Client(chan, Client))
286 * Part client from channel.
287 * This function lets a client part from a channel. First, the function checks
288 * if the channel exists and the client is a member of it and sends out
289 * appropriate error messages if not. The real work is done by the function
293 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
297 assert(Client != NULL);
298 assert(Name != NULL);
299 assert(Reason != NULL);
301 /* Check that specified channel exists */
302 chan = Channel_Search(Name);
304 IRC_WriteErrClient(Client, ERR_NOSUCHCHANNEL_MSG,
305 Client_ID(Client), Name);
309 /* Check that the client is in the channel */
310 if (!Get_Cl2Chan(chan, Client)) {
311 IRC_WriteErrClient(Client, ERR_NOTONCHANNEL_MSG,
312 Client_ID(Client), Name);
316 if (Conf_MorePrivacy)
319 /* Part client from channel */
320 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
328 * Kick user from Channel
331 Channel_Kick(CLIENT *Peer, CLIENT *Target, CLIENT *Origin, const char *Name,
335 bool can_kick = false;
337 assert(Peer != NULL);
338 assert(Target != NULL);
339 assert(Origin != NULL);
340 assert(Name != NULL);
341 assert(Reason != NULL);
343 /* Check that channel exists */
344 chan = Channel_Search( Name );
346 IRC_WriteErrClient(Origin, ERR_NOSUCHCHANNEL_MSG,
347 Client_ID(Origin), Name);
351 if (Client_Type(Peer) != CLIENT_SERVER &&
352 Client_Type(Origin) != CLIENT_SERVICE) {
353 /* Check that user is on the specified channel */
354 if (!Channel_IsMemberOf(chan, Origin)) {
355 IRC_WriteErrClient(Origin, ERR_NOTONCHANNEL_MSG,
356 Client_ID(Origin), Name);
361 /* Check that the client to be kicked is on the specified channel */
362 if (!Channel_IsMemberOf(chan, Target)) {
363 IRC_WriteErrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
364 Client_ID(Origin), Client_ID(Target), Name );
368 if(Client_Type(Peer) == CLIENT_USER) {
369 /* Channel mode 'Q' and user mode 'q' on target: nobody but
370 * IRC Operators and servers can kick the target user */
371 if ((Channel_HasMode(chan, 'Q')
372 || Client_HasMode(Target, 'q')
373 || Client_Type(Target) == CLIENT_SERVICE)
374 && !Client_HasMode(Origin, 'o')) {
375 IRC_WriteErrClient(Origin, ERR_KICKDENY_MSG,
376 Client_ID(Origin), Name,
381 /* Check if client has the rights to kick target */
383 /* Owner can kick everyone */
384 if (Channel_UserHasMode(chan, Peer, 'q'))
387 /* Admin can't kick owner */
388 else if (Channel_UserHasMode(chan, Peer, 'a') &&
389 !Channel_UserHasMode(chan, Target, 'q'))
392 /* Op can't kick owner | admin */
393 else if (Channel_UserHasMode(chan, Peer, 'o') &&
394 !Channel_UserHasMode(chan, Target, 'q') &&
395 !Channel_UserHasMode(chan, Target, 'a'))
398 /* Half Op can't kick owner | admin | op */
399 else if (Channel_UserHasMode(chan, Peer, 'h') &&
400 !Channel_UserHasMode(chan, Target, 'q') &&
401 !Channel_UserHasMode(chan, Target, 'a') &&
402 !Channel_UserHasMode(chan, Target, 'o'))
405 /* IRC operators & IRCd with OperCanMode enabled
406 * can kick anyways regardless of privilege */
407 else if(Client_HasMode(Origin, 'o') && Conf_OperCanMode)
411 IRC_WriteErrClient(Origin, ERR_CHANOPPRIVTOOLOW_MSG,
412 Client_ID(Origin), Name);
417 /* Kick Client from channel */
418 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
423 Channel_Quit( CLIENT *Client, const char *Reason )
427 assert( Client != NULL );
428 assert( Reason != NULL );
430 if (Conf_MorePrivacy)
433 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
439 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
446 * Get number of channels this server knows and that are "visible" to
447 * the given client. If no client is given, all channels will be counted.
449 * @param Client The client to check or NULL.
450 * @return Number of channels visible to the client.
453 Channel_CountVisible (CLIENT *Client)
456 unsigned long count = 0;
461 if (!Channel_HasMode(c, 's')
462 || Channel_IsMemberOf(c, Client))
473 Channel_MemberCount( CHANNEL *Chan )
476 unsigned long count = 0;
478 assert( Chan != NULL );
480 cl2chan = My_Cl2Chan;
483 if( cl2chan->channel == Chan ) count++;
484 cl2chan = cl2chan->next;
487 } /* Channel_MemberCount */
491 Channel_CountForUser( CLIENT *Client )
493 /* Count number of channels a user is member of. */
498 assert( Client != NULL );
500 cl2chan = My_Cl2Chan;
503 if( cl2chan->client == Client ) count++;
504 cl2chan = cl2chan->next;
508 } /* Channel_CountForUser */
512 Channel_Name( const CHANNEL *Chan )
514 assert( Chan != NULL );
520 Channel_Modes( CHANNEL *Chan )
522 assert( Chan != NULL );
524 } /* Channel_Modes */
528 Channel_HasMode( CHANNEL *Chan, char Mode )
530 assert( Chan != NULL );
531 return strchr( Chan->modes, Mode ) != NULL;
532 } /* Channel_HasMode */
536 Channel_Key( CHANNEL *Chan )
538 assert( Chan != NULL );
544 Channel_MaxUsers( CHANNEL *Chan )
546 assert( Chan != NULL );
547 return Chan->maxusers;
548 } /* Channel_MaxUsers */
552 Channel_First( void )
555 } /* Channel_First */
559 Channel_Next( CHANNEL *Chan )
561 assert( Chan != NULL );
567 Channel_Search( const char *Name )
569 /* Search channel structure */
574 assert( Name != NULL );
576 search_hash = Hash( Name );
580 if( search_hash == c->hash )
583 if( strcasecmp( Name, c->name ) == 0 ) return c;
588 } /* Channel_Search */
592 Channel_FirstMember( CHANNEL *Chan )
594 assert( Chan != NULL );
595 return Get_First_Cl2Chan( NULL, Chan );
596 } /* Channel_FirstMember */
600 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
602 assert( Chan != NULL );
603 assert( Cl2Chan != NULL );
604 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
605 } /* Channel_NextMember */
609 Channel_FirstChannelOf( CLIENT *Client )
611 assert( Client != NULL );
612 return Get_First_Cl2Chan( Client, NULL );
613 } /* Channel_FirstChannelOf */
617 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
619 assert( Client != NULL );
620 assert( Cl2Chan != NULL );
621 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
622 } /* Channel_NextChannelOf */
626 Channel_GetClient( CL2CHAN *Cl2Chan )
628 assert( Cl2Chan != NULL );
629 return Cl2Chan->client;
630 } /* Channel_GetClient */
634 Channel_GetChannel( CL2CHAN *Cl2Chan )
636 assert( Cl2Chan != NULL );
637 return Cl2Chan->channel;
638 } /* Channel_GetChannel */
642 Channel_IsValidName( const char *Name )
644 assert( Name != NULL );
647 if (strlen(Name) <= 1)
650 if (strchr("#&+", Name[0]) == NULL)
652 if (strlen(Name) >= CHANNEL_NAME_LEN)
655 return Name[strcspn(Name, " ,:\007")] == 0;
656 } /* Channel_IsValidName */
660 Channel_ModeAdd( CHANNEL *Chan, char Mode )
663 * If the channel already had this mode, return false.
664 * If the channel mode was newly set return true.
669 assert( Chan != NULL );
671 x[0] = Mode; x[1] = '\0';
672 if( ! Channel_HasMode( Chan, x[0] ))
674 /* Channel does not have this mode yet, set it */
675 strlcat( Chan->modes, x, sizeof( Chan->modes ));
679 } /* Channel_ModeAdd */
683 Channel_ModeDel( CHANNEL *Chan, char Mode )
686 * if the mode was removed return true.
687 * if the channel did not have the mode, return false.
691 assert( Chan != NULL );
693 p = strchr( Chan->modes, Mode );
694 if( ! p ) return false;
696 /* Channel has mode -> delete */
703 } /* Channel_ModeDel */
707 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
709 /* Set Channel-User-Mode.
710 * if mode was newly set, return true.
711 * if the User already had this channel-mode, return false.
717 assert( Chan != NULL );
718 assert( Client != NULL );
720 cl2chan = Get_Cl2Chan( Chan, Client );
721 assert( cl2chan != NULL );
723 x[0] = Mode; x[1] = '\0';
724 if( ! strchr( cl2chan->modes, x[0] ))
726 /* mode not set, -> set it */
727 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
731 } /* Channel_UserModeAdd */
735 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
737 /* Delete Channel-User-Mode.
738 * If Mode was removed, return true.
739 * If User did not have the Channel-Mode, return false.
745 assert( Chan != NULL );
746 assert( Client != NULL );
748 cl2chan = Get_Cl2Chan( Chan, Client );
749 assert( cl2chan != NULL );
751 p = strchr( cl2chan->modes, Mode );
752 if( ! p ) return false;
754 /* Client has Mode -> delete */
761 } /* Channel_UserModeDel */
765 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
767 /* return Users' Channel-Modes */
771 assert( Chan != NULL );
772 assert( Client != NULL );
774 cl2chan = Get_Cl2Chan( Chan, Client );
775 assert( cl2chan != NULL );
777 return cl2chan->modes;
778 } /* Channel_UserModes */
782 * Test if a user has a given channel user mode.
784 * @param Chan The channel to check.
785 * @param Client The client to check.
786 * @param Mode The channel user mode to test for.
787 * @return true if the user has the given channel user mode set.
790 Channel_UserHasMode( CHANNEL *Chan, CLIENT *Client, char Mode )
792 char *channel_user_modes;
794 assert(Chan != NULL);
795 assert(Client != NULL);
798 channel_user_modes = Channel_UserModes(Chan, Client);
799 if (!channel_user_modes || !*channel_user_modes)
802 return strchr(channel_user_modes, Mode) != NULL;
803 } /* Channel_UserHasMode */
807 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
809 /* Test if Client is on Channel Chan */
811 assert( Chan != NULL );
812 assert( Client != NULL );
813 return Get_Cl2Chan(Chan, Client) != NULL;
814 } /* Channel_IsMemberOf */
818 Channel_Topic( CHANNEL *Chan )
821 assert( Chan != NULL );
822 ret = array_start(&Chan->topic);
823 return ret ? ret : "";
824 } /* Channel_Topic */
830 Channel_TopicTime(CHANNEL *Chan)
832 assert(Chan != NULL);
833 return (unsigned int) Chan->topic_time;
834 } /* Channel_TopicTime */
838 Channel_TopicWho(CHANNEL *Chan)
840 assert(Chan != NULL);
841 return Chan->topic_who;
842 } /* Channel_TopicWho */
846 Channel_CreationTime(CHANNEL *Chan)
848 assert(Chan != NULL);
849 return (unsigned int) Chan->creation_time;
850 } /* Channel_CreationTime */
856 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
859 assert( Chan != NULL );
860 assert( Topic != NULL );
863 if (len < array_bytes(&Chan->topic))
864 array_free(&Chan->topic);
866 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
867 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
868 Topic, Chan->name, strerror(errno));
870 Chan->topic_time = time(NULL);
871 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
872 strlcpy(Chan->topic_who, Client_ID(Client),
873 sizeof Chan->topic_who);
875 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
876 sizeof Chan->topic_who);
880 } /* Channel_SetTopic */
884 Channel_SetModes( CHANNEL *Chan, const char *Modes )
886 assert( Chan != NULL );
887 assert( Modes != NULL );
889 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
890 } /* Channel_SetModes */
894 Channel_SetKey( CHANNEL *Chan, const char *Key )
896 assert( Chan != NULL );
897 assert( Key != NULL );
899 strlcpy( Chan->key, Key, sizeof( Chan->key ));
900 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
901 } /* Channel_SetKey */
905 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
907 assert( Chan != NULL );
909 Chan->maxusers = Count;
910 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
911 } /* Channel_SetMaxUsers */
915 * Check if a client is allowed to send to a specific channel.
917 * @param Chan The channel to check.
918 * @param From The client that wants to send.
919 * @return true if the client is allowed to send, false otherwise.
922 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
924 bool is_member, has_voice, is_halfop, is_op, is_chanadmin, is_owner;
926 is_member = has_voice = is_halfop = is_op = is_chanadmin = is_owner = false;
928 /* The server itself always can send messages :-) */
929 if (Client_ThisServer() == From)
932 if (Channel_IsMemberOf(Chan, From)) {
934 if (Channel_UserHasMode(Chan, From, 'v'))
936 if (Channel_UserHasMode(Chan, From, 'h'))
938 if (Channel_UserHasMode(Chan, From, 'o'))
940 if (Channel_UserHasMode(Chan, From, 'a'))
942 if (Channel_UserHasMode(Chan, From, 'q'))
947 * Is the client allowed to write to channel?
949 * If channel mode n set: non-members cannot send to channel.
950 * If channel mode m set: need voice.
952 if (Channel_HasMode(Chan, 'n') && !is_member)
955 if (Channel_HasMode(Chan, 'M') && !Client_HasMode(From, 'R')
956 && !Client_HasMode(From, 'o'))
959 if (has_voice || is_halfop || is_op || is_chanadmin || is_owner)
962 if (Channel_HasMode(Chan, 'm'))
965 if (Lists_Check(&Chan->list_excepts, From))
968 return !Lists_Check(&Chan->list_bans, From);
973 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
974 bool SendErrors, const char *Text)
976 if (!Can_Send_To_Channel(Chan, From)) {
978 return CONNECTED; /* no error, see RFC 2812 */
979 if (Channel_HasMode(Chan, 'M'))
980 return IRC_WriteErrClient(From, ERR_NEEDREGGEDNICK_MSG,
981 Client_ID(From), Channel_Name(Chan));
983 return IRC_WriteErrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
984 Client_ID(From), Channel_Name(Chan));
987 if (Client_Conn(From) > NONE)
988 Conn_UpdateIdle(Client_Conn(From));
990 IRC_WriteStrChannelPrefix(Client, Chan, From, true, "%s %s :%s",
991 Command, Channel_Name(Chan), Text);
997 Channel_Create( const char *Name )
999 /* Create new CHANNEL structure and add it to linked list */
1002 assert( Name != NULL );
1004 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
1007 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
1010 memset( c, 0, sizeof( CHANNEL ));
1011 strlcpy( c->name, Name, sizeof( c->name ));
1012 c->hash = Hash( c->name );
1013 c->next = My_Channels;
1015 c->creation_time = time(NULL);
1018 LogDebug("Created new channel structure for \"%s\".", Name);
1020 } /* Channel_Create */
1024 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
1028 assert( Chan != NULL );
1029 assert( Client != NULL );
1031 cl2chan = My_Cl2Chan;
1034 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
1035 cl2chan = cl2chan->next;
1042 Add_Client( CHANNEL *Chan, CLIENT *Client )
1046 assert( Chan != NULL );
1047 assert( Client != NULL );
1049 /* Create new CL2CHAN structure */
1050 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
1053 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
1056 cl2chan->channel = Chan;
1057 cl2chan->client = Client;
1058 strcpy( cl2chan->modes, "" );
1061 cl2chan->next = My_Cl2Chan;
1062 My_Cl2Chan = cl2chan;
1064 LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
1071 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
1073 CL2CHAN *cl2chan, *last_cl2chan;
1076 assert( Chan != NULL );
1077 assert( Client != NULL );
1078 assert( Origin != NULL );
1079 assert( Reason != NULL );
1081 /* Do not inform other servers if the channel is local to this server,
1082 * regardless of what the caller requested! */
1084 InformServer = !Channel_IsLocal(Chan);
1086 last_cl2chan = NULL;
1087 cl2chan = My_Cl2Chan;
1090 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
1091 last_cl2chan = cl2chan;
1092 cl2chan = cl2chan->next;
1094 if( ! cl2chan ) return false;
1096 c = cl2chan->channel;
1097 assert( c != NULL );
1099 /* maintain cl2chan list */
1100 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
1101 else My_Cl2Chan = cl2chan->next;
1107 /* QUIT: other servers have already been notified,
1108 * see Client_Destroy(); so only inform other clients
1109 * in same channel. */
1110 assert( InformServer == false );
1111 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1112 Client_Mask( Client ), c->name, Reason );
1115 /* User was KICKed: inform other servers (public
1116 * channels) and all users in the channel */
1118 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
1119 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
1120 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
1121 c->name, Client_ID( Client ), Reason );
1122 if ((Client_Conn(Client) > NONE) &&
1123 (Client_Type(Client) == CLIENT_USER))
1125 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
1126 c->name, Client_ID( Client ), Reason);
1128 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
1129 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
1132 if (Conf_MorePrivacy)
1136 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
1138 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
1141 if ((Client_Conn(Origin) > NONE) &&
1142 (Client_Type(Origin) == CLIENT_USER))
1144 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
1145 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1146 Client_Mask(Client), c->name, Reason);
1150 /* When channel is empty and is not pre-defined, delete */
1151 if( ! Channel_HasMode( Chan, 'P' ))
1153 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
1157 } /* Remove_Client */
1161 Channel_AddBan(CHANNEL *c, const char *mask, const char *who )
1163 struct list_head *h = Channel_GetListBans(c);
1164 LogDebug("Adding \"%s\" to \"%s\" ban list", mask, Channel_Name(c));
1165 return Lists_Add(h, mask, time(NULL), who, false);
1170 Channel_AddExcept(CHANNEL *c, const char *mask, const char *who )
1172 struct list_head *h = Channel_GetListExcepts(c);
1173 LogDebug("Adding \"%s\" to \"%s\" exception list", mask, Channel_Name(c));
1174 return Lists_Add(h, mask, time(NULL), who, false);
1179 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce, const char *who )
1181 struct list_head *h = Channel_GetListInvites(c);
1182 LogDebug("Adding \"%s\" to \"%s\" invite list", mask, Channel_Name(c));
1183 return Lists_Add(h, mask, time(NULL), who, onlyonce);
1188 ShowChannelList(struct list_head *head, CLIENT *Client, CHANNEL *Channel,
1189 char *msg, char *msg_end)
1191 struct list_elem *e;
1193 assert (Client != NULL);
1194 assert (Channel != NULL);
1196 e = Lists_GetFirst(head);
1198 if (!IRC_WriteStrClient(Client, msg, Client_ID(Client),
1199 Channel_Name(Channel),
1202 Lists_GetValidity(e)))
1203 return DISCONNECTED;
1204 e = Lists_GetNext(e);
1207 return IRC_WriteStrClient(Client, msg_end, Client_ID(Client),
1208 Channel_Name(Channel));
1213 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1215 struct list_head *h;
1217 assert( Channel != NULL );
1219 h = Channel_GetListBans(Channel);
1220 return ShowChannelList(h, Client, Channel, RPL_BANLIST_MSG,
1221 RPL_ENDOFBANLIST_MSG);
1226 Channel_ShowExcepts( CLIENT *Client, CHANNEL *Channel )
1228 struct list_head *h;
1230 assert( Channel != NULL );
1232 h = Channel_GetListExcepts(Channel);
1233 return ShowChannelList(h, Client, Channel, RPL_EXCEPTLIST_MSG,
1234 RPL_ENDOFEXCEPTLIST_MSG);
1239 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1241 struct list_head *h;
1243 assert( Channel != NULL );
1245 h = Channel_GetListInvites(Channel);
1246 return ShowChannelList(h, Client, Channel, RPL_INVITELIST_MSG,
1247 RPL_ENDOFINVITELIST_MSG);
1252 * Log a message to the local &SERVER channel, if it exists.
1255 Channel_LogServer(const char *msg)
1260 assert(msg != NULL);
1262 sc = Channel_Search("&SERVER");
1266 c = Client_ThisServer();
1267 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1268 } /* Channel_LogServer */
1272 Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
1274 char *file_name, line[COMMAND_LEN], *nick, *pass;
1277 assert(Chan != NULL);
1278 assert(Client != NULL);
1279 assert(Key != NULL);
1281 if (!Channel_HasMode(Chan, 'k'))
1285 if (strcmp(Chan->key, Key) == 0)
1288 file_name = array_start(&Chan->keyfile);
1291 fd = fopen(file_name, "r");
1293 Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
1294 file_name, Chan->name, strerror(errno));
1298 while (fgets(line, (int)sizeof(line), fd) != NULL) {
1300 if (! (nick = strchr(line, ':')))
1303 if (!Match(line, Client_User(Client)))
1305 if (! (pass = strchr(nick, ':')))
1308 if (!Match(nick, Client_ID(Client)))
1310 if (strcmp(Key, pass) != 0)
1318 } /* Channel_CheckKey */
1322 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1324 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1325 } /* Get_First_Cl2Chan */
1329 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1333 assert( Client != NULL || Channel != NULL );
1338 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1339 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1340 cl2chan = cl2chan->next;
1343 } /* Get_Next_Cl2Chan */
1347 * Remove a channel and free all of its data structures.
1350 Delete_Channel(CHANNEL *Chan)
1352 CHANNEL *chan, *last_chan;
1363 assert(chan != NULL);
1367 /* maintain channel list */
1369 last_chan->next = chan->next;
1371 My_Channels = chan->next;
1373 LogDebug("Freed channel structure for \"%s\".", Chan->name);
1375 } /* Delete_Channel */
1379 Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
1383 assert(Chan != NULL);
1384 assert(KeyFile != NULL);
1386 len = strlen(KeyFile);
1387 if (len < array_bytes(&Chan->keyfile)) {
1388 Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
1389 array_free(&Chan->keyfile);
1395 if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
1397 "Could not set new channel key file \"%s\" for %s: %s",
1398 KeyFile, Chan->name, strerror(errno));
1400 Log(LOG_INFO|LOG_snotice,
1401 "New local channel key file \"%s\" for %s activated.",
1402 KeyFile, Chan->name);