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,
297 char *ptr, *target_modes;
298 bool can_kick = false;
300 assert(Peer != NULL);
301 assert(Target != NULL);
302 assert(Origin != NULL);
303 assert(Name != NULL);
304 assert(Reason != NULL);
306 /* Check that channel exists */
307 chan = Channel_Search( Name );
310 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
314 if (Client_Type(Peer) != CLIENT_SERVER &&
315 Client_Type(Origin) != CLIENT_SERVICE) {
316 /* Check that user is on the specified channel */
317 if (!Channel_IsMemberOf(chan, Origin)) {
318 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG,
319 Client_ID(Origin), Name);
324 if(Client_Type(Peer) == CLIENT_USER) {
325 /* Check if client has the rights to kick target */
326 ptr = Channel_UserModes(chan, Peer);
327 target_modes = Channel_UserModes(chan, Target);
329 /* Owner can kick everyone */
334 /* Admin can't kick owner */
336 if (!strchr(target_modes, 'q')) {
341 /* Op can't kick owner | admin */
343 if (!strchr(target_modes, 'q') &&
344 !strchr(target_modes, 'a')) {
349 /* Half Op can't kick owner | admin | op */
351 if (!strchr(target_modes, 'q') &&
352 !strchr(target_modes, 'a') &&
353 !strchr(target_modes, 'o')) {
362 IRC_WriteStrClient(Origin, ERR_CHANOPPRIVTOLOW_MSG,
363 Client_ID(Origin), Name);
368 /* Check that the client to be kicked is on the specified channel */
369 if (!Channel_IsMemberOf(chan, Target)) {
370 IRC_WriteStrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
371 Client_ID(Origin), Client_ID(Target), Name );
375 /* Kick Client from channel */
376 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
381 Channel_Quit( CLIENT *Client, const char *Reason )
385 assert( Client != NULL );
386 assert( Reason != NULL );
388 if (Conf_MorePrivacy)
391 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
397 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
404 * Get number of channels this server knows and that are "visible" to
405 * the given client. If no client is given, all channels will be counted.
407 * @param Client The client to check or NULL.
408 * @return Number of channels visible to the client.
411 Channel_CountVisible (CLIENT *Client)
414 unsigned long count = 0;
419 if (!strchr(Channel_Modes(c), 's')
420 || Channel_IsMemberOf(c, Client))
431 Channel_MemberCount( CHANNEL *Chan )
434 unsigned long count = 0;
436 assert( Chan != NULL );
438 cl2chan = My_Cl2Chan;
441 if( cl2chan->channel == Chan ) count++;
442 cl2chan = cl2chan->next;
445 } /* Channel_MemberCount */
449 Channel_CountForUser( CLIENT *Client )
451 /* Count number of channels a user is member of. */
456 assert( Client != NULL );
458 cl2chan = My_Cl2Chan;
461 if( cl2chan->client == Client ) count++;
462 cl2chan = cl2chan->next;
466 } /* Channel_CountForUser */
470 Channel_Name( const CHANNEL *Chan )
472 assert( Chan != NULL );
478 Channel_Modes( CHANNEL *Chan )
480 assert( Chan != NULL );
482 } /* Channel_Modes */
486 Channel_Key( CHANNEL *Chan )
488 assert( Chan != NULL );
494 Channel_MaxUsers( CHANNEL *Chan )
496 assert( Chan != NULL );
497 return Chan->maxusers;
498 } /* Channel_MaxUsers */
502 Channel_First( void )
505 } /* Channel_First */
509 Channel_Next( CHANNEL *Chan )
511 assert( Chan != NULL );
517 Channel_Search( const char *Name )
519 /* Search channel structure */
524 assert( Name != NULL );
526 search_hash = Hash( Name );
530 if( search_hash == c->hash )
533 if( strcasecmp( Name, c->name ) == 0 ) return c;
538 } /* Channel_Search */
542 Channel_FirstMember( CHANNEL *Chan )
544 assert( Chan != NULL );
545 return Get_First_Cl2Chan( NULL, Chan );
546 } /* Channel_FirstMember */
550 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
552 assert( Chan != NULL );
553 assert( Cl2Chan != NULL );
554 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
555 } /* Channel_NextMember */
559 Channel_FirstChannelOf( CLIENT *Client )
561 assert( Client != NULL );
562 return Get_First_Cl2Chan( Client, NULL );
563 } /* Channel_FirstChannelOf */
567 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
569 assert( Client != NULL );
570 assert( Cl2Chan != NULL );
571 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
572 } /* Channel_NextChannelOf */
576 Channel_GetClient( CL2CHAN *Cl2Chan )
578 assert( Cl2Chan != NULL );
579 return Cl2Chan->client;
580 } /* Channel_GetClient */
584 Channel_GetChannel( CL2CHAN *Cl2Chan )
586 assert( Cl2Chan != NULL );
587 return Cl2Chan->channel;
588 } /* Channel_GetChannel */
592 Channel_IsValidName( const char *Name )
594 assert( Name != NULL );
597 if (strlen(Name) <= 1)
600 if (strchr("#&+", Name[0]) == NULL)
602 if (strlen(Name) >= CHANNEL_NAME_LEN)
605 return Name[strcspn(Name, " ,:\007")] == 0;
606 } /* Channel_IsValidName */
610 Channel_ModeAdd( CHANNEL *Chan, char Mode )
613 * If the channel already had this mode, return false.
614 * If the channel mode was newly set return true.
619 assert( Chan != NULL );
621 x[0] = Mode; x[1] = '\0';
622 if( ! strchr( Chan->modes, x[0] ))
624 /* Channel does not have this mode yet, set it */
625 strlcat( Chan->modes, x, sizeof( Chan->modes ));
629 } /* Channel_ModeAdd */
633 Channel_ModeDel( CHANNEL *Chan, char Mode )
636 * if the mode was removed return true.
637 * if the channel did not have the mode, return false.
641 assert( Chan != NULL );
643 p = strchr( Chan->modes, Mode );
644 if( ! p ) return false;
646 /* Channel has mode -> delete */
653 } /* Channel_ModeDel */
657 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
659 /* Set Channel-User-Mode.
660 * if mode was newly set, return true.
661 * if the User already had this channel-mode, return false.
667 assert( Chan != NULL );
668 assert( Client != NULL );
670 cl2chan = Get_Cl2Chan( Chan, Client );
671 assert( cl2chan != NULL );
673 x[0] = Mode; x[1] = '\0';
674 if( ! strchr( cl2chan->modes, x[0] ))
676 /* mode not set, -> set it */
677 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
681 } /* Channel_UserModeAdd */
685 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
687 /* Delete Channel-User-Mode.
688 * If Mode was removed, return true.
689 * If User did not have the Channel-Mode, return false.
695 assert( Chan != NULL );
696 assert( Client != NULL );
698 cl2chan = Get_Cl2Chan( Chan, Client );
699 assert( cl2chan != NULL );
701 p = strchr( cl2chan->modes, Mode );
702 if( ! p ) return false;
704 /* Client has Mode -> delete */
711 } /* Channel_UserModeDel */
715 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
717 /* return Users' Channel-Modes */
721 assert( Chan != NULL );
722 assert( Client != NULL );
724 cl2chan = Get_Cl2Chan( Chan, Client );
725 assert( cl2chan != NULL );
727 return cl2chan->modes;
728 } /* Channel_UserModes */
732 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
734 /* Test if Client is on Channel Chan */
736 assert( Chan != NULL );
737 assert( Client != NULL );
738 return Get_Cl2Chan(Chan, Client) != NULL;
739 } /* Channel_IsMemberOf */
743 Channel_Topic( CHANNEL *Chan )
746 assert( Chan != NULL );
747 ret = array_start(&Chan->topic);
748 return ret ? ret : "";
749 } /* Channel_Topic */
755 Channel_TopicTime(CHANNEL *Chan)
757 assert(Chan != NULL);
758 return (unsigned int) Chan->topic_time;
759 } /* Channel_TopicTime */
763 Channel_TopicWho(CHANNEL *Chan)
765 assert(Chan != NULL);
766 return Chan->topic_who;
767 } /* Channel_TopicWho */
771 Channel_CreationTime(CHANNEL *Chan)
773 assert(Chan != NULL);
774 return (unsigned int) Chan->creation_time;
775 } /* Channel_CreationTime */
781 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
784 assert( Chan != NULL );
785 assert( Topic != NULL );
788 if (len < array_bytes(&Chan->topic))
789 array_free(&Chan->topic);
791 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
792 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
793 Topic, Chan->name, strerror(errno));
795 Chan->topic_time = time(NULL);
796 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
797 strlcpy(Chan->topic_who, Client_ID(Client),
798 sizeof Chan->topic_who);
800 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
801 sizeof Chan->topic_who);
805 } /* Channel_SetTopic */
809 Channel_SetModes( CHANNEL *Chan, const char *Modes )
811 assert( Chan != NULL );
812 assert( Modes != NULL );
814 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
815 } /* Channel_SetModes */
819 Channel_SetKey( CHANNEL *Chan, const char *Key )
821 assert( Chan != NULL );
822 assert( Key != NULL );
824 strlcpy( Chan->key, Key, sizeof( Chan->key ));
825 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
826 } /* Channel_SetKey */
830 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
832 assert( Chan != NULL );
834 Chan->maxusers = Count;
835 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
836 } /* Channel_SetMaxUsers */
840 * Check if a client is allowed to send to a specific channel.
842 * @param Chan The channel to check.
843 * @param From The client that wants to send.
844 * @return true if the client is allowed to send, false otherwise.
847 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
849 bool is_member, has_voice, is_halfop, is_op, is_chanadmin, is_owner;
851 is_member = has_voice = is_halfop = is_op = is_chanadmin = is_owner = false;
853 /* The server itself always can send messages :-) */
854 if (Client_ThisServer() == From)
857 if (Channel_IsMemberOf(Chan, From)) {
859 if (strchr(Channel_UserModes(Chan, From), 'v'))
861 if (strchr(Channel_UserModes(Chan, From), 'h'))
863 if (strchr(Channel_UserModes(Chan, From), 'o'))
865 if (strchr(Channel_UserModes(Chan, From), 'a'))
867 if (strchr(Channel_UserModes(Chan, From), 'q'))
872 * Is the client allowed to write to channel?
874 * If channel mode n set: non-members cannot send to channel.
875 * If channel mode m set: need voice.
877 if (strchr(Channel_Modes(Chan), 'n') && !is_member)
880 if (has_voice || is_halfop || is_op || is_chanadmin || is_owner)
883 if (strchr(Channel_Modes(Chan), 'm'))
886 if (Lists_Check(&Chan->list_excepts, From))
889 return !Lists_Check(&Chan->list_bans, From);
894 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
895 bool SendErrors, const char *Text)
897 if (!Can_Send_To_Channel(Chan, From)) {
899 return CONNECTED; /* no error, see RFC 2812 */
900 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
901 Client_ID(From), Channel_Name(Chan));
904 if (Client_Conn(From) > NONE)
905 Conn_UpdateIdle(Client_Conn(From));
907 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
908 "%s %s :%s", Command, Channel_Name(Chan), Text);
913 Channel_Create( const char *Name )
915 /* Create new CHANNEL structure and add it to linked list */
918 assert( Name != NULL );
920 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
923 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
926 memset( c, 0, sizeof( CHANNEL ));
927 strlcpy( c->name, Name, sizeof( c->name ));
928 c->hash = Hash( c->name );
929 c->next = My_Channels;
931 c->creation_time = time(NULL);
934 LogDebug("Created new channel structure for \"%s\".", Name);
936 } /* Channel_Create */
940 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
944 assert( Chan != NULL );
945 assert( Client != NULL );
947 cl2chan = My_Cl2Chan;
950 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
951 cl2chan = cl2chan->next;
958 Add_Client( CHANNEL *Chan, CLIENT *Client )
962 assert( Chan != NULL );
963 assert( Client != NULL );
965 /* Create new CL2CHAN structure */
966 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
969 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
972 cl2chan->channel = Chan;
973 cl2chan->client = Client;
974 strcpy( cl2chan->modes, "" );
977 cl2chan->next = My_Cl2Chan;
978 My_Cl2Chan = cl2chan;
980 LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
987 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
989 CL2CHAN *cl2chan, *last_cl2chan;
992 assert( Chan != NULL );
993 assert( Client != NULL );
994 assert( Origin != NULL );
995 assert( Reason != NULL );
997 /* Do not inform other servers if the channel is local to this server,
998 * regardless of what the caller requested! */
1000 InformServer = !Channel_IsLocal(Chan);
1002 last_cl2chan = NULL;
1003 cl2chan = My_Cl2Chan;
1006 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
1007 last_cl2chan = cl2chan;
1008 cl2chan = cl2chan->next;
1010 if( ! cl2chan ) return false;
1012 c = cl2chan->channel;
1013 assert( c != NULL );
1015 /* maintain cl2chan list */
1016 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
1017 else My_Cl2Chan = cl2chan->next;
1023 /* QUIT: other servers have already been notified,
1024 * see Client_Destroy(); so only inform other clients
1025 * in same channel. */
1026 assert( InformServer == false );
1027 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1028 Client_Mask( Client ), c->name, Reason );
1031 /* User was KICKed: inform other servers (public
1032 * channels) and all users in the channel */
1034 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
1035 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
1036 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
1037 c->name, Client_ID( Client ), Reason );
1038 if ((Client_Conn(Client) > NONE) &&
1039 (Client_Type(Client) == CLIENT_USER))
1041 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
1042 c->name, Client_ID( Client ), Reason);
1044 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
1045 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
1048 if (Conf_MorePrivacy)
1052 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
1054 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
1057 if ((Client_Conn(Origin) > NONE) &&
1058 (Client_Type(Origin) == CLIENT_USER))
1060 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
1061 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1062 Client_Mask(Client), c->name, Reason);
1066 /* When channel is empty and is not pre-defined, delete */
1067 if( ! strchr( Channel_Modes( Chan ), 'P' ))
1069 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
1073 } /* Remove_Client */
1077 Channel_AddBan(CHANNEL *c, const char *mask )
1079 struct list_head *h = Channel_GetListBans(c);
1080 LogDebug("Adding \"%s\" to \"%s\" ban list", mask, Channel_Name(c));
1081 return Lists_Add(h, mask, false, NULL);
1086 Channel_AddExcept(CHANNEL *c, const char *mask )
1088 struct list_head *h = Channel_GetListExcepts(c);
1089 LogDebug("Adding \"%s\" to \"%s\" exception list", mask, Channel_Name(c));
1090 return Lists_Add(h, mask, false, NULL);
1095 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
1097 struct list_head *h = Channel_GetListInvites(c);
1098 LogDebug("Adding \"%s\" to \"%s\" invite list", mask, Channel_Name(c));
1099 return Lists_Add(h, mask, onlyonce, NULL);
1104 ShowChannelList(struct list_head *head, CLIENT *Client, CHANNEL *Channel,
1105 char *msg, char *msg_end)
1107 struct list_elem *e;
1109 assert (Client != NULL);
1110 assert (Channel != NULL);
1112 e = Lists_GetFirst(head);
1114 if (!IRC_WriteStrClient(Client, msg, Client_ID(Client),
1115 Channel_Name(Channel),
1117 return DISCONNECTED;
1118 e = Lists_GetNext(e);
1121 return IRC_WriteStrClient(Client, msg_end, Client_ID(Client),
1122 Channel_Name(Channel));
1127 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1129 struct list_head *h;
1131 assert( Channel != NULL );
1133 h = Channel_GetListBans(Channel);
1134 return ShowChannelList(h, Client, Channel, RPL_BANLIST_MSG,
1135 RPL_ENDOFBANLIST_MSG);
1140 Channel_ShowExcepts( CLIENT *Client, CHANNEL *Channel )
1142 struct list_head *h;
1144 assert( Channel != NULL );
1146 h = Channel_GetListExcepts(Channel);
1147 return ShowChannelList(h, Client, Channel, RPL_EXCEPTLIST_MSG,
1148 RPL_ENDOFEXCEPTLIST_MSG);
1153 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1155 struct list_head *h;
1157 assert( Channel != NULL );
1159 h = Channel_GetListInvites(Channel);
1160 return ShowChannelList(h, Client, Channel, RPL_INVITELIST_MSG,
1161 RPL_ENDOFINVITELIST_MSG);
1166 * Log a message to the local &SERVER channel, if it exists.
1169 Channel_LogServer(const char *msg)
1174 assert(msg != NULL);
1176 sc = Channel_Search("&SERVER");
1180 c = Client_ThisServer();
1181 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1182 } /* Channel_LogServer */
1186 Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
1188 char *file_name, line[COMMAND_LEN], *nick, *pass;
1191 assert(Chan != NULL);
1192 assert(Client != NULL);
1193 assert(Key != NULL);
1195 if (!strchr(Chan->modes, 'k'))
1199 if (strcmp(Chan->key, Key) == 0)
1202 file_name = array_start(&Chan->keyfile);
1205 fd = fopen(file_name, "r");
1207 Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
1208 file_name, Chan->name, strerror(errno));
1212 while (fgets(line, (int)sizeof(line), fd) != NULL) {
1214 if (! (nick = strchr(line, ':')))
1217 if (!Match(line, Client_User(Client)))
1219 if (! (pass = strchr(nick, ':')))
1222 if (!Match(nick, Client_ID(Client)))
1224 if (strcmp(Key, pass) != 0)
1232 } /* Channel_CheckKey */
1236 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1238 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1239 } /* Get_First_Cl2Chan */
1243 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1247 assert( Client != NULL || Channel != NULL );
1252 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1253 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1254 cl2chan = cl2chan->next;
1257 } /* Get_Next_Cl2Chan */
1261 * Remove a channel and free all of its data structures.
1264 Delete_Channel(CHANNEL *Chan)
1266 CHANNEL *chan, *last_chan;
1277 assert(chan != NULL);
1281 /* maintain channel list */
1283 last_chan->next = chan->next;
1285 My_Channels = chan->next;
1287 LogDebug("Freed channel structure for \"%s\".", Chan->name);
1289 } /* Delete_Channel */
1293 Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
1297 assert(Chan != NULL);
1298 assert(KeyFile != NULL);
1300 len = strlen(KeyFile);
1301 if (len < array_bytes(&Chan->keyfile)) {
1302 Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
1303 array_free(&Chan->keyfile);
1309 if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
1311 "Could not set new channel key file \"%s\" for %s: %s",
1312 KeyFile, Chan->name, strerror(errno));
1314 Log(LOG_INFO|LOG_snotice,
1315 "New local channel key file \"%s\" for %s activated.",
1316 KeyFile, Chan->name);