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 GLOBAL struct list_head *
75 Channel_GetListBans(CHANNEL *c)
82 GLOBAL struct list_head *
83 Channel_GetListExcepts(CHANNEL *c)
86 return &c->list_excepts;
90 GLOBAL struct list_head *
91 Channel_GetListInvites(CHANNEL *c)
94 return &c->list_invites;
99 * Generate predefined persistent channels and &SERVER
102 Channel_InitPredefined( void )
105 const struct Conf_Channel *conf_chan;
107 size_t i, channel_count = array_length(&Conf_Channels, sizeof(*conf_chan));
109 conf_chan = array_start(&Conf_Channels);
111 assert(channel_count == 0 || conf_chan != NULL);
113 for (i = 0; i < channel_count; i++, conf_chan++) {
114 if (!conf_chan->name[0])
116 if (!Channel_IsValidName(conf_chan->name)) {
118 "Can't create pre-defined channel: invalid name: \"%s\"",
123 new_chan = Channel_Search(conf_chan->name);
126 "Can't create pre-defined channel \"%s\": name already in use.",
128 Set_KeyFile(new_chan, conf_chan->keyfile);
132 new_chan = Channel_Create(conf_chan->name);
134 Log(LOG_ERR, "Can't create pre-defined channel \"%s\"",
138 Log(LOG_INFO, "Created pre-defined channel \"%s\"",
141 Channel_ModeAdd(new_chan, 'P');
143 if (conf_chan->topic[0])
144 Channel_SetTopic(new_chan, NULL, conf_chan->topic);
146 c = conf_chan->modes;
148 Channel_ModeAdd(new_chan, *c++);
150 Channel_SetKey(new_chan, conf_chan->key);
151 Channel_SetMaxUsers(new_chan, conf_chan->maxusers);
152 Set_KeyFile(new_chan, conf_chan->keyfile);
155 array_free(&Conf_Channels);
157 /* Make sure the local &SERVER channel exists */
158 if (!Channel_Search("&SERVER")) {
159 new_chan = Channel_Create("&SERVER");
161 Channel_SetModes(new_chan, "mnPt");
162 Channel_SetTopic(new_chan, Client_ThisServer(),
165 Log(LOG_ERR, "Failed to create \"&SERVER\" channel!");
167 LogDebug("Required channel \"&SERVER\" already exists, ok.");
168 } /* Channel_InitPredefined */
172 Free_Channel(CHANNEL *chan)
174 array_free(&chan->topic);
175 array_free(&chan->keyfile);
176 Lists_Free(&chan->list_bans);
177 Lists_Free(&chan->list_excepts);
178 Lists_Free(&chan->list_invites);
188 CL2CHAN *cl2chan, *cl2chan_next;
190 /* free struct Channel */
198 /* Free Channel allocation table */
199 cl2chan = My_Cl2Chan;
201 cl2chan_next = cl2chan->next;
203 cl2chan = cl2chan_next;
210 * This function lets a client join a channel. First, the function
211 * checks that the specified channel name is valid and that the client
212 * isn't already a member. If the specified channel doesn't exist,
213 * a new channel is created. Client is added to channel by function
217 Channel_Join( CLIENT *Client, const char *Name )
221 assert(Client != NULL);
222 assert(Name != NULL);
224 /* Check that the channel name is valid */
225 if (! Channel_IsValidName(Name)) {
226 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
227 Client_ID(Client), Name);
231 chan = Channel_Search(Name);
233 /* Check if the client is already in the channel */
234 if (Get_Cl2Chan(chan, Client))
237 /* If the specified channel does not exist, the channel
239 chan = Channel_Create(Name);
244 /* Add user to Channel */
245 if (! Add_Client(chan, Client))
253 * Part client from channel.
254 * This function lets a client part from a channel. First, the function checks
255 * if the channel exists and the client is a member of it and sends out
256 * appropriate error messages if not. The real work is done by the function
260 Channel_Part(CLIENT * Client, CLIENT * Origin, const char *Name, const char *Reason)
264 assert(Client != NULL);
265 assert(Name != NULL);
266 assert(Reason != NULL);
268 /* Check that specified channel exists */
269 chan = Channel_Search(Name);
271 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
272 Client_ID(Client), Name);
276 /* Check that the client is in the channel */
277 if (!Get_Cl2Chan(chan, Client)) {
278 IRC_WriteStrClient(Client, ERR_NOTONCHANNEL_MSG,
279 Client_ID(Client), Name);
283 if (Conf_MorePrivacy)
286 /* Part client from channel */
287 if (!Remove_Client(REMOVE_PART, chan, Client, Origin, Reason, true))
295 * Kick user from Channel
298 Channel_Kick(CLIENT *Peer, CLIENT *Target, CLIENT *Origin, const char *Name,
303 assert(Peer != NULL);
304 assert(Target != NULL);
305 assert(Origin != NULL);
306 assert(Name != NULL);
307 assert(Reason != NULL);
309 /* Check that channel exists */
310 chan = Channel_Search( Name );
313 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
317 if (Client_Type(Peer) != CLIENT_SERVER &&
318 Client_Type(Origin) != CLIENT_SERVICE) {
319 /* Check that user is on the specified channel */
320 if (!Channel_IsMemberOf(chan, Origin)) {
321 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG,
322 Client_ID(Origin), Name);
326 /* Check if user has operator status */
327 if (!strchr(Channel_UserModes(chan, Origin), 'o')) {
328 IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG,
329 Client_ID(Origin), Name);
334 /* Check that the client to be kicked is on the specified channel */
335 if (!Channel_IsMemberOf(chan, Target)) {
336 IRC_WriteStrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
337 Client_ID(Origin), Client_ID(Target), Name );
341 /* Kick Client from channel */
342 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
347 Channel_Quit( CLIENT *Client, const char *Reason )
351 assert( Client != NULL );
352 assert( Reason != NULL );
354 if (Conf_MorePrivacy)
357 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
363 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
370 * Get number of channels this server knows and that are "visible" to
371 * the given client. If no client is given, all channels will be counted.
373 * @param Client The client to check or NULL.
374 * @return Number of channels visible to the client.
377 Channel_CountVisible (CLIENT *Client)
380 unsigned long count = 0;
385 if (!strchr(Channel_Modes(c), 's')
386 || Channel_IsMemberOf(c, Client))
397 Channel_MemberCount( CHANNEL *Chan )
400 unsigned long count = 0;
402 assert( Chan != NULL );
404 cl2chan = My_Cl2Chan;
407 if( cl2chan->channel == Chan ) count++;
408 cl2chan = cl2chan->next;
411 } /* Channel_MemberCount */
415 Channel_CountForUser( CLIENT *Client )
417 /* Count number of channels a user is member of. */
422 assert( Client != NULL );
424 cl2chan = My_Cl2Chan;
427 if( cl2chan->client == Client ) count++;
428 cl2chan = cl2chan->next;
432 } /* Channel_CountForUser */
436 Channel_Name( const CHANNEL *Chan )
438 assert( Chan != NULL );
444 Channel_Modes( CHANNEL *Chan )
446 assert( Chan != NULL );
448 } /* Channel_Modes */
452 Channel_Key( CHANNEL *Chan )
454 assert( Chan != NULL );
460 Channel_MaxUsers( CHANNEL *Chan )
462 assert( Chan != NULL );
463 return Chan->maxusers;
464 } /* Channel_MaxUsers */
468 Channel_First( void )
471 } /* Channel_First */
475 Channel_Next( CHANNEL *Chan )
477 assert( Chan != NULL );
483 Channel_Search( const char *Name )
485 /* Search channel structure */
490 assert( Name != NULL );
492 search_hash = Hash( Name );
496 if( search_hash == c->hash )
499 if( strcasecmp( Name, c->name ) == 0 ) return c;
504 } /* Channel_Search */
508 Channel_FirstMember( CHANNEL *Chan )
510 assert( Chan != NULL );
511 return Get_First_Cl2Chan( NULL, Chan );
512 } /* Channel_FirstMember */
516 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
518 assert( Chan != NULL );
519 assert( Cl2Chan != NULL );
520 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
521 } /* Channel_NextMember */
525 Channel_FirstChannelOf( CLIENT *Client )
527 assert( Client != NULL );
528 return Get_First_Cl2Chan( Client, NULL );
529 } /* Channel_FirstChannelOf */
533 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
535 assert( Client != NULL );
536 assert( Cl2Chan != NULL );
537 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
538 } /* Channel_NextChannelOf */
542 Channel_GetClient( CL2CHAN *Cl2Chan )
544 assert( Cl2Chan != NULL );
545 return Cl2Chan->client;
546 } /* Channel_GetClient */
550 Channel_GetChannel( CL2CHAN *Cl2Chan )
552 assert( Cl2Chan != NULL );
553 return Cl2Chan->channel;
554 } /* Channel_GetChannel */
558 Channel_IsValidName( const char *Name )
560 assert( Name != NULL );
563 if (strlen(Name) <= 1)
566 if (strchr("#&+", Name[0]) == NULL)
568 if (strlen(Name) >= CHANNEL_NAME_LEN)
571 return Name[strcspn(Name, " ,:\007")] == 0;
572 } /* Channel_IsValidName */
576 Channel_ModeAdd( CHANNEL *Chan, char Mode )
579 * If the channel already had this mode, return false.
580 * If the channel mode was newly set return true.
585 assert( Chan != NULL );
587 x[0] = Mode; x[1] = '\0';
588 if( ! strchr( Chan->modes, x[0] ))
590 /* Channel does not have this mode yet, set it */
591 strlcat( Chan->modes, x, sizeof( Chan->modes ));
595 } /* Channel_ModeAdd */
599 Channel_ModeDel( CHANNEL *Chan, char Mode )
602 * if the mode was removed return true.
603 * if the channel did not have the mode, return false.
607 assert( Chan != NULL );
609 p = strchr( Chan->modes, Mode );
610 if( ! p ) return false;
612 /* Channel has mode -> delete */
619 } /* Channel_ModeDel */
623 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
625 /* Set Channel-User-Mode.
626 * if mode was newly set, return true.
627 * if the User already had this channel-mode, return false.
633 assert( Chan != NULL );
634 assert( Client != NULL );
636 cl2chan = Get_Cl2Chan( Chan, Client );
637 assert( cl2chan != NULL );
639 x[0] = Mode; x[1] = '\0';
640 if( ! strchr( cl2chan->modes, x[0] ))
642 /* mode not set, -> set it */
643 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
647 } /* Channel_UserModeAdd */
651 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
653 /* Delete Channel-User-Mode.
654 * If Mode was removed, return true.
655 * If User did not have the Channel-Mode, return false.
661 assert( Chan != NULL );
662 assert( Client != NULL );
664 cl2chan = Get_Cl2Chan( Chan, Client );
665 assert( cl2chan != NULL );
667 p = strchr( cl2chan->modes, Mode );
668 if( ! p ) return false;
670 /* Client has Mode -> delete */
677 } /* Channel_UserModeDel */
681 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
683 /* return Users' Channel-Modes */
687 assert( Chan != NULL );
688 assert( Client != NULL );
690 cl2chan = Get_Cl2Chan( Chan, Client );
691 assert( cl2chan != NULL );
693 return cl2chan->modes;
694 } /* Channel_UserModes */
698 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
700 /* Test if Client is on Channel Chan */
702 assert( Chan != NULL );
703 assert( Client != NULL );
704 return Get_Cl2Chan(Chan, Client) != NULL;
705 } /* Channel_IsMemberOf */
709 Channel_Topic( CHANNEL *Chan )
712 assert( Chan != NULL );
713 ret = array_start(&Chan->topic);
714 return ret ? ret : "";
715 } /* Channel_Topic */
721 Channel_TopicTime(CHANNEL *Chan)
723 assert(Chan != NULL);
724 return (unsigned int) Chan->topic_time;
725 } /* Channel_TopicTime */
729 Channel_TopicWho(CHANNEL *Chan)
731 assert(Chan != NULL);
732 return Chan->topic_who;
733 } /* Channel_TopicWho */
737 Channel_CreationTime(CHANNEL *Chan)
739 assert(Chan != NULL);
740 return (unsigned int) Chan->creation_time;
741 } /* Channel_CreationTime */
747 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
750 assert( Chan != NULL );
751 assert( Topic != NULL );
754 if (len < array_bytes(&Chan->topic))
755 array_free(&Chan->topic);
757 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
758 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
759 Topic, Chan->name, strerror(errno));
761 Chan->topic_time = time(NULL);
762 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
763 strlcpy(Chan->topic_who, Client_ID(Client),
764 sizeof Chan->topic_who);
766 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
767 sizeof Chan->topic_who);
771 } /* Channel_SetTopic */
775 Channel_SetModes( CHANNEL *Chan, const char *Modes )
777 assert( Chan != NULL );
778 assert( Modes != NULL );
780 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
781 } /* Channel_SetModes */
785 Channel_SetKey( CHANNEL *Chan, const char *Key )
787 assert( Chan != NULL );
788 assert( Key != NULL );
790 strlcpy( Chan->key, Key, sizeof( Chan->key ));
791 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
792 } /* Channel_SetKey */
796 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
798 assert( Chan != NULL );
800 Chan->maxusers = Count;
801 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
802 } /* Channel_SetMaxUsers */
806 * Check if a client is allowed to send to a specific channel.
808 * @param Chan The channel to check.
809 * @param From The client that wants to send.
810 * @return true if the client is allowed to send, false otherwise.
813 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
815 bool is_member, has_voice, is_op;
817 is_member = has_voice = is_op = false;
819 /* The server itself always can send messages :-) */
820 if (Client_ThisServer() == From)
823 if (Channel_IsMemberOf(Chan, From)) {
825 if (strchr(Channel_UserModes(Chan, From), 'v'))
827 if (strchr(Channel_UserModes(Chan, From), 'o'))
832 * Is the client allowed to write to channel?
834 * If channel mode n set: non-members cannot send to channel.
835 * If channel mode m set: need voice.
837 if (strchr(Channel_Modes(Chan), 'n') && !is_member)
840 if (strchr(Channel_Modes(Chan), 'M') && !Client_HasMode(From, 'R')
841 && !Client_HasMode(From, 'o'))
844 if (is_op || has_voice)
847 if (strchr(Channel_Modes(Chan), 'm'))
850 if (Lists_Check(&Chan->list_excepts, From))
853 return !Lists_Check(&Chan->list_bans, From);
858 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
859 bool SendErrors, const char *Text)
861 if (!Can_Send_To_Channel(Chan, From)) {
863 return CONNECTED; /* no error, see RFC 2812 */
864 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
865 Client_ID(From), Channel_Name(Chan));
868 if (Client_Conn(From) > NONE)
869 Conn_UpdateIdle(Client_Conn(From));
871 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
872 "%s %s :%s", Command, Channel_Name(Chan), Text);
877 Channel_Create( const char *Name )
879 /* Create new CHANNEL structure and add it to linked list */
882 assert( Name != NULL );
884 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
887 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
890 memset( c, 0, sizeof( CHANNEL ));
891 strlcpy( c->name, Name, sizeof( c->name ));
892 c->hash = Hash( c->name );
893 c->next = My_Channels;
895 c->creation_time = time(NULL);
898 LogDebug("Created new channel structure for \"%s\".", Name);
900 } /* Channel_Create */
904 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
908 assert( Chan != NULL );
909 assert( Client != NULL );
911 cl2chan = My_Cl2Chan;
914 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
915 cl2chan = cl2chan->next;
922 Add_Client( CHANNEL *Chan, CLIENT *Client )
926 assert( Chan != NULL );
927 assert( Client != NULL );
929 /* Create new CL2CHAN structure */
930 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
933 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
936 cl2chan->channel = Chan;
937 cl2chan->client = Client;
938 strcpy( cl2chan->modes, "" );
941 cl2chan->next = My_Cl2Chan;
942 My_Cl2Chan = cl2chan;
944 LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
951 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
953 CL2CHAN *cl2chan, *last_cl2chan;
956 assert( Chan != NULL );
957 assert( Client != NULL );
958 assert( Origin != NULL );
959 assert( Reason != NULL );
961 /* Do not inform other servers if the channel is local to this server,
962 * regardless of what the caller requested! */
964 InformServer = !Channel_IsLocal(Chan);
967 cl2chan = My_Cl2Chan;
970 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
971 last_cl2chan = cl2chan;
972 cl2chan = cl2chan->next;
974 if( ! cl2chan ) return false;
976 c = cl2chan->channel;
979 /* maintain cl2chan list */
980 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
981 else My_Cl2Chan = cl2chan->next;
987 /* QUIT: other servers have already been notified,
988 * see Client_Destroy(); so only inform other clients
989 * in same channel. */
990 assert( InformServer == false );
991 LogDebug("User \"%s\" left channel \"%s\" (%s).",
992 Client_Mask( Client ), c->name, Reason );
995 /* User was KICKed: inform other servers (public
996 * channels) and all users in the channel */
998 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
999 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
1000 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
1001 c->name, Client_ID( Client ), Reason );
1002 if ((Client_Conn(Client) > NONE) &&
1003 (Client_Type(Client) == CLIENT_USER))
1005 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
1006 c->name, Client_ID( Client ), Reason);
1008 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
1009 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
1012 if (Conf_MorePrivacy)
1016 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
1018 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
1021 if ((Client_Conn(Origin) > NONE) &&
1022 (Client_Type(Origin) == CLIENT_USER))
1024 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
1025 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1026 Client_Mask(Client), c->name, Reason);
1030 /* When channel is empty and is not pre-defined, delete */
1031 if( ! strchr( Channel_Modes( Chan ), 'P' ))
1033 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
1037 } /* Remove_Client */
1041 Channel_AddBan(CHANNEL *c, const char *mask )
1043 struct list_head *h = Channel_GetListBans(c);
1044 LogDebug("Adding \"%s\" to \"%s\" ban list", mask, Channel_Name(c));
1045 return Lists_Add(h, mask, false, NULL);
1050 Channel_AddExcept(CHANNEL *c, const char *mask )
1052 struct list_head *h = Channel_GetListExcepts(c);
1053 LogDebug("Adding \"%s\" to \"%s\" exception list", mask, Channel_Name(c));
1054 return Lists_Add(h, mask, false, NULL);
1059 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
1061 struct list_head *h = Channel_GetListInvites(c);
1062 LogDebug("Adding \"%s\" to \"%s\" invite list", mask, Channel_Name(c));
1063 return Lists_Add(h, mask, onlyonce, NULL);
1068 ShowChannelList(struct list_head *head, CLIENT *Client, CHANNEL *Channel,
1069 char *msg, char *msg_end)
1071 struct list_elem *e;
1073 assert (Client != NULL);
1074 assert (Channel != NULL);
1076 e = Lists_GetFirst(head);
1078 if (!IRC_WriteStrClient(Client, msg, Client_ID(Client),
1079 Channel_Name(Channel),
1081 return DISCONNECTED;
1082 e = Lists_GetNext(e);
1085 return IRC_WriteStrClient(Client, msg_end, Client_ID(Client),
1086 Channel_Name(Channel));
1091 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1093 struct list_head *h;
1095 assert( Channel != NULL );
1097 h = Channel_GetListBans(Channel);
1098 return ShowChannelList(h, Client, Channel, RPL_BANLIST_MSG,
1099 RPL_ENDOFBANLIST_MSG);
1104 Channel_ShowExcepts( CLIENT *Client, CHANNEL *Channel )
1106 struct list_head *h;
1108 assert( Channel != NULL );
1110 h = Channel_GetListExcepts(Channel);
1111 return ShowChannelList(h, Client, Channel, RPL_EXCEPTLIST_MSG,
1112 RPL_ENDOFEXCEPTLIST_MSG);
1117 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1119 struct list_head *h;
1121 assert( Channel != NULL );
1123 h = Channel_GetListInvites(Channel);
1124 return ShowChannelList(h, Client, Channel, RPL_INVITELIST_MSG,
1125 RPL_ENDOFINVITELIST_MSG);
1130 * Log a message to the local &SERVER channel, if it exists.
1133 Channel_LogServer(const char *msg)
1138 assert(msg != NULL);
1140 sc = Channel_Search("&SERVER");
1144 c = Client_ThisServer();
1145 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1146 } /* Channel_LogServer */
1150 Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
1152 char *file_name, line[COMMAND_LEN], *nick, *pass;
1155 assert(Chan != NULL);
1156 assert(Client != NULL);
1157 assert(Key != NULL);
1159 if (!strchr(Chan->modes, 'k'))
1163 if (strcmp(Chan->key, Key) == 0)
1166 file_name = array_start(&Chan->keyfile);
1169 fd = fopen(file_name, "r");
1171 Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
1172 file_name, Chan->name, strerror(errno));
1176 while (fgets(line, (int)sizeof(line), fd) != NULL) {
1178 if (! (nick = strchr(line, ':')))
1181 if (!Match(line, Client_User(Client)))
1183 if (! (pass = strchr(nick, ':')))
1186 if (!Match(nick, Client_ID(Client)))
1188 if (strcmp(Key, pass) != 0)
1196 } /* Channel_CheckKey */
1200 * Check wether a client is allowed to administer a channel or not.
1202 * @param Chan The channel to test.
1203 * @param Client The client from which the command has been received.
1204 * @param Origin The originator of the command (or NULL).
1205 * @param OnChannel Set to true if the originator is member of the channel.
1206 * @param AdminOk Set to true if the client is allowed to do
1207 * administrative tasks on this channel.
1208 * @param UseServerMode Set to true if ngIRCd should emulate "server mode",
1209 * that is send commands as if originating from a server
1210 * and not the originator of the command.
1213 Channel_CheckAdminRights(CHANNEL *Chan, CLIENT *Client, CLIENT *Origin,
1214 bool *OnChannel, bool *AdminOk, bool *UseServerMode)
1216 assert (Chan != NULL);
1217 assert (Client != NULL);
1218 assert (OnChannel != NULL);
1219 assert (AdminOk != NULL);
1220 assert (UseServerMode != NULL);
1222 /* Use the client as origin, if no origin has been given (no prefix?) */
1228 *UseServerMode = false;
1230 if (Client_Type(Client) != CLIENT_USER
1231 && Client_Type(Client) != CLIENT_SERVER
1232 && Client_Type(Client) != CLIENT_SERVICE)
1235 /* Allow channel administration if the client is a server or service */
1236 if (Client_Type(Client) != CLIENT_USER) {
1241 *OnChannel = Channel_IsMemberOf(Chan, Origin);
1243 if (*OnChannel && strchr(Channel_UserModes(Chan, Origin), 'o')) {
1244 /* User is a channel operator */
1246 } else if (Conf_OperCanMode) {
1247 /* IRC operators are allowed to administer channels as well */
1248 if (Client_OperByMe(Origin)) {
1250 if (Conf_OperServerMode)
1251 *UseServerMode = true;
1254 } /* Channel_CheckAdminRights */
1258 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1260 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1261 } /* Get_First_Cl2Chan */
1265 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1269 assert( Client != NULL || Channel != NULL );
1274 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1275 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1276 cl2chan = cl2chan->next;
1279 } /* Get_Next_Cl2Chan */
1283 * Remove a channel and free all of its data structures.
1286 Delete_Channel(CHANNEL *Chan)
1288 CHANNEL *chan, *last_chan;
1299 assert(chan != NULL);
1303 /* maintain channel list */
1305 last_chan->next = chan->next;
1307 My_Channels = chan->next;
1309 LogDebug("Freed channel structure for \"%s\".", Chan->name);
1311 } /* Delete_Channel */
1315 Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
1319 assert(Chan != NULL);
1320 assert(KeyFile != NULL);
1322 len = strlen(KeyFile);
1323 if (len < array_bytes(&Chan->keyfile)) {
1324 Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
1325 array_free(&Chan->keyfile);
1331 if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
1333 "Could not set new channel key file \"%s\" for %s: %s",
1334 KeyFile, Chan->name, strerror(errno));
1336 Log(LOG_INFO|LOG_snotice,
1337 "New local channel key file \"%s\" for %s activated.",
1338 KeyFile, Chan->name);