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 IRC_WriteStrServersPrefix(Client, Origin,
279 /* Send reply to client and inform other servers */
280 ok = IRC_WriteStrClientPrefix(Client, Origin,
284 IRC_WriteStrServersPrefix(Client, Origin,
289 LogDebug("%s \"%s\": Mode change, now \"%s\".",
290 Client_TypeText(Target), Client_Mask(Target),
291 Client_Modes(Target));
294 IRC_SetPenalty(Client, 1);
300 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
302 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
303 const char *mode_ptr;
305 /* Member or not? -- That's the question! */
306 if (!Channel_IsMemberOf(Channel, Origin))
307 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
308 Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
310 /* The sender is a member: generate extended reply */
311 strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
312 mode_ptr = the_modes;
318 snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
319 strlcat(the_args, argadd, sizeof(the_args));
322 strlcat(the_args, " ", sizeof(the_args));
323 strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
329 strlcat(the_modes, the_args, sizeof(the_modes));
331 if (!IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
332 Client_ID(Origin), Channel_Name(Channel),
336 if (!IRC_WriteStrClient(Origin, RPL_CREATIONTIME_MSG,
337 Client_ID(Origin), Channel_Name(Channel),
338 Channel_CreationTime(Channel)))
346 * Handle channel mode and channel-user mode changes
349 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
351 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
352 argadd[CLIENT_PASS_LEN], *mode_ptr;
353 bool connected, set, skiponce, retval, onchannel, modeok, use_servermode;
354 int mode_arg, arg_arg;
359 if (Channel_IsModeless(Channel))
360 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
361 Client_ID(Client), Channel_Name(Channel));
363 /* Mode request: let's answer it :-) */
365 return Channel_Mode_Answer_Request(Origin, Channel);
367 Channel_CheckAdminRights(Channel, Client, Origin,
368 &onchannel, &modeok, &use_servermode);
370 if (!onchannel && !modeok)
371 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
373 Channel_Name(Channel));
376 mode_ptr = Req->argv[mode_arg];
377 if (Req->argc > mode_arg + 1)
378 arg_arg = mode_arg + 1;
382 /* Initial state: set or unset modes? */
396 /* Prepare reply string */
397 strcpy(the_modes, set ? "+" : "-");
401 connected = CONNECTED;
406 /* Try next argument if there's any */
407 if (arg_arg > mode_arg)
412 if (mode_arg >= Req->argc)
414 mode_ptr = Req->argv[mode_arg];
416 if (Req->argc > mode_arg + 1)
417 arg_arg = mode_arg + 1;
426 if (((*mode_ptr == '+') && !set)
427 || ((*mode_ptr == '-') && set)) {
428 /* Action modifier ("+"/"-") must be changed ... */
429 len = strlen(the_modes) - 1;
430 if (the_modes[len] == '+' || the_modes[len] == '-') {
431 /* Adjust last action modifier in result */
432 the_modes[len] = *mode_ptr;
434 /* Append modifier character to result string */
436 strlcat(the_modes, x, sizeof(the_modes));
438 set = *mode_ptr == '+';
443 /* Are there arguments left? */
444 if (arg_arg >= Req->argc)
452 /* --- Channel modes --- */
453 case 'i': /* Invite only */
454 case 'm': /* Moderated */
455 case 'n': /* Only members can write */
456 case 's': /* Secret channel */
457 case 't': /* Topic locked */
458 case 'z': /* Secure connections only */
462 connected = IRC_WriteStrClient(Origin,
463 ERR_CHANOPRIVSNEEDED_MSG,
464 Client_ID(Origin), Channel_Name(Channel));
466 case 'k': /* Channel key */
471 connected = IRC_WriteStrClient(Origin,
472 ERR_CHANOPRIVSNEEDED_MSG,
474 Channel_Name(Channel));
477 if (arg_arg > mode_arg) {
479 Channel_ModeDel(Channel, 'k');
480 Channel_SetKey(Channel,
482 strlcpy(argadd, Channel_Key(Channel),
486 connected = IRC_WriteStrClient(Origin,
487 ERR_CHANOPRIVSNEEDED_MSG,
489 Channel_Name(Channel));
491 Req->argv[arg_arg][0] = '\0';
494 connected = IRC_WriteStrClient(Origin,
495 ERR_NEEDMOREPARAMS_MSG,
496 Client_ID(Origin), Req->command);
500 case 'l': /* Member limit */
505 connected = IRC_WriteStrClient(Origin,
506 ERR_CHANOPRIVSNEEDED_MSG,
508 Channel_Name(Channel));
511 if (arg_arg > mode_arg) {
513 l = atol(Req->argv[arg_arg]);
514 if (l > 0 && l < 0xFFFF) {
515 Channel_ModeDel(Channel, 'l');
516 Channel_SetMaxUsers(Channel, l);
517 snprintf(argadd, sizeof(argadd),
522 connected = IRC_WriteStrClient(Origin,
523 ERR_CHANOPRIVSNEEDED_MSG,
525 Channel_Name(Channel));
527 Req->argv[arg_arg][0] = '\0';
530 connected = IRC_WriteStrClient(Origin,
531 ERR_NEEDMOREPARAMS_MSG,
532 Client_ID(Origin), Req->command);
536 case 'O': /* IRC operators only */
538 /* Only IRC operators are allowed to
539 * set the 'O' channel mode! */
540 if (set && !(Client_OperByMe(Client)
541 || Client_Type(Client) == CLIENT_SERVER))
542 connected = IRC_WriteStrClient(Origin,
543 ERR_NOPRIVILEGES_MSG,
548 connected = IRC_WriteStrClient(Origin,
549 ERR_CHANOPRIVSNEEDED_MSG,
551 Channel_Name(Channel));
553 case 'P': /* Persistent channel */
555 /* Only IRC operators are allowed to
556 * set the 'P' 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 /* --- Channel user modes --- */
571 case 'o': /* Channel operator */
572 case 'v': /* Voice */
573 if (arg_arg > mode_arg) {
575 client = Client_Search(Req->argv[arg_arg]);
579 connected = IRC_WriteStrClient(Client,
584 connected = IRC_WriteStrClient(Origin,
585 ERR_CHANOPRIVSNEEDED_MSG,
587 Channel_Name(Channel));
589 Req->argv[arg_arg][0] = '\0';
592 connected = IRC_WriteStrClient(Origin,
593 ERR_NEEDMOREPARAMS_MSG,
594 Client_ID(Origin), Req->command);
598 /* --- Channel lists --- */
599 case 'I': /* Invite lists */
600 case 'b': /* Ban lists */
601 if (arg_arg > mode_arg) {
605 ? Add_Ban_Invite(*mode_ptr, Origin,
608 : Del_Ban_Invite(*mode_ptr, Origin,
612 connected = IRC_WriteStrClient(Origin,
613 ERR_CHANOPRIVSNEEDED_MSG,
615 Channel_Name(Channel));
617 Req->argv[arg_arg][0] = '\0';
620 if (*mode_ptr == 'I')
621 Channel_ShowInvites(Origin, Channel);
623 Channel_ShowBans(Origin, Channel);
628 "Unknown mode \"%c%c\" from \"%s\" on %s!?",
629 set ? '+' : '-', *mode_ptr, Client_ID(Origin),
630 Channel_Name(Channel));
631 if (Client_Type(Client) != CLIENT_SERVER)
632 connected = IRC_WriteStrClient(Origin,
633 ERR_UMODEUNKNOWNFLAG2_MSG,
635 set ? '+' : '-', *mode_ptr);
643 /* Is there a valid mode change? */
647 /* Validate target client */
648 if (client && (!Channel_IsMemberOf(Channel, client))) {
649 if (!IRC_WriteStrClient
650 (Origin, ERR_USERNOTINCHANNEL_MSG,
651 Client_ID(Origin), Client_ID(client),
652 Channel_Name(Channel)))
659 /* Channel-User-Mode */
661 ? Channel_UserModeAdd(Channel, client, x[0])
662 : Channel_UserModeDel(Channel, client, x[0]);
664 strlcat(the_args, " ", sizeof(the_args));
665 strlcat(the_args, Client_ID(client),
667 strlcat(the_modes, x, sizeof(the_modes));
669 ("User \"%s\": Mode change on %s, now \"%s\"",
670 Client_Mask(client), Channel_Name(Channel),
671 Channel_UserModes(Channel, client));
676 ? Channel_ModeAdd(Channel, x[0])
677 : Channel_ModeDel(Channel, x[0]);
679 strlcat(the_modes, x, sizeof(the_modes));
680 LogDebug("Channel %s: Mode change, now \"%s\".",
681 Channel_Name(Channel),
682 Channel_Modes(Channel));
686 /* Are there additional arguments to add? */
688 strlcat(the_args, " ", sizeof(the_args));
689 strlcat(the_args, argadd, sizeof(the_args));
694 /* Are there changed modes? */
696 /* Clean up mode string */
697 len = strlen(the_modes) - 1;
698 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
699 the_modes[len] = '\0';
701 if (Client_Type(Client) == CLIENT_SERVER) {
702 /* MODE requests for local channels from other servers
703 * are definitely invalid! */
704 if (Channel_IsLocal(Channel)) {
705 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
709 /* Forward mode changes to channel users and all the
710 * other remote servers: */
711 IRC_WriteStrServersPrefix(Client, Origin,
712 "MODE %s %s%s", Channel_Name(Channel),
713 the_modes, the_args);
714 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
715 false, "MODE %s %s%s", Channel_Name(Channel),
716 the_modes, the_args);
719 Origin = Client_ThisServer();
720 /* Send reply to client and inform other servers and channel users */
721 connected = IRC_WriteStrClientPrefix(Client, Origin,
722 "MODE %s %s%s", Channel_Name(Channel),
723 the_modes, the_args);
724 /* Only forward requests for non-local channels */
725 if (!Channel_IsLocal(Channel))
726 IRC_WriteStrServersPrefix(Client, Origin,
727 "MODE %s %s%s", Channel_Name(Channel),
728 the_modes, the_args);
729 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
730 false, "MODE %s %s%s", Channel_Name(Channel),
731 the_modes, the_args);
735 IRC_SetPenalty(Client, 1);
741 IRC_AWAY( CLIENT *Client, REQUEST *Req )
743 assert( Client != NULL );
744 assert( Req != NULL );
746 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
748 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
750 Client_SetAway( Client, Req->argv[0] );
751 Client_ModeAdd( Client, 'a' );
752 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
753 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
757 Client_ModeDel( Client, 'a' );
758 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
759 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
765 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
771 assert( Client != NULL );
772 assert( Channel != NULL );
773 assert( Pattern != NULL );
774 assert(what == 'I' || what == 'b');
776 mask = Lists_MakeMask(Pattern);
778 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
781 ret = Channel_AddInvite(Channel, mask, false);
783 ret = Channel_AddBan(Channel, mask);
787 if (already && (Client_Type(Prefix) == CLIENT_SERVER))
791 return Send_ListChange("+I", Prefix, Client, Channel, mask);
792 return Send_ListChange("+b", Prefix, Client, Channel, mask);
797 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
800 struct list_head *list;
802 assert( Client != NULL );
803 assert( Channel != NULL );
804 assert( Pattern != NULL );
805 assert(what == 'I' || what == 'b');
807 mask = Lists_MakeMask( Pattern );
810 list = Channel_GetListInvites(Channel);
812 list = Channel_GetListBans(Channel);
814 Lists_Del(list, mask);
816 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
817 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
822 Send_ListChange(const char *Mode, CLIENT *Prefix, CLIENT *Client,
823 CHANNEL *Channel, const char *Mask)
827 if( Client_Type( Client ) == CLIENT_USER )
829 /* send confirmation to client */
830 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
834 /* to other servers */
835 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
837 /* and local users in channel */
838 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
841 } /* Send_ListChange */