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, ...)
27 #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 IRC_MODE( CLIENT *Client, REQUEST *Req )
59 assert( Client != NULL );
60 assert( Req != NULL );
63 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
65 /* Origin for answers */
66 if( Client_Type( Client ) == CLIENT_SERVER )
68 origin = Client_Search( Req->prefix );
69 if( ! origin ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
73 /* Channel or user mode? */
74 cl = NULL; chan = NULL;
75 if (Client_IsValidNick(Req->argv[0]))
76 cl = Client_Search(Req->argv[0]);
77 if (Channel_IsValidName(Req->argv[0]))
78 chan = Channel_Search(Req->argv[0]);
81 return Client_Mode(Client, Req, origin, cl);
83 return Channel_Mode(Client, Req, origin, chan);
85 /* No target found! */
86 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
87 Client_ID(Client), Req->argv[0]);
92 Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target )
94 /* Handle client mode requests */
96 char the_modes[COMMAND_LEN], x[2], *mode_ptr;
101 /* Is the client allowed to request or change the modes? */
102 if( Client_Type( Client ) == CLIENT_USER )
104 /* Users are only allowed to manipulate their own modes! */
105 if( Target != Client ) return IRC_WriteStrClient( Client, ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
108 /* Mode request: let's answer it :-) */
109 if( Req->argc == 1 ) return IRC_WriteStrClient( Origin, RPL_UMODEIS_MSG, Client_ID( Origin ), Client_Modes( Target ));
112 mode_ptr = Req->argv[mode_arg];
114 /* Initial state: set or unset modes? */
115 if( *mode_ptr == '+' ) set = true;
116 else if( *mode_ptr == '-' ) set = false;
117 else return IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Origin ));
119 /* Prepare reply string */
120 if( set ) strcpy( the_modes, "+" );
121 else strcpy( the_modes, "-" );
130 /* Try next argument if there's any */
132 if( mode_arg < Req->argc ) mode_ptr = Req->argv[mode_arg];
140 if((( *mode_ptr == '+' ) && ( ! set )) || (( *mode_ptr == '-' ) && ( set )))
142 /* Action modifier ("+"/"-") must be changed ... */
143 len = strlen( the_modes ) - 1;
144 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' ))
146 /* Adjust last action modifier in result */
147 the_modes[len] = *mode_ptr;
151 /* Append modifier character to result string */
153 strlcat( the_modes, x, sizeof( the_modes ));
155 if( *mode_ptr == '+' ) set = true;
165 case 'i': /* Invisible */
166 case 's': /* Server messages */
167 case 'w': /* Wallops messages */
172 if( Client_Type( Client ) == CLIENT_SERVER )
175 Client_SetAway( Origin, DEFAULT_AWAY_MSG );
177 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
180 case 'c': /* Receive connect notices
181 * (only settable by IRC operators!) */
182 if(!set || Client_OperByMe(Origin)
183 || Client_Type(Client) == CLIENT_SERVER)
186 ok = IRC_WriteStrClient(Origin,
187 ERR_NOPRIVILEGES_MSG,
190 case 'o': /* IRC operator (only unsettable!) */
191 if(( ! set ) || ( Client_Type( Client ) == CLIENT_SERVER ))
193 Client_SetOperByMe( Target, false );
196 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
199 case 'r': /* Restricted (only settable) */
200 if(( set ) || ( Client_Type( Client ) == CLIENT_SERVER )) x[0] = 'r';
201 else ok = IRC_WriteStrClient( Origin, ERR_RESTRICTED_MSG, Client_ID( Origin ));
205 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\"!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ));
206 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
212 /* Is there a valid mode change? */
213 if( ! x[0] ) continue;
218 if( Client_ModeAdd( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
224 if( Client_ModeDel( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
229 /* Are there changed modes? */
232 /* Remoce needless action modifier characters */
233 len = strlen( the_modes ) - 1;
234 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' )) the_modes[len] = '\0';
236 if( Client_Type( Client ) == CLIENT_SERVER )
238 /* Forward modes to other servers */
239 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
243 /* Send reply to client and inform other servers */
244 ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
245 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
247 LogDebug("%s \"%s\": Mode change, now \"%s\".",
248 Client_TypeText(Target), Client_Mask(Target),
249 Client_Modes(Target));
252 IRC_SetPenalty( Client, 1 );
258 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
260 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
261 const char *mode_ptr;
263 /* Member or not? -- That's the question! */
264 if (!Channel_IsMemberOf(Channel, Origin))
265 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
266 Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
268 /* The sender is a member: generate extended reply */
269 strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
270 mode_ptr = the_modes;
276 snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
277 strlcat(the_args, argadd, sizeof(the_args));
280 strlcat(the_args, " ", sizeof(the_args));
281 strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
287 strlcat(the_modes, the_args, sizeof(the_modes));
289 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
290 Client_ID(Origin), Channel_Name(Channel), the_modes);
295 * Handle channel mode and channel-user mode changes
298 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
300 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
301 argadd[CLIENT_PASS_LEN], *mode_ptr;
302 bool connected, set, modeok = true, skiponce, use_servermode = false, retval;
303 int mode_arg, arg_arg;
308 if (Channel_IsModeless(Channel))
309 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
310 Client_ID(Client), Channel_Name(Channel));
312 /* Mode request: let's answer it :-) */
314 return Channel_Mode_Answer_Request(Origin, Channel);
316 /* Is the user allowed to change modes? */
317 if (Client_Type(Client) == CLIENT_USER) {
318 /* Is the originating user on that channel? */
319 if (!Channel_IsMemberOf(Channel, Origin))
320 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
321 Client_ID(Origin), Channel_Name(Channel));
323 /* channel operator? */
324 if (strchr(Channel_UserModes(Channel, Origin), 'o'))
326 else if (Conf_OperCanMode) {
327 /* IRC-Operators can use MODE as well */
328 if (Client_OperByMe(Origin)) {
330 if (Conf_OperServerMode)
331 use_servermode = true; /* Change Origin to Server */
337 mode_ptr = Req->argv[mode_arg];
338 if (Req->argc > mode_arg + 1)
339 arg_arg = mode_arg + 1;
343 /* Initial state: set or unset modes? */
357 /* Prepare reply string */
358 strcpy(the_modes, set ? "+" : "-");
362 connected = CONNECTED;
367 /* Try next argument if there's any */
368 if (arg_arg > mode_arg)
373 if (mode_arg >= Req->argc)
375 mode_ptr = Req->argv[mode_arg];
377 if (Req->argc > mode_arg + 1)
378 arg_arg = mode_arg + 1;
387 if (((*mode_ptr == '+') && !set)
388 || ((*mode_ptr == '-') && set)) {
389 /* Action modifier ("+"/"-") must be changed ... */
390 len = strlen(the_modes) - 1;
391 if (the_modes[len] == '+' || the_modes[len] == '-') {
392 /* Adjust last action modifier in result */
393 the_modes[len] = *mode_ptr;
395 /* Append modifier character to result string */
397 strlcat(the_modes, x, sizeof(the_modes));
399 set = *mode_ptr == '+';
404 /* Are there arguments left? */
405 if (arg_arg >= Req->argc)
413 /* --- Channel modes --- */
414 case 'i': /* Invite only */
415 case 'm': /* Moderated */
416 case 'n': /* Only members can write */
417 case 's': /* Secret channel */
418 case 't': /* Topic locked */
419 case 'z': /* Secure connections only */
423 connected = IRC_WriteStrClient(Origin,
424 ERR_CHANOPRIVSNEEDED_MSG,
425 Client_ID(Origin), Channel_Name(Channel));
427 case 'k': /* Channel key */
432 connected = IRC_WriteStrClient(Origin,
433 ERR_CHANOPRIVSNEEDED_MSG,
435 Channel_Name(Channel));
438 if (arg_arg > mode_arg) {
440 Channel_ModeDel(Channel, 'k');
441 Channel_SetKey(Channel,
443 strlcpy(argadd, Channel_Key(Channel),
447 connected = IRC_WriteStrClient(Origin,
448 ERR_CHANOPRIVSNEEDED_MSG,
450 Channel_Name(Channel));
452 Req->argv[arg_arg][0] = '\0';
455 connected = IRC_WriteStrClient(Origin,
456 ERR_NEEDMOREPARAMS_MSG,
457 Client_ID(Origin), Req->command);
461 case 'l': /* Member limit */
466 connected = IRC_WriteStrClient(Origin,
467 ERR_CHANOPRIVSNEEDED_MSG,
469 Channel_Name(Channel));
472 if (arg_arg > mode_arg) {
474 l = atol(Req->argv[arg_arg]);
475 if (l > 0 && l < 0xFFFF) {
476 Channel_ModeDel(Channel, 'l');
477 Channel_SetMaxUsers(Channel, l);
478 snprintf(argadd, sizeof(argadd),
483 connected = IRC_WriteStrClient(Origin,
484 ERR_CHANOPRIVSNEEDED_MSG,
486 Channel_Name(Channel));
488 Req->argv[arg_arg][0] = '\0';
491 connected = IRC_WriteStrClient(Origin,
492 ERR_NEEDMOREPARAMS_MSG,
493 Client_ID(Origin), Req->command);
497 case 'P': /* Persistent channel */
499 /* Only IRC operators are allowed to
500 * set the 'P' channel mode! */
501 if (set && !(Client_OperByMe(Client)
502 || Client_Type(Client) == CLIENT_SERVER))
503 connected = IRC_WriteStrClient(Origin,
504 ERR_NOPRIVILEGES_MSG,
509 connected = IRC_WriteStrClient(Origin,
510 ERR_CHANOPRIVSNEEDED_MSG,
512 Channel_Name(Channel));
514 /* --- Channel user modes --- */
515 case 'o': /* Channel operator */
516 case 'v': /* Voice */
517 if (arg_arg > mode_arg) {
519 client = Client_Search(Req->argv[arg_arg]);
523 connected = IRC_WriteStrClient(Client,
528 connected = IRC_WriteStrClient(Origin,
529 ERR_CHANOPRIVSNEEDED_MSG,
531 Channel_Name(Channel));
533 Req->argv[arg_arg][0] = '\0';
536 connected = IRC_WriteStrClient(Origin,
537 ERR_NEEDMOREPARAMS_MSG,
538 Client_ID(Origin), Req->command);
542 /* --- Channel lists --- */
543 case 'I': /* Invite lists */
544 case 'b': /* Ban lists */
545 if (arg_arg > mode_arg) {
549 ? Add_Ban_Invite(*mode_ptr, Origin,
552 : Del_Ban_Invite(*mode_ptr, Origin,
556 connected = IRC_WriteStrClient(Origin,
557 ERR_CHANOPRIVSNEEDED_MSG,
559 Channel_Name(Channel));
561 Req->argv[arg_arg][0] = '\0';
564 if (*mode_ptr == 'I')
565 Channel_ShowInvites(Origin, Channel);
567 Channel_ShowBans(Origin, Channel);
572 "Unknown mode \"%c%c\" from \"%s\" on %s!?",
573 set ? '+' : '-', *mode_ptr, Client_ID(Origin),
574 Channel_Name(Channel));
575 if (Client_Type(Client) != CLIENT_SERVER)
576 connected = IRC_WriteStrClient(Origin,
577 ERR_UMODEUNKNOWNFLAG2_MSG,
579 set ? '+' : '-', *mode_ptr);
587 /* Is there a valid mode change? */
591 /* Validate target client */
592 if (client && (!Channel_IsMemberOf(Channel, client))) {
593 if (!IRC_WriteStrClient
594 (Origin, ERR_USERNOTINCHANNEL_MSG,
595 Client_ID(Origin), Client_ID(client),
596 Channel_Name(Channel)))
603 /* Channel-User-Mode */
605 ? Channel_UserModeAdd(Channel, client, x[0])
606 : Channel_UserModeDel(Channel, client, x[0]);
608 strlcat(the_args, " ", sizeof(the_args));
609 strlcat(the_args, Client_ID(client),
611 strlcat(the_modes, x, sizeof(the_modes));
613 ("User \"%s\": Mode change on %s, now \"%s\"",
614 Client_Mask(client), Channel_Name(Channel),
615 Channel_UserModes(Channel, client));
620 ? Channel_ModeAdd(Channel, x[0])
621 : Channel_ModeDel(Channel, x[0]);
623 strlcat(the_modes, x, sizeof(the_modes));
624 LogDebug("Channel %s: Mode change, now \"%s\".",
625 Channel_Name(Channel),
626 Channel_Modes(Channel));
630 /* Are there additional arguments to add? */
632 strlcat(the_args, " ", sizeof(the_args));
633 strlcat(the_args, argadd, sizeof(the_args));
638 /* Are there changed modes? */
640 /* Clean up mode string */
641 len = strlen(the_modes) - 1;
642 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
643 the_modes[len] = '\0';
645 if (Client_Type(Client) == CLIENT_SERVER) {
646 /* MODE requests for local channels from other servers
647 * are definitely invalid! */
648 if (Channel_IsLocal(Channel)) {
649 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
653 /* Forward mode changes to channel users and all the
654 * other remote servers: */
655 IRC_WriteStrServersPrefix(Client, Origin,
656 "MODE %s %s%s", Channel_Name(Channel),
657 the_modes, the_args);
658 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
659 false, "MODE %s %s%s", Channel_Name(Channel),
660 the_modes, the_args);
663 Origin = Client_ThisServer();
664 /* Send reply to client and inform other servers and channel users */
665 connected = IRC_WriteStrClientPrefix(Client, Origin,
666 "MODE %s %s%s", Channel_Name(Channel),
667 the_modes, the_args);
668 /* Only forward requests for non-local channels */
669 if (!Channel_IsLocal(Channel))
670 IRC_WriteStrServersPrefix(Client, Origin,
671 "MODE %s %s%s", Channel_Name(Channel),
672 the_modes, the_args);
673 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
674 false, "MODE %s %s%s", Channel_Name(Channel),
675 the_modes, the_args);
679 IRC_SetPenalty(Client, 1);
685 IRC_AWAY( CLIENT *Client, REQUEST *Req )
687 assert( Client != NULL );
688 assert( Req != NULL );
690 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
692 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
694 Client_SetAway( Client, Req->argv[0] );
695 Client_ModeAdd( Client, 'a' );
696 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
697 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
701 Client_ModeDel( Client, 'a' );
702 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
703 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
709 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
715 assert( Client != NULL );
716 assert( Channel != NULL );
717 assert( Pattern != NULL );
718 assert(what == 'I' || what == 'b');
720 mask = Lists_MakeMask(Pattern);
722 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
725 ret = Channel_AddInvite(Channel, mask, false);
727 ret = Channel_AddBan(Channel, mask);
731 if (already && (Client_Type(Prefix) == CLIENT_SERVER))
735 return Send_ListChange("+I", Prefix, Client, Channel, mask);
736 return Send_ListChange("+b", Prefix, Client, Channel, mask);
741 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
744 struct list_head *list;
746 assert( Client != NULL );
747 assert( Channel != NULL );
748 assert( Pattern != NULL );
749 assert(what == 'I' || what == 'b');
751 mask = Lists_MakeMask( Pattern );
754 list = Channel_GetListInvites(Channel);
756 list = Channel_GetListBans(Channel);
758 Lists_Del(list, mask);
760 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
761 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
766 Send_ListChange(const char *Mode, CLIENT *Prefix, CLIENT *Client,
767 CHANNEL *Channel, const char *Mask)
771 if( Client_Type( Client ) == CLIENT_USER )
773 /* send confirmation to client */
774 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
778 /* to other servers */
779 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
781 /* and local users in channel */
782 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
785 } /* Send_ListChange */