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.
16 * IRC channel commands
28 #include "conn-func.h"
35 #include "irc-write.h"
39 #include "irc-channel.h"
43 * Part from all channels.
45 * RFC 2812, (3.2.1 Join message Command):
46 * Note that this message accepts a special argument ("0"), which is a
47 * special request to leave all channels the user is currently a member of.
48 * The server will process this message as if the user had sent a PART
49 * command (See Section 3.2.2) for each channel he is a member of.
51 * @param client Client that initiated the part request
52 * @param target Client that should part all joined channels
53 * @returns CONNECTED or DISCONNECTED
56 part_from_all_channels(CLIENT* client, CLIENT *target)
61 while ((cl2chan = Channel_FirstChannelOf(target))) {
62 chan = Channel_GetChannel(cl2chan);
63 assert( chan != NULL );
64 Channel_Part(target, client, Channel_Name(chan), Client_ID(target));
67 } /* part_from_all_channels */
71 * Check weather a local client is allowed to join an already existing
74 * @param Client Client that sent the JOIN command
75 * @param chan Channel to check
76 * @param channame Name of the channel
77 * @param key Provided channel key (or NULL)
78 * @returns true if client is allowed to join, false otherwise
81 join_allowed(CLIENT *Client, CHANNEL *chan, const char *channame,
84 bool is_invited, is_banned;
85 const char *channel_modes;
87 /* Allow IRC operators to overwrite channel limits */
88 if (strchr(Client_Modes(Client), 'o'))
91 is_banned = Lists_Check(Channel_GetListBans(chan), Client);
92 is_invited = Lists_Check(Channel_GetListInvites(chan), Client);
94 if (is_banned && !is_invited) {
95 /* Client is banned from channel (and not on invite list) */
96 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG,
97 Client_ID(Client), channame);
101 channel_modes = Channel_Modes(chan);
102 if (strchr(channel_modes, 'i') && !is_invited) {
103 /* Channel is "invite-only" and client is not on invite list */
104 IRC_WriteStrClient(Client, ERR_INVITEONLYCHAN_MSG,
105 Client_ID(Client), channame);
109 if (!Channel_CheckKey(chan, Client, key ? key : "")) {
110 /* Channel is protected by a channel key and the client
111 * didn't specify the correct one */
112 IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG,
113 Client_ID(Client), channame);
117 if (strchr(channel_modes, 'l') &&
118 (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
119 /* There are more clints joined to this channel than allowed */
120 IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG,
121 Client_ID(Client), channame);
125 if (strchr(channel_modes, 'z') && !Conn_UsesSSL(Client_Conn(Client))) {
126 /* Only "secure" clients are allowed, but clients doesn't
127 * use SSL encryption */
128 IRC_WriteStrClient(Client, ERR_SECURECHANNEL_MSG,
129 Client_ID(Client), channame);
133 if (strchr(channel_modes, 'O') && !Client_OperByMe(Client)) {
134 /* Only IRC operators are allowed! */
135 IRC_WriteStrClient(Client, ERR_OPONLYCHANNEL_MSG,
136 Client_ID(Client), channame);
140 if (strchr(channel_modes, 'R') && !strchr(Client_Modes(Client), 'R')) {
141 /* Only registered users are allowed! */
142 IRC_WriteStrClient(Client, ERR_REGONLYCHANNEL_MSG,
143 Client_ID(Client), channame);
152 * Set user channel modes.
154 * @param chan Channel
155 * @param target User to set modes for
156 * @param flags Channel modes to add
159 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
163 Channel_UserModeAdd(chan, target, *flags);
168 /* If channel persistent and client is ircop: make client chanop */
169 if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
170 Channel_UserModeAdd(chan, target, 'o');
171 } /* join_set_channelmodes */
175 * Forward JOIN command to a specific server
177 * This function diffentiates between servers using RFC 2813 mode that
178 * support the JOIN command with appended ASCII 7 character and channel
179 * modes, and servers using RFC 1459 protocol which require separate JOIN
182 * @param To Forward JOIN (and MODE) command to this peer server
183 * @param Prefix Client used to prefix the genrated commands
184 * @param Data Parameters of JOIN command to forward, probably
185 * containing channel modes separated by ASCII 7.
188 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
191 char str[COMMAND_LEN], *ptr = NULL;
193 strlcpy(str, (char *)Data, sizeof(str));
194 conn = Client_Conn(To);
196 if (Conn_Options(conn) & CONN_RFC1459) {
197 /* RFC 1459 compatibility mode, appended modes are NOT
198 * supported, so strip them off! */
199 ptr = strchr(str, 0x7);
204 IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
206 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
208 } /* cb_join_forward */
212 * Forward JOIN command to all servers
214 * This function calls cb_join_forward(), which differentiates between
215 * protocol implementations (e.g. RFC 2812, RFC 1459).
217 * @param Client Client used to prefix the genrated commands
218 * @param target Forward JOIN (and MODE) command to this peer server
219 * @param chan Channel structure
220 * @param channame Channel name
223 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
224 const char *channame)
226 char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
228 /* RFC 2813, 4.2.1: channel modes are separated from the channel
229 * name with ASCII 7, if any, and not spaces: */
230 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
236 /* forward to other servers (if it is not a local channel) */
237 if (!Channel_IsLocal(chan)) {
238 snprintf(str, sizeof(str), "%s%s", channame, modes);
239 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
240 cb_join_forward, str);
243 /* tell users in this channel about the new client */
244 IRC_WriteStrChannelPrefix(Client, chan, target, false,
245 "JOIN :%s", channame);
247 /* synchronize channel modes */
249 IRC_WriteStrChannelPrefix(Client, chan, target, false,
250 "MODE %s +%s %s", channame,
251 &modes[1], Client_ID(target));
257 * Aknowledge user JOIN request and send "channel info" numerics.
259 * @param Client Client used to prefix the genrated commands
260 * @param target Forward commands/numerics to this user
261 * @param chan Channel structure
262 * @param channame Channel name
265 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
266 const char *channame)
270 if (Client_Type(Client) != CLIENT_USER)
272 /* acknowledge join */
273 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
276 /* Send topic to client, if any */
277 topic = Channel_Topic(chan);
278 assert(topic != NULL);
280 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
281 Client_ID(Client), channame, topic))
284 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
285 Client_ID(Client), channame,
286 Channel_TopicWho(chan),
287 Channel_TopicTime(chan)))
291 /* send list of channel members to client */
292 if (!IRC_Send_NAMES(Client, chan))
294 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client),
296 } /* join_send_topic */
300 * Handler for the IRC "JOIN" command.
302 * See RFC 2812, 3.2.1 "Join message"; RFC 2813, 4.2.1 "Join message".
304 * @param Client The client from which this command has been received
305 * @param Req Request structure with prefix and all parameters
306 * @returns CONNECTED or DISCONNECTED
309 IRC_JOIN( CLIENT *Client, REQUEST *Req )
311 char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
315 assert (Client != NULL);
316 assert (Req != NULL);
318 /* Bad number of arguments? */
319 if (Req->argc < 1 || Req->argc > 2)
320 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
321 Client_ID(Client), Req->command);
323 /* Who is the sender? */
324 if (Client_Type(Client) == CLIENT_SERVER)
325 target = Client_Search(Req->prefix);
330 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
331 Client_ID(Client), Req->prefix);
333 /* Is argument "0"? */
334 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
335 return part_from_all_channels(Client, target);
337 /* Are channel keys given? */
339 key = strtok_r(Req->argv[1], ",", &lastkey);
341 channame = Req->argv[0];
342 channame = strtok_r(channame, ",", &lastchan);
344 /* Make sure that "channame" is not the empty string ("JOIN :") */
346 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
347 Client_ID(Client), Req->command);
352 /* Did the server include channel-user-modes? */
353 if (Client_Type(Client) == CLIENT_SERVER) {
354 flags = strchr(channame, 0x7);
361 chan = Channel_Search(channame);
362 if (!chan && Conf_PredefChannelsOnly) {
363 /* channel must be created, but forbidden by config */
364 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG,
365 Client_ID(Client), channame);
370 if (Client_Type(Client) == CLIENT_USER) {
371 /* Test if the user has reached the channel limit */
372 if ((Conf_MaxJoins > 0) &&
373 (Channel_CountForUser(Client) >= Conf_MaxJoins))
374 return IRC_WriteStrClient(Client,
375 ERR_TOOMANYCHANNELS_MSG,
376 Client_ID(Client), channame);
378 /* Already existing channel: check if the
379 * client is allowed to join */
380 if (!join_allowed(Client, chan, channame, key))
383 /* New channel: first user will become channel
384 * operator unless this is a modeless channel */
385 if (*channame != '+')
389 /* Local client: update idle time */
390 Conn_UpdateIdle(Client_Conn(Client));
392 /* Remote server: we don't need to know whether the
393 * client is invited or not, but we have to make sure
394 * that the "one shot" entries (generated by INVITE
395 * commands) in this list become deleted when a user
396 * joins a channel this way. */
398 (void)Lists_Check(Channel_GetListInvites(chan),
402 /* Join channel (and create channel if it doesn't exist) */
403 if (!Channel_Join(target, channame))
406 if (!chan) { /* channel is new; it has been created above */
407 chan = Channel_Search(channame);
408 assert(chan != NULL);
409 if (Channel_IsModeless(chan)) {
410 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
411 Channel_ModeAdd(chan, 'n'); /* no external msgs */
414 assert(chan != NULL);
416 join_set_channelmodes(chan, target, flags);
418 join_forward(Client, target, chan, channame);
420 if (!join_send_topic(Client, target, chan, channame))
421 break; /* write error */
424 channame = strtok_r(NULL, ",", &lastchan);
426 key = strtok_r(NULL, ",", &lastkey);
433 * Handler for the IRC "PART" command.
435 * See RFC 2812, 3.2.2: "Part message".
437 * @param Client The client from which this command has been received
438 * @param Req Request structure with prefix and all parameters
439 * @returns CONNECTED or DISCONNECTED
442 IRC_PART(CLIENT * Client, REQUEST * Req)
447 assert(Client != NULL);
450 if (Req->argc < 1 || Req->argc > 2)
451 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
452 Client_ID(Client), Req->command);
455 if (Client_Type(Client) == CLIENT_SERVER)
456 target = Client_Search(Req->prefix);
460 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
461 Client_ID(Client), Req->prefix);
463 /* Loop over all the given channel names */
464 chan = strtok(Req->argv[0], ",");
466 /* Make sure that "chan" is not the empty string ("PART :") */
468 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
469 Client_ID(Client), Req->command);
472 Channel_Part(target, Client, chan,
473 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
474 chan = strtok(NULL, ",");
477 /* Update idle time, if local client */
478 if (Client_Conn(Client) > NONE)
479 Conn_UpdateIdle(Client_Conn(Client));
486 * Handler for the IRC "TOPIC" command.
488 * See RFC 2812, 3.2.4 "Topic message".
490 * @param Client The client from which this command has been received
491 * @param Req Request structure with prefix and all parameters
492 * @returns CONNECTED or DISCONNECTED
495 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
500 bool onchannel, topicok, use_servermode, r;
502 assert( Client != NULL );
503 assert( Req != NULL );
505 if (Req->argc < 1 || Req->argc > 2)
506 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
507 Client_ID(Client), Req->command);
509 if (Client_Type(Client) == CLIENT_SERVER)
510 from = Client_Search(Req->prefix);
515 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
516 Client_ID(Client), Req->prefix);
518 chan = Channel_Search(Req->argv[0]);
520 return IRC_WriteStrClient(from, ERR_NOSUCHCHANNEL_MSG,
521 Client_ID(from), Req->argv[0]);
523 Channel_CheckAdminRights(chan, Client, from,
524 &onchannel, &topicok, &use_servermode);
526 if (!onchannel && !topicok)
527 return IRC_WriteStrClient(from, ERR_NOTONCHANNEL_MSG,
528 Client_ID(from), Req->argv[0]);
530 if (Req->argc == 1) {
531 /* Request actual topic */
532 topic = Channel_Topic(chan);
534 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
536 Channel_Name(chan), topic);
540 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
543 Channel_TopicWho(chan),
544 Channel_TopicTime(chan));
549 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
554 if (strchr(Channel_Modes(chan), 't')) {
555 /* Topic Lock. Is the user a channel or IRC operator? */
557 return IRC_WriteStrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
563 Channel_SetTopic(chan, from, Req->argv[1]);
564 LogDebug("%s \"%s\" set topic on \"%s\": %s",
565 Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
566 Req->argv[1][0] ? Req->argv[1] : "<none>");
569 from = Client_ThisServer();
571 /* Update channel and forward new topic to other servers */
572 if (!Channel_IsLocal(chan))
573 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
574 Req->argv[0], Req->argv[1]);
575 IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
576 Req->argv[0], Req->argv[1]);
578 if (Client_Type(Client) == CLIENT_USER)
579 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
580 Req->argv[0], Req->argv[1]);
587 * Handler for the IRC "LIST" command.
589 * See RFC 2812, 3.2.6 "List message".
591 * This implementation handles the local case as well as the forwarding of the
592 * LIST command to other servers in the IRC network.
594 * @param Client The client from which this command has been received
595 * @param Req Request structure with prefix and all parameters
596 * @returns CONNECTED or DISCONNECTED
599 IRC_LIST( CLIENT *Client, REQUEST *Req )
603 CLIENT *from, *target;
605 assert( Client != NULL );
606 assert( Req != NULL );
608 /* Bad number of prameters? */
610 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
611 Client_ID( Client ), Req->command );
614 pattern = strtok( Req->argv[0], "," );
618 /* Get sender from prefix, if any */
619 if( Client_Type( Client ) == CLIENT_SERVER )
620 from = Client_Search( Req->prefix );
625 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
626 Client_ID( Client ), Req->prefix );
630 /* Forward to other server? */
631 target = Client_Search( Req->argv[1] );
632 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
633 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
634 Client_ID( Client ), Req->argv[1] );
636 if( target != Client_ThisServer( ))
638 /* Target is indeed an other server, forward it! */
639 return IRC_WriteStrClientPrefix( target, from,
640 "LIST %s :%s", Client_ID( from ),
647 /* Loop through all the channels */
648 chan = Channel_First( );
651 /* Check search pattern */
652 if( Match( pattern, Channel_Name( chan )))
655 if( ! strchr( Channel_Modes( chan ), 's' ) ||
656 Channel_IsMemberOf( chan, from ))
658 if( ! IRC_WriteStrClient( from,
659 RPL_LIST_MSG, Client_ID( from ),
660 Channel_Name( chan ),
661 Channel_MemberCount( chan ),
662 Channel_Topic( chan )))
666 chan = Channel_Next( chan );
669 /* Get next name ... */
671 pattern = strtok( NULL, "," );
676 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
681 * Handler for the IRC+ command "CHANINFO".
683 * See doc/Protocol.txt, section II.3:
684 * "Exchange channel-modes, topics, and persistent channels".
686 * @param Client The client from which this command has been received
687 * @param Req Request structure with prefix and all parameters
688 * @returns CONNECTED or DISCONNECTED
691 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
693 char modes_add[COMMAND_LEN], l[16], *ptr;
698 assert( Client != NULL );
699 assert( Req != NULL );
701 /* Bad number of parameters? */
702 if (Req->argc < 2 || Req->argc == 4 || Req->argc > 5)
703 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
704 Client_ID(Client), Req->command);
706 /* Compatibility kludge */
707 if( Req->argc == 5 ) arg_topic = 4;
708 else if( Req->argc == 3 ) arg_topic = 2;
712 from = Client_Search( Req->prefix );
713 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
715 /* Search or create channel */
716 chan = Channel_Search( Req->argv[0] );
717 if( ! chan ) chan = Channel_Create( Req->argv[0] );
718 if( ! chan ) return CONNECTED;
720 if( Req->argv[1][0] == '+' )
722 ptr = Channel_Modes( chan );
725 /* OK, this channel doesn't have modes jet, set the received ones: */
726 Channel_SetModes( chan, &Req->argv[1][1] );
730 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
731 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
735 /* Delete modes which we never want to inherit */
736 Channel_ModeDel( chan, 'l' );
737 Channel_ModeDel( chan, 'k' );
740 strcpy( modes_add, "" );
741 ptr = Channel_Modes( chan );
746 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
747 strlcat( modes_add, l, sizeof( modes_add ));
751 strlcat( modes_add, " ", sizeof( modes_add ));
752 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
757 /* Inform members of this channel */
758 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
761 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
766 ptr = Channel_Topic( chan );
767 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
769 /* OK, there is no topic jet */
770 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
771 IRC_WriteStrChannelPrefix(Client, chan, from, false,
772 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
776 /* Forward CHANINFO to other serevrs */
777 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] );
778 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
779 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );