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