1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2005 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
23 #include "inspircd_config.h"
25 #include <ext/hash_map>
33 #include "helperfuncs.h"
35 #include "inspstring.h"
40 #define nspace __gnu_cxx
45 class ModuleSpanningTree;
46 static ModuleSpanningTree* TreeProtocolModule;
48 extern std::vector<Module*> modules;
49 extern std::vector<ircd_module*> factory;
52 enum ServerState { LISTENER, CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED };
54 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, irc::StrHashComp> user_hash;
55 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, irc::StrHashComp> chan_hash;
57 extern user_hash clientlist;
58 extern chan_hash chanlist;
63 bool DoOneToOne(std::string prefix, std::string command, std::deque<std::string> params, std::string target);
64 bool DoOneToAllButSender(std::string prefix, std::string command, std::deque<std::string> params, std::string omit);
65 bool DoOneToMany(std::string prefix, std::string command, std::deque<std::string> params);
66 bool DoOneToAllButSenderRaw(std::string data,std::string omit, std::string prefix,std::string command,std::deque<std::string> params);
67 void ReadConfiguration(bool rebind);
72 std::vector<TreeServer*> Children;
73 std::string ServerName;
74 std::string ServerDesc;
75 std::string VersionString;
78 TreeSocket* Socket; // for directly connected servers this points at the socket object
88 UserCount = OperCount = 0;
91 TreeServer(std::string Name, std::string Desc) : ServerName(Name), ServerDesc(Desc)
95 UserCount = OperCount = 0;
98 TreeServer(std::string Name, std::string Desc, TreeServer* Above, TreeSocket* Sock) : Parent(Above), ServerName(Name), ServerDesc(Desc), Socket(Sock)
101 UserCount = OperCount = 0;
104 std::string GetName()
106 return this->ServerName;
109 std::string GetDesc()
111 return this->ServerDesc;
114 std::string GetVersion()
116 return this->VersionString;
121 return this->UserCount;
126 return this->OperCount;
129 TreeSocket* GetSocket()
134 TreeServer* GetParent()
139 unsigned int ChildCount()
141 return Children.size();
144 TreeServer* GetChild(unsigned int n)
146 if (n < Children.size())
156 void AddChild(TreeServer* Child)
158 Children.push_back(Child);
161 bool DelChild(TreeServer* Child)
163 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
174 // removes child nodes of this node, and of that node, etc etc
177 bool stillchildren = true;
178 while (stillchildren)
180 stillchildren = false;
181 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
183 TreeServer* s = (TreeServer*)*a;
187 stillchildren = true;
201 std::string SendPass;
202 std::string RecvPass;
203 unsigned long AutoConnect;
204 time_t NextConnectTime;
207 /* $ModDesc: Povides a spanning tree server link protocol */
211 TreeServer *TreeRoot;
212 std::vector<Link> LinkBlocks;
214 TreeServer* RouteEnumerate(TreeServer* Current, std::string ServerName)
216 if (Current->GetName() == ServerName)
218 for (unsigned int q = 0; q < Current->ChildCount(); q++)
220 TreeServer* found = RouteEnumerate(Current->GetChild(q),ServerName);
229 // Returns the locally connected server we must route a
230 // message through to reach server 'ServerName'. This
231 // only applies to one-to-one and not one-to-many routing.
232 TreeServer* BestRouteTo(std::string ServerName)
234 if (ServerName.c_str() == TreeRoot->GetName())
238 // first, find the server by recursively walking the tree
239 TreeServer* Found = RouteEnumerate(TreeRoot,ServerName);
240 // did we find it? If not, they did something wrong, abort.
247 // The server exists, follow its parent nodes until
248 // the parent of the current is 'TreeRoot', we know
249 // then that this is a directly-connected server.
250 while ((Found) && (Found->GetParent() != TreeRoot))
252 Found = Found->GetParent();
258 bool LookForServer(TreeServer* Current, std::string ServerName)
260 if (ServerName == Current->GetName())
262 for (unsigned int q = 0; q < Current->ChildCount(); q++)
264 if (LookForServer(Current->GetChild(q),ServerName))
272 void RFindServer(TreeServer* Current, std::string ServerName)
274 if ((ServerName == Current->GetName()) && (!Found))
281 for (unsigned int q = 0; q < Current->ChildCount(); q++)
284 RFindServer(Current->GetChild(q),ServerName);
290 TreeServer* FindServer(std::string ServerName)
293 RFindServer(TreeRoot,ServerName);
297 bool IsServer(std::string ServerName)
299 return LookForServer(TreeRoot,ServerName);
302 class TreeSocket : public InspSocket
305 std::string in_buffer;
306 ServerState LinkState;
307 std::string InboundServerName;
308 std::string InboundDescription;
310 int num_lost_servers;
312 bool LastPingWasGood;
316 TreeSocket(std::string host, int port, bool listening, unsigned long maxtime)
317 : InspSocket(host, port, listening, maxtime)
320 this->LinkState = LISTENER;
323 TreeSocket(std::string host, int port, bool listening, unsigned long maxtime, std::string ServerName)
324 : InspSocket(host, port, listening, maxtime)
327 this->LinkState = CONNECTING;
330 TreeSocket(int newfd, char* ip)
331 : InspSocket(newfd, ip)
333 this->LinkState = WAIT_AUTH_1;
336 virtual bool OnConnected()
338 if (this->LinkState == CONNECTING)
340 Srv->SendOpers("*** Connection to "+myhost+"["+this->GetIP()+"] established.");
341 // we should send our details here.
342 // if the other side is satisfied, they send theirs.
343 // we do not need to change state here.
344 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
346 if (x->Name == this->myhost)
348 // found who we're supposed to be connecting to, send the neccessary gubbins.
349 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
357 void SetNextPingTime(time_t t)
360 LastPingWasGood = false;
363 time_t NextPingTime()
365 return this->NextPing;
368 bool AnsweredLastPing()
370 return LastPingWasGood;
375 LastPingWasGood = true;
378 virtual void OnError(InspSocketError e)
382 virtual int OnDisconnect()
387 // recursively send the server tree with distances as hops
388 void SendServers(TreeServer* Current, TreeServer* s, int hops)
391 for (unsigned int q = 0; q < Current->ChildCount(); q++)
393 TreeServer* recursive_server = Current->GetChild(q);
394 if (recursive_server != s)
396 // :source.server SERVER server.name hops :Description
397 snprintf(command,1024,":%s SERVER %s * %d :%s",Current->GetName().c_str(),recursive_server->GetName().c_str(),hops,recursive_server->GetDesc().c_str());
398 this->WriteLine(command);
399 // down to next level
400 this->SendServers(recursive_server, s, hops+1);
405 void SquitServer(TreeServer* Current)
407 // recursively squit the servers attached to 'Current'
408 for (unsigned int q = 0; q < Current->ChildCount(); q++)
410 TreeServer* recursive_server = Current->GetChild(q);
411 this->SquitServer(recursive_server);
413 // Now we've whacked the kids, whack self
415 bool quittingpeople = true;
416 while (quittingpeople)
418 quittingpeople = false;
419 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
421 if (!strcasecmp(u->second->server,Current->GetName().c_str()))
423 Srv->QuitUser(u->second,Current->GetName()+" "+std::string(Srv->GetServerName()));
425 quittingpeople = true;
432 void Squit(TreeServer* Current,std::string reason)
436 std::deque<std::string> params;
437 params.push_back(Current->GetName());
438 params.push_back(":"+reason);
439 DoOneToAllButSender(Current->GetParent()->GetName(),"SQUIT",params,Current->GetName());
440 if (Current->GetParent() == TreeRoot)
442 Srv->SendOpers("Server \002"+Current->GetName()+"\002 split: "+reason);
446 Srv->SendOpers("Server \002"+Current->GetName()+"\002 split from server \002"+Current->GetParent()->GetName()+"\002 with reason: "+reason);
448 num_lost_servers = 0;
450 SquitServer(Current);
452 Current->GetParent()->DelChild(Current);
454 WriteOpers("Netsplit complete, lost \002%d\002 users on \002%d\002 servers.", num_lost_users, num_lost_servers);
458 log(DEFAULT,"Squit from unknown server");
462 bool ForceMode(std::string source, std::deque<std::string> params)
464 userrec* who = new userrec;
465 who->fd = FD_MAGIC_NUMBER;
466 if (params.size() < 2)
469 for (unsigned int q = 0; q < params.size(); q++)
471 modelist[q] = (char*)params[q].c_str();
473 Srv->SendMode(modelist,params.size(),who);
474 DoOneToAllButSender(source,"FMODE",params,source);
479 bool ForceTopic(std::string source, std::deque<std::string> params)
481 // FTOPIC %s %lu %s :%s
482 if (params.size() != 4)
484 std::string channel = params[0];
485 time_t ts = atoi(params[1].c_str());
486 std::string setby = params[2];
487 std::string topic = params[3];
489 chanrec* c = Srv->FindChannel(channel);
492 if ((ts >= c->topicset) || (!*c->topic))
494 strlcpy(c->topic,topic.c_str(),MAXTOPIC);
495 strlcpy(c->setby,setby.c_str(),NICKMAX);
497 WriteChannelWithServ((char*)source.c_str(), c, "TOPIC %s :%s", c->name, c->topic);
502 // all done, send it on its way
503 params[3] = ":" + params[3];
504 DoOneToAllButSender(source,"FTOPIC",params,source);
509 bool ForceJoin(std::string source, std::deque<std::string> params)
511 if (params.size() < 3)
515 char modestring[MAXBUF];
516 char* mode_users[127];
517 mode_users[0] = first;
518 mode_users[1] = modestring;
519 strcpy(mode_users[1],"+");
520 unsigned int modectr = 2;
523 std::string channel = params[0];
524 time_t TS = atoi(params[1].c_str());
527 chanrec* chan = Srv->FindChannel(channel);
532 strlcpy(mode_users[0],channel.c_str(),MAXBUF);
534 // default is a high value, which if we dont have this
535 // channel will let the other side apply their modes.
536 time_t ourTS = time(NULL)+20;
537 chanrec* us = Srv->FindChannel(channel);
543 log(DEBUG,"FJOIN detected, our TS=%lu, their TS=%lu",ourTS,TS);
545 // do this first, so our mode reversals are correctly received by other servers
546 // if there is a TS collision.
547 DoOneToAllButSender(source,"FJOIN",params,source);
549 for (unsigned int usernum = 2; usernum < params.size(); usernum++)
551 // process one channel at a time, applying modes.
552 char* usr = (char*)params[usernum].c_str();
553 char permissions = *usr;
558 mode_users[modectr++] = usr;
559 strlcat(modestring,"o",MAXBUF);
563 mode_users[modectr++] = usr;
564 strlcat(modestring,"h",MAXBUF);
568 mode_users[modectr++] = usr;
569 strlcat(modestring,"v",MAXBUF);
572 who = Srv->FindNick(usr);
575 Srv->JoinUserToChannel(who,channel,key);
576 if (modectr >= (MAXMODES-1))
578 // theres a mode for this user. push them onto the mode queue, and flush it
579 // if there are more than MAXMODES to go.
582 log(DEBUG,"Our our channel newer than theirs, accepting their modes");
583 Srv->SendMode(mode_users,modectr,who);
587 log(DEBUG,"Their channel newer than ours, bouncing their modes");
589 std::deque<std::string> params;
590 // modes are now being UNSET...
591 *mode_users[1] = '-';
592 for (unsigned int x = 0; x < modectr; x++)
594 params.push_back(mode_users[x]);
596 // tell everyone to bounce the modes. bad modes, bad!
597 DoOneToMany(Srv->GetServerName(),"FMODE",params);
599 strcpy(mode_users[1],"+");
604 // there werent enough modes built up to flush it during FJOIN,
605 // or, there are a number left over. flush them out.
606 if ((modectr > 2) && (who))
610 log(DEBUG,"Our our channel newer than theirs, accepting their modes");
611 Srv->SendMode(mode_users,modectr,who);
615 log(DEBUG,"Their channel newer than ours, bouncing their modes");
616 std::deque<std::string> params;
617 *mode_users[1] = '-';
618 for (unsigned int x = 0; x < modectr; x++)
620 params.push_back(mode_users[x]);
622 DoOneToMany(Srv->GetServerName(),"FMODE",params);
628 bool IntroduceClient(std::string source, std::deque<std::string> params)
630 if (params.size() < 8)
632 // NICK age nick host dhost ident +modes ip :gecos
634 std::string nick = params[1];
635 std::string host = params[2];
636 std::string dhost = params[3];
637 std::string ident = params[4];
638 time_t age = atoi(params[0].c_str());
639 std::string modes = params[5];
640 while (*(modes.c_str()) == '+')
642 char* m = (char*)modes.c_str();
646 std::string ip = params[6];
647 std::string gecos = params[7];
648 char* tempnick = (char*)nick.c_str();
649 log(DEBUG,"Introduce client %s!%s@%s",tempnick,ident.c_str(),host.c_str());
651 user_hash::iterator iter;
652 iter = clientlist.find(tempnick);
653 if (iter != clientlist.end())
656 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);
657 this->WriteLine(":"+Srv->GetServerName()+" KILL "+tempnick+" :Nickname collision");
661 clientlist[tempnick] = new userrec();
662 clientlist[tempnick]->fd = FD_MAGIC_NUMBER;
663 strlcpy(clientlist[tempnick]->nick, tempnick,NICKMAX);
664 strlcpy(clientlist[tempnick]->host, host.c_str(),160);
665 strlcpy(clientlist[tempnick]->dhost, dhost.c_str(),160);
666 clientlist[tempnick]->server = (char*)FindServerNamePtr(source.c_str());
667 strlcpy(clientlist[tempnick]->ident, ident.c_str(),IDENTMAX);
668 strlcpy(clientlist[tempnick]->fullname, gecos.c_str(),MAXGECOS);
669 clientlist[tempnick]->registered = 7;
670 clientlist[tempnick]->signon = age;
671 strlcpy(clientlist[tempnick]->modes, modes.c_str(),53);
672 strlcpy(clientlist[tempnick]->ip,ip.c_str(),16);
673 for (int i = 0; i < MAXCHANS; i++)
675 clientlist[tempnick]->chans[i].channel = NULL;
676 clientlist[tempnick]->chans[i].uc_modes = 0;
678 params[7] = ":" + params[7];
679 DoOneToAllButSender(source,"NICK",params,source);
683 void SendFJoins(TreeServer* Current, chanrec* c)
686 snprintf(list,MAXBUF,":%s FJOIN %s %lu",Srv->GetServerName().c_str(),c->name,(unsigned long)c->age);
687 std::vector<char*> *ulist = c->GetUsers();
688 for (unsigned int i = 0; i < ulist->size(); i++)
690 char* o = (*ulist)[i];
691 userrec* otheruser = (userrec*)o;
692 strlcat(list," ",MAXBUF);
693 strlcat(list,cmode(otheruser,c),MAXBUF);
694 strlcat(list,otheruser->nick,MAXBUF);
695 if (strlen(list)>(480-NICKMAX))
697 this->WriteLine(list);
698 snprintf(list,MAXBUF,":%s FJOIN %s %lu",Srv->GetServerName().c_str(),c->name,(unsigned long)c->age);
701 if (list[strlen(list)-1] != ':')
703 this->WriteLine(list);
707 void SendChannelModes(TreeServer* Current)
710 for (chan_hash::iterator c = chanlist.begin(); c != chanlist.end(); c++)
712 SendFJoins(Current, c->second);
713 snprintf(data,MAXBUF,":%s FMODE %s +%s",Srv->GetServerName().c_str(),c->second->name,chanmodes(c->second));
714 this->WriteLine(data);
715 if (*c->second->topic)
717 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);
718 this->WriteLine(data);
720 for (BanList::iterator b = c->second->bans.begin(); b != c->second->bans.end(); b++)
722 snprintf(data,MAXBUF,":%s FMODE %s +b %s",Srv->GetServerName().c_str(),c->second->name,b->data);
723 this->WriteLine(data);
725 FOREACH_MOD OnSyncChannel(c->second,(Module*)TreeProtocolModule,(void*)this);
729 // send all users and their channels
730 void SendUsers(TreeServer* Current)
733 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
735 if (u->second->registered == 7)
737 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);
738 this->WriteLine(data);
739 if (strchr(u->second->modes,'o'))
741 this->WriteLine(":"+std::string(u->second->nick)+" OPERTYPE "+std::string(u->second->oper));
743 //char* chl = chlist(u->second,u->second);
746 // this->WriteLine(":"+std::string(u->second->nick)+" FJOIN "+std::string(chl));
748 FOREACH_MOD OnSyncUser(u->second,(Module*)TreeProtocolModule,(void*)this);
753 void DoBurst(TreeServer* s)
755 Srv->SendOpers("*** Bursting to "+s->GetName()+".");
756 this->WriteLine("BURST");
758 this->SendServers(TreeRoot,s,1);
759 // Send users and their channels
761 // Send everything else (channel modes etc)
762 this->SendChannelModes(s);
763 this->WriteLine("ENDBURST");
766 virtual bool OnDataReady()
768 char* data = this->Read();
771 this->in_buffer += data;
772 while (in_buffer.find("\n") != std::string::npos)
774 char* line = (char*)in_buffer.c_str();
775 std::string ret = "";
776 while ((*line != '\n') && (strlen(line)))
781 if ((*line == '\n') || (*line == '\r'))
784 if (!this->ProcessLine(ret))
790 return (data != NULL);
793 int WriteLine(std::string line)
795 return this->Write(line + "\r\n");
798 bool Error(std::deque<std::string> params)
800 if (params.size() < 1)
802 std::string Errmsg = params[0];
803 std::string SName = myhost;
804 if (InboundServerName != "")
806 SName = InboundServerName;
808 Srv->SendOpers("*** ERROR from "+SName+": "+Errmsg);
809 // we will return false to cause the socket to close.
813 bool OperType(std::string prefix, std::deque<std::string> params)
815 if (params.size() != 1)
817 std::string opertype = params[0];
818 userrec* u = Srv->FindNick(prefix);
821 strlcpy(u->oper,opertype.c_str(),NICKMAX);
822 if (!strchr(u->modes,'o'))
824 strcat(u->modes,"o");
826 DoOneToAllButSender(u->server,"OPERTYPE",params,u->server);
831 bool RemoteRehash(std::string prefix, std::deque<std::string> params)
833 if (params.size() < 1)
835 std::string servermask = params[0];
836 if (Srv->MatchText(Srv->GetServerName(),servermask))
838 Srv->SendOpers("*** Remote rehash initiated from server \002"+prefix+"\002.");
840 ReadConfiguration(false);
842 DoOneToAllButSender(prefix,"REHASH",params,prefix);
846 bool RemoteKill(std::string prefix, std::deque<std::string> params)
848 if (params.size() != 2)
850 std::string nick = params[0];
851 std::string reason = params[1];
852 userrec* u = Srv->FindNick(prefix);
853 userrec* who = Srv->FindNick(nick);
856 std::string sourceserv = prefix;
859 sourceserv = u->server;
861 params[1] = ":" + params[1];
862 DoOneToAllButSender(prefix,"KILL",params,sourceserv);
863 Srv->QuitUser(who,reason);
868 bool LocalPong(std::string prefix, std::deque<std::string> params)
870 if (params.size() < 1)
872 TreeServer* ServerSource = FindServer(prefix);
875 ServerSource->SetPingFlag();
880 bool LocalPing(std::string prefix, std::deque<std::string> params)
882 if (params.size() < 1)
884 std::string stufftobounce = params[0];
885 this->WriteLine(":"+Srv->GetServerName()+" PONG "+stufftobounce);
889 bool RemoteServer(std::string prefix, std::deque<std::string> params)
891 if (params.size() < 4)
893 std::string servername = params[0];
894 std::string password = params[1];
895 // hopcount is not used for a remote server, we calculate this ourselves
896 std::string description = params[3];
897 TreeServer* ParentOfThis = FindServer(prefix);
900 this->WriteLine("ERROR :Protocol error - Introduced remote server from unknown server "+prefix);
903 TreeServer* CheckDupe = FindServer(servername);
906 this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
909 TreeServer* Node = new TreeServer(servername,description,ParentOfThis,NULL);
910 ParentOfThis->AddChild(Node);
911 params[3] = ":" + params[3];
912 DoOneToAllButSender(prefix,"SERVER",params,prefix);
913 Srv->SendOpers("*** Server \002"+prefix+"\002 introduced server \002"+servername+"\002 ("+description+")");
917 bool Outbound_Reply_Server(std::deque<std::string> params)
919 if (params.size() < 4)
921 std::string servername = params[0];
922 std::string password = params[1];
923 int hops = atoi(params[2].c_str());
926 this->WriteLine("ERROR :Server too far away for authentication");
929 std::string description = params[3];
930 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
932 if ((x->Name == servername) && (x->RecvPass == password))
934 TreeServer* CheckDupe = FindServer(servername);
937 this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
940 // Begin the sync here. this kickstarts the
941 // other side, waiting in WAIT_AUTH_2 state,
942 // into starting their burst, as it shows
944 this->LinkState = CONNECTED;
945 // we should add the details of this server now
946 // to the servers tree, as a child of the root
948 TreeServer* Node = new TreeServer(servername,description,TreeRoot,this);
949 TreeRoot->AddChild(Node);
950 params[3] = ":" + params[3];
951 DoOneToAllButSender(TreeRoot->GetName(),"SERVER",params,servername);
956 this->WriteLine("ERROR :Invalid credentials");
960 bool Inbound_Server(std::deque<std::string> params)
962 if (params.size() < 4)
964 std::string servername = params[0];
965 std::string password = params[1];
966 int hops = atoi(params[2].c_str());
969 this->WriteLine("ERROR :Server too far away for authentication");
972 std::string description = params[3];
973 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
975 if ((x->Name == servername) && (x->RecvPass == password))
977 TreeServer* CheckDupe = FindServer(servername);
980 this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
983 Srv->SendOpers("*** Verified incoming server connection from \002"+servername+"\002["+this->GetIP()+"] ("+description+")");
984 this->InboundServerName = servername;
985 this->InboundDescription = description;
986 // this is good. Send our details: Our server name and description and hopcount of 0,
987 // along with the sendpass from this block.
988 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
989 // move to the next state, we are now waiting for THEM.
990 this->LinkState = WAIT_AUTH_2;
994 this->WriteLine("ERROR :Invalid credentials");
998 std::deque<std::string> Split(std::string line, bool stripcolon)
1000 std::deque<std::string> n;
1001 if (!strchr(line.c_str(),' '))
1006 std::stringstream s(line);
1007 std::string param = "";
1026 if ((param == ":") && (item > 0))
1049 bool ProcessLine(std::string line)
1051 char* l = (char*)line.c_str();
1052 while ((strlen(l)) && (l[strlen(l)-1] == '\r') || (l[strlen(l)-1] == '\n'))
1053 l[strlen(l)-1] = '\0';
1057 Srv->Log(DEBUG,"IN: '"+line+"'");
1058 std::deque<std::string> params = this->Split(line,true);
1059 std::string command = "";
1060 std::string prefix = "";
1061 if (((params[0].c_str())[0] == ':') && (params.size() > 1))
1064 command = params[1];
1065 char* pref = (char*)prefix.c_str();
1073 command = params[0];
1077 switch (this->LinkState)
1082 // Waiting for SERVER command from remote server. Server initiating
1083 // the connection sends the first SERVER command, listening server
1084 // replies with theirs if its happy, then if the initiator is happy,
1085 // it starts to send its net sync, which starts the merge, otherwise
1086 // it sends an ERROR.
1087 if (command == "SERVER")
1089 return this->Inbound_Server(params);
1091 else if (command == "ERROR")
1093 return this->Error(params);
1097 // Waiting for start of other side's netmerge to say they liked our
1099 if (command == "SERVER")
1101 // cant do this, they sent it to us in the WAIT_AUTH_1 state!
1105 else if (command == "BURST")
1107 this->LinkState = CONNECTED;
1108 Node = new TreeServer(InboundServerName,InboundDescription,TreeRoot,this);
1109 TreeRoot->AddChild(Node);
1111 params.push_back(InboundServerName);
1112 params.push_back("*");
1113 params.push_back("1");
1114 params.push_back(":"+InboundDescription);
1115 DoOneToAllButSender(TreeRoot->GetName(),"SERVER",params,InboundServerName);
1116 this->DoBurst(Node);
1118 else if (command == "ERROR")
1120 return this->Error(params);
1125 this->WriteLine("ERROR :Internal error -- listening socket accepted its own descriptor!!!");
1129 if (command == "SERVER")
1131 // another server we connected to, which was in WAIT_AUTH_1 state,
1132 // has just sent us their credentials. If we get this far, theyre
1133 // happy with OUR credentials, and they are now in WAIT_AUTH_2 state.
1134 // if we're happy with this, we should send our netburst which
1135 // kickstarts the merge.
1136 return this->Outbound_Reply_Server(params);
1138 else if (command == "ERROR")
1140 return this->Error(params);
1144 // This is the 'authenticated' state, when all passwords
1145 // have been exchanged and anything past this point is taken
1147 std::string target = "";
1148 if ((command == "NICK") && (params.size() > 1))
1150 return this->IntroduceClient(prefix,params);
1152 else if (command == "FJOIN")
1154 return this->ForceJoin(prefix,params);
1156 else if (command == "SERVER")
1158 return this->RemoteServer(prefix,params);
1160 else if (command == "ERROR")
1162 return this->Error(params);
1164 else if (command == "OPERTYPE")
1166 return this->OperType(prefix,params);
1168 else if (command == "FMODE")
1170 return this->ForceMode(prefix,params);
1172 else if (command == "KILL")
1174 return this->RemoteKill(prefix,params);
1176 else if (command == "FTOPIC")
1178 return this->ForceTopic(prefix,params);
1180 else if (command == "REHASH")
1182 return this->RemoteRehash(prefix,params);
1184 else if (command == "PING")
1186 return this->LocalPing(prefix,params);
1188 else if (command == "PONG")
1190 return this->LocalPong(prefix,params);
1192 else if (command == "SQUIT")
1194 if (params.size() == 2)
1196 this->Squit(FindServer(params[0]),params[1]);
1202 // not a special inter-server command.
1203 // Emulate the actual user doing the command,
1204 // this saves us having a huge ugly parser.
1205 userrec* who = Srv->FindNick(prefix);
1206 std::string sourceserv = this->myhost;
1207 if (this->InboundServerName != "")
1209 sourceserv = this->InboundServerName;
1214 target = who->server;
1215 char* strparams[127];
1216 for (unsigned int q = 0; q < params.size(); q++)
1218 strparams[q] = (char*)params[q].c_str();
1220 Srv->CallCommandHandler(command, strparams, params.size(), who);
1224 // its not a user. Its either a server, or somethings screwed up.
1225 if (IsServer(prefix))
1227 target = Srv->GetServerName();
1231 log(DEBUG,"Command with unknown origin '%s'",prefix.c_str());
1235 return DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1244 virtual std::string GetName()
1246 std::string sourceserv = this->myhost;
1247 if (this->InboundServerName != "")
1249 sourceserv = this->InboundServerName;
1254 virtual void OnTimeout()
1256 if (this->LinkState == CONNECTING)
1258 Srv->SendOpers("*** CONNECT: Connection to \002"+myhost+"\002 timed out.");
1262 virtual void OnClose()
1264 // Connection closed.
1265 // If the connection is fully up (state CONNECTED)
1266 // then propogate a netsplit to all peers.
1267 std::string quitserver = this->myhost;
1268 if (this->InboundServerName != "")
1270 quitserver = this->InboundServerName;
1272 TreeServer* s = FindServer(quitserver);
1275 Squit(s,"Remote host closed the connection");
1279 virtual int OnIncomingConnection(int newsock, char* ip)
1281 TreeSocket* s = new TreeSocket(newsock, ip);
1287 void AddThisServer(TreeServer* server, std::deque<TreeServer*> &list)
1289 for (unsigned int c = 0; c < list.size(); c++)
1291 if (list[c] == server)
1296 list.push_back(server);
1299 // returns a list of DIRECT servernames for a specific channel
1300 std::deque<TreeServer*> GetListOfServersForChannel(chanrec* c)
1302 std::deque<TreeServer*> list;
1303 std::vector<char*> *ulist = c->GetUsers();
1304 for (unsigned int i = 0; i < ulist->size(); i++)
1306 char* o = (*ulist)[i];
1307 userrec* otheruser = (userrec*)o;
1308 if (std::string(otheruser->server) != Srv->GetServerName())
1310 TreeServer* best = BestRouteTo(otheruser->server);
1312 AddThisServer(best,list);
1318 bool DoOneToAllButSenderRaw(std::string data,std::string omit,std::string prefix,std::string command,std::deque<std::string> params)
1320 TreeServer* omitroute = BestRouteTo(omit);
1321 if ((command == "NOTICE") || (command == "PRIVMSG"))
1323 if ((params.size() >= 2) && (*(params[0].c_str()) != '$'))
1325 if (*(params[0].c_str()) != '#')
1327 // special routing for private messages/notices
1328 userrec* d = Srv->FindNick(params[0]);
1331 std::deque<std::string> par;
1333 par.push_back(params[0]);
1334 par.push_back(":"+params[1]);
1335 DoOneToOne(prefix,command,par,d->server);
1341 log(DEBUG,"Channel privmsg going to chan %s",params[0].c_str());
1342 chanrec* c = Srv->FindChannel(params[0]);
1345 std::deque<TreeServer*> list = GetListOfServersForChannel(c);
1346 log(DEBUG,"Got a list of %d servers",list.size());
1347 for (unsigned int i = 0; i < list.size(); i++)
1349 TreeSocket* Sock = list[i]->GetSocket();
1350 if ((Sock) && (list[i]->GetName() != omit) && (omitroute != list[i]))
1352 log(DEBUG,"Writing privmsg to server %s",list[i]->GetName().c_str());
1353 Sock->WriteLine(data);
1361 for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1363 TreeServer* Route = TreeRoot->GetChild(x);
1364 if ((Route->GetSocket()) && (Route->GetName() != omit) && (omitroute != Route))
1366 TreeSocket* Sock = Route->GetSocket();
1367 Sock->WriteLine(data);
1373 bool DoOneToAllButSender(std::string prefix, std::string command, std::deque<std::string> params, std::string omit)
1375 TreeServer* omitroute = BestRouteTo(omit);
1376 std::string FullLine = ":" + prefix + " " + command;
1377 for (unsigned int x = 0; x < params.size(); x++)
1379 FullLine = FullLine + " " + params[x];
1381 for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1383 TreeServer* Route = TreeRoot->GetChild(x);
1384 // Send the line IF:
1385 // The route has a socket (its a direct connection)
1386 // The route isnt the one to be omitted
1387 // The route isnt the path to the one to be omitted
1388 if ((Route->GetSocket()) && (Route->GetName() != omit) && (omitroute != Route))
1390 TreeSocket* Sock = Route->GetSocket();
1391 Sock->WriteLine(FullLine);
1397 bool DoOneToMany(std::string prefix, std::string command, std::deque<std::string> params)
1399 std::string FullLine = ":" + prefix + " " + command;
1400 for (unsigned int x = 0; x < params.size(); x++)
1402 FullLine = FullLine + " " + params[x];
1404 for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1406 TreeServer* Route = TreeRoot->GetChild(x);
1407 if (Route->GetSocket())
1409 TreeSocket* Sock = Route->GetSocket();
1410 Sock->WriteLine(FullLine);
1416 bool DoOneToOne(std::string prefix, std::string command, std::deque<std::string> params, std::string target)
1418 TreeServer* Route = BestRouteTo(target);
1421 std::string FullLine = ":" + prefix + " " + command;
1422 for (unsigned int x = 0; x < params.size(); x++)
1424 FullLine = FullLine + " " + params[x];
1426 if (Route->GetSocket())
1428 TreeSocket* Sock = Route->GetSocket();
1429 Sock->WriteLine(FullLine);
1439 std::vector<TreeSocket*> Bindings;
1441 void ReadConfiguration(bool rebind)
1445 for (int j =0; j < Conf->Enumerate("bind"); j++)
1447 std::string Type = Conf->ReadValue("bind","type",j);
1448 std::string IP = Conf->ReadValue("bind","address",j);
1449 long Port = Conf->ReadInteger("bind","port",j,true);
1450 if (Type == "servers")
1456 TreeSocket* listener = new TreeSocket(IP.c_str(),Port,true,10);
1457 if (listener->GetState() == I_LISTENING)
1459 Srv->AddSocket(listener);
1460 Bindings.push_back(listener);
1464 log(DEFAULT,"m_spanningtree: Warning: Failed to bind server port %d",Port);
1472 for (int j =0; j < Conf->Enumerate("link"); j++)
1475 L.Name = Conf->ReadValue("link","name",j);
1476 L.IPAddr = Conf->ReadValue("link","ipaddr",j);
1477 L.Port = Conf->ReadInteger("link","port",j,true);
1478 L.SendPass = Conf->ReadValue("link","sendpass",j);
1479 L.RecvPass = Conf->ReadValue("link","recvpass",j);
1480 L.AutoConnect = Conf->ReadInteger("link","autoconnect",j,true);
1481 L.NextConnectTime = time(NULL) + L.AutoConnect;
1482 LinkBlocks.push_back(L);
1483 log(DEBUG,"m_spanningtree: Read server %s with host %s:%d",L.Name.c_str(),L.IPAddr.c_str(),L.Port);
1488 class ModuleSpanningTree : public Module
1490 std::vector<TreeSocket*> Bindings;
1496 ModuleSpanningTree()
1499 Conf = new ConfigReader;
1502 // Create the root of the tree
1503 TreeRoot = new TreeServer(Srv->GetServerName(),Srv->GetServerDescription());
1505 ReadConfiguration(true);
1508 void ShowLinks(TreeServer* Current, userrec* user, int hops)
1510 std::string Parent = TreeRoot->GetName();
1511 if (Current->GetParent())
1513 Parent = Current->GetParent()->GetName();
1515 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1517 ShowLinks(Current->GetChild(q),user,hops+1);
1519 WriteServ(user->fd,"364 %s %s %s :%d %s",user->nick,Current->GetName().c_str(),Parent.c_str(),hops,Current->GetDesc().c_str());
1522 int CountLocalServs()
1524 return TreeRoot->ChildCount();
1527 void CountServsRecursive(TreeServer* Current)
1530 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1532 CountServsRecursive(Current->GetChild(q));
1539 CountServsRecursive(TreeRoot);
1543 void HandleLinks(char** parameters, int pcnt, userrec* user)
1545 ShowLinks(TreeRoot,user,0);
1546 WriteServ(user->fd,"365 %s * :End of /LINKS list.",user->nick);
1550 void HandleLusers(char** parameters, int pcnt, userrec* user)
1552 WriteServ(user->fd,"251 %s :There are %d users and %d invisible on %d servers",user->nick,usercnt()-usercount_invisible(),usercount_invisible(),this->CountServs());
1553 WriteServ(user->fd,"252 %s %d :operator(s) online",user->nick,usercount_opers());
1554 WriteServ(user->fd,"253 %s %d :unknown connections",user->nick,usercount_unknown());
1555 WriteServ(user->fd,"254 %s %d :channels formed",user->nick,chancount());
1556 WriteServ(user->fd,"254 %s :I have %d clients and %d servers",user->nick,local_count(),this->CountLocalServs());
1560 // WARNING: NOT THREAD SAFE - DONT GET ANY SMART IDEAS.
1562 void ShowMap(TreeServer* Current, userrec* user, int depth, char matrix[128][80])
1566 for (int t = 0; t < depth; t++)
1568 matrix[line][t] = ' ';
1570 strlcpy(&matrix[line][depth],Current->GetName().c_str(),80);
1572 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1574 ShowMap(Current->GetChild(q),user,depth+2,matrix);
1579 // Ok, prepare to be confused.
1580 // After much mulling over how to approach this, it struck me that
1581 // the 'usual' way of doing a /MAP isnt the best way. Instead of
1582 // keeping track of a ton of ascii characters, and line by line
1583 // under recursion working out where to place them using multiplications
1584 // and divisons, we instead render the map onto a backplane of characters
1585 // (a character matrix), then draw the branches as a series of "L" shapes
1586 // from the nodes. This is not only friendlier on CPU it uses less stack.
1588 void HandleMap(char** parameters, int pcnt, userrec* user)
1590 // This array represents a virtual screen which we will
1591 // "scratch" draw to, as the console device of an irc
1592 // client does not provide for a proper terminal.
1593 char matrix[128][80];
1594 for (unsigned int t = 0; t < 128; t++)
1596 matrix[t][0] = '\0';
1599 // The only recursive bit is called here.
1600 ShowMap(TreeRoot,user,0,matrix);
1601 // Process each line one by one. The algorithm has a limit of
1602 // 128 servers (which is far more than a spanning tree should have
1603 // anyway, so we're ok). This limit can be raised simply by making
1604 // the character matrix deeper, 128 rows taking 10k of memory.
1605 for (int l = 1; l < line; l++)
1607 // scan across the line looking for the start of the
1608 // servername (the recursive part of the algorithm has placed
1609 // the servers at indented positions depending on what they
1611 int first_nonspace = 0;
1612 while (matrix[l][first_nonspace] == ' ')
1617 // Draw the `- (corner) section: this may be overwritten by
1618 // another L shape passing along the same vertical pane, becoming
1619 // a |- (branch) section instead.
1620 matrix[l][first_nonspace] = '-';
1621 matrix[l][first_nonspace-1] = '`';
1623 // Draw upwards until we hit the parent server, causing possibly
1624 // other corners (`-) to become branches (|-)
1625 while ((matrix[l2][first_nonspace-1] == ' ') || (matrix[l2][first_nonspace-1] == '`'))
1627 matrix[l2][first_nonspace-1] = '|';
1631 // dump the whole lot to the user. This is the easy bit, honest.
1632 for (int t = 0; t < line; t++)
1634 WriteServ(user->fd,"006 %s :%s",user->nick,&matrix[t][0]);
1636 WriteServ(user->fd,"007 %s :End of /MAP",user->nick);
1640 int HandleSquit(char** parameters, int pcnt, userrec* user)
1642 TreeServer* s = FindServer(parameters[0]);
1645 TreeSocket* sock = s->GetSocket();
1648 WriteOpers("*** SQUIT: Server \002%s\002 removed from network by %s",parameters[0],user->nick);
1649 sock->Squit(s,"Server quit by "+std::string(user->nick)+"!"+std::string(user->ident)+"@"+std::string(user->host));
1654 WriteServ(user->fd,"NOTICE %s :*** SQUIT: The server \002%s\002 is not directly connected.",user->nick,parameters[0]);
1659 WriteServ(user->fd,"NOTICE %s :*** SQUIT: The server \002%s\002 does not exist on the network.",user->nick,parameters[0]);
1664 void DoPingChecks(time_t curtime)
1666 for (unsigned int j = 0; j < TreeRoot->ChildCount(); j++)
1668 TreeServer* serv = TreeRoot->GetChild(j);
1669 TreeSocket* sock = serv->GetSocket();
1672 if (serv->NextPingTime() > curtime)
1674 if (serv->AnsweredLastPing())
1676 sock->WriteLine(":"+Srv->GetServerName()+" PING "+serv->GetName());
1677 serv->SetNextPingTime(curtime + 60);
1681 // they didnt answer, boot them
1682 WriteOpers("*** Server \002%s\002 pinged out",serv->GetName().c_str());
1683 sock->Squit(serv,"Ping timeout");
1692 void AutoConnectServers(time_t curtime)
1694 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
1696 if ((x->AutoConnect) && (curtime >= x->NextConnectTime))
1698 log(DEBUG,"Auto-Connecting %s",x->Name.c_str());
1699 x->NextConnectTime = curtime + x->AutoConnect;
1700 TreeServer* CheckDupe = FindServer(x->Name);
1703 // an autoconnected server is not connected. Check if its time to connect it
1704 WriteOpers("*** AUTOCONNECT: Auto-connecting server \002%s\002 (%lu seconds until next attempt)",x->Name.c_str(),x->AutoConnect);
1705 TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name);
1706 Srv->AddSocket(newsocket);
1712 int HandleConnect(char** parameters, int pcnt, userrec* user)
1714 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
1716 if (Srv->MatchText(x->Name.c_str(),parameters[0]))
1718 TreeServer* CheckDupe = FindServer(x->Name);
1721 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);
1722 TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name);
1723 Srv->AddSocket(newsocket);
1728 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());
1733 WriteServ(user->fd,"NOTICE %s :*** CONNECT: No server matching \002%s\002 could be found in the config file.",user->nick,parameters[0]);
1737 virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
1739 if (command == "CONNECT")
1741 return this->HandleConnect(parameters,pcnt,user);
1743 else if (command == "SQUIT")
1745 return this->HandleSquit(parameters,pcnt,user);
1747 else if (command == "MAP")
1749 this->HandleMap(parameters,pcnt,user);
1752 else if (command == "LUSERS")
1754 this->HandleLusers(parameters,pcnt,user);
1757 else if (command == "LINKS")
1759 this->HandleLinks(parameters,pcnt,user);
1762 else if (Srv->IsValidModuleCommand(command, pcnt, user))
1764 // this bit of code cleverly routes all module commands
1765 // to all remote severs *automatically* so that modules
1766 // can just handle commands locally, without having
1767 // to have any special provision in place for remote
1768 // commands and linking protocols.
1769 std::deque<std::string> params;
1771 for (int j = 0; j < pcnt; j++)
1773 if (strchr(parameters[j],' '))
1775 params.push_back(":" + std::string(parameters[j]));
1779 params.push_back(std::string(parameters[j]));
1782 DoOneToMany(user->nick,command,params);
1787 virtual void OnGetServerDescription(std::string servername,std::string &description)
1789 TreeServer* s = FindServer(servername);
1792 description = s->GetDesc();
1796 virtual void OnUserInvite(userrec* source,userrec* dest,chanrec* channel)
1798 if (std::string(source->server) == Srv->GetServerName())
1800 std::deque<std::string> params;
1801 params.push_back(dest->nick);
1802 params.push_back(channel->name);
1803 DoOneToMany(source->nick,"INVITE",params);
1807 virtual void OnPostLocalTopicChange(userrec* user, chanrec* chan, std::string topic)
1809 std::deque<std::string> params;
1810 params.push_back(chan->name);
1811 params.push_back(":"+topic);
1812 DoOneToMany(user->nick,"TOPIC",params);
1815 virtual void OnWallops(userrec* user, std::string text)
1817 if (std::string(user->server) == Srv->GetServerName())
1819 std::deque<std::string> params;
1820 params.push_back(":"+text);
1821 DoOneToMany(user->nick,"WALLOPS",params);
1825 virtual void OnUserNotice(userrec* user, void* dest, int target_type, std::string text)
1827 if (target_type == TYPE_USER)
1829 userrec* d = (userrec*)dest;
1830 if ((std::string(d->server) != Srv->GetServerName()) && (std::string(user->server) == Srv->GetServerName()))
1832 std::deque<std::string> params;
1834 params.push_back(d->nick);
1835 params.push_back(":"+text);
1836 DoOneToOne(user->nick,"NOTICE",params,d->server);
1841 if (std::string(user->server) == Srv->GetServerName())
1843 chanrec *c = (chanrec*)dest;
1844 std::deque<TreeServer*> list = GetListOfServersForChannel(c);
1845 for (unsigned int i = 0; i < list.size(); i++)
1847 TreeSocket* Sock = list[i]->GetSocket();
1849 Sock->WriteLine(":"+std::string(user->nick)+" NOTICE "+std::string(c->name)+" :"+text);
1855 virtual void OnUserMessage(userrec* user, void* dest, int target_type, std::string text)
1857 if (target_type == TYPE_USER)
1859 // route private messages which are targetted at clients only to the server
1860 // which needs to receive them
1861 userrec* d = (userrec*)dest;
1862 if ((std::string(d->server) != Srv->GetServerName()) && (std::string(user->server) == Srv->GetServerName()))
1864 std::deque<std::string> params;
1866 params.push_back(d->nick);
1867 params.push_back(":"+text);
1868 DoOneToOne(user->nick,"PRIVMSG",params,d->server);
1873 if (std::string(user->server) == Srv->GetServerName())
1875 chanrec *c = (chanrec*)dest;
1876 std::deque<TreeServer*> list = GetListOfServersForChannel(c);
1877 for (unsigned int i = 0; i < list.size(); i++)
1879 TreeSocket* Sock = list[i]->GetSocket();
1881 Sock->WriteLine(":"+std::string(user->nick)+" PRIVMSG "+std::string(c->name)+" :"+text);
1887 virtual void OnBackgroundTimer(time_t curtime)
1889 AutoConnectServers(curtime);
1890 DoPingChecks(curtime);
1893 virtual void OnUserJoin(userrec* user, chanrec* channel)
1895 // Only do this for local users
1896 if (std::string(user->server) == Srv->GetServerName())
1898 std::deque<std::string> params;
1900 params.push_back(channel->name);
1903 // if the channel has a key, force the join by emulating the key.
1904 params.push_back(channel->key);
1906 if (channel->GetUserCounter() > 1)
1908 // not the first in the channel
1909 DoOneToMany(user->nick,"JOIN",params);
1913 // first in the channel, set up their permissions
1914 // and the channel TS with FJOIN.
1916 snprintf(ts,24,"%lu",(unsigned long)channel->age);
1918 params.push_back(channel->name);
1919 params.push_back(ts);
1920 params.push_back("@"+std::string(user->nick));
1921 DoOneToMany(Srv->GetServerName(),"FJOIN",params);
1926 virtual void OnUserPart(userrec* user, chanrec* channel)
1928 if (std::string(user->server) == Srv->GetServerName())
1930 std::deque<std::string> params;
1932 params.push_back(channel->name);
1933 DoOneToMany(user->nick,"PART",params);
1937 virtual void OnUserConnect(userrec* user)
1939 char agestr[MAXBUF];
1940 if (std::string(user->server) == Srv->GetServerName())
1942 std::deque<std::string> params;
1943 snprintf(agestr,MAXBUF,"%lu",(unsigned long)user->age);
1945 params.push_back(agestr);
1946 params.push_back(user->nick);
1947 params.push_back(user->host);
1948 params.push_back(user->dhost);
1949 params.push_back(user->ident);
1950 params.push_back("+"+std::string(user->modes));
1951 params.push_back(user->ip);
1952 params.push_back(":"+std::string(user->fullname));
1953 DoOneToMany(Srv->GetServerName(),"NICK",params);
1957 virtual void OnUserQuit(userrec* user, std::string reason)
1959 if (std::string(user->server) == Srv->GetServerName())
1961 std::deque<std::string> params;
1962 params.push_back(":"+reason);
1963 DoOneToMany(user->nick,"QUIT",params);
1967 virtual void OnUserPostNick(userrec* user, std::string oldnick)
1969 if (std::string(user->server) == Srv->GetServerName())
1971 std::deque<std::string> params;
1972 params.push_back(user->nick);
1973 DoOneToMany(oldnick,"NICK",params);
1977 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, std::string reason)
1979 if (std::string(source->server) == Srv->GetServerName())
1981 std::deque<std::string> params;
1982 params.push_back(chan->name);
1983 params.push_back(user->nick);
1984 params.push_back(":"+reason);
1985 DoOneToMany(source->nick,"KICK",params);
1989 virtual void OnRemoteKill(userrec* source, userrec* dest, std::string reason)
1991 std::deque<std::string> params;
1992 params.push_back(dest->nick);
1993 params.push_back(":"+reason);
1994 DoOneToMany(source->nick,"KILL",params);
1997 virtual void OnRehash(std::string parameter)
1999 if (parameter != "")
2001 std::deque<std::string> params;
2002 params.push_back(parameter);
2003 DoOneToMany(Srv->GetServerName(),"REHASH",params);
2005 if (Srv->MatchText(Srv->GetServerName(),parameter))
2007 Srv->SendOpers("*** Remote rehash initiated from server \002"+Srv->GetServerName()+"\002.");
2008 Srv->RehashServer();
2011 ReadConfiguration(false);
2014 // note: the protocol does not allow direct umode +o except
2015 // via NICK with 8 params. sending OPERTYPE infers +o modechange
2017 virtual void OnOper(userrec* user, std::string opertype)
2019 if (std::string(user->server) == Srv->GetServerName())
2021 std::deque<std::string> params;
2022 params.push_back(opertype);
2023 DoOneToMany(user->nick,"OPERTYPE",params);
2027 virtual void OnMode(userrec* user, void* dest, int target_type, std::string text)
2029 if (std::string(user->server) == Srv->GetServerName())
2031 if (target_type == TYPE_USER)
2033 userrec* u = (userrec*)dest;
2034 std::deque<std::string> params;
2035 params.push_back(u->nick);
2036 params.push_back(text);
2037 DoOneToMany(user->nick,"MODE",params);
2041 chanrec* c = (chanrec*)dest;
2042 std::deque<std::string> params;
2043 params.push_back(c->name);
2044 params.push_back(text);
2045 DoOneToMany(user->nick,"MODE",params);
2050 virtual void ProtoSendMode(void* opaque, int target_type, void* target, std::string modeline)
2052 TreeSocket* s = (TreeSocket*)opaque;
2055 if (target_type == TYPE_USER)
2057 userrec* u = (userrec*)target;
2058 s->WriteLine(":"+Srv->GetServerName()+" FMODE "+u->nick+" "+modeline);
2062 chanrec* c = (chanrec*)target;
2063 s->WriteLine(":"+Srv->GetServerName()+" FMODE "+c->name+" "+modeline);
2068 virtual ~ModuleSpanningTree()
2073 virtual Version GetVersion()
2075 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
2080 class ModuleSpanningTreeFactory : public ModuleFactory
2083 ModuleSpanningTreeFactory()
2087 ~ModuleSpanningTreeFactory()
2091 virtual Module * CreateModule()
2093 TreeProtocolModule = new ModuleSpanningTree;
2094 return TreeProtocolModule;
2100 extern "C" void * init_module( void )
2102 return new ModuleSpanningTreeFactory;