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((char what, CLIENT *Prefix, CLIENT *Client,
45 CHANNEL *Channel, const char *Pattern));
46 static bool Del_Ban_Invite PARAMS((char what, CLIENT *Prefix, CLIENT *Client,
47 CHANNEL *Channel, const char *Pattern));
49 static bool Send_ListChange PARAMS((const bool IsAdd, const char ModeChar,
50 CLIENT *Prefix, CLIENT *Client,
51 CHANNEL *Channel, const char *Mask));
55 * Handler for the IRC "MODE" command.
57 * See RFC 2812 section 3.1.5 ("user mode message") and section 3.2.3
58 * ("channel mode message"), and RFC 2811 section 4 ("channel modes").
60 * @param Client The client from which this command has been received.
61 * @param Req Request structure with prefix and all parameters.
62 * @returns CONNECTED or DISCONNECTED.
65 IRC_MODE( CLIENT *Client, REQUEST *Req )
70 assert(Client != NULL);
75 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
76 Client_ID(Client), Req->command);
78 /* Origin for answers */
79 if (Client_Type(Client) == CLIENT_SERVER) {
80 origin = Client_Search(Req->prefix);
82 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
88 /* Channel or user mode? */
89 cl = NULL; chan = NULL;
90 if (Client_IsValidNick(Req->argv[0]))
91 cl = Client_Search(Req->argv[0]);
92 if (Channel_IsValidName(Req->argv[0]))
93 chan = Channel_Search(Req->argv[0]);
96 return Client_Mode(Client, Req, origin, cl);
98 return Channel_Mode(Client, Req, origin, chan);
100 /* No target found! */
101 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
102 Client_ID(Client), Req->argv[0]);
107 * Check if the "mode limit" for a client has been reached.
109 * This limit doesn't apply for servers or services!
111 * @param Client The client to check.
112 * @param Count The number of modes already handled.
113 * @return true if the limit has been reached.
116 Mode_Limit_Reached(CLIENT *Client, int Count)
118 if (Client_Type(Client) == CLIENT_SERVER
119 || Client_Type(Client) == CLIENT_SERVICE)
121 if (Count < MAX_HNDL_MODES_ARG)
128 * Handle client mode requests
130 * @param Client The client from which this command has been received.
131 * @param Req Request structure with prefix and all parameters.
132 * @param Origin The originator of the MODE command (prefix).
133 * @param Target The target (client) of this MODE command.
134 * @returns CONNECTED or DISCONNECTED.
137 Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target )
139 char the_modes[COMMAND_LEN], x[2], *mode_ptr;
144 /* Is the client allowed to request or change the modes? */
145 if (Client_Type(Client) == CLIENT_USER) {
146 /* Users are only allowed to manipulate their own modes! */
147 if (Target != Client)
148 return IRC_WriteStrClient(Client,
149 ERR_USERSDONTMATCH_MSG,
153 /* Mode request: let's answer it :-) */
155 return IRC_WriteStrClient(Origin, RPL_UMODEIS_MSG,
157 Client_Modes(Target));
160 mode_ptr = Req->argv[mode_arg];
162 /* Initial state: set or unset modes? */
163 if (*mode_ptr == '+') {
165 strcpy(the_modes, "+");
166 } else if (*mode_ptr == '-') {
168 strcpy(the_modes, "-");
170 return IRC_WriteStrClient(Origin, ERR_UMODEUNKNOWNFLAG_MSG,
178 /* Try next argument if there's any */
180 if (mode_arg < Req->argc)
181 mode_ptr = Req->argv[mode_arg];
189 if ((*mode_ptr == '+' && !set)
190 || (*mode_ptr == '-' && set)) {
191 /* Action modifier ("+"/"-") must be changed */
192 len = strlen(the_modes) - 1;
193 if (the_modes[len] == '+'
194 || the_modes[len] == '-') {
195 /* Last character in the "result
196 * string" was an "action", so just
197 * overwrite it with the new action */
198 the_modes[len] = *mode_ptr;
200 /* Append new modifier character to
201 * the resulting mode string */
203 strlcat(the_modes, x,
206 if (*mode_ptr == '+')
217 case 'i': /* Invisible */
218 case 's': /* Server messages */
219 case 'w': /* Wallops messages */
223 if (Client_Type(Client) == CLIENT_SERVER) {
225 Client_SetAway(Origin, DEFAULT_AWAY_MSG);
227 ok = IRC_WriteStrClient(Origin,
228 ERR_NOPRIVILEGES_MSG,
231 case 'c': /* Receive connect notices
232 * (only settable by IRC operators!) */
233 if (!set || Client_Type(Client) == CLIENT_SERVER
234 || Client_OperByMe(Origin))
237 ok = IRC_WriteStrClient(Origin,
238 ERR_NOPRIVILEGES_MSG,
241 case 'o': /* IRC operator (only unsettable!) */
242 if (!set || Client_Type(Client) == CLIENT_SERVER) {
243 Client_SetOperByMe(Target, false);
246 ok = IRC_WriteStrClient(Origin,
247 ERR_NOPRIVILEGES_MSG,
250 case 'r': /* Restricted (only settable) */
251 if (set || Client_Type(Client) == CLIENT_SERVER)
254 ok = IRC_WriteStrClient(Origin,
258 case 'x': /* Cloak hostname */
259 if (Client_HasMode(Client, 'r'))
260 ok = IRC_WriteStrClient(Origin,
267 if (Client_Type(Client) != CLIENT_SERVER) {
269 "Unknown mode \"%c%c\" from \"%s\"!?",
270 set ? '+' : '-', *mode_ptr,
272 ok = IRC_WriteStrClient(Origin,
273 ERR_UMODEUNKNOWNFLAG2_MSG,
280 "Handling unknown mode \"%c%c\" from \"%s\" for \"%s\" ...",
281 set ? '+' : '-', *mode_ptr,
282 Client_ID(Origin), Client_ID(Target));
290 /* Is there a valid mode change? */
295 if (Client_ModeAdd(Target, x[0]))
296 strlcat(the_modes, x, sizeof(the_modes));
298 if (Client_ModeDel(Target, x[0]))
299 strlcat(the_modes, x, sizeof(the_modes));
303 /* Are there changed modes? */
305 /* Remove needless action modifier characters */
306 len = strlen(the_modes) - 1;
307 if (the_modes[len] == '+' || the_modes[len] == '-')
308 the_modes[len] = '\0';
310 if (Client_Type(Client) == CLIENT_SERVER) {
311 /* Forward modes to other servers */
312 if (Client_Conn(Target) != NONE) {
313 /* Remote server (service?) changed modes
314 * for one of our clients. Inform it! */
315 IRC_WriteStrClientPrefix(Target, Origin,
320 IRC_WriteStrServersPrefix(Client, Origin,
325 /* Send reply to client and inform other servers */
326 ok = IRC_WriteStrClientPrefix(Client, Origin,
330 IRC_WriteStrServersPrefix(Client, Origin,
335 LogDebug("%s \"%s\": Mode change, now \"%s\".",
336 Client_TypeText(Target), Client_Mask(Target),
337 Client_Modes(Target));
340 IRC_SetPenalty(Client, 1);
346 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
348 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
349 const char *mode_ptr;
351 /* Member or not? -- That's the question! */
352 if (!Channel_IsMemberOf(Channel, Origin))
353 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
354 Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
356 /* The sender is a member: generate extended reply */
357 strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
358 mode_ptr = the_modes;
364 snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
365 strlcat(the_args, argadd, sizeof(the_args));
368 strlcat(the_args, " ", sizeof(the_args));
369 strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
375 strlcat(the_modes, the_args, sizeof(the_modes));
377 if (!IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
378 Client_ID(Origin), Channel_Name(Channel),
382 if (!IRC_WriteStrClient(Origin, RPL_CREATIONTIME_MSG,
383 Client_ID(Origin), Channel_Name(Channel),
384 Channel_CreationTime(Channel)))
392 * Handle channel mode and channel-user mode changes
395 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
397 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
398 argadd[CLIENT_PASS_LEN], *mode_ptr;
399 bool connected, set, skiponce, retval, onchannel, modeok, use_servermode;
400 int mode_arg, arg_arg, mode_arg_count = 0;
405 if (Channel_IsModeless(Channel))
406 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
407 Client_ID(Client), Channel_Name(Channel));
409 /* Mode request: let's answer it :-) */
411 return Channel_Mode_Answer_Request(Origin, Channel);
413 Channel_CheckAdminRights(Channel, Client, Origin,
414 &onchannel, &modeok, &use_servermode);
416 if (!onchannel && !modeok)
417 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
419 Channel_Name(Channel));
422 mode_ptr = Req->argv[mode_arg];
423 if (Req->argc > mode_arg + 1)
424 arg_arg = mode_arg + 1;
428 /* Initial state: set or unset modes? */
442 /* Prepare reply string */
443 strcpy(the_modes, set ? "+" : "-");
447 connected = CONNECTED;
452 /* Try next argument if there's any */
455 if (arg_arg > mode_arg)
460 if (mode_arg >= Req->argc)
462 mode_ptr = Req->argv[mode_arg];
464 if (Req->argc > mode_arg + 1)
465 arg_arg = mode_arg + 1;
474 if (((*mode_ptr == '+') && !set)
475 || ((*mode_ptr == '-') && set)) {
476 /* Action modifier ("+"/"-") must be changed ... */
477 len = strlen(the_modes) - 1;
478 if (the_modes[len] == '+' || the_modes[len] == '-') {
479 /* Adjust last action modifier in result */
480 the_modes[len] = *mode_ptr;
482 /* Append modifier character to result string */
484 strlcat(the_modes, x, sizeof(the_modes));
486 set = *mode_ptr == '+';
491 /* Are there arguments left? */
492 if (arg_arg >= Req->argc)
500 /* --- Channel modes --- */
501 case 'i': /* Invite only */
502 case 'm': /* Moderated */
503 case 'n': /* Only members can write */
504 case 'R': /* Registered users only */
505 case 's': /* Secret channel */
506 case 't': /* Topic locked */
507 case 'z': /* Secure connections only */
511 connected = IRC_WriteStrClient(Origin,
512 ERR_CHANOPRIVSNEEDED_MSG,
513 Client_ID(Origin), Channel_Name(Channel));
515 case 'k': /* Channel key */
516 if (Mode_Limit_Reached(Client, mode_arg_count++))
522 connected = IRC_WriteStrClient(Origin,
523 ERR_CHANOPRIVSNEEDED_MSG,
525 Channel_Name(Channel));
528 if (arg_arg > mode_arg) {
530 Channel_ModeDel(Channel, 'k');
531 Channel_SetKey(Channel,
533 strlcpy(argadd, Channel_Key(Channel),
537 connected = IRC_WriteStrClient(Origin,
538 ERR_CHANOPRIVSNEEDED_MSG,
540 Channel_Name(Channel));
542 Req->argv[arg_arg][0] = '\0';
545 connected = IRC_WriteStrClient(Origin,
546 ERR_NEEDMOREPARAMS_MSG,
547 Client_ID(Origin), Req->command);
551 case 'l': /* Member limit */
552 if (Mode_Limit_Reached(Client, mode_arg_count++))
558 connected = IRC_WriteStrClient(Origin,
559 ERR_CHANOPRIVSNEEDED_MSG,
561 Channel_Name(Channel));
564 if (arg_arg > mode_arg) {
566 l = atol(Req->argv[arg_arg]);
567 if (l > 0 && l < 0xFFFF) {
568 Channel_ModeDel(Channel, 'l');
569 Channel_SetMaxUsers(Channel, l);
570 snprintf(argadd, sizeof(argadd),
575 connected = IRC_WriteStrClient(Origin,
576 ERR_CHANOPRIVSNEEDED_MSG,
578 Channel_Name(Channel));
580 Req->argv[arg_arg][0] = '\0';
583 connected = IRC_WriteStrClient(Origin,
584 ERR_NEEDMOREPARAMS_MSG,
585 Client_ID(Origin), Req->command);
589 case 'O': /* IRC operators only */
591 /* Only IRC operators are allowed to
592 * set the 'O' channel mode! */
593 if (set && !(Client_OperByMe(Client)
594 || Client_Type(Client) == CLIENT_SERVER))
595 connected = IRC_WriteStrClient(Origin,
596 ERR_NOPRIVILEGES_MSG,
601 connected = IRC_WriteStrClient(Origin,
602 ERR_CHANOPRIVSNEEDED_MSG,
604 Channel_Name(Channel));
606 case 'P': /* Persistent channel */
608 /* Only IRC operators are allowed to
609 * set the 'P' channel mode! */
610 if (set && !(Client_OperByMe(Client)
611 || Client_Type(Client) == CLIENT_SERVER))
612 connected = IRC_WriteStrClient(Origin,
613 ERR_NOPRIVILEGES_MSG,
618 connected = IRC_WriteStrClient(Origin,
619 ERR_CHANOPRIVSNEEDED_MSG,
621 Channel_Name(Channel));
623 /* --- Channel user modes --- */
627 if (Client_Type(Client) != CLIENT_SERVER) {
628 connected = IRC_WriteStrClient(Origin,
629 ERR_CHANOPRIVSNEEDED_MSG,
631 Channel_Name(Channel));
634 case 'o': /* Channel operator */
635 case 'v': /* Voice */
636 if (arg_arg > mode_arg) {
638 client = Client_Search(Req->argv[arg_arg]);
642 connected = IRC_WriteStrClient(Client,
647 connected = IRC_WriteStrClient(Origin,
648 ERR_CHANOPRIVSNEEDED_MSG,
650 Channel_Name(Channel));
652 Req->argv[arg_arg][0] = '\0';
655 connected = IRC_WriteStrClient(Origin,
656 ERR_NEEDMOREPARAMS_MSG,
657 Client_ID(Origin), Req->command);
661 /* --- Channel lists --- */
662 case 'I': /* Invite lists */
663 case 'b': /* Ban lists */
664 if (Mode_Limit_Reached(Client, mode_arg_count++))
666 if (arg_arg > mode_arg) {
670 ? Add_Ban_Invite(*mode_ptr, Origin,
673 : Del_Ban_Invite(*mode_ptr, Origin,
677 connected = IRC_WriteStrClient(Origin,
678 ERR_CHANOPRIVSNEEDED_MSG,
680 Channel_Name(Channel));
682 Req->argv[arg_arg][0] = '\0';
685 if (*mode_ptr == 'I')
686 Channel_ShowInvites(Origin, Channel);
688 Channel_ShowBans(Origin, Channel);
692 if (Client_Type(Client) != CLIENT_SERVER) {
694 "Unknown mode \"%c%c\" from \"%s\" on %s!?",
695 set ? '+' : '-', *mode_ptr,
696 Client_ID(Origin), Channel_Name(Channel));
697 connected = IRC_WriteStrClient(Origin,
699 Client_ID(Origin), *mode_ptr,
700 Channel_Name(Channel));
704 "Handling unknown mode \"%c%c\" from \"%s\" on %s ...",
705 set ? '+' : '-', *mode_ptr,
706 Client_ID(Origin), Channel_Name(Channel));
714 /* Is there a valid mode change? */
718 /* Validate target client */
719 if (client && (!Channel_IsMemberOf(Channel, client))) {
720 if (!IRC_WriteStrClient
721 (Origin, ERR_USERNOTINCHANNEL_MSG,
722 Client_ID(Origin), Client_ID(client),
723 Channel_Name(Channel)))
730 /* Channel-User-Mode */
732 ? Channel_UserModeAdd(Channel, client, x[0])
733 : Channel_UserModeDel(Channel, client, x[0]);
735 strlcat(the_args, " ", sizeof(the_args));
736 strlcat(the_args, Client_ID(client),
738 strlcat(the_modes, x, sizeof(the_modes));
740 ("User \"%s\": Mode change on %s, now \"%s\"",
741 Client_Mask(client), Channel_Name(Channel),
742 Channel_UserModes(Channel, client));
747 ? Channel_ModeAdd(Channel, x[0])
748 : Channel_ModeDel(Channel, x[0]);
750 strlcat(the_modes, x, sizeof(the_modes));
751 LogDebug("Channel %s: Mode change, now \"%s\".",
752 Channel_Name(Channel),
753 Channel_Modes(Channel));
757 /* Are there additional arguments to add? */
759 strlcat(the_args, " ", sizeof(the_args));
760 strlcat(the_args, argadd, sizeof(the_args));
765 /* Are there changed modes? */
767 /* Clean up mode string */
768 len = strlen(the_modes) - 1;
769 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
770 the_modes[len] = '\0';
772 if (Client_Type(Client) == CLIENT_SERVER) {
773 /* MODE requests for local channels from other servers
774 * are definitely invalid! */
775 if (Channel_IsLocal(Channel)) {
776 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
780 /* Forward mode changes to channel users and all the
781 * other remote servers: */
782 IRC_WriteStrServersPrefix(Client, Origin,
783 "MODE %s %s%s", Channel_Name(Channel),
784 the_modes, the_args);
785 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
786 false, "MODE %s %s%s", Channel_Name(Channel),
787 the_modes, the_args);
790 Origin = Client_ThisServer();
791 /* Send reply to client and inform other servers and channel users */
792 connected = IRC_WriteStrClientPrefix(Client, Origin,
793 "MODE %s %s%s", Channel_Name(Channel),
794 the_modes, the_args);
795 /* Only forward requests for non-local channels */
796 if (!Channel_IsLocal(Channel))
797 IRC_WriteStrServersPrefix(Client, Origin,
798 "MODE %s %s%s", Channel_Name(Channel),
799 the_modes, the_args);
800 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
801 false, "MODE %s %s%s", Channel_Name(Channel),
802 the_modes, the_args);
806 IRC_SetPenalty(Client, 1);
812 IRC_AWAY( CLIENT *Client, REQUEST *Req )
814 assert( Client != NULL );
815 assert( Req != NULL );
817 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
819 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
821 Client_SetAway( Client, Req->argv[0] );
822 Client_ModeAdd( Client, 'a' );
823 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
824 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
828 Client_ModeDel( Client, 'a' );
829 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
830 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
836 * Add entries to channel ban and invite lists.
838 * @param what Can be 'I' for invite or 'b' for ban list.
839 * @param Prefix The originator of the command.
840 * @param Client The sender of the command.
841 * @param Channel The channel of which the list should be modified.
842 * @param Pattern The pattern to add to the list.
843 * @return CONNECTED or DISCONNECTED.
846 Add_Ban_Invite(char what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel,
850 struct list_head *list;
852 assert(Client != NULL);
853 assert(Channel != NULL);
854 assert(Pattern != NULL);
855 assert(what == 'I' || what == 'b');
857 mask = Lists_MakeMask(Pattern);
860 list = Channel_GetListInvites(Channel);
862 list = Channel_GetListBans(Channel);
864 if (Lists_CheckDupeMask(list, mask))
866 if (Client_Type(Client) == CLIENT_USER &&
867 Lists_Count(list) >= MAX_HNDL_CHANNEL_LISTS)
868 return IRC_WriteStrClient(Client, ERR_LISTFULL_MSG,
870 Channel_Name(Channel), mask,
871 MAX_HNDL_CHANNEL_LISTS);
874 if (!Channel_AddInvite(Channel, mask, false))
877 if (!Channel_AddBan(Channel, mask))
880 return Send_ListChange(true, what, Prefix, Client, Channel, mask);
885 * Delete entries from channel ban and invite lists.
887 * @param what Can be 'I' for invite or 'b' for ban list.
888 * @param Prefix The originator of the command.
889 * @param Client The sender of the command.
890 * @param Channel The channel of which the list should be modified.
891 * @param Pattern The pattern to add to the list.
892 * @return CONNECTED or DISCONNECTED.
895 Del_Ban_Invite(char what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel,
899 struct list_head *list;
901 assert(Client != NULL);
902 assert(Channel != NULL);
903 assert(Pattern != NULL);
904 assert(what == 'I' || what == 'b');
906 mask = Lists_MakeMask(Pattern);
909 list = Channel_GetListInvites(Channel);
911 list = Channel_GetListBans(Channel);
913 if (!Lists_CheckDupeMask(list, mask))
915 Lists_Del(list, mask);
917 return Send_ListChange(false, what, Prefix, Client, Channel, mask);
922 * Send information about changed channel ban/invite lists to clients.
924 * @param IsAdd true if the list item has been added, false otherwise.
925 * @param ModeChar The mode to use (e. g. 'b' or 'I')
926 * @param Prefix The originator of the mode list change.
927 * @param Client The sender of the command.
928 * @param Channel The channel of which the list has been modified.
929 * @param Mask The mask which has been added or removed.
930 * @return CONNECTED or DISCONNECTED.
933 Send_ListChange(const bool IsAdd, const char ModeChar, CLIENT *Prefix,
934 CLIENT *Client, CHANNEL *Channel, const char *Mask)
938 /* Send confirmation to the client */
939 if (Client_Type(Client) == CLIENT_USER)
940 ok = IRC_WriteStrClientPrefix(Client, Prefix, "MODE %s %c%c %s",
941 Channel_Name(Channel),
945 /* to other servers */
946 IRC_WriteStrServersPrefix(Client, Prefix, "MODE %s %c%c %s",
947 Channel_Name(Channel), IsAdd ? '+' : '-',
950 /* and local users in channel */
951 IRC_WriteStrChannelPrefix(Client, Channel, Prefix, false,
952 "MODE %s %c%c %s", Channel_Name(Channel),
953 IsAdd ? '+' : '-', ModeChar, Mask );
956 } /* Send_ListChange */