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.
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 if (!IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
298 Client_ID(Origin), Channel_Name(Channel),
302 if (!IRC_WriteStrClient(Origin, RPL_CREATIONTIME_MSG,
303 Client_ID(Origin), Channel_Name(Channel),
304 Channel_CreationTime(Channel)))
312 * Handle channel mode and channel-user mode changes
315 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
317 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
318 argadd[CLIENT_PASS_LEN], *mode_ptr;
319 bool connected, set, skiponce, retval, onchannel, modeok, use_servermode;
320 int mode_arg, arg_arg;
325 if (Channel_IsModeless(Channel))
326 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
327 Client_ID(Client), Channel_Name(Channel));
329 /* Mode request: let's answer it :-) */
331 return Channel_Mode_Answer_Request(Origin, Channel);
333 Channel_CheckAdminRights(Channel, Client, Origin,
334 &onchannel, &modeok, &use_servermode);
336 if (!onchannel && !modeok)
337 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
339 Channel_Name(Channel));
342 mode_ptr = Req->argv[mode_arg];
343 if (Req->argc > mode_arg + 1)
344 arg_arg = mode_arg + 1;
348 /* Initial state: set or unset modes? */
362 /* Prepare reply string */
363 strcpy(the_modes, set ? "+" : "-");
367 connected = CONNECTED;
372 /* Try next argument if there's any */
373 if (arg_arg > mode_arg)
378 if (mode_arg >= Req->argc)
380 mode_ptr = Req->argv[mode_arg];
382 if (Req->argc > mode_arg + 1)
383 arg_arg = mode_arg + 1;
392 if (((*mode_ptr == '+') && !set)
393 || ((*mode_ptr == '-') && set)) {
394 /* Action modifier ("+"/"-") must be changed ... */
395 len = strlen(the_modes) - 1;
396 if (the_modes[len] == '+' || the_modes[len] == '-') {
397 /* Adjust last action modifier in result */
398 the_modes[len] = *mode_ptr;
400 /* Append modifier character to result string */
402 strlcat(the_modes, x, sizeof(the_modes));
404 set = *mode_ptr == '+';
409 /* Are there arguments left? */
410 if (arg_arg >= Req->argc)
418 /* --- Channel modes --- */
419 case 'i': /* Invite only */
420 case 'm': /* Moderated */
421 case 'n': /* Only members can write */
422 case 's': /* Secret channel */
423 case 't': /* Topic locked */
424 case 'z': /* Secure connections only */
428 connected = IRC_WriteStrClient(Origin,
429 ERR_CHANOPRIVSNEEDED_MSG,
430 Client_ID(Origin), Channel_Name(Channel));
432 case 'k': /* Channel key */
437 connected = IRC_WriteStrClient(Origin,
438 ERR_CHANOPRIVSNEEDED_MSG,
440 Channel_Name(Channel));
443 if (arg_arg > mode_arg) {
445 Channel_ModeDel(Channel, 'k');
446 Channel_SetKey(Channel,
448 strlcpy(argadd, Channel_Key(Channel),
452 connected = IRC_WriteStrClient(Origin,
453 ERR_CHANOPRIVSNEEDED_MSG,
455 Channel_Name(Channel));
457 Req->argv[arg_arg][0] = '\0';
460 connected = IRC_WriteStrClient(Origin,
461 ERR_NEEDMOREPARAMS_MSG,
462 Client_ID(Origin), Req->command);
466 case 'l': /* Member limit */
471 connected = IRC_WriteStrClient(Origin,
472 ERR_CHANOPRIVSNEEDED_MSG,
474 Channel_Name(Channel));
477 if (arg_arg > mode_arg) {
479 l = atol(Req->argv[arg_arg]);
480 if (l > 0 && l < 0xFFFF) {
481 Channel_ModeDel(Channel, 'l');
482 Channel_SetMaxUsers(Channel, l);
483 snprintf(argadd, sizeof(argadd),
488 connected = IRC_WriteStrClient(Origin,
489 ERR_CHANOPRIVSNEEDED_MSG,
491 Channel_Name(Channel));
493 Req->argv[arg_arg][0] = '\0';
496 connected = IRC_WriteStrClient(Origin,
497 ERR_NEEDMOREPARAMS_MSG,
498 Client_ID(Origin), Req->command);
502 case 'P': /* Persistent channel */
504 /* Only IRC operators are allowed to
505 * set the 'P' channel mode! */
506 if (set && !(Client_OperByMe(Client)
507 || Client_Type(Client) == CLIENT_SERVER))
508 connected = IRC_WriteStrClient(Origin,
509 ERR_NOPRIVILEGES_MSG,
514 connected = IRC_WriteStrClient(Origin,
515 ERR_CHANOPRIVSNEEDED_MSG,
517 Channel_Name(Channel));
519 /* --- Channel user modes --- */
520 case 'o': /* Channel operator */
521 case 'v': /* Voice */
522 if (arg_arg > mode_arg) {
524 client = Client_Search(Req->argv[arg_arg]);
528 connected = IRC_WriteStrClient(Client,
533 connected = IRC_WriteStrClient(Origin,
534 ERR_CHANOPRIVSNEEDED_MSG,
536 Channel_Name(Channel));
538 Req->argv[arg_arg][0] = '\0';
541 connected = IRC_WriteStrClient(Origin,
542 ERR_NEEDMOREPARAMS_MSG,
543 Client_ID(Origin), Req->command);
547 /* --- Channel lists --- */
548 case 'I': /* Invite lists */
549 case 'b': /* Ban lists */
550 if (arg_arg > mode_arg) {
554 ? Add_Ban_Invite(*mode_ptr, Origin,
557 : Del_Ban_Invite(*mode_ptr, Origin,
561 connected = IRC_WriteStrClient(Origin,
562 ERR_CHANOPRIVSNEEDED_MSG,
564 Channel_Name(Channel));
566 Req->argv[arg_arg][0] = '\0';
569 if (*mode_ptr == 'I')
570 Channel_ShowInvites(Origin, Channel);
572 Channel_ShowBans(Origin, Channel);
577 "Unknown mode \"%c%c\" from \"%s\" on %s!?",
578 set ? '+' : '-', *mode_ptr, Client_ID(Origin),
579 Channel_Name(Channel));
580 if (Client_Type(Client) != CLIENT_SERVER)
581 connected = IRC_WriteStrClient(Origin,
582 ERR_UMODEUNKNOWNFLAG2_MSG,
584 set ? '+' : '-', *mode_ptr);
592 /* Is there a valid mode change? */
596 /* Validate target client */
597 if (client && (!Channel_IsMemberOf(Channel, client))) {
598 if (!IRC_WriteStrClient
599 (Origin, ERR_USERNOTINCHANNEL_MSG,
600 Client_ID(Origin), Client_ID(client),
601 Channel_Name(Channel)))
608 /* Channel-User-Mode */
610 ? Channel_UserModeAdd(Channel, client, x[0])
611 : Channel_UserModeDel(Channel, client, x[0]);
613 strlcat(the_args, " ", sizeof(the_args));
614 strlcat(the_args, Client_ID(client),
616 strlcat(the_modes, x, sizeof(the_modes));
618 ("User \"%s\": Mode change on %s, now \"%s\"",
619 Client_Mask(client), Channel_Name(Channel),
620 Channel_UserModes(Channel, client));
625 ? Channel_ModeAdd(Channel, x[0])
626 : Channel_ModeDel(Channel, x[0]);
628 strlcat(the_modes, x, sizeof(the_modes));
629 LogDebug("Channel %s: Mode change, now \"%s\".",
630 Channel_Name(Channel),
631 Channel_Modes(Channel));
635 /* Are there additional arguments to add? */
637 strlcat(the_args, " ", sizeof(the_args));
638 strlcat(the_args, argadd, sizeof(the_args));
643 /* Are there changed modes? */
645 /* Clean up mode string */
646 len = strlen(the_modes) - 1;
647 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
648 the_modes[len] = '\0';
650 if (Client_Type(Client) == CLIENT_SERVER) {
651 /* MODE requests for local channels from other servers
652 * are definitely invalid! */
653 if (Channel_IsLocal(Channel)) {
654 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
658 /* Forward mode changes to channel users and all the
659 * other remote servers: */
660 IRC_WriteStrServersPrefix(Client, Origin,
661 "MODE %s %s%s", Channel_Name(Channel),
662 the_modes, the_args);
663 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
664 false, "MODE %s %s%s", Channel_Name(Channel),
665 the_modes, the_args);
668 Origin = Client_ThisServer();
669 /* Send reply to client and inform other servers and channel users */
670 connected = IRC_WriteStrClientPrefix(Client, Origin,
671 "MODE %s %s%s", Channel_Name(Channel),
672 the_modes, the_args);
673 /* Only forward requests for non-local channels */
674 if (!Channel_IsLocal(Channel))
675 IRC_WriteStrServersPrefix(Client, Origin,
676 "MODE %s %s%s", Channel_Name(Channel),
677 the_modes, the_args);
678 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
679 false, "MODE %s %s%s", Channel_Name(Channel),
680 the_modes, the_args);
684 IRC_SetPenalty(Client, 1);
690 IRC_AWAY( CLIENT *Client, REQUEST *Req )
692 assert( Client != NULL );
693 assert( Req != NULL );
695 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
697 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
699 Client_SetAway( Client, Req->argv[0] );
700 Client_ModeAdd( Client, 'a' );
701 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
702 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
706 Client_ModeDel( Client, 'a' );
707 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
708 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
714 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
720 assert( Client != NULL );
721 assert( Channel != NULL );
722 assert( Pattern != NULL );
723 assert(what == 'I' || what == 'b');
725 mask = Lists_MakeMask(Pattern);
727 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
730 ret = Channel_AddInvite(Channel, mask, false);
732 ret = Channel_AddBan(Channel, mask);
736 if (already && (Client_Type(Prefix) == CLIENT_SERVER))
740 return Send_ListChange("+I", Prefix, Client, Channel, mask);
741 return Send_ListChange("+b", Prefix, Client, Channel, mask);
746 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
749 struct list_head *list;
751 assert( Client != NULL );
752 assert( Channel != NULL );
753 assert( Pattern != NULL );
754 assert(what == 'I' || what == 'b');
756 mask = Lists_MakeMask( Pattern );
759 list = Channel_GetListInvites(Channel);
761 list = Channel_GetListBans(Channel);
763 Lists_Del(list, mask);
765 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
766 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
771 Send_ListChange(const char *Mode, CLIENT *Prefix, CLIENT *Client,
772 CHANNEL *Channel, const char *Mask)
776 if( Client_Type( Client ) == CLIENT_USER )
778 /* send confirmation to client */
779 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
783 /* to other servers */
784 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
786 /* and local users in channel */
787 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
790 } /* Send_ListChange */