2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2005 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, ...)
17 static char UNUSED id[] = "$Id: irc-mode.c,v 1.52 2008/02/24 18:44:41 fw Exp $";
29 #include "irc-write.h"
41 static bool Client_Mode PARAMS(( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target ));
42 static bool Channel_Mode PARAMS(( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel ));
44 static bool Add_Invite PARAMS(( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern ));
45 static bool Add_Ban PARAMS(( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern ));
47 static bool Del_Invite PARAMS(( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern ));
48 static bool Del_Ban PARAMS(( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern ));
50 static bool Send_ListChange PARAMS(( char *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, 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 'o': /* IRC operator (only unsettable!) */
181 if(( ! set ) || ( Client_Type( Client ) == CLIENT_SERVER ))
183 Client_SetOperByMe( Target, false );
186 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
189 case 'r': /* Restricted (only settable) */
190 if(( set ) || ( Client_Type( Client ) == CLIENT_SERVER )) x[0] = 'r';
191 else ok = IRC_WriteStrClient( Origin, ERR_RESTRICTED_MSG, Client_ID( Origin ));
195 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\"!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ));
196 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
202 /* Is there a valid mode change? */
203 if( ! x[0] ) continue;
208 if( Client_ModeAdd( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
214 if( Client_ModeDel( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
219 /* Are there changed modes? */
222 /* Remoce needless action modifier characters */
223 len = strlen( the_modes ) - 1;
224 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' )) the_modes[len] = '\0';
226 if( Client_Type( Client ) == CLIENT_SERVER )
228 /* Forward modes to other servers */
229 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
233 /* Send reply to client and inform other servers */
234 ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
235 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
237 Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( Target ), Client_Modes( Target ));
240 IRC_SetPenalty( Client, 1 );
246 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
248 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
249 const char *mode_ptr;
251 /* Member or not? -- That's the question! */
252 if (!Channel_IsMemberOf(Channel, Origin))
253 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
254 Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
256 /* The sender is a member: generate extended reply */
257 strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
258 mode_ptr = the_modes;
264 snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
265 strlcat(the_args, argadd, sizeof(the_args));
268 strlcat(the_args, " ", sizeof(the_args));
269 strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
275 strlcat(the_modes, the_args, sizeof(the_modes));
277 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
278 Client_ID(Origin), Channel_Name(Channel), the_modes);
283 Channel_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel )
285 /* Handle channel and channel-user modes */
287 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2], argadd[CLIENT_PASS_LEN], *mode_ptr;
288 bool ok, set, modeok = true, skiponce, use_servermode = false;
289 int mode_arg, arg_arg;
294 /* Mode request: let's answer it :-) */
296 return Channel_Mode_Answer_Request(Origin, Channel);
298 /* Is the user allowed to change modes? */
299 if (Client_Type(Client) == CLIENT_USER) {
300 /* Is the originating user on that channel? */
301 if (!Channel_IsMemberOf(Channel, Origin))
302 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
303 Client_ID(Origin), Channel_Name(Channel));
305 /* channel operator? */
306 if (strchr(Channel_UserModes(Channel, Origin), 'o'))
308 else if(Conf_OperCanMode) {
309 /* IRC-Operators can use MODE as well */
310 if (Client_OperByMe(Origin)) {
312 if (Conf_OperServerMode)
313 use_servermode = true; /* Change Origin to Server */
319 mode_ptr = Req->argv[mode_arg];
320 if (Req->argc > mode_arg + 1)
321 arg_arg = mode_arg + 1;
325 /* Initial state: set or unset modes? */
328 case '-': set = false; break;
329 case '+': set = true; break;
335 /* Prepare reply string */
336 strcpy(the_modes, set ? "+" : "-");
345 /* Try next argument if there's any */
346 if (arg_arg > mode_arg)
351 if (mode_arg >= Req->argc)
353 mode_ptr = Req->argv[mode_arg];
355 if (Req->argc > mode_arg + 1)
356 arg_arg = mode_arg + 1;
365 if (((*mode_ptr == '+') && !set) || ((*mode_ptr == '-') && set)) {
366 /* Action modifier ("+"/"-") must be changed ... */
367 len = strlen( the_modes ) - 1;
368 if ((the_modes[len] == '+') || (the_modes[len] == '-')) {
369 /* Adjust last action modifier in result */
370 the_modes[len] = *mode_ptr;
372 /* Append modifier character to result string */
374 strlcat(the_modes, x, sizeof(the_modes));
376 set = *mode_ptr == '+';
381 /* Are there arguments left? */
382 if( arg_arg >= Req->argc ) arg_arg = -1;
390 /* --- Channel modes --- */
392 case 'i': /* Invite only */
393 case 'm': /* Moderated */
394 case 'n': /* Only members can write */
395 case 's': /* Secret channel */
396 case 't': /* Topic locked */
397 if( modeok ) x[0] = *mode_ptr;
398 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
401 case 'k': /* Channel key */
404 if( modeok ) x[0] = *mode_ptr;
405 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
408 if( arg_arg > mode_arg )
412 Channel_ModeDel( Channel, 'k' );
413 Channel_SetKey( Channel, Req->argv[arg_arg] );
414 strlcpy( argadd, Channel_Key( Channel ), sizeof( argadd ));
417 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
418 Req->argv[arg_arg][0] = '\0';
421 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
424 case 'l': /* Member limit */
427 if( modeok ) x[0] = *mode_ptr;
428 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
431 if( arg_arg > mode_arg )
435 l = atol( Req->argv[arg_arg] );
436 if( l > 0 && l < 0xFFFF )
438 Channel_ModeDel( Channel, 'l' );
439 Channel_SetMaxUsers( Channel, l );
440 snprintf( argadd, sizeof( argadd ), "%ld", l );
444 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
445 Req->argv[arg_arg][0] = '\0';
448 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
451 case 'P': /* Persistent channel */
453 /* Only IRC operators are allowed to
454 * set the 'P' channel mode! */
455 if (set && ! (Client_OperByMe(Client)
456 || Client_Type(Client) == CLIENT_SERVER)) {
457 ok = IRC_WriteStrClient(Origin,
458 ERR_NOPRIVILEGES_MSG,
463 ok = IRC_WriteStrClient(Origin,
464 ERR_CHANOPRIVSNEEDED_MSG,
466 Channel_Name(Channel));
469 /* --- Channel user modes --- */
471 case 'o': /* Channel operator */
472 case 'v': /* Voice */
473 if( arg_arg > mode_arg )
477 client = Client_Search( Req->argv[arg_arg] );
478 if( client ) x[0] = *mode_ptr;
479 else ok = IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[arg_arg] );
481 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
482 Req->argv[arg_arg][0] = '\0';
485 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
488 /* --- Channel lists --- */
490 case 'I': /* Invite lists */
491 if( arg_arg > mode_arg )
496 if( set ) Add_Invite( Origin, Client, Channel, Req->argv[arg_arg] );
497 else Del_Invite( Origin, Client, Channel, Req->argv[arg_arg] );
499 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
500 Req->argv[arg_arg][0] = '\0';
503 else Channel_ShowInvites( Origin, Channel );
506 case 'b': /* Ban lists */
507 if( arg_arg > mode_arg )
512 if( set ) Add_Ban( Origin, Client, Channel, Req->argv[arg_arg] );
513 else Del_Ban( Origin, Client, Channel, Req->argv[arg_arg] );
515 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
516 Req->argv[arg_arg][0] = '\0';
519 else Channel_ShowBans( Origin, Channel );
523 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\" on %s!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ), Channel_Name( Channel ));
524 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
530 /* Is there a valid mode change? */
531 if( ! x[0] ) continue;
533 /* Validate target client */
534 if( client && ( ! Channel_IsMemberOf( Channel, client )))
536 if( ! IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( client ), Channel_Name( Channel ))) break;
545 /* Channel-User-Mode */
546 if( Channel_UserModeAdd( Channel, client, x[0] ))
548 strlcat( the_args, " ", sizeof( the_args ));
549 strlcat( the_args, Client_ID( client ), sizeof( the_args ));
550 strlcat( the_modes, x, sizeof( the_modes ));
551 Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
557 if( Channel_ModeAdd( Channel, x[0] ))
559 strlcat( the_modes, x, sizeof( the_modes ));
560 Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
569 /* Channel-User-Mode */
570 if( Channel_UserModeDel( Channel, client, x[0] ))
572 strlcat( the_args, " ", sizeof( the_args ));
573 strlcat( the_args, Client_ID( client ), sizeof( the_args ));
574 strlcat( the_modes, x, sizeof( the_modes ));
575 Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
581 if( Channel_ModeDel( Channel, x[0] ))
583 strlcat( the_modes, x, sizeof( the_modes ));
584 Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
589 /* Are there additional arguments to add? */
592 strlcat( the_args, " ", sizeof( the_args ));
593 strlcat( the_args, argadd, sizeof( the_args ));
598 /* Are there changed modes? */
600 /* Clean up mode string */
601 len = strlen(the_modes) - 1;
602 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
603 the_modes[len] = '\0';
605 if (Client_Type(Client) == CLIENT_SERVER) {
606 /* Forward mode changes to channel users and other servers */
607 IRC_WriteStrServersPrefix(Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args);
608 IRC_WriteStrChannelPrefix(Client, Channel, Origin, false, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args);
611 Origin = Client_ThisServer();
612 /* Send reply to client and inform other servers and channel users */
613 ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
614 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
615 IRC_WriteStrChannelPrefix( Client, Channel, Origin, false, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
619 IRC_SetPenalty( Client, 1 );
625 IRC_AWAY( CLIENT *Client, REQUEST *Req )
627 assert( Client != NULL );
628 assert( Req != NULL );
630 /* Falsche Anzahl Parameter? */
631 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
633 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
636 Client_SetAway( Client, Req->argv[0] );
637 Client_ModeAdd( Client, 'a' );
638 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
639 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
644 Client_ModeDel( Client, 'a' );
645 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
646 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
652 Add_Invite( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern )
657 assert( Client != NULL );
658 assert( Channel != NULL );
659 assert( Pattern != NULL );
661 mask = Lists_MakeMask( Pattern );
663 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask );
665 if( ! Channel_AddInvite(Channel, mask, false ))
668 if ( already && ( Client_Type( Prefix ) == CLIENT_SERVER ))
671 return Send_ListChange( "+I", Prefix, Client, Channel, mask );
676 Add_Ban( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern )
681 assert( Client != NULL );
682 assert( Channel != NULL );
683 assert( Pattern != NULL );
685 mask = Lists_MakeMask( Pattern );
687 already = Lists_CheckDupeMask(Channel_GetListBans(Channel), mask );
689 if( ! Channel_AddBan(Channel, mask))
692 if ( already && ( Client_Type( Prefix ) == CLIENT_SERVER ))
695 return Send_ListChange( "+b", Prefix, Client, Channel, mask );
700 Del_Invite( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern )
704 assert( Client != NULL );
705 assert( Channel != NULL );
706 assert( Pattern != NULL );
708 mask = Lists_MakeMask( Pattern );
709 Lists_Del(Channel_GetListInvites(Channel), mask);
710 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
715 Del_Ban( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern )
719 assert( Client != NULL );
720 assert( Channel != NULL );
721 assert( Pattern != NULL );
723 mask = Lists_MakeMask( Pattern );
724 Lists_Del(Channel_GetListBans(Channel), mask);
725 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
730 Send_ListChange( char *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Mask )
732 /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
736 if( Client_Type( Client ) == CLIENT_USER )
738 /* Bestaetigung an Client */
739 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
743 /* an andere Server */
744 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
746 /* und lokale User im Channel */
747 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
750 } /* Send_ListChange */