2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2012 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_GetListExcepts(CHANNEL *c)
94 return &c->list_excepts;
98 GLOBAL struct list_head *
99 Channel_GetListInvites(CHANNEL *c)
102 return &c->list_invites;
107 Channel_InitPredefined( void )
109 /* Generate predefined persistent channels */
112 const struct Conf_Channel *conf_chan;
114 size_t i, channel_count = array_length(&Conf_Channels, sizeof(*conf_chan));
116 conf_chan = array_start(&Conf_Channels);
118 assert(channel_count == 0 || conf_chan != NULL);
120 for (i = 0; i < channel_count; i++, conf_chan++) {
121 if (!conf_chan->name[0])
123 if (!Channel_IsValidName(conf_chan->name)) {
125 "Can't create pre-defined channel: invalid name: \"%s\"",
130 new_chan = Channel_Search(conf_chan->name);
133 "Can't create pre-defined channel \"%s\": name already in use.",
135 Set_KeyFile(new_chan, conf_chan->keyfile);
139 new_chan = Channel_Create(conf_chan->name);
141 Log(LOG_ERR, "Can't create pre-defined channel \"%s\"",
145 Log(LOG_INFO, "Created pre-defined channel \"%s\"",
148 Channel_ModeAdd(new_chan, 'P');
150 if (conf_chan->topic[0])
151 Channel_SetTopic(new_chan, NULL, conf_chan->topic);
153 c = conf_chan->modes;
155 Channel_ModeAdd(new_chan, *c++);
157 Channel_SetKey(new_chan, conf_chan->key);
158 Channel_SetMaxUsers(new_chan, conf_chan->maxusers);
159 Set_KeyFile(new_chan, conf_chan->keyfile);
162 array_free(&Conf_Channels);
163 } /* Channel_InitPredefined */
167 Free_Channel(CHANNEL *chan)
169 array_free(&chan->topic);
170 array_free(&chan->keyfile);
171 Lists_Free(&chan->list_bans);
172 Lists_Free(&chan->list_excepts);
173 Lists_Free(&chan->list_invites);
183 CL2CHAN *cl2chan, *cl2chan_next;
185 /* free struct Channel */
193 /* Free Channel allocation table */
194 cl2chan = My_Cl2Chan;
196 cl2chan_next = cl2chan->next;
198 cl2chan = cl2chan_next;
205 * This function lets a client join a channel. First, the function
206 * checks that the specified channel name is valid and that the client
207 * isn't already a member. If the specified channel doesn't exist,
208 * a new channel is created. Client is added to channel by function
212 Channel_Join( CLIENT *Client, const char *Name )
216 assert(Client != NULL);
217 assert(Name != NULL);
219 /* Check that the channel name is valid */
220 if (! Channel_IsValidName(Name)) {
221 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
222 Client_ID(Client), Name);
226 chan = Channel_Search(Name);
228 /* Check if the client is already in the channel */
229 if (Get_Cl2Chan(chan, Client))
232 /* If the specified channel does not exist, the channel
234 chan = Channel_Create(Name);
239 /* Add user to Channel */
240 if (! Add_Client(chan, Client))
248 * Part client from channel.
249 * This function lets a client part from a channel. First, the function checks
250 * if the channel exists and the client is a member of it and sends out
251 * appropriate error messages if not. The real work is done by the function
255 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
259 assert(Client != NULL);
260 assert(Name != NULL);
261 assert(Reason != NULL);
263 /* Check that specified channel exists */
264 chan = Channel_Search(Name);
266 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
267 Client_ID(Client), Name);
271 /* Check that the client is in the channel */
272 if (!Get_Cl2Chan(chan, Client)) {
273 IRC_WriteStrClient(Client, ERR_NOTONCHANNEL_MSG,
274 Client_ID(Client), Name);
278 if (Conf_MorePrivacy)
281 /* Part client from channel */
282 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
290 * Kick user from Channel
293 Channel_Kick(CLIENT *Peer, CLIENT *Target, CLIENT *Origin, const char *Name,
298 assert(Peer != NULL);
299 assert(Target != NULL);
300 assert(Origin != NULL);
301 assert(Name != NULL);
302 assert(Reason != NULL);
304 /* Check that channel exists */
305 chan = Channel_Search( Name );
308 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
312 if (Client_Type(Peer) != CLIENT_SERVER &&
313 Client_Type(Origin) != CLIENT_SERVICE) {
314 /* Check that user is on the specified channel */
315 if (!Channel_IsMemberOf(chan, Origin)) {
316 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG,
317 Client_ID(Origin), Name);
321 /* Check if user has operator status */
322 if (!strchr(Channel_UserModes(chan, Origin), 'o')) {
323 IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG,
324 Client_ID(Origin), Name);
329 /* Check that the client to be kicked is on the specified channel */
330 if (!Channel_IsMemberOf(chan, Target)) {
331 IRC_WriteStrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
332 Client_ID(Origin), Client_ID(Target), Name );
336 /* Kick Client from channel */
337 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
342 Channel_Quit( CLIENT *Client, const char *Reason )
346 assert( Client != NULL );
347 assert( Reason != NULL );
349 if (Conf_MorePrivacy)
352 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
358 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
365 * Get number of channels this server knows and that are "visible" to
366 * the given client. If no client is given, all channels will be counted.
368 * @param Client The client to check or NULL.
369 * @return Number of channels visible to the client.
372 Channel_CountVisible (CLIENT *Client)
375 unsigned long count = 0;
380 if (!strchr(Channel_Modes(c), 's')
381 || Channel_IsMemberOf(c, Client))
392 Channel_MemberCount( CHANNEL *Chan )
395 unsigned long count = 0;
397 assert( Chan != NULL );
399 cl2chan = My_Cl2Chan;
402 if( cl2chan->channel == Chan ) count++;
403 cl2chan = cl2chan->next;
406 } /* Channel_MemberCount */
410 Channel_CountForUser( CLIENT *Client )
412 /* Count number of channels a user is member of. */
417 assert( Client != NULL );
419 cl2chan = My_Cl2Chan;
422 if( cl2chan->client == Client ) count++;
423 cl2chan = cl2chan->next;
427 } /* Channel_CountForUser */
431 Channel_Name( const CHANNEL *Chan )
433 assert( Chan != NULL );
439 Channel_Modes( CHANNEL *Chan )
441 assert( Chan != NULL );
443 } /* Channel_Modes */
447 Channel_Key( CHANNEL *Chan )
449 assert( Chan != NULL );
455 Channel_MaxUsers( CHANNEL *Chan )
457 assert( Chan != NULL );
458 return Chan->maxusers;
459 } /* Channel_MaxUsers */
463 Channel_First( void )
466 } /* Channel_First */
470 Channel_Next( CHANNEL *Chan )
472 assert( Chan != NULL );
478 Channel_Search( const char *Name )
480 /* Search channel structure */
485 assert( Name != NULL );
487 search_hash = Hash( Name );
491 if( search_hash == c->hash )
494 if( strcasecmp( Name, c->name ) == 0 ) return c;
499 } /* Channel_Search */
503 Channel_FirstMember( CHANNEL *Chan )
505 assert( Chan != NULL );
506 return Get_First_Cl2Chan( NULL, Chan );
507 } /* Channel_FirstMember */
511 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
513 assert( Chan != NULL );
514 assert( Cl2Chan != NULL );
515 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
516 } /* Channel_NextMember */
520 Channel_FirstChannelOf( CLIENT *Client )
522 assert( Client != NULL );
523 return Get_First_Cl2Chan( Client, NULL );
524 } /* Channel_FirstChannelOf */
528 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
530 assert( Client != NULL );
531 assert( Cl2Chan != NULL );
532 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
533 } /* Channel_NextChannelOf */
537 Channel_GetClient( CL2CHAN *Cl2Chan )
539 assert( Cl2Chan != NULL );
540 return Cl2Chan->client;
541 } /* Channel_GetClient */
545 Channel_GetChannel( CL2CHAN *Cl2Chan )
547 assert( Cl2Chan != NULL );
548 return Cl2Chan->channel;
549 } /* Channel_GetChannel */
553 Channel_IsValidName( const char *Name )
555 assert( Name != NULL );
558 if (strlen(Name) <= 1)
561 if (strchr("#&+", Name[0]) == NULL)
563 if (strlen(Name) >= CHANNEL_NAME_LEN)
566 return Name[strcspn(Name, " ,:\007")] == 0;
567 } /* Channel_IsValidName */
571 Channel_ModeAdd( CHANNEL *Chan, char Mode )
574 * If the channel already had this mode, return false.
575 * If the channel mode was newly set return true.
580 assert( Chan != NULL );
582 x[0] = Mode; x[1] = '\0';
583 if( ! strchr( Chan->modes, x[0] ))
585 /* Channel does not have this mode yet, set it */
586 strlcat( Chan->modes, x, sizeof( Chan->modes ));
590 } /* Channel_ModeAdd */
594 Channel_ModeDel( CHANNEL *Chan, char Mode )
597 * if the mode was removed return true.
598 * if the channel did not have the mode, return false.
602 assert( Chan != NULL );
604 p = strchr( Chan->modes, Mode );
605 if( ! p ) return false;
607 /* Channel has mode -> delete */
614 } /* Channel_ModeDel */
618 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
620 /* Set Channel-User-Mode.
621 * if mode was newly set, return true.
622 * if the User already had this channel-mode, return false.
628 assert( Chan != NULL );
629 assert( Client != NULL );
631 cl2chan = Get_Cl2Chan( Chan, Client );
632 assert( cl2chan != NULL );
634 x[0] = Mode; x[1] = '\0';
635 if( ! strchr( cl2chan->modes, x[0] ))
637 /* mode not set, -> set it */
638 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
642 } /* Channel_UserModeAdd */
646 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
648 /* Delete Channel-User-Mode.
649 * If Mode was removed, return true.
650 * If User did not have the Channel-Mode, return false.
656 assert( Chan != NULL );
657 assert( Client != NULL );
659 cl2chan = Get_Cl2Chan( Chan, Client );
660 assert( cl2chan != NULL );
662 p = strchr( cl2chan->modes, Mode );
663 if( ! p ) return false;
665 /* Client has Mode -> delete */
672 } /* Channel_UserModeDel */
676 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
678 /* return Users' Channel-Modes */
682 assert( Chan != NULL );
683 assert( Client != NULL );
685 cl2chan = Get_Cl2Chan( Chan, Client );
686 assert( cl2chan != NULL );
688 return cl2chan->modes;
689 } /* Channel_UserModes */
693 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
695 /* Test if Client is on Channel Chan */
697 assert( Chan != NULL );
698 assert( Client != NULL );
699 return Get_Cl2Chan(Chan, Client) != NULL;
700 } /* Channel_IsMemberOf */
704 Channel_Topic( CHANNEL *Chan )
707 assert( Chan != NULL );
708 ret = array_start(&Chan->topic);
709 return ret ? ret : "";
710 } /* Channel_Topic */
716 Channel_TopicTime(CHANNEL *Chan)
718 assert(Chan != NULL);
719 return (unsigned int) Chan->topic_time;
720 } /* Channel_TopicTime */
724 Channel_TopicWho(CHANNEL *Chan)
726 assert(Chan != NULL);
727 return Chan->topic_who;
728 } /* Channel_TopicWho */
732 Channel_CreationTime(CHANNEL *Chan)
734 assert(Chan != NULL);
735 return (unsigned int) Chan->creation_time;
736 } /* Channel_CreationTime */
742 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
745 assert( Chan != NULL );
746 assert( Topic != NULL );
749 if (len < array_bytes(&Chan->topic))
750 array_free(&Chan->topic);
752 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
753 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
754 Topic, Chan->name, strerror(errno));
756 Chan->topic_time = time(NULL);
757 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
758 strlcpy(Chan->topic_who, Client_ID(Client),
759 sizeof Chan->topic_who);
761 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
762 sizeof Chan->topic_who);
766 } /* Channel_SetTopic */
770 Channel_SetModes( CHANNEL *Chan, const char *Modes )
772 assert( Chan != NULL );
773 assert( Modes != NULL );
775 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
776 } /* Channel_SetModes */
780 Channel_SetKey( CHANNEL *Chan, const char *Key )
782 assert( Chan != NULL );
783 assert( Key != NULL );
785 strlcpy( Chan->key, Key, sizeof( Chan->key ));
786 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
787 } /* Channel_SetKey */
791 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
793 assert( Chan != NULL );
795 Chan->maxusers = Count;
796 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
797 } /* Channel_SetMaxUsers */
801 * Check if a client is allowed to send to a specific channel.
803 * @param Chan The channel to check.
804 * @param From The client that wants to send.
805 * @return true if the client is allowed to send, false otherwise.
808 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
810 bool is_member, has_voice, is_op;
812 is_member = has_voice = is_op = false;
814 /* The server itself always can send messages :-) */
815 if (Client_ThisServer() == From)
818 if (Channel_IsMemberOf(Chan, From)) {
820 if (strchr(Channel_UserModes(Chan, From), 'v'))
822 if (strchr(Channel_UserModes(Chan, From), 'o'))
827 * Is the client allowed to write to channel?
829 * If channel mode n set: non-members cannot send to channel.
830 * If channel mode m set: need voice.
832 if (strchr(Channel_Modes(Chan), 'n') && !is_member)
835 if (is_op || has_voice)
838 if (strchr(Channel_Modes(Chan), 'm'))
841 if (Lists_Check(&Chan->list_excepts, From))
844 return !Lists_Check(&Chan->list_bans, From);
849 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
850 bool SendErrors, const char *Text)
852 if (!Can_Send_To_Channel(Chan, From)) {
854 return CONNECTED; /* no error, see RFC 2812 */
855 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
856 Client_ID(From), Channel_Name(Chan));
859 if (Client_Conn(From) > NONE)
860 Conn_UpdateIdle(Client_Conn(From));
862 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
863 "%s %s :%s", Command, Channel_Name(Chan), Text);
868 Channel_Create( const char *Name )
870 /* Create new CHANNEL structure and add it to linked list */
873 assert( Name != NULL );
875 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
878 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
881 memset( c, 0, sizeof( CHANNEL ));
882 strlcpy( c->name, Name, sizeof( c->name ));
883 c->hash = Hash( c->name );
884 c->next = My_Channels;
886 c->creation_time = time(NULL);
889 LogDebug("Created new channel structure for \"%s\".", Name);
891 } /* Channel_Create */
895 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
899 assert( Chan != NULL );
900 assert( Client != NULL );
902 cl2chan = My_Cl2Chan;
905 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
906 cl2chan = cl2chan->next;
913 Add_Client( CHANNEL *Chan, CLIENT *Client )
917 assert( Chan != NULL );
918 assert( Client != NULL );
920 /* Create new CL2CHAN structure */
921 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
924 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
927 cl2chan->channel = Chan;
928 cl2chan->client = Client;
929 strcpy( cl2chan->modes, "" );
932 cl2chan->next = My_Cl2Chan;
933 My_Cl2Chan = cl2chan;
935 LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
942 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
944 CL2CHAN *cl2chan, *last_cl2chan;
947 assert( Chan != NULL );
948 assert( Client != NULL );
949 assert( Origin != NULL );
950 assert( Reason != NULL );
952 /* Do not inform other servers if the channel is local to this server,
953 * regardless of what the caller requested! */
955 InformServer = !Channel_IsLocal(Chan);
958 cl2chan = My_Cl2Chan;
961 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
962 last_cl2chan = cl2chan;
963 cl2chan = cl2chan->next;
965 if( ! cl2chan ) return false;
967 c = cl2chan->channel;
970 /* maintain cl2chan list */
971 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
972 else My_Cl2Chan = cl2chan->next;
978 /* QUIT: other servers have already been notified,
979 * see Client_Destroy(); so only inform other clients
980 * in same channel. */
981 assert( InformServer == false );
982 LogDebug("User \"%s\" left channel \"%s\" (%s).",
983 Client_Mask( Client ), c->name, Reason );
986 /* User was KICKed: inform other servers (public
987 * channels) and all users in the channel */
989 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
990 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
991 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
992 c->name, Client_ID( Client ), Reason );
993 if ((Client_Conn(Client) > NONE) &&
994 (Client_Type(Client) == CLIENT_USER))
996 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
997 c->name, Client_ID( Client ), Reason);
999 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
1000 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
1003 if (Conf_MorePrivacy)
1007 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
1009 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
1012 if ((Client_Conn(Origin) > NONE) &&
1013 (Client_Type(Origin) == CLIENT_USER))
1015 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
1016 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1017 Client_Mask(Client), c->name, Reason);
1021 /* When channel is empty and is not pre-defined, delete */
1022 if( ! strchr( Channel_Modes( Chan ), 'P' ))
1024 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
1028 } /* Remove_Client */
1032 Channel_AddBan(CHANNEL *c, const char *mask )
1034 struct list_head *h = Channel_GetListBans(c);
1035 LogDebug("Adding \"%s\" to \"%s\" ban list", mask, Channel_Name(c));
1036 return Lists_Add(h, mask, false, NULL);
1041 Channel_AddExcept(CHANNEL *c, const char *mask )
1043 struct list_head *h = Channel_GetListExcepts(c);
1044 LogDebug("Adding \"%s\" to \"%s\" exception list", mask, Channel_Name(c));
1045 return Lists_Add(h, mask, false, NULL);
1050 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
1052 struct list_head *h = Channel_GetListInvites(c);
1053 LogDebug("Adding \"%s\" to \"%s\" invite list", mask, Channel_Name(c));
1054 return Lists_Add(h, mask, onlyonce, NULL);
1059 ShowChannelList(struct list_head *head, CLIENT *Client, CHANNEL *Channel,
1060 char *msg, char *msg_end)
1062 struct list_elem *e;
1064 assert (Client != NULL);
1065 assert (Channel != NULL);
1067 e = Lists_GetFirst(head);
1069 if (!IRC_WriteStrClient(Client, msg, Client_ID(Client),
1070 Channel_Name(Channel),
1072 return DISCONNECTED;
1073 e = Lists_GetNext(e);
1076 return IRC_WriteStrClient(Client, msg_end, Client_ID(Client),
1077 Channel_Name(Channel));
1082 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1084 struct list_head *h;
1086 assert( Channel != NULL );
1088 h = Channel_GetListBans(Channel);
1089 return ShowChannelList(h, Client, Channel, RPL_BANLIST_MSG,
1090 RPL_ENDOFBANLIST_MSG);
1095 Channel_ShowExcepts( CLIENT *Client, CHANNEL *Channel )
1097 struct list_head *h;
1099 assert( Channel != NULL );
1101 h = Channel_GetListExcepts(Channel);
1102 return ShowChannelList(h, Client, Channel, RPL_EXCEPTLIST_MSG,
1103 RPL_ENDOFEXCEPTLIST_MSG);
1108 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1110 struct list_head *h;
1112 assert( Channel != NULL );
1114 h = Channel_GetListInvites(Channel);
1115 return ShowChannelList(h, Client, Channel, RPL_INVITELIST_MSG,
1116 RPL_ENDOFINVITELIST_MSG);
1121 * Log a message to the local &SERVER channel, if it exists.
1124 Channel_LogServer(const char *msg)
1129 assert(msg != NULL);
1131 sc = Channel_Search("&SERVER");
1135 c = Client_ThisServer();
1136 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1137 } /* Channel_LogServer */
1141 Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
1143 char *file_name, line[COMMAND_LEN], *nick, *pass;
1146 assert(Chan != NULL);
1147 assert(Client != NULL);
1148 assert(Key != NULL);
1150 if (!strchr(Chan->modes, 'k'))
1154 if (strcmp(Chan->key, Key) == 0)
1157 file_name = array_start(&Chan->keyfile);
1160 fd = fopen(file_name, "r");
1162 Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
1163 file_name, Chan->name, strerror(errno));
1167 while (fgets(line, (int)sizeof(line), fd) != NULL) {
1169 if (! (nick = strchr(line, ':')))
1172 if (!Match(line, Client_User(Client)))
1174 if (! (pass = strchr(nick, ':')))
1177 if (!Match(nick, Client_ID(Client)))
1179 if (strcmp(Key, pass) != 0)
1187 } /* Channel_CheckKey */
1191 * Check wether a client is allowed to administer a channel or not.
1193 * @param Chan The channel to test.
1194 * @param Client The client from which the command has been received.
1195 * @param Origin The originator of the command (or NULL).
1196 * @param OnChannel Set to true if the originator is member of the channel.
1197 * @param AdminOk Set to true if the client is allowed to do
1198 * administrative tasks on this channel.
1199 * @param UseServerMode Set to true if ngIRCd should emulate "server mode",
1200 * that is send commands as if originating from a server
1201 * and not the originator of the command.
1204 Channel_CheckAdminRights(CHANNEL *Chan, CLIENT *Client, CLIENT *Origin,
1205 bool *OnChannel, bool *AdminOk, bool *UseServerMode)
1207 assert (Chan != NULL);
1208 assert (Client != NULL);
1209 assert (OnChannel != NULL);
1210 assert (AdminOk != NULL);
1211 assert (UseServerMode != NULL);
1213 /* Use the client as origin, if no origin has been given (no prefix?) */
1219 *UseServerMode = false;
1221 if (Client_Type(Client) != CLIENT_USER
1222 && Client_Type(Client) != CLIENT_SERVER
1223 && Client_Type(Client) != CLIENT_SERVICE)
1226 /* Allow channel administration if the client is a server or service */
1227 if (Client_Type(Client) != CLIENT_USER) {
1232 *OnChannel = Channel_IsMemberOf(Chan, Origin);
1234 if (*OnChannel && strchr(Channel_UserModes(Chan, Origin), 'o')) {
1235 /* User is a channel operator */
1237 } else if (Conf_OperCanMode) {
1238 /* IRC operators are allowed to administer channels as well */
1239 if (Client_OperByMe(Origin)) {
1241 if (Conf_OperServerMode)
1242 *UseServerMode = true;
1245 } /* Channel_CheckAdminRights */
1249 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1251 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1252 } /* Get_First_Cl2Chan */
1256 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1260 assert( Client != NULL || Channel != NULL );
1265 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1266 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1267 cl2chan = cl2chan->next;
1270 } /* Get_Next_Cl2Chan */
1274 * Remove a channel and free all of its data structures.
1277 Delete_Channel(CHANNEL *Chan)
1279 CHANNEL *chan, *last_chan;
1290 assert(chan != NULL);
1294 /* maintain channel list */
1296 last_chan->next = chan->next;
1298 My_Channels = chan->next;
1300 LogDebug("Freed channel structure for \"%s\".", Chan->name);
1302 } /* Delete_Channel */
1306 Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
1310 assert(Chan != NULL);
1311 assert(KeyFile != NULL);
1313 len = strlen(KeyFile);
1314 if (len < array_bytes(&Chan->keyfile)) {
1315 Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
1316 array_free(&Chan->keyfile);
1322 if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
1324 "Could not set new channel key file \"%s\" for %s: %s",
1325 KeyFile, Chan->name, strerror(errno));
1327 Log(LOG_INFO|LOG_snotice,
1328 "New local channel key file \"%s\" for %s activated.",
1329 KeyFile, Chan->name);