2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
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.
11 * IRC channel commands
17 static char UNUSED id[] = "$Id: irc-channel.c,v 1.45 2008/02/24 18:57:38 fw Exp $";
35 #include "irc-write.h"
40 #include "irc-channel.h"
44 * RFC 2812, (3.2.1 Join message Command):
45 * Note that this message
46 * accepts a special argument ("0"), which is a special request to leave all
47 * channels the user is currently a member of. The server will process this
48 * message as if the user had sent a PART command (See Section 3.2.2) for
49 * each channel he is a member of.
52 part_from_all_channels(CLIENT* client, CLIENT *target)
57 while ((cl2chan = Channel_FirstChannelOf(target))) {
58 chan = Channel_GetChannel(cl2chan);
59 assert( chan != NULL );
60 Channel_Part(target, client, Channel_Name(chan), Client_ID(target));
67 join_allowed(CLIENT *Client, CLIENT *target, CHANNEL *chan,
68 const char *channame, const char *key)
70 bool is_invited, is_banned;
71 const char *channel_modes;
73 is_banned = Lists_Check(Channel_GetListBans(chan), target);
74 is_invited = Lists_Check(Channel_GetListInvites(chan), target);
76 if (is_banned && !is_invited) {
77 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
81 channel_modes = Channel_Modes(chan);
82 if ((strchr(channel_modes, 'i')) && !is_invited) {
83 /* Channel is "invite-only" (and Client wasn't invited) */
84 IRC_WriteStrClient(Client, ERR_INVITEONLYCHAN_MSG, Client_ID(Client), channame);
88 /* Is the channel protected by a key? */
89 if (strchr(channel_modes, 'k') &&
90 strcmp(Channel_Key(chan), key ? key : ""))
92 IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG, Client_ID(Client), channame);
95 /* Are there already too many members? */
96 if ((strchr(channel_modes, 'l')) && (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
97 IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG, Client_ID(Client), channame);
105 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
109 Channel_UserModeAdd(chan, target, *flags);
114 /* If channel persistent and client is ircop: make client chanop */
115 if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
116 Channel_UserModeAdd(chan, target, 'o');
121 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
122 const char *channame)
126 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
132 /* forward to other servers */
133 IRC_WriteStrServersPrefix(Client, target, "JOIN :%s%s", channame, modes);
135 /* tell users in this channel about the new client */
136 IRC_WriteStrChannelPrefix(Client, chan, target, false, "JOIN :%s", channame);
138 IRC_WriteStrChannelPrefix(Client, chan, target, false, "MODE %s +%s %s",
139 channame, &modes[1], Client_ID(target));
144 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
145 const char *channame)
149 if (Client_Type(Client) != CLIENT_USER)
151 /* acknowledge join */
152 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
155 /* Send topic to client, if any */
156 topic = Channel_Topic(chan);
157 assert(topic != NULL);
159 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
160 Client_ID(Client), channame, topic))
163 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
164 Client_ID(Client), channame,
165 Channel_TopicWho(chan),
166 Channel_TopicTime(chan)))
170 /* send list of channel members to client */
171 if (!IRC_Send_NAMES(Client, chan))
173 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client), Channel_Name(chan));
178 IRC_JOIN( CLIENT *Client, REQUEST *Req )
180 char *channame, *channame_ptr, *key, *key_ptr, *flags;
184 assert( Client != NULL );
185 assert( Req != NULL );
187 /* Bad number of arguments? */
188 if (Req->argc < 1 || Req->argc > 2)
189 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
190 Client_ID(Client), Req->command);
192 /* Who is the sender? */
193 if (Client_Type(Client) == CLIENT_SERVER)
194 target = Client_Search(Req->prefix);
199 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
201 /* Is argument "0"? */
202 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
203 return part_from_all_channels(Client, target);
205 /* Are channel keys given? */
208 key_ptr = strchr(key, ',');
209 if (key_ptr) *key_ptr = '\0';
211 key = key_ptr = NULL;
213 channame = Req->argv[0];
214 channame_ptr = strchr(channame, ',');
215 if (channame_ptr) *channame_ptr = '\0';
220 /* Did the server include channel-user-modes? */
221 if (Client_Type(Client) == CLIENT_SERVER) {
222 flags = strchr(channame, 0x7);
229 chan = Channel_Search(channame);
230 if (!chan && Conf_PredefChannelsOnly) {
231 /* channel must be created, but server does not allow this */
232 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
237 if (Client_Type(Client) == CLIENT_USER) {
238 /* Test if the user has reached his maximum channel count */
239 if ((Conf_MaxJoins > 0) && (Channel_CountForUser(Client) >= Conf_MaxJoins))
240 return IRC_WriteStrClient(Client, ERR_TOOMANYCHANNELS_MSG,
241 Client_ID(Client), channame);
242 if (!chan) /* New Channel: first user will be channel operator */
245 if (!join_allowed(Client, target, chan, channame, key))
248 /* Remote server: we don't need to know whether the
249 * client is invited or not, but we have to make sure
250 * that the "one shot" entries (generated by INVITE
251 * commands) in this list become deleted when a user
252 * joins a channel this way. */
253 if (chan) (void)Lists_Check(Channel_GetListInvites(chan), target);
256 /* Join channel (and create channel if it doesn't exist) */
257 if (!Channel_Join(target, channame))
260 if (!chan) /* channel is new; it has been created above */
261 chan = Channel_Search(channame);
262 assert(chan != NULL);
264 join_set_channelmodes(chan, target, flags);
266 join_forward(Client, target, chan, channame);
268 if (!join_send_topic(Client, target, chan, channame))
269 break; /* write error */
272 channame = channame_ptr;
275 channame_ptr = strchr(channame, ',');
276 if (channame_ptr) *channame_ptr = '\0';
280 key_ptr = strchr(key, ',');
281 if (key_ptr) *key_ptr = '\0';
290 IRC_PART( CLIENT *Client, REQUEST *Req )
295 assert( Client != NULL );
296 assert( Req != NULL );
298 if (Req->argc < 1 || Req->argc > 2)
299 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
300 Client_ID(Client), Req->command);
302 /* Wer ist der Absender? */
303 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
304 else target = Client;
305 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
307 /* Channel-Namen durchgehen */
308 chan = strtok(Req->argv[0], ",");
310 Channel_Part(target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID(target));
312 chan = strtok(NULL, ",");
319 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
326 assert( Client != NULL );
327 assert( Req != NULL );
329 /* Falsche Anzahl Parameter? */
330 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
332 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
334 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
336 /* Welcher Channel? */
337 chan = Channel_Search( Req->argv[0] );
338 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
340 /* Ist der User Mitglied in dem Channel? */
341 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
345 /* Request actual topic */
346 topic = Channel_Topic(chan);
348 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
349 Client_ID(Client), Channel_Name(chan), topic);
351 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
352 Client_ID(Client), Channel_Name(chan),
353 Channel_TopicWho(chan),
354 Channel_TopicTime(chan));
359 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
360 Client_ID(from), Channel_Name(chan));
363 if( strchr( Channel_Modes( chan ), 't' ))
365 /* Topic Lock. Ist der User ein Channel Operator? */
366 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
370 Channel_SetTopic(chan, from, Req->argv[1]);
371 Log(LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s",
372 Client_Mask(from), Channel_Name(chan),
373 Req->argv[1][0] ? Req->argv[1] : "<none>");
375 /* im Channel bekannt machen und an Server weiterleiten */
376 IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
377 IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
379 if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
380 else return CONNECTED;
385 * Handler for the IRC "LIST" command.
386 * This implementation handles the local case as well as the forwarding of the
387 * LIST command to other servers in the IRC network.
390 IRC_LIST( CLIENT *Client, REQUEST *Req )
394 CLIENT *from, *target;
396 assert( Client != NULL );
397 assert( Req != NULL );
399 /* Bad number of prameters? */
401 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
402 Client_ID( Client ), Req->command );
405 pattern = strtok( Req->argv[0], "," );
409 /* Get sender from prefix, if any */
410 if( Client_Type( Client ) == CLIENT_SERVER )
411 from = Client_Search( Req->prefix );
416 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
417 Client_ID( Client ), Req->prefix );
421 /* Forward to other server? */
422 target = Client_Search( Req->argv[1] );
423 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
424 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
425 Client_ID( Client ), Req->argv[1] );
427 if( target != Client_ThisServer( ))
429 /* Target is indeed an other server, forward it! */
430 return IRC_WriteStrClientPrefix( target, from,
431 "LIST %s :%s", Client_ID( from ),
438 /* Loop through all the channels */
439 chan = Channel_First( );
442 /* Check search pattern */
443 if( Match( pattern, Channel_Name( chan )))
446 if( ! strchr( Channel_Modes( chan ), 's' ) ||
447 Channel_IsMemberOf( chan, from ))
449 if( ! IRC_WriteStrClient( from,
450 RPL_LIST_MSG, Client_ID( from ),
451 Channel_Name( chan ),
452 Channel_MemberCount( chan ),
453 Channel_Topic( chan )))
457 chan = Channel_Next( chan );
460 /* Get next name ... */
462 pattern = strtok( NULL, "," );
467 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
472 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
474 char modes_add[COMMAND_LEN], l[16], *ptr;
479 assert( Client != NULL );
480 assert( Req != NULL );
482 /* Bad number of parameters? */
483 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
485 /* Compatibility kludge */
486 if( Req->argc == 5 ) arg_topic = 4;
487 else if( Req->argc == 3 ) arg_topic = 2;
491 from = Client_Search( Req->prefix );
492 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
494 /* Search or create channel */
495 chan = Channel_Search( Req->argv[0] );
496 if( ! chan ) chan = Channel_Create( Req->argv[0] );
497 if( ! chan ) return CONNECTED;
499 if( Req->argv[1][0] == '+' )
501 ptr = Channel_Modes( chan );
504 /* OK, this channel doesn't have modes jet, set the received ones: */
505 Channel_SetModes( chan, &Req->argv[1][1] );
509 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
510 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
514 /* Delete modes which we never want to inherit */
515 Channel_ModeDel( chan, 'l' );
516 Channel_ModeDel( chan, 'k' );
519 strcpy( modes_add, "" );
520 ptr = Channel_Modes( chan );
525 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
526 strlcat( modes_add, l, sizeof( modes_add ));
530 strlcat( modes_add, " ", sizeof( modes_add ));
531 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
536 /* Inform members of this channel */
537 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
540 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
545 ptr = Channel_Topic( chan );
546 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
548 /* OK, there is no topic jet */
549 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
550 IRC_WriteStrChannelPrefix(Client, chan, from, false,
551 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
555 /* Forward CHANINFO to other serevrs */
556 if( Req->argc == 5 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2], Req->argv[3], Req->argv[4] );
557 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
558 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );