2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
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;
86 /* Allow IRC operators to overwrite channel limits */
87 if (Client_HasMode(Client, 'o'))
90 is_banned = Lists_Check(Channel_GetListBans(chan), Client);
91 is_exception = Lists_Check(Channel_GetListExcepts(chan), Client);
92 is_invited = Lists_Check(Channel_GetListInvites(chan), Client);
94 if (is_banned && !is_invited && !is_exception) {
95 /* Client is banned from channel (and not on invite list) */
96 IRC_WriteErrClient(Client, ERR_BANNEDFROMCHAN_MSG,
97 Client_ID(Client), channame);
101 if (Channel_HasMode(chan, 'i') && !is_invited) {
102 /* Channel is "invite-only" and client is not on invite list */
103 IRC_WriteErrClient(Client, ERR_INVITEONLYCHAN_MSG,
104 Client_ID(Client), channame);
108 if (!Channel_CheckKey(chan, Client, key ? key : "")) {
109 /* Channel is protected by a channel key and the client
110 * didn't specify the correct one */
111 IRC_WriteErrClient(Client, ERR_BADCHANNELKEY_MSG,
112 Client_ID(Client), channame);
116 if (Channel_HasMode(chan, 'l') &&
117 (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
118 /* There are more clints joined to this channel than allowed */
119 IRC_WriteErrClient(Client, ERR_CHANNELISFULL_MSG,
120 Client_ID(Client), channame);
124 if (Channel_HasMode(chan, 'z') && !Conn_UsesSSL(Client_Conn(Client))) {
125 /* Only "secure" clients are allowed, but clients doesn't
126 * use SSL encryption */
127 IRC_WriteErrClient(Client, ERR_SECURECHANNEL_MSG,
128 Client_ID(Client), channame);
132 if (Channel_HasMode(chan, 'O') && !Client_OperByMe(Client)) {
133 /* Only IRC operators are allowed! */
134 IRC_WriteErrClient(Client, ERR_OPONLYCHANNEL_MSG,
135 Client_ID(Client), channame);
139 if (Channel_HasMode(chan, 'R') && !Client_HasMode(Client, 'R')) {
140 /* Only registered users are allowed! */
141 IRC_WriteErrClient(Client, ERR_REGONLYCHANNEL_MSG,
142 Client_ID(Client), channame);
150 * Set user channel modes.
152 * @param chan Channel
153 * @param target User to set modes for
154 * @param flags Channel modes to add
157 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
161 Channel_UserModeAdd(chan, target, *flags);
166 /* If the channel is persistent (+P) and client is an IRC op:
167 * make client chanop, if not disabled in configuration. */
168 if (Channel_HasMode(chan, 'P') && Conf_OperChanPAutoOp
169 && Client_HasMode(target, 'o'))
170 Channel_UserModeAdd(chan, target, 'o');
171 } /* join_set_channelmodes */
174 * Forward JOIN command to a specific server
176 * This function differentiates between servers using RFC 2813 mode that
177 * support the JOIN command with appended ASCII 7 character and channel
178 * modes, and servers using RFC 1459 protocol which require separate JOIN
181 * @param To Forward JOIN (and MODE) command to this peer server
182 * @param Prefix Client used to prefix the genrated commands
183 * @param Data Parameters of JOIN command to forward, probably
184 * containing channel modes separated by ASCII 7.
187 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
190 char str[COMMAND_LEN], *ptr = NULL;
192 strlcpy(str, (char *)Data, sizeof(str));
193 conn = Client_Conn(To);
195 if (Conn_Options(conn) & CONN_RFC1459) {
196 /* RFC 1459 compatibility mode, appended modes are NOT
197 * supported, so strip them off! */
198 ptr = strchr(str, 0x7);
203 IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
205 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
207 } /* cb_join_forward */
210 * Forward JOIN command to all servers
212 * This function calls cb_join_forward(), which differentiates between
213 * protocol implementations (e.g. RFC 2812, RFC 1459).
215 * @param Client Client used to prefix the genrated commands
216 * @param target Forward JOIN (and MODE) command to this peer server
217 * @param chan Channel structure
218 * @param channame Channel name
221 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
222 const char *channame)
224 char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
226 /* RFC 2813, 4.2.1: channel modes are separated from the channel
227 * name with ASCII 7, if any, and not spaces: */
228 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
234 /* forward to other servers (if it is not a local channel) */
235 if (!Channel_IsLocal(chan)) {
236 snprintf(str, sizeof(str), "%s%s", channame, modes);
237 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
238 cb_join_forward, str);
241 /* tell users in this channel about the new client */
242 IRC_WriteStrChannelPrefix(Client, chan, target, false,
243 "JOIN :%s", channame);
245 /* synchronize channel modes */
247 IRC_WriteStrChannelPrefix(Client, chan, target, false,
248 "MODE %s +%s %s", channame,
249 &modes[1], Client_ID(target));
254 * Acknowledge user JOIN request and send "channel info" numerics.
256 * @param Client Client used to prefix the genrated commands
257 * @param target Forward commands/numerics to this user
258 * @param chan Channel structure
259 * @param channame Channel name
262 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
263 const char *channame)
267 if (Client_Type(Client) != CLIENT_USER)
269 /* acknowledge join */
270 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
273 /* Send topic to client, if any */
274 topic = Channel_Topic(chan);
275 assert(topic != NULL);
277 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
278 Client_ID(Client), channame, topic))
281 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
282 Client_ID(Client), channame,
283 Channel_TopicWho(chan),
284 Channel_TopicTime(chan)))
288 /* send list of channel members to client */
289 if (!IRC_Send_NAMES(Client, chan))
291 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client),
293 } /* join_send_topic */
296 * Handler for the IRC "JOIN" command.
298 * @param Client The client from which this command has been received.
299 * @param Req Request structure with prefix and all parameters.
300 * @return CONNECTED or DISCONNECTED.
303 IRC_JOIN( CLIENT *Client, REQUEST *Req )
305 char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
309 assert (Client != NULL);
310 assert (Req != NULL);
312 _IRC_ARGC_BETWEEN_OR_RETURN_(Client, Req, 1, 2)
313 _IRC_GET_SENDER_OR_RETURN_(target, Req, Client)
315 /* Is argument "0"? */
316 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
317 return part_from_all_channels(Client, target);
319 /* Are channel keys given? */
321 key = strtok_r(Req->argv[1], ",", &lastkey);
323 channame = Req->argv[0];
324 channame = strtok_r(channame, ",", &lastchan);
326 /* Make sure that "channame" is not the empty string ("JOIN :") */
328 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
329 Client_ID(Client), Req->command);
334 /* Did the server include channel-user-modes? */
335 if (Client_Type(Client) == CLIENT_SERVER) {
336 flags = strchr(channame, 0x7);
343 chan = Channel_Search(channame);
344 if (!chan && !strchr(Conf_AllowedChannelTypes, channame[0])) {
345 /* channel must be created, but forbidden by config */
346 IRC_WriteErrClient(Client, ERR_NOSUCHCHANNEL_MSG,
347 Client_ID(Client), channame);
352 if (Client_Type(Client) == CLIENT_USER) {
354 /* Already existing channel: already member? */
355 if (Channel_IsMemberOf(chan, Client))
359 /* Test if the user has reached the channel limit */
360 if ((Conf_MaxJoins > 0) &&
361 (Channel_CountForUser(Client) >= Conf_MaxJoins)) {
362 if (!IRC_WriteErrClient(Client,
363 ERR_TOOMANYCHANNELS_MSG,
364 Client_ID(Client), channame))
370 /* Already existing channel: check if the
371 * client is allowed to join */
372 if (!join_allowed(Client, chan, channame, key))
375 /* New channel: first user will become channel
376 * operator unless this is a modeless channel */
377 if (*channame != '+')
381 /* Local client: update idle time */
382 Conn_UpdateIdle(Client_Conn(Client));
384 /* Remote server: we don't need to know whether the
385 * client is invited or not, but we have to make sure
386 * that the "one shot" entries (generated by INVITE
387 * commands) in this list become deleted when a user
388 * joins a channel this way. */
390 (void)Lists_Check(Channel_GetListInvites(chan),
394 /* Join channel (and create channel if it doesn't exist) */
395 if (!Channel_Join(target, channame))
398 if (!chan) { /* channel is new; it has been created above */
399 chan = Channel_Search(channame);
400 assert(chan != NULL);
401 if (Channel_IsModeless(chan)) {
402 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
403 Channel_ModeAdd(chan, 'n'); /* no external msgs */
406 assert(chan != NULL);
408 join_set_channelmodes(chan, target, flags);
410 join_forward(Client, target, chan, channame);
412 if (!join_send_topic(Client, target, chan, channame))
413 break; /* write error */
417 channame = strtok_r(NULL, ",", &lastchan);
419 key = strtok_r(NULL, ",", &lastkey);
425 * Handler for the IRC "PART" command.
427 * @param Client The client from which this command has been received.
428 * @param Req Request structure with prefix and all parameters.
429 * @return CONNECTED or DISCONNECTED.
432 IRC_PART(CLIENT * Client, REQUEST * Req)
437 assert(Client != NULL);
440 _IRC_ARGC_BETWEEN_OR_RETURN_(Client, Req, 1, 2)
441 _IRC_GET_SENDER_OR_RETURN_(target, Req, Client)
443 /* Loop over all the given channel names */
444 chan = strtok(Req->argv[0], ",");
446 /* Make sure that "chan" is not the empty string ("PART :") */
448 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
449 Client_ID(Client), Req->command);
452 Channel_Part(target, Client, chan,
453 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
454 chan = strtok(NULL, ",");
457 /* Update idle time, if local client */
458 if (Client_Conn(Client) > NONE)
459 Conn_UpdateIdle(Client_Conn(Client));
465 * Handler for the IRC "TOPIC" command.
467 * @param Client The client from which this command has been received.
468 * @param Req Request structure with prefix and all parameters.
469 * @return CONNECTED or DISCONNECTED.
472 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
479 assert( Client != NULL );
480 assert( Req != NULL );
482 IRC_SetPenalty(Client, 1);
484 _IRC_ARGC_BETWEEN_OR_RETURN_(Client, Req, 1, 2)
485 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
487 chan = Channel_Search(Req->argv[0]);
489 return IRC_WriteErrClient(from, ERR_NOSUCHCHANNEL_MSG,
490 Client_ID(from), Req->argv[0]);
492 /* Only remote servers and channel members are allowed to change the
493 * channel topic, and IRC operators when the Conf_OperCanMode option
494 * is set in the server configuration. */
495 if (Client_Type(Client) != CLIENT_SERVER) {
496 topic_power = Client_HasMode(from, 'o');
497 if (!Channel_IsMemberOf(chan, from)
498 && !(Conf_OperCanMode && topic_power))
499 return IRC_WriteErrClient(from, ERR_NOTONCHANNEL_MSG,
500 Client_ID(from), Req->argv[0]);
504 if (Req->argc == 1) {
505 /* Request actual topic */
506 topic = Channel_Topic(chan);
508 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
510 Channel_Name(chan), topic);
514 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
517 Channel_TopicWho(chan),
518 Channel_TopicTime(chan));
523 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
528 if (Channel_HasMode(chan, 't')) {
529 /* Topic Lock. Is the user a channel op or IRC operator? */
531 !Channel_UserHasMode(chan, from, 'h') &&
532 !Channel_UserHasMode(chan, from, 'o') &&
533 !Channel_UserHasMode(chan, from, 'a') &&
534 !Channel_UserHasMode(chan, from, 'q'))
535 return IRC_WriteErrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
541 Channel_SetTopic(chan, from, Req->argv[1]);
542 LogDebug("%s \"%s\" set topic on \"%s\": %s",
543 Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
544 Req->argv[1][0] ? Req->argv[1] : "<none>");
546 if (Conf_OperServerMode)
547 from = Client_ThisServer();
549 /* Update channel and forward new topic to other servers */
550 if (!Channel_IsLocal(chan))
551 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
552 Req->argv[0], Req->argv[1]);
553 IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
554 Req->argv[0], Req->argv[1]);
556 if (Client_Type(Client) == CLIENT_USER)
557 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
558 Req->argv[0], Req->argv[1]);
564 * Handler for the IRC "LIST" command.
566 * @param Client The client from which this command has been received.
567 * @param Req Request structure with prefix and all parameters.
568 * @return CONNECTED or DISCONNECTED.
571 IRC_LIST( CLIENT *Client, REQUEST *Req )
575 CLIENT *from, *target;
578 assert(Client != NULL);
581 IRC_SetPenalty(Client, 2);
583 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
584 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
587 pattern = strtok(Req->argv[0], ",");
591 if (Req->argc == 2) {
592 /* Forward to other server? */
593 target = Client_Search(Req->argv[1]);
594 if (! target || Client_Type(target) != CLIENT_SERVER)
595 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
599 if (target != Client_ThisServer()) {
600 /* Target is indeed an other server, forward it! */
601 return IRC_WriteStrClientPrefix(target, from,
609 /* Loop through all the channels */
611 ngt_LowerStr(pattern);
612 chan = Channel_First();
614 /* Check search pattern */
615 if (MatchCaseInsensitive(pattern, Channel_Name(chan))) {
617 if (!Channel_HasMode(chan, 's')
618 || Channel_IsMemberOf(chan, from)
619 || (!Conf_MorePrivacy && Client_OperByMe(Client))) {
620 if ((Conf_MaxListSize > 0)
621 && IRC_CheckListTooBig(from, count,
625 if (!IRC_WriteStrClient(from,
626 RPL_LIST_MSG, Client_ID(from),
628 Channel_MemberCount(chan),
629 Channel_Topic( chan )))
634 chan = Channel_Next(chan);
637 /* Get next name ... */
639 pattern = strtok(NULL, ",");
644 return IRC_WriteStrClient(from, RPL_LISTEND_MSG, Client_ID(from));
648 * Handler for the IRC+ "CHANINFO" command.
650 * @param Client The client from which this command has been received.
651 * @param Req Request structure with prefix and all parameters.
652 * @return CONNECTED or DISCONNECTED.
655 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
657 char modes_add[COMMAND_LEN], l[16];
662 assert( Client != NULL );
663 assert( Req != NULL );
665 /* Bad number of parameters? */
666 if (Req->argc < 2 || Req->argc == 4 || Req->argc > 5)
667 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
668 Client_ID(Client), Req->command);
670 /* Compatibility kludge */
673 else if(Req->argc == 3)
678 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
680 /* Search or create channel */
681 chan = Channel_Search( Req->argv[0] );
683 chan = Channel_Create( Req->argv[0] );
687 if (Req->argv[1][0] == '+') {
688 if (!*Channel_Modes(chan)) {
689 /* OK, this channel doesn't have modes yet,
690 * set the received ones: */
691 Channel_SetModes(chan, &Req->argv[1][1]);
694 if(Channel_HasMode(chan, 'k'))
695 Channel_SetKey(chan, Req->argv[2]);
696 if(Channel_HasMode(chan, 'l'))
697 Channel_SetMaxUsers(chan, atol(Req->argv[3]));
699 /* Delete modes which we never want to inherit */
700 Channel_ModeDel(chan, 'l');
701 Channel_ModeDel(chan, 'k');
704 strcpy(modes_add, "");
705 if (Channel_HasMode(chan, 'l')) {
706 snprintf(l, sizeof(l), " %lu",
707 Channel_MaxUsers(chan));
708 strlcat(modes_add, l, sizeof(modes_add));
710 if (Channel_HasMode(chan, 'k')) {
711 strlcat(modes_add, " ", sizeof(modes_add));
712 strlcat(modes_add, Channel_Key(chan),
716 /* Inform members of this channel */
717 IRC_WriteStrChannelPrefix(Client, chan, from, false,
718 "MODE %s +%s%s", Req->argv[0],
719 Channel_Modes(chan), modes_add);
723 Log(LOG_WARNING, "CHANINFO: invalid MODE format ignored!");
727 if (!*Channel_Topic(chan) && Req->argv[arg_topic][0]) {
728 /* OK, there is no topic jet */
729 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
730 IRC_WriteStrChannelPrefix(Client, chan, from, false,
731 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
735 /* Forward CHANINFO to other servers */
737 IRC_WriteStrServersPrefixFlag(Client, from, 'C',
738 "CHANINFO %s %s %s %s :%s",
739 Req->argv[0], Req->argv[1],
740 Req->argv[2], Req->argv[3],
742 else if (Req->argc == 3)
743 IRC_WriteStrServersPrefixFlag(Client, from, 'C',
744 "CHANINFO %s %s :%s",
745 Req->argv[0], Req->argv[1],
748 IRC_WriteStrServersPrefixFlag(Client, from, 'C',
750 Req->argv[0], Req->argv[1]);