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 --- */
600 if (Client_Type(Client) != CLIENT_SERVER) {
601 connected = IRC_WriteStrClient(Origin,
602 ERR_CHANOPRIVSNEEDED_MSG,
604 Channel_Name(Channel));
607 case 'o': /* Channel operator */
608 case 'v': /* Voice */
609 if (arg_arg > mode_arg) {
611 client = Client_Search(Req->argv[arg_arg]);
615 connected = IRC_WriteStrClient(Client,
620 connected = IRC_WriteStrClient(Origin,
621 ERR_CHANOPRIVSNEEDED_MSG,
623 Channel_Name(Channel));
625 Req->argv[arg_arg][0] = '\0';
628 connected = IRC_WriteStrClient(Origin,
629 ERR_NEEDMOREPARAMS_MSG,
630 Client_ID(Origin), Req->command);
634 /* --- Channel lists --- */
635 case 'I': /* Invite lists */
636 case 'b': /* Ban lists */
637 if (arg_arg > mode_arg) {
641 ? Add_Ban_Invite(*mode_ptr, Origin,
644 : Del_Ban_Invite(*mode_ptr, Origin,
648 connected = IRC_WriteStrClient(Origin,
649 ERR_CHANOPRIVSNEEDED_MSG,
651 Channel_Name(Channel));
653 Req->argv[arg_arg][0] = '\0';
656 if (*mode_ptr == 'I')
657 Channel_ShowInvites(Origin, Channel);
659 Channel_ShowBans(Origin, Channel);
663 if (Client_Type(Client) != CLIENT_SERVER) {
665 "Unknown mode \"%c%c\" from \"%s\" on %s!?",
666 set ? '+' : '-', *mode_ptr,
667 Client_ID(Origin), Channel_Name(Channel));
668 connected = IRC_WriteStrClient(Origin,
669 ERR_UMODEUNKNOWNFLAG2_MSG,
671 set ? '+' : '-', *mode_ptr);
676 "Handling unknown mode \"%c%c\" from \"%s\" on %s ...",
677 set ? '+' : '-', *mode_ptr,
678 Client_ID(Origin), Channel_Name(Channel));
686 /* Is there a valid mode change? */
690 /* Validate target client */
691 if (client && (!Channel_IsMemberOf(Channel, client))) {
692 if (!IRC_WriteStrClient
693 (Origin, ERR_USERNOTINCHANNEL_MSG,
694 Client_ID(Origin), Client_ID(client),
695 Channel_Name(Channel)))
702 /* Channel-User-Mode */
704 ? Channel_UserModeAdd(Channel, client, x[0])
705 : Channel_UserModeDel(Channel, client, x[0]);
707 strlcat(the_args, " ", sizeof(the_args));
708 strlcat(the_args, Client_ID(client),
710 strlcat(the_modes, x, sizeof(the_modes));
712 ("User \"%s\": Mode change on %s, now \"%s\"",
713 Client_Mask(client), Channel_Name(Channel),
714 Channel_UserModes(Channel, client));
719 ? Channel_ModeAdd(Channel, x[0])
720 : Channel_ModeDel(Channel, x[0]);
722 strlcat(the_modes, x, sizeof(the_modes));
723 LogDebug("Channel %s: Mode change, now \"%s\".",
724 Channel_Name(Channel),
725 Channel_Modes(Channel));
729 /* Are there additional arguments to add? */
731 strlcat(the_args, " ", sizeof(the_args));
732 strlcat(the_args, argadd, sizeof(the_args));
737 /* Are there changed modes? */
739 /* Clean up mode string */
740 len = strlen(the_modes) - 1;
741 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
742 the_modes[len] = '\0';
744 if (Client_Type(Client) == CLIENT_SERVER) {
745 /* MODE requests for local channels from other servers
746 * are definitely invalid! */
747 if (Channel_IsLocal(Channel)) {
748 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
752 /* Forward mode changes to channel users and all the
753 * other remote servers: */
754 IRC_WriteStrServersPrefix(Client, Origin,
755 "MODE %s %s%s", Channel_Name(Channel),
756 the_modes, the_args);
757 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
758 false, "MODE %s %s%s", Channel_Name(Channel),
759 the_modes, the_args);
762 Origin = Client_ThisServer();
763 /* Send reply to client and inform other servers and channel users */
764 connected = IRC_WriteStrClientPrefix(Client, Origin,
765 "MODE %s %s%s", Channel_Name(Channel),
766 the_modes, the_args);
767 /* Only forward requests for non-local channels */
768 if (!Channel_IsLocal(Channel))
769 IRC_WriteStrServersPrefix(Client, Origin,
770 "MODE %s %s%s", Channel_Name(Channel),
771 the_modes, the_args);
772 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
773 false, "MODE %s %s%s", Channel_Name(Channel),
774 the_modes, the_args);
778 IRC_SetPenalty(Client, 1);
784 IRC_AWAY( CLIENT *Client, REQUEST *Req )
786 assert( Client != NULL );
787 assert( Req != NULL );
789 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
791 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
793 Client_SetAway( Client, Req->argv[0] );
794 Client_ModeAdd( Client, 'a' );
795 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
796 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
800 Client_ModeDel( Client, 'a' );
801 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
802 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
808 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
814 assert( Client != NULL );
815 assert( Channel != NULL );
816 assert( Pattern != NULL );
817 assert(what == 'I' || what == 'b');
819 mask = Lists_MakeMask(Pattern);
821 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
824 ret = Channel_AddInvite(Channel, mask, false);
826 ret = Channel_AddBan(Channel, mask);
830 if (already && (Client_Type(Prefix) == CLIENT_SERVER))
834 return Send_ListChange("+I", Prefix, Client, Channel, mask);
835 return Send_ListChange("+b", Prefix, Client, Channel, mask);
840 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
843 struct list_head *list;
845 assert( Client != NULL );
846 assert( Channel != NULL );
847 assert( Pattern != NULL );
848 assert(what == 'I' || what == 'b');
850 mask = Lists_MakeMask( Pattern );
853 list = Channel_GetListInvites(Channel);
855 list = Channel_GetListBans(Channel);
857 Lists_Del(list, mask);
859 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
860 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
865 Send_ListChange(const char *Mode, CLIENT *Prefix, CLIENT *Client,
866 CHANNEL *Channel, const char *Mask)
870 if( Client_Type( Client ) == CLIENT_USER )
872 /* send confirmation to client */
873 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
877 /* to other servers */
878 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
880 /* and local users in channel */
881 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
884 } /* Send_ListChange */