1 #include "configreader.h"
5 #include "commands/cmd_whois.h"
6 #include "commands/cmd_stats.h"
11 #include "transport.h"
12 #include "socketengine.h"
14 #include "m_spanningtree/main.h"
15 #include "m_spanningtree/utils.h"
16 #include "m_spanningtree/treeserver.h"
17 #include "m_spanningtree/link.h"
18 #include "m_spanningtree/treesocket.h"
19 #include "m_spanningtree/resolvers.h"
20 #include "m_spanningtree/handshaketimer.h"
22 /* $ModDep: m_spanningtree/timesynctimer.h m_spanningtree/resolvers.h m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/link.h m_spanningtree/treesocket.h */
24 int TreeSocket::WriteLine(std::string line)
26 Instance->Log(DEBUG, "-> %s", line.c_str());
28 return this->Write(line);
31 /* Handle ERROR command */
32 bool TreeSocket::Error(std::deque<std::string> ¶ms)
34 if (params.size() < 1)
36 this->Instance->SNO->WriteToSnoMask('l',"ERROR from %s: %s",(InboundServerName != "" ? InboundServerName.c_str() : myhost.c_str()),params[0].c_str());
37 /* we will return false to cause the socket to close. */
41 /** remote MOTD. leet, huh? */
42 bool TreeSocket::Motd(const std::string &prefix, std::deque<std::string> ¶ms)
44 if (params.size() > 0)
46 if (this->Instance->MatchText(this->Instance->Config->ServerName, params[0]))
48 /* It's for our server */
50 userrec* source = this->Instance->FindNick(prefix);
54 std::deque<std::string> par;
55 par.push_back(prefix);
58 if (!Instance->Config->MOTD.size())
60 par[1] = std::string("::")+Instance->Config->ServerName+" 422 "+source->nick+" :Message of the day file is missing.";
61 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
65 par[1] = std::string("::")+Instance->Config->ServerName+" 375 "+source->nick+" :"+Instance->Config->ServerName+" message of the day";
66 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
68 for (unsigned int i = 0; i < Instance->Config->MOTD.size(); i++)
70 par[1] = std::string("::")+Instance->Config->ServerName+" 372 "+source->nick+" :- "+Instance->Config->MOTD[i];
71 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
74 par[1] = std::string("::")+Instance->Config->ServerName+" 376 "+source->nick+" End of message of the day.";
75 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
81 userrec* source = this->Instance->FindNick(prefix);
83 Utils->DoOneToOne(prefix, "MOTD", params, params[0]);
89 /** remote ADMIN. leet, huh? */
90 bool TreeSocket::Admin(const std::string &prefix, std::deque<std::string> ¶ms)
92 if (params.size() > 0)
94 if (this->Instance->MatchText(this->Instance->Config->ServerName, params[0]))
96 /* It's for our server */
98 userrec* source = this->Instance->FindNick(prefix);
101 std::deque<std::string> par;
102 par.push_back(prefix);
104 par[1] = std::string("::")+Instance->Config->ServerName+" 256 "+source->nick+" :Administrative info for "+Instance->Config->ServerName;
105 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
106 par[1] = std::string("::")+Instance->Config->ServerName+" 257 "+source->nick+" :Name - "+Instance->Config->AdminName;
107 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
108 par[1] = std::string("::")+Instance->Config->ServerName+" 258 "+source->nick+" :Nickname - "+Instance->Config->AdminNick;
109 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
110 par[1] = std::string("::")+Instance->Config->ServerName+" 258 "+source->nick+" :E-Mail - "+Instance->Config->AdminEmail;
111 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
117 userrec* source = this->Instance->FindNick(prefix);
119 Utils->DoOneToOne(prefix, "ADMIN", params, params[0]);
125 bool TreeSocket::Stats(const std::string &prefix, std::deque<std::string> ¶ms)
127 /* Get the reply to a STATS query if it matches this servername,
128 * and send it back as a load of PUSH queries
130 if (params.size() > 1)
132 if (this->Instance->MatchText(this->Instance->Config->ServerName, params[1]))
134 /* It's for our server */
136 userrec* source = this->Instance->FindNick(prefix);
139 std::deque<std::string> par;
140 par.push_back(prefix);
142 DoStats(this->Instance, *(params[0].c_str()), source, results);
143 for (size_t i = 0; i < results.size(); i++)
145 par[1] = "::" + results[i];
146 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
153 userrec* source = this->Instance->FindNick(prefix);
155 Utils->DoOneToOne(prefix, "STATS", params, params[1]);
162 /** Because the core won't let users or even SERVERS set +o,
163 * we use the OPERTYPE command to do this.
165 bool TreeSocket::OperType(const std::string &prefix, std::deque<std::string> ¶ms)
167 if (params.size() != 1)
169 std::string opertype = params[0];
170 userrec* u = this->Instance->FindNick(prefix);
173 u->modes[UM_OPERATOR] = 1;
174 this->Instance->all_opers.push_back(u);
175 strlcpy(u->oper,opertype.c_str(),NICKMAX-1);
176 Utils->DoOneToAllButSender(u->nick,"OPERTYPE",params,u->server);
177 this->Instance->SNO->WriteToSnoMask('o',"From %s: User %s (%s@%s) is now an IRC operator of type %s",u->server, u->nick,u->ident,u->host,irc::Spacify(opertype.c_str()));
182 /** Because Andy insists that services-compatible servers must
183 * implement SVSNICK and SVSJOIN, that's exactly what we do :p
185 bool TreeSocket::ForceNick(const std::string &prefix, std::deque<std::string> ¶ms)
187 if (params.size() < 3)
190 userrec* u = this->Instance->FindNick(params[0]);
194 Utils->DoOneToAllButSender(prefix,"SVSNICK",params,prefix);
197 std::deque<std::string> par;
198 par.push_back(params[1]);
199 if (!u->ForceNickChange(params[1].c_str()))
201 userrec::QuitUser(this->Instance, u, "Nickname collision");
204 u->age = atoi(params[2].c_str());
210 bool TreeSocket::OperQuit(const std::string &prefix, std::deque<std::string> ¶ms)
212 if (params.size() < 1)
215 userrec* u = this->Instance->FindNick(prefix);
219 Utils->DoOneToAllButSender(prefix,"OPERQUIT",params,prefix);
220 u->SetOperQuit(params[0]);
226 * Remote SQUIT (RSQUIT). Routing works similar to SVSNICK: Route it to the server that the target is connected to locally,
227 * then let that server do the dirty work (squit it!). Example:
228 * A -> B -> C -> D: oper on A squits D, A routes to B, B routes to C, C notices D connected locally, kills it. -- w00t
230 bool TreeSocket::RemoteSquit(const std::string &prefix, std::deque<std::string> ¶ms)
232 /* ok.. :w00t RSQUIT jupe.barafranca.com :reason here */
233 if (params.size() < 2)
236 TreeServer* s = Utils->FindServerMask(params[0]);
240 if (s == Utils->TreeRoot)
242 this->Instance->SNO->WriteToSnoMask('l',"What the fuck, I recieved a remote SQUIT for myself? :< (from %s", prefix.c_str());
246 TreeSocket* sock = s->GetSocket();
250 /* it's locally connected, KILL IT! */
251 Instance->SNO->WriteToSnoMask('l',"RSQUIT: Server \002%s\002 removed from network by %s: %s", params[0].c_str(), prefix.c_str(), params[1].c_str());
252 sock->Squit(s,"Server quit by " + prefix + ": " + params[1]);
253 Instance->SE->DelFd(sock);
259 /* route the rsquit */
260 params[1] = ":" + params[1];
261 Utils->DoOneToOne(prefix, "RSQUIT", params, params[0]);
266 /* mother fucker! it doesn't exist */
272 bool TreeSocket::ServiceJoin(const std::string &prefix, std::deque<std::string> ¶ms)
274 if (params.size() < 2)
277 userrec* u = this->Instance->FindNick(params[0]);
281 /* only join if it's local, otherwise just pass it on! */
283 chanrec::JoinUser(this->Instance, u, params[1].c_str(), false);
284 Utils->DoOneToAllButSender(prefix,"SVSJOIN",params,prefix);
289 bool TreeSocket::RemoteRehash(const std::string &prefix, std::deque<std::string> ¶ms)
291 if (params.size() < 1)
294 std::string servermask = params[0];
296 if (this->Instance->MatchText(this->Instance->Config->ServerName,servermask))
298 this->Instance->SNO->WriteToSnoMask('l',"Remote rehash initiated by \002"+prefix+"\002.");
299 this->Instance->RehashServer();
300 Utils->ReadConfiguration(false);
301 InitializeDisabledCommands(Instance->Config->DisabledCommands, Instance);
303 Utils->DoOneToAllButSender(prefix,"REHASH",params,prefix);
307 bool TreeSocket::RemoteKill(const std::string &prefix, std::deque<std::string> ¶ms)
309 if (params.size() != 2)
312 std::string nick = params[0];
313 userrec* u = this->Instance->FindNick(prefix);
314 userrec* who = this->Instance->FindNick(nick);
318 /* Prepend kill source, if we don't have one */
319 std::string sourceserv = prefix;
322 sourceserv = u->server;
324 if (*(params[1].c_str()) != '[')
326 params[1] = "[" + sourceserv + "] Killed (" + params[1] +")";
328 std::string reason = params[1];
329 params[1] = ":" + params[1];
330 Utils->DoOneToAllButSender(prefix,"KILL",params,sourceserv);
331 who->Write(":%s KILL %s :%s (%s)", sourceserv.c_str(), who->nick, sourceserv.c_str(), reason.c_str());
332 userrec::QuitUser(this->Instance,who,reason);
337 bool TreeSocket::LocalPong(const std::string &prefix, std::deque<std::string> ¶ms)
339 if (params.size() < 1)
342 if (params.size() == 1)
344 TreeServer* ServerSource = Utils->FindServer(prefix);
347 ServerSource->SetPingFlag();
352 std::string forwardto = params[1];
353 if (forwardto == this->Instance->Config->ServerName)
356 * this is a PONG for us
357 * if the prefix is a user, check theyre local, and if they are,
358 * dump the PONG reply back to their fd. If its a server, do nowt.
359 * Services might want to send these s->s, but we dont need to yet.
361 userrec* u = this->Instance->FindNick(prefix);
364 u->WriteServ("PONG %s %s",params[0].c_str(),params[1].c_str());
369 // not for us, pass it on :)
370 Utils->DoOneToOne(prefix,"PONG",params,forwardto);
377 bool TreeSocket::MetaData(const std::string &prefix, std::deque<std::string> ¶ms)
379 if (params.size() < 2)
382 params.push_back("");
383 TreeServer* ServerSource = Utils->FindServer(prefix);
386 if (params[0] == "*")
388 FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_OTHER,NULL,params[1],params[2]));
390 else if (*(params[0].c_str()) == '#')
392 chanrec* c = this->Instance->FindChan(params[0]);
395 FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_CHANNEL,c,params[1],params[2]));
398 else if (*(params[0].c_str()) != '#')
400 userrec* u = this->Instance->FindNick(params[0]);
403 FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_USER,u,params[1],params[2]));
408 params[2] = ":" + params[2];
409 Utils->DoOneToAllButSender(prefix,"METADATA",params,prefix);
413 bool TreeSocket::ServerVersion(const std::string &prefix, std::deque<std::string> ¶ms)
415 if (params.size() < 1)
418 TreeServer* ServerSource = Utils->FindServer(prefix);
422 ServerSource->SetVersion(params[0]);
424 params[0] = ":" + params[0];
425 Utils->DoOneToAllButSender(prefix,"VERSION",params,prefix);
429 bool TreeSocket::ChangeHost(const std::string &prefix, std::deque<std::string> ¶ms)
431 if (params.size() < 1)
433 userrec* u = this->Instance->FindNick(prefix);
437 u->ChangeDisplayedHost(params[0].c_str());
438 Utils->DoOneToAllButSender(prefix,"FHOST",params,u->server);
443 bool TreeSocket::AddLine(const std::string &prefix, std::deque<std::string> ¶ms)
445 if (params.size() < 6)
447 bool propogate = false;
449 Utils->lines_to_apply = 0;
450 switch (*(params[0].c_str()))
453 propogate = Instance->XLines->add_zline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
454 Instance->XLines->zline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
456 Utils->lines_to_apply |= APPLY_ZLINES;
459 propogate = Instance->XLines->add_qline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
460 Instance->XLines->qline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
462 Utils->lines_to_apply |= APPLY_QLINES;
465 propogate = Instance->XLines->add_eline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
466 Instance->XLines->eline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
469 propogate = Instance->XLines->add_gline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
470 Instance->XLines->gline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
472 Utils->lines_to_apply |= APPLY_GLINES;
475 propogate = Instance->XLines->add_kline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
477 Utils->lines_to_apply |= APPLY_KLINES;
480 /* Just in case... */
481 this->Instance->SNO->WriteToSnoMask('x',"\2WARNING\2: Invalid xline type '"+params[0]+"' sent by server "+prefix+", ignored!");
485 /* Send it on its way */
488 if (atoi(params[4].c_str()))
490 this->Instance->SNO->WriteToSnoMask('x',"%s Added %cLINE on %s to expire in %lu seconds (%s).",prefix.c_str(),*(params[0].c_str()),params[1].c_str(),atoi(params[4].c_str()),params[5].c_str());
494 this->Instance->SNO->WriteToSnoMask('x',"%s Added permenant %cLINE on %s (%s).",prefix.c_str(),*(params[0].c_str()),params[1].c_str(),params[5].c_str());
496 params[5] = ":" + params[5];
497 Utils->DoOneToAllButSender(prefix,"ADDLINE",params,prefix);
501 Instance->XLines->apply_lines(Utils->lines_to_apply);
502 Utils->lines_to_apply = 0;
507 bool TreeSocket::ChangeName(const std::string &prefix, std::deque<std::string> ¶ms)
509 if (params.size() < 1)
511 userrec* u = this->Instance->FindNick(prefix);
514 u->ChangeName(params[0].c_str());
515 params[0] = ":" + params[0];
516 Utils->DoOneToAllButSender(prefix,"FNAME",params,u->server);
521 bool TreeSocket::Whois(const std::string &prefix, std::deque<std::string> ¶ms)
523 if (params.size() < 1)
525 userrec* u = this->Instance->FindNick(prefix);
528 // an incoming request
529 if (params.size() == 1)
531 userrec* x = this->Instance->FindNick(params[0]);
532 if ((x) && (IS_LOCAL(x)))
534 userrec* x = this->Instance->FindNick(params[0]);
537 snprintf(signon,MAXBUF,"%lu",(unsigned long)x->signon);
538 snprintf(idle,MAXBUF,"%lu",(unsigned long)abs((x->idle_lastmsg)-Instance->Time(true)));
539 std::deque<std::string> par;
540 par.push_back(prefix);
541 par.push_back(signon);
543 // ours, we're done, pass it BACK
544 Utils->DoOneToOne(params[0],"IDLE",par,u->server);
548 // not ours pass it on
549 Utils->DoOneToOne(prefix,"IDLE",params,x->server);
552 else if (params.size() == 3)
554 std::string who_did_the_whois = params[0];
555 userrec* who_to_send_to = this->Instance->FindNick(who_did_the_whois);
556 if ((who_to_send_to) && (IS_LOCAL(who_to_send_to)))
558 // an incoming reply to a whois we sent out
559 std::string nick_whoised = prefix;
560 unsigned long signon = atoi(params[1].c_str());
561 unsigned long idle = atoi(params[2].c_str());
562 if ((who_to_send_to) && (IS_LOCAL(who_to_send_to)))
563 do_whois(this->Instance,who_to_send_to,u,signon,idle,nick_whoised.c_str());
567 // not ours, pass it on
568 Utils->DoOneToOne(prefix,"IDLE",params,who_to_send_to->server);
575 bool TreeSocket::Push(const std::string &prefix, std::deque<std::string> ¶ms)
577 if (params.size() < 2)
579 userrec* u = this->Instance->FindNick(params[0]);
588 // continue the raw onwards
589 params[1] = ":" + params[1];
590 Utils->DoOneToOne(prefix,"PUSH",params,u->server);
595 bool TreeSocket::HandleSetTime(const std::string &prefix, std::deque<std::string> ¶ms)
597 if (!params.size() || !Utils->EnableTimeSync)
602 if ((params.size() == 2) && (params[1] == "FORCE"))
605 time_t them = atoi(params[0].c_str());
606 time_t us = Instance->Time(false);
608 time_t diff = them - us;
610 Utils->DoOneToAllButSender(prefix, "TIMESET", params, prefix);
612 if (force || (them != us))
614 time_t old = Instance->SetTimeDelta(diff);
615 Instance->Log(DEBUG, "TS (diff %d) from %s applied (old delta was %d)", diff, prefix.c_str(), old);
621 bool TreeSocket::Time(const std::string &prefix, std::deque<std::string> ¶ms)
623 // :source.server TIME remote.server sendernick
624 // :remote.server TIME source.server sendernick TS
625 if (params.size() == 2)
627 // someone querying our time?
628 if (this->Instance->Config->ServerName == params[0])
630 userrec* u = this->Instance->FindNick(params[1]);
633 params.push_back(ConvToStr(Instance->Time(false)));
635 Utils->DoOneToOne(this->Instance->Config->ServerName,"TIME",params,params[0]);
640 // not us, pass it on
641 userrec* u = this->Instance->FindNick(params[1]);
643 Utils->DoOneToOne(prefix,"TIME",params,params[0]);
646 else if (params.size() == 3)
648 // a response to a previous TIME
649 userrec* u = this->Instance->FindNick(params[1]);
650 if ((u) && (IS_LOCAL(u)))
652 time_t rawtime = atol(params[2].c_str());
653 struct tm * timeinfo;
654 timeinfo = localtime(&rawtime);
656 snprintf(tms,26,"%s",asctime(timeinfo));
658 u->WriteServ("391 %s %s :%s",u->nick,prefix.c_str(),tms);
663 Utils->DoOneToOne(prefix,"TIME",params,u->server);
669 bool TreeSocket::LocalPing(const std::string &prefix, std::deque<std::string> ¶ms)
671 if (params.size() < 1)
673 if (params.size() == 1)
675 std::string stufftobounce = params[0];
676 this->WriteLine(std::string(":")+this->Instance->Config->ServerName+" PONG "+stufftobounce);
681 std::string forwardto = params[1];
682 if (forwardto == this->Instance->Config->ServerName)
684 // this is a ping for us, send back PONG to the requesting server
685 params[1] = params[0];
686 params[0] = forwardto;
687 Utils->DoOneToOne(forwardto,"PONG",params,params[1]);
691 // not for us, pass it on :)
692 Utils->DoOneToOne(prefix,"PING",params,forwardto);
698 /** TODO: This creates a total mess of output and needs to really use irc::modestacker.
700 bool TreeSocket::RemoveStatus(const std::string &prefix, std::deque<std::string> ¶ms)
702 if (params.size() < 1)
704 chanrec* c = Instance->FindChan(params[0]);
707 for (char modeletter = 'A'; modeletter <= 'z'; modeletter++)
709 ModeHandler* mh = Instance->Modes->FindMode(modeletter, MODETYPE_CHANNEL);
717 bool TreeSocket::RemoteServer(const std::string &prefix, std::deque<std::string> ¶ms)
719 if (params.size() < 4)
721 std::string servername = params[0];
722 std::string password = params[1];
723 // hopcount is not used for a remote server, we calculate this ourselves
724 std::string description = params[3];
725 TreeServer* ParentOfThis = Utils->FindServer(prefix);
728 this->WriteLine("ERROR :Protocol error - Introduced remote server from unknown server "+prefix);
731 TreeServer* CheckDupe = Utils->FindServer(servername);
734 this->WriteLine("ERROR :Server "+servername+" already exists!");
735 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+servername+"\2 denied, already exists");
738 TreeServer* Node = new TreeServer(this->Utils,this->Instance,servername,description,ParentOfThis,NULL);
739 ParentOfThis->AddChild(Node);
740 params[3] = ":" + params[3];
741 Utils->DoOneToAllButSender(prefix,"SERVER",params,prefix);
742 this->Instance->SNO->WriteToSnoMask('l',"Server \002"+prefix+"\002 introduced server \002"+servername+"\002 ("+description+")");
746 bool TreeSocket::Outbound_Reply_Server(std::deque<std::string> ¶ms)
748 if (params.size() < 4)
751 irc::string servername = params[0].c_str();
752 std::string sname = params[0];
753 std::string password = params[1];
754 int hops = atoi(params[2].c_str());
758 this->WriteLine("ERROR :Server too far away for authentication");
759 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
762 std::string description = params[3];
763 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
765 if ((x->Name == servername) && (x->RecvPass == password))
767 TreeServer* CheckDupe = Utils->FindServer(sname);
770 this->WriteLine("ERROR :Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
771 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
774 // Begin the sync here. this kickstarts the
775 // other side, waiting in WAIT_AUTH_2 state,
776 // into starting their burst, as it shows
778 this->LinkState = CONNECTED;
779 // we should add the details of this server now
780 // to the servers tree, as a child of the root
782 TreeServer* Node = new TreeServer(this->Utils,this->Instance,sname,description,Utils->TreeRoot,this);
783 Utils->TreeRoot->AddChild(Node);
784 params[3] = ":" + params[3];
785 Utils->DoOneToAllButSender(Utils->TreeRoot->GetName(),"SERVER",params,sname);
786 this->bursting = true;
791 this->WriteLine("ERROR :Invalid credentials");
792 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
796 bool TreeSocket::Inbound_Server(std::deque<std::string> ¶ms)
798 if (params.size() < 4)
800 irc::string servername = params[0].c_str();
801 std::string sname = params[0];
802 std::string password = params[1];
803 int hops = atoi(params[2].c_str());
807 this->WriteLine("ERROR :Server too far away for authentication");
808 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
811 std::string description = params[3];
812 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
814 if ((x->Name == servername) && (x->RecvPass == password))
816 TreeServer* CheckDupe = Utils->FindServer(sname);
819 this->WriteLine("ERROR :Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
820 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
823 this->Instance->SNO->WriteToSnoMask('l',"Verified incoming server connection from \002"+sname+"\002["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] ("+description+")");
826 std::string name = InspSocketNameRequest((Module*)Utils->Creator, this->Hook).Send();
827 this->Instance->SNO->WriteToSnoMask('l',"Connection from \2"+sname+"\2["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] using transport \2"+name+"\2");
830 this->InboundServerName = sname;
831 this->InboundDescription = description;
832 // this is good. Send our details: Our server name and description and hopcount of 0,
833 // along with the sendpass from this block.
834 this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+x->SendPass+" 0 :"+this->Instance->Config->ServerDesc);
835 // move to the next state, we are now waiting for THEM.
836 this->LinkState = WAIT_AUTH_2;
840 this->WriteLine("ERROR :Invalid credentials");
841 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
845 void TreeSocket::Split(const std::string &line, std::deque<std::string> &n)
848 irc::tokenstream tokens(line);
850 while ((param = tokens.GetToken()) != "")
855 bool TreeSocket::ProcessLine(std::string &line)
857 std::deque<std::string> params;
861 line = line.substr(0, line.find_first_of("\r\n"));
866 Instance->Log(DEBUG, "<- %s", line.c_str());
868 this->Split(line.c_str(),params);
870 if ((params[0][0] == ':') && (params.size() > 1))
872 prefix = params[0].substr(1);
875 command = params[0].c_str();
877 switch (this->LinkState)
882 // Waiting for SERVER command from remote server. Server initiating
883 // the connection sends the first SERVER command, listening server
884 // replies with theirs if its happy, then if the initiator is happy,
885 // it starts to send its net sync, which starts the merge, otherwise
886 // it sends an ERROR.
887 if (command == "PASS")
889 /* Silently ignored */
891 else if (command == "SERVER")
893 return this->Inbound_Server(params);
895 else if (command == "ERROR")
897 return this->Error(params);
899 else if (command == "USER")
901 this->WriteLine("ERROR :Client connections to this port are prohibited.");
904 else if (command == "CAPAB")
906 return this->Capab(params);
908 else if ((command == "U") || (command == "S"))
910 this->WriteLine("ERROR :Cannot use the old-style mesh linking protocol with m_spanningtree.so!");
915 std::string error("ERROR :Invalid command in negotiation phase: ");
916 error.append(command.c_str());
917 this->WriteLine(error);
922 // Waiting for start of other side's netmerge to say they liked our
924 if (command == "SERVER")
926 // cant do this, they sent it to us in the WAIT_AUTH_1 state!
930 else if ((command == "U") || (command == "S"))
932 this->WriteLine("ERROR :Cannot use the old-style mesh linking protocol with m_spanningtree.so!");
935 else if (command == "BURST")
937 if (params.size() && Utils->EnableTimeSync)
939 bool we_have_delta = (Instance->Time(false) != Instance->Time(true));
940 time_t them = atoi(params[0].c_str());
941 time_t delta = them - Instance->Time(false);
942 if ((delta < -600) || (delta > 600))
944 this->Instance->SNO->WriteToSnoMask('l',"\2ERROR\2: Your clocks are out by %d seconds (this is more than ten minutes). Link aborted, \2PLEASE SYNC YOUR CLOCKS!\2",abs(delta));
945 this->WriteLine("ERROR :Your clocks are out by "+ConvToStr(abs(delta))+" seconds (this is more than ten minutes). Link aborted, PLEASE SYNC YOUR CLOCKS!");
949 if (!Utils->MasterTime && !we_have_delta)
951 this->Instance->SetTimeDelta(delta);
952 // Send this new timestamp to any other servers
953 Utils->DoOneToMany(Utils->TreeRoot->GetName(), "TIMESET", params);
956 this->LinkState = CONNECTED;
957 Node = new TreeServer(this->Utils,this->Instance,InboundServerName,InboundDescription,Utils->TreeRoot,this);
958 Utils->TreeRoot->AddChild(Node);
960 params.push_back(InboundServerName);
961 params.push_back("*");
962 params.push_back("1");
963 params.push_back(":"+InboundDescription);
964 Utils->DoOneToAllButSender(Utils->TreeRoot->GetName(),"SERVER",params,InboundServerName);
965 this->bursting = true;
968 else if (command == "ERROR")
970 return this->Error(params);
972 else if (command == "CAPAB")
974 return this->Capab(params);
979 this->WriteLine("ERROR :Internal error -- listening socket accepted its own descriptor!!!");
983 if (command == "SERVER")
985 // another server we connected to, which was in WAIT_AUTH_1 state,
986 // has just sent us their credentials. If we get this far, theyre
987 // happy with OUR credentials, and they are now in WAIT_AUTH_2 state.
988 // if we're happy with this, we should send our netburst which
989 // kickstarts the merge.
990 return this->Outbound_Reply_Server(params);
992 else if (command == "ERROR")
994 return this->Error(params);
998 // This is the 'authenticated' state, when all passwords
999 // have been exchanged and anything past this point is taken
1004 std::string direction = prefix;
1005 userrec* t = this->Instance->FindNick(prefix);
1008 direction = t->server;
1010 TreeServer* route_back_again = Utils->BestRouteTo(direction);
1011 if ((!route_back_again) || (route_back_again->GetSocket() != this))
1013 if (route_back_again)
1014 Instance->Log(DEBUG,"Protocol violation: Fake direction in command '%s' from connection '%s'",line.c_str(),this->GetName().c_str());
1018 * When there is activity on the socket, reset the ping counter so
1019 * that we're not wasting bandwidth pinging an active server.
1021 route_back_again->SetNextPingTime(time(NULL) + 60);
1022 route_back_again->SetPingFlag();
1025 if (command == "SVSMODE")
1027 /* Services expects us to implement
1028 * SVSMODE. In inspircd its the same as
1033 std::string target = "";
1034 /* Yes, know, this is a mess. Its reasonably fast though as we're
1035 * working with std::string here.
1037 if ((command == "NICK") && (params.size() >= 8))
1039 return this->IntroduceClient(prefix,params);
1041 else if (command == "FJOIN")
1043 return this->ForceJoin(prefix,params);
1045 else if (command == "STATS")
1047 return this->Stats(prefix, params);
1049 else if (command == "MOTD")
1051 return this->Motd(prefix, params);
1053 else if (command == "ADMIN")
1055 return this->Admin(prefix, params);
1057 else if (command == "SERVER")
1059 return this->RemoteServer(prefix,params);
1061 else if (command == "ERROR")
1063 return this->Error(params);
1065 else if (command == "OPERTYPE")
1067 return this->OperType(prefix,params);
1069 else if (command == "FMODE")
1071 return this->ForceMode(prefix,params);
1073 else if (command == "KILL")
1075 return this->RemoteKill(prefix,params);
1077 else if (command == "FTOPIC")
1079 return this->ForceTopic(prefix,params);
1081 else if (command == "REHASH")
1083 return this->RemoteRehash(prefix,params);
1085 else if (command == "METADATA")
1087 return this->MetaData(prefix,params);
1089 else if (command == "REMSTATUS")
1091 return this->RemoveStatus(prefix,params);
1093 else if (command == "PING")
1096 * We just got a ping from a server that's bursting.
1097 * This can't be right, so set them to not bursting, and
1098 * apply their lines.
1102 this->bursting = false;
1103 Instance->XLines->apply_lines(Utils->lines_to_apply);
1104 Utils->lines_to_apply = 0;
1108 prefix = this->GetName();
1110 return this->LocalPing(prefix,params);
1112 else if (command == "PONG")
1115 * We just got a pong from a server that's bursting.
1116 * This can't be right, so set them to not bursting, and
1117 * apply their lines.
1121 this->bursting = false;
1122 Instance->XLines->apply_lines(Utils->lines_to_apply);
1123 Utils->lines_to_apply = 0;
1127 prefix = this->GetName();
1129 return this->LocalPong(prefix,params);
1131 else if (command == "VERSION")
1133 return this->ServerVersion(prefix,params);
1135 else if (command == "FHOST")
1137 return this->ChangeHost(prefix,params);
1139 else if (command == "FNAME")
1141 return this->ChangeName(prefix,params);
1143 else if (command == "ADDLINE")
1145 return this->AddLine(prefix,params);
1147 else if (command == "SVSNICK")
1151 prefix = this->GetName();
1153 return this->ForceNick(prefix,params);
1155 else if (command == "OPERQUIT")
1157 return this->OperQuit(prefix,params);
1159 else if (command == "RSQUIT")
1161 return this->RemoteSquit(prefix, params);
1163 else if (command == "IDLE")
1165 return this->Whois(prefix,params);
1167 else if (command == "PUSH")
1169 return this->Push(prefix,params);
1171 else if (command == "TIMESET")
1173 return this->HandleSetTime(prefix, params);
1175 else if (command == "TIME")
1177 return this->Time(prefix,params);
1179 else if ((command == "KICK") && (Utils->IsServer(prefix)))
1181 std::string sourceserv = this->myhost;
1182 if (params.size() == 3)
1184 userrec* user = this->Instance->FindNick(params[1]);
1185 chanrec* chan = this->Instance->FindChan(params[0]);
1188 if (!chan->ServerKickUser(user, params[2].c_str(), false))
1189 /* Yikes, the channels gone! */
1193 if (this->InboundServerName != "")
1195 sourceserv = this->InboundServerName;
1197 return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1199 else if (command == "SVSJOIN")
1203 prefix = this->GetName();
1205 return this->ServiceJoin(prefix,params);
1207 else if (command == "SQUIT")
1209 if (params.size() == 2)
1211 this->Squit(Utils->FindServer(params[0]),params[1]);
1215 else if (command == "OPERNOTICE")
1217 std::string sourceserv = this->myhost;
1218 if (this->InboundServerName != "")
1219 sourceserv = this->InboundServerName;
1220 if (params.size() >= 1)
1221 Instance->WriteOpers("*** From " + sourceserv + ": " + params[0]);
1222 return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params);
1224 else if (command == "MODENOTICE")
1226 std::string sourceserv = this->myhost;
1227 if (this->InboundServerName != "")
1228 sourceserv = this->InboundServerName;
1229 if (params.size() >= 2)
1231 Instance->WriteMode(params[0].c_str(), WM_AND, "*** From %s: %s", sourceserv.c_str(), params[1].c_str());
1233 return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params);
1235 else if (command == "SNONOTICE")
1237 std::string sourceserv = this->myhost;
1238 if (this->InboundServerName != "")
1239 sourceserv = this->InboundServerName;
1240 if (params.size() >= 2)
1242 Instance->SNO->WriteToSnoMask(*(params[0].c_str()), "From " + sourceserv + ": "+ params[1]);
1244 return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params);
1246 else if (command == "ENDBURST")
1248 this->bursting = false;
1249 Instance->XLines->apply_lines(Utils->lines_to_apply);
1250 Utils->lines_to_apply = 0;
1251 std::string sourceserv = this->myhost;
1252 if (this->InboundServerName != "")
1254 sourceserv = this->InboundServerName;
1256 this->Instance->SNO->WriteToSnoMask('l',"Received end of netburst from \2%s\2",sourceserv.c_str());
1258 Event rmode((char*)sourceserv.c_str(), (Module*)Utils->Creator, "new_server");
1259 rmode.Send(Instance);
1265 // not a special inter-server command.
1266 // Emulate the actual user doing the command,
1267 // this saves us having a huge ugly parser.
1268 userrec* who = this->Instance->FindNick(prefix);
1269 std::string sourceserv = this->myhost;
1270 if (this->InboundServerName != "")
1272 sourceserv = this->InboundServerName;
1274 if ((!who) && (command == "MODE"))
1276 if (Utils->IsServer(prefix))
1278 const char* modelist[127];
1279 for (size_t i = 0; i < params.size(); i++)
1280 modelist[i] = params[i].c_str();
1281 userrec* fake = new userrec(Instance);
1282 fake->SetFd(FD_MAGIC_NUMBER);
1283 this->Instance->SendMode(modelist, params.size(), fake);
1286 /* Hot potato! pass it on! */
1287 return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1292 if ((command == "NICK") && (params.size() > 0))
1294 /* On nick messages, check that the nick doesnt
1295 * already exist here. If it does, kill their copy,
1298 userrec* x = this->Instance->FindNick(params[0]);
1299 if ((x) && (x != who))
1301 std::deque<std::string> p;
1302 p.push_back(params[0]);
1303 p.push_back("Nickname collision ("+prefix+" -> "+params[0]+")");
1304 Utils->DoOneToMany(this->Instance->Config->ServerName,"KILL",p);
1306 p.push_back(prefix);
1307 p.push_back("Nickname collision");
1308 Utils->DoOneToMany(this->Instance->Config->ServerName,"KILL",p);
1309 userrec::QuitUser(this->Instance,x,"Nickname collision ("+prefix+" -> "+params[0]+")");
1310 userrec* y = this->Instance->FindNick(prefix);
1313 userrec::QuitUser(this->Instance,y,"Nickname collision");
1315 return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1319 target = who->server;
1320 const char* strparams[127];
1321 for (unsigned int q = 0; q < params.size(); q++)
1323 strparams[q] = params[q].c_str();
1325 switch (this->Instance->CallCommandHandler(command.c_str(), strparams, params.size(), who))
1328 this->WriteLine("ERROR :Unrecognised command '"+std::string(command.c_str())+"' -- possibly loaded mismatched modules");
1335 /* CMD_SUCCESS and CMD_USER_DELETED fall through here */
1341 // its not a user. Its either a server, or somethings screwed up.
1342 if (Utils->IsServer(prefix))
1343 target = this->Instance->Config->ServerName;
1347 return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1356 std::string TreeSocket::GetName()
1358 std::string sourceserv = this->myhost;
1359 if (this->InboundServerName != "")
1361 sourceserv = this->InboundServerName;
1366 void TreeSocket::OnTimeout()
1368 if (this->LinkState == CONNECTING)
1370 this->Instance->SNO->WriteToSnoMask('l',"CONNECT: Connection to \002"+myhost+"\002 timed out.");
1371 Link* MyLink = Utils->FindLink(myhost);
1373 Utils->DoFailOver(MyLink);
1377 void TreeSocket::OnClose()
1379 // Connection closed.
1380 // If the connection is fully up (state CONNECTED)
1381 // then propogate a netsplit to all peers.
1382 std::string quitserver = this->myhost;
1383 if (this->InboundServerName != "")
1385 quitserver = this->InboundServerName;
1387 TreeServer* s = Utils->FindServer(quitserver);
1390 Squit(s,"Remote host closed the connection");
1393 if (quitserver != "")
1394 this->Instance->SNO->WriteToSnoMask('l',"Connection to '\2%s\2' failed.",quitserver.c_str());
1397 int TreeSocket::OnIncomingConnection(int newsock, char* ip)
1399 /* To prevent anyone from attempting to flood opers/DDoS by connecting to the server port,
1400 * or discovering if this port is the server port, we don't allow connections from any
1401 * IPs for which we don't have a link block.
1405 found = (std::find(Utils->ValidIPs.begin(), Utils->ValidIPs.end(), ip) != Utils->ValidIPs.end());
1408 for (vector<std::string>::iterator i = Utils->ValidIPs.begin(); i != Utils->ValidIPs.end(); i++)
1409 if (irc::sockets::MatchCIDR(ip, (*i).c_str()))
1414 this->Instance->SNO->WriteToSnoMask('l',"Server connection from %s denied (no link blocks with that IP address)", ip);
1420 TreeSocket* s = new TreeSocket(this->Utils, this->Instance, newsock, ip, this->Hook);
1421 s = s; /* Whinge whinge whinge, thats all GCC ever does. */