]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/coremods/core_whois.cpp
Convert WriteNumeric() calls to pass the parameters of the numeric as method parameters
[user/henk/code/inspircd.git] / src / coremods / core_whois.cpp
index 7464e052753cad86ab0235389e32ad137d212d64..0645eda4c0b8c999f90f8b4d5f0d868d8fe33498 100644 (file)
 
 #include "inspircd.h"
 
+class WhoisContextImpl : public Whois::Context
+{
+       Events::ModuleEventProvider& lineevprov;
+
+ public:
+       WhoisContextImpl(LocalUser* src, User* targ, Events::ModuleEventProvider& evprov)
+               : Whois::Context(src, targ)
+               , lineevprov(evprov)
+       {
+       }
+
+       using Whois::Context::SendLine;
+       void SendLine(unsigned int numeric, const std::string& text) CXX11_OVERRIDE;
+};
+
+void WhoisContextImpl::SendLine(unsigned int numeric, const std::string& text)
+{
+       std::string copy_text = target->nick;
+       copy_text.push_back(' ');
+       copy_text.append(text);
+
+       ModResult MOD_RESULT;
+       FIRST_MOD_RESULT_CUSTOM(lineevprov, Whois::LineEventListener, OnWhoisLine, MOD_RESULT, (*this, numeric, copy_text));
+
+       if (MOD_RESULT != MOD_RES_DENY)
+               source->WriteNumeric(numeric, copy_text);
+}
+
 /** Handle /WHOIS.
  */
 class CommandWhois : public SplitCommand
@@ -28,10 +56,11 @@ class CommandWhois : public SplitCommand
        ChanModeReference secretmode;
        ChanModeReference privatemode;
        UserModeReference snomaskmode;
+       Events::ModuleEventProvider evprov;
+       Events::ModuleEventProvider lineevprov;
 
-       void SplitChanList(User* source, User* dest, const std::string& cl);
-       void DoWhois(User* user, User* dest, unsigned long signon, unsigned long idle);
-       std::string ChannelList(User* source, User* dest, bool spy);
+       void DoWhois(LocalUser* user, User* dest, unsigned long signon, unsigned long idle);
+       void SendChanList(WhoisContextImpl& whois);
 
  public:
        /** Constructor for whois.
@@ -41,6 +70,8 @@ class CommandWhois : public SplitCommand
                , secretmode(parent, "secret")
                , privatemode(parent, "private")
                , snomaskmode(parent, "snomask")
+               , evprov(parent, "event/whois")
+               , lineevprov(parent, "event/whoisline")
        {
                Penalty = 2;
                syntax = "<nick>{,<nick>}";
@@ -55,116 +86,143 @@ class CommandWhois : public SplitCommand
        CmdResult HandleRemote(const std::vector<std::string>& parameters, RemoteUser* target);
 };
 
-std::string CommandWhois::ChannelList(User* source, User* dest, bool spy)
+class WhoisNumericSink
 {
-       std::string list;
+       WhoisContextImpl& whois;
+ public:
+       WhoisNumericSink(WhoisContextImpl& whoisref)
+               : whois(whoisref)
+       {
+       }
 
-       for (User::ChanList::iterator i = dest->chans.begin(); i != dest->chans.end(); i++)
+       void operator()(Numeric::Numeric& numeric) const
        {
-               Membership* memb = *i;
-               Channel* c = memb->chan;
-               /* If the target is the sender, neither +p nor +s is set, or
-                * the channel contains the user, it is not a spy channel
-                */
-               if (spy != (source == dest || !(c->IsModeSet(privatemode) || c->IsModeSet(secretmode)) || c->HasUser(source)))
-               {
-                       char prefix = memb->GetPrefixChar();
-                       if (prefix)
-                               list.push_back(prefix);
-                       list.append(c->name).push_back(' ');
-               }
+               whois.SendLine(numeric);
        }
+};
 
-       return list;
-}
+class WhoisChanListNumericBuilder : public Numeric::GenericBuilder<' ', false, WhoisNumericSink>
+{
+ public:
+       WhoisChanListNumericBuilder(WhoisContextImpl& whois)
+               : Numeric::GenericBuilder<' ', false, WhoisNumericSink>(WhoisNumericSink(whois), 319, true, whois.GetSource()->nick.size() + whois.GetTarget()->nick.size() + 1)
+       {
+       }
+};
 
-void CommandWhois::SplitChanList(User* source, User* dest, const std::string& cl)
+class WhoisChanList
 {
-       std::string line;
-       std::ostringstream prefix;
-       std::string::size_type start, pos;
+       const ServerConfig::OperSpyWhoisState spywhois;
+       WhoisChanListNumericBuilder num;
+       WhoisChanListNumericBuilder spynum;
+       std::string prefixstr;
 
-       prefix << dest->nick << " :";
-       line = prefix.str();
-       int namelen = ServerInstance->Config->ServerName.length() + 6;
+       void AddMember(Membership* memb, WhoisChanListNumericBuilder& out)
+       {
+               prefixstr.clear();
+               const char prefix = memb->GetPrefixChar();
+               if (prefix)
+                       prefixstr.push_back(prefix);
+               out.Add(prefixstr, memb->chan->name);
+       }
 
-       for (start = 0; (pos = cl.find(' ', start)) != std::string::npos; start = pos+1)
+ public:
+       WhoisChanList(WhoisContextImpl& whois)
+               : spywhois(whois.GetSource()->HasPrivPermission("users/auspex") ? ServerInstance->Config->OperSpyWhois : ServerConfig::SPYWHOIS_NONE)
+               , num(whois)
+               , spynum(whois)
        {
-               if (line.length() + namelen + pos - start > 510)
-               {
-                       ServerInstance->SendWhoisLine(source, dest, 319, line);
-                       line = prefix.str();
-               }
+       }
 
-               line.append(cl, start, pos - start + 1);
+       void AddVisible(Membership* memb)
+       {
+               AddMember(memb, num);
        }
 
-       if (line.length() != prefix.str().length())
+       void AddHidden(Membership* memb)
        {
-               ServerInstance->SendWhoisLine(source, dest, 319, line);
+               if (spywhois == ServerConfig::SPYWHOIS_NONE)
+                       return;
+               AddMember(memb, (spywhois == ServerConfig::SPYWHOIS_SPLITMSG ? spynum : num));
        }
-}
 
-void CommandWhois::DoWhois(User* user, User* dest, unsigned long signon, unsigned long idle)
-{
-       ServerInstance->SendWhoisLine(user, dest, 311, "%s %s %s * :%s", dest->nick.c_str(), dest->ident.c_str(), dest->dhost.c_str(), dest->fullname.c_str());
-       if (user == dest || user->HasPrivPermission("users/auspex"))
+       void Flush(WhoisContextImpl& whois)
        {
-               ServerInstance->SendWhoisLine(user, dest, 378, "%s :is connecting from %s@%s %s", dest->nick.c_str(), dest->ident.c_str(), dest->host.c_str(), dest->GetIPString().c_str());
+               num.Flush();
+               if (!spynum.IsEmpty())
+                       whois.SendLine(336, ":is on private/secret channels:");
+               spynum.Flush();
        }
+};
 
-       std::string cl = ChannelList(user, dest, false);
-       const ServerConfig::OperSpyWhoisState state = user->HasPrivPermission("users/auspex") ? ServerInstance->Config->OperSpyWhois : ServerConfig::SPYWHOIS_NONE;
+void CommandWhois::SendChanList(WhoisContextImpl& whois)
+{
+       WhoisChanList chanlist(whois);
 
-       if (state == ServerConfig::SPYWHOIS_SINGLEMSG)
-               cl.append(ChannelList(user, dest, true));
+       User* const target = whois.GetTarget();
+       for (User::ChanList::iterator i = target->chans.begin(); i != target->chans.end(); ++i)
+       {
+               Membership* memb = *i;
+               Channel* c = memb->chan;
+               /* If the target is the sender, neither +p nor +s is set, or
+                * the channel contains the user, it is not a spy channel
+                */
+               if ((whois.IsSelfWhois()) || ((!c->IsModeSet(privatemode)) && (!c->IsModeSet(secretmode))) || (c->HasUser(whois.GetSource())))
+                       chanlist.AddVisible(memb);
+               else
+                       chanlist.AddHidden(memb);
+       }
+
+       chanlist.Flush(whois);
+}
 
-       SplitChanList(user, dest, cl);
+void CommandWhois::DoWhois(LocalUser* user, User* dest, unsigned long signon, unsigned long idle)
+{
+       WhoisContextImpl whois(user, dest, lineevprov);
 
-       if (state == ServerConfig::SPYWHOIS_SPLITMSG)
+       whois.SendLine(311, "%s %s * :%s", dest->ident.c_str(), dest->dhost.c_str(), dest->fullname.c_str());
+       if (whois.IsSelfWhois() || user->HasPrivPermission("users/auspex"))
        {
-               std::string scl = ChannelList(user, dest, true);
-               if (scl.length())
-               {
-                       ServerInstance->SendWhoisLine(user, dest, 336, "%s :is on private/secret channels:", dest->nick.c_str());
-                       SplitChanList(user, dest, scl);
-               }
+               whois.SendLine(378, ":is connecting from %s@%s %s", dest->ident.c_str(), dest->host.c_str(), dest->GetIPString().c_str());
        }
-       if (user != dest && !ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"))
+
+       SendChanList(whois);
+
+       if (!whois.IsSelfWhois() && !ServerInstance->Config->HideWhoisServer.empty() && !user->HasPrivPermission("servers/auspex"))
        {
-               ServerInstance->SendWhoisLine(user, dest, 312, "%s %s :%s", dest->nick.c_str(), ServerInstance->Config->HideWhoisServer.c_str(), ServerInstance->Config->Network.c_str());
+               whois.SendLine(312, "%s :%s", ServerInstance->Config->HideWhoisServer.c_str(), ServerInstance->Config->Network.c_str());
        }
        else
        {
-               ServerInstance->SendWhoisLine(user, dest, 312, "%s %s :%s", dest->nick.c_str(), dest->server->GetName().c_str(), dest->server->GetDesc().c_str());
+               whois.SendLine(312, "%s :%s", dest->server->GetName().c_str(), dest->server->GetDesc().c_str());
        }
 
        if (dest->IsAway())
        {
-               ServerInstance->SendWhoisLine(user, dest, 301, "%s :%s", dest->nick.c_str(), dest->awaymsg.c_str());
+               whois.SendLine(301, ":%s", dest->awaymsg.c_str());
        }
 
        if (dest->IsOper())
        {
                if (ServerInstance->Config->GenericOper)
-                       ServerInstance->SendWhoisLine(user, dest, 313, "%s :is an IRC operator", dest->nick.c_str());
+                       whois.SendLine(313, ":is an IRC operator");
                else
-                       ServerInstance->SendWhoisLine(user, dest, 313, "%s :is %s %s on %s", dest->nick.c_str(), (strchr("AEIOUaeiou",dest->oper->name[0]) ? "an" : "a"),dest->oper->name.c_str(), ServerInstance->Config->Network.c_str());
+                       whois.SendLine(313, ":is %s %s on %s", (strchr("AEIOUaeiou",dest->oper->name[0]) ? "an" : "a"),dest->oper->name.c_str(), ServerInstance->Config->Network.c_str());
        }
 
-       if (user == dest || user->HasPrivPermission("users/auspex"))
+       if (whois.IsSelfWhois() || user->HasPrivPermission("users/auspex"))
        {
                if (dest->IsModeSet(snomaskmode))
                {
-                       ServerInstance->SendWhoisLine(user, dest, 379, "%s :is using modes +%s %s", dest->nick.c_str(), dest->FormatModes(), snomaskmode->GetUserParameter(dest).c_str());
+                       whois.SendLine(379, ":is using modes +%s %s", dest->FormatModes(), snomaskmode->GetUserParameter(dest).c_str());
                }
                else
                {
-                       ServerInstance->SendWhoisLine(user, dest, 379, "%s :is using modes +%s", dest->nick.c_str(), dest->FormatModes());
+                       whois.SendLine(379, ":is using modes +%s", dest->FormatModes());
                }
        }
 
-       FOREACH_MOD(OnWhois, (user,dest));
+       FOREACH_MOD_CUSTOM(evprov, Whois::EventListener, OnWhois, (whois));
 
        /*
         * We only send these if we've been provided them. That is, if hidewhois is turned off, and user is local, or
@@ -172,10 +230,10 @@ void CommandWhois::DoWhois(User* user, User* dest, unsigned long signon, unsigne
         */
        if ((idle) || (signon))
        {
-               ServerInstance->SendWhoisLine(user, dest, 317, "%s %lu %lu :seconds idle, signon time", dest->nick.c_str(), idle, signon);
+               whois.SendLine(317, "%lu %lu :seconds idle, signon time", idle, signon);
        }
 
-       ServerInstance->SendWhoisLine(user, dest, 318, "%s :End of /WHOIS list.", dest->nick.c_str());
+       whois.SendLine(318, ":End of /WHOIS list.");
 }
 
 CmdResult CommandWhois::HandleRemote(const std::vector<std::string>& parameters, RemoteUser* target)
@@ -187,8 +245,13 @@ CmdResult CommandWhois::HandleRemote(const std::vector<std::string>& parameters,
        if (!user)
                return CMD_FAILURE;
 
+       // User doing the whois must be on this server
+       LocalUser* localuser = IS_LOCAL(user);
+       if (!localuser)
+               return CMD_FAILURE;
+
        unsigned long idle = ConvToInt(parameters.back());
-       DoWhois(user, target, target->signon, idle);
+       DoWhois(localuser, target, target->signon, idle);
 
        return CMD_SUCCESS;
 }
@@ -233,8 +296,8 @@ CmdResult CommandWhois::HandleLocal(const std::vector<std::string>& parameters,
        else
        {
                /* no such nick/channel */
-               user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", !parameters[userindex].empty() ? parameters[userindex].c_str() : "*");
-               user->WriteNumeric(RPL_ENDOFWHOIS, "%s :End of /WHOIS list.", !parameters[userindex].empty() ? parameters[userindex].c_str() : "*");
+               user->WriteNumeric(Numerics::NoSuchNick(!parameters[userindex].empty() ? parameters[userindex] : "*"));
+               user->WriteNumeric(RPL_ENDOFWHOIS, (!parameters[userindex].empty() ? parameters[userindex] : "*"), "End of /WHOIS list.");
                return CMD_FAILURE;
        }