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, 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 string with a fake request */
142 if(conf_chan->modes[0]) {
143 strlcpy(modes, conf_chan->modes, sizeof(modes));
144 strlcpy(name, conf_chan->name, sizeof(name));
145 Log(LOG_DEBUG, "Evaluate \"MODE %s %s\".", name, modes);
147 Req.argv[Req.argc++] = name;
148 Req.prefix = Client_ID(Client_ThisServer());
149 Req.command = "MODE";
150 c = strtok(modes, " ");
151 while (c && Req.argc<15) {
152 Req.argv[Req.argc++] = c;
155 IRC_MODE(Client_ThisServer(), &Req);
158 Channel_SetKey(new_chan, conf_chan->key);
159 Channel_SetMaxUsers(new_chan, conf_chan->maxusers);
160 Set_KeyFile(new_chan, conf_chan->keyfile);
163 array_free(&Conf_Channels);
165 /* Make sure the local &SERVER channel exists */
166 if (!Channel_Search("&SERVER")) {
167 new_chan = Channel_Create("&SERVER");
169 Channel_SetModes(new_chan, "mnPt");
170 Channel_SetTopic(new_chan, Client_ThisServer(),
173 Log(LOG_ERR, "Failed to create \"&SERVER\" channel!");
175 LogDebug("Required channel \"&SERVER\" already exists, ok.");
176 } /* Channel_InitPredefined */
180 Free_Channel(CHANNEL *chan)
182 array_free(&chan->topic);
183 array_free(&chan->keyfile);
184 Lists_Free(&chan->list_bans);
185 Lists_Free(&chan->list_excepts);
186 Lists_Free(&chan->list_invites);
196 CL2CHAN *cl2chan, *cl2chan_next;
198 /* free struct Channel */
206 /* Free Channel allocation table */
207 cl2chan = My_Cl2Chan;
209 cl2chan_next = cl2chan->next;
211 cl2chan = cl2chan_next;
218 * This function lets a client join a channel. First, the function
219 * checks that the specified channel name is valid and that the client
220 * isn't already a member. If the specified channel doesn't exist,
221 * a new channel is created. Client is added to channel by function
225 Channel_Join( CLIENT *Client, const char *Name )
229 assert(Client != NULL);
230 assert(Name != NULL);
232 /* Check that the channel name is valid */
233 if (! Channel_IsValidName(Name)) {
234 IRC_WriteErrClient(Client, ERR_NOSUCHCHANNEL_MSG,
235 Client_ID(Client), Name);
239 chan = Channel_Search(Name);
241 /* Check if the client is already in the channel */
242 if (Get_Cl2Chan(chan, Client))
245 /* If the specified channel does not exist, the channel
247 chan = Channel_Create(Name);
252 /* Add user to Channel */
253 if (! Add_Client(chan, Client))
261 * Part client from channel.
262 * This function lets a client part from a channel. First, the function checks
263 * if the channel exists and the client is a member of it and sends out
264 * appropriate error messages if not. The real work is done by the function
268 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
272 assert(Client != NULL);
273 assert(Name != NULL);
274 assert(Reason != NULL);
276 /* Check that specified channel exists */
277 chan = Channel_Search(Name);
279 IRC_WriteErrClient(Client, ERR_NOSUCHCHANNEL_MSG,
280 Client_ID(Client), Name);
284 /* Check that the client is in the channel */
285 if (!Get_Cl2Chan(chan, Client)) {
286 IRC_WriteErrClient(Client, ERR_NOTONCHANNEL_MSG,
287 Client_ID(Client), Name);
291 if (Conf_MorePrivacy)
294 /* Part client from channel */
295 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
303 * Kick user from Channel
306 Channel_Kick(CLIENT *Peer, CLIENT *Target, CLIENT *Origin, const char *Name,
310 bool can_kick = false;
312 assert(Peer != NULL);
313 assert(Target != NULL);
314 assert(Origin != NULL);
315 assert(Name != NULL);
316 assert(Reason != NULL);
318 /* Check that channel exists */
319 chan = Channel_Search( Name );
321 IRC_WriteErrClient(Origin, ERR_NOSUCHCHANNEL_MSG,
322 Client_ID(Origin), Name);
326 if (Client_Type(Peer) != CLIENT_SERVER &&
327 Client_Type(Origin) != CLIENT_SERVICE) {
328 /* Check that user is on the specified channel */
329 if (!Channel_IsMemberOf(chan, Origin)) {
330 IRC_WriteErrClient(Origin, ERR_NOTONCHANNEL_MSG,
331 Client_ID(Origin), Name);
336 /* Check that the client to be kicked is on the specified channel */
337 if (!Channel_IsMemberOf(chan, Target)) {
338 IRC_WriteErrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
339 Client_ID(Origin), Client_ID(Target), Name );
343 if(Client_Type(Peer) == CLIENT_USER) {
344 /* Channel mode 'Q' and user mode 'q' on target: nobody but
345 * IRC Operators and servers can kick the target user */
346 if ((Channel_HasMode(chan, 'Q')
347 || Client_HasMode(Target, 'q')
348 || Client_Type(Target) == CLIENT_SERVICE)
349 && !Client_HasMode(Origin, 'o')) {
350 IRC_WriteErrClient(Origin, ERR_KICKDENY_MSG,
351 Client_ID(Origin), Name,
356 /* Check if client has the rights to kick target */
358 /* Owner can kick everyone */
359 if (Channel_UserHasMode(chan, Peer, 'q'))
362 /* Admin can't kick owner */
363 else if (Channel_UserHasMode(chan, Peer, 'a') &&
364 !Channel_UserHasMode(chan, Target, 'q'))
367 /* Op can't kick owner | admin */
368 else if (Channel_UserHasMode(chan, Peer, 'o') &&
369 !Channel_UserHasMode(chan, Target, 'q') &&
370 !Channel_UserHasMode(chan, Target, 'a'))
373 /* Half Op can't kick owner | admin | op */
374 else if (Channel_UserHasMode(chan, Peer, 'h') &&
375 !Channel_UserHasMode(chan, Target, 'q') &&
376 !Channel_UserHasMode(chan, Target, 'a') &&
377 !Channel_UserHasMode(chan, Target, 'o'))
380 /* IRC operators & IRCd with OperCanMode enabled
381 * can kick anyways regardless of privilege */
382 else if(Client_HasMode(Origin, 'o') && Conf_OperCanMode)
386 IRC_WriteErrClient(Origin, ERR_CHANOPPRIVTOOLOW_MSG,
387 Client_ID(Origin), Name);
392 /* Kick Client from channel */
393 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
398 Channel_Quit( CLIENT *Client, const char *Reason )
402 assert( Client != NULL );
403 assert( Reason != NULL );
405 if (Conf_MorePrivacy)
408 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
414 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
421 * Get number of channels this server knows and that are "visible" to
422 * the given client. If no client is given, all channels will be counted.
424 * @param Client The client to check or NULL.
425 * @return Number of channels visible to the client.
428 Channel_CountVisible (CLIENT *Client)
431 unsigned long count = 0;
436 if (!Channel_HasMode(c, 's')
437 || Channel_IsMemberOf(c, Client))
448 Channel_MemberCount( CHANNEL *Chan )
451 unsigned long count = 0;
453 assert( Chan != NULL );
455 cl2chan = My_Cl2Chan;
458 if( cl2chan->channel == Chan ) count++;
459 cl2chan = cl2chan->next;
462 } /* Channel_MemberCount */
466 Channel_CountForUser( CLIENT *Client )
468 /* Count number of channels a user is member of. */
473 assert( Client != NULL );
475 cl2chan = My_Cl2Chan;
478 if( cl2chan->client == Client ) count++;
479 cl2chan = cl2chan->next;
483 } /* Channel_CountForUser */
487 Channel_Name( const CHANNEL *Chan )
489 assert( Chan != NULL );
495 Channel_Modes( CHANNEL *Chan )
497 assert( Chan != NULL );
499 } /* Channel_Modes */
503 Channel_HasMode( CHANNEL *Chan, char Mode )
505 assert( Chan != NULL );
506 return strchr( Chan->modes, Mode ) != NULL;
507 } /* Channel_HasMode */
511 Channel_Key( CHANNEL *Chan )
513 assert( Chan != NULL );
519 Channel_MaxUsers( CHANNEL *Chan )
521 assert( Chan != NULL );
522 return Chan->maxusers;
523 } /* Channel_MaxUsers */
527 Channel_First( void )
530 } /* Channel_First */
534 Channel_Next( CHANNEL *Chan )
536 assert( Chan != NULL );
542 Channel_Search( const char *Name )
544 /* Search channel structure */
549 assert( Name != NULL );
551 search_hash = Hash( Name );
555 if( search_hash == c->hash )
558 if( strcasecmp( Name, c->name ) == 0 ) return c;
563 } /* Channel_Search */
567 Channel_FirstMember( CHANNEL *Chan )
569 assert( Chan != NULL );
570 return Get_First_Cl2Chan( NULL, Chan );
571 } /* Channel_FirstMember */
575 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
577 assert( Chan != NULL );
578 assert( Cl2Chan != NULL );
579 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
580 } /* Channel_NextMember */
584 Channel_FirstChannelOf( CLIENT *Client )
586 assert( Client != NULL );
587 return Get_First_Cl2Chan( Client, NULL );
588 } /* Channel_FirstChannelOf */
592 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
594 assert( Client != NULL );
595 assert( Cl2Chan != NULL );
596 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
597 } /* Channel_NextChannelOf */
601 Channel_GetClient( CL2CHAN *Cl2Chan )
603 assert( Cl2Chan != NULL );
604 return Cl2Chan->client;
605 } /* Channel_GetClient */
609 Channel_GetChannel( CL2CHAN *Cl2Chan )
611 assert( Cl2Chan != NULL );
612 return Cl2Chan->channel;
613 } /* Channel_GetChannel */
617 Channel_IsValidName( const char *Name )
619 assert( Name != NULL );
622 if (strlen(Name) <= 1)
625 if (strchr("#&+", Name[0]) == NULL)
627 if (strlen(Name) >= CHANNEL_NAME_LEN)
630 return Name[strcspn(Name, " ,:\007")] == 0;
631 } /* Channel_IsValidName */
635 Channel_ModeAdd( CHANNEL *Chan, char Mode )
638 * If the channel already had this mode, return false.
639 * If the channel mode was newly set return true.
644 assert( Chan != NULL );
646 x[0] = Mode; x[1] = '\0';
647 if( ! Channel_HasMode( Chan, x[0] ))
649 /* Channel does not have this mode yet, set it */
650 strlcat( Chan->modes, x, sizeof( Chan->modes ));
654 } /* Channel_ModeAdd */
658 Channel_ModeDel( CHANNEL *Chan, char Mode )
661 * if the mode was removed return true.
662 * if the channel did not have the mode, return false.
666 assert( Chan != NULL );
668 p = strchr( Chan->modes, Mode );
669 if( ! p ) return false;
671 /* Channel has mode -> delete */
678 } /* Channel_ModeDel */
682 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
684 /* Set Channel-User-Mode.
685 * if mode was newly set, return true.
686 * if the User already had this channel-mode, return false.
692 assert( Chan != NULL );
693 assert( Client != NULL );
695 cl2chan = Get_Cl2Chan( Chan, Client );
696 assert( cl2chan != NULL );
698 x[0] = Mode; x[1] = '\0';
699 if( ! strchr( cl2chan->modes, x[0] ))
701 /* mode not set, -> set it */
702 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
706 } /* Channel_UserModeAdd */
710 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
712 /* Delete Channel-User-Mode.
713 * If Mode was removed, return true.
714 * If User did not have the Channel-Mode, return false.
720 assert( Chan != NULL );
721 assert( Client != NULL );
723 cl2chan = Get_Cl2Chan( Chan, Client );
724 assert( cl2chan != NULL );
726 p = strchr( cl2chan->modes, Mode );
727 if( ! p ) return false;
729 /* Client has Mode -> delete */
736 } /* Channel_UserModeDel */
740 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
742 /* return Users' Channel-Modes */
746 assert( Chan != NULL );
747 assert( Client != NULL );
749 cl2chan = Get_Cl2Chan( Chan, Client );
750 assert( cl2chan != NULL );
752 return cl2chan->modes;
753 } /* Channel_UserModes */
757 Channel_UserHasMode( CHANNEL *Chan, CLIENT *Client, char Mode )
759 return strchr(Channel_UserModes(Chan, Client), Mode) != NULL;
760 } /* Channel_UserHasMode */
764 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
766 /* Test if Client is on Channel Chan */
768 assert( Chan != NULL );
769 assert( Client != NULL );
770 return Get_Cl2Chan(Chan, Client) != NULL;
771 } /* Channel_IsMemberOf */
775 Channel_Topic( CHANNEL *Chan )
778 assert( Chan != NULL );
779 ret = array_start(&Chan->topic);
780 return ret ? ret : "";
781 } /* Channel_Topic */
787 Channel_TopicTime(CHANNEL *Chan)
789 assert(Chan != NULL);
790 return (unsigned int) Chan->topic_time;
791 } /* Channel_TopicTime */
795 Channel_TopicWho(CHANNEL *Chan)
797 assert(Chan != NULL);
798 return Chan->topic_who;
799 } /* Channel_TopicWho */
803 Channel_CreationTime(CHANNEL *Chan)
805 assert(Chan != NULL);
806 return (unsigned int) Chan->creation_time;
807 } /* Channel_CreationTime */
813 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
816 assert( Chan != NULL );
817 assert( Topic != NULL );
820 if (len < array_bytes(&Chan->topic))
821 array_free(&Chan->topic);
823 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
824 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
825 Topic, Chan->name, strerror(errno));
827 Chan->topic_time = time(NULL);
828 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
829 strlcpy(Chan->topic_who, Client_ID(Client),
830 sizeof Chan->topic_who);
832 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
833 sizeof Chan->topic_who);
837 } /* Channel_SetTopic */
841 Channel_SetModes( CHANNEL *Chan, const char *Modes )
843 assert( Chan != NULL );
844 assert( Modes != NULL );
846 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
847 } /* Channel_SetModes */
851 Channel_SetKey( CHANNEL *Chan, const char *Key )
853 assert( Chan != NULL );
854 assert( Key != NULL );
856 strlcpy( Chan->key, Key, sizeof( Chan->key ));
857 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
858 } /* Channel_SetKey */
862 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
864 assert( Chan != NULL );
866 Chan->maxusers = Count;
867 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
868 } /* Channel_SetMaxUsers */
872 * Check if a client is allowed to send to a specific channel.
874 * @param Chan The channel to check.
875 * @param From The client that wants to send.
876 * @return true if the client is allowed to send, false otherwise.
879 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
881 bool is_member, has_voice, is_halfop, is_op, is_chanadmin, is_owner;
883 is_member = has_voice = is_halfop = is_op = is_chanadmin = is_owner = false;
885 /* The server itself always can send messages :-) */
886 if (Client_ThisServer() == From)
889 if (Channel_IsMemberOf(Chan, From)) {
891 if (Channel_UserHasMode(Chan, From, 'v'))
893 if (Channel_UserHasMode(Chan, From, 'h'))
895 if (Channel_UserHasMode(Chan, From, 'o'))
897 if (Channel_UserHasMode(Chan, From, 'a'))
899 if (Channel_UserHasMode(Chan, From, 'q'))
904 * Is the client allowed to write to channel?
906 * If channel mode n set: non-members cannot send to channel.
907 * If channel mode m set: need voice.
909 if (Channel_HasMode(Chan, 'n') && !is_member)
912 if (Channel_HasMode(Chan, 'M') && !Client_HasMode(From, 'R')
913 && !Client_HasMode(From, 'o'))
916 if (has_voice || is_halfop || is_op || is_chanadmin || is_owner)
919 if (Channel_HasMode(Chan, 'm'))
922 if (Lists_Check(&Chan->list_excepts, From))
925 return !Lists_Check(&Chan->list_bans, From);
930 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
931 bool SendErrors, const char *Text)
933 if (!Can_Send_To_Channel(Chan, From)) {
935 return CONNECTED; /* no error, see RFC 2812 */
936 if (Channel_HasMode(Chan, 'M'))
937 return IRC_WriteErrClient(From, ERR_NEEDREGGEDNICK_MSG,
938 Client_ID(From), Channel_Name(Chan));
940 return IRC_WriteErrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
941 Client_ID(From), Channel_Name(Chan));
944 if (Client_Conn(From) > NONE)
945 Conn_UpdateIdle(Client_Conn(From));
947 IRC_WriteStrChannelPrefix(Client, Chan, From, true, "%s %s :%s",
948 Command, Channel_Name(Chan), Text);
954 Channel_Create( const char *Name )
956 /* Create new CHANNEL structure and add it to linked list */
959 assert( Name != NULL );
961 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
964 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
967 memset( c, 0, sizeof( CHANNEL ));
968 strlcpy( c->name, Name, sizeof( c->name ));
969 c->hash = Hash( c->name );
970 c->next = My_Channels;
972 c->creation_time = time(NULL);
975 LogDebug("Created new channel structure for \"%s\".", Name);
977 } /* Channel_Create */
981 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
985 assert( Chan != NULL );
986 assert( Client != NULL );
988 cl2chan = My_Cl2Chan;
991 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
992 cl2chan = cl2chan->next;
999 Add_Client( CHANNEL *Chan, CLIENT *Client )
1003 assert( Chan != NULL );
1004 assert( Client != NULL );
1006 /* Create new CL2CHAN structure */
1007 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
1010 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
1013 cl2chan->channel = Chan;
1014 cl2chan->client = Client;
1015 strcpy( cl2chan->modes, "" );
1018 cl2chan->next = My_Cl2Chan;
1019 My_Cl2Chan = cl2chan;
1021 LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
1028 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
1030 CL2CHAN *cl2chan, *last_cl2chan;
1033 assert( Chan != NULL );
1034 assert( Client != NULL );
1035 assert( Origin != NULL );
1036 assert( Reason != NULL );
1038 /* Do not inform other servers if the channel is local to this server,
1039 * regardless of what the caller requested! */
1041 InformServer = !Channel_IsLocal(Chan);
1043 last_cl2chan = NULL;
1044 cl2chan = My_Cl2Chan;
1047 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
1048 last_cl2chan = cl2chan;
1049 cl2chan = cl2chan->next;
1051 if( ! cl2chan ) return false;
1053 c = cl2chan->channel;
1054 assert( c != NULL );
1056 /* maintain cl2chan list */
1057 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
1058 else My_Cl2Chan = cl2chan->next;
1064 /* QUIT: other servers have already been notified,
1065 * see Client_Destroy(); so only inform other clients
1066 * in same channel. */
1067 assert( InformServer == false );
1068 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1069 Client_Mask( Client ), c->name, Reason );
1072 /* User was KICKed: inform other servers (public
1073 * channels) and all users in the channel */
1075 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
1076 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
1077 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
1078 c->name, Client_ID( Client ), Reason );
1079 if ((Client_Conn(Client) > NONE) &&
1080 (Client_Type(Client) == CLIENT_USER))
1082 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
1083 c->name, Client_ID( Client ), Reason);
1085 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
1086 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
1089 if (Conf_MorePrivacy)
1093 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
1095 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
1098 if ((Client_Conn(Origin) > NONE) &&
1099 (Client_Type(Origin) == CLIENT_USER))
1101 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
1102 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1103 Client_Mask(Client), c->name, Reason);
1107 /* When channel is empty and is not pre-defined, delete */
1108 if( ! Channel_HasMode( Chan, 'P' ))
1110 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
1114 } /* Remove_Client */
1118 Channel_AddBan(CHANNEL *c, const char *mask, const char *who )
1120 struct list_head *h = Channel_GetListBans(c);
1121 LogDebug("Adding \"%s\" to \"%s\" ban list", mask, Channel_Name(c));
1122 return Lists_Add(h, mask, time(NULL), who, false);
1127 Channel_AddExcept(CHANNEL *c, const char *mask, const char *who )
1129 struct list_head *h = Channel_GetListExcepts(c);
1130 LogDebug("Adding \"%s\" to \"%s\" exception list", mask, Channel_Name(c));
1131 return Lists_Add(h, mask, time(NULL), who, false);
1136 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce, const char *who )
1138 struct list_head *h = Channel_GetListInvites(c);
1139 LogDebug("Adding \"%s\" to \"%s\" invite list", mask, Channel_Name(c));
1140 return Lists_Add(h, mask, time(NULL), who, onlyonce);
1145 ShowChannelList(struct list_head *head, CLIENT *Client, CHANNEL *Channel,
1146 char *msg, char *msg_end)
1148 struct list_elem *e;
1150 assert (Client != NULL);
1151 assert (Channel != NULL);
1153 e = Lists_GetFirst(head);
1155 if (!IRC_WriteStrClient(Client, msg, Client_ID(Client),
1156 Channel_Name(Channel),
1159 Lists_GetValidity(e)))
1160 return DISCONNECTED;
1161 e = Lists_GetNext(e);
1164 return IRC_WriteStrClient(Client, msg_end, Client_ID(Client),
1165 Channel_Name(Channel));
1170 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1172 struct list_head *h;
1174 assert( Channel != NULL );
1176 h = Channel_GetListBans(Channel);
1177 return ShowChannelList(h, Client, Channel, RPL_BANLIST_MSG,
1178 RPL_ENDOFBANLIST_MSG);
1183 Channel_ShowExcepts( CLIENT *Client, CHANNEL *Channel )
1185 struct list_head *h;
1187 assert( Channel != NULL );
1189 h = Channel_GetListExcepts(Channel);
1190 return ShowChannelList(h, Client, Channel, RPL_EXCEPTLIST_MSG,
1191 RPL_ENDOFEXCEPTLIST_MSG);
1196 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1198 struct list_head *h;
1200 assert( Channel != NULL );
1202 h = Channel_GetListInvites(Channel);
1203 return ShowChannelList(h, Client, Channel, RPL_INVITELIST_MSG,
1204 RPL_ENDOFINVITELIST_MSG);
1209 * Log a message to the local &SERVER channel, if it exists.
1212 Channel_LogServer(const char *msg)
1217 assert(msg != NULL);
1219 sc = Channel_Search("&SERVER");
1223 c = Client_ThisServer();
1224 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1225 } /* Channel_LogServer */
1229 Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
1231 char *file_name, line[COMMAND_LEN], *nick, *pass;
1234 assert(Chan != NULL);
1235 assert(Client != NULL);
1236 assert(Key != NULL);
1238 if (!Channel_HasMode(Chan, 'k'))
1242 if (strcmp(Chan->key, Key) == 0)
1245 file_name = array_start(&Chan->keyfile);
1248 fd = fopen(file_name, "r");
1250 Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
1251 file_name, Chan->name, strerror(errno));
1255 while (fgets(line, (int)sizeof(line), fd) != NULL) {
1257 if (! (nick = strchr(line, ':')))
1260 if (!Match(line, Client_User(Client)))
1262 if (! (pass = strchr(nick, ':')))
1265 if (!Match(nick, Client_ID(Client)))
1267 if (strcmp(Key, pass) != 0)
1275 } /* Channel_CheckKey */
1279 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1281 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1282 } /* Get_First_Cl2Chan */
1286 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1290 assert( Client != NULL || Channel != NULL );
1295 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1296 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1297 cl2chan = cl2chan->next;
1300 } /* Get_Next_Cl2Chan */
1304 * Remove a channel and free all of its data structures.
1307 Delete_Channel(CHANNEL *Chan)
1309 CHANNEL *chan, *last_chan;
1320 assert(chan != NULL);
1324 /* maintain channel list */
1326 last_chan->next = chan->next;
1328 My_Channels = chan->next;
1330 LogDebug("Freed channel structure for \"%s\".", Chan->name);
1332 } /* Delete_Channel */
1336 Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
1340 assert(Chan != NULL);
1341 assert(KeyFile != NULL);
1343 len = strlen(KeyFile);
1344 if (len < array_bytes(&Chan->keyfile)) {
1345 Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
1346 array_free(&Chan->keyfile);
1352 if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
1354 "Could not set new channel key file \"%s\" for %s: %s",
1355 KeyFile, Chan->name, strerror(errno));
1357 Log(LOG_INFO|LOG_snotice,
1358 "New local channel key file \"%s\" for %s activated.",
1359 KeyFile, Chan->name);