]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree.cpp
Frigging warning
[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 enum ServerState { LISTENER, CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED };
46
47 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, irc::StrHashComp> user_hash;
48 extern user_hash clientlist;
49
50 class TreeServer;
51 class TreeSocket;
52
53 bool DoOneToOne(std::string prefix, std::string command, std::deque<std::string> params, std::string target);
54 bool DoOneToAllButSender(std::string prefix, std::string command, std::deque<std::string> params, std::string omit);
55 bool DoOneToMany(std::string prefix, std::string command, std::deque<std::string> params);
56 bool DoOneToAllButSenderRaw(std::string data,std::string omit);
57
58 class TreeServer
59 {
60         TreeServer* Parent;
61         std::vector<TreeServer*> Children;
62         std::string ServerName;
63         std::string ServerDesc;
64         std::string VersionString;
65         int UserCount;
66         int OperCount;
67         TreeSocket* Socket;     // for directly connected servers this points at the socket object
68         
69  public:
70
71         TreeServer()
72         {
73                 Parent = NULL;
74                 ServerName = "";
75                 ServerDesc = "";
76                 VersionString = "";
77                 UserCount = OperCount = 0;
78         }
79
80         TreeServer(std::string Name, std::string Desc) : ServerName(Name), ServerDesc(Desc)
81         {
82                 Parent = NULL;
83                 VersionString = "";
84                 UserCount = OperCount = 0;
85         }
86
87         TreeServer(std::string Name, std::string Desc, TreeServer* Above, TreeSocket* Sock) : Parent(Above), ServerName(Name), ServerDesc(Desc), Socket(Sock)
88         {
89                 VersionString = "";
90                 UserCount = OperCount = 0;
91         }
92
93         std::string GetName()
94         {
95                 return this->ServerName;
96         }
97
98         std::string GetDesc()
99         {
100                 return this->ServerDesc;
101         }
102
103         std::string GetVersion()
104         {
105                 return this->VersionString;
106         }
107
108         int GetUserCount()
109         {
110                 return this->UserCount;
111         }
112
113         int GetOperCount()
114         {
115                 return this->OperCount;
116         }
117
118         TreeSocket* GetSocket()
119         {
120                 return this->Socket;
121         }
122
123         TreeServer* GetParent()
124         {
125                 return this->Parent;
126         }
127
128         unsigned int ChildCount()
129         {
130                 return Children.size();
131         }
132
133         TreeServer* GetChild(unsigned int n)
134         {
135                 if (n < Children.size())
136                 {
137                         return Children[n];
138                 }
139                 else
140                 {
141                         return NULL;
142                 }
143         }
144
145         void AddChild(TreeServer* Child)
146         {
147                 Children.push_back(Child);
148         }
149
150         bool DelChild(TreeServer* Child)
151         {
152                 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
153                 {
154                         if (*a == Child)
155                         {
156                                 Children.erase(a);
157                                 return true;
158                         }
159                 }
160                 return false;
161         }
162
163         // removes child nodes of this node, and of that node, etc etc
164         bool Tidy()
165         {
166                 bool stillchildren = true;
167                 while (stillchildren)
168                 {
169                         stillchildren = false;
170                         for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
171                         {
172                                 TreeServer* s = (TreeServer*)*a;
173                                 s->Tidy();
174                                 Children.erase(a);
175                                 delete s;
176                                 stillchildren = true;
177                                 break;
178                         }
179                 }
180                 return true;
181         }
182 };
183
184 class Link
185 {
186  public:
187          std::string Name;
188          std::string IPAddr;
189          int Port;
190          std::string SendPass;
191          std::string RecvPass;
192 };
193
194 /* $ModDesc: Povides a spanning tree server link protocol */
195
196 Server *Srv;
197 ConfigReader *Conf;
198 TreeServer *TreeRoot;
199 std::vector<Link> LinkBlocks;
200
201 TreeServer* RouteEnumerate(TreeServer* Current, std::string ServerName)
202 {
203         if (Current->GetName() == ServerName)
204                 return Current;
205         for (unsigned int q = 0; q < Current->ChildCount(); q++)
206         {
207                 TreeServer* found = RouteEnumerate(Current->GetChild(q),ServerName);
208                 if (found)
209                 {
210                         return found;
211                 }
212         }
213         return NULL;
214 }
215
216 // Returns the locally connected server we must route a
217 // message through to reach server 'ServerName'. This
218 // only applies to one-to-one and not one-to-many routing.
219 TreeServer* BestRouteTo(std::string ServerName)
220 {
221         log(DEBUG,"Finding best route to %s",ServerName.c_str());
222         // first, find the server by recursively walking the tree
223         TreeServer* Found = RouteEnumerate(TreeRoot,ServerName);
224         // did we find it? If not, they did something wrong, abort.
225         if (!Found)
226         {
227                 log(DEBUG,"Failed to find %s by walking tree!",ServerName.c_str());
228                 return NULL;
229         }
230         else
231         {
232                 // The server exists, follow its parent nodes until
233                 // the parent of the current is 'TreeRoot', we know
234                 // then that this is a directly-connected server.
235                 while ((Found) && (Found->GetParent() != TreeRoot))
236                 {
237                         Found = Found->GetParent();
238                 }
239                 log(DEBUG,"Route to %s is via %s",ServerName.c_str(),Found->GetName().c_str());
240                 return Found;
241         }
242 }
243
244 bool LookForServer(TreeServer* Current, std::string ServerName)
245 {
246         if (ServerName == Current->GetName())
247                 return true;
248         for (unsigned int q = 0; q < Current->ChildCount(); q++)
249         {
250                 if (LookForServer(Current->GetChild(q),ServerName))
251                         return true;
252         }
253         return false;
254 }
255
256 TreeServer* Found;
257
258 void RFindServer(TreeServer* Current, std::string ServerName)
259 {
260         if ((ServerName == Current->GetName()) && (!Found))
261         {
262                 Found = Current;
263                 log(DEBUG,"Found server %s desc %s",Current->GetName().c_str(),Current->GetDesc().c_str());
264                 return;
265         }
266         if (!Found)
267         {
268                 for (unsigned int q = 0; q < Current->ChildCount(); q++)
269                 {
270                         if (!Found)
271                                 RFindServer(Current->GetChild(q),ServerName);
272                 }
273         }
274         return;
275 }
276
277 TreeServer* FindServer(std::string ServerName)
278 {
279         Found = NULL;
280         RFindServer(TreeRoot,ServerName);
281         return Found;
282 }
283
284 bool IsServer(std::string ServerName)
285 {
286         return LookForServer(TreeRoot,ServerName);
287 }
288
289 class TreeSocket : public InspSocket
290 {
291         std::string myhost;
292         std::string in_buffer;
293         ServerState LinkState;
294         std::string InboundServerName;
295         std::string InboundDescription;
296         int num_lost_users;
297         int num_lost_servers;
298         
299  public:
300
301         TreeSocket(std::string host, int port, bool listening, unsigned long maxtime)
302                 : InspSocket(host, port, listening, maxtime)
303         {
304                 Srv->Log(DEBUG,"Create new listening");
305                 myhost = host;
306                 this->LinkState = LISTENER;
307         }
308
309         TreeSocket(std::string host, int port, bool listening, unsigned long maxtime, std::string ServerName)
310                 : InspSocket(host, port, listening, maxtime)
311         {
312                 Srv->Log(DEBUG,"Create new outbound");
313                 myhost = ServerName;
314                 this->LinkState = CONNECTING;
315         }
316
317         TreeSocket(int newfd, char* ip)
318                 : InspSocket(newfd, ip)
319         {
320                 Srv->Log(DEBUG,"Associate new inbound");
321                 this->LinkState = WAIT_AUTH_1;
322         }
323         
324         virtual bool OnConnected()
325         {
326                 if (this->LinkState == CONNECTING)
327                 {
328                         Srv->SendOpers("*** Connection to "+myhost+"["+this->GetIP()+"] established.");
329                         // we should send our details here.
330                         // if the other side is satisfied, they send theirs.
331                         // we do not need to change state here.
332                         for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
333                         {
334                                 if (x->Name == this->myhost)
335                                 {
336                                         // found who we're supposed to be connecting to, send the neccessary gubbins.
337                                         this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
338                                         return true;
339                                 }
340                         }
341                 }
342                 log(DEBUG,"Outbound connection ERROR: Could not find the right link block!");
343                 return true;
344         }
345         
346         virtual void OnError(InspSocketError e)
347         {
348         }
349
350         virtual int OnDisconnect()
351         {
352                 return true;
353         }
354
355         // recursively send the server tree with distances as hops
356         void SendServers(TreeServer* Current, TreeServer* s, int hops)
357         {
358                 char command[1024];
359                 for (unsigned int q = 0; q < Current->ChildCount(); q++)
360                 {
361                         TreeServer* recursive_server = Current->GetChild(q);
362                         if (recursive_server != s)
363                         {
364                                 // :source.server SERVER server.name hops :Description
365                                 snprintf(command,1024,":%s SERVER %s * %d :%s",Current->GetName().c_str(),recursive_server->GetName().c_str(),hops,recursive_server->GetDesc().c_str());
366                                 this->WriteLine(command);
367                                 // down to next level
368                                 this->SendServers(recursive_server, s, hops+1);
369                         }
370                 }
371         }
372
373         void SquitServer(TreeServer* Current)
374         {
375                 // recursively squit the servers attached to 'Current'
376                 for (unsigned int q = 0; q < Current->ChildCount(); q++)
377                 {
378                         TreeServer* recursive_server = Current->GetChild(q);
379                         this->SquitServer(recursive_server);
380                 }
381                 // Now we've whacked the kids, whack self
382                 log(DEBUG,"Deleted %s",Current->GetName().c_str());
383                 num_lost_servers++;
384                 bool quittingpeople = true;
385                 while (quittingpeople)
386                 {
387                         quittingpeople = false;
388                         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
389                         {
390                                 if (!strcasecmp(u->second->server,Current->GetName().c_str()))
391                                 {
392                                         log(DEBUG,"Quitting user %s of server %s",u->second->nick,u->second->server);
393                                         Srv->QuitUser(u->second,Current->GetName()+" "+std::string(Srv->GetServerName()));
394                                         num_lost_users++;
395                                         quittingpeople = true;
396                                         break;
397                                 }
398                         }
399                 }
400         }
401
402         void Squit(TreeServer* Current,std::string reason)
403         {
404                 if (Current)
405                 {
406                         std::deque<std::string> params;
407                         params.push_back(Current->GetName());
408                         params.push_back(":"+reason);
409                         DoOneToAllButSender(Current->GetParent()->GetName(),"SQUIT",params,Current->GetName());
410                         if (Current->GetParent() == TreeRoot)
411                         {
412                                 Srv->SendOpers("Server \002"+Current->GetName()+"\002 split: "+reason);
413                         }
414                         else
415                         {
416                                 Srv->SendOpers("Server \002"+Current->GetName()+"\002 split from server \002"+Current->GetParent()->GetName()+"\002 with reason: "+reason);
417                         }
418                         num_lost_servers = 0;
419                         num_lost_users = 0;
420                         SquitServer(Current);
421                         Current->Tidy();
422                         Current->GetParent()->DelChild(Current);
423                         delete Current;
424                         WriteOpers("Netsplit complete, lost \002%d\002 users on \002%d\002 servers.", num_lost_users, num_lost_servers);
425                 }
426                 else
427                 {
428                         log(DEBUG,"Squit from unknown server");
429                 }
430         }
431
432         bool ForceJoin(std::string source, std::deque<std::string> params)
433         {
434                 if (params.size() < 1)
435                         return true;
436                 for (unsigned int channelnum = 0; channelnum < params.size(); channelnum++)
437                 {
438                         // process one channel at a time, applying modes.
439                         char* channel = (char*)params[channelnum].c_str();
440                         char permissions = *channel;
441                         char* mode = NULL;
442                         switch (permissions)
443                         {
444                                 case '@':
445                                         channel++;
446                                         mode = "+o";
447                                 break;
448                                 case '%':
449                                         channel++;
450                                         mode = "+h";
451                                 break;
452                                 case '+':
453                                         channel++;
454                                         mode = "+v";
455                                 break;
456                         }
457                         userrec* who = Srv->FindNick(source);
458                         if (who)
459                         {
460                                 char* key = "";
461                                 chanrec* chan = Srv->FindChannel(channel);
462                                 if ((chan) && (*chan->key))
463                                 {
464                                         key = chan->key;
465                                 }
466                                 Srv->JoinUserToChannel(who,channel,key);
467                                 if (mode)
468                                 {
469                                         char* modelist[3];
470                                         modelist[0] = channel;
471                                         modelist[1] = mode;
472                                         modelist[2] = who->nick;
473                                         Srv->SendMode(modelist,3,who);
474                                 }
475                                 DoOneToAllButSender(source,"FJOIN",params,who->server);
476                         }
477                 }
478                 return true;
479         }
480
481         bool IntroduceClient(std::string source, std::deque<std::string> params)
482         {
483                 if (params.size() < 8)
484                         return true;
485                 // NICK age nick host dhost ident +modes ip :gecos
486                 //       0   1    2    3      4     5    6   7
487                 std::string nick = params[1];
488                 std::string host = params[2];
489                 std::string dhost = params[3];
490                 std::string ident = params[4];
491                 time_t age = atoi(params[0].c_str());
492                 std::string modes = params[5];
493                 std::string ip = params[6];
494                 std::string gecos = params[7];
495                 char* tempnick = (char*)nick.c_str();
496                 log(DEBUG,"Introduce client %s!%s@%s",tempnick,ident.c_str(),host.c_str());
497                 
498                 user_hash::iterator iter;
499                 iter = clientlist.find(tempnick);
500                 if (iter != clientlist.end())
501                 {
502                         // nick collision
503                         log(DEBUG,"Nick collision on %s!%s@%s",tempnick,ident.c_str(),host.c_str());
504                         return true;
505                 }
506                 
507                 clientlist[tempnick] = new userrec();
508                 clientlist[tempnick]->fd = FD_MAGIC_NUMBER;
509                 strlcpy(clientlist[tempnick]->nick, tempnick,NICKMAX);
510                 strlcpy(clientlist[tempnick]->host, host.c_str(),160);
511                 strlcpy(clientlist[tempnick]->dhost, dhost.c_str(),160);
512                 clientlist[tempnick]->server = (char*)FindServerNamePtr(source.c_str());
513                 strlcpy(clientlist[tempnick]->ident, ident.c_str(),IDENTMAX);
514                 strlcpy(clientlist[tempnick]->fullname, gecos.c_str(),MAXGECOS);
515                 clientlist[tempnick]->registered = 7;
516                 clientlist[tempnick]->signon = age;
517                 strlcpy(clientlist[tempnick]->ip,ip.c_str(),16);
518                 for (int i = 0; i < MAXCHANS; i++)
519                 {
520                         clientlist[tempnick]->chans[i].channel = NULL;
521                         clientlist[tempnick]->chans[i].uc_modes = 0;
522                 }
523                 DoOneToAllButSender(source,"NICK",params,source);
524                 return true;
525         }
526
527         // send all users and their channels
528         void SendUsers(TreeServer* Current)
529         {
530                 char data[MAXBUF];
531                 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
532                 {
533                         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);
534                         this->WriteLine(data);
535                         if (strchr(u->second->modes,'o'))
536                         {
537                                 this->WriteLine(":"+std::string(u->second->nick)+" OPERTYPE "+std::string(u->second->oper));
538                         }
539                         char* chl = chlist(u->second,u->second);
540                         if (*chl)
541                         {
542                                 this->WriteLine(":"+std::string(u->second->nick)+" FJOIN "+std::string(chl));
543                         }
544                 }
545         }
546
547         void DoBurst(TreeServer* s)
548         {
549                 log(DEBUG,"Beginning network burst");
550                 Srv->SendOpers("*** Bursting to "+s->GetName()+".");
551                 this->WriteLine("BURST");
552                 // Send server tree
553                 this->SendServers(TreeRoot,s,1);
554                 // Send users and their channels
555                 this->SendUsers(s);
556                 // TODO: Send everything else (channel modes etc)
557                 this->WriteLine("ENDBURST");
558         }
559
560         virtual bool OnDataReady()
561         {
562                 char* data = this->Read();
563                 if (data)
564                 {
565                         this->in_buffer += data;
566                         while (in_buffer.find("\n") != std::string::npos)
567                         {
568                                 char* line = (char*)in_buffer.c_str();
569                                 std::string ret = "";
570                                 while ((*line != '\n') && (strlen(line)))
571                                 {
572                                         ret = ret + *line;
573                                         line++;
574                                 }
575                                 if ((*line == '\n') || (*line == '\r'))
576                                         line++;
577                                 in_buffer = line;
578                                 if (!this->ProcessLine(ret))
579                                 {
580                                         return false;
581                                 }
582                         }
583                 }
584                 return (data != NULL);
585         }
586
587         int WriteLine(std::string line)
588         {
589                 return this->Write(line + "\r\n");
590         }
591
592         bool Error(std::deque<std::string> params)
593         {
594                 if (params.size() < 1)
595                         return false;
596                 std::string Errmsg = params[0];
597                 std::string SName = myhost;
598                 if (InboundServerName != "")
599                 {
600                         SName = InboundServerName;
601                 }
602                 Srv->SendOpers("*** ERROR from "+SName+": "+Errmsg);
603                 // we will return false to cause the socket to close.
604                 return false;
605         }
606
607         bool OperType(std::string prefix, std::deque<std::string> params)
608         {
609                 if (params.size() != 1)
610                         return true;
611                 std::string opertype = params[0];
612                 userrec* u = Srv->FindNick(prefix);
613                 if (u)
614                 {
615                         strlcpy(u->oper,opertype.c_str(),NICKMAX);
616                         if (!strchr(u->modes,'o'))
617                         {
618                                 strcat(u->modes,"o");
619                         }
620                         DoOneToAllButSender(u->server,"OPERTYPE",params,u->server);
621                 }
622                 return true;
623         }
624
625         bool RemoteServer(std::string prefix, std::deque<std::string> params)
626         {
627                 if (params.size() < 4)
628                         return false;
629                 std::string servername = params[0];
630                 std::string password = params[1];
631                 int hops = atoi(params[2].c_str());
632                 std::string description = params[3];
633                 if (!hops)
634                 {
635                         this->WriteLine("ERROR :Protocol error - Introduced remote server with incorrect hopcount!");
636                         return false;
637                 }
638                 TreeServer* ParentOfThis = FindServer(prefix);
639                 if (!ParentOfThis)
640                 {
641                         this->WriteLine("ERROR :Protocol error - Introduced remote server from unknown server "+prefix);
642                         return false;
643                 }
644                 TreeServer* Node = new TreeServer(servername,description,ParentOfThis,NULL);
645                 ParentOfThis->AddChild(Node);
646                 params[3] = ":" + params[3];
647                 DoOneToAllButSender(prefix,"SERVER",params,prefix);
648                 Srv->SendOpers("*** Server \002"+prefix+"\002 introduced server \002"+servername+"\002 ("+description+")");
649                 return true;
650         }
651
652         bool Outbound_Reply_Server(std::deque<std::string> params)
653         {
654                 if (params.size() < 4)
655                         return false;
656                 std::string servername = params[0];
657                 std::string password = params[1];
658                 int hops = atoi(params[2].c_str());
659                 if (hops)
660                 {
661                         this->WriteLine("ERROR :Server too far away for authentication");
662                         return false;
663                 }
664                 std::string description = params[3];
665                 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
666                 {
667                         if ((x->Name == servername) && (x->RecvPass == password))
668                         {
669                                 // Begin the sync here. this kickstarts the
670                                 // other side, waiting in WAIT_AUTH_2 state,
671                                 // into starting their burst, as it shows
672                                 // that we're happy.
673                                 this->LinkState = CONNECTED;
674                                 // we should add the details of this server now
675                                 // to the servers tree, as a child of the root
676                                 // node.
677                                 TreeServer* Node = new TreeServer(servername,description,TreeRoot,this);
678                                 TreeRoot->AddChild(Node);
679                                 DoOneToAllButSender(TreeRoot->GetName(),"SERVER",params,servername);
680                                 this->DoBurst(Node);
681                                 return true;
682                         }
683                 }
684                 this->WriteLine("ERROR :Invalid credentials");
685                 return false;
686         }
687
688         bool Inbound_Server(std::deque<std::string> params)
689         {
690                 if (params.size() < 4)
691                         return false;
692                 std::string servername = params[0];
693                 std::string password = params[1];
694                 int hops = atoi(params[2].c_str());
695                 if (hops)
696                 {
697                         this->WriteLine("ERROR :Server too far away for authentication");
698                         return false;
699                 }
700                 std::string description = params[3];
701                 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
702                 {
703                         if ((x->Name == servername) && (x->RecvPass == password))
704                         {
705                                 Srv->SendOpers("*** Verified incoming server connection from \002"+servername+"\002["+this->GetIP()+"] ("+description+")");
706                                 this->InboundServerName = servername;
707                                 this->InboundDescription = description;
708                                 // this is good. Send our details: Our server name and description and hopcount of 0,
709                                 // along with the sendpass from this block.
710                                 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
711                                 // move to the next state, we are now waiting for THEM.
712                                 this->LinkState = WAIT_AUTH_2;
713                                 return true;
714                         }
715                 }
716                 this->WriteLine("ERROR :Invalid credentials");
717                 return false;
718         }
719
720         std::deque<std::string> Split(std::string line, bool stripcolon)
721         {
722                 std::deque<std::string> n;
723                 std::stringstream s(line);
724                 std::string param = "";
725                 n.clear();
726                 int item = 0;
727                 while (!s.eof())
728                 {
729                         s >> param;
730                         if ((param.c_str()[0] == ':') && (item))
731                         {
732                                 char* str = (char*)param.c_str();
733                                 str++;
734                                 param = str;
735                                 std::string append;
736                                 while (!s.eof())
737                                 {
738                                         append = "";
739                                         s >> append;
740                                         if (append != "")
741                                         {
742                                                 param = param + " " + append;
743                                         }
744                                 }
745                         }
746                         item++;
747                         n.push_back(param);
748                 }
749                 return n;
750         }
751
752         bool ProcessLine(std::string line)
753         {
754                 Srv->Log(DEBUG,"inbound-line: '"+line+"'");
755                 std::deque<std::string> params = this->Split(line,true);
756                 std::string command = "";
757                 std::string prefix = "";
758                 if (((params[0].c_str())[0] == ':') && (params.size() > 1))
759                 {
760                         prefix = params[0];
761                         command = params[1];
762                         char* pref = (char*)prefix.c_str();
763                         prefix = ++pref;
764                         params.pop_front();
765                         params.pop_front();
766                 }
767                 else
768                 {
769                         prefix = "";
770                         command = params[0];
771                         params.pop_front();
772                 }
773                 
774                 switch (this->LinkState)
775                 {
776                         TreeServer* Node;
777                         
778                         case WAIT_AUTH_1:
779                                 // Waiting for SERVER command from remote server. Server initiating
780                                 // the connection sends the first SERVER command, listening server
781                                 // replies with theirs if its happy, then if the initiator is happy,
782                                 // it starts to send its net sync, which starts the merge, otherwise
783                                 // it sends an ERROR.
784                                 if (command == "SERVER")
785                                 {
786                                         return this->Inbound_Server(params);
787                                 }
788                                 else if (command == "ERROR")
789                                 {
790                                         return this->Error(params);
791                                 }
792                         break;
793                         case WAIT_AUTH_2:
794                                 // Waiting for start of other side's netmerge to say they liked our
795                                 // password.
796                                 if (command == "SERVER")
797                                 {
798                                         // cant do this, they sent it to us in the WAIT_AUTH_1 state!
799                                         // silently ignore.
800                                         return true;
801                                 }
802                                 else if (command == "BURST")
803                                 {
804                                         this->LinkState = CONNECTED;
805                                         Node = new TreeServer(InboundServerName,InboundDescription,TreeRoot,this);
806                                         TreeRoot->AddChild(Node);
807                                         params.clear();
808                                         params.push_back(InboundServerName);
809                                         params.push_back("*");
810                                         params.push_back("1");
811                                         params.push_back(":"+InboundDescription);
812                                         DoOneToAllButSender(TreeRoot->GetName(),"SERVER",params,InboundServerName);
813                                         this->DoBurst(Node);
814                                 }
815                                 else if (command == "ERROR")
816                                 {
817                                         return this->Error(params);
818                                 }
819                                 
820                         break;
821                         case LISTENER:
822                                 this->WriteLine("ERROR :Internal error -- listening socket accepted its own descriptor!!!");
823                                 return false;
824                         break;
825                         case CONNECTING:
826                                 if (command == "SERVER")
827                                 {
828                                         // another server we connected to, which was in WAIT_AUTH_1 state,
829                                         // has just sent us their credentials. If we get this far, theyre
830                                         // happy with OUR credentials, and they are now in WAIT_AUTH_2 state.
831                                         // if we're happy with this, we should send our netburst which
832                                         // kickstarts the merge.
833                                         return this->Outbound_Reply_Server(params);
834                                 }
835                         break;
836                         case CONNECTED:
837                                 // This is the 'authenticated' state, when all passwords
838                                 // have been exchanged and anything past this point is taken
839                                 // as gospel.
840                                 std::string target = "";
841                                 if ((command == "NICK") && (params.size() > 1))
842                                 {
843                                         return this->IntroduceClient(prefix,params);
844                                 }
845                                 else if (command == "FJOIN")
846                                 {
847                                         return this->ForceJoin(prefix,params);
848                                 }
849                                 else if (command == "SERVER")
850                                 {
851                                         return this->RemoteServer(prefix,params);
852                                 }
853                                 else if (command == "OPERTYPE")
854                                 {
855                                         return this->OperType(prefix,params);
856                                 }
857                                 else if (command == "SQUIT")
858                                 {
859                                         if (params.size() == 2)
860                                         {
861                                                 this->Squit(FindServer(params[0]),params[1]);
862                                         }
863                                         return true;
864                                 }
865                                 else
866                                 {
867                                         // not a special inter-server command.
868                                         // Emulate the actual user doing the command,
869                                         // this saves us having a huge ugly parser.
870                                         userrec* who = Srv->FindNick(prefix);
871                                         std::string sourceserv = this->myhost;
872                                         if (this->InboundServerName != "")
873                                         {
874                                                 sourceserv = this->InboundServerName;
875                                         }
876                                         if (who)
877                                         {
878                                                 // its a user
879                                                 target = who->server;
880                                                 char* strparams[127];
881                                                 for (unsigned int q = 0; q < params.size(); q++)
882                                                 {
883                                                         strparams[q] = (char*)params[q].c_str();
884                                                 }
885                                                 log(DEBUG,"*** CALL COMMAND HANDLER FOR %s, SOURCE: '%s'",command.c_str(),who->nick);
886                                                 Srv->CallCommandHandler(command, strparams, params.size(), who);
887                                         }
888                                         else
889                                         {
890                                                 // its not a user. Its either a server, or somethings screwed up.
891                                                 if (IsServer(prefix))
892                                                 {
893                                                         target = Srv->GetServerName();
894                                                 }
895                                                 else
896                                                 {
897                                                         log(DEBUG,"Command with unknown origin '%s'",prefix.c_str());
898                                                         return true;
899                                                 }
900                                         }
901                                         return DoOneToAllButSenderRaw(line,sourceserv);
902
903                                 }
904                                 return true;
905                         break;  
906                 }
907                 return true;
908         }
909
910         virtual void OnTimeout()
911         {
912                 if (this->LinkState == CONNECTING)
913                 {
914                         Srv->SendOpers("*** CONNECT: Connection to \002"+myhost+"\002 timed out.");
915                 }
916         }
917
918         virtual void OnClose()
919         {
920                 // Connection closed.
921                 // If the connection is fully up (state CONNECTED)
922                 // then propogate a netsplit to all peers.
923                 std::string quitserver = this->myhost;
924                 if (this->InboundServerName != "")
925                 {
926                         quitserver = this->InboundServerName;
927                 }
928                 TreeServer* s = FindServer(quitserver);
929                 if (s)
930                 {
931                         std::deque<std::string> params;
932                         params.push_back(quitserver);
933                         params.push_back(":Remote host closed the connection");
934                         DoOneToAllButSender(Srv->GetServerName(),"SQUIT",params,quitserver);
935                         Squit(s,"Remote host closed the connection");
936                 }
937         }
938
939         virtual int OnIncomingConnection(int newsock, char* ip)
940         {
941                 TreeSocket* s = new TreeSocket(newsock, ip);
942                 Srv->AddSocket(s);
943                 return true;
944         }
945 };
946
947 bool DoOneToAllButSenderRaw(std::string data,std::string omit)
948 {
949         for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
950         {
951                 TreeServer* Route = TreeRoot->GetChild(x);
952                 if ((Route->GetSocket()) && (Route->GetName() != omit) && (BestRouteTo(omit) != Route))
953                 {
954                         TreeSocket* Sock = Route->GetSocket();
955                         log(DEBUG,"Sending RAW to %s",Route->GetName().c_str());
956                         Sock->WriteLine(data);
957                 }
958         }
959         return true;
960 }
961
962 bool DoOneToAllButSender(std::string prefix, std::string command, std::deque<std::string> params, std::string omit)
963 {
964         log(DEBUG,"ALLBUTONE: Comes from %s SHOULD NOT go back to %s",prefix.c_str(),omit.c_str());
965         // TODO: Special stuff with privmsg and notice
966         std::string FullLine = ":" + prefix + " " + command;
967         for (unsigned int x = 0; x < params.size(); x++)
968         {
969                 FullLine = FullLine + " " + params[x];
970         }
971         for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
972         {
973                 TreeServer* Route = TreeRoot->GetChild(x);
974                 // Send the line IF:
975                 // The route has a socket (its a direct connection)
976                 // The route isnt the one to be omitted
977                 // The route isnt the path to the one to be omitted
978                 if ((Route->GetSocket()) && (Route->GetName() != omit) && (BestRouteTo(omit) != Route))
979                 {
980                         TreeSocket* Sock = Route->GetSocket();
981                         log(DEBUG,"Sending to %s",Route->GetName().c_str());
982                         Sock->WriteLine(FullLine);
983                 }
984         }
985         return true;
986 }
987
988 bool DoOneToMany(std::string prefix, std::string command, std::deque<std::string> params)
989 {
990         std::string FullLine = ":" + prefix + " " + command;
991         for (unsigned int x = 0; x < params.size(); x++)
992         {
993                 FullLine = FullLine + " " + params[x];
994         }
995         for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
996         {
997                 TreeServer* Route = TreeRoot->GetChild(x);
998                 if (Route->GetSocket())
999                 {
1000                         TreeSocket* Sock = Route->GetSocket();
1001                         Sock->WriteLine(FullLine);
1002                 }
1003         }
1004         return true;
1005 }
1006
1007 bool DoOneToOne(std::string prefix, std::string command, std::deque<std::string> params, std::string target)
1008 {
1009         TreeServer* Route = BestRouteTo(target);
1010         if (Route)
1011         {
1012                 std::string FullLine = ":" + prefix + " " + command;
1013                 for (unsigned int x = 0; x < params.size(); x++)
1014                 {
1015                         FullLine = FullLine + " " + params[x];
1016                 }
1017                 if (Route->GetSocket())
1018                 {
1019                         TreeSocket* Sock = Route->GetSocket();
1020                         Sock->WriteLine(FullLine);
1021                 }
1022                 return true;
1023         }
1024         else
1025         {
1026                 log(DEBUG,"Could not route message with target %s: %s",target.c_str(),command.c_str());
1027                 return true;
1028         }
1029 }
1030
1031
1032 class ModuleSpanningTree : public Module
1033 {
1034         std::vector<TreeSocket*> Bindings;
1035         int line;
1036
1037  public:
1038
1039         void ReadConfiguration(bool rebind)
1040         {
1041                 if (rebind)
1042                 {
1043                         for (int j =0; j < Conf->Enumerate("bind"); j++)
1044                         {
1045                                 std::string Type = Conf->ReadValue("bind","type",j);
1046                                 std::string IP = Conf->ReadValue("bind","address",j);
1047                                 long Port = Conf->ReadInteger("bind","port",j,true);
1048                                 if (Type == "servers")
1049                                 {
1050                                         if (IP == "*")
1051                                         {
1052                                                 IP = "";
1053                                         }
1054                                         TreeSocket* listener = new TreeSocket(IP.c_str(),Port,true,10);
1055                                         if (listener->GetState() == I_LISTENING)
1056                                         {
1057                                                 Srv->AddSocket(listener);
1058                                                 Bindings.push_back(listener);
1059                                         }
1060                                         else
1061                                         {
1062                                                 log(DEFAULT,"m_spanningtree: Warning: Failed to bind server port %d",Port);
1063                                                 listener->Close();
1064                                                 delete listener;
1065                                         }
1066                                 }
1067                         }
1068                 }
1069                 LinkBlocks.clear();
1070                 for (int j =0; j < Conf->Enumerate("link"); j++)
1071                 {
1072                         Link L;
1073                         L.Name = Conf->ReadValue("link","name",j);
1074                         L.IPAddr = Conf->ReadValue("link","ipaddr",j);
1075                         L.Port = Conf->ReadInteger("link","port",j,true);
1076                         L.SendPass = Conf->ReadValue("link","sendpass",j);
1077                         L.RecvPass = Conf->ReadValue("link","recvpass",j);
1078                         LinkBlocks.push_back(L);
1079                         log(DEBUG,"m_spanningtree: Read server %s with host %s:%d",L.Name.c_str(),L.IPAddr.c_str(),L.Port);
1080                 }
1081         }
1082
1083         ModuleSpanningTree()
1084         {
1085                 Srv = new Server;
1086                 Conf = new ConfigReader;
1087                 Bindings.clear();
1088
1089                 // Create the root of the tree
1090                 TreeRoot = new TreeServer(Srv->GetServerName(),Srv->GetServerDescription());
1091
1092                 ReadConfiguration(true);
1093         }
1094
1095         void ShowLinks(TreeServer* Current, userrec* user, int hops)
1096         {
1097                 std::string Parent = TreeRoot->GetName();
1098                 if (Current->GetParent())
1099                 {
1100                         Parent = Current->GetParent()->GetName();
1101                 }
1102                 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1103                 {
1104                         ShowLinks(Current->GetChild(q),user,hops+1);
1105                 }
1106                 WriteServ(user->fd,"364 %s %s %s :%d %s",user->nick,Current->GetName().c_str(),Parent.c_str(),hops,Current->GetDesc().c_str());
1107         }
1108
1109         void HandleLinks(char** parameters, int pcnt, userrec* user)
1110         {
1111                 ShowLinks(TreeRoot,user,0);
1112                 WriteServ(user->fd,"365 %s * :End of /LINKS list.",user->nick);
1113                 return;
1114         }
1115
1116         void HandleLusers(char** parameters, int pcnt, userrec* user)
1117         {
1118                 return;
1119         }
1120
1121         // WARNING: NOT THREAD SAFE - DONT GET ANY SMART IDEAS.
1122
1123         void ShowMap(TreeServer* Current, userrec* user, int depth, char matrix[128][80])
1124         {
1125                 if (line < 128)
1126                 {
1127                         for (int t = 0; t < depth; t++)
1128                         {
1129                                 matrix[line][t] = ' ';
1130                         }
1131                         strlcpy(&matrix[line][depth],Current->GetName().c_str(),80);
1132                         line++;
1133                         for (unsigned int q = 0; q < Current->ChildCount(); q++)
1134                         {
1135                                 ShowMap(Current->GetChild(q),user,depth+2,matrix);
1136                         }
1137                 }
1138         }
1139
1140         // Ok, prepare to be confused.
1141         // After much mulling over how to approach this, it struck me that
1142         // the 'usual' way of doing a /MAP isnt the best way. Instead of
1143         // keeping track of a ton of ascii characters, and line by line
1144         // under recursion working out where to place them using multiplications
1145         // and divisons, we instead render the map onto a backplane of characters
1146         // (a character matrix), then draw the branches as a series of "L" shapes
1147         // from the nodes. This is not only friendlier on CPU it uses less stack.
1148
1149         void HandleMap(char** parameters, int pcnt, userrec* user)
1150         {
1151                 // This array represents a virtual screen which we will
1152                 // "scratch" draw to, as the console device of an irc
1153                 // client does not provide for a proper terminal.
1154                 char matrix[128][80];
1155                 for (unsigned int t = 0; t < 128; t++)
1156                 {
1157                         matrix[t][0] = '\0';
1158                 }
1159                 line = 0;
1160                 // The only recursive bit is called here.
1161                 ShowMap(TreeRoot,user,0,matrix);
1162                 // Process each line one by one. The algorithm has a limit of
1163                 // 128 servers (which is far more than a spanning tree should have
1164                 // anyway, so we're ok). This limit can be raised simply by making
1165                 // the character matrix deeper, 128 rows taking 10k of memory.
1166                 for (int l = 1; l < line; l++)
1167                 {
1168                         // scan across the line looking for the start of the
1169                         // servername (the recursive part of the algorithm has placed
1170                         // the servers at indented positions depending on what they
1171                         // are related to)
1172                         int first_nonspace = 0;
1173                         while (matrix[l][first_nonspace] == ' ')
1174                         {
1175                                 first_nonspace++;
1176                         }
1177                         first_nonspace--;
1178                         // Draw the `- (corner) section: this may be overwritten by
1179                         // another L shape passing along the same vertical pane, becoming
1180                         // a |- (branch) section instead.
1181                         matrix[l][first_nonspace] = '-';
1182                         matrix[l][first_nonspace-1] = '`';
1183                         int l2 = l - 1;
1184                         // Draw upwards until we hit the parent server, causing possibly
1185                         // other corners (`-) to become branches (|-)
1186                         while ((matrix[l2][first_nonspace-1] == ' ') || (matrix[l2][first_nonspace-1] == '`'))
1187                         {
1188                                 matrix[l2][first_nonspace-1] = '|';
1189                                 l2--;
1190                         }
1191                 }
1192                 // dump the whole lot to the user. This is the easy bit, honest.
1193                 for (int t = 0; t < line; t++)
1194                 {
1195                         WriteServ(user->fd,"006 %s :%s",user->nick,&matrix[t][0]);
1196                 }
1197                 WriteServ(user->fd,"007 %s :End of /MAP",user->nick);
1198                 return;
1199         }
1200
1201         int HandleSquit(char** parameters, int pcnt, userrec* user)
1202         {
1203                 return 1;
1204         }
1205
1206         int HandleConnect(char** parameters, int pcnt, userrec* user)
1207         {
1208                 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
1209                 {
1210                         if (Srv->MatchText(x->Name.c_str(),parameters[0]))
1211                         {
1212                                 WriteServ(user->fd,"NOTICE %s :*** CONNECT: Connecting to server: %s (%s:%d)",user->nick,x->Name.c_str(),x->IPAddr.c_str(),x->Port);
1213                                 TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name);
1214                                 Srv->AddSocket(newsocket);
1215                                 return 1;
1216                         }
1217                 }
1218                 WriteServ(user->fd,"NOTICE %s :*** CONNECT: No matching server could be found in the config file.",user->nick);
1219                 return 1;
1220         }
1221
1222         virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
1223         {
1224                 if (command == "CONNECT")
1225                 {
1226                         return this->HandleConnect(parameters,pcnt,user);
1227                 }
1228                 else if (command == "SQUIT")
1229                 {
1230                         return this->HandleSquit(parameters,pcnt,user);
1231                 }
1232                 else if (command == "MAP")
1233                 {
1234                         this->HandleMap(parameters,pcnt,user);
1235                         return 1;
1236                 }
1237                 else if (command == "LUSERS")
1238                 {
1239                         this->HandleLusers(parameters,pcnt,user);
1240                         return 1;
1241                 }
1242                 else if (command == "LINKS")
1243                 {
1244                         this->HandleLinks(parameters,pcnt,user);
1245                         return 1;
1246                 }
1247                 return 0;
1248         }
1249
1250         virtual void OnUserNotice(userrec* user, void* dest, int target_type, std::string text)
1251         {
1252                 if (target_type == TYPE_USER)
1253                 {
1254                         userrec* d = (userrec*)dest;
1255                         if ((std::string(d->server) != Srv->GetServerName()) && (std::string(user->server) == Srv->GetServerName()))
1256                         {
1257                                 std::deque<std::string> params;
1258                                 params.clear();
1259                                 params.push_back(d->nick);
1260                                 params.push_back(":"+text);
1261                                 DoOneToOne(user->nick,"NOTICE",params,d->server);
1262                         }
1263                 }
1264                 else
1265                 {
1266                         if (std::string(user->server) == Srv->GetServerName())
1267                         {
1268                                 chanrec *c = (chanrec*)dest;
1269                                 std::deque<std::string> params;
1270                                 params.push_back(c->name);
1271                                 params.push_back(":"+text);
1272                                 DoOneToMany(user->nick,"NOTICE",params);
1273                         }
1274                 }
1275         }
1276
1277         virtual void OnUserMessage(userrec* user, void* dest, int target_type, std::string text)
1278         {
1279                 if (target_type == TYPE_USER)
1280                 {
1281                         // route private messages which are targetted at clients only to the server
1282                         // which needs to receive them
1283                         userrec* d = (userrec*)dest;
1284                         if ((std::string(d->server) != Srv->GetServerName()) && (std::string(user->server) == Srv->GetServerName()))
1285                         {
1286                                 std::deque<std::string> params;
1287                                 params.clear();
1288                                 params.push_back(d->nick);
1289                                 params.push_back(":"+text);
1290                                 DoOneToOne(user->nick,"PRIVMSG",params,d->server);
1291                         }
1292                 }
1293                 else
1294                 {
1295                         if (std::string(user->server) == Srv->GetServerName())
1296                         {
1297                                 chanrec *c = (chanrec*)dest;
1298                                 std::deque<std::string> params;
1299                                 params.push_back(c->name);
1300                                 params.push_back(":"+text);
1301                                 DoOneToMany(user->nick,"PRIVMSG",params);
1302                         }
1303                 }
1304         }
1305
1306         virtual void OnUserJoin(userrec* user, chanrec* channel)
1307         {
1308                 // Only do this for local users
1309                 if (std::string(user->server) == Srv->GetServerName())
1310                 {
1311                         log(DEBUG,"**** User on %s JOINS %s",user->server,channel->name);
1312                         std::deque<std::string> params;
1313                         params.clear();
1314                         params.push_back(channel->name);
1315                         if (*channel->key)
1316                         {
1317                                 log(DEBUG,"**** With key %s",channel->key);
1318                                 // if the channel has a key, force the join by emulating the key.
1319                                 params.push_back(channel->key);
1320                         }
1321                         DoOneToMany(user->nick,"JOIN",params);
1322                 }
1323         }
1324
1325         virtual void OnUserPart(userrec* user, chanrec* channel)
1326         {
1327                 if (std::string(user->server) == Srv->GetServerName())
1328                 {
1329                         log(DEBUG,"**** User on %s PARTS %s",user->server,channel->name);
1330                         std::deque<std::string> params;
1331                         params.clear();
1332                         params.push_back(channel->name);
1333                         DoOneToMany(user->nick,"PART",params);
1334                 }
1335         }
1336
1337         virtual void OnUserConnect(userrec* user)
1338         {
1339                 char agestr[MAXBUF];
1340                 if (std::string(user->server) == Srv->GetServerName())
1341                 {
1342                         log(DEBUG,"**** User on %s CONNECTS: %s",user->server,user->nick);
1343                         std::deque<std::string> params;
1344                         snprintf(agestr,MAXBUF,"%lu",(unsigned long)user->age);
1345                         params.clear();
1346                         params.push_back(agestr);
1347                         params.push_back(user->nick);
1348                         params.push_back(user->host);
1349                         params.push_back(user->dhost);
1350                         params.push_back(user->ident);
1351                         params.push_back("+"+std::string(user->modes));
1352                         params.push_back(user->ip);
1353                         params.push_back(":"+std::string(user->fullname));
1354                         DoOneToMany(Srv->GetServerName(),"NICK",params);
1355                 }
1356         }
1357
1358         virtual void OnUserQuit(userrec* user, std::string reason)
1359         {
1360                 if (std::string(user->server) == Srv->GetServerName())
1361                 {
1362                         log(DEBUG,"**** User on %s QUITS: %s",user->server,user->nick);
1363                         std::deque<std::string> params;
1364                         params.push_back(":"+reason);
1365                         DoOneToMany(user->nick,"QUIT",params);
1366                 }
1367         }
1368
1369         virtual void OnUserPostNick(userrec* user, std::string oldnick)
1370         {
1371                 if (std::string(user->server) == Srv->GetServerName())
1372                 {
1373                         log(DEBUG,"**** User on %s changes NICK: %s",user->server,user->nick);
1374                         std::deque<std::string> params;
1375                         params.push_back(user->nick);
1376                         DoOneToMany(oldnick,"NICK",params);
1377                 }
1378         }
1379
1380         // note: the protocol does not allow direct umode +o except
1381         // via NICK with 8 params. sending OPERTYPE infers +o modechange
1382         // locally.
1383         virtual void OnOper(userrec* user, std::string opertype)
1384         {
1385                 if (std::string(user->server) == Srv->GetServerName())
1386                 {
1387                         std::deque<std::string> params;
1388                         params.push_back(opertype);
1389                         DoOneToMany(user->nick,"OPERTYPE",params);
1390                 }
1391         }
1392
1393         virtual void OnMode(userrec* user, void* dest, int target_type, std::string text)
1394         {
1395                 if (std::string(user->server) == Srv->GetServerName())
1396                 {
1397                         if (target_type == TYPE_USER)
1398                         {
1399                                 userrec* u = (userrec*)dest;
1400                                 std::deque<std::string> params;
1401                                 params.push_back(u->nick);
1402                                 params.push_back(text);
1403                                 DoOneToMany(user->nick,"MODE",params);
1404                         }
1405                         else
1406                         {
1407                                 chanrec* c = (chanrec*)dest;
1408                                 std::deque<std::string> params;
1409                                 params.push_back(c->name);
1410                                 params.push_back(text);
1411                                 DoOneToMany(user->nick,"MODE",params);
1412                         }
1413                 }
1414         }
1415
1416         virtual ~ModuleSpanningTree()
1417         {
1418                 delete Srv;
1419         }
1420
1421         virtual Version GetVersion()
1422         {
1423                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
1424         }
1425 };
1426
1427
1428 class ModuleSpanningTreeFactory : public ModuleFactory
1429 {
1430  public:
1431         ModuleSpanningTreeFactory()
1432         {
1433         }
1434         
1435         ~ModuleSpanningTreeFactory()
1436         {
1437         }
1438         
1439         virtual Module * CreateModule()
1440         {
1441                 return new ModuleSpanningTree;
1442         }
1443         
1444 };
1445
1446
1447 extern "C" void * init_module( void )
1448 {
1449         return new ModuleSpanningTreeFactory;
1450 }
1451