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.36 2005/02/27 20:09:44 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;
158 case 'i': /* Invisible */
159 case 's': /* Server messages */
164 if( Client_Type( Client ) == CLIENT_SERVER )
167 Client_SetAway( Client, DEFAULT_AWAY_MSG );
169 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
172 case 'o': /* IRC operator (only unsettable!) */
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 case 'r': /* Restricted (only settable) */
182 if(( set ) || ( Client_Type( Client ) == CLIENT_SERVER )) x[0] = 'r';
183 else ok = IRC_WriteStrClient( Origin, ERR_RESTRICTED_MSG, Client_ID( Origin ));
187 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\"!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ));
188 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
194 /* Is there a valid mode change? */
195 if( ! x[0] ) continue;
200 if( Client_ModeAdd( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
206 if( Client_ModeDel( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
211 /* Are there changed modes? */
214 /* Remoce needless action modifier characters */
215 if(( the_modes[strlen( the_modes ) - 1] == '+' ) || ( the_modes[strlen( the_modes ) - 1] == '-' )) the_modes[strlen( the_modes ) - 1] = '\0';
217 if( Client_Type( Client ) == CLIENT_SERVER )
219 /* Forward modes to other servers */
220 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
224 /* Send reply to client and inform other servers */
225 ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
226 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
228 Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( Target ), Client_Modes( Target ));
231 IRC_SetPenalty( Client, 1 );
237 Channel_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel )
239 /* Handle channel and channel-user modes */
241 CHAR the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2], argadd[CLIENT_PASS_LEN], *mode_ptr;
242 BOOLEAN ok, set, modeok, skiponce;
243 INT mode_arg, arg_arg;
247 /* Mode request: let's answer it :-) */
250 /* Member or not? -- That's the question! */
251 if( ! Channel_IsMemberOf( Channel, Origin )) return IRC_WriteStrClient( Origin, RPL_CHANNELMODEIS_MSG, Client_ID( Origin ), Channel_Name( Channel ), Channel_Modes( Channel ));
253 /* The sender is a member: generate extended reply */
254 strlcpy( the_modes, Channel_Modes( Channel ), sizeof( the_modes ));
255 mode_ptr = the_modes;
256 strcpy( the_args, "" );
262 snprintf( argadd, sizeof( argadd ), " %ld", Channel_MaxUsers( Channel ));
263 strlcat( the_args, argadd, sizeof( the_args ));
266 strlcat( the_args, " ", sizeof( the_args ));
267 strlcat( the_args, Channel_Key( Channel ), sizeof( the_args ));
272 if( the_args[0] ) strlcat( the_modes, the_args, sizeof( the_modes ));
274 return IRC_WriteStrClient( Origin, RPL_CHANNELMODEIS_MSG, Client_ID( Origin ), Channel_Name( Channel ), the_modes );
277 /* Is the user allowed to change modes? */
278 if( Client_Type( Client ) == CLIENT_USER )
280 /* Is the originating user on that channel? */
281 if( ! Channel_IsMemberOf( Channel, Origin )) return IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Channel_Name( Channel ));
283 /* Is he channel operator? */
284 if( strchr( Channel_UserModes( Channel, Origin ), 'o' )) modeok = TRUE;
286 if( Conf_OperCanMode )
288 /* auch IRC-Operatoren duerfen MODE verwenden */
289 if( Client_OperByMe( Origin )) modeok = TRUE;
295 mode_ptr = Req->argv[mode_arg];
296 if( Req->argc > mode_arg + 1 ) arg_arg = mode_arg + 1;
299 /* Initial state: set or unset modes? */
301 if( *mode_ptr == '-' ) set = FALSE;
302 else if( *mode_ptr == '+' ) set = TRUE;
303 else set = skiponce = TRUE;
305 /* Prepare reply string */
306 if( set ) strcpy( the_modes, "+" );
307 else strcpy( the_modes, "-" );
308 strcpy( the_args, " " );
314 if( ! skiponce ) mode_ptr++;
317 /* Try next argument if there's any */
318 if( arg_arg > mode_arg ) mode_arg = arg_arg;
320 if( mode_arg < Req->argc ) mode_ptr = Req->argv[mode_arg];
322 if( Req->argc > mode_arg + 1 ) arg_arg = mode_arg + 1;
331 if((( *mode_ptr == '+' ) && ( ! set )) || (( *mode_ptr == '-' ) && ( set )))
333 /* Action modifier ("+"/"-") must be changed ... */
334 if(( the_modes[strlen( the_modes ) - 1] == '+' ) || ( the_modes[strlen( the_modes ) - 1] == '-' ))
336 /* Adjust last action modifier in result */
337 the_modes[strlen( the_modes ) - 1] = *mode_ptr;
341 /* Append modifier character to result string */
343 strlcat( the_modes, x, sizeof( the_modes ));
345 if( *mode_ptr == '+' ) set = TRUE;
351 /* Are there arguments left? */
352 if( arg_arg >= Req->argc ) arg_arg = -1;
360 /* --- Channel modes --- */
362 case 'i': /* Invite only */
363 case 'm': /* Moderated */
364 case 'n': /* Only members can write */
365 case 't': /* Topic locked */
366 if( modeok ) x[0] = *mode_ptr;
367 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
370 case 'k': /* Channel key */
373 if( modeok ) x[0] = *mode_ptr;
374 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
377 if( arg_arg > mode_arg )
381 Channel_ModeDel( Channel, 'k' );
382 Channel_SetKey( Channel, Req->argv[arg_arg] );
383 strlcpy( argadd, Channel_Key( Channel ), sizeof( argadd ));
386 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
387 Req->argv[arg_arg][0] = '\0';
390 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
393 case 'l': /* Member limit */
396 if( modeok ) x[0] = *mode_ptr;
397 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
400 if( arg_arg > mode_arg )
404 l = atol( Req->argv[arg_arg] );
405 if( l > 0 && l < 0xFFFF )
407 Channel_ModeDel( Channel, 'l' );
408 Channel_SetMaxUsers( Channel, l );
409 snprintf( argadd, sizeof( argadd ), "%ld", l );
413 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
414 Req->argv[arg_arg][0] = '\0';
417 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
420 case 'P': /* Persistent channel */
423 if( set && ( ! Client_OperByMe( Client )))
425 /* Only IRC operators are allowed to set P mode */
426 ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
430 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
433 /* --- Channel user modes --- */
435 case 'o': /* Channel operator */
436 case 'v': /* Voice */
437 if( arg_arg > mode_arg )
441 client = Client_Search( Req->argv[arg_arg] );
442 if( client ) x[0] = *mode_ptr;
443 else ok = IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), 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 ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
452 /* --- Channel lists --- */
454 case 'I': /* Invite lists */
455 if( arg_arg > mode_arg )
460 if( set ) Add_Invite( Origin, Client, Channel, Req->argv[arg_arg] );
461 else Del_Invite( Origin, Client, Channel, Req->argv[arg_arg] );
463 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
464 Req->argv[arg_arg][0] = '\0';
467 else Lists_ShowInvites( Origin, Channel );
470 case 'b': /* Ban lists */
471 if( arg_arg > mode_arg )
476 if( set ) Add_Ban( Origin, Client, Channel, Req->argv[arg_arg] );
477 else Del_Ban( 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_ShowBans( Origin, Channel );
487 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\" on %s!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ), Channel_Name( Channel ));
488 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
494 /* Is there a valid mode change? */
495 if( ! x[0] ) continue;
497 /* Validate target client */
498 if( client && ( ! Channel_IsMemberOf( Channel, client )))
500 if( ! IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( client ), Channel_Name( Channel ))) break;
509 /* Channel-User-Mode */
510 if( Channel_UserModeAdd( Channel, client, x[0] ))
512 strlcat( the_args, Client_ID( client ), sizeof( the_args ));
513 strlcat( the_args, " ", sizeof( the_args ));
514 strlcat( the_modes, x, sizeof( the_modes ));
515 Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
521 if( Channel_ModeAdd( Channel, x[0] ))
523 strlcat( the_modes, x, sizeof( the_modes ));
524 Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
533 /* Channel-User-Mode */
534 if( Channel_UserModeDel( Channel, client, x[0] ))
536 strlcat( the_args, Client_ID( client ), sizeof( the_args ));
537 strlcat( the_args, " ", sizeof( the_args ));
538 strlcat( the_modes, x, sizeof( the_modes ));
539 Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
545 if( Channel_ModeDel( Channel, x[0] ))
547 strlcat( the_modes, x, sizeof( the_modes ));
548 Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
553 /* Are there additional arguments to add? */
556 if( the_args[strlen( the_args ) - 1] != ' ' ) strlcat( the_args, " ", sizeof( the_args ));
557 strlcat( the_args, argadd, sizeof( the_args ));
562 /* Are there changed modes? */
565 /* Clean up mode string */
566 if(( the_modes[strlen( the_modes ) - 1] == '+' ) || ( the_modes[strlen( the_modes ) - 1] == '-' )) the_modes[strlen( the_modes ) - 1] = '\0';
568 /* Clean up argument string if there are none */
569 if( ! the_args[1] ) the_args[0] = '\0';
571 if( Client_Type( Client ) == CLIENT_SERVER )
573 /* Forward mode changes to channel users and other servers */
574 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
575 IRC_WriteStrChannelPrefix( Client, Channel, Origin, FALSE, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
579 /* Send reply to client and inform other servers and channel users */
580 ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
581 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
582 IRC_WriteStrChannelPrefix( Client, Channel, Origin, FALSE, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
586 IRC_SetPenalty( Client, 1 );
592 IRC_AWAY( CLIENT *Client, REQUEST *Req )
594 assert( Client != NULL );
595 assert( Req != NULL );
597 /* Falsche Anzahl Parameter? */
598 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
600 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
603 Client_SetAway( Client, Req->argv[0] );
604 Client_ModeAdd( Client, 'a' );
605 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
606 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
611 Client_ModeDel( Client, 'a' );
612 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
613 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
619 Add_Invite( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
624 assert( Client != NULL );
625 assert( Channel != NULL );
626 assert( Pattern != NULL );
628 mask = Lists_MakeMask( Pattern );
630 already = Lists_IsInviteEntry( mask, Channel );
632 if( ! Lists_AddInvited( mask, Channel, FALSE )) return CONNECTED;
634 if(( Client_Type( Prefix ) == CLIENT_SERVER ) && ( already == TRUE )) return CONNECTED;
636 return Send_ListChange( "+I", Prefix, Client, Channel, mask );
641 Add_Ban( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
646 assert( Client != NULL );
647 assert( Channel != NULL );
648 assert( Pattern != NULL );
650 mask = Lists_MakeMask( Pattern );
652 already = Lists_IsBanEntry( mask, Channel );
654 if( ! Lists_AddBanned( mask, Channel )) return CONNECTED;
656 if(( Client_Type( Prefix ) == CLIENT_SERVER ) && ( already == TRUE )) return CONNECTED;
658 return Send_ListChange( "+b", Prefix, Client, Channel, mask );
663 Del_Invite( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
667 assert( Client != NULL );
668 assert( Channel != NULL );
669 assert( Pattern != NULL );
671 mask = Lists_MakeMask( Pattern );
672 Lists_DelInvited( mask, Channel );
673 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
678 Del_Ban( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
682 assert( Client != NULL );
683 assert( Channel != NULL );
684 assert( Pattern != NULL );
686 mask = Lists_MakeMask( Pattern );
687 Lists_DelBanned( mask, Channel );
688 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
693 Send_ListChange( CHAR *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Mask )
695 /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
699 if( Client_Type( Client ) == CLIENT_USER )
701 /* Bestaetigung an Client */
702 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
706 /* an andere Server */
707 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
709 /* und lokale User im Channel */
710 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, FALSE, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
713 } /* Send_ListChange */