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 class ModuleSpanningTree;
46 static ModuleSpanningTree* TreeProtocolModule;
48 extern std::vector<Module*> modules;
49 extern std::vector<ircd_module*> factory;
52 enum ServerState { LISTENER, CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED };
54 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, irc::StrHashComp> user_hash;
55 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, irc::StrHashComp> chan_hash;
57 extern user_hash clientlist;
58 extern chan_hash chanlist;
63 bool DoOneToOne(std::string prefix, std::string command, std::deque<std::string> params, std::string target);
64 bool DoOneToAllButSender(std::string prefix, std::string command, std::deque<std::string> params, std::string omit);
65 bool DoOneToMany(std::string prefix, std::string command, std::deque<std::string> params);
66 bool DoOneToAllButSenderRaw(std::string data,std::string omit, std::string prefix,std::string command,std::deque<std::string> params);
67 void ReadConfiguration(bool rebind);
72 std::vector<TreeServer*> Children;
73 std::string ServerName;
74 std::string ServerDesc;
75 std::string VersionString;
78 TreeSocket* Socket; // for directly connected servers this points at the socket object
88 UserCount = OperCount = 0;
91 TreeServer(std::string Name, std::string Desc) : ServerName(Name), ServerDesc(Desc)
95 UserCount = OperCount = 0;
98 TreeServer(std::string Name, std::string Desc, TreeServer* Above, TreeSocket* Sock) : Parent(Above), ServerName(Name), ServerDesc(Desc), Socket(Sock)
101 UserCount = OperCount = 0;
104 std::string GetName()
106 return this->ServerName;
109 std::string GetDesc()
111 return this->ServerDesc;
114 std::string GetVersion()
116 return this->VersionString;
121 return this->UserCount;
126 return this->OperCount;
129 TreeSocket* GetSocket()
134 TreeServer* GetParent()
139 unsigned int ChildCount()
141 return Children.size();
144 TreeServer* GetChild(unsigned int n)
146 if (n < Children.size())
156 void AddChild(TreeServer* Child)
158 Children.push_back(Child);
161 bool DelChild(TreeServer* Child)
163 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
174 // removes child nodes of this node, and of that node, etc etc
177 bool stillchildren = true;
178 while (stillchildren)
180 stillchildren = false;
181 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
183 TreeServer* s = (TreeServer*)*a;
187 stillchildren = true;
201 std::string SendPass;
202 std::string RecvPass;
203 unsigned long AutoConnect;
204 time_t NextConnectTime;
207 /* $ModDesc: Povides a spanning tree server link protocol */
211 TreeServer *TreeRoot;
212 std::vector<Link> LinkBlocks;
214 TreeServer* RouteEnumerate(TreeServer* Current, std::string ServerName)
216 if (Current->GetName() == ServerName)
218 for (unsigned int q = 0; q < Current->ChildCount(); q++)
220 TreeServer* found = RouteEnumerate(Current->GetChild(q),ServerName);
229 // Returns the locally connected server we must route a
230 // message through to reach server 'ServerName'. This
231 // only applies to one-to-one and not one-to-many routing.
232 TreeServer* BestRouteTo(std::string ServerName)
234 if (ServerName.c_str() == TreeRoot->GetName())
238 // first, find the server by recursively walking the tree
239 TreeServer* Found = RouteEnumerate(TreeRoot,ServerName);
240 // did we find it? If not, they did something wrong, abort.
247 // The server exists, follow its parent nodes until
248 // the parent of the current is 'TreeRoot', we know
249 // then that this is a directly-connected server.
250 while ((Found) && (Found->GetParent() != TreeRoot))
252 Found = Found->GetParent();
258 bool LookForServer(TreeServer* Current, std::string ServerName)
260 if (ServerName == Current->GetName())
262 for (unsigned int q = 0; q < Current->ChildCount(); q++)
264 if (LookForServer(Current->GetChild(q),ServerName))
272 void RFindServer(TreeServer* Current, std::string ServerName)
274 if ((ServerName == Current->GetName()) && (!Found))
281 for (unsigned int q = 0; q < Current->ChildCount(); q++)
284 RFindServer(Current->GetChild(q),ServerName);
290 TreeServer* FindServer(std::string ServerName)
293 RFindServer(TreeRoot,ServerName);
297 bool IsServer(std::string ServerName)
299 return LookForServer(TreeRoot,ServerName);
302 class TreeSocket : public InspSocket
305 std::string in_buffer;
306 ServerState LinkState;
307 std::string InboundServerName;
308 std::string InboundDescription;
310 int num_lost_servers;
314 TreeSocket(std::string host, int port, bool listening, unsigned long maxtime)
315 : InspSocket(host, port, listening, maxtime)
318 this->LinkState = LISTENER;
321 TreeSocket(std::string host, int port, bool listening, unsigned long maxtime, std::string ServerName)
322 : InspSocket(host, port, listening, maxtime)
325 this->LinkState = CONNECTING;
328 TreeSocket(int newfd, char* ip)
329 : InspSocket(newfd, ip)
331 this->LinkState = WAIT_AUTH_1;
334 virtual bool OnConnected()
336 if (this->LinkState == CONNECTING)
338 Srv->SendOpers("*** Connection to "+myhost+"["+this->GetIP()+"] established.");
339 // we should send our details here.
340 // if the other side is satisfied, they send theirs.
341 // we do not need to change state here.
342 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
344 if (x->Name == this->myhost)
346 // found who we're supposed to be connecting to, send the neccessary gubbins.
347 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
355 virtual void OnError(InspSocketError e)
359 virtual int OnDisconnect()
364 // recursively send the server tree with distances as hops
365 void SendServers(TreeServer* Current, TreeServer* s, int hops)
368 for (unsigned int q = 0; q < Current->ChildCount(); q++)
370 TreeServer* recursive_server = Current->GetChild(q);
371 if (recursive_server != s)
373 // :source.server SERVER server.name hops :Description
374 snprintf(command,1024,":%s SERVER %s * %d :%s",Current->GetName().c_str(),recursive_server->GetName().c_str(),hops,recursive_server->GetDesc().c_str());
375 this->WriteLine(command);
376 // down to next level
377 this->SendServers(recursive_server, s, hops+1);
382 void SquitServer(TreeServer* Current)
384 // recursively squit the servers attached to 'Current'
385 for (unsigned int q = 0; q < Current->ChildCount(); q++)
387 TreeServer* recursive_server = Current->GetChild(q);
388 this->SquitServer(recursive_server);
390 // Now we've whacked the kids, whack self
392 bool quittingpeople = true;
393 while (quittingpeople)
395 quittingpeople = false;
396 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
398 if (!strcasecmp(u->second->server,Current->GetName().c_str()))
400 Srv->QuitUser(u->second,Current->GetName()+" "+std::string(Srv->GetServerName()));
402 quittingpeople = true;
409 void Squit(TreeServer* Current,std::string reason)
413 std::deque<std::string> params;
414 params.push_back(Current->GetName());
415 params.push_back(":"+reason);
416 DoOneToAllButSender(Current->GetParent()->GetName(),"SQUIT",params,Current->GetName());
417 if (Current->GetParent() == TreeRoot)
419 Srv->SendOpers("Server \002"+Current->GetName()+"\002 split: "+reason);
423 Srv->SendOpers("Server \002"+Current->GetName()+"\002 split from server \002"+Current->GetParent()->GetName()+"\002 with reason: "+reason);
425 num_lost_servers = 0;
427 SquitServer(Current);
429 Current->GetParent()->DelChild(Current);
431 WriteOpers("Netsplit complete, lost \002%d\002 users on \002%d\002 servers.", num_lost_users, num_lost_servers);
435 log(DEFAULT,"Squit from unknown server");
439 bool ForceMode(std::string source, std::deque<std::string> params)
441 userrec* who = new userrec;
442 who->fd = FD_MAGIC_NUMBER;
443 if (params.size() < 2)
446 for (unsigned int q = 0; q < params.size(); q++)
448 modelist[q] = (char*)params[q].c_str();
450 Srv->SendMode(modelist,params.size(),who);
451 DoOneToAllButSender(source,"FMODE",params,source);
456 bool ForceTopic(std::string source, std::deque<std::string> params)
458 // FTOPIC %s %lu %s :%s
459 if (params.size() != 4)
461 std::string channel = params[0];
462 time_t ts = atoi(params[1].c_str());
463 std::string setby = params[2];
464 std::string topic = params[3];
466 chanrec* c = Srv->FindChannel(channel);
469 if ((ts >= c->topicset) || (!*c->topic))
471 strlcpy(c->topic,topic.c_str(),MAXTOPIC);
472 strlcpy(c->setby,setby.c_str(),NICKMAX);
474 WriteChannelWithServ((char*)source.c_str(), c, "TOPIC %s :%s", c->name, c->topic);
479 // all done, send it on its way
480 params[3] = ":" + params[3];
481 DoOneToAllButSender(source,"FTOPIC",params,source);
486 bool ForceJoin(std::string source, std::deque<std::string> params)
488 if (params.size() < 3)
492 char modestring[MAXBUF];
493 char* mode_users[127];
494 mode_users[0] = first;
495 mode_users[1] = modestring;
496 strcpy(mode_users[1],"+");
497 unsigned int modectr = 2;
500 std::string channel = params[0];
501 time_t TS = atoi(params[1].c_str());
504 chanrec* chan = Srv->FindChannel(channel);
509 strlcpy(mode_users[0],channel.c_str(),MAXBUF);
511 // default is a high value, which if we dont have this
512 // channel will let the other side apply their modes.
513 time_t ourTS = time(NULL)+20;
514 chanrec* us = Srv->FindChannel(channel);
520 log(DEBUG,"FJOIN detected, our TS=%lu, their TS=%lu",ourTS,TS);
522 // do this first, so our mode reversals are correctly received by other servers
523 // if there is a TS collision.
524 DoOneToAllButSender(source,"FJOIN",params,source);
526 for (unsigned int usernum = 2; usernum < params.size(); usernum++)
528 // process one channel at a time, applying modes.
529 char* usr = (char*)params[usernum].c_str();
530 char permissions = *usr;
535 mode_users[modectr++] = usr;
536 strlcat(modestring,"o",MAXBUF);
540 mode_users[modectr++] = usr;
541 strlcat(modestring,"h",MAXBUF);
545 mode_users[modectr++] = usr;
546 strlcat(modestring,"v",MAXBUF);
549 who = Srv->FindNick(usr);
552 Srv->JoinUserToChannel(who,channel,key);
553 if (modectr >= (MAXMODES-1))
555 // theres a mode for this user. push them onto the mode queue, and flush it
556 // if there are more than MAXMODES to go.
559 log(DEBUG,"Our our channel newer than theirs, accepting their modes");
560 Srv->SendMode(mode_users,modectr,who);
564 log(DEBUG,"Their channel newer than ours, bouncing their modes");
566 std::deque<std::string> params;
567 // modes are now being UNSET...
568 *mode_users[1] = '-';
569 for (unsigned int x = 0; x < modectr; x++)
571 params.push_back(mode_users[x]);
573 // tell everyone to bounce the modes. bad modes, bad!
574 DoOneToMany(Srv->GetServerName(),"FMODE",params);
576 strcpy(mode_users[1],"+");
581 // there werent enough modes built up to flush it during FJOIN,
582 // or, there are a number left over. flush them out.
583 if ((modectr > 2) && (who))
587 log(DEBUG,"Our our channel newer than theirs, accepting their modes");
588 Srv->SendMode(mode_users,modectr,who);
592 log(DEBUG,"Their channel newer than ours, bouncing their modes");
593 std::deque<std::string> params;
594 *mode_users[1] = '-';
595 for (unsigned int x = 0; x < modectr; x++)
597 params.push_back(mode_users[x]);
599 DoOneToMany(Srv->GetServerName(),"FMODE",params);
605 bool IntroduceClient(std::string source, std::deque<std::string> params)
607 if (params.size() < 8)
609 // NICK age nick host dhost ident +modes ip :gecos
611 std::string nick = params[1];
612 std::string host = params[2];
613 std::string dhost = params[3];
614 std::string ident = params[4];
615 time_t age = atoi(params[0].c_str());
616 std::string modes = params[5];
617 while (*(modes.c_str()) == '+')
619 char* m = (char*)modes.c_str();
623 std::string ip = params[6];
624 std::string gecos = params[7];
625 char* tempnick = (char*)nick.c_str();
626 log(DEBUG,"Introduce client %s!%s@%s",tempnick,ident.c_str(),host.c_str());
628 user_hash::iterator iter;
629 iter = clientlist.find(tempnick);
630 if (iter != clientlist.end())
633 log(DEBUG,"Nick collision on %s!%s@%s: %lu %lu",tempnick,ident.c_str(),host.c_str(),(unsigned long)age,(unsigned long)iter->second->age);
634 this->WriteLine(":"+Srv->GetServerName()+" KILL "+tempnick+" :Nickname collision");
638 clientlist[tempnick] = new userrec();
639 clientlist[tempnick]->fd = FD_MAGIC_NUMBER;
640 strlcpy(clientlist[tempnick]->nick, tempnick,NICKMAX);
641 strlcpy(clientlist[tempnick]->host, host.c_str(),160);
642 strlcpy(clientlist[tempnick]->dhost, dhost.c_str(),160);
643 clientlist[tempnick]->server = (char*)FindServerNamePtr(source.c_str());
644 strlcpy(clientlist[tempnick]->ident, ident.c_str(),IDENTMAX);
645 strlcpy(clientlist[tempnick]->fullname, gecos.c_str(),MAXGECOS);
646 clientlist[tempnick]->registered = 7;
647 clientlist[tempnick]->signon = age;
648 strlcpy(clientlist[tempnick]->modes, modes.c_str(),53);
649 strlcpy(clientlist[tempnick]->ip,ip.c_str(),16);
650 for (int i = 0; i < MAXCHANS; i++)
652 clientlist[tempnick]->chans[i].channel = NULL;
653 clientlist[tempnick]->chans[i].uc_modes = 0;
655 params[7] = ":" + params[7];
656 DoOneToAllButSender(source,"NICK",params,source);
660 void SendFJoins(TreeServer* Current, chanrec* c)
663 snprintf(list,MAXBUF,":%s FJOIN %s %lu",Srv->GetServerName().c_str(),c->name,(unsigned long)c->age);
664 std::vector<char*> *ulist = c->GetUsers();
665 for (unsigned int i = 0; i < ulist->size(); i++)
667 char* o = (*ulist)[i];
668 userrec* otheruser = (userrec*)o;
669 strlcat(list," ",MAXBUF);
670 strlcat(list,cmode(otheruser,c),MAXBUF);
671 strlcat(list,otheruser->nick,MAXBUF);
672 if (strlen(list)>(480-NICKMAX))
674 this->WriteLine(list);
675 snprintf(list,MAXBUF,":%s FJOIN %s %lu",Srv->GetServerName().c_str(),c->name,(unsigned long)c->age);
678 if (list[strlen(list)-1] != ':')
680 this->WriteLine(list);
684 void SendChannelModes(TreeServer* Current)
687 for (chan_hash::iterator c = chanlist.begin(); c != chanlist.end(); c++)
689 SendFJoins(Current, c->second);
690 snprintf(data,MAXBUF,":%s FMODE %s +%s",Srv->GetServerName().c_str(),c->second->name,chanmodes(c->second));
691 this->WriteLine(data);
692 if (*c->second->topic)
694 snprintf(data,MAXBUF,":%s FTOPIC %s %lu %s :%s",Srv->GetServerName().c_str(),c->second->name,(unsigned long)c->second->topicset,c->second->setby,c->second->topic);
695 this->WriteLine(data);
697 for (BanList::iterator b = c->second->bans.begin(); b != c->second->bans.end(); b++)
699 snprintf(data,MAXBUF,":%s FMODE %s +b %s",Srv->GetServerName().c_str(),c->second->name,b->data);
700 this->WriteLine(data);
702 FOREACH_MOD OnSyncChannel(c->second,(Module*)TreeProtocolModule,(void*)this);
706 // send all users and their channels
707 void SendUsers(TreeServer* Current)
710 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
712 if (u->second->registered == 7)
714 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);
715 this->WriteLine(data);
716 if (strchr(u->second->modes,'o'))
718 this->WriteLine(":"+std::string(u->second->nick)+" OPERTYPE "+std::string(u->second->oper));
720 //char* chl = chlist(u->second,u->second);
723 // this->WriteLine(":"+std::string(u->second->nick)+" FJOIN "+std::string(chl));
725 FOREACH_MOD OnSyncUser(u->second,(Module*)TreeProtocolModule,(void*)this);
730 void DoBurst(TreeServer* s)
732 Srv->SendOpers("*** Bursting to "+s->GetName()+".");
733 this->WriteLine("BURST");
735 this->SendServers(TreeRoot,s,1);
736 // Send users and their channels
738 // Send everything else (channel modes etc)
739 this->SendChannelModes(s);
740 this->WriteLine("ENDBURST");
743 virtual bool OnDataReady()
745 char* data = this->Read();
748 this->in_buffer += data;
749 while (in_buffer.find("\n") != std::string::npos)
751 char* line = (char*)in_buffer.c_str();
752 std::string ret = "";
753 while ((*line != '\n') && (strlen(line)))
758 if ((*line == '\n') || (*line == '\r'))
761 if (!this->ProcessLine(ret))
767 return (data != NULL);
770 int WriteLine(std::string line)
772 return this->Write(line + "\r\n");
775 bool Error(std::deque<std::string> params)
777 if (params.size() < 1)
779 std::string Errmsg = params[0];
780 std::string SName = myhost;
781 if (InboundServerName != "")
783 SName = InboundServerName;
785 Srv->SendOpers("*** ERROR from "+SName+": "+Errmsg);
786 // we will return false to cause the socket to close.
790 bool OperType(std::string prefix, std::deque<std::string> params)
792 if (params.size() != 1)
794 std::string opertype = params[0];
795 userrec* u = Srv->FindNick(prefix);
798 strlcpy(u->oper,opertype.c_str(),NICKMAX);
799 if (!strchr(u->modes,'o'))
801 strcat(u->modes,"o");
803 DoOneToAllButSender(u->server,"OPERTYPE",params,u->server);
808 bool RemoteRehash(std::string prefix, std::deque<std::string> params)
810 if (params.size() < 1)
812 std::string servermask = params[0];
813 if (Srv->MatchText(Srv->GetServerName(),servermask))
815 Srv->SendOpers("*** Remote rehash initiated from server \002"+prefix+"\002.");
817 ReadConfiguration(false);
819 DoOneToAllButSender(prefix,"REHASH",params,prefix);
823 bool RemoteKill(std::string prefix, std::deque<std::string> params)
825 if (params.size() != 2)
827 std::string nick = params[0];
828 std::string reason = params[1];
829 userrec* u = Srv->FindNick(prefix);
830 userrec* who = Srv->FindNick(nick);
833 std::string sourceserv = prefix;
836 sourceserv = u->server;
838 params[1] = ":" + params[1];
839 DoOneToAllButSender(prefix,"KILL",params,sourceserv);
840 Srv->QuitUser(who,reason);
845 bool RemoteServer(std::string prefix, std::deque<std::string> params)
847 if (params.size() < 4)
849 std::string servername = params[0];
850 std::string password = params[1];
851 // hopcount is not used for a remote server, we calculate this ourselves
852 std::string description = params[3];
853 TreeServer* ParentOfThis = FindServer(prefix);
856 this->WriteLine("ERROR :Protocol error - Introduced remote server from unknown server "+prefix);
859 TreeServer* CheckDupe = FindServer(servername);
862 this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
865 TreeServer* Node = new TreeServer(servername,description,ParentOfThis,NULL);
866 ParentOfThis->AddChild(Node);
867 params[3] = ":" + params[3];
868 DoOneToAllButSender(prefix,"SERVER",params,prefix);
869 Srv->SendOpers("*** Server \002"+prefix+"\002 introduced server \002"+servername+"\002 ("+description+")");
873 bool Outbound_Reply_Server(std::deque<std::string> params)
875 if (params.size() < 4)
877 std::string servername = params[0];
878 std::string password = params[1];
879 int hops = atoi(params[2].c_str());
882 this->WriteLine("ERROR :Server too far away for authentication");
885 std::string description = params[3];
886 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
888 if ((x->Name == servername) && (x->RecvPass == password))
890 TreeServer* CheckDupe = FindServer(servername);
893 this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
896 // Begin the sync here. this kickstarts the
897 // other side, waiting in WAIT_AUTH_2 state,
898 // into starting their burst, as it shows
900 this->LinkState = CONNECTED;
901 // we should add the details of this server now
902 // to the servers tree, as a child of the root
904 TreeServer* Node = new TreeServer(servername,description,TreeRoot,this);
905 TreeRoot->AddChild(Node);
906 params[3] = ":" + params[3];
907 DoOneToAllButSender(TreeRoot->GetName(),"SERVER",params,servername);
912 this->WriteLine("ERROR :Invalid credentials");
916 bool Inbound_Server(std::deque<std::string> params)
918 if (params.size() < 4)
920 std::string servername = params[0];
921 std::string password = params[1];
922 int hops = atoi(params[2].c_str());
925 this->WriteLine("ERROR :Server too far away for authentication");
928 std::string description = params[3];
929 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
931 if ((x->Name == servername) && (x->RecvPass == password))
933 TreeServer* CheckDupe = FindServer(servername);
936 this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
939 Srv->SendOpers("*** Verified incoming server connection from \002"+servername+"\002["+this->GetIP()+"] ("+description+")");
940 this->InboundServerName = servername;
941 this->InboundDescription = description;
942 // this is good. Send our details: Our server name and description and hopcount of 0,
943 // along with the sendpass from this block.
944 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
945 // move to the next state, we are now waiting for THEM.
946 this->LinkState = WAIT_AUTH_2;
950 this->WriteLine("ERROR :Invalid credentials");
954 std::deque<std::string> Split(std::string line, bool stripcolon)
956 std::deque<std::string> n;
957 if (!strchr(line.c_str(),' '))
962 std::stringstream s(line);
963 std::string param = "";
982 if ((param == ":") && (item > 0))
1005 bool ProcessLine(std::string line)
1007 char* l = (char*)line.c_str();
1008 while ((strlen(l)) && (l[strlen(l)-1] == '\r') || (l[strlen(l)-1] == '\n'))
1009 l[strlen(l)-1] = '\0';
1013 Srv->Log(DEBUG,"IN: '"+line+"'");
1014 std::deque<std::string> params = this->Split(line,true);
1015 std::string command = "";
1016 std::string prefix = "";
1017 if (((params[0].c_str())[0] == ':') && (params.size() > 1))
1020 command = params[1];
1021 char* pref = (char*)prefix.c_str();
1029 command = params[0];
1033 switch (this->LinkState)
1038 // Waiting for SERVER command from remote server. Server initiating
1039 // the connection sends the first SERVER command, listening server
1040 // replies with theirs if its happy, then if the initiator is happy,
1041 // it starts to send its net sync, which starts the merge, otherwise
1042 // it sends an ERROR.
1043 if (command == "SERVER")
1045 return this->Inbound_Server(params);
1047 else if (command == "ERROR")
1049 return this->Error(params);
1053 // Waiting for start of other side's netmerge to say they liked our
1055 if (command == "SERVER")
1057 // cant do this, they sent it to us in the WAIT_AUTH_1 state!
1061 else if (command == "BURST")
1063 this->LinkState = CONNECTED;
1064 Node = new TreeServer(InboundServerName,InboundDescription,TreeRoot,this);
1065 TreeRoot->AddChild(Node);
1067 params.push_back(InboundServerName);
1068 params.push_back("*");
1069 params.push_back("1");
1070 params.push_back(":"+InboundDescription);
1071 DoOneToAllButSender(TreeRoot->GetName(),"SERVER",params,InboundServerName);
1072 this->DoBurst(Node);
1074 else if (command == "ERROR")
1076 return this->Error(params);
1081 this->WriteLine("ERROR :Internal error -- listening socket accepted its own descriptor!!!");
1085 if (command == "SERVER")
1087 // another server we connected to, which was in WAIT_AUTH_1 state,
1088 // has just sent us their credentials. If we get this far, theyre
1089 // happy with OUR credentials, and they are now in WAIT_AUTH_2 state.
1090 // if we're happy with this, we should send our netburst which
1091 // kickstarts the merge.
1092 return this->Outbound_Reply_Server(params);
1094 else if (command == "ERROR")
1096 return this->Error(params);
1100 // This is the 'authenticated' state, when all passwords
1101 // have been exchanged and anything past this point is taken
1103 std::string target = "";
1104 if ((command == "NICK") && (params.size() > 1))
1106 return this->IntroduceClient(prefix,params);
1108 else if (command == "FJOIN")
1110 return this->ForceJoin(prefix,params);
1112 else if (command == "SERVER")
1114 return this->RemoteServer(prefix,params);
1116 else if (command == "ERROR")
1118 return this->Error(params);
1120 else if (command == "OPERTYPE")
1122 return this->OperType(prefix,params);
1124 else if (command == "FMODE")
1126 return this->ForceMode(prefix,params);
1128 else if (command == "KILL")
1130 return this->RemoteKill(prefix,params);
1132 else if (command == "FTOPIC")
1134 return this->ForceTopic(prefix,params);
1136 else if (command == "REHASH")
1138 return this->RemoteRehash(prefix,params);
1140 else if (command == "SQUIT")
1142 if (params.size() == 2)
1144 this->Squit(FindServer(params[0]),params[1]);
1150 // not a special inter-server command.
1151 // Emulate the actual user doing the command,
1152 // this saves us having a huge ugly parser.
1153 userrec* who = Srv->FindNick(prefix);
1154 std::string sourceserv = this->myhost;
1155 if (this->InboundServerName != "")
1157 sourceserv = this->InboundServerName;
1162 target = who->server;
1163 char* strparams[127];
1164 for (unsigned int q = 0; q < params.size(); q++)
1166 strparams[q] = (char*)params[q].c_str();
1168 Srv->CallCommandHandler(command, strparams, params.size(), who);
1172 // its not a user. Its either a server, or somethings screwed up.
1173 if (IsServer(prefix))
1175 target = Srv->GetServerName();
1179 log(DEBUG,"Command with unknown origin '%s'",prefix.c_str());
1183 return DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1192 virtual std::string GetName()
1194 std::string sourceserv = this->myhost;
1195 if (this->InboundServerName != "")
1197 sourceserv = this->InboundServerName;
1202 virtual void OnTimeout()
1204 if (this->LinkState == CONNECTING)
1206 Srv->SendOpers("*** CONNECT: Connection to \002"+myhost+"\002 timed out.");
1210 virtual void OnClose()
1212 // Connection closed.
1213 // If the connection is fully up (state CONNECTED)
1214 // then propogate a netsplit to all peers.
1215 std::string quitserver = this->myhost;
1216 if (this->InboundServerName != "")
1218 quitserver = this->InboundServerName;
1220 TreeServer* s = FindServer(quitserver);
1223 Squit(s,"Remote host closed the connection");
1227 virtual int OnIncomingConnection(int newsock, char* ip)
1229 TreeSocket* s = new TreeSocket(newsock, ip);
1235 void AddThisServer(TreeServer* server, std::deque<TreeServer*> &list)
1237 for (unsigned int c = 0; c < list.size(); c++)
1239 if (list[c] == server)
1244 list.push_back(server);
1247 // returns a list of DIRECT servernames for a specific channel
1248 std::deque<TreeServer*> GetListOfServersForChannel(chanrec* c)
1250 std::deque<TreeServer*> list;
1251 std::vector<char*> *ulist = c->GetUsers();
1252 for (unsigned int i = 0; i < ulist->size(); i++)
1254 char* o = (*ulist)[i];
1255 userrec* otheruser = (userrec*)o;
1256 if (std::string(otheruser->server) != Srv->GetServerName())
1258 TreeServer* best = BestRouteTo(otheruser->server);
1260 AddThisServer(best,list);
1266 bool DoOneToAllButSenderRaw(std::string data,std::string omit,std::string prefix,std::string command,std::deque<std::string> params)
1268 TreeServer* omitroute = BestRouteTo(omit);
1269 if ((command == "NOTICE") || (command == "PRIVMSG"))
1271 if ((params.size() >= 2) && (*(params[0].c_str()) != '$'))
1273 if (*(params[0].c_str()) != '#')
1275 // special routing for private messages/notices
1276 userrec* d = Srv->FindNick(params[0]);
1279 std::deque<std::string> par;
1281 par.push_back(params[0]);
1282 par.push_back(":"+params[1]);
1283 DoOneToOne(prefix,command,par,d->server);
1289 log(DEBUG,"Channel privmsg going to chan %s",params[0].c_str());
1290 chanrec* c = Srv->FindChannel(params[0]);
1293 std::deque<TreeServer*> list = GetListOfServersForChannel(c);
1294 log(DEBUG,"Got a list of %d servers",list.size());
1295 for (unsigned int i = 0; i < list.size(); i++)
1297 TreeSocket* Sock = list[i]->GetSocket();
1298 if ((Sock) && (list[i]->GetName() != omit) && (omitroute != list[i]))
1300 log(DEBUG,"Writing privmsg to server %s",list[i]->GetName().c_str());
1301 Sock->WriteLine(data);
1309 for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1311 TreeServer* Route = TreeRoot->GetChild(x);
1312 if ((Route->GetSocket()) && (Route->GetName() != omit) && (omitroute != Route))
1314 TreeSocket* Sock = Route->GetSocket();
1315 Sock->WriteLine(data);
1321 bool DoOneToAllButSender(std::string prefix, std::string command, std::deque<std::string> params, std::string omit)
1323 TreeServer* omitroute = BestRouteTo(omit);
1324 std::string FullLine = ":" + prefix + " " + command;
1325 for (unsigned int x = 0; x < params.size(); x++)
1327 FullLine = FullLine + " " + params[x];
1329 for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1331 TreeServer* Route = TreeRoot->GetChild(x);
1332 // Send the line IF:
1333 // The route has a socket (its a direct connection)
1334 // The route isnt the one to be omitted
1335 // The route isnt the path to the one to be omitted
1336 if ((Route->GetSocket()) && (Route->GetName() != omit) && (omitroute != Route))
1338 TreeSocket* Sock = Route->GetSocket();
1339 Sock->WriteLine(FullLine);
1345 bool DoOneToMany(std::string prefix, std::string command, std::deque<std::string> params)
1347 std::string FullLine = ":" + prefix + " " + command;
1348 for (unsigned int x = 0; x < params.size(); x++)
1350 FullLine = FullLine + " " + params[x];
1352 for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1354 TreeServer* Route = TreeRoot->GetChild(x);
1355 if (Route->GetSocket())
1357 TreeSocket* Sock = Route->GetSocket();
1358 Sock->WriteLine(FullLine);
1364 bool DoOneToOne(std::string prefix, std::string command, std::deque<std::string> params, std::string target)
1366 TreeServer* Route = BestRouteTo(target);
1369 std::string FullLine = ":" + prefix + " " + command;
1370 for (unsigned int x = 0; x < params.size(); x++)
1372 FullLine = FullLine + " " + params[x];
1374 if (Route->GetSocket())
1376 TreeSocket* Sock = Route->GetSocket();
1377 Sock->WriteLine(FullLine);
1387 std::vector<TreeSocket*> Bindings;
1389 void ReadConfiguration(bool rebind)
1393 for (int j =0; j < Conf->Enumerate("bind"); j++)
1395 std::string Type = Conf->ReadValue("bind","type",j);
1396 std::string IP = Conf->ReadValue("bind","address",j);
1397 long Port = Conf->ReadInteger("bind","port",j,true);
1398 if (Type == "servers")
1404 TreeSocket* listener = new TreeSocket(IP.c_str(),Port,true,10);
1405 if (listener->GetState() == I_LISTENING)
1407 Srv->AddSocket(listener);
1408 Bindings.push_back(listener);
1412 log(DEFAULT,"m_spanningtree: Warning: Failed to bind server port %d",Port);
1420 for (int j =0; j < Conf->Enumerate("link"); j++)
1423 L.Name = Conf->ReadValue("link","name",j);
1424 L.IPAddr = Conf->ReadValue("link","ipaddr",j);
1425 L.Port = Conf->ReadInteger("link","port",j,true);
1426 L.SendPass = Conf->ReadValue("link","sendpass",j);
1427 L.RecvPass = Conf->ReadValue("link","recvpass",j);
1428 L.AutoConnect = Conf->ReadInteger("link","autoconnect",j,true);
1429 L.NextConnectTime = time(NULL) + L.AutoConnect;
1430 LinkBlocks.push_back(L);
1431 log(DEBUG,"m_spanningtree: Read server %s with host %s:%d",L.Name.c_str(),L.IPAddr.c_str(),L.Port);
1436 class ModuleSpanningTree : public Module
1438 std::vector<TreeSocket*> Bindings;
1444 ModuleSpanningTree()
1447 Conf = new ConfigReader;
1450 // Create the root of the tree
1451 TreeRoot = new TreeServer(Srv->GetServerName(),Srv->GetServerDescription());
1453 ReadConfiguration(true);
1456 void ShowLinks(TreeServer* Current, userrec* user, int hops)
1458 std::string Parent = TreeRoot->GetName();
1459 if (Current->GetParent())
1461 Parent = Current->GetParent()->GetName();
1463 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1465 ShowLinks(Current->GetChild(q),user,hops+1);
1467 WriteServ(user->fd,"364 %s %s %s :%d %s",user->nick,Current->GetName().c_str(),Parent.c_str(),hops,Current->GetDesc().c_str());
1470 int CountLocalServs()
1472 return TreeRoot->ChildCount();
1475 void CountServsRecursive(TreeServer* Current)
1478 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1480 CountServsRecursive(Current->GetChild(q));
1487 CountServsRecursive(TreeRoot);
1491 void HandleLinks(char** parameters, int pcnt, userrec* user)
1493 ShowLinks(TreeRoot,user,0);
1494 WriteServ(user->fd,"365 %s * :End of /LINKS list.",user->nick);
1498 void HandleLusers(char** parameters, int pcnt, userrec* user)
1500 WriteServ(user->fd,"251 %s :There are %d users and %d invisible on %d servers",user->nick,usercnt()-usercount_invisible(),usercount_invisible(),this->CountServs());
1501 WriteServ(user->fd,"252 %s %d :operator(s) online",user->nick,usercount_opers());
1502 WriteServ(user->fd,"253 %s %d :unknown connections",user->nick,usercount_unknown());
1503 WriteServ(user->fd,"254 %s %d :channels formed",user->nick,chancount());
1504 WriteServ(user->fd,"254 %s :I have %d clients and %d servers",user->nick,local_count(),this->CountLocalServs());
1508 // WARNING: NOT THREAD SAFE - DONT GET ANY SMART IDEAS.
1510 void ShowMap(TreeServer* Current, userrec* user, int depth, char matrix[128][80])
1514 for (int t = 0; t < depth; t++)
1516 matrix[line][t] = ' ';
1518 strlcpy(&matrix[line][depth],Current->GetName().c_str(),80);
1520 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1522 ShowMap(Current->GetChild(q),user,depth+2,matrix);
1527 // Ok, prepare to be confused.
1528 // After much mulling over how to approach this, it struck me that
1529 // the 'usual' way of doing a /MAP isnt the best way. Instead of
1530 // keeping track of a ton of ascii characters, and line by line
1531 // under recursion working out where to place them using multiplications
1532 // and divisons, we instead render the map onto a backplane of characters
1533 // (a character matrix), then draw the branches as a series of "L" shapes
1534 // from the nodes. This is not only friendlier on CPU it uses less stack.
1536 void HandleMap(char** parameters, int pcnt, userrec* user)
1538 // This array represents a virtual screen which we will
1539 // "scratch" draw to, as the console device of an irc
1540 // client does not provide for a proper terminal.
1541 char matrix[128][80];
1542 for (unsigned int t = 0; t < 128; t++)
1544 matrix[t][0] = '\0';
1547 // The only recursive bit is called here.
1548 ShowMap(TreeRoot,user,0,matrix);
1549 // Process each line one by one. The algorithm has a limit of
1550 // 128 servers (which is far more than a spanning tree should have
1551 // anyway, so we're ok). This limit can be raised simply by making
1552 // the character matrix deeper, 128 rows taking 10k of memory.
1553 for (int l = 1; l < line; l++)
1555 // scan across the line looking for the start of the
1556 // servername (the recursive part of the algorithm has placed
1557 // the servers at indented positions depending on what they
1559 int first_nonspace = 0;
1560 while (matrix[l][first_nonspace] == ' ')
1565 // Draw the `- (corner) section: this may be overwritten by
1566 // another L shape passing along the same vertical pane, becoming
1567 // a |- (branch) section instead.
1568 matrix[l][first_nonspace] = '-';
1569 matrix[l][first_nonspace-1] = '`';
1571 // Draw upwards until we hit the parent server, causing possibly
1572 // other corners (`-) to become branches (|-)
1573 while ((matrix[l2][first_nonspace-1] == ' ') || (matrix[l2][first_nonspace-1] == '`'))
1575 matrix[l2][first_nonspace-1] = '|';
1579 // dump the whole lot to the user. This is the easy bit, honest.
1580 for (int t = 0; t < line; t++)
1582 WriteServ(user->fd,"006 %s :%s",user->nick,&matrix[t][0]);
1584 WriteServ(user->fd,"007 %s :End of /MAP",user->nick);
1588 int HandleSquit(char** parameters, int pcnt, userrec* user)
1590 TreeServer* s = FindServer(parameters[0]);
1593 TreeSocket* sock = s->GetSocket();
1596 WriteOpers("*** SQUIT: Server \002%s\002 removed from network by %s",parameters[0],user->nick);
1597 sock->Squit(s,"Server quit by "+std::string(user->nick)+"!"+std::string(user->ident)+"@"+std::string(user->host));
1602 WriteServ(user->fd,"NOTICE %s :*** SQUIT: The server \002%s\002 is not directly connected.",user->nick,parameters[0]);
1607 WriteServ(user->fd,"NOTICE %s :*** SQUIT: The server \002%s\002 does not exist on the network.",user->nick,parameters[0]);
1612 void AutoConnectServers(time_t curtime)
1614 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
1616 log(DEBUG,"TICK! Autoconnect=%lu, curtime=%lu, NextConnectTime=%lu",x->AutoConnect,(unsigned long)curtime,(unsigned long)x->NextConnectTime);
1617 if ((x->AutoConnect) && (curtime >= x->NextConnectTime))
1619 log(DEBUG,"TICK! AUTOCONNECTING %s",x->Name.c_str());
1620 TreeServer* CheckDupe = FindServer(x->Name);
1623 log(DEBUG,"TICK! Not a dupe!");
1624 // an autoconnected server is not connected. Check if its time to connect it
1625 x->NextConnectTime = curtime + x->AutoConnect;
1626 WriteOpers("*** AUTOCONNECT: Auto-connecting server \002%s\002 (%lu seconds until next attempt)",x->Name.c_str(),x->AutoConnect);
1627 TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name);
1628 Srv->AddSocket(newsocket);
1634 int HandleConnect(char** parameters, int pcnt, userrec* user)
1636 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
1638 if (Srv->MatchText(x->Name.c_str(),parameters[0]))
1640 TreeServer* CheckDupe = FindServer(x->Name);
1643 WriteServ(user->fd,"NOTICE %s :*** CONNECT: Connecting to server: \002%s\002 (%s:%d)",user->nick,x->Name.c_str(),x->IPAddr.c_str(),x->Port);
1644 TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name);
1645 Srv->AddSocket(newsocket);
1650 WriteServ(user->fd,"NOTICE %s :*** CONNECT: Server \002%s\002 already exists on the network and is connected via \002%s\002",user->nick,x->Name.c_str(),CheckDupe->GetParent()->GetName().c_str());
1655 WriteServ(user->fd,"NOTICE %s :*** CONNECT: No server matching \002%s\002 could be found in the config file.",user->nick,parameters[0]);
1659 virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
1661 if (command == "CONNECT")
1663 return this->HandleConnect(parameters,pcnt,user);
1665 else if (command == "SQUIT")
1667 return this->HandleSquit(parameters,pcnt,user);
1669 else if (command == "MAP")
1671 this->HandleMap(parameters,pcnt,user);
1674 else if (command == "LUSERS")
1676 this->HandleLusers(parameters,pcnt,user);
1679 else if (command == "LINKS")
1681 this->HandleLinks(parameters,pcnt,user);
1684 else if (Srv->IsValidModuleCommand(command, pcnt, user))
1686 // this bit of code cleverly routes all module commands
1687 // to all remote severs *automatically* so that modules
1688 // can just handle commands locally, without having
1689 // to have any special provision in place for remote
1690 // commands and linking protocols.
1691 std::deque<std::string> params;
1693 for (int j = 0; j < pcnt; j++)
1695 if (strchr(parameters[j],' '))
1697 params.push_back(":" + std::string(parameters[j]));
1701 params.push_back(std::string(parameters[j]));
1704 DoOneToMany(user->nick,command,params);
1709 virtual void OnGetServerDescription(std::string servername,std::string &description)
1711 TreeServer* s = FindServer(servername);
1714 description = s->GetDesc();
1718 virtual void OnUserInvite(userrec* source,userrec* dest,chanrec* channel)
1720 if (std::string(source->server) == Srv->GetServerName())
1722 std::deque<std::string> params;
1723 params.push_back(dest->nick);
1724 params.push_back(channel->name);
1725 DoOneToMany(source->nick,"INVITE",params);
1729 virtual void OnPostLocalTopicChange(userrec* user, chanrec* chan, std::string topic)
1731 std::deque<std::string> params;
1732 params.push_back(chan->name);
1733 params.push_back(":"+topic);
1734 DoOneToMany(user->nick,"TOPIC",params);
1737 virtual void OnWallops(userrec* user, std::string text)
1739 if (std::string(user->server) == Srv->GetServerName())
1741 std::deque<std::string> params;
1742 params.push_back(":"+text);
1743 DoOneToMany(user->nick,"WALLOPS",params);
1747 virtual void OnUserNotice(userrec* user, void* dest, int target_type, std::string text)
1749 if (target_type == TYPE_USER)
1751 userrec* d = (userrec*)dest;
1752 if ((std::string(d->server) != Srv->GetServerName()) && (std::string(user->server) == Srv->GetServerName()))
1754 std::deque<std::string> params;
1756 params.push_back(d->nick);
1757 params.push_back(":"+text);
1758 DoOneToOne(user->nick,"NOTICE",params,d->server);
1763 if (std::string(user->server) == Srv->GetServerName())
1765 chanrec *c = (chanrec*)dest;
1766 std::deque<TreeServer*> list = GetListOfServersForChannel(c);
1767 for (unsigned int i = 0; i < list.size(); i++)
1769 TreeSocket* Sock = list[i]->GetSocket();
1771 Sock->WriteLine(":"+std::string(user->nick)+" NOTICE "+std::string(c->name)+" :"+text);
1777 virtual void OnUserMessage(userrec* user, void* dest, int target_type, std::string text)
1779 if (target_type == TYPE_USER)
1781 // route private messages which are targetted at clients only to the server
1782 // which needs to receive them
1783 userrec* d = (userrec*)dest;
1784 if ((std::string(d->server) != Srv->GetServerName()) && (std::string(user->server) == Srv->GetServerName()))
1786 std::deque<std::string> params;
1788 params.push_back(d->nick);
1789 params.push_back(":"+text);
1790 DoOneToOne(user->nick,"PRIVMSG",params,d->server);
1795 if (std::string(user->server) == Srv->GetServerName())
1797 chanrec *c = (chanrec*)dest;
1798 std::deque<TreeServer*> list = GetListOfServersForChannel(c);
1799 for (unsigned int i = 0; i < list.size(); i++)
1801 TreeSocket* Sock = list[i]->GetSocket();
1803 Sock->WriteLine(":"+std::string(user->nick)+" PRIVMSG "+std::string(c->name)+" :"+text);
1809 virtual void OnBackgroundTimer(time_t curtime)
1811 AutoConnectServers(curtime);
1814 virtual void OnUserJoin(userrec* user, chanrec* channel)
1816 // Only do this for local users
1817 if (std::string(user->server) == Srv->GetServerName())
1819 std::deque<std::string> params;
1821 params.push_back(channel->name);
1824 // if the channel has a key, force the join by emulating the key.
1825 params.push_back(channel->key);
1827 if (channel->GetUserCounter() > 1)
1829 // not the first in the channel
1830 DoOneToMany(user->nick,"JOIN",params);
1834 // first in the channel, set up their permissions
1835 // and the channel TS with FJOIN.
1837 snprintf(ts,24,"%lu",(unsigned long)channel->age);
1839 params.push_back(channel->name);
1840 params.push_back(ts);
1841 params.push_back("@"+std::string(user->nick));
1842 DoOneToMany(Srv->GetServerName(),"FJOIN",params);
1847 virtual void OnUserPart(userrec* user, chanrec* channel)
1849 if (std::string(user->server) == Srv->GetServerName())
1851 std::deque<std::string> params;
1853 params.push_back(channel->name);
1854 DoOneToMany(user->nick,"PART",params);
1858 virtual void OnUserConnect(userrec* user)
1860 char agestr[MAXBUF];
1861 if (std::string(user->server) == Srv->GetServerName())
1863 std::deque<std::string> params;
1864 snprintf(agestr,MAXBUF,"%lu",(unsigned long)user->age);
1866 params.push_back(agestr);
1867 params.push_back(user->nick);
1868 params.push_back(user->host);
1869 params.push_back(user->dhost);
1870 params.push_back(user->ident);
1871 params.push_back("+"+std::string(user->modes));
1872 params.push_back(user->ip);
1873 params.push_back(":"+std::string(user->fullname));
1874 DoOneToMany(Srv->GetServerName(),"NICK",params);
1878 virtual void OnUserQuit(userrec* user, std::string reason)
1880 if (std::string(user->server) == Srv->GetServerName())
1882 std::deque<std::string> params;
1883 params.push_back(":"+reason);
1884 DoOneToMany(user->nick,"QUIT",params);
1888 virtual void OnUserPostNick(userrec* user, std::string oldnick)
1890 if (std::string(user->server) == Srv->GetServerName())
1892 std::deque<std::string> params;
1893 params.push_back(user->nick);
1894 DoOneToMany(oldnick,"NICK",params);
1898 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, std::string reason)
1900 if (std::string(source->server) == Srv->GetServerName())
1902 std::deque<std::string> params;
1903 params.push_back(chan->name);
1904 params.push_back(user->nick);
1905 params.push_back(":"+reason);
1906 DoOneToMany(source->nick,"KICK",params);
1910 virtual void OnRemoteKill(userrec* source, userrec* dest, std::string reason)
1912 std::deque<std::string> params;
1913 params.push_back(dest->nick);
1914 params.push_back(":"+reason);
1915 DoOneToMany(source->nick,"KILL",params);
1918 virtual void OnRehash(std::string parameter)
1920 if (parameter != "")
1922 std::deque<std::string> params;
1923 params.push_back(parameter);
1924 DoOneToMany(Srv->GetServerName(),"REHASH",params);
1926 if (Srv->MatchText(Srv->GetServerName(),parameter))
1928 Srv->SendOpers("*** Remote rehash initiated from server \002"+Srv->GetServerName()+"\002.");
1929 Srv->RehashServer();
1932 ReadConfiguration(false);
1935 // note: the protocol does not allow direct umode +o except
1936 // via NICK with 8 params. sending OPERTYPE infers +o modechange
1938 virtual void OnOper(userrec* user, std::string opertype)
1940 if (std::string(user->server) == Srv->GetServerName())
1942 std::deque<std::string> params;
1943 params.push_back(opertype);
1944 DoOneToMany(user->nick,"OPERTYPE",params);
1948 virtual void OnMode(userrec* user, void* dest, int target_type, std::string text)
1950 if (std::string(user->server) == Srv->GetServerName())
1952 if (target_type == TYPE_USER)
1954 userrec* u = (userrec*)dest;
1955 std::deque<std::string> params;
1956 params.push_back(u->nick);
1957 params.push_back(text);
1958 DoOneToMany(user->nick,"MODE",params);
1962 chanrec* c = (chanrec*)dest;
1963 std::deque<std::string> params;
1964 params.push_back(c->name);
1965 params.push_back(text);
1966 DoOneToMany(user->nick,"MODE",params);
1971 virtual void ProtoSendMode(void* opaque, int target_type, void* target, std::string modeline)
1973 TreeSocket* s = (TreeSocket*)opaque;
1976 if (target_type == TYPE_USER)
1978 userrec* u = (userrec*)target;
1979 s->WriteLine(":"+Srv->GetServerName()+" FMODE "+u->nick+" "+modeline);
1983 chanrec* c = (chanrec*)target;
1984 s->WriteLine(":"+Srv->GetServerName()+" FMODE "+c->name+" "+modeline);
1989 virtual ~ModuleSpanningTree()
1994 virtual Version GetVersion()
1996 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
2001 class ModuleSpanningTreeFactory : public ModuleFactory
2004 ModuleSpanningTreeFactory()
2008 ~ModuleSpanningTreeFactory()
2012 virtual Module * CreateModule()
2014 TreeProtocolModule = new ModuleSpanningTree;
2015 return TreeProtocolModule;
2021 extern "C" void * init_module( void )
2023 return new ModuleSpanningTreeFactory;