1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2005 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
23 #include "inspircd_config.h"
25 #include <ext/hash_map>
33 #include "helperfuncs.h"
35 #include "inspstring.h"
40 #define nspace __gnu_cxx
45 enum ServerState { LISTENER, CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED };
47 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, irc::StrHashComp> user_hash;
48 extern user_hash clientlist;
50 const char* OneToEither[] = { "PRIVMSG", "NOTICE", NULL };
51 const char* OneToMany[] = { "NICK", "QUIT", "JOIN", "PART", "MODE", "INVITE", "KICK", "KILL", NULL };
52 const char* OneToOne[] = { "REHASH", "DIE", "TRACE", "WHOIS", NULL };
57 bool DoOneToOne(std::string prefix, std::string command, std::deque<std::string> params, std::string target);
58 bool DoOneToAllButSender(std::string prefix, std::string command, std::deque<std::string> params, std::string omit);
59 bool DoOneToMany(std::string prefix, std::string command, std::deque<std::string> params);
64 std::vector<TreeServer*> Children;
65 std::string ServerName;
66 std::string ServerDesc;
67 std::string VersionString;
70 TreeSocket* Socket; // for directly connected servers this points at the socket object
80 UserCount = OperCount = 0;
83 TreeServer(std::string Name, std::string Desc) : ServerName(Name), ServerDesc(Desc)
87 UserCount = OperCount = 0;
90 TreeServer(std::string Name, std::string Desc, TreeServer* Above, TreeSocket* Sock) : Parent(Above), ServerName(Name), ServerDesc(Desc), Socket(Sock)
93 UserCount = OperCount = 0;
98 return this->ServerName;
101 std::string GetDesc()
103 return this->ServerDesc;
106 std::string GetVersion()
108 return this->VersionString;
113 return this->UserCount;
118 return this->OperCount;
121 TreeSocket* GetSocket()
126 TreeServer* GetParent()
131 unsigned int ChildCount()
133 return Children.size();
136 TreeServer* GetChild(unsigned int n)
138 if (n < Children.size())
148 void AddChild(TreeServer* Child)
150 Children.push_back(Child);
153 bool DelChild(TreeServer* Child)
155 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
173 std::string SendPass;
174 std::string RecvPass;
177 /* $ModDesc: Povides a spanning tree server link protocol */
181 TreeServer *TreeRoot;
182 std::vector<Link> LinkBlocks;
184 TreeServer* RouteEnumerate(TreeServer* Current, std::string ServerName)
186 if (Current->GetName() == ServerName)
188 for (unsigned int q = 0; q < Current->ChildCount(); q++)
190 TreeServer* found = RouteEnumerate(Current->GetChild(q),ServerName);
199 // Returns the locally connected server we must route a
200 // message through to reach server 'ServerName'. This
201 // only applies to one-to-one and not one-to-many routing.
202 TreeServer* BestRouteTo(std::string ServerName)
204 log(DEBUG,"Finding best route to %s",ServerName.c_str());
205 // first, find the server by recursively walking the tree
206 TreeServer* Found = RouteEnumerate(TreeRoot,ServerName);
207 // did we find it? If not, they did something wrong, abort.
210 log(DEBUG,"Failed to find %s by walking tree!",ServerName.c_str());
215 // The server exists, follow its parent nodes until
216 // the parent of the current is 'TreeRoot', we know
217 // then that this is a directly-connected server.
218 while ((Found) && (Found->GetParent() != TreeRoot))
220 Found = Found->GetParent();
222 log(DEBUG,"Route to %s is via %s",ServerName.c_str(),Found->GetName().c_str());
227 bool LookForServer(TreeServer* Current, std::string ServerName)
229 if (ServerName == Current->GetName())
231 for (unsigned int q = 0; q < Current->ChildCount(); q++)
233 if (LookForServer(Current->GetChild(q),ServerName))
241 void RFindServer(TreeServer* Current, std::string ServerName)
243 if ((ServerName == Current->GetName()) && (!Found))
246 log(DEBUG,"Found server %s desc %s",Current->GetName().c_str(),Current->GetDesc().c_str());
251 for (unsigned int q = 0; q < Current->ChildCount(); q++)
254 RFindServer(Current->GetChild(q),ServerName);
260 TreeServer* FindServer(std::string ServerName)
263 RFindServer(TreeRoot,ServerName);
267 bool IsServer(std::string ServerName)
269 return LookForServer(TreeRoot,ServerName);
272 class TreeSocket : public InspSocket
275 std::string in_buffer;
276 ServerState LinkState;
277 std::string InboundServerName;
278 std::string InboundDescription;
282 TreeSocket(std::string host, int port, bool listening, unsigned long maxtime)
283 : InspSocket(host, port, listening, maxtime)
285 Srv->Log(DEBUG,"Create new listening");
287 this->LinkState = LISTENER;
290 TreeSocket(std::string host, int port, bool listening, unsigned long maxtime, std::string ServerName)
291 : InspSocket(host, port, listening, maxtime)
293 Srv->Log(DEBUG,"Create new outbound");
295 this->LinkState = CONNECTING;
298 TreeSocket(int newfd, char* ip)
299 : InspSocket(newfd, ip)
301 Srv->Log(DEBUG,"Associate new inbound");
302 this->LinkState = WAIT_AUTH_1;
305 virtual bool OnConnected()
307 if (this->LinkState == CONNECTING)
309 Srv->SendOpers("*** Connection to "+myhost+"["+this->GetIP()+"] established.");
310 // we should send our details here.
311 // if the other side is satisfied, they send theirs.
312 // we do not need to change state here.
313 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
315 if (x->Name == this->myhost)
317 // found who we're supposed to be connecting to, send the neccessary gubbins.
318 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
323 log(DEBUG,"Outbound connection ERROR: Could not find the right link block!");
327 virtual void OnError(InspSocketError e)
331 virtual int OnDisconnect()
336 // recursively send the server tree with distances as hops
337 void SendServers(TreeServer* Current, TreeServer* s, int hops)
340 for (unsigned int q = 0; q < Current->ChildCount(); q++)
342 TreeServer* recursive_server = Current->GetChild(q);
343 if (recursive_server != s)
345 // :source.server SERVER server.name hops :Description
346 snprintf(command,1024,":%s SERVER %s * %d :%s",Current->GetName().c_str(),recursive_server->GetName().c_str(),hops,recursive_server->GetDesc().c_str());
347 this->WriteLine(command);
348 // down to next level
349 this->SendServers(recursive_server, s, hops+1);
354 bool ForceJoin(std::string source, std::deque<std::string> params)
356 if (params.size() < 1)
358 for (unsigned int channelnum = 0; channelnum < params.size(); channelnum++)
360 // process one channel at a time, applying modes.
361 char* channel = (char*)params[channelnum].c_str();
362 char permissions = *channel;
379 userrec* who = Srv->FindNick(source);
383 chanrec* chan = Srv->FindChannel(channel);
384 if ((chan) && (*chan->key))
388 Srv->JoinUserToChannel(who,channel,key);
392 modelist[0] = channel;
394 modelist[2] = who->nick;
395 Srv->SendMode(modelist,3,who);
397 DoOneToAllButSender(source,"FJOIN",params,who->server);
403 bool IntroduceClient(std::string source, std::deque<std::string> params)
405 if (params.size() < 8)
407 // NICK age nick host dhost ident +modes ip :gecos
409 std::string nick = params[1];
410 std::string host = params[2];
411 std::string dhost = params[3];
412 std::string ident = params[4];
413 time_t age = atoi(params[0].c_str());
414 std::string modes = params[5];
415 std::string ip = params[6];
416 std::string gecos = params[7];
417 char* tempnick = (char*)nick.c_str();
418 log(DEBUG,"Introduce client %s!%s@%s",tempnick,ident.c_str(),host.c_str());
420 user_hash::iterator iter;
421 iter = clientlist.find(tempnick);
422 if (iter != clientlist.end())
425 log(DEBUG,"Nick collision on %s!%s@%s",tempnick,ident.c_str(),host.c_str());
429 clientlist[tempnick] = new userrec();
430 clientlist[tempnick]->fd = FD_MAGIC_NUMBER;
431 strlcpy(clientlist[tempnick]->nick, tempnick,NICKMAX);
432 strlcpy(clientlist[tempnick]->host, host.c_str(),160);
433 strlcpy(clientlist[tempnick]->dhost, dhost.c_str(),160);
434 clientlist[tempnick]->server = (char*)FindServerNamePtr(source.c_str());
435 strlcpy(clientlist[tempnick]->ident, ident.c_str(),IDENTMAX);
436 strlcpy(clientlist[tempnick]->fullname, gecos.c_str(),MAXGECOS);
437 clientlist[tempnick]->registered = 7;
438 clientlist[tempnick]->signon = age;
439 strlcpy(clientlist[tempnick]->ip,ip.c_str(),16);
440 for (int i = 0; i < MAXCHANS; i++)
442 clientlist[tempnick]->chans[i].channel = NULL;
443 clientlist[tempnick]->chans[i].uc_modes = 0;
445 DoOneToAllButSender(source,"NICK",params,source);
449 // send all users and their channels
450 void SendUsers(TreeServer* Current)
453 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
455 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);
456 this->WriteLine(data);
457 if (strchr(u->second->modes,'o'))
459 this->WriteLine(":"+std::string(u->second->nick)+" OPERTYPE "+std::string(u->second->oper));
461 char* chl = chlist(u->second,u->second);
464 this->WriteLine(":"+std::string(u->second->nick)+" FJOIN "+std::string(chl));
469 void DoBurst(TreeServer* s)
471 log(DEBUG,"Beginning network burst");
472 Srv->SendOpers("*** Bursting to "+s->GetName()+".");
473 this->WriteLine("BURST");
475 this->SendServers(TreeRoot,s,1);
476 // Send users and their channels
478 // TODO: Send everything else (channel modes etc)
479 this->WriteLine("ENDBURST");
482 virtual bool OnDataReady()
484 char* data = this->Read();
487 this->in_buffer += data;
488 while (in_buffer.find("\n") != std::string::npos)
490 char* line = (char*)in_buffer.c_str();
491 std::string ret = "";
492 while ((*line != '\n') && (strlen(line)))
497 if ((*line == '\n') || (*line == '\r'))
500 if (!this->ProcessLine(ret))
506 return (data != NULL);
509 int WriteLine(std::string line)
511 return this->Write(line + "\r\n");
514 bool Error(std::deque<std::string> params)
516 if (params.size() < 1)
518 std::string Errmsg = params[0];
519 std::string SName = myhost;
520 if (InboundServerName != "")
522 SName = InboundServerName;
524 Srv->SendOpers("*** ERROR from "+SName+": "+Errmsg);
525 // we will return false to cause the socket to close.
529 bool RemoteServer(std::string prefix, std::deque<std::string> params)
531 if (params.size() < 4)
533 std::string servername = params[0];
534 std::string password = params[1];
535 int hops = atoi(params[2].c_str());
536 std::string description = params[3];
539 this->WriteLine("ERROR :Protocol error - Introduced remote server with incorrect hopcount!");
542 TreeServer* ParentOfThis = FindServer(prefix);
545 this->WriteLine("ERROR :Protocol error - Introduced remote server from unknown server "+prefix);
548 TreeServer* Node = new TreeServer(servername,description,ParentOfThis,NULL);
549 ParentOfThis->AddChild(Node);
550 DoOneToAllButSender(prefix,"SERVER",params,prefix);
551 Srv->SendOpers("*** Server "+prefix+" introduced server "+servername+" ("+description+")");
555 bool Outbound_Reply_Server(std::deque<std::string> params)
557 if (params.size() < 4)
559 std::string servername = params[0];
560 std::string password = params[1];
561 int hops = atoi(params[2].c_str());
564 this->WriteLine("ERROR :Server too far away for authentication");
567 std::string description = params[3];
568 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
570 if ((x->Name == servername) && (x->RecvPass == password))
572 // Begin the sync here. this kickstarts the
573 // other side, waiting in WAIT_AUTH_2 state,
574 // into starting their burst, as it shows
576 this->LinkState = CONNECTED;
577 // we should add the details of this server now
578 // to the servers tree, as a child of the root
580 TreeServer* Node = new TreeServer(servername,description,TreeRoot,this);
581 TreeRoot->AddChild(Node);
582 DoOneToAllButSender(servername,"SERVER",params,servername);
587 this->WriteLine("ERROR :Invalid credentials");
591 bool Inbound_Server(std::deque<std::string> params)
593 if (params.size() < 4)
595 std::string servername = params[0];
596 std::string password = params[1];
597 int hops = atoi(params[2].c_str());
600 this->WriteLine("ERROR :Server too far away for authentication");
603 std::string description = params[3];
604 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
606 if ((x->Name == servername) && (x->RecvPass == password))
608 Srv->SendOpers("*** Verified incoming server connection from "+servername+"["+this->GetIP()+"] ("+description+")");
609 this->InboundServerName = servername;
610 this->InboundDescription = description;
611 // this is good. Send our details: Our server name and description and hopcount of 0,
612 // along with the sendpass from this block.
613 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
614 // move to the next state, we are now waiting for THEM.
615 this->LinkState = WAIT_AUTH_2;
619 this->WriteLine("ERROR :Invalid credentials");
623 std::deque<std::string> Split(std::string line)
625 std::deque<std::string> n;
626 std::stringstream s(line);
627 std::string param = "";
633 if ((param.c_str()[0] == ':') && (item))
635 char* str = (char*)param.c_str();
645 param = param + " " + append;
655 bool ProcessLine(std::string line)
657 Srv->SendToModeMask("o",WM_AND,"inbound-line: '"+line+"'");
659 std::deque<std::string> params = this->Split(line);
660 std::string command = "";
661 std::string prefix = "";
662 if (((params[0].c_str())[0] == ':') && (params.size() > 1))
666 char* pref = (char*)prefix.c_str();
678 switch (this->LinkState)
683 // Waiting for SERVER command from remote server. Server initiating
684 // the connection sends the first SERVER command, listening server
685 // replies with theirs if its happy, then if the initiator is happy,
686 // it starts to send its net sync, which starts the merge, otherwise
687 // it sends an ERROR.
688 if (command == "SERVER")
690 return this->Inbound_Server(params);
692 else if (command == "ERROR")
694 return this->Error(params);
698 // Waiting for start of other side's netmerge to say they liked our
700 if (command == "SERVER")
702 // cant do this, they sent it to us in the WAIT_AUTH_1 state!
706 else if (command == "BURST")
708 this->LinkState = CONNECTED;
709 Node = new TreeServer(InboundServerName,InboundDescription,TreeRoot,this);
710 TreeRoot->AddChild(Node);
712 params.push_back(InboundServerName);
713 params.push_back("*");
714 params.push_back("1");
715 params.push_back(InboundDescription);
716 DoOneToAllButSender(InboundServerName,"SERVER",params,InboundServerName);
719 else if (command == "ERROR")
721 return this->Error(params);
726 this->WriteLine("ERROR :Internal error -- listening socket accepted its own descriptor!!!");
730 if (command == "SERVER")
732 // another server we connected to, which was in WAIT_AUTH_1 state,
733 // has just sent us their credentials. If we get this far, theyre
734 // happy with OUR credentials, and they are now in WAIT_AUTH_2 state.
735 // if we're happy with this, we should send our netburst which
736 // kickstarts the merge.
737 return this->Outbound_Reply_Server(params);
741 // This is the 'authenticated' state, when all passwords
742 // have been exchanged and anything past this point is taken
744 std::string target = "";
745 if ((command == "NICK") && (params.size() > 1))
747 return this->IntroduceClient(prefix,params);
749 else if (command == "FJOIN")
751 return this->ForceJoin(prefix,params);
753 else if (command == "SERVER")
755 return this->RemoteServer(prefix,params);
759 // not a special inter-server command.
760 // Emulate the actual user doing the command,
761 // this saves us having a huge ugly parser.
762 userrec* who = Srv->FindNick(prefix);
763 std::string sourceserv = this->myhost;
764 if (this->InboundServerName != "")
766 sourceserv = this->InboundServerName;
771 target = who->server;
772 char* strparams[127];
773 for (unsigned int q = 0; q < params.size(); q++)
775 strparams[q] = (char*)params[q].c_str();
777 log(DEBUG,"*** CALL COMMAND HANDLER FOR %s, SOURCE: '%s'",command.c_str(),who->nick);
778 Srv->CallCommandHandler(command, strparams, params.size(), who);
782 // its not a user. Its either a server, or somethings screwed up.
783 if (IsServer(prefix))
785 target = Srv->GetServerName();
789 log(DEBUG,"Command with unknown origin '%s'",prefix.c_str());
793 return DoOneToAllButSender(prefix,command,params,sourceserv);
802 virtual void OnTimeout()
804 if (this->LinkState == CONNECTING)
806 Srv->SendOpers("*** CONNECT: Connection to "+myhost+" timed out.");
810 virtual void OnClose()
814 virtual int OnIncomingConnection(int newsock, char* ip)
816 TreeSocket* s = new TreeSocket(newsock, ip);
822 bool DoOneToAllButSender(std::string prefix, std::string command, std::deque<std::string> params, std::string omit)
824 log(DEBUG,"ALLBUTONE: Comes from %s SHOULD NOT go back to %s",prefix.c_str(),omit.c_str());
825 // TODO: Special stuff with privmsg and notice
826 std::string FullLine = ":" + prefix + " " + command;
827 for (unsigned int x = 0; x < params.size(); x++)
829 FullLine = FullLine + " " + params[x];
831 for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
833 TreeServer* Route = TreeRoot->GetChild(x);
835 // The route has a socket (its a direct connection)
836 // The route isnt the one to be omitted
837 // The route isnt the path to the one to be omitted
838 if ((Route->GetSocket()) && (Route->GetName() != omit) && (BestRouteTo(omit) != Route))
840 TreeSocket* Sock = Route->GetSocket();
841 log(DEBUG,"Sending to %s",Route->GetName().c_str());
842 Sock->WriteLine(FullLine);
848 bool DoOneToMany(std::string prefix, std::string command, std::deque<std::string> params)
850 std::string FullLine = ":" + prefix + " " + command;
851 for (unsigned int x = 0; x < params.size(); x++)
853 FullLine = FullLine + " " + params[x];
855 for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
857 TreeServer* Route = TreeRoot->GetChild(x);
858 if (Route->GetSocket())
860 TreeSocket* Sock = Route->GetSocket();
861 Sock->WriteLine(FullLine);
867 bool DoOneToOne(std::string prefix, std::string command, std::deque<std::string> params, std::string target)
869 TreeServer* Route = BestRouteTo(target);
872 std::string FullLine = ":" + prefix + " " + command;
873 for (unsigned int x = 0; x < params.size(); x++)
875 FullLine = FullLine + " " + params[x];
877 if (Route->GetSocket())
879 TreeSocket* Sock = Route->GetSocket();
880 Sock->WriteLine(FullLine);
886 log(DEBUG,"Could not route message with target %s: %s",target.c_str(),command.c_str());
892 class ModuleSpanningTree : public Module
894 std::vector<TreeSocket*> Bindings;
899 void ReadConfiguration(bool rebind)
903 for (int j =0; j < Conf->Enumerate("bind"); j++)
905 std::string Type = Conf->ReadValue("bind","type",j);
906 std::string IP = Conf->ReadValue("bind","address",j);
907 long Port = Conf->ReadInteger("bind","port",j,true);
908 if (Type == "servers")
914 TreeSocket* listener = new TreeSocket(IP.c_str(),Port,true,10);
915 if (listener->GetState() == I_LISTENING)
917 Srv->AddSocket(listener);
918 Bindings.push_back(listener);
922 log(DEFAULT,"m_spanningtree: Warning: Failed to bind server port %d",Port);
930 for (int j =0; j < Conf->Enumerate("link"); j++)
933 L.Name = Conf->ReadValue("link","name",j);
934 L.IPAddr = Conf->ReadValue("link","ipaddr",j);
935 L.Port = Conf->ReadInteger("link","port",j,true);
936 L.SendPass = Conf->ReadValue("link","sendpass",j);
937 L.RecvPass = Conf->ReadValue("link","recvpass",j);
938 LinkBlocks.push_back(L);
939 log(DEBUG,"m_spanningtree: Read server %s with host %s:%d",L.Name.c_str(),L.IPAddr.c_str(),L.Port);
946 Conf = new ConfigReader;
949 // Create the root of the tree
950 TreeRoot = new TreeServer(Srv->GetServerName(),Srv->GetServerDescription());
952 ReadConfiguration(true);
955 void ShowLinks(TreeServer* Current, userrec* user, int hops)
957 std::string Parent = TreeRoot->GetName();
958 if (Current->GetParent())
960 Parent = Current->GetParent()->GetName();
962 for (unsigned int q = 0; q < Current->ChildCount(); q++)
964 ShowLinks(Current->GetChild(q),user,hops+1);
966 WriteServ(user->fd,"364 %s %s %s :%d %s",user->nick,Current->GetName().c_str(),Parent.c_str(),hops,Current->GetDesc().c_str());
969 void HandleLinks(char** parameters, int pcnt, userrec* user)
971 ShowLinks(TreeRoot,user,0);
972 WriteServ(user->fd,"365 %s * :End of /LINKS list.",user->nick);
976 void HandleLusers(char** parameters, int pcnt, userrec* user)
981 // WARNING: NOT THREAD SAFE - DONT GET ANY SMART IDEAS.
983 void ShowMap(TreeServer* Current, userrec* user, int depth, char matrix[128][80])
987 for (int t = 0; t < depth; t++)
989 matrix[line][t] = ' ';
991 strlcpy(&matrix[line][depth],Current->GetName().c_str(),80);
993 for (unsigned int q = 0; q < Current->ChildCount(); q++)
995 ShowMap(Current->GetChild(q),user,depth+2,matrix);
1000 // Ok, prepare to be confused.
1001 // After much mulling over how to approach this, it struck me that
1002 // the 'usual' way of doing a /MAP isnt the best way. Instead of
1003 // keeping track of a ton of ascii characters, and line by line
1004 // under recursion working out where to place them using multiplications
1005 // and divisons, we instead render the map onto a backplane of characters
1006 // (a character matrix), then draw the branches as a series of "L" shapes
1007 // from the nodes. This is not only friendlier on CPU it uses less stack.
1009 void HandleMap(char** parameters, int pcnt, userrec* user)
1011 // This array represents a virtual screen which we will
1012 // "scratch" draw to, as the console device of an irc
1013 // client does not provide for a proper terminal.
1014 char matrix[128][80];
1015 for (unsigned int t = 0; t < 128; t++)
1017 matrix[t][0] = '\0';
1020 // The only recursive bit is called here.
1021 ShowMap(TreeRoot,user,0,matrix);
1022 // Process each line one by one. The algorithm has a limit of
1023 // 128 servers (which is far more than a spanning tree should have
1024 // anyway, so we're ok). This limit can be raised simply by making
1025 // the character matrix deeper, 128 rows taking 10k of memory.
1026 for (int l = 1; l < line; l++)
1028 // scan across the line looking for the start of the
1029 // servername (the recursive part of the algorithm has placed
1030 // the servers at indented positions depending on what they
1032 int first_nonspace = 0;
1033 while (matrix[l][first_nonspace] == ' ')
1038 // Draw the `- (corner) section: this may be overwritten by
1039 // another L shape passing along the same vertical pane, becoming
1040 // a |- (branch) section instead.
1041 matrix[l][first_nonspace] = '-';
1042 matrix[l][first_nonspace-1] = '`';
1044 // Draw upwards until we hit the parent server, causing possibly
1045 // other corners (`-) to become branches (|-)
1046 while ((matrix[l2][first_nonspace-1] == ' ') || (matrix[l2][first_nonspace-1] == '`'))
1048 matrix[l2][first_nonspace-1] = '|';
1052 // dump the whole lot to the user. This is the easy bit, honest.
1053 for (int t = 0; t < line; t++)
1055 WriteServ(user->fd,"006 %s :%s",user->nick,&matrix[t][0]);
1057 WriteServ(user->fd,"007 %s :End of /MAP",user->nick);
1061 int HandleSquit(char** parameters, int pcnt, userrec* user)
1066 int HandleConnect(char** parameters, int pcnt, userrec* user)
1068 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
1070 if (Srv->MatchText(x->Name.c_str(),parameters[0]))
1072 WriteServ(user->fd,"NOTICE %s :*** CONNECT: Connecting to server: %s (%s:%d)",user->nick,x->Name.c_str(),x->IPAddr.c_str(),x->Port);
1073 TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name);
1074 Srv->AddSocket(newsocket);
1078 WriteServ(user->fd,"NOTICE %s :*** CONNECT: No matching server could be found in the config file.",user->nick);
1082 virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
1084 if (command == "CONNECT")
1086 return this->HandleConnect(parameters,pcnt,user);
1088 else if (command == "SQUIT")
1090 return this->HandleSquit(parameters,pcnt,user);
1092 else if (command == "MAP")
1094 this->HandleMap(parameters,pcnt,user);
1097 else if (command == "LUSERS")
1099 this->HandleLusers(parameters,pcnt,user);
1102 else if (command == "LINKS")
1104 this->HandleLinks(parameters,pcnt,user);
1110 virtual void OnUserMessage(userrec* user, void* dest, int target_type, std::string text)
1112 if (target_type == TYPE_USER)
1114 // route private messages which are targetted at clients only to the server
1115 // which needs to receive them
1116 userrec* d = (userrec*)dest;
1117 if ((std::string(d->server) != Srv->GetServerName()) && (std::string(user->server) == Srv->GetServerName()))
1119 std::deque<std::string> params;
1121 params.push_back(d->nick);
1122 params.push_back(":"+text);
1123 DoOneToOne(user->nick,"PRIVMSG",params,d->server);
1128 virtual void OnUserJoin(userrec* user, chanrec* channel)
1130 // Only do this for local users
1131 if (std::string(user->server) == Srv->GetServerName())
1133 log(DEBUG,"**** User on %s JOINS %s",user->server,channel->name);
1134 std::deque<std::string> params;
1136 params.push_back(channel->name);
1139 log(DEBUG,"**** With key %s",channel->key);
1140 // if the channel has a key, force the join by emulating the key.
1141 params.push_back(channel->key);
1143 DoOneToMany(user->nick,"JOIN",params);
1147 virtual void OnUserPart(userrec* user, chanrec* channel)
1149 if (std::string(user->server) == Srv->GetServerName())
1151 log(DEBUG,"**** User on %s PARTS %s",user->server,channel->name);
1152 std::deque<std::string> params;
1154 params.push_back(channel->name);
1155 DoOneToMany(user->nick,"PART",params);
1159 virtual ~ModuleSpanningTree()
1164 virtual Version GetVersion()
1166 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
1171 class ModuleSpanningTreeFactory : public ModuleFactory
1174 ModuleSpanningTreeFactory()
1178 ~ModuleSpanningTreeFactory()
1182 virtual Module * CreateModule()
1184 return new ModuleSpanningTree;
1190 extern "C" void * init_module( void )
1192 return new ModuleSpanningTreeFactory;