]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/coremods/core_message.cpp
Add the Numerics::CannotSendTo class and switch stuff to use it.
[user/henk/code/inspircd.git] / src / coremods / core_message.cpp
index 961307b06713b13b69a1b48c5250532e9e02a3c9..b4404995df2de07a48ac4c98850e7a73da544c71 100644 (file)
@@ -1,9 +1,12 @@
 /*
  * InspIRCd -- Internet Relay Chat Daemon
  *
+ *   Copyright (C) 2017-2019 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2013, 2017-2018 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
- *   Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
- *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
+ *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
+ *   Copyright (C) 2006-2007, 2010 Craig Edwards <brain@inspircd.org>
  *
  * This file is part of InspIRCd.  InspIRCd is free software: you can
  * redistribute it and/or modify it under the terms of the GNU General Public
 
 #include "inspircd.h"
 
+enum
+{
+       // From RFC 2812.
+       ERR_NOSUCHSERVICE = 408
+};
+
 class MessageDetailsImpl : public MessageDetails
 {
 public:
@@ -119,7 +128,7 @@ namespace
        {
                // If the source is local and was not sending a CTCP reply then update their idle time.
                LocalUser* lsource = IS_LOCAL(source);
-               if (lsource && (msgdetails.type != MSG_NOTICE || !msgdetails.IsCTCP()))
+               if (lsource && msgdetails.update_idle && (msgdetails.type != MSG_NOTICE || !msgdetails.IsCTCP()))
                        lsource->idle_lastmsg = ServerInstance->Time();
 
                // Inform modules that a message was sent.
@@ -132,8 +141,6 @@ class CommandMessage : public Command
 {
  private:
        const MessageType msgtype;
-       ChanModeReference moderatedmode;
-       ChanModeReference noextmsgmode;
 
        CmdResult HandleChannelTarget(User* source, const Params& parameters, const char* target, PrefixMode* pm)
        {
@@ -145,32 +152,6 @@ class CommandMessage : public Command
                        return CMD_FAILURE;
                }
 
-               if (IS_LOCAL(source))
-               {
-                       if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(source))
-                       {
-                               // The noextmsg mode is set and the source is not in the channel.
-                               source->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (no external messages)");
-                               return CMD_FAILURE;
-                       }
-
-                       bool no_chan_priv = chan->GetPrefixValue(source) < VOICE_VALUE;
-                       if (no_chan_priv && chan->IsModeSet(moderatedmode))
-                       {
-                               // The moderated mode is set and the source has no status rank.
-                               source->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (+m)");
-                               return CMD_FAILURE;
-                       }
-
-                       if (no_chan_priv && ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL && chan->IsBanned(source))
-                       {
-                               // The source is banned in the channel and restrictbannedusers is enabled.
-                               if (ServerInstance->Config->RestrictBannedUsers == ServerConfig::BUT_RESTRICT_NOTIFY)
-                                       source->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (you're banned)");
-                               return CMD_FAILURE;
-                       }
-               }
-
                // Fire the pre-message events.
                MessageTarget msgtarget(chan, pm ? pm->GetPrefix() : 0);
                MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
@@ -294,10 +275,8 @@ class CommandMessage : public Command
        CommandMessage(Module* parent, MessageType mt)
                : Command(parent, ClientProtocol::Messages::Privmsg::CommandStrFromMsgType(mt), 2, 2)
                , msgtype(mt)
-               , moderatedmode(parent, "moderated")
-               , noextmsgmode(parent, "noextmsg")
        {
-               syntax = "<target>{,<target>} <message>";
+               syntax = "<target>[,<target>]+ :<message>";
        }
 
        /** Handle command.
@@ -305,7 +284,35 @@ class CommandMessage : public Command
         * @param user The user issuing the command
         * @return A value from CmdResult to indicate command success or failure.
         */
-       CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE;
+       CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
+       {
+               if (CommandParser::LoopCall(user, this, parameters, 0))
+                       return CMD_SUCCESS;
+
+               // The specified message was empty.
+               if (parameters[1].empty())
+               {
+                       user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
+                       return CMD_FAILURE;
+               }
+
+               // The target is a server glob.
+               if (parameters[0][0] == '$')
+                       return HandleServerTarget(user, parameters);
+
+               // If the message begins with a status character then look it up.
+               const char* target = parameters[0].c_str();
+               PrefixMode* pmh = ServerInstance->Modes->FindPrefix(target[0]);
+               if (pmh)
+                       target++;
+
+               // The target is a channel name.
+               if (*target == '#')
+                       return HandleChannelTarget(user, parameters, target, pmh);
+
+               // The target is a nickname.
+               return HandleUserTarget(user, parameters);
+       }
 
        RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
        {
@@ -317,45 +324,116 @@ class CommandMessage : public Command
        }
 };
 
-CmdResult CommandMessage::Handle(User* user, const Params& parameters)
+class CommandSQuery : public SplitCommand
 {
-       if (CommandParser::LoopCall(user, this, parameters, 0))
-               return CMD_SUCCESS;
+ public:
+       CommandSQuery(Module* Creator)
+               : SplitCommand(Creator, "SQUERY", 2, 2)
+       {
+               syntax = "<service> :<message>";
+       }
+
+       CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
+       {
+               // The specified message was empty.
+               if (parameters[1].empty())
+               {
+                       user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
+                       return CMD_FAILURE;
+               }
 
-       // The target is a server glob.
-       if (parameters[0][0] == '$')
-               return HandleServerTarget(user, parameters);
+               // The target can be either a nick or a nick@server mask.
+               User* target;
+               const char* targetserver = strchr(parameters[0].c_str(), '@');
+               if (targetserver)
+               {
+                       // The target is a user on a specific server (e.g. jto@tolsun.oulu.fi).
+                       target = ServerInstance->FindNickOnly(parameters[0].substr(0, targetserver - parameters[0].c_str()));
+                       if (target && strcasecmp(target->server->GetName().c_str(), targetserver + 1))
+                               target = NULL;
+               }
+               else
+               {
+                       // The targer can be on any server.
+                       target = ServerInstance->FindNickOnly(parameters[0]);
+               }
 
-       // If the message begins with a status character then look it up.
-       const char* target = parameters[0].c_str();
-       PrefixMode* pmh = ServerInstance->Modes->FindPrefix(target[0]);
-       if (pmh)
-               target++;
+               if (!target || target->registered != REG_ALL || !target->server->IsULine())
+               {
+                       // The target user does not exist, is not fully registered, or is not a service.
+                       user->WriteNumeric(ERR_NOSUCHSERVICE, parameters[0], "No such service");
+                       return CMD_FAILURE;
+               }
+
+               // Fire the pre-message events.
+               MessageTarget msgtarget(target);
+               MessageDetailsImpl msgdetails(MSG_PRIVMSG, parameters[1], parameters.GetTags());
+               if (!FirePreEvents(user, msgtarget, msgdetails))
+                       return CMD_FAILURE;
 
-       // The target is a channel name.
-       if (*target == '#')
-               return HandleChannelTarget(user, parameters, target, pmh);
+               // The SQUERY command targets a service on a U-lined server. This can never
+               // be on the server local to the source so we don't need to do any routing
+               // logic and can forward it as a PRIVMSG.
 
-       // The target is a nickname.
-       return HandleUserTarget(user, parameters);
-}
+               // Fire the post-message event.
+               return FirePostEvent(user, msgtarget, msgdetails);
+       }
+};
 
 class ModuleCoreMessage : public Module
 {
  private:
        CommandMessage cmdprivmsg;
        CommandMessage cmdnotice;
+       CommandSQuery cmdsquery;
+       ChanModeReference moderatedmode;
+       ChanModeReference noextmsgmode;
 
  public:
        ModuleCoreMessage()
                : cmdprivmsg(this, MSG_PRIVMSG)
                , cmdnotice(this, MSG_NOTICE)
+               , cmdsquery(this)
+               , moderatedmode(this, "moderated")
+               , noextmsgmode(this, "noextmsg")
        {
        }
 
+       ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
+       {
+               if (!IS_LOCAL(user) || target.type != MessageTarget::TYPE_CHANNEL)
+                       return MOD_RES_PASSTHRU;
+
+               Channel* chan = target.Get<Channel>();
+               if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(user))
+               {
+                       // The noextmsg mode is set and the user is not in the channel.
+                       user->WriteNumeric(Numerics::CannotSendTo(chan, "external messages", *noextmsgmode));
+                       return MOD_RES_DENY;
+               }
+
+               bool no_chan_priv = chan->GetPrefixValue(user) < VOICE_VALUE;
+               if (no_chan_priv && chan->IsModeSet(moderatedmode))
+               {
+                       // The moderated mode is set and the user has no status rank.
+                       user->WriteNumeric(Numerics::CannotSendTo(chan, "messages", *moderatedmode));
+                       return MOD_RES_DENY;
+               }
+
+               if (no_chan_priv && ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL && chan->IsBanned(user))
+               {
+                       // The user is banned in the channel and restrictbannedusers is enabled.
+                       if (ServerInstance->Config->RestrictBannedUsers == ServerConfig::BUT_RESTRICT_NOTIFY)
+                               user->WriteNumeric(Numerics::CannotSendTo(chan, "You cannot send messages to this channel whilst banned."));
+                       return MOD_RES_DENY;
+               }
+
+               return MOD_RES_PASSTHRU;
+       }
+
        Version GetVersion() CXX11_OVERRIDE
        {
-               return Version("PRIVMSG, NOTICE", VF_CORE|VF_VENDOR);
+               return Version("Provides the NOTICE, PRIVMSG, and SQUERY commands", VF_CORE|VF_VENDOR);
        }
 };