2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2011 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.
30 #include "conn-func.h"
36 #include "irc-write.h"
52 static CHANNEL *My_Channels;
53 static CL2CHAN *My_Cl2Chan;
56 static CL2CHAN *Get_Cl2Chan PARAMS(( CHANNEL *Chan, CLIENT *Client ));
57 static CL2CHAN *Add_Client PARAMS(( CHANNEL *Chan, CLIENT *Client ));
58 static bool Remove_Client PARAMS(( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer ));
59 static CL2CHAN *Get_First_Cl2Chan PARAMS(( CLIENT *Client, CHANNEL *Chan ));
60 static CL2CHAN *Get_Next_Cl2Chan PARAMS(( CL2CHAN *Start, CLIENT *Client, CHANNEL *Chan ));
61 static void Delete_Channel PARAMS(( CHANNEL *Chan ));
62 static void Free_Channel PARAMS(( CHANNEL *Chan ));
63 static void Set_KeyFile PARAMS((CHANNEL *Chan, const char *KeyFile));
74 sc = Channel_Create("&SERVER");
76 Channel_SetModes(sc, "mnPt");
77 Channel_SetTopic(sc, Client_ThisServer(), "Server Messages");
82 GLOBAL struct list_head *
83 Channel_GetListBans(CHANNEL *c)
90 GLOBAL struct list_head *
91 Channel_GetListInvites(CHANNEL *c)
94 return &c->list_invites;
99 Channel_InitPredefined( void )
101 /* Generate predefined persistent channels */
104 const struct Conf_Channel *conf_chan;
106 size_t i, channel_count = array_length(&Conf_Channels, sizeof(*conf_chan));
108 conf_chan = array_start(&Conf_Channels);
110 assert(channel_count == 0 || conf_chan != NULL);
112 for (i = 0; i < channel_count; i++, conf_chan++) {
113 if (!conf_chan->name[0] || !Channel_IsValidName(conf_chan->name)) {
114 Log(LOG_ERR, "Can't create pre-defined channel: invalid name: \"%s\"",
119 new_chan = Channel_Search(conf_chan->name);
122 "Can't create pre-defined channel \"%s\": name already in use.",
124 Set_KeyFile(new_chan, conf_chan->keyfile);
128 new_chan = Channel_Create(conf_chan->name);
130 Log(LOG_ERR, "Can't create pre-defined channel \"%s\"",
134 Log(LOG_INFO, "Created pre-defined channel \"%s\"",
137 Channel_ModeAdd(new_chan, 'P');
139 if (conf_chan->topic[0])
140 Channel_SetTopic(new_chan, NULL, conf_chan->topic);
142 c = conf_chan->modes;
144 Channel_ModeAdd(new_chan, *c++);
146 Channel_SetKey(new_chan, conf_chan->key);
147 Channel_SetMaxUsers(new_chan, conf_chan->maxusers);
148 Set_KeyFile(new_chan, conf_chan->keyfile);
151 array_free(&Conf_Channels);
152 } /* Channel_InitPredefined */
156 Free_Channel(CHANNEL *chan)
158 array_free(&chan->topic);
159 array_free(&chan->keyfile);
160 Lists_Free(&chan->list_bans);
161 Lists_Free(&chan->list_invites);
171 CL2CHAN *cl2chan, *cl2chan_next;
173 /* free struct Channel */
181 /* Free Channel allocation table */
182 cl2chan = My_Cl2Chan;
184 cl2chan_next = cl2chan->next;
186 cl2chan = cl2chan_next;
193 * This function lets a client join a channel. First, the function
194 * checks that the specified channel name is valid and that the client
195 * isn't already a member. If the specified channel doesn't exist,
196 * a new channel is created. Client is added to channel by function
200 Channel_Join( CLIENT *Client, const char *Name )
204 assert(Client != NULL);
205 assert(Name != NULL);
207 /* Check that the channel name is valid */
208 if (! Channel_IsValidName(Name)) {
209 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
210 Client_ID(Client), Name);
214 chan = Channel_Search(Name);
216 /* Check if the client is already in the channel */
217 if (Get_Cl2Chan(chan, Client))
220 /* If the specified channel does not exist, the channel
222 chan = Channel_Create(Name);
227 /* Add user to Channel */
228 if (! Add_Client(chan, Client))
236 * Part client from channel.
237 * This function lets a client part from a channel. First, the function checks
238 * if the channel exists and the client is a member of it and sends out
239 * appropriate error messages if not. The real work is done by the function
243 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
247 assert(Client != NULL);
248 assert(Name != NULL);
249 assert(Reason != NULL);
251 /* Check that specified channel exists */
252 chan = Channel_Search(Name);
254 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
255 Client_ID(Client), Name);
259 /* Check that the client is in the channel */
260 if (!Get_Cl2Chan(chan, Client)) {
261 IRC_WriteStrClient(Client, ERR_NOTONCHANNEL_MSG,
262 Client_ID(Client), Name);
266 if (Conf_MorePrivacy)
269 /* Part client from channel */
270 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
278 * Kick user from Channel
281 Channel_Kick(CLIENT *Peer, CLIENT *Target, CLIENT *Origin, const char *Name,
286 assert(Peer != NULL);
287 assert(Target != NULL);
288 assert(Origin != NULL);
289 assert(Name != NULL);
290 assert(Reason != NULL);
292 /* Check that channel exists */
293 chan = Channel_Search( Name );
296 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
300 if (Client_Type(Peer) != CLIENT_SERVER &&
301 Client_Type(Origin) != CLIENT_SERVICE) {
302 /* Check that user is on the specified channel */
303 if (!Channel_IsMemberOf(chan, Origin)) {
304 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG,
305 Client_ID(Origin), Name);
309 /* Check if user has operator status */
310 if (!strchr(Channel_UserModes(chan, Origin), 'o')) {
311 IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG,
312 Client_ID(Origin), Name);
317 /* Check that the client to be kicked is on the specified channel */
318 if (!Channel_IsMemberOf(chan, Target)) {
319 IRC_WriteStrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
320 Client_ID(Origin), Client_ID(Target), Name );
324 /* Kick Client from channel */
325 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
330 Channel_Quit( CLIENT *Client, const char *Reason )
334 assert( Client != NULL );
335 assert( Reason != NULL );
337 if (Conf_MorePrivacy)
340 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
346 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
353 Channel_Count( void )
356 unsigned long count = 0;
365 } /* Channel_Count */
369 Channel_MemberCount( CHANNEL *Chan )
372 unsigned long count = 0;
374 assert( Chan != NULL );
376 cl2chan = My_Cl2Chan;
379 if( cl2chan->channel == Chan ) count++;
380 cl2chan = cl2chan->next;
383 } /* Channel_MemberCount */
387 Channel_CountForUser( CLIENT *Client )
389 /* Count number of channels a user is member of. */
394 assert( Client != NULL );
396 cl2chan = My_Cl2Chan;
399 if( cl2chan->client == Client ) count++;
400 cl2chan = cl2chan->next;
404 } /* Channel_CountForUser */
408 Channel_Name( const CHANNEL *Chan )
410 assert( Chan != NULL );
416 Channel_Modes( CHANNEL *Chan )
418 assert( Chan != NULL );
420 } /* Channel_Modes */
424 Channel_Key( CHANNEL *Chan )
426 assert( Chan != NULL );
432 Channel_MaxUsers( CHANNEL *Chan )
434 assert( Chan != NULL );
435 return Chan->maxusers;
436 } /* Channel_MaxUsers */
440 Channel_First( void )
443 } /* Channel_First */
447 Channel_Next( CHANNEL *Chan )
449 assert( Chan != NULL );
455 Channel_Search( const char *Name )
457 /* Search channel structure */
462 assert( Name != NULL );
464 search_hash = Hash( Name );
468 if( search_hash == c->hash )
471 if( strcasecmp( Name, c->name ) == 0 ) return c;
476 } /* Channel_Search */
480 Channel_FirstMember( CHANNEL *Chan )
482 assert( Chan != NULL );
483 return Get_First_Cl2Chan( NULL, Chan );
484 } /* Channel_FirstMember */
488 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
490 assert( Chan != NULL );
491 assert( Cl2Chan != NULL );
492 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
493 } /* Channel_NextMember */
497 Channel_FirstChannelOf( CLIENT *Client )
499 assert( Client != NULL );
500 return Get_First_Cl2Chan( Client, NULL );
501 } /* Channel_FirstChannelOf */
505 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
507 assert( Client != NULL );
508 assert( Cl2Chan != NULL );
509 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
510 } /* Channel_NextChannelOf */
514 Channel_GetClient( CL2CHAN *Cl2Chan )
516 assert( Cl2Chan != NULL );
517 return Cl2Chan->client;
518 } /* Channel_GetClient */
522 Channel_GetChannel( CL2CHAN *Cl2Chan )
524 assert( Cl2Chan != NULL );
525 return Cl2Chan->channel;
526 } /* Channel_GetChannel */
530 Channel_IsValidName( const char *Name )
532 assert( Name != NULL );
535 if (strlen(Name) <= 1)
538 if (strchr("#&+", Name[0]) == NULL)
540 if (strlen(Name) >= CHANNEL_NAME_LEN)
543 return Name[strcspn(Name, " ,:\007")] == 0;
544 } /* Channel_IsValidName */
548 Channel_ModeAdd( CHANNEL *Chan, char Mode )
551 * If the channel already had this mode, return false.
552 * If the channel mode was newly set return true.
557 assert( Chan != NULL );
559 x[0] = Mode; x[1] = '\0';
560 if( ! strchr( Chan->modes, x[0] ))
562 /* Channel does not have this mode yet, set it */
563 strlcat( Chan->modes, x, sizeof( Chan->modes ));
567 } /* Channel_ModeAdd */
571 Channel_ModeDel( CHANNEL *Chan, char Mode )
574 * if the mode was removed return true.
575 * if the channel did not have the mode, return false.
579 assert( Chan != NULL );
581 p = strchr( Chan->modes, Mode );
582 if( ! p ) return false;
584 /* Channel has mode -> delete */
591 } /* Channel_ModeDel */
595 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
597 /* Set Channel-User-Mode.
598 * if mode was newly set, return true.
599 * if the User already had this channel-mode, return false.
605 assert( Chan != NULL );
606 assert( Client != NULL );
608 cl2chan = Get_Cl2Chan( Chan, Client );
609 assert( cl2chan != NULL );
611 x[0] = Mode; x[1] = '\0';
612 if( ! strchr( cl2chan->modes, x[0] ))
614 /* mode not set, -> set it */
615 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
619 } /* Channel_UserModeAdd */
623 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
625 /* Delete Channel-User-Mode.
626 * If Mode was removed, return true.
627 * If User did not have the Channel-Mode, return false.
633 assert( Chan != NULL );
634 assert( Client != NULL );
636 cl2chan = Get_Cl2Chan( Chan, Client );
637 assert( cl2chan != NULL );
639 p = strchr( cl2chan->modes, Mode );
640 if( ! p ) return false;
642 /* Client has Mode -> delete */
649 } /* Channel_UserModeDel */
653 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
655 /* return Users' Channel-Modes */
659 assert( Chan != NULL );
660 assert( Client != NULL );
662 cl2chan = Get_Cl2Chan( Chan, Client );
663 assert( cl2chan != NULL );
665 return cl2chan->modes;
666 } /* Channel_UserModes */
670 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
672 /* Test if Client is on Channel Chan */
674 assert( Chan != NULL );
675 assert( Client != NULL );
676 return Get_Cl2Chan(Chan, Client) != NULL;
677 } /* Channel_IsMemberOf */
681 Channel_Topic( CHANNEL *Chan )
684 assert( Chan != NULL );
685 ret = array_start(&Chan->topic);
686 return ret ? ret : "";
687 } /* Channel_Topic */
693 Channel_TopicTime(CHANNEL *Chan)
695 assert(Chan != NULL);
696 return (unsigned int) Chan->topic_time;
697 } /* Channel_TopicTime */
701 Channel_TopicWho(CHANNEL *Chan)
703 assert(Chan != NULL);
704 return Chan->topic_who;
705 } /* Channel_TopicWho */
709 Channel_CreationTime(CHANNEL *Chan)
711 assert(Chan != NULL);
712 return (unsigned int) Chan->creation_time;
713 } /* Channel_CreationTime */
719 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
722 assert( Chan != NULL );
723 assert( Topic != NULL );
726 if (len < array_bytes(&Chan->topic))
727 array_free(&Chan->topic);
729 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
730 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
731 Topic, Chan->name, strerror(errno));
733 Chan->topic_time = time(NULL);
734 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
735 strlcpy(Chan->topic_who, Client_ID(Client),
736 sizeof Chan->topic_who);
738 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
739 sizeof Chan->topic_who);
743 } /* Channel_SetTopic */
747 Channel_SetModes( CHANNEL *Chan, const char *Modes )
749 assert( Chan != NULL );
750 assert( Modes != NULL );
752 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
753 } /* Channel_SetModes */
757 Channel_SetKey( CHANNEL *Chan, const char *Key )
759 assert( Chan != NULL );
760 assert( Key != NULL );
762 strlcpy( Chan->key, Key, sizeof( Chan->key ));
763 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
764 } /* Channel_SetKey */
768 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
770 assert( Chan != NULL );
772 Chan->maxusers = Count;
773 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
774 } /* Channel_SetMaxUsers */
778 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
780 bool is_member, has_voice, is_op;
782 is_member = has_voice = is_op = false;
784 /* The server itself always can send messages :-) */
785 if (Client_ThisServer() == From)
788 if (Channel_IsMemberOf(Chan, From)) {
790 if (strchr(Channel_UserModes(Chan, From), 'v'))
792 if (strchr(Channel_UserModes(Chan, From), 'o'))
797 * Is the client allowed to write to channel?
799 * If channel mode n set: non-members cannot send to channel.
800 * If channel mode m set: need voice.
802 if (strchr(Channel_Modes(Chan), 'n') && !is_member)
805 if (is_op || has_voice)
808 if (strchr(Channel_Modes(Chan), 'm'))
811 return !Lists_Check(&Chan->list_bans, From);
816 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
817 bool SendErrors, const char *Text)
819 if (!Can_Send_To_Channel(Chan, From)) {
821 return CONNECTED; /* no error, see RFC 2812 */
822 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
823 Client_ID(From), Channel_Name(Chan));
826 if (Client_Conn(From) > NONE)
827 Conn_UpdateIdle(Client_Conn(From));
829 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
830 "%s %s :%s", Command, Channel_Name(Chan), Text);
835 Channel_Create( const char *Name )
837 /* Create new CHANNEL structure and add it to linked list */
840 assert( Name != NULL );
842 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
845 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
848 memset( c, 0, sizeof( CHANNEL ));
849 strlcpy( c->name, Name, sizeof( c->name ));
850 c->hash = Hash( c->name );
851 c->next = My_Channels;
853 c->creation_time = time(NULL);
856 LogDebug("Created new channel structure for \"%s\".", Name);
858 } /* Channel_Create */
862 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
866 assert( Chan != NULL );
867 assert( Client != NULL );
869 cl2chan = My_Cl2Chan;
872 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
873 cl2chan = cl2chan->next;
880 Add_Client( CHANNEL *Chan, CLIENT *Client )
884 assert( Chan != NULL );
885 assert( Client != NULL );
887 /* Create new CL2CHAN structure */
888 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
891 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
894 cl2chan->channel = Chan;
895 cl2chan->client = Client;
896 strcpy( cl2chan->modes, "" );
899 cl2chan->next = My_Cl2Chan;
900 My_Cl2Chan = cl2chan;
902 LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
909 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
911 CL2CHAN *cl2chan, *last_cl2chan;
914 assert( Chan != NULL );
915 assert( Client != NULL );
916 assert( Origin != NULL );
917 assert( Reason != NULL );
919 /* Do not inform other servers if the channel is local to this server,
920 * regardless of what the caller requested! */
922 InformServer = !Channel_IsLocal(Chan);
925 cl2chan = My_Cl2Chan;
928 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
929 last_cl2chan = cl2chan;
930 cl2chan = cl2chan->next;
932 if( ! cl2chan ) return false;
934 c = cl2chan->channel;
937 /* maintain cl2chan list */
938 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
939 else My_Cl2Chan = cl2chan->next;
945 /* QUIT: other servers have already been notified,
946 * see Client_Destroy(); so only inform other clients
947 * in same channel. */
948 assert( InformServer == false );
949 LogDebug("User \"%s\" left channel \"%s\" (%s).",
950 Client_Mask( Client ), c->name, Reason );
953 /* User was KICKed: inform other servers (public
954 * channels) and all users in the channel */
956 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
957 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
958 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
959 c->name, Client_ID( Client ), Reason );
960 if ((Client_Conn(Client) > NONE) &&
961 (Client_Type(Client) == CLIENT_USER))
963 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
964 c->name, Client_ID( Client ), Reason);
966 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
967 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
970 if (Conf_MorePrivacy)
974 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
976 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
979 if ((Client_Conn(Origin) > NONE) &&
980 (Client_Type(Origin) == CLIENT_USER))
982 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
983 LogDebug("User \"%s\" left channel \"%s\" (%s).",
984 Client_Mask(Client), c->name, Reason);
988 /* When channel is empty and is not pre-defined, delete */
989 if( ! strchr( Channel_Modes( Chan ), 'P' ))
991 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
995 } /* Remove_Client */
999 Channel_AddBan(CHANNEL *c, const char *mask )
1001 struct list_head *h = Channel_GetListBans(c);
1002 LogDebug("Adding \"%s\" to \"%s\" %s list", mask, Channel_Name(c), "ban");
1003 return Lists_Add(h, mask, false);
1008 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
1010 struct list_head *h = Channel_GetListInvites(c);
1011 LogDebug("Adding \"%s\" to \"%s\" %s list", mask, Channel_Name(c), "invite");
1012 return Lists_Add(h, mask, onlyonce);
1017 ShowInvitesBans(struct list_head *head, CLIENT *Client, CHANNEL *Channel, bool invite)
1019 struct list_elem *e;
1020 char *msg = invite ? RPL_INVITELIST_MSG : RPL_BANLIST_MSG;
1023 assert( Client != NULL );
1024 assert( Channel != NULL );
1026 e = Lists_GetFirst(head);
1028 if( ! IRC_WriteStrClient( Client, msg, Client_ID( Client ),
1029 Channel_Name( Channel ), Lists_GetMask(e) )) return DISCONNECTED;
1030 e = Lists_GetNext(e);
1033 msg_end = invite ? RPL_ENDOFINVITELIST_MSG : RPL_ENDOFBANLIST_MSG;
1034 return IRC_WriteStrClient( Client, msg_end, Client_ID( Client ), Channel_Name( Channel ));
1039 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1041 struct list_head *h;
1043 assert( Channel != NULL );
1045 h = Channel_GetListBans(Channel);
1046 return ShowInvitesBans(h, Client, Channel, false);
1051 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1053 struct list_head *h;
1055 assert( Channel != NULL );
1057 h = Channel_GetListInvites(Channel);
1058 return ShowInvitesBans(h, Client, Channel, true);
1063 * Log a message to the local &SERVER channel, if it exists.
1066 Channel_LogServer(const char *msg)
1071 assert(msg != NULL);
1073 sc = Channel_Search("&SERVER");
1077 c = Client_ThisServer();
1078 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1079 } /* Channel_LogServer */
1083 Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
1085 char *file_name, line[COMMAND_LEN], *nick, *pass;
1088 assert(Chan != NULL);
1089 assert(Client != NULL);
1090 assert(Key != NULL);
1092 if (!strchr(Chan->modes, 'k'))
1096 if (strcmp(Chan->key, Key) == 0)
1099 file_name = array_start(&Chan->keyfile);
1102 fd = fopen(file_name, "r");
1104 Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
1105 file_name, Chan->name, strerror(errno));
1109 while (fgets(line, (int)sizeof(line), fd) != NULL) {
1111 if (! (nick = strchr(line, ':')))
1114 if (!Match(line, Client_User(Client)))
1116 if (! (pass = strchr(nick, ':')))
1119 if (!Match(nick, Client_ID(Client)))
1121 if (strcmp(Key, pass) != 0)
1129 } /* Channel_CheckKey */
1133 * Check wether a client is allowed to administer a channel or not.
1135 * @param Chan The channel to test.
1136 * @param Client The client from which the command has been received.
1137 * @param Origin The originator of the command (or NULL).
1138 * @param OnChannel Set to true if the originator is member of the channel.
1139 * @param AdminOk Set to true if the client is allowed to do
1140 * administrative tasks on this channel.
1141 * @param UseServerMode Set to true if ngIRCd should emulate "server mode",
1142 * that is send commands as if originating from a server
1143 * and not the originator of the command.
1146 Channel_CheckAdminRights(CHANNEL *Chan, CLIENT *Client, CLIENT *Origin,
1147 bool *OnChannel, bool *AdminOk, bool *UseServerMode)
1149 assert (Chan != NULL);
1150 assert (Client != NULL);
1151 assert (OnChannel != NULL);
1152 assert (AdminOk != NULL);
1153 assert (UseServerMode != NULL);
1155 /* Use the client as origin, if no origin has been given (no prefix?) */
1161 *UseServerMode = false;
1163 if (Client_Type(Client) != CLIENT_USER
1164 && Client_Type(Client) != CLIENT_SERVER
1165 && Client_Type(Client) != CLIENT_SERVICE)
1168 /* Allow channel administration if the client is a server or service */
1169 if (Client_Type(Client) != CLIENT_USER) {
1174 *OnChannel = Channel_IsMemberOf(Chan, Origin);
1176 if (*OnChannel && strchr(Channel_UserModes(Chan, Origin), 'o')) {
1177 /* User is a channel operator */
1179 } else if (Conf_OperCanMode) {
1180 /* IRC operators are allowed to administer channels as well */
1181 if (Client_OperByMe(Origin)) {
1183 if (Conf_OperServerMode)
1184 *UseServerMode = true;
1187 } /* Channel_CheckAdminRights */
1191 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1193 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1194 } /* Get_First_Cl2Chan */
1198 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1202 assert( Client != NULL || Channel != NULL );
1207 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1208 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1209 cl2chan = cl2chan->next;
1212 } /* Get_Next_Cl2Chan */
1216 * Remove a channel and free all of its data structures.
1219 Delete_Channel(CHANNEL *Chan)
1221 CHANNEL *chan, *last_chan;
1232 assert(chan != NULL);
1236 /* maintain channel list */
1238 last_chan->next = chan->next;
1240 My_Channels = chan->next;
1242 LogDebug("Freed channel structure for \"%s\".", Chan->name);
1244 } /* Delete_Channel */
1248 Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
1252 assert(Chan != NULL);
1253 assert(KeyFile != NULL);
1255 len = strlen(KeyFile);
1256 if (len < array_bytes(&Chan->keyfile)) {
1257 Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
1258 array_free(&Chan->keyfile);
1264 if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
1266 "Could not set new channel key file \"%s\" for %s: %s",
1267 KeyFile, Chan->name, strerror(errno));
1269 Log(LOG_INFO|LOG_snotice,
1270 "New local channel key file \"%s\" for %s activated.",
1271 KeyFile, Chan->name);