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
90 UserCount = OperCount = 0;
91 VersionString = GetVersionString();
94 TreeServer(std::string Name, std::string Desc) : ServerName(Name), ServerDesc(Desc)
98 UserCount = OperCount = 0;
99 VersionString = GetVersionString();
102 TreeServer(std::string Name, std::string Desc, TreeServer* Above, TreeSocket* Sock) : Parent(Above), ServerName(Name), ServerDesc(Desc), Socket(Sock)
105 UserCount = OperCount = 0;
106 this->SetNextPingTime(time(NULL) + 60);
110 std::string GetName()
112 return this->ServerName;
115 std::string GetDesc()
117 return this->ServerDesc;
120 std::string GetVersion()
122 return this->VersionString;
125 void SetNextPingTime(time_t t)
128 LastPingWasGood = false;
131 time_t NextPingTime()
133 return this->NextPing;
136 bool AnsweredLastPing()
138 return LastPingWasGood;
143 LastPingWasGood = true;
148 return this->UserCount;
153 return this->OperCount;
156 TreeSocket* GetSocket()
161 TreeServer* GetParent()
166 std::string GetVersion()
168 return VersionString;
171 void SetVersion(std::string Version)
173 VersionString = Version;
176 unsigned int ChildCount()
178 return Children.size();
181 TreeServer* GetChild(unsigned int n)
183 if (n < Children.size())
193 void AddChild(TreeServer* Child)
195 Children.push_back(Child);
198 bool DelChild(TreeServer* Child)
200 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
211 // removes child nodes of this node, and of that node, etc etc
214 bool stillchildren = true;
215 while (stillchildren)
217 stillchildren = false;
218 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
220 TreeServer* s = (TreeServer*)*a;
224 stillchildren = true;
238 std::string SendPass;
239 std::string RecvPass;
240 unsigned long AutoConnect;
241 time_t NextConnectTime;
244 /* $ModDesc: Povides a spanning tree server link protocol */
248 TreeServer *TreeRoot;
249 std::vector<Link> LinkBlocks;
251 TreeServer* RouteEnumerate(TreeServer* Current, std::string ServerName)
253 if (Current->GetName() == ServerName)
255 for (unsigned int q = 0; q < Current->ChildCount(); q++)
257 TreeServer* found = RouteEnumerate(Current->GetChild(q),ServerName);
266 // Returns the locally connected server we must route a
267 // message through to reach server 'ServerName'. This
268 // only applies to one-to-one and not one-to-many routing.
269 TreeServer* BestRouteTo(std::string ServerName)
271 if (ServerName.c_str() == TreeRoot->GetName())
275 // first, find the server by recursively walking the tree
276 TreeServer* Found = RouteEnumerate(TreeRoot,ServerName);
277 // did we find it? If not, they did something wrong, abort.
284 // The server exists, follow its parent nodes until
285 // the parent of the current is 'TreeRoot', we know
286 // then that this is a directly-connected server.
287 while ((Found) && (Found->GetParent() != TreeRoot))
289 Found = Found->GetParent();
295 bool LookForServer(TreeServer* Current, std::string ServerName)
297 if (ServerName == Current->GetName())
299 for (unsigned int q = 0; q < Current->ChildCount(); q++)
301 if (LookForServer(Current->GetChild(q),ServerName))
309 void RFindServer(TreeServer* Current, std::string ServerName)
311 if ((ServerName == Current->GetName()) && (!Found))
318 for (unsigned int q = 0; q < Current->ChildCount(); q++)
321 RFindServer(Current->GetChild(q),ServerName);
327 void RFindServerMask(TreeServer* Current, std::string ServerName)
329 if (Srv->MatchText(Current->GetName(),ServerName) && (!Found))
336 for (unsigned int q = 0; q < Current->ChildCount(); q++)
339 RFindServerMask(Current->GetChild(q),ServerName);
344 TreeServer* FindServer(std::string ServerName)
347 RFindServer(TreeRoot,ServerName);
351 TreeServer* FindServerMask(std::string ServerName)
354 RFindServerMask(TreeRoot,ServerName);
358 bool IsServer(std::string ServerName)
360 return LookForServer(TreeRoot,ServerName);
363 class TreeSocket : public InspSocket
366 std::string in_buffer;
367 ServerState LinkState;
368 std::string InboundServerName;
369 std::string InboundDescription;
371 int num_lost_servers;
373 bool LastPingWasGood;
377 TreeSocket(std::string host, int port, bool listening, unsigned long maxtime)
378 : InspSocket(host, port, listening, maxtime)
381 this->LinkState = LISTENER;
384 TreeSocket(std::string host, int port, bool listening, unsigned long maxtime, std::string ServerName)
385 : InspSocket(host, port, listening, maxtime)
388 this->LinkState = CONNECTING;
391 TreeSocket(int newfd, char* ip)
392 : InspSocket(newfd, ip)
394 this->LinkState = WAIT_AUTH_1;
397 virtual bool OnConnected()
399 if (this->LinkState == CONNECTING)
401 Srv->SendOpers("*** Connection to "+myhost+"["+this->GetIP()+"] established.");
402 // we should send our details here.
403 // if the other side is satisfied, they send theirs.
404 // we do not need to change state here.
405 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
407 if (x->Name == this->myhost)
409 // found who we're supposed to be connecting to, send the neccessary gubbins.
410 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
418 virtual void OnError(InspSocketError e)
422 virtual int OnDisconnect()
427 // recursively send the server tree with distances as hops
428 void SendServers(TreeServer* Current, TreeServer* s, int hops)
431 for (unsigned int q = 0; q < Current->ChildCount(); q++)
433 TreeServer* recursive_server = Current->GetChild(q);
434 if (recursive_server != s)
436 // :source.server SERVER server.name hops :Description
437 snprintf(command,1024,":%s SERVER %s * %d :%s",Current->GetName().c_str(),recursive_server->GetName().c_str(),hops,recursive_server->GetDesc().c_str());
438 this->WriteLine(command);
439 this->WriteLine(":"+recursive_server->GetName()+" VERSION :"+recursive_server->GetVersion());
440 // down to next level
441 this->SendServers(recursive_server, s, hops+1);
446 void SquitServer(TreeServer* Current)
448 // recursively squit the servers attached to 'Current'
449 for (unsigned int q = 0; q < Current->ChildCount(); q++)
451 TreeServer* recursive_server = Current->GetChild(q);
452 this->SquitServer(recursive_server);
454 // Now we've whacked the kids, whack self
456 bool quittingpeople = true;
457 while (quittingpeople)
459 quittingpeople = false;
460 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
462 if (!strcasecmp(u->second->server,Current->GetName().c_str()))
464 Srv->QuitUser(u->second,Current->GetName()+" "+std::string(Srv->GetServerName()));
466 quittingpeople = true;
473 void Squit(TreeServer* Current,std::string reason)
477 std::deque<std::string> params;
478 params.push_back(Current->GetName());
479 params.push_back(":"+reason);
480 DoOneToAllButSender(Current->GetParent()->GetName(),"SQUIT",params,Current->GetName());
481 if (Current->GetParent() == TreeRoot)
483 Srv->SendOpers("Server \002"+Current->GetName()+"\002 split: "+reason);
487 Srv->SendOpers("Server \002"+Current->GetName()+"\002 split from server \002"+Current->GetParent()->GetName()+"\002 with reason: "+reason);
489 num_lost_servers = 0;
491 SquitServer(Current);
493 Current->GetParent()->DelChild(Current);
495 WriteOpers("Netsplit complete, lost \002%d\002 users on \002%d\002 servers.", num_lost_users, num_lost_servers);
499 log(DEFAULT,"Squit from unknown server");
503 bool ForceMode(std::string source, std::deque<std::string> params)
505 userrec* who = new userrec;
506 who->fd = FD_MAGIC_NUMBER;
507 if (params.size() < 2)
510 for (unsigned int q = 0; q < params.size(); q++)
512 modelist[q] = (char*)params[q].c_str();
514 Srv->SendMode(modelist,params.size(),who);
515 DoOneToAllButSender(source,"FMODE",params,source);
520 bool ForceTopic(std::string source, std::deque<std::string> params)
522 // FTOPIC %s %lu %s :%s
523 if (params.size() != 4)
525 std::string channel = params[0];
526 time_t ts = atoi(params[1].c_str());
527 std::string setby = params[2];
528 std::string topic = params[3];
530 chanrec* c = Srv->FindChannel(channel);
533 if ((ts >= c->topicset) || (!*c->topic))
535 strlcpy(c->topic,topic.c_str(),MAXTOPIC);
536 strlcpy(c->setby,setby.c_str(),NICKMAX);
538 WriteChannelWithServ((char*)source.c_str(), c, "TOPIC %s :%s", c->name, c->topic);
543 // all done, send it on its way
544 params[3] = ":" + params[3];
545 DoOneToAllButSender(source,"FTOPIC",params,source);
550 bool ForceJoin(std::string source, std::deque<std::string> params)
552 if (params.size() < 3)
556 char modestring[MAXBUF];
557 char* mode_users[127];
558 mode_users[0] = first;
559 mode_users[1] = modestring;
560 strcpy(mode_users[1],"+");
561 unsigned int modectr = 2;
564 std::string channel = params[0];
565 time_t TS = atoi(params[1].c_str());
568 chanrec* chan = Srv->FindChannel(channel);
573 strlcpy(mode_users[0],channel.c_str(),MAXBUF);
575 // default is a high value, which if we dont have this
576 // channel will let the other side apply their modes.
577 time_t ourTS = time(NULL)+20;
578 chanrec* us = Srv->FindChannel(channel);
584 log(DEBUG,"FJOIN detected, our TS=%lu, their TS=%lu",ourTS,TS);
586 // do this first, so our mode reversals are correctly received by other servers
587 // if there is a TS collision.
588 DoOneToAllButSender(source,"FJOIN",params,source);
590 for (unsigned int usernum = 2; usernum < params.size(); usernum++)
592 // process one channel at a time, applying modes.
593 char* usr = (char*)params[usernum].c_str();
594 char permissions = *usr;
599 mode_users[modectr++] = usr;
600 strlcat(modestring,"o",MAXBUF);
604 mode_users[modectr++] = usr;
605 strlcat(modestring,"h",MAXBUF);
609 mode_users[modectr++] = usr;
610 strlcat(modestring,"v",MAXBUF);
613 who = Srv->FindNick(usr);
616 Srv->JoinUserToChannel(who,channel,key);
617 if (modectr >= (MAXMODES-1))
619 // theres a mode for this user. push them onto the mode queue, and flush it
620 // if there are more than MAXMODES to go.
623 log(DEBUG,"Our our channel newer than theirs, accepting their modes");
624 Srv->SendMode(mode_users,modectr,who);
628 log(DEBUG,"Their channel newer than ours, bouncing their modes");
630 std::deque<std::string> params;
631 // modes are now being UNSET...
632 *mode_users[1] = '-';
633 for (unsigned int x = 0; x < modectr; x++)
635 params.push_back(mode_users[x]);
637 // tell everyone to bounce the modes. bad modes, bad!
638 DoOneToMany(Srv->GetServerName(),"FMODE",params);
640 strcpy(mode_users[1],"+");
645 // there werent enough modes built up to flush it during FJOIN,
646 // or, there are a number left over. flush them out.
647 if ((modectr > 2) && (who))
651 log(DEBUG,"Our our channel newer than theirs, accepting their modes");
652 Srv->SendMode(mode_users,modectr,who);
656 log(DEBUG,"Their channel newer than ours, bouncing their modes");
657 std::deque<std::string> params;
658 *mode_users[1] = '-';
659 for (unsigned int x = 0; x < modectr; x++)
661 params.push_back(mode_users[x]);
663 DoOneToMany(Srv->GetServerName(),"FMODE",params);
669 bool IntroduceClient(std::string source, std::deque<std::string> params)
671 if (params.size() < 8)
673 // NICK age nick host dhost ident +modes ip :gecos
675 std::string nick = params[1];
676 std::string host = params[2];
677 std::string dhost = params[3];
678 std::string ident = params[4];
679 time_t age = atoi(params[0].c_str());
680 std::string modes = params[5];
681 while (*(modes.c_str()) == '+')
683 char* m = (char*)modes.c_str();
687 std::string ip = params[6];
688 std::string gecos = params[7];
689 char* tempnick = (char*)nick.c_str();
690 log(DEBUG,"Introduce client %s!%s@%s",tempnick,ident.c_str(),host.c_str());
692 user_hash::iterator iter;
693 iter = clientlist.find(tempnick);
694 if (iter != clientlist.end())
697 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);
698 this->WriteLine(":"+Srv->GetServerName()+" KILL "+tempnick+" :Nickname collision");
702 clientlist[tempnick] = new userrec();
703 clientlist[tempnick]->fd = FD_MAGIC_NUMBER;
704 strlcpy(clientlist[tempnick]->nick, tempnick,NICKMAX);
705 strlcpy(clientlist[tempnick]->host, host.c_str(),160);
706 strlcpy(clientlist[tempnick]->dhost, dhost.c_str(),160);
707 clientlist[tempnick]->server = (char*)FindServerNamePtr(source.c_str());
708 strlcpy(clientlist[tempnick]->ident, ident.c_str(),IDENTMAX);
709 strlcpy(clientlist[tempnick]->fullname, gecos.c_str(),MAXGECOS);
710 clientlist[tempnick]->registered = 7;
711 clientlist[tempnick]->signon = age;
712 strlcpy(clientlist[tempnick]->modes, modes.c_str(),53);
713 strlcpy(clientlist[tempnick]->ip,ip.c_str(),16);
714 for (int i = 0; i < MAXCHANS; i++)
716 clientlist[tempnick]->chans[i].channel = NULL;
717 clientlist[tempnick]->chans[i].uc_modes = 0;
719 params[7] = ":" + params[7];
720 DoOneToAllButSender(source,"NICK",params,source);
724 void SendFJoins(TreeServer* Current, chanrec* c)
727 snprintf(list,MAXBUF,":%s FJOIN %s %lu",Srv->GetServerName().c_str(),c->name,(unsigned long)c->age);
728 std::vector<char*> *ulist = c->GetUsers();
729 for (unsigned int i = 0; i < ulist->size(); i++)
731 char* o = (*ulist)[i];
732 userrec* otheruser = (userrec*)o;
733 strlcat(list," ",MAXBUF);
734 strlcat(list,cmode(otheruser,c),MAXBUF);
735 strlcat(list,otheruser->nick,MAXBUF);
736 if (strlen(list)>(480-NICKMAX))
738 this->WriteLine(list);
739 snprintf(list,MAXBUF,":%s FJOIN %s %lu",Srv->GetServerName().c_str(),c->name,(unsigned long)c->age);
742 if (list[strlen(list)-1] != ':')
744 this->WriteLine(list);
748 void SendChannelModes(TreeServer* Current)
751 for (chan_hash::iterator c = chanlist.begin(); c != chanlist.end(); c++)
753 SendFJoins(Current, c->second);
754 snprintf(data,MAXBUF,":%s FMODE %s +%s",Srv->GetServerName().c_str(),c->second->name,chanmodes(c->second));
755 this->WriteLine(data);
756 if (*c->second->topic)
758 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);
759 this->WriteLine(data);
761 for (BanList::iterator b = c->second->bans.begin(); b != c->second->bans.end(); b++)
763 snprintf(data,MAXBUF,":%s FMODE %s +b %s",Srv->GetServerName().c_str(),c->second->name,b->data);
764 this->WriteLine(data);
766 FOREACH_MOD OnSyncChannel(c->second,(Module*)TreeProtocolModule,(void*)this);
770 // send all users and their channels
771 void SendUsers(TreeServer* Current)
774 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
776 if (u->second->registered == 7)
778 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);
779 this->WriteLine(data);
780 if (strchr(u->second->modes,'o'))
782 this->WriteLine(":"+std::string(u->second->nick)+" OPERTYPE "+std::string(u->second->oper));
784 //char* chl = chlist(u->second,u->second);
787 // this->WriteLine(":"+std::string(u->second->nick)+" FJOIN "+std::string(chl));
789 FOREACH_MOD OnSyncUser(u->second,(Module*)TreeProtocolModule,(void*)this);
794 void DoBurst(TreeServer* s)
796 Srv->SendOpers("*** Bursting to "+s->GetName()+".");
797 this->WriteLine("BURST");
798 // send our version string
799 this->WriteLine(":"+s->GetName()+" VERSION :"+GetVersionString());
801 this->SendServers(TreeRoot,s,1);
802 // Send users and their channels
804 // Send everything else (channel modes etc)
805 this->SendChannelModes(s);
806 this->WriteLine("ENDBURST");
809 virtual bool OnDataReady()
811 char* data = this->Read();
814 this->in_buffer += data;
815 while (in_buffer.find("\n") != std::string::npos)
817 char* line = (char*)in_buffer.c_str();
818 std::string ret = "";
819 while ((*line != '\n') && (strlen(line)))
824 if ((*line == '\n') || (*line == '\r'))
827 if (!this->ProcessLine(ret))
833 return (data != NULL);
836 int WriteLine(std::string line)
838 return this->Write(line + "\r\n");
841 bool Error(std::deque<std::string> params)
843 if (params.size() < 1)
845 std::string Errmsg = params[0];
846 std::string SName = myhost;
847 if (InboundServerName != "")
849 SName = InboundServerName;
851 Srv->SendOpers("*** ERROR from "+SName+": "+Errmsg);
852 // we will return false to cause the socket to close.
856 bool OperType(std::string prefix, std::deque<std::string> params)
858 if (params.size() != 1)
860 std::string opertype = params[0];
861 userrec* u = Srv->FindNick(prefix);
864 strlcpy(u->oper,opertype.c_str(),NICKMAX);
865 if (!strchr(u->modes,'o'))
867 strcat(u->modes,"o");
869 DoOneToAllButSender(u->server,"OPERTYPE",params,u->server);
874 bool RemoteRehash(std::string prefix, std::deque<std::string> params)
876 if (params.size() < 1)
878 std::string servermask = params[0];
879 if (Srv->MatchText(Srv->GetServerName(),servermask))
881 Srv->SendOpers("*** Remote rehash initiated from server \002"+prefix+"\002.");
883 ReadConfiguration(false);
885 DoOneToAllButSender(prefix,"REHASH",params,prefix);
889 bool RemoteKill(std::string prefix, std::deque<std::string> params)
891 if (params.size() != 2)
893 std::string nick = params[0];
894 std::string reason = params[1];
895 userrec* u = Srv->FindNick(prefix);
896 userrec* who = Srv->FindNick(nick);
899 std::string sourceserv = prefix;
902 sourceserv = u->server;
904 params[1] = ":" + params[1];
905 DoOneToAllButSender(prefix,"KILL",params,sourceserv);
906 Srv->QuitUser(who,reason);
911 bool LocalPong(std::string prefix, std::deque<std::string> params)
913 if (params.size() < 1)
915 TreeServer* ServerSource = FindServer(prefix);
918 ServerSource->SetPingFlag();
923 bool ServerVersion(std::string prefix, std::deque<std::string> params)
925 if (params.size() < 1)
927 TreeServer* ServerSource = FindServer(prefix);
930 ServerSource->SetVersion(params[0]);
932 DoOneToAllButSender(prefix,"VERSION",params);
936 bool LocalPing(std::string prefix, std::deque<std::string> params)
938 if (params.size() < 1)
940 std::string stufftobounce = params[0];
941 this->WriteLine(":"+Srv->GetServerName()+" PONG "+stufftobounce);
945 bool RemoteServer(std::string prefix, std::deque<std::string> params)
947 if (params.size() < 4)
949 std::string servername = params[0];
950 std::string password = params[1];
951 // hopcount is not used for a remote server, we calculate this ourselves
952 std::string description = params[3];
953 TreeServer* ParentOfThis = FindServer(prefix);
956 this->WriteLine("ERROR :Protocol error - Introduced remote server from unknown server "+prefix);
959 TreeServer* CheckDupe = FindServer(servername);
962 this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
965 TreeServer* Node = new TreeServer(servername,description,ParentOfThis,NULL);
966 ParentOfThis->AddChild(Node);
967 params[3] = ":" + params[3];
968 DoOneToAllButSender(prefix,"SERVER",params,prefix);
969 Srv->SendOpers("*** Server \002"+prefix+"\002 introduced server \002"+servername+"\002 ("+description+")");
973 bool Outbound_Reply_Server(std::deque<std::string> params)
975 if (params.size() < 4)
977 std::string servername = params[0];
978 std::string password = params[1];
979 int hops = atoi(params[2].c_str());
982 this->WriteLine("ERROR :Server too far away for authentication");
985 std::string description = params[3];
986 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
988 if ((x->Name == servername) && (x->RecvPass == password))
990 TreeServer* CheckDupe = FindServer(servername);
993 this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
996 // Begin the sync here. this kickstarts the
997 // other side, waiting in WAIT_AUTH_2 state,
998 // into starting their burst, as it shows
1000 this->LinkState = CONNECTED;
1001 // we should add the details of this server now
1002 // to the servers tree, as a child of the root
1004 TreeServer* Node = new TreeServer(servername,description,TreeRoot,this);
1005 TreeRoot->AddChild(Node);
1006 params[3] = ":" + params[3];
1007 DoOneToAllButSender(TreeRoot->GetName(),"SERVER",params,servername);
1008 this->DoBurst(Node);
1012 this->WriteLine("ERROR :Invalid credentials");
1016 bool Inbound_Server(std::deque<std::string> params)
1018 if (params.size() < 4)
1020 std::string servername = params[0];
1021 std::string password = params[1];
1022 int hops = atoi(params[2].c_str());
1025 this->WriteLine("ERROR :Server too far away for authentication");
1028 std::string description = params[3];
1029 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
1031 if ((x->Name == servername) && (x->RecvPass == password))
1033 TreeServer* CheckDupe = FindServer(servername);
1036 this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
1039 Srv->SendOpers("*** Verified incoming server connection from \002"+servername+"\002["+this->GetIP()+"] ("+description+")");
1040 this->InboundServerName = servername;
1041 this->InboundDescription = description;
1042 // this is good. Send our details: Our server name and description and hopcount of 0,
1043 // along with the sendpass from this block.
1044 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
1045 // move to the next state, we are now waiting for THEM.
1046 this->LinkState = WAIT_AUTH_2;
1050 this->WriteLine("ERROR :Invalid credentials");
1054 std::deque<std::string> Split(std::string line, bool stripcolon)
1056 std::deque<std::string> n;
1057 if (!strchr(line.c_str(),' '))
1062 std::stringstream s(line);
1063 std::string param = "";
1082 if ((param == ":") && (item > 0))
1105 bool ProcessLine(std::string line)
1107 char* l = (char*)line.c_str();
1108 while ((strlen(l)) && (l[strlen(l)-1] == '\r') || (l[strlen(l)-1] == '\n'))
1109 l[strlen(l)-1] = '\0';
1113 Srv->Log(DEBUG,"IN: '"+line+"'");
1114 std::deque<std::string> params = this->Split(line,true);
1115 std::string command = "";
1116 std::string prefix = "";
1117 if (((params[0].c_str())[0] == ':') && (params.size() > 1))
1120 command = params[1];
1121 char* pref = (char*)prefix.c_str();
1129 command = params[0];
1133 switch (this->LinkState)
1138 // Waiting for SERVER command from remote server. Server initiating
1139 // the connection sends the first SERVER command, listening server
1140 // replies with theirs if its happy, then if the initiator is happy,
1141 // it starts to send its net sync, which starts the merge, otherwise
1142 // it sends an ERROR.
1143 if (command == "SERVER")
1145 return this->Inbound_Server(params);
1147 else if (command == "ERROR")
1149 return this->Error(params);
1153 // Waiting for start of other side's netmerge to say they liked our
1155 if (command == "SERVER")
1157 // cant do this, they sent it to us in the WAIT_AUTH_1 state!
1161 else if (command == "BURST")
1163 this->LinkState = CONNECTED;
1164 Node = new TreeServer(InboundServerName,InboundDescription,TreeRoot,this);
1165 TreeRoot->AddChild(Node);
1167 params.push_back(InboundServerName);
1168 params.push_back("*");
1169 params.push_back("1");
1170 params.push_back(":"+InboundDescription);
1171 DoOneToAllButSender(TreeRoot->GetName(),"SERVER",params,InboundServerName);
1172 this->DoBurst(Node);
1174 else if (command == "ERROR")
1176 return this->Error(params);
1181 this->WriteLine("ERROR :Internal error -- listening socket accepted its own descriptor!!!");
1185 if (command == "SERVER")
1187 // another server we connected to, which was in WAIT_AUTH_1 state,
1188 // has just sent us their credentials. If we get this far, theyre
1189 // happy with OUR credentials, and they are now in WAIT_AUTH_2 state.
1190 // if we're happy with this, we should send our netburst which
1191 // kickstarts the merge.
1192 return this->Outbound_Reply_Server(params);
1194 else if (command == "ERROR")
1196 return this->Error(params);
1200 // This is the 'authenticated' state, when all passwords
1201 // have been exchanged and anything past this point is taken
1203 std::string target = "";
1204 if ((command == "NICK") && (params.size() > 1))
1206 return this->IntroduceClient(prefix,params);
1208 else if (command == "FJOIN")
1210 return this->ForceJoin(prefix,params);
1212 else if (command == "SERVER")
1214 return this->RemoteServer(prefix,params);
1216 else if (command == "ERROR")
1218 return this->Error(params);
1220 else if (command == "OPERTYPE")
1222 return this->OperType(prefix,params);
1224 else if (command == "FMODE")
1226 return this->ForceMode(prefix,params);
1228 else if (command == "KILL")
1230 return this->RemoteKill(prefix,params);
1232 else if (command == "FTOPIC")
1234 return this->ForceTopic(prefix,params);
1236 else if (command == "REHASH")
1238 return this->RemoteRehash(prefix,params);
1240 else if (command == "PING")
1242 return this->LocalPing(prefix,params);
1244 else if (command == "PONG")
1246 return this->LocalPong(prefix,params);
1248 else if (command == "VERSION")
1250 return this->ServerVersion(prefix,params);
1252 else if (command == "SQUIT")
1254 if (params.size() == 2)
1256 this->Squit(FindServer(params[0]),params[1]);
1262 // not a special inter-server command.
1263 // Emulate the actual user doing the command,
1264 // this saves us having a huge ugly parser.
1265 userrec* who = Srv->FindNick(prefix);
1266 std::string sourceserv = this->myhost;
1267 if (this->InboundServerName != "")
1269 sourceserv = this->InboundServerName;
1274 target = who->server;
1275 char* strparams[127];
1276 for (unsigned int q = 0; q < params.size(); q++)
1278 strparams[q] = (char*)params[q].c_str();
1280 Srv->CallCommandHandler(command, strparams, params.size(), who);
1284 // its not a user. Its either a server, or somethings screwed up.
1285 if (IsServer(prefix))
1287 target = Srv->GetServerName();
1291 log(DEBUG,"Command with unknown origin '%s'",prefix.c_str());
1295 return DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1304 virtual std::string GetName()
1306 std::string sourceserv = this->myhost;
1307 if (this->InboundServerName != "")
1309 sourceserv = this->InboundServerName;
1314 virtual void OnTimeout()
1316 if (this->LinkState == CONNECTING)
1318 Srv->SendOpers("*** CONNECT: Connection to \002"+myhost+"\002 timed out.");
1322 virtual void OnClose()
1324 // Connection closed.
1325 // If the connection is fully up (state CONNECTED)
1326 // then propogate a netsplit to all peers.
1327 std::string quitserver = this->myhost;
1328 if (this->InboundServerName != "")
1330 quitserver = this->InboundServerName;
1332 TreeServer* s = FindServer(quitserver);
1335 Squit(s,"Remote host closed the connection");
1339 virtual int OnIncomingConnection(int newsock, char* ip)
1341 TreeSocket* s = new TreeSocket(newsock, ip);
1347 void AddThisServer(TreeServer* server, std::deque<TreeServer*> &list)
1349 for (unsigned int c = 0; c < list.size(); c++)
1351 if (list[c] == server)
1356 list.push_back(server);
1359 // returns a list of DIRECT servernames for a specific channel
1360 std::deque<TreeServer*> GetListOfServersForChannel(chanrec* c)
1362 std::deque<TreeServer*> list;
1363 std::vector<char*> *ulist = c->GetUsers();
1364 for (unsigned int i = 0; i < ulist->size(); i++)
1366 char* o = (*ulist)[i];
1367 userrec* otheruser = (userrec*)o;
1368 if (std::string(otheruser->server) != Srv->GetServerName())
1370 TreeServer* best = BestRouteTo(otheruser->server);
1372 AddThisServer(best,list);
1378 bool DoOneToAllButSenderRaw(std::string data,std::string omit,std::string prefix,std::string command,std::deque<std::string> params)
1380 TreeServer* omitroute = BestRouteTo(omit);
1381 if ((command == "NOTICE") || (command == "PRIVMSG"))
1383 if ((params.size() >= 2) && (*(params[0].c_str()) != '$'))
1385 if (*(params[0].c_str()) != '#')
1387 // special routing for private messages/notices
1388 userrec* d = Srv->FindNick(params[0]);
1391 std::deque<std::string> par;
1393 par.push_back(params[0]);
1394 par.push_back(":"+params[1]);
1395 DoOneToOne(prefix,command,par,d->server);
1401 log(DEBUG,"Channel privmsg going to chan %s",params[0].c_str());
1402 chanrec* c = Srv->FindChannel(params[0]);
1405 std::deque<TreeServer*> list = GetListOfServersForChannel(c);
1406 log(DEBUG,"Got a list of %d servers",list.size());
1407 for (unsigned int i = 0; i < list.size(); i++)
1409 TreeSocket* Sock = list[i]->GetSocket();
1410 if ((Sock) && (list[i]->GetName() != omit) && (omitroute != list[i]))
1412 log(DEBUG,"Writing privmsg to server %s",list[i]->GetName().c_str());
1413 Sock->WriteLine(data);
1421 for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1423 TreeServer* Route = TreeRoot->GetChild(x);
1424 if ((Route->GetSocket()) && (Route->GetName() != omit) && (omitroute != Route))
1426 TreeSocket* Sock = Route->GetSocket();
1427 Sock->WriteLine(data);
1433 bool DoOneToAllButSender(std::string prefix, std::string command, std::deque<std::string> params, std::string omit)
1435 TreeServer* omitroute = BestRouteTo(omit);
1436 std::string FullLine = ":" + prefix + " " + command;
1437 for (unsigned int x = 0; x < params.size(); x++)
1439 FullLine = FullLine + " " + params[x];
1441 for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1443 TreeServer* Route = TreeRoot->GetChild(x);
1444 // Send the line IF:
1445 // The route has a socket (its a direct connection)
1446 // The route isnt the one to be omitted
1447 // The route isnt the path to the one to be omitted
1448 if ((Route->GetSocket()) && (Route->GetName() != omit) && (omitroute != Route))
1450 TreeSocket* Sock = Route->GetSocket();
1451 Sock->WriteLine(FullLine);
1457 bool DoOneToMany(std::string prefix, std::string command, std::deque<std::string> params)
1459 std::string FullLine = ":" + prefix + " " + command;
1460 for (unsigned int x = 0; x < params.size(); x++)
1462 FullLine = FullLine + " " + params[x];
1464 for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1466 TreeServer* Route = TreeRoot->GetChild(x);
1467 if (Route->GetSocket())
1469 TreeSocket* Sock = Route->GetSocket();
1470 Sock->WriteLine(FullLine);
1476 bool DoOneToOne(std::string prefix, std::string command, std::deque<std::string> params, std::string target)
1478 TreeServer* Route = BestRouteTo(target);
1481 std::string FullLine = ":" + prefix + " " + command;
1482 for (unsigned int x = 0; x < params.size(); x++)
1484 FullLine = FullLine + " " + params[x];
1486 if (Route->GetSocket())
1488 TreeSocket* Sock = Route->GetSocket();
1489 Sock->WriteLine(FullLine);
1499 std::vector<TreeSocket*> Bindings;
1501 void ReadConfiguration(bool rebind)
1505 for (int j =0; j < Conf->Enumerate("bind"); j++)
1507 std::string Type = Conf->ReadValue("bind","type",j);
1508 std::string IP = Conf->ReadValue("bind","address",j);
1509 long Port = Conf->ReadInteger("bind","port",j,true);
1510 if (Type == "servers")
1516 TreeSocket* listener = new TreeSocket(IP.c_str(),Port,true,10);
1517 if (listener->GetState() == I_LISTENING)
1519 Srv->AddSocket(listener);
1520 Bindings.push_back(listener);
1524 log(DEFAULT,"m_spanningtree: Warning: Failed to bind server port %d",Port);
1532 for (int j =0; j < Conf->Enumerate("link"); j++)
1535 L.Name = Conf->ReadValue("link","name",j);
1536 L.IPAddr = Conf->ReadValue("link","ipaddr",j);
1537 L.Port = Conf->ReadInteger("link","port",j,true);
1538 L.SendPass = Conf->ReadValue("link","sendpass",j);
1539 L.RecvPass = Conf->ReadValue("link","recvpass",j);
1540 L.AutoConnect = Conf->ReadInteger("link","autoconnect",j,true);
1541 L.NextConnectTime = time(NULL) + L.AutoConnect;
1542 LinkBlocks.push_back(L);
1543 log(DEBUG,"m_spanningtree: Read server %s with host %s:%d",L.Name.c_str(),L.IPAddr.c_str(),L.Port);
1548 class ModuleSpanningTree : public Module
1550 std::vector<TreeSocket*> Bindings;
1556 ModuleSpanningTree()
1559 Conf = new ConfigReader;
1562 // Create the root of the tree
1563 TreeRoot = new TreeServer(Srv->GetServerName(),Srv->GetServerDescription());
1565 ReadConfiguration(true);
1568 void ShowLinks(TreeServer* Current, userrec* user, int hops)
1570 std::string Parent = TreeRoot->GetName();
1571 if (Current->GetParent())
1573 Parent = Current->GetParent()->GetName();
1575 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1577 ShowLinks(Current->GetChild(q),user,hops+1);
1579 WriteServ(user->fd,"364 %s %s %s :%d %s",user->nick,Current->GetName().c_str(),Parent.c_str(),hops,Current->GetDesc().c_str());
1582 int CountLocalServs()
1584 return TreeRoot->ChildCount();
1587 void CountServsRecursive(TreeServer* Current)
1590 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1592 CountServsRecursive(Current->GetChild(q));
1599 CountServsRecursive(TreeRoot);
1603 void HandleLinks(char** parameters, int pcnt, userrec* user)
1605 ShowLinks(TreeRoot,user,0);
1606 WriteServ(user->fd,"365 %s * :End of /LINKS list.",user->nick);
1610 void HandleLusers(char** parameters, int pcnt, userrec* user)
1612 WriteServ(user->fd,"251 %s :There are %d users and %d invisible on %d servers",user->nick,usercnt()-usercount_invisible(),usercount_invisible(),this->CountServs());
1613 WriteServ(user->fd,"252 %s %d :operator(s) online",user->nick,usercount_opers());
1614 WriteServ(user->fd,"253 %s %d :unknown connections",user->nick,usercount_unknown());
1615 WriteServ(user->fd,"254 %s %d :channels formed",user->nick,chancount());
1616 WriteServ(user->fd,"254 %s :I have %d clients and %d servers",user->nick,local_count(),this->CountLocalServs());
1620 // WARNING: NOT THREAD SAFE - DONT GET ANY SMART IDEAS.
1622 void ShowMap(TreeServer* Current, userrec* user, int depth, char matrix[128][80])
1626 for (int t = 0; t < depth; t++)
1628 matrix[line][t] = ' ';
1630 strlcpy(&matrix[line][depth],Current->GetName().c_str(),80);
1632 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1634 ShowMap(Current->GetChild(q),user,depth+2,matrix);
1639 // Ok, prepare to be confused.
1640 // After much mulling over how to approach this, it struck me that
1641 // the 'usual' way of doing a /MAP isnt the best way. Instead of
1642 // keeping track of a ton of ascii characters, and line by line
1643 // under recursion working out where to place them using multiplications
1644 // and divisons, we instead render the map onto a backplane of characters
1645 // (a character matrix), then draw the branches as a series of "L" shapes
1646 // from the nodes. This is not only friendlier on CPU it uses less stack.
1648 void HandleMap(char** parameters, int pcnt, userrec* user)
1650 // This array represents a virtual screen which we will
1651 // "scratch" draw to, as the console device of an irc
1652 // client does not provide for a proper terminal.
1653 char matrix[128][80];
1654 for (unsigned int t = 0; t < 128; t++)
1656 matrix[t][0] = '\0';
1659 // The only recursive bit is called here.
1660 ShowMap(TreeRoot,user,0,matrix);
1661 // Process each line one by one. The algorithm has a limit of
1662 // 128 servers (which is far more than a spanning tree should have
1663 // anyway, so we're ok). This limit can be raised simply by making
1664 // the character matrix deeper, 128 rows taking 10k of memory.
1665 for (int l = 1; l < line; l++)
1667 // scan across the line looking for the start of the
1668 // servername (the recursive part of the algorithm has placed
1669 // the servers at indented positions depending on what they
1671 int first_nonspace = 0;
1672 while (matrix[l][first_nonspace] == ' ')
1677 // Draw the `- (corner) section: this may be overwritten by
1678 // another L shape passing along the same vertical pane, becoming
1679 // a |- (branch) section instead.
1680 matrix[l][first_nonspace] = '-';
1681 matrix[l][first_nonspace-1] = '`';
1683 // Draw upwards until we hit the parent server, causing possibly
1684 // other corners (`-) to become branches (|-)
1685 while ((matrix[l2][first_nonspace-1] == ' ') || (matrix[l2][first_nonspace-1] == '`'))
1687 matrix[l2][first_nonspace-1] = '|';
1691 // dump the whole lot to the user. This is the easy bit, honest.
1692 for (int t = 0; t < line; t++)
1694 WriteServ(user->fd,"006 %s :%s",user->nick,&matrix[t][0]);
1696 WriteServ(user->fd,"007 %s :End of /MAP",user->nick);
1700 int HandleSquit(char** parameters, int pcnt, userrec* user)
1702 TreeServer* s = FindServerMask(parameters[0]);
1705 TreeSocket* sock = s->GetSocket();
1708 WriteOpers("*** SQUIT: Server \002%s\002 removed from network by %s",parameters[0],user->nick);
1709 sock->Squit(s,"Server quit by "+std::string(user->nick)+"!"+std::string(user->ident)+"@"+std::string(user->host));
1714 WriteServ(user->fd,"NOTICE %s :*** SQUIT: The server \002%s\002 is not directly connected.",user->nick,parameters[0]);
1719 WriteServ(user->fd,"NOTICE %s :*** SQUIT: The server \002%s\002 does not exist on the network.",user->nick,parameters[0]);
1724 void DoPingChecks(time_t curtime)
1726 for (unsigned int j = 0; j < TreeRoot->ChildCount(); j++)
1728 TreeServer* serv = TreeRoot->GetChild(j);
1729 TreeSocket* sock = serv->GetSocket();
1732 if (curtime >= serv->NextPingTime())
1734 if (serv->AnsweredLastPing())
1736 sock->WriteLine(":"+Srv->GetServerName()+" PING "+serv->GetName());
1737 serv->SetNextPingTime(curtime + 60);
1741 // they didnt answer, boot them
1742 WriteOpers("*** Server \002%s\002 pinged out",serv->GetName().c_str());
1743 sock->Squit(serv,"Ping timeout");
1752 void AutoConnectServers(time_t curtime)
1754 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
1756 if ((x->AutoConnect) && (curtime >= x->NextConnectTime))
1758 log(DEBUG,"Auto-Connecting %s",x->Name.c_str());
1759 x->NextConnectTime = curtime + x->AutoConnect;
1760 TreeServer* CheckDupe = FindServer(x->Name);
1763 // an autoconnected server is not connected. Check if its time to connect it
1764 WriteOpers("*** AUTOCONNECT: Auto-connecting server \002%s\002 (%lu seconds until next attempt)",x->Name.c_str(),x->AutoConnect);
1765 TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name);
1766 Srv->AddSocket(newsocket);
1772 int HandleVersion(char** parameters, int pcnt, userrec* user)
1774 // we've already checked if pcnt > 0, so this is safe
1775 TreeServer* found = FindServerMask(parameters[0]);
1778 std::string Version = found->GetVersion();
1779 WriteServ(user->fd,"351 %s :%s",user->nick,Version.c_str());
1783 WriteServ(user->fd,"402 %s :No such server",parameters[0]);
1787 int HandleConnect(char** parameters, int pcnt, userrec* user)
1789 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
1791 if (Srv->MatchText(x->Name.c_str(),parameters[0]))
1793 TreeServer* CheckDupe = FindServer(x->Name);
1796 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);
1797 TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name);
1798 Srv->AddSocket(newsocket);
1803 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());
1808 WriteServ(user->fd,"NOTICE %s :*** CONNECT: No server matching \002%s\002 could be found in the config file.",user->nick,parameters[0]);
1812 virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
1814 if (command == "CONNECT")
1816 return this->HandleConnect(parameters,pcnt,user);
1818 else if (command == "SQUIT")
1820 return this->HandleSquit(parameters,pcnt,user);
1822 else if (command == "MAP")
1824 this->HandleMap(parameters,pcnt,user);
1827 else if (command == "LUSERS")
1829 this->HandleLusers(parameters,pcnt,user);
1832 else if (command == "LINKS")
1834 this->HandleLinks(parameters,pcnt,user);
1837 else if ((command == "VERSION") && (pcnt > 0))
1839 this->HandleVersion(parameters,pcnt,user);
1842 else if (Srv->IsValidModuleCommand(command, pcnt, user))
1844 // this bit of code cleverly routes all module commands
1845 // to all remote severs *automatically* so that modules
1846 // can just handle commands locally, without having
1847 // to have any special provision in place for remote
1848 // commands and linking protocols.
1849 std::deque<std::string> params;
1851 for (int j = 0; j < pcnt; j++)
1853 if (strchr(parameters[j],' '))
1855 params.push_back(":" + std::string(parameters[j]));
1859 params.push_back(std::string(parameters[j]));
1862 DoOneToMany(user->nick,command,params);
1867 virtual void OnGetServerDescription(std::string servername,std::string &description)
1869 TreeServer* s = FindServer(servername);
1872 description = s->GetDesc();
1876 virtual void OnUserInvite(userrec* source,userrec* dest,chanrec* channel)
1878 if (std::string(source->server) == Srv->GetServerName())
1880 std::deque<std::string> params;
1881 params.push_back(dest->nick);
1882 params.push_back(channel->name);
1883 DoOneToMany(source->nick,"INVITE",params);
1887 virtual void OnPostLocalTopicChange(userrec* user, chanrec* chan, std::string topic)
1889 std::deque<std::string> params;
1890 params.push_back(chan->name);
1891 params.push_back(":"+topic);
1892 DoOneToMany(user->nick,"TOPIC",params);
1895 virtual void OnWallops(userrec* user, std::string text)
1897 if (std::string(user->server) == Srv->GetServerName())
1899 std::deque<std::string> params;
1900 params.push_back(":"+text);
1901 DoOneToMany(user->nick,"WALLOPS",params);
1905 virtual void OnUserNotice(userrec* user, void* dest, int target_type, std::string text)
1907 if (target_type == TYPE_USER)
1909 userrec* d = (userrec*)dest;
1910 if ((std::string(d->server) != Srv->GetServerName()) && (std::string(user->server) == Srv->GetServerName()))
1912 std::deque<std::string> params;
1914 params.push_back(d->nick);
1915 params.push_back(":"+text);
1916 DoOneToOne(user->nick,"NOTICE",params,d->server);
1921 if (std::string(user->server) == Srv->GetServerName())
1923 chanrec *c = (chanrec*)dest;
1924 std::deque<TreeServer*> list = GetListOfServersForChannel(c);
1925 for (unsigned int i = 0; i < list.size(); i++)
1927 TreeSocket* Sock = list[i]->GetSocket();
1929 Sock->WriteLine(":"+std::string(user->nick)+" NOTICE "+std::string(c->name)+" :"+text);
1935 virtual void OnUserMessage(userrec* user, void* dest, int target_type, std::string text)
1937 if (target_type == TYPE_USER)
1939 // route private messages which are targetted at clients only to the server
1940 // which needs to receive them
1941 userrec* d = (userrec*)dest;
1942 if ((std::string(d->server) != Srv->GetServerName()) && (std::string(user->server) == Srv->GetServerName()))
1944 std::deque<std::string> params;
1946 params.push_back(d->nick);
1947 params.push_back(":"+text);
1948 DoOneToOne(user->nick,"PRIVMSG",params,d->server);
1953 if (std::string(user->server) == Srv->GetServerName())
1955 chanrec *c = (chanrec*)dest;
1956 std::deque<TreeServer*> list = GetListOfServersForChannel(c);
1957 for (unsigned int i = 0; i < list.size(); i++)
1959 TreeSocket* Sock = list[i]->GetSocket();
1961 Sock->WriteLine(":"+std::string(user->nick)+" PRIVMSG "+std::string(c->name)+" :"+text);
1967 virtual void OnBackgroundTimer(time_t curtime)
1969 AutoConnectServers(curtime);
1970 DoPingChecks(curtime);
1973 virtual void OnUserJoin(userrec* user, chanrec* channel)
1975 // Only do this for local users
1976 if (std::string(user->server) == Srv->GetServerName())
1978 std::deque<std::string> params;
1980 params.push_back(channel->name);
1983 // if the channel has a key, force the join by emulating the key.
1984 params.push_back(channel->key);
1986 if (channel->GetUserCounter() > 1)
1988 // not the first in the channel
1989 DoOneToMany(user->nick,"JOIN",params);
1993 // first in the channel, set up their permissions
1994 // and the channel TS with FJOIN.
1996 snprintf(ts,24,"%lu",(unsigned long)channel->age);
1998 params.push_back(channel->name);
1999 params.push_back(ts);
2000 params.push_back("@"+std::string(user->nick));
2001 DoOneToMany(Srv->GetServerName(),"FJOIN",params);
2006 virtual void OnUserPart(userrec* user, chanrec* channel)
2008 if (std::string(user->server) == Srv->GetServerName())
2010 std::deque<std::string> params;
2012 params.push_back(channel->name);
2013 DoOneToMany(user->nick,"PART",params);
2017 virtual void OnUserConnect(userrec* user)
2019 char agestr[MAXBUF];
2020 if (std::string(user->server) == Srv->GetServerName())
2022 std::deque<std::string> params;
2023 snprintf(agestr,MAXBUF,"%lu",(unsigned long)user->age);
2025 params.push_back(agestr);
2026 params.push_back(user->nick);
2027 params.push_back(user->host);
2028 params.push_back(user->dhost);
2029 params.push_back(user->ident);
2030 params.push_back("+"+std::string(user->modes));
2031 params.push_back(user->ip);
2032 params.push_back(":"+std::string(user->fullname));
2033 DoOneToMany(Srv->GetServerName(),"NICK",params);
2037 virtual void OnUserQuit(userrec* user, std::string reason)
2039 if (std::string(user->server) == Srv->GetServerName())
2041 std::deque<std::string> params;
2042 params.push_back(":"+reason);
2043 DoOneToMany(user->nick,"QUIT",params);
2047 virtual void OnUserPostNick(userrec* user, std::string oldnick)
2049 if (std::string(user->server) == Srv->GetServerName())
2051 std::deque<std::string> params;
2052 params.push_back(user->nick);
2053 DoOneToMany(oldnick,"NICK",params);
2057 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, std::string reason)
2059 if (std::string(source->server) == Srv->GetServerName())
2061 std::deque<std::string> params;
2062 params.push_back(chan->name);
2063 params.push_back(user->nick);
2064 params.push_back(":"+reason);
2065 DoOneToMany(source->nick,"KICK",params);
2069 virtual void OnRemoteKill(userrec* source, userrec* dest, std::string reason)
2071 std::deque<std::string> params;
2072 params.push_back(dest->nick);
2073 params.push_back(":"+reason);
2074 DoOneToMany(source->nick,"KILL",params);
2077 virtual void OnRehash(std::string parameter)
2079 if (parameter != "")
2081 std::deque<std::string> params;
2082 params.push_back(parameter);
2083 DoOneToMany(Srv->GetServerName(),"REHASH",params);
2085 if (Srv->MatchText(Srv->GetServerName(),parameter))
2087 Srv->SendOpers("*** Remote rehash initiated from server \002"+Srv->GetServerName()+"\002.");
2088 Srv->RehashServer();
2091 ReadConfiguration(false);
2094 // note: the protocol does not allow direct umode +o except
2095 // via NICK with 8 params. sending OPERTYPE infers +o modechange
2097 virtual void OnOper(userrec* user, std::string opertype)
2099 if (std::string(user->server) == Srv->GetServerName())
2101 std::deque<std::string> params;
2102 params.push_back(opertype);
2103 DoOneToMany(user->nick,"OPERTYPE",params);
2107 virtual void OnMode(userrec* user, void* dest, int target_type, std::string text)
2109 if (std::string(user->server) == Srv->GetServerName())
2111 if (target_type == TYPE_USER)
2113 userrec* u = (userrec*)dest;
2114 std::deque<std::string> params;
2115 params.push_back(u->nick);
2116 params.push_back(text);
2117 DoOneToMany(user->nick,"MODE",params);
2121 chanrec* c = (chanrec*)dest;
2122 std::deque<std::string> params;
2123 params.push_back(c->name);
2124 params.push_back(text);
2125 DoOneToMany(user->nick,"MODE",params);
2130 virtual void ProtoSendMode(void* opaque, int target_type, void* target, std::string modeline)
2132 TreeSocket* s = (TreeSocket*)opaque;
2135 if (target_type == TYPE_USER)
2137 userrec* u = (userrec*)target;
2138 s->WriteLine(":"+Srv->GetServerName()+" FMODE "+u->nick+" "+modeline);
2142 chanrec* c = (chanrec*)target;
2143 s->WriteLine(":"+Srv->GetServerName()+" FMODE "+c->name+" "+modeline);
2148 virtual ~ModuleSpanningTree()
2153 virtual Version GetVersion()
2155 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
2160 class ModuleSpanningTreeFactory : public ModuleFactory
2163 ModuleSpanningTreeFactory()
2167 ~ModuleSpanningTreeFactory()
2171 virtual Module * CreateModule()
2173 TreeProtocolModule = new ModuleSpanningTree;
2174 return TreeProtocolModule;
2180 extern "C" void * init_module( void )
2182 return new ModuleSpanningTreeFactory;