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