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.38 2005/03/05 11:44:01 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;
96 /* Is the client allowed to request or change the modes? */
97 if( Client_Type( Client ) == CLIENT_USER )
99 /* Users are only allowed to manipulate their own modes! */
100 if( Target != Client ) return IRC_WriteStrClient( Client, ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
103 /* Mode request: let's answer it :-) */
104 if( Req->argc == 1 ) return IRC_WriteStrClient( Origin, RPL_UMODEIS_MSG, Client_ID( Origin ), Client_Modes( Target ));
107 mode_ptr = Req->argv[mode_arg];
109 /* Initial state: set or unset modes? */
110 if( *mode_ptr == '+' ) set = TRUE;
111 else if( *mode_ptr == '-' ) set = FALSE;
112 else return IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Origin ));
114 /* Prepare reply string */
115 if( set ) strcpy( the_modes, "+" );
116 else strcpy( the_modes, "-" );
125 /* Try next argument if there's any */
127 if( mode_arg < Req->argc ) mode_ptr = Req->argv[mode_arg];
135 if((( *mode_ptr == '+' ) && ( ! set )) || (( *mode_ptr == '-' ) && ( set )))
137 /* Action modifier ("+"/"-") must be changed ... */
138 len = strlen( the_modes ) - 1;
139 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' ))
141 /* Adjust last action modifier in result */
142 the_modes[len] = *mode_ptr;
146 /* Append modifier character to result string */
148 strlcat( the_modes, x, sizeof( the_modes ));
150 if( *mode_ptr == '+' ) set = TRUE;
160 case 'i': /* Invisible */
161 case 's': /* Server messages */
166 if( Client_Type( Client ) == CLIENT_SERVER )
169 Client_SetAway( Client, DEFAULT_AWAY_MSG );
171 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
174 case 'o': /* IRC operator (only unsettable!) */
175 if(( ! set ) || ( Client_Type( Client ) == CLIENT_SERVER ))
177 Client_SetOperByMe( Target, FALSE );
180 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
183 case 'r': /* Restricted (only settable) */
184 if(( set ) || ( Client_Type( Client ) == CLIENT_SERVER )) x[0] = 'r';
185 else ok = IRC_WriteStrClient( Origin, ERR_RESTRICTED_MSG, Client_ID( Origin ));
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] )) strlcat( the_modes, x, sizeof( the_modes ));
208 if( Client_ModeDel( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
213 /* Are there changed modes? */
216 /* Remoce needless action modifier characters */
217 len = strlen( the_modes ) - 1;
218 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' )) the_modes[len] = '\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;
251 /* Mode request: let's answer it :-) */
254 /* Member or not? -- That's the question! */
255 if( ! Channel_IsMemberOf( Channel, Origin )) return IRC_WriteStrClient( Origin, RPL_CHANNELMODEIS_MSG, Client_ID( Origin ), Channel_Name( Channel ), Channel_Modes( Channel ));
257 /* The sender is a member: generate extended reply */
258 strlcpy( the_modes, Channel_Modes( Channel ), sizeof( the_modes ));
259 mode_ptr = the_modes;
260 strcpy( the_args, "" );
266 snprintf( argadd, sizeof( argadd ), " %ld", Channel_MaxUsers( Channel ));
267 strlcat( the_args, argadd, sizeof( the_args ));
270 strlcat( the_args, " ", sizeof( the_args ));
271 strlcat( the_args, Channel_Key( Channel ), sizeof( the_args ));
276 if( the_args[0] ) strlcat( the_modes, the_args, sizeof( the_modes ));
278 return IRC_WriteStrClient( Origin, RPL_CHANNELMODEIS_MSG, Client_ID( Origin ), Channel_Name( Channel ), the_modes );
281 /* Is the user allowed to change modes? */
282 if( Client_Type( Client ) == CLIENT_USER )
284 /* Is the originating user on that channel? */
285 if( ! Channel_IsMemberOf( Channel, Origin )) return IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Channel_Name( Channel ));
287 /* Is he channel operator? */
288 if( strchr( Channel_UserModes( Channel, Origin ), 'o' )) modeok = TRUE;
290 if( Conf_OperCanMode )
292 /* auch IRC-Operatoren duerfen MODE verwenden */
293 if( Client_OperByMe( Origin )) modeok = TRUE;
299 mode_ptr = Req->argv[mode_arg];
300 if( Req->argc > mode_arg + 1 ) arg_arg = mode_arg + 1;
303 /* Initial state: set or unset modes? */
305 if( *mode_ptr == '-' ) set = FALSE;
306 else if( *mode_ptr == '+' ) set = TRUE;
307 else set = skiponce = TRUE;
309 /* Prepare reply string */
310 if( set ) strcpy( the_modes, "+" );
311 else strcpy( the_modes, "-" );
312 strcpy( the_args, " " );
318 if( ! skiponce ) mode_ptr++;
321 /* Try next argument if there's any */
322 if( arg_arg > mode_arg ) mode_arg = arg_arg;
324 if( mode_arg < Req->argc ) mode_ptr = Req->argv[mode_arg];
326 if( Req->argc > mode_arg + 1 ) arg_arg = mode_arg + 1;
335 if((( *mode_ptr == '+' ) && ( ! set )) || (( *mode_ptr == '-' ) && ( set )))
337 /* Action modifier ("+"/"-") must be changed ... */
338 len = strlen( the_modes ) - 1;
339 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' ))
341 /* Adjust last action modifier in result */
342 the_modes[len] = *mode_ptr;
346 /* Append modifier character to result string */
348 strlcat( the_modes, x, sizeof( the_modes ));
350 if( *mode_ptr == '+' ) set = TRUE;
356 /* Are there arguments left? */
357 if( arg_arg >= Req->argc ) arg_arg = -1;
365 /* --- Channel modes --- */
367 case 'i': /* Invite only */
368 case 'm': /* Moderated */
369 case 'n': /* Only members can write */
370 case 's': /* Secret channel */
371 case 't': /* Topic locked */
372 if( modeok ) x[0] = *mode_ptr;
373 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
376 case 'k': /* Channel key */
379 if( modeok ) x[0] = *mode_ptr;
380 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
383 if( arg_arg > mode_arg )
387 Channel_ModeDel( Channel, 'k' );
388 Channel_SetKey( Channel, Req->argv[arg_arg] );
389 strlcpy( argadd, Channel_Key( Channel ), sizeof( argadd ));
392 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
393 Req->argv[arg_arg][0] = '\0';
396 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
399 case 'l': /* Member limit */
402 if( modeok ) x[0] = *mode_ptr;
403 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
406 if( arg_arg > mode_arg )
410 l = atol( Req->argv[arg_arg] );
411 if( l > 0 && l < 0xFFFF )
413 Channel_ModeDel( Channel, 'l' );
414 Channel_SetMaxUsers( Channel, l );
415 snprintf( argadd, sizeof( argadd ), "%ld", l );
419 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
420 Req->argv[arg_arg][0] = '\0';
423 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
426 case 'P': /* Persistent channel */
429 if( set && ( ! Client_OperByMe( Client )))
431 /* Only IRC operators are allowed to set P mode */
432 ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
436 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
439 /* --- Channel user modes --- */
441 case 'o': /* Channel operator */
442 case 'v': /* Voice */
443 if( arg_arg > mode_arg )
447 client = Client_Search( Req->argv[arg_arg] );
448 if( client ) x[0] = *mode_ptr;
449 else ok = IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[arg_arg] );
451 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
452 Req->argv[arg_arg][0] = '\0';
455 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
458 /* --- Channel lists --- */
460 case 'I': /* Invite lists */
461 if( arg_arg > mode_arg )
466 if( set ) Add_Invite( Origin, Client, Channel, Req->argv[arg_arg] );
467 else Del_Invite( Origin, Client, Channel, Req->argv[arg_arg] );
469 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
470 Req->argv[arg_arg][0] = '\0';
473 else Lists_ShowInvites( Origin, Channel );
476 case 'b': /* Ban lists */
477 if( arg_arg > mode_arg )
482 if( set ) Add_Ban( Origin, Client, Channel, Req->argv[arg_arg] );
483 else Del_Ban( Origin, Client, Channel, Req->argv[arg_arg] );
485 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
486 Req->argv[arg_arg][0] = '\0';
489 else Lists_ShowBans( Origin, Channel );
493 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\" on %s!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ), Channel_Name( Channel ));
494 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
500 /* Is there a valid mode change? */
501 if( ! x[0] ) continue;
503 /* Validate target client */
504 if( client && ( ! Channel_IsMemberOf( Channel, client )))
506 if( ! IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( client ), Channel_Name( Channel ))) break;
515 /* Channel-User-Mode */
516 if( Channel_UserModeAdd( Channel, client, x[0] ))
518 strlcat( the_args, Client_ID( client ), sizeof( the_args ));
519 strlcat( the_args, " ", sizeof( the_args ));
520 strlcat( the_modes, x, sizeof( the_modes ));
521 Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
527 if( Channel_ModeAdd( Channel, x[0] ))
529 strlcat( the_modes, x, sizeof( the_modes ));
530 Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
539 /* Channel-User-Mode */
540 if( Channel_UserModeDel( Channel, client, x[0] ))
542 strlcat( the_args, Client_ID( client ), sizeof( the_args ));
543 strlcat( the_args, " ", sizeof( the_args ));
544 strlcat( the_modes, x, sizeof( the_modes ));
545 Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
551 if( Channel_ModeDel( Channel, x[0] ))
553 strlcat( the_modes, x, sizeof( the_modes ));
554 Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
559 /* Are there additional arguments to add? */
562 len = strlen( the_args ) - 1;
563 if( the_args[len] != ' ' ) strlcat( the_args, " ", sizeof( the_args ));
564 strlcat( the_args, argadd, sizeof( the_args ));
569 /* Are there changed modes? */
572 /* Clean up mode string */
573 len = strlen( the_modes ) - 1;
574 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' )) the_modes[len] = '\0';
576 /* Clean up argument string if there are none */
577 if( ! the_args[1] ) the_args[0] = '\0';
579 if( Client_Type( Client ) == CLIENT_SERVER )
581 /* Forward mode changes to channel users and other servers */
582 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
583 IRC_WriteStrChannelPrefix( Client, Channel, Origin, FALSE, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
587 /* Send reply to client and inform other servers and channel users */
588 ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
589 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
590 IRC_WriteStrChannelPrefix( Client, Channel, Origin, FALSE, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
594 IRC_SetPenalty( Client, 1 );
600 IRC_AWAY( CLIENT *Client, REQUEST *Req )
602 assert( Client != NULL );
603 assert( Req != NULL );
605 /* Falsche Anzahl Parameter? */
606 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
608 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
611 Client_SetAway( Client, Req->argv[0] );
612 Client_ModeAdd( Client, 'a' );
613 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
614 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
619 Client_ModeDel( Client, 'a' );
620 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
621 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
627 Add_Invite( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
632 assert( Client != NULL );
633 assert( Channel != NULL );
634 assert( Pattern != NULL );
636 mask = Lists_MakeMask( Pattern );
638 already = Lists_IsInviteEntry( mask, Channel );
640 if( ! Lists_AddInvited( mask, Channel, FALSE )) return CONNECTED;
642 if(( Client_Type( Prefix ) == CLIENT_SERVER ) && ( already == TRUE )) return CONNECTED;
644 return Send_ListChange( "+I", Prefix, Client, Channel, mask );
649 Add_Ban( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
654 assert( Client != NULL );
655 assert( Channel != NULL );
656 assert( Pattern != NULL );
658 mask = Lists_MakeMask( Pattern );
660 already = Lists_IsBanEntry( mask, Channel );
662 if( ! Lists_AddBanned( mask, Channel )) return CONNECTED;
664 if(( Client_Type( Prefix ) == CLIENT_SERVER ) && ( already == TRUE )) return CONNECTED;
666 return Send_ListChange( "+b", Prefix, Client, Channel, mask );
671 Del_Invite( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
675 assert( Client != NULL );
676 assert( Channel != NULL );
677 assert( Pattern != NULL );
679 mask = Lists_MakeMask( Pattern );
680 Lists_DelInvited( mask, Channel );
681 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
686 Del_Ban( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
690 assert( Client != NULL );
691 assert( Channel != NULL );
692 assert( Pattern != NULL );
694 mask = Lists_MakeMask( Pattern );
695 Lists_DelBanned( mask, Channel );
696 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
701 Send_ListChange( CHAR *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Mask )
703 /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
707 if( Client_Type( Client ) == CLIENT_USER )
709 /* Bestaetigung an Client */
710 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
714 /* an andere Server */
715 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
717 /* und lokale User im Channel */
718 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, FALSE, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
721 } /* Send_ListChange */