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];
168 if((( *mode_ptr == '+' ) && ( ! set )) || (( *mode_ptr == '-' ) && ( set )))
170 /* Action modifier ("+"/"-") must be changed ... */
171 len = strlen( the_modes ) - 1;
172 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' ))
174 /* Adjust last action modifier in result */
175 the_modes[len] = *mode_ptr;
179 /* Append modifier character to result string */
181 strlcat( the_modes, x, sizeof( the_modes ));
183 if( *mode_ptr == '+' ) set = true;
193 case 'i': /* Invisible */
194 case 's': /* Server messages */
195 case 'w': /* Wallops messages */
200 if( Client_Type( Client ) == CLIENT_SERVER )
203 Client_SetAway( Origin, DEFAULT_AWAY_MSG );
205 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
208 case 'c': /* Receive connect notices
209 * (only settable by IRC operators!) */
210 if(!set || Client_OperByMe(Origin)
211 || Client_Type(Client) == CLIENT_SERVER)
214 ok = IRC_WriteStrClient(Origin,
215 ERR_NOPRIVILEGES_MSG,
219 case 'o': /* IRC operator (only unsettable!) */
220 if(( ! set ) || ( Client_Type( Client ) == CLIENT_SERVER ))
222 Client_SetOperByMe( Target, false );
225 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
228 case 'r': /* Restricted (only settable) */
229 if(( set ) || ( Client_Type( Client ) == CLIENT_SERVER )) x[0] = 'r';
230 else ok = IRC_WriteStrClient( Origin, ERR_RESTRICTED_MSG, Client_ID( Origin ));
233 case 'x': /* Cloak hostname */
234 if (Client_HasMode(Client, 'r'))
235 ok = IRC_WriteStrClient(Origin,
243 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\"!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ));
244 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
251 /* Is there a valid mode change? */
256 if (Client_ModeAdd(Target, x[0]))
257 strlcat(the_modes, x, sizeof(the_modes));
259 if (Client_ModeDel(Target, x[0]))
260 strlcat(the_modes, x, sizeof(the_modes));
265 /* Are there changed modes? */
267 /* Remoce needless action modifier characters */
268 len = strlen(the_modes) - 1;
269 if (the_modes[len] == '+' || the_modes[len] == '-')
270 the_modes[len] = '\0';
272 if (Client_Type(Client) == CLIENT_SERVER) {
273 /* Forward modes to other servers */
274 if (Client_Conn(Target) != NONE) {
275 /* Remote server (service?) changed modes
276 * for one of our clients. Inform it! */
277 IRC_WriteStrClientPrefix(Target, Origin,
282 IRC_WriteStrServersPrefix(Client, Origin,
287 /* Send reply to client and inform other servers */
288 ok = IRC_WriteStrClientPrefix(Client, Origin,
292 IRC_WriteStrServersPrefix(Client, Origin,
297 LogDebug("%s \"%s\": Mode change, now \"%s\".",
298 Client_TypeText(Target), Client_Mask(Target),
299 Client_Modes(Target));
302 IRC_SetPenalty(Client, 1);
308 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
310 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
311 const char *mode_ptr;
313 /* Member or not? -- That's the question! */
314 if (!Channel_IsMemberOf(Channel, Origin))
315 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
316 Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
318 /* The sender is a member: generate extended reply */
319 strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
320 mode_ptr = the_modes;
326 snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
327 strlcat(the_args, argadd, sizeof(the_args));
330 strlcat(the_args, " ", sizeof(the_args));
331 strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
337 strlcat(the_modes, the_args, sizeof(the_modes));
339 if (!IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
340 Client_ID(Origin), Channel_Name(Channel),
344 if (!IRC_WriteStrClient(Origin, RPL_CREATIONTIME_MSG,
345 Client_ID(Origin), Channel_Name(Channel),
346 Channel_CreationTime(Channel)))
354 * Handle channel mode and channel-user mode changes
357 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
359 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
360 argadd[CLIENT_PASS_LEN], *mode_ptr;
361 bool connected, set, skiponce, retval, onchannel, modeok, use_servermode;
362 int mode_arg, arg_arg;
367 if (Channel_IsModeless(Channel))
368 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
369 Client_ID(Client), Channel_Name(Channel));
371 /* Mode request: let's answer it :-) */
373 return Channel_Mode_Answer_Request(Origin, Channel);
375 Channel_CheckAdminRights(Channel, Client, Origin,
376 &onchannel, &modeok, &use_servermode);
378 if (!onchannel && !modeok)
379 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
381 Channel_Name(Channel));
384 mode_ptr = Req->argv[mode_arg];
385 if (Req->argc > mode_arg + 1)
386 arg_arg = mode_arg + 1;
390 /* Initial state: set or unset modes? */
404 /* Prepare reply string */
405 strcpy(the_modes, set ? "+" : "-");
409 connected = CONNECTED;
414 /* Try next argument if there's any */
415 if (arg_arg > mode_arg)
420 if (mode_arg >= Req->argc)
422 mode_ptr = Req->argv[mode_arg];
424 if (Req->argc > mode_arg + 1)
425 arg_arg = mode_arg + 1;
434 if (((*mode_ptr == '+') && !set)
435 || ((*mode_ptr == '-') && set)) {
436 /* Action modifier ("+"/"-") must be changed ... */
437 len = strlen(the_modes) - 1;
438 if (the_modes[len] == '+' || the_modes[len] == '-') {
439 /* Adjust last action modifier in result */
440 the_modes[len] = *mode_ptr;
442 /* Append modifier character to result string */
444 strlcat(the_modes, x, sizeof(the_modes));
446 set = *mode_ptr == '+';
451 /* Are there arguments left? */
452 if (arg_arg >= Req->argc)
460 /* --- Channel modes --- */
461 case 'i': /* Invite only */
462 case 'm': /* Moderated */
463 case 'n': /* Only members can write */
464 case 's': /* Secret channel */
465 case 't': /* Topic locked */
466 case 'z': /* Secure connections only */
470 connected = IRC_WriteStrClient(Origin,
471 ERR_CHANOPRIVSNEEDED_MSG,
472 Client_ID(Origin), Channel_Name(Channel));
474 case 'k': /* Channel key */
479 connected = IRC_WriteStrClient(Origin,
480 ERR_CHANOPRIVSNEEDED_MSG,
482 Channel_Name(Channel));
485 if (arg_arg > mode_arg) {
487 Channel_ModeDel(Channel, 'k');
488 Channel_SetKey(Channel,
490 strlcpy(argadd, Channel_Key(Channel),
494 connected = IRC_WriteStrClient(Origin,
495 ERR_CHANOPRIVSNEEDED_MSG,
497 Channel_Name(Channel));
499 Req->argv[arg_arg][0] = '\0';
502 connected = IRC_WriteStrClient(Origin,
503 ERR_NEEDMOREPARAMS_MSG,
504 Client_ID(Origin), Req->command);
508 case 'l': /* Member limit */
513 connected = IRC_WriteStrClient(Origin,
514 ERR_CHANOPRIVSNEEDED_MSG,
516 Channel_Name(Channel));
519 if (arg_arg > mode_arg) {
521 l = atol(Req->argv[arg_arg]);
522 if (l > 0 && l < 0xFFFF) {
523 Channel_ModeDel(Channel, 'l');
524 Channel_SetMaxUsers(Channel, l);
525 snprintf(argadd, sizeof(argadd),
530 connected = IRC_WriteStrClient(Origin,
531 ERR_CHANOPRIVSNEEDED_MSG,
533 Channel_Name(Channel));
535 Req->argv[arg_arg][0] = '\0';
538 connected = IRC_WriteStrClient(Origin,
539 ERR_NEEDMOREPARAMS_MSG,
540 Client_ID(Origin), Req->command);
544 case 'O': /* IRC operators only */
546 /* Only IRC operators are allowed to
547 * set the 'O' channel mode! */
548 if (set && !(Client_OperByMe(Client)
549 || Client_Type(Client) == CLIENT_SERVER))
550 connected = IRC_WriteStrClient(Origin,
551 ERR_NOPRIVILEGES_MSG,
556 connected = IRC_WriteStrClient(Origin,
557 ERR_CHANOPRIVSNEEDED_MSG,
559 Channel_Name(Channel));
561 case 'P': /* Persistent channel */
563 /* Only IRC operators are allowed to
564 * set the 'P' channel mode! */
565 if (set && !(Client_OperByMe(Client)
566 || Client_Type(Client) == CLIENT_SERVER))
567 connected = IRC_WriteStrClient(Origin,
568 ERR_NOPRIVILEGES_MSG,
573 connected = IRC_WriteStrClient(Origin,
574 ERR_CHANOPRIVSNEEDED_MSG,
576 Channel_Name(Channel));
578 /* --- Channel user modes --- */
579 case 'o': /* Channel operator */
580 case 'v': /* Voice */
581 if (arg_arg > mode_arg) {
583 client = Client_Search(Req->argv[arg_arg]);
587 connected = IRC_WriteStrClient(Client,
592 connected = IRC_WriteStrClient(Origin,
593 ERR_CHANOPRIVSNEEDED_MSG,
595 Channel_Name(Channel));
597 Req->argv[arg_arg][0] = '\0';
600 connected = IRC_WriteStrClient(Origin,
601 ERR_NEEDMOREPARAMS_MSG,
602 Client_ID(Origin), Req->command);
606 /* --- Channel lists --- */
607 case 'I': /* Invite lists */
608 case 'b': /* Ban lists */
609 if (arg_arg > mode_arg) {
613 ? Add_Ban_Invite(*mode_ptr, Origin,
616 : Del_Ban_Invite(*mode_ptr, Origin,
620 connected = IRC_WriteStrClient(Origin,
621 ERR_CHANOPRIVSNEEDED_MSG,
623 Channel_Name(Channel));
625 Req->argv[arg_arg][0] = '\0';
628 if (*mode_ptr == 'I')
629 Channel_ShowInvites(Origin, Channel);
631 Channel_ShowBans(Origin, Channel);
636 "Unknown mode \"%c%c\" from \"%s\" on %s!?",
637 set ? '+' : '-', *mode_ptr, Client_ID(Origin),
638 Channel_Name(Channel));
639 if (Client_Type(Client) != CLIENT_SERVER)
640 connected = IRC_WriteStrClient(Origin,
641 ERR_UMODEUNKNOWNFLAG2_MSG,
643 set ? '+' : '-', *mode_ptr);
651 /* Is there a valid mode change? */
655 /* Validate target client */
656 if (client && (!Channel_IsMemberOf(Channel, client))) {
657 if (!IRC_WriteStrClient
658 (Origin, ERR_USERNOTINCHANNEL_MSG,
659 Client_ID(Origin), Client_ID(client),
660 Channel_Name(Channel)))
667 /* Channel-User-Mode */
669 ? Channel_UserModeAdd(Channel, client, x[0])
670 : Channel_UserModeDel(Channel, client, x[0]);
672 strlcat(the_args, " ", sizeof(the_args));
673 strlcat(the_args, Client_ID(client),
675 strlcat(the_modes, x, sizeof(the_modes));
677 ("User \"%s\": Mode change on %s, now \"%s\"",
678 Client_Mask(client), Channel_Name(Channel),
679 Channel_UserModes(Channel, client));
684 ? Channel_ModeAdd(Channel, x[0])
685 : Channel_ModeDel(Channel, x[0]);
687 strlcat(the_modes, x, sizeof(the_modes));
688 LogDebug("Channel %s: Mode change, now \"%s\".",
689 Channel_Name(Channel),
690 Channel_Modes(Channel));
694 /* Are there additional arguments to add? */
696 strlcat(the_args, " ", sizeof(the_args));
697 strlcat(the_args, argadd, sizeof(the_args));
702 /* Are there changed modes? */
704 /* Clean up mode string */
705 len = strlen(the_modes) - 1;
706 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
707 the_modes[len] = '\0';
709 if (Client_Type(Client) == CLIENT_SERVER) {
710 /* MODE requests for local channels from other servers
711 * are definitely invalid! */
712 if (Channel_IsLocal(Channel)) {
713 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
717 /* Forward mode changes to channel users and all the
718 * other remote servers: */
719 IRC_WriteStrServersPrefix(Client, Origin,
720 "MODE %s %s%s", Channel_Name(Channel),
721 the_modes, the_args);
722 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
723 false, "MODE %s %s%s", Channel_Name(Channel),
724 the_modes, the_args);
727 Origin = Client_ThisServer();
728 /* Send reply to client and inform other servers and channel users */
729 connected = IRC_WriteStrClientPrefix(Client, Origin,
730 "MODE %s %s%s", Channel_Name(Channel),
731 the_modes, the_args);
732 /* Only forward requests for non-local channels */
733 if (!Channel_IsLocal(Channel))
734 IRC_WriteStrServersPrefix(Client, Origin,
735 "MODE %s %s%s", Channel_Name(Channel),
736 the_modes, the_args);
737 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
738 false, "MODE %s %s%s", Channel_Name(Channel),
739 the_modes, the_args);
743 IRC_SetPenalty(Client, 1);
749 IRC_AWAY( CLIENT *Client, REQUEST *Req )
751 assert( Client != NULL );
752 assert( Req != NULL );
754 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
756 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
758 Client_SetAway( Client, Req->argv[0] );
759 Client_ModeAdd( Client, 'a' );
760 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
761 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
765 Client_ModeDel( Client, 'a' );
766 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
767 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
773 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
779 assert( Client != NULL );
780 assert( Channel != NULL );
781 assert( Pattern != NULL );
782 assert(what == 'I' || what == 'b');
784 mask = Lists_MakeMask(Pattern);
786 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
789 ret = Channel_AddInvite(Channel, mask, false);
791 ret = Channel_AddBan(Channel, mask);
795 if (already && (Client_Type(Prefix) == CLIENT_SERVER))
799 return Send_ListChange("+I", Prefix, Client, Channel, mask);
800 return Send_ListChange("+b", Prefix, Client, Channel, mask);
805 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
808 struct list_head *list;
810 assert( Client != NULL );
811 assert( Channel != NULL );
812 assert( Pattern != NULL );
813 assert(what == 'I' || what == 'b');
815 mask = Lists_MakeMask( Pattern );
818 list = Channel_GetListInvites(Channel);
820 list = Channel_GetListBans(Channel);
822 Lists_Del(list, mask);
824 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
825 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
830 Send_ListChange(const char *Mode, CLIENT *Prefix, CLIENT *Client,
831 CHANNEL *Channel, const char *Mask)
835 if( Client_Type( Client ) == CLIENT_USER )
837 /* send confirmation to client */
838 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
842 /* to other servers */
843 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
845 /* and local users in channel */
846 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
849 } /* Send_ListChange */