2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2011 Alexander Barton (alex@barton.de) and Contributors.
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.
16 * IRC commands for mode changes (like MODE, AWAY, etc.)
28 #include "irc-write.h"
39 static bool Client_Mode PARAMS(( CLIENT *Client, REQUEST *Req, CLIENT *Origin,
41 static bool Channel_Mode PARAMS(( CLIENT *Client, REQUEST *Req, CLIENT *Origin,
44 static bool Add_Ban_Invite PARAMS((int what, CLIENT *Prefix, CLIENT *Client,
45 CHANNEL *Channel, const char *Pattern));
46 static bool Del_Ban_Invite PARAMS((int what, CLIENT *Prefix, CLIENT *Client,
47 CHANNEL *Channel, const char *Pattern));
49 static bool Send_ListChange PARAMS((const char *Mode, CLIENT *Prefix,
50 CLIENT *Client, CHANNEL *Channel, const char *Mask));
54 IRC_MODE( CLIENT *Client, REQUEST *Req )
59 assert( Client != NULL );
60 assert( Req != NULL );
63 if( Req->argc < 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
65 /* Origin for answers */
66 if( Client_Type( Client ) == CLIENT_SERVER )
68 origin = Client_Search( Req->prefix );
69 if( ! origin ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
73 /* Channel or user mode? */
74 cl = NULL; chan = NULL;
75 if (Client_IsValidNick(Req->argv[0]))
76 cl = Client_Search(Req->argv[0]);
77 if (Channel_IsValidName(Req->argv[0]))
78 chan = Channel_Search(Req->argv[0]);
81 return Client_Mode(Client, Req, origin, cl);
83 return Channel_Mode(Client, Req, origin, chan);
85 /* No target found! */
86 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
87 Client_ID(Client), Req->argv[0]);
92 Client_Mode( CLIENT *Client, REQUEST *Req, CLIENT *Origin, CLIENT *Target )
94 /* Handle client mode requests */
96 char the_modes[COMMAND_LEN], x[2], *mode_ptr;
101 /* Is the client allowed to request or change the modes? */
102 if( Client_Type( Client ) == CLIENT_USER )
104 /* Users are only allowed to manipulate their own modes! */
105 if( Target != Client ) return IRC_WriteStrClient( Client, ERR_USERSDONTMATCH_MSG, Client_ID( Client ));
108 /* Mode request: let's answer it :-) */
109 if( Req->argc == 1 ) return IRC_WriteStrClient( Origin, RPL_UMODEIS_MSG, Client_ID( Origin ), Client_Modes( Target ));
112 mode_ptr = Req->argv[mode_arg];
114 /* Initial state: set or unset modes? */
115 if( *mode_ptr == '+' ) set = true;
116 else if( *mode_ptr == '-' ) set = false;
117 else return IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG_MSG, Client_ID( Origin ));
119 /* Prepare reply string */
120 if( set ) strcpy( the_modes, "+" );
121 else strcpy( the_modes, "-" );
130 /* Try next argument if there's any */
132 if( mode_arg < Req->argc ) mode_ptr = Req->argv[mode_arg];
140 if((( *mode_ptr == '+' ) && ( ! set )) || (( *mode_ptr == '-' ) && ( set )))
142 /* Action modifier ("+"/"-") must be changed ... */
143 len = strlen( the_modes ) - 1;
144 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' ))
146 /* Adjust last action modifier in result */
147 the_modes[len] = *mode_ptr;
151 /* Append modifier character to result string */
153 strlcat( the_modes, x, sizeof( the_modes ));
155 if( *mode_ptr == '+' ) set = true;
165 case 'i': /* Invisible */
166 case 's': /* Server messages */
167 case 'w': /* Wallops messages */
172 if( Client_Type( Client ) == CLIENT_SERVER )
175 Client_SetAway( Origin, DEFAULT_AWAY_MSG );
177 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
180 case 'c': /* Receive connect notices
181 * (only settable by IRC operators!) */
182 if(!set || Client_OperByMe(Origin)
183 || Client_Type(Client) == CLIENT_SERVER)
186 ok = IRC_WriteStrClient(Origin,
187 ERR_NOPRIVILEGES_MSG,
191 case 'o': /* IRC operator (only unsettable!) */
192 if(( ! set ) || ( Client_Type( Client ) == CLIENT_SERVER ))
194 Client_SetOperByMe( Target, false );
197 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
200 case 'r': /* Restricted (only settable) */
201 if(( set ) || ( Client_Type( Client ) == CLIENT_SERVER )) x[0] = 'r';
202 else ok = IRC_WriteStrClient( Origin, ERR_RESTRICTED_MSG, Client_ID( Origin ));
205 case 'x': /* Cloak hostname */
206 if (Client_HasMode(Client, 'r'))
207 ok = IRC_WriteStrClient(Origin,
215 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\"!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ));
216 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
222 /* Is there a valid mode change? */
223 if( ! x[0] ) continue;
228 if( Client_ModeAdd( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
234 if( Client_ModeDel( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
239 /* Are there changed modes? */
242 /* Remoce needless action modifier characters */
243 len = strlen( the_modes ) - 1;
244 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' )) the_modes[len] = '\0';
246 if( Client_Type( Client ) == CLIENT_SERVER )
248 /* Forward modes to other servers */
249 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
253 /* Send reply to client and inform other servers */
254 ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
255 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
257 LogDebug("%s \"%s\": Mode change, now \"%s\".",
258 Client_TypeText(Target), Client_Mask(Target),
259 Client_Modes(Target));
262 IRC_SetPenalty( Client, 1 );
268 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
270 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
271 const char *mode_ptr;
273 /* Member or not? -- That's the question! */
274 if (!Channel_IsMemberOf(Channel, Origin))
275 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
276 Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
278 /* The sender is a member: generate extended reply */
279 strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
280 mode_ptr = the_modes;
286 snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
287 strlcat(the_args, argadd, sizeof(the_args));
290 strlcat(the_args, " ", sizeof(the_args));
291 strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
297 strlcat(the_modes, the_args, sizeof(the_modes));
299 if (!IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
300 Client_ID(Origin), Channel_Name(Channel),
304 if (!IRC_WriteStrClient(Origin, RPL_CREATIONTIME_MSG,
305 Client_ID(Origin), Channel_Name(Channel),
306 Channel_CreationTime(Channel)))
314 * Handle channel mode and channel-user mode changes
317 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
319 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
320 argadd[CLIENT_PASS_LEN], *mode_ptr;
321 bool connected, set, skiponce, retval, onchannel, modeok, use_servermode;
322 int mode_arg, arg_arg;
327 if (Channel_IsModeless(Channel))
328 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
329 Client_ID(Client), Channel_Name(Channel));
331 /* Mode request: let's answer it :-) */
333 return Channel_Mode_Answer_Request(Origin, Channel);
335 Channel_CheckAdminRights(Channel, Client, Origin,
336 &onchannel, &modeok, &use_servermode);
338 if (!onchannel && !modeok)
339 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
341 Channel_Name(Channel));
344 mode_ptr = Req->argv[mode_arg];
345 if (Req->argc > mode_arg + 1)
346 arg_arg = mode_arg + 1;
350 /* Initial state: set or unset modes? */
364 /* Prepare reply string */
365 strcpy(the_modes, set ? "+" : "-");
369 connected = CONNECTED;
374 /* Try next argument if there's any */
375 if (arg_arg > mode_arg)
380 if (mode_arg >= Req->argc)
382 mode_ptr = Req->argv[mode_arg];
384 if (Req->argc > mode_arg + 1)
385 arg_arg = mode_arg + 1;
394 if (((*mode_ptr == '+') && !set)
395 || ((*mode_ptr == '-') && set)) {
396 /* Action modifier ("+"/"-") must be changed ... */
397 len = strlen(the_modes) - 1;
398 if (the_modes[len] == '+' || the_modes[len] == '-') {
399 /* Adjust last action modifier in result */
400 the_modes[len] = *mode_ptr;
402 /* Append modifier character to result string */
404 strlcat(the_modes, x, sizeof(the_modes));
406 set = *mode_ptr == '+';
411 /* Are there arguments left? */
412 if (arg_arg >= Req->argc)
420 /* --- Channel modes --- */
421 case 'i': /* Invite only */
422 case 'm': /* Moderated */
423 case 'n': /* Only members can write */
424 case 's': /* Secret channel */
425 case 't': /* Topic locked */
426 case 'z': /* Secure connections only */
430 connected = IRC_WriteStrClient(Origin,
431 ERR_CHANOPRIVSNEEDED_MSG,
432 Client_ID(Origin), Channel_Name(Channel));
434 case 'k': /* Channel key */
439 connected = IRC_WriteStrClient(Origin,
440 ERR_CHANOPRIVSNEEDED_MSG,
442 Channel_Name(Channel));
445 if (arg_arg > mode_arg) {
447 Channel_ModeDel(Channel, 'k');
448 Channel_SetKey(Channel,
450 strlcpy(argadd, Channel_Key(Channel),
454 connected = IRC_WriteStrClient(Origin,
455 ERR_CHANOPRIVSNEEDED_MSG,
457 Channel_Name(Channel));
459 Req->argv[arg_arg][0] = '\0';
462 connected = IRC_WriteStrClient(Origin,
463 ERR_NEEDMOREPARAMS_MSG,
464 Client_ID(Origin), Req->command);
468 case 'l': /* Member limit */
473 connected = IRC_WriteStrClient(Origin,
474 ERR_CHANOPRIVSNEEDED_MSG,
476 Channel_Name(Channel));
479 if (arg_arg > mode_arg) {
481 l = atol(Req->argv[arg_arg]);
482 if (l > 0 && l < 0xFFFF) {
483 Channel_ModeDel(Channel, 'l');
484 Channel_SetMaxUsers(Channel, l);
485 snprintf(argadd, sizeof(argadd),
490 connected = IRC_WriteStrClient(Origin,
491 ERR_CHANOPRIVSNEEDED_MSG,
493 Channel_Name(Channel));
495 Req->argv[arg_arg][0] = '\0';
498 connected = IRC_WriteStrClient(Origin,
499 ERR_NEEDMOREPARAMS_MSG,
500 Client_ID(Origin), Req->command);
504 case 'O': /* IRC operators only */
506 /* Only IRC operators are allowed to
507 * set the 'O' channel mode! */
508 if (set && !(Client_OperByMe(Client)
509 || Client_Type(Client) == CLIENT_SERVER))
510 connected = IRC_WriteStrClient(Origin,
511 ERR_NOPRIVILEGES_MSG,
516 connected = IRC_WriteStrClient(Origin,
517 ERR_CHANOPRIVSNEEDED_MSG,
519 Channel_Name(Channel));
521 case 'P': /* Persistent channel */
523 /* Only IRC operators are allowed to
524 * set the 'P' channel mode! */
525 if (set && !(Client_OperByMe(Client)
526 || Client_Type(Client) == CLIENT_SERVER))
527 connected = IRC_WriteStrClient(Origin,
528 ERR_NOPRIVILEGES_MSG,
533 connected = IRC_WriteStrClient(Origin,
534 ERR_CHANOPRIVSNEEDED_MSG,
536 Channel_Name(Channel));
538 /* --- Channel user modes --- */
539 case 'o': /* Channel operator */
540 case 'v': /* Voice */
541 if (arg_arg > mode_arg) {
543 client = Client_Search(Req->argv[arg_arg]);
547 connected = IRC_WriteStrClient(Client,
552 connected = IRC_WriteStrClient(Origin,
553 ERR_CHANOPRIVSNEEDED_MSG,
555 Channel_Name(Channel));
557 Req->argv[arg_arg][0] = '\0';
560 connected = IRC_WriteStrClient(Origin,
561 ERR_NEEDMOREPARAMS_MSG,
562 Client_ID(Origin), Req->command);
566 /* --- Channel lists --- */
567 case 'I': /* Invite lists */
568 case 'b': /* Ban lists */
569 if (arg_arg > mode_arg) {
573 ? Add_Ban_Invite(*mode_ptr, Origin,
576 : Del_Ban_Invite(*mode_ptr, Origin,
580 connected = IRC_WriteStrClient(Origin,
581 ERR_CHANOPRIVSNEEDED_MSG,
583 Channel_Name(Channel));
585 Req->argv[arg_arg][0] = '\0';
588 if (*mode_ptr == 'I')
589 Channel_ShowInvites(Origin, Channel);
591 Channel_ShowBans(Origin, Channel);
596 "Unknown mode \"%c%c\" from \"%s\" on %s!?",
597 set ? '+' : '-', *mode_ptr, Client_ID(Origin),
598 Channel_Name(Channel));
599 if (Client_Type(Client) != CLIENT_SERVER)
600 connected = IRC_WriteStrClient(Origin,
601 ERR_UMODEUNKNOWNFLAG2_MSG,
603 set ? '+' : '-', *mode_ptr);
611 /* Is there a valid mode change? */
615 /* Validate target client */
616 if (client && (!Channel_IsMemberOf(Channel, client))) {
617 if (!IRC_WriteStrClient
618 (Origin, ERR_USERNOTINCHANNEL_MSG,
619 Client_ID(Origin), Client_ID(client),
620 Channel_Name(Channel)))
627 /* Channel-User-Mode */
629 ? Channel_UserModeAdd(Channel, client, x[0])
630 : Channel_UserModeDel(Channel, client, x[0]);
632 strlcat(the_args, " ", sizeof(the_args));
633 strlcat(the_args, Client_ID(client),
635 strlcat(the_modes, x, sizeof(the_modes));
637 ("User \"%s\": Mode change on %s, now \"%s\"",
638 Client_Mask(client), Channel_Name(Channel),
639 Channel_UserModes(Channel, client));
644 ? Channel_ModeAdd(Channel, x[0])
645 : Channel_ModeDel(Channel, x[0]);
647 strlcat(the_modes, x, sizeof(the_modes));
648 LogDebug("Channel %s: Mode change, now \"%s\".",
649 Channel_Name(Channel),
650 Channel_Modes(Channel));
654 /* Are there additional arguments to add? */
656 strlcat(the_args, " ", sizeof(the_args));
657 strlcat(the_args, argadd, sizeof(the_args));
662 /* Are there changed modes? */
664 /* Clean up mode string */
665 len = strlen(the_modes) - 1;
666 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
667 the_modes[len] = '\0';
669 if (Client_Type(Client) == CLIENT_SERVER) {
670 /* MODE requests for local channels from other servers
671 * are definitely invalid! */
672 if (Channel_IsLocal(Channel)) {
673 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
677 /* Forward mode changes to channel users and all the
678 * other remote servers: */
679 IRC_WriteStrServersPrefix(Client, Origin,
680 "MODE %s %s%s", Channel_Name(Channel),
681 the_modes, the_args);
682 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
683 false, "MODE %s %s%s", Channel_Name(Channel),
684 the_modes, the_args);
687 Origin = Client_ThisServer();
688 /* Send reply to client and inform other servers and channel users */
689 connected = IRC_WriteStrClientPrefix(Client, Origin,
690 "MODE %s %s%s", Channel_Name(Channel),
691 the_modes, the_args);
692 /* Only forward requests for non-local channels */
693 if (!Channel_IsLocal(Channel))
694 IRC_WriteStrServersPrefix(Client, Origin,
695 "MODE %s %s%s", Channel_Name(Channel),
696 the_modes, the_args);
697 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
698 false, "MODE %s %s%s", Channel_Name(Channel),
699 the_modes, the_args);
703 IRC_SetPenalty(Client, 1);
709 IRC_AWAY( CLIENT *Client, REQUEST *Req )
711 assert( Client != NULL );
712 assert( Req != NULL );
714 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
716 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
718 Client_SetAway( Client, Req->argv[0] );
719 Client_ModeAdd( Client, 'a' );
720 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
721 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
725 Client_ModeDel( Client, 'a' );
726 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
727 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
733 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
739 assert( Client != NULL );
740 assert( Channel != NULL );
741 assert( Pattern != NULL );
742 assert(what == 'I' || what == 'b');
744 mask = Lists_MakeMask(Pattern);
746 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
749 ret = Channel_AddInvite(Channel, mask, false);
751 ret = Channel_AddBan(Channel, mask);
755 if (already && (Client_Type(Prefix) == CLIENT_SERVER))
759 return Send_ListChange("+I", Prefix, Client, Channel, mask);
760 return Send_ListChange("+b", Prefix, Client, Channel, mask);
765 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
768 struct list_head *list;
770 assert( Client != NULL );
771 assert( Channel != NULL );
772 assert( Pattern != NULL );
773 assert(what == 'I' || what == 'b');
775 mask = Lists_MakeMask( Pattern );
778 list = Channel_GetListInvites(Channel);
780 list = Channel_GetListBans(Channel);
782 Lists_Del(list, mask);
784 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
785 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
790 Send_ListChange(const char *Mode, CLIENT *Prefix, CLIENT *Client,
791 CHANNEL *Channel, const char *Mask)
795 if( Client_Type( Client ) == CLIENT_USER )
797 /* send confirmation to client */
798 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
802 /* to other servers */
803 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
805 /* and local users in channel */
806 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
809 } /* Send_ListChange */