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.30 2003/01/17 19:04:19 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 ));
239 Channel_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel )
241 /* Handle channel and channel-user modes */
243 CHAR the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2], argadd[CLIENT_PASS_LEN], *mode_ptr;
244 BOOLEAN ok, set, modeok, skiponce;
245 INT mode_arg, arg_arg;
249 /* Mode request: let's answer it :-) */
252 /* Member or not? -- That's the question! */
253 if( ! Channel_IsMemberOf( Channel, Origin )) return IRC_WriteStrClient( Origin, RPL_CHANNELMODEIS_MSG, Client_ID( Origin ), Channel_Name( Channel ), Channel_Modes( Channel ));
255 /* The sender is a member: generate extended reply */
256 strlcpy( the_modes, Channel_Modes( Channel ), sizeof( the_modes ));
257 mode_ptr = the_modes;
258 strcpy( the_args, "" );
264 snprintf( argadd, sizeof( argadd ), " %ld", 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 ));
274 if( the_args[0] ) strlcat( the_modes, the_args, sizeof( the_modes ));
276 return IRC_WriteStrClient( Origin, RPL_CHANNELMODEIS_MSG, Client_ID( Origin ), Channel_Name( Channel ), the_modes );
279 /* Is the user allowed to change modes? */
280 if( Client_Type( Client ) == CLIENT_USER )
282 /* Is the originating user on that channel? */
283 if( ! Channel_IsMemberOf( Channel, Origin )) return IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Channel_Name( Channel ));
285 /* Is he channel operator? */
286 if( strchr( Channel_UserModes( Channel, Origin ), 'o' )) modeok = TRUE;
288 if( Conf_OperCanMode )
290 /* auch IRC-Operatoren duerfen MODE verwenden */
291 if( Client_OperByMe( Origin )) modeok = TRUE;
297 mode_ptr = Req->argv[mode_arg];
298 if( Req->argc > mode_arg + 1 ) arg_arg = mode_arg + 1;
301 /* Initial state: set or unset modes? */
303 if( *mode_ptr == '-' ) set = FALSE;
304 else if( *mode_ptr == '+' ) set = TRUE;
305 else set = skiponce = TRUE;
307 /* Prepare reply string */
308 if( set ) strcpy( the_modes, "+" );
309 else strcpy( the_modes, "-" );
310 strcpy( the_args, " " );
316 if( ! skiponce ) mode_ptr++;
319 /* Try next argument if there's any */
320 if( arg_arg > mode_arg ) mode_arg = arg_arg;
322 if( mode_arg < Req->argc ) mode_ptr = Req->argv[mode_arg];
324 if( Req->argc > mode_arg + 1 ) arg_arg = mode_arg + 1;
333 if((( *mode_ptr == '+' ) && ( ! set )) || (( *mode_ptr == '-' ) && ( set )))
335 /* Action modifier ("+"/"-") must be changed ... */
336 if(( the_modes[strlen( the_modes ) - 1] == '+' ) || ( the_modes[strlen( the_modes ) - 1] == '-' ))
338 /* Adjust last action modifier in result */
339 the_modes[strlen( the_modes ) - 1] = *mode_ptr;
343 /* Append modifier character to result string */
345 strlcat( the_modes, x, sizeof( the_modes ));
347 if( *mode_ptr == '+' ) set = TRUE;
353 /* Are there arguments left? */
354 if( arg_arg >= Req->argc ) arg_arg = -1;
365 if( modeok ) x[0] = 'i';
366 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
370 if( modeok ) x[0] = 'm';
371 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
374 /* kein Schreiben in den Channel von aussen */
375 if( modeok ) x[0] = 'n';
376 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
380 if( modeok ) x[0] = 't';
381 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
384 /* Persistent channel */
387 if( set && ( ! Client_OperByMe( Client )))
389 /* Only IRC operators are allowed to set P mode */
390 ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
394 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
397 /* Channel user modes */
399 /* Channel operator */
402 if( arg_arg > mode_arg )
406 client = Client_Search( Req->argv[arg_arg] );
407 if( client ) x[0] = *mode_ptr;
408 else ok = IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[arg_arg] );
410 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
411 Req->argv[arg_arg][0] = '\0';
414 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
420 if( modeok ) x[0] = *mode_ptr;
421 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
424 if( arg_arg > mode_arg )
428 Channel_ModeDel( Channel, 'k' );
429 Channel_SetKey( Channel, Req->argv[arg_arg] );
430 strlcpy( argadd, Channel_Key( Channel ), sizeof( argadd ));
433 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
434 Req->argv[arg_arg][0] = '\0';
437 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
443 if( modeok ) x[0] = *mode_ptr;
444 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
447 if( arg_arg > mode_arg )
451 l = atol( Req->argv[arg_arg] );
452 if( l > 0 && l < 0xFFFF )
454 Channel_ModeDel( Channel, 'l' );
455 Channel_SetMaxUsers( Channel, l );
456 snprintf( argadd, sizeof( argadd ), "%ld", l );
460 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
461 Req->argv[arg_arg][0] = '\0';
464 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
470 if( arg_arg > mode_arg )
475 if( set ) Add_Invite( Origin, Client, Channel, Req->argv[arg_arg] );
476 else Del_Invite( Origin, Client, Channel, Req->argv[arg_arg] );
478 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
479 Req->argv[arg_arg][0] = '\0';
482 else Lists_ShowInvites( Origin, Channel );
486 if( arg_arg > mode_arg )
491 if( set ) Add_Ban( Origin, Client, Channel, Req->argv[arg_arg] );
492 else Del_Ban( Origin, Client, Channel, Req->argv[arg_arg] );
494 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
495 Req->argv[arg_arg][0] = '\0';
498 else Lists_ShowBans( Origin, Channel );
502 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\" on %s!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ), Channel_Name( Channel ));
503 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
509 /* Is there a valid mode change? */
510 if( ! x[0] ) continue;
517 /* Channel-User-Mode */
518 if( Channel_UserModeAdd( Channel, client, x[0] ))
520 strlcat( the_args, Client_ID( client ), sizeof( the_args ));
521 strlcat( the_args, " ", sizeof( the_args ));
522 strlcat( the_modes, x, sizeof( the_modes ));
523 Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
529 if( Channel_ModeAdd( Channel, x[0] ))
531 strlcat( the_modes, x, sizeof( the_modes ));
532 Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
541 /* Channel-User-Mode */
542 if( Channel_UserModeDel( Channel, client, x[0] ))
544 strlcat( the_args, Client_ID( client ), sizeof( the_args ));
545 strlcat( the_args, " ", sizeof( the_args ));
546 strlcat( the_modes, x, sizeof( the_modes ));
547 Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
553 if( Channel_ModeDel( Channel, x[0] ))
555 strlcat( the_modes, x, sizeof( the_modes ));
556 Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
561 /* Are there additional arguments to add? */
564 if( the_args[strlen( the_args ) - 1] != ' ' ) strlcat( the_args, " ", sizeof( the_args ));
565 strlcat( the_args, argadd, sizeof( the_args ));
570 /* Are there changed modes? */
573 /* Clean up mode string */
574 if(( the_modes[strlen( the_modes ) - 1] == '+' ) || ( the_modes[strlen( the_modes ) - 1] == '-' )) the_modes[strlen( the_modes ) - 1] = '\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 );
599 IRC_AWAY( CLIENT *Client, REQUEST *Req )
601 assert( Client != NULL );
602 assert( Req != NULL );
604 /* Falsche Anzahl Parameter? */
605 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
607 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
610 Client_SetAway( Client, Req->argv[0] );
611 Client_ModeAdd( Client, 'a' );
612 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
613 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
618 Client_ModeDel( Client, 'a' );
619 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
620 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
626 Add_Invite( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
630 assert( Client != NULL );
631 assert( Channel != NULL );
632 assert( Pattern != NULL );
634 mask = Lists_MakeMask( Pattern );
636 if( ! Lists_AddInvited( Prefix, mask, Channel, FALSE )) return CONNECTED;
637 return Send_ListChange( "+I", Prefix, Client, Channel, mask );
642 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 if( ! Lists_AddBanned( Prefix, mask, Channel )) return CONNECTED;
653 return Send_ListChange( "+b", Prefix, Client, Channel, mask );
658 Del_Invite( 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 );
667 Lists_DelInvited( mask, Channel );
668 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
673 Del_Ban( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Pattern )
677 assert( Client != NULL );
678 assert( Channel != NULL );
679 assert( Pattern != NULL );
681 mask = Lists_MakeMask( Pattern );
682 Lists_DelBanned( mask, Channel );
683 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
688 Send_ListChange( CHAR *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, CHAR *Mask )
690 /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
694 if( Client_Type( Client ) == CLIENT_USER )
696 /* Bestaetigung an Client */
697 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
701 /* an andere Server */
702 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
704 /* und lokale User im Channel */
705 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, FALSE, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
708 } /* Send_ListChange */