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.52 2008/02/24 18:44:41 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_Ban_Invite PARAMS((int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern ));
46 static bool Del_Invite PARAMS(( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern ));
47 static bool Del_Ban PARAMS(( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern ));
49 static bool Send_ListChange PARAMS(( char *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Mask ));
53 IRC_MODE( CLIENT *Client, REQUEST *Req )
58 assert( Client != NULL );
59 assert( Req != NULL );
62 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
64 /* Origin for answers */
65 if( Client_Type( Client ) == CLIENT_SERVER )
67 origin = Client_Search( Req->prefix );
68 if( ! origin ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
72 /* Channel or user mode? */
73 cl = NULL; chan = NULL;
74 if (Client_IsValidNick(Req->argv[0]))
75 cl = Client_Search(Req->argv[0]);
76 if (Channel_IsValidName(Req->argv[0]))
77 chan = Channel_Search(Req->argv[0]);
80 return Client_Mode(Client, Req, origin, cl);
82 return Channel_Mode(Client, Req, origin, chan);
84 /* No target found! */
85 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
86 Client_ID(Client), Req->argv[0]);
91 Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target )
93 /* Handle client mode requests */
95 char the_modes[COMMAND_LEN], x[2], *mode_ptr;
100 /* Is the client allowed to request or change the modes? */
101 if( Client_Type( Client ) == CLIENT_USER )
103 /* Users are only allowed to manipulate their own modes! */
104 if( Target != Client ) return IRC_WriteStrClient( Client, ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
107 /* Mode request: let's answer it :-) */
108 if( Req->argc == 1 ) return IRC_WriteStrClient( Origin, RPL_UMODEIS_MSG, Client_ID( Origin ), Client_Modes( Target ));
111 mode_ptr = Req->argv[mode_arg];
113 /* Initial state: set or unset modes? */
114 if( *mode_ptr == '+' ) set = true;
115 else if( *mode_ptr == '-' ) set = false;
116 else return IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Origin ));
118 /* Prepare reply string */
119 if( set ) strcpy( the_modes, "+" );
120 else strcpy( the_modes, "-" );
129 /* Try next argument if there's any */
131 if( mode_arg < Req->argc ) mode_ptr = Req->argv[mode_arg];
139 if((( *mode_ptr == '+' ) && ( ! set )) || (( *mode_ptr == '-' ) && ( set )))
141 /* Action modifier ("+"/"-") must be changed ... */
142 len = strlen( the_modes ) - 1;
143 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' ))
145 /* Adjust last action modifier in result */
146 the_modes[len] = *mode_ptr;
150 /* Append modifier character to result string */
152 strlcat( the_modes, x, sizeof( the_modes ));
154 if( *mode_ptr == '+' ) set = true;
164 case 'i': /* Invisible */
165 case 's': /* Server messages */
166 case 'w': /* Wallops messages */
171 if( Client_Type( Client ) == CLIENT_SERVER )
174 Client_SetAway( Origin, DEFAULT_AWAY_MSG );
176 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
179 case 'o': /* IRC operator (only unsettable!) */
180 if(( ! set ) || ( Client_Type( Client ) == CLIENT_SERVER ))
182 Client_SetOperByMe( Target, false );
185 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
188 case 'r': /* Restricted (only settable) */
189 if(( set ) || ( Client_Type( Client ) == CLIENT_SERVER )) x[0] = 'r';
190 else ok = IRC_WriteStrClient( Origin, ERR_RESTRICTED_MSG, Client_ID( Origin ));
194 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\"!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ));
195 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
201 /* Is there a valid mode change? */
202 if( ! x[0] ) continue;
207 if( Client_ModeAdd( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
213 if( Client_ModeDel( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
218 /* Are there changed modes? */
221 /* Remoce needless action modifier characters */
222 len = strlen( the_modes ) - 1;
223 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' )) the_modes[len] = '\0';
225 if( Client_Type( Client ) == CLIENT_SERVER )
227 /* Forward modes to other servers */
228 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
232 /* Send reply to client and inform other servers */
233 ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
234 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
236 Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( Target ), Client_Modes( Target ));
239 IRC_SetPenalty( Client, 1 );
245 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
247 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
248 const char *mode_ptr;
250 /* Member or not? -- That's the question! */
251 if (!Channel_IsMemberOf(Channel, Origin))
252 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
253 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;
263 snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
264 strlcat(the_args, argadd, sizeof(the_args));
267 strlcat(the_args, " ", sizeof(the_args));
268 strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
274 strlcat(the_modes, the_args, sizeof(the_modes));
276 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
277 Client_ID(Origin), Channel_Name(Channel), the_modes);
282 Channel_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel )
284 /* Handle channel and channel-user modes */
286 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2], argadd[CLIENT_PASS_LEN], *mode_ptr;
287 bool ok, set, modeok = true, skiponce, use_servermode = false;
288 int mode_arg, arg_arg;
293 /* Mode request: let's answer it :-) */
295 return Channel_Mode_Answer_Request(Origin, Channel);
297 /* Is the user allowed to change modes? */
298 if (Client_Type(Client) == CLIENT_USER) {
299 /* Is the originating user on that channel? */
300 if (!Channel_IsMemberOf(Channel, Origin))
301 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
302 Client_ID(Origin), Channel_Name(Channel));
304 /* channel operator? */
305 if (strchr(Channel_UserModes(Channel, Origin), 'o'))
307 else if(Conf_OperCanMode) {
308 /* IRC-Operators can use MODE as well */
309 if (Client_OperByMe(Origin)) {
311 if (Conf_OperServerMode)
312 use_servermode = true; /* Change Origin to Server */
318 mode_ptr = Req->argv[mode_arg];
319 if (Req->argc > mode_arg + 1)
320 arg_arg = mode_arg + 1;
324 /* Initial state: set or unset modes? */
327 case '-': set = false; break;
328 case '+': set = true; break;
334 /* Prepare reply string */
335 strcpy(the_modes, set ? "+" : "-");
344 /* Try next argument if there's any */
345 if (arg_arg > mode_arg)
350 if (mode_arg >= Req->argc)
352 mode_ptr = Req->argv[mode_arg];
354 if (Req->argc > mode_arg + 1)
355 arg_arg = mode_arg + 1;
364 if (((*mode_ptr == '+') && !set) || ((*mode_ptr == '-') && set)) {
365 /* Action modifier ("+"/"-") must be changed ... */
366 len = strlen( the_modes ) - 1;
367 if ((the_modes[len] == '+') || (the_modes[len] == '-')) {
368 /* Adjust last action modifier in result */
369 the_modes[len] = *mode_ptr;
371 /* Append modifier character to result string */
373 strlcat(the_modes, x, sizeof(the_modes));
375 set = *mode_ptr == '+';
380 /* Are there arguments left? */
381 if( arg_arg >= Req->argc ) arg_arg = -1;
389 /* --- Channel modes --- */
391 case 'i': /* Invite only */
392 case 'm': /* Moderated */
393 case 'n': /* Only members can write */
394 case 's': /* Secret channel */
395 case 't': /* Topic locked */
396 if( modeok ) x[0] = *mode_ptr;
397 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
400 case 'k': /* Channel key */
403 if( modeok ) x[0] = *mode_ptr;
404 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
407 if( arg_arg > mode_arg )
411 Channel_ModeDel( Channel, 'k' );
412 Channel_SetKey( Channel, Req->argv[arg_arg] );
413 strlcpy( argadd, Channel_Key( Channel ), sizeof( argadd ));
416 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
417 Req->argv[arg_arg][0] = '\0';
420 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
423 case 'l': /* Member limit */
426 if( modeok ) x[0] = *mode_ptr;
427 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
430 if( arg_arg > mode_arg )
434 l = atol( Req->argv[arg_arg] );
435 if( l > 0 && l < 0xFFFF )
437 Channel_ModeDel( Channel, 'l' );
438 Channel_SetMaxUsers( Channel, l );
439 snprintf( argadd, sizeof( argadd ), "%ld", l );
443 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
444 Req->argv[arg_arg][0] = '\0';
447 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
450 case 'P': /* Persistent channel */
452 /* Only IRC operators are allowed to
453 * set the 'P' channel mode! */
454 if (set && ! (Client_OperByMe(Client)
455 || Client_Type(Client) == CLIENT_SERVER)) {
456 ok = IRC_WriteStrClient(Origin,
457 ERR_NOPRIVILEGES_MSG,
462 ok = IRC_WriteStrClient(Origin,
463 ERR_CHANOPRIVSNEEDED_MSG,
465 Channel_Name(Channel));
468 /* --- Channel user modes --- */
470 case 'o': /* Channel operator */
471 case 'v': /* Voice */
472 if( arg_arg > mode_arg )
476 client = Client_Search( Req->argv[arg_arg] );
477 if( client ) x[0] = *mode_ptr;
478 else ok = IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->argv[arg_arg] );
480 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
481 Req->argv[arg_arg][0] = '\0';
484 else ok = IRC_WriteStrClient( Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID( Origin ), Req->command );
487 /* --- Channel lists --- */
489 case 'I': /* Invite lists */
490 if( arg_arg > mode_arg )
495 if( set ) Add_Ban_Invite(*mode_ptr, Origin, Client, Channel, Req->argv[arg_arg] );
496 else Del_Invite( 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_ShowInvites( Origin, Channel );
505 case 'b': /* Ban lists */
506 if( arg_arg > mode_arg )
511 if( set ) Add_Ban_Invite(*mode_ptr, Origin, Client, Channel, Req->argv[arg_arg] );
512 else Del_Ban( Origin, Client, Channel, Req->argv[arg_arg] );
514 else ok = IRC_WriteStrClient( Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( Origin ), Channel_Name( Channel ));
515 Req->argv[arg_arg][0] = '\0';
518 else Channel_ShowBans( Origin, Channel );
522 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\" on %s!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ), Channel_Name( Channel ));
523 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
529 /* Is there a valid mode change? */
530 if( ! x[0] ) continue;
532 /* Validate target client */
533 if( client && ( ! Channel_IsMemberOf( Channel, client )))
535 if( ! IRC_WriteStrClient( Origin, ERR_USERNOTINCHANNEL_MSG, Client_ID( Origin ), Client_ID( client ), Channel_Name( Channel ))) break;
544 /* Channel-User-Mode */
545 if( Channel_UserModeAdd( Channel, client, x[0] ))
547 strlcat( the_args, " ", sizeof( the_args ));
548 strlcat( the_args, Client_ID( client ), sizeof( the_args ));
549 strlcat( the_modes, x, sizeof( the_modes ));
550 Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
556 if( Channel_ModeAdd( Channel, x[0] ))
558 strlcat( the_modes, x, sizeof( the_modes ));
559 Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
568 /* Channel-User-Mode */
569 if( Channel_UserModeDel( Channel, client, x[0] ))
571 strlcat( the_args, " ", sizeof( the_args ));
572 strlcat( the_args, Client_ID( client ), sizeof( the_args ));
573 strlcat( the_modes, x, sizeof( the_modes ));
574 Log( LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"", Client_Mask( client ), Channel_Name( Channel ), Channel_UserModes( Channel, client ));
580 if( Channel_ModeDel( Channel, x[0] ))
582 strlcat( the_modes, x, sizeof( the_modes ));
583 Log( LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name( Channel ), Channel_Modes( Channel ));
588 /* Are there additional arguments to add? */
591 strlcat( the_args, " ", sizeof( the_args ));
592 strlcat( the_args, argadd, sizeof( the_args ));
597 /* Are there changed modes? */
599 /* Clean up mode string */
600 len = strlen(the_modes) - 1;
601 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
602 the_modes[len] = '\0';
604 if (Client_Type(Client) == CLIENT_SERVER) {
605 /* Forward mode changes to channel users and other servers */
606 IRC_WriteStrServersPrefix(Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args);
607 IRC_WriteStrChannelPrefix(Client, Channel, Origin, false, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args);
610 Origin = Client_ThisServer();
611 /* Send reply to client and inform other servers and channel users */
612 ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
613 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
614 IRC_WriteStrChannelPrefix( Client, Channel, Origin, false, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args );
618 IRC_SetPenalty( Client, 1 );
624 IRC_AWAY( CLIENT *Client, REQUEST *Req )
626 assert( Client != NULL );
627 assert( Req != NULL );
629 /* Falsche Anzahl Parameter? */
630 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
632 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
635 Client_SetAway( Client, Req->argv[0] );
636 Client_ModeAdd( Client, 'a' );
637 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
638 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
643 Client_ModeDel( Client, 'a' );
644 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
645 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
651 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern )
657 assert( Client != NULL );
658 assert( Channel != NULL );
659 assert( Pattern != NULL );
660 assert(what == 'I' || what == 'b');
662 mask = Lists_MakeMask(Pattern);
664 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
667 ret = Channel_AddInvite(Channel, mask, false);
669 ret = Channel_AddBan(Channel, mask);
673 if (already && (Client_Type(Prefix) == CLIENT_SERVER))
677 return Send_ListChange("+I", Prefix, Client, Channel, mask);
678 return Send_ListChange("+b", Prefix, Client, Channel, mask);
683 Del_Invite( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern )
687 assert( Client != NULL );
688 assert( Channel != NULL );
689 assert( Pattern != NULL );
691 mask = Lists_MakeMask( Pattern );
692 Lists_Del(Channel_GetListInvites(Channel), mask);
693 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
698 Del_Ban( CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, char *Pattern )
702 assert( Client != NULL );
703 assert( Channel != NULL );
704 assert( Pattern != NULL );
706 mask = Lists_MakeMask( Pattern );
707 Lists_Del(Channel_GetListBans(Channel), mask);
708 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
713 Send_ListChange( char *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Mask )
715 /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
719 if( Client_Type( Client ) == CLIENT_USER )
721 /* Bestaetigung an Client */
722 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
726 /* an andere Server */
727 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
729 /* und lokale User im Channel */
730 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
733 } /* Send_ListChange */