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;
106 std::string GetName()
108 return this->ServerName;
111 std::string GetDesc()
113 return this->ServerDesc;
116 std::string GetVersion()
118 return this->VersionString;
121 void SetNextPingTime(time_t t)
124 LastPingWasGood = false;
127 time_t NextPingTime()
129 return this->NextPing;
132 bool AnsweredLastPing()
134 return LastPingWasGood;
139 LastPingWasGood = true;
144 return this->UserCount;
149 return this->OperCount;
152 TreeSocket* GetSocket()
157 TreeServer* GetParent()
162 unsigned int ChildCount()
164 return Children.size();
167 TreeServer* GetChild(unsigned int n)
169 if (n < Children.size())
179 void AddChild(TreeServer* Child)
181 Children.push_back(Child);
184 bool DelChild(TreeServer* Child)
186 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
197 // removes child nodes of this node, and of that node, etc etc
200 bool stillchildren = true;
201 while (stillchildren)
203 stillchildren = false;
204 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
206 TreeServer* s = (TreeServer*)*a;
210 stillchildren = true;
224 std::string SendPass;
225 std::string RecvPass;
226 unsigned long AutoConnect;
227 time_t NextConnectTime;
230 /* $ModDesc: Povides a spanning tree server link protocol */
234 TreeServer *TreeRoot;
235 std::vector<Link> LinkBlocks;
237 TreeServer* RouteEnumerate(TreeServer* Current, std::string ServerName)
239 if (Current->GetName() == ServerName)
241 for (unsigned int q = 0; q < Current->ChildCount(); q++)
243 TreeServer* found = RouteEnumerate(Current->GetChild(q),ServerName);
252 // Returns the locally connected server we must route a
253 // message through to reach server 'ServerName'. This
254 // only applies to one-to-one and not one-to-many routing.
255 TreeServer* BestRouteTo(std::string ServerName)
257 if (ServerName.c_str() == TreeRoot->GetName())
261 // first, find the server by recursively walking the tree
262 TreeServer* Found = RouteEnumerate(TreeRoot,ServerName);
263 // did we find it? If not, they did something wrong, abort.
270 // The server exists, follow its parent nodes until
271 // the parent of the current is 'TreeRoot', we know
272 // then that this is a directly-connected server.
273 while ((Found) && (Found->GetParent() != TreeRoot))
275 Found = Found->GetParent();
281 bool LookForServer(TreeServer* Current, std::string ServerName)
283 if (ServerName == Current->GetName())
285 for (unsigned int q = 0; q < Current->ChildCount(); q++)
287 if (LookForServer(Current->GetChild(q),ServerName))
295 void RFindServer(TreeServer* Current, std::string ServerName)
297 if ((ServerName == Current->GetName()) && (!Found))
304 for (unsigned int q = 0; q < Current->ChildCount(); q++)
307 RFindServer(Current->GetChild(q),ServerName);
313 TreeServer* FindServer(std::string ServerName)
316 RFindServer(TreeRoot,ServerName);
320 bool IsServer(std::string ServerName)
322 return LookForServer(TreeRoot,ServerName);
325 class TreeSocket : public InspSocket
328 std::string in_buffer;
329 ServerState LinkState;
330 std::string InboundServerName;
331 std::string InboundDescription;
333 int num_lost_servers;
335 bool LastPingWasGood;
339 TreeSocket(std::string host, int port, bool listening, unsigned long maxtime)
340 : InspSocket(host, port, listening, maxtime)
343 this->LinkState = LISTENER;
346 TreeSocket(std::string host, int port, bool listening, unsigned long maxtime, std::string ServerName)
347 : InspSocket(host, port, listening, maxtime)
350 this->LinkState = CONNECTING;
353 TreeSocket(int newfd, char* ip)
354 : InspSocket(newfd, ip)
356 this->LinkState = WAIT_AUTH_1;
359 virtual bool OnConnected()
361 if (this->LinkState == CONNECTING)
363 Srv->SendOpers("*** Connection to "+myhost+"["+this->GetIP()+"] established.");
364 // we should send our details here.
365 // if the other side is satisfied, they send theirs.
366 // we do not need to change state here.
367 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
369 if (x->Name == this->myhost)
371 // found who we're supposed to be connecting to, send the neccessary gubbins.
372 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
380 virtual void OnError(InspSocketError e)
384 virtual int OnDisconnect()
389 // recursively send the server tree with distances as hops
390 void SendServers(TreeServer* Current, TreeServer* s, int hops)
393 for (unsigned int q = 0; q < Current->ChildCount(); q++)
395 TreeServer* recursive_server = Current->GetChild(q);
396 if (recursive_server != s)
398 // :source.server SERVER server.name hops :Description
399 snprintf(command,1024,":%s SERVER %s * %d :%s",Current->GetName().c_str(),recursive_server->GetName().c_str(),hops,recursive_server->GetDesc().c_str());
400 this->WriteLine(command);
401 // down to next level
402 this->SendServers(recursive_server, s, hops+1);
407 void SquitServer(TreeServer* Current)
409 // recursively squit the servers attached to 'Current'
410 for (unsigned int q = 0; q < Current->ChildCount(); q++)
412 TreeServer* recursive_server = Current->GetChild(q);
413 this->SquitServer(recursive_server);
415 // Now we've whacked the kids, whack self
417 bool quittingpeople = true;
418 while (quittingpeople)
420 quittingpeople = false;
421 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
423 if (!strcasecmp(u->second->server,Current->GetName().c_str()))
425 Srv->QuitUser(u->second,Current->GetName()+" "+std::string(Srv->GetServerName()));
427 quittingpeople = true;
434 void Squit(TreeServer* Current,std::string reason)
438 std::deque<std::string> params;
439 params.push_back(Current->GetName());
440 params.push_back(":"+reason);
441 DoOneToAllButSender(Current->GetParent()->GetName(),"SQUIT",params,Current->GetName());
442 if (Current->GetParent() == TreeRoot)
444 Srv->SendOpers("Server \002"+Current->GetName()+"\002 split: "+reason);
448 Srv->SendOpers("Server \002"+Current->GetName()+"\002 split from server \002"+Current->GetParent()->GetName()+"\002 with reason: "+reason);
450 num_lost_servers = 0;
452 SquitServer(Current);
454 Current->GetParent()->DelChild(Current);
456 WriteOpers("Netsplit complete, lost \002%d\002 users on \002%d\002 servers.", num_lost_users, num_lost_servers);
460 log(DEFAULT,"Squit from unknown server");
464 bool ForceMode(std::string source, std::deque<std::string> params)
466 userrec* who = new userrec;
467 who->fd = FD_MAGIC_NUMBER;
468 if (params.size() < 2)
471 for (unsigned int q = 0; q < params.size(); q++)
473 modelist[q] = (char*)params[q].c_str();
475 Srv->SendMode(modelist,params.size(),who);
476 DoOneToAllButSender(source,"FMODE",params,source);
481 bool ForceTopic(std::string source, std::deque<std::string> params)
483 // FTOPIC %s %lu %s :%s
484 if (params.size() != 4)
486 std::string channel = params[0];
487 time_t ts = atoi(params[1].c_str());
488 std::string setby = params[2];
489 std::string topic = params[3];
491 chanrec* c = Srv->FindChannel(channel);
494 if ((ts >= c->topicset) || (!*c->topic))
496 strlcpy(c->topic,topic.c_str(),MAXTOPIC);
497 strlcpy(c->setby,setby.c_str(),NICKMAX);
499 WriteChannelWithServ((char*)source.c_str(), c, "TOPIC %s :%s", c->name, c->topic);
504 // all done, send it on its way
505 params[3] = ":" + params[3];
506 DoOneToAllButSender(source,"FTOPIC",params,source);
511 bool ForceJoin(std::string source, std::deque<std::string> params)
513 if (params.size() < 3)
517 char modestring[MAXBUF];
518 char* mode_users[127];
519 mode_users[0] = first;
520 mode_users[1] = modestring;
521 strcpy(mode_users[1],"+");
522 unsigned int modectr = 2;
525 std::string channel = params[0];
526 time_t TS = atoi(params[1].c_str());
529 chanrec* chan = Srv->FindChannel(channel);
534 strlcpy(mode_users[0],channel.c_str(),MAXBUF);
536 // default is a high value, which if we dont have this
537 // channel will let the other side apply their modes.
538 time_t ourTS = time(NULL)+20;
539 chanrec* us = Srv->FindChannel(channel);
545 log(DEBUG,"FJOIN detected, our TS=%lu, their TS=%lu",ourTS,TS);
547 // do this first, so our mode reversals are correctly received by other servers
548 // if there is a TS collision.
549 DoOneToAllButSender(source,"FJOIN",params,source);
551 for (unsigned int usernum = 2; usernum < params.size(); usernum++)
553 // process one channel at a time, applying modes.
554 char* usr = (char*)params[usernum].c_str();
555 char permissions = *usr;
560 mode_users[modectr++] = usr;
561 strlcat(modestring,"o",MAXBUF);
565 mode_users[modectr++] = usr;
566 strlcat(modestring,"h",MAXBUF);
570 mode_users[modectr++] = usr;
571 strlcat(modestring,"v",MAXBUF);
574 who = Srv->FindNick(usr);
577 Srv->JoinUserToChannel(who,channel,key);
578 if (modectr >= (MAXMODES-1))
580 // theres a mode for this user. push them onto the mode queue, and flush it
581 // if there are more than MAXMODES to go.
584 log(DEBUG,"Our our channel newer than theirs, accepting their modes");
585 Srv->SendMode(mode_users,modectr,who);
589 log(DEBUG,"Their channel newer than ours, bouncing their modes");
591 std::deque<std::string> params;
592 // modes are now being UNSET...
593 *mode_users[1] = '-';
594 for (unsigned int x = 0; x < modectr; x++)
596 params.push_back(mode_users[x]);
598 // tell everyone to bounce the modes. bad modes, bad!
599 DoOneToMany(Srv->GetServerName(),"FMODE",params);
601 strcpy(mode_users[1],"+");
606 // there werent enough modes built up to flush it during FJOIN,
607 // or, there are a number left over. flush them out.
608 if ((modectr > 2) && (who))
612 log(DEBUG,"Our our channel newer than theirs, accepting their modes");
613 Srv->SendMode(mode_users,modectr,who);
617 log(DEBUG,"Their channel newer than ours, bouncing their modes");
618 std::deque<std::string> params;
619 *mode_users[1] = '-';
620 for (unsigned int x = 0; x < modectr; x++)
622 params.push_back(mode_users[x]);
624 DoOneToMany(Srv->GetServerName(),"FMODE",params);
630 bool IntroduceClient(std::string source, std::deque<std::string> params)
632 if (params.size() < 8)
634 // NICK age nick host dhost ident +modes ip :gecos
636 std::string nick = params[1];
637 std::string host = params[2];
638 std::string dhost = params[3];
639 std::string ident = params[4];
640 time_t age = atoi(params[0].c_str());
641 std::string modes = params[5];
642 while (*(modes.c_str()) == '+')
644 char* m = (char*)modes.c_str();
648 std::string ip = params[6];
649 std::string gecos = params[7];
650 char* tempnick = (char*)nick.c_str();
651 log(DEBUG,"Introduce client %s!%s@%s",tempnick,ident.c_str(),host.c_str());
653 user_hash::iterator iter;
654 iter = clientlist.find(tempnick);
655 if (iter != clientlist.end())
658 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);
659 this->WriteLine(":"+Srv->GetServerName()+" KILL "+tempnick+" :Nickname collision");
663 clientlist[tempnick] = new userrec();
664 clientlist[tempnick]->fd = FD_MAGIC_NUMBER;
665 strlcpy(clientlist[tempnick]->nick, tempnick,NICKMAX);
666 strlcpy(clientlist[tempnick]->host, host.c_str(),160);
667 strlcpy(clientlist[tempnick]->dhost, dhost.c_str(),160);
668 clientlist[tempnick]->server = (char*)FindServerNamePtr(source.c_str());
669 strlcpy(clientlist[tempnick]->ident, ident.c_str(),IDENTMAX);
670 strlcpy(clientlist[tempnick]->fullname, gecos.c_str(),MAXGECOS);
671 clientlist[tempnick]->registered = 7;
672 clientlist[tempnick]->signon = age;
673 strlcpy(clientlist[tempnick]->modes, modes.c_str(),53);
674 strlcpy(clientlist[tempnick]->ip,ip.c_str(),16);
675 for (int i = 0; i < MAXCHANS; i++)
677 clientlist[tempnick]->chans[i].channel = NULL;
678 clientlist[tempnick]->chans[i].uc_modes = 0;
680 params[7] = ":" + params[7];
681 DoOneToAllButSender(source,"NICK",params,source);
685 void SendFJoins(TreeServer* Current, chanrec* c)
688 snprintf(list,MAXBUF,":%s FJOIN %s %lu",Srv->GetServerName().c_str(),c->name,(unsigned long)c->age);
689 std::vector<char*> *ulist = c->GetUsers();
690 for (unsigned int i = 0; i < ulist->size(); i++)
692 char* o = (*ulist)[i];
693 userrec* otheruser = (userrec*)o;
694 strlcat(list," ",MAXBUF);
695 strlcat(list,cmode(otheruser,c),MAXBUF);
696 strlcat(list,otheruser->nick,MAXBUF);
697 if (strlen(list)>(480-NICKMAX))
699 this->WriteLine(list);
700 snprintf(list,MAXBUF,":%s FJOIN %s %lu",Srv->GetServerName().c_str(),c->name,(unsigned long)c->age);
703 if (list[strlen(list)-1] != ':')
705 this->WriteLine(list);
709 void SendChannelModes(TreeServer* Current)
712 for (chan_hash::iterator c = chanlist.begin(); c != chanlist.end(); c++)
714 SendFJoins(Current, c->second);
715 snprintf(data,MAXBUF,":%s FMODE %s +%s",Srv->GetServerName().c_str(),c->second->name,chanmodes(c->second));
716 this->WriteLine(data);
717 if (*c->second->topic)
719 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);
720 this->WriteLine(data);
722 for (BanList::iterator b = c->second->bans.begin(); b != c->second->bans.end(); b++)
724 snprintf(data,MAXBUF,":%s FMODE %s +b %s",Srv->GetServerName().c_str(),c->second->name,b->data);
725 this->WriteLine(data);
727 FOREACH_MOD OnSyncChannel(c->second,(Module*)TreeProtocolModule,(void*)this);
731 // send all users and their channels
732 void SendUsers(TreeServer* Current)
735 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
737 if (u->second->registered == 7)
739 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);
740 this->WriteLine(data);
741 if (strchr(u->second->modes,'o'))
743 this->WriteLine(":"+std::string(u->second->nick)+" OPERTYPE "+std::string(u->second->oper));
745 //char* chl = chlist(u->second,u->second);
748 // this->WriteLine(":"+std::string(u->second->nick)+" FJOIN "+std::string(chl));
750 FOREACH_MOD OnSyncUser(u->second,(Module*)TreeProtocolModule,(void*)this);
755 void DoBurst(TreeServer* s)
757 Srv->SendOpers("*** Bursting to "+s->GetName()+".");
758 this->WriteLine("BURST");
760 this->SendServers(TreeRoot,s,1);
761 // Send users and their channels
763 // Send everything else (channel modes etc)
764 this->SendChannelModes(s);
765 this->WriteLine("ENDBURST");
768 virtual bool OnDataReady()
770 char* data = this->Read();
773 this->in_buffer += data;
774 while (in_buffer.find("\n") != std::string::npos)
776 char* line = (char*)in_buffer.c_str();
777 std::string ret = "";
778 while ((*line != '\n') && (strlen(line)))
783 if ((*line == '\n') || (*line == '\r'))
786 if (!this->ProcessLine(ret))
792 return (data != NULL);
795 int WriteLine(std::string line)
797 return this->Write(line + "\r\n");
800 bool Error(std::deque<std::string> params)
802 if (params.size() < 1)
804 std::string Errmsg = params[0];
805 std::string SName = myhost;
806 if (InboundServerName != "")
808 SName = InboundServerName;
810 Srv->SendOpers("*** ERROR from "+SName+": "+Errmsg);
811 // we will return false to cause the socket to close.
815 bool OperType(std::string prefix, std::deque<std::string> params)
817 if (params.size() != 1)
819 std::string opertype = params[0];
820 userrec* u = Srv->FindNick(prefix);
823 strlcpy(u->oper,opertype.c_str(),NICKMAX);
824 if (!strchr(u->modes,'o'))
826 strcat(u->modes,"o");
828 DoOneToAllButSender(u->server,"OPERTYPE",params,u->server);
833 bool RemoteRehash(std::string prefix, std::deque<std::string> params)
835 if (params.size() < 1)
837 std::string servermask = params[0];
838 if (Srv->MatchText(Srv->GetServerName(),servermask))
840 Srv->SendOpers("*** Remote rehash initiated from server \002"+prefix+"\002.");
842 ReadConfiguration(false);
844 DoOneToAllButSender(prefix,"REHASH",params,prefix);
848 bool RemoteKill(std::string prefix, std::deque<std::string> params)
850 if (params.size() != 2)
852 std::string nick = params[0];
853 std::string reason = params[1];
854 userrec* u = Srv->FindNick(prefix);
855 userrec* who = Srv->FindNick(nick);
858 std::string sourceserv = prefix;
861 sourceserv = u->server;
863 params[1] = ":" + params[1];
864 DoOneToAllButSender(prefix,"KILL",params,sourceserv);
865 Srv->QuitUser(who,reason);
870 bool LocalPong(std::string prefix, std::deque<std::string> params)
872 if (params.size() < 1)
874 TreeServer* ServerSource = FindServer(prefix);
877 ServerSource->SetPingFlag();
882 bool LocalPing(std::string prefix, std::deque<std::string> params)
884 if (params.size() < 1)
886 std::string stufftobounce = params[0];
887 this->WriteLine(":"+Srv->GetServerName()+" PONG "+stufftobounce);
891 bool RemoteServer(std::string prefix, std::deque<std::string> params)
893 if (params.size() < 4)
895 std::string servername = params[0];
896 std::string password = params[1];
897 // hopcount is not used for a remote server, we calculate this ourselves
898 std::string description = params[3];
899 TreeServer* ParentOfThis = FindServer(prefix);
902 this->WriteLine("ERROR :Protocol error - Introduced remote server from unknown server "+prefix);
905 TreeServer* CheckDupe = FindServer(servername);
908 this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
911 TreeServer* Node = new TreeServer(servername,description,ParentOfThis,NULL);
912 ParentOfThis->AddChild(Node);
913 params[3] = ":" + params[3];
914 DoOneToAllButSender(prefix,"SERVER",params,prefix);
915 Srv->SendOpers("*** Server \002"+prefix+"\002 introduced server \002"+servername+"\002 ("+description+")");
919 bool Outbound_Reply_Server(std::deque<std::string> params)
921 if (params.size() < 4)
923 std::string servername = params[0];
924 std::string password = params[1];
925 int hops = atoi(params[2].c_str());
928 this->WriteLine("ERROR :Server too far away for authentication");
931 std::string description = params[3];
932 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
934 if ((x->Name == servername) && (x->RecvPass == password))
936 TreeServer* CheckDupe = FindServer(servername);
939 this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
942 // Begin the sync here. this kickstarts the
943 // other side, waiting in WAIT_AUTH_2 state,
944 // into starting their burst, as it shows
946 this->LinkState = CONNECTED;
947 // we should add the details of this server now
948 // to the servers tree, as a child of the root
950 TreeServer* Node = new TreeServer(servername,description,TreeRoot,this);
951 TreeRoot->AddChild(Node);
952 params[3] = ":" + params[3];
953 DoOneToAllButSender(TreeRoot->GetName(),"SERVER",params,servername);
958 this->WriteLine("ERROR :Invalid credentials");
962 bool Inbound_Server(std::deque<std::string> params)
964 if (params.size() < 4)
966 std::string servername = params[0];
967 std::string password = params[1];
968 int hops = atoi(params[2].c_str());
971 this->WriteLine("ERROR :Server too far away for authentication");
974 std::string description = params[3];
975 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
977 if ((x->Name == servername) && (x->RecvPass == password))
979 TreeServer* CheckDupe = FindServer(servername);
982 this->WriteLine("ERROR :Server "+servername+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
985 Srv->SendOpers("*** Verified incoming server connection from \002"+servername+"\002["+this->GetIP()+"] ("+description+")");
986 this->InboundServerName = servername;
987 this->InboundDescription = description;
988 // this is good. Send our details: Our server name and description and hopcount of 0,
989 // along with the sendpass from this block.
990 this->WriteLine("SERVER "+Srv->GetServerName()+" "+x->SendPass+" 0 :"+Srv->GetServerDescription());
991 // move to the next state, we are now waiting for THEM.
992 this->LinkState = WAIT_AUTH_2;
996 this->WriteLine("ERROR :Invalid credentials");
1000 std::deque<std::string> Split(std::string line, bool stripcolon)
1002 std::deque<std::string> n;
1003 if (!strchr(line.c_str(),' '))
1008 std::stringstream s(line);
1009 std::string param = "";
1028 if ((param == ":") && (item > 0))
1051 bool ProcessLine(std::string line)
1053 char* l = (char*)line.c_str();
1054 while ((strlen(l)) && (l[strlen(l)-1] == '\r') || (l[strlen(l)-1] == '\n'))
1055 l[strlen(l)-1] = '\0';
1059 Srv->Log(DEBUG,"IN: '"+line+"'");
1060 std::deque<std::string> params = this->Split(line,true);
1061 std::string command = "";
1062 std::string prefix = "";
1063 if (((params[0].c_str())[0] == ':') && (params.size() > 1))
1066 command = params[1];
1067 char* pref = (char*)prefix.c_str();
1075 command = params[0];
1079 switch (this->LinkState)
1084 // Waiting for SERVER command from remote server. Server initiating
1085 // the connection sends the first SERVER command, listening server
1086 // replies with theirs if its happy, then if the initiator is happy,
1087 // it starts to send its net sync, which starts the merge, otherwise
1088 // it sends an ERROR.
1089 if (command == "SERVER")
1091 return this->Inbound_Server(params);
1093 else if (command == "ERROR")
1095 return this->Error(params);
1099 // Waiting for start of other side's netmerge to say they liked our
1101 if (command == "SERVER")
1103 // cant do this, they sent it to us in the WAIT_AUTH_1 state!
1107 else if (command == "BURST")
1109 this->LinkState = CONNECTED;
1110 Node = new TreeServer(InboundServerName,InboundDescription,TreeRoot,this);
1111 TreeRoot->AddChild(Node);
1113 params.push_back(InboundServerName);
1114 params.push_back("*");
1115 params.push_back("1");
1116 params.push_back(":"+InboundDescription);
1117 DoOneToAllButSender(TreeRoot->GetName(),"SERVER",params,InboundServerName);
1118 this->DoBurst(Node);
1120 else if (command == "ERROR")
1122 return this->Error(params);
1127 this->WriteLine("ERROR :Internal error -- listening socket accepted its own descriptor!!!");
1131 if (command == "SERVER")
1133 // another server we connected to, which was in WAIT_AUTH_1 state,
1134 // has just sent us their credentials. If we get this far, theyre
1135 // happy with OUR credentials, and they are now in WAIT_AUTH_2 state.
1136 // if we're happy with this, we should send our netburst which
1137 // kickstarts the merge.
1138 return this->Outbound_Reply_Server(params);
1140 else if (command == "ERROR")
1142 return this->Error(params);
1146 // This is the 'authenticated' state, when all passwords
1147 // have been exchanged and anything past this point is taken
1149 std::string target = "";
1150 if ((command == "NICK") && (params.size() > 1))
1152 return this->IntroduceClient(prefix,params);
1154 else if (command == "FJOIN")
1156 return this->ForceJoin(prefix,params);
1158 else if (command == "SERVER")
1160 return this->RemoteServer(prefix,params);
1162 else if (command == "ERROR")
1164 return this->Error(params);
1166 else if (command == "OPERTYPE")
1168 return this->OperType(prefix,params);
1170 else if (command == "FMODE")
1172 return this->ForceMode(prefix,params);
1174 else if (command == "KILL")
1176 return this->RemoteKill(prefix,params);
1178 else if (command == "FTOPIC")
1180 return this->ForceTopic(prefix,params);
1182 else if (command == "REHASH")
1184 return this->RemoteRehash(prefix,params);
1186 else if (command == "PING")
1188 return this->LocalPing(prefix,params);
1190 else if (command == "PONG")
1192 return this->LocalPong(prefix,params);
1194 else if (command == "SQUIT")
1196 if (params.size() == 2)
1198 this->Squit(FindServer(params[0]),params[1]);
1204 // not a special inter-server command.
1205 // Emulate the actual user doing the command,
1206 // this saves us having a huge ugly parser.
1207 userrec* who = Srv->FindNick(prefix);
1208 std::string sourceserv = this->myhost;
1209 if (this->InboundServerName != "")
1211 sourceserv = this->InboundServerName;
1216 target = who->server;
1217 char* strparams[127];
1218 for (unsigned int q = 0; q < params.size(); q++)
1220 strparams[q] = (char*)params[q].c_str();
1222 Srv->CallCommandHandler(command, strparams, params.size(), who);
1226 // its not a user. Its either a server, or somethings screwed up.
1227 if (IsServer(prefix))
1229 target = Srv->GetServerName();
1233 log(DEBUG,"Command with unknown origin '%s'",prefix.c_str());
1237 return DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1246 virtual std::string GetName()
1248 std::string sourceserv = this->myhost;
1249 if (this->InboundServerName != "")
1251 sourceserv = this->InboundServerName;
1256 virtual void OnTimeout()
1258 if (this->LinkState == CONNECTING)
1260 Srv->SendOpers("*** CONNECT: Connection to \002"+myhost+"\002 timed out.");
1264 virtual void OnClose()
1266 // Connection closed.
1267 // If the connection is fully up (state CONNECTED)
1268 // then propogate a netsplit to all peers.
1269 std::string quitserver = this->myhost;
1270 if (this->InboundServerName != "")
1272 quitserver = this->InboundServerName;
1274 TreeServer* s = FindServer(quitserver);
1277 Squit(s,"Remote host closed the connection");
1281 virtual int OnIncomingConnection(int newsock, char* ip)
1283 TreeSocket* s = new TreeSocket(newsock, ip);
1289 void AddThisServer(TreeServer* server, std::deque<TreeServer*> &list)
1291 for (unsigned int c = 0; c < list.size(); c++)
1293 if (list[c] == server)
1298 list.push_back(server);
1301 // returns a list of DIRECT servernames for a specific channel
1302 std::deque<TreeServer*> GetListOfServersForChannel(chanrec* c)
1304 std::deque<TreeServer*> list;
1305 std::vector<char*> *ulist = c->GetUsers();
1306 for (unsigned int i = 0; i < ulist->size(); i++)
1308 char* o = (*ulist)[i];
1309 userrec* otheruser = (userrec*)o;
1310 if (std::string(otheruser->server) != Srv->GetServerName())
1312 TreeServer* best = BestRouteTo(otheruser->server);
1314 AddThisServer(best,list);
1320 bool DoOneToAllButSenderRaw(std::string data,std::string omit,std::string prefix,std::string command,std::deque<std::string> params)
1322 TreeServer* omitroute = BestRouteTo(omit);
1323 if ((command == "NOTICE") || (command == "PRIVMSG"))
1325 if ((params.size() >= 2) && (*(params[0].c_str()) != '$'))
1327 if (*(params[0].c_str()) != '#')
1329 // special routing for private messages/notices
1330 userrec* d = Srv->FindNick(params[0]);
1333 std::deque<std::string> par;
1335 par.push_back(params[0]);
1336 par.push_back(":"+params[1]);
1337 DoOneToOne(prefix,command,par,d->server);
1343 log(DEBUG,"Channel privmsg going to chan %s",params[0].c_str());
1344 chanrec* c = Srv->FindChannel(params[0]);
1347 std::deque<TreeServer*> list = GetListOfServersForChannel(c);
1348 log(DEBUG,"Got a list of %d servers",list.size());
1349 for (unsigned int i = 0; i < list.size(); i++)
1351 TreeSocket* Sock = list[i]->GetSocket();
1352 if ((Sock) && (list[i]->GetName() != omit) && (omitroute != list[i]))
1354 log(DEBUG,"Writing privmsg to server %s",list[i]->GetName().c_str());
1355 Sock->WriteLine(data);
1363 for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1365 TreeServer* Route = TreeRoot->GetChild(x);
1366 if ((Route->GetSocket()) && (Route->GetName() != omit) && (omitroute != Route))
1368 TreeSocket* Sock = Route->GetSocket();
1369 Sock->WriteLine(data);
1375 bool DoOneToAllButSender(std::string prefix, std::string command, std::deque<std::string> params, std::string omit)
1377 TreeServer* omitroute = BestRouteTo(omit);
1378 std::string FullLine = ":" + prefix + " " + command;
1379 for (unsigned int x = 0; x < params.size(); x++)
1381 FullLine = FullLine + " " + params[x];
1383 for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1385 TreeServer* Route = TreeRoot->GetChild(x);
1386 // Send the line IF:
1387 // The route has a socket (its a direct connection)
1388 // The route isnt the one to be omitted
1389 // The route isnt the path to the one to be omitted
1390 if ((Route->GetSocket()) && (Route->GetName() != omit) && (omitroute != Route))
1392 TreeSocket* Sock = Route->GetSocket();
1393 Sock->WriteLine(FullLine);
1399 bool DoOneToMany(std::string prefix, std::string command, std::deque<std::string> params)
1401 std::string FullLine = ":" + prefix + " " + command;
1402 for (unsigned int x = 0; x < params.size(); x++)
1404 FullLine = FullLine + " " + params[x];
1406 for (unsigned int x = 0; x < TreeRoot->ChildCount(); x++)
1408 TreeServer* Route = TreeRoot->GetChild(x);
1409 if (Route->GetSocket())
1411 TreeSocket* Sock = Route->GetSocket();
1412 Sock->WriteLine(FullLine);
1418 bool DoOneToOne(std::string prefix, std::string command, std::deque<std::string> params, std::string target)
1420 TreeServer* Route = BestRouteTo(target);
1423 std::string FullLine = ":" + prefix + " " + command;
1424 for (unsigned int x = 0; x < params.size(); x++)
1426 FullLine = FullLine + " " + params[x];
1428 if (Route->GetSocket())
1430 TreeSocket* Sock = Route->GetSocket();
1431 Sock->WriteLine(FullLine);
1441 std::vector<TreeSocket*> Bindings;
1443 void ReadConfiguration(bool rebind)
1447 for (int j =0; j < Conf->Enumerate("bind"); j++)
1449 std::string Type = Conf->ReadValue("bind","type",j);
1450 std::string IP = Conf->ReadValue("bind","address",j);
1451 long Port = Conf->ReadInteger("bind","port",j,true);
1452 if (Type == "servers")
1458 TreeSocket* listener = new TreeSocket(IP.c_str(),Port,true,10);
1459 if (listener->GetState() == I_LISTENING)
1461 Srv->AddSocket(listener);
1462 Bindings.push_back(listener);
1466 log(DEFAULT,"m_spanningtree: Warning: Failed to bind server port %d",Port);
1474 for (int j =0; j < Conf->Enumerate("link"); j++)
1477 L.Name = Conf->ReadValue("link","name",j);
1478 L.IPAddr = Conf->ReadValue("link","ipaddr",j);
1479 L.Port = Conf->ReadInteger("link","port",j,true);
1480 L.SendPass = Conf->ReadValue("link","sendpass",j);
1481 L.RecvPass = Conf->ReadValue("link","recvpass",j);
1482 L.AutoConnect = Conf->ReadInteger("link","autoconnect",j,true);
1483 L.NextConnectTime = time(NULL) + L.AutoConnect;
1484 LinkBlocks.push_back(L);
1485 log(DEBUG,"m_spanningtree: Read server %s with host %s:%d",L.Name.c_str(),L.IPAddr.c_str(),L.Port);
1490 class ModuleSpanningTree : public Module
1492 std::vector<TreeSocket*> Bindings;
1498 ModuleSpanningTree()
1501 Conf = new ConfigReader;
1504 // Create the root of the tree
1505 TreeRoot = new TreeServer(Srv->GetServerName(),Srv->GetServerDescription());
1507 ReadConfiguration(true);
1510 void ShowLinks(TreeServer* Current, userrec* user, int hops)
1512 std::string Parent = TreeRoot->GetName();
1513 if (Current->GetParent())
1515 Parent = Current->GetParent()->GetName();
1517 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1519 ShowLinks(Current->GetChild(q),user,hops+1);
1521 WriteServ(user->fd,"364 %s %s %s :%d %s",user->nick,Current->GetName().c_str(),Parent.c_str(),hops,Current->GetDesc().c_str());
1524 int CountLocalServs()
1526 return TreeRoot->ChildCount();
1529 void CountServsRecursive(TreeServer* Current)
1532 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1534 CountServsRecursive(Current->GetChild(q));
1541 CountServsRecursive(TreeRoot);
1545 void HandleLinks(char** parameters, int pcnt, userrec* user)
1547 ShowLinks(TreeRoot,user,0);
1548 WriteServ(user->fd,"365 %s * :End of /LINKS list.",user->nick);
1552 void HandleLusers(char** parameters, int pcnt, userrec* user)
1554 WriteServ(user->fd,"251 %s :There are %d users and %d invisible on %d servers",user->nick,usercnt()-usercount_invisible(),usercount_invisible(),this->CountServs());
1555 WriteServ(user->fd,"252 %s %d :operator(s) online",user->nick,usercount_opers());
1556 WriteServ(user->fd,"253 %s %d :unknown connections",user->nick,usercount_unknown());
1557 WriteServ(user->fd,"254 %s %d :channels formed",user->nick,chancount());
1558 WriteServ(user->fd,"254 %s :I have %d clients and %d servers",user->nick,local_count(),this->CountLocalServs());
1562 // WARNING: NOT THREAD SAFE - DONT GET ANY SMART IDEAS.
1564 void ShowMap(TreeServer* Current, userrec* user, int depth, char matrix[128][80])
1568 for (int t = 0; t < depth; t++)
1570 matrix[line][t] = ' ';
1572 strlcpy(&matrix[line][depth],Current->GetName().c_str(),80);
1574 for (unsigned int q = 0; q < Current->ChildCount(); q++)
1576 ShowMap(Current->GetChild(q),user,depth+2,matrix);
1581 // Ok, prepare to be confused.
1582 // After much mulling over how to approach this, it struck me that
1583 // the 'usual' way of doing a /MAP isnt the best way. Instead of
1584 // keeping track of a ton of ascii characters, and line by line
1585 // under recursion working out where to place them using multiplications
1586 // and divisons, we instead render the map onto a backplane of characters
1587 // (a character matrix), then draw the branches as a series of "L" shapes
1588 // from the nodes. This is not only friendlier on CPU it uses less stack.
1590 void HandleMap(char** parameters, int pcnt, userrec* user)
1592 // This array represents a virtual screen which we will
1593 // "scratch" draw to, as the console device of an irc
1594 // client does not provide for a proper terminal.
1595 char matrix[128][80];
1596 for (unsigned int t = 0; t < 128; t++)
1598 matrix[t][0] = '\0';
1601 // The only recursive bit is called here.
1602 ShowMap(TreeRoot,user,0,matrix);
1603 // Process each line one by one. The algorithm has a limit of
1604 // 128 servers (which is far more than a spanning tree should have
1605 // anyway, so we're ok). This limit can be raised simply by making
1606 // the character matrix deeper, 128 rows taking 10k of memory.
1607 for (int l = 1; l < line; l++)
1609 // scan across the line looking for the start of the
1610 // servername (the recursive part of the algorithm has placed
1611 // the servers at indented positions depending on what they
1613 int first_nonspace = 0;
1614 while (matrix[l][first_nonspace] == ' ')
1619 // Draw the `- (corner) section: this may be overwritten by
1620 // another L shape passing along the same vertical pane, becoming
1621 // a |- (branch) section instead.
1622 matrix[l][first_nonspace] = '-';
1623 matrix[l][first_nonspace-1] = '`';
1625 // Draw upwards until we hit the parent server, causing possibly
1626 // other corners (`-) to become branches (|-)
1627 while ((matrix[l2][first_nonspace-1] == ' ') || (matrix[l2][first_nonspace-1] == '`'))
1629 matrix[l2][first_nonspace-1] = '|';
1633 // dump the whole lot to the user. This is the easy bit, honest.
1634 for (int t = 0; t < line; t++)
1636 WriteServ(user->fd,"006 %s :%s",user->nick,&matrix[t][0]);
1638 WriteServ(user->fd,"007 %s :End of /MAP",user->nick);
1642 int HandleSquit(char** parameters, int pcnt, userrec* user)
1644 TreeServer* s = FindServer(parameters[0]);
1647 TreeSocket* sock = s->GetSocket();
1650 WriteOpers("*** SQUIT: Server \002%s\002 removed from network by %s",parameters[0],user->nick);
1651 sock->Squit(s,"Server quit by "+std::string(user->nick)+"!"+std::string(user->ident)+"@"+std::string(user->host));
1656 WriteServ(user->fd,"NOTICE %s :*** SQUIT: The server \002%s\002 is not directly connected.",user->nick,parameters[0]);
1661 WriteServ(user->fd,"NOTICE %s :*** SQUIT: The server \002%s\002 does not exist on the network.",user->nick,parameters[0]);
1666 void DoPingChecks(time_t curtime)
1668 for (unsigned int j = 0; j < TreeRoot->ChildCount(); j++)
1670 TreeServer* serv = TreeRoot->GetChild(j);
1671 TreeSocket* sock = serv->GetSocket();
1674 if (serv->NextPingTime() > curtime)
1676 if (serv->AnsweredLastPing())
1678 sock->WriteLine(":"+Srv->GetServerName()+" PING "+serv->GetName());
1679 serv->SetNextPingTime(curtime + 60);
1683 // they didnt answer, boot them
1684 WriteOpers("*** Server \002%s\002 pinged out",serv->GetName().c_str());
1685 sock->Squit(serv,"Ping timeout");
1694 void AutoConnectServers(time_t curtime)
1696 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
1698 if ((x->AutoConnect) && (curtime >= x->NextConnectTime))
1700 log(DEBUG,"Auto-Connecting %s",x->Name.c_str());
1701 x->NextConnectTime = curtime + x->AutoConnect;
1702 TreeServer* CheckDupe = FindServer(x->Name);
1705 // an autoconnected server is not connected. Check if its time to connect it
1706 WriteOpers("*** AUTOCONNECT: Auto-connecting server \002%s\002 (%lu seconds until next attempt)",x->Name.c_str(),x->AutoConnect);
1707 TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name);
1708 Srv->AddSocket(newsocket);
1714 int HandleConnect(char** parameters, int pcnt, userrec* user)
1716 for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
1718 if (Srv->MatchText(x->Name.c_str(),parameters[0]))
1720 TreeServer* CheckDupe = FindServer(x->Name);
1723 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);
1724 TreeSocket* newsocket = new TreeSocket(x->IPAddr,x->Port,false,10,x->Name);
1725 Srv->AddSocket(newsocket);
1730 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());
1735 WriteServ(user->fd,"NOTICE %s :*** CONNECT: No server matching \002%s\002 could be found in the config file.",user->nick,parameters[0]);
1739 virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
1741 if (command == "CONNECT")
1743 return this->HandleConnect(parameters,pcnt,user);
1745 else if (command == "SQUIT")
1747 return this->HandleSquit(parameters,pcnt,user);
1749 else if (command == "MAP")
1751 this->HandleMap(parameters,pcnt,user);
1754 else if (command == "LUSERS")
1756 this->HandleLusers(parameters,pcnt,user);
1759 else if (command == "LINKS")
1761 this->HandleLinks(parameters,pcnt,user);
1764 else if (Srv->IsValidModuleCommand(command, pcnt, user))
1766 // this bit of code cleverly routes all module commands
1767 // to all remote severs *automatically* so that modules
1768 // can just handle commands locally, without having
1769 // to have any special provision in place for remote
1770 // commands and linking protocols.
1771 std::deque<std::string> params;
1773 for (int j = 0; j < pcnt; j++)
1775 if (strchr(parameters[j],' '))
1777 params.push_back(":" + std::string(parameters[j]));
1781 params.push_back(std::string(parameters[j]));
1784 DoOneToMany(user->nick,command,params);
1789 virtual void OnGetServerDescription(std::string servername,std::string &description)
1791 TreeServer* s = FindServer(servername);
1794 description = s->GetDesc();
1798 virtual void OnUserInvite(userrec* source,userrec* dest,chanrec* channel)
1800 if (std::string(source->server) == Srv->GetServerName())
1802 std::deque<std::string> params;
1803 params.push_back(dest->nick);
1804 params.push_back(channel->name);
1805 DoOneToMany(source->nick,"INVITE",params);
1809 virtual void OnPostLocalTopicChange(userrec* user, chanrec* chan, std::string topic)
1811 std::deque<std::string> params;
1812 params.push_back(chan->name);
1813 params.push_back(":"+topic);
1814 DoOneToMany(user->nick,"TOPIC",params);
1817 virtual void OnWallops(userrec* user, std::string text)
1819 if (std::string(user->server) == Srv->GetServerName())
1821 std::deque<std::string> params;
1822 params.push_back(":"+text);
1823 DoOneToMany(user->nick,"WALLOPS",params);
1827 virtual void OnUserNotice(userrec* user, void* dest, int target_type, std::string text)
1829 if (target_type == TYPE_USER)
1831 userrec* d = (userrec*)dest;
1832 if ((std::string(d->server) != Srv->GetServerName()) && (std::string(user->server) == Srv->GetServerName()))
1834 std::deque<std::string> params;
1836 params.push_back(d->nick);
1837 params.push_back(":"+text);
1838 DoOneToOne(user->nick,"NOTICE",params,d->server);
1843 if (std::string(user->server) == Srv->GetServerName())
1845 chanrec *c = (chanrec*)dest;
1846 std::deque<TreeServer*> list = GetListOfServersForChannel(c);
1847 for (unsigned int i = 0; i < list.size(); i++)
1849 TreeSocket* Sock = list[i]->GetSocket();
1851 Sock->WriteLine(":"+std::string(user->nick)+" NOTICE "+std::string(c->name)+" :"+text);
1857 virtual void OnUserMessage(userrec* user, void* dest, int target_type, std::string text)
1859 if (target_type == TYPE_USER)
1861 // route private messages which are targetted at clients only to the server
1862 // which needs to receive them
1863 userrec* d = (userrec*)dest;
1864 if ((std::string(d->server) != Srv->GetServerName()) && (std::string(user->server) == Srv->GetServerName()))
1866 std::deque<std::string> params;
1868 params.push_back(d->nick);
1869 params.push_back(":"+text);
1870 DoOneToOne(user->nick,"PRIVMSG",params,d->server);
1875 if (std::string(user->server) == Srv->GetServerName())
1877 chanrec *c = (chanrec*)dest;
1878 std::deque<TreeServer*> list = GetListOfServersForChannel(c);
1879 for (unsigned int i = 0; i < list.size(); i++)
1881 TreeSocket* Sock = list[i]->GetSocket();
1883 Sock->WriteLine(":"+std::string(user->nick)+" PRIVMSG "+std::string(c->name)+" :"+text);
1889 virtual void OnBackgroundTimer(time_t curtime)
1891 AutoConnectServers(curtime);
1892 DoPingChecks(curtime);
1895 virtual void OnUserJoin(userrec* user, chanrec* channel)
1897 // Only do this for local users
1898 if (std::string(user->server) == Srv->GetServerName())
1900 std::deque<std::string> params;
1902 params.push_back(channel->name);
1905 // if the channel has a key, force the join by emulating the key.
1906 params.push_back(channel->key);
1908 if (channel->GetUserCounter() > 1)
1910 // not the first in the channel
1911 DoOneToMany(user->nick,"JOIN",params);
1915 // first in the channel, set up their permissions
1916 // and the channel TS with FJOIN.
1918 snprintf(ts,24,"%lu",(unsigned long)channel->age);
1920 params.push_back(channel->name);
1921 params.push_back(ts);
1922 params.push_back("@"+std::string(user->nick));
1923 DoOneToMany(Srv->GetServerName(),"FJOIN",params);
1928 virtual void OnUserPart(userrec* user, chanrec* channel)
1930 if (std::string(user->server) == Srv->GetServerName())
1932 std::deque<std::string> params;
1934 params.push_back(channel->name);
1935 DoOneToMany(user->nick,"PART",params);
1939 virtual void OnUserConnect(userrec* user)
1941 char agestr[MAXBUF];
1942 if (std::string(user->server) == Srv->GetServerName())
1944 std::deque<std::string> params;
1945 snprintf(agestr,MAXBUF,"%lu",(unsigned long)user->age);
1947 params.push_back(agestr);
1948 params.push_back(user->nick);
1949 params.push_back(user->host);
1950 params.push_back(user->dhost);
1951 params.push_back(user->ident);
1952 params.push_back("+"+std::string(user->modes));
1953 params.push_back(user->ip);
1954 params.push_back(":"+std::string(user->fullname));
1955 DoOneToMany(Srv->GetServerName(),"NICK",params);
1959 virtual void OnUserQuit(userrec* user, std::string reason)
1961 if (std::string(user->server) == Srv->GetServerName())
1963 std::deque<std::string> params;
1964 params.push_back(":"+reason);
1965 DoOneToMany(user->nick,"QUIT",params);
1969 virtual void OnUserPostNick(userrec* user, std::string oldnick)
1971 if (std::string(user->server) == Srv->GetServerName())
1973 std::deque<std::string> params;
1974 params.push_back(user->nick);
1975 DoOneToMany(oldnick,"NICK",params);
1979 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, std::string reason)
1981 if (std::string(source->server) == Srv->GetServerName())
1983 std::deque<std::string> params;
1984 params.push_back(chan->name);
1985 params.push_back(user->nick);
1986 params.push_back(":"+reason);
1987 DoOneToMany(source->nick,"KICK",params);
1991 virtual void OnRemoteKill(userrec* source, userrec* dest, std::string reason)
1993 std::deque<std::string> params;
1994 params.push_back(dest->nick);
1995 params.push_back(":"+reason);
1996 DoOneToMany(source->nick,"KILL",params);
1999 virtual void OnRehash(std::string parameter)
2001 if (parameter != "")
2003 std::deque<std::string> params;
2004 params.push_back(parameter);
2005 DoOneToMany(Srv->GetServerName(),"REHASH",params);
2007 if (Srv->MatchText(Srv->GetServerName(),parameter))
2009 Srv->SendOpers("*** Remote rehash initiated from server \002"+Srv->GetServerName()+"\002.");
2010 Srv->RehashServer();
2013 ReadConfiguration(false);
2016 // note: the protocol does not allow direct umode +o except
2017 // via NICK with 8 params. sending OPERTYPE infers +o modechange
2019 virtual void OnOper(userrec* user, std::string opertype)
2021 if (std::string(user->server) == Srv->GetServerName())
2023 std::deque<std::string> params;
2024 params.push_back(opertype);
2025 DoOneToMany(user->nick,"OPERTYPE",params);
2029 virtual void OnMode(userrec* user, void* dest, int target_type, std::string text)
2031 if (std::string(user->server) == Srv->GetServerName())
2033 if (target_type == TYPE_USER)
2035 userrec* u = (userrec*)dest;
2036 std::deque<std::string> params;
2037 params.push_back(u->nick);
2038 params.push_back(text);
2039 DoOneToMany(user->nick,"MODE",params);
2043 chanrec* c = (chanrec*)dest;
2044 std::deque<std::string> params;
2045 params.push_back(c->name);
2046 params.push_back(text);
2047 DoOneToMany(user->nick,"MODE",params);
2052 virtual void ProtoSendMode(void* opaque, int target_type, void* target, std::string modeline)
2054 TreeSocket* s = (TreeSocket*)opaque;
2057 if (target_type == TYPE_USER)
2059 userrec* u = (userrec*)target;
2060 s->WriteLine(":"+Srv->GetServerName()+" FMODE "+u->nick+" "+modeline);
2064 chanrec* c = (chanrec*)target;
2065 s->WriteLine(":"+Srv->GetServerName()+" FMODE "+c->name+" "+modeline);
2070 virtual ~ModuleSpanningTree()
2075 virtual Version GetVersion()
2077 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
2082 class ModuleSpanningTreeFactory : public ModuleFactory
2085 ModuleSpanningTreeFactory()
2089 ~ModuleSpanningTreeFactory()
2093 virtual Module * CreateModule()
2095 TreeProtocolModule = new ModuleSpanningTree;
2096 return TreeProtocolModule;
2102 extern "C" void * init_module( void )
2104 return new ModuleSpanningTreeFactory;