2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2010 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
27 #include "conn-func.h"
34 #include "irc-write.h"
39 #include "irc-channel.h"
43 * RFC 2812, (3.2.1 Join message Command):
44 * Note that this message
45 * accepts a special argument ("0"), which is a special request to leave all
46 * channels the user is currently a member of. The server will process this
47 * message as if the user had sent a PART command (See Section 3.2.2) for
48 * each channel he is a member of.
51 part_from_all_channels(CLIENT* client, CLIENT *target)
56 while ((cl2chan = Channel_FirstChannelOf(target))) {
57 chan = Channel_GetChannel(cl2chan);
58 assert( chan != NULL );
59 Channel_Part(target, client, Channel_Name(chan), Client_ID(target));
66 * Check weather a local client is allowed to join an already existing
68 * @param Client Client that sent the JOIN command
69 * @param chan Channel to check
70 * @param channame Name of the channel
71 * @param key Provided channel key (or NULL if none has been provided)
72 * @return true if client is allowed to join channel, false otherwise
75 join_allowed(CLIENT *Client, CHANNEL *chan, const char *channame,
78 bool is_invited, is_banned;
79 const char *channel_modes;
81 /* Allow IRC operators to overwrite channel limits */
82 if (strchr(Client_Modes(Client), 'o'))
85 is_banned = Lists_Check(Channel_GetListBans(chan), Client);
86 is_invited = Lists_Check(Channel_GetListInvites(chan), Client);
88 if (is_banned && !is_invited) {
89 /* Client is banned from channel (and not on invite list) */
90 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG,
91 Client_ID(Client), channame);
95 channel_modes = Channel_Modes(chan);
96 if (strchr(channel_modes, 'i') && !is_invited) {
97 /* Channel is "invite-only" and client is not on invite list */
98 IRC_WriteStrClient(Client, ERR_INVITEONLYCHAN_MSG,
99 Client_ID(Client), channame);
103 if (!Channel_CheckKey(chan, Client, key ? key : "")) {
104 /* Channel is protected by a channel key and the client
105 * didn't specify the correct one */
106 IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG,
107 Client_ID(Client), channame);
111 if (strchr(channel_modes, 'l') &&
112 (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
113 /* There are more clints joined to this channel than allowed */
114 IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG,
115 Client_ID(Client), channame);
119 if (strchr(channel_modes, 'z') && !Conn_UsesSSL(Client_Conn(Client))) {
120 /* Only "secure" clients are allowed, but clients doesn't
121 * use SSL encryption */
122 IRC_WriteStrClient(Client, ERR_SECURECHANNEL_MSG,
123 Client_ID(Client), channame);
132 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
136 Channel_UserModeAdd(chan, target, *flags);
141 /* If channel persistent and client is ircop: make client chanop */
142 if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
143 Channel_UserModeAdd(chan, target, 'o');
148 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
151 char str[COMMAND_LEN], *ptr = NULL;
153 strlcpy(str, (char *)Data, sizeof(str));
154 conn = Client_Conn(To);
156 if (Conn_Options(conn) & CONN_RFC1459) {
157 /* RFC 1459 compatibility mode, appended modes are NOT
158 * supported, so strip them off! */
159 ptr = strchr(str, 0x7);
164 IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
166 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
168 } /* cb_join_forward */
172 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
173 const char *channame)
175 char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
177 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
183 /* forward to other servers (if it is not a local channel) */
184 if (!Channel_IsLocal(chan)) {
185 snprintf(str, sizeof(str), "%s%s", channame, modes);
186 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
187 cb_join_forward, str);
190 /* tell users in this channel about the new client */
191 IRC_WriteStrChannelPrefix(Client, chan, target, false,
192 "JOIN :%s", channame);
194 /* syncronize channel modes */
196 IRC_WriteStrChannelPrefix(Client, chan, target, false,
197 "MODE %s +%s %s", channame,
198 &modes[1], Client_ID(target));
204 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
205 const char *channame)
209 if (Client_Type(Client) != CLIENT_USER)
211 /* acknowledge join */
212 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
215 /* Send topic to client, if any */
216 topic = Channel_Topic(chan);
217 assert(topic != NULL);
219 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
220 Client_ID(Client), channame, topic))
223 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
224 Client_ID(Client), channame,
225 Channel_TopicWho(chan),
226 Channel_TopicTime(chan)))
230 /* send list of channel members to client */
231 if (!IRC_Send_NAMES(Client, chan))
233 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client), Channel_Name(chan));
238 IRC_JOIN( CLIENT *Client, REQUEST *Req )
240 char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
244 assert( Client != NULL );
245 assert( Req != NULL );
247 /* Bad number of arguments? */
248 if (Req->argc < 1 || Req->argc > 2)
249 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
250 Client_ID(Client), Req->command);
252 /* Who is the sender? */
253 if (Client_Type(Client) == CLIENT_SERVER)
254 target = Client_Search(Req->prefix);
259 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
261 /* Is argument "0"? */
262 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
263 return part_from_all_channels(Client, target);
265 /* Are channel keys given? */
267 key = strtok_r(Req->argv[1], ",", &lastkey);
269 channame = Req->argv[0];
270 channame = strtok_r(channame, ",", &lastchan);
272 /* Make sure that "channame" is not the empty string ("JOIN :") */
274 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
275 Client_ID(Client), Req->command);
280 /* Did the server include channel-user-modes? */
281 if (Client_Type(Client) == CLIENT_SERVER) {
282 flags = strchr(channame, 0x7);
289 chan = Channel_Search(channame);
290 if (!chan && Conf_PredefChannelsOnly) {
291 /* channel must be created, but server does not allow this */
292 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
297 if (Client_Type(Client) == CLIENT_USER) {
298 /* Test if the user has reached the channel limit */
299 if ((Conf_MaxJoins > 0) &&
300 (Channel_CountForUser(Client) >= Conf_MaxJoins))
301 return IRC_WriteStrClient(Client,
302 ERR_TOOMANYCHANNELS_MSG,
303 Client_ID(Client), channame);
305 /* Already existing channel: check if the
306 * client is allowed to join */
307 if (!join_allowed(Client, chan, channame, key))
310 /* New channel: first user will become channel
311 * operator unless this is a modeless channel */
312 if (*channame != '+')
316 /* Local client: update idle time */
317 Conn_UpdateIdle(Client_Conn(Client));
319 /* Remote server: we don't need to know whether the
320 * client is invited or not, but we have to make sure
321 * that the "one shot" entries (generated by INVITE
322 * commands) in this list become deleted when a user
323 * joins a channel this way. */
325 (void)Lists_Check(Channel_GetListInvites(chan),
329 /* Join channel (and create channel if it doesn't exist) */
330 if (!Channel_Join(target, channame))
333 if (!chan) { /* channel is new; it has been created above */
334 chan = Channel_Search(channame);
335 assert(chan != NULL);
336 if (Channel_IsModeless(chan)) {
337 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
338 Channel_ModeAdd(chan, 'n'); /* no external msgs */
341 assert(chan != NULL);
343 join_set_channelmodes(chan, target, flags);
345 join_forward(Client, target, chan, channame);
347 if (!join_send_topic(Client, target, chan, channame))
348 break; /* write error */
351 channame = strtok_r(NULL, ",", &lastchan);
353 key = strtok_r(NULL, ",", &lastkey);
360 * Handler for the IRC "PART" command.
363 IRC_PART(CLIENT * Client, REQUEST * Req)
368 assert(Client != NULL);
371 if (Req->argc < 1 || Req->argc > 2)
372 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
373 Client_ID(Client), Req->command);
376 if (Client_Type(Client) == CLIENT_SERVER)
377 target = Client_Search(Req->prefix);
381 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
382 Client_ID(Client), Req->prefix);
384 /* Loop over all the given channel names */
385 chan = strtok(Req->argv[0], ",");
387 /* Make sure that "chan" is not the empty string ("PART :") */
389 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
390 Client_ID(Client), Req->command);
393 Channel_Part(target, Client, chan,
394 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
395 chan = strtok(NULL, ",");
398 /* Update idle time, if local client */
399 if (Client_Conn(Client) > NONE)
400 Conn_UpdateIdle(Client_Conn(Client));
407 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
414 assert( Client != NULL );
415 assert( Req != NULL );
417 if ((Req->argc < 1) || (Req->argc > 2))
418 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
420 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
422 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
424 /* Welcher Channel? */
425 chan = Channel_Search( Req->argv[0] );
426 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
428 /* Ist der User Mitglied in dem Channel? */
429 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
433 /* Request actual topic */
434 topic = Channel_Topic(chan);
436 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
437 Client_ID(Client), Channel_Name(chan), topic);
439 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
440 Client_ID(Client), Channel_Name(chan),
441 Channel_TopicWho(chan),
442 Channel_TopicTime(chan));
447 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
448 Client_ID(from), Channel_Name(chan));
451 if( strchr( Channel_Modes( chan ), 't' ))
453 /* Topic Lock. Ist der User ein Channel Operator? */
454 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
458 Channel_SetTopic(chan, from, Req->argv[1]);
459 LogDebug("%s \"%s\" set topic on \"%s\": %s",
460 Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
461 Req->argv[1][0] ? Req->argv[1] : "<none>");
463 /* Update channel and forward new topic to other servers */
464 if (!Channel_IsLocal(chan))
465 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
466 Req->argv[0], Req->argv[1]);
467 IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
468 Req->argv[0], Req->argv[1]);
470 if (Client_Type(Client) == CLIENT_USER)
471 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
472 Req->argv[0], Req->argv[1]);
479 * Handler for the IRC "LIST" command.
480 * This implementation handles the local case as well as the forwarding of the
481 * LIST command to other servers in the IRC network.
484 IRC_LIST( CLIENT *Client, REQUEST *Req )
488 CLIENT *from, *target;
490 assert( Client != NULL );
491 assert( Req != NULL );
493 /* Bad number of prameters? */
495 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
496 Client_ID( Client ), Req->command );
499 pattern = strtok( Req->argv[0], "," );
503 /* Get sender from prefix, if any */
504 if( Client_Type( Client ) == CLIENT_SERVER )
505 from = Client_Search( Req->prefix );
510 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
511 Client_ID( Client ), Req->prefix );
515 /* Forward to other server? */
516 target = Client_Search( Req->argv[1] );
517 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
518 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
519 Client_ID( Client ), Req->argv[1] );
521 if( target != Client_ThisServer( ))
523 /* Target is indeed an other server, forward it! */
524 return IRC_WriteStrClientPrefix( target, from,
525 "LIST %s :%s", Client_ID( from ),
532 /* Loop through all the channels */
533 chan = Channel_First( );
536 /* Check search pattern */
537 if( Match( pattern, Channel_Name( chan )))
540 if( ! strchr( Channel_Modes( chan ), 's' ) ||
541 Channel_IsMemberOf( chan, from ))
543 if( ! IRC_WriteStrClient( from,
544 RPL_LIST_MSG, Client_ID( from ),
545 Channel_Name( chan ),
546 Channel_MemberCount( chan ),
547 Channel_Topic( chan )))
551 chan = Channel_Next( chan );
554 /* Get next name ... */
556 pattern = strtok( NULL, "," );
561 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
566 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
568 char modes_add[COMMAND_LEN], l[16], *ptr;
573 assert( Client != NULL );
574 assert( Req != NULL );
576 /* Bad number of parameters? */
577 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
579 /* Compatibility kludge */
580 if( Req->argc == 5 ) arg_topic = 4;
581 else if( Req->argc == 3 ) arg_topic = 2;
585 from = Client_Search( Req->prefix );
586 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
588 /* Search or create channel */
589 chan = Channel_Search( Req->argv[0] );
590 if( ! chan ) chan = Channel_Create( Req->argv[0] );
591 if( ! chan ) return CONNECTED;
593 if( Req->argv[1][0] == '+' )
595 ptr = Channel_Modes( chan );
598 /* OK, this channel doesn't have modes jet, set the received ones: */
599 Channel_SetModes( chan, &Req->argv[1][1] );
603 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
604 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
608 /* Delete modes which we never want to inherit */
609 Channel_ModeDel( chan, 'l' );
610 Channel_ModeDel( chan, 'k' );
613 strcpy( modes_add, "" );
614 ptr = Channel_Modes( chan );
619 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
620 strlcat( modes_add, l, sizeof( modes_add ));
624 strlcat( modes_add, " ", sizeof( modes_add ));
625 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
630 /* Inform members of this channel */
631 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
634 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
639 ptr = Channel_Topic( chan );
640 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
642 /* OK, there is no topic jet */
643 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
644 IRC_WriteStrChannelPrefix(Client, chan, from, false,
645 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
649 /* Forward CHANINFO to other serevrs */
650 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] );
651 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
652 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );