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;
93 TreeServer(std::string Name, std::string Desc) : ServerName(Name), ServerDesc(Desc)
97 UserCount = OperCount = 0;
100 TreeServer(std::string Name, std::string Desc, TreeServer* Above, TreeSocket* Sock) : Parent(Above), ServerName(Name), ServerDesc(Desc), Socket(Sock)
103 UserCount = OperCount = 0;
104 this->SetNextPingTime(time(NULL) + 60);
108 std::string GetName()
110 return this->ServerName;
113 std::string GetDesc()
115 return this->ServerDesc;
118 std::string GetVersion()
120 return this->VersionString;
123 void SetNextPingTime(time_t t)
126 LastPingWasGood = false;
129 time_t NextPingTime()
131 return this->NextPing;
134 bool AnsweredLastPing()
136 return LastPingWasGood;
141 LastPingWasGood = true;
146 return this->UserCount;
151 return this->OperCount;
154 TreeSocket* GetSocket()
159 TreeServer* GetParent()
164 unsigned int ChildCount()
166 return Children.size();
169 TreeServer* GetChild(unsigned int n)
171 if (n < Children.size())
181 void AddChild(TreeServer* Child)
183 Children.push_back(Child);
186 bool DelChild(TreeServer* Child)
188 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
199 // removes child nodes of this node, and of that node, etc etc
202 bool stillchildren = true;
203 while (stillchildren)
205 stillchildren = false;
206 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
208 TreeServer* s = (TreeServer*)*a;
212 stillchildren = true;
226 std::string SendPass;
227 std::string RecvPass;
228 unsigned long AutoConnect;
229 time_t NextConnectTime;
232 /* $ModDesc: Povides a spanning tree server link protocol */
236 TreeServer *TreeRoot;
237 std::vector<Link> LinkBlocks;
239 TreeServer* RouteEnumerate(TreeServer* Current, std::string ServerName)
241 if (Current->GetName() == ServerName)
243 for (unsigned int q = 0; q < Current->ChildCount(); q++)
245 TreeServer* found = RouteEnumerate(Current->GetChild(q),ServerName);
254 // Returns the locally connected server we must route a
255 // message through to reach server 'ServerName'. This
256 // only applies to one-to-one and not one-to-many routing.
257 TreeServer* BestRouteTo(std::string ServerName)
259 if (ServerName.c_str() == TreeRoot->GetName())
263 // first, find the server by recursively walking the tree
264 TreeServer* Found = RouteEnumerate(TreeRoot,ServerName);
265 // did we find it? If not, they did something wrong, abort.
272 // The server exists, follow its parent nodes until
273 // the parent of the current is 'TreeRoot', we know
274 // then that this is a directly-connected server.
275 while ((Found) && (Found->GetParent() != TreeRoot))
277 Found = Found->GetParent();
283 bool LookForServer(TreeServer* Current, std::string ServerName)
285 if (ServerName == Current->GetName())
287 for (unsigned int q = 0; q < Current->ChildCount(); q++)
289 if (LookForServer(Current->GetChild(q),ServerName))
297 void RFindServer(TreeServer* Current, std::string ServerName)
299 if ((ServerName == Current->GetName()) && (!Found))
306 for (unsigned int q = 0; q < Current->ChildCount(); q++)
309 RFindServer(Current->GetChild(q),ServerName);
315 TreeServer* FindServer(std::string ServerName)
318 RFindServer(TreeRoot,ServerName);
322 bool IsServer(std::string ServerName)
324 return LookForServer(TreeRoot,ServerName);
327 class TreeSocket : public InspSocket
330 std::string in_buffer;
331 ServerState LinkState;
332 std::string InboundServerName;
333 std::string InboundDescription;
335 int num_lost_servers;
337 bool LastPingWasGood;
341 TreeSocket(std::string host, int port, bool listening, unsigned long maxtime)
342 : InspSocket(host, port, listening, maxtime)
345 this->LinkState = LISTENER;
348 TreeSocket(std::string host, int port, bool listening, unsigned long maxtime, std::string ServerName)
349 : InspSocket(host, port, listening, maxtime)
352 this->LinkState = CONNECTING;
355 TreeSocket(int newfd, char* ip)
356 : InspSocket(newfd, ip)
358 this->LinkState = WAIT_AUTH_1;
361 virtual bool OnConnected()
363 if (this->LinkState == CONNECTING)
365 Srv->SendOpers("*** Connection to "+myhost+"["+this->GetIP()+"] established.");
366 // we should send our details here.
367 // if the other side is satisfied, they send theirs.
368 // we do not need to change state here.
369 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
371 if (x->Name == this->myhost)
373 // found who we're supposed to be connecting to, send the neccessary gubbins.
374 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
382 virtual void OnError(InspSocketError e)
386 virtual int OnDisconnect()
391 // recursively send the server tree with distances as hops
392 void SendServers(TreeServer* Current, TreeServer* s, int hops)
395 for (unsigned int q = 0; q < Current->ChildCount(); q++)
397 TreeServer* recursive_server = Current->GetChild(q);
398 if (recursive_server != s)
400 // :source.server SERVER server.name hops :Description
401 snprintf(command,1024,":%s SERVER %s * %d :%s",Current->GetName().c_str(),recursive_server->GetName().c_str(),hops,recursive_server->GetDesc().c_str());
402 this->WriteLine(command);
403 // down to next level
404 this->SendServers(recursive_server, s, hops+1);
409 void SquitServer(TreeServer* Current)
411 // recursively squit the servers attached to 'Current'
412 for (unsigned int q = 0; q < Current->ChildCount(); q++)
414 TreeServer* recursive_server = Current->GetChild(q);
415 this->SquitServer(recursive_server);
417 // Now we've whacked the kids, whack self
419 bool quittingpeople = true;
420 while (quittingpeople)
422 quittingpeople = false;
423 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
425 if (!strcasecmp(u->second->server,Current->GetName().c_str()))
427 Srv->QuitUser(u->second,Current->GetName()+" "+std::string(Srv->GetServerName()));
429 quittingpeople = true;
436 void Squit(TreeServer* Current,std::string reason)
440 std::deque<std::string> params;
441 params.push_back(Current->GetName());
442 params.push_back(":"+reason);
443 DoOneToAllButSender(Current->GetParent()->GetName(),"SQUIT",params,Current->GetName());
444 if (Current->GetParent() == TreeRoot)
446 Srv->SendOpers("Server \002"+Current->GetName()+"\002 split: "+reason);
450 Srv->SendOpers("Server \002"+Current->GetName()+"\002 split from server \002"+Current->GetParent()->GetName()+"\002 with reason: "+reason);
452 num_lost_servers = 0;
454 SquitServer(Current);
456 Current->GetParent()->DelChild(Current);
458 WriteOpers("Netsplit complete, lost \002%d\002 users on \002%d\002 servers.", num_lost_users, num_lost_servers);
462 log(DEFAULT,"Squit from unknown server");
466 bool ForceMode(std::string source, std::deque<std::string> params)
468 userrec* who = new userrec;
469 who->fd = FD_MAGIC_NUMBER;
470 if (params.size() < 2)
473 for (unsigned int q = 0; q < params.size(); q++)
475 modelist[q] = (char*)params[q].c_str();
477 Srv->SendMode(modelist,params.size(),who);
478 DoOneToAllButSender(source,"FMODE",params,source);
483 bool ForceTopic(std::string source, std::deque<std::string> params)
485 // FTOPIC %s %lu %s :%s
486 if (params.size() != 4)
488 std::string channel = params[0];
489 time_t ts = atoi(params[1].c_str());
490 std::string setby = params[2];
491 std::string topic = params[3];
493 chanrec* c = Srv->FindChannel(channel);
496 if ((ts >= c->topicset) || (!*c->topic))
498 strlcpy(c->topic,topic.c_str(),MAXTOPIC);
499 strlcpy(c->setby,setby.c_str(),NICKMAX);
501 WriteChannelWithServ((char*)source.c_str(), c, "TOPIC %s :%s", c->name, c->topic);
506 // all done, send it on its way
507 params[3] = ":" + params[3];
508 DoOneToAllButSender(source,"FTOPIC",params,source);
513 bool ForceJoin(std::string source, std::deque<std::string> params)
515 if (params.size() < 3)
519 char modestring[MAXBUF];
520 char* mode_users[127];
521 mode_users[0] = first;
522 mode_users[1] = modestring;
523 strcpy(mode_users[1],"+");
524 unsigned int modectr = 2;
527 std::string channel = params[0];
528 time_t TS = atoi(params[1].c_str());
531 chanrec* chan = Srv->FindChannel(channel);
536 strlcpy(mode_users[0],channel.c_str(),MAXBUF);
538 // default is a high value, which if we dont have this
539 // channel will let the other side apply their modes.
540 time_t ourTS = time(NULL)+20;
541 chanrec* us = Srv->FindChannel(channel);
547 log(DEBUG,"FJOIN detected, our TS=%lu, their TS=%lu",ourTS,TS);
549 // do this first, so our mode reversals are correctly received by other servers
550 // if there is a TS collision.
551 DoOneToAllButSender(source,"FJOIN",params,source);
553 for (unsigned int usernum = 2; usernum < params.size(); usernum++)
555 // process one channel at a time, applying modes.
556 char* usr = (char*)params[usernum].c_str();
557 char permissions = *usr;
562 mode_users[modectr++] = usr;
563 strlcat(modestring,"o",MAXBUF);
567 mode_users[modectr++] = usr;
568 strlcat(modestring,"h",MAXBUF);
572 mode_users[modectr++] = usr;
573 strlcat(modestring,"v",MAXBUF);
576 who = Srv->FindNick(usr);
579 Srv->JoinUserToChannel(who,channel,key);
580 if (modectr >= (MAXMODES-1))
582 // theres a mode for this user. push them onto the mode queue, and flush it
583 // if there are more than MAXMODES to go.
586 log(DEBUG,"Our our channel newer than theirs, accepting their modes");
587 Srv->SendMode(mode_users,modectr,who);
591 log(DEBUG,"Their channel newer than ours, bouncing their modes");
593 std::deque<std::string> params;
594 // modes are now being UNSET...
595 *mode_users[1] = '-';
596 for (unsigned int x = 0; x < modectr; x++)
598 params.push_back(mode_users[x]);
600 // tell everyone to bounce the modes. bad modes, bad!
601 DoOneToMany(Srv->GetServerName(),"FMODE",params);
603 strcpy(mode_users[1],"+");
608 // there werent enough modes built up to flush it during FJOIN,
609 // or, there are a number left over. flush them out.
610 if ((modectr > 2) && (who))
614 log(DEBUG,"Our our channel newer than theirs, accepting their modes");
615 Srv->SendMode(mode_users,modectr,who);
619 log(DEBUG,"Their channel newer than ours, bouncing their modes");
620 std::deque<std::string> params;
621 *mode_users[1] = '-';
622 for (unsigned int x = 0; x < modectr; x++)
624 params.push_back(mode_users[x]);
626 DoOneToMany(Srv->GetServerName(),"FMODE",params);
632 bool IntroduceClient(std::string source, std::deque<std::string> params)
634 if (params.size() < 8)
636 // NICK age nick host dhost ident +modes ip :gecos
638 std::string nick = params[1];
639 std::string host = params[2];
640 std::string dhost = params[3];
641 std::string ident = params[4];
642 time_t age = atoi(params[0].c_str());
643 std::string modes = params[5];
644 while (*(modes.c_str()) == '+')
646 char* m = (char*)modes.c_str();
650 std::string ip = params[6];
651 std::string gecos = params[7];
652 char* tempnick = (char*)nick.c_str();
653 log(DEBUG,"Introduce client %s!%s@%s",tempnick,ident.c_str(),host.c_str());
655 user_hash::iterator iter;
656 iter = clientlist.find(tempnick);
657 if (iter != clientlist.end())
660 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);
661 this->WriteLine(":"+Srv->GetServerName()+" KILL "+tempnick+" :Nickname collision");
665 clientlist[tempnick] = new userrec();
666 clientlist[tempnick]->fd = FD_MAGIC_NUMBER;
667 strlcpy(clientlist[tempnick]->nick, tempnick,NICKMAX);
668 strlcpy(clientlist[tempnick]->host, host.c_str(),160);
669 strlcpy(clientlist[tempnick]->dhost, dhost.c_str(),160);
670 clientlist[tempnick]->server = (char*)FindServerNamePtr(source.c_str());
671 strlcpy(clientlist[tempnick]->ident, ident.c_str(),IDENTMAX);
672 strlcpy(clientlist[tempnick]->fullname, gecos.c_str(),MAXGECOS);
673 clientlist[tempnick]->registered = 7;
674 clientlist[tempnick]->signon = age;
675 strlcpy(clientlist[tempnick]->modes, modes.c_str(),53);
676 strlcpy(clientlist[tempnick]->ip,ip.c_str(),16);
677 for (int i = 0; i < MAXCHANS; i++)
679 clientlist[tempnick]->chans[i].channel = NULL;
680 clientlist[tempnick]->chans[i].uc_modes = 0;
682 params[7] = ":" + params[7];
683 DoOneToAllButSender(source,"NICK",params,source);
687 void SendFJoins(TreeServer* Current, chanrec* c)
690 snprintf(list,MAXBUF,":%s FJOIN %s %lu",Srv->GetServerName().c_str(),c->name,(unsigned long)c->age);
691 std::vector<char*> *ulist = c->GetUsers();
692 for (unsigned int i = 0; i < ulist->size(); i++)
694 char* o = (*ulist)[i];
695 userrec* otheruser = (userrec*)o;
696 strlcat(list," ",MAXBUF);
697 strlcat(list,cmode(otheruser,c),MAXBUF);
698 strlcat(list,otheruser->nick,MAXBUF);
699 if (strlen(list)>(480-NICKMAX))
701 this->WriteLine(list);
702 snprintf(list,MAXBUF,":%s FJOIN %s %lu",Srv->GetServerName().c_str(),c->name,(unsigned long)c->age);
705 if (list[strlen(list)-1] != ':')
707 this->WriteLine(list);
711 void SendChannelModes(TreeServer* Current)
714 for (chan_hash::iterator c = chanlist.begin(); c != chanlist.end(); c++)
716 SendFJoins(Current, c->second);
717 snprintf(data,MAXBUF,":%s FMODE %s +%s",Srv->GetServerName().c_str(),c->second->name,chanmodes(c->second));
718 this->WriteLine(data);
719 if (*c->second->topic)
721 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);
722 this->WriteLine(data);
724 for (BanList::iterator b = c->second->bans.begin(); b != c->second->bans.end(); b++)
726 snprintf(data,MAXBUF,":%s FMODE %s +b %s",Srv->GetServerName().c_str(),c->second->name,b->data);
727 this->WriteLine(data);
729 FOREACH_MOD OnSyncChannel(c->second,(Module*)TreeProtocolModule,(void*)this);
733 // send all users and their channels
734 void SendUsers(TreeServer* Current)
737 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
739 if (u->second->registered == 7)
741 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);
742 this->WriteLine(data);
743 if (strchr(u->second->modes,'o'))
745 this->WriteLine(":"+std::string(u->second->nick)+" OPERTYPE "+std::string(u->second->oper));
747 //char* chl = chlist(u->second,u->second);
750 // this->WriteLine(":"+std::string(u->second->nick)+" FJOIN "+std::string(chl));
752 FOREACH_MOD OnSyncUser(u->second,(Module*)TreeProtocolModule,(void*)this);
757 void DoBurst(TreeServer* s)
759 Srv->SendOpers("*** Bursting to "+s->GetName()+".");
760 this->WriteLine("BURST");
762 this->SendServers(TreeRoot,s,1);
763 // Send users and their channels
765 // Send everything else (channel modes etc)
766 this->SendChannelModes(s);
767 this->WriteLine("ENDBURST");
770 virtual bool OnDataReady()
772 char* data = this->Read();
775 this->in_buffer += data;
776 while (in_buffer.find("\n") != std::string::npos)
778 char* line = (char*)in_buffer.c_str();
779 std::string ret = "";
780 while ((*line != '\n') && (strlen(line)))
785 if ((*line == '\n') || (*line == '\r'))
788 if (!this->ProcessLine(ret))
794 return (data != NULL);
797 int WriteLine(std::string line)
799 return this->Write(line + "\r\n");
802 bool Error(std::deque<std::string> params)
804 if (params.size() < 1)
806 std::string Errmsg = params[0];
807 std::string SName = myhost;
808 if (InboundServerName != "")
810 SName = InboundServerName;
812 Srv->SendOpers("*** ERROR from "+SName+": "+Errmsg);
813 // we will return false to cause the socket to close.
817 bool OperType(std::string prefix, std::deque<std::string> params)
819 if (params.size() != 1)
821 std::string opertype = params[0];
822 userrec* u = Srv->FindNick(prefix);
825 strlcpy(u->oper,opertype.c_str(),NICKMAX);
826 if (!strchr(u->modes,'o'))
828 strcat(u->modes,"o");
830 DoOneToAllButSender(u->server,"OPERTYPE",params,u->server);
835 bool RemoteRehash(std::string prefix, std::deque<std::string> params)
837 if (params.size() < 1)
839 std::string servermask = params[0];
840 if (Srv->MatchText(Srv->GetServerName(),servermask))
842 Srv->SendOpers("*** Remote rehash initiated from server \002"+prefix+"\002.");
844 ReadConfiguration(false);
846 DoOneToAllButSender(prefix,"REHASH",params,prefix);
850 bool RemoteKill(std::string prefix, std::deque<std::string> params)
852 if (params.size() != 2)
854 std::string nick = params[0];
855 std::string reason = params[1];
856 userrec* u = Srv->FindNick(prefix);
857 userrec* who = Srv->FindNick(nick);
860 std::string sourceserv = prefix;
863 sourceserv = u->server;
865 params[1] = ":" + params[1];
866 DoOneToAllButSender(prefix,"KILL",params,sourceserv);
867 Srv->QuitUser(who,reason);
872 bool LocalPong(std::string prefix, std::deque<std::string> params)
874 if (params.size() < 1)
876 TreeServer* ServerSource = FindServer(prefix);
879 ServerSource->SetPingFlag();
884 bool LocalPing(std::string prefix, std::deque<std::string> params)
886 if (params.size() < 1)
888 std::string stufftobounce = params[0];
889 this->WriteLine(":"+Srv->GetServerName()+" PONG "+stufftobounce);
893 bool RemoteServer(std::string prefix, std::deque<std::string> params)
895 if (params.size() < 4)
897 std::string servername = params[0];
898 std::string password = params[1];
899 // hopcount is not used for a remote server, we calculate this ourselves
900 std::string description = params[3];
901 TreeServer* ParentOfThis = FindServer(prefix);
904 this->WriteLine("ERROR :Protocol error - Introduced remote server from unknown server "+prefix);
907 TreeServer* CheckDupe = FindServer(servername);
910 this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
913 TreeServer* Node = new TreeServer(servername,description,ParentOfThis,NULL);
914 ParentOfThis->AddChild(Node);
915 params[3] = ":" + params[3];
916 DoOneToAllButSender(prefix,"SERVER",params,prefix);
917 Srv->SendOpers("*** Server \002"+prefix+"\002 introduced server \002"+servername+"\002 ("+description+")");
921 bool Outbound_Reply_Server(std::deque<std::string> params)
923 if (params.size() < 4)
925 std::string servername = params[0];
926 std::string password = params[1];
927 int hops = atoi(params[2].c_str());
930 this->WriteLine("ERROR :Server too far away for authentication");
933 std::string description = params[3];
934 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
936 if ((x->Name == servername) && (x->RecvPass == password))
938 TreeServer* CheckDupe = FindServer(servername);
941 this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
944 // Begin the sync here. this kickstarts the
945 // other side, waiting in WAIT_AUTH_2 state,
946 // into starting their burst, as it shows
948 this->LinkState = CONNECTED;
949 // we should add the details of this server now
950 // to the servers tree, as a child of the root
952 TreeServer* Node = new TreeServer(servername,description,TreeRoot,this);
953 TreeRoot->AddChild(Node);
954 params[3] = ":" + params[3];
955 DoOneToAllButSender(TreeRoot->GetName(),"SERVER",params,servername);
960 this->WriteLine("ERROR :Invalid credentials");
964 bool Inbound_Server(std::deque<std::string> params)
966 if (params.size() < 4)
968 std::string servername = params[0];
969 std::string password = params[1];
970 int hops = atoi(params[2].c_str());
973 this->WriteLine("ERROR :Server too far away for authentication");
976 std::string description = params[3];
977 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
979 if ((x->Name == servername) && (x->RecvPass == password))
981 TreeServer* CheckDupe = FindServer(servername);
984 this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
987 Srv->SendOpers("*** Verified incoming server connection from \002"+servername+"\002["+this->GetIP()+"] ("+description+")");
988 this->InboundServerName = servername;
989 this->InboundDescription = description;
990 // this is good. Send our details: Our server name and description and hopcount of 0,
991 // along with the sendpass from this block.
992 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
993 // move to the next state, we are now waiting for THEM.
994 this->LinkState = WAIT_AUTH_2;
998 this->WriteLine("ERROR :Invalid credentials");
1002 std::deque<std::string> Split(std::string line, bool stripcolon)
1004 std::deque<std::string> n;
1005 if (!strchr(line.c_str(),' '))
1010 std::stringstream s(line);
1011 std::string param = "";
1030 if ((param == ":") && (item > 0))
1053 bool ProcessLine(std::string line)
1055 char* l = (char*)line.c_str();
1056 while ((strlen(l)) && (l[strlen(l)-1] == '\r') || (l[strlen(l)-1] == '\n'))
1057 l[strlen(l)-1] = '\0';
1061 Srv->Log(DEBUG,"IN: '"+line+"'");
1062 std::deque<std::string> params = this->Split(line,true);
1063 std::string command = "";
1064 std::string prefix = "";
1065 if (((params[0].c_str())[0] == ':') && (params.size() > 1))
1068 command = params[1];
1069 char* pref = (char*)prefix.c_str();
1077 command = params[0];
1081 switch (this->LinkState)
1086 // Waiting for SERVER command from remote server. Server initiating
1087 // the connection sends the first SERVER command, listening server
1088 // replies with theirs if its happy, then if the initiator is happy,
1089 // it starts to send its net sync, which starts the merge, otherwise
1090 // it sends an ERROR.
1091 if (command == "SERVER")
1093 return this->Inbound_Server(params);
1095 else if (command == "ERROR")
1097 return this->Error(params);
1101 // Waiting for start of other side's netmerge to say they liked our
1103 if (command == "SERVER")
1105 // cant do this, they sent it to us in the WAIT_AUTH_1 state!
1109 else if (command == "BURST")
1111 this->LinkState = CONNECTED;
1112 Node = new TreeServer(InboundServerName,InboundDescription,TreeRoot,this);
1113 TreeRoot->AddChild(Node);
1115 params.push_back(InboundServerName);
1116 params.push_back("*");
1117 params.push_back("1");
1118 params.push_back(":"+InboundDescription);
1119 DoOneToAllButSender(TreeRoot->GetName(),"SERVER",params,InboundServerName);
1120 this->DoBurst(Node);
1122 else if (command == "ERROR")
1124 return this->Error(params);
1129 this->WriteLine("ERROR :Internal error -- listening socket accepted its own descriptor!!!");
1133 if (command == "SERVER")
1135 // another server we connected to, which was in WAIT_AUTH_1 state,
1136 // has just sent us their credentials. If we get this far, theyre
1137 // happy with OUR credentials, and they are now in WAIT_AUTH_2 state.
1138 // if we're happy with this, we should send our netburst which
1139 // kickstarts the merge.
1140 return this->Outbound_Reply_Server(params);
1142 else if (command == "ERROR")
1144 return this->Error(params);
1148 // This is the 'authenticated' state, when all passwords
1149 // have been exchanged and anything past this point is taken
1151 std::string target = "";
1152 if ((command == "NICK") && (params.size() > 1))
1154 return this->IntroduceClient(prefix,params);
1156 else if (command == "FJOIN")
1158 return this->ForceJoin(prefix,params);
1160 else if (command == "SERVER")
1162 return this->RemoteServer(prefix,params);
1164 else if (command == "ERROR")
1166 return this->Error(params);
1168 else if (command == "OPERTYPE")
1170 return this->OperType(prefix,params);
1172 else if (command == "FMODE")
1174 return this->ForceMode(prefix,params);
1176 else if (command == "KILL")
1178 return this->RemoteKill(prefix,params);
1180 else if (command == "FTOPIC")
1182 return this->ForceTopic(prefix,params);
1184 else if (command == "REHASH")
1186 return this->RemoteRehash(prefix,params);
1188 else if (command == "PING")
1190 return this->LocalPing(prefix,params);
1192 else if (command == "PONG")
1194 return this->LocalPong(prefix,params);
1196 else if (command == "SQUIT")
1198 if (params.size() == 2)
1200 this->Squit(FindServer(params[0]),params[1]);
1206 // not a special inter-server command.
1207 // Emulate the actual user doing the command,
1208 // this saves us having a huge ugly parser.
1209 userrec* who = Srv->FindNick(prefix);
1210 std::string sourceserv = this->myhost;
1211 if (this->InboundServerName != "")
1213 sourceserv = this->InboundServerName;
1218 target = who->server;
1219 char* strparams[127];
1220 for (unsigned int q = 0; q < params.size(); q++)
1222 strparams[q] = (char*)params[q].c_str();
1224 Srv->CallCommandHandler(command, strparams, params.size(), who);
1228 // its not a user. Its either a server, or somethings screwed up.
1229 if (IsServer(prefix))
1231 target = Srv->GetServerName();
1235 log(DEBUG,"Command with unknown origin '%s'",prefix.c_str());
1239 return DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1248 virtual std::string GetName()
1250 std::string sourceserv = this->myhost;
1251 if (this->InboundServerName != "")
1253 sourceserv = this->InboundServerName;
1258 virtual void OnTimeout()
1260 if (this->LinkState == CONNECTING)
1262 Srv->SendOpers("*** CONNECT: Connection to \002"+myhost+"\002 timed out.");
1266 virtual void OnClose()
1268 // Connection closed.
1269 // If the connection is fully up (state CONNECTED)
1270 // then propogate a netsplit to all peers.
1271 std::string quitserver = this->myhost;
1272 if (this->InboundServerName != "")
1274 quitserver = this->InboundServerName;
1276 TreeServer* s = FindServer(quitserver);
1279 Squit(s,"Remote host closed the connection");
1283 virtual int OnIncomingConnection(int newsock, char* ip)
1285 TreeSocket* s = new TreeSocket(newsock, ip);
1291 void AddThisServer(TreeServer* server, std::deque<TreeServer*> &list)
1293 for (unsigned int c = 0; c < list.size(); c++)
1295 if (list[c] == server)
1300 list.push_back(server);
1303 // returns a list of DIRECT servernames for a specific channel
1304 std::deque<TreeServer*> GetListOfServersForChannel(chanrec* c)
1306 std::deque<TreeServer*> list;
1307 std::vector<char*> *ulist = c->GetUsers();
1308 for (unsigned int i = 0; i < ulist->size(); i++)
1310 char* o = (*ulist)[i];
1311 userrec* otheruser = (userrec*)o;
1312 if (std::string(otheruser->server) != Srv->GetServerName())
1314 TreeServer* best = BestRouteTo(otheruser->server);
1316 AddThisServer(best,list);
1322 bool DoOneToAllButSenderRaw(std::string data,std::string omit,std::string prefix,std::string command,std::deque<std::string> params)
1324 TreeServer* omitroute = BestRouteTo(omit);
1325 if ((command == "NOTICE") || (command == "PRIVMSG"))
1327 if ((params.size() >= 2) && (*(params[0].c_str()) != '$'))
1329 if (*(params[0].c_str()) != '#')
1331 // special routing for private messages/notices
1332 userrec* d = Srv->FindNick(params[0]);
1335 std::deque<std::string> par;
1337 par.push_back(params[0]);
1338 par.push_back(":"+params[1]);
1339 DoOneToOne(prefix,command,par,d->server);
1345 log(DEBUG,"Channel privmsg going to chan %s",params[0].c_str());
1346 chanrec* c = Srv->FindChannel(params[0]);
1349 std::deque<TreeServer*> list = GetListOfServersForChannel(c);
1350 log(DEBUG,"Got a list of %d servers",list.size());
1351 for (unsigned int i = 0; i < list.size(); i++)
1353 TreeSocket* Sock = list[i]->GetSocket();
1354 if ((Sock) && (list[i]->GetName() != omit) && (omitroute != list[i]))
1356 log(DEBUG,"Writing privmsg to server %s",list[i]->GetName().c_str());
1357 Sock->WriteLine(data);
1365 for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1367 TreeServer* Route = TreeRoot->GetChild(x);
1368 if ((Route->GetSocket()) && (Route->GetName() != omit) && (omitroute != Route))
1370 TreeSocket* Sock = Route->GetSocket();
1371 Sock->WriteLine(data);
1377 bool DoOneToAllButSender(std::string prefix, std::string command, std::deque<std::string> params, std::string omit)
1379 TreeServer* omitroute = BestRouteTo(omit);
1380 std::string FullLine = ":" + prefix + " " + command;
1381 for (unsigned int x = 0; x < params.size(); x++)
1383 FullLine = FullLine + " " + params[x];
1385 for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1387 TreeServer* Route = TreeRoot->GetChild(x);
1388 // Send the line IF:
1389 // The route has a socket (its a direct connection)
1390 // The route isnt the one to be omitted
1391 // The route isnt the path to the one to be omitted
1392 if ((Route->GetSocket()) && (Route->GetName() != omit) && (omitroute != Route))
1394 TreeSocket* Sock = Route->GetSocket();
1395 Sock->WriteLine(FullLine);
1401 bool DoOneToMany(std::string prefix, std::string command, std::deque<std::string> params)
1403 std::string FullLine = ":" + prefix + " " + command;
1404 for (unsigned int x = 0; x < params.size(); x++)
1406 FullLine = FullLine + " " + params[x];
1408 for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1410 TreeServer* Route = TreeRoot->GetChild(x);
1411 if (Route->GetSocket())
1413 TreeSocket* Sock = Route->GetSocket();
1414 Sock->WriteLine(FullLine);
1420 bool DoOneToOne(std::string prefix, std::string command, std::deque<std::string> params, std::string target)
1422 TreeServer* Route = BestRouteTo(target);
1425 std::string FullLine = ":" + prefix + " " + command;
1426 for (unsigned int x = 0; x < params.size(); x++)
1428 FullLine = FullLine + " " + params[x];
1430 if (Route->GetSocket())
1432 TreeSocket* Sock = Route->GetSocket();
1433 Sock->WriteLine(FullLine);
1443 std::vector<TreeSocket*> Bindings;
1445 void ReadConfiguration(bool rebind)
1449 for (int j =0; j < Conf->Enumerate("bind"); j++)
1451 std::string Type = Conf->ReadValue("bind","type",j);
1452 std::string IP = Conf->ReadValue("bind","address",j);
1453 long Port = Conf->ReadInteger("bind","port",j,true);
1454 if (Type == "servers")
1460 TreeSocket* listener = new TreeSocket(IP.c_str(),Port,true,10);
1461 if (listener->GetState() == I_LISTENING)
1463 Srv->AddSocket(listener);
1464 Bindings.push_back(listener);
1468 log(DEFAULT,"m_spanningtree: Warning: Failed to bind server port %d",Port);
1476 for (int j =0; j < Conf->Enumerate("link"); j++)
1479 L.Name = Conf->ReadValue("link","name",j);
1480 L.IPAddr = Conf->ReadValue("link","ipaddr",j);
1481 L.Port = Conf->ReadInteger("link","port",j,true);
1482 L.SendPass = Conf->ReadValue("link","sendpass",j);
1483 L.RecvPass = Conf->ReadValue("link","recvpass",j);
1484 L.AutoConnect = Conf->ReadInteger("link","autoconnect",j,true);
1485 L.NextConnectTime = time(NULL) + L.AutoConnect;
1486 LinkBlocks.push_back(L);
1487 log(DEBUG,"m_spanningtree: Read server %s with host %s:%d",L.Name.c_str(),L.IPAddr.c_str(),L.Port);
1492 class ModuleSpanningTree : public Module
1494 std::vector<TreeSocket*> Bindings;
1500 ModuleSpanningTree()
1503 Conf = new ConfigReader;
1506 // Create the root of the tree
1507 TreeRoot = new TreeServer(Srv->GetServerName(),Srv->GetServerDescription());
1509 ReadConfiguration(true);
1512 void ShowLinks(TreeServer* Current, userrec* user, int hops)
1514 std::string Parent = TreeRoot->GetName();
1515 if (Current->GetParent())
1517 Parent = Current->GetParent()->GetName();
1519 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1521 ShowLinks(Current->GetChild(q),user,hops+1);
1523 WriteServ(user->fd,"364 %s %s %s :%d %s",user->nick,Current->GetName().c_str(),Parent.c_str(),hops,Current->GetDesc().c_str());
1526 int CountLocalServs()
1528 return TreeRoot->ChildCount();
1531 void CountServsRecursive(TreeServer* Current)
1534 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1536 CountServsRecursive(Current->GetChild(q));
1543 CountServsRecursive(TreeRoot);
1547 void HandleLinks(char** parameters, int pcnt, userrec* user)
1549 ShowLinks(TreeRoot,user,0);
1550 WriteServ(user->fd,"365 %s * :End of /LINKS list.",user->nick);
1554 void HandleLusers(char** parameters, int pcnt, userrec* user)
1556 WriteServ(user->fd,"251 %s :There are %d users and %d invisible on %d servers",user->nick,usercnt()-usercount_invisible(),usercount_invisible(),this->CountServs());
1557 WriteServ(user->fd,"252 %s %d :operator(s) online",user->nick,usercount_opers());
1558 WriteServ(user->fd,"253 %s %d :unknown connections",user->nick,usercount_unknown());
1559 WriteServ(user->fd,"254 %s %d :channels formed",user->nick,chancount());
1560 WriteServ(user->fd,"254 %s :I have %d clients and %d servers",user->nick,local_count(),this->CountLocalServs());
1564 // WARNING: NOT THREAD SAFE - DONT GET ANY SMART IDEAS.
1566 void ShowMap(TreeServer* Current, userrec* user, int depth, char matrix[128][80])
1570 for (int t = 0; t < depth; t++)
1572 matrix[line][t] = ' ';
1574 strlcpy(&matrix[line][depth],Current->GetName().c_str(),80);
1576 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1578 ShowMap(Current->GetChild(q),user,depth+2,matrix);
1583 // Ok, prepare to be confused.
1584 // After much mulling over how to approach this, it struck me that
1585 // the 'usual' way of doing a /MAP isnt the best way. Instead of
1586 // keeping track of a ton of ascii characters, and line by line
1587 // under recursion working out where to place them using multiplications
1588 // and divisons, we instead render the map onto a backplane of characters
1589 // (a character matrix), then draw the branches as a series of "L" shapes
1590 // from the nodes. This is not only friendlier on CPU it uses less stack.
1592 void HandleMap(char** parameters, int pcnt, userrec* user)
1594 // This array represents a virtual screen which we will
1595 // "scratch" draw to, as the console device of an irc
1596 // client does not provide for a proper terminal.
1597 char matrix[128][80];
1598 for (unsigned int t = 0; t < 128; t++)
1600 matrix[t][0] = '\0';
1603 // The only recursive bit is called here.
1604 ShowMap(TreeRoot,user,0,matrix);
1605 // Process each line one by one. The algorithm has a limit of
1606 // 128 servers (which is far more than a spanning tree should have
1607 // anyway, so we're ok). This limit can be raised simply by making
1608 // the character matrix deeper, 128 rows taking 10k of memory.
1609 for (int l = 1; l < line; l++)
1611 // scan across the line looking for the start of the
1612 // servername (the recursive part of the algorithm has placed
1613 // the servers at indented positions depending on what they
1615 int first_nonspace = 0;
1616 while (matrix[l][first_nonspace] == ' ')
1621 // Draw the `- (corner) section: this may be overwritten by
1622 // another L shape passing along the same vertical pane, becoming
1623 // a |- (branch) section instead.
1624 matrix[l][first_nonspace] = '-';
1625 matrix[l][first_nonspace-1] = '`';
1627 // Draw upwards until we hit the parent server, causing possibly
1628 // other corners (`-) to become branches (|-)
1629 while ((matrix[l2][first_nonspace-1] == ' ') || (matrix[l2][first_nonspace-1] == '`'))
1631 matrix[l2][first_nonspace-1] = '|';
1635 // dump the whole lot to the user. This is the easy bit, honest.
1636 for (int t = 0; t < line; t++)
1638 WriteServ(user->fd,"006 %s :%s",user->nick,&matrix[t][0]);
1640 WriteServ(user->fd,"007 %s :End of /MAP",user->nick);
1644 int HandleSquit(char** parameters, int pcnt, userrec* user)
1646 TreeServer* s = FindServer(parameters[0]);
1649 TreeSocket* sock = s->GetSocket();
1652 WriteOpers("*** SQUIT: Server \002%s\002 removed from network by %s",parameters[0],user->nick);
1653 sock->Squit(s,"Server quit by "+std::string(user->nick)+"!"+std::string(user->ident)+"@"+std::string(user->host));
1658 WriteServ(user->fd,"NOTICE %s :*** SQUIT: The server \002%s\002 is not directly connected.",user->nick,parameters[0]);
1663 WriteServ(user->fd,"NOTICE %s :*** SQUIT: The server \002%s\002 does not exist on the network.",user->nick,parameters[0]);
1668 void DoPingChecks(time_t curtime)
1670 for (unsigned int j = 0; j < TreeRoot->ChildCount(); j++)
1672 TreeServer* serv = TreeRoot->GetChild(j);
1673 TreeSocket* sock = serv->GetSocket();
1676 if (curtime >= serv->NextPingTime())
1678 if (serv->AnsweredLastPing())
1680 sock->WriteLine(":"+Srv->GetServerName()+" PING "+serv->GetName());
1681 serv->SetNextPingTime(curtime + 60);
1685 // they didnt answer, boot them
1686 WriteOpers("*** Server \002%s\002 pinged out",serv->GetName().c_str());
1687 sock->Squit(serv,"Ping timeout");
1696 void AutoConnectServers(time_t curtime)
1698 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
1700 if ((x->AutoConnect) && (curtime >= x->NextConnectTime))
1702 log(DEBUG,"Auto-Connecting %s",x->Name.c_str());
1703 x->NextConnectTime = curtime + x->AutoConnect;
1704 TreeServer* CheckDupe = FindServer(x->Name);
1707 // an autoconnected server is not connected. Check if its time to connect it
1708 WriteOpers("*** AUTOCONNECT: Auto-connecting server \002%s\002 (%lu seconds until next attempt)",x->Name.c_str(),x->AutoConnect);
1709 TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name);
1710 Srv->AddSocket(newsocket);
1716 int HandleConnect(char** parameters, int pcnt, userrec* user)
1718 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
1720 if (Srv->MatchText(x->Name.c_str(),parameters[0]))
1722 TreeServer* CheckDupe = FindServer(x->Name);
1725 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);
1726 TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name);
1727 Srv->AddSocket(newsocket);
1732 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());
1737 WriteServ(user->fd,"NOTICE %s :*** CONNECT: No server matching \002%s\002 could be found in the config file.",user->nick,parameters[0]);
1741 virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
1743 if (command == "CONNECT")
1745 return this->HandleConnect(parameters,pcnt,user);
1747 else if (command == "SQUIT")
1749 return this->HandleSquit(parameters,pcnt,user);
1751 else if (command == "MAP")
1753 this->HandleMap(parameters,pcnt,user);
1756 else if (command == "LUSERS")
1758 this->HandleLusers(parameters,pcnt,user);
1761 else if (command == "LINKS")
1763 this->HandleLinks(parameters,pcnt,user);
1766 else if (Srv->IsValidModuleCommand(command, pcnt, user))
1768 // this bit of code cleverly routes all module commands
1769 // to all remote severs *automatically* so that modules
1770 // can just handle commands locally, without having
1771 // to have any special provision in place for remote
1772 // commands and linking protocols.
1773 std::deque<std::string> params;
1775 for (int j = 0; j < pcnt; j++)
1777 if (strchr(parameters[j],' '))
1779 params.push_back(":" + std::string(parameters[j]));
1783 params.push_back(std::string(parameters[j]));
1786 DoOneToMany(user->nick,command,params);
1791 virtual void OnGetServerDescription(std::string servername,std::string &description)
1793 TreeServer* s = FindServer(servername);
1796 description = s->GetDesc();
1800 virtual void OnUserInvite(userrec* source,userrec* dest,chanrec* channel)
1802 if (std::string(source->server) == Srv->GetServerName())
1804 std::deque<std::string> params;
1805 params.push_back(dest->nick);
1806 params.push_back(channel->name);
1807 DoOneToMany(source->nick,"INVITE",params);
1811 virtual void OnPostLocalTopicChange(userrec* user, chanrec* chan, std::string topic)
1813 std::deque<std::string> params;
1814 params.push_back(chan->name);
1815 params.push_back(":"+topic);
1816 DoOneToMany(user->nick,"TOPIC",params);
1819 virtual void OnWallops(userrec* user, std::string text)
1821 if (std::string(user->server) == Srv->GetServerName())
1823 std::deque<std::string> params;
1824 params.push_back(":"+text);
1825 DoOneToMany(user->nick,"WALLOPS",params);
1829 virtual void OnUserNotice(userrec* user, void* dest, int target_type, std::string text)
1831 if (target_type == TYPE_USER)
1833 userrec* d = (userrec*)dest;
1834 if ((std::string(d->server) != Srv->GetServerName()) && (std::string(user->server) == Srv->GetServerName()))
1836 std::deque<std::string> params;
1838 params.push_back(d->nick);
1839 params.push_back(":"+text);
1840 DoOneToOne(user->nick,"NOTICE",params,d->server);
1845 if (std::string(user->server) == Srv->GetServerName())
1847 chanrec *c = (chanrec*)dest;
1848 std::deque<TreeServer*> list = GetListOfServersForChannel(c);
1849 for (unsigned int i = 0; i < list.size(); i++)
1851 TreeSocket* Sock = list[i]->GetSocket();
1853 Sock->WriteLine(":"+std::string(user->nick)+" NOTICE "+std::string(c->name)+" :"+text);
1859 virtual void OnUserMessage(userrec* user, void* dest, int target_type, std::string text)
1861 if (target_type == TYPE_USER)
1863 // route private messages which are targetted at clients only to the server
1864 // which needs to receive them
1865 userrec* d = (userrec*)dest;
1866 if ((std::string(d->server) != Srv->GetServerName()) && (std::string(user->server) == Srv->GetServerName()))
1868 std::deque<std::string> params;
1870 params.push_back(d->nick);
1871 params.push_back(":"+text);
1872 DoOneToOne(user->nick,"PRIVMSG",params,d->server);
1877 if (std::string(user->server) == Srv->GetServerName())
1879 chanrec *c = (chanrec*)dest;
1880 std::deque<TreeServer*> list = GetListOfServersForChannel(c);
1881 for (unsigned int i = 0; i < list.size(); i++)
1883 TreeSocket* Sock = list[i]->GetSocket();
1885 Sock->WriteLine(":"+std::string(user->nick)+" PRIVMSG "+std::string(c->name)+" :"+text);
1891 virtual void OnBackgroundTimer(time_t curtime)
1893 AutoConnectServers(curtime);
1894 DoPingChecks(curtime);
1897 virtual void OnUserJoin(userrec* user, chanrec* channel)
1899 // Only do this for local users
1900 if (std::string(user->server) == Srv->GetServerName())
1902 std::deque<std::string> params;
1904 params.push_back(channel->name);
1907 // if the channel has a key, force the join by emulating the key.
1908 params.push_back(channel->key);
1910 if (channel->GetUserCounter() > 1)
1912 // not the first in the channel
1913 DoOneToMany(user->nick,"JOIN",params);
1917 // first in the channel, set up their permissions
1918 // and the channel TS with FJOIN.
1920 snprintf(ts,24,"%lu",(unsigned long)channel->age);
1922 params.push_back(channel->name);
1923 params.push_back(ts);
1924 params.push_back("@"+std::string(user->nick));
1925 DoOneToMany(Srv->GetServerName(),"FJOIN",params);
1930 virtual void OnUserPart(userrec* user, chanrec* channel)
1932 if (std::string(user->server) == Srv->GetServerName())
1934 std::deque<std::string> params;
1936 params.push_back(channel->name);
1937 DoOneToMany(user->nick,"PART",params);
1941 virtual void OnUserConnect(userrec* user)
1943 char agestr[MAXBUF];
1944 if (std::string(user->server) == Srv->GetServerName())
1946 std::deque<std::string> params;
1947 snprintf(agestr,MAXBUF,"%lu",(unsigned long)user->age);
1949 params.push_back(agestr);
1950 params.push_back(user->nick);
1951 params.push_back(user->host);
1952 params.push_back(user->dhost);
1953 params.push_back(user->ident);
1954 params.push_back("+"+std::string(user->modes));
1955 params.push_back(user->ip);
1956 params.push_back(":"+std::string(user->fullname));
1957 DoOneToMany(Srv->GetServerName(),"NICK",params);
1961 virtual void OnUserQuit(userrec* user, std::string reason)
1963 if (std::string(user->server) == Srv->GetServerName())
1965 std::deque<std::string> params;
1966 params.push_back(":"+reason);
1967 DoOneToMany(user->nick,"QUIT",params);
1971 virtual void OnUserPostNick(userrec* user, std::string oldnick)
1973 if (std::string(user->server) == Srv->GetServerName())
1975 std::deque<std::string> params;
1976 params.push_back(user->nick);
1977 DoOneToMany(oldnick,"NICK",params);
1981 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, std::string reason)
1983 if (std::string(source->server) == Srv->GetServerName())
1985 std::deque<std::string> params;
1986 params.push_back(chan->name);
1987 params.push_back(user->nick);
1988 params.push_back(":"+reason);
1989 DoOneToMany(source->nick,"KICK",params);
1993 virtual void OnRemoteKill(userrec* source, userrec* dest, std::string reason)
1995 std::deque<std::string> params;
1996 params.push_back(dest->nick);
1997 params.push_back(":"+reason);
1998 DoOneToMany(source->nick,"KILL",params);
2001 virtual void OnRehash(std::string parameter)
2003 if (parameter != "")
2005 std::deque<std::string> params;
2006 params.push_back(parameter);
2007 DoOneToMany(Srv->GetServerName(),"REHASH",params);
2009 if (Srv->MatchText(Srv->GetServerName(),parameter))
2011 Srv->SendOpers("*** Remote rehash initiated from server \002"+Srv->GetServerName()+"\002.");
2012 Srv->RehashServer();
2015 ReadConfiguration(false);
2018 // note: the protocol does not allow direct umode +o except
2019 // via NICK with 8 params. sending OPERTYPE infers +o modechange
2021 virtual void OnOper(userrec* user, std::string opertype)
2023 if (std::string(user->server) == Srv->GetServerName())
2025 std::deque<std::string> params;
2026 params.push_back(opertype);
2027 DoOneToMany(user->nick,"OPERTYPE",params);
2031 virtual void OnMode(userrec* user, void* dest, int target_type, std::string text)
2033 if (std::string(user->server) == Srv->GetServerName())
2035 if (target_type == TYPE_USER)
2037 userrec* u = (userrec*)dest;
2038 std::deque<std::string> params;
2039 params.push_back(u->nick);
2040 params.push_back(text);
2041 DoOneToMany(user->nick,"MODE",params);
2045 chanrec* c = (chanrec*)dest;
2046 std::deque<std::string> params;
2047 params.push_back(c->name);
2048 params.push_back(text);
2049 DoOneToMany(user->nick,"MODE",params);
2054 virtual void ProtoSendMode(void* opaque, int target_type, void* target, std::string modeline)
2056 TreeSocket* s = (TreeSocket*)opaque;
2059 if (target_type == TYPE_USER)
2061 userrec* u = (userrec*)target;
2062 s->WriteLine(":"+Srv->GetServerName()+" FMODE "+u->nick+" "+modeline);
2066 chanrec* c = (chanrec*)target;
2067 s->WriteLine(":"+Srv->GetServerName()+" FMODE "+c->name+" "+modeline);
2072 virtual ~ModuleSpanningTree()
2077 virtual Version GetVersion()
2079 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
2084 class ModuleSpanningTreeFactory : public ModuleFactory
2087 ModuleSpanningTreeFactory()
2091 ~ModuleSpanningTreeFactory()
2095 virtual Module * CreateModule()
2097 TreeProtocolModule = new ModuleSpanningTree;
2098 return TreeProtocolModule;
2104 extern "C" void * init_module( void )
2106 return new ModuleSpanningTreeFactory;