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 Log(LOG_DEBUG, "Evaluate \"MODE %s %s\".", name, modes);
152 c = strtok(modes, " ");
153 while (c && Req.argc < 15) {
154 Req.argv[Req.argc++] = c;
158 IRC_MODE(Client_ThisServer(), &Req);
160 /* Original channel modes srings are no longer needed */
161 free(conf_chan->modes[n]);
165 Channel_SetKey(new_chan, conf_chan->key);
166 Channel_SetMaxUsers(new_chan, conf_chan->maxusers);
167 Set_KeyFile(new_chan, conf_chan->keyfile);
170 "Created pre-defined channel \"%s\", mode \"%s\" (key \"%s\", limit %d).",
171 new_chan->name, new_chan->modes, new_chan->key,
175 array_free(&Conf_Channels);
177 /* Make sure the local &SERVER channel exists */
178 if (!Channel_Search("&SERVER")) {
179 new_chan = Channel_Create("&SERVER");
181 Channel_SetModes(new_chan, "mnPt");
182 Channel_SetTopic(new_chan, Client_ThisServer(),
185 Log(LOG_ERR, "Failed to create \"&SERVER\" channel!");
187 LogDebug("Required channel \"&SERVER\" already exists, ok.");
188 } /* Channel_InitPredefined */
192 Free_Channel(CHANNEL *chan)
194 array_free(&chan->topic);
195 array_free(&chan->keyfile);
196 Lists_Free(&chan->list_bans);
197 Lists_Free(&chan->list_excepts);
198 Lists_Free(&chan->list_invites);
208 CL2CHAN *cl2chan, *cl2chan_next;
210 /* free struct Channel */
218 /* Free Channel allocation table */
219 cl2chan = My_Cl2Chan;
221 cl2chan_next = cl2chan->next;
223 cl2chan = cl2chan_next;
230 * This function lets a client join a channel. First, the function
231 * checks that the specified channel name is valid and that the client
232 * isn't already a member. If the specified channel doesn't exist,
233 * a new channel is created. Client is added to channel by function
237 Channel_Join( CLIENT *Client, const char *Name )
241 assert(Client != NULL);
242 assert(Name != NULL);
244 /* Check that the channel name is valid */
245 if (! Channel_IsValidName(Name)) {
246 IRC_WriteErrClient(Client, ERR_NOSUCHCHANNEL_MSG,
247 Client_ID(Client), Name);
251 chan = Channel_Search(Name);
253 /* Check if the client is already in the channel */
254 if (Get_Cl2Chan(chan, Client))
257 /* If the specified channel does not exist, the channel
259 chan = Channel_Create(Name);
264 /* Add user to Channel */
265 if (! Add_Client(chan, Client))
273 * Part client from channel.
274 * This function lets a client part from a channel. First, the function checks
275 * if the channel exists and the client is a member of it and sends out
276 * appropriate error messages if not. The real work is done by the function
280 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
284 assert(Client != NULL);
285 assert(Name != NULL);
286 assert(Reason != NULL);
288 /* Check that specified channel exists */
289 chan = Channel_Search(Name);
291 IRC_WriteErrClient(Client, ERR_NOSUCHCHANNEL_MSG,
292 Client_ID(Client), Name);
296 /* Check that the client is in the channel */
297 if (!Get_Cl2Chan(chan, Client)) {
298 IRC_WriteErrClient(Client, ERR_NOTONCHANNEL_MSG,
299 Client_ID(Client), Name);
303 if (Conf_MorePrivacy)
306 /* Part client from channel */
307 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
315 * Kick user from Channel
318 Channel_Kick(CLIENT *Peer, CLIENT *Target, CLIENT *Origin, const char *Name,
322 bool can_kick = false;
324 assert(Peer != NULL);
325 assert(Target != NULL);
326 assert(Origin != NULL);
327 assert(Name != NULL);
328 assert(Reason != NULL);
330 /* Check that channel exists */
331 chan = Channel_Search( Name );
333 IRC_WriteErrClient(Origin, ERR_NOSUCHCHANNEL_MSG,
334 Client_ID(Origin), Name);
338 if (Client_Type(Peer) != CLIENT_SERVER &&
339 Client_Type(Origin) != CLIENT_SERVICE) {
340 /* Check that user is on the specified channel */
341 if (!Channel_IsMemberOf(chan, Origin)) {
342 IRC_WriteErrClient(Origin, ERR_NOTONCHANNEL_MSG,
343 Client_ID(Origin), Name);
348 /* Check that the client to be kicked is on the specified channel */
349 if (!Channel_IsMemberOf(chan, Target)) {
350 IRC_WriteErrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
351 Client_ID(Origin), Client_ID(Target), Name );
355 if(Client_Type(Peer) == CLIENT_USER) {
356 /* Channel mode 'Q' and user mode 'q' on target: nobody but
357 * IRC Operators and servers can kick the target user */
358 if ((Channel_HasMode(chan, 'Q')
359 || Client_HasMode(Target, 'q')
360 || Client_Type(Target) == CLIENT_SERVICE)
361 && !Client_HasMode(Origin, 'o')) {
362 IRC_WriteErrClient(Origin, ERR_KICKDENY_MSG,
363 Client_ID(Origin), Name,
368 /* Check if client has the rights to kick target */
370 /* Owner can kick everyone */
371 if (Channel_UserHasMode(chan, Peer, 'q'))
374 /* Admin can't kick owner */
375 else if (Channel_UserHasMode(chan, Peer, 'a') &&
376 !Channel_UserHasMode(chan, Target, 'q'))
379 /* Op can't kick owner | admin */
380 else if (Channel_UserHasMode(chan, Peer, 'o') &&
381 !Channel_UserHasMode(chan, Target, 'q') &&
382 !Channel_UserHasMode(chan, Target, 'a'))
385 /* Half Op can't kick owner | admin | op */
386 else if (Channel_UserHasMode(chan, Peer, 'h') &&
387 !Channel_UserHasMode(chan, Target, 'q') &&
388 !Channel_UserHasMode(chan, Target, 'a') &&
389 !Channel_UserHasMode(chan, Target, 'o'))
392 /* IRC operators & IRCd with OperCanMode enabled
393 * can kick anyways regardless of privilege */
394 else if(Client_HasMode(Origin, 'o') && Conf_OperCanMode)
398 IRC_WriteErrClient(Origin, ERR_CHANOPPRIVTOOLOW_MSG,
399 Client_ID(Origin), Name);
404 /* Kick Client from channel */
405 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
410 Channel_Quit( CLIENT *Client, const char *Reason )
414 assert( Client != NULL );
415 assert( Reason != NULL );
417 if (Conf_MorePrivacy)
420 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
426 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
433 * Get number of channels this server knows and that are "visible" to
434 * the given client. If no client is given, all channels will be counted.
436 * @param Client The client to check or NULL.
437 * @return Number of channels visible to the client.
440 Channel_CountVisible (CLIENT *Client)
443 unsigned long count = 0;
448 if (!Channel_HasMode(c, 's')
449 || Channel_IsMemberOf(c, Client))
460 Channel_MemberCount( CHANNEL *Chan )
463 unsigned long count = 0;
465 assert( Chan != NULL );
467 cl2chan = My_Cl2Chan;
470 if( cl2chan->channel == Chan ) count++;
471 cl2chan = cl2chan->next;
474 } /* Channel_MemberCount */
478 Channel_CountForUser( CLIENT *Client )
480 /* Count number of channels a user is member of. */
485 assert( Client != NULL );
487 cl2chan = My_Cl2Chan;
490 if( cl2chan->client == Client ) count++;
491 cl2chan = cl2chan->next;
495 } /* Channel_CountForUser */
499 Channel_Name( const CHANNEL *Chan )
501 assert( Chan != NULL );
507 Channel_Modes( CHANNEL *Chan )
509 assert( Chan != NULL );
511 } /* Channel_Modes */
515 Channel_HasMode( CHANNEL *Chan, char Mode )
517 assert( Chan != NULL );
518 return strchr( Chan->modes, Mode ) != NULL;
519 } /* Channel_HasMode */
523 Channel_Key( CHANNEL *Chan )
525 assert( Chan != NULL );
531 Channel_MaxUsers( CHANNEL *Chan )
533 assert( Chan != NULL );
534 return Chan->maxusers;
535 } /* Channel_MaxUsers */
539 Channel_First( void )
542 } /* Channel_First */
546 Channel_Next( CHANNEL *Chan )
548 assert( Chan != NULL );
554 Channel_Search( const char *Name )
556 /* Search channel structure */
561 assert( Name != NULL );
563 search_hash = Hash( Name );
567 if( search_hash == c->hash )
570 if( strcasecmp( Name, c->name ) == 0 ) return c;
575 } /* Channel_Search */
579 Channel_FirstMember( CHANNEL *Chan )
581 assert( Chan != NULL );
582 return Get_First_Cl2Chan( NULL, Chan );
583 } /* Channel_FirstMember */
587 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
589 assert( Chan != NULL );
590 assert( Cl2Chan != NULL );
591 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
592 } /* Channel_NextMember */
596 Channel_FirstChannelOf( CLIENT *Client )
598 assert( Client != NULL );
599 return Get_First_Cl2Chan( Client, NULL );
600 } /* Channel_FirstChannelOf */
604 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
606 assert( Client != NULL );
607 assert( Cl2Chan != NULL );
608 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
609 } /* Channel_NextChannelOf */
613 Channel_GetClient( CL2CHAN *Cl2Chan )
615 assert( Cl2Chan != NULL );
616 return Cl2Chan->client;
617 } /* Channel_GetClient */
621 Channel_GetChannel( CL2CHAN *Cl2Chan )
623 assert( Cl2Chan != NULL );
624 return Cl2Chan->channel;
625 } /* Channel_GetChannel */
629 Channel_IsValidName( const char *Name )
631 assert( Name != NULL );
634 if (strlen(Name) <= 1)
637 if (strchr("#&+", Name[0]) == NULL)
639 if (strlen(Name) >= CHANNEL_NAME_LEN)
642 return Name[strcspn(Name, " ,:\007")] == 0;
643 } /* Channel_IsValidName */
647 Channel_ModeAdd( CHANNEL *Chan, char Mode )
650 * If the channel already had this mode, return false.
651 * If the channel mode was newly set return true.
656 assert( Chan != NULL );
658 x[0] = Mode; x[1] = '\0';
659 if( ! Channel_HasMode( Chan, x[0] ))
661 /* Channel does not have this mode yet, set it */
662 strlcat( Chan->modes, x, sizeof( Chan->modes ));
666 } /* Channel_ModeAdd */
670 Channel_ModeDel( CHANNEL *Chan, char Mode )
673 * if the mode was removed return true.
674 * if the channel did not have the mode, return false.
678 assert( Chan != NULL );
680 p = strchr( Chan->modes, Mode );
681 if( ! p ) return false;
683 /* Channel has mode -> delete */
690 } /* Channel_ModeDel */
694 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
696 /* Set Channel-User-Mode.
697 * if mode was newly set, return true.
698 * if the User already had this channel-mode, return false.
704 assert( Chan != NULL );
705 assert( Client != NULL );
707 cl2chan = Get_Cl2Chan( Chan, Client );
708 assert( cl2chan != NULL );
710 x[0] = Mode; x[1] = '\0';
711 if( ! strchr( cl2chan->modes, x[0] ))
713 /* mode not set, -> set it */
714 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
718 } /* Channel_UserModeAdd */
722 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
724 /* Delete Channel-User-Mode.
725 * If Mode was removed, return true.
726 * If User did not have the Channel-Mode, return false.
732 assert( Chan != NULL );
733 assert( Client != NULL );
735 cl2chan = Get_Cl2Chan( Chan, Client );
736 assert( cl2chan != NULL );
738 p = strchr( cl2chan->modes, Mode );
739 if( ! p ) return false;
741 /* Client has Mode -> delete */
748 } /* Channel_UserModeDel */
752 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
754 /* return Users' Channel-Modes */
758 assert( Chan != NULL );
759 assert( Client != NULL );
761 cl2chan = Get_Cl2Chan( Chan, Client );
762 assert( cl2chan != NULL );
764 return cl2chan->modes;
765 } /* Channel_UserModes */
769 Channel_UserHasMode( CHANNEL *Chan, CLIENT *Client, char Mode )
771 return strchr(Channel_UserModes(Chan, Client), Mode) != NULL;
772 } /* Channel_UserHasMode */
776 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
778 /* Test if Client is on Channel Chan */
780 assert( Chan != NULL );
781 assert( Client != NULL );
782 return Get_Cl2Chan(Chan, Client) != NULL;
783 } /* Channel_IsMemberOf */
787 Channel_Topic( CHANNEL *Chan )
790 assert( Chan != NULL );
791 ret = array_start(&Chan->topic);
792 return ret ? ret : "";
793 } /* Channel_Topic */
799 Channel_TopicTime(CHANNEL *Chan)
801 assert(Chan != NULL);
802 return (unsigned int) Chan->topic_time;
803 } /* Channel_TopicTime */
807 Channel_TopicWho(CHANNEL *Chan)
809 assert(Chan != NULL);
810 return Chan->topic_who;
811 } /* Channel_TopicWho */
815 Channel_CreationTime(CHANNEL *Chan)
817 assert(Chan != NULL);
818 return (unsigned int) Chan->creation_time;
819 } /* Channel_CreationTime */
825 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
828 assert( Chan != NULL );
829 assert( Topic != NULL );
832 if (len < array_bytes(&Chan->topic))
833 array_free(&Chan->topic);
835 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
836 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
837 Topic, Chan->name, strerror(errno));
839 Chan->topic_time = time(NULL);
840 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
841 strlcpy(Chan->topic_who, Client_ID(Client),
842 sizeof Chan->topic_who);
844 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
845 sizeof Chan->topic_who);
849 } /* Channel_SetTopic */
853 Channel_SetModes( CHANNEL *Chan, const char *Modes )
855 assert( Chan != NULL );
856 assert( Modes != NULL );
858 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
859 } /* Channel_SetModes */
863 Channel_SetKey( CHANNEL *Chan, const char *Key )
865 assert( Chan != NULL );
866 assert( Key != NULL );
868 strlcpy( Chan->key, Key, sizeof( Chan->key ));
869 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
870 } /* Channel_SetKey */
874 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
876 assert( Chan != NULL );
878 Chan->maxusers = Count;
879 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
880 } /* Channel_SetMaxUsers */
884 * Check if a client is allowed to send to a specific channel.
886 * @param Chan The channel to check.
887 * @param From The client that wants to send.
888 * @return true if the client is allowed to send, false otherwise.
891 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
893 bool is_member, has_voice, is_halfop, is_op, is_chanadmin, is_owner;
895 is_member = has_voice = is_halfop = is_op = is_chanadmin = is_owner = false;
897 /* The server itself always can send messages :-) */
898 if (Client_ThisServer() == From)
901 if (Channel_IsMemberOf(Chan, From)) {
903 if (Channel_UserHasMode(Chan, From, 'v'))
905 if (Channel_UserHasMode(Chan, From, 'h'))
907 if (Channel_UserHasMode(Chan, From, 'o'))
909 if (Channel_UserHasMode(Chan, From, 'a'))
911 if (Channel_UserHasMode(Chan, From, 'q'))
916 * Is the client allowed to write to channel?
918 * If channel mode n set: non-members cannot send to channel.
919 * If channel mode m set: need voice.
921 if (Channel_HasMode(Chan, 'n') && !is_member)
924 if (Channel_HasMode(Chan, 'M') && !Client_HasMode(From, 'R')
925 && !Client_HasMode(From, 'o'))
928 if (has_voice || is_halfop || is_op || is_chanadmin || is_owner)
931 if (Channel_HasMode(Chan, 'm'))
934 if (Lists_Check(&Chan->list_excepts, From))
937 return !Lists_Check(&Chan->list_bans, From);
942 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
943 bool SendErrors, const char *Text)
945 if (!Can_Send_To_Channel(Chan, From)) {
947 return CONNECTED; /* no error, see RFC 2812 */
948 if (Channel_HasMode(Chan, 'M'))
949 return IRC_WriteErrClient(From, ERR_NEEDREGGEDNICK_MSG,
950 Client_ID(From), Channel_Name(Chan));
952 return IRC_WriteErrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
953 Client_ID(From), Channel_Name(Chan));
956 if (Client_Conn(From) > NONE)
957 Conn_UpdateIdle(Client_Conn(From));
959 IRC_WriteStrChannelPrefix(Client, Chan, From, true, "%s %s :%s",
960 Command, Channel_Name(Chan), Text);
966 Channel_Create( const char *Name )
968 /* Create new CHANNEL structure and add it to linked list */
971 assert( Name != NULL );
973 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
976 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
979 memset( c, 0, sizeof( CHANNEL ));
980 strlcpy( c->name, Name, sizeof( c->name ));
981 c->hash = Hash( c->name );
982 c->next = My_Channels;
984 c->creation_time = time(NULL);
987 LogDebug("Created new channel structure for \"%s\".", Name);
989 } /* Channel_Create */
993 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
997 assert( Chan != NULL );
998 assert( Client != NULL );
1000 cl2chan = My_Cl2Chan;
1003 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
1004 cl2chan = cl2chan->next;
1011 Add_Client( CHANNEL *Chan, CLIENT *Client )
1015 assert( Chan != NULL );
1016 assert( Client != NULL );
1018 /* Create new CL2CHAN structure */
1019 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
1022 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
1025 cl2chan->channel = Chan;
1026 cl2chan->client = Client;
1027 strcpy( cl2chan->modes, "" );
1030 cl2chan->next = My_Cl2Chan;
1031 My_Cl2Chan = cl2chan;
1033 LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
1040 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
1042 CL2CHAN *cl2chan, *last_cl2chan;
1045 assert( Chan != NULL );
1046 assert( Client != NULL );
1047 assert( Origin != NULL );
1048 assert( Reason != NULL );
1050 /* Do not inform other servers if the channel is local to this server,
1051 * regardless of what the caller requested! */
1053 InformServer = !Channel_IsLocal(Chan);
1055 last_cl2chan = NULL;
1056 cl2chan = My_Cl2Chan;
1059 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
1060 last_cl2chan = cl2chan;
1061 cl2chan = cl2chan->next;
1063 if( ! cl2chan ) return false;
1065 c = cl2chan->channel;
1066 assert( c != NULL );
1068 /* maintain cl2chan list */
1069 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
1070 else My_Cl2Chan = cl2chan->next;
1076 /* QUIT: other servers have already been notified,
1077 * see Client_Destroy(); so only inform other clients
1078 * in same channel. */
1079 assert( InformServer == false );
1080 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1081 Client_Mask( Client ), c->name, Reason );
1084 /* User was KICKed: inform other servers (public
1085 * channels) and all users in the channel */
1087 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
1088 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
1089 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
1090 c->name, Client_ID( Client ), Reason );
1091 if ((Client_Conn(Client) > NONE) &&
1092 (Client_Type(Client) == CLIENT_USER))
1094 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
1095 c->name, Client_ID( Client ), Reason);
1097 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
1098 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
1101 if (Conf_MorePrivacy)
1105 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
1107 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
1110 if ((Client_Conn(Origin) > NONE) &&
1111 (Client_Type(Origin) == CLIENT_USER))
1113 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
1114 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1115 Client_Mask(Client), c->name, Reason);
1119 /* When channel is empty and is not pre-defined, delete */
1120 if( ! Channel_HasMode( Chan, 'P' ))
1122 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
1126 } /* Remove_Client */
1130 Channel_AddBan(CHANNEL *c, const char *mask, const char *who )
1132 struct list_head *h = Channel_GetListBans(c);
1133 LogDebug("Adding \"%s\" to \"%s\" ban list", mask, Channel_Name(c));
1134 return Lists_Add(h, mask, time(NULL), who, false);
1139 Channel_AddExcept(CHANNEL *c, const char *mask, const char *who )
1141 struct list_head *h = Channel_GetListExcepts(c);
1142 LogDebug("Adding \"%s\" to \"%s\" exception list", mask, Channel_Name(c));
1143 return Lists_Add(h, mask, time(NULL), who, false);
1148 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce, const char *who )
1150 struct list_head *h = Channel_GetListInvites(c);
1151 LogDebug("Adding \"%s\" to \"%s\" invite list", mask, Channel_Name(c));
1152 return Lists_Add(h, mask, time(NULL), who, onlyonce);
1157 ShowChannelList(struct list_head *head, CLIENT *Client, CHANNEL *Channel,
1158 char *msg, char *msg_end)
1160 struct list_elem *e;
1162 assert (Client != NULL);
1163 assert (Channel != NULL);
1165 e = Lists_GetFirst(head);
1167 if (!IRC_WriteStrClient(Client, msg, Client_ID(Client),
1168 Channel_Name(Channel),
1171 Lists_GetValidity(e)))
1172 return DISCONNECTED;
1173 e = Lists_GetNext(e);
1176 return IRC_WriteStrClient(Client, msg_end, Client_ID(Client),
1177 Channel_Name(Channel));
1182 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1184 struct list_head *h;
1186 assert( Channel != NULL );
1188 h = Channel_GetListBans(Channel);
1189 return ShowChannelList(h, Client, Channel, RPL_BANLIST_MSG,
1190 RPL_ENDOFBANLIST_MSG);
1195 Channel_ShowExcepts( CLIENT *Client, CHANNEL *Channel )
1197 struct list_head *h;
1199 assert( Channel != NULL );
1201 h = Channel_GetListExcepts(Channel);
1202 return ShowChannelList(h, Client, Channel, RPL_EXCEPTLIST_MSG,
1203 RPL_ENDOFEXCEPTLIST_MSG);
1208 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1210 struct list_head *h;
1212 assert( Channel != NULL );
1214 h = Channel_GetListInvites(Channel);
1215 return ShowChannelList(h, Client, Channel, RPL_INVITELIST_MSG,
1216 RPL_ENDOFINVITELIST_MSG);
1221 * Log a message to the local &SERVER channel, if it exists.
1224 Channel_LogServer(const char *msg)
1229 assert(msg != NULL);
1231 sc = Channel_Search("&SERVER");
1235 c = Client_ThisServer();
1236 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1237 } /* Channel_LogServer */
1241 Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
1243 char *file_name, line[COMMAND_LEN], *nick, *pass;
1246 assert(Chan != NULL);
1247 assert(Client != NULL);
1248 assert(Key != NULL);
1250 if (!Channel_HasMode(Chan, 'k'))
1254 if (strcmp(Chan->key, Key) == 0)
1257 file_name = array_start(&Chan->keyfile);
1260 fd = fopen(file_name, "r");
1262 Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
1263 file_name, Chan->name, strerror(errno));
1267 while (fgets(line, (int)sizeof(line), fd) != NULL) {
1269 if (! (nick = strchr(line, ':')))
1272 if (!Match(line, Client_User(Client)))
1274 if (! (pass = strchr(nick, ':')))
1277 if (!Match(nick, Client_ID(Client)))
1279 if (strcmp(Key, pass) != 0)
1287 } /* Channel_CheckKey */
1291 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1293 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1294 } /* Get_First_Cl2Chan */
1298 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1302 assert( Client != NULL || Channel != NULL );
1307 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1308 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1309 cl2chan = cl2chan->next;
1312 } /* Get_Next_Cl2Chan */
1316 * Remove a channel and free all of its data structures.
1319 Delete_Channel(CHANNEL *Chan)
1321 CHANNEL *chan, *last_chan;
1332 assert(chan != NULL);
1336 /* maintain channel list */
1338 last_chan->next = chan->next;
1340 My_Channels = chan->next;
1342 LogDebug("Freed channel structure for \"%s\".", Chan->name);
1344 } /* Delete_Channel */
1348 Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
1352 assert(Chan != NULL);
1353 assert(KeyFile != NULL);
1355 len = strlen(KeyFile);
1356 if (len < array_bytes(&Chan->keyfile)) {
1357 Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
1358 array_free(&Chan->keyfile);
1364 if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
1366 "Could not set new channel key file \"%s\" for %s: %s",
1367 KeyFile, Chan->name, strerror(errno));
1369 Log(LOG_INFO|LOG_snotice,
1370 "New local channel key file \"%s\" for %s activated.",
1371 KeyFile, Chan->name);