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"
36 #include "irc-write.h"
40 #include "irc-channel.h"
44 * Part from all channels.
46 * RFC 2812, (3.2.1 Join message Command):
47 * Note that this message accepts a special argument ("0"), which is a
48 * special request to leave all channels the user is currently a member of.
49 * The server will process this message as if the user had sent a PART
50 * command (See Section 3.2.2) for each channel he is a member of.
52 * @param client Client that initiated the part request
53 * @param target Client that should part all joined channels
54 * @returns CONNECTED or DISCONNECTED
57 part_from_all_channels(CLIENT* client, CLIENT *target)
62 while ((cl2chan = Channel_FirstChannelOf(target))) {
63 chan = Channel_GetChannel(cl2chan);
64 assert( chan != NULL );
65 Channel_Part(target, client, Channel_Name(chan), Client_ID(target));
68 } /* part_from_all_channels */
72 * Check weather a local client is allowed to join an already existing
75 * @param Client Client that sent the JOIN command
76 * @param chan Channel to check
77 * @param channame Name of the channel
78 * @param key Provided channel key (or NULL)
79 * @returns true if client is allowed to join, false otherwise
82 join_allowed(CLIENT *Client, CHANNEL *chan, const char *channame,
85 bool is_invited, is_banned, is_exception;
86 const char *channel_modes;
88 /* Allow IRC operators to overwrite channel limits */
89 if (strchr(Client_Modes(Client), 'o'))
92 is_banned = Lists_Check(Channel_GetListBans(chan), Client);
93 is_exception = Lists_Check(Channel_GetListExcepts(chan), Client);
94 is_invited = Lists_Check(Channel_GetListInvites(chan), Client);
96 if (is_banned && !is_invited && !is_exception) {
97 /* Client is banned from channel (and not on invite list) */
98 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG,
99 Client_ID(Client), channame);
103 channel_modes = Channel_Modes(chan);
104 if (strchr(channel_modes, 'i') && !is_invited) {
105 /* Channel is "invite-only" and client is not on invite list */
106 IRC_WriteStrClient(Client, ERR_INVITEONLYCHAN_MSG,
107 Client_ID(Client), channame);
111 if (!Channel_CheckKey(chan, Client, key ? key : "")) {
112 /* Channel is protected by a channel key and the client
113 * didn't specify the correct one */
114 IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG,
115 Client_ID(Client), channame);
119 if (strchr(channel_modes, 'l') &&
120 (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
121 /* There are more clints joined to this channel than allowed */
122 IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG,
123 Client_ID(Client), channame);
127 if (strchr(channel_modes, 'z') && !Conn_UsesSSL(Client_Conn(Client))) {
128 /* Only "secure" clients are allowed, but clients doesn't
129 * use SSL encryption */
130 IRC_WriteStrClient(Client, ERR_SECURECHANNEL_MSG,
131 Client_ID(Client), channame);
135 if (strchr(channel_modes, 'O') && !Client_OperByMe(Client)) {
136 /* Only IRC operators are allowed! */
137 IRC_WriteStrClient(Client, ERR_OPONLYCHANNEL_MSG,
138 Client_ID(Client), channame);
142 if (strchr(channel_modes, 'R') && !strchr(Client_Modes(Client), 'R')) {
143 /* Only registered users are allowed! */
144 IRC_WriteStrClient(Client, ERR_REGONLYCHANNEL_MSG,
145 Client_ID(Client), channame);
154 * Set user channel modes.
156 * @param chan Channel
157 * @param target User to set modes for
158 * @param flags Channel modes to add
161 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
165 Channel_UserModeAdd(chan, target, *flags);
170 /* If the channel is persistent (+P) and client is an IRC op:
171 * make client chanop, if not disabled in configuration. */
172 if (strchr(Channel_Modes(chan), 'P') && Conf_OperChanPAutoOp
173 && strchr(Client_Modes(target), 'o'))
174 Channel_UserModeAdd(chan, target, 'o');
175 } /* join_set_channelmodes */
179 * Forward JOIN command to a specific server
181 * This function diffentiates between servers using RFC 2813 mode that
182 * support the JOIN command with appended ASCII 7 character and channel
183 * modes, and servers using RFC 1459 protocol which require separate JOIN
186 * @param To Forward JOIN (and MODE) command to this peer server
187 * @param Prefix Client used to prefix the genrated commands
188 * @param Data Parameters of JOIN command to forward, probably
189 * containing channel modes separated by ASCII 7.
192 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
195 char str[COMMAND_LEN], *ptr = NULL;
197 strlcpy(str, (char *)Data, sizeof(str));
198 conn = Client_Conn(To);
200 if (Conn_Options(conn) & CONN_RFC1459) {
201 /* RFC 1459 compatibility mode, appended modes are NOT
202 * supported, so strip them off! */
203 ptr = strchr(str, 0x7);
208 IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
210 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
212 } /* cb_join_forward */
216 * Forward JOIN command to all servers
218 * This function calls cb_join_forward(), which differentiates between
219 * protocol implementations (e.g. RFC 2812, RFC 1459).
221 * @param Client Client used to prefix the genrated commands
222 * @param target Forward JOIN (and MODE) command to this peer server
223 * @param chan Channel structure
224 * @param channame Channel name
227 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
228 const char *channame)
230 char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
232 /* RFC 2813, 4.2.1: channel modes are separated from the channel
233 * name with ASCII 7, if any, and not spaces: */
234 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
240 /* forward to other servers (if it is not a local channel) */
241 if (!Channel_IsLocal(chan)) {
242 snprintf(str, sizeof(str), "%s%s", channame, modes);
243 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
244 cb_join_forward, str);
247 /* tell users in this channel about the new client */
248 IRC_WriteStrChannelPrefix(Client, chan, target, false,
249 "JOIN :%s", channame);
251 /* synchronize channel modes */
253 IRC_WriteStrChannelPrefix(Client, chan, target, false,
254 "MODE %s +%s %s", channame,
255 &modes[1], Client_ID(target));
261 * Aknowledge user JOIN request and send "channel info" numerics.
263 * @param Client Client used to prefix the genrated commands
264 * @param target Forward commands/numerics to this user
265 * @param chan Channel structure
266 * @param channame Channel name
269 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
270 const char *channame)
274 if (Client_Type(Client) != CLIENT_USER)
276 /* acknowledge join */
277 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
280 /* Send topic to client, if any */
281 topic = Channel_Topic(chan);
282 assert(topic != NULL);
284 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
285 Client_ID(Client), channame, topic))
288 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
289 Client_ID(Client), channame,
290 Channel_TopicWho(chan),
291 Channel_TopicTime(chan)))
295 /* send list of channel members to client */
296 if (!IRC_Send_NAMES(Client, chan))
298 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client),
300 } /* join_send_topic */
304 * Handler for the IRC "JOIN" command.
306 * See RFC 2812, 3.2.1 "Join message"; RFC 2813, 4.2.1 "Join message".
308 * @param Client The client from which this command has been received
309 * @param Req Request structure with prefix and all parameters
310 * @returns CONNECTED or DISCONNECTED
313 IRC_JOIN( CLIENT *Client, REQUEST *Req )
315 char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
319 assert (Client != NULL);
320 assert (Req != NULL);
322 /* Bad number of arguments? */
323 if (Req->argc < 1 || Req->argc > 2)
324 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
325 Client_ID(Client), Req->command);
327 /* Who is the sender? */
328 if (Client_Type(Client) == CLIENT_SERVER)
329 target = Client_Search(Req->prefix);
334 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
335 Client_ID(Client), Req->prefix);
337 /* Is argument "0"? */
338 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
339 return part_from_all_channels(Client, target);
341 /* Are channel keys given? */
343 key = strtok_r(Req->argv[1], ",", &lastkey);
345 channame = Req->argv[0];
346 channame = strtok_r(channame, ",", &lastchan);
348 /* Make sure that "channame" is not the empty string ("JOIN :") */
350 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
351 Client_ID(Client), Req->command);
356 /* Did the server include channel-user-modes? */
357 if (Client_Type(Client) == CLIENT_SERVER) {
358 flags = strchr(channame, 0x7);
365 chan = Channel_Search(channame);
366 if (!chan && Conf_PredefChannelsOnly) {
367 /* channel must be created, but forbidden by config */
368 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG,
369 Client_ID(Client), channame);
374 if (Client_Type(Client) == CLIENT_USER) {
376 /* Already existing channel: already member? */
377 if (Channel_IsMemberOf(chan, Client))
381 /* Test if the user has reached the channel limit */
382 if ((Conf_MaxJoins > 0) &&
383 (Channel_CountForUser(Client) >= Conf_MaxJoins)) {
384 if (!IRC_WriteStrClient(Client,
385 ERR_TOOMANYCHANNELS_MSG,
386 Client_ID(Client), channame))
392 /* Already existing channel: check if the
393 * client is allowed to join */
394 if (!join_allowed(Client, chan, channame, key))
397 /* New channel: first user will become channel
398 * operator unless this is a modeless channel */
399 if (*channame != '+')
403 /* Local client: update idle time */
404 Conn_UpdateIdle(Client_Conn(Client));
406 /* Remote server: we don't need to know whether the
407 * client is invited or not, but we have to make sure
408 * that the "one shot" entries (generated by INVITE
409 * commands) in this list become deleted when a user
410 * joins a channel this way. */
412 (void)Lists_Check(Channel_GetListInvites(chan),
416 /* Join channel (and create channel if it doesn't exist) */
417 if (!Channel_Join(target, channame))
420 if (!chan) { /* channel is new; it has been created above */
421 chan = Channel_Search(channame);
422 assert(chan != NULL);
423 if (Channel_IsModeless(chan)) {
424 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
425 Channel_ModeAdd(chan, 'n'); /* no external msgs */
428 assert(chan != NULL);
430 join_set_channelmodes(chan, target, flags);
432 join_forward(Client, target, chan, channame);
434 if (!join_send_topic(Client, target, chan, channame))
435 break; /* write error */
439 channame = strtok_r(NULL, ",", &lastchan);
441 key = strtok_r(NULL, ",", &lastkey);
448 * Handler for the IRC "PART" command.
450 * See RFC 2812, 3.2.2: "Part message".
452 * @param Client The client from which this command has been received
453 * @param Req Request structure with prefix and all parameters
454 * @returns CONNECTED or DISCONNECTED
457 IRC_PART(CLIENT * Client, REQUEST * Req)
462 assert(Client != NULL);
465 if (Req->argc < 1 || Req->argc > 2)
466 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
467 Client_ID(Client), Req->command);
470 if (Client_Type(Client) == CLIENT_SERVER)
471 target = Client_Search(Req->prefix);
475 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
476 Client_ID(Client), Req->prefix);
478 /* Loop over all the given channel names */
479 chan = strtok(Req->argv[0], ",");
481 /* Make sure that "chan" is not the empty string ("PART :") */
483 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
484 Client_ID(Client), Req->command);
487 Channel_Part(target, Client, chan,
488 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
489 chan = strtok(NULL, ",");
492 /* Update idle time, if local client */
493 if (Client_Conn(Client) > NONE)
494 Conn_UpdateIdle(Client_Conn(Client));
501 * Handler for the IRC "TOPIC" command.
503 * See RFC 2812, 3.2.4 "Topic message".
505 * @param Client The client from which this command has been received
506 * @param Req Request structure with prefix and all parameters
507 * @returns CONNECTED or DISCONNECTED
510 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
517 assert( Client != NULL );
518 assert( Req != NULL );
520 if (Req->argc < 1 || Req->argc > 2)
521 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
522 Client_ID(Client), Req->command);
524 if (Client_Type(Client) == CLIENT_SERVER)
525 from = Client_Search(Req->prefix);
530 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
531 Client_ID(Client), Req->prefix);
533 chan = Channel_Search(Req->argv[0]);
535 return IRC_WriteStrClient(from, ERR_NOSUCHCHANNEL_MSG,
536 Client_ID(from), Req->argv[0]);
538 /* Only remote servers and channel members are allowed to change the
539 * channel topic, and IRC opreators when the Conf_OperCanMode option
540 * is set in the server configuration. */
541 if (Client_Type(Client) != CLIENT_SERVER) {
542 topic_power = Client_HasMode(from, 'o');
543 if (!Channel_IsMemberOf(chan, from)
544 && !(Conf_OperCanMode && topic_power))
545 return IRC_WriteStrClient(from, ERR_NOTONCHANNEL_MSG,
546 Client_ID(from), Req->argv[0]);
550 if (Req->argc == 1) {
551 /* Request actual topic */
552 topic = Channel_Topic(chan);
554 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
556 Channel_Name(chan), topic);
560 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
563 Channel_TopicWho(chan),
564 Channel_TopicTime(chan));
569 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
574 if (strchr(Channel_Modes(chan), 't')) {
575 /* Topic Lock. Is the user a channel op or IRC operator? */
577 !strchr(Channel_UserModes(chan, from), 'h') &&
578 !strchr(Channel_UserModes(chan, from), 'o') &&
579 !strchr(Channel_UserModes(chan, from), 'a') &&
580 !strchr(Channel_UserModes(chan, from), 'q'))
581 return IRC_WriteStrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
587 Channel_SetTopic(chan, from, Req->argv[1]);
588 LogDebug("%s \"%s\" set topic on \"%s\": %s",
589 Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
590 Req->argv[1][0] ? Req->argv[1] : "<none>");
592 if (Conf_OperServerMode)
593 from = Client_ThisServer();
595 /* Update channel and forward new topic to other servers */
596 if (!Channel_IsLocal(chan))
597 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
598 Req->argv[0], Req->argv[1]);
599 IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
600 Req->argv[0], Req->argv[1]);
602 if (Client_Type(Client) == CLIENT_USER)
603 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
604 Req->argv[0], Req->argv[1]);
611 * Handler for the IRC "LIST" command.
613 * See RFC 2812, 3.2.6 "List message".
615 * This implementation handles the local case as well as the forwarding of the
616 * LIST command to other servers in the IRC network.
618 * @param Client The client from which this command has been received.
619 * @param Req Request structure with prefix and all parameters.
620 * @return CONNECTED or DISCONNECTED.
623 IRC_LIST( CLIENT *Client, REQUEST *Req )
627 CLIENT *from, *target;
630 assert(Client != NULL);
633 /* Bad number of prameters? */
635 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
636 Client_ID(Client), Req->command);
639 pattern = strtok(Req->argv[0], ",");
643 /* Get sender from prefix, if any */
644 if (Client_Type(Client) == CLIENT_SERVER)
645 from = Client_Search(Req->prefix);
650 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
651 Client_ID(Client), Req->prefix);
653 if (Req->argc == 2) {
654 /* Forward to other server? */
655 target = Client_Search(Req->argv[1]);
656 if (! target || Client_Type(target) != CLIENT_SERVER)
657 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
661 if (target != Client_ThisServer()) {
662 /* Target is indeed an other server, forward it! */
663 return IRC_WriteStrClientPrefix(target, from,
671 /* Loop through all the channels */
673 ngt_LowerStr(pattern);
674 chan = Channel_First();
676 /* Check search pattern */
677 if (MatchCaseInsensitive(pattern, Channel_Name(chan))) {
679 if (!strchr(Channel_Modes(chan), 's')
680 || Channel_IsMemberOf(chan, from)
681 || (!Conf_MorePrivacy && Client_OperByMe(Client))) {
682 if ((Conf_MaxListSize > 0)
683 && IRC_CheckListTooBig(from, count,
687 if (!IRC_WriteStrClient(from,
688 RPL_LIST_MSG, Client_ID(from),
690 Channel_MemberCount(chan),
691 Channel_Topic( chan )))
696 chan = Channel_Next(chan);
699 /* Get next name ... */
701 pattern = strtok(NULL, ",");
706 IRC_SetPenalty(from, 2);
707 return IRC_WriteStrClient(from, RPL_LISTEND_MSG, Client_ID(from));
712 * Handler for the IRC+ command "CHANINFO".
714 * See doc/Protocol.txt, section II.3:
715 * "Exchange channel-modes, topics, and persistent channels".
717 * @param Client The client from which this command has been received
718 * @param Req Request structure with prefix and all parameters
719 * @returns CONNECTED or DISCONNECTED
722 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
724 char modes_add[COMMAND_LEN], l[16], *ptr;
729 assert( Client != NULL );
730 assert( Req != NULL );
732 /* Bad number of parameters? */
733 if (Req->argc < 2 || Req->argc == 4 || Req->argc > 5)
734 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
735 Client_ID(Client), Req->command);
737 /* Compatibility kludge */
738 if( Req->argc == 5 ) arg_topic = 4;
739 else if( Req->argc == 3 ) arg_topic = 2;
743 from = Client_Search( Req->prefix );
744 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
746 /* Search or create channel */
747 chan = Channel_Search( Req->argv[0] );
748 if( ! chan ) chan = Channel_Create( Req->argv[0] );
749 if( ! chan ) return CONNECTED;
751 if( Req->argv[1][0] == '+' )
753 ptr = Channel_Modes( chan );
756 /* OK, this channel doesn't have modes jet, set the received ones: */
757 Channel_SetModes( chan, &Req->argv[1][1] );
761 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
762 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
766 /* Delete modes which we never want to inherit */
767 Channel_ModeDel( chan, 'l' );
768 Channel_ModeDel( chan, 'k' );
771 strcpy( modes_add, "" );
772 ptr = Channel_Modes( chan );
777 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
778 strlcat( modes_add, l, sizeof( modes_add ));
782 strlcat( modes_add, " ", sizeof( modes_add ));
783 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
788 /* Inform members of this channel */
789 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
792 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
797 ptr = Channel_Topic( chan );
798 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
800 /* OK, there is no topic jet */
801 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
802 IRC_WriteStrChannelPrefix(Client, chan, from, false,
803 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
807 /* Forward CHANINFO to other serevrs */
808 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] );
809 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
810 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );