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;
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_invited = Lists_Check(Channel_GetListInvites(chan), Client);
95 if (is_banned && !is_invited) {
96 /* Client is banned from channel (and not on invite list) */
97 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG,
98 Client_ID(Client), channame);
102 channel_modes = Channel_Modes(chan);
103 if (strchr(channel_modes, 'i') && !is_invited) {
104 /* Channel is "invite-only" and client is not on invite list */
105 IRC_WriteStrClient(Client, ERR_INVITEONLYCHAN_MSG,
106 Client_ID(Client), channame);
110 if (!Channel_CheckKey(chan, Client, key ? key : "")) {
111 /* Channel is protected by a channel key and the client
112 * didn't specify the correct one */
113 IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG,
114 Client_ID(Client), channame);
118 if (strchr(channel_modes, 'l') &&
119 (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
120 /* There are more clints joined to this channel than allowed */
121 IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG,
122 Client_ID(Client), channame);
126 if (strchr(channel_modes, 'z') && !Conn_UsesSSL(Client_Conn(Client))) {
127 /* Only "secure" clients are allowed, but clients doesn't
128 * use SSL encryption */
129 IRC_WriteStrClient(Client, ERR_SECURECHANNEL_MSG,
130 Client_ID(Client), channame);
134 if (strchr(channel_modes, 'O') && !Client_OperByMe(Client)) {
135 /* Only IRC operators are allowed! */
136 IRC_WriteStrClient(Client, ERR_OPONLYCHANNEL_MSG,
137 Client_ID(Client), channame);
141 if (strchr(channel_modes, 'R') && !strchr(Client_Modes(Client), 'R')) {
142 /* Only registered users are allowed! */
143 IRC_WriteStrClient(Client, ERR_REGONLYCHANNEL_MSG,
144 Client_ID(Client), channame);
153 * Set user channel modes.
155 * @param chan Channel
156 * @param target User to set modes for
157 * @param flags Channel modes to add
160 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
164 Channel_UserModeAdd(chan, target, *flags);
169 /* If channel persistent and client is ircop: make client chanop */
170 if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
171 Channel_UserModeAdd(chan, target, 'o');
172 } /* join_set_channelmodes */
176 * Forward JOIN command to a specific server
178 * This function diffentiates between servers using RFC 2813 mode that
179 * support the JOIN command with appended ASCII 7 character and channel
180 * modes, and servers using RFC 1459 protocol which require separate JOIN
183 * @param To Forward JOIN (and MODE) command to this peer server
184 * @param Prefix Client used to prefix the genrated commands
185 * @param Data Parameters of JOIN command to forward, probably
186 * containing channel modes separated by ASCII 7.
189 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
192 char str[COMMAND_LEN], *ptr = NULL;
194 strlcpy(str, (char *)Data, sizeof(str));
195 conn = Client_Conn(To);
197 if (Conn_Options(conn) & CONN_RFC1459) {
198 /* RFC 1459 compatibility mode, appended modes are NOT
199 * supported, so strip them off! */
200 ptr = strchr(str, 0x7);
205 IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
207 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
209 } /* cb_join_forward */
213 * Forward JOIN command to all servers
215 * This function calls cb_join_forward(), which differentiates between
216 * protocol implementations (e.g. RFC 2812, RFC 1459).
218 * @param Client Client used to prefix the genrated commands
219 * @param target Forward JOIN (and MODE) command to this peer server
220 * @param chan Channel structure
221 * @param channame Channel name
224 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
225 const char *channame)
227 char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
229 /* RFC 2813, 4.2.1: channel modes are separated from the channel
230 * name with ASCII 7, if any, and not spaces: */
231 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
237 /* forward to other servers (if it is not a local channel) */
238 if (!Channel_IsLocal(chan)) {
239 snprintf(str, sizeof(str), "%s%s", channame, modes);
240 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
241 cb_join_forward, str);
244 /* tell users in this channel about the new client */
245 IRC_WriteStrChannelPrefix(Client, chan, target, false,
246 "JOIN :%s", channame);
248 /* synchronize channel modes */
250 IRC_WriteStrChannelPrefix(Client, chan, target, false,
251 "MODE %s +%s %s", channame,
252 &modes[1], Client_ID(target));
258 * Aknowledge user JOIN request and send "channel info" numerics.
260 * @param Client Client used to prefix the genrated commands
261 * @param target Forward commands/numerics to this user
262 * @param chan Channel structure
263 * @param channame Channel name
266 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
267 const char *channame)
271 if (Client_Type(Client) != CLIENT_USER)
273 /* acknowledge join */
274 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
277 /* Send topic to client, if any */
278 topic = Channel_Topic(chan);
279 assert(topic != NULL);
281 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
282 Client_ID(Client), channame, topic))
285 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
286 Client_ID(Client), channame,
287 Channel_TopicWho(chan),
288 Channel_TopicTime(chan)))
292 /* send list of channel members to client */
293 if (!IRC_Send_NAMES(Client, chan))
295 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client),
297 } /* join_send_topic */
301 * Handler for the IRC "JOIN" command.
303 * See RFC 2812, 3.2.1 "Join message"; RFC 2813, 4.2.1 "Join message".
305 * @param Client The client from which this command has been received
306 * @param Req Request structure with prefix and all parameters
307 * @returns CONNECTED or DISCONNECTED
310 IRC_JOIN( CLIENT *Client, REQUEST *Req )
312 char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
316 assert (Client != NULL);
317 assert (Req != NULL);
319 /* Bad number of arguments? */
320 if (Req->argc < 1 || Req->argc > 2)
321 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
322 Client_ID(Client), Req->command);
324 /* Who is the sender? */
325 if (Client_Type(Client) == CLIENT_SERVER)
326 target = Client_Search(Req->prefix);
331 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
332 Client_ID(Client), Req->prefix);
334 /* Is argument "0"? */
335 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
336 return part_from_all_channels(Client, target);
338 /* Are channel keys given? */
340 key = strtok_r(Req->argv[1], ",", &lastkey);
342 channame = Req->argv[0];
343 channame = strtok_r(channame, ",", &lastchan);
345 /* Make sure that "channame" is not the empty string ("JOIN :") */
347 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
348 Client_ID(Client), Req->command);
353 /* Did the server include channel-user-modes? */
354 if (Client_Type(Client) == CLIENT_SERVER) {
355 flags = strchr(channame, 0x7);
362 chan = Channel_Search(channame);
363 if (!chan && Conf_PredefChannelsOnly) {
364 /* channel must be created, but forbidden by config */
365 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG,
366 Client_ID(Client), channame);
371 if (Client_Type(Client) == CLIENT_USER) {
373 /* Already existing channel: already member? */
374 if (Channel_IsMemberOf(chan, Client))
378 /* Test if the user has reached the channel limit */
379 if ((Conf_MaxJoins > 0) &&
380 (Channel_CountForUser(Client) >= Conf_MaxJoins))
381 return IRC_WriteStrClient(Client,
382 ERR_TOOMANYCHANNELS_MSG,
383 Client_ID(Client), channame);
385 /* Already existing channel: check if the
386 * client is allowed to join */
387 if (!join_allowed(Client, chan, channame, key))
390 /* New channel: first user will become channel
391 * operator unless this is a modeless channel */
392 if (*channame != '+')
396 /* Local client: update idle time */
397 Conn_UpdateIdle(Client_Conn(Client));
399 /* Remote server: we don't need to know whether the
400 * client is invited or not, but we have to make sure
401 * that the "one shot" entries (generated by INVITE
402 * commands) in this list become deleted when a user
403 * joins a channel this way. */
405 (void)Lists_Check(Channel_GetListInvites(chan),
409 /* Join channel (and create channel if it doesn't exist) */
410 if (!Channel_Join(target, channame))
413 if (!chan) { /* channel is new; it has been created above */
414 chan = Channel_Search(channame);
415 assert(chan != NULL);
416 if (Channel_IsModeless(chan)) {
417 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
418 Channel_ModeAdd(chan, 'n'); /* no external msgs */
421 assert(chan != NULL);
423 join_set_channelmodes(chan, target, flags);
425 join_forward(Client, target, chan, channame);
427 if (!join_send_topic(Client, target, chan, channame))
428 break; /* write error */
432 channame = strtok_r(NULL, ",", &lastchan);
434 key = strtok_r(NULL, ",", &lastkey);
441 * Handler for the IRC "PART" command.
443 * See RFC 2812, 3.2.2: "Part message".
445 * @param Client The client from which this command has been received
446 * @param Req Request structure with prefix and all parameters
447 * @returns CONNECTED or DISCONNECTED
450 IRC_PART(CLIENT * Client, REQUEST * Req)
455 assert(Client != NULL);
458 if (Req->argc < 1 || Req->argc > 2)
459 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
460 Client_ID(Client), Req->command);
463 if (Client_Type(Client) == CLIENT_SERVER)
464 target = Client_Search(Req->prefix);
468 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
469 Client_ID(Client), Req->prefix);
471 /* Loop over all the given channel names */
472 chan = strtok(Req->argv[0], ",");
474 /* Make sure that "chan" is not the empty string ("PART :") */
476 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
477 Client_ID(Client), Req->command);
480 Channel_Part(target, Client, chan,
481 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
482 chan = strtok(NULL, ",");
485 /* Update idle time, if local client */
486 if (Client_Conn(Client) > NONE)
487 Conn_UpdateIdle(Client_Conn(Client));
494 * Handler for the IRC "TOPIC" command.
496 * See RFC 2812, 3.2.4 "Topic message".
498 * @param Client The client from which this command has been received
499 * @param Req Request structure with prefix and all parameters
500 * @returns CONNECTED or DISCONNECTED
503 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
508 bool onchannel, topicok, use_servermode, r;
510 assert( Client != NULL );
511 assert( Req != NULL );
513 if (Req->argc < 1 || Req->argc > 2)
514 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
515 Client_ID(Client), Req->command);
517 if (Client_Type(Client) == CLIENT_SERVER)
518 from = Client_Search(Req->prefix);
523 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
524 Client_ID(Client), Req->prefix);
526 chan = Channel_Search(Req->argv[0]);
528 return IRC_WriteStrClient(from, ERR_NOSUCHCHANNEL_MSG,
529 Client_ID(from), Req->argv[0]);
531 Channel_CheckAdminRights(chan, Client, from,
532 &onchannel, &topicok, &use_servermode);
534 if (!onchannel && !topicok)
535 return IRC_WriteStrClient(from, ERR_NOTONCHANNEL_MSG,
536 Client_ID(from), Req->argv[0]);
538 if (Req->argc == 1) {
539 /* Request actual topic */
540 topic = Channel_Topic(chan);
542 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
544 Channel_Name(chan), topic);
548 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
551 Channel_TopicWho(chan),
552 Channel_TopicTime(chan));
557 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
562 if (strchr(Channel_Modes(chan), 't')) {
563 /* Topic Lock. Is the user a channel or IRC operator? */
565 return IRC_WriteStrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
571 Channel_SetTopic(chan, from, Req->argv[1]);
572 LogDebug("%s \"%s\" set topic on \"%s\": %s",
573 Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
574 Req->argv[1][0] ? Req->argv[1] : "<none>");
577 from = Client_ThisServer();
579 /* Update channel and forward new topic to other servers */
580 if (!Channel_IsLocal(chan))
581 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
582 Req->argv[0], Req->argv[1]);
583 IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
584 Req->argv[0], Req->argv[1]);
586 if (Client_Type(Client) == CLIENT_USER)
587 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
588 Req->argv[0], Req->argv[1]);
595 * Handler for the IRC "LIST" command.
597 * See RFC 2812, 3.2.6 "List message".
599 * This implementation handles the local case as well as the forwarding of the
600 * LIST command to other servers in the IRC network.
602 * @param Client The client from which this command has been received.
603 * @param Req Request structure with prefix and all parameters.
604 * @return CONNECTED or DISCONNECTED.
607 IRC_LIST( CLIENT *Client, REQUEST *Req )
611 CLIENT *from, *target;
614 assert(Client != NULL);
617 /* Bad number of prameters? */
619 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
620 Client_ID(Client), Req->command);
623 pattern = strtok(Req->argv[0], ",");
627 /* Get sender from prefix, if any */
628 if (Client_Type(Client) == CLIENT_SERVER)
629 from = Client_Search(Req->prefix);
634 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
635 Client_ID(Client), Req->prefix);
637 if (Req->argc == 2) {
638 /* Forward to other server? */
639 target = Client_Search(Req->argv[1]);
640 if (! target || Client_Type(target) != CLIENT_SERVER)
641 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
645 if (target != Client_ThisServer()) {
646 /* Target is indeed an other server, forward it! */
647 return IRC_WriteStrClientPrefix(target, from,
655 /* Loop through all the channels */
657 ngt_LowerStr(pattern);
658 chan = Channel_First();
660 /* Check search pattern */
661 if (MatchCaseInsensitive(pattern, Channel_Name(chan))) {
663 if (!strchr(Channel_Modes(chan), 's')
664 || Channel_IsMemberOf(chan, from)) {
665 if (IRC_CheckListTooBig(from, count,
669 if (!IRC_WriteStrClient(from,
670 RPL_LIST_MSG, Client_ID(from),
672 Channel_MemberCount(chan),
673 Channel_Topic( chan )))
678 chan = Channel_Next(chan);
681 /* Get next name ... */
683 pattern = strtok(NULL, ",");
688 IRC_SetPenalty(from, 2);
689 return IRC_WriteStrClient(from, RPL_LISTEND_MSG, Client_ID(from));
694 * Handler for the IRC+ command "CHANINFO".
696 * See doc/Protocol.txt, section II.3:
697 * "Exchange channel-modes, topics, and persistent channels".
699 * @param Client The client from which this command has been received
700 * @param Req Request structure with prefix and all parameters
701 * @returns CONNECTED or DISCONNECTED
704 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
706 char modes_add[COMMAND_LEN], l[16], *ptr;
711 assert( Client != NULL );
712 assert( Req != NULL );
714 /* Bad number of parameters? */
715 if (Req->argc < 2 || Req->argc == 4 || Req->argc > 5)
716 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
717 Client_ID(Client), Req->command);
719 /* Compatibility kludge */
720 if( Req->argc == 5 ) arg_topic = 4;
721 else if( Req->argc == 3 ) arg_topic = 2;
725 from = Client_Search( Req->prefix );
726 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
728 /* Search or create channel */
729 chan = Channel_Search( Req->argv[0] );
730 if( ! chan ) chan = Channel_Create( Req->argv[0] );
731 if( ! chan ) return CONNECTED;
733 if( Req->argv[1][0] == '+' )
735 ptr = Channel_Modes( chan );
738 /* OK, this channel doesn't have modes jet, set the received ones: */
739 Channel_SetModes( chan, &Req->argv[1][1] );
743 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
744 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
748 /* Delete modes which we never want to inherit */
749 Channel_ModeDel( chan, 'l' );
750 Channel_ModeDel( chan, 'k' );
753 strcpy( modes_add, "" );
754 ptr = Channel_Modes( chan );
759 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
760 strlcat( modes_add, l, sizeof( modes_add ));
764 strlcat( modes_add, " ", sizeof( modes_add ));
765 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
770 /* Inform members of this channel */
771 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
774 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
779 ptr = Channel_Topic( chan );
780 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
782 /* OK, there is no topic jet */
783 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
784 IRC_WriteStrChannelPrefix(Client, chan, from, false,
785 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
789 /* Forward CHANINFO to other serevrs */
790 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] );
791 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
792 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );