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 * 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,
258 "Handling unknown mode \"%c%c\" from \"%s\" for \"%s\" ...",
259 set ? '+' : '-', *mode_ptr,
260 Client_ID(Origin), Client_ID(Target));
268 /* Is there a valid mode change? */
273 if (Client_ModeAdd(Target, x[0]))
274 strlcat(the_modes, x, sizeof(the_modes));
276 if (Client_ModeDel(Target, x[0]))
277 strlcat(the_modes, x, sizeof(the_modes));
281 /* Are there changed modes? */
283 /* Remove needless action modifier characters */
284 len = strlen(the_modes) - 1;
285 if (the_modes[len] == '+' || the_modes[len] == '-')
286 the_modes[len] = '\0';
288 if (Client_Type(Client) == CLIENT_SERVER) {
289 /* Forward modes to other servers */
290 if (Client_Conn(Target) != NONE) {
291 /* Remote server (service?) changed modes
292 * for one of our clients. Inform it! */
293 IRC_WriteStrClientPrefix(Target, Origin,
298 IRC_WriteStrServersPrefix(Client, Origin,
303 /* Send reply to client and inform other servers */
304 ok = IRC_WriteStrClientPrefix(Client, Origin,
308 IRC_WriteStrServersPrefix(Client, Origin,
313 LogDebug("%s \"%s\": Mode change, now \"%s\".",
314 Client_TypeText(Target), Client_Mask(Target),
315 Client_Modes(Target));
318 IRC_SetPenalty(Client, 1);
324 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
326 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
327 const char *mode_ptr;
329 /* Member or not? -- That's the question! */
330 if (!Channel_IsMemberOf(Channel, Origin))
331 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
332 Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
334 /* The sender is a member: generate extended reply */
335 strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
336 mode_ptr = the_modes;
342 snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
343 strlcat(the_args, argadd, sizeof(the_args));
346 strlcat(the_args, " ", sizeof(the_args));
347 strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
353 strlcat(the_modes, the_args, sizeof(the_modes));
355 if (!IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
356 Client_ID(Origin), Channel_Name(Channel),
360 if (!IRC_WriteStrClient(Origin, RPL_CREATIONTIME_MSG,
361 Client_ID(Origin), Channel_Name(Channel),
362 Channel_CreationTime(Channel)))
370 * Handle channel mode and channel-user mode changes
373 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
375 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
376 argadd[CLIENT_PASS_LEN], *mode_ptr;
377 bool connected, set, skiponce, retval, onchannel, modeok, use_servermode;
378 int mode_arg, arg_arg, mode_arg_count = 0;
383 if (Channel_IsModeless(Channel))
384 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
385 Client_ID(Client), Channel_Name(Channel));
387 /* Mode request: let's answer it :-) */
389 return Channel_Mode_Answer_Request(Origin, Channel);
391 Channel_CheckAdminRights(Channel, Client, Origin,
392 &onchannel, &modeok, &use_servermode);
394 if (!onchannel && !modeok)
395 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
397 Channel_Name(Channel));
400 mode_ptr = Req->argv[mode_arg];
401 if (Req->argc > mode_arg + 1)
402 arg_arg = mode_arg + 1;
406 /* Initial state: set or unset modes? */
420 /* Prepare reply string */
421 strcpy(the_modes, set ? "+" : "-");
425 connected = CONNECTED;
430 /* 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 'R': /* Registered users only */
483 case 's': /* Secret channel */
484 case 't': /* Topic locked */
485 case 'z': /* Secure connections only */
489 connected = IRC_WriteStrClient(Origin,
490 ERR_CHANOPRIVSNEEDED_MSG,
491 Client_ID(Origin), Channel_Name(Channel));
493 case 'k': /* Channel key */
494 if (mode_arg_count++ >= MAX_HNDL_MODES_ARG)
500 connected = IRC_WriteStrClient(Origin,
501 ERR_CHANOPRIVSNEEDED_MSG,
503 Channel_Name(Channel));
506 if (arg_arg > mode_arg) {
508 Channel_ModeDel(Channel, 'k');
509 Channel_SetKey(Channel,
511 strlcpy(argadd, Channel_Key(Channel),
515 connected = IRC_WriteStrClient(Origin,
516 ERR_CHANOPRIVSNEEDED_MSG,
518 Channel_Name(Channel));
520 Req->argv[arg_arg][0] = '\0';
523 connected = IRC_WriteStrClient(Origin,
524 ERR_NEEDMOREPARAMS_MSG,
525 Client_ID(Origin), Req->command);
529 case 'l': /* Member limit */
530 if (mode_arg_count++ >= MAX_HNDL_MODES_ARG)
536 connected = IRC_WriteStrClient(Origin,
537 ERR_CHANOPRIVSNEEDED_MSG,
539 Channel_Name(Channel));
542 if (arg_arg > mode_arg) {
544 l = atol(Req->argv[arg_arg]);
545 if (l > 0 && l < 0xFFFF) {
546 Channel_ModeDel(Channel, 'l');
547 Channel_SetMaxUsers(Channel, l);
548 snprintf(argadd, sizeof(argadd),
553 connected = IRC_WriteStrClient(Origin,
554 ERR_CHANOPRIVSNEEDED_MSG,
556 Channel_Name(Channel));
558 Req->argv[arg_arg][0] = '\0';
561 connected = IRC_WriteStrClient(Origin,
562 ERR_NEEDMOREPARAMS_MSG,
563 Client_ID(Origin), Req->command);
567 case 'O': /* IRC operators only */
569 /* Only IRC operators are allowed to
570 * set the 'O' channel mode! */
571 if (set && !(Client_OperByMe(Client)
572 || Client_Type(Client) == CLIENT_SERVER))
573 connected = IRC_WriteStrClient(Origin,
574 ERR_NOPRIVILEGES_MSG,
579 connected = IRC_WriteStrClient(Origin,
580 ERR_CHANOPRIVSNEEDED_MSG,
582 Channel_Name(Channel));
584 case 'P': /* Persistent channel */
586 /* Only IRC operators are allowed to
587 * set the 'P' channel mode! */
588 if (set && !(Client_OperByMe(Client)
589 || Client_Type(Client) == CLIENT_SERVER))
590 connected = IRC_WriteStrClient(Origin,
591 ERR_NOPRIVILEGES_MSG,
596 connected = IRC_WriteStrClient(Origin,
597 ERR_CHANOPRIVSNEEDED_MSG,
599 Channel_Name(Channel));
601 /* --- Channel user modes --- */
605 if (Client_Type(Client) != CLIENT_SERVER) {
606 connected = IRC_WriteStrClient(Origin,
607 ERR_CHANOPRIVSNEEDED_MSG,
609 Channel_Name(Channel));
612 case 'o': /* Channel operator */
613 case 'v': /* Voice */
614 if (arg_arg > mode_arg) {
616 client = Client_Search(Req->argv[arg_arg]);
620 connected = IRC_WriteStrClient(Client,
625 connected = IRC_WriteStrClient(Origin,
626 ERR_CHANOPRIVSNEEDED_MSG,
628 Channel_Name(Channel));
630 Req->argv[arg_arg][0] = '\0';
633 connected = IRC_WriteStrClient(Origin,
634 ERR_NEEDMOREPARAMS_MSG,
635 Client_ID(Origin), Req->command);
639 /* --- Channel lists --- */
640 case 'I': /* Invite lists */
641 case 'b': /* Ban lists */
642 if (mode_arg_count++ >= MAX_HNDL_MODES_ARG)
644 if (arg_arg > mode_arg) {
648 ? Add_Ban_Invite(*mode_ptr, Origin,
651 : Del_Ban_Invite(*mode_ptr, Origin,
655 connected = IRC_WriteStrClient(Origin,
656 ERR_CHANOPRIVSNEEDED_MSG,
658 Channel_Name(Channel));
660 Req->argv[arg_arg][0] = '\0';
663 if (*mode_ptr == 'I')
664 Channel_ShowInvites(Origin, Channel);
666 Channel_ShowBans(Origin, Channel);
670 if (Client_Type(Client) != CLIENT_SERVER) {
672 "Unknown mode \"%c%c\" from \"%s\" on %s!?",
673 set ? '+' : '-', *mode_ptr,
674 Client_ID(Origin), Channel_Name(Channel));
675 connected = IRC_WriteStrClient(Origin,
676 ERR_UMODEUNKNOWNFLAG2_MSG,
678 set ? '+' : '-', *mode_ptr);
682 "Handling unknown mode \"%c%c\" from \"%s\" on %s ...",
683 set ? '+' : '-', *mode_ptr,
684 Client_ID(Origin), Channel_Name(Channel));
692 /* Is there a valid mode change? */
696 /* Validate target client */
697 if (client && (!Channel_IsMemberOf(Channel, client))) {
698 if (!IRC_WriteStrClient
699 (Origin, ERR_USERNOTINCHANNEL_MSG,
700 Client_ID(Origin), Client_ID(client),
701 Channel_Name(Channel)))
708 /* Channel-User-Mode */
710 ? Channel_UserModeAdd(Channel, client, x[0])
711 : Channel_UserModeDel(Channel, client, x[0]);
713 strlcat(the_args, " ", sizeof(the_args));
714 strlcat(the_args, Client_ID(client),
716 strlcat(the_modes, x, sizeof(the_modes));
718 ("User \"%s\": Mode change on %s, now \"%s\"",
719 Client_Mask(client), Channel_Name(Channel),
720 Channel_UserModes(Channel, client));
725 ? Channel_ModeAdd(Channel, x[0])
726 : Channel_ModeDel(Channel, x[0]);
728 strlcat(the_modes, x, sizeof(the_modes));
729 LogDebug("Channel %s: Mode change, now \"%s\".",
730 Channel_Name(Channel),
731 Channel_Modes(Channel));
735 /* Are there additional arguments to add? */
737 strlcat(the_args, " ", sizeof(the_args));
738 strlcat(the_args, argadd, sizeof(the_args));
743 /* Are there changed modes? */
745 /* Clean up mode string */
746 len = strlen(the_modes) - 1;
747 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
748 the_modes[len] = '\0';
750 if (Client_Type(Client) == CLIENT_SERVER) {
751 /* MODE requests for local channels from other servers
752 * are definitely invalid! */
753 if (Channel_IsLocal(Channel)) {
754 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
758 /* Forward mode changes to channel users and all the
759 * other remote servers: */
760 IRC_WriteStrServersPrefix(Client, Origin,
761 "MODE %s %s%s", Channel_Name(Channel),
762 the_modes, the_args);
763 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
764 false, "MODE %s %s%s", Channel_Name(Channel),
765 the_modes, the_args);
768 Origin = Client_ThisServer();
769 /* Send reply to client and inform other servers and channel users */
770 connected = IRC_WriteStrClientPrefix(Client, Origin,
771 "MODE %s %s%s", Channel_Name(Channel),
772 the_modes, the_args);
773 /* Only forward requests for non-local channels */
774 if (!Channel_IsLocal(Channel))
775 IRC_WriteStrServersPrefix(Client, Origin,
776 "MODE %s %s%s", Channel_Name(Channel),
777 the_modes, the_args);
778 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
779 false, "MODE %s %s%s", Channel_Name(Channel),
780 the_modes, the_args);
784 IRC_SetPenalty(Client, 1);
790 IRC_AWAY( CLIENT *Client, REQUEST *Req )
792 assert( Client != NULL );
793 assert( Req != NULL );
795 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
797 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
799 Client_SetAway( Client, Req->argv[0] );
800 Client_ModeAdd( Client, 'a' );
801 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
802 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
806 Client_ModeDel( Client, 'a' );
807 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
808 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
814 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
820 assert( Client != NULL );
821 assert( Channel != NULL );
822 assert( Pattern != NULL );
823 assert(what == 'I' || what == 'b');
825 mask = Lists_MakeMask(Pattern);
827 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
830 ret = Channel_AddInvite(Channel, mask, false);
832 ret = Channel_AddBan(Channel, mask);
836 if (already && (Client_Type(Prefix) == CLIENT_SERVER))
840 return Send_ListChange("+I", Prefix, Client, Channel, mask);
841 return Send_ListChange("+b", Prefix, Client, Channel, mask);
846 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
849 struct list_head *list;
851 assert( Client != NULL );
852 assert( Channel != NULL );
853 assert( Pattern != NULL );
854 assert(what == 'I' || what == 'b');
856 mask = Lists_MakeMask( Pattern );
859 list = Channel_GetListInvites(Channel);
861 list = Channel_GetListBans(Channel);
863 Lists_Del(list, mask);
865 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
866 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
871 Send_ListChange(const char *Mode, CLIENT *Prefix, CLIENT *Client,
872 CHANNEL *Channel, const char *Mask)
876 if( Client_Type( Client ) == CLIENT_USER )
878 /* send confirmation to client */
879 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
883 /* to other servers */
884 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
886 /* and local users in channel */
887 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
890 } /* Send_ListChange */