2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2010 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"
38 static bool Client_Mode PARAMS(( CLIENT *Client, REQUEST *Req, CLIENT *Origin,
40 static bool Channel_Mode PARAMS(( CLIENT *Client, REQUEST *Req, CLIENT *Origin,
43 static bool Add_Ban_Invite PARAMS((int what, CLIENT *Prefix, CLIENT *Client,
44 CHANNEL *Channel, const char *Pattern));
45 static bool Del_Ban_Invite PARAMS((int what, CLIENT *Prefix, CLIENT *Client,
46 CHANNEL *Channel, const char *Pattern));
48 static bool Send_ListChange PARAMS((const char *Mode, CLIENT *Prefix,
49 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 'c': /* Receive connect notices
180 * (only settable by IRC operators!) */
181 if(!set || Client_OperByMe(Origin)
182 || Client_Type(Client) == CLIENT_SERVER)
185 ok = IRC_WriteStrClient(Origin,
186 ERR_NOPRIVILEGES_MSG,
189 case 'o': /* IRC operator (only unsettable!) */
190 if(( ! set ) || ( Client_Type( Client ) == CLIENT_SERVER ))
192 Client_SetOperByMe( Target, false );
195 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
198 case 'r': /* Restricted (only settable) */
199 if(( set ) || ( Client_Type( Client ) == CLIENT_SERVER )) x[0] = 'r';
200 else ok = IRC_WriteStrClient( Origin, ERR_RESTRICTED_MSG, Client_ID( Origin ));
204 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\"!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ));
205 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
211 /* Is there a valid mode change? */
212 if( ! x[0] ) continue;
217 if( Client_ModeAdd( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
223 if( Client_ModeDel( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
228 /* Are there changed modes? */
231 /* Remoce needless action modifier characters */
232 len = strlen( the_modes ) - 1;
233 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' )) the_modes[len] = '\0';
235 if( Client_Type( Client ) == CLIENT_SERVER )
237 /* Forward modes to other servers */
238 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
242 /* Send reply to client and inform other servers */
243 ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
244 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
246 LogDebug("%s \"%s\": Mode change, now \"%s\".",
247 Client_TypeText(Target), Client_Mask(Target),
248 Client_Modes(Target));
251 IRC_SetPenalty( Client, 1 );
257 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
259 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
260 const char *mode_ptr;
262 /* Member or not? -- That's the question! */
263 if (!Channel_IsMemberOf(Channel, Origin))
264 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
265 Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
267 /* The sender is a member: generate extended reply */
268 strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
269 mode_ptr = the_modes;
275 snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
276 strlcat(the_args, argadd, sizeof(the_args));
279 strlcat(the_args, " ", sizeof(the_args));
280 strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
286 strlcat(the_modes, the_args, sizeof(the_modes));
288 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
289 Client_ID(Origin), Channel_Name(Channel), the_modes);
294 * Handle channel mode and channel-user mode changes
297 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
299 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
300 argadd[CLIENT_PASS_LEN], *mode_ptr;
301 bool connected, set, modeok = true, skiponce, use_servermode = false, retval;
302 int mode_arg, arg_arg;
307 if (Channel_IsModeless(Channel))
308 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
309 Client_ID(Client), Channel_Name(Channel));
311 /* Mode request: let's answer it :-) */
313 return Channel_Mode_Answer_Request(Origin, Channel);
315 /* Is the user allowed to change modes? */
316 if (Client_Type(Client) == CLIENT_USER) {
317 /* Is the originating user on that channel? */
318 if (!Channel_IsMemberOf(Channel, Origin))
319 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
320 Client_ID(Origin), Channel_Name(Channel));
322 /* channel operator? */
323 if (strchr(Channel_UserModes(Channel, Origin), 'o'))
325 else if (Conf_OperCanMode) {
326 /* IRC-Operators can use MODE as well */
327 if (Client_OperByMe(Origin)) {
329 if (Conf_OperServerMode)
330 use_servermode = true; /* Change Origin to Server */
336 mode_ptr = Req->argv[mode_arg];
337 if (Req->argc > mode_arg + 1)
338 arg_arg = mode_arg + 1;
342 /* Initial state: set or unset modes? */
356 /* Prepare reply string */
357 strcpy(the_modes, set ? "+" : "-");
361 connected = CONNECTED;
366 /* Try next argument if there's any */
367 if (arg_arg > mode_arg)
372 if (mode_arg >= Req->argc)
374 mode_ptr = Req->argv[mode_arg];
376 if (Req->argc > mode_arg + 1)
377 arg_arg = mode_arg + 1;
386 if (((*mode_ptr == '+') && !set)
387 || ((*mode_ptr == '-') && set)) {
388 /* Action modifier ("+"/"-") must be changed ... */
389 len = strlen(the_modes) - 1;
390 if (the_modes[len] == '+' || the_modes[len] == '-') {
391 /* Adjust last action modifier in result */
392 the_modes[len] = *mode_ptr;
394 /* Append modifier character to result string */
396 strlcat(the_modes, x, sizeof(the_modes));
398 set = *mode_ptr == '+';
403 /* Are there arguments left? */
404 if (arg_arg >= Req->argc)
412 /* --- Channel modes --- */
413 case 'i': /* Invite only */
414 case 'm': /* Moderated */
415 case 'n': /* Only members can write */
416 case 's': /* Secret channel */
417 case 't': /* Topic locked */
418 case 'z': /* Secure connections only */
422 connected = IRC_WriteStrClient(Origin,
423 ERR_CHANOPRIVSNEEDED_MSG,
424 Client_ID(Origin), Channel_Name(Channel));
426 case 'k': /* Channel key */
431 connected = IRC_WriteStrClient(Origin,
432 ERR_CHANOPRIVSNEEDED_MSG,
434 Channel_Name(Channel));
437 if (arg_arg > mode_arg) {
439 Channel_ModeDel(Channel, 'k');
440 Channel_SetKey(Channel,
442 strlcpy(argadd, Channel_Key(Channel),
446 connected = IRC_WriteStrClient(Origin,
447 ERR_CHANOPRIVSNEEDED_MSG,
449 Channel_Name(Channel));
451 Req->argv[arg_arg][0] = '\0';
454 connected = IRC_WriteStrClient(Origin,
455 ERR_NEEDMOREPARAMS_MSG,
456 Client_ID(Origin), Req->command);
460 case 'l': /* Member limit */
465 connected = IRC_WriteStrClient(Origin,
466 ERR_CHANOPRIVSNEEDED_MSG,
468 Channel_Name(Channel));
471 if (arg_arg > mode_arg) {
473 l = atol(Req->argv[arg_arg]);
474 if (l > 0 && l < 0xFFFF) {
475 Channel_ModeDel(Channel, 'l');
476 Channel_SetMaxUsers(Channel, l);
477 snprintf(argadd, sizeof(argadd),
482 connected = IRC_WriteStrClient(Origin,
483 ERR_CHANOPRIVSNEEDED_MSG,
485 Channel_Name(Channel));
487 Req->argv[arg_arg][0] = '\0';
490 connected = IRC_WriteStrClient(Origin,
491 ERR_NEEDMOREPARAMS_MSG,
492 Client_ID(Origin), Req->command);
496 case 'P': /* Persistent channel */
498 /* Only IRC operators are allowed to
499 * set the 'P' channel mode! */
500 if (set && !(Client_OperByMe(Client)
501 || Client_Type(Client) == CLIENT_SERVER))
502 connected = IRC_WriteStrClient(Origin,
503 ERR_NOPRIVILEGES_MSG,
508 connected = IRC_WriteStrClient(Origin,
509 ERR_CHANOPRIVSNEEDED_MSG,
511 Channel_Name(Channel));
513 /* --- Channel user modes --- */
514 case 'o': /* Channel operator */
515 case 'v': /* Voice */
516 if (arg_arg > mode_arg) {
518 client = Client_Search(Req->argv[arg_arg]);
522 connected = IRC_WriteStrClient(Client,
527 connected = IRC_WriteStrClient(Origin,
528 ERR_CHANOPRIVSNEEDED_MSG,
530 Channel_Name(Channel));
532 Req->argv[arg_arg][0] = '\0';
535 connected = IRC_WriteStrClient(Origin,
536 ERR_NEEDMOREPARAMS_MSG,
537 Client_ID(Origin), Req->command);
541 /* --- Channel lists --- */
542 case 'I': /* Invite lists */
543 case 'b': /* Ban lists */
544 if (arg_arg > mode_arg) {
548 ? Add_Ban_Invite(*mode_ptr, Origin,
551 : Del_Ban_Invite(*mode_ptr, Origin,
555 connected = IRC_WriteStrClient(Origin,
556 ERR_CHANOPRIVSNEEDED_MSG,
558 Channel_Name(Channel));
560 Req->argv[arg_arg][0] = '\0';
563 if (*mode_ptr == 'I')
564 Channel_ShowInvites(Origin, Channel);
566 Channel_ShowBans(Origin, Channel);
571 "Unknown mode \"%c%c\" from \"%s\" on %s!?",
572 set ? '+' : '-', *mode_ptr, Client_ID(Origin),
573 Channel_Name(Channel));
574 if (Client_Type(Client) != CLIENT_SERVER)
575 connected = IRC_WriteStrClient(Origin,
576 ERR_UMODEUNKNOWNFLAG2_MSG,
578 set ? '+' : '-', *mode_ptr);
586 /* Is there a valid mode change? */
590 /* Validate target client */
591 if (client && (!Channel_IsMemberOf(Channel, client))) {
592 if (!IRC_WriteStrClient
593 (Origin, ERR_USERNOTINCHANNEL_MSG,
594 Client_ID(Origin), Client_ID(client),
595 Channel_Name(Channel)))
602 /* Channel-User-Mode */
604 ? Channel_UserModeAdd(Channel, client, x[0])
605 : Channel_UserModeDel(Channel, client, x[0]);
607 strlcat(the_args, " ", sizeof(the_args));
608 strlcat(the_args, Client_ID(client),
610 strlcat(the_modes, x, sizeof(the_modes));
612 ("User \"%s\": Mode change on %s, now \"%s\"",
613 Client_Mask(client), Channel_Name(Channel),
614 Channel_UserModes(Channel, client));
619 ? Channel_ModeAdd(Channel, x[0])
620 : Channel_ModeDel(Channel, x[0]);
622 strlcat(the_modes, x, sizeof(the_modes));
623 LogDebug("Channel %s: Mode change, now \"%s\".",
624 Channel_Name(Channel),
625 Channel_Modes(Channel));
629 /* Are there additional arguments to add? */
631 strlcat(the_args, " ", sizeof(the_args));
632 strlcat(the_args, argadd, sizeof(the_args));
637 /* Are there changed modes? */
639 /* Clean up mode string */
640 len = strlen(the_modes) - 1;
641 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
642 the_modes[len] = '\0';
644 if (Client_Type(Client) == CLIENT_SERVER) {
645 /* MODE requests for local channels from other servers
646 * are definitely invalid! */
647 if (Channel_IsLocal(Channel)) {
648 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
652 /* Forward mode changes to channel users and all the
653 * other remote servers: */
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);
662 Origin = Client_ThisServer();
663 /* Send reply to client and inform other servers and channel users */
664 connected = IRC_WriteStrClientPrefix(Client, Origin,
665 "MODE %s %s%s", Channel_Name(Channel),
666 the_modes, the_args);
667 /* Only forward requests for non-local channels */
668 if (!Channel_IsLocal(Channel))
669 IRC_WriteStrServersPrefix(Client, Origin,
670 "MODE %s %s%s", Channel_Name(Channel),
671 the_modes, the_args);
672 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
673 false, "MODE %s %s%s", Channel_Name(Channel),
674 the_modes, the_args);
678 IRC_SetPenalty(Client, 1);
684 IRC_AWAY( CLIENT *Client, REQUEST *Req )
686 assert( Client != NULL );
687 assert( Req != NULL );
689 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
691 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
693 Client_SetAway( Client, Req->argv[0] );
694 Client_ModeAdd( Client, 'a' );
695 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
696 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
700 Client_ModeDel( Client, 'a' );
701 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
702 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
708 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
714 assert( Client != NULL );
715 assert( Channel != NULL );
716 assert( Pattern != NULL );
717 assert(what == 'I' || what == 'b');
719 mask = Lists_MakeMask(Pattern);
721 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
724 ret = Channel_AddInvite(Channel, mask, false);
726 ret = Channel_AddBan(Channel, mask);
730 if (already && (Client_Type(Prefix) == CLIENT_SERVER))
734 return Send_ListChange("+I", Prefix, Client, Channel, mask);
735 return Send_ListChange("+b", Prefix, Client, Channel, mask);
740 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
743 struct list_head *list;
745 assert( Client != NULL );
746 assert( Channel != NULL );
747 assert( Pattern != NULL );
748 assert(what == 'I' || what == 'b');
750 mask = Lists_MakeMask( Pattern );
753 list = Channel_GetListInvites(Channel);
755 list = Channel_GetListBans(Channel);
757 Lists_Del(list, mask);
759 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
760 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
765 Send_ListChange(const char *Mode, CLIENT *Prefix, CLIENT *Client,
766 CHANNEL *Channel, const char *Mask)
770 if( Client_Type( Client ) == CLIENT_USER )
772 /* send confirmation to client */
773 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
777 /* to other servers */
778 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
780 /* and local users in channel */
781 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
784 } /* Send_ListChange */