2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2011 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 commands for mode changes (like MODE, AWAY, etc.)
28 #include "irc-write.h"
39 static bool Client_Mode PARAMS(( CLIENT *Client, REQUEST *Req, CLIENT *Origin,
41 static bool Channel_Mode PARAMS(( CLIENT *Client, REQUEST *Req, CLIENT *Origin,
44 static bool Add_Ban_Invite PARAMS((int what, CLIENT *Prefix, CLIENT *Client,
45 CHANNEL *Channel, const char *Pattern));
46 static bool Del_Ban_Invite PARAMS((int what, CLIENT *Prefix, CLIENT *Client,
47 CHANNEL *Channel, const char *Pattern));
49 static bool Send_ListChange PARAMS((const char *Mode, CLIENT *Prefix,
50 CLIENT *Client, CHANNEL *Channel, const char *Mask));
54 * Handler for the IRC "MODE" command.
56 * See RFC 2812 section 3.1.5 ("user mode message") and section 3.2.3
57 * ("channel mode message"), and RFC 2811 section 4 ("channel modes").
59 * @param Client The client from which this command has been received.
60 * @param Req Request structure with prefix and all parameters.
61 * @returns CONNECTED or DISCONNECTED.
64 IRC_MODE( CLIENT *Client, REQUEST *Req )
69 assert(Client != NULL);
74 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
75 Client_ID(Client), Req->command);
77 /* Origin for answers */
78 if (Client_Type(Client) == CLIENT_SERVER) {
79 origin = Client_Search(Req->prefix);
81 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
87 /* Channel or user mode? */
88 cl = NULL; chan = NULL;
89 if (Client_IsValidNick(Req->argv[0]))
90 cl = Client_Search(Req->argv[0]);
91 if (Channel_IsValidName(Req->argv[0]))
92 chan = Channel_Search(Req->argv[0]);
95 return Client_Mode(Client, Req, origin, cl);
97 return Channel_Mode(Client, Req, origin, chan);
99 /* No target found! */
100 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
101 Client_ID(Client), Req->argv[0]);
106 * Handle client mode requests
108 * @param Client The client from which this command has been received.
109 * @param Req Request structure with prefix and all parameters.
110 * @param Origin The originator of the MODE command (prefix).
111 * @param Target The target (client) of this MODE command.
112 * @returns CONNECTED or DISCONNECTED.
115 Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target )
117 char the_modes[COMMAND_LEN], x[2], *mode_ptr;
122 /* Is the client allowed to request or change the modes? */
123 if (Client_Type(Client) == CLIENT_USER) {
124 /* Users are only allowed to manipulate their own modes! */
125 if (Target != Client)
126 return IRC_WriteStrClient(Client,
127 ERR_USERSDONTMATCH_MSG,
131 /* Mode request: let's answer it :-) */
133 return IRC_WriteStrClient(Origin, RPL_UMODEIS_MSG,
135 Client_Modes(Target));
138 mode_ptr = Req->argv[mode_arg];
140 /* Initial state: set or unset modes? */
141 if (*mode_ptr == '+') {
143 strcpy(the_modes, "+");
144 } else if (*mode_ptr == '-') {
146 strcpy(the_modes, "-");
148 return IRC_WriteStrClient(Origin, ERR_UMODEUNKNOWNFLAG_MSG,
156 /* Try next argument if there's any */
158 if (mode_arg < Req->argc)
159 mode_ptr = Req->argv[mode_arg];
167 if ((*mode_ptr == '+' && !set)
168 || (*mode_ptr == '-' && set)) {
169 /* Action modifier ("+"/"-") must be changed */
170 len = strlen(the_modes) - 1;
171 if (the_modes[len] == '+'
172 || the_modes[len] == '-') {
173 /* Last character in the "result
174 * string" was an "action", so just
175 * overwrite it with the new action */
176 the_modes[len] = *mode_ptr;
178 /* Append new modifier character to
179 * the resulting mode string */
181 strlcat(the_modes, x,
184 if (*mode_ptr == '+')
195 case 'i': /* Invisible */
196 case 's': /* Server messages */
197 case 'w': /* Wallops messages */
201 if (Client_Type(Client) == CLIENT_SERVER) {
203 Client_SetAway(Origin, DEFAULT_AWAY_MSG);
205 ok = IRC_WriteStrClient(Origin,
206 ERR_NOPRIVILEGES_MSG,
209 case 'c': /* Receive connect notices
210 * (only settable by IRC operators!) */
211 if (!set || Client_Type(Client) == CLIENT_SERVER
212 || Client_OperByMe(Origin))
215 ok = IRC_WriteStrClient(Origin,
216 ERR_NOPRIVILEGES_MSG,
219 case 'o': /* IRC operator (only unsettable!) */
220 if (!set || Client_Type(Client) == CLIENT_SERVER) {
221 Client_SetOperByMe(Target, false);
224 ok = IRC_WriteStrClient(Origin,
225 ERR_NOPRIVILEGES_MSG,
228 case 'r': /* Restricted (only settable) */
229 if (set || Client_Type(Client) == CLIENT_SERVER)
232 ok = IRC_WriteStrClient(Origin,
236 case 'x': /* Cloak hostname */
237 if (Client_HasMode(Client, 'r'))
238 ok = IRC_WriteStrClient(Origin,
245 if (Client_Type(Client) != CLIENT_SERVER) {
247 "Unknown mode \"%c%c\" from \"%s\"!?",
248 set ? '+' : '-', *mode_ptr,
250 ok = IRC_WriteStrClient(Origin,
251 ERR_UMODEUNKNOWNFLAG2_MSG,
259 "Handling unknown mode \"%c%c\" from \"%s\" for \"%s\" ...",
260 set ? '+' : '-', *mode_ptr,
261 Client_ID(Origin), Client_ID(Target));
269 /* Is there a valid mode change? */
274 if (Client_ModeAdd(Target, x[0]))
275 strlcat(the_modes, x, sizeof(the_modes));
277 if (Client_ModeDel(Target, x[0]))
278 strlcat(the_modes, x, sizeof(the_modes));
283 /* Are there changed modes? */
285 /* Remoce needless action modifier characters */
286 len = strlen(the_modes) - 1;
287 if (the_modes[len] == '+' || the_modes[len] == '-')
288 the_modes[len] = '\0';
290 if (Client_Type(Client) == CLIENT_SERVER) {
291 /* Forward modes to other servers */
292 if (Client_Conn(Target) != NONE) {
293 /* Remote server (service?) changed modes
294 * for one of our clients. Inform it! */
295 IRC_WriteStrClientPrefix(Target, Origin,
300 IRC_WriteStrServersPrefix(Client, Origin,
305 /* Send reply to client and inform other servers */
306 ok = IRC_WriteStrClientPrefix(Client, Origin,
310 IRC_WriteStrServersPrefix(Client, Origin,
315 LogDebug("%s \"%s\": Mode change, now \"%s\".",
316 Client_TypeText(Target), Client_Mask(Target),
317 Client_Modes(Target));
320 IRC_SetPenalty(Client, 1);
326 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
328 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
329 const char *mode_ptr;
331 /* Member or not? -- That's the question! */
332 if (!Channel_IsMemberOf(Channel, Origin))
333 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
334 Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
336 /* The sender is a member: generate extended reply */
337 strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
338 mode_ptr = the_modes;
344 snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
345 strlcat(the_args, argadd, sizeof(the_args));
348 strlcat(the_args, " ", sizeof(the_args));
349 strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
355 strlcat(the_modes, the_args, sizeof(the_modes));
357 if (!IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
358 Client_ID(Origin), Channel_Name(Channel),
362 if (!IRC_WriteStrClient(Origin, RPL_CREATIONTIME_MSG,
363 Client_ID(Origin), Channel_Name(Channel),
364 Channel_CreationTime(Channel)))
372 * Handle channel mode and channel-user mode changes
375 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
377 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
378 argadd[CLIENT_PASS_LEN], *mode_ptr;
379 bool connected, set, skiponce, retval, onchannel, modeok, use_servermode;
380 int mode_arg, arg_arg;
385 if (Channel_IsModeless(Channel))
386 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
387 Client_ID(Client), Channel_Name(Channel));
389 /* Mode request: let's answer it :-) */
391 return Channel_Mode_Answer_Request(Origin, Channel);
393 Channel_CheckAdminRights(Channel, Client, Origin,
394 &onchannel, &modeok, &use_servermode);
396 if (!onchannel && !modeok)
397 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
399 Channel_Name(Channel));
402 mode_ptr = Req->argv[mode_arg];
403 if (Req->argc > mode_arg + 1)
404 arg_arg = mode_arg + 1;
408 /* Initial state: set or unset modes? */
422 /* Prepare reply string */
423 strcpy(the_modes, set ? "+" : "-");
427 connected = CONNECTED;
432 /* Try next argument if there's any */
433 if (arg_arg > mode_arg)
438 if (mode_arg >= Req->argc)
440 mode_ptr = Req->argv[mode_arg];
442 if (Req->argc > mode_arg + 1)
443 arg_arg = mode_arg + 1;
452 if (((*mode_ptr == '+') && !set)
453 || ((*mode_ptr == '-') && set)) {
454 /* Action modifier ("+"/"-") must be changed ... */
455 len = strlen(the_modes) - 1;
456 if (the_modes[len] == '+' || the_modes[len] == '-') {
457 /* Adjust last action modifier in result */
458 the_modes[len] = *mode_ptr;
460 /* Append modifier character to result string */
462 strlcat(the_modes, x, sizeof(the_modes));
464 set = *mode_ptr == '+';
469 /* Are there arguments left? */
470 if (arg_arg >= Req->argc)
478 /* --- Channel modes --- */
479 case 'i': /* Invite only */
480 case 'm': /* Moderated */
481 case 'n': /* Only members can write */
482 case 's': /* Secret channel */
483 case 't': /* Topic locked */
484 case 'z': /* Secure connections only */
488 connected = IRC_WriteStrClient(Origin,
489 ERR_CHANOPRIVSNEEDED_MSG,
490 Client_ID(Origin), Channel_Name(Channel));
492 case 'k': /* Channel key */
497 connected = IRC_WriteStrClient(Origin,
498 ERR_CHANOPRIVSNEEDED_MSG,
500 Channel_Name(Channel));
503 if (arg_arg > mode_arg) {
505 Channel_ModeDel(Channel, 'k');
506 Channel_SetKey(Channel,
508 strlcpy(argadd, Channel_Key(Channel),
512 connected = IRC_WriteStrClient(Origin,
513 ERR_CHANOPRIVSNEEDED_MSG,
515 Channel_Name(Channel));
517 Req->argv[arg_arg][0] = '\0';
520 connected = IRC_WriteStrClient(Origin,
521 ERR_NEEDMOREPARAMS_MSG,
522 Client_ID(Origin), Req->command);
526 case 'l': /* Member limit */
531 connected = IRC_WriteStrClient(Origin,
532 ERR_CHANOPRIVSNEEDED_MSG,
534 Channel_Name(Channel));
537 if (arg_arg > mode_arg) {
539 l = atol(Req->argv[arg_arg]);
540 if (l > 0 && l < 0xFFFF) {
541 Channel_ModeDel(Channel, 'l');
542 Channel_SetMaxUsers(Channel, l);
543 snprintf(argadd, sizeof(argadd),
548 connected = IRC_WriteStrClient(Origin,
549 ERR_CHANOPRIVSNEEDED_MSG,
551 Channel_Name(Channel));
553 Req->argv[arg_arg][0] = '\0';
556 connected = IRC_WriteStrClient(Origin,
557 ERR_NEEDMOREPARAMS_MSG,
558 Client_ID(Origin), Req->command);
562 case 'O': /* IRC operators only */
564 /* Only IRC operators are allowed to
565 * set the 'O' channel mode! */
566 if (set && !(Client_OperByMe(Client)
567 || Client_Type(Client) == CLIENT_SERVER))
568 connected = IRC_WriteStrClient(Origin,
569 ERR_NOPRIVILEGES_MSG,
574 connected = IRC_WriteStrClient(Origin,
575 ERR_CHANOPRIVSNEEDED_MSG,
577 Channel_Name(Channel));
579 case 'P': /* Persistent channel */
581 /* Only IRC operators are allowed to
582 * set the 'P' channel mode! */
583 if (set && !(Client_OperByMe(Client)
584 || Client_Type(Client) == CLIENT_SERVER))
585 connected = IRC_WriteStrClient(Origin,
586 ERR_NOPRIVILEGES_MSG,
591 connected = IRC_WriteStrClient(Origin,
592 ERR_CHANOPRIVSNEEDED_MSG,
594 Channel_Name(Channel));
596 /* --- Channel user modes --- */
597 case 'o': /* Channel operator */
598 case 'v': /* Voice */
599 if (arg_arg > mode_arg) {
601 client = Client_Search(Req->argv[arg_arg]);
605 connected = IRC_WriteStrClient(Client,
610 connected = IRC_WriteStrClient(Origin,
611 ERR_CHANOPRIVSNEEDED_MSG,
613 Channel_Name(Channel));
615 Req->argv[arg_arg][0] = '\0';
618 connected = IRC_WriteStrClient(Origin,
619 ERR_NEEDMOREPARAMS_MSG,
620 Client_ID(Origin), Req->command);
624 /* --- Channel lists --- */
625 case 'I': /* Invite lists */
626 case 'b': /* Ban lists */
627 if (arg_arg > mode_arg) {
631 ? Add_Ban_Invite(*mode_ptr, Origin,
634 : Del_Ban_Invite(*mode_ptr, Origin,
638 connected = IRC_WriteStrClient(Origin,
639 ERR_CHANOPRIVSNEEDED_MSG,
641 Channel_Name(Channel));
643 Req->argv[arg_arg][0] = '\0';
646 if (*mode_ptr == 'I')
647 Channel_ShowInvites(Origin, Channel);
649 Channel_ShowBans(Origin, Channel);
653 if (Client_Type(Client) != CLIENT_SERVER) {
655 "Unknown mode \"%c%c\" from \"%s\" on %s!?",
656 set ? '+' : '-', *mode_ptr,
657 Client_ID(Origin), Channel_Name(Channel));
658 connected = IRC_WriteStrClient(Origin,
659 ERR_UMODEUNKNOWNFLAG2_MSG,
661 set ? '+' : '-', *mode_ptr);
666 "Handling unknown mode \"%c%c\" from \"%s\" on %s ...",
667 set ? '+' : '-', *mode_ptr,
668 Client_ID(Origin), Channel_Name(Channel));
676 /* Is there a valid mode change? */
680 /* Validate target client */
681 if (client && (!Channel_IsMemberOf(Channel, client))) {
682 if (!IRC_WriteStrClient
683 (Origin, ERR_USERNOTINCHANNEL_MSG,
684 Client_ID(Origin), Client_ID(client),
685 Channel_Name(Channel)))
692 /* Channel-User-Mode */
694 ? Channel_UserModeAdd(Channel, client, x[0])
695 : Channel_UserModeDel(Channel, client, x[0]);
697 strlcat(the_args, " ", sizeof(the_args));
698 strlcat(the_args, Client_ID(client),
700 strlcat(the_modes, x, sizeof(the_modes));
702 ("User \"%s\": Mode change on %s, now \"%s\"",
703 Client_Mask(client), Channel_Name(Channel),
704 Channel_UserModes(Channel, client));
709 ? Channel_ModeAdd(Channel, x[0])
710 : Channel_ModeDel(Channel, x[0]);
712 strlcat(the_modes, x, sizeof(the_modes));
713 LogDebug("Channel %s: Mode change, now \"%s\".",
714 Channel_Name(Channel),
715 Channel_Modes(Channel));
719 /* Are there additional arguments to add? */
721 strlcat(the_args, " ", sizeof(the_args));
722 strlcat(the_args, argadd, sizeof(the_args));
727 /* Are there changed modes? */
729 /* Clean up mode string */
730 len = strlen(the_modes) - 1;
731 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
732 the_modes[len] = '\0';
734 if (Client_Type(Client) == CLIENT_SERVER) {
735 /* MODE requests for local channels from other servers
736 * are definitely invalid! */
737 if (Channel_IsLocal(Channel)) {
738 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
742 /* Forward mode changes to channel users and all the
743 * other remote servers: */
744 IRC_WriteStrServersPrefix(Client, Origin,
745 "MODE %s %s%s", Channel_Name(Channel),
746 the_modes, the_args);
747 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
748 false, "MODE %s %s%s", Channel_Name(Channel),
749 the_modes, the_args);
752 Origin = Client_ThisServer();
753 /* Send reply to client and inform other servers and channel users */
754 connected = IRC_WriteStrClientPrefix(Client, Origin,
755 "MODE %s %s%s", Channel_Name(Channel),
756 the_modes, the_args);
757 /* Only forward requests for non-local channels */
758 if (!Channel_IsLocal(Channel))
759 IRC_WriteStrServersPrefix(Client, Origin,
760 "MODE %s %s%s", Channel_Name(Channel),
761 the_modes, the_args);
762 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
763 false, "MODE %s %s%s", Channel_Name(Channel),
764 the_modes, the_args);
768 IRC_SetPenalty(Client, 1);
774 IRC_AWAY( CLIENT *Client, REQUEST *Req )
776 assert( Client != NULL );
777 assert( Req != NULL );
779 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
781 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
783 Client_SetAway( Client, Req->argv[0] );
784 Client_ModeAdd( Client, 'a' );
785 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
786 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
790 Client_ModeDel( Client, 'a' );
791 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
792 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
798 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
804 assert( Client != NULL );
805 assert( Channel != NULL );
806 assert( Pattern != NULL );
807 assert(what == 'I' || what == 'b');
809 mask = Lists_MakeMask(Pattern);
811 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
814 ret = Channel_AddInvite(Channel, mask, false);
816 ret = Channel_AddBan(Channel, mask);
820 if (already && (Client_Type(Prefix) == CLIENT_SERVER))
824 return Send_ListChange("+I", Prefix, Client, Channel, mask);
825 return Send_ListChange("+b", Prefix, Client, Channel, mask);
830 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
833 struct list_head *list;
835 assert( Client != NULL );
836 assert( Channel != NULL );
837 assert( Pattern != NULL );
838 assert(what == 'I' || what == 'b');
840 mask = Lists_MakeMask( Pattern );
843 list = Channel_GetListInvites(Channel);
845 list = Channel_GetListBans(Channel);
847 Lists_Del(list, mask);
849 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
850 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
855 Send_ListChange(const char *Mode, CLIENT *Prefix, CLIENT *Client,
856 CHANNEL *Channel, const char *Mask)
860 if( Client_Type( Client ) == CLIENT_USER )
862 /* send confirmation to client */
863 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
867 /* to other servers */
868 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
870 /* and local users in channel */
871 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
874 } /* Send_ListChange */