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 channel persistent and client is ircop: make client chanop */
171 if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
172 Channel_UserModeAdd(chan, target, 'o');
173 } /* join_set_channelmodes */
177 * Forward JOIN command to a specific server
179 * This function diffentiates between servers using RFC 2813 mode that
180 * support the JOIN command with appended ASCII 7 character and channel
181 * modes, and servers using RFC 1459 protocol which require separate JOIN
184 * @param To Forward JOIN (and MODE) command to this peer server
185 * @param Prefix Client used to prefix the genrated commands
186 * @param Data Parameters of JOIN command to forward, probably
187 * containing channel modes separated by ASCII 7.
190 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
193 char str[COMMAND_LEN], *ptr = NULL;
195 strlcpy(str, (char *)Data, sizeof(str));
196 conn = Client_Conn(To);
198 if (Conn_Options(conn) & CONN_RFC1459) {
199 /* RFC 1459 compatibility mode, appended modes are NOT
200 * supported, so strip them off! */
201 ptr = strchr(str, 0x7);
206 IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
208 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
210 } /* cb_join_forward */
214 * Forward JOIN command to all servers
216 * This function calls cb_join_forward(), which differentiates between
217 * protocol implementations (e.g. RFC 2812, RFC 1459).
219 * @param Client Client used to prefix the genrated commands
220 * @param target Forward JOIN (and MODE) command to this peer server
221 * @param chan Channel structure
222 * @param channame Channel name
225 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
226 const char *channame)
228 char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
230 /* RFC 2813, 4.2.1: channel modes are separated from the channel
231 * name with ASCII 7, if any, and not spaces: */
232 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
238 /* forward to other servers (if it is not a local channel) */
239 if (!Channel_IsLocal(chan)) {
240 snprintf(str, sizeof(str), "%s%s", channame, modes);
241 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
242 cb_join_forward, str);
245 /* tell users in this channel about the new client */
246 IRC_WriteStrChannelPrefix(Client, chan, target, false,
247 "JOIN :%s", channame);
249 /* synchronize channel modes */
251 IRC_WriteStrChannelPrefix(Client, chan, target, false,
252 "MODE %s +%s %s", channame,
253 &modes[1], Client_ID(target));
259 * Aknowledge user JOIN request and send "channel info" numerics.
261 * @param Client Client used to prefix the genrated commands
262 * @param target Forward commands/numerics to this user
263 * @param chan Channel structure
264 * @param channame Channel name
267 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
268 const char *channame)
272 if (Client_Type(Client) != CLIENT_USER)
274 /* acknowledge join */
275 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
278 /* Send topic to client, if any */
279 topic = Channel_Topic(chan);
280 assert(topic != NULL);
282 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
283 Client_ID(Client), channame, topic))
286 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
287 Client_ID(Client), channame,
288 Channel_TopicWho(chan),
289 Channel_TopicTime(chan)))
293 /* send list of channel members to client */
294 if (!IRC_Send_NAMES(Client, chan))
296 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client),
298 } /* join_send_topic */
302 * Handler for the IRC "JOIN" command.
304 * See RFC 2812, 3.2.1 "Join message"; RFC 2813, 4.2.1 "Join message".
306 * @param Client The client from which this command has been received
307 * @param Req Request structure with prefix and all parameters
308 * @returns CONNECTED or DISCONNECTED
311 IRC_JOIN( CLIENT *Client, REQUEST *Req )
313 char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
317 assert (Client != NULL);
318 assert (Req != NULL);
320 /* Bad number of arguments? */
321 if (Req->argc < 1 || Req->argc > 2)
322 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
323 Client_ID(Client), Req->command);
325 /* Who is the sender? */
326 if (Client_Type(Client) == CLIENT_SERVER)
327 target = Client_Search(Req->prefix);
332 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
333 Client_ID(Client), Req->prefix);
335 /* Is argument "0"? */
336 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
337 return part_from_all_channels(Client, target);
339 /* Are channel keys given? */
341 key = strtok_r(Req->argv[1], ",", &lastkey);
343 channame = Req->argv[0];
344 channame = strtok_r(channame, ",", &lastchan);
346 /* Make sure that "channame" is not the empty string ("JOIN :") */
348 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
349 Client_ID(Client), Req->command);
354 /* Did the server include channel-user-modes? */
355 if (Client_Type(Client) == CLIENT_SERVER) {
356 flags = strchr(channame, 0x7);
363 chan = Channel_Search(channame);
364 if (!chan && Conf_PredefChannelsOnly) {
365 /* channel must be created, but forbidden by config */
366 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG,
367 Client_ID(Client), channame);
372 if (Client_Type(Client) == CLIENT_USER) {
374 /* Already existing channel: already member? */
375 if (Channel_IsMemberOf(chan, Client))
379 /* Test if the user has reached the channel limit */
380 if ((Conf_MaxJoins > 0) &&
381 (Channel_CountForUser(Client) >= Conf_MaxJoins)) {
382 if (!IRC_WriteStrClient(Client,
383 ERR_TOOMANYCHANNELS_MSG,
384 Client_ID(Client), channame))
390 /* Already existing channel: check if the
391 * client is allowed to join */
392 if (!join_allowed(Client, chan, channame, key))
395 /* New channel: first user will become channel
396 * operator unless this is a modeless channel */
397 if (*channame != '+')
401 /* Local client: update idle time */
402 Conn_UpdateIdle(Client_Conn(Client));
404 /* Remote server: we don't need to know whether the
405 * client is invited or not, but we have to make sure
406 * that the "one shot" entries (generated by INVITE
407 * commands) in this list become deleted when a user
408 * joins a channel this way. */
410 (void)Lists_Check(Channel_GetListInvites(chan),
414 /* Join channel (and create channel if it doesn't exist) */
415 if (!Channel_Join(target, channame))
418 if (!chan) { /* channel is new; it has been created above */
419 chan = Channel_Search(channame);
420 assert(chan != NULL);
421 if (Channel_IsModeless(chan)) {
422 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
423 Channel_ModeAdd(chan, 'n'); /* no external msgs */
426 assert(chan != NULL);
428 join_set_channelmodes(chan, target, flags);
430 join_forward(Client, target, chan, channame);
432 if (!join_send_topic(Client, target, chan, channame))
433 break; /* write error */
437 channame = strtok_r(NULL, ",", &lastchan);
439 key = strtok_r(NULL, ",", &lastkey);
446 * Handler for the IRC "PART" command.
448 * See RFC 2812, 3.2.2: "Part message".
450 * @param Client The client from which this command has been received
451 * @param Req Request structure with prefix and all parameters
452 * @returns CONNECTED or DISCONNECTED
455 IRC_PART(CLIENT * Client, REQUEST * Req)
460 assert(Client != NULL);
463 if (Req->argc < 1 || Req->argc > 2)
464 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
465 Client_ID(Client), Req->command);
468 if (Client_Type(Client) == CLIENT_SERVER)
469 target = Client_Search(Req->prefix);
473 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
474 Client_ID(Client), Req->prefix);
476 /* Loop over all the given channel names */
477 chan = strtok(Req->argv[0], ",");
479 /* Make sure that "chan" is not the empty string ("PART :") */
481 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
482 Client_ID(Client), Req->command);
485 Channel_Part(target, Client, chan,
486 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
487 chan = strtok(NULL, ",");
490 /* Update idle time, if local client */
491 if (Client_Conn(Client) > NONE)
492 Conn_UpdateIdle(Client_Conn(Client));
499 * Handler for the IRC "TOPIC" command.
501 * See RFC 2812, 3.2.4 "Topic message".
503 * @param Client The client from which this command has been received
504 * @param Req Request structure with prefix and all parameters
505 * @returns CONNECTED or DISCONNECTED
508 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
513 bool onchannel, topicok, use_servermode, r;
515 assert( Client != NULL );
516 assert( Req != NULL );
518 if (Req->argc < 1 || Req->argc > 2)
519 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
520 Client_ID(Client), Req->command);
522 if (Client_Type(Client) == CLIENT_SERVER)
523 from = Client_Search(Req->prefix);
528 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
529 Client_ID(Client), Req->prefix);
531 chan = Channel_Search(Req->argv[0]);
533 return IRC_WriteStrClient(from, ERR_NOSUCHCHANNEL_MSG,
534 Client_ID(from), Req->argv[0]);
536 Channel_CheckAdminRights(chan, Client, from,
537 &onchannel, &topicok, &use_servermode);
539 if (!onchannel && !topicok)
540 return IRC_WriteStrClient(from, ERR_NOTONCHANNEL_MSG,
541 Client_ID(from), Req->argv[0]);
543 if (Req->argc == 1) {
544 /* Request actual topic */
545 topic = Channel_Topic(chan);
547 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
549 Channel_Name(chan), topic);
553 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
556 Channel_TopicWho(chan),
557 Channel_TopicTime(chan));
562 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
567 if (strchr(Channel_Modes(chan), 't')) {
568 /* Topic Lock. Is the user a channel or IRC operator? */
570 return IRC_WriteStrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
576 Channel_SetTopic(chan, from, Req->argv[1]);
577 LogDebug("%s \"%s\" set topic on \"%s\": %s",
578 Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
579 Req->argv[1][0] ? Req->argv[1] : "<none>");
582 from = Client_ThisServer();
584 /* Update channel and forward new topic to other servers */
585 if (!Channel_IsLocal(chan))
586 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
587 Req->argv[0], Req->argv[1]);
588 IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
589 Req->argv[0], Req->argv[1]);
591 if (Client_Type(Client) == CLIENT_USER)
592 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
593 Req->argv[0], Req->argv[1]);
600 * Handler for the IRC "LIST" command.
602 * See RFC 2812, 3.2.6 "List message".
604 * This implementation handles the local case as well as the forwarding of the
605 * LIST command to other servers in the IRC network.
607 * @param Client The client from which this command has been received.
608 * @param Req Request structure with prefix and all parameters.
609 * @return CONNECTED or DISCONNECTED.
612 IRC_LIST( CLIENT *Client, REQUEST *Req )
616 CLIENT *from, *target;
619 assert(Client != NULL);
622 /* Bad number of prameters? */
624 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
625 Client_ID(Client), Req->command);
628 pattern = strtok(Req->argv[0], ",");
632 /* Get sender from prefix, if any */
633 if (Client_Type(Client) == CLIENT_SERVER)
634 from = Client_Search(Req->prefix);
639 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
640 Client_ID(Client), Req->prefix);
642 if (Req->argc == 2) {
643 /* Forward to other server? */
644 target = Client_Search(Req->argv[1]);
645 if (! target || Client_Type(target) != CLIENT_SERVER)
646 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
650 if (target != Client_ThisServer()) {
651 /* Target is indeed an other server, forward it! */
652 return IRC_WriteStrClientPrefix(target, from,
660 /* Loop through all the channels */
662 ngt_LowerStr(pattern);
663 chan = Channel_First();
665 /* Check search pattern */
666 if (MatchCaseInsensitive(pattern, Channel_Name(chan))) {
668 if (!strchr(Channel_Modes(chan), 's')
669 || Channel_IsMemberOf(chan, from)) {
670 if (IRC_CheckListTooBig(from, count,
674 if (!IRC_WriteStrClient(from,
675 RPL_LIST_MSG, Client_ID(from),
677 Channel_MemberCount(chan),
678 Channel_Topic( chan )))
683 chan = Channel_Next(chan);
686 /* Get next name ... */
688 pattern = strtok(NULL, ",");
693 IRC_SetPenalty(from, 2);
694 return IRC_WriteStrClient(from, RPL_LISTEND_MSG, Client_ID(from));
699 * Handler for the IRC+ command "CHANINFO".
701 * See doc/Protocol.txt, section II.3:
702 * "Exchange channel-modes, topics, and persistent channels".
704 * @param Client The client from which this command has been received
705 * @param Req Request structure with prefix and all parameters
706 * @returns CONNECTED or DISCONNECTED
709 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
711 char modes_add[COMMAND_LEN], l[16], *ptr;
716 assert( Client != NULL );
717 assert( Req != NULL );
719 /* Bad number of parameters? */
720 if (Req->argc < 2 || Req->argc == 4 || Req->argc > 5)
721 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
722 Client_ID(Client), Req->command);
724 /* Compatibility kludge */
725 if( Req->argc == 5 ) arg_topic = 4;
726 else if( Req->argc == 3 ) arg_topic = 2;
730 from = Client_Search( Req->prefix );
731 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
733 /* Search or create channel */
734 chan = Channel_Search( Req->argv[0] );
735 if( ! chan ) chan = Channel_Create( Req->argv[0] );
736 if( ! chan ) return CONNECTED;
738 if( Req->argv[1][0] == '+' )
740 ptr = Channel_Modes( chan );
743 /* OK, this channel doesn't have modes jet, set the received ones: */
744 Channel_SetModes( chan, &Req->argv[1][1] );
748 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
749 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
753 /* Delete modes which we never want to inherit */
754 Channel_ModeDel( chan, 'l' );
755 Channel_ModeDel( chan, 'k' );
758 strcpy( modes_add, "" );
759 ptr = Channel_Modes( chan );
764 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
765 strlcat( modes_add, l, sizeof( modes_add ));
769 strlcat( modes_add, " ", sizeof( modes_add ));
770 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
775 /* Inform members of this channel */
776 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
779 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
784 ptr = Channel_Topic( chan );
785 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
787 /* OK, there is no topic jet */
788 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
789 IRC_WriteStrChannelPrefix(Client, chan, from, false,
790 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
794 /* Forward CHANINFO to other serevrs */
795 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] );
796 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
797 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );