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.51 2008/02/16 11:27:49 fw Exp $";
29 #include "irc-write.h"
41 static bool Client_Mode PARAMS(( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target ));
42 static bool Channel_Mode PARAMS(( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel ));
44 static bool Add_Invite PARAMS(( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern ));
45 static bool Add_Ban PARAMS(( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern ));
47 static bool Del_Invite PARAMS(( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern ));
48 static bool Del_Ban PARAMS(( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern ));
50 static bool 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? */
74 cl = NULL; chan = NULL;
75 if (Client_IsValidNick(Req->argv[0]))
76 cl = Client_Search(Req->argv[0]);
77 if (Channel_IsValidName(Req->argv[0]))
78 chan = Channel_Search(Req->argv[0]);
81 return Client_Mode(Client, Req, origin, cl);
83 return Channel_Mode(Client, Req, origin, chan);
85 /* No target found! */
86 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
87 Client_ID(Client), Req->argv[0]);
92 Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target )
94 /* Handle client mode requests */
96 char the_modes[COMMAND_LEN], x[2], *mode_ptr;
101 /* Is the client allowed to request or change the modes? */
102 if( Client_Type( Client ) == CLIENT_USER )
104 /* Users are only allowed to manipulate their own modes! */
105 if( Target != Client ) return IRC_WriteStrClient( Client, ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
108 /* Mode request: let's answer it :-) */
109 if( Req->argc == 1 ) return IRC_WriteStrClient( Origin, RPL_UMODEIS_MSG, Client_ID( Origin ), Client_Modes( Target ));
112 mode_ptr = Req->argv[mode_arg];
114 /* Initial state: set or unset modes? */
115 if( *mode_ptr == '+' ) set = true;
116 else if( *mode_ptr == '-' ) set = false;
117 else return IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Origin ));
119 /* Prepare reply string */
120 if( set ) strcpy( the_modes, "+" );
121 else strcpy( the_modes, "-" );
130 /* Try next argument if there's any */
132 if( mode_arg < Req->argc ) mode_ptr = Req->argv[mode_arg];
140 if((( *mode_ptr == '+' ) && ( ! set )) || (( *mode_ptr == '-' ) && ( set )))
142 /* Action modifier ("+"/"-") must be changed ... */
143 len = strlen( the_modes ) - 1;
144 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' ))
146 /* Adjust last action modifier in result */
147 the_modes[len] = *mode_ptr;
151 /* Append modifier character to result string */
153 strlcat( the_modes, x, sizeof( the_modes ));
155 if( *mode_ptr == '+' ) set = true;
165 case 'i': /* Invisible */
166 case 's': /* Server messages */
167 case 'w': /* Wallops messages */
172 if( Client_Type( Client ) == CLIENT_SERVER )
175 Client_SetAway( Origin, DEFAULT_AWAY_MSG );
177 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
180 case 'o': /* IRC operator (only unsettable!) */
181 if(( ! set ) || ( Client_Type( Client ) == CLIENT_SERVER ))
183 Client_SetOperByMe( Target, false );
186 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
189 case 'r': /* Restricted (only settable) */
190 if(( set ) || ( Client_Type( Client ) == CLIENT_SERVER )) x[0] = 'r';
191 else ok = IRC_WriteStrClient( Origin, ERR_RESTRICTED_MSG, Client_ID( Origin ));
195 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\"!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ));
196 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
202 /* Is there a valid mode change? */
203 if( ! x[0] ) continue;
208 if( Client_ModeAdd( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
214 if( Client_ModeDel( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
219 /* Are there changed modes? */
222 /* Remoce needless action modifier characters */
223 len = strlen( the_modes ) - 1;
224 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' )) the_modes[len] = '\0';
226 if( Client_Type( Client ) == CLIENT_SERVER )
228 /* Forward modes to other servers */
229 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
233 /* Send reply to client and inform other servers */
234 ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
235 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
237 Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( Target ), Client_Modes( Target ));
240 IRC_SetPenalty( Client, 1 );
246 Channel_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel )
248 /* Handle channel and channel-user modes */
250 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2], argadd[CLIENT_PASS_LEN], *mode_ptr;
251 bool ok, set, modeok = false, skiponce, use_servermode = false;
252 int mode_arg, arg_arg;
257 /* Mode request: let's answer it :-) */
260 /* Member or not? -- That's the question! */
261 if( ! Channel_IsMemberOf( Channel, Origin )) return IRC_WriteStrClient( Origin, RPL_CHANNELMODEIS_MSG, Client_ID( Origin ), Channel_Name( Channel ), Channel_Modes( Channel ));
263 /* The sender is a member: generate extended reply */
264 strlcpy( the_modes, Channel_Modes( Channel ), sizeof( the_modes ));
265 mode_ptr = the_modes;
272 snprintf( argadd, sizeof( argadd ), " %lu", Channel_MaxUsers( Channel ));
273 strlcat( the_args, argadd, sizeof( the_args ));
276 strlcat( the_args, " ", sizeof( the_args ));
277 strlcat( the_args, Channel_Key( Channel ), sizeof( the_args ));
282 if( the_args[0] ) strlcat( the_modes, the_args, sizeof( the_modes ));
284 return IRC_WriteStrClient( Origin, RPL_CHANNELMODEIS_MSG, Client_ID( Origin ), Channel_Name( Channel ), the_modes );
287 /* Is the user allowed to change modes? */
288 if( Client_Type( Client ) == CLIENT_USER )
290 /* Is the originating user on that channel? */
291 if( ! Channel_IsMemberOf( Channel, Origin )) return IRC_WriteStrClient( Origin, ERR_NOTONCHANNEL_MSG, Client_ID( Origin ), Channel_Name( Channel ));
293 /* Is he channel operator? */
294 if( strchr( Channel_UserModes( Channel, Origin ), 'o' )) modeok = true;
295 else if( Conf_OperCanMode )
297 /* IRC-Operators can use MODE as well */
298 if( Client_OperByMe( Origin )) {
300 if ( Conf_OperServerMode ) use_servermode = true; /* Change Origin to Server */
307 mode_ptr = Req->argv[mode_arg];
308 if( Req->argc > mode_arg + 1 ) arg_arg = mode_arg + 1;
311 /* Initial state: set or unset modes? */
313 if( *mode_ptr == '-' ) set = false;
314 else if( *mode_ptr == '+' ) set = true;
315 else set = skiponce = true;
317 /* Prepare reply string */
318 if( set ) strcpy( the_modes, "+" );
319 else strcpy( the_modes, "-" );
326 if( ! skiponce ) mode_ptr++;
329 /* Try next argument if there's any */
330 if( arg_arg > mode_arg ) mode_arg = arg_arg;
332 if( mode_arg < Req->argc ) mode_ptr = Req->argv[mode_arg];
334 if( Req->argc > mode_arg + 1 ) arg_arg = mode_arg + 1;
343 if((( *mode_ptr == '+' ) && ( ! set )) || (( *mode_ptr == '-' ) && ( set )))
345 /* Action modifier ("+"/"-") must be changed ... */
346 len = strlen( the_modes ) - 1;
347 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' ))
349 /* Adjust last action modifier in result */
350 the_modes[len] = *mode_ptr;
354 /* Append modifier character to result string */
356 strlcat( the_modes, x, sizeof( the_modes ));
358 if( *mode_ptr == '+' ) set = true;
364 /* Are there arguments left? */
365 if( arg_arg >= Req->argc ) arg_arg = -1;
373 /* --- Channel modes --- */
375 case 'i': /* Invite only */
376 case 'm': /* Moderated */
377 case 'n': /* Only members can write */
378 case 's': /* Secret channel */
379 case 't': /* Topic locked */
380 if( modeok ) x[0] = *mode_ptr;
381 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
384 case 'k': /* Channel key */
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 strlcpy( argadd, Channel_Key( Channel ), sizeof( argadd ));
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 );
407 case 'l': /* Member limit */
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 snprintf( argadd, sizeof( 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 );
434 case 'P': /* Persistent channel */
436 /* Only IRC operators are allowed to
437 * set the 'P' channel mode! */
438 if (set && ! (Client_OperByMe(Client)
439 || Client_Type(Client) == CLIENT_SERVER)) {
440 ok = IRC_WriteStrClient(Origin,
441 ERR_NOPRIVILEGES_MSG,
446 ok = IRC_WriteStrClient(Origin,
447 ERR_CHANOPRIVSNEEDED_MSG,
449 Channel_Name(Channel));
452 /* --- Channel user modes --- */
454 case 'o': /* Channel operator */
455 case 'v': /* Voice */
456 if( arg_arg > mode_arg )
460 client = Client_Search( Req->argv[arg_arg] );
461 if( client ) x[0] = *mode_ptr;
462 else ok = IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[arg_arg] );
464 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
465 Req->argv[arg_arg][0] = '\0';
468 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
471 /* --- Channel lists --- */
473 case 'I': /* Invite lists */
474 if( arg_arg > mode_arg )
479 if( set ) Add_Invite( Origin, Client, Channel, Req->argv[arg_arg] );
480 else Del_Invite( Origin, Client, Channel, Req->argv[arg_arg] );
482 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
483 Req->argv[arg_arg][0] = '\0';
486 else Channel_ShowInvites( Origin, Channel );
489 case 'b': /* Ban lists */
490 if( arg_arg > mode_arg )
495 if( set ) Add_Ban( Origin, Client, Channel, Req->argv[arg_arg] );
496 else Del_Ban( Origin, Client, Channel, Req->argv[arg_arg] );
498 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
499 Req->argv[arg_arg][0] = '\0';
502 else Channel_ShowBans( Origin, Channel );
506 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\" on %s!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ), Channel_Name( Channel ));
507 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
513 /* Is there a valid mode change? */
514 if( ! x[0] ) continue;
516 /* Validate target client */
517 if( client && ( ! Channel_IsMemberOf( Channel, client )))
519 if( ! IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( client ), Channel_Name( Channel ))) break;
528 /* Channel-User-Mode */
529 if( Channel_UserModeAdd( Channel, client, x[0] ))
531 strlcat( the_args, " ", sizeof( the_args ));
532 strlcat( the_args, Client_ID( client ), sizeof( the_args ));
533 strlcat( the_modes, x, sizeof( the_modes ));
534 Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
540 if( Channel_ModeAdd( Channel, x[0] ))
542 strlcat( the_modes, x, sizeof( the_modes ));
543 Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
552 /* Channel-User-Mode */
553 if( Channel_UserModeDel( Channel, client, x[0] ))
555 strlcat( the_args, " ", sizeof( the_args ));
556 strlcat( the_args, Client_ID( client ), sizeof( the_args ));
557 strlcat( the_modes, x, sizeof( the_modes ));
558 Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
564 if( Channel_ModeDel( Channel, x[0] ))
566 strlcat( the_modes, x, sizeof( the_modes ));
567 Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
572 /* Are there additional arguments to add? */
575 strlcat( the_args, " ", sizeof( the_args ));
576 strlcat( the_args, argadd, sizeof( the_args ));
581 /* Are there changed modes? */
584 /* Clean up mode string */
585 len = strlen( the_modes ) - 1;
586 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' )) the_modes[len] = '\0';
588 if( Client_Type( Client ) == CLIENT_SERVER )
590 /* Forward mode changes to channel users and other servers */
591 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
592 IRC_WriteStrChannelPrefix( Client, Channel, Origin, false, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
596 if ( use_servermode ) Origin = Client_ThisServer();
598 /* Send reply to client and inform other servers and channel users */
599 ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
600 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
601 IRC_WriteStrChannelPrefix( Client, Channel, Origin, false, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
605 IRC_SetPenalty( Client, 1 );
611 IRC_AWAY( CLIENT *Client, REQUEST *Req )
613 assert( Client != NULL );
614 assert( Req != NULL );
616 /* Falsche Anzahl Parameter? */
617 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
619 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
622 Client_SetAway( Client, Req->argv[0] );
623 Client_ModeAdd( Client, 'a' );
624 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
625 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
630 Client_ModeDel( Client, 'a' );
631 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
632 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
638 Add_Invite( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern )
643 assert( Client != NULL );
644 assert( Channel != NULL );
645 assert( Pattern != NULL );
647 mask = Lists_MakeMask( Pattern );
649 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask );
651 if( ! Channel_AddInvite(Channel, mask, false ))
654 if ( already && ( Client_Type( Prefix ) == CLIENT_SERVER ))
657 return Send_ListChange( "+I", Prefix, Client, Channel, mask );
662 Add_Ban( 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 );
673 already = Lists_CheckDupeMask(Channel_GetListBans(Channel), mask );
675 if( ! Channel_AddBan(Channel, mask))
678 if ( already && ( Client_Type( Prefix ) == CLIENT_SERVER ))
681 return Send_ListChange( "+b", Prefix, Client, Channel, mask );
686 Del_Invite( 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_Del(Channel_GetListInvites(Channel), mask);
696 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
701 Del_Ban( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern )
705 assert( Client != NULL );
706 assert( Channel != NULL );
707 assert( Pattern != NULL );
709 mask = Lists_MakeMask( Pattern );
710 Lists_Del(Channel_GetListBans(Channel), mask);
711 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
716 Send_ListChange( char *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Mask )
718 /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
722 if( Client_Type( Client ) == CLIENT_USER )
724 /* Bestaetigung an Client */
725 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
729 /* an andere Server */
730 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
732 /* und lokale User im Channel */
733 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
736 } /* Send_ListChange */