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);
145 * Set user channel modes.
147 * @param chan Channel
148 * @param target User to set modes for
149 * @param flags Channel modes to add
152 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
156 Channel_UserModeAdd(chan, target, *flags);
161 /* If channel persistent and client is ircop: make client chanop */
162 if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
163 Channel_UserModeAdd(chan, target, 'o');
164 } /* join_set_channelmodes */
168 * Forward JOIN command to a specific server
170 * This function diffentiates between servers using RFC 2813 mode that
171 * support the JOIN command with appended ASCII 7 character and channel
172 * modes, and servers using RFC 1459 protocol which require separate JOIN
175 * @param To Forward JOIN (and MODE) command to this peer server
176 * @param Prefix Client used to prefix the genrated commands
177 * @param Data Parameters of JOIN command to forward, probably
178 * containing channel modes separated by ASCII 7.
181 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
184 char str[COMMAND_LEN], *ptr = NULL;
186 strlcpy(str, (char *)Data, sizeof(str));
187 conn = Client_Conn(To);
189 if (Conn_Options(conn) & CONN_RFC1459) {
190 /* RFC 1459 compatibility mode, appended modes are NOT
191 * supported, so strip them off! */
192 ptr = strchr(str, 0x7);
197 IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
199 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
201 } /* cb_join_forward */
205 * Forward JOIN command to all servers
207 * This function calls cb_join_forward(), which differentiates between
208 * protocol implementations (e.g. RFC 2812, RFC 1459).
210 * @param Client Client used to prefix the genrated commands
211 * @param target Forward JOIN (and MODE) command to this peer server
212 * @param chan Channel structure
213 * @param channame Channel name
216 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
217 const char *channame)
219 char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
221 /* RFC 2813, 4.2.1: channel modes are separated from the channel
222 * name with ASCII 7, if any, and not spaces: */
223 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
229 /* forward to other servers (if it is not a local channel) */
230 if (!Channel_IsLocal(chan)) {
231 snprintf(str, sizeof(str), "%s%s", channame, modes);
232 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
233 cb_join_forward, str);
236 /* tell users in this channel about the new client */
237 IRC_WriteStrChannelPrefix(Client, chan, target, false,
238 "JOIN :%s", channame);
240 /* syncronize channel modes */
242 IRC_WriteStrChannelPrefix(Client, chan, target, false,
243 "MODE %s +%s %s", channame,
244 &modes[1], Client_ID(target));
250 * Aknowledge user JOIN request and send "channel info" numerics.
252 * @param Client Client used to prefix the genrated commands
253 * @param target Forward commands/numerics to this user
254 * @param chan Channel structure
255 * @param channame Channel name
258 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
259 const char *channame)
263 if (Client_Type(Client) != CLIENT_USER)
265 /* acknowledge join */
266 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
269 /* Send topic to client, if any */
270 topic = Channel_Topic(chan);
271 assert(topic != NULL);
273 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
274 Client_ID(Client), channame, topic))
277 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
278 Client_ID(Client), channame,
279 Channel_TopicWho(chan),
280 Channel_TopicTime(chan)))
284 /* send list of channel members to client */
285 if (!IRC_Send_NAMES(Client, chan))
287 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client),
289 } /* join_send_topic */
293 * Handler for the IRC "JOIN" command.
295 * See RFC 2812, 3.2.1 "Join message"; RFC 2813, 4.2.1 "Join message".
297 * @param Client The client from which this command has been received
298 * @param Req Request structure with prefix and all parameters
299 * @returns CONNECTED or DISCONNECTED
302 IRC_JOIN( CLIENT *Client, REQUEST *Req )
304 char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
308 assert( Client != NULL );
309 assert( Req != NULL );
311 /* Bad number of arguments? */
312 if (Req->argc < 1 || Req->argc > 2)
313 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
314 Client_ID(Client), Req->command);
316 /* Who is the sender? */
317 if (Client_Type(Client) == CLIENT_SERVER)
318 target = Client_Search(Req->prefix);
323 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
325 /* Is argument "0"? */
326 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
327 return part_from_all_channels(Client, target);
329 /* Are channel keys given? */
331 key = strtok_r(Req->argv[1], ",", &lastkey);
333 channame = Req->argv[0];
334 channame = strtok_r(channame, ",", &lastchan);
336 /* Make sure that "channame" is not the empty string ("JOIN :") */
338 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
339 Client_ID(Client), Req->command);
344 /* Did the server include channel-user-modes? */
345 if (Client_Type(Client) == CLIENT_SERVER) {
346 flags = strchr(channame, 0x7);
353 chan = Channel_Search(channame);
354 if (!chan && Conf_PredefChannelsOnly) {
355 /* channel must be created, but server does not allow this */
356 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
361 if (Client_Type(Client) == CLIENT_USER) {
362 /* Test if the user has reached the channel limit */
363 if ((Conf_MaxJoins > 0) &&
364 (Channel_CountForUser(Client) >= Conf_MaxJoins))
365 return IRC_WriteStrClient(Client,
366 ERR_TOOMANYCHANNELS_MSG,
367 Client_ID(Client), channame);
369 /* Already existing channel: check if the
370 * client is allowed to join */
371 if (!join_allowed(Client, chan, channame, key))
374 /* New channel: first user will become channel
375 * operator unless this is a modeless channel */
376 if (*channame != '+')
380 /* Local client: update idle time */
381 Conn_UpdateIdle(Client_Conn(Client));
383 /* Remote server: we don't need to know whether the
384 * client is invited or not, but we have to make sure
385 * that the "one shot" entries (generated by INVITE
386 * commands) in this list become deleted when a user
387 * joins a channel this way. */
389 (void)Lists_Check(Channel_GetListInvites(chan),
393 /* Join channel (and create channel if it doesn't exist) */
394 if (!Channel_Join(target, channame))
397 if (!chan) { /* channel is new; it has been created above */
398 chan = Channel_Search(channame);
399 assert(chan != NULL);
400 if (Channel_IsModeless(chan)) {
401 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
402 Channel_ModeAdd(chan, 'n'); /* no external msgs */
405 assert(chan != NULL);
407 join_set_channelmodes(chan, target, flags);
409 join_forward(Client, target, chan, channame);
411 if (!join_send_topic(Client, target, chan, channame))
412 break; /* write error */
415 channame = strtok_r(NULL, ",", &lastchan);
417 key = strtok_r(NULL, ",", &lastkey);
424 * Handler for the IRC "PART" command.
426 * See RFC 2812, 3.2.2: "Part message".
428 * @param Client The client from which this command has been received
429 * @param Req Request structure with prefix and all parameters
430 * @returns CONNECTED or DISCONNECTED
433 IRC_PART(CLIENT * Client, REQUEST * Req)
438 assert(Client != NULL);
441 if (Req->argc < 1 || Req->argc > 2)
442 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
443 Client_ID(Client), Req->command);
446 if (Client_Type(Client) == CLIENT_SERVER)
447 target = Client_Search(Req->prefix);
451 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
452 Client_ID(Client), Req->prefix);
454 /* Loop over all the given channel names */
455 chan = strtok(Req->argv[0], ",");
457 /* Make sure that "chan" is not the empty string ("PART :") */
459 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
460 Client_ID(Client), Req->command);
463 Channel_Part(target, Client, chan,
464 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
465 chan = strtok(NULL, ",");
468 /* Update idle time, if local client */
469 if (Client_Conn(Client) > NONE)
470 Conn_UpdateIdle(Client_Conn(Client));
477 * Handler for the IRC "TOPIC" command.
479 * See RFC 2812, 3.2.4 "Topic message".
481 * @param Client The client from which this command has been received
482 * @param Req Request structure with prefix and all parameters
483 * @returns CONNECTED or DISCONNECTED
486 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
491 bool onchannel, topicok, use_servermode, r;
493 assert( Client != NULL );
494 assert( Req != NULL );
496 if (Req->argc < 1 || Req->argc > 2)
497 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
498 Client_ID(Client), Req->command);
500 if (Client_Type(Client) == CLIENT_SERVER)
501 from = Client_Search(Req->prefix);
506 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
507 Client_ID(Client), Req->prefix);
509 chan = Channel_Search(Req->argv[0]);
511 return IRC_WriteStrClient(from, ERR_NOSUCHCHANNEL_MSG,
512 Client_ID(from), Req->argv[0]);
514 Channel_CheckAdminRights(chan, Client, from,
515 &onchannel, &topicok, &use_servermode);
517 if (!onchannel && !topicok)
518 return IRC_WriteStrClient(from, ERR_NOTONCHANNEL_MSG,
519 Client_ID(from), Req->argv[0]);
521 if (Req->argc == 1) {
522 /* Request actual topic */
523 topic = Channel_Topic(chan);
525 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
527 Channel_Name(chan), topic);
529 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
532 Channel_TopicWho(chan),
533 Channel_TopicTime(chan));
538 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
543 if (strchr(Channel_Modes(chan), 't')) {
544 /* Topic Lock. Is the user a channel or IRC operator? */
546 return IRC_WriteStrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
552 Channel_SetTopic(chan, from, Req->argv[1]);
553 LogDebug("%s \"%s\" set topic on \"%s\": %s",
554 Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
555 Req->argv[1][0] ? Req->argv[1] : "<none>");
558 from = Client_ThisServer();
560 /* Update channel and forward new topic to other servers */
561 if (!Channel_IsLocal(chan))
562 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
563 Req->argv[0], Req->argv[1]);
564 IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
565 Req->argv[0], Req->argv[1]);
567 if (Client_Type(Client) == CLIENT_USER)
568 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
569 Req->argv[0], Req->argv[1]);
576 * Handler for the IRC "LIST" command.
578 * See RFC 2812, 3.2.6 "List message".
580 * This implementation handles the local case as well as the forwarding of the
581 * LIST command to other servers in the IRC network.
583 * @param Client The client from which this command has been received
584 * @param Req Request structure with prefix and all parameters
585 * @returns CONNECTED or DISCONNECTED
588 IRC_LIST( CLIENT *Client, REQUEST *Req )
592 CLIENT *from, *target;
594 assert( Client != NULL );
595 assert( Req != NULL );
597 /* Bad number of prameters? */
599 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
600 Client_ID( Client ), Req->command );
603 pattern = strtok( Req->argv[0], "," );
607 /* Get sender from prefix, if any */
608 if( Client_Type( Client ) == CLIENT_SERVER )
609 from = Client_Search( Req->prefix );
614 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
615 Client_ID( Client ), Req->prefix );
619 /* Forward to other server? */
620 target = Client_Search( Req->argv[1] );
621 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
622 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
623 Client_ID( Client ), Req->argv[1] );
625 if( target != Client_ThisServer( ))
627 /* Target is indeed an other server, forward it! */
628 return IRC_WriteStrClientPrefix( target, from,
629 "LIST %s :%s", Client_ID( from ),
636 /* Loop through all the channels */
637 chan = Channel_First( );
640 /* Check search pattern */
641 if( Match( pattern, Channel_Name( chan )))
644 if( ! strchr( Channel_Modes( chan ), 's' ) ||
645 Channel_IsMemberOf( chan, from ))
647 if( ! IRC_WriteStrClient( from,
648 RPL_LIST_MSG, Client_ID( from ),
649 Channel_Name( chan ),
650 Channel_MemberCount( chan ),
651 Channel_Topic( chan )))
655 chan = Channel_Next( chan );
658 /* Get next name ... */
660 pattern = strtok( NULL, "," );
665 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
670 * Handler for the IRC+ command "CHANINFO".
672 * See doc/Protocol.txt, section II.3:
673 * "Exchange channel-modes, topics, and persistent channels".
675 * @param Client The client from which this command has been received
676 * @param Req Request structure with prefix and all parameters
677 * @returns CONNECTED or DISCONNECTED
680 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
682 char modes_add[COMMAND_LEN], l[16], *ptr;
687 assert( Client != NULL );
688 assert( Req != NULL );
690 /* Bad number of parameters? */
691 if (Req->argc < 2 || Req->argc == 4 || Req->argc > 5)
692 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
693 Client_ID(Client), Req->command);
695 /* Compatibility kludge */
696 if( Req->argc == 5 ) arg_topic = 4;
697 else if( Req->argc == 3 ) arg_topic = 2;
701 from = Client_Search( Req->prefix );
702 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
704 /* Search or create channel */
705 chan = Channel_Search( Req->argv[0] );
706 if( ! chan ) chan = Channel_Create( Req->argv[0] );
707 if( ! chan ) return CONNECTED;
709 if( Req->argv[1][0] == '+' )
711 ptr = Channel_Modes( chan );
714 /* OK, this channel doesn't have modes jet, set the received ones: */
715 Channel_SetModes( chan, &Req->argv[1][1] );
719 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
720 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
724 /* Delete modes which we never want to inherit */
725 Channel_ModeDel( chan, 'l' );
726 Channel_ModeDel( chan, 'k' );
729 strcpy( modes_add, "" );
730 ptr = Channel_Modes( chan );
735 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
736 strlcat( modes_add, l, sizeof( modes_add ));
740 strlcat( modes_add, " ", sizeof( modes_add ));
741 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
746 /* Inform members of this channel */
747 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
750 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
755 ptr = Channel_Topic( chan );
756 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
758 /* OK, there is no topic jet */
759 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
760 IRC_WriteStrChannelPrefix(Client, chan, from, false,
761 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
765 /* Forward CHANINFO to other serevrs */
766 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] );
767 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
768 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );