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);
107 std::string GetName()
109 return this->ServerName;
112 std::string GetDesc()
114 return this->ServerDesc;
117 std::string GetVersion()
119 return this->VersionString;
122 void SetNextPingTime(time_t t)
125 LastPingWasGood = false;
128 time_t NextPingTime()
130 return this->NextPing;
133 bool AnsweredLastPing()
135 return LastPingWasGood;
140 LastPingWasGood = true;
145 return this->UserCount;
150 return this->OperCount;
153 TreeSocket* GetSocket()
158 TreeServer* GetParent()
163 unsigned int ChildCount()
165 return Children.size();
168 TreeServer* GetChild(unsigned int n)
170 if (n < Children.size())
180 void AddChild(TreeServer* Child)
182 Children.push_back(Child);
185 bool DelChild(TreeServer* Child)
187 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
198 // removes child nodes of this node, and of that node, etc etc
201 bool stillchildren = true;
202 while (stillchildren)
204 stillchildren = false;
205 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
207 TreeServer* s = (TreeServer*)*a;
211 stillchildren = true;
225 std::string SendPass;
226 std::string RecvPass;
227 unsigned long AutoConnect;
228 time_t NextConnectTime;
231 /* $ModDesc: Povides a spanning tree server link protocol */
235 TreeServer *TreeRoot;
236 std::vector<Link> LinkBlocks;
238 TreeServer* RouteEnumerate(TreeServer* Current, std::string ServerName)
240 if (Current->GetName() == ServerName)
242 for (unsigned int q = 0; q < Current->ChildCount(); q++)
244 TreeServer* found = RouteEnumerate(Current->GetChild(q),ServerName);
253 // Returns the locally connected server we must route a
254 // message through to reach server 'ServerName'. This
255 // only applies to one-to-one and not one-to-many routing.
256 TreeServer* BestRouteTo(std::string ServerName)
258 if (ServerName.c_str() == TreeRoot->GetName())
262 // first, find the server by recursively walking the tree
263 TreeServer* Found = RouteEnumerate(TreeRoot,ServerName);
264 // did we find it? If not, they did something wrong, abort.
271 // The server exists, follow its parent nodes until
272 // the parent of the current is 'TreeRoot', we know
273 // then that this is a directly-connected server.
274 while ((Found) && (Found->GetParent() != TreeRoot))
276 Found = Found->GetParent();
282 bool LookForServer(TreeServer* Current, std::string ServerName)
284 if (ServerName == Current->GetName())
286 for (unsigned int q = 0; q < Current->ChildCount(); q++)
288 if (LookForServer(Current->GetChild(q),ServerName))
296 void RFindServer(TreeServer* Current, std::string ServerName)
298 if ((ServerName == Current->GetName()) && (!Found))
305 for (unsigned int q = 0; q < Current->ChildCount(); q++)
308 RFindServer(Current->GetChild(q),ServerName);
314 TreeServer* FindServer(std::string ServerName)
317 RFindServer(TreeRoot,ServerName);
321 bool IsServer(std::string ServerName)
323 return LookForServer(TreeRoot,ServerName);
326 class TreeSocket : public InspSocket
329 std::string in_buffer;
330 ServerState LinkState;
331 std::string InboundServerName;
332 std::string InboundDescription;
334 int num_lost_servers;
336 bool LastPingWasGood;
340 TreeSocket(std::string host, int port, bool listening, unsigned long maxtime)
341 : InspSocket(host, port, listening, maxtime)
344 this->LinkState = LISTENER;
347 TreeSocket(std::string host, int port, bool listening, unsigned long maxtime, std::string ServerName)
348 : InspSocket(host, port, listening, maxtime)
351 this->LinkState = CONNECTING;
354 TreeSocket(int newfd, char* ip)
355 : InspSocket(newfd, ip)
357 this->LinkState = WAIT_AUTH_1;
360 virtual bool OnConnected()
362 if (this->LinkState == CONNECTING)
364 Srv->SendOpers("*** Connection to "+myhost+"["+this->GetIP()+"] established.");
365 // we should send our details here.
366 // if the other side is satisfied, they send theirs.
367 // we do not need to change state here.
368 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
370 if (x->Name == this->myhost)
372 // found who we're supposed to be connecting to, send the neccessary gubbins.
373 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
381 virtual void OnError(InspSocketError e)
385 virtual int OnDisconnect()
390 // recursively send the server tree with distances as hops
391 void SendServers(TreeServer* Current, TreeServer* s, int hops)
394 for (unsigned int q = 0; q < Current->ChildCount(); q++)
396 TreeServer* recursive_server = Current->GetChild(q);
397 if (recursive_server != s)
399 // :source.server SERVER server.name hops :Description
400 snprintf(command,1024,":%s SERVER %s * %d :%s",Current->GetName().c_str(),recursive_server->GetName().c_str(),hops,recursive_server->GetDesc().c_str());
401 this->WriteLine(command);
402 // down to next level
403 this->SendServers(recursive_server, s, hops+1);
408 void SquitServer(TreeServer* Current)
410 // recursively squit the servers attached to 'Current'
411 for (unsigned int q = 0; q < Current->ChildCount(); q++)
413 TreeServer* recursive_server = Current->GetChild(q);
414 this->SquitServer(recursive_server);
416 // Now we've whacked the kids, whack self
418 bool quittingpeople = true;
419 while (quittingpeople)
421 quittingpeople = false;
422 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
424 if (!strcasecmp(u->second->server,Current->GetName().c_str()))
426 Srv->QuitUser(u->second,Current->GetName()+" "+std::string(Srv->GetServerName()));
428 quittingpeople = true;
435 void Squit(TreeServer* Current,std::string reason)
439 std::deque<std::string> params;
440 params.push_back(Current->GetName());
441 params.push_back(":"+reason);
442 DoOneToAllButSender(Current->GetParent()->GetName(),"SQUIT",params,Current->GetName());
443 if (Current->GetParent() == TreeRoot)
445 Srv->SendOpers("Server \002"+Current->GetName()+"\002 split: "+reason);
449 Srv->SendOpers("Server \002"+Current->GetName()+"\002 split from server \002"+Current->GetParent()->GetName()+"\002 with reason: "+reason);
451 num_lost_servers = 0;
453 SquitServer(Current);
455 Current->GetParent()->DelChild(Current);
457 WriteOpers("Netsplit complete, lost \002%d\002 users on \002%d\002 servers.", num_lost_users, num_lost_servers);
461 log(DEFAULT,"Squit from unknown server");
465 bool ForceMode(std::string source, std::deque<std::string> params)
467 userrec* who = new userrec;
468 who->fd = FD_MAGIC_NUMBER;
469 if (params.size() < 2)
472 for (unsigned int q = 0; q < params.size(); q++)
474 modelist[q] = (char*)params[q].c_str();
476 Srv->SendMode(modelist,params.size(),who);
477 DoOneToAllButSender(source,"FMODE",params,source);
482 bool ForceTopic(std::string source, std::deque<std::string> params)
484 // FTOPIC %s %lu %s :%s
485 if (params.size() != 4)
487 std::string channel = params[0];
488 time_t ts = atoi(params[1].c_str());
489 std::string setby = params[2];
490 std::string topic = params[3];
492 chanrec* c = Srv->FindChannel(channel);
495 if ((ts >= c->topicset) || (!*c->topic))
497 strlcpy(c->topic,topic.c_str(),MAXTOPIC);
498 strlcpy(c->setby,setby.c_str(),NICKMAX);
500 WriteChannelWithServ((char*)source.c_str(), c, "TOPIC %s :%s", c->name, c->topic);
505 // all done, send it on its way
506 params[3] = ":" + params[3];
507 DoOneToAllButSender(source,"FTOPIC",params,source);
512 bool ForceJoin(std::string source, std::deque<std::string> params)
514 if (params.size() < 3)
518 char modestring[MAXBUF];
519 char* mode_users[127];
520 mode_users[0] = first;
521 mode_users[1] = modestring;
522 strcpy(mode_users[1],"+");
523 unsigned int modectr = 2;
526 std::string channel = params[0];
527 time_t TS = atoi(params[1].c_str());
530 chanrec* chan = Srv->FindChannel(channel);
535 strlcpy(mode_users[0],channel.c_str(),MAXBUF);
537 // default is a high value, which if we dont have this
538 // channel will let the other side apply their modes.
539 time_t ourTS = time(NULL)+20;
540 chanrec* us = Srv->FindChannel(channel);
546 log(DEBUG,"FJOIN detected, our TS=%lu, their TS=%lu",ourTS,TS);
548 // do this first, so our mode reversals are correctly received by other servers
549 // if there is a TS collision.
550 DoOneToAllButSender(source,"FJOIN",params,source);
552 for (unsigned int usernum = 2; usernum < params.size(); usernum++)
554 // process one channel at a time, applying modes.
555 char* usr = (char*)params[usernum].c_str();
556 char permissions = *usr;
561 mode_users[modectr++] = usr;
562 strlcat(modestring,"o",MAXBUF);
566 mode_users[modectr++] = usr;
567 strlcat(modestring,"h",MAXBUF);
571 mode_users[modectr++] = usr;
572 strlcat(modestring,"v",MAXBUF);
575 who = Srv->FindNick(usr);
578 Srv->JoinUserToChannel(who,channel,key);
579 if (modectr >= (MAXMODES-1))
581 // theres a mode for this user. push them onto the mode queue, and flush it
582 // if there are more than MAXMODES to go.
585 log(DEBUG,"Our our channel newer than theirs, accepting their modes");
586 Srv->SendMode(mode_users,modectr,who);
590 log(DEBUG,"Their channel newer than ours, bouncing their modes");
592 std::deque<std::string> params;
593 // modes are now being UNSET...
594 *mode_users[1] = '-';
595 for (unsigned int x = 0; x < modectr; x++)
597 params.push_back(mode_users[x]);
599 // tell everyone to bounce the modes. bad modes, bad!
600 DoOneToMany(Srv->GetServerName(),"FMODE",params);
602 strcpy(mode_users[1],"+");
607 // there werent enough modes built up to flush it during FJOIN,
608 // or, there are a number left over. flush them out.
609 if ((modectr > 2) && (who))
613 log(DEBUG,"Our our channel newer than theirs, accepting their modes");
614 Srv->SendMode(mode_users,modectr,who);
618 log(DEBUG,"Their channel newer than ours, bouncing their modes");
619 std::deque<std::string> params;
620 *mode_users[1] = '-';
621 for (unsigned int x = 0; x < modectr; x++)
623 params.push_back(mode_users[x]);
625 DoOneToMany(Srv->GetServerName(),"FMODE",params);
631 bool IntroduceClient(std::string source, std::deque<std::string> params)
633 if (params.size() < 8)
635 // NICK age nick host dhost ident +modes ip :gecos
637 std::string nick = params[1];
638 std::string host = params[2];
639 std::string dhost = params[3];
640 std::string ident = params[4];
641 time_t age = atoi(params[0].c_str());
642 std::string modes = params[5];
643 while (*(modes.c_str()) == '+')
645 char* m = (char*)modes.c_str();
649 std::string ip = params[6];
650 std::string gecos = params[7];
651 char* tempnick = (char*)nick.c_str();
652 log(DEBUG,"Introduce client %s!%s@%s",tempnick,ident.c_str(),host.c_str());
654 user_hash::iterator iter;
655 iter = clientlist.find(tempnick);
656 if (iter != clientlist.end())
659 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);
660 this->WriteLine(":"+Srv->GetServerName()+" KILL "+tempnick+" :Nickname collision");
664 clientlist[tempnick] = new userrec();
665 clientlist[tempnick]->fd = FD_MAGIC_NUMBER;
666 strlcpy(clientlist[tempnick]->nick, tempnick,NICKMAX);
667 strlcpy(clientlist[tempnick]->host, host.c_str(),160);
668 strlcpy(clientlist[tempnick]->dhost, dhost.c_str(),160);
669 clientlist[tempnick]->server = (char*)FindServerNamePtr(source.c_str());
670 strlcpy(clientlist[tempnick]->ident, ident.c_str(),IDENTMAX);
671 strlcpy(clientlist[tempnick]->fullname, gecos.c_str(),MAXGECOS);
672 clientlist[tempnick]->registered = 7;
673 clientlist[tempnick]->signon = age;
674 strlcpy(clientlist[tempnick]->modes, modes.c_str(),53);
675 strlcpy(clientlist[tempnick]->ip,ip.c_str(),16);
676 for (int i = 0; i < MAXCHANS; i++)
678 clientlist[tempnick]->chans[i].channel = NULL;
679 clientlist[tempnick]->chans[i].uc_modes = 0;
681 params[7] = ":" + params[7];
682 DoOneToAllButSender(source,"NICK",params,source);
686 void SendFJoins(TreeServer* Current, chanrec* c)
689 snprintf(list,MAXBUF,":%s FJOIN %s %lu",Srv->GetServerName().c_str(),c->name,(unsigned long)c->age);
690 std::vector<char*> *ulist = c->GetUsers();
691 for (unsigned int i = 0; i < ulist->size(); i++)
693 char* o = (*ulist)[i];
694 userrec* otheruser = (userrec*)o;
695 strlcat(list," ",MAXBUF);
696 strlcat(list,cmode(otheruser,c),MAXBUF);
697 strlcat(list,otheruser->nick,MAXBUF);
698 if (strlen(list)>(480-NICKMAX))
700 this->WriteLine(list);
701 snprintf(list,MAXBUF,":%s FJOIN %s %lu",Srv->GetServerName().c_str(),c->name,(unsigned long)c->age);
704 if (list[strlen(list)-1] != ':')
706 this->WriteLine(list);
710 void SendChannelModes(TreeServer* Current)
713 for (chan_hash::iterator c = chanlist.begin(); c != chanlist.end(); c++)
715 SendFJoins(Current, c->second);
716 snprintf(data,MAXBUF,":%s FMODE %s +%s",Srv->GetServerName().c_str(),c->second->name,chanmodes(c->second));
717 this->WriteLine(data);
718 if (*c->second->topic)
720 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);
721 this->WriteLine(data);
723 for (BanList::iterator b = c->second->bans.begin(); b != c->second->bans.end(); b++)
725 snprintf(data,MAXBUF,":%s FMODE %s +b %s",Srv->GetServerName().c_str(),c->second->name,b->data);
726 this->WriteLine(data);
728 FOREACH_MOD OnSyncChannel(c->second,(Module*)TreeProtocolModule,(void*)this);
732 // send all users and their channels
733 void SendUsers(TreeServer* Current)
736 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
738 if (u->second->registered == 7)
740 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);
741 this->WriteLine(data);
742 if (strchr(u->second->modes,'o'))
744 this->WriteLine(":"+std::string(u->second->nick)+" OPERTYPE "+std::string(u->second->oper));
746 //char* chl = chlist(u->second,u->second);
749 // this->WriteLine(":"+std::string(u->second->nick)+" FJOIN "+std::string(chl));
751 FOREACH_MOD OnSyncUser(u->second,(Module*)TreeProtocolModule,(void*)this);
756 void DoBurst(TreeServer* s)
758 Srv->SendOpers("*** Bursting to "+s->GetName()+".");
759 this->WriteLine("BURST");
761 this->SendServers(TreeRoot,s,1);
762 // Send users and their channels
764 // Send everything else (channel modes etc)
765 this->SendChannelModes(s);
766 this->WriteLine("ENDBURST");
769 virtual bool OnDataReady()
771 char* data = this->Read();
774 this->in_buffer += data;
775 while (in_buffer.find("\n") != std::string::npos)
777 char* line = (char*)in_buffer.c_str();
778 std::string ret = "";
779 while ((*line != '\n') && (strlen(line)))
784 if ((*line == '\n') || (*line == '\r'))
787 if (!this->ProcessLine(ret))
793 return (data != NULL);
796 int WriteLine(std::string line)
798 return this->Write(line + "\r\n");
801 bool Error(std::deque<std::string> params)
803 if (params.size() < 1)
805 std::string Errmsg = params[0];
806 std::string SName = myhost;
807 if (InboundServerName != "")
809 SName = InboundServerName;
811 Srv->SendOpers("*** ERROR from "+SName+": "+Errmsg);
812 // we will return false to cause the socket to close.
816 bool OperType(std::string prefix, std::deque<std::string> params)
818 if (params.size() != 1)
820 std::string opertype = params[0];
821 userrec* u = Srv->FindNick(prefix);
824 strlcpy(u->oper,opertype.c_str(),NICKMAX);
825 if (!strchr(u->modes,'o'))
827 strcat(u->modes,"o");
829 DoOneToAllButSender(u->server,"OPERTYPE",params,u->server);
834 bool RemoteRehash(std::string prefix, std::deque<std::string> params)
836 if (params.size() < 1)
838 std::string servermask = params[0];
839 if (Srv->MatchText(Srv->GetServerName(),servermask))
841 Srv->SendOpers("*** Remote rehash initiated from server \002"+prefix+"\002.");
843 ReadConfiguration(false);
845 DoOneToAllButSender(prefix,"REHASH",params,prefix);
849 bool RemoteKill(std::string prefix, std::deque<std::string> params)
851 if (params.size() != 2)
853 std::string nick = params[0];
854 std::string reason = params[1];
855 userrec* u = Srv->FindNick(prefix);
856 userrec* who = Srv->FindNick(nick);
859 std::string sourceserv = prefix;
862 sourceserv = u->server;
864 params[1] = ":" + params[1];
865 DoOneToAllButSender(prefix,"KILL",params,sourceserv);
866 Srv->QuitUser(who,reason);
871 bool LocalPong(std::string prefix, std::deque<std::string> params)
873 if (params.size() < 1)
875 TreeServer* ServerSource = FindServer(prefix);
878 ServerSource->SetPingFlag();
883 bool LocalPing(std::string prefix, std::deque<std::string> params)
885 if (params.size() < 1)
887 std::string stufftobounce = params[0];
888 this->WriteLine(":"+Srv->GetServerName()+" PONG "+stufftobounce);
892 bool RemoteServer(std::string prefix, std::deque<std::string> params)
894 if (params.size() < 4)
896 std::string servername = params[0];
897 std::string password = params[1];
898 // hopcount is not used for a remote server, we calculate this ourselves
899 std::string description = params[3];
900 TreeServer* ParentOfThis = FindServer(prefix);
903 this->WriteLine("ERROR :Protocol error - Introduced remote server from unknown server "+prefix);
906 TreeServer* CheckDupe = FindServer(servername);
909 this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
912 TreeServer* Node = new TreeServer(servername,description,ParentOfThis,NULL);
913 ParentOfThis->AddChild(Node);
914 params[3] = ":" + params[3];
915 DoOneToAllButSender(prefix,"SERVER",params,prefix);
916 Srv->SendOpers("*** Server \002"+prefix+"\002 introduced server \002"+servername+"\002 ("+description+")");
920 bool Outbound_Reply_Server(std::deque<std::string> params)
922 if (params.size() < 4)
924 std::string servername = params[0];
925 std::string password = params[1];
926 int hops = atoi(params[2].c_str());
929 this->WriteLine("ERROR :Server too far away for authentication");
932 std::string description = params[3];
933 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
935 if ((x->Name == servername) && (x->RecvPass == password))
937 TreeServer* CheckDupe = FindServer(servername);
940 this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
943 // Begin the sync here. this kickstarts the
944 // other side, waiting in WAIT_AUTH_2 state,
945 // into starting their burst, as it shows
947 this->LinkState = CONNECTED;
948 // we should add the details of this server now
949 // to the servers tree, as a child of the root
951 TreeServer* Node = new TreeServer(servername,description,TreeRoot,this);
952 TreeRoot->AddChild(Node);
953 params[3] = ":" + params[3];
954 DoOneToAllButSender(TreeRoot->GetName(),"SERVER",params,servername);
959 this->WriteLine("ERROR :Invalid credentials");
963 bool Inbound_Server(std::deque<std::string> params)
965 if (params.size() < 4)
967 std::string servername = params[0];
968 std::string password = params[1];
969 int hops = atoi(params[2].c_str());
972 this->WriteLine("ERROR :Server too far away for authentication");
975 std::string description = params[3];
976 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
978 if ((x->Name == servername) && (x->RecvPass == password))
980 TreeServer* CheckDupe = FindServer(servername);
983 this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
986 Srv->SendOpers("*** Verified incoming server connection from \002"+servername+"\002["+this->GetIP()+"] ("+description+")");
987 this->InboundServerName = servername;
988 this->InboundDescription = description;
989 // this is good. Send our details: Our server name and description and hopcount of 0,
990 // along with the sendpass from this block.
991 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
992 // move to the next state, we are now waiting for THEM.
993 this->LinkState = WAIT_AUTH_2;
997 this->WriteLine("ERROR :Invalid credentials");
1001 std::deque<std::string> Split(std::string line, bool stripcolon)
1003 std::deque<std::string> n;
1004 if (!strchr(line.c_str(),' '))
1009 std::stringstream s(line);
1010 std::string param = "";
1029 if ((param == ":") && (item > 0))
1052 bool ProcessLine(std::string line)
1054 char* l = (char*)line.c_str();
1055 while ((strlen(l)) && (l[strlen(l)-1] == '\r') || (l[strlen(l)-1] == '\n'))
1056 l[strlen(l)-1] = '\0';
1060 Srv->Log(DEBUG,"IN: '"+line+"'");
1061 std::deque<std::string> params = this->Split(line,true);
1062 std::string command = "";
1063 std::string prefix = "";
1064 if (((params[0].c_str())[0] == ':') && (params.size() > 1))
1067 command = params[1];
1068 char* pref = (char*)prefix.c_str();
1076 command = params[0];
1080 switch (this->LinkState)
1085 // Waiting for SERVER command from remote server. Server initiating
1086 // the connection sends the first SERVER command, listening server
1087 // replies with theirs if its happy, then if the initiator is happy,
1088 // it starts to send its net sync, which starts the merge, otherwise
1089 // it sends an ERROR.
1090 if (command == "SERVER")
1092 return this->Inbound_Server(params);
1094 else if (command == "ERROR")
1096 return this->Error(params);
1100 // Waiting for start of other side's netmerge to say they liked our
1102 if (command == "SERVER")
1104 // cant do this, they sent it to us in the WAIT_AUTH_1 state!
1108 else if (command == "BURST")
1110 this->LinkState = CONNECTED;
1111 Node = new TreeServer(InboundServerName,InboundDescription,TreeRoot,this);
1112 TreeRoot->AddChild(Node);
1114 params.push_back(InboundServerName);
1115 params.push_back("*");
1116 params.push_back("1");
1117 params.push_back(":"+InboundDescription);
1118 DoOneToAllButSender(TreeRoot->GetName(),"SERVER",params,InboundServerName);
1119 this->DoBurst(Node);
1121 else if (command == "ERROR")
1123 return this->Error(params);
1128 this->WriteLine("ERROR :Internal error -- listening socket accepted its own descriptor!!!");
1132 if (command == "SERVER")
1134 // another server we connected to, which was in WAIT_AUTH_1 state,
1135 // has just sent us their credentials. If we get this far, theyre
1136 // happy with OUR credentials, and they are now in WAIT_AUTH_2 state.
1137 // if we're happy with this, we should send our netburst which
1138 // kickstarts the merge.
1139 return this->Outbound_Reply_Server(params);
1141 else if (command == "ERROR")
1143 return this->Error(params);
1147 // This is the 'authenticated' state, when all passwords
1148 // have been exchanged and anything past this point is taken
1150 std::string target = "";
1151 if ((command == "NICK") && (params.size() > 1))
1153 return this->IntroduceClient(prefix,params);
1155 else if (command == "FJOIN")
1157 return this->ForceJoin(prefix,params);
1159 else if (command == "SERVER")
1161 return this->RemoteServer(prefix,params);
1163 else if (command == "ERROR")
1165 return this->Error(params);
1167 else if (command == "OPERTYPE")
1169 return this->OperType(prefix,params);
1171 else if (command == "FMODE")
1173 return this->ForceMode(prefix,params);
1175 else if (command == "KILL")
1177 return this->RemoteKill(prefix,params);
1179 else if (command == "FTOPIC")
1181 return this->ForceTopic(prefix,params);
1183 else if (command == "REHASH")
1185 return this->RemoteRehash(prefix,params);
1187 else if (command == "PING")
1189 return this->LocalPing(prefix,params);
1191 else if (command == "PONG")
1193 return this->LocalPong(prefix,params);
1195 else if (command == "SQUIT")
1197 if (params.size() == 2)
1199 this->Squit(FindServer(params[0]),params[1]);
1205 // not a special inter-server command.
1206 // Emulate the actual user doing the command,
1207 // this saves us having a huge ugly parser.
1208 userrec* who = Srv->FindNick(prefix);
1209 std::string sourceserv = this->myhost;
1210 if (this->InboundServerName != "")
1212 sourceserv = this->InboundServerName;
1217 target = who->server;
1218 char* strparams[127];
1219 for (unsigned int q = 0; q < params.size(); q++)
1221 strparams[q] = (char*)params[q].c_str();
1223 Srv->CallCommandHandler(command, strparams, params.size(), who);
1227 // its not a user. Its either a server, or somethings screwed up.
1228 if (IsServer(prefix))
1230 target = Srv->GetServerName();
1234 log(DEBUG,"Command with unknown origin '%s'",prefix.c_str());
1238 return DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1247 virtual std::string GetName()
1249 std::string sourceserv = this->myhost;
1250 if (this->InboundServerName != "")
1252 sourceserv = this->InboundServerName;
1257 virtual void OnTimeout()
1259 if (this->LinkState == CONNECTING)
1261 Srv->SendOpers("*** CONNECT: Connection to \002"+myhost+"\002 timed out.");
1265 virtual void OnClose()
1267 // Connection closed.
1268 // If the connection is fully up (state CONNECTED)
1269 // then propogate a netsplit to all peers.
1270 std::string quitserver = this->myhost;
1271 if (this->InboundServerName != "")
1273 quitserver = this->InboundServerName;
1275 TreeServer* s = FindServer(quitserver);
1278 Squit(s,"Remote host closed the connection");
1282 virtual int OnIncomingConnection(int newsock, char* ip)
1284 TreeSocket* s = new TreeSocket(newsock, ip);
1290 void AddThisServer(TreeServer* server, std::deque<TreeServer*> &list)
1292 for (unsigned int c = 0; c < list.size(); c++)
1294 if (list[c] == server)
1299 list.push_back(server);
1302 // returns a list of DIRECT servernames for a specific channel
1303 std::deque<TreeServer*> GetListOfServersForChannel(chanrec* c)
1305 std::deque<TreeServer*> list;
1306 std::vector<char*> *ulist = c->GetUsers();
1307 for (unsigned int i = 0; i < ulist->size(); i++)
1309 char* o = (*ulist)[i];
1310 userrec* otheruser = (userrec*)o;
1311 if (std::string(otheruser->server) != Srv->GetServerName())
1313 TreeServer* best = BestRouteTo(otheruser->server);
1315 AddThisServer(best,list);
1321 bool DoOneToAllButSenderRaw(std::string data,std::string omit,std::string prefix,std::string command,std::deque<std::string> params)
1323 TreeServer* omitroute = BestRouteTo(omit);
1324 if ((command == "NOTICE") || (command == "PRIVMSG"))
1326 if ((params.size() >= 2) && (*(params[0].c_str()) != '$'))
1328 if (*(params[0].c_str()) != '#')
1330 // special routing for private messages/notices
1331 userrec* d = Srv->FindNick(params[0]);
1334 std::deque<std::string> par;
1336 par.push_back(params[0]);
1337 par.push_back(":"+params[1]);
1338 DoOneToOne(prefix,command,par,d->server);
1344 log(DEBUG,"Channel privmsg going to chan %s",params[0].c_str());
1345 chanrec* c = Srv->FindChannel(params[0]);
1348 std::deque<TreeServer*> list = GetListOfServersForChannel(c);
1349 log(DEBUG,"Got a list of %d servers",list.size());
1350 for (unsigned int i = 0; i < list.size(); i++)
1352 TreeSocket* Sock = list[i]->GetSocket();
1353 if ((Sock) && (list[i]->GetName() != omit) && (omitroute != list[i]))
1355 log(DEBUG,"Writing privmsg to server %s",list[i]->GetName().c_str());
1356 Sock->WriteLine(data);
1364 for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1366 TreeServer* Route = TreeRoot->GetChild(x);
1367 if ((Route->GetSocket()) && (Route->GetName() != omit) && (omitroute != Route))
1369 TreeSocket* Sock = Route->GetSocket();
1370 Sock->WriteLine(data);
1376 bool DoOneToAllButSender(std::string prefix, std::string command, std::deque<std::string> params, std::string omit)
1378 TreeServer* omitroute = BestRouteTo(omit);
1379 std::string FullLine = ":" + prefix + " " + command;
1380 for (unsigned int x = 0; x < params.size(); x++)
1382 FullLine = FullLine + " " + params[x];
1384 for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1386 TreeServer* Route = TreeRoot->GetChild(x);
1387 // Send the line IF:
1388 // The route has a socket (its a direct connection)
1389 // The route isnt the one to be omitted
1390 // The route isnt the path to the one to be omitted
1391 if ((Route->GetSocket()) && (Route->GetName() != omit) && (omitroute != Route))
1393 TreeSocket* Sock = Route->GetSocket();
1394 Sock->WriteLine(FullLine);
1400 bool DoOneToMany(std::string prefix, std::string command, std::deque<std::string> params)
1402 std::string FullLine = ":" + prefix + " " + command;
1403 for (unsigned int x = 0; x < params.size(); x++)
1405 FullLine = FullLine + " " + params[x];
1407 for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1409 TreeServer* Route = TreeRoot->GetChild(x);
1410 if (Route->GetSocket())
1412 TreeSocket* Sock = Route->GetSocket();
1413 Sock->WriteLine(FullLine);
1419 bool DoOneToOne(std::string prefix, std::string command, std::deque<std::string> params, std::string target)
1421 TreeServer* Route = BestRouteTo(target);
1424 std::string FullLine = ":" + prefix + " " + command;
1425 for (unsigned int x = 0; x < params.size(); x++)
1427 FullLine = FullLine + " " + params[x];
1429 if (Route->GetSocket())
1431 TreeSocket* Sock = Route->GetSocket();
1432 Sock->WriteLine(FullLine);
1442 std::vector<TreeSocket*> Bindings;
1444 void ReadConfiguration(bool rebind)
1448 for (int j =0; j < Conf->Enumerate("bind"); j++)
1450 std::string Type = Conf->ReadValue("bind","type",j);
1451 std::string IP = Conf->ReadValue("bind","address",j);
1452 long Port = Conf->ReadInteger("bind","port",j,true);
1453 if (Type == "servers")
1459 TreeSocket* listener = new TreeSocket(IP.c_str(),Port,true,10);
1460 if (listener->GetState() == I_LISTENING)
1462 Srv->AddSocket(listener);
1463 Bindings.push_back(listener);
1467 log(DEFAULT,"m_spanningtree: Warning: Failed to bind server port %d",Port);
1475 for (int j =0; j < Conf->Enumerate("link"); j++)
1478 L.Name = Conf->ReadValue("link","name",j);
1479 L.IPAddr = Conf->ReadValue("link","ipaddr",j);
1480 L.Port = Conf->ReadInteger("link","port",j,true);
1481 L.SendPass = Conf->ReadValue("link","sendpass",j);
1482 L.RecvPass = Conf->ReadValue("link","recvpass",j);
1483 L.AutoConnect = Conf->ReadInteger("link","autoconnect",j,true);
1484 L.NextConnectTime = time(NULL) + L.AutoConnect;
1485 LinkBlocks.push_back(L);
1486 log(DEBUG,"m_spanningtree: Read server %s with host %s:%d",L.Name.c_str(),L.IPAddr.c_str(),L.Port);
1491 class ModuleSpanningTree : public Module
1493 std::vector<TreeSocket*> Bindings;
1499 ModuleSpanningTree()
1502 Conf = new ConfigReader;
1505 // Create the root of the tree
1506 TreeRoot = new TreeServer(Srv->GetServerName(),Srv->GetServerDescription());
1508 ReadConfiguration(true);
1511 void ShowLinks(TreeServer* Current, userrec* user, int hops)
1513 std::string Parent = TreeRoot->GetName();
1514 if (Current->GetParent())
1516 Parent = Current->GetParent()->GetName();
1518 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1520 ShowLinks(Current->GetChild(q),user,hops+1);
1522 WriteServ(user->fd,"364 %s %s %s :%d %s",user->nick,Current->GetName().c_str(),Parent.c_str(),hops,Current->GetDesc().c_str());
1525 int CountLocalServs()
1527 return TreeRoot->ChildCount();
1530 void CountServsRecursive(TreeServer* Current)
1533 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1535 CountServsRecursive(Current->GetChild(q));
1542 CountServsRecursive(TreeRoot);
1546 void HandleLinks(char** parameters, int pcnt, userrec* user)
1548 ShowLinks(TreeRoot,user,0);
1549 WriteServ(user->fd,"365 %s * :End of /LINKS list.",user->nick);
1553 void HandleLusers(char** parameters, int pcnt, userrec* user)
1555 WriteServ(user->fd,"251 %s :There are %d users and %d invisible on %d servers",user->nick,usercnt()-usercount_invisible(),usercount_invisible(),this->CountServs());
1556 WriteServ(user->fd,"252 %s %d :operator(s) online",user->nick,usercount_opers());
1557 WriteServ(user->fd,"253 %s %d :unknown connections",user->nick,usercount_unknown());
1558 WriteServ(user->fd,"254 %s %d :channels formed",user->nick,chancount());
1559 WriteServ(user->fd,"254 %s :I have %d clients and %d servers",user->nick,local_count(),this->CountLocalServs());
1563 // WARNING: NOT THREAD SAFE - DONT GET ANY SMART IDEAS.
1565 void ShowMap(TreeServer* Current, userrec* user, int depth, char matrix[128][80])
1569 for (int t = 0; t < depth; t++)
1571 matrix[line][t] = ' ';
1573 strlcpy(&matrix[line][depth],Current->GetName().c_str(),80);
1575 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1577 ShowMap(Current->GetChild(q),user,depth+2,matrix);
1582 // Ok, prepare to be confused.
1583 // After much mulling over how to approach this, it struck me that
1584 // the 'usual' way of doing a /MAP isnt the best way. Instead of
1585 // keeping track of a ton of ascii characters, and line by line
1586 // under recursion working out where to place them using multiplications
1587 // and divisons, we instead render the map onto a backplane of characters
1588 // (a character matrix), then draw the branches as a series of "L" shapes
1589 // from the nodes. This is not only friendlier on CPU it uses less stack.
1591 void HandleMap(char** parameters, int pcnt, userrec* user)
1593 // This array represents a virtual screen which we will
1594 // "scratch" draw to, as the console device of an irc
1595 // client does not provide for a proper terminal.
1596 char matrix[128][80];
1597 for (unsigned int t = 0; t < 128; t++)
1599 matrix[t][0] = '\0';
1602 // The only recursive bit is called here.
1603 ShowMap(TreeRoot,user,0,matrix);
1604 // Process each line one by one. The algorithm has a limit of
1605 // 128 servers (which is far more than a spanning tree should have
1606 // anyway, so we're ok). This limit can be raised simply by making
1607 // the character matrix deeper, 128 rows taking 10k of memory.
1608 for (int l = 1; l < line; l++)
1610 // scan across the line looking for the start of the
1611 // servername (the recursive part of the algorithm has placed
1612 // the servers at indented positions depending on what they
1614 int first_nonspace = 0;
1615 while (matrix[l][first_nonspace] == ' ')
1620 // Draw the `- (corner) section: this may be overwritten by
1621 // another L shape passing along the same vertical pane, becoming
1622 // a |- (branch) section instead.
1623 matrix[l][first_nonspace] = '-';
1624 matrix[l][first_nonspace-1] = '`';
1626 // Draw upwards until we hit the parent server, causing possibly
1627 // other corners (`-) to become branches (|-)
1628 while ((matrix[l2][first_nonspace-1] == ' ') || (matrix[l2][first_nonspace-1] == '`'))
1630 matrix[l2][first_nonspace-1] = '|';
1634 // dump the whole lot to the user. This is the easy bit, honest.
1635 for (int t = 0; t < line; t++)
1637 WriteServ(user->fd,"006 %s :%s",user->nick,&matrix[t][0]);
1639 WriteServ(user->fd,"007 %s :End of /MAP",user->nick);
1643 int HandleSquit(char** parameters, int pcnt, userrec* user)
1645 TreeServer* s = FindServer(parameters[0]);
1648 TreeSocket* sock = s->GetSocket();
1651 WriteOpers("*** SQUIT: Server \002%s\002 removed from network by %s",parameters[0],user->nick);
1652 sock->Squit(s,"Server quit by "+std::string(user->nick)+"!"+std::string(user->ident)+"@"+std::string(user->host));
1657 WriteServ(user->fd,"NOTICE %s :*** SQUIT: The server \002%s\002 is not directly connected.",user->nick,parameters[0]);
1662 WriteServ(user->fd,"NOTICE %s :*** SQUIT: The server \002%s\002 does not exist on the network.",user->nick,parameters[0]);
1667 void DoPingChecks(time_t curtime)
1669 for (unsigned int j = 0; j < TreeRoot->ChildCount(); j++)
1671 TreeServer* serv = TreeRoot->GetChild(j);
1672 TreeSocket* sock = serv->GetSocket();
1675 if (curtime >= serv->NextPingTime())
1677 if (serv->AnsweredLastPing())
1679 sock->WriteLine(":"+Srv->GetServerName()+" PING "+serv->GetName());
1680 serv->SetNextPingTime(curtime + 60);
1684 // they didnt answer, boot them
1685 WriteOpers("*** Server \002%s\002 pinged out",serv->GetName().c_str());
1686 sock->Squit(serv,"Ping timeout");
1695 void AutoConnectServers(time_t curtime)
1697 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
1699 if ((x->AutoConnect) && (curtime >= x->NextConnectTime))
1701 log(DEBUG,"Auto-Connecting %s",x->Name.c_str());
1702 x->NextConnectTime = curtime + x->AutoConnect;
1703 TreeServer* CheckDupe = FindServer(x->Name);
1706 // an autoconnected server is not connected. Check if its time to connect it
1707 WriteOpers("*** AUTOCONNECT: Auto-connecting server \002%s\002 (%lu seconds until next attempt)",x->Name.c_str(),x->AutoConnect);
1708 TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name);
1709 Srv->AddSocket(newsocket);
1715 int HandleConnect(char** parameters, int pcnt, userrec* user)
1717 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
1719 if (Srv->MatchText(x->Name.c_str(),parameters[0]))
1721 TreeServer* CheckDupe = FindServer(x->Name);
1724 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);
1725 TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name);
1726 Srv->AddSocket(newsocket);
1731 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());
1736 WriteServ(user->fd,"NOTICE %s :*** CONNECT: No server matching \002%s\002 could be found in the config file.",user->nick,parameters[0]);
1740 virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
1742 if (command == "CONNECT")
1744 return this->HandleConnect(parameters,pcnt,user);
1746 else if (command == "SQUIT")
1748 return this->HandleSquit(parameters,pcnt,user);
1750 else if (command == "MAP")
1752 this->HandleMap(parameters,pcnt,user);
1755 else if (command == "LUSERS")
1757 this->HandleLusers(parameters,pcnt,user);
1760 else if (command == "LINKS")
1762 this->HandleLinks(parameters,pcnt,user);
1765 else if (Srv->IsValidModuleCommand(command, pcnt, user))
1767 // this bit of code cleverly routes all module commands
1768 // to all remote severs *automatically* so that modules
1769 // can just handle commands locally, without having
1770 // to have any special provision in place for remote
1771 // commands and linking protocols.
1772 std::deque<std::string> params;
1774 for (int j = 0; j < pcnt; j++)
1776 if (strchr(parameters[j],' '))
1778 params.push_back(":" + std::string(parameters[j]));
1782 params.push_back(std::string(parameters[j]));
1785 DoOneToMany(user->nick,command,params);
1790 virtual void OnGetServerDescription(std::string servername,std::string &description)
1792 TreeServer* s = FindServer(servername);
1795 description = s->GetDesc();
1799 virtual void OnUserInvite(userrec* source,userrec* dest,chanrec* channel)
1801 if (std::string(source->server) == Srv->GetServerName())
1803 std::deque<std::string> params;
1804 params.push_back(dest->nick);
1805 params.push_back(channel->name);
1806 DoOneToMany(source->nick,"INVITE",params);
1810 virtual void OnPostLocalTopicChange(userrec* user, chanrec* chan, std::string topic)
1812 std::deque<std::string> params;
1813 params.push_back(chan->name);
1814 params.push_back(":"+topic);
1815 DoOneToMany(user->nick,"TOPIC",params);
1818 virtual void OnWallops(userrec* user, std::string text)
1820 if (std::string(user->server) == Srv->GetServerName())
1822 std::deque<std::string> params;
1823 params.push_back(":"+text);
1824 DoOneToMany(user->nick,"WALLOPS",params);
1828 virtual void OnUserNotice(userrec* user, void* dest, int target_type, std::string text)
1830 if (target_type == TYPE_USER)
1832 userrec* d = (userrec*)dest;
1833 if ((std::string(d->server) != Srv->GetServerName()) && (std::string(user->server) == Srv->GetServerName()))
1835 std::deque<std::string> params;
1837 params.push_back(d->nick);
1838 params.push_back(":"+text);
1839 DoOneToOne(user->nick,"NOTICE",params,d->server);
1844 if (std::string(user->server) == Srv->GetServerName())
1846 chanrec *c = (chanrec*)dest;
1847 std::deque<TreeServer*> list = GetListOfServersForChannel(c);
1848 for (unsigned int i = 0; i < list.size(); i++)
1850 TreeSocket* Sock = list[i]->GetSocket();
1852 Sock->WriteLine(":"+std::string(user->nick)+" NOTICE "+std::string(c->name)+" :"+text);
1858 virtual void OnUserMessage(userrec* user, void* dest, int target_type, std::string text)
1860 if (target_type == TYPE_USER)
1862 // route private messages which are targetted at clients only to the server
1863 // which needs to receive them
1864 userrec* d = (userrec*)dest;
1865 if ((std::string(d->server) != Srv->GetServerName()) && (std::string(user->server) == Srv->GetServerName()))
1867 std::deque<std::string> params;
1869 params.push_back(d->nick);
1870 params.push_back(":"+text);
1871 DoOneToOne(user->nick,"PRIVMSG",params,d->server);
1876 if (std::string(user->server) == Srv->GetServerName())
1878 chanrec *c = (chanrec*)dest;
1879 std::deque<TreeServer*> list = GetListOfServersForChannel(c);
1880 for (unsigned int i = 0; i < list.size(); i++)
1882 TreeSocket* Sock = list[i]->GetSocket();
1884 Sock->WriteLine(":"+std::string(user->nick)+" PRIVMSG "+std::string(c->name)+" :"+text);
1890 virtual void OnBackgroundTimer(time_t curtime)
1892 AutoConnectServers(curtime);
1893 DoPingChecks(curtime);
1896 virtual void OnUserJoin(userrec* user, chanrec* channel)
1898 // Only do this for local users
1899 if (std::string(user->server) == Srv->GetServerName())
1901 std::deque<std::string> params;
1903 params.push_back(channel->name);
1906 // if the channel has a key, force the join by emulating the key.
1907 params.push_back(channel->key);
1909 if (channel->GetUserCounter() > 1)
1911 // not the first in the channel
1912 DoOneToMany(user->nick,"JOIN",params);
1916 // first in the channel, set up their permissions
1917 // and the channel TS with FJOIN.
1919 snprintf(ts,24,"%lu",(unsigned long)channel->age);
1921 params.push_back(channel->name);
1922 params.push_back(ts);
1923 params.push_back("@"+std::string(user->nick));
1924 DoOneToMany(Srv->GetServerName(),"FJOIN",params);
1929 virtual void OnUserPart(userrec* user, chanrec* channel)
1931 if (std::string(user->server) == Srv->GetServerName())
1933 std::deque<std::string> params;
1935 params.push_back(channel->name);
1936 DoOneToMany(user->nick,"PART",params);
1940 virtual void OnUserConnect(userrec* user)
1942 char agestr[MAXBUF];
1943 if (std::string(user->server) == Srv->GetServerName())
1945 std::deque<std::string> params;
1946 snprintf(agestr,MAXBUF,"%lu",(unsigned long)user->age);
1948 params.push_back(agestr);
1949 params.push_back(user->nick);
1950 params.push_back(user->host);
1951 params.push_back(user->dhost);
1952 params.push_back(user->ident);
1953 params.push_back("+"+std::string(user->modes));
1954 params.push_back(user->ip);
1955 params.push_back(":"+std::string(user->fullname));
1956 DoOneToMany(Srv->GetServerName(),"NICK",params);
1960 virtual void OnUserQuit(userrec* user, std::string reason)
1962 if (std::string(user->server) == Srv->GetServerName())
1964 std::deque<std::string> params;
1965 params.push_back(":"+reason);
1966 DoOneToMany(user->nick,"QUIT",params);
1970 virtual void OnUserPostNick(userrec* user, std::string oldnick)
1972 if (std::string(user->server) == Srv->GetServerName())
1974 std::deque<std::string> params;
1975 params.push_back(user->nick);
1976 DoOneToMany(oldnick,"NICK",params);
1980 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, std::string reason)
1982 if (std::string(source->server) == Srv->GetServerName())
1984 std::deque<std::string> params;
1985 params.push_back(chan->name);
1986 params.push_back(user->nick);
1987 params.push_back(":"+reason);
1988 DoOneToMany(source->nick,"KICK",params);
1992 virtual void OnRemoteKill(userrec* source, userrec* dest, std::string reason)
1994 std::deque<std::string> params;
1995 params.push_back(dest->nick);
1996 params.push_back(":"+reason);
1997 DoOneToMany(source->nick,"KILL",params);
2000 virtual void OnRehash(std::string parameter)
2002 if (parameter != "")
2004 std::deque<std::string> params;
2005 params.push_back(parameter);
2006 DoOneToMany(Srv->GetServerName(),"REHASH",params);
2008 if (Srv->MatchText(Srv->GetServerName(),parameter))
2010 Srv->SendOpers("*** Remote rehash initiated from server \002"+Srv->GetServerName()+"\002.");
2011 Srv->RehashServer();
2014 ReadConfiguration(false);
2017 // note: the protocol does not allow direct umode +o except
2018 // via NICK with 8 params. sending OPERTYPE infers +o modechange
2020 virtual void OnOper(userrec* user, std::string opertype)
2022 if (std::string(user->server) == Srv->GetServerName())
2024 std::deque<std::string> params;
2025 params.push_back(opertype);
2026 DoOneToMany(user->nick,"OPERTYPE",params);
2030 virtual void OnMode(userrec* user, void* dest, int target_type, std::string text)
2032 if (std::string(user->server) == Srv->GetServerName())
2034 if (target_type == TYPE_USER)
2036 userrec* u = (userrec*)dest;
2037 std::deque<std::string> params;
2038 params.push_back(u->nick);
2039 params.push_back(text);
2040 DoOneToMany(user->nick,"MODE",params);
2044 chanrec* c = (chanrec*)dest;
2045 std::deque<std::string> params;
2046 params.push_back(c->name);
2047 params.push_back(text);
2048 DoOneToMany(user->nick,"MODE",params);
2053 virtual void ProtoSendMode(void* opaque, int target_type, void* target, std::string modeline)
2055 TreeSocket* s = (TreeSocket*)opaque;
2058 if (target_type == TYPE_USER)
2060 userrec* u = (userrec*)target;
2061 s->WriteLine(":"+Srv->GetServerName()+" FMODE "+u->nick+" "+modeline);
2065 chanrec* c = (chanrec*)target;
2066 s->WriteLine(":"+Srv->GetServerName()+" FMODE "+c->name+" "+modeline);
2071 virtual ~ModuleSpanningTree()
2076 virtual Version GetVersion()
2078 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
2083 class ModuleSpanningTreeFactory : public ModuleFactory
2086 ModuleSpanningTreeFactory()
2090 ~ModuleSpanningTreeFactory()
2094 virtual Module * CreateModule()
2096 TreeProtocolModule = new ModuleSpanningTree;
2097 return TreeProtocolModule;
2103 extern "C" void * init_module( void )
2105 return new ModuleSpanningTreeFactory;