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, ...)
27 #include "irc-write.h"
39 static bool Client_Mode PARAMS(( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target ));
40 static bool Channel_Mode PARAMS(( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel ));
42 static bool Add_Ban_Invite PARAMS((int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern));
43 static bool Del_Ban_Invite PARAMS((int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern));
45 static bool Send_ListChange PARAMS(( char *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Mask ));
49 IRC_MODE( CLIENT *Client, REQUEST *Req )
54 assert( Client != NULL );
55 assert( Req != NULL );
58 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
60 /* Origin for answers */
61 if( Client_Type( Client ) == CLIENT_SERVER )
63 origin = Client_Search( Req->prefix );
64 if( ! origin ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
68 /* Channel or user mode? */
69 cl = NULL; chan = NULL;
70 if (Client_IsValidNick(Req->argv[0]))
71 cl = Client_Search(Req->argv[0]);
72 if (Channel_IsValidName(Req->argv[0]))
73 chan = Channel_Search(Req->argv[0]);
76 return Client_Mode(Client, Req, origin, cl);
78 return Channel_Mode(Client, Req, origin, chan);
80 /* No target found! */
81 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
82 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 */
162 case 'w': /* Wallops messages */
167 if( Client_Type( Client ) == CLIENT_SERVER )
170 Client_SetAway( Origin, DEFAULT_AWAY_MSG );
172 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
175 case 'o': /* IRC operator (only unsettable!) */
176 if(( ! set ) || ( Client_Type( Client ) == CLIENT_SERVER ))
178 Client_SetOperByMe( Target, false );
181 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
184 case 'r': /* Restricted (only settable) */
185 if(( set ) || ( Client_Type( Client ) == CLIENT_SERVER )) x[0] = 'r';
186 else ok = IRC_WriteStrClient( Origin, ERR_RESTRICTED_MSG, Client_ID( Origin ));
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 len = strlen( the_modes ) - 1;
219 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' )) the_modes[len] = '\0';
221 if( Client_Type( Client ) == CLIENT_SERVER )
223 /* Forward modes to other servers */
224 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
228 /* Send reply to client and inform other servers */
229 ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
230 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
232 Log( LOG_DEBUG, "User \"%s\": Mode change, now \"%s\".", Client_Mask( Target ), Client_Modes( Target ));
235 IRC_SetPenalty( Client, 1 );
241 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
243 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
244 const char *mode_ptr;
246 /* Member or not? -- That's the question! */
247 if (!Channel_IsMemberOf(Channel, Origin))
248 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
249 Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
251 /* The sender is a member: generate extended reply */
252 strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
253 mode_ptr = the_modes;
259 snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
260 strlcat(the_args, argadd, sizeof(the_args));
263 strlcat(the_args, " ", sizeof(the_args));
264 strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
270 strlcat(the_modes, the_args, sizeof(the_modes));
272 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
273 Client_ID(Origin), Channel_Name(Channel), the_modes);
278 Channel_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel )
280 /* Handle channel and channel-user modes */
282 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2], argadd[CLIENT_PASS_LEN], *mode_ptr;
283 bool ok, set, modeok = true, skiponce, use_servermode = false, retval;
284 int mode_arg, arg_arg;
289 /* Are modes allowed on channel? */
290 if (Channel_Name(Channel)[0] == '+')
291 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
292 Client_ID(Client), Channel_Name(Channel));
294 /* Mode request: let's answer it :-) */
296 return Channel_Mode_Answer_Request(Origin, Channel);
298 /* Is the user allowed to change modes? */
299 if (Client_Type(Client) == CLIENT_USER) {
300 /* Is the originating user on that channel? */
301 if (!Channel_IsMemberOf(Channel, Origin))
302 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
303 Client_ID(Origin), Channel_Name(Channel));
305 /* channel operator? */
306 if (strchr(Channel_UserModes(Channel, Origin), 'o'))
308 else if(Conf_OperCanMode) {
309 /* IRC-Operators can use MODE as well */
310 if (Client_OperByMe(Origin)) {
312 if (Conf_OperServerMode)
313 use_servermode = true; /* Change Origin to Server */
319 mode_ptr = Req->argv[mode_arg];
320 if (Req->argc > mode_arg + 1)
321 arg_arg = mode_arg + 1;
325 /* Initial state: set or unset modes? */
328 case '-': set = false; break;
329 case '+': set = true; break;
335 /* Prepare reply string */
336 strcpy(the_modes, set ? "+" : "-");
345 /* Try next argument if there's any */
346 if (arg_arg > mode_arg)
351 if (mode_arg >= Req->argc)
353 mode_ptr = Req->argv[mode_arg];
355 if (Req->argc > mode_arg + 1)
356 arg_arg = mode_arg + 1;
365 if (((*mode_ptr == '+') && !set) || ((*mode_ptr == '-') && set)) {
366 /* Action modifier ("+"/"-") must be changed ... */
367 len = strlen( the_modes ) - 1;
368 if ((the_modes[len] == '+') || (the_modes[len] == '-')) {
369 /* Adjust last action modifier in result */
370 the_modes[len] = *mode_ptr;
372 /* Append modifier character to result string */
374 strlcat(the_modes, x, sizeof(the_modes));
376 set = *mode_ptr == '+';
381 /* Are there arguments left? */
382 if( arg_arg >= Req->argc ) arg_arg = -1;
389 /* --- Channel modes --- */
390 case 'i': /* Invite only */
391 case 'm': /* Moderated */
392 case 'n': /* Only members can write */
393 case 's': /* Secret channel */
394 case 't': /* Topic locked */
398 ok = IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID(Origin), Channel_Name(Channel));
400 case 'k': /* Channel key */
405 ok = IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID(Origin), Channel_Name(Channel));
408 if (arg_arg > mode_arg) {
410 Channel_ModeDel(Channel, 'k');
411 Channel_SetKey(Channel, Req->argv[arg_arg]);
412 strlcpy(argadd, Channel_Key(Channel), sizeof(argadd));
415 ok = IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID(Origin), Channel_Name(Channel));
417 Req->argv[arg_arg][0] = '\0';
420 ok = IRC_WriteStrClient(Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID(Origin), Req->command);
423 case 'l': /* Member limit */
428 ok = IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID(Origin), Channel_Name(Channel));
431 if (arg_arg > mode_arg) {
433 l = atol(Req->argv[arg_arg]);
434 if (l > 0 && l < 0xFFFF) {
435 Channel_ModeDel(Channel, 'l');
436 Channel_SetMaxUsers(Channel, l);
437 snprintf(argadd, sizeof(argadd), "%ld", l);
441 ok = IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID(Origin), Channel_Name(Channel));
443 Req->argv[arg_arg][0] = '\0';
446 ok = IRC_WriteStrClient(Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID(Origin), Req->command);
449 case 'P': /* Persistent channel */
451 /* Only IRC operators are allowed to
452 * set the 'P' channel mode! */
453 if (set && !(Client_OperByMe(Client) || Client_Type(Client) == CLIENT_SERVER))
454 ok = IRC_WriteStrClient(Origin, ERR_NOPRIVILEGES_MSG, Client_ID(Origin));
458 ok = IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID(Origin), Channel_Name(Channel));
460 /* --- Channel user modes --- */
461 case 'o': /* Channel operator */
462 case 'v': /* Voice */
463 if (arg_arg > mode_arg) {
465 client = Client_Search(Req->argv[arg_arg]);
469 ok = IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->argv[arg_arg]);
471 ok = IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG, Client_ID(Origin), Channel_Name(Channel));
473 Req->argv[arg_arg][0] = '\0';
476 ok = IRC_WriteStrClient(Origin, ERR_NEEDMOREPARAMS_MSG, Client_ID(Origin), Req->command);
479 /* --- Channel lists --- */
480 case 'I': /* Invite lists */
481 case 'b': /* Ban lists */
482 if (arg_arg > mode_arg) {
485 ok = set ? Add_Ban_Invite(*mode_ptr, Origin, Client, Channel, Req->argv[arg_arg])
486 : Del_Ban_Invite(*mode_ptr, Origin, Client, Channel, Req->argv[arg_arg]);
488 ok = IRC_WriteStrClient(Origin, ERR_CHANOPRIVSNEEDED_MSG,
489 Client_ID(Origin), Channel_Name(Channel));
491 Req->argv[arg_arg][0] = '\0';
494 if (*mode_ptr == 'I')
495 Channel_ShowInvites(Origin, Channel);
497 Channel_ShowBans(Origin, Channel);
501 Log(LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\" on %s!?",
502 set ? '+' : '-', *mode_ptr, Client_ID(Origin), Channel_Name(Channel));
503 if (Client_Type(Client) != CLIENT_SERVER)
504 ok = IRC_WriteStrClient(Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID(Origin), set ? '+' : '-', *mode_ptr);
512 /* Is there a valid mode change? */
516 /* Validate target client */
517 if (client && (!Channel_IsMemberOf(Channel, client))) {
518 if (!IRC_WriteStrClient(Origin, ERR_USERNOTINCHANNEL_MSG,
519 Client_ID(Origin), Client_ID(client), Channel_Name(Channel)))
526 /* Channel-User-Mode */
527 retval = set ? Channel_UserModeAdd(Channel, client, x[0]) : Channel_UserModeDel(Channel, client, x[0]);
529 strlcat(the_args, " ", sizeof(the_args));
530 strlcat(the_args, Client_ID(client), sizeof(the_args));
531 strlcat(the_modes, x, sizeof(the_modes));
532 Log(LOG_DEBUG, "User \"%s\": Mode change on %s, now \"%s\"",
533 Client_Mask(client), Channel_Name(Channel), Channel_UserModes(Channel, client));
537 retval = set ? Channel_ModeAdd(Channel, x[0]) : Channel_ModeDel(Channel, x[0]);
539 strlcat(the_modes, x, sizeof(the_modes));
540 Log(LOG_DEBUG, "Channel %s: Mode change, now \"%s\".", Channel_Name(Channel), Channel_Modes(Channel));
544 /* Are there additional arguments to add? */
546 strlcat(the_args, " ", sizeof(the_args));
547 strlcat(the_args, argadd, sizeof(the_args));
551 /* Are there changed modes? */
553 /* Clean up mode string */
554 len = strlen(the_modes) - 1;
555 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
556 the_modes[len] = '\0';
558 if (Client_Type(Client) == CLIENT_SERVER) {
559 /* Forward mode changes to channel users and other servers */
560 IRC_WriteStrServersPrefix(Client, Origin, "MODE %s %s%s", Channel_Name( Channel ), the_modes, the_args);
561 IRC_WriteStrChannelPrefix(Client, Channel, Origin, false, "MODE %s %s%s", Channel_Name(Channel), the_modes, the_args);
564 Origin = Client_ThisServer();
565 /* Send reply to client and inform other servers and channel users */
566 ok = IRC_WriteStrClientPrefix(Client, Origin, "MODE %s %s%s",
567 Channel_Name(Channel), the_modes, the_args);
568 IRC_WriteStrServersPrefix(Client, Origin, "MODE %s %s%s",
569 Channel_Name(Channel), the_modes, the_args);
570 IRC_WriteStrChannelPrefix(Client, Channel, Origin, false, "MODE %s %s%s",
571 Channel_Name(Channel), the_modes, the_args);
575 IRC_SetPenalty(Client, 1);
581 IRC_AWAY( CLIENT *Client, REQUEST *Req )
583 assert( Client != NULL );
584 assert( Req != NULL );
586 /* Falsche Anzahl Parameter? */
587 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
589 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
592 Client_SetAway( Client, Req->argv[0] );
593 Client_ModeAdd( Client, 'a' );
594 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
595 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
600 Client_ModeDel( Client, 'a' );
601 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
602 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
608 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
614 assert( Client != NULL );
615 assert( Channel != NULL );
616 assert( Pattern != NULL );
617 assert(what == 'I' || what == 'b');
619 mask = Lists_MakeMask(Pattern);
621 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
624 ret = Channel_AddInvite(Channel, mask, false);
626 ret = Channel_AddBan(Channel, mask);
630 if (already && (Client_Type(Prefix) == CLIENT_SERVER))
634 return Send_ListChange("+I", Prefix, Client, Channel, mask);
635 return Send_ListChange("+b", Prefix, Client, Channel, mask);
640 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
643 struct list_head *list;
645 assert( Client != NULL );
646 assert( Channel != NULL );
647 assert( Pattern != NULL );
648 assert(what == 'I' || what == 'b');
650 mask = Lists_MakeMask( Pattern );
653 list = Channel_GetListInvites(Channel);
655 list = Channel_GetListBans(Channel);
657 Lists_Del(list, mask);
659 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
660 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
665 Send_ListChange( char *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Mask )
667 /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
671 if( Client_Type( Client ) == CLIENT_USER )
673 /* Bestaetigung an Client */
674 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
678 /* an andere Server */
679 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
681 /* und lokale User im Channel */
682 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
685 } /* Send_ListChange */