2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by 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.35 2004/04/25 15:42:05 alex Exp $";
29 #include "irc-write.h"
41 LOCAL BOOLEAN Client_Mode PARAMS(( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target ));
42 LOCAL BOOLEAN Channel_Mode PARAMS(( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel ));
44 LOCAL BOOLEAN Add_Invite PARAMS(( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern ));
45 LOCAL BOOLEAN Add_Ban PARAMS(( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern ));
47 LOCAL BOOLEAN Del_Invite PARAMS(( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern ));
48 LOCAL BOOLEAN Del_Ban PARAMS(( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern ));
50 LOCAL BOOLEAN 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? */
75 if( Client_IsValidNick( Req->argv[0] )) cl = Client_Search( Req->argv[0] );
76 if( Channel_IsValidName( Req->argv[0] )) chan = Channel_Search( Req->argv[0] );
78 if( cl ) return Client_Mode( Client, Req, origin, cl );
79 if( chan ) return Channel_Mode( Client, Req, origin, chan );
81 /* No target found! */
82 return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[0] );
87 Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target )
89 /* Handle client mode requests */
91 CHAR the_modes[COMMAND_LEN], x[2], *mode_ptr;
95 /* Is the client allowed to request or change the modes? */
96 if( Client_Type( Client ) == CLIENT_USER )
98 /* Users are only allowed to manipulate their own modes! */
99 if( Target != Client ) return IRC_WriteStrClient( Client, ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
102 /* Mode request: let's answer it :-) */
103 if( Req->argc == 1 ) return IRC_WriteStrClient( Origin, RPL_UMODEIS_MSG, Client_ID( Origin ), Client_Modes( Target ));
106 mode_ptr = Req->argv[mode_arg];
108 /* Initial state: set or unset modes? */
109 if( *mode_ptr == '+' ) set = TRUE;
110 else if( *mode_ptr == '-' ) set = FALSE;
111 else return IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Origin ));
113 /* Prepare reply string */
114 if( set ) strcpy( the_modes, "+" );
115 else strcpy( the_modes, "-" );
124 /* Try next argument if there's any */
126 if( mode_arg < Req->argc ) mode_ptr = Req->argv[mode_arg];
134 if((( *mode_ptr == '+' ) && ( ! set )) || (( *mode_ptr == '-' ) && ( set )))
136 /* Action modifier ("+"/"-") must be changed ... */
137 if(( the_modes[strlen( the_modes ) - 1] == '+' ) || ( the_modes[strlen( the_modes ) - 1] == '-' ))
139 /* Adjust last action modifier in result */
140 the_modes[strlen( the_modes ) - 1] = *mode_ptr;
144 /* Append modifier character to result string */
146 strlcat( the_modes, x, sizeof( the_modes ));
148 if( *mode_ptr == '+' ) set = TRUE;
160 if( Client_Type( Client ) == CLIENT_SERVER )
163 Client_SetAway( Client, DEFAULT_AWAY_MSG );
165 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
172 /* IRC operator (only unsetable!) */
173 if(( ! set ) || ( Client_Type( Client ) == CLIENT_SERVER ))
175 Client_SetOperByMe( Target, FALSE );
178 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
181 /* Restricted (only setable) */
182 if(( set ) || ( Client_Type( Client ) == CLIENT_SERVER )) x[0] = 'r';
183 else ok = IRC_WriteStrClient( Origin, ERR_RESTRICTED_MSG, Client_ID( Origin ));
186 /* Server messages */
190 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\"!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ));
191 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
197 /* Is there a valid mode change? */
198 if( ! x[0] ) continue;
203 if( Client_ModeAdd( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
209 if( Client_ModeDel( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
214 /* Are there changed modes? */
217 /* Remoce needless action modifier characters */
218 if(( the_modes[strlen( the_modes ) - 1] == '+' ) || ( the_modes[strlen( the_modes ) - 1] == '-' )) the_modes[strlen( the_modes ) - 1] = '\0';
220 if( Client_Type( Client ) == CLIENT_SERVER )
222 /* Forward modes to other servers */
223 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
227 /* Send reply to client and inform other servers */
228 ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
229 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
231 Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( Target ), Client_Modes( Target ));
234 IRC_SetPenalty( Client, 1 );
240 Channel_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel )
242 /* Handle channel and channel-user modes */
244 CHAR the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2], argadd[CLIENT_PASS_LEN], *mode_ptr;
245 BOOLEAN ok, set, modeok, skiponce;
246 INT mode_arg, arg_arg;
250 /* Mode request: let's answer it :-) */
253 /* Member or not? -- That's the question! */
254 if( ! Channel_IsMemberOf( Channel, Origin )) return IRC_WriteStrClient( Origin, RPL_CHANNELMODEIS_MSG, 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;
259 strcpy( the_args, "" );
265 snprintf( argadd, sizeof( argadd ), " %ld", Channel_MaxUsers( Channel ));
266 strlcat( the_args, argadd, sizeof( the_args ));
269 strlcat( the_args, " ", sizeof( the_args ));
270 strlcat( the_args, Channel_Key( Channel ), sizeof( the_args ));
275 if( the_args[0] ) strlcat( the_modes, the_args, sizeof( the_modes ));
277 return IRC_WriteStrClient( Origin, RPL_CHANNELMODEIS_MSG, Client_ID( Origin ), Channel_Name( Channel ), the_modes );
280 /* Is the user allowed to change modes? */
281 if( Client_Type( Client ) == CLIENT_USER )
283 /* Is the originating user on that channel? */
284 if( ! Channel_IsMemberOf( Channel, Origin )) return IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Channel_Name( Channel ));
286 /* Is he channel operator? */
287 if( strchr( Channel_UserModes( Channel, Origin ), 'o' )) modeok = TRUE;
289 if( Conf_OperCanMode )
291 /* auch IRC-Operatoren duerfen MODE verwenden */
292 if( Client_OperByMe( Origin )) modeok = TRUE;
298 mode_ptr = Req->argv[mode_arg];
299 if( Req->argc > mode_arg + 1 ) arg_arg = mode_arg + 1;
302 /* Initial state: set or unset modes? */
304 if( *mode_ptr == '-' ) set = FALSE;
305 else if( *mode_ptr == '+' ) set = TRUE;
306 else set = skiponce = TRUE;
308 /* Prepare reply string */
309 if( set ) strcpy( the_modes, "+" );
310 else strcpy( the_modes, "-" );
311 strcpy( the_args, " " );
317 if( ! skiponce ) mode_ptr++;
320 /* Try next argument if there's any */
321 if( arg_arg > mode_arg ) mode_arg = arg_arg;
323 if( mode_arg < Req->argc ) mode_ptr = Req->argv[mode_arg];
325 if( Req->argc > mode_arg + 1 ) arg_arg = mode_arg + 1;
334 if((( *mode_ptr == '+' ) && ( ! set )) || (( *mode_ptr == '-' ) && ( set )))
336 /* Action modifier ("+"/"-") must be changed ... */
337 if(( the_modes[strlen( the_modes ) - 1] == '+' ) || ( the_modes[strlen( the_modes ) - 1] == '-' ))
339 /* Adjust last action modifier in result */
340 the_modes[strlen( the_modes ) - 1] = *mode_ptr;
344 /* Append modifier character to result string */
346 strlcat( the_modes, x, sizeof( the_modes ));
348 if( *mode_ptr == '+' ) set = TRUE;
354 /* Are there arguments left? */
355 if( arg_arg >= Req->argc ) arg_arg = -1;
366 if( modeok ) x[0] = 'i';
367 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
371 if( modeok ) x[0] = 'm';
372 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
375 /* kein Schreiben in den Channel von aussen */
376 if( modeok ) x[0] = 'n';
377 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
381 if( modeok ) x[0] = 't';
382 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
385 /* Persistent channel */
388 if( set && ( ! Client_OperByMe( Client )))
390 /* Only IRC operators are allowed to set P mode */
391 ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
395 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
398 /* Channel user modes */
400 /* Channel operator */
403 if( arg_arg > mode_arg )
407 client = Client_Search( Req->argv[arg_arg] );
408 if( client ) x[0] = *mode_ptr;
409 else ok = IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[arg_arg] );
411 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
412 Req->argv[arg_arg][0] = '\0';
415 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
421 if( modeok ) x[0] = *mode_ptr;
422 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
425 if( arg_arg > mode_arg )
429 Channel_ModeDel( Channel, 'k' );
430 Channel_SetKey( Channel, Req->argv[arg_arg] );
431 strlcpy( argadd, Channel_Key( Channel ), sizeof( argadd ));
434 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
435 Req->argv[arg_arg][0] = '\0';
438 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
444 if( modeok ) x[0] = *mode_ptr;
445 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
448 if( arg_arg > mode_arg )
452 l = atol( Req->argv[arg_arg] );
453 if( l > 0 && l < 0xFFFF )
455 Channel_ModeDel( Channel, 'l' );
456 Channel_SetMaxUsers( Channel, l );
457 snprintf( argadd, sizeof( argadd ), "%ld", l );
461 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
462 Req->argv[arg_arg][0] = '\0';
465 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
471 if( arg_arg > mode_arg )
476 if( set ) Add_Invite( Origin, Client, Channel, Req->argv[arg_arg] );
477 else Del_Invite( Origin, Client, Channel, Req->argv[arg_arg] );
479 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
480 Req->argv[arg_arg][0] = '\0';
483 else Lists_ShowInvites( Origin, Channel );
487 if( arg_arg > mode_arg )
492 if( set ) Add_Ban( Origin, Client, Channel, Req->argv[arg_arg] );
493 else Del_Ban( Origin, Client, Channel, Req->argv[arg_arg] );
495 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
496 Req->argv[arg_arg][0] = '\0';
499 else Lists_ShowBans( Origin, Channel );
503 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\" on %s!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ), Channel_Name( Channel ));
504 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
510 /* Is there a valid mode change? */
511 if( ! x[0] ) continue;
513 /* Validate target client */
514 if( client && ( ! Channel_IsMemberOf( Channel, client )))
516 if( ! IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( client ), Channel_Name( Channel ))) break;
525 /* Channel-User-Mode */
526 if( Channel_UserModeAdd( Channel, client, x[0] ))
528 strlcat( the_args, Client_ID( client ), sizeof( the_args ));
529 strlcat( the_args, " ", sizeof( the_args ));
530 strlcat( the_modes, x, sizeof( the_modes ));
531 Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
537 if( Channel_ModeAdd( Channel, x[0] ))
539 strlcat( the_modes, x, sizeof( the_modes ));
540 Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
549 /* Channel-User-Mode */
550 if( Channel_UserModeDel( Channel, client, x[0] ))
552 strlcat( the_args, Client_ID( client ), sizeof( the_args ));
553 strlcat( the_args, " ", sizeof( the_args ));
554 strlcat( the_modes, x, sizeof( the_modes ));
555 Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
561 if( Channel_ModeDel( Channel, x[0] ))
563 strlcat( the_modes, x, sizeof( the_modes ));
564 Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
569 /* Are there additional arguments to add? */
572 if( the_args[strlen( the_args ) - 1] != ' ' ) strlcat( the_args, " ", sizeof( the_args ));
573 strlcat( the_args, argadd, sizeof( the_args ));
578 /* Are there changed modes? */
581 /* Clean up mode string */
582 if(( the_modes[strlen( the_modes ) - 1] == '+' ) || ( the_modes[strlen( the_modes ) - 1] == '-' )) the_modes[strlen( the_modes ) - 1] = '\0';
584 /* Clean up argument string if there are none */
585 if( ! the_args[1] ) the_args[0] = '\0';
587 if( Client_Type( Client ) == CLIENT_SERVER )
589 /* Forward mode changes to channel users and other servers */
590 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
591 IRC_WriteStrChannelPrefix( Client, Channel, Origin, FALSE, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
595 /* Send reply to client and inform other servers and channel users */
596 ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
597 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
598 IRC_WriteStrChannelPrefix( Client, Channel, Origin, FALSE, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
602 IRC_SetPenalty( Client, 1 );
608 IRC_AWAY( CLIENT *Client, REQUEST *Req )
610 assert( Client != NULL );
611 assert( Req != NULL );
613 /* Falsche Anzahl Parameter? */
614 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
616 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
619 Client_SetAway( Client, Req->argv[0] );
620 Client_ModeAdd( Client, 'a' );
621 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
622 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
627 Client_ModeDel( Client, 'a' );
628 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
629 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
635 Add_Invite( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
640 assert( Client != NULL );
641 assert( Channel != NULL );
642 assert( Pattern != NULL );
644 mask = Lists_MakeMask( Pattern );
646 already = Lists_IsInviteEntry( mask, Channel );
648 if( ! Lists_AddInvited( mask, Channel, FALSE )) return CONNECTED;
650 if(( Client_Type( Prefix ) == CLIENT_SERVER ) && ( already == TRUE )) return CONNECTED;
652 return Send_ListChange( "+I", Prefix, Client, Channel, mask );
657 Add_Ban( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
662 assert( Client != NULL );
663 assert( Channel != NULL );
664 assert( Pattern != NULL );
666 mask = Lists_MakeMask( Pattern );
668 already = Lists_IsBanEntry( mask, Channel );
670 if( ! Lists_AddBanned( mask, Channel )) return CONNECTED;
672 if(( Client_Type( Prefix ) == CLIENT_SERVER ) && ( already == TRUE )) return CONNECTED;
674 return Send_ListChange( "+b", Prefix, Client, Channel, mask );
679 Del_Invite( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
683 assert( Client != NULL );
684 assert( Channel != NULL );
685 assert( Pattern != NULL );
687 mask = Lists_MakeMask( Pattern );
688 Lists_DelInvited( mask, Channel );
689 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
694 Del_Ban( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
698 assert( Client != NULL );
699 assert( Channel != NULL );
700 assert( Pattern != NULL );
702 mask = Lists_MakeMask( Pattern );
703 Lists_DelBanned( mask, Channel );
704 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
709 Send_ListChange( CHAR *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Mask )
711 /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
715 if( Client_Type( Client ) == CLIENT_USER )
717 /* Bestaetigung an Client */
718 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
722 /* an andere Server */
723 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
725 /* und lokale User im Channel */
726 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, FALSE, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
729 } /* Send_ListChange */