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,
188 case 'o': /* IRC operator (only unsettable!) */
189 if(( ! set ) || ( Client_Type( Client ) == CLIENT_SERVER ))
191 Client_SetOperByMe( Target, false );
194 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
197 case 'r': /* Restricted (only settable) */
198 if(( set ) || ( Client_Type( Client ) == CLIENT_SERVER )) x[0] = 'r';
199 else ok = IRC_WriteStrClient( Origin, ERR_RESTRICTED_MSG, Client_ID( Origin ));
203 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\"!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ));
204 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
210 /* Is there a valid mode change? */
211 if( ! x[0] ) continue;
216 if( Client_ModeAdd( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
222 if( Client_ModeDel( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
227 /* Are there changed modes? */
230 /* Remoce needless action modifier characters */
231 len = strlen( the_modes ) - 1;
232 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' )) the_modes[len] = '\0';
234 if( Client_Type( Client ) == CLIENT_SERVER )
236 /* Forward modes to other servers */
237 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
241 /* Send reply to client and inform other servers */
242 ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
243 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
245 LogDebug("%s \"%s\": Mode change, now \"%s\".",
246 Client_TypeText(Target), Client_Mask(Target),
247 Client_Modes(Target));
250 IRC_SetPenalty( Client, 1 );
256 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
258 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
259 const char *mode_ptr;
261 /* Member or not? -- That's the question! */
262 if (!Channel_IsMemberOf(Channel, Origin))
263 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
264 Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
266 /* The sender is a member: generate extended reply */
267 strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
268 mode_ptr = the_modes;
274 snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
275 strlcat(the_args, argadd, sizeof(the_args));
278 strlcat(the_args, " ", sizeof(the_args));
279 strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
285 strlcat(the_modes, the_args, sizeof(the_modes));
287 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
288 Client_ID(Origin), Channel_Name(Channel), the_modes);
293 * Handle channel mode and channel-user mode changes
296 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
298 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
299 argadd[CLIENT_PASS_LEN], *mode_ptr;
300 bool connected, set, modeok = true, skiponce, use_servermode = false, retval;
301 int mode_arg, arg_arg;
306 if (Channel_IsModeless(Channel))
307 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
308 Client_ID(Client), Channel_Name(Channel));
310 /* Mode request: let's answer it :-) */
312 return Channel_Mode_Answer_Request(Origin, Channel);
314 /* Is the user allowed to change modes? */
315 if (Client_Type(Client) == CLIENT_USER) {
316 /* Is the originating user on that channel? */
317 if (!Channel_IsMemberOf(Channel, Origin))
318 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
319 Client_ID(Origin), Channel_Name(Channel));
321 /* channel operator? */
322 if (strchr(Channel_UserModes(Channel, Origin), 'o'))
324 else if (Conf_OperCanMode) {
325 /* IRC-Operators can use MODE as well */
326 if (Client_OperByMe(Origin)) {
328 if (Conf_OperServerMode)
329 use_servermode = true; /* Change Origin to Server */
335 mode_ptr = Req->argv[mode_arg];
336 if (Req->argc > mode_arg + 1)
337 arg_arg = mode_arg + 1;
341 /* Initial state: set or unset modes? */
355 /* Prepare reply string */
356 strcpy(the_modes, set ? "+" : "-");
360 connected = CONNECTED;
365 /* Try next argument if there's any */
366 if (arg_arg > mode_arg)
371 if (mode_arg >= Req->argc)
373 mode_ptr = Req->argv[mode_arg];
375 if (Req->argc > mode_arg + 1)
376 arg_arg = mode_arg + 1;
385 if (((*mode_ptr == '+') && !set)
386 || ((*mode_ptr == '-') && set)) {
387 /* Action modifier ("+"/"-") must be changed ... */
388 len = strlen(the_modes) - 1;
389 if (the_modes[len] == '+' || the_modes[len] == '-') {
390 /* Adjust last action modifier in result */
391 the_modes[len] = *mode_ptr;
393 /* Append modifier character to result string */
395 strlcat(the_modes, x, sizeof(the_modes));
397 set = *mode_ptr == '+';
402 /* Are there arguments left? */
403 if (arg_arg >= Req->argc)
411 /* --- Channel modes --- */
412 case 'i': /* Invite only */
413 case 'm': /* Moderated */
414 case 'n': /* Only members can write */
415 case 's': /* Secret channel */
416 case 't': /* Topic locked */
417 case 'z': /* Secure connections only */
421 connected = IRC_WriteStrClient(Origin,
422 ERR_CHANOPRIVSNEEDED_MSG,
423 Client_ID(Origin), Channel_Name(Channel));
425 case 'k': /* Channel key */
430 connected = IRC_WriteStrClient(Origin,
431 ERR_CHANOPRIVSNEEDED_MSG,
433 Channel_Name(Channel));
436 if (arg_arg > mode_arg) {
438 Channel_ModeDel(Channel, 'k');
439 Channel_SetKey(Channel,
441 strlcpy(argadd, Channel_Key(Channel),
445 connected = IRC_WriteStrClient(Origin,
446 ERR_CHANOPRIVSNEEDED_MSG,
448 Channel_Name(Channel));
450 Req->argv[arg_arg][0] = '\0';
453 connected = IRC_WriteStrClient(Origin,
454 ERR_NEEDMOREPARAMS_MSG,
455 Client_ID(Origin), Req->command);
459 case 'l': /* Member limit */
464 connected = IRC_WriteStrClient(Origin,
465 ERR_CHANOPRIVSNEEDED_MSG,
467 Channel_Name(Channel));
470 if (arg_arg > mode_arg) {
472 l = atol(Req->argv[arg_arg]);
473 if (l > 0 && l < 0xFFFF) {
474 Channel_ModeDel(Channel, 'l');
475 Channel_SetMaxUsers(Channel, l);
476 snprintf(argadd, sizeof(argadd),
481 connected = IRC_WriteStrClient(Origin,
482 ERR_CHANOPRIVSNEEDED_MSG,
484 Channel_Name(Channel));
486 Req->argv[arg_arg][0] = '\0';
489 connected = IRC_WriteStrClient(Origin,
490 ERR_NEEDMOREPARAMS_MSG,
491 Client_ID(Origin), Req->command);
495 case 'P': /* Persistent channel */
497 /* Only IRC operators are allowed to
498 * set the 'P' channel mode! */
499 if (set && !(Client_OperByMe(Client)
500 || Client_Type(Client) == CLIENT_SERVER))
501 connected = IRC_WriteStrClient(Origin,
502 ERR_NOPRIVILEGES_MSG,
507 connected = IRC_WriteStrClient(Origin,
508 ERR_CHANOPRIVSNEEDED_MSG,
510 Channel_Name(Channel));
512 /* --- Channel user modes --- */
513 case 'o': /* Channel operator */
514 case 'v': /* Voice */
515 if (arg_arg > mode_arg) {
517 client = Client_Search(Req->argv[arg_arg]);
521 connected = IRC_WriteStrClient(Client,
526 connected = IRC_WriteStrClient(Origin,
527 ERR_CHANOPRIVSNEEDED_MSG,
529 Channel_Name(Channel));
531 Req->argv[arg_arg][0] = '\0';
534 connected = IRC_WriteStrClient(Origin,
535 ERR_NEEDMOREPARAMS_MSG,
536 Client_ID(Origin), Req->command);
540 /* --- Channel lists --- */
541 case 'I': /* Invite lists */
542 case 'b': /* Ban lists */
543 if (arg_arg > mode_arg) {
547 ? Add_Ban_Invite(*mode_ptr, Origin,
550 : Del_Ban_Invite(*mode_ptr, Origin,
554 connected = IRC_WriteStrClient(Origin,
555 ERR_CHANOPRIVSNEEDED_MSG,
557 Channel_Name(Channel));
559 Req->argv[arg_arg][0] = '\0';
562 if (*mode_ptr == 'I')
563 Channel_ShowInvites(Origin, Channel);
565 Channel_ShowBans(Origin, Channel);
570 "Unknown mode \"%c%c\" from \"%s\" on %s!?",
571 set ? '+' : '-', *mode_ptr, Client_ID(Origin),
572 Channel_Name(Channel));
573 if (Client_Type(Client) != CLIENT_SERVER)
574 connected = IRC_WriteStrClient(Origin,
575 ERR_UMODEUNKNOWNFLAG2_MSG,
577 set ? '+' : '-', *mode_ptr);
585 /* Is there a valid mode change? */
589 /* Validate target client */
590 if (client && (!Channel_IsMemberOf(Channel, client))) {
591 if (!IRC_WriteStrClient
592 (Origin, ERR_USERNOTINCHANNEL_MSG,
593 Client_ID(Origin), Client_ID(client),
594 Channel_Name(Channel)))
601 /* Channel-User-Mode */
603 ? Channel_UserModeAdd(Channel, client, x[0])
604 : Channel_UserModeDel(Channel, client, x[0]);
606 strlcat(the_args, " ", sizeof(the_args));
607 strlcat(the_args, Client_ID(client),
609 strlcat(the_modes, x, sizeof(the_modes));
611 ("User \"%s\": Mode change on %s, now \"%s\"",
612 Client_Mask(client), Channel_Name(Channel),
613 Channel_UserModes(Channel, client));
618 ? Channel_ModeAdd(Channel, x[0])
619 : Channel_ModeDel(Channel, x[0]);
621 strlcat(the_modes, x, sizeof(the_modes));
622 LogDebug("Channel %s: Mode change, now \"%s\".",
623 Channel_Name(Channel),
624 Channel_Modes(Channel));
628 /* Are there additional arguments to add? */
630 strlcat(the_args, " ", sizeof(the_args));
631 strlcat(the_args, argadd, sizeof(the_args));
636 /* Are there changed modes? */
638 /* Clean up mode string */
639 len = strlen(the_modes) - 1;
640 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
641 the_modes[len] = '\0';
643 if (Client_Type(Client) == CLIENT_SERVER) {
644 /* MODE requests for local channels from other servers
645 * are definitely invalid! */
646 if (Channel_IsLocal(Channel)) {
647 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
651 /* Forward mode changes to channel users and all the
652 * other remote servers: */
653 IRC_WriteStrServersPrefix(Client, Origin,
654 "MODE %s %s%s", Channel_Name(Channel),
655 the_modes, the_args);
656 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
657 false, "MODE %s %s%s", Channel_Name(Channel),
658 the_modes, the_args);
661 Origin = Client_ThisServer();
662 /* Send reply to client and inform other servers and channel users */
663 connected = IRC_WriteStrClientPrefix(Client, Origin,
664 "MODE %s %s%s", Channel_Name(Channel),
665 the_modes, the_args);
666 /* Only forward requests for non-local channels */
667 if (!Channel_IsLocal(Channel))
668 IRC_WriteStrServersPrefix(Client, Origin,
669 "MODE %s %s%s", Channel_Name(Channel),
670 the_modes, the_args);
671 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
672 false, "MODE %s %s%s", Channel_Name(Channel),
673 the_modes, the_args);
677 IRC_SetPenalty(Client, 1);
683 IRC_AWAY( CLIENT *Client, REQUEST *Req )
685 assert( Client != NULL );
686 assert( Req != NULL );
688 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
690 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
692 Client_SetAway( Client, Req->argv[0] );
693 Client_ModeAdd( Client, 'a' );
694 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
695 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
699 Client_ModeDel( Client, 'a' );
700 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
701 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
707 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
713 assert( Client != NULL );
714 assert( Channel != NULL );
715 assert( Pattern != NULL );
716 assert(what == 'I' || what == 'b');
718 mask = Lists_MakeMask(Pattern);
720 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
723 ret = Channel_AddInvite(Channel, mask, false);
725 ret = Channel_AddBan(Channel, mask);
729 if (already && (Client_Type(Prefix) == CLIENT_SERVER))
733 return Send_ListChange("+I", Prefix, Client, Channel, mask);
734 return Send_ListChange("+b", Prefix, Client, Channel, mask);
739 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
742 struct list_head *list;
744 assert( Client != NULL );
745 assert( Channel != NULL );
746 assert( Pattern != NULL );
747 assert(what == 'I' || what == 'b');
749 mask = Lists_MakeMask( Pattern );
752 list = Channel_GetListInvites(Channel);
754 list = Channel_GetListBans(Channel);
756 Lists_Del(list, mask);
758 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
759 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
764 Send_ListChange(const char *Mode, CLIENT *Prefix, CLIENT *Client,
765 CHANNEL *Channel, const char *Mask)
769 if( Client_Type( Client ) == CLIENT_USER )
771 /* send confirmation to client */
772 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
776 /* to other servers */
777 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
779 /* and local users in channel */
780 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
783 } /* Send_ListChange */