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 Channel_Count( void )
359 unsigned long count = 0;
368 } /* Channel_Count */
372 Channel_MemberCount( CHANNEL *Chan )
375 unsigned long count = 0;
377 assert( Chan != NULL );
379 cl2chan = My_Cl2Chan;
382 if( cl2chan->channel == Chan ) count++;
383 cl2chan = cl2chan->next;
386 } /* Channel_MemberCount */
390 Channel_CountForUser( CLIENT *Client )
392 /* Count number of channels a user is member of. */
397 assert( Client != NULL );
399 cl2chan = My_Cl2Chan;
402 if( cl2chan->client == Client ) count++;
403 cl2chan = cl2chan->next;
407 } /* Channel_CountForUser */
411 Channel_Name( const CHANNEL *Chan )
413 assert( Chan != NULL );
419 Channel_Modes( CHANNEL *Chan )
421 assert( Chan != NULL );
423 } /* Channel_Modes */
427 Channel_Key( CHANNEL *Chan )
429 assert( Chan != NULL );
435 Channel_MaxUsers( CHANNEL *Chan )
437 assert( Chan != NULL );
438 return Chan->maxusers;
439 } /* Channel_MaxUsers */
443 Channel_First( void )
446 } /* Channel_First */
450 Channel_Next( CHANNEL *Chan )
452 assert( Chan != NULL );
458 Channel_Search( const char *Name )
460 /* Search channel structure */
465 assert( Name != NULL );
467 search_hash = Hash( Name );
471 if( search_hash == c->hash )
474 if( strcasecmp( Name, c->name ) == 0 ) return c;
479 } /* Channel_Search */
483 Channel_FirstMember( CHANNEL *Chan )
485 assert( Chan != NULL );
486 return Get_First_Cl2Chan( NULL, Chan );
487 } /* Channel_FirstMember */
491 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
493 assert( Chan != NULL );
494 assert( Cl2Chan != NULL );
495 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
496 } /* Channel_NextMember */
500 Channel_FirstChannelOf( CLIENT *Client )
502 assert( Client != NULL );
503 return Get_First_Cl2Chan( Client, NULL );
504 } /* Channel_FirstChannelOf */
508 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
510 assert( Client != NULL );
511 assert( Cl2Chan != NULL );
512 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
513 } /* Channel_NextChannelOf */
517 Channel_GetClient( CL2CHAN *Cl2Chan )
519 assert( Cl2Chan != NULL );
520 return Cl2Chan->client;
521 } /* Channel_GetClient */
525 Channel_GetChannel( CL2CHAN *Cl2Chan )
527 assert( Cl2Chan != NULL );
528 return Cl2Chan->channel;
529 } /* Channel_GetChannel */
533 Channel_IsValidName( const char *Name )
535 assert( Name != NULL );
538 if (strlen(Name) <= 1)
541 if (strchr("#&+", Name[0]) == NULL)
543 if (strlen(Name) >= CHANNEL_NAME_LEN)
546 return Name[strcspn(Name, " ,:\007")] == 0;
547 } /* Channel_IsValidName */
551 Channel_ModeAdd( CHANNEL *Chan, char Mode )
554 * If the channel already had this mode, return false.
555 * If the channel mode was newly set return true.
560 assert( Chan != NULL );
562 x[0] = Mode; x[1] = '\0';
563 if( ! strchr( Chan->modes, x[0] ))
565 /* Channel does not have this mode yet, set it */
566 strlcat( Chan->modes, x, sizeof( Chan->modes ));
570 } /* Channel_ModeAdd */
574 Channel_ModeDel( CHANNEL *Chan, char Mode )
577 * if the mode was removed return true.
578 * if the channel did not have the mode, return false.
582 assert( Chan != NULL );
584 p = strchr( Chan->modes, Mode );
585 if( ! p ) return false;
587 /* Channel has mode -> delete */
594 } /* Channel_ModeDel */
598 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
600 /* Set Channel-User-Mode.
601 * if mode was newly set, return true.
602 * if the User already had this channel-mode, return false.
608 assert( Chan != NULL );
609 assert( Client != NULL );
611 cl2chan = Get_Cl2Chan( Chan, Client );
612 assert( cl2chan != NULL );
614 x[0] = Mode; x[1] = '\0';
615 if( ! strchr( cl2chan->modes, x[0] ))
617 /* mode not set, -> set it */
618 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
622 } /* Channel_UserModeAdd */
626 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
628 /* Delete Channel-User-Mode.
629 * If Mode was removed, return true.
630 * If User did not have the Channel-Mode, return false.
636 assert( Chan != NULL );
637 assert( Client != NULL );
639 cl2chan = Get_Cl2Chan( Chan, Client );
640 assert( cl2chan != NULL );
642 p = strchr( cl2chan->modes, Mode );
643 if( ! p ) return false;
645 /* Client has Mode -> delete */
652 } /* Channel_UserModeDel */
656 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
658 /* return Users' Channel-Modes */
662 assert( Chan != NULL );
663 assert( Client != NULL );
665 cl2chan = Get_Cl2Chan( Chan, Client );
666 assert( cl2chan != NULL );
668 return cl2chan->modes;
669 } /* Channel_UserModes */
673 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
675 /* Test if Client is on Channel Chan */
677 assert( Chan != NULL );
678 assert( Client != NULL );
679 return Get_Cl2Chan(Chan, Client) != NULL;
680 } /* Channel_IsMemberOf */
684 Channel_Topic( CHANNEL *Chan )
687 assert( Chan != NULL );
688 ret = array_start(&Chan->topic);
689 return ret ? ret : "";
690 } /* Channel_Topic */
696 Channel_TopicTime(CHANNEL *Chan)
698 assert(Chan != NULL);
699 return (unsigned int) Chan->topic_time;
700 } /* Channel_TopicTime */
704 Channel_TopicWho(CHANNEL *Chan)
706 assert(Chan != NULL);
707 return Chan->topic_who;
708 } /* Channel_TopicWho */
712 Channel_CreationTime(CHANNEL *Chan)
714 assert(Chan != NULL);
715 return (unsigned int) Chan->creation_time;
716 } /* Channel_CreationTime */
722 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
725 assert( Chan != NULL );
726 assert( Topic != NULL );
729 if (len < array_bytes(&Chan->topic))
730 array_free(&Chan->topic);
732 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
733 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
734 Topic, Chan->name, strerror(errno));
736 Chan->topic_time = time(NULL);
737 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
738 strlcpy(Chan->topic_who, Client_ID(Client),
739 sizeof Chan->topic_who);
741 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
742 sizeof Chan->topic_who);
746 } /* Channel_SetTopic */
750 Channel_SetModes( CHANNEL *Chan, const char *Modes )
752 assert( Chan != NULL );
753 assert( Modes != NULL );
755 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
756 } /* Channel_SetModes */
760 Channel_SetKey( CHANNEL *Chan, const char *Key )
762 assert( Chan != NULL );
763 assert( Key != NULL );
765 strlcpy( Chan->key, Key, sizeof( Chan->key ));
766 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
767 } /* Channel_SetKey */
771 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
773 assert( Chan != NULL );
775 Chan->maxusers = Count;
776 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
777 } /* Channel_SetMaxUsers */
781 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
783 bool is_member, has_voice, is_op;
785 is_member = has_voice = is_op = false;
787 /* The server itself always can send messages :-) */
788 if (Client_ThisServer() == From)
791 if (Channel_IsMemberOf(Chan, From)) {
793 if (strchr(Channel_UserModes(Chan, From), 'v'))
795 if (strchr(Channel_UserModes(Chan, From), 'o'))
800 * Is the client allowed to write to channel?
802 * If channel mode n set: non-members cannot send to channel.
803 * If channel mode m set: need voice.
805 if (strchr(Channel_Modes(Chan), 'n') && !is_member)
808 if (is_op || has_voice)
811 if (strchr(Channel_Modes(Chan), 'm'))
814 return !Lists_Check(&Chan->list_bans, From);
819 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
820 bool SendErrors, const char *Text)
822 if (!Can_Send_To_Channel(Chan, From)) {
824 return CONNECTED; /* no error, see RFC 2812 */
825 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
826 Client_ID(From), Channel_Name(Chan));
829 if (Client_Conn(From) > NONE)
830 Conn_UpdateIdle(Client_Conn(From));
832 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
833 "%s %s :%s", Command, Channel_Name(Chan), Text);
838 Channel_Create( const char *Name )
840 /* Create new CHANNEL structure and add it to linked list */
843 assert( Name != NULL );
845 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
848 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
851 memset( c, 0, sizeof( CHANNEL ));
852 strlcpy( c->name, Name, sizeof( c->name ));
853 c->hash = Hash( c->name );
854 c->next = My_Channels;
856 c->creation_time = time(NULL);
859 LogDebug("Created new channel structure for \"%s\".", Name);
861 } /* Channel_Create */
865 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
869 assert( Chan != NULL );
870 assert( Client != NULL );
872 cl2chan = My_Cl2Chan;
875 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
876 cl2chan = cl2chan->next;
883 Add_Client( CHANNEL *Chan, CLIENT *Client )
887 assert( Chan != NULL );
888 assert( Client != NULL );
890 /* Create new CL2CHAN structure */
891 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
894 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
897 cl2chan->channel = Chan;
898 cl2chan->client = Client;
899 strcpy( cl2chan->modes, "" );
902 cl2chan->next = My_Cl2Chan;
903 My_Cl2Chan = cl2chan;
905 LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
912 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
914 CL2CHAN *cl2chan, *last_cl2chan;
917 assert( Chan != NULL );
918 assert( Client != NULL );
919 assert( Origin != NULL );
920 assert( Reason != NULL );
922 /* Do not inform other servers if the channel is local to this server,
923 * regardless of what the caller requested! */
925 InformServer = !Channel_IsLocal(Chan);
928 cl2chan = My_Cl2Chan;
931 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
932 last_cl2chan = cl2chan;
933 cl2chan = cl2chan->next;
935 if( ! cl2chan ) return false;
937 c = cl2chan->channel;
940 /* maintain cl2chan list */
941 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
942 else My_Cl2Chan = cl2chan->next;
948 /* QUIT: other servers have already been notified,
949 * see Client_Destroy(); so only inform other clients
950 * in same channel. */
951 assert( InformServer == false );
952 LogDebug("User \"%s\" left channel \"%s\" (%s).",
953 Client_Mask( Client ), c->name, Reason );
956 /* User was KICKed: inform other servers (public
957 * channels) and all users in the channel */
959 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
960 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
961 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
962 c->name, Client_ID( Client ), Reason );
963 if ((Client_Conn(Client) > NONE) &&
964 (Client_Type(Client) == CLIENT_USER))
966 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
967 c->name, Client_ID( Client ), Reason);
969 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
970 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
973 if (Conf_MorePrivacy)
977 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
979 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
982 if ((Client_Conn(Origin) > NONE) &&
983 (Client_Type(Origin) == CLIENT_USER))
985 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
986 LogDebug("User \"%s\" left channel \"%s\" (%s).",
987 Client_Mask(Client), c->name, Reason);
991 /* When channel is empty and is not pre-defined, delete */
992 if( ! strchr( Channel_Modes( Chan ), 'P' ))
994 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
998 } /* Remove_Client */
1002 Channel_AddBan(CHANNEL *c, const char *mask )
1004 struct list_head *h = Channel_GetListBans(c);
1005 LogDebug("Adding \"%s\" to \"%s\" %s list", mask, Channel_Name(c), "ban");
1006 return Lists_Add(h, mask, false, NULL);
1011 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
1013 struct list_head *h = Channel_GetListInvites(c);
1014 LogDebug("Adding \"%s\" to \"%s\" %s list", mask, Channel_Name(c), "invite");
1015 return Lists_Add(h, mask, onlyonce, NULL);
1020 ShowInvitesBans(struct list_head *head, CLIENT *Client, CHANNEL *Channel, bool invite)
1022 struct list_elem *e;
1023 char *msg = invite ? RPL_INVITELIST_MSG : RPL_BANLIST_MSG;
1026 assert( Client != NULL );
1027 assert( Channel != NULL );
1029 e = Lists_GetFirst(head);
1031 if( ! IRC_WriteStrClient( Client, msg, Client_ID( Client ),
1032 Channel_Name( Channel ), Lists_GetMask(e) )) return DISCONNECTED;
1033 e = Lists_GetNext(e);
1036 msg_end = invite ? RPL_ENDOFINVITELIST_MSG : RPL_ENDOFBANLIST_MSG;
1037 return IRC_WriteStrClient( Client, msg_end, Client_ID( Client ), Channel_Name( Channel ));
1042 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1044 struct list_head *h;
1046 assert( Channel != NULL );
1048 h = Channel_GetListBans(Channel);
1049 return ShowInvitesBans(h, Client, Channel, false);
1054 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1056 struct list_head *h;
1058 assert( Channel != NULL );
1060 h = Channel_GetListInvites(Channel);
1061 return ShowInvitesBans(h, Client, Channel, true);
1066 * Log a message to the local &SERVER channel, if it exists.
1069 Channel_LogServer(const char *msg)
1074 assert(msg != NULL);
1076 sc = Channel_Search("&SERVER");
1080 c = Client_ThisServer();
1081 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1082 } /* Channel_LogServer */
1086 Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
1088 char *file_name, line[COMMAND_LEN], *nick, *pass;
1091 assert(Chan != NULL);
1092 assert(Client != NULL);
1093 assert(Key != NULL);
1095 if (!strchr(Chan->modes, 'k'))
1099 if (strcmp(Chan->key, Key) == 0)
1102 file_name = array_start(&Chan->keyfile);
1105 fd = fopen(file_name, "r");
1107 Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
1108 file_name, Chan->name, strerror(errno));
1112 while (fgets(line, (int)sizeof(line), fd) != NULL) {
1114 if (! (nick = strchr(line, ':')))
1117 if (!Match(line, Client_User(Client)))
1119 if (! (pass = strchr(nick, ':')))
1122 if (!Match(nick, Client_ID(Client)))
1124 if (strcmp(Key, pass) != 0)
1132 } /* Channel_CheckKey */
1136 * Check wether a client is allowed to administer a channel or not.
1138 * @param Chan The channel to test.
1139 * @param Client The client from which the command has been received.
1140 * @param Origin The originator of the command (or NULL).
1141 * @param OnChannel Set to true if the originator is member of the channel.
1142 * @param AdminOk Set to true if the client is allowed to do
1143 * administrative tasks on this channel.
1144 * @param UseServerMode Set to true if ngIRCd should emulate "server mode",
1145 * that is send commands as if originating from a server
1146 * and not the originator of the command.
1149 Channel_CheckAdminRights(CHANNEL *Chan, CLIENT *Client, CLIENT *Origin,
1150 bool *OnChannel, bool *AdminOk, bool *UseServerMode)
1152 assert (Chan != NULL);
1153 assert (Client != NULL);
1154 assert (OnChannel != NULL);
1155 assert (AdminOk != NULL);
1156 assert (UseServerMode != NULL);
1158 /* Use the client as origin, if no origin has been given (no prefix?) */
1164 *UseServerMode = false;
1166 if (Client_Type(Client) != CLIENT_USER
1167 && Client_Type(Client) != CLIENT_SERVER
1168 && Client_Type(Client) != CLIENT_SERVICE)
1171 /* Allow channel administration if the client is a server or service */
1172 if (Client_Type(Client) != CLIENT_USER) {
1177 *OnChannel = Channel_IsMemberOf(Chan, Origin);
1179 if (*OnChannel && strchr(Channel_UserModes(Chan, Origin), 'o')) {
1180 /* User is a channel operator */
1182 } else if (Conf_OperCanMode) {
1183 /* IRC operators are allowed to administer channels as well */
1184 if (Client_OperByMe(Origin)) {
1186 if (Conf_OperServerMode)
1187 *UseServerMode = true;
1190 } /* Channel_CheckAdminRights */
1194 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1196 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1197 } /* Get_First_Cl2Chan */
1201 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1205 assert( Client != NULL || Channel != NULL );
1210 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1211 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1212 cl2chan = cl2chan->next;
1215 } /* Get_Next_Cl2Chan */
1219 * Remove a channel and free all of its data structures.
1222 Delete_Channel(CHANNEL *Chan)
1224 CHANNEL *chan, *last_chan;
1235 assert(chan != NULL);
1239 /* maintain channel list */
1241 last_chan->next = chan->next;
1243 My_Channels = chan->next;
1245 LogDebug("Freed channel structure for \"%s\".", Chan->name);
1247 } /* Delete_Channel */
1251 Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
1255 assert(Chan != NULL);
1256 assert(KeyFile != NULL);
1258 len = strlen(KeyFile);
1259 if (len < array_bytes(&Chan->keyfile)) {
1260 Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
1261 array_free(&Chan->keyfile);
1267 if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
1269 "Could not set new channel key file \"%s\" for %s: %s",
1270 KeyFile, Chan->name, strerror(errno));
1272 Log(LOG_INFO|LOG_snotice,
1273 "New local channel key file \"%s\" for %s activated.",
1274 KeyFile, Chan->name);