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