]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_spanningtree.cpp
Clean up ghosts on nick collision
[user/henk/code/inspircd.git] / src / modules / m_spanningtree.cpp
index 369bda8ef765dfe2e5d5291523a1bbf317a9811b..ca25fe4a689938d2b09bed10e10cfcbf55f66043 100644 (file)
@@ -2,7 +2,7 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  Inspire is copyright (C) 2002-2005 ChatSpike-Dev.
+ *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
  *                       E-mail:
  *                <brain@chatspike.net>
  *               <Craig@chatspike.net>
@@ -203,7 +203,7 @@ class TreeServer
        {
                VersionString = "";
                UserCount = OperCount = 0;
-               this->SetNextPingTime(time(NULL) + 60);
+               this->SetNextPingTime(time(NULL) + 120);
                this->SetPingFlag();
 
                /* find the 'route' for this server (e.g. the one directly connected
@@ -452,6 +452,7 @@ class Link
         unsigned long AutoConnect;
         time_t NextConnectTime;
         std::string EncryptionKey;
+        bool HiddenFromStats;
 };
 
 /* The usual stuff for inspircd modules,
@@ -634,12 +635,12 @@ class TreeSocket : public InspSocket
        {
                if (this->LinkState == CONNECTING)
                {
-                       Srv->SendOpers("*** Connection to "+myhost+"["+this->GetIP()+"] established.");
                        /* we do not need to change state here. */
                        for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
                        {
                                if (x->Name == this->myhost)
                                {
+                                       Srv->SendOpers("*** Connection to \2"+myhost+"\2["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] established.");
                                        this->SendCapabilities();
                                        if (x->EncryptionKey != "")
                                        {
@@ -664,6 +665,7 @@ class TreeSocket : public InspSocket
                 * If that happens the connection hangs here until it's closed. Unlikely
                 * and rather harmless.
                 */
+               Srv->SendOpers("*** Connection to \2"+myhost+"\2 lost link tag(!)");
                return true;
        }
        
@@ -853,6 +855,7 @@ class TreeSocket : public InspSocket
                time_t ts = atoi(params[1].c_str());
                std::string setby = params[2];
                std::string topic = params[3];
+               std::string nsource = source;
 
                chanrec* c = Srv->FindChannel(channel);
                if (c)
@@ -868,14 +871,25 @@ class TreeSocket : public InspSocket
                                 * update the set time and set nick.
                                 */
                                if (oldtopic != topic)
-                                       WriteChannelWithServ((char*)source.c_str(), c, "TOPIC %s :%s", c->name, c->topic);
+                               {
+                                       userrec* user = Srv->FindNick(source);
+                                       if (!user)
+                                       {
+                                               WriteChannelWithServ((char*)source.c_str(), c, "TOPIC %s :%s", c->name, c->topic);
+                                       }
+                                       else
+                                       {
+                                               WriteChannel(c, user, "TOPIC %s :%s", c->name, c->topic);
+                                               nsource = user->server;
+                                       }
+                               }
                        }
                        
                }
                
                /* all done, send it on its way */
                params[3] = ":" + params[3];
-               DoOneToAllButSender(source,"FTOPIC",params,source);
+               DoOneToAllButSender(source,"FTOPIC",params,nsource);
 
                return true;
        }
@@ -927,56 +941,61 @@ class TreeSocket : public InspSocket
                {
                        /* process one channel at a time, applying modes. */
                        char* usr = (char*)params[usernum].c_str();
-                       char permissions = *usr;
-                       switch (permissions)
-                       {
-                               case '@':
-                                       usr++;
-                                       mode_users[modectr++] = usr;
-                                       strlcat(modestring,"o",MAXBUF);
-                               break;
-                               case '%':
-                                       usr++;
-                                       mode_users[modectr++] = usr;
-                                       strlcat(modestring,"h",MAXBUF);
-                               break;
-                               case '+':
-                                       usr++;
-                                       mode_users[modectr++] = usr;
-                                       strlcat(modestring,"v",MAXBUF);
-                               break;
-                       }
-                       who = Srv->FindNick(usr);
-                       if (who)
-                       {
-                               Srv->JoinUserToChannel(who,channel,key);
-                               if (modectr >= (MAXMODES-1))
-                               {
-                                       /* theres a mode for this user. push them onto the mode queue, and flush it
-                                        * if there are more than MAXMODES to go.
-                                        */
-                                       if ((ourTS >= TS) || (Srv->IsUlined(who->server)))
+                       /* Safety check just to make sure someones not sent us an FJOIN full of spaces
+                        * (is this even possible?) */
+                       if (usr && *usr)
+                       {
+                               char permissions = *usr;
+                               switch (permissions)
+                               {
+                                       case '@':
+                                               usr++;
+                                               mode_users[modectr++] = usr;
+                                               strlcat(modestring,"o",MAXBUF);
+                                       break;
+                                       case '%':
+                                               usr++;
+                                               mode_users[modectr++] = usr;
+                                               strlcat(modestring,"h",MAXBUF);
+                                       break;
+                                       case '+':
+                                               usr++;
+                                               mode_users[modectr++] = usr;
+                                               strlcat(modestring,"v",MAXBUF);
+                                       break;
+                               }
+                               who = Srv->FindNick(usr);
+                               if (who)
+                               {
+                                       Srv->JoinUserToChannel(who,channel,key);
+                                       if (modectr >= (MAXMODES-1))
                                        {
-                                               /* We also always let u-lined clients win, no matter what the TS value */
-                                               log(DEBUG,"Our our channel newer than theirs, accepting their modes");
-                                               Srv->SendMode(mode_users,modectr,who);
-                                       }
-                                       else
-                                       {
-                                               log(DEBUG,"Their channel newer than ours, bouncing their modes");
-                                               /* bouncy bouncy! */
-                                               std::deque<std::string> params;
-                                               /* modes are now being UNSET... */
-                                               *mode_users[1] = '-';
-                                               for (unsigned int x = 0; x < modectr; x++)
+                                               /* theres a mode for this user. push them onto the mode queue, and flush it
+                                                * if there are more than MAXMODES to go.
+                                                */
+                                               if ((ourTS >= TS) || (Srv->IsUlined(who->server)))
                                                {
-                                                       params.push_back(mode_users[x]);
+                                                       /* We also always let u-lined clients win, no matter what the TS value */
+                                                       log(DEBUG,"Our our channel newer than theirs, accepting their modes");
+                                                       Srv->SendMode(mode_users,modectr,who);
                                                }
-                                               // tell everyone to bounce the modes. bad modes, bad!
-                                               DoOneToMany(Srv->GetServerName(),"FMODE",params);
+                                               else
+                                               {
+                                                       log(DEBUG,"Their channel newer than ours, bouncing their modes");
+                                                       /* bouncy bouncy! */
+                                                       std::deque<std::string> params;
+                                                       /* modes are now being UNSET... */
+                                                       *mode_users[1] = '-';
+                                                       for (unsigned int x = 0; x < modectr; x++)
+                                                       {
+                                                               params.push_back(mode_users[x]);
+                                                       }
+                                                       // tell everyone to bounce the modes. bad modes, bad!
+                                                       DoOneToMany(Srv->GetServerName(),"FMODE",params);
+                                               }
+                                               strcpy(mode_users[1],"+");
+                                               modectr = 2;
                                        }
-                                       strcpy(mode_users[1],"+");
-                                       modectr = 2;
                                }
                        }
                }
@@ -1050,7 +1069,7 @@ class TreeSocket : public InspSocket
                clientlist[tempnick]->registered = 7;
                clientlist[tempnick]->signon = age;
                strlcpy(clientlist[tempnick]->modes, modes.c_str(),53);
-               strlcpy(clientlist[tempnick]->ip,ip.c_str(),16);
+               inet_aton(ip.c_str(),&clientlist[tempnick]->ip4);
 
                ucrec a;
                a.channel = NULL;
@@ -1060,7 +1079,7 @@ class TreeSocket : public InspSocket
 
                if (!this->bursting)
                {
-                       WriteOpers("*** Client connecting at %s: %s!%s@%s [%s]",clientlist[tempnick]->server,clientlist[tempnick]->nick,clientlist[tempnick]->ident,clientlist[tempnick]->host,clientlist[tempnick]->ip);
+                       WriteOpers("*** Client connecting at %s: %s!%s@%s [%s]",clientlist[tempnick]->server,clientlist[tempnick]->nick,clientlist[tempnick]->ident,clientlist[tempnick]->host,(char*)inet_ntoa(clientlist[tempnick]->ip4));
                }
                params[7] = ":" + params[7];
                DoOneToAllButSender(source,"NICK",params,source);
@@ -1083,10 +1102,10 @@ class TreeSocket : public InspSocket
                log(DEBUG,"Sending FJOINs to other server for %s",c->name);
                char list[MAXBUF];
                snprintf(list,MAXBUF,":%s FJOIN %s %lu",Srv->GetServerName().c_str(),c->name,(unsigned long)c->age);
-               std::vector<char*> *ulist = c->GetUsers();
-               for (unsigned int i = 0; i < ulist->size(); i++)
+               std::map<char*,char*> *ulist = c->GetUsers();
+               for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
                {
-                       char* o = (*ulist)[i];
+                       char* o = i->second;
                        userrec* otheruser = (userrec*)o;
                        strlcat(list," ",MAXBUF);
                        strlcat(list,cmode(otheruser,c),MAXBUF);
@@ -1160,7 +1179,7 @@ class TreeSocket : public InspSocket
                for (chan_hash::iterator c = chanlist.begin(); c != chanlist.end(); c++)
                {
                        SendFJoins(Current, c->second);
-                       snprintf(data,MAXBUF,":%s FMODE %s +%s",Srv->GetServerName().c_str(),c->second->name,chanmodes(c->second));
+                       snprintf(data,MAXBUF,":%s FMODE %s +%s",Srv->GetServerName().c_str(),c->second->name,chanmodes(c->second,true));
                        this->WriteLine(data);
                        if (*c->second->topic)
                        {
@@ -1191,7 +1210,7 @@ class TreeSocket : public InspSocket
                {
                        if (u->second->registered == 7)
                        {
-                               snprintf(data,MAXBUF,":%s NICK %lu %s %s %s %s +%s %s :%s",u->second->server,(unsigned long)u->second->age,u->second->nick,u->second->host,u->second->dhost,u->second->ident,u->second->modes,u->second->ip,u->second->fullname);
+                               snprintf(data,MAXBUF,":%s NICK %lu %s %s %s %s +%s %s :%s",u->second->server,(unsigned long)u->second->age,u->second->nick,u->second->host,u->second->dhost,u->second->ident,u->second->modes,(char*)inet_ntoa(u->second->ip4),u->second->fullname);
                                this->WriteLine(data);
                                if (strchr(u->second->modes,'o'))
                                {
@@ -1226,6 +1245,7 @@ class TreeSocket : public InspSocket
                /* Send everything else (channel modes, xlines etc) */
                this->SendChannelModes(s);
                this->SendXLines(s);
+               FOREACH_MOD(I_OnSyncOtherMetaData,OnSyncOtherMetaData((Module*)TreeProtocolModule,(void*)this));
                this->WriteLine("ENDBURST");
                Srv->SendOpers("*** Finished bursting to \2"+s->GetName()+"\2.");
        }
@@ -1241,7 +1261,8 @@ class TreeSocket : public InspSocket
         virtual bool OnDataReady()
        {
                char* data = this->Read();
-               if (data)
+               /* Check that the data read is a valid pointer and it has some content */
+               if (data && *data)
                {
                        this->in_buffer += data;
                        /* While there is at least one new line in the buffer,
@@ -1290,6 +1311,9 @@ class TreeSocket : public InspSocket
                                }
                        }
                }
+               /* EAGAIN returns an empty but non-NULL string, so this
+                * evaluates to TRUE for EAGAIN but to FALSE for EOF.
+                */
                return (data != NULL);
        }
 
@@ -1450,7 +1474,11 @@ class TreeSocket : public InspSocket
                TreeServer* ServerSource = FindServer(prefix);
                if (ServerSource)
                {
-                       if (*(params[0].c_str()) == '#')
+                       if (params[0] == "*")
+                       {
+                               FOREACH_MOD(I_OnDecodeMetaData,OnDecodeMetaData(TYPE_OTHER,NULL,params[1],params[2]));
+                       }
+                       else if (*(params[0].c_str()) == '#')
                        {
                                chanrec* c = Srv->FindChannel(params[0]);
                                if (c)
@@ -1458,7 +1486,7 @@ class TreeSocket : public InspSocket
                                        FOREACH_MOD(I_OnDecodeMetaData,OnDecodeMetaData(TYPE_CHANNEL,c,params[1],params[2]));
                                }
                        }
-                       else
+                       else if (*(params[0].c_str()) != '#')
                        {
                                userrec* u = Srv->FindNick(params[0]);
                                if (u)
@@ -1735,7 +1763,7 @@ class TreeSocket : public InspSocket
                                        Srv->SendOpers("*** Server connection from \2"+servername+"\2 denied, remote server did not enable AES.");
                                        return false;
                                }
-                               Srv->SendOpers("*** Verified incoming server connection from \002"+servername+"\002["+this->GetIP()+"] ("+description+")");
+                               Srv->SendOpers("*** Verified incoming server connection from \002"+servername+"\002["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] ("+description+")");
                                this->InboundServerName = servername;
                                this->InboundDescription = description;
                                // this is good. Send our details: Our server name and description and hopcount of 0,
@@ -1810,6 +1838,7 @@ class TreeSocket : public InspSocket
                if (line == "")
                        return true;
                Srv->Log(DEBUG,"IN: "+line);
+               
                std::deque<std::string> params;
                this->Split(line,true,params);
                std::string command = "";
@@ -1878,6 +1907,11 @@ class TreeSocket : public InspSocket
                                {
                                        return this->Capab(params);
                                }
+                               else if ((command == "U") || (command == "S"))
+                               {
+                                       this->WriteLine("ERROR :Cannot use the old-style mesh linking protocol with m_spanningtree.so!");
+                                       return false;
+                               }
                                else
                                {
                                        this->WriteLine("ERROR :Invalid command in negotiation phase.");
@@ -1893,6 +1927,11 @@ class TreeSocket : public InspSocket
                                        // silently ignore.
                                        return true;
                                }
+                               else if ((command == "U") || (command == "S"))
+                               {
+                                       this->WriteLine("ERROR :Cannot use the old-style mesh linking protocol with m_spanningtree.so!");
+                                       return false;
+                               }
                                else if (command == "BURST")
                                {
                                        this->LinkState = CONNECTED;
@@ -1956,6 +1995,13 @@ class TreeSocket : public InspSocket
                                                        log(DEBUG,"Protocol violation: Fake direction in command '%s' from connection '%s'",line.c_str(),this->GetName().c_str());
                                                return true;
                                        }
+
+                                       /* Fix by brain:
+                                        * When there is activity on the socket, reset the ping counter so
+                                        * that we're not wasting bandwidth pinging an active server.
+                                        */                     
+                                       route_back_again->SetNextPingTime(time(NULL) + 120);
+                                       route_back_again->SetPingFlag();
                                }
                                
                                if (command == "SVSMODE")
@@ -2080,6 +2126,32 @@ class TreeSocket : public InspSocket
                                        }
                                        if (who)
                                        {
+                                               if ((command == "NICK") && (params.size() > 0))
+                                               {
+                                                       /* On nick messages, check that the nick doesnt
+                                                        * already exist here. If it does, kill their copy,
+                                                        * and our copy.
+                                                        */
+                                                       userrec* x = Srv->FindNick(params[0]);
+                                                       if (x)
+                                                       {
+                                                               std::deque<std::string> p;
+                                                               p.push_back(params[0]);
+                                                               p.push_back("Nickname collision ("+prefix+" -> "+params[0]+")");
+                                                               DoOneToMany(Srv->GetServerName(),"KILL",p);
+                                                               p.clear();
+                                                               p.push_back(prefix);
+                                                               p.push_back("KILL "+prefix+" :Nickname collision");
+                                                               DoOneToMany(Srv->GetServerName(),"KILL",p);
+                                                               Srv->QuitUser(x,"Nickname collision ("+prefix+" -> "+params[0]+")");
+                                                               userrec* y = Srv->FindNick(prefix);
+                                                               if (y)
+                                                               {
+                                                                       Srv->QuitUser(y,"Nickname collision");
+                                                               }
+                                                               return DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
+                                                       }
+                                               }
                                                // its a user
                                                target = who->server;
                                                char* strparams[127];
@@ -2144,7 +2216,7 @@ class TreeSocket : public InspSocket
                {
                        Squit(s,"Remote host closed the connection");
                }
-               WriteOpers("Server '\2%s\2[%s]' closed the connection.",quitserver.c_str(),this->GetIP().c_str());
+               WriteOpers("Server '\2%s\2' closed the connection.",quitserver.c_str());
        }
 
        virtual int OnIncomingConnection(int newsock, char* ip)
@@ -2170,11 +2242,10 @@ void AddThisServer(TreeServer* server, std::deque<TreeServer*> &list)
 // returns a list of DIRECT servernames for a specific channel
 void GetListOfServersForChannel(chanrec* c, std::deque<TreeServer*> &list)
 {
-       std::vector<char*> *ulist = c->GetUsers();
-       unsigned int ucount = ulist->size();
-       for (unsigned int i = 0; i < ucount; i++)
+       std::map<char*,char*> *ulist = c->GetUsers();
+       for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
        {
-               char* o = (*ulist)[i];
+               char* o = i->second;
                userrec* otheruser = (userrec*)o;
                if (otheruser->fd < 0)
                {
@@ -2358,6 +2429,7 @@ void ReadConfiguration(bool rebind)
                L.RecvPass = Conf->ReadValue("link","recvpass",j);
                L.AutoConnect = Conf->ReadInteger("link","autoconnect",j,true);
                L.EncryptionKey =  Conf->ReadValue("link","encryptionkey",j);
+               L.HiddenFromStats = Conf->ReadFlag("link","hidden",j);
                L.NextConnectTime = time(NULL) + L.AutoConnect;
                /* Bugfix by brain, do not allow people to enter bad configurations */
                if ((L.RecvPass != "") && (L.SendPass != "") && (L.Name != "") && (L.Port))
@@ -2571,7 +2643,7 @@ class ModuleSpanningTree : public Module
                                log(DEBUG,"Splitting server %s",s->GetName().c_str());
                                WriteOpers("*** SQUIT: Server \002%s\002 removed from network by %s",parameters[0],user->nick);
                                sock->Squit(s,"Server quit by "+std::string(user->nick)+"!"+std::string(user->ident)+"@"+std::string(user->host));
-                               sock->Close();
+                               Srv->RemoveSocket(sock);
                        }
                        else
                        {
@@ -2615,22 +2687,23 @@ class ModuleSpanningTree : public Module
                        TreeSocket* sock = serv->GetSocket();
                        if (sock)
                        {
-                               if (curtime >= serv->NextPingTime())
-                               {
-                                       if (serv->AnsweredLastPing())
-                                       {
-                                               sock->WriteLine(":"+Srv->GetServerName()+" PING "+serv->GetName());
-                                               serv->SetNextPingTime(curtime + 60);
-                                       }
-                                       else
-                                       {
-                                               // they didnt answer, boot them
-                                               WriteOpers("*** Server \002%s\002 pinged out",serv->GetName().c_str());
-                                               sock->Squit(serv,"Ping timeout");
-                                               sock->Close();
-                                               return;
-                                       }
-                               }
+                                if (curtime >= serv->NextPingTime())
+                                {               
+                                        if (serv->AnsweredLastPing())
+                                        {               
+                                                sock->WriteLine(":"+Srv->GetServerName()+" PING "+serv->GetName());
+                                                serv->SetNextPingTime(curtime + 120);
+                                        }                       
+                                        else            
+                                        {       
+                                                // they didnt answer, boot them
+                                                WriteOpers("*** Server \002%s\002 pinged out",serv->GetName().c_str());
+                                                sock->Squit(serv,"Ping timeout");
+                                               Srv->RemoveSocket(sock);
+                                                return;
+                                        }
+                                }
+
                        }
                }
        }
@@ -2699,7 +2772,7 @@ class ModuleSpanningTree : public Module
                                TreeServer* CheckDupe = FindServer(x->Name);
                                if (!CheckDupe)
                                {
-                                       WriteServ(user->fd,"NOTICE %s :*** CONNECT: Connecting to server: \002%s\002 (%s:%d)",user->nick,x->Name.c_str(),x->IPAddr.c_str(),x->Port);
+                                       WriteServ(user->fd,"NOTICE %s :*** CONNECT: Connecting to server: \002%s\002 (%s:%d)",user->nick,x->Name.c_str(),(x->HiddenFromStats ? "<hidden>" : x->IPAddr.c_str()),x->Port);
                                        TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name);
                                        Srv->AddSocket(newsocket);
                                        return 1;
@@ -2721,11 +2794,11 @@ class ModuleSpanningTree : public Module
                {
                        for (unsigned int i = 0; i < LinkBlocks.size(); i++)
                        {
-                               WriteServ(user->fd,"213 %s C *@%s * %s %d 0 %s",user->nick,LinkBlocks[i].IPAddr.c_str(),LinkBlocks[i].Name.c_str(),LinkBlocks[i].Port,(LinkBlocks[i].EncryptionKey != "" ? "es" : " s"));
+                               WriteServ(user->fd,"213 %s C *@%s * %s %d 0 %c%c%c",user->nick,(LinkBlocks[i].HiddenFromStats ? "<hidden>" : LinkBlocks[i].IPAddr).c_str(),LinkBlocks[i].Name.c_str(),LinkBlocks[i].Port,(LinkBlocks[i].EncryptionKey != "" ? 'e' : '-'),(LinkBlocks[i].AutoConnect ? 'a' : '-'),'s');
                                WriteServ(user->fd,"244 %s H * * %s",user->nick,LinkBlocks[i].Name.c_str());
                        }
-                       WriteServ(user->fd,"219 %s %s :End of /STATS report",user->nick,parameters[0]);
-                       WriteOpers("*** Notice: Stats '%s' requested by %s (%s@%s)",parameters[0],user->nick,user->ident,user->host);
+                       WriteServ(user->fd,"219 %s %c :End of /STATS report",user->nick,statschar);
+                       WriteOpers("*** Notice: Stats '%c' requested by %s (%s@%s)",statschar,user->nick,user->ident,user->host);
                        return 1;
                }
                return 0;
@@ -2961,12 +3034,14 @@ class ModuleSpanningTree : public Module
                DoOneToMany(user->nick,"FNAME",params);
        }
 
-       virtual void OnUserPart(userrec* user, chanrec* channel)
+       virtual void OnUserPart(userrec* user, chanrec* channel, std::string partmessage)
        {
                if (user->fd > -1)
                {
                        std::deque<std::string> params;
                        params.push_back(channel->name);
+                       if (partmessage != "")
+                               params.push_back(":"+partmessage);
                        DoOneToMany(user->nick,"PART",params);
                }
        }
@@ -2984,7 +3059,7 @@ class ModuleSpanningTree : public Module
                        params.push_back(user->dhost);
                        params.push_back(user->ident);
                        params.push_back("+"+std::string(user->modes));
-                       params.push_back(user->ip);
+                       params.push_back((char*)inet_ntoa(user->ip4));
                        params.push_back(":"+std::string(user->fullname));
                        DoOneToMany(Srv->GetServerName(),"NICK",params);
 
@@ -3025,7 +3100,7 @@ class ModuleSpanningTree : public Module
 
        virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, std::string reason)
        {
-               if (source->fd > -1)
+                if ((source) && (source->fd > -1))
                {
                        std::deque<std::string> params;
                        params.push_back(chan->name);
@@ -3190,7 +3265,11 @@ class ModuleSpanningTree : public Module
                                userrec* u = (userrec*)target;
                                s->WriteLine(":"+Srv->GetServerName()+" METADATA "+u->nick+" "+extname+" :"+extdata);
                        }
-                       else
+                       else if (target_type == TYPE_OTHER)
+                       {
+                               s->WriteLine(":"+Srv->GetServerName()+" METADATA * "+extname+" :"+extdata);
+                       }
+                       else if (target_type == TYPE_CHANNEL)
                        {
                                chanrec* c = (chanrec*)target;
                                s->WriteLine(":"+Srv->GetServerName()+" METADATA "+c->name+" "+extname+" :"+extdata);
@@ -3198,6 +3277,18 @@ class ModuleSpanningTree : public Module
                }
        }
 
+       virtual void OnEvent(Event* event)
+       {
+               if (event->GetEventID() == "send_metadata")
+               {
+                       std::deque<std::string>* params = (std::deque<std::string>*)event->GetData();
+                       if (params->size() < 3)
+                               return;
+                       (*params)[2] = ":" + (*params)[2];
+                       DoOneToMany(Srv->GetServerName(),"METADATA",*params);
+               }
+       }
+
        virtual ~ModuleSpanningTree()
        {
        }
@@ -3215,7 +3306,19 @@ class ModuleSpanningTree : public Module
                List[I_OnUserQuit] = List[I_OnUserPostNick] = List[I_OnUserKick] = List[I_OnRemoteKill] = List[I_OnRehash] = 1;
                List[I_OnOper] = List[I_OnAddGLine] = List[I_OnAddZLine] = List[I_OnAddQLine] = List[I_OnAddELine] = 1;
                List[I_OnDelGLine] = List[I_OnDelZLine] = List[I_OnDelQLine] = List[I_OnDelELine] = List[I_ProtoSendMode] = List[I_OnMode] = 1;
-               List[I_OnStats] = List[I_ProtoSendMetaData] = 1;
+               List[I_OnStats] = List[I_ProtoSendMetaData] = List[I_OnEvent] = 1;
+       }
+
+       /* It is IMPORTANT that m_spanningtree is the last module in the chain
+        * so that any activity it sees is FINAL, e.g. we arent going to send out
+        * a NICK message before m_cloaking has finished putting the +x on the user,
+        * etc etc.
+        * Therefore, we return PRIORITY_LAST to make sure we end up at the END of
+        * the module call queue.
+        */
+       Priority Prioritize()
+       {
+               return PRIORITY_LAST;
        }
 };