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])
115 if (!Channel_IsValidName(conf_chan->name)) {
117 "Can't create pre-defined channel: invalid name: \"%s\"",
122 new_chan = Channel_Search(conf_chan->name);
125 "Can't create pre-defined channel \"%s\": name already in use.",
127 Set_KeyFile(new_chan, conf_chan->keyfile);
131 new_chan = Channel_Create(conf_chan->name);
133 Log(LOG_ERR, "Can't create pre-defined channel \"%s\"",
137 Log(LOG_INFO, "Created pre-defined channel \"%s\"",
140 Channel_ModeAdd(new_chan, 'P');
142 if (conf_chan->topic[0])
143 Channel_SetTopic(new_chan, NULL, conf_chan->topic);
145 c = conf_chan->modes;
147 Channel_ModeAdd(new_chan, *c++);
149 Channel_SetKey(new_chan, conf_chan->key);
150 Channel_SetMaxUsers(new_chan, conf_chan->maxusers);
151 Set_KeyFile(new_chan, conf_chan->keyfile);
154 array_free(&Conf_Channels);
155 } /* Channel_InitPredefined */
159 Free_Channel(CHANNEL *chan)
161 array_free(&chan->topic);
162 array_free(&chan->keyfile);
163 Lists_Free(&chan->list_bans);
164 Lists_Free(&chan->list_invites);
174 CL2CHAN *cl2chan, *cl2chan_next;
176 /* free struct Channel */
184 /* Free Channel allocation table */
185 cl2chan = My_Cl2Chan;
187 cl2chan_next = cl2chan->next;
189 cl2chan = cl2chan_next;
196 * This function lets a client join a channel. First, the function
197 * checks that the specified channel name is valid and that the client
198 * isn't already a member. If the specified channel doesn't exist,
199 * a new channel is created. Client is added to channel by function
203 Channel_Join( CLIENT *Client, const char *Name )
207 assert(Client != NULL);
208 assert(Name != NULL);
210 /* Check that the channel name is valid */
211 if (! Channel_IsValidName(Name)) {
212 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
213 Client_ID(Client), Name);
217 chan = Channel_Search(Name);
219 /* Check if the client is already in the channel */
220 if (Get_Cl2Chan(chan, Client))
223 /* If the specified channel does not exist, the channel
225 chan = Channel_Create(Name);
230 /* Add user to Channel */
231 if (! Add_Client(chan, Client))
239 * Part client from channel.
240 * This function lets a client part from a channel. First, the function checks
241 * if the channel exists and the client is a member of it and sends out
242 * appropriate error messages if not. The real work is done by the function
246 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
250 assert(Client != NULL);
251 assert(Name != NULL);
252 assert(Reason != NULL);
254 /* Check that specified channel exists */
255 chan = Channel_Search(Name);
257 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
258 Client_ID(Client), Name);
262 /* Check that the client is in the channel */
263 if (!Get_Cl2Chan(chan, Client)) {
264 IRC_WriteStrClient(Client, ERR_NOTONCHANNEL_MSG,
265 Client_ID(Client), Name);
269 if (Conf_MorePrivacy)
272 /* Part client from channel */
273 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
281 * Kick user from Channel
284 Channel_Kick(CLIENT *Peer, CLIENT *Target, CLIENT *Origin, const char *Name,
289 assert(Peer != NULL);
290 assert(Target != NULL);
291 assert(Origin != NULL);
292 assert(Name != NULL);
293 assert(Reason != NULL);
295 /* Check that channel exists */
296 chan = Channel_Search( Name );
299 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
303 if (Client_Type(Peer) != CLIENT_SERVER &&
304 Client_Type(Origin) != CLIENT_SERVICE) {
305 /* Check that user is on the specified channel */
306 if (!Channel_IsMemberOf(chan, Origin)) {
307 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG,
308 Client_ID(Origin), Name);
312 /* Check if user has operator status */
313 if (!strchr(Channel_UserModes(chan, Origin), 'o')) {
314 IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG,
315 Client_ID(Origin), Name);
320 /* Check that the client to be kicked is on the specified channel */
321 if (!Channel_IsMemberOf(chan, Target)) {
322 IRC_WriteStrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
323 Client_ID(Origin), Client_ID(Target), Name );
327 /* Kick Client from channel */
328 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
333 Channel_Quit( CLIENT *Client, const char *Reason )
337 assert( Client != NULL );
338 assert( Reason != NULL );
340 if (Conf_MorePrivacy)
343 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
349 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
356 * Get number of channels this server knows and that are "visible" to
357 * the given client. If no client is given, all channels will be counted.
359 * @param Client The client to check or NULL.
360 * @return Number of channels visible to the client.
363 Channel_CountVisible (CLIENT *Client)
366 unsigned long count = 0;
371 if (!strchr(Channel_Modes(c), 's')
372 || Channel_IsMemberOf(c, Client))
383 Channel_MemberCount( CHANNEL *Chan )
386 unsigned long count = 0;
388 assert( Chan != NULL );
390 cl2chan = My_Cl2Chan;
393 if( cl2chan->channel == Chan ) count++;
394 cl2chan = cl2chan->next;
397 } /* Channel_MemberCount */
401 Channel_CountForUser( CLIENT *Client )
403 /* Count number of channels a user is member of. */
408 assert( Client != NULL );
410 cl2chan = My_Cl2Chan;
413 if( cl2chan->client == Client ) count++;
414 cl2chan = cl2chan->next;
418 } /* Channel_CountForUser */
422 Channel_Name( const CHANNEL *Chan )
424 assert( Chan != NULL );
430 Channel_Modes( CHANNEL *Chan )
432 assert( Chan != NULL );
434 } /* Channel_Modes */
438 Channel_Key( CHANNEL *Chan )
440 assert( Chan != NULL );
446 Channel_MaxUsers( CHANNEL *Chan )
448 assert( Chan != NULL );
449 return Chan->maxusers;
450 } /* Channel_MaxUsers */
454 Channel_First( void )
457 } /* Channel_First */
461 Channel_Next( CHANNEL *Chan )
463 assert( Chan != NULL );
469 Channel_Search( const char *Name )
471 /* Search channel structure */
476 assert( Name != NULL );
478 search_hash = Hash( Name );
482 if( search_hash == c->hash )
485 if( strcasecmp( Name, c->name ) == 0 ) return c;
490 } /* Channel_Search */
494 Channel_FirstMember( CHANNEL *Chan )
496 assert( Chan != NULL );
497 return Get_First_Cl2Chan( NULL, Chan );
498 } /* Channel_FirstMember */
502 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
504 assert( Chan != NULL );
505 assert( Cl2Chan != NULL );
506 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
507 } /* Channel_NextMember */
511 Channel_FirstChannelOf( CLIENT *Client )
513 assert( Client != NULL );
514 return Get_First_Cl2Chan( Client, NULL );
515 } /* Channel_FirstChannelOf */
519 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
521 assert( Client != NULL );
522 assert( Cl2Chan != NULL );
523 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
524 } /* Channel_NextChannelOf */
528 Channel_GetClient( CL2CHAN *Cl2Chan )
530 assert( Cl2Chan != NULL );
531 return Cl2Chan->client;
532 } /* Channel_GetClient */
536 Channel_GetChannel( CL2CHAN *Cl2Chan )
538 assert( Cl2Chan != NULL );
539 return Cl2Chan->channel;
540 } /* Channel_GetChannel */
544 Channel_IsValidName( const char *Name )
546 assert( Name != NULL );
549 if (strlen(Name) <= 1)
552 if (strchr("#&+", Name[0]) == NULL)
554 if (strlen(Name) >= CHANNEL_NAME_LEN)
557 return Name[strcspn(Name, " ,:\007")] == 0;
558 } /* Channel_IsValidName */
562 Channel_ModeAdd( CHANNEL *Chan, char Mode )
565 * If the channel already had this mode, return false.
566 * If the channel mode was newly set return true.
571 assert( Chan != NULL );
573 x[0] = Mode; x[1] = '\0';
574 if( ! strchr( Chan->modes, x[0] ))
576 /* Channel does not have this mode yet, set it */
577 strlcat( Chan->modes, x, sizeof( Chan->modes ));
581 } /* Channel_ModeAdd */
585 Channel_ModeDel( CHANNEL *Chan, char Mode )
588 * if the mode was removed return true.
589 * if the channel did not have the mode, return false.
593 assert( Chan != NULL );
595 p = strchr( Chan->modes, Mode );
596 if( ! p ) return false;
598 /* Channel has mode -> delete */
605 } /* Channel_ModeDel */
609 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
611 /* Set Channel-User-Mode.
612 * if mode was newly set, return true.
613 * if the User already had this channel-mode, return false.
619 assert( Chan != NULL );
620 assert( Client != NULL );
622 cl2chan = Get_Cl2Chan( Chan, Client );
623 assert( cl2chan != NULL );
625 x[0] = Mode; x[1] = '\0';
626 if( ! strchr( cl2chan->modes, x[0] ))
628 /* mode not set, -> set it */
629 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
633 } /* Channel_UserModeAdd */
637 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
639 /* Delete Channel-User-Mode.
640 * If Mode was removed, return true.
641 * If User did not have the Channel-Mode, return false.
647 assert( Chan != NULL );
648 assert( Client != NULL );
650 cl2chan = Get_Cl2Chan( Chan, Client );
651 assert( cl2chan != NULL );
653 p = strchr( cl2chan->modes, Mode );
654 if( ! p ) return false;
656 /* Client has Mode -> delete */
663 } /* Channel_UserModeDel */
667 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
669 /* return Users' Channel-Modes */
673 assert( Chan != NULL );
674 assert( Client != NULL );
676 cl2chan = Get_Cl2Chan( Chan, Client );
677 assert( cl2chan != NULL );
679 return cl2chan->modes;
680 } /* Channel_UserModes */
684 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
686 /* Test if Client is on Channel Chan */
688 assert( Chan != NULL );
689 assert( Client != NULL );
690 return Get_Cl2Chan(Chan, Client) != NULL;
691 } /* Channel_IsMemberOf */
695 Channel_Topic( CHANNEL *Chan )
698 assert( Chan != NULL );
699 ret = array_start(&Chan->topic);
700 return ret ? ret : "";
701 } /* Channel_Topic */
707 Channel_TopicTime(CHANNEL *Chan)
709 assert(Chan != NULL);
710 return (unsigned int) Chan->topic_time;
711 } /* Channel_TopicTime */
715 Channel_TopicWho(CHANNEL *Chan)
717 assert(Chan != NULL);
718 return Chan->topic_who;
719 } /* Channel_TopicWho */
723 Channel_CreationTime(CHANNEL *Chan)
725 assert(Chan != NULL);
726 return (unsigned int) Chan->creation_time;
727 } /* Channel_CreationTime */
733 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
736 assert( Chan != NULL );
737 assert( Topic != NULL );
740 if (len < array_bytes(&Chan->topic))
741 array_free(&Chan->topic);
743 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
744 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
745 Topic, Chan->name, strerror(errno));
747 Chan->topic_time = time(NULL);
748 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
749 strlcpy(Chan->topic_who, Client_ID(Client),
750 sizeof Chan->topic_who);
752 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
753 sizeof Chan->topic_who);
757 } /* Channel_SetTopic */
761 Channel_SetModes( CHANNEL *Chan, const char *Modes )
763 assert( Chan != NULL );
764 assert( Modes != NULL );
766 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
767 } /* Channel_SetModes */
771 Channel_SetKey( CHANNEL *Chan, const char *Key )
773 assert( Chan != NULL );
774 assert( Key != NULL );
776 strlcpy( Chan->key, Key, sizeof( Chan->key ));
777 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
778 } /* Channel_SetKey */
782 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
784 assert( Chan != NULL );
786 Chan->maxusers = Count;
787 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
788 } /* Channel_SetMaxUsers */
792 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
794 bool is_member, has_voice, is_op;
796 is_member = has_voice = is_op = false;
798 /* The server itself always can send messages :-) */
799 if (Client_ThisServer() == From)
802 if (Channel_IsMemberOf(Chan, From)) {
804 if (strchr(Channel_UserModes(Chan, From), 'v'))
806 if (strchr(Channel_UserModes(Chan, From), 'o'))
811 * Is the client allowed to write to channel?
813 * If channel mode n set: non-members cannot send to channel.
814 * If channel mode m set: need voice.
816 if (strchr(Channel_Modes(Chan), 'n') && !is_member)
819 if (is_op || has_voice)
822 if (strchr(Channel_Modes(Chan), 'm'))
825 return !Lists_Check(&Chan->list_bans, From);
830 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
831 bool SendErrors, const char *Text)
833 if (!Can_Send_To_Channel(Chan, From)) {
835 return CONNECTED; /* no error, see RFC 2812 */
836 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
837 Client_ID(From), Channel_Name(Chan));
840 if (Client_Conn(From) > NONE)
841 Conn_UpdateIdle(Client_Conn(From));
843 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
844 "%s %s :%s", Command, Channel_Name(Chan), Text);
849 Channel_Create( const char *Name )
851 /* Create new CHANNEL structure and add it to linked list */
854 assert( Name != NULL );
856 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
859 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
862 memset( c, 0, sizeof( CHANNEL ));
863 strlcpy( c->name, Name, sizeof( c->name ));
864 c->hash = Hash( c->name );
865 c->next = My_Channels;
867 c->creation_time = time(NULL);
870 LogDebug("Created new channel structure for \"%s\".", Name);
872 } /* Channel_Create */
876 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
880 assert( Chan != NULL );
881 assert( Client != NULL );
883 cl2chan = My_Cl2Chan;
886 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
887 cl2chan = cl2chan->next;
894 Add_Client( CHANNEL *Chan, CLIENT *Client )
898 assert( Chan != NULL );
899 assert( Client != NULL );
901 /* Create new CL2CHAN structure */
902 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
905 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
908 cl2chan->channel = Chan;
909 cl2chan->client = Client;
910 strcpy( cl2chan->modes, "" );
913 cl2chan->next = My_Cl2Chan;
914 My_Cl2Chan = cl2chan;
916 LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
923 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
925 CL2CHAN *cl2chan, *last_cl2chan;
928 assert( Chan != NULL );
929 assert( Client != NULL );
930 assert( Origin != NULL );
931 assert( Reason != NULL );
933 /* Do not inform other servers if the channel is local to this server,
934 * regardless of what the caller requested! */
936 InformServer = !Channel_IsLocal(Chan);
939 cl2chan = My_Cl2Chan;
942 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
943 last_cl2chan = cl2chan;
944 cl2chan = cl2chan->next;
946 if( ! cl2chan ) return false;
948 c = cl2chan->channel;
951 /* maintain cl2chan list */
952 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
953 else My_Cl2Chan = cl2chan->next;
959 /* QUIT: other servers have already been notified,
960 * see Client_Destroy(); so only inform other clients
961 * in same channel. */
962 assert( InformServer == false );
963 LogDebug("User \"%s\" left channel \"%s\" (%s).",
964 Client_Mask( Client ), c->name, Reason );
967 /* User was KICKed: inform other servers (public
968 * channels) and all users in the channel */
970 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
971 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
972 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
973 c->name, Client_ID( Client ), Reason );
974 if ((Client_Conn(Client) > NONE) &&
975 (Client_Type(Client) == CLIENT_USER))
977 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
978 c->name, Client_ID( Client ), Reason);
980 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
981 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
984 if (Conf_MorePrivacy)
988 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
990 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
993 if ((Client_Conn(Origin) > NONE) &&
994 (Client_Type(Origin) == CLIENT_USER))
996 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
997 LogDebug("User \"%s\" left channel \"%s\" (%s).",
998 Client_Mask(Client), c->name, Reason);
1002 /* When channel is empty and is not pre-defined, delete */
1003 if( ! strchr( Channel_Modes( Chan ), 'P' ))
1005 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
1009 } /* Remove_Client */
1013 Channel_AddBan(CHANNEL *c, const char *mask )
1015 struct list_head *h = Channel_GetListBans(c);
1016 LogDebug("Adding \"%s\" to \"%s\" %s list", mask, Channel_Name(c), "ban");
1017 return Lists_Add(h, mask, false, NULL);
1022 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
1024 struct list_head *h = Channel_GetListInvites(c);
1025 LogDebug("Adding \"%s\" to \"%s\" %s list", mask, Channel_Name(c), "invite");
1026 return Lists_Add(h, mask, onlyonce, NULL);
1031 ShowChannelList(struct list_head *head, CLIENT *Client, CHANNEL *Channel,
1032 char *msg, char *msg_end)
1034 struct list_elem *e;
1036 assert (Client != NULL);
1037 assert (Channel != NULL);
1039 e = Lists_GetFirst(head);
1041 if (!IRC_WriteStrClient(Client, msg, Client_ID(Client),
1042 Channel_Name(Channel),
1044 return DISCONNECTED;
1045 e = Lists_GetNext(e);
1048 return IRC_WriteStrClient(Client, msg_end, Client_ID(Client),
1049 Channel_Name(Channel));
1054 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1056 struct list_head *h;
1058 assert( Channel != NULL );
1060 h = Channel_GetListBans(Channel);
1061 return ShowChannelList(h, Client, Channel, RPL_BANLIST_MSG,
1062 RPL_ENDOFBANLIST_MSG);
1067 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1069 struct list_head *h;
1071 assert( Channel != NULL );
1073 h = Channel_GetListInvites(Channel);
1074 return ShowChannelList(h, Client, Channel, RPL_INVITELIST_MSG,
1075 RPL_ENDOFINVITELIST_MSG);
1080 * Log a message to the local &SERVER channel, if it exists.
1083 Channel_LogServer(const char *msg)
1088 assert(msg != NULL);
1090 sc = Channel_Search("&SERVER");
1094 c = Client_ThisServer();
1095 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1096 } /* Channel_LogServer */
1100 Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
1102 char *file_name, line[COMMAND_LEN], *nick, *pass;
1105 assert(Chan != NULL);
1106 assert(Client != NULL);
1107 assert(Key != NULL);
1109 if (!strchr(Chan->modes, 'k'))
1113 if (strcmp(Chan->key, Key) == 0)
1116 file_name = array_start(&Chan->keyfile);
1119 fd = fopen(file_name, "r");
1121 Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
1122 file_name, Chan->name, strerror(errno));
1126 while (fgets(line, (int)sizeof(line), fd) != NULL) {
1128 if (! (nick = strchr(line, ':')))
1131 if (!Match(line, Client_User(Client)))
1133 if (! (pass = strchr(nick, ':')))
1136 if (!Match(nick, Client_ID(Client)))
1138 if (strcmp(Key, pass) != 0)
1146 } /* Channel_CheckKey */
1150 * Check wether a client is allowed to administer a channel or not.
1152 * @param Chan The channel to test.
1153 * @param Client The client from which the command has been received.
1154 * @param Origin The originator of the command (or NULL).
1155 * @param OnChannel Set to true if the originator is member of the channel.
1156 * @param AdminOk Set to true if the client is allowed to do
1157 * administrative tasks on this channel.
1158 * @param UseServerMode Set to true if ngIRCd should emulate "server mode",
1159 * that is send commands as if originating from a server
1160 * and not the originator of the command.
1163 Channel_CheckAdminRights(CHANNEL *Chan, CLIENT *Client, CLIENT *Origin,
1164 bool *OnChannel, bool *AdminOk, bool *UseServerMode)
1166 assert (Chan != NULL);
1167 assert (Client != NULL);
1168 assert (OnChannel != NULL);
1169 assert (AdminOk != NULL);
1170 assert (UseServerMode != NULL);
1172 /* Use the client as origin, if no origin has been given (no prefix?) */
1178 *UseServerMode = false;
1180 if (Client_Type(Client) != CLIENT_USER
1181 && Client_Type(Client) != CLIENT_SERVER
1182 && Client_Type(Client) != CLIENT_SERVICE)
1185 /* Allow channel administration if the client is a server or service */
1186 if (Client_Type(Client) != CLIENT_USER) {
1191 *OnChannel = Channel_IsMemberOf(Chan, Origin);
1193 if (*OnChannel && strchr(Channel_UserModes(Chan, Origin), 'o')) {
1194 /* User is a channel operator */
1196 } else if (Conf_OperCanMode) {
1197 /* IRC operators are allowed to administer channels as well */
1198 if (Client_OperByMe(Origin)) {
1200 if (Conf_OperServerMode)
1201 *UseServerMode = true;
1204 } /* Channel_CheckAdminRights */
1208 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1210 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1211 } /* Get_First_Cl2Chan */
1215 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1219 assert( Client != NULL || Channel != NULL );
1224 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1225 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1226 cl2chan = cl2chan->next;
1229 } /* Get_Next_Cl2Chan */
1233 * Remove a channel and free all of its data structures.
1236 Delete_Channel(CHANNEL *Chan)
1238 CHANNEL *chan, *last_chan;
1249 assert(chan != NULL);
1253 /* maintain channel list */
1255 last_chan->next = chan->next;
1257 My_Channels = chan->next;
1259 LogDebug("Freed channel structure for \"%s\".", Chan->name);
1261 } /* Delete_Channel */
1265 Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
1269 assert(Chan != NULL);
1270 assert(KeyFile != NULL);
1272 len = strlen(KeyFile);
1273 if (len < array_bytes(&Chan->keyfile)) {
1274 Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
1275 array_free(&Chan->keyfile);
1281 if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
1283 "Could not set new channel key file \"%s\" for %s: %s",
1284 KeyFile, Chan->name, strerror(errno));
1286 Log(LOG_INFO|LOG_snotice,
1287 "New local channel key file \"%s\" for %s activated.",
1288 KeyFile, Chan->name);