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 LogDebug("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\" (%s, user limit %d).",
186 new_chan->name, new_chan->modes,
187 new_chan->key[0] ? "channel key set" : "no channel key",
191 /* Make sure the local &SERVER channel exists */
192 if (!Channel_Search("&SERVER")) {
193 new_chan = Channel_Create("&SERVER");
195 Channel_SetModes(new_chan, "mnPt");
196 Channel_SetTopic(new_chan, Client_ThisServer(),
199 Log(LOG_ERR, "Failed to create \"&SERVER\" channel!");
201 LogDebug("Required channel \"&SERVER\" already exists, ok.");
202 } /* Channel_InitPredefined */
206 Free_Channel(CHANNEL *chan)
208 array_free(&chan->topic);
209 array_free(&chan->keyfile);
210 Lists_Free(&chan->list_bans);
211 Lists_Free(&chan->list_excepts);
212 Lists_Free(&chan->list_invites);
222 CL2CHAN *cl2chan, *cl2chan_next;
224 /* free struct Channel */
232 /* Free Channel allocation table */
233 cl2chan = My_Cl2Chan;
235 cl2chan_next = cl2chan->next;
237 cl2chan = cl2chan_next;
244 * This function lets a client join a channel. First, the function
245 * checks that the specified channel name is valid and that the client
246 * isn't already a member. If the specified channel doesn't exist,
247 * a new channel is created. Client is added to channel by function
251 Channel_Join( CLIENT *Client, const char *Name )
255 assert(Client != NULL);
256 assert(Name != NULL);
258 /* Check that the channel name is valid */
259 if (! Channel_IsValidName(Name)) {
260 IRC_WriteErrClient(Client, ERR_NOSUCHCHANNEL_MSG,
261 Client_ID(Client), Name);
265 chan = Channel_Search(Name);
267 /* Check if the client is already in the channel */
268 if (Get_Cl2Chan(chan, Client))
271 /* If the specified channel does not exist, the channel
273 chan = Channel_Create(Name);
278 /* Add user to Channel */
279 if (! Add_Client(chan, Client))
287 * Part client from channel.
288 * This function lets a client part from a channel. First, the function checks
289 * if the channel exists and the client is a member of it and sends out
290 * appropriate error messages if not. The real work is done by the function
294 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
298 assert(Client != NULL);
299 assert(Name != NULL);
300 assert(Reason != NULL);
302 /* Check that specified channel exists */
303 chan = Channel_Search(Name);
305 IRC_WriteErrClient(Client, ERR_NOSUCHCHANNEL_MSG,
306 Client_ID(Client), Name);
310 /* Check that the client is in the channel */
311 if (!Get_Cl2Chan(chan, Client)) {
312 IRC_WriteErrClient(Client, ERR_NOTONCHANNEL_MSG,
313 Client_ID(Client), Name);
317 if (Conf_MorePrivacy)
320 /* Part client from channel */
321 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
329 * Kick user from Channel
332 Channel_Kick(CLIENT *Peer, CLIENT *Target, CLIENT *Origin, const char *Name,
336 bool can_kick = false;
338 assert(Peer != NULL);
339 assert(Target != NULL);
340 assert(Origin != NULL);
341 assert(Name != NULL);
342 assert(Reason != NULL);
344 /* Check that channel exists */
345 chan = Channel_Search( Name );
347 IRC_WriteErrClient(Origin, ERR_NOSUCHCHANNEL_MSG,
348 Client_ID(Origin), Name);
352 if (Client_Type(Peer) != CLIENT_SERVER &&
353 Client_Type(Origin) != CLIENT_SERVICE) {
354 /* Check that user is on the specified channel */
355 if (!Channel_IsMemberOf(chan, Origin)) {
356 IRC_WriteErrClient(Origin, ERR_NOTONCHANNEL_MSG,
357 Client_ID(Origin), Name);
362 /* Check that the client to be kicked is on the specified channel */
363 if (!Channel_IsMemberOf(chan, Target)) {
364 IRC_WriteErrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
365 Client_ID(Origin), Client_ID(Target), Name );
369 if(Client_Type(Peer) == CLIENT_USER) {
370 /* Channel mode 'Q' and user mode 'q' on target: nobody but
371 * IRC Operators and servers can kick the target user */
372 if ((Channel_HasMode(chan, 'Q')
373 || Client_HasMode(Target, 'q')
374 || Client_Type(Target) == CLIENT_SERVICE)
375 && !Client_HasMode(Origin, 'o')) {
376 IRC_WriteErrClient(Origin, ERR_KICKDENY_MSG,
377 Client_ID(Origin), Name,
382 /* Check if client has the rights to kick target */
384 /* Owner can kick everyone */
385 if (Channel_UserHasMode(chan, Peer, 'q'))
388 /* Admin can't kick owner */
389 else if (Channel_UserHasMode(chan, Peer, 'a') &&
390 !Channel_UserHasMode(chan, Target, 'q'))
393 /* Op can't kick owner | admin */
394 else if (Channel_UserHasMode(chan, Peer, 'o') &&
395 !Channel_UserHasMode(chan, Target, 'q') &&
396 !Channel_UserHasMode(chan, Target, 'a'))
399 /* Half Op can't kick owner | admin | op */
400 else if (Channel_UserHasMode(chan, Peer, 'h') &&
401 !Channel_UserHasMode(chan, Target, 'q') &&
402 !Channel_UserHasMode(chan, Target, 'a') &&
403 !Channel_UserHasMode(chan, Target, 'o'))
406 /* IRC operators & IRCd with OperCanMode enabled
407 * can kick anyways regardless of privilege */
408 else if(Client_HasMode(Origin, 'o') && Conf_OperCanMode)
412 IRC_WriteErrClient(Origin, ERR_CHANOPPRIVTOOLOW_MSG,
413 Client_ID(Origin), Name);
418 /* Kick Client from channel */
419 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
424 Channel_Quit( CLIENT *Client, const char *Reason )
428 assert( Client != NULL );
429 assert( Reason != NULL );
431 if (Conf_MorePrivacy)
434 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
440 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
447 * Get number of channels this server knows and that are "visible" to
448 * the given client. If no client is given, all channels will be counted.
450 * @param Client The client to check or NULL.
451 * @return Number of channels visible to the client.
454 Channel_CountVisible (CLIENT *Client)
457 unsigned long count = 0;
462 if (!Channel_HasMode(c, 's')
463 || Channel_IsMemberOf(c, Client))
474 Channel_MemberCount( CHANNEL *Chan )
477 unsigned long count = 0;
479 assert( Chan != NULL );
481 cl2chan = My_Cl2Chan;
484 if( cl2chan->channel == Chan ) count++;
485 cl2chan = cl2chan->next;
488 } /* Channel_MemberCount */
492 Channel_CountForUser( CLIENT *Client )
494 /* Count number of channels a user is member of. */
499 assert( Client != NULL );
501 cl2chan = My_Cl2Chan;
504 if( cl2chan->client == Client ) count++;
505 cl2chan = cl2chan->next;
509 } /* Channel_CountForUser */
513 Channel_Name( const CHANNEL *Chan )
515 assert( Chan != NULL );
521 Channel_Modes( CHANNEL *Chan )
523 assert( Chan != NULL );
525 } /* Channel_Modes */
529 Channel_HasMode( CHANNEL *Chan, char Mode )
531 assert( Chan != NULL );
532 return strchr( Chan->modes, Mode ) != NULL;
533 } /* Channel_HasMode */
537 Channel_Key( CHANNEL *Chan )
539 assert( Chan != NULL );
545 Channel_MaxUsers( CHANNEL *Chan )
547 assert( Chan != NULL );
548 return Chan->maxusers;
549 } /* Channel_MaxUsers */
553 Channel_First( void )
556 } /* Channel_First */
560 Channel_Next( CHANNEL *Chan )
562 assert( Chan != NULL );
568 Channel_Search( const char *Name )
570 /* Search channel structure */
575 assert( Name != NULL );
577 search_hash = Hash( Name );
581 if( search_hash == c->hash )
584 if( strcasecmp( Name, c->name ) == 0 ) return c;
589 } /* Channel_Search */
593 Channel_FirstMember( CHANNEL *Chan )
595 assert( Chan != NULL );
596 return Get_First_Cl2Chan( NULL, Chan );
597 } /* Channel_FirstMember */
601 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
603 assert( Chan != NULL );
604 assert( Cl2Chan != NULL );
605 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
606 } /* Channel_NextMember */
610 Channel_FirstChannelOf( CLIENT *Client )
612 assert( Client != NULL );
613 return Get_First_Cl2Chan( Client, NULL );
614 } /* Channel_FirstChannelOf */
618 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
620 assert( Client != NULL );
621 assert( Cl2Chan != NULL );
622 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
623 } /* Channel_NextChannelOf */
627 Channel_GetClient( CL2CHAN *Cl2Chan )
629 assert( Cl2Chan != NULL );
630 return Cl2Chan->client;
631 } /* Channel_GetClient */
635 Channel_GetChannel( CL2CHAN *Cl2Chan )
637 assert( Cl2Chan != NULL );
638 return Cl2Chan->channel;
639 } /* Channel_GetChannel */
643 Channel_IsValidName( const char *Name )
645 assert( Name != NULL );
648 if (strlen(Name) <= 1)
651 if (strchr("#&+", Name[0]) == NULL)
653 if (strlen(Name) >= CHANNEL_NAME_LEN)
656 return Name[strcspn(Name, " ,:\007")] == 0;
657 } /* Channel_IsValidName */
661 Channel_ModeAdd( CHANNEL *Chan, char Mode )
664 * If the channel already had this mode, return false.
665 * If the channel mode was newly set return true.
670 assert( Chan != NULL );
672 x[0] = Mode; x[1] = '\0';
673 if( ! Channel_HasMode( Chan, x[0] ))
675 /* Channel does not have this mode yet, set it */
676 strlcat( Chan->modes, x, sizeof( Chan->modes ));
680 } /* Channel_ModeAdd */
684 Channel_ModeDel( CHANNEL *Chan, char Mode )
687 * if the mode was removed return true.
688 * if the channel did not have the mode, return false.
692 assert( Chan != NULL );
694 p = strchr( Chan->modes, Mode );
695 if( ! p ) return false;
697 /* Channel has mode -> delete */
704 } /* Channel_ModeDel */
708 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
710 /* Set Channel-User-Mode.
711 * if mode was newly set, return true.
712 * if the User already had this channel-mode, return false.
718 assert( Chan != NULL );
719 assert( Client != NULL );
721 cl2chan = Get_Cl2Chan( Chan, Client );
722 assert( cl2chan != NULL );
724 x[0] = Mode; x[1] = '\0';
725 if( ! strchr( cl2chan->modes, x[0] ))
727 /* mode not set, -> set it */
728 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
732 } /* Channel_UserModeAdd */
736 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
738 /* Delete Channel-User-Mode.
739 * If Mode was removed, return true.
740 * If User did not have the Channel-Mode, return false.
746 assert( Chan != NULL );
747 assert( Client != NULL );
749 cl2chan = Get_Cl2Chan( Chan, Client );
750 assert( cl2chan != NULL );
752 p = strchr( cl2chan->modes, Mode );
753 if( ! p ) return false;
755 /* Client has Mode -> delete */
762 } /* Channel_UserModeDel */
766 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
768 /* return Users' Channel-Modes */
772 assert( Chan != NULL );
773 assert( Client != NULL );
775 cl2chan = Get_Cl2Chan( Chan, Client );
776 assert( cl2chan != NULL );
778 return cl2chan->modes;
779 } /* Channel_UserModes */
783 * Test if a user has a given channel user mode.
785 * @param Chan The channel to check.
786 * @param Client The client to check.
787 * @param Mode The channel user mode to test for.
788 * @return true if the user has the given channel user mode set.
791 Channel_UserHasMode( CHANNEL *Chan, CLIENT *Client, char Mode )
793 char *channel_user_modes;
795 assert(Chan != NULL);
796 assert(Client != NULL);
799 channel_user_modes = Channel_UserModes(Chan, Client);
800 if (!channel_user_modes || !*channel_user_modes)
803 return strchr(channel_user_modes, Mode) != NULL;
804 } /* Channel_UserHasMode */
808 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
810 /* Test if Client is on Channel Chan */
812 assert( Chan != NULL );
813 assert( Client != NULL );
814 return Get_Cl2Chan(Chan, Client) != NULL;
815 } /* Channel_IsMemberOf */
819 Channel_Topic( CHANNEL *Chan )
822 assert( Chan != NULL );
823 ret = array_start(&Chan->topic);
824 return ret ? ret : "";
825 } /* Channel_Topic */
831 Channel_TopicTime(CHANNEL *Chan)
833 assert(Chan != NULL);
834 return (unsigned int) Chan->topic_time;
835 } /* Channel_TopicTime */
839 Channel_TopicWho(CHANNEL *Chan)
841 assert(Chan != NULL);
842 return Chan->topic_who;
843 } /* Channel_TopicWho */
847 Channel_CreationTime(CHANNEL *Chan)
849 assert(Chan != NULL);
850 return (unsigned int) Chan->creation_time;
851 } /* Channel_CreationTime */
857 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
860 assert( Chan != NULL );
861 assert( Topic != NULL );
864 if (len < array_bytes(&Chan->topic))
865 array_free(&Chan->topic);
867 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
868 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
869 Topic, Chan->name, strerror(errno));
871 Chan->topic_time = time(NULL);
872 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
873 strlcpy(Chan->topic_who, Client_ID(Client),
874 sizeof Chan->topic_who);
876 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
877 sizeof Chan->topic_who);
881 } /* Channel_SetTopic */
885 Channel_SetModes( CHANNEL *Chan, const char *Modes )
887 assert( Chan != NULL );
888 assert( Modes != NULL );
890 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
891 } /* Channel_SetModes */
895 Channel_SetKey( CHANNEL *Chan, const char *Key )
897 assert( Chan != NULL );
898 assert( Key != NULL );
900 strlcpy( Chan->key, Key, sizeof( Chan->key ));
901 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
902 } /* Channel_SetKey */
906 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
908 assert( Chan != NULL );
910 Chan->maxusers = Count;
911 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
912 } /* Channel_SetMaxUsers */
916 * Check if a client is allowed to send to a specific channel.
918 * @param Chan The channel to check.
919 * @param From The client that wants to send.
920 * @return true if the client is allowed to send, false otherwise.
923 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
925 bool is_member, has_voice, is_halfop, is_op, is_chanadmin, is_owner;
927 is_member = has_voice = is_halfop = is_op = is_chanadmin = is_owner = false;
929 /* The server itself always can send messages :-) */
930 if (Client_ThisServer() == From)
933 if (Channel_IsMemberOf(Chan, From)) {
935 if (Channel_UserHasMode(Chan, From, 'v'))
937 if (Channel_UserHasMode(Chan, From, 'h'))
939 if (Channel_UserHasMode(Chan, From, 'o'))
941 if (Channel_UserHasMode(Chan, From, 'a'))
943 if (Channel_UserHasMode(Chan, From, 'q'))
948 * Is the client allowed to write to channel?
950 * If channel mode n set: non-members cannot send to channel.
951 * If channel mode m set: need voice.
953 if (Channel_HasMode(Chan, 'n') && !is_member)
956 if (Channel_HasMode(Chan, 'M') && !Client_HasMode(From, 'R')
957 && !Client_HasMode(From, 'o'))
960 if (has_voice || is_halfop || is_op || is_chanadmin || is_owner)
963 if (Channel_HasMode(Chan, 'm'))
966 if (Lists_Check(&Chan->list_excepts, From))
969 return !Lists_Check(&Chan->list_bans, From);
974 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
975 bool SendErrors, const char *Text)
977 if (!Can_Send_To_Channel(Chan, From)) {
979 return CONNECTED; /* no error, see RFC 2812 */
980 if (Channel_HasMode(Chan, 'M'))
981 return IRC_WriteErrClient(From, ERR_NEEDREGGEDNICK_MSG,
982 Client_ID(From), Channel_Name(Chan));
984 return IRC_WriteErrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
985 Client_ID(From), Channel_Name(Chan));
988 if (Client_Conn(From) > NONE)
989 Conn_UpdateIdle(Client_Conn(From));
991 IRC_WriteStrChannelPrefix(Client, Chan, From, true, "%s %s :%s",
992 Command, Channel_Name(Chan), Text);
998 Channel_Create( const char *Name )
1000 /* Create new CHANNEL structure and add it to linked list */
1003 assert( Name != NULL );
1005 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
1008 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
1011 memset( c, 0, sizeof( CHANNEL ));
1012 strlcpy( c->name, Name, sizeof( c->name ));
1013 c->hash = Hash( c->name );
1014 c->next = My_Channels;
1016 c->creation_time = time(NULL);
1019 LogDebug("Created new channel structure for \"%s\".", Name);
1021 } /* Channel_Create */
1025 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
1029 assert( Chan != NULL );
1030 assert( Client != NULL );
1032 cl2chan = My_Cl2Chan;
1035 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
1036 cl2chan = cl2chan->next;
1043 Add_Client( CHANNEL *Chan, CLIENT *Client )
1047 assert( Chan != NULL );
1048 assert( Client != NULL );
1050 /* Create new CL2CHAN structure */
1051 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
1054 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
1057 cl2chan->channel = Chan;
1058 cl2chan->client = Client;
1059 strcpy( cl2chan->modes, "" );
1062 cl2chan->next = My_Cl2Chan;
1063 My_Cl2Chan = cl2chan;
1065 LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
1072 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
1074 CL2CHAN *cl2chan, *last_cl2chan;
1077 assert( Chan != NULL );
1078 assert( Client != NULL );
1079 assert( Origin != NULL );
1080 assert( Reason != NULL );
1082 /* Do not inform other servers if the channel is local to this server,
1083 * regardless of what the caller requested! */
1085 InformServer = !Channel_IsLocal(Chan);
1087 last_cl2chan = NULL;
1088 cl2chan = My_Cl2Chan;
1091 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
1092 last_cl2chan = cl2chan;
1093 cl2chan = cl2chan->next;
1095 if( ! cl2chan ) return false;
1097 c = cl2chan->channel;
1098 assert( c != NULL );
1100 /* maintain cl2chan list */
1101 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
1102 else My_Cl2Chan = cl2chan->next;
1108 /* QUIT: other servers have already been notified,
1109 * see Client_Destroy(); so only inform other clients
1110 * in same channel. */
1111 assert( InformServer == false );
1112 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1113 Client_Mask( Client ), c->name, Reason );
1116 /* User was KICKed: inform other servers (public
1117 * channels) and all users in the channel */
1119 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
1120 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
1121 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
1122 c->name, Client_ID( Client ), Reason );
1123 if ((Client_Conn(Client) > NONE) &&
1124 (Client_Type(Client) == CLIENT_USER))
1126 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
1127 c->name, Client_ID( Client ), Reason);
1129 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
1130 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
1133 if (Conf_MorePrivacy)
1137 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
1139 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
1142 if ((Client_Conn(Origin) > NONE) &&
1143 (Client_Type(Origin) == CLIENT_USER))
1145 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
1146 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1147 Client_Mask(Client), c->name, Reason);
1151 /* When channel is empty and is not pre-defined, delete */
1152 if( ! Channel_HasMode( Chan, 'P' ))
1154 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
1158 } /* Remove_Client */
1162 Channel_AddBan(CHANNEL *c, const char *mask, const char *who )
1164 struct list_head *h = Channel_GetListBans(c);
1165 LogDebug("Adding \"%s\" to \"%s\" ban list", mask, Channel_Name(c));
1166 return Lists_Add(h, mask, time(NULL), who, false);
1171 Channel_AddExcept(CHANNEL *c, const char *mask, const char *who )
1173 struct list_head *h = Channel_GetListExcepts(c);
1174 LogDebug("Adding \"%s\" to \"%s\" exception list", mask, Channel_Name(c));
1175 return Lists_Add(h, mask, time(NULL), who, false);
1180 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce, const char *who )
1182 struct list_head *h = Channel_GetListInvites(c);
1183 LogDebug("Adding \"%s\" to \"%s\" invite list", mask, Channel_Name(c));
1184 return Lists_Add(h, mask, time(NULL), who, onlyonce);
1189 ShowChannelList(struct list_head *head, CLIENT *Client, CHANNEL *Channel,
1190 char *msg, char *msg_end)
1192 struct list_elem *e;
1194 assert (Client != NULL);
1195 assert (Channel != NULL);
1197 e = Lists_GetFirst(head);
1199 if (!IRC_WriteStrClient(Client, msg, Client_ID(Client),
1200 Channel_Name(Channel),
1203 Lists_GetValidity(e)))
1204 return DISCONNECTED;
1205 e = Lists_GetNext(e);
1208 return IRC_WriteStrClient(Client, msg_end, Client_ID(Client),
1209 Channel_Name(Channel));
1214 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1216 struct list_head *h;
1218 assert( Channel != NULL );
1220 h = Channel_GetListBans(Channel);
1221 return ShowChannelList(h, Client, Channel, RPL_BANLIST_MSG,
1222 RPL_ENDOFBANLIST_MSG);
1227 Channel_ShowExcepts( CLIENT *Client, CHANNEL *Channel )
1229 struct list_head *h;
1231 assert( Channel != NULL );
1233 h = Channel_GetListExcepts(Channel);
1234 return ShowChannelList(h, Client, Channel, RPL_EXCEPTLIST_MSG,
1235 RPL_ENDOFEXCEPTLIST_MSG);
1240 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1242 struct list_head *h;
1244 assert( Channel != NULL );
1246 h = Channel_GetListInvites(Channel);
1247 return ShowChannelList(h, Client, Channel, RPL_INVITELIST_MSG,
1248 RPL_ENDOFINVITELIST_MSG);
1253 * Log a message to the local &SERVER channel, if it exists.
1256 Channel_LogServer(const char *msg)
1261 assert(msg != NULL);
1263 sc = Channel_Search("&SERVER");
1267 c = Client_ThisServer();
1268 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1269 } /* Channel_LogServer */
1273 Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
1275 char *file_name, line[COMMAND_LEN], *nick, *pass;
1278 assert(Chan != NULL);
1279 assert(Client != NULL);
1280 assert(Key != NULL);
1282 if (!Channel_HasMode(Chan, 'k'))
1286 if (strcmp(Chan->key, Key) == 0)
1289 file_name = array_start(&Chan->keyfile);
1292 fd = fopen(file_name, "r");
1294 Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
1295 file_name, Chan->name, strerror(errno));
1299 while (fgets(line, (int)sizeof(line), fd) != NULL) {
1301 if (! (nick = strchr(line, ':')))
1304 if (!Match(line, Client_User(Client)))
1306 if (! (pass = strchr(nick, ':')))
1309 if (!Match(nick, Client_ID(Client)))
1311 if (strcmp(Key, pass) != 0)
1319 } /* Channel_CheckKey */
1323 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1325 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1326 } /* Get_First_Cl2Chan */
1330 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1334 assert( Client != NULL || Channel != NULL );
1339 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1340 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1341 cl2chan = cl2chan->next;
1344 } /* Get_Next_Cl2Chan */
1348 * Remove a channel and free all of its data structures.
1351 Delete_Channel(CHANNEL *Chan)
1353 CHANNEL *chan, *last_chan;
1364 assert(chan != NULL);
1368 /* maintain channel list */
1370 last_chan->next = chan->next;
1372 My_Channels = chan->next;
1374 LogDebug("Freed channel structure for \"%s\".", Chan->name);
1376 } /* Delete_Channel */
1380 Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
1384 assert(Chan != NULL);
1385 assert(KeyFile != NULL);
1387 len = strlen(KeyFile);
1388 if (len < array_bytes(&Chan->keyfile)) {
1389 Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
1390 array_free(&Chan->keyfile);
1396 if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
1398 "Could not set new channel key file \"%s\" for %s: %s",
1399 KeyFile, Chan->name, strerror(errno));
1401 Log(LOG_INFO|LOG_snotice,
1402 "New local channel key file \"%s\" for %s activated.",
1403 KeyFile, Chan->name);