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,
302 char *ptr, *target_modes;
303 bool can_kick = false;
305 assert(Peer != NULL);
306 assert(Target != NULL);
307 assert(Origin != NULL);
308 assert(Name != NULL);
309 assert(Reason != NULL);
311 /* Check that channel exists */
312 chan = Channel_Search( Name );
315 IRC_WriteStrClient( Origin, ERR_NOSUCHCHANNEL_MSG, Client_ID( Origin ), Name );
319 if (Client_Type(Peer) != CLIENT_SERVER &&
320 Client_Type(Origin) != CLIENT_SERVICE) {
321 /* Check that user is on the specified channel */
322 if (!Channel_IsMemberOf(chan, Origin)) {
323 IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG,
324 Client_ID(Origin), Name);
329 if(Client_Type(Peer) == CLIENT_USER) {
330 /* Channel mode 'Q' and user mode 'q' on target: nobody but
331 * IRC Operators and servers can kick the target user */
332 if ((strchr(Channel_Modes(chan), 'Q')
333 || Client_HasMode(Target, 'q'))
334 && !Client_HasMode(Origin, 'o')) {
335 IRC_WriteStrClient(Origin, ERR_KICKDENY_MSG,
336 Client_ID(Origin), Name,
341 /* Check if client has the rights to kick target */
342 ptr = Channel_UserModes(chan, Peer);
343 target_modes = Channel_UserModes(chan, Target);
345 /* Owner can kick everyone */
350 /* Admin can't kick owner */
352 if (!strchr(target_modes, 'q')) {
357 /* Op can't kick owner | admin */
359 if (!strchr(target_modes, 'q') &&
360 !strchr(target_modes, 'a')) {
365 /* Half Op can't kick owner | admin | op */
367 if (!strchr(target_modes, 'q') &&
368 !strchr(target_modes, 'a') &&
369 !strchr(target_modes, 'o')) {
378 IRC_WriteStrClient(Origin, ERR_CHANOPPRIVTOOLOW_MSG,
379 Client_ID(Origin), Name);
384 /* Check that the client to be kicked is on the specified channel */
385 if (!Channel_IsMemberOf(chan, Target)) {
386 IRC_WriteStrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
387 Client_ID(Origin), Client_ID(Target), Name );
391 /* Kick Client from channel */
392 Remove_Client( REMOVE_KICK, chan, Target, Origin, Reason, true);
397 Channel_Quit( CLIENT *Client, const char *Reason )
401 assert( Client != NULL );
402 assert( Reason != NULL );
404 if (Conf_MorePrivacy)
407 IRC_WriteStrRelatedPrefix( Client, Client, false, "QUIT :%s", Reason );
413 Remove_Client( REMOVE_QUIT, c, Client, Client, Reason, false );
420 * Get number of channels this server knows and that are "visible" to
421 * the given client. If no client is given, all channels will be counted.
423 * @param Client The client to check or NULL.
424 * @return Number of channels visible to the client.
427 Channel_CountVisible (CLIENT *Client)
430 unsigned long count = 0;
435 if (!strchr(Channel_Modes(c), 's')
436 || Channel_IsMemberOf(c, Client))
447 Channel_MemberCount( CHANNEL *Chan )
450 unsigned long count = 0;
452 assert( Chan != NULL );
454 cl2chan = My_Cl2Chan;
457 if( cl2chan->channel == Chan ) count++;
458 cl2chan = cl2chan->next;
461 } /* Channel_MemberCount */
465 Channel_CountForUser( CLIENT *Client )
467 /* Count number of channels a user is member of. */
472 assert( Client != NULL );
474 cl2chan = My_Cl2Chan;
477 if( cl2chan->client == Client ) count++;
478 cl2chan = cl2chan->next;
482 } /* Channel_CountForUser */
486 Channel_Name( const CHANNEL *Chan )
488 assert( Chan != NULL );
494 Channel_Modes( CHANNEL *Chan )
496 assert( Chan != NULL );
498 } /* Channel_Modes */
502 Channel_Key( CHANNEL *Chan )
504 assert( Chan != NULL );
510 Channel_MaxUsers( CHANNEL *Chan )
512 assert( Chan != NULL );
513 return Chan->maxusers;
514 } /* Channel_MaxUsers */
518 Channel_First( void )
521 } /* Channel_First */
525 Channel_Next( CHANNEL *Chan )
527 assert( Chan != NULL );
533 Channel_Search( const char *Name )
535 /* Search channel structure */
540 assert( Name != NULL );
542 search_hash = Hash( Name );
546 if( search_hash == c->hash )
549 if( strcasecmp( Name, c->name ) == 0 ) return c;
554 } /* Channel_Search */
558 Channel_FirstMember( CHANNEL *Chan )
560 assert( Chan != NULL );
561 return Get_First_Cl2Chan( NULL, Chan );
562 } /* Channel_FirstMember */
566 Channel_NextMember( CHANNEL *Chan, CL2CHAN *Cl2Chan )
568 assert( Chan != NULL );
569 assert( Cl2Chan != NULL );
570 return Get_Next_Cl2Chan( Cl2Chan->next, NULL, Chan );
571 } /* Channel_NextMember */
575 Channel_FirstChannelOf( CLIENT *Client )
577 assert( Client != NULL );
578 return Get_First_Cl2Chan( Client, NULL );
579 } /* Channel_FirstChannelOf */
583 Channel_NextChannelOf( CLIENT *Client, CL2CHAN *Cl2Chan )
585 assert( Client != NULL );
586 assert( Cl2Chan != NULL );
587 return Get_Next_Cl2Chan( Cl2Chan->next, Client, NULL );
588 } /* Channel_NextChannelOf */
592 Channel_GetClient( CL2CHAN *Cl2Chan )
594 assert( Cl2Chan != NULL );
595 return Cl2Chan->client;
596 } /* Channel_GetClient */
600 Channel_GetChannel( CL2CHAN *Cl2Chan )
602 assert( Cl2Chan != NULL );
603 return Cl2Chan->channel;
604 } /* Channel_GetChannel */
608 Channel_IsValidName( const char *Name )
610 assert( Name != NULL );
613 if (strlen(Name) <= 1)
616 if (strchr("#&+", Name[0]) == NULL)
618 if (strlen(Name) >= CHANNEL_NAME_LEN)
621 return Name[strcspn(Name, " ,:\007")] == 0;
622 } /* Channel_IsValidName */
626 Channel_ModeAdd( CHANNEL *Chan, char Mode )
629 * If the channel already had this mode, return false.
630 * If the channel mode was newly set return true.
635 assert( Chan != NULL );
637 x[0] = Mode; x[1] = '\0';
638 if( ! strchr( Chan->modes, x[0] ))
640 /* Channel does not have this mode yet, set it */
641 strlcat( Chan->modes, x, sizeof( Chan->modes ));
645 } /* Channel_ModeAdd */
649 Channel_ModeDel( CHANNEL *Chan, char Mode )
652 * if the mode was removed return true.
653 * if the channel did not have the mode, return false.
657 assert( Chan != NULL );
659 p = strchr( Chan->modes, Mode );
660 if( ! p ) return false;
662 /* Channel has mode -> delete */
669 } /* Channel_ModeDel */
673 Channel_UserModeAdd( CHANNEL *Chan, CLIENT *Client, char Mode )
675 /* Set Channel-User-Mode.
676 * if mode was newly set, return true.
677 * if the User already had this channel-mode, return false.
683 assert( Chan != NULL );
684 assert( Client != NULL );
686 cl2chan = Get_Cl2Chan( Chan, Client );
687 assert( cl2chan != NULL );
689 x[0] = Mode; x[1] = '\0';
690 if( ! strchr( cl2chan->modes, x[0] ))
692 /* mode not set, -> set it */
693 strlcat( cl2chan->modes, x, sizeof( cl2chan->modes ));
697 } /* Channel_UserModeAdd */
701 Channel_UserModeDel( CHANNEL *Chan, CLIENT *Client, char Mode )
703 /* Delete Channel-User-Mode.
704 * If Mode was removed, return true.
705 * If User did not have the Channel-Mode, return false.
711 assert( Chan != NULL );
712 assert( Client != NULL );
714 cl2chan = Get_Cl2Chan( Chan, Client );
715 assert( cl2chan != NULL );
717 p = strchr( cl2chan->modes, Mode );
718 if( ! p ) return false;
720 /* Client has Mode -> delete */
727 } /* Channel_UserModeDel */
731 Channel_UserModes( CHANNEL *Chan, CLIENT *Client )
733 /* return Users' Channel-Modes */
737 assert( Chan != NULL );
738 assert( Client != NULL );
740 cl2chan = Get_Cl2Chan( Chan, Client );
741 assert( cl2chan != NULL );
743 return cl2chan->modes;
744 } /* Channel_UserModes */
748 Channel_IsMemberOf( CHANNEL *Chan, CLIENT *Client )
750 /* Test if Client is on Channel Chan */
752 assert( Chan != NULL );
753 assert( Client != NULL );
754 return Get_Cl2Chan(Chan, Client) != NULL;
755 } /* Channel_IsMemberOf */
759 Channel_Topic( CHANNEL *Chan )
762 assert( Chan != NULL );
763 ret = array_start(&Chan->topic);
764 return ret ? ret : "";
765 } /* Channel_Topic */
771 Channel_TopicTime(CHANNEL *Chan)
773 assert(Chan != NULL);
774 return (unsigned int) Chan->topic_time;
775 } /* Channel_TopicTime */
779 Channel_TopicWho(CHANNEL *Chan)
781 assert(Chan != NULL);
782 return Chan->topic_who;
783 } /* Channel_TopicWho */
787 Channel_CreationTime(CHANNEL *Chan)
789 assert(Chan != NULL);
790 return (unsigned int) Chan->creation_time;
791 } /* Channel_CreationTime */
797 Channel_SetTopic(CHANNEL *Chan, CLIENT *Client, const char *Topic)
800 assert( Chan != NULL );
801 assert( Topic != NULL );
804 if (len < array_bytes(&Chan->topic))
805 array_free(&Chan->topic);
807 if (len >= COMMAND_LEN || !array_copyb(&Chan->topic, Topic, len+1))
808 Log(LOG_WARNING, "could not set new Topic \"%s\" on %s: %s",
809 Topic, Chan->name, strerror(errno));
811 Chan->topic_time = time(NULL);
812 if (Client != NULL && Client_Type(Client) != CLIENT_SERVER)
813 strlcpy(Chan->topic_who, Client_ID(Client),
814 sizeof Chan->topic_who);
816 strlcpy(Chan->topic_who, DEFAULT_TOPIC_ID,
817 sizeof Chan->topic_who);
821 } /* Channel_SetTopic */
825 Channel_SetModes( CHANNEL *Chan, const char *Modes )
827 assert( Chan != NULL );
828 assert( Modes != NULL );
830 strlcpy( Chan->modes, Modes, sizeof( Chan->modes ));
831 } /* Channel_SetModes */
835 Channel_SetKey( CHANNEL *Chan, const char *Key )
837 assert( Chan != NULL );
838 assert( Key != NULL );
840 strlcpy( Chan->key, Key, sizeof( Chan->key ));
841 LogDebug("Channel %s: Key is now \"%s\".", Chan->name, Chan->key );
842 } /* Channel_SetKey */
846 Channel_SetMaxUsers(CHANNEL *Chan, unsigned long Count)
848 assert( Chan != NULL );
850 Chan->maxusers = Count;
851 LogDebug("Channel %s: Member limit is now %lu.", Chan->name, Chan->maxusers );
852 } /* Channel_SetMaxUsers */
856 * Check if a client is allowed to send to a specific channel.
858 * @param Chan The channel to check.
859 * @param From The client that wants to send.
860 * @return true if the client is allowed to send, false otherwise.
863 Can_Send_To_Channel(CHANNEL *Chan, CLIENT *From)
865 bool is_member, has_voice, is_halfop, is_op, is_chanadmin, is_owner;
867 is_member = has_voice = is_halfop = is_op = is_chanadmin = is_owner = false;
869 /* The server itself always can send messages :-) */
870 if (Client_ThisServer() == From)
873 if (Channel_IsMemberOf(Chan, From)) {
875 if (strchr(Channel_UserModes(Chan, From), 'v'))
877 if (strchr(Channel_UserModes(Chan, From), 'h'))
879 if (strchr(Channel_UserModes(Chan, From), 'o'))
881 if (strchr(Channel_UserModes(Chan, From), 'a'))
883 if (strchr(Channel_UserModes(Chan, From), 'q'))
888 * Is the client allowed to write to channel?
890 * If channel mode n set: non-members cannot send to channel.
891 * If channel mode m set: need voice.
893 if (strchr(Channel_Modes(Chan), 'n') && !is_member)
896 if (strchr(Channel_Modes(Chan), 'M') && !Client_HasMode(From, 'R')
897 && !Client_HasMode(From, 'o'))
900 if (has_voice || is_halfop || is_op || is_chanadmin || is_owner)
903 if (strchr(Channel_Modes(Chan), 'm'))
906 if (Lists_Check(&Chan->list_excepts, From))
909 return !Lists_Check(&Chan->list_bans, From);
914 Channel_Write(CHANNEL *Chan, CLIENT *From, CLIENT *Client, const char *Command,
915 bool SendErrors, const char *Text)
917 if (!Can_Send_To_Channel(Chan, From)) {
919 return CONNECTED; /* no error, see RFC 2812 */
920 return IRC_WriteStrClient(From, ERR_CANNOTSENDTOCHAN_MSG,
921 Client_ID(From), Channel_Name(Chan));
924 if (Client_Conn(From) > NONE)
925 Conn_UpdateIdle(Client_Conn(From));
927 return IRC_WriteStrChannelPrefix(Client, Chan, From, true,
928 "%s %s :%s", Command, Channel_Name(Chan), Text);
933 Channel_Create( const char *Name )
935 /* Create new CHANNEL structure and add it to linked list */
938 assert( Name != NULL );
940 c = (CHANNEL *)malloc( sizeof( CHANNEL ));
943 Log( LOG_EMERG, "Can't allocate memory! [New_Chan]" );
946 memset( c, 0, sizeof( CHANNEL ));
947 strlcpy( c->name, Name, sizeof( c->name ));
948 c->hash = Hash( c->name );
949 c->next = My_Channels;
951 c->creation_time = time(NULL);
954 LogDebug("Created new channel structure for \"%s\".", Name);
956 } /* Channel_Create */
960 Get_Cl2Chan( CHANNEL *Chan, CLIENT *Client )
964 assert( Chan != NULL );
965 assert( Client != NULL );
967 cl2chan = My_Cl2Chan;
970 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) return cl2chan;
971 cl2chan = cl2chan->next;
978 Add_Client( CHANNEL *Chan, CLIENT *Client )
982 assert( Chan != NULL );
983 assert( Client != NULL );
985 /* Create new CL2CHAN structure */
986 cl2chan = (CL2CHAN *)malloc( sizeof( CL2CHAN ));
989 Log( LOG_EMERG, "Can't allocate memory! [Add_Client]" );
992 cl2chan->channel = Chan;
993 cl2chan->client = Client;
994 strcpy( cl2chan->modes, "" );
997 cl2chan->next = My_Cl2Chan;
998 My_Cl2Chan = cl2chan;
1000 LogDebug("User \"%s\" joined channel \"%s\".", Client_Mask(Client), Chan->name);
1007 Remove_Client( int Type, CHANNEL *Chan, CLIENT *Client, CLIENT *Origin, const char *Reason, bool InformServer )
1009 CL2CHAN *cl2chan, *last_cl2chan;
1012 assert( Chan != NULL );
1013 assert( Client != NULL );
1014 assert( Origin != NULL );
1015 assert( Reason != NULL );
1017 /* Do not inform other servers if the channel is local to this server,
1018 * regardless of what the caller requested! */
1020 InformServer = !Channel_IsLocal(Chan);
1022 last_cl2chan = NULL;
1023 cl2chan = My_Cl2Chan;
1026 if(( cl2chan->channel == Chan ) && ( cl2chan->client == Client )) break;
1027 last_cl2chan = cl2chan;
1028 cl2chan = cl2chan->next;
1030 if( ! cl2chan ) return false;
1032 c = cl2chan->channel;
1033 assert( c != NULL );
1035 /* maintain cl2chan list */
1036 if( last_cl2chan ) last_cl2chan->next = cl2chan->next;
1037 else My_Cl2Chan = cl2chan->next;
1043 /* QUIT: other servers have already been notified,
1044 * see Client_Destroy(); so only inform other clients
1045 * in same channel. */
1046 assert( InformServer == false );
1047 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1048 Client_Mask( Client ), c->name, Reason );
1051 /* User was KICKed: inform other servers (public
1052 * channels) and all users in the channel */
1054 IRC_WriteStrServersPrefix( Client_NextHop( Origin ),
1055 Origin, "KICK %s %s :%s", c->name, Client_ID( Client ), Reason);
1056 IRC_WriteStrChannelPrefix(Client, c, Origin, false, "KICK %s %s :%s",
1057 c->name, Client_ID( Client ), Reason );
1058 if ((Client_Conn(Client) > NONE) &&
1059 (Client_Type(Client) == CLIENT_USER))
1061 IRC_WriteStrClientPrefix(Client, Origin, "KICK %s %s :%s",
1062 c->name, Client_ID( Client ), Reason);
1064 LogDebug("User \"%s\" has been kicked off \"%s\" by \"%s\": %s.",
1065 Client_Mask( Client ), c->name, Client_ID(Origin), Reason);
1068 if (Conf_MorePrivacy)
1072 IRC_WriteStrServersPrefix(Origin, Client, "PART %s :%s", c->name, Reason);
1074 IRC_WriteStrChannelPrefix(Origin, c, Client, false, "PART %s :%s",
1077 if ((Client_Conn(Origin) > NONE) &&
1078 (Client_Type(Origin) == CLIENT_USER))
1080 IRC_WriteStrClientPrefix( Origin, Client, "PART %s :%s", c->name, Reason);
1081 LogDebug("User \"%s\" left channel \"%s\" (%s).",
1082 Client_Mask(Client), c->name, Reason);
1086 /* When channel is empty and is not pre-defined, delete */
1087 if( ! strchr( Channel_Modes( Chan ), 'P' ))
1089 if( ! Get_First_Cl2Chan( NULL, Chan )) Delete_Channel( Chan );
1093 } /* Remove_Client */
1097 Channel_AddBan(CHANNEL *c, const char *mask )
1099 struct list_head *h = Channel_GetListBans(c);
1100 LogDebug("Adding \"%s\" to \"%s\" ban list", mask, Channel_Name(c));
1101 return Lists_Add(h, mask, false, NULL);
1106 Channel_AddExcept(CHANNEL *c, const char *mask )
1108 struct list_head *h = Channel_GetListExcepts(c);
1109 LogDebug("Adding \"%s\" to \"%s\" exception list", mask, Channel_Name(c));
1110 return Lists_Add(h, mask, false, NULL);
1115 Channel_AddInvite(CHANNEL *c, const char *mask, bool onlyonce)
1117 struct list_head *h = Channel_GetListInvites(c);
1118 LogDebug("Adding \"%s\" to \"%s\" invite list", mask, Channel_Name(c));
1119 return Lists_Add(h, mask, onlyonce, NULL);
1124 ShowChannelList(struct list_head *head, CLIENT *Client, CHANNEL *Channel,
1125 char *msg, char *msg_end)
1127 struct list_elem *e;
1129 assert (Client != NULL);
1130 assert (Channel != NULL);
1132 e = Lists_GetFirst(head);
1134 if (!IRC_WriteStrClient(Client, msg, Client_ID(Client),
1135 Channel_Name(Channel),
1137 return DISCONNECTED;
1138 e = Lists_GetNext(e);
1141 return IRC_WriteStrClient(Client, msg_end, Client_ID(Client),
1142 Channel_Name(Channel));
1147 Channel_ShowBans( CLIENT *Client, CHANNEL *Channel )
1149 struct list_head *h;
1151 assert( Channel != NULL );
1153 h = Channel_GetListBans(Channel);
1154 return ShowChannelList(h, Client, Channel, RPL_BANLIST_MSG,
1155 RPL_ENDOFBANLIST_MSG);
1160 Channel_ShowExcepts( CLIENT *Client, CHANNEL *Channel )
1162 struct list_head *h;
1164 assert( Channel != NULL );
1166 h = Channel_GetListExcepts(Channel);
1167 return ShowChannelList(h, Client, Channel, RPL_EXCEPTLIST_MSG,
1168 RPL_ENDOFEXCEPTLIST_MSG);
1173 Channel_ShowInvites( CLIENT *Client, CHANNEL *Channel )
1175 struct list_head *h;
1177 assert( Channel != NULL );
1179 h = Channel_GetListInvites(Channel);
1180 return ShowChannelList(h, Client, Channel, RPL_INVITELIST_MSG,
1181 RPL_ENDOFINVITELIST_MSG);
1186 * Log a message to the local &SERVER channel, if it exists.
1189 Channel_LogServer(const char *msg)
1194 assert(msg != NULL);
1196 sc = Channel_Search("&SERVER");
1200 c = Client_ThisServer();
1201 Channel_Write(sc, c, c, "PRIVMSG", false, msg);
1202 } /* Channel_LogServer */
1206 Channel_CheckKey(CHANNEL *Chan, CLIENT *Client, const char *Key)
1208 char *file_name, line[COMMAND_LEN], *nick, *pass;
1211 assert(Chan != NULL);
1212 assert(Client != NULL);
1213 assert(Key != NULL);
1215 if (!strchr(Chan->modes, 'k'))
1219 if (strcmp(Chan->key, Key) == 0)
1222 file_name = array_start(&Chan->keyfile);
1225 fd = fopen(file_name, "r");
1227 Log(LOG_ERR, "Can't open channel key file \"%s\" for %s: %s",
1228 file_name, Chan->name, strerror(errno));
1232 while (fgets(line, (int)sizeof(line), fd) != NULL) {
1234 if (! (nick = strchr(line, ':')))
1237 if (!Match(line, Client_User(Client)))
1239 if (! (pass = strchr(nick, ':')))
1242 if (!Match(nick, Client_ID(Client)))
1244 if (strcmp(Key, pass) != 0)
1252 } /* Channel_CheckKey */
1256 Get_First_Cl2Chan( CLIENT *Client, CHANNEL *Chan )
1258 return Get_Next_Cl2Chan( My_Cl2Chan, Client, Chan );
1259 } /* Get_First_Cl2Chan */
1263 Get_Next_Cl2Chan( CL2CHAN *Start, CLIENT *Client, CHANNEL *Channel )
1267 assert( Client != NULL || Channel != NULL );
1272 if(( Client ) && ( cl2chan->client == Client )) return cl2chan;
1273 if(( Channel ) && ( cl2chan->channel == Channel )) return cl2chan;
1274 cl2chan = cl2chan->next;
1277 } /* Get_Next_Cl2Chan */
1281 * Remove a channel and free all of its data structures.
1284 Delete_Channel(CHANNEL *Chan)
1286 CHANNEL *chan, *last_chan;
1297 assert(chan != NULL);
1301 /* maintain channel list */
1303 last_chan->next = chan->next;
1305 My_Channels = chan->next;
1307 LogDebug("Freed channel structure for \"%s\".", Chan->name);
1309 } /* Delete_Channel */
1313 Set_KeyFile(CHANNEL *Chan, const char *KeyFile)
1317 assert(Chan != NULL);
1318 assert(KeyFile != NULL);
1320 len = strlen(KeyFile);
1321 if (len < array_bytes(&Chan->keyfile)) {
1322 Log(LOG_INFO, "Channel key file of %s removed.", Chan->name);
1323 array_free(&Chan->keyfile);
1329 if (!array_copyb(&Chan->keyfile, KeyFile, len+1))
1331 "Could not set new channel key file \"%s\" for %s: %s",
1332 KeyFile, Chan->name, strerror(errno));
1334 Log(LOG_INFO|LOG_snotice,
1335 "New local channel key file \"%s\" for %s activated.",
1336 KeyFile, Chan->name);