]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree.cpp
Changed to stop first ping being bad automatically
[user/henk/code/inspircd.git] / src / modules / m_spanningtree.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2005 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include <vector>
21 #include <deque>
22 #include "globals.h"
23 #include "inspircd_config.h"
24 #ifdef GCC3
25 #include <ext/hash_map>
26 #else
27 #include <hash_map>
28 #endif
29 #include "users.h"
30 #include "channels.h"
31 #include "modules.h"
32 #include "socket.h"
33 #include "helperfuncs.h"
34 #include "inspircd.h"
35 #include "inspstring.h"
36 #include "hashcomp.h"
37 #include "message.h"
38
39 #ifdef GCC3
40 #define nspace __gnu_cxx
41 #else
42 #define nspace std
43 #endif
44
45 class ModuleSpanningTree;
46 static ModuleSpanningTree* TreeProtocolModule;
47
48 extern std::vector<Module*> modules;
49 extern std::vector<ircd_module*> factory;
50 extern int MODCOUNT;
51
52 enum ServerState { LISTENER, CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED };
53
54 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, irc::StrHashComp> user_hash;
55 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, irc::StrHashComp> chan_hash;
56
57 extern user_hash clientlist;
58 extern chan_hash chanlist;
59
60 class TreeServer;
61 class TreeSocket;
62
63 bool DoOneToOne(std::string prefix, std::string command, std::deque<std::string> params, std::string target);
64 bool DoOneToAllButSender(std::string prefix, std::string command, std::deque<std::string> params, std::string omit);
65 bool DoOneToMany(std::string prefix, std::string command, std::deque<std::string> params);
66 bool DoOneToAllButSenderRaw(std::string data,std::string omit, std::string prefix,std::string command,std::deque<std::string> params);
67 void ReadConfiguration(bool rebind);
68
69 class TreeServer
70 {
71         TreeServer* Parent;
72         std::vector<TreeServer*> Children;
73         std::string ServerName;
74         std::string ServerDesc;
75         std::string VersionString;
76         int UserCount;
77         int OperCount;
78         TreeSocket* Socket;     // for directly connected servers this points at the socket object
79         time_t NextPing;
80         bool LastPingWasGood;
81         
82  public:
83
84         TreeServer()
85         {
86                 Parent = NULL;
87                 ServerName = "";
88                 ServerDesc = "";
89                 VersionString = "";
90                 UserCount = OperCount = 0;
91         }
92
93         TreeServer(std::string Name, std::string Desc) : ServerName(Name), ServerDesc(Desc)
94         {
95                 Parent = NULL;
96                 VersionString = "";
97                 UserCount = OperCount = 0;
98         }
99
100         TreeServer(std::string Name, std::string Desc, TreeServer* Above, TreeSocket* Sock) : Parent(Above), ServerName(Name), ServerDesc(Desc), Socket(Sock)
101         {
102                 VersionString = "";
103                 UserCount = OperCount = 0;
104                 this->SetNextPingTime(time(NULL) + 60);
105                 this->SetPingFlag();
106         }
107
108         std::string GetName()
109         {
110                 return this->ServerName;
111         }
112
113         std::string GetDesc()
114         {
115                 return this->ServerDesc;
116         }
117
118         std::string GetVersion()
119         {
120                 return this->VersionString;
121         }
122
123         void SetNextPingTime(time_t t)
124         {
125                 this->NextPing = t;
126                 LastPingWasGood = false;
127         }
128
129         time_t NextPingTime()
130         {
131                 return this->NextPing;
132         }
133
134         bool AnsweredLastPing()
135         {
136                 return LastPingWasGood;
137         }
138
139         void SetPingFlag()
140         {
141                 LastPingWasGood = true;
142         }
143
144         int GetUserCount()
145         {
146                 return this->UserCount;
147         }
148
149         int GetOperCount()
150         {
151                 return this->OperCount;
152         }
153
154         TreeSocket* GetSocket()
155         {
156                 return this->Socket;
157         }
158
159         TreeServer* GetParent()
160         {
161                 return this->Parent;
162         }
163
164         unsigned int ChildCount()
165         {
166                 return Children.size();
167         }
168
169         TreeServer* GetChild(unsigned int n)
170         {
171                 if (n < Children.size())
172                 {
173                         return Children[n];
174                 }
175                 else
176                 {
177                         return NULL;
178                 }
179         }
180
181         void AddChild(TreeServer* Child)
182         {
183                 Children.push_back(Child);
184         }
185
186         bool DelChild(TreeServer* Child)
187         {
188                 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
189                 {
190                         if (*a == Child)
191                         {
192                                 Children.erase(a);
193                                 return true;
194                         }
195                 }
196                 return false;
197         }
198
199         // removes child nodes of this node, and of that node, etc etc
200         bool Tidy()
201         {
202                 bool stillchildren = true;
203                 while (stillchildren)
204                 {
205                         stillchildren = false;
206                         for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
207                         {
208                                 TreeServer* s = (TreeServer*)*a;
209                                 s->Tidy();
210                                 Children.erase(a);
211                                 delete s;
212                                 stillchildren = true;
213                                 break;
214                         }
215                 }
216                 return true;
217         }
218 };
219
220 class Link
221 {
222  public:
223          std::string Name;
224          std::string IPAddr;
225          int Port;
226          std::string SendPass;
227          std::string RecvPass;
228          unsigned long AutoConnect;
229          time_t NextConnectTime;
230 };
231
232 /* $ModDesc: Povides a spanning tree server link protocol */
233
234 Server *Srv;
235 ConfigReader *Conf;
236 TreeServer *TreeRoot;
237 std::vector<Link> LinkBlocks;
238
239 TreeServer* RouteEnumerate(TreeServer* Current, std::string ServerName)
240 {
241         if (Current->GetName() == ServerName)
242                 return Current;
243         for (unsigned int q = 0; q < Current->ChildCount(); q++)
244         {
245                 TreeServer* found = RouteEnumerate(Current->GetChild(q),ServerName);
246                 if (found)
247                 {
248                         return found;
249                 }
250         }
251         return NULL;
252 }
253
254 // Returns the locally connected server we must route a
255 // message through to reach server 'ServerName'. This
256 // only applies to one-to-one and not one-to-many routing.
257 TreeServer* BestRouteTo(std::string ServerName)
258 {
259         if (ServerName.c_str() == TreeRoot->GetName())
260         {
261                 return NULL;
262         }
263         // first, find the server by recursively walking the tree
264         TreeServer* Found = RouteEnumerate(TreeRoot,ServerName);
265         // did we find it? If not, they did something wrong, abort.
266         if (!Found)
267         {
268                 return NULL;
269         }
270         else
271         {
272                 // The server exists, follow its parent nodes until
273                 // the parent of the current is 'TreeRoot', we know
274                 // then that this is a directly-connected server.
275                 while ((Found) && (Found->GetParent() != TreeRoot))
276                 {
277                         Found = Found->GetParent();
278                 }
279                 return Found;
280         }
281 }
282
283 bool LookForServer(TreeServer* Current, std::string ServerName)
284 {
285         if (ServerName == Current->GetName())
286                 return true;
287         for (unsigned int q = 0; q < Current->ChildCount(); q++)
288         {
289                 if (LookForServer(Current->GetChild(q),ServerName))
290                         return true;
291         }
292         return false;
293 }
294
295 TreeServer* Found;
296
297 void RFindServer(TreeServer* Current, std::string ServerName)
298 {
299         if ((ServerName == Current->GetName()) && (!Found))
300         {
301                 Found = Current;
302                 return;
303         }
304         if (!Found)
305         {
306                 for (unsigned int q = 0; q < Current->ChildCount(); q++)
307                 {
308                         if (!Found)
309                                 RFindServer(Current->GetChild(q),ServerName);
310                 }
311         }
312         return;
313 }
314
315 TreeServer* FindServer(std::string ServerName)
316 {
317         Found = NULL;
318         RFindServer(TreeRoot,ServerName);
319         return Found;
320 }
321
322 bool IsServer(std::string ServerName)
323 {
324         return LookForServer(TreeRoot,ServerName);
325 }
326
327 class TreeSocket : public InspSocket
328 {
329         std::string myhost;
330         std::string in_buffer;
331         ServerState LinkState;
332         std::string InboundServerName;
333         std::string InboundDescription;
334         int num_lost_users;
335         int num_lost_servers;
336         time_t NextPing;
337         bool LastPingWasGood;
338         
339  public:
340
341         TreeSocket(std::string host, int port, bool listening, unsigned long maxtime)
342                 : InspSocket(host, port, listening, maxtime)
343         {
344                 myhost = host;
345                 this->LinkState = LISTENER;
346         }
347
348         TreeSocket(std::string host, int port, bool listening, unsigned long maxtime, std::string ServerName)
349                 : InspSocket(host, port, listening, maxtime)
350         {
351                 myhost = ServerName;
352                 this->LinkState = CONNECTING;
353         }
354
355         TreeSocket(int newfd, char* ip)
356                 : InspSocket(newfd, ip)
357         {
358                 this->LinkState = WAIT_AUTH_1;
359         }
360         
361         virtual bool OnConnected()
362         {
363                 if (this->LinkState == CONNECTING)
364                 {
365                         Srv->SendOpers("*** Connection to "+myhost+"["+this->GetIP()+"] established.");
366                         // we should send our details here.
367                         // if the other side is satisfied, they send theirs.
368                         // we do not need to change state here.
369                         for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
370                         {
371                                 if (x->Name == this->myhost)
372                                 {
373                                         // found who we're supposed to be connecting to, send the neccessary gubbins.
374                                         this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
375                                         return true;
376                                 }
377                         }
378                 }
379                 return true;
380         }
381         
382         virtual void OnError(InspSocketError e)
383         {
384         }
385
386         virtual int OnDisconnect()
387         {
388                 return true;
389         }
390
391         // recursively send the server tree with distances as hops
392         void SendServers(TreeServer* Current, TreeServer* s, int hops)
393         {
394                 char command[1024];
395                 for (unsigned int q = 0; q < Current->ChildCount(); q++)
396                 {
397                         TreeServer* recursive_server = Current->GetChild(q);
398                         if (recursive_server != s)
399                         {
400                                 // :source.server SERVER server.name hops :Description
401                                 snprintf(command,1024,":%s SERVER %s * %d :%s",Current->GetName().c_str(),recursive_server->GetName().c_str(),hops,recursive_server->GetDesc().c_str());
402                                 this->WriteLine(command);
403                                 // down to next level
404                                 this->SendServers(recursive_server, s, hops+1);
405                         }
406                 }
407         }
408
409         void SquitServer(TreeServer* Current)
410         {
411                 // recursively squit the servers attached to 'Current'
412                 for (unsigned int q = 0; q < Current->ChildCount(); q++)
413                 {
414                         TreeServer* recursive_server = Current->GetChild(q);
415                         this->SquitServer(recursive_server);
416                 }
417                 // Now we've whacked the kids, whack self
418                 num_lost_servers++;
419                 bool quittingpeople = true;
420                 while (quittingpeople)
421                 {
422                         quittingpeople = false;
423                         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
424                         {
425                                 if (!strcasecmp(u->second->server,Current->GetName().c_str()))
426                                 {
427                                         Srv->QuitUser(u->second,Current->GetName()+" "+std::string(Srv->GetServerName()));
428                                         num_lost_users++;
429                                         quittingpeople = true;
430                                         break;
431                                 }
432                         }
433                 }
434         }
435
436         void Squit(TreeServer* Current,std::string reason)
437         {
438                 if (Current)
439                 {
440                         std::deque<std::string> params;
441                         params.push_back(Current->GetName());
442                         params.push_back(":"+reason);
443                         DoOneToAllButSender(Current->GetParent()->GetName(),"SQUIT",params,Current->GetName());
444                         if (Current->GetParent() == TreeRoot)
445                         {
446                                 Srv->SendOpers("Server \002"+Current->GetName()+"\002 split: "+reason);
447                         }
448                         else
449                         {
450                                 Srv->SendOpers("Server \002"+Current->GetName()+"\002 split from server \002"+Current->GetParent()->GetName()+"\002 with reason: "+reason);
451                         }
452                         num_lost_servers = 0;
453                         num_lost_users = 0;
454                         SquitServer(Current);
455                         Current->Tidy();
456                         Current->GetParent()->DelChild(Current);
457                         delete Current;
458                         WriteOpers("Netsplit complete, lost \002%d\002 users on \002%d\002 servers.", num_lost_users, num_lost_servers);
459                 }
460                 else
461                 {
462                         log(DEFAULT,"Squit from unknown server");
463                 }
464         }
465
466         bool ForceMode(std::string source, std::deque<std::string> params)
467         {
468                 userrec* who = new userrec;
469                 who->fd = FD_MAGIC_NUMBER;
470                 if (params.size() < 2)
471                         return true;
472                 char* modelist[255];
473                 for (unsigned int q = 0; q < params.size(); q++)
474                 {
475                         modelist[q] = (char*)params[q].c_str();
476                 }
477                 Srv->SendMode(modelist,params.size(),who);
478                 DoOneToAllButSender(source,"FMODE",params,source);
479                 delete who;
480                 return true;
481         }
482
483         bool ForceTopic(std::string source, std::deque<std::string> params)
484         {
485                 // FTOPIC %s %lu %s :%s
486                 if (params.size() != 4)
487                         return true;
488                 std::string channel = params[0];
489                 time_t ts = atoi(params[1].c_str());
490                 std::string setby = params[2];
491                 std::string topic = params[3];
492
493                 chanrec* c = Srv->FindChannel(channel);
494                 if (c)
495                 {
496                         if ((ts >= c->topicset) || (!*c->topic))
497                         {
498                                 strlcpy(c->topic,topic.c_str(),MAXTOPIC);
499                                 strlcpy(c->setby,setby.c_str(),NICKMAX);
500                                 c->topicset = ts;
501                                 WriteChannelWithServ((char*)source.c_str(), c, "TOPIC %s :%s", c->name, c->topic);
502                         }
503                         
504                 }
505                 
506                 // all done, send it on its way
507                 params[3] = ":" + params[3];
508                 DoOneToAllButSender(source,"FTOPIC",params,source);
509
510                 return true;
511         }
512
513         bool ForceJoin(std::string source, std::deque<std::string> params)
514         {
515                 if (params.size() < 3)
516                         return true;
517
518                 char first[MAXBUF];
519                 char modestring[MAXBUF];
520                 char* mode_users[127];
521                 mode_users[0] = first;
522                 mode_users[1] = modestring;
523                 strcpy(mode_users[1],"+");
524                 unsigned int modectr = 2;
525                 
526                 userrec* who = NULL;
527                 std::string channel = params[0];
528                 time_t TS = atoi(params[1].c_str());
529                 char* key = "";
530                 
531                 chanrec* chan = Srv->FindChannel(channel);
532                 if (chan)
533                 {
534                         key = chan->key;
535                 }
536                 strlcpy(mode_users[0],channel.c_str(),MAXBUF);
537
538                 // default is a high value, which if we dont have this
539                 // channel will let the other side apply their modes.
540                 time_t ourTS = time(NULL)+20;
541                 chanrec* us = Srv->FindChannel(channel);
542                 if (us)
543                 {
544                         ourTS = us->age;
545                 }
546
547                 log(DEBUG,"FJOIN detected, our TS=%lu, their TS=%lu",ourTS,TS);
548
549                 // do this first, so our mode reversals are correctly received by other servers
550                 // if there is a TS collision.
551                 DoOneToAllButSender(source,"FJOIN",params,source);
552                 
553                 for (unsigned int usernum = 2; usernum < params.size(); usernum++)
554                 {
555                         // process one channel at a time, applying modes.
556                         char* usr = (char*)params[usernum].c_str();
557                         char permissions = *usr;
558                         switch (permissions)
559                         {
560                                 case '@':
561                                         usr++;
562                                         mode_users[modectr++] = usr;
563                                         strlcat(modestring,"o",MAXBUF);
564                                 break;
565                                 case '%':
566                                         usr++;
567                                         mode_users[modectr++] = usr;
568                                         strlcat(modestring,"h",MAXBUF);
569                                 break;
570                                 case '+':
571                                         usr++;
572                                         mode_users[modectr++] = usr;
573                                         strlcat(modestring,"v",MAXBUF);
574                                 break;
575                         }
576                         who = Srv->FindNick(usr);
577                         if (who)
578                         {
579                                 Srv->JoinUserToChannel(who,channel,key);
580                                 if (modectr >= (MAXMODES-1))
581                                 {
582                                         // theres a mode for this user. push them onto the mode queue, and flush it
583                                         // if there are more than MAXMODES to go.
584                                         if (ourTS >= TS)
585                                         {
586                                                 log(DEBUG,"Our our channel newer than theirs, accepting their modes");
587                                                 Srv->SendMode(mode_users,modectr,who);
588                                         }
589                                         else
590                                         {
591                                                 log(DEBUG,"Their channel newer than ours, bouncing their modes");
592                                                 // bouncy bouncy!
593                                                 std::deque<std::string> params;
594                                                 // modes are now being UNSET...
595                                                 *mode_users[1] = '-';
596                                                 for (unsigned int x = 0; x < modectr; x++)
597                                                 {
598                                                         params.push_back(mode_users[x]);
599                                                 }
600                                                 // tell everyone to bounce the modes. bad modes, bad!
601                                                 DoOneToMany(Srv->GetServerName(),"FMODE",params);
602                                         }
603                                         strcpy(mode_users[1],"+");
604                                         modectr = 2;
605                                 }
606                         }
607                 }
608                 // there werent enough modes built up to flush it during FJOIN,
609                 // or, there are a number left over. flush them out.
610                 if ((modectr > 2) && (who))
611                 {
612                         if (ourTS >= TS)
613                         {
614                                 log(DEBUG,"Our our channel newer than theirs, accepting their modes");
615                                 Srv->SendMode(mode_users,modectr,who);
616                         }
617                         else
618                         {
619                                 log(DEBUG,"Their channel newer than ours, bouncing their modes");
620                                 std::deque<std::string> params;
621                                 *mode_users[1] = '-';
622                                 for (unsigned int x = 0; x < modectr; x++)
623                                 {
624                                         params.push_back(mode_users[x]);
625                                 }
626                                 DoOneToMany(Srv->GetServerName(),"FMODE",params);
627                         }
628                 }
629                 return true;
630         }
631
632         bool IntroduceClient(std::string source, std::deque<std::string> params)
633         {
634                 if (params.size() < 8)
635                         return true;
636                 // NICK age nick host dhost ident +modes ip :gecos
637                 //       0   1    2    3      4     5    6   7
638                 std::string nick = params[1];
639                 std::string host = params[2];
640                 std::string dhost = params[3];
641                 std::string ident = params[4];
642                 time_t age = atoi(params[0].c_str());
643                 std::string modes = params[5];
644                 while (*(modes.c_str()) == '+')
645                 {
646                         char* m = (char*)modes.c_str();
647                         m++;
648                         modes = m;
649                 }
650                 std::string ip = params[6];
651                 std::string gecos = params[7];
652                 char* tempnick = (char*)nick.c_str();
653                 log(DEBUG,"Introduce client %s!%s@%s",tempnick,ident.c_str(),host.c_str());
654                 
655                 user_hash::iterator iter;
656                 iter = clientlist.find(tempnick);
657                 if (iter != clientlist.end())
658                 {
659                         // nick collision
660                         log(DEBUG,"Nick collision on %s!%s@%s: %lu %lu",tempnick,ident.c_str(),host.c_str(),(unsigned long)age,(unsigned long)iter->second->age);
661                         this->WriteLine(":"+Srv->GetServerName()+" KILL "+tempnick+" :Nickname collision");
662                         return true;
663                 }
664
665                 clientlist[tempnick] = new userrec();
666                 clientlist[tempnick]->fd = FD_MAGIC_NUMBER;
667                 strlcpy(clientlist[tempnick]->nick, tempnick,NICKMAX);
668                 strlcpy(clientlist[tempnick]->host, host.c_str(),160);
669                 strlcpy(clientlist[tempnick]->dhost, dhost.c_str(),160);
670                 clientlist[tempnick]->server = (char*)FindServerNamePtr(source.c_str());
671                 strlcpy(clientlist[tempnick]->ident, ident.c_str(),IDENTMAX);
672                 strlcpy(clientlist[tempnick]->fullname, gecos.c_str(),MAXGECOS);
673                 clientlist[tempnick]->registered = 7;
674                 clientlist[tempnick]->signon = age;
675                 strlcpy(clientlist[tempnick]->modes, modes.c_str(),53);
676                 strlcpy(clientlist[tempnick]->ip,ip.c_str(),16);
677                 for (int i = 0; i < MAXCHANS; i++)
678                 {
679                         clientlist[tempnick]->chans[i].channel = NULL;
680                         clientlist[tempnick]->chans[i].uc_modes = 0;
681                 }
682                 params[7] = ":" + params[7];
683                 DoOneToAllButSender(source,"NICK",params,source);
684                 return true;
685         }
686
687         void SendFJoins(TreeServer* Current, chanrec* c)
688         {
689                 char list[MAXBUF];
690                 snprintf(list,MAXBUF,":%s FJOIN %s %lu",Srv->GetServerName().c_str(),c->name,(unsigned long)c->age);
691                 std::vector<char*> *ulist = c->GetUsers();
692                 for (unsigned int i = 0; i < ulist->size(); i++)
693                 {
694                         char* o = (*ulist)[i];
695                         userrec* otheruser = (userrec*)o;
696                         strlcat(list," ",MAXBUF);
697                         strlcat(list,cmode(otheruser,c),MAXBUF);
698                         strlcat(list,otheruser->nick,MAXBUF);
699                         if (strlen(list)>(480-NICKMAX))
700                         {
701                                 this->WriteLine(list);
702                                 snprintf(list,MAXBUF,":%s FJOIN %s %lu",Srv->GetServerName().c_str(),c->name,(unsigned long)c->age);
703                         }
704                 }
705                 if (list[strlen(list)-1] != ':')
706                 {
707                         this->WriteLine(list);
708                 }
709         }
710
711         void SendChannelModes(TreeServer* Current)
712         {
713                 char data[MAXBUF];
714                 for (chan_hash::iterator c = chanlist.begin(); c != chanlist.end(); c++)
715                 {
716                         SendFJoins(Current, c->second);
717                         snprintf(data,MAXBUF,":%s FMODE %s +%s",Srv->GetServerName().c_str(),c->second->name,chanmodes(c->second));
718                         this->WriteLine(data);
719                         if (*c->second->topic)
720                         {
721                                 snprintf(data,MAXBUF,":%s FTOPIC %s %lu %s :%s",Srv->GetServerName().c_str(),c->second->name,(unsigned long)c->second->topicset,c->second->setby,c->second->topic);
722                                 this->WriteLine(data);
723                         }
724                         for (BanList::iterator b = c->second->bans.begin(); b != c->second->bans.end(); b++)
725                         {
726                                 snprintf(data,MAXBUF,":%s FMODE %s +b %s",Srv->GetServerName().c_str(),c->second->name,b->data);
727                                 this->WriteLine(data);
728                         }
729                         FOREACH_MOD OnSyncChannel(c->second,(Module*)TreeProtocolModule,(void*)this);
730                 }
731         }
732
733         // send all users and their channels
734         void SendUsers(TreeServer* Current)
735         {
736                 char data[MAXBUF];
737                 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
738                 {
739                         if (u->second->registered == 7)
740                         {
741                                 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);
742                                 this->WriteLine(data);
743                                 if (strchr(u->second->modes,'o'))
744                                 {
745                                         this->WriteLine(":"+std::string(u->second->nick)+" OPERTYPE "+std::string(u->second->oper));
746                                 }
747                                 //char* chl = chlist(u->second,u->second);
748                                 //if (*chl)
749                                 //{
750                                 //      this->WriteLine(":"+std::string(u->second->nick)+" FJOIN "+std::string(chl));
751                                 //}
752                                 FOREACH_MOD OnSyncUser(u->second,(Module*)TreeProtocolModule,(void*)this);
753                         }
754                 }
755         }
756
757         void DoBurst(TreeServer* s)
758         {
759                 Srv->SendOpers("*** Bursting to "+s->GetName()+".");
760                 this->WriteLine("BURST");
761                 // Send server tree
762                 this->SendServers(TreeRoot,s,1);
763                 // Send users and their channels
764                 this->SendUsers(s);
765                 // Send everything else (channel modes etc)
766                 this->SendChannelModes(s);
767                 this->WriteLine("ENDBURST");
768         }
769
770         virtual bool OnDataReady()
771         {
772                 char* data = this->Read();
773                 if (data)
774                 {
775                         this->in_buffer += data;
776                         while (in_buffer.find("\n") != std::string::npos)
777                         {
778                                 char* line = (char*)in_buffer.c_str();
779                                 std::string ret = "";
780                                 while ((*line != '\n') && (strlen(line)))
781                                 {
782                                         ret = ret + *line;
783                                         line++;
784                                 }
785                                 if ((*line == '\n') || (*line == '\r'))
786                                         line++;
787                                 in_buffer = line;
788                                 if (!this->ProcessLine(ret))
789                                 {
790                                         return false;
791                                 }
792                         }
793                 }
794                 return (data != NULL);
795         }
796
797         int WriteLine(std::string line)
798         {
799                 return this->Write(line + "\r\n");
800         }
801
802         bool Error(std::deque<std::string> params)
803         {
804                 if (params.size() < 1)
805                         return false;
806                 std::string Errmsg = params[0];
807                 std::string SName = myhost;
808                 if (InboundServerName != "")
809                 {
810                         SName = InboundServerName;
811                 }
812                 Srv->SendOpers("*** ERROR from "+SName+": "+Errmsg);
813                 // we will return false to cause the socket to close.
814                 return false;
815         }
816
817         bool OperType(std::string prefix, std::deque<std::string> params)
818         {
819                 if (params.size() != 1)
820                         return true;
821                 std::string opertype = params[0];
822                 userrec* u = Srv->FindNick(prefix);
823                 if (u)
824                 {
825                         strlcpy(u->oper,opertype.c_str(),NICKMAX);
826                         if (!strchr(u->modes,'o'))
827                         {
828                                 strcat(u->modes,"o");
829                         }
830                         DoOneToAllButSender(u->server,"OPERTYPE",params,u->server);
831                 }
832                 return true;
833         }
834
835         bool RemoteRehash(std::string prefix, std::deque<std::string> params)
836         {
837                 if (params.size() < 1)
838                         return true;
839                 std::string servermask = params[0];
840                 if (Srv->MatchText(Srv->GetServerName(),servermask))
841                 {
842                         Srv->SendOpers("*** Remote rehash initiated from server \002"+prefix+"\002.");
843                         Srv->RehashServer();
844                         ReadConfiguration(false);
845                 }
846                 DoOneToAllButSender(prefix,"REHASH",params,prefix);
847                 return true;
848         }
849
850         bool RemoteKill(std::string prefix, std::deque<std::string> params)
851         {
852                 if (params.size() != 2)
853                         return true;
854                 std::string nick = params[0];
855                 std::string reason = params[1];
856                 userrec* u = Srv->FindNick(prefix);
857                 userrec* who = Srv->FindNick(nick);
858                 if (who)
859                 {
860                         std::string sourceserv = prefix;
861                         if (u)
862                         {
863                                 sourceserv = u->server;
864                         }
865                         params[1] = ":" + params[1];
866                         DoOneToAllButSender(prefix,"KILL",params,sourceserv);
867                         Srv->QuitUser(who,reason);
868                 }
869                 return true;
870         }
871
872         bool LocalPong(std::string prefix, std::deque<std::string> params)
873         {
874                 if (params.size() < 1)
875                         return true;
876                 TreeServer* ServerSource = FindServer(prefix);
877                 if (ServerSource)
878                 {
879                         ServerSource->SetPingFlag();
880                 }
881                 return true;
882         }
883         
884         bool LocalPing(std::string prefix, std::deque<std::string> params)
885         {
886                 if (params.size() < 1)
887                         return true;
888                 std::string stufftobounce = params[0];
889                 this->WriteLine(":"+Srv->GetServerName()+" PONG "+stufftobounce);
890                 return true;
891         }
892
893         bool RemoteServer(std::string prefix, std::deque<std::string> params)
894         {
895                 if (params.size() < 4)
896                         return false;
897                 std::string servername = params[0];
898                 std::string password = params[1];
899                 // hopcount is not used for a remote server, we calculate this ourselves
900                 std::string description = params[3];
901                 TreeServer* ParentOfThis = FindServer(prefix);
902                 if (!ParentOfThis)
903                 {
904                         this->WriteLine("ERROR :Protocol error - Introduced remote server from unknown server "+prefix);
905                         return false;
906                 }
907                 TreeServer* CheckDupe = FindServer(servername);
908                 if (CheckDupe)
909                 {
910                         this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
911                         return false;
912                 }
913                 TreeServer* Node = new TreeServer(servername,description,ParentOfThis,NULL);
914                 ParentOfThis->AddChild(Node);
915                 params[3] = ":" + params[3];
916                 DoOneToAllButSender(prefix,"SERVER",params,prefix);
917                 Srv->SendOpers("*** Server \002"+prefix+"\002 introduced server \002"+servername+"\002 ("+description+")");
918                 return true;
919         }
920
921         bool Outbound_Reply_Server(std::deque<std::string> params)
922         {
923                 if (params.size() < 4)
924                         return false;
925                 std::string servername = params[0];
926                 std::string password = params[1];
927                 int hops = atoi(params[2].c_str());
928                 if (hops)
929                 {
930                         this->WriteLine("ERROR :Server too far away for authentication");
931                         return false;
932                 }
933                 std::string description = params[3];
934                 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
935                 {
936                         if ((x->Name == servername) && (x->RecvPass == password))
937                         {
938                                 TreeServer* CheckDupe = FindServer(servername);
939                                 if (CheckDupe)
940                                 {
941                                         this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
942                                         return false;
943                                 }
944                                 // Begin the sync here. this kickstarts the
945                                 // other side, waiting in WAIT_AUTH_2 state,
946                                 // into starting their burst, as it shows
947                                 // that we're happy.
948                                 this->LinkState = CONNECTED;
949                                 // we should add the details of this server now
950                                 // to the servers tree, as a child of the root
951                                 // node.
952                                 TreeServer* Node = new TreeServer(servername,description,TreeRoot,this);
953                                 TreeRoot->AddChild(Node);
954                                 params[3] = ":" + params[3];
955                                 DoOneToAllButSender(TreeRoot->GetName(),"SERVER",params,servername);
956                                 this->DoBurst(Node);
957                                 return true;
958                         }
959                 }
960                 this->WriteLine("ERROR :Invalid credentials");
961                 return false;
962         }
963
964         bool Inbound_Server(std::deque<std::string> params)
965         {
966                 if (params.size() < 4)
967                         return false;
968                 std::string servername = params[0];
969                 std::string password = params[1];
970                 int hops = atoi(params[2].c_str());
971                 if (hops)
972                 {
973                         this->WriteLine("ERROR :Server too far away for authentication");
974                         return false;
975                 }
976                 std::string description = params[3];
977                 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
978                 {
979                         if ((x->Name == servername) && (x->RecvPass == password))
980                         {
981                                 TreeServer* CheckDupe = FindServer(servername);
982                                 if (CheckDupe)
983                                 {
984                                         this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
985                                         return false;
986                                 }
987                                 Srv->SendOpers("*** Verified incoming server connection from \002"+servername+"\002["+this->GetIP()+"] ("+description+")");
988                                 this->InboundServerName = servername;
989                                 this->InboundDescription = description;
990                                 // this is good. Send our details: Our server name and description and hopcount of 0,
991                                 // along with the sendpass from this block.
992                                 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
993                                 // move to the next state, we are now waiting for THEM.
994                                 this->LinkState = WAIT_AUTH_2;
995                                 return true;
996                         }
997                 }
998                 this->WriteLine("ERROR :Invalid credentials");
999                 return false;
1000         }
1001
1002         std::deque<std::string> Split(std::string line, bool stripcolon)
1003         {
1004                 std::deque<std::string> n;
1005                 if (!strchr(line.c_str(),' '))
1006                 {
1007                         n.push_back(line);
1008                         return n;
1009                 }
1010                 std::stringstream s(line);
1011                 std::string param = "";
1012                 n.clear();
1013                 int item = 0;
1014                 while (!s.eof())
1015                 {
1016                         char c;
1017                         s.get(c);
1018                         if (c == ' ')
1019                         {
1020                                 n.push_back(param);
1021                                 param = "";
1022                                 item++;
1023                         }
1024                         else
1025                         {
1026                                 if (!s.eof())
1027                                 {
1028                                         param = param + c;
1029                                 }
1030                                 if ((param == ":") && (item > 0))
1031                                 {
1032                                         param = "";
1033                                         while (!s.eof())
1034                                         {
1035                                                 s.get(c);
1036                                                 if (!s.eof())
1037                                                 {
1038                                                         param = param + c;
1039                                                 }
1040                                         }
1041                                         n.push_back(param);
1042                                         param = "";
1043                                 }
1044                         }
1045                 }
1046                 if (param != "")
1047                 {
1048                         n.push_back(param);
1049                 }
1050                 return n;
1051         }
1052
1053         bool ProcessLine(std::string line)
1054         {
1055                 char* l = (char*)line.c_str();
1056                 while ((strlen(l)) && (l[strlen(l)-1] == '\r') || (l[strlen(l)-1] == '\n'))
1057                         l[strlen(l)-1] = '\0';
1058                 line = l;
1059                 if (line == "")
1060                         return true;
1061                 Srv->Log(DEBUG,"IN: '"+line+"'");
1062                 std::deque<std::string> params = this->Split(line,true);
1063                 std::string command = "";
1064                 std::string prefix = "";
1065                 if (((params[0].c_str())[0] == ':') && (params.size() > 1))
1066                 {
1067                         prefix = params[0];
1068                         command = params[1];
1069                         char* pref = (char*)prefix.c_str();
1070                         prefix = ++pref;
1071                         params.pop_front();
1072                         params.pop_front();
1073                 }
1074                 else
1075                 {
1076                         prefix = "";
1077                         command = params[0];
1078                         params.pop_front();
1079                 }
1080                 
1081                 switch (this->LinkState)
1082                 {
1083                         TreeServer* Node;
1084                         
1085                         case WAIT_AUTH_1:
1086                                 // Waiting for SERVER command from remote server. Server initiating
1087                                 // the connection sends the first SERVER command, listening server
1088                                 // replies with theirs if its happy, then if the initiator is happy,
1089                                 // it starts to send its net sync, which starts the merge, otherwise
1090                                 // it sends an ERROR.
1091                                 if (command == "SERVER")
1092                                 {
1093                                         return this->Inbound_Server(params);
1094                                 }
1095                                 else if (command == "ERROR")
1096                                 {
1097                                         return this->Error(params);
1098                                 }
1099                         break;
1100                         case WAIT_AUTH_2:
1101                                 // Waiting for start of other side's netmerge to say they liked our
1102                                 // password.
1103                                 if (command == "SERVER")
1104                                 {
1105                                         // cant do this, they sent it to us in the WAIT_AUTH_1 state!
1106                                         // silently ignore.
1107                                         return true;
1108                                 }
1109                                 else if (command == "BURST")
1110                                 {
1111                                         this->LinkState = CONNECTED;
1112                                         Node = new TreeServer(InboundServerName,InboundDescription,TreeRoot,this);
1113                                         TreeRoot->AddChild(Node);
1114                                         params.clear();
1115                                         params.push_back(InboundServerName);
1116                                         params.push_back("*");
1117                                         params.push_back("1");
1118                                         params.push_back(":"+InboundDescription);
1119                                         DoOneToAllButSender(TreeRoot->GetName(),"SERVER",params,InboundServerName);
1120                                         this->DoBurst(Node);
1121                                 }
1122                                 else if (command == "ERROR")
1123                                 {
1124                                         return this->Error(params);
1125                                 }
1126                                 
1127                         break;
1128                         case LISTENER:
1129                                 this->WriteLine("ERROR :Internal error -- listening socket accepted its own descriptor!!!");
1130                                 return false;
1131                         break;
1132                         case CONNECTING:
1133                                 if (command == "SERVER")
1134                                 {
1135                                         // another server we connected to, which was in WAIT_AUTH_1 state,
1136                                         // has just sent us their credentials. If we get this far, theyre
1137                                         // happy with OUR credentials, and they are now in WAIT_AUTH_2 state.
1138                                         // if we're happy with this, we should send our netburst which
1139                                         // kickstarts the merge.
1140                                         return this->Outbound_Reply_Server(params);
1141                                 }
1142                                 else if (command == "ERROR")
1143                                 {
1144                                         return this->Error(params);
1145                                 }
1146                         break;
1147                         case CONNECTED:
1148                                 // This is the 'authenticated' state, when all passwords
1149                                 // have been exchanged and anything past this point is taken
1150                                 // as gospel.
1151                                 std::string target = "";
1152                                 if ((command == "NICK") && (params.size() > 1))
1153                                 {
1154                                         return this->IntroduceClient(prefix,params);
1155                                 }
1156                                 else if (command == "FJOIN")
1157                                 {
1158                                         return this->ForceJoin(prefix,params);
1159                                 }
1160                                 else if (command == "SERVER")
1161                                 {
1162                                         return this->RemoteServer(prefix,params);
1163                                 }
1164                                 else if (command == "ERROR")
1165                                 {
1166                                         return this->Error(params);
1167                                 }
1168                                 else if (command == "OPERTYPE")
1169                                 {
1170                                         return this->OperType(prefix,params);
1171                                 }
1172                                 else if (command == "FMODE")
1173                                 {
1174                                         return this->ForceMode(prefix,params);
1175                                 }
1176                                 else if (command == "KILL")
1177                                 {
1178                                         return this->RemoteKill(prefix,params);
1179                                 }
1180                                 else if (command == "FTOPIC")
1181                                 {
1182                                         return this->ForceTopic(prefix,params);
1183                                 }
1184                                 else if (command == "REHASH")
1185                                 {
1186                                         return this->RemoteRehash(prefix,params);
1187                                 }
1188                                 else if (command == "PING")
1189                                 {
1190                                         return this->LocalPing(prefix,params);
1191                                 }
1192                                 else if (command == "PONG")
1193                                 {
1194                                         return this->LocalPong(prefix,params);
1195                                 }
1196                                 else if (command == "SQUIT")
1197                                 {
1198                                         if (params.size() == 2)
1199                                         {
1200                                                 this->Squit(FindServer(params[0]),params[1]);
1201                                         }
1202                                         return true;
1203                                 }
1204                                 else
1205                                 {
1206                                         // not a special inter-server command.
1207                                         // Emulate the actual user doing the command,
1208                                         // this saves us having a huge ugly parser.
1209                                         userrec* who = Srv->FindNick(prefix);
1210                                         std::string sourceserv = this->myhost;
1211                                         if (this->InboundServerName != "")
1212                                         {
1213                                                 sourceserv = this->InboundServerName;
1214                                         }
1215                                         if (who)
1216                                         {
1217                                                 // its a user
1218                                                 target = who->server;
1219                                                 char* strparams[127];
1220                                                 for (unsigned int q = 0; q < params.size(); q++)
1221                                                 {
1222                                                         strparams[q] = (char*)params[q].c_str();
1223                                                 }
1224                                                 Srv->CallCommandHandler(command, strparams, params.size(), who);
1225                                         }
1226                                         else
1227                                         {
1228                                                 // its not a user. Its either a server, or somethings screwed up.
1229                                                 if (IsServer(prefix))
1230                                                 {
1231                                                         target = Srv->GetServerName();
1232                                                 }
1233                                                 else
1234                                                 {
1235                                                         log(DEBUG,"Command with unknown origin '%s'",prefix.c_str());
1236                                                         return true;
1237                                                 }
1238                                         }
1239                                         return DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1240
1241                                 }
1242                                 return true;
1243                         break;
1244                 }
1245                 return true;
1246         }
1247
1248         virtual std::string GetName()
1249         {
1250                 std::string sourceserv = this->myhost;
1251                 if (this->InboundServerName != "")
1252                 {
1253                         sourceserv = this->InboundServerName;
1254                 }
1255                 return sourceserv;
1256         }
1257
1258         virtual void OnTimeout()
1259         {
1260                 if (this->LinkState == CONNECTING)
1261                 {
1262                         Srv->SendOpers("*** CONNECT: Connection to \002"+myhost+"\002 timed out.");
1263                 }
1264         }
1265
1266         virtual void OnClose()
1267         {
1268                 // Connection closed.
1269                 // If the connection is fully up (state CONNECTED)
1270                 // then propogate a netsplit to all peers.
1271                 std::string quitserver = this->myhost;
1272                 if (this->InboundServerName != "")
1273                 {
1274                         quitserver = this->InboundServerName;
1275                 }
1276                 TreeServer* s = FindServer(quitserver);
1277                 if (s)
1278                 {
1279                         Squit(s,"Remote host closed the connection");
1280                 }
1281         }
1282
1283         virtual int OnIncomingConnection(int newsock, char* ip)
1284         {
1285                 TreeSocket* s = new TreeSocket(newsock, ip);
1286                 Srv->AddSocket(s);
1287                 return true;
1288         }
1289 };
1290
1291 void AddThisServer(TreeServer* server, std::deque<TreeServer*> &list)
1292 {
1293         for (unsigned int c = 0; c < list.size(); c++)
1294         {
1295                 if (list[c] == server)
1296                 {
1297                         return;
1298                 }
1299         }
1300         list.push_back(server);
1301 }
1302
1303 // returns a list of DIRECT servernames for a specific channel
1304 std::deque<TreeServer*> GetListOfServersForChannel(chanrec* c)
1305 {
1306         std::deque<TreeServer*> list;
1307         std::vector<char*> *ulist = c->GetUsers();
1308         for (unsigned int i = 0; i < ulist->size(); i++)
1309         {
1310                 char* o = (*ulist)[i];
1311                 userrec* otheruser = (userrec*)o;
1312                 if (std::string(otheruser->server) != Srv->GetServerName())
1313                 {
1314                         TreeServer* best = BestRouteTo(otheruser->server);
1315                         if (best)
1316                                 AddThisServer(best,list);
1317                 }
1318         }
1319         return list;
1320 }
1321
1322 bool DoOneToAllButSenderRaw(std::string data,std::string omit,std::string prefix,std::string command,std::deque<std::string> params)
1323 {
1324         TreeServer* omitroute = BestRouteTo(omit);
1325         if ((command == "NOTICE") || (command == "PRIVMSG"))
1326         {
1327                 if ((params.size() >= 2) && (*(params[0].c_str()) != '$'))
1328                 {
1329                         if (*(params[0].c_str()) != '#')
1330                         {
1331                                 // special routing for private messages/notices
1332                                 userrec* d = Srv->FindNick(params[0]);
1333                                 if (d)
1334                                 {
1335                                         std::deque<std::string> par;
1336                                         par.clear();
1337                                         par.push_back(params[0]);
1338                                         par.push_back(":"+params[1]);
1339                                         DoOneToOne(prefix,command,par,d->server);
1340                                         return true;
1341                                 }
1342                         }
1343                         else
1344                         {
1345                                 log(DEBUG,"Channel privmsg going to chan %s",params[0].c_str());
1346                                 chanrec* c = Srv->FindChannel(params[0]);
1347                                 if (c)
1348                                 {
1349                                         std::deque<TreeServer*> list = GetListOfServersForChannel(c);
1350                                         log(DEBUG,"Got a list of %d servers",list.size());
1351                                         for (unsigned int i = 0; i < list.size(); i++)
1352                                         {
1353                                                 TreeSocket* Sock = list[i]->GetSocket();
1354                                                 if ((Sock) && (list[i]->GetName() != omit) && (omitroute != list[i]))
1355                                                 {
1356                                                         log(DEBUG,"Writing privmsg to server %s",list[i]->GetName().c_str());
1357                                                         Sock->WriteLine(data);
1358                                                 }
1359                                         }
1360                                         return true;
1361                                 }
1362                         }
1363                 }
1364         }
1365         for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1366         {
1367                 TreeServer* Route = TreeRoot->GetChild(x);
1368                 if ((Route->GetSocket()) && (Route->GetName() != omit) && (omitroute != Route))
1369                 {
1370                         TreeSocket* Sock = Route->GetSocket();
1371                         Sock->WriteLine(data);
1372                 }
1373         }
1374         return true;
1375 }
1376
1377 bool DoOneToAllButSender(std::string prefix, std::string command, std::deque<std::string> params, std::string omit)
1378 {
1379         TreeServer* omitroute = BestRouteTo(omit);
1380         std::string FullLine = ":" + prefix + " " + command;
1381         for (unsigned int x = 0; x < params.size(); x++)
1382         {
1383                 FullLine = FullLine + " " + params[x];
1384         }
1385         for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1386         {
1387                 TreeServer* Route = TreeRoot->GetChild(x);
1388                 // Send the line IF:
1389                 // The route has a socket (its a direct connection)
1390                 // The route isnt the one to be omitted
1391                 // The route isnt the path to the one to be omitted
1392                 if ((Route->GetSocket()) && (Route->GetName() != omit) && (omitroute != Route))
1393                 {
1394                         TreeSocket* Sock = Route->GetSocket();
1395                         Sock->WriteLine(FullLine);
1396                 }
1397         }
1398         return true;
1399 }
1400
1401 bool DoOneToMany(std::string prefix, std::string command, std::deque<std::string> params)
1402 {
1403         std::string FullLine = ":" + prefix + " " + command;
1404         for (unsigned int x = 0; x < params.size(); x++)
1405         {
1406                 FullLine = FullLine + " " + params[x];
1407         }
1408         for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1409         {
1410                 TreeServer* Route = TreeRoot->GetChild(x);
1411                 if (Route->GetSocket())
1412                 {
1413                         TreeSocket* Sock = Route->GetSocket();
1414                         Sock->WriteLine(FullLine);
1415                 }
1416         }
1417         return true;
1418 }
1419
1420 bool DoOneToOne(std::string prefix, std::string command, std::deque<std::string> params, std::string target)
1421 {
1422         TreeServer* Route = BestRouteTo(target);
1423         if (Route)
1424         {
1425                 std::string FullLine = ":" + prefix + " " + command;
1426                 for (unsigned int x = 0; x < params.size(); x++)
1427                 {
1428                         FullLine = FullLine + " " + params[x];
1429                 }
1430                 if (Route->GetSocket())
1431                 {
1432                         TreeSocket* Sock = Route->GetSocket();
1433                         Sock->WriteLine(FullLine);
1434                 }
1435                 return true;
1436         }
1437         else
1438         {
1439                 return true;
1440         }
1441 }
1442
1443 std::vector<TreeSocket*> Bindings;
1444
1445 void ReadConfiguration(bool rebind)
1446 {
1447         if (rebind)
1448         {
1449                 for (int j =0; j < Conf->Enumerate("bind"); j++)
1450                 {
1451                         std::string Type = Conf->ReadValue("bind","type",j);
1452                         std::string IP = Conf->ReadValue("bind","address",j);
1453                         long Port = Conf->ReadInteger("bind","port",j,true);
1454                         if (Type == "servers")
1455                         {
1456                                 if (IP == "*")
1457                                 {
1458                                         IP = "";
1459                                 }
1460                                 TreeSocket* listener = new TreeSocket(IP.c_str(),Port,true,10);
1461                                 if (listener->GetState() == I_LISTENING)
1462                                 {
1463                                         Srv->AddSocket(listener);
1464                                         Bindings.push_back(listener);
1465                                 }
1466                                 else
1467                                 {
1468                                         log(DEFAULT,"m_spanningtree: Warning: Failed to bind server port %d",Port);
1469                                         listener->Close();
1470                                         delete listener;
1471                                 }
1472                         }
1473                 }
1474         }
1475         LinkBlocks.clear();
1476         for (int j =0; j < Conf->Enumerate("link"); j++)
1477         {
1478                 Link L;
1479                 L.Name = Conf->ReadValue("link","name",j);
1480                 L.IPAddr = Conf->ReadValue("link","ipaddr",j);
1481                 L.Port = Conf->ReadInteger("link","port",j,true);
1482                 L.SendPass = Conf->ReadValue("link","sendpass",j);
1483                 L.RecvPass = Conf->ReadValue("link","recvpass",j);
1484                 L.AutoConnect = Conf->ReadInteger("link","autoconnect",j,true);
1485                 L.NextConnectTime = time(NULL) + L.AutoConnect;
1486                 LinkBlocks.push_back(L);
1487                 log(DEBUG,"m_spanningtree: Read server %s with host %s:%d",L.Name.c_str(),L.IPAddr.c_str(),L.Port);
1488         }
1489 }
1490
1491
1492 class ModuleSpanningTree : public Module
1493 {
1494         std::vector<TreeSocket*> Bindings;
1495         int line;
1496         int NumServers;
1497
1498  public:
1499
1500         ModuleSpanningTree()
1501         {
1502                 Srv = new Server;
1503                 Conf = new ConfigReader;
1504                 Bindings.clear();
1505
1506                 // Create the root of the tree
1507                 TreeRoot = new TreeServer(Srv->GetServerName(),Srv->GetServerDescription());
1508
1509                 ReadConfiguration(true);
1510         }
1511
1512         void ShowLinks(TreeServer* Current, userrec* user, int hops)
1513         {
1514                 std::string Parent = TreeRoot->GetName();
1515                 if (Current->GetParent())
1516                 {
1517                         Parent = Current->GetParent()->GetName();
1518                 }
1519                 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1520                 {
1521                         ShowLinks(Current->GetChild(q),user,hops+1);
1522                 }
1523                 WriteServ(user->fd,"364 %s %s %s :%d %s",user->nick,Current->GetName().c_str(),Parent.c_str(),hops,Current->GetDesc().c_str());
1524         }
1525
1526         int CountLocalServs()
1527         {
1528                 return TreeRoot->ChildCount();
1529         }
1530
1531         void CountServsRecursive(TreeServer* Current)
1532         {
1533                 NumServers++;
1534                 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1535                 {
1536                         CountServsRecursive(Current->GetChild(q));
1537                 }
1538         }
1539         
1540         int CountServs()
1541         {
1542                 NumServers = 0;
1543                 CountServsRecursive(TreeRoot);
1544                 return NumServers;
1545         }
1546
1547         void HandleLinks(char** parameters, int pcnt, userrec* user)
1548         {
1549                 ShowLinks(TreeRoot,user,0);
1550                 WriteServ(user->fd,"365 %s * :End of /LINKS list.",user->nick);
1551                 return;
1552         }
1553
1554         void HandleLusers(char** parameters, int pcnt, userrec* user)
1555         {
1556                 WriteServ(user->fd,"251 %s :There are %d users and %d invisible on %d servers",user->nick,usercnt()-usercount_invisible(),usercount_invisible(),this->CountServs());
1557                 WriteServ(user->fd,"252 %s %d :operator(s) online",user->nick,usercount_opers());
1558                 WriteServ(user->fd,"253 %s %d :unknown connections",user->nick,usercount_unknown());
1559                 WriteServ(user->fd,"254 %s %d :channels formed",user->nick,chancount());
1560                 WriteServ(user->fd,"254 %s :I have %d clients and %d servers",user->nick,local_count(),this->CountLocalServs());
1561                 return;
1562         }
1563
1564         // WARNING: NOT THREAD SAFE - DONT GET ANY SMART IDEAS.
1565
1566         void ShowMap(TreeServer* Current, userrec* user, int depth, char matrix[128][80])
1567         {
1568                 if (line < 128)
1569                 {
1570                         for (int t = 0; t < depth; t++)
1571                         {
1572                                 matrix[line][t] = ' ';
1573                         }
1574                         strlcpy(&matrix[line][depth],Current->GetName().c_str(),80);
1575                         line++;
1576                         for (unsigned int q = 0; q < Current->ChildCount(); q++)
1577                         {
1578                                 ShowMap(Current->GetChild(q),user,depth+2,matrix);
1579                         }
1580                 }
1581         }
1582
1583         // Ok, prepare to be confused.
1584         // After much mulling over how to approach this, it struck me that
1585         // the 'usual' way of doing a /MAP isnt the best way. Instead of
1586         // keeping track of a ton of ascii characters, and line by line
1587         // under recursion working out where to place them using multiplications
1588         // and divisons, we instead render the map onto a backplane of characters
1589         // (a character matrix), then draw the branches as a series of "L" shapes
1590         // from the nodes. This is not only friendlier on CPU it uses less stack.
1591
1592         void HandleMap(char** parameters, int pcnt, userrec* user)
1593         {
1594                 // This array represents a virtual screen which we will
1595                 // "scratch" draw to, as the console device of an irc
1596                 // client does not provide for a proper terminal.
1597                 char matrix[128][80];
1598                 for (unsigned int t = 0; t < 128; t++)
1599                 {
1600                         matrix[t][0] = '\0';
1601                 }
1602                 line = 0;
1603                 // The only recursive bit is called here.
1604                 ShowMap(TreeRoot,user,0,matrix);
1605                 // Process each line one by one. The algorithm has a limit of
1606                 // 128 servers (which is far more than a spanning tree should have
1607                 // anyway, so we're ok). This limit can be raised simply by making
1608                 // the character matrix deeper, 128 rows taking 10k of memory.
1609                 for (int l = 1; l < line; l++)
1610                 {
1611                         // scan across the line looking for the start of the
1612                         // servername (the recursive part of the algorithm has placed
1613                         // the servers at indented positions depending on what they
1614                         // are related to)
1615                         int first_nonspace = 0;
1616                         while (matrix[l][first_nonspace] == ' ')
1617                         {
1618                                 first_nonspace++;
1619                         }
1620                         first_nonspace--;
1621                         // Draw the `- (corner) section: this may be overwritten by
1622                         // another L shape passing along the same vertical pane, becoming
1623                         // a |- (branch) section instead.
1624                         matrix[l][first_nonspace] = '-';
1625                         matrix[l][first_nonspace-1] = '`';
1626                         int l2 = l - 1;
1627                         // Draw upwards until we hit the parent server, causing possibly
1628                         // other corners (`-) to become branches (|-)
1629                         while ((matrix[l2][first_nonspace-1] == ' ') || (matrix[l2][first_nonspace-1] == '`'))
1630                         {
1631                                 matrix[l2][first_nonspace-1] = '|';
1632                                 l2--;
1633                         }
1634                 }
1635                 // dump the whole lot to the user. This is the easy bit, honest.
1636                 for (int t = 0; t < line; t++)
1637                 {
1638                         WriteServ(user->fd,"006 %s :%s",user->nick,&matrix[t][0]);
1639                 }
1640                 WriteServ(user->fd,"007 %s :End of /MAP",user->nick);
1641                 return;
1642         }
1643
1644         int HandleSquit(char** parameters, int pcnt, userrec* user)
1645         {
1646                 TreeServer* s = FindServer(parameters[0]);
1647                 if (s)
1648                 {
1649                         TreeSocket* sock = s->GetSocket();
1650                         if (sock)
1651                         {
1652                                 WriteOpers("*** SQUIT: Server \002%s\002 removed from network by %s",parameters[0],user->nick);
1653                                 sock->Squit(s,"Server quit by "+std::string(user->nick)+"!"+std::string(user->ident)+"@"+std::string(user->host));
1654                                 sock->Close();
1655                         }
1656                         else
1657                         {
1658                                 WriteServ(user->fd,"NOTICE %s :*** SQUIT: The server \002%s\002 is not directly connected.",user->nick,parameters[0]);
1659                         }
1660                 }
1661                 else
1662                 {
1663                          WriteServ(user->fd,"NOTICE %s :*** SQUIT: The server \002%s\002 does not exist on the network.",user->nick,parameters[0]);
1664                 }
1665                 return 1;
1666         }
1667
1668         void DoPingChecks(time_t curtime)
1669         {
1670                 for (unsigned int j = 0; j < TreeRoot->ChildCount(); j++)
1671                 {
1672                         TreeServer* serv = TreeRoot->GetChild(j);
1673                         TreeSocket* sock = serv->GetSocket();
1674                         if (sock)
1675                         {
1676                                 if (curtime >= serv->NextPingTime())
1677                                 {
1678                                         if (serv->AnsweredLastPing())
1679                                         {
1680                                                 sock->WriteLine(":"+Srv->GetServerName()+" PING "+serv->GetName());
1681                                                 serv->SetNextPingTime(curtime + 60);
1682                                         }
1683                                         else
1684                                         {
1685                                                 // they didnt answer, boot them
1686                                                 WriteOpers("*** Server \002%s\002 pinged out",serv->GetName().c_str());
1687                                                 sock->Squit(serv,"Ping timeout");
1688                                                 sock->Close();
1689                                                 return;
1690                                         }
1691                                 }
1692                         }
1693                 }
1694         }
1695
1696         void AutoConnectServers(time_t curtime)
1697         {
1698                 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
1699                 {
1700                         if ((x->AutoConnect) && (curtime >= x->NextConnectTime))
1701                         {
1702                                 log(DEBUG,"Auto-Connecting %s",x->Name.c_str());
1703                                 x->NextConnectTime = curtime + x->AutoConnect;
1704                                 TreeServer* CheckDupe = FindServer(x->Name);
1705                                 if (!CheckDupe)
1706                                 {
1707                                         // an autoconnected server is not connected. Check if its time to connect it
1708                                         WriteOpers("*** AUTOCONNECT: Auto-connecting server \002%s\002 (%lu seconds until next attempt)",x->Name.c_str(),x->AutoConnect);
1709                                         TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name);
1710                                         Srv->AddSocket(newsocket);
1711                                 }
1712                         }
1713                 }
1714         }
1715         
1716         int HandleConnect(char** parameters, int pcnt, userrec* user)
1717         {
1718                 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
1719                 {
1720                         if (Srv->MatchText(x->Name.c_str(),parameters[0]))
1721                         {
1722                                 TreeServer* CheckDupe = FindServer(x->Name);
1723                                 if (!CheckDupe)
1724                                 {
1725                                         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);
1726                                         TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name);
1727                                         Srv->AddSocket(newsocket);
1728                                         return 1;
1729                                 }
1730                                 else
1731                                 {
1732                                         WriteServ(user->fd,"NOTICE %s :*** CONNECT: Server \002%s\002 already exists on the network and is connected via \002%s\002",user->nick,x->Name.c_str(),CheckDupe->GetParent()->GetName().c_str());
1733                                         return 1;
1734                                 }
1735                         }
1736                 }
1737                 WriteServ(user->fd,"NOTICE %s :*** CONNECT: No server matching \002%s\002 could be found in the config file.",user->nick,parameters[0]);
1738                 return 1;
1739         }
1740
1741         virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
1742         {
1743                 if (command == "CONNECT")
1744                 {
1745                         return this->HandleConnect(parameters,pcnt,user);
1746                 }
1747                 else if (command == "SQUIT")
1748                 {
1749                         return this->HandleSquit(parameters,pcnt,user);
1750                 }
1751                 else if (command == "MAP")
1752                 {
1753                         this->HandleMap(parameters,pcnt,user);
1754                         return 1;
1755                 }
1756                 else if (command == "LUSERS")
1757                 {
1758                         this->HandleLusers(parameters,pcnt,user);
1759                         return 1;
1760                 }
1761                 else if (command == "LINKS")
1762                 {
1763                         this->HandleLinks(parameters,pcnt,user);
1764                         return 1;
1765                 }
1766                 else if (Srv->IsValidModuleCommand(command, pcnt, user))
1767                 {
1768                         // this bit of code cleverly routes all module commands
1769                         // to all remote severs *automatically* so that modules
1770                         // can just handle commands locally, without having
1771                         // to have any special provision in place for remote
1772                         // commands and linking protocols.
1773                         std::deque<std::string> params;
1774                         params.clear();
1775                         for (int j = 0; j < pcnt; j++)
1776                         {
1777                                 if (strchr(parameters[j],' '))
1778                                 {
1779                                         params.push_back(":" + std::string(parameters[j]));
1780                                 }
1781                                 else
1782                                 {
1783                                         params.push_back(std::string(parameters[j]));
1784                                 }
1785                         }
1786                         DoOneToMany(user->nick,command,params);
1787                 }
1788                 return 0;
1789         }
1790
1791         virtual void OnGetServerDescription(std::string servername,std::string &description)
1792         {
1793                 TreeServer* s = FindServer(servername);
1794                 if (s)
1795                 {
1796                         description = s->GetDesc();
1797                 }
1798         }
1799
1800         virtual void OnUserInvite(userrec* source,userrec* dest,chanrec* channel)
1801         {
1802                 if (std::string(source->server) == Srv->GetServerName())
1803                 {
1804                         std::deque<std::string> params;
1805                         params.push_back(dest->nick);
1806                         params.push_back(channel->name);
1807                         DoOneToMany(source->nick,"INVITE",params);
1808                 }
1809         }
1810
1811         virtual void OnPostLocalTopicChange(userrec* user, chanrec* chan, std::string topic)
1812         {
1813                 std::deque<std::string> params;
1814                 params.push_back(chan->name);
1815                 params.push_back(":"+topic);
1816                 DoOneToMany(user->nick,"TOPIC",params);
1817         }
1818
1819         virtual void OnWallops(userrec* user, std::string text)
1820         {
1821                 if (std::string(user->server) == Srv->GetServerName())
1822                 {
1823                         std::deque<std::string> params;
1824                         params.push_back(":"+text);
1825                         DoOneToMany(user->nick,"WALLOPS",params);
1826                 }
1827         }
1828
1829         virtual void OnUserNotice(userrec* user, void* dest, int target_type, std::string text)
1830         {
1831                 if (target_type == TYPE_USER)
1832                 {
1833                         userrec* d = (userrec*)dest;
1834                         if ((std::string(d->server) != Srv->GetServerName()) && (std::string(user->server) == Srv->GetServerName()))
1835                         {
1836                                 std::deque<std::string> params;
1837                                 params.clear();
1838                                 params.push_back(d->nick);
1839                                 params.push_back(":"+text);
1840                                 DoOneToOne(user->nick,"NOTICE",params,d->server);
1841                         }
1842                 }
1843                 else
1844                 {
1845                         if (std::string(user->server) == Srv->GetServerName())
1846                         {
1847                                 chanrec *c = (chanrec*)dest;
1848                                 std::deque<TreeServer*> list = GetListOfServersForChannel(c);
1849                                 for (unsigned int i = 0; i < list.size(); i++)
1850                                 {
1851                                         TreeSocket* Sock = list[i]->GetSocket();
1852                                         if (Sock)
1853                                                 Sock->WriteLine(":"+std::string(user->nick)+" NOTICE "+std::string(c->name)+" :"+text);
1854                                 }
1855                         }
1856                 }
1857         }
1858
1859         virtual void OnUserMessage(userrec* user, void* dest, int target_type, std::string text)
1860         {
1861                 if (target_type == TYPE_USER)
1862                 {
1863                         // route private messages which are targetted at clients only to the server
1864                         // which needs to receive them
1865                         userrec* d = (userrec*)dest;
1866                         if ((std::string(d->server) != Srv->GetServerName()) && (std::string(user->server) == Srv->GetServerName()))
1867                         {
1868                                 std::deque<std::string> params;
1869                                 params.clear();
1870                                 params.push_back(d->nick);
1871                                 params.push_back(":"+text);
1872                                 DoOneToOne(user->nick,"PRIVMSG",params,d->server);
1873                         }
1874                 }
1875                 else
1876                 {
1877                         if (std::string(user->server) == Srv->GetServerName())
1878                         {
1879                                 chanrec *c = (chanrec*)dest;
1880                                 std::deque<TreeServer*> list = GetListOfServersForChannel(c);
1881                                 for (unsigned int i = 0; i < list.size(); i++)
1882                                 {
1883                                         TreeSocket* Sock = list[i]->GetSocket();
1884                                         if (Sock)
1885                                                 Sock->WriteLine(":"+std::string(user->nick)+" PRIVMSG "+std::string(c->name)+" :"+text);
1886                                 }
1887                         }
1888                 }
1889         }
1890
1891         virtual void OnBackgroundTimer(time_t curtime)
1892         {
1893                 AutoConnectServers(curtime);
1894                 DoPingChecks(curtime);
1895         }
1896
1897         virtual void OnUserJoin(userrec* user, chanrec* channel)
1898         {
1899                 // Only do this for local users
1900                 if (std::string(user->server) == Srv->GetServerName())
1901                 {
1902                         std::deque<std::string> params;
1903                         params.clear();
1904                         params.push_back(channel->name);
1905                         if (*channel->key)
1906                         {
1907                                 // if the channel has a key, force the join by emulating the key.
1908                                 params.push_back(channel->key);
1909                         }
1910                         if (channel->GetUserCounter() > 1)
1911                         {
1912                                 // not the first in the channel
1913                                 DoOneToMany(user->nick,"JOIN",params);
1914                         }
1915                         else
1916                         {
1917                                 // first in the channel, set up their permissions
1918                                 // and the channel TS with FJOIN.
1919                                 char ts[24];
1920                                 snprintf(ts,24,"%lu",(unsigned long)channel->age);
1921                                 params.clear();
1922                                 params.push_back(channel->name);
1923                                 params.push_back(ts);
1924                                 params.push_back("@"+std::string(user->nick));
1925                                 DoOneToMany(Srv->GetServerName(),"FJOIN",params);
1926                         }
1927                 }
1928         }
1929
1930         virtual void OnUserPart(userrec* user, chanrec* channel)
1931         {
1932                 if (std::string(user->server) == Srv->GetServerName())
1933                 {
1934                         std::deque<std::string> params;
1935                         params.clear();
1936                         params.push_back(channel->name);
1937                         DoOneToMany(user->nick,"PART",params);
1938                 }
1939         }
1940
1941         virtual void OnUserConnect(userrec* user)
1942         {
1943                 char agestr[MAXBUF];
1944                 if (std::string(user->server) == Srv->GetServerName())
1945                 {
1946                         std::deque<std::string> params;
1947                         snprintf(agestr,MAXBUF,"%lu",(unsigned long)user->age);
1948                         params.clear();
1949                         params.push_back(agestr);
1950                         params.push_back(user->nick);
1951                         params.push_back(user->host);
1952                         params.push_back(user->dhost);
1953                         params.push_back(user->ident);
1954                         params.push_back("+"+std::string(user->modes));
1955                         params.push_back(user->ip);
1956                         params.push_back(":"+std::string(user->fullname));
1957                         DoOneToMany(Srv->GetServerName(),"NICK",params);
1958                 }
1959         }
1960
1961         virtual void OnUserQuit(userrec* user, std::string reason)
1962         {
1963                 if (std::string(user->server) == Srv->GetServerName())
1964                 {
1965                         std::deque<std::string> params;
1966                         params.push_back(":"+reason);
1967                         DoOneToMany(user->nick,"QUIT",params);
1968                 }
1969         }
1970
1971         virtual void OnUserPostNick(userrec* user, std::string oldnick)
1972         {
1973                 if (std::string(user->server) == Srv->GetServerName())
1974                 {
1975                         std::deque<std::string> params;
1976                         params.push_back(user->nick);
1977                         DoOneToMany(oldnick,"NICK",params);
1978                 }
1979         }
1980
1981         virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, std::string reason)
1982         {
1983                 if (std::string(source->server) == Srv->GetServerName())
1984                 {
1985                         std::deque<std::string> params;
1986                         params.push_back(chan->name);
1987                         params.push_back(user->nick);
1988                         params.push_back(":"+reason);
1989                         DoOneToMany(source->nick,"KICK",params);
1990                 }
1991         }
1992
1993         virtual void OnRemoteKill(userrec* source, userrec* dest, std::string reason)
1994         {
1995                 std::deque<std::string> params;
1996                 params.push_back(dest->nick);
1997                 params.push_back(":"+reason);
1998                 DoOneToMany(source->nick,"KILL",params);
1999         }
2000
2001         virtual void OnRehash(std::string parameter)
2002         {
2003                 if (parameter != "")
2004                 {
2005                         std::deque<std::string> params;
2006                         params.push_back(parameter);
2007                         DoOneToMany(Srv->GetServerName(),"REHASH",params);
2008                         // check for self
2009                         if (Srv->MatchText(Srv->GetServerName(),parameter))
2010                         {
2011                                 Srv->SendOpers("*** Remote rehash initiated from server \002"+Srv->GetServerName()+"\002.");
2012                                 Srv->RehashServer();
2013                         }
2014                 }
2015                 ReadConfiguration(false);
2016         }
2017
2018         // note: the protocol does not allow direct umode +o except
2019         // via NICK with 8 params. sending OPERTYPE infers +o modechange
2020         // locally.
2021         virtual void OnOper(userrec* user, std::string opertype)
2022         {
2023                 if (std::string(user->server) == Srv->GetServerName())
2024                 {
2025                         std::deque<std::string> params;
2026                         params.push_back(opertype);
2027                         DoOneToMany(user->nick,"OPERTYPE",params);
2028                 }
2029         }
2030
2031         virtual void OnMode(userrec* user, void* dest, int target_type, std::string text)
2032         {
2033                 if (std::string(user->server) == Srv->GetServerName())
2034                 {
2035                         if (target_type == TYPE_USER)
2036                         {
2037                                 userrec* u = (userrec*)dest;
2038                                 std::deque<std::string> params;
2039                                 params.push_back(u->nick);
2040                                 params.push_back(text);
2041                                 DoOneToMany(user->nick,"MODE",params);
2042                         }
2043                         else
2044                         {
2045                                 chanrec* c = (chanrec*)dest;
2046                                 std::deque<std::string> params;
2047                                 params.push_back(c->name);
2048                                 params.push_back(text);
2049                                 DoOneToMany(user->nick,"MODE",params);
2050                         }
2051                 }
2052         }
2053
2054         virtual void ProtoSendMode(void* opaque, int target_type, void* target, std::string modeline)
2055         {
2056                 TreeSocket* s = (TreeSocket*)opaque;
2057                 if (target)
2058                 {
2059                         if (target_type == TYPE_USER)
2060                         {
2061                                 userrec* u = (userrec*)target;
2062                                 s->WriteLine(":"+Srv->GetServerName()+" FMODE "+u->nick+" "+modeline);
2063                         }
2064                         else
2065                         {
2066                                 chanrec* c = (chanrec*)target;
2067                                 s->WriteLine(":"+Srv->GetServerName()+" FMODE "+c->name+" "+modeline);
2068                         }
2069                 }
2070         }
2071
2072         virtual ~ModuleSpanningTree()
2073         {
2074                 delete Srv;
2075         }
2076
2077         virtual Version GetVersion()
2078         {
2079                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
2080         }
2081 };
2082
2083
2084 class ModuleSpanningTreeFactory : public ModuleFactory
2085 {
2086  public:
2087         ModuleSpanningTreeFactory()
2088         {
2089         }
2090         
2091         ~ModuleSpanningTreeFactory()
2092         {
2093         }
2094         
2095         virtual Module * CreateModule()
2096         {
2097                 TreeProtocolModule = new ModuleSpanningTree;
2098                 return TreeProtocolModule;
2099         }
2100         
2101 };
2102
2103
2104 extern "C" void * init_module( void )
2105 {
2106         return new ModuleSpanningTreeFactory;
2107 }