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.
32 #include "conn-func.h"
38 #include "irc-write.h"
54 static CHANNEL *My_Channels;
55 static CL2CHAN *My_Cl2Chan;
58 static CL2CHAN *Get_Cl2Chan PARAMS(( CHANNEL *Chan, CLIENT *Client ));
59 static CL2CHAN *Add_Client PARAMS(( CHANNEL *Chan, CLIENT *Client ));
60 static bool Remove_Client PARAMS(( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer ));
61 static CL2CHAN *Get_First_Cl2Chan PARAMS(( CLIENT *Client, CHANNEL *Chan ));
62 static CL2CHAN *Get_Next_Cl2Chan PARAMS(( CL2CHAN *Start, CLIENT *Client, CHANNEL *Chan ));
63 static void Delete_Channel PARAMS(( CHANNEL *Chan ));
64 static void Free_Channel PARAMS(( CHANNEL *Chan ));
65 static void Set_KeyFile PARAMS((CHANNEL *Chan, const char *KeyFile));
76 sc = Channel_Create("&SERVER");
78 Channel_SetModes(sc, "mnPt");
79 Channel_SetTopic(sc, Client_ThisServer(), "Server Messages");
84 GLOBAL struct list_head *
85 Channel_GetListBans(CHANNEL *c)
92 GLOBAL struct list_head *
93 Channel_GetListInvites(CHANNEL *c)
96 return &c->list_invites;
101 Channel_InitPredefined( void )
103 /* Generate predefined persistent channels */
106 const struct Conf_Channel *conf_chan;
108 size_t i, channel_count = array_length(&Conf_Channels, sizeof(*conf_chan));
110 conf_chan = array_start(&Conf_Channels);
112 assert(channel_count == 0 || conf_chan != NULL);
114 for (i = 0; i < channel_count; i++, conf_chan++) {
115 if (!conf_chan->name[0] || !Channel_IsValidName(conf_chan->name)) {
116 Log(LOG_ERR, "Can't create pre-defined channel: invalid name: \"%s\"",
121 new_chan = Channel_Search(conf_chan->name);
124 "Can't create pre-defined channel \"%s\": name already in use.",
126 Set_KeyFile(new_chan, conf_chan->keyfile);
130 new_chan = Channel_Create(conf_chan->name);
132 Log(LOG_ERR, "Can't create pre-defined channel \"%s\"",
136 Log(LOG_INFO, "Created pre-defined channel \"%s\"",
139 Channel_ModeAdd(new_chan, 'P');
141 if (conf_chan->topic[0])
142 Channel_SetTopic(new_chan, NULL, conf_chan->topic);
144 c = conf_chan->modes;
146 Channel_ModeAdd(new_chan, *c++);
148 Channel_SetKey(new_chan, conf_chan->key);
149 Channel_SetMaxUsers(new_chan, conf_chan->maxusers);
150 Set_KeyFile(new_chan, conf_chan->keyfile);
153 array_free(&Conf_Channels);
154 } /* Channel_InitPredefined */
158 Free_Channel(CHANNEL *chan)
160 array_free(&chan->topic);
161 array_free(&chan->keyfile);
162 Lists_Free(&chan->list_bans);
163 Lists_Free(&chan->list_invites);
173 CL2CHAN *cl2chan, *cl2chan_next;
175 /* free struct Channel */
183 /* Free Channel allocation table */
184 cl2chan = My_Cl2Chan;
186 cl2chan_next = cl2chan->next;
188 cl2chan = cl2chan_next;
195 * This function lets a client join a channel. First, the function
196 * checks that the specified channel name is valid and that the client
197 * isn't already a member. If the specified channel doesn't exist,
198 * a new channel is created. Client is added to channel by function
202 Channel_Join( CLIENT *Client, const char *Name )
206 assert(Client != NULL);
207 assert(Name != NULL);
209 /* Check that the channel name is valid */
210 if (! Channel_IsValidName(Name)) {
211 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
212 Client_ID(Client), Name);
216 chan = Channel_Search(Name);
218 /* Check if the client is already in the channel */
219 if (Get_Cl2Chan(chan, Client))
222 /* If the specified channel does not exist, the channel
224 chan = Channel_Create(Name);
229 /* Add user to Channel */
230 if (! Add_Client(chan, Client))
238 * Part client from channel.
239 * This function lets a client part from a channel. First, the function checks
240 * if the channel exists and the client is a member of it and sends out
241 * appropriate error messages if not. The real work is done by the function
245 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
249 assert(Client != NULL);
250 assert(Name != NULL);
251 assert(Reason != NULL);
253 /* Check that specified channel exists */
254 chan = Channel_Search(Name);
256 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
257 Client_ID(Client), Name);
261 /* Check that the client is in the channel */
262 if (!Get_Cl2Chan(chan, Client)) {
263 IRC_WriteStrClient(Client, ERR_NOTONCHANNEL_MSG,
264 Client_ID(Client), Name);
268 /* Part client from channel */
269 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
277 * Kick user from Channel
280 Channel_Kick(CLIENT *Peer, CLIENT *Target, CLIENT *Origin, const char *Name,
285 assert(Peer != NULL);
286 assert(Target != NULL);
287 assert(Origin != NULL);
288 assert(Name != NULL);
289 assert(Reason != NULL);
291 /* Check that channel exists */
292 chan = Channel_Search( Name );
295 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
299 if (Client_Type(Peer) != CLIENT_SERVER &&
300 Client_Type(Origin) != CLIENT_SERVICE) {
301 /* Check that user is on the specified channel */
302 if (!Channel_IsMemberOf(chan, Origin)) {
303 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG,
304 Client_ID(Origin), Name);
308 /* Check if user has operator status */
309 if (!strchr(Channel_UserModes(chan, Origin), 'o')) {
310 IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG,
311 Client_ID(Origin), Name);
316 /* Check that the client to be kicked is on the specified channel */
317 if (!Channel_IsMemberOf(chan, Target)) {
318 IRC_WriteStrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
319 Client_ID(Origin), Client_ID(Target), Name );
323 /* Kick Client from channel */
324 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
329 Channel_Quit( CLIENT *Client, const char *Reason )
333 assert( Client != NULL );
334 assert( Reason != NULL );
336 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
342 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
349 Channel_Count( void )
352 unsigned long count = 0;
361 } /* Channel_Count */
365 Channel_MemberCount( CHANNEL *Chan )
368 unsigned long count = 0;
370 assert( Chan != NULL );
372 cl2chan = My_Cl2Chan;
375 if( cl2chan->channel == Chan ) count++;
376 cl2chan = cl2chan->next;
379 } /* Channel_MemberCount */
383 Channel_CountForUser( CLIENT *Client )
385 /* Count number of channels a user is member of. */
390 assert( Client != NULL );
392 cl2chan = My_Cl2Chan;
395 if( cl2chan->client == Client ) count++;
396 cl2chan = cl2chan->next;
400 } /* Channel_CountForUser */
404 Channel_Name( const CHANNEL *Chan )
406 assert( Chan != NULL );
412 Channel_Modes( CHANNEL *Chan )
414 assert( Chan != NULL );
416 } /* Channel_Modes */
420 Channel_Key( CHANNEL *Chan )
422 assert( Chan != NULL );
428 Channel_MaxUsers( CHANNEL *Chan )
430 assert( Chan != NULL );
431 return Chan->maxusers;
432 } /* Channel_MaxUsers */
436 Channel_First( void )
439 } /* Channel_First */
443 Channel_Next( CHANNEL *Chan )
445 assert( Chan != NULL );
451 Channel_Search( const char *Name )
453 /* Search channel structure */
458 assert( Name != NULL );
460 search_hash = Hash( Name );
464 if( search_hash == c->hash )
467 if( strcasecmp( Name, c->name ) == 0 ) return c;
472 } /* Channel_Search */
476 Channel_FirstMember( CHANNEL *Chan )
478 assert( Chan != NULL );
479 return Get_First_Cl2Chan( NULL, Chan );
480 } /* Channel_FirstMember */
484 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
486 assert( Chan != NULL );
487 assert( Cl2Chan != NULL );
488 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
489 } /* Channel_NextMember */
493 Channel_FirstChannelOf( CLIENT *Client )
495 assert( Client != NULL );
496 return Get_First_Cl2Chan( Client, NULL );
497 } /* Channel_FirstChannelOf */
501 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
503 assert( Client != NULL );
504 assert( Cl2Chan != NULL );
505 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
506 } /* Channel_NextChannelOf */
510 Channel_GetClient( CL2CHAN *Cl2Chan )
512 assert( Cl2Chan != NULL );
513 return Cl2Chan->client;
514 } /* Channel_GetClient */
518 Channel_GetChannel( CL2CHAN *Cl2Chan )
520 assert( Cl2Chan != NULL );
521 return Cl2Chan->channel;
522 } /* Channel_GetChannel */
526 Channel_IsValidName( const char *Name )
528 assert( Name != NULL );
531 if (strlen(Name) <= 1)
534 if (strchr("#&+", Name[0]) == NULL)
536 if (strlen(Name) >= CHANNEL_NAME_LEN)
539 return Name[strcspn(Name, " ,:\007")] == 0;
540 } /* Channel_IsValidName */
544 Channel_ModeAdd( CHANNEL *Chan, char Mode )
547 * If the channel already had this mode, return false.
548 * If the channel mode was newly set return true.
553 assert( Chan != NULL );
555 x[0] = Mode; x[1] = '\0';
556 if( ! strchr( Chan->modes, x[0] ))
558 /* Channel does not have this mode yet, set it */
559 strlcat( Chan->modes, x, sizeof( Chan->modes ));
563 } /* Channel_ModeAdd */
567 Channel_ModeDel( CHANNEL *Chan, char Mode )
570 * if the mode was removed return true.
571 * if the channel did not have the mode, return false.
575 assert( Chan != NULL );
577 p = strchr( Chan->modes, Mode );
578 if( ! p ) return false;
580 /* Channel has mode -> delete */
587 } /* Channel_ModeDel */
591 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
593 /* Set Channel-User-Mode.
594 * if mode was newly set, return true.
595 * if the User already had this channel-mode, return false.
601 assert( Chan != NULL );
602 assert( Client != NULL );
604 cl2chan = Get_Cl2Chan( Chan, Client );
605 assert( cl2chan != NULL );
607 x[0] = Mode; x[1] = '\0';
608 if( ! strchr( cl2chan->modes, x[0] ))
610 /* mode not set, -> set it */
611 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
615 } /* Channel_UserModeAdd */
619 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
621 /* Delete Channel-User-Mode.
622 * If Mode was removed, return true.
623 * If User did not have the Channel-Mode, return false.
629 assert( Chan != NULL );
630 assert( Client != NULL );
632 cl2chan = Get_Cl2Chan( Chan, Client );
633 assert( cl2chan != NULL );
635 p = strchr( cl2chan->modes, Mode );
636 if( ! p ) return false;
638 /* Client has Mode -> delete */
645 } /* Channel_UserModeDel */
649 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
651 /* return Users' Channel-Modes */
655 assert( Chan != NULL );
656 assert( Client != NULL );
658 cl2chan = Get_Cl2Chan( Chan, Client );
659 assert( cl2chan != NULL );
661 return cl2chan->modes;
662 } /* Channel_UserModes */
666 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
668 /* Test if Client is on Channel Chan */
670 assert( Chan != NULL );
671 assert( Client != NULL );
672 return Get_Cl2Chan(Chan, Client) != NULL;
673 } /* Channel_IsMemberOf */
677 Channel_Topic( CHANNEL *Chan )
680 assert( Chan != NULL );
681 ret = array_start(&Chan->topic);
682 return ret ? ret : "";
683 } /* Channel_Topic */
689 Channel_TopicTime(CHANNEL *Chan)
691 assert(Chan != NULL);
692 return (unsigned int) Chan->topic_time;
693 } /* Channel_TopicTime */
697 Channel_TopicWho(CHANNEL *Chan)
699 assert(Chan != NULL);
700 return Chan->topic_who;
701 } /* Channel_TopicWho */
705 Channel_CreationTime(CHANNEL *Chan)
707 assert(Chan != NULL);
708 return (unsigned int) Chan->creation_time;
709 } /* Channel_CreationTime */
715 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
718 assert( Chan != NULL );
719 assert( Topic != NULL );
722 if (len < array_bytes(&Chan->topic))
723 array_free(&Chan->topic);
725 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
726 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
727 Topic, Chan->name, strerror(errno));
729 Chan->topic_time = time(NULL);
730 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
731 strlcpy(Chan->topic_who, Client_ID(Client),
732 sizeof Chan->topic_who);
734 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
735 sizeof Chan->topic_who);
739 } /* Channel_SetTopic */
743 Channel_SetModes( CHANNEL *Chan, const char *Modes )
745 assert( Chan != NULL );
746 assert( Modes != NULL );
748 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
749 } /* Channel_SetModes */
753 Channel_SetKey( CHANNEL *Chan, const char *Key )
755 assert( Chan != NULL );
756 assert( Key != NULL );
758 strlcpy( Chan->key, Key, sizeof( Chan->key ));
759 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
760 } /* Channel_SetKey */
764 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
766 assert( Chan != NULL );
768 Chan->maxusers = Count;
769 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
770 } /* Channel_SetMaxUsers */
774 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
776 bool is_member, has_voice, is_op;
778 is_member = has_voice = is_op = false;
780 /* The server itself always can send messages :-) */
781 if (Client_ThisServer() == From)
784 if (Channel_IsMemberOf(Chan, From)) {
786 if (strchr(Channel_UserModes(Chan, From), 'v'))
788 if (strchr(Channel_UserModes(Chan, From), 'o'))
793 * Is the client allowed to write to channel?
795 * If channel mode n set: non-members cannot send to channel.
796 * If channel mode m set: need voice.
798 if (strchr(Channel_Modes(Chan), 'n') && !is_member)
801 if (is_op || has_voice)
804 if (strchr(Channel_Modes(Chan), 'm'))
807 return !Lists_Check(&Chan->list_bans, From);
812 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
813 bool SendErrors, const char *Text)
815 if (!Can_Send_To_Channel(Chan, From)) {
817 return CONNECTED; /* no error, see RFC 2812 */
818 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
819 Client_ID(From), Channel_Name(Chan));
822 if (Client_Conn(From) > NONE)
823 Conn_UpdateIdle(Client_Conn(From));
825 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
826 "%s %s :%s", Command, Channel_Name(Chan), Text);
831 Channel_Create( const char *Name )
833 /* Create new CHANNEL structure and add it to linked list */
836 assert( Name != NULL );
838 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
841 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
844 memset( c, 0, sizeof( CHANNEL ));
845 strlcpy( c->name, Name, sizeof( c->name ));
846 c->hash = Hash( c->name );
847 c->next = My_Channels;
849 c->creation_time = time(NULL);
852 LogDebug("Created new channel structure for \"%s\".", Name);
854 } /* Channel_Create */
858 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
862 assert( Chan != NULL );
863 assert( Client != NULL );
865 cl2chan = My_Cl2Chan;
868 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
869 cl2chan = cl2chan->next;
876 Add_Client( CHANNEL *Chan, CLIENT *Client )
880 assert( Chan != NULL );
881 assert( Client != NULL );
883 /* Create new CL2CHAN structure */
884 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
887 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
890 cl2chan->channel = Chan;
891 cl2chan->client = Client;
892 strcpy( cl2chan->modes, "" );
895 cl2chan->next = My_Cl2Chan;
896 My_Cl2Chan = cl2chan;
898 LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
905 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
907 CL2CHAN *cl2chan, *last_cl2chan;
910 assert( Chan != NULL );
911 assert( Client != NULL );
912 assert( Origin != NULL );
913 assert( Reason != NULL );
915 /* Do not inform other servers if the channel is local to this server,
916 * regardless of what the caller requested! */
918 InformServer = !Channel_IsLocal(Chan);
921 cl2chan = My_Cl2Chan;
924 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
925 last_cl2chan = cl2chan;
926 cl2chan = cl2chan->next;
928 if( ! cl2chan ) return false;
930 c = cl2chan->channel;
933 /* maintain cl2chan list */
934 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
935 else My_Cl2Chan = cl2chan->next;
941 /* QUIT: other servers have already been notified,
942 * see Client_Destroy(); so only inform other clients
943 * in same channel. */
944 assert( InformServer == false );
945 LogDebug("User \"%s\" left channel \"%s\" (%s).",
946 Client_Mask( Client ), c->name, Reason );
949 /* User was KICKed: inform other servers (public
950 * channels) and all users in the channel */
952 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
953 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
954 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
955 c->name, Client_ID( Client ), Reason );
956 if ((Client_Conn(Client) > NONE) &&
957 (Client_Type(Client) == CLIENT_USER))
959 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
960 c->name, Client_ID( Client ), Reason);
962 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
963 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
967 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
969 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
972 if ((Client_Conn(Origin) > NONE) &&
973 (Client_Type(Origin) == CLIENT_USER))
975 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
976 LogDebug("User \"%s\" left channel \"%s\" (%s).",
977 Client_Mask(Client), c->name, Reason);
981 /* When channel is empty and is not pre-defined, delete */
982 if( ! strchr( Channel_Modes( Chan ), 'P' ))
984 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
988 } /* Remove_Client */
992 Channel_AddBan(CHANNEL *c, const char *mask )
994 struct list_head *h = Channel_GetListBans(c);
995 LogDebug("Adding \"%s\" to \"%s\" %s list", mask, Channel_Name(c), "ban");
996 return Lists_Add(h, mask, false);
1001 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
1003 struct list_head *h = Channel_GetListInvites(c);
1004 LogDebug("Adding \"%s\" to \"%s\" %s list", mask, Channel_Name(c), "invite");
1005 return Lists_Add(h, mask, onlyonce);
1010 ShowInvitesBans(struct list_head *head, CLIENT *Client, CHANNEL *Channel, bool invite)
1012 struct list_elem *e;
1013 char *msg = invite ? RPL_INVITELIST_MSG : RPL_BANLIST_MSG;
1016 assert( Client != NULL );
1017 assert( Channel != NULL );
1019 e = Lists_GetFirst(head);
1021 if( ! IRC_WriteStrClient( Client, msg, Client_ID( Client ),
1022 Channel_Name( Channel ), Lists_GetMask(e) )) return DISCONNECTED;
1023 e = Lists_GetNext(e);
1026 msg_end = invite ? RPL_ENDOFINVITELIST_MSG : RPL_ENDOFBANLIST_MSG;
1027 return IRC_WriteStrClient( Client, msg_end, Client_ID( Client ), Channel_Name( Channel ));
1032 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1034 struct list_head *h;
1036 assert( Channel != NULL );
1038 h = Channel_GetListBans(Channel);
1039 return ShowInvitesBans(h, Client, Channel, false);
1044 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1046 struct list_head *h;
1048 assert( Channel != NULL );
1050 h = Channel_GetListInvites(Channel);
1051 return ShowInvitesBans(h, Client, Channel, true);
1056 * Log a message to the local &SERVER channel, if it exists.
1059 Channel_LogServer(const char *msg)
1064 assert(msg != NULL);
1066 sc = Channel_Search("&SERVER");
1070 c = Client_ThisServer();
1071 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1072 } /* Channel_LogServer */
1076 Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
1078 char *file_name, line[COMMAND_LEN], *nick, *pass;
1081 assert(Chan != NULL);
1082 assert(Client != NULL);
1083 assert(Key != NULL);
1085 if (!strchr(Chan->modes, 'k'))
1087 if (strcmp(Chan->key, Key) == 0)
1092 file_name = array_start(&Chan->keyfile);
1095 fd = fopen(file_name, "r");
1097 Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
1098 file_name, Chan->name, strerror(errno));
1102 while (fgets(line, (int)sizeof(line), fd) != NULL) {
1104 if (! (nick = strchr(line, ':')))
1107 if (!Match(line, Client_User(Client)))
1109 if (! (pass = strchr(nick, ':')))
1112 if (!Match(nick, Client_ID(Client)))
1114 if (strcmp(Key, pass) != 0)
1122 } /* Channel_CheckKey */
1126 * Check wether a client is allowed to administer a channel or not.
1128 * @param Chan The channel to test.
1129 * @param Client The client from which the command has been received.
1130 * @param Origin The originator of the command (or NULL).
1131 * @param OnChannel Set to true if the originator is member of the channel.
1132 * @param AdminOk Set to true if the client is allowed to do
1133 * administrative tasks on this channel.
1134 * @param UseServerMode Set to true if ngIRCd should emulate "server mode",
1135 * that is send commands as if originating from a server
1136 * and not the originator of the command.
1139 Channel_CheckAdminRights(CHANNEL *Chan, CLIENT *Client, CLIENT *Origin,
1140 bool *OnChannel, bool *AdminOk, bool *UseServerMode)
1142 assert (Chan != NULL);
1143 assert (Client != NULL);
1144 assert (OnChannel != NULL);
1145 assert (AdminOk != NULL);
1146 assert (UseServerMode != NULL);
1148 /* Use the client as origin, if no origin has been given (no prefix?) */
1154 *UseServerMode = false;
1156 if (Client_Type(Client) != CLIENT_USER
1157 && Client_Type(Client) != CLIENT_SERVER
1158 && Client_Type(Client) != CLIENT_SERVICE)
1161 /* Allow channel administration if the client is a server or service */
1162 if (Client_Type(Client) != CLIENT_USER) {
1167 *OnChannel = Channel_IsMemberOf(Chan, Origin);
1169 if (*OnChannel && strchr(Channel_UserModes(Chan, Origin), 'o')) {
1170 /* User is a channel operator */
1172 } else if (Conf_OperCanMode) {
1173 /* IRC operators are allowed to administer channels as well */
1174 if (Client_OperByMe(Origin)) {
1176 if (Conf_OperServerMode)
1177 *UseServerMode = true;
1180 } /* Channel_CheckAdminRights */
1184 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1186 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1187 } /* Get_First_Cl2Chan */
1191 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1195 assert( Client != NULL || Channel != NULL );
1200 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1201 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1202 cl2chan = cl2chan->next;
1205 } /* Get_Next_Cl2Chan */
1209 * Remove a channel and free all of its data structures.
1212 Delete_Channel(CHANNEL *Chan)
1214 CHANNEL *chan, *last_chan;
1225 assert(chan != NULL);
1229 /* maintain channel list */
1231 last_chan->next = chan->next;
1233 My_Channels = chan->next;
1235 LogDebug("Freed channel structure for \"%s\".", Chan->name);
1237 } /* Delete_Channel */
1241 Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
1245 assert(Chan != NULL);
1246 assert(KeyFile != NULL);
1248 len = strlen(KeyFile);
1249 if (len < array_bytes(&Chan->keyfile)) {
1250 Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
1251 array_free(&Chan->keyfile);
1257 if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
1259 "Could not set new channel key file \"%s\" for %s: %s",
1260 KeyFile, Chan->name, strerror(errno));
1262 Log(LOG_INFO|LOG_snotice,
1263 "New local channel key file \"%s\" for %s activated.",
1264 KeyFile, Chan->name);