2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2018 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
26 #include "conn-func.h"
34 #include "irc-macros.h"
35 #include "irc-write.h"
38 #include "irc-channel.h"
41 * Part from all channels.
43 * RFC 2812, (3.2.1 Join message Command):
44 * Note that this message accepts a special argument ("0"), which is a
45 * special request to leave all channels the user is currently a member of.
46 * The server will process this message as if the user had sent a PART
47 * command (See Section 3.2.2) for each channel he is a member of.
49 * @param client Client that initiated the part request
50 * @param target Client that should part all joined channels
51 * @returns CONNECTED or DISCONNECTED
54 part_from_all_channels(CLIENT* client, CLIENT *target)
59 while ((cl2chan = Channel_FirstChannelOf(target))) {
60 chan = Channel_GetChannel(cl2chan);
61 assert( chan != NULL );
62 Channel_Part(target, client, Channel_Name(chan), Client_ID(target));
65 } /* part_from_all_channels */
68 * Check weather a local client is allowed to join an already existing
71 * @param Client Client that sent the JOIN command
72 * @param chan Channel to check
73 * @param channame Name of the channel
74 * @param key Provided channel key (or NULL)
75 * @returns true if client is allowed to join, false otherwise
78 join_allowed(CLIENT *Client, CHANNEL *chan, const char *channame,
81 bool is_invited, is_banned, is_exception;
83 /* Allow IRC operators to overwrite channel limits */
84 if (Client_HasMode(Client, 'o'))
87 is_banned = Lists_Check(Channel_GetListBans(chan), Client);
88 is_exception = Lists_Check(Channel_GetListExcepts(chan), Client);
89 is_invited = Lists_Check(Channel_GetListInvites(chan), Client);
91 if (is_banned && !is_invited && !is_exception) {
92 /* Client is banned from channel (and not on invite list) */
93 IRC_WriteErrClient(Client, ERR_BANNEDFROMCHAN_MSG,
94 Client_ID(Client), channame);
98 if (Channel_HasMode(chan, 'i') && !is_invited) {
99 /* Channel is "invite-only" and client is not on invite list */
100 IRC_WriteErrClient(Client, ERR_INVITEONLYCHAN_MSG,
101 Client_ID(Client), channame);
105 if (!Channel_CheckKey(chan, Client, key ? key : "")) {
106 /* Channel is protected by a channel key and the client
107 * didn't specify the correct one */
108 IRC_WriteErrClient(Client, ERR_BADCHANNELKEY_MSG,
109 Client_ID(Client), channame);
113 if (Channel_HasMode(chan, 'l') &&
114 (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
115 /* There are more clints joined to this channel than allowed */
116 IRC_WriteErrClient(Client, ERR_CHANNELISFULL_MSG,
117 Client_ID(Client), channame);
121 if (Channel_HasMode(chan, 'z') && !Conn_UsesSSL(Client_Conn(Client))) {
122 /* Only "secure" clients are allowed, but clients doesn't
123 * use SSL encryption */
124 IRC_WriteErrClient(Client, ERR_SECURECHANNEL_MSG,
125 Client_ID(Client), channame);
129 if (Channel_HasMode(chan, 'O') && !Client_HasMode(Client, 'o')) {
130 /* Only IRC operators are allowed! */
131 IRC_WriteErrClient(Client, ERR_OPONLYCHANNEL_MSG,
132 Client_ID(Client), channame);
136 if (Channel_HasMode(chan, 'R') && !Client_HasMode(Client, 'R')) {
137 /* Only registered users are allowed! */
138 IRC_WriteErrClient(Client, ERR_REGONLYCHANNEL_MSG,
139 Client_ID(Client), channame);
147 * Set user channel modes.
149 * @param chan Channel
150 * @param target User to set modes for
151 * @param flags Channel modes to add
154 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
158 Channel_UserModeAdd(chan, target, *flags);
163 /* If the channel is persistent (+P) and client is an IRC op:
164 * make client chanop, if not disabled in configuration. */
165 if (Channel_HasMode(chan, 'P') && Conf_OperChanPAutoOp
166 && Client_HasMode(target, 'o'))
167 Channel_UserModeAdd(chan, target, 'o');
168 } /* join_set_channelmodes */
171 * Forward JOIN command to a specific server
173 * This function differentiates between servers using RFC 2813 mode that
174 * support the JOIN command with appended ASCII 7 character and channel
175 * modes, and servers using RFC 1459 protocol which require separate JOIN
178 * @param To Forward JOIN (and MODE) command to this peer server
179 * @param Prefix Client used to prefix the generated commands
180 * @param Data Parameters of JOIN command to forward, probably
181 * containing channel modes separated by ASCII 7.
184 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
187 char str[COMMAND_LEN], *ptr = NULL;
189 strlcpy(str, (char *)Data, sizeof(str));
190 conn = Client_Conn(To);
192 if (Conn_Options(conn) & CONN_RFC1459) {
193 /* RFC 1459 compatibility mode, appended modes are NOT
194 * supported, so strip them off! */
195 ptr = strchr(str, 0x7);
200 IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
202 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
204 } /* cb_join_forward */
207 * Forward JOIN command to all servers
209 * This function calls cb_join_forward(), which differentiates between
210 * protocol implementations (e.g. RFC 2812, RFC 1459).
212 * @param Client Client used to prefix the generated commands
213 * @param target Forward JOIN (and MODE) command to this peer server
214 * @param chan Channel structure
215 * @param channame Channel name
218 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
219 const char *channame)
221 char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
223 /* RFC 2813, 4.2.1: channel modes are separated from the channel
224 * name with ASCII 7, if any, and not spaces: */
225 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
231 /* forward to other servers (if it is not a local channel) */
232 if (!Channel_IsLocal(chan)) {
233 snprintf(str, sizeof(str), "%s%s", channame, modes);
234 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
235 cb_join_forward, str);
238 /* tell users in this channel about the new client */
239 IRC_WriteStrChannelPrefix(Client, chan, target, false,
240 "JOIN :%s", channame);
242 /* synchronize channel modes */
244 IRC_WriteStrChannelPrefix(Client, chan, target, false,
245 "MODE %s +%s %s", channame,
246 &modes[1], Client_ID(target));
251 * Send channel TOPIC and NAMES list to a newly (N)JOIN'ed client.
253 * @param Client Client used to prefix the generated commands
254 * @param Chan Channel structure
257 IRC_Send_Channel_Info(CLIENT *Client, CHANNEL *Chan)
261 /* Send the topic (if any) to the new client: */
262 topic = Channel_Topic(Chan);
263 assert(topic != NULL);
265 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
266 Client_ID(Client), Channel_Name(Chan), topic))
269 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
270 Client_ID(Client), Channel_Name(Chan),
271 Channel_TopicWho(Chan),
272 Channel_TopicTime(Chan)))
277 /* Send list of channel members to the new client: */
278 if (!IRC_Send_NAMES(Client, Chan))
280 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG,
281 Client_ID(Client), Channel_Name(Chan));
285 * Handler for the IRC "JOIN" command.
287 * @param Client The client from which this command has been received.
288 * @param Req Request structure with prefix and all parameters.
289 * @return CONNECTED or DISCONNECTED.
292 IRC_JOIN( CLIENT *Client, REQUEST *Req )
294 char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
298 assert (Client != NULL);
299 assert (Req != NULL);
301 _IRC_GET_SENDER_OR_RETURN_(target, Req, Client)
303 /* Is argument "0"? */
304 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
305 return part_from_all_channels(Client, target);
307 /* Are channel keys given? */
309 key = strtok_r(Req->argv[1], ",", &lastkey);
311 channame = Req->argv[0];
312 channame = strtok_r(channame, ",", &lastchan);
314 /* Make sure that "channame" is not the empty string ("JOIN :") */
316 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
317 Client_ID(Client), Req->command);
322 /* Did the server include channel-user-modes? */
323 if (Client_Type(Client) == CLIENT_SERVER) {
324 flags = strchr(channame, 0x7);
331 chan = Channel_Search(channame);
334 if (Client_Type(Client) == CLIENT_USER) {
336 /* Already existing channel: already member? */
337 if (Channel_IsMemberOf(chan, Client))
340 /* Channel must be created */
341 if (!strchr(Conf_AllowedChannelTypes, channame[0])) {
342 /* ... but channel type is not allowed! */
343 IRC_WriteErrClient(Client,
344 ERR_NOSUCHCHANNEL_MSG,
345 Client_ID(Client), channame);
350 /* Test if the user has reached the channel limit */
351 if ((Conf_MaxJoins > 0) &&
352 (Channel_CountForUser(Client) >= Conf_MaxJoins)) {
353 if (!IRC_WriteErrClient(Client,
354 ERR_TOOMANYCHANNELS_MSG,
355 Client_ID(Client), channame))
361 /* Already existing channel: check if the
362 * client is allowed to join */
363 if (!join_allowed(Client, chan, channame, key))
366 /* New channel: first user will become channel
367 * operator unless this is a modeless channel */
368 if (*channame != '+')
372 /* Local client: update idle time */
373 Conn_UpdateIdle(Client_Conn(Client));
375 /* Remote server: we don't need to know whether the
376 * client is invited or not, but we have to make sure
377 * that the "one shot" entries (generated by INVITE
378 * commands) in this list become deleted when a user
379 * joins a channel this way. */
381 (void)Lists_Check(Channel_GetListInvites(chan),
385 /* Join channel (and create channel if it doesn't exist) */
386 if (!Channel_Join(target, channame))
389 if (!chan) { /* channel is new; it has been created above */
390 chan = Channel_Search(channame);
391 assert(chan != NULL);
392 if (Channel_IsModeless(chan)) {
393 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
394 Channel_ModeAdd(chan, 'n'); /* no external msgs */
397 assert(chan != NULL);
399 join_set_channelmodes(chan, target, flags);
401 join_forward(Client, target, chan, channame);
403 if (Client_Type(Client) == CLIENT_USER) {
404 /* Acknowledge join ... */
405 if (!IRC_WriteStrClientPrefix(Client, target,
406 "JOIN :%s", channame))
407 break; /* write error */
408 /* ... and greet new user: */
409 if (!IRC_Send_Channel_Info(Client, chan))
410 break; /* write error */
415 channame = strtok_r(NULL, ",", &lastchan);
417 key = strtok_r(NULL, ",", &lastkey);
423 * Handler for the IRC "PART" command.
425 * @param Client The client from which this command has been received.
426 * @param Req Request structure with prefix and all parameters.
427 * @return CONNECTED or DISCONNECTED.
430 IRC_PART(CLIENT * Client, REQUEST * Req)
435 assert(Client != NULL);
438 _IRC_GET_SENDER_OR_RETURN_(target, Req, Client)
440 /* Loop over all the given channel names */
441 chan = strtok(Req->argv[0], ",");
443 /* Make sure that "chan" is not the empty string ("PART :") */
445 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
446 Client_ID(Client), Req->command);
449 Channel_Part(target, Client, chan,
450 Req->argc > 1 ? Req->argv[1] : "");
451 chan = strtok(NULL, ",");
454 /* Update idle time, if local client */
455 if (Client_Conn(Client) > NONE)
456 Conn_UpdateIdle(Client_Conn(Client));
462 * Handler for the IRC "TOPIC" command.
464 * @param Client The client from which this command has been received.
465 * @param Req Request structure with prefix and all parameters.
466 * @return CONNECTED or DISCONNECTED.
469 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
476 assert( Client != NULL );
477 assert( Req != NULL );
479 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
481 chan = Channel_Search(Req->argv[0]);
483 return IRC_WriteErrClient(from, ERR_NOSUCHCHANNEL_MSG,
484 Client_ID(from), Req->argv[0]);
486 /* Only remote servers and channel members are allowed to change the
487 * channel topic, and IRC operators when the Conf_OperCanMode option
488 * is set in the server configuration. */
489 if (Client_Type(Client) != CLIENT_SERVER) {
490 topic_power = Client_HasMode(from, 'o');
491 if (!Channel_IsMemberOf(chan, from)
492 && !(Conf_OperCanMode && topic_power))
493 return IRC_WriteErrClient(from, ERR_NOTONCHANNEL_MSG,
494 Client_ID(from), Req->argv[0]);
498 if (Req->argc == 1) {
499 /* Request current topic */
500 topic = Channel_Topic(chan);
502 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
504 Channel_Name(chan), topic);
508 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
511 Channel_TopicWho(chan),
512 Channel_TopicTime(chan));
517 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
522 if (Channel_HasMode(chan, 't')) {
523 /* Topic Lock. Is the user a channel op or IRC operator? */
525 !Channel_UserHasMode(chan, from, 'h') &&
526 !Channel_UserHasMode(chan, from, 'o') &&
527 !Channel_UserHasMode(chan, from, 'a') &&
528 !Channel_UserHasMode(chan, from, 'q'))
529 return IRC_WriteErrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
534 LogDebug("%s \"%s\" set topic on \"%s\": %s",
535 Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
536 Req->argv[1][0] ? Req->argv[1] : "<none>");
538 if (Conf_OperServerMode)
539 from = Client_ThisServer();
541 /* Update channel and forward new topic to other servers */
542 if (!Channel_IsLocal(chan))
543 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
544 Req->argv[0], Req->argv[1]);
546 /* Infrom local clients, but only when the topic really changed. */
547 if (strcmp(Req->argv[1], Channel_Topic(chan)) != 0)
548 IRC_WriteStrChannelPrefix(Client, chan, from, false,
549 "TOPIC %s :%s", Req->argv[0],
552 /* Update topic, setter, and timestamp. */
553 Channel_SetTopic(chan, from, Req->argv[1]);
555 /* Send confirmation when the local client is a user. */
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_GET_SENDER_OR_RETURN_(from, Req, Client)
584 pattern = strtok(Req->argv[0], ",");
588 if (Req->argc == 2) {
589 /* Forward to other server? */
590 target = Client_Search(Req->argv[1]);
591 if (! target || Client_Type(target) != CLIENT_SERVER)
592 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
596 if (target != Client_ThisServer()) {
597 /* Target is indeed an other server, forward it! */
598 return IRC_WriteStrClientPrefix(target, from,
606 if (!IRC_WriteStrClient(from, RPL_LISTSTART_MSG, Client_ID(from)))
610 /* Loop through all the channels */
612 ngt_LowerStr(pattern);
613 chan = Channel_First();
615 /* Check search pattern */
616 if (MatchCaseInsensitive(pattern, Channel_Name(chan))) {
618 if (!Channel_HasMode(chan, 's')
619 || Channel_IsMemberOf(chan, from)
620 || Client_HasMode(from, 'o'))
622 if ((Conf_MaxListSize > 0)
623 && IRC_CheckListTooBig(from, count,
627 if (!IRC_WriteStrClient(from,
628 RPL_LIST_MSG, Client_ID(from),
630 Channel_MemberCount(chan),
631 Channel_Topic( chan )))
636 chan = Channel_Next(chan);
639 /* Get next name ... */
641 pattern = strtok(NULL, ",");
646 return IRC_WriteStrClient(from, RPL_LISTEND_MSG, Client_ID(from));
650 * Handler for the IRC+ "CHANINFO" command.
652 * @param Client The client from which this command has been received.
653 * @param Req Request structure with prefix and all parameters.
654 * @return CONNECTED or DISCONNECTED.
657 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
659 char modes_add[COMMAND_LEN], l[16];
664 assert( Client != NULL );
665 assert( Req != NULL );
667 /* Bad number of parameters? */
668 if (Req->argc < 2 || Req->argc == 4 || Req->argc > 5)
669 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
670 Client_ID(Client), Req->command);
672 /* Compatibility kludge */
675 else if(Req->argc == 3)
680 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
682 /* Search or create channel */
683 chan = Channel_Search( Req->argv[0] );
685 chan = Channel_Create( Req->argv[0] );
689 if (Req->argv[1][0] == '+') {
690 if (!*Channel_Modes(chan)) {
691 /* OK, this channel doesn't have modes yet,
692 * set the received ones: */
693 Channel_SetModes(chan, &Req->argv[1][1]);
696 if(Channel_HasMode(chan, 'k'))
697 Channel_SetKey(chan, Req->argv[2]);
698 if(Channel_HasMode(chan, 'l'))
699 Channel_SetMaxUsers(chan, atol(Req->argv[3]));
701 /* Delete modes which we never want to inherit */
702 Channel_ModeDel(chan, 'l');
703 Channel_ModeDel(chan, 'k');
706 strcpy(modes_add, "");
707 if (Channel_HasMode(chan, 'l')) {
708 snprintf(l, sizeof(l), " %lu",
709 Channel_MaxUsers(chan));
710 strlcat(modes_add, l, sizeof(modes_add));
712 if (Channel_HasMode(chan, 'k')) {
713 strlcat(modes_add, " ", sizeof(modes_add));
714 strlcat(modes_add, Channel_Key(chan),
718 /* Inform members of this channel */
719 IRC_WriteStrChannelPrefix(Client, chan, from, false,
720 "MODE %s +%s%s", Req->argv[0],
721 Channel_Modes(chan), modes_add);
725 Log(LOG_WARNING, "CHANINFO: invalid MODE format ignored!");
729 if (!*Channel_Topic(chan) && Req->argv[arg_topic][0]) {
730 /* OK, there is no topic jet */
731 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
732 IRC_WriteStrChannelPrefix(Client, chan, from, false,
733 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
737 /* Forward CHANINFO to other servers */
739 IRC_WriteStrServersPrefixFlag(Client, from, 'C',
740 "CHANINFO %s %s %s %s :%s",
741 Req->argv[0], Req->argv[1],
742 Req->argv[2], Req->argv[3],
744 else if (Req->argc == 3)
745 IRC_WriteStrServersPrefixFlag(Client, from, 'C',
746 "CHANINFO %s %s :%s",
747 Req->argv[0], Req->argv[1],
750 IRC_WriteStrServersPrefixFlag(Client, from, 'C',
752 Req->argv[0], Req->argv[1]);