]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_spanningtree/main.cpp
Add back the prefix on a server target message.
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / main.cpp
index 2fd578698e2151f722117c43080d7a65b5a59858..5ed66b80541f299cbf3d3f630a7149b2cc003fed 100644 (file)
@@ -1,11 +1,15 @@
 /*
  * InspIRCd -- Internet Relay Chat Daemon
  *
+ *   Copyright (C) 2019 linuxdaemon <linuxdaemon.irc@gmail.com>
+ *   Copyright (C) 2013, 2017-2020 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2013, 2016 Adam <Adam@anope.org>
+ *   Copyright (C) 2012-2016, 2018 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
- *   Copyright (C) 2007-2009 Craig Edwards <craigedwards@brainbox.cc>
- *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
- *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
- *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
+ *   Copyright (C) 2007-2009 Robin Burchell <robin+git@viroteck.net>
+ *   Copyright (C) 2007-2009 Dennis Friis <peavey@inspircd.org>
+ *   Copyright (C) 2005, 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
@@ -25,7 +29,6 @@
 #include "socket.h"
 #include "xline.h"
 #include "iohook.h"
-#include "modules/server.h"
 
 #include "resolvers.h"
 #include "main.h"
 #include "translate.h"
 
 ModuleSpanningTree::ModuleSpanningTree()
-       : Stats::EventListener(this)
+       : Away::EventListener(this)
+       , Stats::EventListener(this)
+       , CTCTags::EventListener(this)
        , rconnect(this)
        , rsquit(this)
        , map(this)
        , commands(this)
        , currmembid(0)
-       , eventprov(this, "event/server")
+       , broadcasteventprov(this, "event/server-broadcast")
+       , linkeventprov(this, "event/server-link")
+       , messageeventprov(this, "event/server-message")
+       , synceventprov(this, "event/server-sync")
+       , sslapi(this)
+       , servicetag(this)
        , DNS(this, "DNS")
+       , tagevprov(this)
        , loopCall(false)
 {
 }
@@ -136,26 +147,12 @@ void ModuleSpanningTree::ShowLinks(TreeServer* Current, User* user, int hops)
                        InspIRCd::Format("%d %s", (((Utils->FlatLinks) && (!user->IsOper())) ? 0 : hops), Current->GetDesc().c_str()));
 }
 
-void ModuleSpanningTree::HandleLinks(const std::vector<std::string>& parameters, User* user)
+void ModuleSpanningTree::HandleLinks(const CommandBase::Params& parameters, User* user)
 {
        ShowLinks(Utils->TreeRoot,user,0);
        user->WriteNumeric(RPL_ENDOFLINKS, '*', "End of /LINKS list.");
 }
 
-std::string ModuleSpanningTree::TimeToStr(time_t secs)
-{
-       time_t mins_up = secs / 60;
-       time_t hours_up = mins_up / 60;
-       time_t days_up = hours_up / 24;
-       secs = secs % 60;
-       mins_up = mins_up % 60;
-       hours_up = hours_up % 24;
-       return ((days_up ? (ConvToStr(days_up) + "d") : "")
-                       + (hours_up ? (ConvToStr(hours_up) + "h") : "")
-                       + (mins_up ? (ConvToStr(mins_up) + "m") : "")
-                       + ConvToStr(secs) + "s");
-}
-
 void ModuleSpanningTree::ConnectServer(Autoconnect* a, bool on_timer)
 {
        if (!a)
@@ -195,37 +192,36 @@ void ModuleSpanningTree::ConnectServer(Autoconnect* a, bool on_timer)
 
 void ModuleSpanningTree::ConnectServer(Link* x, Autoconnect* y)
 {
-       bool ipvalid = true;
-
        if (InspIRCd::Match(ServerInstance->Config->ServerName, x->Name, ascii_case_insensitive_map))
        {
                ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Not connecting to myself.");
                return;
        }
 
-       if (strchr(x->IPAddr.c_str(),':'))
+       irc::sockets::sockaddrs sa;
+       if (x->IPAddr.find('/') != std::string::npos)
        {
-               in6_addr n;
-               if (inet_pton(AF_INET6, x->IPAddr.c_str(), &n) < 1)
-                       ipvalid = false;
+               if (!irc::sockets::isunix(x->IPAddr) || !irc::sockets::untosa(x->IPAddr, sa))
+               {
+                       // We don't use the family() != AF_UNSPEC check below for UNIX sockets as
+                       // that results in a DNS lookup.
+                       ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Error connecting \002%s\002: %s is not a UNIX socket!",
+                               x->Name.c_str(), x->IPAddr.c_str());
+                       return;
+               }
        }
        else
        {
-               in_addr n;
-               if (inet_pton(AF_INET, x->IPAddr.c_str(),&n) < 1)
-                       ipvalid = false;
+               // If this fails then the IP sa will be AF_UNSPEC.
+               irc::sockets::aptosa(x->IPAddr, x->Port, sa);
        }
 
        /* Do we already have an IP? If so, no need to resolve it. */
-       if (ipvalid)
+       if (sa.family() != AF_UNSPEC)
        {
                // Create a TreeServer object that will start connecting immediately in the background
-               TreeSocket* newsocket = new TreeSocket(x, y, x->IPAddr);
-               if (newsocket->GetFd() > -1)
-               {
-                       /* Handled automatically on success */
-               }
-               else
+               TreeSocket* newsocket = new TreeSocket(x, y, sa);
+               if (!newsocket->HasFd())
                {
                        ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Error connecting \002%s\002: %s.",
                                x->Name.c_str(), newsocket->getError().c_str());
@@ -243,7 +239,7 @@ void ModuleSpanningTree::ConnectServer(Link* x, Autoconnect* y)
                irc::sockets::sockaddrs bind;
                if ((!x->Bind.empty()) && (irc::sockets::aptosa(x->Bind, 0, bind)))
                {
-                       if (bind.sa.sa_family == AF_INET)
+                       if (bind.family() == AF_INET)
                                start_type = DNS::QUERY_A;
                }
 
@@ -297,7 +293,7 @@ void ModuleSpanningTree::DoConnectTimeout(time_t curtime)
        }
 }
 
-ModResult ModuleSpanningTree::HandleVersion(const std::vector<std::string>& parameters, User* user)
+ModResult ModuleSpanningTree::HandleVersion(const CommandBase::Params& parameters, User* user)
 {
        // We've already confirmed that !parameters.empty(), so this is safe
        TreeServer* found = Utils->FindServerMask(parameters[0]);
@@ -314,8 +310,12 @@ ModResult ModuleSpanningTree::HandleVersion(const std::vector<std::string>& para
                // If it's empty it might be that the server is still syncing (full version hasn't arrived yet)
                // or the server is a 2.0 server and does not send a full version.
                bool showfull = ((user->IsOper()) && (!found->GetFullVersion().empty()));
-               const std::string& Version = (showfull ? found->GetFullVersion() : found->GetVersion());
-               user->WriteNumeric(RPL_VERSION, Version);
+
+               Numeric::Numeric numeric(RPL_VERSION);
+               irc::tokenstream tokens(showfull ? found->GetFullVersion() : found->GetVersion());
+               for (std::string token; tokens.GetTrailing(token); )
+                       numeric.push(token);
+               user->WriteNumeric(numeric);
        }
        else
        {
@@ -324,7 +324,7 @@ ModResult ModuleSpanningTree::HandleVersion(const std::vector<std::string>& para
        return MOD_RES_DENY;
 }
 
-ModResult ModuleSpanningTree::HandleConnect(const std::vector<std::string>& parameters, User* user)
+ModResult ModuleSpanningTree::HandleConnect(const CommandBase::Params& parameters, User* user)
 {
        for (std::vector<reference<Link> >::iterator i = Utils->LinkBlocks.begin(); i < Utils->LinkBlocks.end(); i++)
        {
@@ -360,10 +360,10 @@ void ModuleSpanningTree::OnUserInvite(User* source, User* dest, Channel* channel
        if (IS_LOCAL(source))
        {
                CmdBuilder params(source, "INVITE");
-               params.push_back(dest->uuid);
-               params.push_back(channel->name);
+               params.push(dest->uuid);
+               params.push(channel->name);
                params.push_int(channel->age);
-               params.push_back(ConvToStr(expiry));
+               params.push(ConvToStr(expiry));
                params.Broadcast();
        }
 }
@@ -395,28 +395,72 @@ void ModuleSpanningTree::OnUserPostMessage(User* user, const MessageTarget& targ
                return;
 
        const char* message_type = (details.type == MSG_PRIVMSG ? "PRIVMSG" : "NOTICE");
-       if (target.type == MessageTarget::TYPE_USER)
+       switch (target.type)
        {
-               User* d = target.Get<User>();
-               if (!IS_LOCAL(d))
+               case MessageTarget::TYPE_USER:
                {
-                       CmdBuilder params(user, message_type);
-                       params.push_back(d->uuid);
-                       params.push_last(details.text);
-                       params.Unicast(d);
+                       User* d = target.Get<User>();
+                       if (!IS_LOCAL(d))
+                       {
+                               CmdBuilder params(user, message_type);
+                               params.push_tags(details.tags_out);
+                               params.push(d->uuid);
+                               params.push_last(details.text);
+                               params.Unicast(d);
+                       }
+                       break;
+               }
+               case MessageTarget::TYPE_CHANNEL:
+               {
+                       Utils->SendChannelMessage(user, target.Get<Channel>(), details.text, target.status, details.tags_out, details.exemptions, message_type);
+                       break;
+               }
+               case MessageTarget::TYPE_SERVER:
+               {
+                       const std::string* serverglob = target.Get<std::string>();
+                       CmdBuilder par(user, message_type);
+                       par.push_tags(details.tags_out);
+                       par.push(std::string("$") + *serverglob);
+                       par.push_last(details.text);
+                       par.Broadcast();
+                       break;
                }
        }
-       else if (target.type == MessageTarget::TYPE_CHANNEL)
-       {
-               Utils->SendChannelMessage(user->uuid, target.Get<Channel>(), details.text, target.status, details.exemptions, message_type);
-       }
-       else if (target.type == MessageTarget::TYPE_SERVER)
+}
+
+void ModuleSpanningTree::OnUserPostTagMessage(User* user, const MessageTarget& target, const CTCTags::TagMessageDetails& details)
+{
+       if (!IS_LOCAL(user))
+               return;
+
+       switch (target.type)
        {
-               const std::string* serverglob = target.Get<std::string>();
-               CmdBuilder par(user, message_type);
-               par.push_back(*serverglob);
-               par.push_last(details.text);
-               par.Broadcast();
+               case MessageTarget::TYPE_USER:
+               {
+                       User* d = target.Get<User>();
+                       if (!IS_LOCAL(d))
+                       {
+                               CmdBuilder params(user, "TAGMSG");
+                               params.push_tags(details.tags_out);
+                               params.push(d->uuid);
+                               params.Unicast(d);
+                       }
+                       break;
+               }
+               case MessageTarget::TYPE_CHANNEL:
+               {
+                       Utils->SendChannelMessage(user, target.Get<Channel>(), "", target.status, details.tags_out, details.exemptions, "TAGMSG");
+                       break;
+               }
+               case MessageTarget::TYPE_SERVER:
+               {
+                       const std::string* serverglob = target.Get<std::string>();
+                       CmdBuilder par(user, "TAGMSG");
+                       par.push_tags(details.tags_out);
+                       par.push(std::string("$") + *serverglob);
+                       par.Broadcast();
+                       break;
+               }
        }
 }
 
@@ -431,6 +475,10 @@ void ModuleSpanningTree::OnUserConnect(LocalUser* user)
        if (user->quitting)
                return;
 
+       // Create the lazy ssl_cert metadata for this user if not already created.
+       if (sslapi)
+               sslapi->GetCertificate(user);
+
        CommandUID::Builder(user).Broadcast();
 
        if (user->IsOper())
@@ -439,7 +487,7 @@ void ModuleSpanningTree::OnUserConnect(LocalUser* user)
        for(Extensible::ExtensibleStore::const_iterator i = user->GetExtList().begin(); i != user->GetExtList().end(); i++)
        {
                ExtensionItem* item = i->first;
-               std::string value = item->serialize(FORMAT_NETWORK, user, i->second);
+               std::string value = item->ToNetwork(user, i->second);
                if (!value.empty())
                        ServerInstance->PI->SendMetaData(user, item->name, value);
        }
@@ -466,12 +514,12 @@ void ModuleSpanningTree::OnUserJoin(Membership* memb, bool sync, bool created_by
        else
        {
                CmdBuilder params(memb->user, "IJOIN");
-               params.push_back(memb->chan->name);
+               params.push(memb->chan->name);
                params.push_int(memb->id);
                if (!memb->modes.empty())
                {
-                       params.push_back(ConvToStr(memb->chan->age));
-                       params.push_back(memb->modes);
+                       params.push(ConvToStr(memb->chan->age));
+                       params.push(memb->modes);
                }
                params.Broadcast();
        }
@@ -485,12 +533,12 @@ void ModuleSpanningTree::OnChangeHost(User* user, const std::string &newhost)
        CmdBuilder(user, "FHOST").push(newhost).Broadcast();
 }
 
-void ModuleSpanningTree::OnChangeName(User* user, const std::string &gecos)
+void ModuleSpanningTree::OnChangeRealName(User* user, const std::string& real)
 {
        if (user->registered != REG_ALL || !IS_LOCAL(user))
                return;
 
-       CmdBuilder(user, "FNAME").push_last(gecos).Broadcast();
+       CmdBuilder(user, "FNAME").push_last(real).Broadcast();
 }
 
 void ModuleSpanningTree::OnChangeIdent(User* user, const std::string &ident)
@@ -506,7 +554,7 @@ void ModuleSpanningTree::OnUserPart(Membership* memb, std::string &partmessage,
        if (IS_LOCAL(memb->user))
        {
                CmdBuilder params(memb->user, "PART");
-               params.push_back(memb->chan->name);
+               params.push(memb->chan->name);
                if (!partmessage.empty())
                        params.push_last(partmessage);
                params.Broadcast();
@@ -546,8 +594,8 @@ void ModuleSpanningTree::OnUserPostNick(User* user, const std::string &oldnick)
        {
                // The nick TS is updated by the core, we don't do it
                CmdBuilder params(user, "NICK");
-               params.push_back(user->nick);
-               params.push_back(ConvToStr(user->age));
+               params.push(user->nick);
+               params.push(ConvToStr(user->age));
                params.Broadcast();
        }
        else if (!loopCall)
@@ -562,8 +610,8 @@ void ModuleSpanningTree::OnUserKick(User* source, Membership* memb, const std::s
                return;
 
        CmdBuilder params(source, "KICK");
-       params.push_back(memb->chan->name);
-       params.push_back(memb->user->uuid);
+       params.push(memb->chan->name);
+       params.push(memb->user->uuid);
        // If a remote user is being kicked by us then send the membership id in the kick too
        if (!IS_LOCAL(memb->user))
                params.push_int(memb->id);
@@ -573,16 +621,13 @@ void ModuleSpanningTree::OnUserKick(User* source, Membership* memb, const std::s
 
 void ModuleSpanningTree::OnPreRehash(User* user, const std::string &parameter)
 {
-       if (loopCall)
-               return; // Don't generate a REHASH here if we're in the middle of processing a message that generated this one
-
        ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "OnPreRehash called with param %s", parameter.c_str());
 
        // Send out to other servers
        if (!parameter.empty() && parameter[0] != '-')
        {
-               CmdBuilder params((user ? user->uuid : ServerInstance->Config->GetSID()), "REHASH");
-               params.push_back(parameter);
+               CmdBuilder params(user ? user : ServerInstance->FakeClient, "REHASH");
+               params.push(parameter);
                params.Forward(user ? TreeServer::Get(user)->GetRoute() : NULL);
        }
 }
@@ -645,7 +690,7 @@ void ModuleSpanningTree::OnUnloadModule(Module* mod)
                {
                        TreeServer* server = i->second;
                        if (!server->IsRoot())
-                               FOREACH_MOD_CUSTOM(GetEventProvider(), ServerEventListener, OnServerSplit, (server));
+                               FOREACH_MOD_CUSTOM(GetLinkEventProvider(), ServerProtocol::LinkEventListener, OnServerSplit, (server, false));
                }
                return;
        }
@@ -705,20 +750,24 @@ void ModuleSpanningTree::OnDelLine(User* user, XLine *x)
                user = ServerInstance->FakeClient;
 
        CmdBuilder params(user, "DELLINE");
-       params.push_back(x->type);
-       params.push_back(x->Displayable());
+       params.push(x->type);
+       params.push(x->Displayable());
        params.Broadcast();
 }
 
-ModResult ModuleSpanningTree::OnSetAway(User* user, const std::string &awaymsg)
+void ModuleSpanningTree::OnUserAway(User* user)
 {
        if (IS_LOCAL(user))
-               CommandAway::Builder(user, awaymsg).Broadcast();
+               CommandAway::Builder(user).Broadcast();
+}
 
-       return MOD_RES_PASSTHRU;
+void ModuleSpanningTree::OnUserBack(User* user)
+{
+       if (IS_LOCAL(user))
+               CommandAway::Builder(user).Broadcast();
 }
 
-void ModuleSpanningTree::OnMode(User* source, User* u, Channel* c, const Modes::ChangeList& modes, ModeParser::ModeProcessFlag processflags, const std::string& output_mode)
+void ModuleSpanningTree::OnMode(User* source, User* u, Channel* c, const Modes::ChangeList& modes, ModeParser::ModeProcessFlag processflags)
 {
        if (processflags & ModeParser::MODE_LOCALONLY)
                return;
@@ -730,7 +779,7 @@ void ModuleSpanningTree::OnMode(User* source, User* u, Channel* c, const Modes::
 
                CmdBuilder params(source, "MODE");
                params.push(u->uuid);
-               params.push(output_mode);
+               params.push(ClientProtocol::Messages::Mode::ToModeLetters(modes));
                params.push_raw(Translate::ModeChangeListToParams(modes.getlist()));
                params.Broadcast();
        }
@@ -739,12 +788,19 @@ void ModuleSpanningTree::OnMode(User* source, User* u, Channel* c, const Modes::
                CmdBuilder params(source, "FMODE");
                params.push(c->name);
                params.push_int(c->age);
-               params.push(output_mode);
+               params.push(ClientProtocol::Messages::Mode::ToModeLetters(modes));
                params.push_raw(Translate::ModeChangeListToParams(modes.getlist()));
                params.Broadcast();
        }
 }
 
+void ModuleSpanningTree::OnShutdown(const std::string& reason)
+{
+       const TreeServer::ChildServers& children = Utils->TreeRoot->GetChildren();
+       while (!children.empty())
+               children.front()->SQuit(reason, true);
+}
+
 CullResult ModuleSpanningTree::cull()
 {
        if (Utils)
@@ -756,7 +812,7 @@ ModuleSpanningTree::~ModuleSpanningTree()
 {
        ServerInstance->PI = &ServerInstance->DefaultProtocolInterface;
 
-       Server* newsrv = new Server(ServerInstance->Config->ServerName, ServerInstance->Config->ServerDesc);
+       Server* newsrv = new Server(ServerInstance->Config->GetSID(), ServerInstance->Config->ServerName, ServerInstance->Config->ServerDesc);
        SetLocalUsersServer(newsrv);
 
        delete Utils;