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.24.2.1 2003/01/02 18:03: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 */
145 x[0] = *mode_ptr; strcat( the_modes, x );
147 if( *mode_ptr == '+' ) set = TRUE;
159 if( Client_Type( Client ) == CLIENT_SERVER )
162 Client_SetAway( Client, DEFAULT_AWAY_MSG );
164 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
171 /* IRC operator (only unsetable!) */
172 if(( ! set ) || ( Client_Type( Client ) == CLIENT_SERVER ))
174 Client_SetOperByMe( Target, FALSE );
177 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
180 /* Restricted (only setable) */
181 if(( set ) || ( Client_Type( Client ) == CLIENT_SERVER )) x[0] = 'r';
182 else ok = IRC_WriteStrClient( Origin, ERR_RESTRICTED_MSG, Client_ID( Origin ));
185 /* Server messages */
189 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\"!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ));
190 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
196 /* Is there a valid mode change? */
197 if( ! x[0] ) continue;
202 if( Client_ModeAdd( Target, x[0] )) strcat( the_modes, x );
208 if( Client_ModeDel( Target, x[0] )) strcat( the_modes, x );
213 /* Are there changed modes? */
216 /* Remoce needless action modifier characters */
217 if(( the_modes[strlen( the_modes ) - 1] == '+' ) || ( the_modes[strlen( the_modes ) - 1] == '-' )) the_modes[strlen( the_modes ) - 1] = '\0';
219 if( Client_Type( Client ) == CLIENT_SERVER )
221 /* Forward modes to other servers */
222 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
226 /* Send reply to client and inform other servers */
227 ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s %s", Client_ID( Target ), the_modes );
228 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
230 Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( Target ), Client_Modes( Target ));
238 Channel_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel )
240 /* Handle channel and channel-user modes */
242 CHAR the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2], argadd[CLIENT_PASS_LEN], *mode_ptr;
243 BOOLEAN ok, set, modeok, skiponce;
244 INT mode_arg, arg_arg;
248 /* Mode request: let's answer it :-) */
249 if( Req->argc == 1 ) return IRC_WriteStrClient( Origin, RPL_CHANNELMODEIS_MSG, Client_ID( Origin ), Channel_Name( Channel ), Channel_Modes( Channel ));
251 /* Is the user allowed to change modes? */
252 if( Client_Type( Client ) == CLIENT_USER )
254 if( strchr( Channel_UserModes( Channel, Origin ), 'o' )) modeok = TRUE;
256 if( Conf_OperCanMode )
258 /* auch IRC-Operatoren duerfen MODE verwenden */
259 if( Client_OperByMe( Origin )) modeok = TRUE;
265 mode_ptr = Req->argv[mode_arg];
266 if( Req->argc > mode_arg + 1 ) arg_arg = mode_arg + 1;
269 /* Initial state: set or unset modes? */
271 if( *mode_ptr == '-' ) set = FALSE;
272 else if( *mode_ptr == '+' ) set = TRUE;
273 else set = skiponce = TRUE;
275 /* Prepare reply string */
276 if( set ) strcpy( the_modes, "+" );
277 else strcpy( the_modes, "-" );
278 strcpy( the_args, " " );
284 if( ! skiponce ) mode_ptr++;
287 /* Try next argument if there's any */
288 if( arg_arg > mode_arg ) mode_arg = arg_arg;
290 if( mode_arg < Req->argc ) mode_ptr = Req->argv[mode_arg];
292 if( Req->argc > mode_arg + 1 ) arg_arg = mode_arg + 1;
301 if((( *mode_ptr == '+' ) && ( ! set )) || (( *mode_ptr == '-' ) && ( set )))
303 /* Action modifier ("+"/"-") must be changed ... */
304 if(( the_modes[strlen( the_modes ) - 1] == '+' ) || ( the_modes[strlen( the_modes ) - 1] == '-' ))
306 /* Adjust last action modifier in result */
307 the_modes[strlen( the_modes ) - 1] = *mode_ptr;
311 /* Append modifier character to result string */
312 x[0] = *mode_ptr; strcat( the_modes, x );
314 if( *mode_ptr == '+' ) set = TRUE;
320 /* Are there arguments left? */
321 if( arg_arg >= Req->argc ) arg_arg = -1;
332 if( modeok ) x[0] = 'i';
333 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
337 if( modeok ) x[0] = 'm';
338 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
341 /* kein Schreiben in den Channel von aussen */
342 if( modeok ) x[0] = 'n';
343 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
347 if( modeok ) x[0] = 't';
348 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
351 /* Persistent channel */
354 if( set && ( ! Client_OperByMe( Client )))
356 /* Only IRC operators are allowed to set P mode */
357 ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
361 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
364 /* Channel user modes */
366 /* Channel operator */
369 if( arg_arg > mode_arg )
373 client = Client_Search( Req->argv[arg_arg] );
374 if( client ) x[0] = *mode_ptr;
375 else ok = IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[arg_arg] );
377 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
378 Req->argv[arg_arg][0] = '\0';
381 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
387 if( modeok ) x[0] = *mode_ptr;
388 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
391 if( arg_arg > mode_arg )
395 Channel_ModeDel( Channel, 'k' );
396 Channel_SetKey( Channel, Req->argv[arg_arg] );
397 strcpy( argadd, Channel_Key( Channel ));
400 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
401 Req->argv[arg_arg][0] = '\0';
404 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
410 if( modeok ) x[0] = *mode_ptr;
411 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
414 if( arg_arg > mode_arg )
418 l = atol( Req->argv[arg_arg] );
419 if( l > 0 && l < 0xFFFF )
421 Channel_ModeDel( Channel, 'l' );
422 Channel_SetMaxUsers( Channel, l );
423 sprintf( argadd, "%ld", l );
427 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
428 Req->argv[arg_arg][0] = '\0';
431 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
437 if( arg_arg > mode_arg )
442 if( set ) Add_Invite( Origin, Client, Channel, Req->argv[arg_arg] );
443 else Del_Invite( Origin, Client, Channel, Req->argv[arg_arg] );
445 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
446 Req->argv[arg_arg][0] = '\0';
449 else Lists_ShowInvites( Origin, Channel );
453 if( arg_arg > mode_arg )
458 if( set ) Add_Ban( Origin, Client, Channel, Req->argv[arg_arg] );
459 else Del_Ban( Origin, Client, Channel, Req->argv[arg_arg] );
461 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
462 Req->argv[arg_arg][0] = '\0';
465 else Lists_ShowBans( Origin, Channel );
469 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\" on %s!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ), Channel_Name( Channel ));
470 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
476 /* Is there a valid mode change? */
477 if( ! x[0] ) continue;
484 /* Channel-User-Mode */
485 if( Channel_UserModeAdd( Channel, client, x[0] ))
487 strcat( the_args, Client_ID( client ));
488 strcat( the_args, " " ); strcat( the_modes, x );
489 Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
495 if( Channel_ModeAdd( Channel, x[0] ))
497 strcat( the_modes, x );
498 Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
507 /* Channel-User-Mode */
508 if( Channel_UserModeDel( Channel, client, x[0] ))
510 strcat( the_args, Client_ID( client ));
511 strcat( the_args, " " ); strcat( the_modes, x );
512 Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
518 if( Channel_ModeDel( Channel, x[0] ))
520 strcat( the_modes, x );
521 Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
526 /* Are there additional arguments to add? */
529 if( the_args[strlen( the_args ) - 1] != ' ' ) strcat( the_args, " " );
530 strcat( the_args, argadd );
535 /* Are there changed modes? */
538 /* Clean up mode string */
539 if(( the_modes[strlen( the_modes ) - 1] == '+' ) || ( the_modes[strlen( the_modes ) - 1] == '-' )) the_modes[strlen( the_modes ) - 1] = '\0';
541 /* Clean up argument string if there are none */
542 if( ! the_args[1] ) the_args[0] = '\0';
544 if( Client_Type( Client ) == CLIENT_SERVER )
546 /* Forward mode changes to channel users and other servers */
547 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
548 IRC_WriteStrChannelPrefix( Client, Channel, Origin, FALSE, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
552 /* Send reply to client and inform other servers and channel users */
553 ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
554 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
555 IRC_WriteStrChannelPrefix( Client, Channel, Origin, FALSE, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
564 IRC_AWAY( CLIENT *Client, REQUEST *Req )
566 assert( Client != NULL );
567 assert( Req != NULL );
569 /* Falsche Anzahl Parameter? */
570 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
572 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
575 Client_SetAway( Client, Req->argv[0] );
576 Client_ModeAdd( Client, 'a' );
577 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
578 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
583 Client_ModeDel( Client, 'a' );
584 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
585 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
591 Add_Invite( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
595 assert( Client != NULL );
596 assert( Channel != NULL );
597 assert( Pattern != NULL );
599 mask = Lists_MakeMask( Pattern );
601 if( ! Lists_AddInvited( Prefix, mask, Channel, FALSE )) return CONNECTED;
602 return Send_ListChange( "+I", Prefix, Client, Channel, mask );
607 Add_Ban( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
611 assert( Client != NULL );
612 assert( Channel != NULL );
613 assert( Pattern != NULL );
615 mask = Lists_MakeMask( Pattern );
617 if( ! Lists_AddBanned( Prefix, mask, Channel )) return CONNECTED;
618 return Send_ListChange( "+b", Prefix, Client, Channel, mask );
623 Del_Invite( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
627 assert( Client != NULL );
628 assert( Channel != NULL );
629 assert( Pattern != NULL );
631 mask = Lists_MakeMask( Pattern );
632 Lists_DelInvited( mask, Channel );
633 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
638 Del_Ban( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
642 assert( Client != NULL );
643 assert( Channel != NULL );
644 assert( Pattern != NULL );
646 mask = Lists_MakeMask( Pattern );
647 Lists_DelBanned( mask, Channel );
648 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
653 Send_ListChange( CHAR *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Mask )
655 /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
659 if( Client_Type( Client ) == CLIENT_USER )
661 /* Bestaetigung an Client */
662 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
666 /* an andere Server */
667 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
669 /* und lokale User im Channel */
670 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, FALSE, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
673 } /* Send_ListChange */