2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2012 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 * Check if the "mode limit" for a client has been reached.
108 * This limit doesn't apply for servers or services!
110 * @param Client The client to check.
111 * @param Count The number of modes already handled.
112 * @return true if the limit has been reached.
115 Mode_Limit_Reached(CLIENT *Client, int Count)
117 if (Client_Type(Client) == CLIENT_SERVER
118 || Client_Type(Client) == CLIENT_SERVICE)
120 if (Count < MAX_HNDL_MODES_ARG)
127 * Handle client mode requests
129 * @param Client The client from which this command has been received.
130 * @param Req Request structure with prefix and all parameters.
131 * @param Origin The originator of the MODE command (prefix).
132 * @param Target The target (client) of this MODE command.
133 * @returns CONNECTED or DISCONNECTED.
136 Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target )
138 char the_modes[COMMAND_LEN], x[2], *mode_ptr;
143 /* Is the client allowed to request or change the modes? */
144 if (Client_Type(Client) == CLIENT_USER) {
145 /* Users are only allowed to manipulate their own modes! */
146 if (Target != Client)
147 return IRC_WriteStrClient(Client,
148 ERR_USERSDONTMATCH_MSG,
152 /* Mode request: let's answer it :-) */
154 return IRC_WriteStrClient(Origin, RPL_UMODEIS_MSG,
156 Client_Modes(Target));
159 mode_ptr = Req->argv[mode_arg];
161 /* Initial state: set or unset modes? */
162 if (*mode_ptr == '+') {
164 strcpy(the_modes, "+");
165 } else if (*mode_ptr == '-') {
167 strcpy(the_modes, "-");
169 return IRC_WriteStrClient(Origin, ERR_UMODEUNKNOWNFLAG_MSG,
177 /* Try next argument if there's any */
179 if (mode_arg < Req->argc)
180 mode_ptr = Req->argv[mode_arg];
188 if ((*mode_ptr == '+' && !set)
189 || (*mode_ptr == '-' && set)) {
190 /* Action modifier ("+"/"-") must be changed */
191 len = strlen(the_modes) - 1;
192 if (the_modes[len] == '+'
193 || the_modes[len] == '-') {
194 /* Last character in the "result
195 * string" was an "action", so just
196 * overwrite it with the new action */
197 the_modes[len] = *mode_ptr;
199 /* Append new modifier character to
200 * the resulting mode string */
202 strlcat(the_modes, x,
205 if (*mode_ptr == '+')
216 case 'i': /* Invisible */
217 case 's': /* Server messages */
218 case 'w': /* Wallops messages */
222 if (Client_Type(Client) == CLIENT_SERVER) {
224 Client_SetAway(Origin, DEFAULT_AWAY_MSG);
226 ok = IRC_WriteStrClient(Origin,
227 ERR_NOPRIVILEGES_MSG,
230 case 'c': /* Receive connect notices
231 * (only settable by IRC operators!) */
232 if (!set || Client_Type(Client) == CLIENT_SERVER
233 || Client_OperByMe(Origin))
236 ok = IRC_WriteStrClient(Origin,
237 ERR_NOPRIVILEGES_MSG,
240 case 'o': /* IRC operator (only unsettable!) */
241 if (!set || Client_Type(Client) == CLIENT_SERVER) {
242 Client_SetOperByMe(Target, false);
245 ok = IRC_WriteStrClient(Origin,
246 ERR_NOPRIVILEGES_MSG,
249 case 'r': /* Restricted (only settable) */
250 if (set || Client_Type(Client) == CLIENT_SERVER)
253 ok = IRC_WriteStrClient(Origin,
257 case 'x': /* Cloak hostname */
258 if (Client_HasMode(Client, 'r'))
259 ok = IRC_WriteStrClient(Origin,
266 if (Client_Type(Client) != CLIENT_SERVER) {
268 "Unknown mode \"%c%c\" from \"%s\"!?",
269 set ? '+' : '-', *mode_ptr,
271 ok = IRC_WriteStrClient(Origin,
272 ERR_UMODEUNKNOWNFLAG2_MSG,
279 "Handling unknown mode \"%c%c\" from \"%s\" for \"%s\" ...",
280 set ? '+' : '-', *mode_ptr,
281 Client_ID(Origin), Client_ID(Target));
289 /* Is there a valid mode change? */
294 if (Client_ModeAdd(Target, x[0]))
295 strlcat(the_modes, x, sizeof(the_modes));
297 if (Client_ModeDel(Target, x[0]))
298 strlcat(the_modes, x, sizeof(the_modes));
302 /* Are there changed modes? */
304 /* Remove needless action modifier characters */
305 len = strlen(the_modes) - 1;
306 if (the_modes[len] == '+' || the_modes[len] == '-')
307 the_modes[len] = '\0';
309 if (Client_Type(Client) == CLIENT_SERVER) {
310 /* Forward modes to other servers */
311 if (Client_Conn(Target) != NONE) {
312 /* Remote server (service?) changed modes
313 * for one of our clients. Inform it! */
314 IRC_WriteStrClientPrefix(Target, Origin,
319 IRC_WriteStrServersPrefix(Client, Origin,
324 /* Send reply to client and inform other servers */
325 ok = IRC_WriteStrClientPrefix(Client, Origin,
329 IRC_WriteStrServersPrefix(Client, Origin,
334 LogDebug("%s \"%s\": Mode change, now \"%s\".",
335 Client_TypeText(Target), Client_Mask(Target),
336 Client_Modes(Target));
339 IRC_SetPenalty(Client, 1);
345 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
347 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
348 const char *mode_ptr;
350 /* Member or not? -- That's the question! */
351 if (!Channel_IsMemberOf(Channel, Origin))
352 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
353 Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
355 /* The sender is a member: generate extended reply */
356 strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
357 mode_ptr = the_modes;
363 snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
364 strlcat(the_args, argadd, sizeof(the_args));
367 strlcat(the_args, " ", sizeof(the_args));
368 strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
374 strlcat(the_modes, the_args, sizeof(the_modes));
376 if (!IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
377 Client_ID(Origin), Channel_Name(Channel),
381 if (!IRC_WriteStrClient(Origin, RPL_CREATIONTIME_MSG,
382 Client_ID(Origin), Channel_Name(Channel),
383 Channel_CreationTime(Channel)))
391 * Handle channel mode and channel-user mode changes
394 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
396 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
397 argadd[CLIENT_PASS_LEN], *mode_ptr;
398 bool connected, set, skiponce, retval, onchannel, modeok, use_servermode;
399 int mode_arg, arg_arg, mode_arg_count = 0;
404 if (Channel_IsModeless(Channel))
405 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
406 Client_ID(Client), Channel_Name(Channel));
408 /* Mode request: let's answer it :-) */
410 return Channel_Mode_Answer_Request(Origin, Channel);
412 Channel_CheckAdminRights(Channel, Client, Origin,
413 &onchannel, &modeok, &use_servermode);
415 if (!onchannel && !modeok)
416 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
418 Channel_Name(Channel));
421 mode_ptr = Req->argv[mode_arg];
422 if (Req->argc > mode_arg + 1)
423 arg_arg = mode_arg + 1;
427 /* Initial state: set or unset modes? */
441 /* Prepare reply string */
442 strcpy(the_modes, set ? "+" : "-");
446 connected = CONNECTED;
451 /* Try next argument if there's any */
454 if (arg_arg > mode_arg)
459 if (mode_arg >= Req->argc)
461 mode_ptr = Req->argv[mode_arg];
463 if (Req->argc > mode_arg + 1)
464 arg_arg = mode_arg + 1;
473 if (((*mode_ptr == '+') && !set)
474 || ((*mode_ptr == '-') && set)) {
475 /* Action modifier ("+"/"-") must be changed ... */
476 len = strlen(the_modes) - 1;
477 if (the_modes[len] == '+' || the_modes[len] == '-') {
478 /* Adjust last action modifier in result */
479 the_modes[len] = *mode_ptr;
481 /* Append modifier character to result string */
483 strlcat(the_modes, x, sizeof(the_modes));
485 set = *mode_ptr == '+';
490 /* Are there arguments left? */
491 if (arg_arg >= Req->argc)
499 /* --- Channel modes --- */
500 case 'i': /* Invite only */
501 case 'm': /* Moderated */
502 case 'n': /* Only members can write */
503 case 'R': /* Registered users only */
504 case 's': /* Secret channel */
505 case 't': /* Topic locked */
506 case 'z': /* Secure connections only */
510 connected = IRC_WriteStrClient(Origin,
511 ERR_CHANOPRIVSNEEDED_MSG,
512 Client_ID(Origin), Channel_Name(Channel));
514 case 'k': /* Channel key */
515 if (Mode_Limit_Reached(Client, mode_arg_count++))
521 connected = IRC_WriteStrClient(Origin,
522 ERR_CHANOPRIVSNEEDED_MSG,
524 Channel_Name(Channel));
527 if (arg_arg > mode_arg) {
529 Channel_ModeDel(Channel, 'k');
530 Channel_SetKey(Channel,
532 strlcpy(argadd, Channel_Key(Channel),
536 connected = IRC_WriteStrClient(Origin,
537 ERR_CHANOPRIVSNEEDED_MSG,
539 Channel_Name(Channel));
541 Req->argv[arg_arg][0] = '\0';
544 connected = IRC_WriteStrClient(Origin,
545 ERR_NEEDMOREPARAMS_MSG,
546 Client_ID(Origin), Req->command);
550 case 'l': /* Member limit */
551 if (Mode_Limit_Reached(Client, mode_arg_count++))
557 connected = IRC_WriteStrClient(Origin,
558 ERR_CHANOPRIVSNEEDED_MSG,
560 Channel_Name(Channel));
563 if (arg_arg > mode_arg) {
565 l = atol(Req->argv[arg_arg]);
566 if (l > 0 && l < 0xFFFF) {
567 Channel_ModeDel(Channel, 'l');
568 Channel_SetMaxUsers(Channel, l);
569 snprintf(argadd, sizeof(argadd),
574 connected = IRC_WriteStrClient(Origin,
575 ERR_CHANOPRIVSNEEDED_MSG,
577 Channel_Name(Channel));
579 Req->argv[arg_arg][0] = '\0';
582 connected = IRC_WriteStrClient(Origin,
583 ERR_NEEDMOREPARAMS_MSG,
584 Client_ID(Origin), Req->command);
588 case 'O': /* IRC operators only */
590 /* Only IRC operators are allowed to
591 * set the 'O' channel mode! */
592 if (set && !(Client_OperByMe(Client)
593 || Client_Type(Client) == CLIENT_SERVER))
594 connected = IRC_WriteStrClient(Origin,
595 ERR_NOPRIVILEGES_MSG,
600 connected = IRC_WriteStrClient(Origin,
601 ERR_CHANOPRIVSNEEDED_MSG,
603 Channel_Name(Channel));
605 case 'P': /* Persistent channel */
607 /* Only IRC operators are allowed to
608 * set the 'P' channel mode! */
609 if (set && !(Client_OperByMe(Client)
610 || Client_Type(Client) == CLIENT_SERVER))
611 connected = IRC_WriteStrClient(Origin,
612 ERR_NOPRIVILEGES_MSG,
617 connected = IRC_WriteStrClient(Origin,
618 ERR_CHANOPRIVSNEEDED_MSG,
620 Channel_Name(Channel));
622 /* --- Channel user modes --- */
626 if (Client_Type(Client) != CLIENT_SERVER) {
627 connected = IRC_WriteStrClient(Origin,
628 ERR_CHANOPRIVSNEEDED_MSG,
630 Channel_Name(Channel));
633 case 'o': /* Channel operator */
634 case 'v': /* Voice */
635 if (arg_arg > mode_arg) {
637 client = Client_Search(Req->argv[arg_arg]);
641 connected = IRC_WriteStrClient(Client,
646 connected = IRC_WriteStrClient(Origin,
647 ERR_CHANOPRIVSNEEDED_MSG,
649 Channel_Name(Channel));
651 Req->argv[arg_arg][0] = '\0';
654 connected = IRC_WriteStrClient(Origin,
655 ERR_NEEDMOREPARAMS_MSG,
656 Client_ID(Origin), Req->command);
660 /* --- Channel lists --- */
661 case 'I': /* Invite lists */
662 case 'b': /* Ban lists */
663 if (Mode_Limit_Reached(Client, mode_arg_count++))
665 if (arg_arg > mode_arg) {
669 ? Add_Ban_Invite(*mode_ptr, Origin,
672 : Del_Ban_Invite(*mode_ptr, Origin,
676 connected = IRC_WriteStrClient(Origin,
677 ERR_CHANOPRIVSNEEDED_MSG,
679 Channel_Name(Channel));
681 Req->argv[arg_arg][0] = '\0';
684 if (*mode_ptr == 'I')
685 Channel_ShowInvites(Origin, Channel);
687 Channel_ShowBans(Origin, Channel);
691 if (Client_Type(Client) != CLIENT_SERVER) {
693 "Unknown mode \"%c%c\" from \"%s\" on %s!?",
694 set ? '+' : '-', *mode_ptr,
695 Client_ID(Origin), Channel_Name(Channel));
696 connected = IRC_WriteStrClient(Origin,
698 Client_ID(Origin), *mode_ptr,
699 Channel_Name(Channel));
703 "Handling unknown mode \"%c%c\" from \"%s\" on %s ...",
704 set ? '+' : '-', *mode_ptr,
705 Client_ID(Origin), Channel_Name(Channel));
713 /* Is there a valid mode change? */
717 /* Validate target client */
718 if (client && (!Channel_IsMemberOf(Channel, client))) {
719 if (!IRC_WriteStrClient
720 (Origin, ERR_USERNOTINCHANNEL_MSG,
721 Client_ID(Origin), Client_ID(client),
722 Channel_Name(Channel)))
729 /* Channel-User-Mode */
731 ? Channel_UserModeAdd(Channel, client, x[0])
732 : Channel_UserModeDel(Channel, client, x[0]);
734 strlcat(the_args, " ", sizeof(the_args));
735 strlcat(the_args, Client_ID(client),
737 strlcat(the_modes, x, sizeof(the_modes));
739 ("User \"%s\": Mode change on %s, now \"%s\"",
740 Client_Mask(client), Channel_Name(Channel),
741 Channel_UserModes(Channel, client));
746 ? Channel_ModeAdd(Channel, x[0])
747 : Channel_ModeDel(Channel, x[0]);
749 strlcat(the_modes, x, sizeof(the_modes));
750 LogDebug("Channel %s: Mode change, now \"%s\".",
751 Channel_Name(Channel),
752 Channel_Modes(Channel));
756 /* Are there additional arguments to add? */
758 strlcat(the_args, " ", sizeof(the_args));
759 strlcat(the_args, argadd, sizeof(the_args));
764 /* Are there changed modes? */
766 /* Clean up mode string */
767 len = strlen(the_modes) - 1;
768 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
769 the_modes[len] = '\0';
771 if (Client_Type(Client) == CLIENT_SERVER) {
772 /* MODE requests for local channels from other servers
773 * are definitely invalid! */
774 if (Channel_IsLocal(Channel)) {
775 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
779 /* Forward mode changes to channel users and all the
780 * other remote servers: */
781 IRC_WriteStrServersPrefix(Client, Origin,
782 "MODE %s %s%s", Channel_Name(Channel),
783 the_modes, the_args);
784 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
785 false, "MODE %s %s%s", Channel_Name(Channel),
786 the_modes, the_args);
789 Origin = Client_ThisServer();
790 /* Send reply to client and inform other servers and channel users */
791 connected = IRC_WriteStrClientPrefix(Client, Origin,
792 "MODE %s %s%s", Channel_Name(Channel),
793 the_modes, the_args);
794 /* Only forward requests for non-local channels */
795 if (!Channel_IsLocal(Channel))
796 IRC_WriteStrServersPrefix(Client, Origin,
797 "MODE %s %s%s", Channel_Name(Channel),
798 the_modes, the_args);
799 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
800 false, "MODE %s %s%s", Channel_Name(Channel),
801 the_modes, the_args);
805 IRC_SetPenalty(Client, 1);
811 IRC_AWAY( CLIENT *Client, REQUEST *Req )
813 assert( Client != NULL );
814 assert( Req != NULL );
816 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
818 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
820 Client_SetAway( Client, Req->argv[0] );
821 Client_ModeAdd( Client, 'a' );
822 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
823 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
827 Client_ModeDel( Client, 'a' );
828 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
829 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
835 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
841 assert( Client != NULL );
842 assert( Channel != NULL );
843 assert( Pattern != NULL );
844 assert(what == 'I' || what == 'b');
846 mask = Lists_MakeMask(Pattern);
848 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
851 ret = Channel_AddInvite(Channel, mask, false);
853 ret = Channel_AddBan(Channel, mask);
857 if (already && (Client_Type(Prefix) == CLIENT_SERVER))
861 return Send_ListChange("+I", Prefix, Client, Channel, mask);
862 return Send_ListChange("+b", Prefix, Client, Channel, mask);
867 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
870 struct list_head *list;
872 assert( Client != NULL );
873 assert( Channel != NULL );
874 assert( Pattern != NULL );
875 assert(what == 'I' || what == 'b');
877 mask = Lists_MakeMask( Pattern );
880 list = Channel_GetListInvites(Channel);
882 list = Channel_GetListBans(Channel);
884 Lists_Del(list, mask);
886 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
887 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
892 Send_ListChange(const char *Mode, CLIENT *Prefix, CLIENT *Client,
893 CHANNEL *Channel, const char *Mask)
897 if( Client_Type( Client ) == CLIENT_USER )
899 /* send confirmation to client */
900 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
904 /* to other servers */
905 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
907 /* and local users in channel */
908 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
911 } /* Send_ListChange */