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 Log(LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\"!?",
246 set ? '+' : '-', *mode_ptr, Client_ID(Origin));
247 if (Client_Type(Client) != CLIENT_SERVER)
248 ok = IRC_WriteStrClient(Origin,
249 ERR_UMODEUNKNOWNFLAG2_MSG,
260 /* Is there a valid mode change? */
265 if (Client_ModeAdd(Target, x[0]))
266 strlcat(the_modes, x, sizeof(the_modes));
268 if (Client_ModeDel(Target, x[0]))
269 strlcat(the_modes, x, sizeof(the_modes));
274 /* Are there changed modes? */
276 /* Remoce needless action modifier characters */
277 len = strlen(the_modes) - 1;
278 if (the_modes[len] == '+' || the_modes[len] == '-')
279 the_modes[len] = '\0';
281 if (Client_Type(Client) == CLIENT_SERVER) {
282 /* Forward modes to other servers */
283 if (Client_Conn(Target) != NONE) {
284 /* Remote server (service?) changed modes
285 * for one of our clients. Inform it! */
286 IRC_WriteStrClientPrefix(Target, Origin,
291 IRC_WriteStrServersPrefix(Client, Origin,
296 /* Send reply to client and inform other servers */
297 ok = IRC_WriteStrClientPrefix(Client, Origin,
301 IRC_WriteStrServersPrefix(Client, Origin,
306 LogDebug("%s \"%s\": Mode change, now \"%s\".",
307 Client_TypeText(Target), Client_Mask(Target),
308 Client_Modes(Target));
311 IRC_SetPenalty(Client, 1);
317 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
319 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
320 const char *mode_ptr;
322 /* Member or not? -- That's the question! */
323 if (!Channel_IsMemberOf(Channel, Origin))
324 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
325 Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
327 /* The sender is a member: generate extended reply */
328 strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
329 mode_ptr = the_modes;
335 snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
336 strlcat(the_args, argadd, sizeof(the_args));
339 strlcat(the_args, " ", sizeof(the_args));
340 strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
346 strlcat(the_modes, the_args, sizeof(the_modes));
348 if (!IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
349 Client_ID(Origin), Channel_Name(Channel),
353 if (!IRC_WriteStrClient(Origin, RPL_CREATIONTIME_MSG,
354 Client_ID(Origin), Channel_Name(Channel),
355 Channel_CreationTime(Channel)))
363 * Handle channel mode and channel-user mode changes
366 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
368 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
369 argadd[CLIENT_PASS_LEN], *mode_ptr;
370 bool connected, set, skiponce, retval, onchannel, modeok, use_servermode;
371 int mode_arg, arg_arg;
376 if (Channel_IsModeless(Channel))
377 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
378 Client_ID(Client), Channel_Name(Channel));
380 /* Mode request: let's answer it :-) */
382 return Channel_Mode_Answer_Request(Origin, Channel);
384 Channel_CheckAdminRights(Channel, Client, Origin,
385 &onchannel, &modeok, &use_servermode);
387 if (!onchannel && !modeok)
388 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
390 Channel_Name(Channel));
393 mode_ptr = Req->argv[mode_arg];
394 if (Req->argc > mode_arg + 1)
395 arg_arg = mode_arg + 1;
399 /* Initial state: set or unset modes? */
413 /* Prepare reply string */
414 strcpy(the_modes, set ? "+" : "-");
418 connected = CONNECTED;
423 /* Try next argument if there's any */
424 if (arg_arg > mode_arg)
429 if (mode_arg >= Req->argc)
431 mode_ptr = Req->argv[mode_arg];
433 if (Req->argc > mode_arg + 1)
434 arg_arg = mode_arg + 1;
443 if (((*mode_ptr == '+') && !set)
444 || ((*mode_ptr == '-') && set)) {
445 /* Action modifier ("+"/"-") must be changed ... */
446 len = strlen(the_modes) - 1;
447 if (the_modes[len] == '+' || the_modes[len] == '-') {
448 /* Adjust last action modifier in result */
449 the_modes[len] = *mode_ptr;
451 /* Append modifier character to result string */
453 strlcat(the_modes, x, sizeof(the_modes));
455 set = *mode_ptr == '+';
460 /* Are there arguments left? */
461 if (arg_arg >= Req->argc)
469 /* --- Channel modes --- */
470 case 'i': /* Invite only */
471 case 'm': /* Moderated */
472 case 'n': /* Only members can write */
473 case 's': /* Secret channel */
474 case 't': /* Topic locked */
475 case 'z': /* Secure connections only */
479 connected = IRC_WriteStrClient(Origin,
480 ERR_CHANOPRIVSNEEDED_MSG,
481 Client_ID(Origin), Channel_Name(Channel));
483 case 'k': /* Channel key */
488 connected = IRC_WriteStrClient(Origin,
489 ERR_CHANOPRIVSNEEDED_MSG,
491 Channel_Name(Channel));
494 if (arg_arg > mode_arg) {
496 Channel_ModeDel(Channel, 'k');
497 Channel_SetKey(Channel,
499 strlcpy(argadd, Channel_Key(Channel),
503 connected = IRC_WriteStrClient(Origin,
504 ERR_CHANOPRIVSNEEDED_MSG,
506 Channel_Name(Channel));
508 Req->argv[arg_arg][0] = '\0';
511 connected = IRC_WriteStrClient(Origin,
512 ERR_NEEDMOREPARAMS_MSG,
513 Client_ID(Origin), Req->command);
517 case 'l': /* Member limit */
522 connected = IRC_WriteStrClient(Origin,
523 ERR_CHANOPRIVSNEEDED_MSG,
525 Channel_Name(Channel));
528 if (arg_arg > mode_arg) {
530 l = atol(Req->argv[arg_arg]);
531 if (l > 0 && l < 0xFFFF) {
532 Channel_ModeDel(Channel, 'l');
533 Channel_SetMaxUsers(Channel, l);
534 snprintf(argadd, sizeof(argadd),
539 connected = IRC_WriteStrClient(Origin,
540 ERR_CHANOPRIVSNEEDED_MSG,
542 Channel_Name(Channel));
544 Req->argv[arg_arg][0] = '\0';
547 connected = IRC_WriteStrClient(Origin,
548 ERR_NEEDMOREPARAMS_MSG,
549 Client_ID(Origin), Req->command);
553 case 'O': /* IRC operators only */
555 /* Only IRC operators are allowed to
556 * set the 'O' channel mode! */
557 if (set && !(Client_OperByMe(Client)
558 || Client_Type(Client) == CLIENT_SERVER))
559 connected = IRC_WriteStrClient(Origin,
560 ERR_NOPRIVILEGES_MSG,
565 connected = IRC_WriteStrClient(Origin,
566 ERR_CHANOPRIVSNEEDED_MSG,
568 Channel_Name(Channel));
570 case 'P': /* Persistent channel */
572 /* Only IRC operators are allowed to
573 * set the 'P' channel mode! */
574 if (set && !(Client_OperByMe(Client)
575 || Client_Type(Client) == CLIENT_SERVER))
576 connected = IRC_WriteStrClient(Origin,
577 ERR_NOPRIVILEGES_MSG,
582 connected = IRC_WriteStrClient(Origin,
583 ERR_CHANOPRIVSNEEDED_MSG,
585 Channel_Name(Channel));
587 /* --- Channel user modes --- */
588 case 'o': /* Channel operator */
589 case 'v': /* Voice */
590 if (arg_arg > mode_arg) {
592 client = Client_Search(Req->argv[arg_arg]);
596 connected = IRC_WriteStrClient(Client,
601 connected = IRC_WriteStrClient(Origin,
602 ERR_CHANOPRIVSNEEDED_MSG,
604 Channel_Name(Channel));
606 Req->argv[arg_arg][0] = '\0';
609 connected = IRC_WriteStrClient(Origin,
610 ERR_NEEDMOREPARAMS_MSG,
611 Client_ID(Origin), Req->command);
615 /* --- Channel lists --- */
616 case 'I': /* Invite lists */
617 case 'b': /* Ban lists */
618 if (arg_arg > mode_arg) {
622 ? Add_Ban_Invite(*mode_ptr, Origin,
625 : Del_Ban_Invite(*mode_ptr, Origin,
629 connected = IRC_WriteStrClient(Origin,
630 ERR_CHANOPRIVSNEEDED_MSG,
632 Channel_Name(Channel));
634 Req->argv[arg_arg][0] = '\0';
637 if (*mode_ptr == 'I')
638 Channel_ShowInvites(Origin, Channel);
640 Channel_ShowBans(Origin, Channel);
645 "Unknown mode \"%c%c\" from \"%s\" on %s!?",
646 set ? '+' : '-', *mode_ptr, Client_ID(Origin),
647 Channel_Name(Channel));
648 if (Client_Type(Client) != CLIENT_SERVER)
649 connected = IRC_WriteStrClient(Origin,
650 ERR_UMODEUNKNOWNFLAG2_MSG,
652 set ? '+' : '-', *mode_ptr);
660 /* Is there a valid mode change? */
664 /* Validate target client */
665 if (client && (!Channel_IsMemberOf(Channel, client))) {
666 if (!IRC_WriteStrClient
667 (Origin, ERR_USERNOTINCHANNEL_MSG,
668 Client_ID(Origin), Client_ID(client),
669 Channel_Name(Channel)))
676 /* Channel-User-Mode */
678 ? Channel_UserModeAdd(Channel, client, x[0])
679 : Channel_UserModeDel(Channel, client, x[0]);
681 strlcat(the_args, " ", sizeof(the_args));
682 strlcat(the_args, Client_ID(client),
684 strlcat(the_modes, x, sizeof(the_modes));
686 ("User \"%s\": Mode change on %s, now \"%s\"",
687 Client_Mask(client), Channel_Name(Channel),
688 Channel_UserModes(Channel, client));
693 ? Channel_ModeAdd(Channel, x[0])
694 : Channel_ModeDel(Channel, x[0]);
696 strlcat(the_modes, x, sizeof(the_modes));
697 LogDebug("Channel %s: Mode change, now \"%s\".",
698 Channel_Name(Channel),
699 Channel_Modes(Channel));
703 /* Are there additional arguments to add? */
705 strlcat(the_args, " ", sizeof(the_args));
706 strlcat(the_args, argadd, sizeof(the_args));
711 /* Are there changed modes? */
713 /* Clean up mode string */
714 len = strlen(the_modes) - 1;
715 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
716 the_modes[len] = '\0';
718 if (Client_Type(Client) == CLIENT_SERVER) {
719 /* MODE requests for local channels from other servers
720 * are definitely invalid! */
721 if (Channel_IsLocal(Channel)) {
722 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
726 /* Forward mode changes to channel users and all the
727 * other remote servers: */
728 IRC_WriteStrServersPrefix(Client, Origin,
729 "MODE %s %s%s", Channel_Name(Channel),
730 the_modes, the_args);
731 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
732 false, "MODE %s %s%s", Channel_Name(Channel),
733 the_modes, the_args);
736 Origin = Client_ThisServer();
737 /* Send reply to client and inform other servers and channel users */
738 connected = IRC_WriteStrClientPrefix(Client, Origin,
739 "MODE %s %s%s", Channel_Name(Channel),
740 the_modes, the_args);
741 /* Only forward requests for non-local channels */
742 if (!Channel_IsLocal(Channel))
743 IRC_WriteStrServersPrefix(Client, Origin,
744 "MODE %s %s%s", Channel_Name(Channel),
745 the_modes, the_args);
746 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
747 false, "MODE %s %s%s", Channel_Name(Channel),
748 the_modes, the_args);
752 IRC_SetPenalty(Client, 1);
758 IRC_AWAY( CLIENT *Client, REQUEST *Req )
760 assert( Client != NULL );
761 assert( Req != NULL );
763 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
765 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
767 Client_SetAway( Client, Req->argv[0] );
768 Client_ModeAdd( Client, 'a' );
769 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
770 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
774 Client_ModeDel( Client, 'a' );
775 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
776 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
782 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
788 assert( Client != NULL );
789 assert( Channel != NULL );
790 assert( Pattern != NULL );
791 assert(what == 'I' || what == 'b');
793 mask = Lists_MakeMask(Pattern);
795 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
798 ret = Channel_AddInvite(Channel, mask, false);
800 ret = Channel_AddBan(Channel, mask);
804 if (already && (Client_Type(Prefix) == CLIENT_SERVER))
808 return Send_ListChange("+I", Prefix, Client, Channel, mask);
809 return Send_ListChange("+b", Prefix, Client, Channel, mask);
814 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
817 struct list_head *list;
819 assert( Client != NULL );
820 assert( Channel != NULL );
821 assert( Pattern != NULL );
822 assert(what == 'I' || what == 'b');
824 mask = Lists_MakeMask( Pattern );
827 list = Channel_GetListInvites(Channel);
829 list = Channel_GetListBans(Channel);
831 Lists_Del(list, mask);
833 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
834 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
839 Send_ListChange(const char *Mode, CLIENT *Prefix, CLIENT *Client,
840 CHANNEL *Channel, const char *Mask)
844 if( Client_Type( Client ) == CLIENT_USER )
846 /* send confirmation to client */
847 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
851 /* to other servers */
852 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
854 /* and local users in channel */
855 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
858 } /* Send_ListChange */