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 Log(LOG_INFO, "Created pre-defined channel \"%s\".",
136 Channel_ModeAdd(new_chan, 'P');
138 if (conf_chan->topic[0])
139 Channel_SetTopic(new_chan, NULL, conf_chan->topic);
141 /* Evaluate modes strings with fake requests */
142 if(conf_chan->modes_num) {
143 /* Prepare fake request structure */
144 strlcpy(name, conf_chan->name, sizeof(name));
145 Log(LOG_INFO, "Evaluating predefined channel modes for \"%s\".", name);
147 Req.prefix = Client_ID(Client_ThisServer());
148 Req.command = "MODE";
150 /* Iterate over channel modes strings */
151 for (n = 0; n < conf_chan->modes_num; n++) {
153 strlcpy(modes, conf_chan->modes[n], sizeof(modes));
154 Log(LOG_DEBUG, "Evaluate \"MODE %s %s\".", name, modes);
155 c = strtok(modes, " ");
156 while (c && Req.argc < 15) {
157 Req.argv[Req.argc++] = c;
161 IRC_MODE(Client_ThisServer(), &Req);
163 /* Original channel modes srings are no longer needed */
164 free(conf_chan->modes[n]);
168 Channel_SetKey(new_chan, conf_chan->key);
169 Channel_SetMaxUsers(new_chan, conf_chan->maxusers);
170 Set_KeyFile(new_chan, conf_chan->keyfile);
173 array_free(&Conf_Channels);
175 /* Make sure the local &SERVER channel exists */
176 if (!Channel_Search("&SERVER")) {
177 new_chan = Channel_Create("&SERVER");
179 Channel_SetModes(new_chan, "mnPt");
180 Channel_SetTopic(new_chan, Client_ThisServer(),
183 Log(LOG_ERR, "Failed to create \"&SERVER\" channel!");
185 LogDebug("Required channel \"&SERVER\" already exists, ok.");
186 } /* Channel_InitPredefined */
190 Free_Channel(CHANNEL *chan)
192 array_free(&chan->topic);
193 array_free(&chan->keyfile);
194 Lists_Free(&chan->list_bans);
195 Lists_Free(&chan->list_excepts);
196 Lists_Free(&chan->list_invites);
206 CL2CHAN *cl2chan, *cl2chan_next;
208 /* free struct Channel */
216 /* Free Channel allocation table */
217 cl2chan = My_Cl2Chan;
219 cl2chan_next = cl2chan->next;
221 cl2chan = cl2chan_next;
228 * This function lets a client join a channel. First, the function
229 * checks that the specified channel name is valid and that the client
230 * isn't already a member. If the specified channel doesn't exist,
231 * a new channel is created. Client is added to channel by function
235 Channel_Join( CLIENT *Client, const char *Name )
239 assert(Client != NULL);
240 assert(Name != NULL);
242 /* Check that the channel name is valid */
243 if (! Channel_IsValidName(Name)) {
244 IRC_WriteErrClient(Client, ERR_NOSUCHCHANNEL_MSG,
245 Client_ID(Client), Name);
249 chan = Channel_Search(Name);
251 /* Check if the client is already in the channel */
252 if (Get_Cl2Chan(chan, Client))
255 /* If the specified channel does not exist, the channel
257 chan = Channel_Create(Name);
262 /* Add user to Channel */
263 if (! Add_Client(chan, Client))
271 * Part client from channel.
272 * This function lets a client part from a channel. First, the function checks
273 * if the channel exists and the client is a member of it and sends out
274 * appropriate error messages if not. The real work is done by the function
278 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
282 assert(Client != NULL);
283 assert(Name != NULL);
284 assert(Reason != NULL);
286 /* Check that specified channel exists */
287 chan = Channel_Search(Name);
289 IRC_WriteErrClient(Client, ERR_NOSUCHCHANNEL_MSG,
290 Client_ID(Client), Name);
294 /* Check that the client is in the channel */
295 if (!Get_Cl2Chan(chan, Client)) {
296 IRC_WriteErrClient(Client, ERR_NOTONCHANNEL_MSG,
297 Client_ID(Client), Name);
301 if (Conf_MorePrivacy)
304 /* Part client from channel */
305 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
313 * Kick user from Channel
316 Channel_Kick(CLIENT *Peer, CLIENT *Target, CLIENT *Origin, const char *Name,
320 bool can_kick = false;
322 assert(Peer != NULL);
323 assert(Target != NULL);
324 assert(Origin != NULL);
325 assert(Name != NULL);
326 assert(Reason != NULL);
328 /* Check that channel exists */
329 chan = Channel_Search( Name );
331 IRC_WriteErrClient(Origin, ERR_NOSUCHCHANNEL_MSG,
332 Client_ID(Origin), Name);
336 if (Client_Type(Peer) != CLIENT_SERVER &&
337 Client_Type(Origin) != CLIENT_SERVICE) {
338 /* Check that user is on the specified channel */
339 if (!Channel_IsMemberOf(chan, Origin)) {
340 IRC_WriteErrClient(Origin, ERR_NOTONCHANNEL_MSG,
341 Client_ID(Origin), Name);
346 /* Check that the client to be kicked is on the specified channel */
347 if (!Channel_IsMemberOf(chan, Target)) {
348 IRC_WriteErrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
349 Client_ID(Origin), Client_ID(Target), Name );
353 if(Client_Type(Peer) == CLIENT_USER) {
354 /* Channel mode 'Q' and user mode 'q' on target: nobody but
355 * IRC Operators and servers can kick the target user */
356 if ((Channel_HasMode(chan, 'Q')
357 || Client_HasMode(Target, 'q')
358 || Client_Type(Target) == CLIENT_SERVICE)
359 && !Client_HasMode(Origin, 'o')) {
360 IRC_WriteErrClient(Origin, ERR_KICKDENY_MSG,
361 Client_ID(Origin), Name,
366 /* Check if client has the rights to kick target */
368 /* Owner can kick everyone */
369 if (Channel_UserHasMode(chan, Peer, 'q'))
372 /* Admin can't kick owner */
373 else if (Channel_UserHasMode(chan, Peer, 'a') &&
374 !Channel_UserHasMode(chan, Target, 'q'))
377 /* Op can't kick owner | admin */
378 else if (Channel_UserHasMode(chan, Peer, 'o') &&
379 !Channel_UserHasMode(chan, Target, 'q') &&
380 !Channel_UserHasMode(chan, Target, 'a'))
383 /* Half Op can't kick owner | admin | op */
384 else if (Channel_UserHasMode(chan, Peer, 'h') &&
385 !Channel_UserHasMode(chan, Target, 'q') &&
386 !Channel_UserHasMode(chan, Target, 'a') &&
387 !Channel_UserHasMode(chan, Target, 'o'))
390 /* IRC operators & IRCd with OperCanMode enabled
391 * can kick anyways regardless of privilege */
392 else if(Client_HasMode(Origin, 'o') && Conf_OperCanMode)
396 IRC_WriteErrClient(Origin, ERR_CHANOPPRIVTOOLOW_MSG,
397 Client_ID(Origin), Name);
402 /* Kick Client from channel */
403 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
408 Channel_Quit( CLIENT *Client, const char *Reason )
412 assert( Client != NULL );
413 assert( Reason != NULL );
415 if (Conf_MorePrivacy)
418 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
424 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
431 * Get number of channels this server knows and that are "visible" to
432 * the given client. If no client is given, all channels will be counted.
434 * @param Client The client to check or NULL.
435 * @return Number of channels visible to the client.
438 Channel_CountVisible (CLIENT *Client)
441 unsigned long count = 0;
446 if (!Channel_HasMode(c, 's')
447 || Channel_IsMemberOf(c, Client))
458 Channel_MemberCount( CHANNEL *Chan )
461 unsigned long count = 0;
463 assert( Chan != NULL );
465 cl2chan = My_Cl2Chan;
468 if( cl2chan->channel == Chan ) count++;
469 cl2chan = cl2chan->next;
472 } /* Channel_MemberCount */
476 Channel_CountForUser( CLIENT *Client )
478 /* Count number of channels a user is member of. */
483 assert( Client != NULL );
485 cl2chan = My_Cl2Chan;
488 if( cl2chan->client == Client ) count++;
489 cl2chan = cl2chan->next;
493 } /* Channel_CountForUser */
497 Channel_Name( const CHANNEL *Chan )
499 assert( Chan != NULL );
505 Channel_Modes( CHANNEL *Chan )
507 assert( Chan != NULL );
509 } /* Channel_Modes */
513 Channel_HasMode( CHANNEL *Chan, char Mode )
515 assert( Chan != NULL );
516 return strchr( Chan->modes, Mode ) != NULL;
517 } /* Channel_HasMode */
521 Channel_Key( CHANNEL *Chan )
523 assert( Chan != NULL );
529 Channel_MaxUsers( CHANNEL *Chan )
531 assert( Chan != NULL );
532 return Chan->maxusers;
533 } /* Channel_MaxUsers */
537 Channel_First( void )
540 } /* Channel_First */
544 Channel_Next( CHANNEL *Chan )
546 assert( Chan != NULL );
552 Channel_Search( const char *Name )
554 /* Search channel structure */
559 assert( Name != NULL );
561 search_hash = Hash( Name );
565 if( search_hash == c->hash )
568 if( strcasecmp( Name, c->name ) == 0 ) return c;
573 } /* Channel_Search */
577 Channel_FirstMember( CHANNEL *Chan )
579 assert( Chan != NULL );
580 return Get_First_Cl2Chan( NULL, Chan );
581 } /* Channel_FirstMember */
585 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
587 assert( Chan != NULL );
588 assert( Cl2Chan != NULL );
589 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
590 } /* Channel_NextMember */
594 Channel_FirstChannelOf( CLIENT *Client )
596 assert( Client != NULL );
597 return Get_First_Cl2Chan( Client, NULL );
598 } /* Channel_FirstChannelOf */
602 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
604 assert( Client != NULL );
605 assert( Cl2Chan != NULL );
606 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
607 } /* Channel_NextChannelOf */
611 Channel_GetClient( CL2CHAN *Cl2Chan )
613 assert( Cl2Chan != NULL );
614 return Cl2Chan->client;
615 } /* Channel_GetClient */
619 Channel_GetChannel( CL2CHAN *Cl2Chan )
621 assert( Cl2Chan != NULL );
622 return Cl2Chan->channel;
623 } /* Channel_GetChannel */
627 Channel_IsValidName( const char *Name )
629 assert( Name != NULL );
632 if (strlen(Name) <= 1)
635 if (strchr("#&+", Name[0]) == NULL)
637 if (strlen(Name) >= CHANNEL_NAME_LEN)
640 return Name[strcspn(Name, " ,:\007")] == 0;
641 } /* Channel_IsValidName */
645 Channel_ModeAdd( CHANNEL *Chan, char Mode )
648 * If the channel already had this mode, return false.
649 * If the channel mode was newly set return true.
654 assert( Chan != NULL );
656 x[0] = Mode; x[1] = '\0';
657 if( ! Channel_HasMode( Chan, x[0] ))
659 /* Channel does not have this mode yet, set it */
660 strlcat( Chan->modes, x, sizeof( Chan->modes ));
664 } /* Channel_ModeAdd */
668 Channel_ModeDel( CHANNEL *Chan, char Mode )
671 * if the mode was removed return true.
672 * if the channel did not have the mode, return false.
676 assert( Chan != NULL );
678 p = strchr( Chan->modes, Mode );
679 if( ! p ) return false;
681 /* Channel has mode -> delete */
688 } /* Channel_ModeDel */
692 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
694 /* Set Channel-User-Mode.
695 * if mode was newly set, return true.
696 * if the User already had this channel-mode, return false.
702 assert( Chan != NULL );
703 assert( Client != NULL );
705 cl2chan = Get_Cl2Chan( Chan, Client );
706 assert( cl2chan != NULL );
708 x[0] = Mode; x[1] = '\0';
709 if( ! strchr( cl2chan->modes, x[0] ))
711 /* mode not set, -> set it */
712 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
716 } /* Channel_UserModeAdd */
720 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
722 /* Delete Channel-User-Mode.
723 * If Mode was removed, return true.
724 * If User did not have the Channel-Mode, return false.
730 assert( Chan != NULL );
731 assert( Client != NULL );
733 cl2chan = Get_Cl2Chan( Chan, Client );
734 assert( cl2chan != NULL );
736 p = strchr( cl2chan->modes, Mode );
737 if( ! p ) return false;
739 /* Client has Mode -> delete */
746 } /* Channel_UserModeDel */
750 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
752 /* return Users' Channel-Modes */
756 assert( Chan != NULL );
757 assert( Client != NULL );
759 cl2chan = Get_Cl2Chan( Chan, Client );
760 assert( cl2chan != NULL );
762 return cl2chan->modes;
763 } /* Channel_UserModes */
767 Channel_UserHasMode( CHANNEL *Chan, CLIENT *Client, char Mode )
769 return strchr(Channel_UserModes(Chan, Client), Mode) != NULL;
770 } /* Channel_UserHasMode */
774 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
776 /* Test if Client is on Channel Chan */
778 assert( Chan != NULL );
779 assert( Client != NULL );
780 return Get_Cl2Chan(Chan, Client) != NULL;
781 } /* Channel_IsMemberOf */
785 Channel_Topic( CHANNEL *Chan )
788 assert( Chan != NULL );
789 ret = array_start(&Chan->topic);
790 return ret ? ret : "";
791 } /* Channel_Topic */
797 Channel_TopicTime(CHANNEL *Chan)
799 assert(Chan != NULL);
800 return (unsigned int) Chan->topic_time;
801 } /* Channel_TopicTime */
805 Channel_TopicWho(CHANNEL *Chan)
807 assert(Chan != NULL);
808 return Chan->topic_who;
809 } /* Channel_TopicWho */
813 Channel_CreationTime(CHANNEL *Chan)
815 assert(Chan != NULL);
816 return (unsigned int) Chan->creation_time;
817 } /* Channel_CreationTime */
823 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
826 assert( Chan != NULL );
827 assert( Topic != NULL );
830 if (len < array_bytes(&Chan->topic))
831 array_free(&Chan->topic);
833 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
834 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
835 Topic, Chan->name, strerror(errno));
837 Chan->topic_time = time(NULL);
838 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
839 strlcpy(Chan->topic_who, Client_ID(Client),
840 sizeof Chan->topic_who);
842 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
843 sizeof Chan->topic_who);
847 } /* Channel_SetTopic */
851 Channel_SetModes( CHANNEL *Chan, const char *Modes )
853 assert( Chan != NULL );
854 assert( Modes != NULL );
856 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
857 } /* Channel_SetModes */
861 Channel_SetKey( CHANNEL *Chan, const char *Key )
863 assert( Chan != NULL );
864 assert( Key != NULL );
866 strlcpy( Chan->key, Key, sizeof( Chan->key ));
867 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
868 } /* Channel_SetKey */
872 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
874 assert( Chan != NULL );
876 Chan->maxusers = Count;
877 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
878 } /* Channel_SetMaxUsers */
882 * Check if a client is allowed to send to a specific channel.
884 * @param Chan The channel to check.
885 * @param From The client that wants to send.
886 * @return true if the client is allowed to send, false otherwise.
889 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
891 bool is_member, has_voice, is_halfop, is_op, is_chanadmin, is_owner;
893 is_member = has_voice = is_halfop = is_op = is_chanadmin = is_owner = false;
895 /* The server itself always can send messages :-) */
896 if (Client_ThisServer() == From)
899 if (Channel_IsMemberOf(Chan, From)) {
901 if (Channel_UserHasMode(Chan, From, 'v'))
903 if (Channel_UserHasMode(Chan, From, 'h'))
905 if (Channel_UserHasMode(Chan, From, 'o'))
907 if (Channel_UserHasMode(Chan, From, 'a'))
909 if (Channel_UserHasMode(Chan, From, 'q'))
914 * Is the client allowed to write to channel?
916 * If channel mode n set: non-members cannot send to channel.
917 * If channel mode m set: need voice.
919 if (Channel_HasMode(Chan, 'n') && !is_member)
922 if (Channel_HasMode(Chan, 'M') && !Client_HasMode(From, 'R')
923 && !Client_HasMode(From, 'o'))
926 if (has_voice || is_halfop || is_op || is_chanadmin || is_owner)
929 if (Channel_HasMode(Chan, 'm'))
932 if (Lists_Check(&Chan->list_excepts, From))
935 return !Lists_Check(&Chan->list_bans, From);
940 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
941 bool SendErrors, const char *Text)
943 if (!Can_Send_To_Channel(Chan, From)) {
945 return CONNECTED; /* no error, see RFC 2812 */
946 if (Channel_HasMode(Chan, 'M'))
947 return IRC_WriteErrClient(From, ERR_NEEDREGGEDNICK_MSG,
948 Client_ID(From), Channel_Name(Chan));
950 return IRC_WriteErrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
951 Client_ID(From), Channel_Name(Chan));
954 if (Client_Conn(From) > NONE)
955 Conn_UpdateIdle(Client_Conn(From));
957 IRC_WriteStrChannelPrefix(Client, Chan, From, true, "%s %s :%s",
958 Command, Channel_Name(Chan), Text);
964 Channel_Create( const char *Name )
966 /* Create new CHANNEL structure and add it to linked list */
969 assert( Name != NULL );
971 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
974 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
977 memset( c, 0, sizeof( CHANNEL ));
978 strlcpy( c->name, Name, sizeof( c->name ));
979 c->hash = Hash( c->name );
980 c->next = My_Channels;
982 c->creation_time = time(NULL);
985 LogDebug("Created new channel structure for \"%s\".", Name);
987 } /* Channel_Create */
991 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
995 assert( Chan != NULL );
996 assert( Client != NULL );
998 cl2chan = My_Cl2Chan;
1001 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
1002 cl2chan = cl2chan->next;
1009 Add_Client( CHANNEL *Chan, CLIENT *Client )
1013 assert( Chan != NULL );
1014 assert( Client != NULL );
1016 /* Create new CL2CHAN structure */
1017 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
1020 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
1023 cl2chan->channel = Chan;
1024 cl2chan->client = Client;
1025 strcpy( cl2chan->modes, "" );
1028 cl2chan->next = My_Cl2Chan;
1029 My_Cl2Chan = cl2chan;
1031 LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
1038 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
1040 CL2CHAN *cl2chan, *last_cl2chan;
1043 assert( Chan != NULL );
1044 assert( Client != NULL );
1045 assert( Origin != NULL );
1046 assert( Reason != NULL );
1048 /* Do not inform other servers if the channel is local to this server,
1049 * regardless of what the caller requested! */
1051 InformServer = !Channel_IsLocal(Chan);
1053 last_cl2chan = NULL;
1054 cl2chan = My_Cl2Chan;
1057 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
1058 last_cl2chan = cl2chan;
1059 cl2chan = cl2chan->next;
1061 if( ! cl2chan ) return false;
1063 c = cl2chan->channel;
1064 assert( c != NULL );
1066 /* maintain cl2chan list */
1067 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
1068 else My_Cl2Chan = cl2chan->next;
1074 /* QUIT: other servers have already been notified,
1075 * see Client_Destroy(); so only inform other clients
1076 * in same channel. */
1077 assert( InformServer == false );
1078 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1079 Client_Mask( Client ), c->name, Reason );
1082 /* User was KICKed: inform other servers (public
1083 * channels) and all users in the channel */
1085 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
1086 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
1087 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
1088 c->name, Client_ID( Client ), Reason );
1089 if ((Client_Conn(Client) > NONE) &&
1090 (Client_Type(Client) == CLIENT_USER))
1092 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
1093 c->name, Client_ID( Client ), Reason);
1095 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
1096 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
1099 if (Conf_MorePrivacy)
1103 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
1105 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
1108 if ((Client_Conn(Origin) > NONE) &&
1109 (Client_Type(Origin) == CLIENT_USER))
1111 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
1112 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1113 Client_Mask(Client), c->name, Reason);
1117 /* When channel is empty and is not pre-defined, delete */
1118 if( ! Channel_HasMode( Chan, 'P' ))
1120 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
1124 } /* Remove_Client */
1128 Channel_AddBan(CHANNEL *c, const char *mask, const char *who )
1130 struct list_head *h = Channel_GetListBans(c);
1131 LogDebug("Adding \"%s\" to \"%s\" ban list", mask, Channel_Name(c));
1132 return Lists_Add(h, mask, time(NULL), who, false);
1137 Channel_AddExcept(CHANNEL *c, const char *mask, const char *who )
1139 struct list_head *h = Channel_GetListExcepts(c);
1140 LogDebug("Adding \"%s\" to \"%s\" exception list", mask, Channel_Name(c));
1141 return Lists_Add(h, mask, time(NULL), who, false);
1146 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce, const char *who )
1148 struct list_head *h = Channel_GetListInvites(c);
1149 LogDebug("Adding \"%s\" to \"%s\" invite list", mask, Channel_Name(c));
1150 return Lists_Add(h, mask, time(NULL), who, onlyonce);
1155 ShowChannelList(struct list_head *head, CLIENT *Client, CHANNEL *Channel,
1156 char *msg, char *msg_end)
1158 struct list_elem *e;
1160 assert (Client != NULL);
1161 assert (Channel != NULL);
1163 e = Lists_GetFirst(head);
1165 if (!IRC_WriteStrClient(Client, msg, Client_ID(Client),
1166 Channel_Name(Channel),
1169 Lists_GetValidity(e)))
1170 return DISCONNECTED;
1171 e = Lists_GetNext(e);
1174 return IRC_WriteStrClient(Client, msg_end, Client_ID(Client),
1175 Channel_Name(Channel));
1180 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1182 struct list_head *h;
1184 assert( Channel != NULL );
1186 h = Channel_GetListBans(Channel);
1187 return ShowChannelList(h, Client, Channel, RPL_BANLIST_MSG,
1188 RPL_ENDOFBANLIST_MSG);
1193 Channel_ShowExcepts( CLIENT *Client, CHANNEL *Channel )
1195 struct list_head *h;
1197 assert( Channel != NULL );
1199 h = Channel_GetListExcepts(Channel);
1200 return ShowChannelList(h, Client, Channel, RPL_EXCEPTLIST_MSG,
1201 RPL_ENDOFEXCEPTLIST_MSG);
1206 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1208 struct list_head *h;
1210 assert( Channel != NULL );
1212 h = Channel_GetListInvites(Channel);
1213 return ShowChannelList(h, Client, Channel, RPL_INVITELIST_MSG,
1214 RPL_ENDOFINVITELIST_MSG);
1219 * Log a message to the local &SERVER channel, if it exists.
1222 Channel_LogServer(const char *msg)
1227 assert(msg != NULL);
1229 sc = Channel_Search("&SERVER");
1233 c = Client_ThisServer();
1234 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1235 } /* Channel_LogServer */
1239 Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
1241 char *file_name, line[COMMAND_LEN], *nick, *pass;
1244 assert(Chan != NULL);
1245 assert(Client != NULL);
1246 assert(Key != NULL);
1248 if (!Channel_HasMode(Chan, 'k'))
1252 if (strcmp(Chan->key, Key) == 0)
1255 file_name = array_start(&Chan->keyfile);
1258 fd = fopen(file_name, "r");
1260 Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
1261 file_name, Chan->name, strerror(errno));
1265 while (fgets(line, (int)sizeof(line), fd) != NULL) {
1267 if (! (nick = strchr(line, ':')))
1270 if (!Match(line, Client_User(Client)))
1272 if (! (pass = strchr(nick, ':')))
1275 if (!Match(nick, Client_ID(Client)))
1277 if (strcmp(Key, pass) != 0)
1285 } /* Channel_CheckKey */
1289 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1291 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1292 } /* Get_First_Cl2Chan */
1296 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1300 assert( Client != NULL || Channel != NULL );
1305 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1306 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1307 cl2chan = cl2chan->next;
1310 } /* Get_Next_Cl2Chan */
1314 * Remove a channel and free all of its data structures.
1317 Delete_Channel(CHANNEL *Chan)
1319 CHANNEL *chan, *last_chan;
1330 assert(chan != NULL);
1334 /* maintain channel list */
1336 last_chan->next = chan->next;
1338 My_Channels = chan->next;
1340 LogDebug("Freed channel structure for \"%s\".", Chan->name);
1342 } /* Delete_Channel */
1346 Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
1350 assert(Chan != NULL);
1351 assert(KeyFile != NULL);
1353 len = strlen(KeyFile);
1354 if (len < array_bytes(&Chan->keyfile)) {
1355 Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
1356 array_free(&Chan->keyfile);
1362 if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
1364 "Could not set new channel key file \"%s\" for %s: %s",
1365 KeyFile, Chan->name, strerror(errno));
1367 Log(LOG_INFO|LOG_snotice,
1368 "New local channel key file \"%s\" for %s activated.",
1369 KeyFile, Chan->name);