]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/coremods/core_privmsg.cpp
Don't update the idle timer when a user replies to a CTCP.
[user/henk/code/inspircd.git] / src / coremods / core_privmsg.cpp
index d691464c0660eff89ab8883239908044cc41892d..dae770abef57588601c00ff5f625e8a592af3e4e 100644 (file)
 
 #include "inspircd.h"
 
-namespace
+class MessageDetailsImpl : public MessageDetails
 {
-       const char* MessageTypeString[] = { "PRIVMSG", "NOTICE" };
-}
+public:
+       MessageDetailsImpl(MessageType mt, const std::string& msg, const ClientProtocol::TagMap& tags)
+               : MessageDetails(mt, msg, tags)
+       {
+       }
+
+       bool IsCTCP(std::string& name, std::string& body) const CXX11_OVERRIDE
+       {
+               if (!this->IsCTCP())
+                       return false;
+
+               size_t end_of_name = text.find(' ', 2);
+               size_t end_of_ctcp = *text.rbegin() == '\x1' ? 1 : 0;
+               if (end_of_name == std::string::npos)
+               {
+                       // The CTCP only contains a name.
+                       name.assign(text, 1, text.length() - 1 - end_of_ctcp);
+                       body.clear();
+                       return true;
+               }
+
+               // The CTCP contains a name and a body.
+               name.assign(text, 1, end_of_name - 1);
+
+               size_t start_of_body = text.find_first_not_of(' ', end_of_name + 1);
+               if (start_of_body == std::string::npos)
+               {
+                       // The CTCP body is provided but empty.
+                       body.clear();
+                       return true;
+               }
+
+               // The CTCP body provided was non-empty.
+               body.assign(text, start_of_body, text.length() - start_of_body - end_of_ctcp);
+               return true;
+       }
+
+       bool IsCTCP(std::string& name) const CXX11_OVERRIDE
+       {
+               if (!this->IsCTCP())
+                       return false;
+
+               size_t end_of_name = text.find(' ', 2);
+               if (end_of_name == std::string::npos)
+               {
+                       // The CTCP only contains a name.
+                       size_t end_of_ctcp = *text.rbegin() == '\x1' ? 1 : 0;
+                       name.assign(text, 1, text.length() - 1 - end_of_ctcp);
+                       return true;
+               }
+
+               // The CTCP contains a name and a body.
+               name.assign(text, 1, end_of_name - 1);
+               return true;
+       }
+
+       bool IsCTCP() const CXX11_OVERRIDE
+       {
+               // According to draft-oakley-irc-ctcp-02 a valid CTCP must begin with SOH and
+               // contain at least one octet which is not NUL, SOH, CR, LF, or SPACE. As most
+               // of these are restricted at the protocol level we only need to check for SOH
+               // and SPACE.
+               return (text.length() >= 2) && (text[0] == '\x1') &&  (text[1] != '\x1') && (text[1] != ' ');
+       }
+};
 
 class MessageCommandBase : public Command
 {
@@ -35,12 +98,13 @@ class MessageCommandBase : public Command
         * @param user User sending the message
         * @param msg The message to send
         * @param mt Type of the message (MSG_PRIVMSG or MSG_NOTICE)
+        * @param tags Message tags to include in the outgoing protocol message
         */
-       static void SendAll(User* user, const std::string& msg, MessageType mt);
+       static void SendAll(User* user, const std::string& msg, MessageType mt, const ClientProtocol::TagMap& tags);
 
  public:
        MessageCommandBase(Module* parent, MessageType mt)
-               : Command(parent, MessageTypeString[mt], 2, 2)
+               : Command(parent, ClientProtocol::Messages::Privmsg::CommandStrFromMsgType(mt), 2, 2)
                , moderatedmode(parent, "moderated")
                , noextmsgmode(parent, "noextmsg")
        {
@@ -52,38 +116,37 @@ class MessageCommandBase : public Command
         * @param user The user issuing the command
         * @return A value from CmdResult to indicate command success or failure.
         */
-       CmdResult HandleMessage(const std::vector<std::string>& parameters, User* user, MessageType mt);
+       CmdResult HandleMessage(User* user, const Params& parameters, MessageType mt);
 
-       RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
+       RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
        {
                if (IS_LOCAL(user))
-                       // This is handled by the OnUserMessage hook to split the LoopCall pieces
+                       // This is handled by the OnUserPostMessage hook to split the LoopCall pieces
                        return ROUTE_LOCALONLY;
                else
                        return ROUTE_MESSAGE(parameters[0]);
        }
 };
 
-void MessageCommandBase::SendAll(User* user, const std::string& msg, MessageType mt)
+void MessageCommandBase::SendAll(User* user, const std::string& msg, MessageType mt, const ClientProtocol::TagMap& tags)
 {
-       const std::string message = ":" + user->GetFullHost() + " " + MessageTypeString[mt] + " $* :" + msg;
+       ClientProtocol::Messages::Privmsg message(ClientProtocol::Messages::Privmsg::nocopy, user, "$*", msg, mt);
+       message.AddTags(tags);
+       message.SetSideEffect(true);
+       ClientProtocol::Event messageevent(ServerInstance->GetRFCEvents().privmsg, message);
+
        const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
        for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
        {
                if ((*i)->registered == REG_ALL)
-                       (*i)->Write(message);
+                       (*i)->Send(messageevent);
        }
 }
 
-CmdResult MessageCommandBase::HandleMessage(const std::vector<std::string>& parameters, User* user, MessageType mt)
+CmdResult MessageCommandBase::HandleMessage(User* user, const Params& parameters, MessageType mt)
 {
        User *dest;
        Channel *chan;
-       CUList except_list;
-
-       LocalUser* localuser = IS_LOCAL(user);
-       if (localuser)
-               localuser->idle_lastmsg = ServerInstance->Time();
 
        if (CommandParser::LoopCall(user, this, parameters, 0))
                return CMD_SUCCESS;
@@ -93,23 +156,27 @@ CmdResult MessageCommandBase::HandleMessage(const std::vector<std::string>& para
                if (!user->HasPrivPermission("users/mass-message"))
                        return CMD_SUCCESS;
 
+               std::string servername(parameters[0], 1);
+               MessageTarget msgtarget(&servername);
+               MessageDetailsImpl msgdetails(mt, parameters[1], parameters.GetTags());
+
                ModResult MOD_RESULT;
-               std::string temp = parameters[1];
-               FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, (void*)parameters[0].c_str(), TYPE_SERVER, temp, 0, except_list, mt));
+               FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, msgtarget, msgdetails));
                if (MOD_RESULT == MOD_RES_DENY)
+               {
+                       FOREACH_MOD(OnUserMessageBlocked, (user, msgtarget, msgdetails));
                        return CMD_FAILURE;
+               }
 
-               const char* text = temp.c_str();
-               const char* servermask = (parameters[0].c_str()) + 1;
-
-               FOREACH_MOD(OnText, (user, (void*)parameters[0].c_str(), TYPE_SERVER, text, 0, except_list));
-               if (InspIRCd::Match(ServerInstance->Config->ServerName, servermask, NULL))
+               FOREACH_MOD(OnUserMessage, (user, msgtarget, msgdetails));
+               if (InspIRCd::Match(ServerInstance->Config->ServerName, servername, NULL))
                {
-                       SendAll(user, text, mt);
+                       SendAll(user, msgdetails.text, mt, msgdetails.tags_out);
                }
-               FOREACH_MOD(OnUserMessage, (user, (void*)parameters[0].c_str(), TYPE_SERVER, text, 0, except_list, mt));
+               FOREACH_MOD(OnUserPostMessage, (user, msgtarget, msgdetails));
                return CMD_SUCCESS;
        }
+
        char status = 0;
        const char* target = parameters[0].c_str();
 
@@ -122,66 +189,65 @@ CmdResult MessageCommandBase::HandleMessage(const std::vector<std::string>& para
        {
                chan = ServerInstance->FindChan(target);
 
-               except_list.insert(user);
-
                if (chan)
                {
-                       if (localuser && chan->GetPrefixValue(user) < VOICE_VALUE)
+                       if (IS_LOCAL(user) && chan->GetPrefixValue(user) < VOICE_VALUE)
                        {
                                if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(user))
                                {
-                                       user->WriteNumeric(ERR_CANNOTSENDTOCHAN, "%s :Cannot send to channel (no external messages)", chan->name.c_str());
+                                       user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (no external messages)");
                                        return CMD_FAILURE;
                                }
 
                                if (chan->IsModeSet(moderatedmode))
                                {
-                                       user->WriteNumeric(ERR_CANNOTSENDTOCHAN, "%s :Cannot send to channel (+m)", chan->name.c_str());
+                                       user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (+m)");
                                        return CMD_FAILURE;
                                }
 
-                               if (ServerInstance->Config->RestrictBannedUsers)
+                               if (ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL)
                                {
                                        if (chan->IsBanned(user))
                                        {
-                                               user->WriteNumeric(ERR_CANNOTSENDTOCHAN, "%s :Cannot send to channel (you're banned)", chan->name.c_str());
+                                               if (ServerInstance->Config->RestrictBannedUsers == ServerConfig::BUT_RESTRICT_NOTIFY)
+                                                       user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (you're banned)");
                                                return CMD_FAILURE;
                                        }
                                }
                        }
-                       ModResult MOD_RESULT;
 
-                       std::string temp = parameters[1];
-                       FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, chan, TYPE_CHANNEL, temp, status, except_list, mt));
+                       MessageTarget msgtarget(chan, status);
+                       MessageDetailsImpl msgdetails(mt, parameters[1], parameters.GetTags());
+                       msgdetails.exemptions.insert(user);
+
+                       ModResult MOD_RESULT;
+                       FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, msgtarget, msgdetails));
                        if (MOD_RESULT == MOD_RES_DENY)
+                       {
+                               FOREACH_MOD(OnUserMessageBlocked, (user, msgtarget, msgdetails));
                                return CMD_FAILURE;
-
-                       const char* text = temp.c_str();
+                       }
 
                        /* Check again, a module may have zapped the input string */
-                       if (temp.empty())
+                       if (msgdetails.text.empty())
                        {
-                               user->WriteNumeric(ERR_NOTEXTTOSEND, ":No text to send");
+                               user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
                                return CMD_FAILURE;
                        }
 
-                       FOREACH_MOD(OnText, (user,chan,TYPE_CHANNEL,text,status,except_list));
+                       FOREACH_MOD(OnUserMessage, (user, msgtarget, msgdetails));
 
-                       if (status)
-                       {
-                               chan->WriteAllExcept(user, false, status, except_list, "%s %c%s :%s", MessageTypeString[mt], status, chan->name.c_str(), text);
-                       }
-                       else
-                       {
-                               chan->WriteAllExcept(user, false, status, except_list, "%s %s :%s", MessageTypeString[mt], chan->name.c_str(), text);
-                       }
+                       ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, chan, msgdetails.text, msgdetails.type, msgtarget.status);
+                       privmsg.AddTags(msgdetails.tags_out);
+                       privmsg.SetSideEffect(true);
+                       chan->Write(ServerInstance->GetRFCEvents().privmsg, privmsg, msgtarget.status, msgdetails.exemptions);
 
-                       FOREACH_MOD(OnUserMessage, (user,chan, TYPE_CHANNEL, text, status, except_list, mt));
+                       FOREACH_MOD(OnUserPostMessage, (user, msgtarget, msgdetails));
                }
                else
                {
-                       /* no such nick/channel */
-                       user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", target);
+                       /* channel does not exist */
+                       user->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
                        return CMD_FAILURE;
                }
                return CMD_SUCCESS;
@@ -189,7 +255,7 @@ CmdResult MessageCommandBase::HandleMessage(const std::vector<std::string>& para
 
        const char* destnick = parameters[0].c_str();
 
-       if (localuser)
+       if (IS_LOCAL(user))
        {
                const char* targetserver = strchr(destnick, '@');
 
@@ -202,7 +268,7 @@ CmdResult MessageCommandBase::HandleMessage(const std::vector<std::string>& para
                        if (dest && strcasecmp(dest->server->GetName().c_str(), targetserver + 1))
                        {
                                /* Incorrect server for user */
-                               user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", parameters[0].c_str());
+                               user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
                                return CMD_FAILURE;
                        }
                }
@@ -216,39 +282,46 @@ CmdResult MessageCommandBase::HandleMessage(const std::vector<std::string>& para
        {
                if (parameters[1].empty())
                {
-                       user->WriteNumeric(ERR_NOTEXTTOSEND, ":No text to send");
+                       user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
                        return CMD_FAILURE;
                }
 
                if ((dest->IsAway()) && (mt == MSG_PRIVMSG))
                {
                        /* auto respond with aweh msg */
-                       user->WriteNumeric(RPL_AWAY, "%s :%s", dest->nick.c_str(), dest->awaymsg.c_str());
+                       user->WriteNumeric(RPL_AWAY, dest->nick, dest->awaymsg);
                }
 
-               ModResult MOD_RESULT;
+               MessageTarget msgtarget(dest);
+               MessageDetailsImpl msgdetails(mt, parameters[1], parameters.GetTags());
+
 
-               std::string temp = parameters[1];
-               FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, dest, TYPE_USER, temp, 0, except_list, mt));
+               ModResult MOD_RESULT;
+               FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, msgtarget, msgdetails));
                if (MOD_RESULT == MOD_RES_DENY)
+               {
+                       FOREACH_MOD(OnUserMessageBlocked, (user, msgtarget, msgdetails));
                        return CMD_FAILURE;
+               }
 
-               const char* text = temp.c_str();
-
-               FOREACH_MOD(OnText, (user, dest, TYPE_USER, text, 0, except_list));
+               FOREACH_MOD(OnUserMessage, (user, msgtarget, msgdetails));
 
-               if (IS_LOCAL(dest))
+               LocalUser* const localtarget = IS_LOCAL(dest);
+               if (localtarget)
                {
                        // direct write, same server
-                       dest->WriteFrom(user, "%s %s :%s", MessageTypeString[mt], dest->nick.c_str(), text);
+                       ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, localtarget->nick, msgdetails.text, mt);
+                       privmsg.AddTags(msgdetails.tags_out);
+                       privmsg.SetSideEffect(true);
+                       localtarget->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
                }
 
-               FOREACH_MOD(OnUserMessage, (user, dest, TYPE_USER, text, 0, except_list, mt));
+               FOREACH_MOD(OnUserPostMessage, (user, msgtarget, msgdetails));
        }
        else
        {
                /* no such nick/channel */
-               user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", parameters[0].c_str());
+               user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
                return CMD_FAILURE;
        }
        return CMD_SUCCESS;
@@ -263,9 +336,9 @@ class CommandMessage : public MessageCommandBase
        {
        }
 
-       CmdResult Handle(const std::vector<std::string>& parameters, User* user)
+       CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
        {
-               return HandleMessage(parameters, user, MT);
+               return HandleMessage(user, parameters, MT);
        }
 };
 
@@ -280,7 +353,21 @@ class ModuleCoreMessage : public Module
        {
        }
 
-       Version GetVersion()
+       void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
+       {
+               // We only handle the idle times of local users.
+               LocalUser* luser = IS_LOCAL(user);
+               if (!luser)
+                       return;
+
+               // We don't update the idle time when a CTCP reply is sent.
+               if (details.type == MSG_NOTICE && details.IsCTCP())
+                       return;
+
+               luser->idle_lastmsg = ServerInstance->Time();
+       }
+
+       Version GetVersion() CXX11_OVERRIDE
        {
                return Version("PRIVMSG, NOTICE", VF_CORE|VF_VENDOR);
        }