]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_spanningtree/main.cpp
Replaced vsnprintf with VAFORMAT pretty much everywhere.
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / main.cpp
index efd899c44b3f07063548cee3a5a587b73d32e508..244180b2f4e5875797d5fa3ca67a8aa1d0dce7da 100644 (file)
@@ -27,7 +27,6 @@
 #include "socket.h"
 #include "xline.h"
 
-#include "cachetimer.h"
 #include "resolvers.h"
 #include "main.h"
 #include "utils.h"
 #include "protocolinterface.h"
 
 ModuleSpanningTree::ModuleSpanningTree()
+       : commands(NULL), DNS(this, "DNS"), Utils(NULL)
 {
-       Utils = new SpanningTreeUtilities(this);
-       commands = new SpanningTreeCommands(this);
-       RefreshTimer = NULL;
 }
 
 SpanningTreeCommands::SpanningTreeCommands(ModuleSpanningTree* module)
        : rconnect(module, module->Utils), rsquit(module, module->Utils),
        svsjoin(module), svspart(module), svsnick(module), metadata(module),
-       uid(module), opertype(module), fjoin(module), fmode(module), ftopic(module),
-       fhost(module), fident(module), fname(module)
+       uid(module), opertype(module), fjoin(module), ijoin(module), resync(module),
+       fmode(module), ftopic(module), fhost(module), fident(module), fname(module)
 {
 }
 
 void ModuleSpanningTree::init()
 {
+       Utils = new SpanningTreeUtilities(this);
+       commands = new SpanningTreeCommands(this);
        ServerInstance->Modules->AddService(commands->rconnect);
        ServerInstance->Modules->AddService(commands->rsquit);
        ServerInstance->Modules->AddService(commands->svsjoin);
@@ -63,13 +62,13 @@ void ModuleSpanningTree::init()
        ServerInstance->Modules->AddService(commands->uid);
        ServerInstance->Modules->AddService(commands->opertype);
        ServerInstance->Modules->AddService(commands->fjoin);
+       ServerInstance->Modules->AddService(commands->ijoin);
+       ServerInstance->Modules->AddService(commands->resync);
        ServerInstance->Modules->AddService(commands->fmode);
        ServerInstance->Modules->AddService(commands->ftopic);
        ServerInstance->Modules->AddService(commands->fhost);
        ServerInstance->Modules->AddService(commands->fident);
        ServerInstance->Modules->AddService(commands->fname);
-       RefreshTimer = new CacheRefreshTimer(Utils);
-       ServerInstance->Timers->AddTimer(RefreshTimer);
 
        Implementation eventlist[] =
        {
@@ -87,7 +86,7 @@ void ModuleSpanningTree::init()
        loopCall = false;
 
        // update our local user count
-       Utils->TreeRoot->SetUserCount(ServerInstance->Users->local_users.size());
+       Utils->TreeRoot->UserCount = ServerInstance->Users->local_users.size();
 }
 
 void ModuleSpanningTree::ShowLinks(TreeServer* Current, User* user, int hops)
@@ -194,8 +193,7 @@ restart:
                                        // ... if we can find a proper route to them
                                        if (tsock)
                                        {
-                                               tsock->WriteLine(":" + ServerInstance->Config->GetSID() + " PING " +
-                                                               ServerInstance->Config->GetSID() + " " + s->GetID());
+                                               tsock->WriteLine(":" + ServerInstance->Config->GetSID() + " PING " + s->GetID());
                                                s->LastPingMsec = ts;
                                        }
                                }
@@ -271,8 +269,7 @@ void ModuleSpanningTree::ConnectServer(Link* x, Autoconnect* y)
                return;
        }
 
-       QueryType start_type = DNS_QUERY_A;
-       start_type = DNS_QUERY_AAAA;
+       DNS::QueryType start_type = DNS::QUERY_AAAA;
        if (strchr(x->IPAddr.c_str(),':'))
        {
                in6_addr n;
@@ -302,16 +299,20 @@ void ModuleSpanningTree::ConnectServer(Link* x, Autoconnect* y)
                        ServerInstance->GlobalCulls.AddItem(newsocket);
                }
        }
+       else if (!DNS)
+       {
+               ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Error connecting \002%s\002: Hostname given and m_dns.so is not loaded, unable to resolve.", x->Name.c_str());
+       }
        else
        {
+               ServernameResolver* snr = new ServernameResolver(Utils, *DNS, x->IPAddr, x, start_type, y);
                try
                {
-                       bool cached = false;
-                       ServernameResolver* snr = new ServernameResolver(Utils, x->IPAddr, x, cached, start_type, y);
-                       ServerInstance->AddResolver(snr, cached);
+                       DNS->Process(snr);
                }
-               catch (ModuleException& e)
+               catch (DNS::Exception& e)
                {
+                       delete snr;
                        ServerInstance->SNO->WriteToSnoMask('l', "CONNECT: Error connecting \002%s\002: %s.",x->Name.c_str(), e.GetReason());
                        ConnectServer(y, false);
                }
@@ -379,15 +380,11 @@ ModResult ModuleSpanningTree::HandleVersion(const std::vector<std::string>& para
  */
 void ModuleSpanningTree::RemoteMessage(User* user, const char* format, ...)
 {
-       char text[MAXBUF];
-       va_list argsPtr;
-
-       va_start(argsPtr, format);
-       vsnprintf(text, MAXBUF, format, argsPtr);
-       va_end(argsPtr);
+       std::string text;
+       VAFORMAT(text, format, format);
 
        if (IS_LOCAL(user))
-               user->WriteServ("NOTICE %s :%s", user->nick.c_str(), text);
+               user->WriteNotice(text);
        else
                ServerInstance->PI->SendUserNotice(user, text);
 }
@@ -467,108 +464,45 @@ void ModuleSpanningTree::OnWallops(User* user, const std::string &text)
        }
 }
 
-void ModuleSpanningTree::OnUserNotice(User* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
+void ModuleSpanningTree::LocalMessage(User* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list, const char* message_type)
 {
-       /* Server origin */
-       if (user == NULL)
+       /* Server or remote origin, dest should always be non-null */
+       if ((!user) || (!IS_LOCAL(user)) || (!dest))
                return;
 
        if (target_type == TYPE_USER)
        {
-               User* d = (User*)dest;
-               if (!IS_LOCAL(d) && IS_LOCAL(user))
+               User* d = (User*) dest;
+               if (!IS_LOCAL(d))
                {
                        parameterlist params;
                        params.push_back(d->uuid);
                        params.push_back(":"+text);
-                       Utils->DoOneToOne(user->uuid,"NOTICE",params,d->server);
+                       Utils->DoOneToOne(user->uuid, message_type, params, d->server);
                }
        }
        else if (target_type == TYPE_CHANNEL)
        {
-               if (IS_LOCAL(user))
-               {
-                       Channel *c = (Channel*)dest;
-                       if (c)
-                       {
-                               std::string cname = c->name;
-                               if (status)
-                                       cname = status + cname;
-                               TreeServerList list;
-                               Utils->GetListOfServersForChannel(c,list,status,exempt_list);
-                               for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
-                               {
-                                       TreeSocket* Sock = i->second->GetSocket();
-                                       if (Sock)
-                                               Sock->WriteLine(":"+std::string(user->uuid)+" NOTICE "+cname+" :"+text);
-                               }
-                       }
-               }
+               Utils->SendChannelMessage(user->uuid, (Channel*)dest, text, status, exempt_list, message_type);
        }
        else if (target_type == TYPE_SERVER)
        {
-               if (IS_LOCAL(user))
-               {
-                       char* target = (char*)dest;
-                       parameterlist par;
-                       par.push_back(target);
-                       par.push_back(":"+text);
-                       Utils->DoOneToMany(user->uuid,"NOTICE",par);
-               }
+               char* target = (char*) dest;
+               parameterlist par;
+               par.push_back(target);
+               par.push_back(":"+text);
+               Utils->DoOneToMany(user->uuid, message_type, par);
        }
 }
 
-void ModuleSpanningTree::OnUserMessage(User* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
+void ModuleSpanningTree::OnUserNotice(User* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
 {
-       /* Server origin */
-       if (user == NULL)
-               return;
+       LocalMessage(user, dest, target_type, text, status, exempt_list, "NOTICE");
+}
 
-       if (target_type == TYPE_USER)
-       {
-               // route private messages which are targetted at clients only to the server
-               // which needs to receive them
-               User* d = (User*)dest;
-               if (!IS_LOCAL(d) && (IS_LOCAL(user)))
-               {
-                       parameterlist params;
-                       params.push_back(d->uuid);
-                       params.push_back(":"+text);
-                       Utils->DoOneToOne(user->uuid,"PRIVMSG",params,d->server);
-               }
-       }
-       else if (target_type == TYPE_CHANNEL)
-       {
-               if (IS_LOCAL(user))
-               {
-                       Channel *c = (Channel*)dest;
-                       if (c)
-                       {
-                               std::string cname = c->name;
-                               if (status)
-                                       cname = status + cname;
-                               TreeServerList list;
-                               Utils->GetListOfServersForChannel(c,list,status,exempt_list);
-                               for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
-                               {
-                                       TreeSocket* Sock = i->second->GetSocket();
-                                       if (Sock)
-                                               Sock->WriteLine(":"+std::string(user->uuid)+" PRIVMSG "+cname+" :"+text);
-                               }
-                       }
-               }
-       }
-       else if (target_type == TYPE_SERVER)
-       {
-               if (IS_LOCAL(user))
-               {
-                       char* target = (char*)dest;
-                       parameterlist par;
-                       par.push_back(target);
-                       par.push_back(":"+text);
-                       Utils->DoOneToMany(user->uuid,"PRIVMSG",par);
-               }
-       }
+void ModuleSpanningTree::OnUserMessage(User* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
+{
+       LocalMessage(user, dest, target_type, text, status, exempt_list, "PRIVMSG");
 }
 
 void ModuleSpanningTree::OnBackgroundTimer(time_t curtime)
@@ -599,7 +533,8 @@ void ModuleSpanningTree::OnUserConnect(LocalUser* user)
        if (user->IsOper())
        {
                params.clear();
-               params.push_back(user->oper->name);
+               params.push_back(":");
+               params[0].append(user->oper->name);
                Utils->DoOneToMany(user->uuid,"OPERTYPE",params);
        }
 
@@ -611,23 +546,32 @@ void ModuleSpanningTree::OnUserConnect(LocalUser* user)
                        ServerInstance->PI->SendMetaData(user, item->name, value);
        }
 
-       Utils->TreeRoot->SetUserCount(1); // increment by 1
+       Utils->TreeRoot->UserCount++;
 }
 
-void ModuleSpanningTree::OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts)
+void ModuleSpanningTree::OnUserJoin(Membership* memb, bool sync, bool created_by_local, CUList& excepts)
 {
        // Only do this for local users
        if (IS_LOCAL(memb->user))
        {
                parameterlist params;
-               // set up their permissions and the channel TS with FJOIN.
-               // All users are FJOINed now, because a module may specify
-               // new joining permissions for the user.
                params.push_back(memb->chan->name);
-               params.push_back(ConvToStr(memb->chan->age));
-               params.push_back(std::string("+") + memb->chan->ChanModes(true));
-               params.push_back(memb->modes+","+memb->user->uuid);
-               Utils->DoOneToMany(ServerInstance->Config->GetSID(),"FJOIN",params);
+               if (created_by_local)
+               {
+                       params.push_back(ConvToStr(memb->chan->age));
+                       params.push_back(std::string("+") + memb->chan->ChanModes(true));
+                       params.push_back(memb->modes+","+memb->user->uuid);
+                       Utils->DoOneToMany(ServerInstance->Config->GetSID(),"FJOIN",params);
+               }
+               else
+               {
+                       if (!memb->modes.empty())
+                       {
+                               params.push_back(ConvToStr(memb->chan->age));
+                               params.push_back(memb->modes);
+                       }
+                       Utils->DoOneToMany(memb->user->uuid, "IJOIN", params);
+               }
        }
 }
 
@@ -694,7 +638,7 @@ void ModuleSpanningTree::OnUserQuit(User* user, const std::string &reason, const
        TreeServer* SourceServer = Utils->FindServer(user->server);
        if (SourceServer)
        {
-               SourceServer->SetUserCount(-1); // decrement by 1
+               SourceServer->UserCount--;
        }
 }
 
@@ -758,7 +702,7 @@ 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("remoterehash", DEBUG, "called with param %s", parameter.c_str());
+       ServerInstance->Logs->Log("remoterehash", LOG_DEBUG, "called with param %s", parameter.c_str());
 
        // Send out to other servers
        if (!parameter.empty() && parameter[0] != '-')
@@ -784,7 +728,7 @@ void ModuleSpanningTree::OnRehash(User* user)
                std::string msg = "Error in configuration: ";
                msg.append(e.GetReason());
                ServerInstance->SNO->WriteToSnoMask('l', msg);
-               if (!IS_LOCAL(user))
+               if (user && !IS_LOCAL(user))
                        ServerInstance->PI->SendSNONotice("L", msg);
        }
 }
@@ -828,7 +772,8 @@ void ModuleSpanningTree::OnOper(User* user, const std::string &opertype)
        if (user->registered != REG_ALL || !IS_LOCAL(user))
                return;
        parameterlist params;
-       params.push_back(opertype);
+       params.push_back(":");
+       params[0].append(opertype);
        Utils->DoOneToMany(user->uuid,"OPERTYPE",params);
 }
 
@@ -951,7 +896,7 @@ void ModuleSpanningTree::ProtoSendMetaData(void* opaque, Extensible* target, con
        if (u)
                s->WriteLine(":"+ServerInstance->Config->GetSID()+" METADATA "+u->uuid+" "+extname+" :"+extdata);
        else if (c)
-               s->WriteLine(":"+ServerInstance->Config->GetSID()+" METADATA "+c->name+" "+extname+" :"+extdata);
+               s->WriteLine(":"+ServerInstance->Config->GetSID()+" METADATA "+c->name+" "+ConvToStr(c->age)+" "+extname+" :"+extdata);
        else if (!target)
                s->WriteLine(":"+ServerInstance->Config->GetSID()+" METADATA * "+extname+" :"+extdata);
 }
@@ -959,7 +904,6 @@ void ModuleSpanningTree::ProtoSendMetaData(void* opaque, Extensible* target, con
 CullResult ModuleSpanningTree::cull()
 {
        Utils->cull();
-       ServerInstance->Timers->DelTimer(RefreshTimer);
        return this->Module::cull();
 }