]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree.cpp
adc4a2a3807fc914ebb66d69370bde4cf844ef08
[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                 if (*(modes.c_str()) == '+')
494                 {
495                         char* m = (char*)modes.c_str();
496                         m++;
497                         modes = m;
498                 }
499                 std::string ip = params[6];
500                 std::string gecos = params[7];
501                 char* tempnick = (char*)nick.c_str();
502                 log(DEBUG,"Introduce client %s!%s@%s",tempnick,ident.c_str(),host.c_str());
503                 
504                 user_hash::iterator iter;
505                 iter = clientlist.find(tempnick);
506                 if (iter != clientlist.end())
507                 {
508                         // nick collision
509                         log(DEBUG,"Nick collision on %s!%s@%s",tempnick,ident.c_str(),host.c_str());
510                         return true;
511                 }
512                 
513                 clientlist[tempnick] = new userrec();
514                 clientlist[tempnick]->fd = FD_MAGIC_NUMBER;
515                 strlcpy(clientlist[tempnick]->nick, tempnick,NICKMAX);
516                 strlcpy(clientlist[tempnick]->host, host.c_str(),160);
517                 strlcpy(clientlist[tempnick]->dhost, dhost.c_str(),160);
518                 clientlist[tempnick]->server = (char*)FindServerNamePtr(source.c_str());
519                 strlcpy(clientlist[tempnick]->ident, ident.c_str(),IDENTMAX);
520                 strlcpy(clientlist[tempnick]->fullname, gecos.c_str(),MAXGECOS);
521                 clientlist[tempnick]->registered = 7;
522                 clientlist[tempnick]->signon = age;
523                 strlcpy(clientlist[tempnick]->ip,ip.c_str(),16);
524                 for (int i = 0; i < MAXCHANS; i++)
525                 {
526                         clientlist[tempnick]->chans[i].channel = NULL;
527                         clientlist[tempnick]->chans[i].uc_modes = 0;
528                 }
529                 DoOneToAllButSender(source,"NICK",params,source);
530                 return true;
531         }
532
533         // send all users and their channels
534         void SendUsers(TreeServer* Current)
535         {
536                 char data[MAXBUF];
537                 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
538                 {
539                         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);
540                         this->WriteLine(data);
541                         if (strchr(u->second->modes,'o'))
542                         {
543                                 this->WriteLine(":"+std::string(u->second->nick)+" OPERTYPE "+std::string(u->second->oper));
544                         }
545                         char* chl = chlist(u->second,u->second);
546                         if (*chl)
547                         {
548                                 this->WriteLine(":"+std::string(u->second->nick)+" FJOIN "+std::string(chl));
549                         }
550                 }
551         }
552
553         void DoBurst(TreeServer* s)
554         {
555                 log(DEBUG,"Beginning network burst");
556                 Srv->SendOpers("*** Bursting to "+s->GetName()+".");
557                 this->WriteLine("BURST");
558                 // Send server tree
559                 this->SendServers(TreeRoot,s,1);
560                 // Send users and their channels
561                 this->SendUsers(s);
562                 // TODO: Send everything else (channel modes etc)
563                 this->WriteLine("ENDBURST");
564         }
565
566         virtual bool OnDataReady()
567         {
568                 char* data = this->Read();
569                 if (data)
570                 {
571                         this->in_buffer += data;
572                         while (in_buffer.find("\n") != std::string::npos)
573                         {
574                                 char* line = (char*)in_buffer.c_str();
575                                 std::string ret = "";
576                                 while ((*line != '\n') && (strlen(line)))
577                                 {
578                                         ret = ret + *line;
579                                         line++;
580                                 }
581                                 if ((*line == '\n') || (*line == '\r'))
582                                         line++;
583                                 in_buffer = line;
584                                 if (!this->ProcessLine(ret))
585                                 {
586                                         return false;
587                                 }
588                         }
589                 }
590                 return (data != NULL);
591         }
592
593         int WriteLine(std::string line)
594         {
595                 return this->Write(line + "\r\n");
596         }
597
598         bool Error(std::deque<std::string> params)
599         {
600                 if (params.size() < 1)
601                         return false;
602                 std::string Errmsg = params[0];
603                 std::string SName = myhost;
604                 if (InboundServerName != "")
605                 {
606                         SName = InboundServerName;
607                 }
608                 Srv->SendOpers("*** ERROR from "+SName+": "+Errmsg);
609                 // we will return false to cause the socket to close.
610                 return false;
611         }
612
613         bool OperType(std::string prefix, std::deque<std::string> params)
614         {
615                 if (params.size() != 1)
616                         return true;
617                 std::string opertype = params[0];
618                 userrec* u = Srv->FindNick(prefix);
619                 if (u)
620                 {
621                         strlcpy(u->oper,opertype.c_str(),NICKMAX);
622                         if (!strchr(u->modes,'o'))
623                         {
624                                 strcat(u->modes,"o");
625                         }
626                         DoOneToAllButSender(u->server,"OPERTYPE",params,u->server);
627                 }
628                 return true;
629         }
630
631         bool RemoteServer(std::string prefix, std::deque<std::string> params)
632         {
633                 if (params.size() < 4)
634                         return false;
635                 std::string servername = params[0];
636                 std::string password = params[1];
637                 int hops = atoi(params[2].c_str());
638                 std::string description = params[3];
639                 if (!hops)
640                 {
641                         this->WriteLine("ERROR :Protocol error - Introduced remote server with incorrect hopcount!");
642                         return false;
643                 }
644                 TreeServer* ParentOfThis = FindServer(prefix);
645                 if (!ParentOfThis)
646                 {
647                         this->WriteLine("ERROR :Protocol error - Introduced remote server from unknown server "+prefix);
648                         return false;
649                 }
650                 TreeServer* Node = new TreeServer(servername,description,ParentOfThis,NULL);
651                 ParentOfThis->AddChild(Node);
652                 params[3] = ":" + params[3];
653                 DoOneToAllButSender(prefix,"SERVER",params,prefix);
654                 Srv->SendOpers("*** Server \002"+prefix+"\002 introduced server \002"+servername+"\002 ("+description+")");
655                 return true;
656         }
657
658         bool Outbound_Reply_Server(std::deque<std::string> params)
659         {
660                 if (params.size() < 4)
661                         return false;
662                 std::string servername = params[0];
663                 std::string password = params[1];
664                 int hops = atoi(params[2].c_str());
665                 if (hops)
666                 {
667                         this->WriteLine("ERROR :Server too far away for authentication");
668                         return false;
669                 }
670                 std::string description = params[3];
671                 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
672                 {
673                         if ((x->Name == servername) && (x->RecvPass == password))
674                         {
675                                 // Begin the sync here. this kickstarts the
676                                 // other side, waiting in WAIT_AUTH_2 state,
677                                 // into starting their burst, as it shows
678                                 // that we're happy.
679                                 this->LinkState = CONNECTED;
680                                 // we should add the details of this server now
681                                 // to the servers tree, as a child of the root
682                                 // node.
683                                 TreeServer* Node = new TreeServer(servername,description,TreeRoot,this);
684                                 TreeRoot->AddChild(Node);
685                                 DoOneToAllButSender(TreeRoot->GetName(),"SERVER",params,servername);
686                                 this->DoBurst(Node);
687                                 return true;
688                         }
689                 }
690                 this->WriteLine("ERROR :Invalid credentials");
691                 return false;
692         }
693
694         bool Inbound_Server(std::deque<std::string> params)
695         {
696                 if (params.size() < 4)
697                         return false;
698                 std::string servername = params[0];
699                 std::string password = params[1];
700                 int hops = atoi(params[2].c_str());
701                 if (hops)
702                 {
703                         this->WriteLine("ERROR :Server too far away for authentication");
704                         return false;
705                 }
706                 std::string description = params[3];
707                 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
708                 {
709                         if ((x->Name == servername) && (x->RecvPass == password))
710                         {
711                                 Srv->SendOpers("*** Verified incoming server connection from \002"+servername+"\002["+this->GetIP()+"] ("+description+")");
712                                 this->InboundServerName = servername;
713                                 this->InboundDescription = description;
714                                 // this is good. Send our details: Our server name and description and hopcount of 0,
715                                 // along with the sendpass from this block.
716                                 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
717                                 // move to the next state, we are now waiting for THEM.
718                                 this->LinkState = WAIT_AUTH_2;
719                                 return true;
720                         }
721                 }
722                 this->WriteLine("ERROR :Invalid credentials");
723                 return false;
724         }
725
726         std::deque<std::string> Split(std::string line, bool stripcolon)
727         {
728                 std::deque<std::string> n;
729                 std::stringstream s(line);
730                 std::string param = "";
731                 n.clear();
732                 int item = 0;
733                 while (!s.eof())
734                 {
735                         s >> param;
736                         if ((param.c_str()[0] == ':') && (item))
737                         {
738                                 char* str = (char*)param.c_str();
739                                 str++;
740                                 param = str;
741                                 std::string append;
742                                 while (!s.eof())
743                                 {
744                                         append = "";
745                                         s >> append;
746                                         if (append != "")
747                                         {
748                                                 param = param + " " + append;
749                                         }
750                                 }
751                         }
752                         item++;
753                         n.push_back(param);
754                 }
755                 return n;
756         }
757
758         bool ProcessLine(std::string line)
759         {
760                 Srv->Log(DEBUG,"inbound-line: '"+line+"'");
761                 std::deque<std::string> params = this->Split(line,true);
762                 std::string command = "";
763                 std::string prefix = "";
764                 if (((params[0].c_str())[0] == ':') && (params.size() > 1))
765                 {
766                         prefix = params[0];
767                         command = params[1];
768                         char* pref = (char*)prefix.c_str();
769                         prefix = ++pref;
770                         params.pop_front();
771                         params.pop_front();
772                 }
773                 else
774                 {
775                         prefix = "";
776                         command = params[0];
777                         params.pop_front();
778                 }
779                 
780                 switch (this->LinkState)
781                 {
782                         TreeServer* Node;
783                         
784                         case WAIT_AUTH_1:
785                                 // Waiting for SERVER command from remote server. Server initiating
786                                 // the connection sends the first SERVER command, listening server
787                                 // replies with theirs if its happy, then if the initiator is happy,
788                                 // it starts to send its net sync, which starts the merge, otherwise
789                                 // it sends an ERROR.
790                                 if (command == "SERVER")
791                                 {
792                                         return this->Inbound_Server(params);
793                                 }
794                                 else if (command == "ERROR")
795                                 {
796                                         return this->Error(params);
797                                 }
798                         break;
799                         case WAIT_AUTH_2:
800                                 // Waiting for start of other side's netmerge to say they liked our
801                                 // password.
802                                 if (command == "SERVER")
803                                 {
804                                         // cant do this, they sent it to us in the WAIT_AUTH_1 state!
805                                         // silently ignore.
806                                         return true;
807                                 }
808                                 else if (command == "BURST")
809                                 {
810                                         this->LinkState = CONNECTED;
811                                         Node = new TreeServer(InboundServerName,InboundDescription,TreeRoot,this);
812                                         TreeRoot->AddChild(Node);
813                                         params.clear();
814                                         params.push_back(InboundServerName);
815                                         params.push_back("*");
816                                         params.push_back("1");
817                                         params.push_back(":"+InboundDescription);
818                                         DoOneToAllButSender(TreeRoot->GetName(),"SERVER",params,InboundServerName);
819                                         this->DoBurst(Node);
820                                 }
821                                 else if (command == "ERROR")
822                                 {
823                                         return this->Error(params);
824                                 }
825                                 
826                         break;
827                         case LISTENER:
828                                 this->WriteLine("ERROR :Internal error -- listening socket accepted its own descriptor!!!");
829                                 return false;
830                         break;
831                         case CONNECTING:
832                                 if (command == "SERVER")
833                                 {
834                                         // another server we connected to, which was in WAIT_AUTH_1 state,
835                                         // has just sent us their credentials. If we get this far, theyre
836                                         // happy with OUR credentials, and they are now in WAIT_AUTH_2 state.
837                                         // if we're happy with this, we should send our netburst which
838                                         // kickstarts the merge.
839                                         return this->Outbound_Reply_Server(params);
840                                 }
841                         break;
842                         case CONNECTED:
843                                 // This is the 'authenticated' state, when all passwords
844                                 // have been exchanged and anything past this point is taken
845                                 // as gospel.
846                                 std::string target = "";
847                                 if ((command == "NICK") && (params.size() > 1))
848                                 {
849                                         return this->IntroduceClient(prefix,params);
850                                 }
851                                 else if (command == "FJOIN")
852                                 {
853                                         return this->ForceJoin(prefix,params);
854                                 }
855                                 else if (command == "SERVER")
856                                 {
857                                         return this->RemoteServer(prefix,params);
858                                 }
859                                 else if (command == "OPERTYPE")
860                                 {
861                                         return this->OperType(prefix,params);
862                                 }
863                                 else if (command == "SQUIT")
864                                 {
865                                         if (params.size() == 2)
866                                         {
867                                                 this->Squit(FindServer(params[0]),params[1]);
868                                         }
869                                         return true;
870                                 }
871                                 else
872                                 {
873                                         // not a special inter-server command.
874                                         // Emulate the actual user doing the command,
875                                         // this saves us having a huge ugly parser.
876                                         userrec* who = Srv->FindNick(prefix);
877                                         std::string sourceserv = this->myhost;
878                                         if (this->InboundServerName != "")
879                                         {
880                                                 sourceserv = this->InboundServerName;
881                                         }
882                                         if (who)
883                                         {
884                                                 // its a user
885                                                 target = who->server;
886                                                 char* strparams[127];
887                                                 for (unsigned int q = 0; q < params.size(); q++)
888                                                 {
889                                                         strparams[q] = (char*)params[q].c_str();
890                                                 }
891                                                 log(DEBUG,"*** CALL COMMAND HANDLER FOR %s, SOURCE: '%s'",command.c_str(),who->nick);
892                                                 Srv->CallCommandHandler(command, strparams, params.size(), who);
893                                         }
894                                         else
895                                         {
896                                                 // its not a user. Its either a server, or somethings screwed up.
897                                                 if (IsServer(prefix))
898                                                 {
899                                                         target = Srv->GetServerName();
900                                                 }
901                                                 else
902                                                 {
903                                                         log(DEBUG,"Command with unknown origin '%s'",prefix.c_str());
904                                                         return true;
905                                                 }
906                                         }
907                                         return DoOneToAllButSenderRaw(line,sourceserv);
908
909                                 }
910                                 return true;
911                         break;  
912                 }
913                 return true;
914         }
915
916         virtual void OnTimeout()
917         {
918                 if (this->LinkState == CONNECTING)
919                 {
920                         Srv->SendOpers("*** CONNECT: Connection to \002"+myhost+"\002 timed out.");
921                 }
922         }
923
924         virtual void OnClose()
925         {
926                 // Connection closed.
927                 // If the connection is fully up (state CONNECTED)
928                 // then propogate a netsplit to all peers.
929                 std::string quitserver = this->myhost;
930                 if (this->InboundServerName != "")
931                 {
932                         quitserver = this->InboundServerName;
933                 }
934                 TreeServer* s = FindServer(quitserver);
935                 if (s)
936                 {
937                         std::deque<std::string> params;
938                         params.push_back(quitserver);
939                         params.push_back(":Remote host closed the connection");
940                         DoOneToAllButSender(Srv->GetServerName(),"SQUIT",params,quitserver);
941                         Squit(s,"Remote host closed the connection");
942                 }
943         }
944
945         virtual int OnIncomingConnection(int newsock, char* ip)
946         {
947                 TreeSocket* s = new TreeSocket(newsock, ip);
948                 Srv->AddSocket(s);
949                 return true;
950         }
951 };
952
953 bool DoOneToAllButSenderRaw(std::string data,std::string omit)
954 {
955         for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
956         {
957                 TreeServer* Route = TreeRoot->GetChild(x);
958                 if ((Route->GetSocket()) && (Route->GetName() != omit) && (BestRouteTo(omit) != Route))
959                 {
960                         TreeSocket* Sock = Route->GetSocket();
961                         log(DEBUG,"Sending RAW to %s",Route->GetName().c_str());
962                         Sock->WriteLine(data);
963                 }
964         }
965         return true;
966 }
967
968 bool DoOneToAllButSender(std::string prefix, std::string command, std::deque<std::string> params, std::string omit)
969 {
970         log(DEBUG,"ALLBUTONE: Comes from %s SHOULD NOT go back to %s",prefix.c_str(),omit.c_str());
971         // TODO: Special stuff with privmsg and notice
972         std::string FullLine = ":" + prefix + " " + command;
973         for (unsigned int x = 0; x < params.size(); x++)
974         {
975                 FullLine = FullLine + " " + params[x];
976         }
977         for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
978         {
979                 TreeServer* Route = TreeRoot->GetChild(x);
980                 // Send the line IF:
981                 // The route has a socket (its a direct connection)
982                 // The route isnt the one to be omitted
983                 // The route isnt the path to the one to be omitted
984                 if ((Route->GetSocket()) && (Route->GetName() != omit) && (BestRouteTo(omit) != Route))
985                 {
986                         TreeSocket* Sock = Route->GetSocket();
987                         log(DEBUG,"Sending to %s",Route->GetName().c_str());
988                         Sock->WriteLine(FullLine);
989                 }
990         }
991         return true;
992 }
993
994 bool DoOneToMany(std::string prefix, std::string command, std::deque<std::string> params)
995 {
996         std::string FullLine = ":" + prefix + " " + command;
997         for (unsigned int x = 0; x < params.size(); x++)
998         {
999                 FullLine = FullLine + " " + params[x];
1000         }
1001         for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1002         {
1003                 TreeServer* Route = TreeRoot->GetChild(x);
1004                 if (Route->GetSocket())
1005                 {
1006                         TreeSocket* Sock = Route->GetSocket();
1007                         Sock->WriteLine(FullLine);
1008                 }
1009         }
1010         return true;
1011 }
1012
1013 bool DoOneToOne(std::string prefix, std::string command, std::deque<std::string> params, std::string target)
1014 {
1015         TreeServer* Route = BestRouteTo(target);
1016         if (Route)
1017         {
1018                 std::string FullLine = ":" + prefix + " " + command;
1019                 for (unsigned int x = 0; x < params.size(); x++)
1020                 {
1021                         FullLine = FullLine + " " + params[x];
1022                 }
1023                 if (Route->GetSocket())
1024                 {
1025                         TreeSocket* Sock = Route->GetSocket();
1026                         Sock->WriteLine(FullLine);
1027                 }
1028                 return true;
1029         }
1030         else
1031         {
1032                 log(DEBUG,"Could not route message with target %s: %s",target.c_str(),command.c_str());
1033                 return true;
1034         }
1035 }
1036
1037
1038 class ModuleSpanningTree : public Module
1039 {
1040         std::vector<TreeSocket*> Bindings;
1041         int line;
1042
1043  public:
1044
1045         void ReadConfiguration(bool rebind)
1046         {
1047                 if (rebind)
1048                 {
1049                         for (int j =0; j < Conf->Enumerate("bind"); j++)
1050                         {
1051                                 std::string Type = Conf->ReadValue("bind","type",j);
1052                                 std::string IP = Conf->ReadValue("bind","address",j);
1053                                 long Port = Conf->ReadInteger("bind","port",j,true);
1054                                 if (Type == "servers")
1055                                 {
1056                                         if (IP == "*")
1057                                         {
1058                                                 IP = "";
1059                                         }
1060                                         TreeSocket* listener = new TreeSocket(IP.c_str(),Port,true,10);
1061                                         if (listener->GetState() == I_LISTENING)
1062                                         {
1063                                                 Srv->AddSocket(listener);
1064                                                 Bindings.push_back(listener);
1065                                         }
1066                                         else
1067                                         {
1068                                                 log(DEFAULT,"m_spanningtree: Warning: Failed to bind server port %d",Port);
1069                                                 listener->Close();
1070                                                 delete listener;
1071                                         }
1072                                 }
1073                         }
1074                 }
1075                 LinkBlocks.clear();
1076                 for (int j =0; j < Conf->Enumerate("link"); j++)
1077                 {
1078                         Link L;
1079                         L.Name = Conf->ReadValue("link","name",j);
1080                         L.IPAddr = Conf->ReadValue("link","ipaddr",j);
1081                         L.Port = Conf->ReadInteger("link","port",j,true);
1082                         L.SendPass = Conf->ReadValue("link","sendpass",j);
1083                         L.RecvPass = Conf->ReadValue("link","recvpass",j);
1084                         LinkBlocks.push_back(L);
1085                         log(DEBUG,"m_spanningtree: Read server %s with host %s:%d",L.Name.c_str(),L.IPAddr.c_str(),L.Port);
1086                 }
1087         }
1088
1089         ModuleSpanningTree()
1090         {
1091                 Srv = new Server;
1092                 Conf = new ConfigReader;
1093                 Bindings.clear();
1094
1095                 // Create the root of the tree
1096                 TreeRoot = new TreeServer(Srv->GetServerName(),Srv->GetServerDescription());
1097
1098                 ReadConfiguration(true);
1099         }
1100
1101         void ShowLinks(TreeServer* Current, userrec* user, int hops)
1102         {
1103                 std::string Parent = TreeRoot->GetName();
1104                 if (Current->GetParent())
1105                 {
1106                         Parent = Current->GetParent()->GetName();
1107                 }
1108                 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1109                 {
1110                         ShowLinks(Current->GetChild(q),user,hops+1);
1111                 }
1112                 WriteServ(user->fd,"364 %s %s %s :%d %s",user->nick,Current->GetName().c_str(),Parent.c_str(),hops,Current->GetDesc().c_str());
1113         }
1114
1115         void HandleLinks(char** parameters, int pcnt, userrec* user)
1116         {
1117                 ShowLinks(TreeRoot,user,0);
1118                 WriteServ(user->fd,"365 %s * :End of /LINKS list.",user->nick);
1119                 return;
1120         }
1121
1122         void HandleLusers(char** parameters, int pcnt, userrec* user)
1123         {
1124                 return;
1125         }
1126
1127         // WARNING: NOT THREAD SAFE - DONT GET ANY SMART IDEAS.
1128
1129         void ShowMap(TreeServer* Current, userrec* user, int depth, char matrix[128][80])
1130         {
1131                 if (line < 128)
1132                 {
1133                         for (int t = 0; t < depth; t++)
1134                         {
1135                                 matrix[line][t] = ' ';
1136                         }
1137                         strlcpy(&matrix[line][depth],Current->GetName().c_str(),80);
1138                         line++;
1139                         for (unsigned int q = 0; q < Current->ChildCount(); q++)
1140                         {
1141                                 ShowMap(Current->GetChild(q),user,depth+2,matrix);
1142                         }
1143                 }
1144         }
1145
1146         // Ok, prepare to be confused.
1147         // After much mulling over how to approach this, it struck me that
1148         // the 'usual' way of doing a /MAP isnt the best way. Instead of
1149         // keeping track of a ton of ascii characters, and line by line
1150         // under recursion working out where to place them using multiplications
1151         // and divisons, we instead render the map onto a backplane of characters
1152         // (a character matrix), then draw the branches as a series of "L" shapes
1153         // from the nodes. This is not only friendlier on CPU it uses less stack.
1154
1155         void HandleMap(char** parameters, int pcnt, userrec* user)
1156         {
1157                 // This array represents a virtual screen which we will
1158                 // "scratch" draw to, as the console device of an irc
1159                 // client does not provide for a proper terminal.
1160                 char matrix[128][80];
1161                 for (unsigned int t = 0; t < 128; t++)
1162                 {
1163                         matrix[t][0] = '\0';
1164                 }
1165                 line = 0;
1166                 // The only recursive bit is called here.
1167                 ShowMap(TreeRoot,user,0,matrix);
1168                 // Process each line one by one. The algorithm has a limit of
1169                 // 128 servers (which is far more than a spanning tree should have
1170                 // anyway, so we're ok). This limit can be raised simply by making
1171                 // the character matrix deeper, 128 rows taking 10k of memory.
1172                 for (int l = 1; l < line; l++)
1173                 {
1174                         // scan across the line looking for the start of the
1175                         // servername (the recursive part of the algorithm has placed
1176                         // the servers at indented positions depending on what they
1177                         // are related to)
1178                         int first_nonspace = 0;
1179                         while (matrix[l][first_nonspace] == ' ')
1180                         {
1181                                 first_nonspace++;
1182                         }
1183                         first_nonspace--;
1184                         // Draw the `- (corner) section: this may be overwritten by
1185                         // another L shape passing along the same vertical pane, becoming
1186                         // a |- (branch) section instead.
1187                         matrix[l][first_nonspace] = '-';
1188                         matrix[l][first_nonspace-1] = '`';
1189                         int l2 = l - 1;
1190                         // Draw upwards until we hit the parent server, causing possibly
1191                         // other corners (`-) to become branches (|-)
1192                         while ((matrix[l2][first_nonspace-1] == ' ') || (matrix[l2][first_nonspace-1] == '`'))
1193                         {
1194                                 matrix[l2][first_nonspace-1] = '|';
1195                                 l2--;
1196                         }
1197                 }
1198                 // dump the whole lot to the user. This is the easy bit, honest.
1199                 for (int t = 0; t < line; t++)
1200                 {
1201                         WriteServ(user->fd,"006 %s :%s",user->nick,&matrix[t][0]);
1202                 }
1203                 WriteServ(user->fd,"007 %s :End of /MAP",user->nick);
1204                 return;
1205         }
1206
1207         int HandleSquit(char** parameters, int pcnt, userrec* user)
1208         {
1209                 return 1;
1210         }
1211
1212         int HandleConnect(char** parameters, int pcnt, userrec* user)
1213         {
1214                 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
1215                 {
1216                         if (Srv->MatchText(x->Name.c_str(),parameters[0]))
1217                         {
1218                                 WriteServ(user->fd,"NOTICE %s :*** CONNECT: Connecting to server: %s (%s:%d)",user->nick,x->Name.c_str(),x->IPAddr.c_str(),x->Port);
1219                                 TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name);
1220                                 Srv->AddSocket(newsocket);
1221                                 return 1;
1222                         }
1223                 }
1224                 WriteServ(user->fd,"NOTICE %s :*** CONNECT: No matching server could be found in the config file.",user->nick);
1225                 return 1;
1226         }
1227
1228         virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
1229         {
1230                 if (command == "CONNECT")
1231                 {
1232                         return this->HandleConnect(parameters,pcnt,user);
1233                 }
1234                 else if (command == "SQUIT")
1235                 {
1236                         return this->HandleSquit(parameters,pcnt,user);
1237                 }
1238                 else if (command == "MAP")
1239                 {
1240                         this->HandleMap(parameters,pcnt,user);
1241                         return 1;
1242                 }
1243                 else if (command == "LUSERS")
1244                 {
1245                         this->HandleLusers(parameters,pcnt,user);
1246                         return 1;
1247                 }
1248                 else if (command == "LINKS")
1249                 {
1250                         this->HandleLinks(parameters,pcnt,user);
1251                         return 1;
1252                 }
1253                 return 0;
1254         }
1255
1256         virtual void OnUserNotice(userrec* user, void* dest, int target_type, std::string text)
1257         {
1258                 if (target_type == TYPE_USER)
1259                 {
1260                         userrec* d = (userrec*)dest;
1261                         if ((std::string(d->server) != Srv->GetServerName()) && (std::string(user->server) == Srv->GetServerName()))
1262                         {
1263                                 std::deque<std::string> params;
1264                                 params.clear();
1265                                 params.push_back(d->nick);
1266                                 params.push_back(":"+text);
1267                                 DoOneToOne(user->nick,"NOTICE",params,d->server);
1268                         }
1269                 }
1270                 else
1271                 {
1272                         if (std::string(user->server) == Srv->GetServerName())
1273                         {
1274                                 chanrec *c = (chanrec*)dest;
1275                                 std::deque<std::string> params;
1276                                 params.push_back(c->name);
1277                                 params.push_back(":"+text);
1278                                 DoOneToMany(user->nick,"NOTICE",params);
1279                         }
1280                 }
1281         }
1282
1283         virtual void OnUserMessage(userrec* user, void* dest, int target_type, std::string text)
1284         {
1285                 if (target_type == TYPE_USER)
1286                 {
1287                         // route private messages which are targetted at clients only to the server
1288                         // which needs to receive them
1289                         userrec* d = (userrec*)dest;
1290                         if ((std::string(d->server) != Srv->GetServerName()) && (std::string(user->server) == Srv->GetServerName()))
1291                         {
1292                                 std::deque<std::string> params;
1293                                 params.clear();
1294                                 params.push_back(d->nick);
1295                                 params.push_back(":"+text);
1296                                 DoOneToOne(user->nick,"PRIVMSG",params,d->server);
1297                         }
1298                 }
1299                 else
1300                 {
1301                         if (std::string(user->server) == Srv->GetServerName())
1302                         {
1303                                 chanrec *c = (chanrec*)dest;
1304                                 std::deque<std::string> params;
1305                                 params.push_back(c->name);
1306                                 params.push_back(":"+text);
1307                                 DoOneToMany(user->nick,"PRIVMSG",params);
1308                         }
1309                 }
1310         }
1311
1312         virtual void OnUserJoin(userrec* user, chanrec* channel)
1313         {
1314                 // Only do this for local users
1315                 if (std::string(user->server) == Srv->GetServerName())
1316                 {
1317                         log(DEBUG,"**** User on %s JOINS %s",user->server,channel->name);
1318                         std::deque<std::string> params;
1319                         params.clear();
1320                         params.push_back(channel->name);
1321                         if (*channel->key)
1322                         {
1323                                 log(DEBUG,"**** With key %s",channel->key);
1324                                 // if the channel has a key, force the join by emulating the key.
1325                                 params.push_back(channel->key);
1326                         }
1327                         DoOneToMany(user->nick,"JOIN",params);
1328                 }
1329         }
1330
1331         virtual void OnUserPart(userrec* user, chanrec* channel)
1332         {
1333                 if (std::string(user->server) == Srv->GetServerName())
1334                 {
1335                         log(DEBUG,"**** User on %s PARTS %s",user->server,channel->name);
1336                         std::deque<std::string> params;
1337                         params.clear();
1338                         params.push_back(channel->name);
1339                         DoOneToMany(user->nick,"PART",params);
1340                 }
1341         }
1342
1343         virtual void OnUserConnect(userrec* user)
1344         {
1345                 char agestr[MAXBUF];
1346                 if (std::string(user->server) == Srv->GetServerName())
1347                 {
1348                         log(DEBUG,"**** User on %s CONNECTS: %s",user->server,user->nick);
1349                         std::deque<std::string> params;
1350                         snprintf(agestr,MAXBUF,"%lu",(unsigned long)user->age);
1351                         params.clear();
1352                         params.push_back(agestr);
1353                         params.push_back(user->nick);
1354                         params.push_back(user->host);
1355                         params.push_back(user->dhost);
1356                         params.push_back(user->ident);
1357                         params.push_back("+"+std::string(user->modes));
1358                         params.push_back(user->ip);
1359                         params.push_back(":"+std::string(user->fullname));
1360                         DoOneToMany(Srv->GetServerName(),"NICK",params);
1361                 }
1362         }
1363
1364         virtual void OnUserQuit(userrec* user, std::string reason)
1365         {
1366                 if (std::string(user->server) == Srv->GetServerName())
1367                 {
1368                         log(DEBUG,"**** User on %s QUITS: %s",user->server,user->nick);
1369                         std::deque<std::string> params;
1370                         params.push_back(":"+reason);
1371                         DoOneToMany(user->nick,"QUIT",params);
1372                 }
1373         }
1374
1375         virtual void OnUserPostNick(userrec* user, std::string oldnick)
1376         {
1377                 if (std::string(user->server) == Srv->GetServerName())
1378                 {
1379                         log(DEBUG,"**** User on %s changes NICK: %s",user->server,user->nick);
1380                         std::deque<std::string> params;
1381                         params.push_back(user->nick);
1382                         DoOneToMany(oldnick,"NICK",params);
1383                 }
1384         }
1385
1386         // note: the protocol does not allow direct umode +o except
1387         // via NICK with 8 params. sending OPERTYPE infers +o modechange
1388         // locally.
1389         virtual void OnOper(userrec* user, std::string opertype)
1390         {
1391                 if (std::string(user->server) == Srv->GetServerName())
1392                 {
1393                         std::deque<std::string> params;
1394                         params.push_back(opertype);
1395                         DoOneToMany(user->nick,"OPERTYPE",params);
1396                 }
1397         }
1398
1399         virtual void OnMode(userrec* user, void* dest, int target_type, std::string text)
1400         {
1401                 log(DEBUG,"*** ONMODE TRIGGER");
1402                 if (std::string(user->server) == Srv->GetServerName())
1403                 {
1404                         log(DEBUG,"*** LOCAL");
1405                         if (target_type == TYPE_USER)
1406                         {
1407                                 userrec* u = (userrec*)dest;
1408                                 std::deque<std::string> params;
1409                                 params.push_back(u->nick);
1410                                 params.push_back(text);
1411                                 DoOneToMany(user->nick,"MODE",params);
1412                         }
1413                         else
1414                         {
1415                                 chanrec* c = (chanrec*)dest;
1416                                 std::deque<std::string> params;
1417                                 params.push_back(c->name);
1418                                 params.push_back(text);
1419                                 DoOneToMany(user->nick,"MODE",params);
1420                         }
1421                 }
1422         }
1423
1424         virtual ~ModuleSpanningTree()
1425         {
1426                 delete Srv;
1427         }
1428
1429         virtual Version GetVersion()
1430         {
1431                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
1432         }
1433 };
1434
1435
1436 class ModuleSpanningTreeFactory : public ModuleFactory
1437 {
1438  public:
1439         ModuleSpanningTreeFactory()
1440         {
1441         }
1442         
1443         ~ModuleSpanningTreeFactory()
1444         {
1445         }
1446         
1447         virtual Module * CreateModule()
1448         {
1449                 return new ModuleSpanningTree;
1450         }
1451         
1452 };
1453
1454
1455 extern "C" void * init_module( void )
1456 {
1457         return new ModuleSpanningTreeFactory;
1458 }
1459