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 /* Part client from channel */
267 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
275 * Kick user from Channel
278 Channel_Kick(CLIENT *Peer, CLIENT *Target, CLIENT *Origin, const char *Name,
283 assert(Peer != NULL);
284 assert(Target != NULL);
285 assert(Origin != NULL);
286 assert(Name != NULL);
287 assert(Reason != NULL);
289 /* Check that channel exists */
290 chan = Channel_Search( Name );
293 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
297 if (Client_Type(Peer) != CLIENT_SERVER &&
298 Client_Type(Origin) != CLIENT_SERVICE) {
299 /* Check that user is on the specified channel */
300 if (!Channel_IsMemberOf(chan, Origin)) {
301 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG,
302 Client_ID(Origin), Name);
306 /* Check if user has operator status */
307 if (!strchr(Channel_UserModes(chan, Origin), 'o')) {
308 IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG,
309 Client_ID(Origin), Name);
314 /* Check that the client to be kicked is on the specified channel */
315 if (!Channel_IsMemberOf(chan, Target)) {
316 IRC_WriteStrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
317 Client_ID(Origin), Client_ID(Target), Name );
321 /* Kick Client from channel */
322 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
327 Channel_Quit( CLIENT *Client, const char *Reason )
331 assert( Client != NULL );
332 assert( Reason != NULL );
334 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
340 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
347 Channel_Count( void )
350 unsigned long count = 0;
359 } /* Channel_Count */
363 Channel_MemberCount( CHANNEL *Chan )
366 unsigned long count = 0;
368 assert( Chan != NULL );
370 cl2chan = My_Cl2Chan;
373 if( cl2chan->channel == Chan ) count++;
374 cl2chan = cl2chan->next;
377 } /* Channel_MemberCount */
381 Channel_CountForUser( CLIENT *Client )
383 /* Count number of channels a user is member of. */
388 assert( Client != NULL );
390 cl2chan = My_Cl2Chan;
393 if( cl2chan->client == Client ) count++;
394 cl2chan = cl2chan->next;
398 } /* Channel_CountForUser */
402 Channel_Name( const CHANNEL *Chan )
404 assert( Chan != NULL );
410 Channel_Modes( CHANNEL *Chan )
412 assert( Chan != NULL );
414 } /* Channel_Modes */
418 Channel_Key( CHANNEL *Chan )
420 assert( Chan != NULL );
426 Channel_MaxUsers( CHANNEL *Chan )
428 assert( Chan != NULL );
429 return Chan->maxusers;
430 } /* Channel_MaxUsers */
434 Channel_First( void )
437 } /* Channel_First */
441 Channel_Next( CHANNEL *Chan )
443 assert( Chan != NULL );
449 Channel_Search( const char *Name )
451 /* Search channel structure */
456 assert( Name != NULL );
458 search_hash = Hash( Name );
462 if( search_hash == c->hash )
465 if( strcasecmp( Name, c->name ) == 0 ) return c;
470 } /* Channel_Search */
474 Channel_FirstMember( CHANNEL *Chan )
476 assert( Chan != NULL );
477 return Get_First_Cl2Chan( NULL, Chan );
478 } /* Channel_FirstMember */
482 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
484 assert( Chan != NULL );
485 assert( Cl2Chan != NULL );
486 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
487 } /* Channel_NextMember */
491 Channel_FirstChannelOf( CLIENT *Client )
493 assert( Client != NULL );
494 return Get_First_Cl2Chan( Client, NULL );
495 } /* Channel_FirstChannelOf */
499 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
501 assert( Client != NULL );
502 assert( Cl2Chan != NULL );
503 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
504 } /* Channel_NextChannelOf */
508 Channel_GetClient( CL2CHAN *Cl2Chan )
510 assert( Cl2Chan != NULL );
511 return Cl2Chan->client;
512 } /* Channel_GetClient */
516 Channel_GetChannel( CL2CHAN *Cl2Chan )
518 assert( Cl2Chan != NULL );
519 return Cl2Chan->channel;
520 } /* Channel_GetChannel */
524 Channel_IsValidName( const char *Name )
526 assert( Name != NULL );
529 if (strlen(Name) <= 1)
532 if (strchr("#&+", Name[0]) == NULL)
534 if (strlen(Name) >= CHANNEL_NAME_LEN)
537 return Name[strcspn(Name, " ,:\007")] == 0;
538 } /* Channel_IsValidName */
542 Channel_ModeAdd( CHANNEL *Chan, char Mode )
545 * If the channel already had this mode, return false.
546 * If the channel mode was newly set return true.
551 assert( Chan != NULL );
553 x[0] = Mode; x[1] = '\0';
554 if( ! strchr( Chan->modes, x[0] ))
556 /* Channel does not have this mode yet, set it */
557 strlcat( Chan->modes, x, sizeof( Chan->modes ));
561 } /* Channel_ModeAdd */
565 Channel_ModeDel( CHANNEL *Chan, char Mode )
568 * if the mode was removed return true.
569 * if the channel did not have the mode, return false.
573 assert( Chan != NULL );
575 p = strchr( Chan->modes, Mode );
576 if( ! p ) return false;
578 /* Channel has mode -> delete */
585 } /* Channel_ModeDel */
589 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
591 /* Set Channel-User-Mode.
592 * if mode was newly set, return true.
593 * if the User already had this channel-mode, return false.
599 assert( Chan != NULL );
600 assert( Client != NULL );
602 cl2chan = Get_Cl2Chan( Chan, Client );
603 assert( cl2chan != NULL );
605 x[0] = Mode; x[1] = '\0';
606 if( ! strchr( cl2chan->modes, x[0] ))
608 /* mode not set, -> set it */
609 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
613 } /* Channel_UserModeAdd */
617 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
619 /* Delete Channel-User-Mode.
620 * If Mode was removed, return true.
621 * If User did not have the Channel-Mode, return false.
627 assert( Chan != NULL );
628 assert( Client != NULL );
630 cl2chan = Get_Cl2Chan( Chan, Client );
631 assert( cl2chan != NULL );
633 p = strchr( cl2chan->modes, Mode );
634 if( ! p ) return false;
636 /* Client has Mode -> delete */
643 } /* Channel_UserModeDel */
647 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
649 /* return Users' Channel-Modes */
653 assert( Chan != NULL );
654 assert( Client != NULL );
656 cl2chan = Get_Cl2Chan( Chan, Client );
657 assert( cl2chan != NULL );
659 return cl2chan->modes;
660 } /* Channel_UserModes */
664 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
666 /* Test if Client is on Channel Chan */
668 assert( Chan != NULL );
669 assert( Client != NULL );
670 return Get_Cl2Chan(Chan, Client) != NULL;
671 } /* Channel_IsMemberOf */
675 Channel_Topic( CHANNEL *Chan )
678 assert( Chan != NULL );
679 ret = array_start(&Chan->topic);
680 return ret ? ret : "";
681 } /* Channel_Topic */
687 Channel_TopicTime(CHANNEL *Chan)
689 assert(Chan != NULL);
690 return (unsigned int) Chan->topic_time;
691 } /* Channel_TopicTime */
695 Channel_TopicWho(CHANNEL *Chan)
697 assert(Chan != NULL);
698 return Chan->topic_who;
699 } /* Channel_TopicWho */
703 Channel_CreationTime(CHANNEL *Chan)
705 assert(Chan != NULL);
706 return (unsigned int) Chan->creation_time;
707 } /* Channel_CreationTime */
713 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
716 assert( Chan != NULL );
717 assert( Topic != NULL );
720 if (len < array_bytes(&Chan->topic))
721 array_free(&Chan->topic);
723 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
724 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
725 Topic, Chan->name, strerror(errno));
727 Chan->topic_time = time(NULL);
728 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
729 strlcpy(Chan->topic_who, Client_ID(Client),
730 sizeof Chan->topic_who);
732 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
733 sizeof Chan->topic_who);
737 } /* Channel_SetTopic */
741 Channel_SetModes( CHANNEL *Chan, const char *Modes )
743 assert( Chan != NULL );
744 assert( Modes != NULL );
746 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
747 } /* Channel_SetModes */
751 Channel_SetKey( CHANNEL *Chan, const char *Key )
753 assert( Chan != NULL );
754 assert( Key != NULL );
756 strlcpy( Chan->key, Key, sizeof( Chan->key ));
757 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
758 } /* Channel_SetKey */
762 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
764 assert( Chan != NULL );
766 Chan->maxusers = Count;
767 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
768 } /* Channel_SetMaxUsers */
772 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
774 bool is_member, has_voice, is_op;
776 is_member = has_voice = is_op = false;
778 /* The server itself always can send messages :-) */
779 if (Client_ThisServer() == From)
782 if (Channel_IsMemberOf(Chan, From)) {
784 if (strchr(Channel_UserModes(Chan, From), 'v'))
786 if (strchr(Channel_UserModes(Chan, From), 'o'))
791 * Is the client allowed to write to channel?
793 * If channel mode n set: non-members cannot send to channel.
794 * If channel mode m set: need voice.
796 if (strchr(Channel_Modes(Chan), 'n') && !is_member)
799 if (is_op || has_voice)
802 if (strchr(Channel_Modes(Chan), 'm'))
805 return !Lists_Check(&Chan->list_bans, From);
810 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
811 bool SendErrors, const char *Text)
813 if (!Can_Send_To_Channel(Chan, From)) {
815 return CONNECTED; /* no error, see RFC 2812 */
816 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
817 Client_ID(From), Channel_Name(Chan));
820 if (Client_Conn(From) > NONE)
821 Conn_UpdateIdle(Client_Conn(From));
823 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
824 "%s %s :%s", Command, Channel_Name(Chan), Text);
829 Channel_Create( const char *Name )
831 /* Create new CHANNEL structure and add it to linked list */
834 assert( Name != NULL );
836 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
839 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
842 memset( c, 0, sizeof( CHANNEL ));
843 strlcpy( c->name, Name, sizeof( c->name ));
844 c->hash = Hash( c->name );
845 c->next = My_Channels;
847 c->creation_time = time(NULL);
850 LogDebug("Created new channel structure for \"%s\".", Name);
852 } /* Channel_Create */
856 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
860 assert( Chan != NULL );
861 assert( Client != NULL );
863 cl2chan = My_Cl2Chan;
866 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
867 cl2chan = cl2chan->next;
874 Add_Client( CHANNEL *Chan, CLIENT *Client )
878 assert( Chan != NULL );
879 assert( Client != NULL );
881 /* Create new CL2CHAN structure */
882 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
885 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
888 cl2chan->channel = Chan;
889 cl2chan->client = Client;
890 strcpy( cl2chan->modes, "" );
893 cl2chan->next = My_Cl2Chan;
894 My_Cl2Chan = cl2chan;
896 LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
903 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
905 CL2CHAN *cl2chan, *last_cl2chan;
908 assert( Chan != NULL );
909 assert( Client != NULL );
910 assert( Origin != NULL );
911 assert( Reason != NULL );
913 /* Do not inform other servers if the channel is local to this server,
914 * regardless of what the caller requested! */
916 InformServer = !Channel_IsLocal(Chan);
919 cl2chan = My_Cl2Chan;
922 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
923 last_cl2chan = cl2chan;
924 cl2chan = cl2chan->next;
926 if( ! cl2chan ) return false;
928 c = cl2chan->channel;
931 /* maintain cl2chan list */
932 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
933 else My_Cl2Chan = cl2chan->next;
939 /* QUIT: other servers have already been notified,
940 * see Client_Destroy(); so only inform other clients
941 * in same channel. */
942 assert( InformServer == false );
943 LogDebug("User \"%s\" left channel \"%s\" (%s).",
944 Client_Mask( Client ), c->name, Reason );
947 /* User was KICKed: inform other servers (public
948 * channels) and all users in the channel */
950 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
951 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
952 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
953 c->name, Client_ID( Client ), Reason );
954 if ((Client_Conn(Client) > NONE) &&
955 (Client_Type(Client) == CLIENT_USER))
957 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
958 c->name, Client_ID( Client ), Reason);
960 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
961 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
965 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
967 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
970 if ((Client_Conn(Origin) > NONE) &&
971 (Client_Type(Origin) == CLIENT_USER))
973 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
974 LogDebug("User \"%s\" left channel \"%s\" (%s).",
975 Client_Mask(Client), c->name, Reason);
979 /* When channel is empty and is not pre-defined, delete */
980 if( ! strchr( Channel_Modes( Chan ), 'P' ))
982 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
986 } /* Remove_Client */
990 Channel_AddBan(CHANNEL *c, const char *mask )
992 struct list_head *h = Channel_GetListBans(c);
993 LogDebug("Adding \"%s\" to \"%s\" %s list", mask, Channel_Name(c), "ban");
994 return Lists_Add(h, mask, false);
999 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
1001 struct list_head *h = Channel_GetListInvites(c);
1002 LogDebug("Adding \"%s\" to \"%s\" %s list", mask, Channel_Name(c), "invite");
1003 return Lists_Add(h, mask, onlyonce);
1008 ShowInvitesBans(struct list_head *head, CLIENT *Client, CHANNEL *Channel, bool invite)
1010 struct list_elem *e;
1011 char *msg = invite ? RPL_INVITELIST_MSG : RPL_BANLIST_MSG;
1014 assert( Client != NULL );
1015 assert( Channel != NULL );
1017 e = Lists_GetFirst(head);
1019 if( ! IRC_WriteStrClient( Client, msg, Client_ID( Client ),
1020 Channel_Name( Channel ), Lists_GetMask(e) )) return DISCONNECTED;
1021 e = Lists_GetNext(e);
1024 msg_end = invite ? RPL_ENDOFINVITELIST_MSG : RPL_ENDOFBANLIST_MSG;
1025 return IRC_WriteStrClient( Client, msg_end, Client_ID( Client ), Channel_Name( Channel ));
1030 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1032 struct list_head *h;
1034 assert( Channel != NULL );
1036 h = Channel_GetListBans(Channel);
1037 return ShowInvitesBans(h, Client, Channel, false);
1042 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1044 struct list_head *h;
1046 assert( Channel != NULL );
1048 h = Channel_GetListInvites(Channel);
1049 return ShowInvitesBans(h, Client, Channel, true);
1054 * Log a message to the local &SERVER channel, if it exists.
1057 Channel_LogServer(const char *msg)
1062 assert(msg != NULL);
1064 sc = Channel_Search("&SERVER");
1068 c = Client_ThisServer();
1069 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1070 } /* Channel_LogServer */
1074 Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
1076 char *file_name, line[COMMAND_LEN], *nick, *pass;
1079 assert(Chan != NULL);
1080 assert(Client != NULL);
1081 assert(Key != NULL);
1083 if (!strchr(Chan->modes, 'k'))
1087 if (strcmp(Chan->key, Key) == 0)
1090 file_name = array_start(&Chan->keyfile);
1093 fd = fopen(file_name, "r");
1095 Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
1096 file_name, Chan->name, strerror(errno));
1100 while (fgets(line, (int)sizeof(line), fd) != NULL) {
1102 if (! (nick = strchr(line, ':')))
1105 if (!Match(line, Client_User(Client)))
1107 if (! (pass = strchr(nick, ':')))
1110 if (!Match(nick, Client_ID(Client)))
1112 if (strcmp(Key, pass) != 0)
1120 } /* Channel_CheckKey */
1124 * Check wether a client is allowed to administer a channel or not.
1126 * @param Chan The channel to test.
1127 * @param Client The client from which the command has been received.
1128 * @param Origin The originator of the command (or NULL).
1129 * @param OnChannel Set to true if the originator is member of the channel.
1130 * @param AdminOk Set to true if the client is allowed to do
1131 * administrative tasks on this channel.
1132 * @param UseServerMode Set to true if ngIRCd should emulate "server mode",
1133 * that is send commands as if originating from a server
1134 * and not the originator of the command.
1137 Channel_CheckAdminRights(CHANNEL *Chan, CLIENT *Client, CLIENT *Origin,
1138 bool *OnChannel, bool *AdminOk, bool *UseServerMode)
1140 assert (Chan != NULL);
1141 assert (Client != NULL);
1142 assert (OnChannel != NULL);
1143 assert (AdminOk != NULL);
1144 assert (UseServerMode != NULL);
1146 /* Use the client as origin, if no origin has been given (no prefix?) */
1152 *UseServerMode = false;
1154 if (Client_Type(Client) != CLIENT_USER
1155 && Client_Type(Client) != CLIENT_SERVER
1156 && Client_Type(Client) != CLIENT_SERVICE)
1159 /* Allow channel administration if the client is a server or service */
1160 if (Client_Type(Client) != CLIENT_USER) {
1165 *OnChannel = Channel_IsMemberOf(Chan, Origin);
1167 if (*OnChannel && strchr(Channel_UserModes(Chan, Origin), 'o')) {
1168 /* User is a channel operator */
1170 } else if (Conf_OperCanMode) {
1171 /* IRC operators are allowed to administer channels as well */
1172 if (Client_OperByMe(Origin)) {
1174 if (Conf_OperServerMode)
1175 *UseServerMode = true;
1178 } /* Channel_CheckAdminRights */
1182 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1184 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1185 } /* Get_First_Cl2Chan */
1189 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1193 assert( Client != NULL || Channel != NULL );
1198 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1199 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1200 cl2chan = cl2chan->next;
1203 } /* Get_Next_Cl2Chan */
1207 * Remove a channel and free all of its data structures.
1210 Delete_Channel(CHANNEL *Chan)
1212 CHANNEL *chan, *last_chan;
1223 assert(chan != NULL);
1227 /* maintain channel list */
1229 last_chan->next = chan->next;
1231 My_Channels = chan->next;
1233 LogDebug("Freed channel structure for \"%s\".", Chan->name);
1235 } /* Delete_Channel */
1239 Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
1243 assert(Chan != NULL);
1244 assert(KeyFile != NULL);
1246 len = strlen(KeyFile);
1247 if (len < array_bytes(&Chan->keyfile)) {
1248 Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
1249 array_free(&Chan->keyfile);
1255 if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
1257 "Could not set new channel key file \"%s\" for %s: %s",
1258 KeyFile, Chan->name, strerror(errno));
1260 Log(LOG_INFO|LOG_snotice,
1261 "New local channel key file \"%s\" for %s activated.",
1262 KeyFile, Chan->name);