2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2010 Alexander Barton (alex@barton.de)
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.
11 * IRC commands for mode changes (MODE, AWAY, ...)
26 #include "irc-write.h"
37 static bool Client_Mode PARAMS(( CLIENT *Client, REQUEST *Req, CLIENT *Origin,
39 static bool Channel_Mode PARAMS(( CLIENT *Client, REQUEST *Req, CLIENT *Origin,
42 static bool Add_Ban_Invite PARAMS((int what, CLIENT *Prefix, CLIENT *Client,
43 CHANNEL *Channel, const char *Pattern));
44 static bool Del_Ban_Invite PARAMS((int what, CLIENT *Prefix, CLIENT *Client,
45 CHANNEL *Channel, const char *Pattern));
47 static bool Send_ListChange PARAMS((const char *Mode, CLIENT *Prefix,
48 CLIENT *Client, CHANNEL *Channel, const char *Mask));
52 IRC_MODE( CLIENT *Client, REQUEST *Req )
57 assert( Client != NULL );
58 assert( Req != NULL );
61 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
63 /* Origin for answers */
64 if( Client_Type( Client ) == CLIENT_SERVER )
66 origin = Client_Search( Req->prefix );
67 if( ! origin ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
71 /* Channel or user mode? */
72 cl = NULL; chan = NULL;
73 if (Client_IsValidNick(Req->argv[0]))
74 cl = Client_Search(Req->argv[0]);
75 if (Channel_IsValidName(Req->argv[0]))
76 chan = Channel_Search(Req->argv[0]);
79 return Client_Mode(Client, Req, origin, cl);
81 return Channel_Mode(Client, Req, origin, chan);
83 /* No target found! */
84 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
85 Client_ID(Client), Req->argv[0]);
90 Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target )
92 /* Handle client mode requests */
94 char the_modes[COMMAND_LEN], x[2], *mode_ptr;
99 /* Is the client allowed to request or change the modes? */
100 if( Client_Type( Client ) == CLIENT_USER )
102 /* Users are only allowed to manipulate their own modes! */
103 if( Target != Client ) return IRC_WriteStrClient( Client, ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
106 /* Mode request: let's answer it :-) */
107 if( Req->argc == 1 ) return IRC_WriteStrClient( Origin, RPL_UMODEIS_MSG, Client_ID( Origin ), Client_Modes( Target ));
110 mode_ptr = Req->argv[mode_arg];
112 /* Initial state: set or unset modes? */
113 if( *mode_ptr == '+' ) set = true;
114 else if( *mode_ptr == '-' ) set = false;
115 else return IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Origin ));
117 /* Prepare reply string */
118 if( set ) strcpy( the_modes, "+" );
119 else strcpy( the_modes, "-" );
128 /* Try next argument if there's any */
130 if( mode_arg < Req->argc ) mode_ptr = Req->argv[mode_arg];
138 if((( *mode_ptr == '+' ) && ( ! set )) || (( *mode_ptr == '-' ) && ( set )))
140 /* Action modifier ("+"/"-") must be changed ... */
141 len = strlen( the_modes ) - 1;
142 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' ))
144 /* Adjust last action modifier in result */
145 the_modes[len] = *mode_ptr;
149 /* Append modifier character to result string */
151 strlcat( the_modes, x, sizeof( the_modes ));
153 if( *mode_ptr == '+' ) set = true;
163 case 'i': /* Invisible */
164 case 's': /* Server messages */
165 case 'w': /* Wallops messages */
170 if( Client_Type( Client ) == CLIENT_SERVER )
173 Client_SetAway( Origin, DEFAULT_AWAY_MSG );
175 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
178 case 'c': /* Receive connect notices
179 * (only settable by IRC operators!) */
180 if(!set || Client_OperByMe(Origin)
181 || Client_Type(Client) == CLIENT_SERVER)
184 ok = IRC_WriteStrClient(Origin,
185 ERR_NOPRIVILEGES_MSG,
189 case 'o': /* IRC operator (only unsettable!) */
190 if(( ! set ) || ( Client_Type( Client ) == CLIENT_SERVER ))
192 Client_SetOperByMe( Target, false );
195 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
198 case 'r': /* Restricted (only settable) */
199 if(( set ) || ( Client_Type( Client ) == CLIENT_SERVER )) x[0] = 'r';
200 else ok = IRC_WriteStrClient( Origin, ERR_RESTRICTED_MSG, Client_ID( Origin ));
203 case 'x': /* Cloak hostname */
204 if (Client_HasMode(Client, 'r'))
205 IRC_WriteStrClient(Origin,
213 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\"!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ));
214 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
220 /* Is there a valid mode change? */
221 if( ! x[0] ) continue;
226 if( Client_ModeAdd( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
232 if( Client_ModeDel( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
237 /* Are there changed modes? */
240 /* Remoce needless action modifier characters */
241 len = strlen( the_modes ) - 1;
242 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' )) the_modes[len] = '\0';
244 if( Client_Type( Client ) == CLIENT_SERVER )
246 /* Forward modes to other servers */
247 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
251 /* Send reply to client and inform other servers */
252 ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
253 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
255 LogDebug("%s \"%s\": Mode change, now \"%s\".",
256 Client_TypeText(Target), Client_Mask(Target),
257 Client_Modes(Target));
260 IRC_SetPenalty( Client, 1 );
266 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
268 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
269 const char *mode_ptr;
271 /* Member or not? -- That's the question! */
272 if (!Channel_IsMemberOf(Channel, Origin))
273 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
274 Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
276 /* The sender is a member: generate extended reply */
277 strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
278 mode_ptr = the_modes;
284 snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
285 strlcat(the_args, argadd, sizeof(the_args));
288 strlcat(the_args, " ", sizeof(the_args));
289 strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
295 strlcat(the_modes, the_args, sizeof(the_modes));
297 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
298 Client_ID(Origin), Channel_Name(Channel), the_modes);
303 * Handle channel mode and channel-user mode changes
306 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
308 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
309 argadd[CLIENT_PASS_LEN], *mode_ptr;
310 bool connected, set, skiponce, retval, onchannel;
311 bool modeok = true, use_servermode = false;
312 int mode_arg, arg_arg;
317 if (Channel_IsModeless(Channel))
318 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
319 Client_ID(Client), Channel_Name(Channel));
321 /* Mode request: let's answer it :-) */
323 return Channel_Mode_Answer_Request(Origin, Channel);
325 /* Is the user allowed to change modes? */
326 if (Client_Type(Client) == CLIENT_USER) {
327 /* Is the originating user on that channel? */
328 onchannel = Channel_IsMemberOf(Channel, Origin);
330 /* channel operator? */
332 strchr(Channel_UserModes(Channel, Origin), 'o')) {
334 } else if (Conf_OperCanMode) {
335 /* IRC-Operators can use MODE as well */
336 if (Client_OperByMe(Origin)) {
338 if (Conf_OperServerMode)
339 use_servermode = true; /* Change Origin to Server */
343 if (!onchannel && !modeok)
344 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
345 Client_ID(Origin), Channel_Name(Channel));
349 mode_ptr = Req->argv[mode_arg];
350 if (Req->argc > mode_arg + 1)
351 arg_arg = mode_arg + 1;
355 /* Initial state: set or unset modes? */
369 /* Prepare reply string */
370 strcpy(the_modes, set ? "+" : "-");
374 connected = CONNECTED;
379 /* Try next argument if there's any */
380 if (arg_arg > mode_arg)
385 if (mode_arg >= Req->argc)
387 mode_ptr = Req->argv[mode_arg];
389 if (Req->argc > mode_arg + 1)
390 arg_arg = mode_arg + 1;
399 if (((*mode_ptr == '+') && !set)
400 || ((*mode_ptr == '-') && set)) {
401 /* Action modifier ("+"/"-") must be changed ... */
402 len = strlen(the_modes) - 1;
403 if (the_modes[len] == '+' || the_modes[len] == '-') {
404 /* Adjust last action modifier in result */
405 the_modes[len] = *mode_ptr;
407 /* Append modifier character to result string */
409 strlcat(the_modes, x, sizeof(the_modes));
411 set = *mode_ptr == '+';
416 /* Are there arguments left? */
417 if (arg_arg >= Req->argc)
425 /* --- Channel modes --- */
426 case 'i': /* Invite only */
427 case 'm': /* Moderated */
428 case 'n': /* Only members can write */
429 case 's': /* Secret channel */
430 case 't': /* Topic locked */
431 case 'z': /* Secure connections only */
435 connected = IRC_WriteStrClient(Origin,
436 ERR_CHANOPRIVSNEEDED_MSG,
437 Client_ID(Origin), Channel_Name(Channel));
439 case 'k': /* Channel key */
444 connected = IRC_WriteStrClient(Origin,
445 ERR_CHANOPRIVSNEEDED_MSG,
447 Channel_Name(Channel));
450 if (arg_arg > mode_arg) {
452 Channel_ModeDel(Channel, 'k');
453 Channel_SetKey(Channel,
455 strlcpy(argadd, Channel_Key(Channel),
459 connected = IRC_WriteStrClient(Origin,
460 ERR_CHANOPRIVSNEEDED_MSG,
462 Channel_Name(Channel));
464 Req->argv[arg_arg][0] = '\0';
467 connected = IRC_WriteStrClient(Origin,
468 ERR_NEEDMOREPARAMS_MSG,
469 Client_ID(Origin), Req->command);
473 case 'l': /* Member limit */
478 connected = IRC_WriteStrClient(Origin,
479 ERR_CHANOPRIVSNEEDED_MSG,
481 Channel_Name(Channel));
484 if (arg_arg > mode_arg) {
486 l = atol(Req->argv[arg_arg]);
487 if (l > 0 && l < 0xFFFF) {
488 Channel_ModeDel(Channel, 'l');
489 Channel_SetMaxUsers(Channel, l);
490 snprintf(argadd, sizeof(argadd),
495 connected = IRC_WriteStrClient(Origin,
496 ERR_CHANOPRIVSNEEDED_MSG,
498 Channel_Name(Channel));
500 Req->argv[arg_arg][0] = '\0';
503 connected = IRC_WriteStrClient(Origin,
504 ERR_NEEDMOREPARAMS_MSG,
505 Client_ID(Origin), Req->command);
509 case 'P': /* Persistent channel */
511 /* Only IRC operators are allowed to
512 * set the 'P' channel mode! */
513 if (set && !(Client_OperByMe(Client)
514 || Client_Type(Client) == CLIENT_SERVER))
515 connected = IRC_WriteStrClient(Origin,
516 ERR_NOPRIVILEGES_MSG,
521 connected = IRC_WriteStrClient(Origin,
522 ERR_CHANOPRIVSNEEDED_MSG,
524 Channel_Name(Channel));
526 /* --- Channel user modes --- */
527 case 'o': /* Channel operator */
528 case 'v': /* Voice */
529 if (arg_arg > mode_arg) {
531 client = Client_Search(Req->argv[arg_arg]);
535 connected = IRC_WriteStrClient(Client,
540 connected = IRC_WriteStrClient(Origin,
541 ERR_CHANOPRIVSNEEDED_MSG,
543 Channel_Name(Channel));
545 Req->argv[arg_arg][0] = '\0';
548 connected = IRC_WriteStrClient(Origin,
549 ERR_NEEDMOREPARAMS_MSG,
550 Client_ID(Origin), Req->command);
554 /* --- Channel lists --- */
555 case 'I': /* Invite lists */
556 case 'b': /* Ban lists */
557 if (arg_arg > mode_arg) {
561 ? Add_Ban_Invite(*mode_ptr, Origin,
564 : Del_Ban_Invite(*mode_ptr, Origin,
568 connected = IRC_WriteStrClient(Origin,
569 ERR_CHANOPRIVSNEEDED_MSG,
571 Channel_Name(Channel));
573 Req->argv[arg_arg][0] = '\0';
576 if (*mode_ptr == 'I')
577 Channel_ShowInvites(Origin, Channel);
579 Channel_ShowBans(Origin, Channel);
584 "Unknown mode \"%c%c\" from \"%s\" on %s!?",
585 set ? '+' : '-', *mode_ptr, Client_ID(Origin),
586 Channel_Name(Channel));
587 if (Client_Type(Client) != CLIENT_SERVER)
588 connected = IRC_WriteStrClient(Origin,
589 ERR_UMODEUNKNOWNFLAG2_MSG,
591 set ? '+' : '-', *mode_ptr);
599 /* Is there a valid mode change? */
603 /* Validate target client */
604 if (client && (!Channel_IsMemberOf(Channel, client))) {
605 if (!IRC_WriteStrClient
606 (Origin, ERR_USERNOTINCHANNEL_MSG,
607 Client_ID(Origin), Client_ID(client),
608 Channel_Name(Channel)))
615 /* Channel-User-Mode */
617 ? Channel_UserModeAdd(Channel, client, x[0])
618 : Channel_UserModeDel(Channel, client, x[0]);
620 strlcat(the_args, " ", sizeof(the_args));
621 strlcat(the_args, Client_ID(client),
623 strlcat(the_modes, x, sizeof(the_modes));
625 ("User \"%s\": Mode change on %s, now \"%s\"",
626 Client_Mask(client), Channel_Name(Channel),
627 Channel_UserModes(Channel, client));
632 ? Channel_ModeAdd(Channel, x[0])
633 : Channel_ModeDel(Channel, x[0]);
635 strlcat(the_modes, x, sizeof(the_modes));
636 LogDebug("Channel %s: Mode change, now \"%s\".",
637 Channel_Name(Channel),
638 Channel_Modes(Channel));
642 /* Are there additional arguments to add? */
644 strlcat(the_args, " ", sizeof(the_args));
645 strlcat(the_args, argadd, sizeof(the_args));
650 /* Are there changed modes? */
652 /* Clean up mode string */
653 len = strlen(the_modes) - 1;
654 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
655 the_modes[len] = '\0';
657 if (Client_Type(Client) == CLIENT_SERVER) {
658 /* MODE requests for local channels from other servers
659 * are definitely invalid! */
660 if (Channel_IsLocal(Channel)) {
661 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
665 /* Forward mode changes to channel users and all the
666 * other remote servers: */
667 IRC_WriteStrServersPrefix(Client, Origin,
668 "MODE %s %s%s", Channel_Name(Channel),
669 the_modes, the_args);
670 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
671 false, "MODE %s %s%s", Channel_Name(Channel),
672 the_modes, the_args);
675 Origin = Client_ThisServer();
676 /* Send reply to client and inform other servers and channel users */
677 connected = IRC_WriteStrClientPrefix(Client, Origin,
678 "MODE %s %s%s", Channel_Name(Channel),
679 the_modes, the_args);
680 /* Only forward requests for non-local channels */
681 if (!Channel_IsLocal(Channel))
682 IRC_WriteStrServersPrefix(Client, Origin,
683 "MODE %s %s%s", Channel_Name(Channel),
684 the_modes, the_args);
685 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
686 false, "MODE %s %s%s", Channel_Name(Channel),
687 the_modes, the_args);
691 IRC_SetPenalty(Client, 1);
697 IRC_AWAY( CLIENT *Client, REQUEST *Req )
699 assert( Client != NULL );
700 assert( Req != NULL );
702 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
704 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
706 Client_SetAway( Client, Req->argv[0] );
707 Client_ModeAdd( Client, 'a' );
708 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
709 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
713 Client_ModeDel( Client, 'a' );
714 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
715 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
721 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
727 assert( Client != NULL );
728 assert( Channel != NULL );
729 assert( Pattern != NULL );
730 assert(what == 'I' || what == 'b');
732 mask = Lists_MakeMask(Pattern);
734 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
737 ret = Channel_AddInvite(Channel, mask, false);
739 ret = Channel_AddBan(Channel, mask);
743 if (already && (Client_Type(Prefix) == CLIENT_SERVER))
747 return Send_ListChange("+I", Prefix, Client, Channel, mask);
748 return Send_ListChange("+b", Prefix, Client, Channel, mask);
753 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
756 struct list_head *list;
758 assert( Client != NULL );
759 assert( Channel != NULL );
760 assert( Pattern != NULL );
761 assert(what == 'I' || what == 'b');
763 mask = Lists_MakeMask( Pattern );
766 list = Channel_GetListInvites(Channel);
768 list = Channel_GetListBans(Channel);
770 Lists_Del(list, mask);
772 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
773 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
778 Send_ListChange(const char *Mode, CLIENT *Prefix, CLIENT *Client,
779 CHANNEL *Channel, const char *Mask)
783 if( Client_Type( Client ) == CLIENT_USER )
785 /* send confirmation to client */
786 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
790 /* to other servers */
791 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
793 /* and local users in channel */
794 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
797 } /* Send_ListChange */