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 /* Are modes allowed on channel? */
294 if (Channel_Name(Channel)[0] == '+')
295 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
296 Client_ID(Client), Channel_Name(Channel));
298 /* Mode request: let's answer it :-) */
300 return Channel_Mode_Answer_Request(Origin, Channel);
302 /* Is the user allowed to change modes? */
303 if (Client_Type(Client) == CLIENT_USER) {
304 /* Is the originating user on that channel? */
305 if (!Channel_IsMemberOf(Channel, Origin))
306 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
307 Client_ID(Origin), Channel_Name(Channel));
309 /* channel operator? */
310 if (strchr(Channel_UserModes(Channel, Origin), 'o'))
312 else if (Conf_OperCanMode) {
313 /* IRC-Operators can use MODE as well */
314 if (Client_OperByMe(Origin)) {
316 if (Conf_OperServerMode)
317 use_servermode = true; /* Change Origin to Server */
323 mode_ptr = Req->argv[mode_arg];
324 if (Req->argc > mode_arg + 1)
325 arg_arg = mode_arg + 1;
329 /* Initial state: set or unset modes? */
343 /* Prepare reply string */
344 strcpy(the_modes, set ? "+" : "-");
353 /* Try next argument if there's any */
354 if (arg_arg > mode_arg)
359 if (mode_arg >= Req->argc)
361 mode_ptr = Req->argv[mode_arg];
363 if (Req->argc > mode_arg + 1)
364 arg_arg = mode_arg + 1;
373 if (((*mode_ptr == '+') && !set)
374 || ((*mode_ptr == '-') && set)) {
375 /* Action modifier ("+"/"-") must be changed ... */
376 len = strlen(the_modes) - 1;
377 if (the_modes[len] == '+' || the_modes[len] == '-') {
378 /* Adjust last action modifier in result */
379 the_modes[len] = *mode_ptr;
381 /* Append modifier character to result string */
383 strlcat(the_modes, x, sizeof(the_modes));
385 set = *mode_ptr == '+';
390 /* Are there arguments left? */
391 if (arg_arg >= Req->argc)
399 /* --- Channel modes --- */
400 case 'i': /* Invite only */
401 case 'm': /* Moderated */
402 case 'n': /* Only members can write */
403 case 's': /* Secret channel */
404 case 't': /* Topic locked */
408 ok = IRC_WriteStrClient(Origin,
409 ERR_CHANOPRIVSNEEDED_MSG,
410 Client_ID(Origin), Channel_Name(Channel));
412 case 'k': /* Channel key */
417 ok = IRC_WriteStrClient(Origin,
418 ERR_CHANOPRIVSNEEDED_MSG,
420 Channel_Name(Channel));
423 if (arg_arg > mode_arg) {
425 Channel_ModeDel(Channel, 'k');
426 Channel_SetKey(Channel,
428 strlcpy(argadd, Channel_Key(Channel),
432 ok = IRC_WriteStrClient(Origin,
433 ERR_CHANOPRIVSNEEDED_MSG,
435 Channel_Name(Channel));
437 Req->argv[arg_arg][0] = '\0';
440 ok = IRC_WriteStrClient(Origin,
441 ERR_NEEDMOREPARAMS_MSG,
442 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);
480 case 'P': /* Persistent channel */
482 /* Only IRC operators are allowed to
483 * set the 'P' channel mode! */
484 if (set && !(Client_OperByMe(Client)
485 || Client_Type(Client) == CLIENT_SERVER))
486 ok = IRC_WriteStrClient(Origin,
487 ERR_NOPRIVILEGES_MSG,
492 ok = IRC_WriteStrClient(Origin,
493 ERR_CHANOPRIVSNEEDED_MSG,
495 Channel_Name(Channel));
497 /* --- Channel user modes --- */
498 case 'o': /* Channel operator */
499 case 'v': /* Voice */
500 if (arg_arg > mode_arg) {
502 client = Client_Search(Req->argv[arg_arg]);
506 ok = IRC_WriteStrClient(Client,
511 ok = IRC_WriteStrClient(Origin,
512 ERR_CHANOPRIVSNEEDED_MSG,
514 Channel_Name(Channel));
516 Req->argv[arg_arg][0] = '\0';
519 ok = IRC_WriteStrClient(Origin,
520 ERR_NEEDMOREPARAMS_MSG,
521 Client_ID(Origin), Req->command);
524 /* --- Channel lists --- */
525 case 'I': /* Invite lists */
526 case 'b': /* Ban lists */
527 if (arg_arg > mode_arg) {
531 ? Add_Ban_Invite(*mode_ptr, Origin,
534 : Del_Ban_Invite(*mode_ptr, Origin,
538 ok = IRC_WriteStrClient(Origin,
539 ERR_CHANOPRIVSNEEDED_MSG,
541 Channel_Name(Channel));
543 Req->argv[arg_arg][0] = '\0';
546 if (*mode_ptr == 'I')
547 Channel_ShowInvites(Origin, Channel);
549 Channel_ShowBans(Origin, Channel);
554 "Unknown mode \"%c%c\" from \"%s\" on %s!?",
555 set ? '+' : '-', *mode_ptr, Client_ID(Origin),
556 Channel_Name(Channel));
557 if (Client_Type(Client) != CLIENT_SERVER)
558 ok = IRC_WriteStrClient(Origin,
559 ERR_UMODEUNKNOWNFLAG2_MSG,
561 set ? '+' : '-', *mode_ptr);
569 /* Is there a valid mode change? */
573 /* Validate target client */
574 if (client && (!Channel_IsMemberOf(Channel, client))) {
575 if (!IRC_WriteStrClient
576 (Origin, ERR_USERNOTINCHANNEL_MSG,
577 Client_ID(Origin), Client_ID(client),
578 Channel_Name(Channel)))
585 /* Channel-User-Mode */
587 ? Channel_UserModeAdd(Channel, client, x[0])
588 : Channel_UserModeDel(Channel, client, x[0]);
590 strlcat(the_args, " ", sizeof(the_args));
591 strlcat(the_args, Client_ID(client),
593 strlcat(the_modes, x, sizeof(the_modes));
595 ("User \"%s\": Mode change on %s, now \"%s\"",
596 Client_Mask(client), Channel_Name(Channel),
597 Channel_UserModes(Channel, client));
602 ? Channel_ModeAdd(Channel, x[0])
603 : Channel_ModeDel(Channel, x[0]);
605 strlcat(the_modes, x, sizeof(the_modes));
606 LogDebug("Channel %s: Mode change, now \"%s\".",
607 Channel_Name(Channel),
608 Channel_Modes(Channel));
612 /* Are there additional arguments to add? */
614 strlcat(the_args, " ", sizeof(the_args));
615 strlcat(the_args, argadd, sizeof(the_args));
620 /* Are there changed modes? */
622 /* Clean up mode string */
623 len = strlen(the_modes) - 1;
624 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
625 the_modes[len] = '\0';
627 if (Client_Type(Client) == CLIENT_SERVER) {
628 /* MODE requests for local channels from other servers
629 * are definitely invalid! */
630 if (Channel_IsLocal(Channel)) {
631 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
635 /* Forward mode changes to channel users and all the
636 * other remote servers: */
637 IRC_WriteStrServersPrefix(Client, Origin,
638 "MODE %s %s%s", Channel_Name(Channel),
639 the_modes, the_args);
640 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
641 false, "MODE %s %s%s", Channel_Name(Channel),
642 the_modes, the_args);
645 Origin = Client_ThisServer();
646 /* Send reply to client and inform other servers and channel users */
647 ok = IRC_WriteStrClientPrefix(Client, Origin,
648 "MODE %s %s%s", Channel_Name(Channel),
649 the_modes, the_args);
650 /* Only forward requests for non-local channels */
651 if (!Channel_IsLocal(Channel))
652 IRC_WriteStrServersPrefix(Client, Origin,
653 "MODE %s %s%s", Channel_Name(Channel),
654 the_modes, the_args);
655 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
656 false, "MODE %s %s%s", Channel_Name(Channel),
657 the_modes, the_args);
661 IRC_SetPenalty(Client, 1);
667 IRC_AWAY( CLIENT *Client, REQUEST *Req )
669 assert( Client != NULL );
670 assert( Req != NULL );
672 /* Falsche Anzahl Parameter? */
673 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
675 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
678 Client_SetAway( Client, Req->argv[0] );
679 Client_ModeAdd( Client, 'a' );
680 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
681 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
686 Client_ModeDel( Client, 'a' );
687 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
688 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
694 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
700 assert( Client != NULL );
701 assert( Channel != NULL );
702 assert( Pattern != NULL );
703 assert(what == 'I' || what == 'b');
705 mask = Lists_MakeMask(Pattern);
707 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
710 ret = Channel_AddInvite(Channel, mask, false);
712 ret = Channel_AddBan(Channel, mask);
716 if (already && (Client_Type(Prefix) == CLIENT_SERVER))
720 return Send_ListChange("+I", Prefix, Client, Channel, mask);
721 return Send_ListChange("+b", Prefix, Client, Channel, mask);
726 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
729 struct list_head *list;
731 assert( Client != NULL );
732 assert( Channel != NULL );
733 assert( Pattern != NULL );
734 assert(what == 'I' || what == 'b');
736 mask = Lists_MakeMask( Pattern );
739 list = Channel_GetListInvites(Channel);
741 list = Channel_GetListBans(Channel);
743 Lists_Del(list, mask);
745 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
746 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
751 Send_ListChange( char *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Mask )
753 /* Bestaetigung an Client schicken & andere Server sowie Channel-User informieren */
757 if( Client_Type( Client ) == CLIENT_USER )
759 /* Bestaetigung an Client */
760 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
764 /* an andere Server */
765 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
767 /* und lokale User im Channel */
768 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
771 } /* Send_ListChange */