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
26 #include "conn-func.h"
34 #include "irc-macros.h"
35 #include "irc-write.h"
39 * Part from all channels.
41 * RFC 2812, (3.2.1 Join message Command):
42 * Note that this message accepts a special argument ("0"), which is a
43 * special request to leave all channels the user is currently a member of.
44 * The server will process this message as if the user had sent a PART
45 * command (See Section 3.2.2) for each channel he is a member of.
47 * @param client Client that initiated the part request
48 * @param target Client that should part all joined channels
49 * @returns CONNECTED or DISCONNECTED
52 part_from_all_channels(CLIENT* client, CLIENT *target)
57 while ((cl2chan = Channel_FirstChannelOf(target))) {
58 chan = Channel_GetChannel(cl2chan);
59 assert( chan != NULL );
60 Channel_Part(target, client, Channel_Name(chan), Client_ID(target));
63 } /* part_from_all_channels */
66 * Check weather a local client is allowed to join an already existing
69 * @param Client Client that sent the JOIN command
70 * @param chan Channel to check
71 * @param channame Name of the channel
72 * @param key Provided channel key (or NULL)
73 * @returns true if client is allowed to join, false otherwise
76 join_allowed(CLIENT *Client, CHANNEL *chan, const char *channame,
79 bool is_invited, is_banned, is_exception;
81 /* Allow IRC operators to overwrite channel limits */
82 if (Client_HasMode(Client, 'o'))
85 is_banned = Lists_Check(Channel_GetListBans(chan), Client);
86 is_exception = Lists_Check(Channel_GetListExcepts(chan), Client);
87 is_invited = Lists_Check(Channel_GetListInvites(chan), Client);
89 if (is_banned && !is_invited && !is_exception) {
90 /* Client is banned from channel (and not on invite list) */
91 IRC_WriteErrClient(Client, ERR_BANNEDFROMCHAN_MSG,
92 Client_ID(Client), channame);
96 if (Channel_HasMode(chan, 'i') && !is_invited) {
97 /* Channel is "invite-only" and client is not on invite list */
98 IRC_WriteErrClient(Client, ERR_INVITEONLYCHAN_MSG,
99 Client_ID(Client), channame);
103 if (!Channel_CheckKey(chan, Client, key ? key : "")) {
104 /* Channel is protected by a channel key and the client
105 * didn't specify the correct one */
106 IRC_WriteErrClient(Client, ERR_BADCHANNELKEY_MSG,
107 Client_ID(Client), channame);
111 if (Channel_HasMode(chan, 'l') &&
112 (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
113 /* There are more clints joined to this channel than allowed */
114 IRC_WriteErrClient(Client, ERR_CHANNELISFULL_MSG,
115 Client_ID(Client), channame);
119 if (Channel_HasMode(chan, 'z') && !Conn_UsesSSL(Client_Conn(Client))) {
120 /* Only "secure" clients are allowed, but clients doesn't
121 * use SSL encryption */
122 IRC_WriteErrClient(Client, ERR_SECURECHANNEL_MSG,
123 Client_ID(Client), channame);
127 if (Channel_HasMode(chan, 'O') && !Client_HasMode(Client, 'o')) {
128 /* Only IRC operators are allowed! */
129 IRC_WriteErrClient(Client, ERR_OPONLYCHANNEL_MSG,
130 Client_ID(Client), channame);
134 if (Channel_HasMode(chan, 'R') && !Client_HasMode(Client, 'R')) {
135 /* Only registered users are allowed! */
136 IRC_WriteErrClient(Client, ERR_REGONLYCHANNEL_MSG,
137 Client_ID(Client), channame);
145 * Set user channel modes.
147 * @param chan Channel
148 * @param target User to set modes for
149 * @param flags Channel modes to add
152 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
156 Channel_UserModeAdd(chan, target, *flags);
161 /* If the channel is persistent (+P) and client is an IRC op:
162 * make client chanop, if not disabled in configuration. */
163 if (Channel_HasMode(chan, 'P') && Conf_OperChanPAutoOp
164 && Client_HasMode(target, 'o'))
165 Channel_UserModeAdd(chan, target, 'o');
166 } /* join_set_channelmodes */
169 * Forward JOIN command to a specific server
171 * This function differentiates between servers using RFC 2813 mode that
172 * support the JOIN command with appended ASCII 7 character and channel
173 * modes, and servers using RFC 1459 protocol which require separate JOIN
176 * @param To Forward JOIN (and MODE) command to this peer server
177 * @param Prefix Client used to prefix the genrated commands
178 * @param Data Parameters of JOIN command to forward, probably
179 * containing channel modes separated by ASCII 7.
182 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
185 char str[COMMAND_LEN], *ptr = NULL;
187 strlcpy(str, (char *)Data, sizeof(str));
188 conn = Client_Conn(To);
190 if (Conn_Options(conn) & CONN_RFC1459) {
191 /* RFC 1459 compatibility mode, appended modes are NOT
192 * supported, so strip them off! */
193 ptr = strchr(str, 0x7);
198 IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
200 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
202 } /* cb_join_forward */
205 * Forward JOIN command to all servers
207 * This function calls cb_join_forward(), which differentiates between
208 * protocol implementations (e.g. RFC 2812, RFC 1459).
210 * @param Client Client used to prefix the genrated commands
211 * @param target Forward JOIN (and MODE) command to this peer server
212 * @param chan Channel structure
213 * @param channame Channel name
216 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
217 const char *channame)
219 char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
221 /* RFC 2813, 4.2.1: channel modes are separated from the channel
222 * name with ASCII 7, if any, and not spaces: */
223 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
229 /* forward to other servers (if it is not a local channel) */
230 if (!Channel_IsLocal(chan)) {
231 snprintf(str, sizeof(str), "%s%s", channame, modes);
232 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
233 cb_join_forward, str);
236 /* tell users in this channel about the new client */
237 IRC_WriteStrChannelPrefix(Client, chan, target, false,
238 "JOIN :%s", channame);
240 /* synchronize channel modes */
242 IRC_WriteStrChannelPrefix(Client, chan, target, false,
243 "MODE %s +%s %s", channame,
244 &modes[1], Client_ID(target));
249 * Acknowledge user JOIN request and send "channel info" numerics.
251 * @param Client Client used to prefix the genrated commands
252 * @param target Forward commands/numerics to this user
253 * @param chan Channel structure
254 * @param channame Channel name
257 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
258 const char *channame)
262 if (Client_Type(Client) != CLIENT_USER)
264 /* acknowledge join */
265 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
268 /* Send topic to client, if any */
269 topic = Channel_Topic(chan);
270 assert(topic != NULL);
272 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
273 Client_ID(Client), channame, topic))
276 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
277 Client_ID(Client), channame,
278 Channel_TopicWho(chan),
279 Channel_TopicTime(chan)))
283 /* send list of channel members to client */
284 if (!IRC_Send_NAMES(Client, chan))
286 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client),
288 } /* join_send_topic */
291 * Handler for the IRC "JOIN" command.
293 * @param Client The client from which this command has been received.
294 * @param Req Request structure with prefix and all parameters.
295 * @return CONNECTED or DISCONNECTED.
298 IRC_JOIN( CLIENT *Client, REQUEST *Req )
300 char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
304 assert (Client != NULL);
305 assert (Req != NULL);
307 _IRC_GET_SENDER_OR_RETURN_(target, Req, Client)
309 /* Is argument "0"? */
310 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
311 return part_from_all_channels(Client, target);
313 /* Are channel keys given? */
315 key = strtok_r(Req->argv[1], ",", &lastkey);
317 channame = Req->argv[0];
318 channame = strtok_r(channame, ",", &lastchan);
320 /* Make sure that "channame" is not the empty string ("JOIN :") */
322 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
323 Client_ID(Client), Req->command);
328 /* Did the server include channel-user-modes? */
329 if (Client_Type(Client) == CLIENT_SERVER) {
330 flags = strchr(channame, 0x7);
337 chan = Channel_Search(channame);
340 if (Client_Type(Client) == CLIENT_USER) {
342 /* Already existing channel: already member? */
343 if (Channel_IsMemberOf(chan, Client))
346 /* Channel must be created */
347 if (!strchr(Conf_AllowedChannelTypes, channame[0])) {
348 /* ... but channel type is not allowed! */
349 IRC_WriteErrClient(Client,
350 ERR_NOSUCHCHANNEL_MSG,
351 Client_ID(Client), channame);
356 /* Test if the user has reached the channel limit */
357 if ((Conf_MaxJoins > 0) &&
358 (Channel_CountForUser(Client) >= Conf_MaxJoins)) {
359 if (!IRC_WriteErrClient(Client,
360 ERR_TOOMANYCHANNELS_MSG,
361 Client_ID(Client), channame))
367 /* Already existing channel: check if the
368 * client is allowed to join */
369 if (!join_allowed(Client, chan, channame, key))
372 /* New channel: first user will become channel
373 * operator unless this is a modeless channel */
374 if (*channame != '+')
378 /* Local client: update idle time */
379 Conn_UpdateIdle(Client_Conn(Client));
381 /* Remote server: we don't need to know whether the
382 * client is invited or not, but we have to make sure
383 * that the "one shot" entries (generated by INVITE
384 * commands) in this list become deleted when a user
385 * joins a channel this way. */
387 (void)Lists_Check(Channel_GetListInvites(chan),
391 /* Join channel (and create channel if it doesn't exist) */
392 if (!Channel_Join(target, channame))
395 if (!chan) { /* channel is new; it has been created above */
396 chan = Channel_Search(channame);
397 assert(chan != NULL);
398 if (Channel_IsModeless(chan)) {
399 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
400 Channel_ModeAdd(chan, 'n'); /* no external msgs */
403 assert(chan != NULL);
405 join_set_channelmodes(chan, target, flags);
407 join_forward(Client, target, chan, channame);
409 if (!join_send_topic(Client, target, chan, channame))
410 break; /* write error */
414 channame = strtok_r(NULL, ",", &lastchan);
416 key = strtok_r(NULL, ",", &lastkey);
422 * Handler for the IRC "PART" command.
424 * @param Client The client from which this command has been received.
425 * @param Req Request structure with prefix and all parameters.
426 * @return CONNECTED or DISCONNECTED.
429 IRC_PART(CLIENT * Client, REQUEST * Req)
434 assert(Client != NULL);
437 _IRC_GET_SENDER_OR_RETURN_(target, Req, Client)
439 /* Loop over all the given channel names */
440 chan = strtok(Req->argv[0], ",");
442 /* Make sure that "chan" is not the empty string ("PART :") */
444 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
445 Client_ID(Client), Req->command);
448 Channel_Part(target, Client, chan,
449 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
450 chan = strtok(NULL, ",");
453 /* Update idle time, if local client */
454 if (Client_Conn(Client) > NONE)
455 Conn_UpdateIdle(Client_Conn(Client));
461 * Handler for the IRC "TOPIC" command.
463 * @param Client The client from which this command has been received.
464 * @param Req Request structure with prefix and all parameters.
465 * @return CONNECTED or DISCONNECTED.
468 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
475 assert( Client != NULL );
476 assert( Req != NULL );
478 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
480 chan = Channel_Search(Req->argv[0]);
482 return IRC_WriteErrClient(from, ERR_NOSUCHCHANNEL_MSG,
483 Client_ID(from), Req->argv[0]);
485 /* Only remote servers and channel members are allowed to change the
486 * channel topic, and IRC operators when the Conf_OperCanMode option
487 * is set in the server configuration. */
488 if (Client_Type(Client) != CLIENT_SERVER) {
489 topic_power = Client_HasMode(from, 'o');
490 if (!Channel_IsMemberOf(chan, from)
491 && !(Conf_OperCanMode && topic_power))
492 return IRC_WriteErrClient(from, ERR_NOTONCHANNEL_MSG,
493 Client_ID(from), Req->argv[0]);
497 if (Req->argc == 1) {
498 /* Request actual topic */
499 topic = Channel_Topic(chan);
501 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
503 Channel_Name(chan), topic);
507 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
510 Channel_TopicWho(chan),
511 Channel_TopicTime(chan));
516 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
521 if (Channel_HasMode(chan, 't')) {
522 /* Topic Lock. Is the user a channel op or IRC operator? */
524 !Channel_UserHasMode(chan, from, 'h') &&
525 !Channel_UserHasMode(chan, from, 'o') &&
526 !Channel_UserHasMode(chan, from, 'a') &&
527 !Channel_UserHasMode(chan, from, 'q'))
528 return IRC_WriteErrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
534 Channel_SetTopic(chan, from, Req->argv[1]);
535 LogDebug("%s \"%s\" set topic on \"%s\": %s",
536 Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
537 Req->argv[1][0] ? Req->argv[1] : "<none>");
539 if (Conf_OperServerMode)
540 from = Client_ThisServer();
542 /* Update channel and forward new topic to other servers */
543 if (!Channel_IsLocal(chan))
544 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
545 Req->argv[0], Req->argv[1]);
546 IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
547 Req->argv[0], Req->argv[1]);
549 if (Client_Type(Client) == CLIENT_USER)
550 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
551 Req->argv[0], Req->argv[1]);
557 * Handler for the IRC "LIST" command.
559 * @param Client The client from which this command has been received.
560 * @param Req Request structure with prefix and all parameters.
561 * @return CONNECTED or DISCONNECTED.
564 IRC_LIST( CLIENT *Client, REQUEST *Req )
568 CLIENT *from, *target;
571 assert(Client != NULL);
574 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
577 pattern = strtok(Req->argv[0], ",");
581 if (Req->argc == 2) {
582 /* Forward to other server? */
583 target = Client_Search(Req->argv[1]);
584 if (! target || Client_Type(target) != CLIENT_SERVER)
585 return IRC_WriteErrClient(from, ERR_NOSUCHSERVER_MSG,
589 if (target != Client_ThisServer()) {
590 /* Target is indeed an other server, forward it! */
591 return IRC_WriteStrClientPrefix(target, from,
599 /* Loop through all the channels */
601 ngt_LowerStr(pattern);
602 chan = Channel_First();
604 /* Check search pattern */
605 if (MatchCaseInsensitive(pattern, Channel_Name(chan))) {
607 if (!Channel_HasMode(chan, 's')
608 || Channel_IsMemberOf(chan, from)
609 || (!Conf_MorePrivacy
610 && Client_HasMode(Client, 'o')
611 && Client_Conn(Client) > NONE))
613 if ((Conf_MaxListSize > 0)
614 && IRC_CheckListTooBig(from, count,
618 if (!IRC_WriteStrClient(from,
619 RPL_LIST_MSG, Client_ID(from),
621 Channel_MemberCount(chan),
622 Channel_Topic( chan )))
627 chan = Channel_Next(chan);
630 /* Get next name ... */
632 pattern = strtok(NULL, ",");
637 return IRC_WriteStrClient(from, RPL_LISTEND_MSG, Client_ID(from));
641 * Handler for the IRC+ "CHANINFO" command.
643 * @param Client The client from which this command has been received.
644 * @param Req Request structure with prefix and all parameters.
645 * @return CONNECTED or DISCONNECTED.
648 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
650 char modes_add[COMMAND_LEN], l[16];
655 assert( Client != NULL );
656 assert( Req != NULL );
658 /* Bad number of parameters? */
659 if (Req->argc < 2 || Req->argc == 4 || Req->argc > 5)
660 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
661 Client_ID(Client), Req->command);
663 /* Compatibility kludge */
666 else if(Req->argc == 3)
671 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
673 /* Search or create channel */
674 chan = Channel_Search( Req->argv[0] );
676 chan = Channel_Create( Req->argv[0] );
680 if (Req->argv[1][0] == '+') {
681 if (!*Channel_Modes(chan)) {
682 /* OK, this channel doesn't have modes yet,
683 * set the received ones: */
684 Channel_SetModes(chan, &Req->argv[1][1]);
687 if(Channel_HasMode(chan, 'k'))
688 Channel_SetKey(chan, Req->argv[2]);
689 if(Channel_HasMode(chan, 'l'))
690 Channel_SetMaxUsers(chan, atol(Req->argv[3]));
692 /* Delete modes which we never want to inherit */
693 Channel_ModeDel(chan, 'l');
694 Channel_ModeDel(chan, 'k');
697 strcpy(modes_add, "");
698 if (Channel_HasMode(chan, 'l')) {
699 snprintf(l, sizeof(l), " %lu",
700 Channel_MaxUsers(chan));
701 strlcat(modes_add, l, sizeof(modes_add));
703 if (Channel_HasMode(chan, 'k')) {
704 strlcat(modes_add, " ", sizeof(modes_add));
705 strlcat(modes_add, Channel_Key(chan),
709 /* Inform members of this channel */
710 IRC_WriteStrChannelPrefix(Client, chan, from, false,
711 "MODE %s +%s%s", Req->argv[0],
712 Channel_Modes(chan), modes_add);
716 Log(LOG_WARNING, "CHANINFO: invalid MODE format ignored!");
720 if (!*Channel_Topic(chan) && Req->argv[arg_topic][0]) {
721 /* OK, there is no topic jet */
722 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
723 IRC_WriteStrChannelPrefix(Client, chan, from, false,
724 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
728 /* Forward CHANINFO to other servers */
730 IRC_WriteStrServersPrefixFlag(Client, from, 'C',
731 "CHANINFO %s %s %s %s :%s",
732 Req->argv[0], Req->argv[1],
733 Req->argv[2], Req->argv[3],
735 else if (Req->argc == 3)
736 IRC_WriteStrServersPrefixFlag(Client, from, 'C',
737 "CHANINFO %s %s :%s",
738 Req->argv[0], Req->argv[1],
741 IRC_WriteStrServersPrefixFlag(Client, from, 'C',
743 Req->argv[0], Req->argv[1]);