2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2008 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 LogDebug("%s \"%s\": Mode change, now \"%s\".",
233 Client_TypeText(Target), Client_Mask(Target),
234 Client_Modes(Target));
237 IRC_SetPenalty( Client, 1 );
243 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
245 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
246 const char *mode_ptr;
248 /* Member or not? -- That's the question! */
249 if (!Channel_IsMemberOf(Channel, Origin))
250 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
251 Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
253 /* The sender is a member: generate extended reply */
254 strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
255 mode_ptr = the_modes;
261 snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
262 strlcat(the_args, argadd, sizeof(the_args));
265 strlcat(the_args, " ", sizeof(the_args));
266 strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
272 strlcat(the_modes, the_args, sizeof(the_modes));
274 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
275 Client_ID(Origin), Channel_Name(Channel), the_modes);
280 * Handle channel mode and channel-user mode changes
283 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
285 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
286 argadd[CLIENT_PASS_LEN], *mode_ptr;
287 bool ok, set, modeok = true, skiponce, use_servermode = false, retval;
288 int mode_arg, arg_arg;
293 if (Channel_IsModeless(Channel))
294 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
295 Client_ID(Client), Channel_Name(Channel));
297 /* Mode request: let's answer it :-) */
299 return Channel_Mode_Answer_Request(Origin, Channel);
301 /* Is the user allowed to change modes? */
302 if (Client_Type(Client) == CLIENT_USER) {
303 /* Is the originating user on that channel? */
304 if (!Channel_IsMemberOf(Channel, Origin))
305 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
306 Client_ID(Origin), Channel_Name(Channel));
308 /* channel operator? */
309 if (strchr(Channel_UserModes(Channel, Origin), 'o'))
311 else if (Conf_OperCanMode) {
312 /* IRC-Operators can use MODE as well */
313 if (Client_OperByMe(Origin)) {
315 if (Conf_OperServerMode)
316 use_servermode = true; /* Change Origin to Server */
322 mode_ptr = Req->argv[mode_arg];
323 if (Req->argc > mode_arg + 1)
324 arg_arg = mode_arg + 1;
328 /* Initial state: set or unset modes? */
342 /* Prepare reply string */
343 strcpy(the_modes, set ? "+" : "-");
352 /* Try next argument if there's any */
353 if (arg_arg > mode_arg)
358 if (mode_arg >= Req->argc)
360 mode_ptr = Req->argv[mode_arg];
362 if (Req->argc > mode_arg + 1)
363 arg_arg = mode_arg + 1;
372 if (((*mode_ptr == '+') && !set)
373 || ((*mode_ptr == '-') && set)) {
374 /* Action modifier ("+"/"-") must be changed ... */
375 len = strlen(the_modes) - 1;
376 if (the_modes[len] == '+' || the_modes[len] == '-') {
377 /* Adjust last action modifier in result */
378 the_modes[len] = *mode_ptr;
380 /* Append modifier character to result string */
382 strlcat(the_modes, x, sizeof(the_modes));
384 set = *mode_ptr == '+';
389 /* Are there arguments left? */
390 if (arg_arg >= Req->argc)
398 /* --- Channel modes --- */
399 case 'i': /* Invite only */
400 case 'm': /* Moderated */
401 case 'n': /* Only members can write */
402 case 's': /* Secret channel */
403 case 't': /* Topic locked */
407 ok = IRC_WriteStrClient(Origin,
408 ERR_CHANOPRIVSNEEDED_MSG,
409 Client_ID(Origin), Channel_Name(Channel));
411 case 'k': /* Channel key */
416 ok = IRC_WriteStrClient(Origin,
417 ERR_CHANOPRIVSNEEDED_MSG,
419 Channel_Name(Channel));
422 if (arg_arg > mode_arg) {
424 Channel_ModeDel(Channel, 'k');
425 Channel_SetKey(Channel,
427 strlcpy(argadd, Channel_Key(Channel),
431 ok = IRC_WriteStrClient(Origin,
432 ERR_CHANOPRIVSNEEDED_MSG,
434 Channel_Name(Channel));
436 Req->argv[arg_arg][0] = '\0';
439 ok = IRC_WriteStrClient(Origin,
440 ERR_NEEDMOREPARAMS_MSG,
441 Client_ID(Origin), Req->command);
445 case 'l': /* Member limit */
450 ok = IRC_WriteStrClient(Origin,
451 ERR_CHANOPRIVSNEEDED_MSG,
453 Channel_Name(Channel));
456 if (arg_arg > mode_arg) {
458 l = atol(Req->argv[arg_arg]);
459 if (l > 0 && l < 0xFFFF) {
460 Channel_ModeDel(Channel, 'l');
461 Channel_SetMaxUsers(Channel, l);
462 snprintf(argadd, sizeof(argadd),
467 ok = IRC_WriteStrClient(Origin,
468 ERR_CHANOPRIVSNEEDED_MSG,
470 Channel_Name(Channel));
472 Req->argv[arg_arg][0] = '\0';
475 ok = IRC_WriteStrClient(Origin,
476 ERR_NEEDMOREPARAMS_MSG,
477 Client_ID(Origin), Req->command);
481 case 'P': /* Persistent channel */
483 /* Only IRC operators are allowed to
484 * set the 'P' channel mode! */
485 if (set && !(Client_OperByMe(Client)
486 || Client_Type(Client) == CLIENT_SERVER))
487 ok = IRC_WriteStrClient(Origin,
488 ERR_NOPRIVILEGES_MSG,
493 ok = IRC_WriteStrClient(Origin,
494 ERR_CHANOPRIVSNEEDED_MSG,
496 Channel_Name(Channel));
498 /* --- Channel user modes --- */
499 case 'o': /* Channel operator */
500 case 'v': /* Voice */
501 if (arg_arg > mode_arg) {
503 client = Client_Search(Req->argv[arg_arg]);
507 ok = IRC_WriteStrClient(Client,
512 ok = IRC_WriteStrClient(Origin,
513 ERR_CHANOPRIVSNEEDED_MSG,
515 Channel_Name(Channel));
517 Req->argv[arg_arg][0] = '\0';
520 ok = IRC_WriteStrClient(Origin,
521 ERR_NEEDMOREPARAMS_MSG,
522 Client_ID(Origin), Req->command);
526 /* --- Channel lists --- */
527 case 'I': /* Invite lists */
528 case 'b': /* Ban lists */
529 if (arg_arg > mode_arg) {
533 ? Add_Ban_Invite(*mode_ptr, Origin,
536 : Del_Ban_Invite(*mode_ptr, Origin,
540 ok = IRC_WriteStrClient(Origin,
541 ERR_CHANOPRIVSNEEDED_MSG,
543 Channel_Name(Channel));
545 Req->argv[arg_arg][0] = '\0';
548 if (*mode_ptr == 'I')
549 Channel_ShowInvites(Origin, Channel);
551 Channel_ShowBans(Origin, Channel);
556 "Unknown mode \"%c%c\" from \"%s\" on %s!?",
557 set ? '+' : '-', *mode_ptr, Client_ID(Origin),
558 Channel_Name(Channel));
559 if (Client_Type(Client) != CLIENT_SERVER)
560 ok = IRC_WriteStrClient(Origin,
561 ERR_UMODEUNKNOWNFLAG2_MSG,
563 set ? '+' : '-', *mode_ptr);
571 /* Is there a valid mode change? */
575 /* Validate target client */
576 if (client && (!Channel_IsMemberOf(Channel, client))) {
577 if (!IRC_WriteStrClient
578 (Origin, ERR_USERNOTINCHANNEL_MSG,
579 Client_ID(Origin), Client_ID(client),
580 Channel_Name(Channel)))
587 /* Channel-User-Mode */
589 ? Channel_UserModeAdd(Channel, client, x[0])
590 : Channel_UserModeDel(Channel, client, x[0]);
592 strlcat(the_args, " ", sizeof(the_args));
593 strlcat(the_args, Client_ID(client),
595 strlcat(the_modes, x, sizeof(the_modes));
597 ("User \"%s\": Mode change on %s, now \"%s\"",
598 Client_Mask(client), Channel_Name(Channel),
599 Channel_UserModes(Channel, client));
604 ? Channel_ModeAdd(Channel, x[0])
605 : Channel_ModeDel(Channel, x[0]);
607 strlcat(the_modes, x, sizeof(the_modes));
608 LogDebug("Channel %s: Mode change, now \"%s\".",
609 Channel_Name(Channel),
610 Channel_Modes(Channel));
614 /* Are there additional arguments to add? */
616 strlcat(the_args, " ", sizeof(the_args));
617 strlcat(the_args, argadd, sizeof(the_args));
622 /* Are there changed modes? */
624 /* Clean up mode string */
625 len = strlen(the_modes) - 1;
626 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
627 the_modes[len] = '\0';
629 if (Client_Type(Client) == CLIENT_SERVER) {
630 /* MODE requests for local channels from other servers
631 * are definitely invalid! */
632 if (Channel_IsLocal(Channel)) {
633 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
637 /* Forward mode changes to channel users and all the
638 * other remote servers: */
639 IRC_WriteStrServersPrefix(Client, Origin,
640 "MODE %s %s%s", Channel_Name(Channel),
641 the_modes, the_args);
642 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
643 false, "MODE %s %s%s", Channel_Name(Channel),
644 the_modes, the_args);
647 Origin = Client_ThisServer();
648 /* Send reply to client and inform other servers and channel users */
649 ok = IRC_WriteStrClientPrefix(Client, Origin,
650 "MODE %s %s%s", Channel_Name(Channel),
651 the_modes, the_args);
652 /* Only forward requests for non-local channels */
653 if (!Channel_IsLocal(Channel))
654 IRC_WriteStrServersPrefix(Client, Origin,
655 "MODE %s %s%s", Channel_Name(Channel),
656 the_modes, the_args);
657 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
658 false, "MODE %s %s%s", Channel_Name(Channel),
659 the_modes, the_args);
663 IRC_SetPenalty(Client, 1);
669 IRC_AWAY( CLIENT *Client, REQUEST *Req )
671 assert( Client != NULL );
672 assert( Req != NULL );
674 /* Falsche Anzahl Parameter? */
675 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
677 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
680 Client_SetAway( Client, Req->argv[0] );
681 Client_ModeAdd( Client, 'a' );
682 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
683 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
688 Client_ModeDel( Client, 'a' );
689 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
690 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
696 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
702 assert( Client != NULL );
703 assert( Channel != NULL );
704 assert( Pattern != NULL );
705 assert(what == 'I' || what == 'b');
707 mask = Lists_MakeMask(Pattern);
709 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
712 ret = Channel_AddInvite(Channel, mask, false);
714 ret = Channel_AddBan(Channel, mask);
718 if (already && (Client_Type(Prefix) == CLIENT_SERVER))
722 return Send_ListChange("+I", Prefix, Client, Channel, mask);
723 return Send_ListChange("+b", Prefix, Client, Channel, mask);
728 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
731 struct list_head *list;
733 assert( Client != NULL );
734 assert( Channel != NULL );
735 assert( Pattern != NULL );
736 assert(what == 'I' || what == 'b');
738 mask = Lists_MakeMask( Pattern );
741 list = Channel_GetListInvites(Channel);
743 list = Channel_GetListBans(Channel);
745 Lists_Del(list, mask);
747 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
748 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
753 Send_ListChange( char *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Mask )
755 /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
759 if( Client_Type( Client ) == CLIENT_USER )
761 /* Bestaetigung an Client */
762 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
766 /* an andere Server */
767 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
769 /* und lokale User im Channel */
770 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
773 } /* Send_ListChange */