2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2013 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-macros.h"
37 #include "irc-write.h"
41 #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 */
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, is_exception;
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_exception = Lists_Check(Channel_GetListExcepts(chan), Client);
93 is_invited = Lists_Check(Channel_GetListInvites(chan), Client);
95 if (is_banned && !is_invited && !is_exception) {
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);
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 the channel is persistent (+P) and client is an IRC op:
169 * make client chanop, if not disabled in configuration. */
170 if (strchr(Channel_Modes(chan), 'P') && Conf_OperChanPAutoOp
171 && strchr(Client_Modes(target), 'o'))
172 Channel_UserModeAdd(chan, target, 'o');
173 } /* join_set_channelmodes */
176 * Forward JOIN command to a specific server
178 * This function differentiates 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 */
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));
256 * Acknowledge user JOIN request and send "channel info" numerics.
258 * @param Client Client used to prefix the genrated commands
259 * @param target Forward commands/numerics to this user
260 * @param chan Channel structure
261 * @param channame Channel name
264 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
265 const char *channame)
269 if (Client_Type(Client) != CLIENT_USER)
271 /* acknowledge join */
272 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
275 /* Send topic to client, if any */
276 topic = Channel_Topic(chan);
277 assert(topic != NULL);
279 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
280 Client_ID(Client), channame, topic))
283 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
284 Client_ID(Client), channame,
285 Channel_TopicWho(chan),
286 Channel_TopicTime(chan)))
290 /* send list of channel members to client */
291 if (!IRC_Send_NAMES(Client, chan))
293 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client),
295 } /* join_send_topic */
298 * Handler for the IRC "JOIN" command.
300 * @param Client The client from which this command has been received.
301 * @param Req Request structure with prefix and all parameters.
302 * @return CONNECTED or DISCONNECTED.
305 IRC_JOIN( CLIENT *Client, REQUEST *Req )
307 char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
311 assert (Client != NULL);
312 assert (Req != NULL);
314 _IRC_ARGC_GE_OR_RETURN_(Client, Req, 1)
315 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
316 _IRC_GET_SENDER_OR_RETURN_(target, Req, Client)
318 /* Is argument "0"? */
319 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
320 return part_from_all_channels(Client, target);
322 /* Are channel keys given? */
324 key = strtok_r(Req->argv[1], ",", &lastkey);
326 channame = Req->argv[0];
327 channame = strtok_r(channame, ",", &lastchan);
329 /* Make sure that "channame" is not the empty string ("JOIN :") */
331 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
332 Client_ID(Client), Req->command);
337 /* Did the server include channel-user-modes? */
338 if (Client_Type(Client) == CLIENT_SERVER) {
339 flags = strchr(channame, 0x7);
346 chan = Channel_Search(channame);
347 if (!chan && Conf_PredefChannelsOnly) {
348 /* channel must be created, but forbidden by config */
349 IRC_WriteStrClient(Client, ERR_NOSUCHCHANNEL_MSG,
350 Client_ID(Client), channame);
355 if (Client_Type(Client) == CLIENT_USER) {
357 /* Already existing channel: already member? */
358 if (Channel_IsMemberOf(chan, Client))
362 /* Test if the user has reached the channel limit */
363 if ((Conf_MaxJoins > 0) &&
364 (Channel_CountForUser(Client) >= Conf_MaxJoins)) {
365 if (!IRC_WriteStrClient(Client,
366 ERR_TOOMANYCHANNELS_MSG,
367 Client_ID(Client), channame))
373 /* Already existing channel: check if the
374 * client is allowed to join */
375 if (!join_allowed(Client, chan, channame, key))
378 /* New channel: first user will become channel
379 * operator unless this is a modeless channel */
380 if (*channame != '+')
384 /* Local client: update idle time */
385 Conn_UpdateIdle(Client_Conn(Client));
387 /* Remote server: we don't need to know whether the
388 * client is invited or not, but we have to make sure
389 * that the "one shot" entries (generated by INVITE
390 * commands) in this list become deleted when a user
391 * joins a channel this way. */
393 (void)Lists_Check(Channel_GetListInvites(chan),
397 /* Join channel (and create channel if it doesn't exist) */
398 if (!Channel_Join(target, channame))
401 if (!chan) { /* channel is new; it has been created above */
402 chan = Channel_Search(channame);
403 assert(chan != NULL);
404 if (Channel_IsModeless(chan)) {
405 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
406 Channel_ModeAdd(chan, 'n'); /* no external msgs */
409 assert(chan != NULL);
411 join_set_channelmodes(chan, target, flags);
413 join_forward(Client, target, chan, channame);
415 if (!join_send_topic(Client, target, chan, channame))
416 break; /* write error */
420 channame = strtok_r(NULL, ",", &lastchan);
422 key = strtok_r(NULL, ",", &lastkey);
428 * Handler for the IRC "PART" command.
430 * @param Client The client from which this command has been received.
431 * @param Req Request structure with prefix and all parameters.
432 * @return CONNECTED or DISCONNECTED.
435 IRC_PART(CLIENT * Client, REQUEST * Req)
440 assert(Client != NULL);
443 _IRC_ARGC_GE_OR_RETURN_(Client, Req, 1)
444 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
445 _IRC_GET_SENDER_OR_RETURN_(target, Req, Client)
447 /* Loop over all the given channel names */
448 chan = strtok(Req->argv[0], ",");
450 /* Make sure that "chan" is not the empty string ("PART :") */
452 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
453 Client_ID(Client), Req->command);
456 Channel_Part(target, Client, chan,
457 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
458 chan = strtok(NULL, ",");
461 /* Update idle time, if local client */
462 if (Client_Conn(Client) > NONE)
463 Conn_UpdateIdle(Client_Conn(Client));
469 * Handler for the IRC "TOPIC" command.
471 * @param Client The client from which this command has been received.
472 * @param Req Request structure with prefix and all parameters.
473 * @return CONNECTED or DISCONNECTED.
476 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
483 assert( Client != NULL );
484 assert( Req != NULL );
486 IRC_SetPenalty(Client, 1);
488 _IRC_ARGC_GE_OR_RETURN_(Client, Req, 1)
489 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
490 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
492 chan = Channel_Search(Req->argv[0]);
494 return IRC_WriteStrClient(from, ERR_NOSUCHCHANNEL_MSG,
495 Client_ID(from), Req->argv[0]);
497 /* Only remote servers and channel members are allowed to change the
498 * channel topic, and IRC operators when the Conf_OperCanMode option
499 * is set in the server configuration. */
500 if (Client_Type(Client) != CLIENT_SERVER) {
501 topic_power = Client_HasMode(from, 'o');
502 if (!Channel_IsMemberOf(chan, from)
503 && !(Conf_OperCanMode && topic_power))
504 return IRC_WriteStrClient(from, ERR_NOTONCHANNEL_MSG,
505 Client_ID(from), Req->argv[0]);
509 if (Req->argc == 1) {
510 /* Request actual topic */
511 topic = Channel_Topic(chan);
513 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
515 Channel_Name(chan), topic);
519 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
522 Channel_TopicWho(chan),
523 Channel_TopicTime(chan));
528 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
533 if (strchr(Channel_Modes(chan), 't')) {
534 /* Topic Lock. Is the user a channel op or IRC operator? */
536 !strchr(Channel_UserModes(chan, from), 'h') &&
537 !strchr(Channel_UserModes(chan, from), 'o') &&
538 !strchr(Channel_UserModes(chan, from), 'a') &&
539 !strchr(Channel_UserModes(chan, from), 'q'))
540 return IRC_WriteStrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
546 Channel_SetTopic(chan, from, Req->argv[1]);
547 LogDebug("%s \"%s\" set topic on \"%s\": %s",
548 Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
549 Req->argv[1][0] ? Req->argv[1] : "<none>");
551 if (Conf_OperServerMode)
552 from = Client_ThisServer();
554 /* Update channel and forward new topic to other servers */
555 if (!Channel_IsLocal(chan))
556 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
557 Req->argv[0], Req->argv[1]);
558 IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
559 Req->argv[0], Req->argv[1]);
561 if (Client_Type(Client) == CLIENT_USER)
562 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
563 Req->argv[0], Req->argv[1]);
569 * Handler for the IRC "LIST" command.
571 * @param Client The client from which this command has been received.
572 * @param Req Request structure with prefix and all parameters.
573 * @return CONNECTED or DISCONNECTED.
576 IRC_LIST( CLIENT *Client, REQUEST *Req )
580 CLIENT *from, *target;
583 assert(Client != NULL);
586 IRC_SetPenalty(Client, 2);
588 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
589 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
592 pattern = strtok(Req->argv[0], ",");
596 if (Req->argc == 2) {
597 /* Forward to other server? */
598 target = Client_Search(Req->argv[1]);
599 if (! target || Client_Type(target) != CLIENT_SERVER)
600 return IRC_WriteStrClient(from, ERR_NOSUCHSERVER_MSG,
604 if (target != Client_ThisServer()) {
605 /* Target is indeed an other server, forward it! */
606 return IRC_WriteStrClientPrefix(target, from,
614 /* Loop through all the channels */
616 ngt_LowerStr(pattern);
617 chan = Channel_First();
619 /* Check search pattern */
620 if (MatchCaseInsensitive(pattern, Channel_Name(chan))) {
622 if (!strchr(Channel_Modes(chan), 's')
623 || Channel_IsMemberOf(chan, from)
624 || (!Conf_MorePrivacy && Client_OperByMe(Client))) {
625 if ((Conf_MaxListSize > 0)
626 && IRC_CheckListTooBig(from, count,
630 if (!IRC_WriteStrClient(from,
631 RPL_LIST_MSG, Client_ID(from),
633 Channel_MemberCount(chan),
634 Channel_Topic( chan )))
639 chan = Channel_Next(chan);
642 /* Get next name ... */
644 pattern = strtok(NULL, ",");
649 return IRC_WriteStrClient(from, RPL_LISTEND_MSG, Client_ID(from));
653 * Handler for the IRC+ "CHANINFO" command.
655 * @param Client The client from which this command has been received.
656 * @param Req Request structure with prefix and all parameters.
657 * @return CONNECTED or DISCONNECTED.
660 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
662 char modes_add[COMMAND_LEN], l[16], *ptr;
667 assert( Client != NULL );
668 assert( Req != NULL );
670 /* Bad number of parameters? */
671 if (Req->argc < 2 || Req->argc == 4 || Req->argc > 5)
672 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
673 Client_ID(Client), Req->command);
675 /* Compatibility kludge */
678 else if(Req->argc == 3)
683 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
685 /* Search or create channel */
686 chan = Channel_Search( Req->argv[0] );
688 chan = Channel_Create( Req->argv[0] );
692 if (Req->argv[1][0] == '+') {
693 ptr = Channel_Modes(chan);
695 /* OK, this channel doesn't have modes jet,
696 * set the received ones: */
697 Channel_SetModes(chan, &Req->argv[1][1]);
700 if(strchr(Channel_Modes(chan), 'k'))
701 Channel_SetKey(chan, Req->argv[2]);
702 if(strchr(Channel_Modes(chan), 'l'))
703 Channel_SetMaxUsers(chan, atol(Req->argv[3]));
705 /* Delete modes which we never want to inherit */
706 Channel_ModeDel(chan, 'l');
707 Channel_ModeDel(chan, 'k');
710 strcpy(modes_add, "");
711 ptr = Channel_Modes(chan);
714 snprintf(l, sizeof(l), " %lu",
715 Channel_MaxUsers(chan));
716 strlcat(modes_add, l,
720 strlcat(modes_add, " ",
722 strlcat(modes_add, Channel_Key(chan),
728 /* Inform members of this channel */
729 IRC_WriteStrChannelPrefix(Client, chan, from, false,
730 "MODE %s +%s%s", Req->argv[0],
731 Channel_Modes(chan), modes_add);
735 Log(LOG_WARNING, "CHANINFO: invalid MODE format ignored!");
739 ptr = Channel_Topic( chan );
740 if (!*ptr && Req->argv[arg_topic][0]) {
741 /* OK, there is no topic jet */
742 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
743 IRC_WriteStrChannelPrefix(Client, chan, from, false,
744 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
748 /* Forward CHANINFO to other servers */
750 IRC_WriteStrServersPrefixFlag(Client, from, 'C',
751 "CHANINFO %s %s %s %s :%s",
752 Req->argv[0], Req->argv[1],
753 Req->argv[2], Req->argv[3],
755 else if (Req->argc == 3)
756 IRC_WriteStrServersPrefixFlag(Client, from, 'C',
757 "CHANINFO %s %s :%s",
758 Req->argv[0], Req->argv[1],
761 IRC_WriteStrServersPrefixFlag(Client, from, 'C',
763 Req->argv[0], Req->argv[1]);