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"
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 'c': /* Receive connect notices
176 * (only settable by IRC operators!) */
177 if(!set || Client_OperByMe(Origin)
178 || Client_Type(Client) == CLIENT_SERVER)
181 ok = IRC_WriteStrClient(Origin,
182 ERR_NOPRIVILEGES_MSG,
185 case 'o': /* IRC operator (only unsettable!) */
186 if(( ! set ) || ( Client_Type( Client ) == CLIENT_SERVER ))
188 Client_SetOperByMe( Target, false );
191 else ok = IRC_WriteStrClient( Origin, ERR_NOPRIVILEGES_MSG, Client_ID( Origin ));
194 case 'r': /* Restricted (only settable) */
195 if(( set ) || ( Client_Type( Client ) == CLIENT_SERVER )) x[0] = 'r';
196 else ok = IRC_WriteStrClient( Origin, ERR_RESTRICTED_MSG, Client_ID( Origin ));
200 Log( LOG_DEBUG, "Unknown mode \"%c%c\" from \"%s\"!?", set ? '+' : '-', *mode_ptr, Client_ID( Origin ));
201 if( Client_Type( Client ) != CLIENT_SERVER ) ok = IRC_WriteStrClient( Origin, ERR_UMODEUNKNOWNFLAG2_MSG, Client_ID( Origin ), set ? '+' : '-', *mode_ptr );
207 /* Is there a valid mode change? */
208 if( ! x[0] ) continue;
213 if( Client_ModeAdd( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
219 if( Client_ModeDel( Target, x[0] )) strlcat( the_modes, x, sizeof( the_modes ));
224 /* Are there changed modes? */
227 /* Remoce needless action modifier characters */
228 len = strlen( the_modes ) - 1;
229 if(( the_modes[len] == '+' ) || ( the_modes[len] == '-' )) the_modes[len] = '\0';
231 if( Client_Type( Client ) == CLIENT_SERVER )
233 /* Forward modes to other servers */
234 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
238 /* Send reply to client and inform other servers */
239 ok = IRC_WriteStrClientPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
240 IRC_WriteStrServersPrefix( Client, Origin, "MODE %s :%s", Client_ID( Target ), the_modes );
242 LogDebug("%s \"%s\": Mode change, now \"%s\".",
243 Client_TypeText(Target), Client_Mask(Target),
244 Client_Modes(Target));
247 IRC_SetPenalty( Client, 1 );
253 Channel_Mode_Answer_Request(CLIENT *Origin, CHANNEL *Channel)
255 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], argadd[CLIENT_PASS_LEN];
256 const char *mode_ptr;
258 /* Member or not? -- That's the question! */
259 if (!Channel_IsMemberOf(Channel, Origin))
260 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
261 Client_ID(Origin), Channel_Name(Channel), Channel_Modes(Channel));
263 /* The sender is a member: generate extended reply */
264 strlcpy(the_modes, Channel_Modes(Channel), sizeof(the_modes));
265 mode_ptr = the_modes;
271 snprintf(argadd, sizeof(argadd), " %lu", Channel_MaxUsers(Channel));
272 strlcat(the_args, argadd, sizeof(the_args));
275 strlcat(the_args, " ", sizeof(the_args));
276 strlcat(the_args, Channel_Key(Channel), sizeof(the_args));
282 strlcat(the_modes, the_args, sizeof(the_modes));
284 return IRC_WriteStrClient(Origin, RPL_CHANNELMODEIS_MSG,
285 Client_ID(Origin), Channel_Name(Channel), the_modes);
290 * Handle channel mode and channel-user mode changes
293 Channel_Mode(CLIENT *Client, REQUEST *Req, CLIENT *Origin, CHANNEL *Channel)
295 char the_modes[COMMAND_LEN], the_args[COMMAND_LEN], x[2],
296 argadd[CLIENT_PASS_LEN], *mode_ptr;
297 bool connected, set, modeok = true, skiponce, use_servermode = false, retval;
298 int mode_arg, arg_arg;
303 if (Channel_IsModeless(Channel))
304 return IRC_WriteStrClient(Client, ERR_NOCHANMODES_MSG,
305 Client_ID(Client), Channel_Name(Channel));
307 /* Mode request: let's answer it :-) */
309 return Channel_Mode_Answer_Request(Origin, Channel);
311 /* Is the user allowed to change modes? */
312 if (Client_Type(Client) == CLIENT_USER) {
313 /* Is the originating user on that channel? */
314 if (!Channel_IsMemberOf(Channel, Origin))
315 return IRC_WriteStrClient(Origin, ERR_NOTONCHANNEL_MSG,
316 Client_ID(Origin), Channel_Name(Channel));
318 /* channel operator? */
319 if (strchr(Channel_UserModes(Channel, Origin), 'o'))
321 else if (Conf_OperCanMode) {
322 /* IRC-Operators can use MODE as well */
323 if (Client_OperByMe(Origin)) {
325 if (Conf_OperServerMode)
326 use_servermode = true; /* Change Origin to Server */
332 mode_ptr = Req->argv[mode_arg];
333 if (Req->argc > mode_arg + 1)
334 arg_arg = mode_arg + 1;
338 /* Initial state: set or unset modes? */
352 /* Prepare reply string */
353 strcpy(the_modes, set ? "+" : "-");
357 connected = CONNECTED;
362 /* Try next argument if there's any */
363 if (arg_arg > mode_arg)
368 if (mode_arg >= Req->argc)
370 mode_ptr = Req->argv[mode_arg];
372 if (Req->argc > mode_arg + 1)
373 arg_arg = mode_arg + 1;
382 if (((*mode_ptr == '+') && !set)
383 || ((*mode_ptr == '-') && set)) {
384 /* Action modifier ("+"/"-") must be changed ... */
385 len = strlen(the_modes) - 1;
386 if (the_modes[len] == '+' || the_modes[len] == '-') {
387 /* Adjust last action modifier in result */
388 the_modes[len] = *mode_ptr;
390 /* Append modifier character to result string */
392 strlcat(the_modes, x, sizeof(the_modes));
394 set = *mode_ptr == '+';
399 /* Are there arguments left? */
400 if (arg_arg >= Req->argc)
408 /* --- Channel modes --- */
409 case 'i': /* Invite only */
410 case 'm': /* Moderated */
411 case 'n': /* Only members can write */
412 case 's': /* Secret channel */
413 case 't': /* Topic locked */
414 case 'z': /* Secure connections only */
418 connected = IRC_WriteStrClient(Origin,
419 ERR_CHANOPRIVSNEEDED_MSG,
420 Client_ID(Origin), Channel_Name(Channel));
422 case 'k': /* Channel key */
427 connected = IRC_WriteStrClient(Origin,
428 ERR_CHANOPRIVSNEEDED_MSG,
430 Channel_Name(Channel));
433 if (arg_arg > mode_arg) {
435 Channel_ModeDel(Channel, 'k');
436 Channel_SetKey(Channel,
438 strlcpy(argadd, Channel_Key(Channel),
442 connected = IRC_WriteStrClient(Origin,
443 ERR_CHANOPRIVSNEEDED_MSG,
445 Channel_Name(Channel));
447 Req->argv[arg_arg][0] = '\0';
450 connected = IRC_WriteStrClient(Origin,
451 ERR_NEEDMOREPARAMS_MSG,
452 Client_ID(Origin), Req->command);
456 case 'l': /* Member limit */
461 connected = IRC_WriteStrClient(Origin,
462 ERR_CHANOPRIVSNEEDED_MSG,
464 Channel_Name(Channel));
467 if (arg_arg > mode_arg) {
469 l = atol(Req->argv[arg_arg]);
470 if (l > 0 && l < 0xFFFF) {
471 Channel_ModeDel(Channel, 'l');
472 Channel_SetMaxUsers(Channel, l);
473 snprintf(argadd, sizeof(argadd),
478 connected = IRC_WriteStrClient(Origin,
479 ERR_CHANOPRIVSNEEDED_MSG,
481 Channel_Name(Channel));
483 Req->argv[arg_arg][0] = '\0';
486 connected = IRC_WriteStrClient(Origin,
487 ERR_NEEDMOREPARAMS_MSG,
488 Client_ID(Origin), Req->command);
492 case 'P': /* Persistent channel */
494 /* Only IRC operators are allowed to
495 * set the 'P' channel mode! */
496 if (set && !(Client_OperByMe(Client)
497 || Client_Type(Client) == CLIENT_SERVER))
498 connected = IRC_WriteStrClient(Origin,
499 ERR_NOPRIVILEGES_MSG,
504 connected = IRC_WriteStrClient(Origin,
505 ERR_CHANOPRIVSNEEDED_MSG,
507 Channel_Name(Channel));
509 /* --- Channel user modes --- */
510 case 'o': /* Channel operator */
511 case 'v': /* Voice */
512 if (arg_arg > mode_arg) {
514 client = Client_Search(Req->argv[arg_arg]);
518 connected = IRC_WriteStrClient(Client,
523 connected = IRC_WriteStrClient(Origin,
524 ERR_CHANOPRIVSNEEDED_MSG,
526 Channel_Name(Channel));
528 Req->argv[arg_arg][0] = '\0';
531 connected = IRC_WriteStrClient(Origin,
532 ERR_NEEDMOREPARAMS_MSG,
533 Client_ID(Origin), Req->command);
537 /* --- Channel lists --- */
538 case 'I': /* Invite lists */
539 case 'b': /* Ban lists */
540 if (arg_arg > mode_arg) {
544 ? Add_Ban_Invite(*mode_ptr, Origin,
547 : Del_Ban_Invite(*mode_ptr, Origin,
551 connected = IRC_WriteStrClient(Origin,
552 ERR_CHANOPRIVSNEEDED_MSG,
554 Channel_Name(Channel));
556 Req->argv[arg_arg][0] = '\0';
559 if (*mode_ptr == 'I')
560 Channel_ShowInvites(Origin, Channel);
562 Channel_ShowBans(Origin, Channel);
567 "Unknown mode \"%c%c\" from \"%s\" on %s!?",
568 set ? '+' : '-', *mode_ptr, Client_ID(Origin),
569 Channel_Name(Channel));
570 if (Client_Type(Client) != CLIENT_SERVER)
571 connected = IRC_WriteStrClient(Origin,
572 ERR_UMODEUNKNOWNFLAG2_MSG,
574 set ? '+' : '-', *mode_ptr);
582 /* Is there a valid mode change? */
586 /* Validate target client */
587 if (client && (!Channel_IsMemberOf(Channel, client))) {
588 if (!IRC_WriteStrClient
589 (Origin, ERR_USERNOTINCHANNEL_MSG,
590 Client_ID(Origin), Client_ID(client),
591 Channel_Name(Channel)))
598 /* Channel-User-Mode */
600 ? Channel_UserModeAdd(Channel, client, x[0])
601 : Channel_UserModeDel(Channel, client, x[0]);
603 strlcat(the_args, " ", sizeof(the_args));
604 strlcat(the_args, Client_ID(client),
606 strlcat(the_modes, x, sizeof(the_modes));
608 ("User \"%s\": Mode change on %s, now \"%s\"",
609 Client_Mask(client), Channel_Name(Channel),
610 Channel_UserModes(Channel, client));
615 ? Channel_ModeAdd(Channel, x[0])
616 : Channel_ModeDel(Channel, x[0]);
618 strlcat(the_modes, x, sizeof(the_modes));
619 LogDebug("Channel %s: Mode change, now \"%s\".",
620 Channel_Name(Channel),
621 Channel_Modes(Channel));
625 /* Are there additional arguments to add? */
627 strlcat(the_args, " ", sizeof(the_args));
628 strlcat(the_args, argadd, sizeof(the_args));
633 /* Are there changed modes? */
635 /* Clean up mode string */
636 len = strlen(the_modes) - 1;
637 if ((the_modes[len] == '+') || (the_modes[len] == '-'))
638 the_modes[len] = '\0';
640 if (Client_Type(Client) == CLIENT_SERVER) {
641 /* MODE requests for local channels from other servers
642 * are definitely invalid! */
643 if (Channel_IsLocal(Channel)) {
644 Log(LOG_ALERT, "Got remote MODE command for local channel!? Ignored.");
648 /* Forward mode changes to channel users and all the
649 * other remote servers: */
650 IRC_WriteStrServersPrefix(Client, Origin,
651 "MODE %s %s%s", Channel_Name(Channel),
652 the_modes, the_args);
653 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
654 false, "MODE %s %s%s", Channel_Name(Channel),
655 the_modes, the_args);
658 Origin = Client_ThisServer();
659 /* Send reply to client and inform other servers and channel users */
660 connected = IRC_WriteStrClientPrefix(Client, Origin,
661 "MODE %s %s%s", Channel_Name(Channel),
662 the_modes, the_args);
663 /* Only forward requests for non-local channels */
664 if (!Channel_IsLocal(Channel))
665 IRC_WriteStrServersPrefix(Client, Origin,
666 "MODE %s %s%s", Channel_Name(Channel),
667 the_modes, the_args);
668 IRC_WriteStrChannelPrefix(Client, Channel, Origin,
669 false, "MODE %s %s%s", Channel_Name(Channel),
670 the_modes, the_args);
674 IRC_SetPenalty(Client, 1);
680 IRC_AWAY( CLIENT *Client, REQUEST *Req )
682 assert( Client != NULL );
683 assert( Req != NULL );
685 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
687 if(( Req->argc == 1 ) && (Req->argv[0][0] ))
689 Client_SetAway( Client, Req->argv[0] );
690 Client_ModeAdd( Client, 'a' );
691 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :+a", Client_ID( Client ));
692 return IRC_WriteStrClient( Client, RPL_NOWAWAY_MSG, Client_ID( Client ));
696 Client_ModeDel( Client, 'a' );
697 IRC_WriteStrServersPrefix( Client, Client, "MODE %s :-a", Client_ID( Client ));
698 return IRC_WriteStrClient( Client, RPL_UNAWAY_MSG, Client_ID( Client ));
704 Add_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
710 assert( Client != NULL );
711 assert( Channel != NULL );
712 assert( Pattern != NULL );
713 assert(what == 'I' || what == 'b');
715 mask = Lists_MakeMask(Pattern);
717 already = Lists_CheckDupeMask(Channel_GetListInvites(Channel), mask);
720 ret = Channel_AddInvite(Channel, mask, false);
722 ret = Channel_AddBan(Channel, mask);
726 if (already && (Client_Type(Prefix) == CLIENT_SERVER))
730 return Send_ListChange("+I", Prefix, Client, Channel, mask);
731 return Send_ListChange("+b", Prefix, Client, Channel, mask);
736 Del_Ban_Invite(int what, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Pattern)
739 struct list_head *list;
741 assert( Client != NULL );
742 assert( Channel != NULL );
743 assert( Pattern != NULL );
744 assert(what == 'I' || what == 'b');
746 mask = Lists_MakeMask( Pattern );
749 list = Channel_GetListInvites(Channel);
751 list = Channel_GetListBans(Channel);
753 Lists_Del(list, mask);
755 return Send_ListChange( "-I", Prefix, Client, Channel, mask );
756 return Send_ListChange( "-b", Prefix, Client, Channel, mask );
761 Send_ListChange( char *Mode, CLIENT *Prefix, CLIENT *Client, CHANNEL *Channel, const char *Mask )
765 if( Client_Type( Client ) == CLIENT_USER )
767 /* send confirmation to client */
768 ok = IRC_WriteStrClientPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
772 /* to other servers */
773 IRC_WriteStrServersPrefix( Client, Prefix, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
775 /* and local users in channel */
776 IRC_WriteStrChannelPrefix( Client, Channel, Prefix, false, "MODE %s %s %s", Channel_Name( Channel ), Mode, Mask );
779 } /* Send_ListChange */