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 bool TreeSocket::Modules(const std::string &prefix, std::deque<std::string> ¶ms)
46 if (!this->Instance->MatchText(this->Instance->Config->ServerName, params[0]))
48 /* Pass it on, not for us */
49 Utils->DoOneToOne(prefix, "MODULES", params, params[0]);
54 std::deque<std::string> par;
55 par.push_back(prefix);
58 userrec* source = this->Instance->FindNick(prefix);
62 for (unsigned int i = 0; i < Instance->Config->module_names.size(); i++)
64 Version V = Instance->modules[i]->GetVersion();
65 char modulename[MAXBUF];
66 char flagstate[MAXBUF];
68 if (V.Flags & VF_STATIC)
69 strlcat(flagstate,", static",MAXBUF);
70 if (V.Flags & VF_VENDOR)
71 strlcat(flagstate,", vendor",MAXBUF);
72 if (V.Flags & VF_COMMON)
73 strlcat(flagstate,", common",MAXBUF);
74 if (V.Flags & VF_SERVICEPROVIDER)
75 strlcat(flagstate,", service provider",MAXBUF);
77 strcpy(flagstate," <no flags>");
78 strlcpy(modulename,Instance->Config->module_names[i].c_str(),256);
81 snprintf(strbuf, MAXBUF, "::%s 900 %s :0x%08lx %d.%d.%d.%d %s (%s)",Instance->Config->ServerName,source->nick,(long unsigned int)Instance->modules[i],V.Major,V.Minor,V.Revision,V.Build,ServerConfig::CleanFilename(modulename),flagstate+2);
85 snprintf(strbuf, MAXBUF, "::%s 900 %s :%s",Instance->Config->ServerName,source->nick,ServerConfig::CleanFilename(modulename));
88 Utils->DoOneToOne(Instance->Config->ServerName, "PUSH", par, source->server);
90 snprintf(strbuf, MAXBUF, "::%s 901 %s :End of MODULES list", Instance->Config->ServerName, source->nick);
92 Utils->DoOneToOne(Instance->Config->ServerName, "PUSH", par, source->server);
96 /** remote MOTD. leet, huh? */
97 bool TreeSocket::Motd(const std::string &prefix, std::deque<std::string> ¶ms)
99 if (params.size() > 0)
101 if (this->Instance->MatchText(this->Instance->Config->ServerName, params[0]))
103 /* It's for our server */
105 userrec* source = this->Instance->FindNick(prefix);
109 std::deque<std::string> par;
110 par.push_back(prefix);
113 if (!Instance->Config->MOTD.size())
115 par[1] = std::string("::")+Instance->Config->ServerName+" 422 "+source->nick+" :Message of the day file is missing.";
116 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
120 par[1] = std::string("::")+Instance->Config->ServerName+" 375 "+source->nick+" :"+Instance->Config->ServerName+" message of the day";
121 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
123 for (unsigned int i = 0; i < Instance->Config->MOTD.size(); i++)
125 par[1] = std::string("::")+Instance->Config->ServerName+" 372 "+source->nick+" :- "+Instance->Config->MOTD[i];
126 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
129 par[1] = std::string("::")+Instance->Config->ServerName+" 376 "+source->nick+" End of message of the day.";
130 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
136 userrec* source = this->Instance->FindNick(prefix);
138 Utils->DoOneToOne(prefix, "MOTD", params, params[0]);
144 /** remote ADMIN. leet, huh? */
145 bool TreeSocket::Admin(const std::string &prefix, std::deque<std::string> ¶ms)
147 if (params.size() > 0)
149 if (this->Instance->MatchText(this->Instance->Config->ServerName, params[0]))
151 /* It's for our server */
153 userrec* source = this->Instance->FindNick(prefix);
156 std::deque<std::string> par;
157 par.push_back(prefix);
159 par[1] = std::string("::")+Instance->Config->ServerName+" 256 "+source->nick+" :Administrative info for "+Instance->Config->ServerName;
160 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
161 par[1] = std::string("::")+Instance->Config->ServerName+" 257 "+source->nick+" :Name - "+Instance->Config->AdminName;
162 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
163 par[1] = std::string("::")+Instance->Config->ServerName+" 258 "+source->nick+" :Nickname - "+Instance->Config->AdminNick;
164 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
165 par[1] = std::string("::")+Instance->Config->ServerName+" 258 "+source->nick+" :E-Mail - "+Instance->Config->AdminEmail;
166 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
172 userrec* source = this->Instance->FindNick(prefix);
174 Utils->DoOneToOne(prefix, "ADMIN", params, params[0]);
180 bool TreeSocket::Stats(const std::string &prefix, std::deque<std::string> ¶ms)
182 /* Get the reply to a STATS query if it matches this servername,
183 * and send it back as a load of PUSH queries
185 if (params.size() > 1)
187 if (this->Instance->MatchText(this->Instance->Config->ServerName, params[1]))
189 /* It's for our server */
191 userrec* source = this->Instance->FindNick(prefix);
194 std::deque<std::string> par;
195 par.push_back(prefix);
197 DoStats(this->Instance, *(params[0].c_str()), source, results);
198 for (size_t i = 0; i < results.size(); i++)
200 par[1] = "::" + results[i];
201 Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
208 userrec* source = this->Instance->FindNick(prefix);
210 Utils->DoOneToOne(prefix, "STATS", params, params[1]);
217 /** Because the core won't let users or even SERVERS set +o,
218 * we use the OPERTYPE command to do this.
220 bool TreeSocket::OperType(const std::string &prefix, std::deque<std::string> ¶ms)
222 if (params.size() != 1)
224 std::string opertype = params[0];
225 userrec* u = this->Instance->FindNick(prefix);
228 u->modes[UM_OPERATOR] = 1;
229 this->Instance->all_opers.push_back(u);
230 strlcpy(u->oper,opertype.c_str(),NICKMAX-1);
231 Utils->DoOneToAllButSender(u->nick,"OPERTYPE",params,u->server);
232 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()));
237 /** Because Andy insists that services-compatible servers must
238 * implement SVSNICK and SVSJOIN, that's exactly what we do :p
240 bool TreeSocket::ForceNick(const std::string &prefix, std::deque<std::string> ¶ms)
242 if (params.size() < 3)
245 userrec* u = this->Instance->FindNick(params[0]);
249 Utils->DoOneToAllButSender(prefix,"SVSNICK",params,prefix);
252 std::deque<std::string> par;
253 par.push_back(params[1]);
254 if (!u->ForceNickChange(params[1].c_str()))
256 userrec::QuitUser(this->Instance, u, "Nickname collision");
259 u->age = atoi(params[2].c_str());
265 bool TreeSocket::OperQuit(const std::string &prefix, std::deque<std::string> ¶ms)
267 if (params.size() < 1)
270 userrec* u = this->Instance->FindNick(prefix);
274 Utils->DoOneToAllButSender(prefix,"OPERQUIT",params,prefix);
275 u->SetOperQuit(params[0]);
281 * Remote SQUIT (RSQUIT). Routing works similar to SVSNICK: Route it to the server that the target is connected to locally,
282 * then let that server do the dirty work (squit it!). Example:
283 * 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
285 bool TreeSocket::RemoteSquit(const std::string &prefix, std::deque<std::string> ¶ms)
287 /* ok.. :w00t RSQUIT jupe.barafranca.com :reason here */
288 if (params.size() < 2)
291 TreeServer* s = Utils->FindServerMask(params[0]);
295 if (s == Utils->TreeRoot)
297 this->Instance->SNO->WriteToSnoMask('l',"What the fuck, I recieved a remote SQUIT for myself? :< (from %s", prefix.c_str());
301 TreeSocket* sock = s->GetSocket();
305 /* it's locally connected, KILL IT! */
306 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());
307 sock->Squit(s,"Server quit by " + prefix + ": " + params[1]);
308 Instance->SE->DelFd(sock);
314 /* route the rsquit */
315 params[1] = ":" + params[1];
316 Utils->DoOneToOne(prefix, "RSQUIT", params, params[0]);
321 /* mother fucker! it doesn't exist */
327 bool TreeSocket::ServiceJoin(const std::string &prefix, std::deque<std::string> ¶ms)
329 if (params.size() < 2)
332 userrec* u = this->Instance->FindNick(params[0]);
336 /* only join if it's local, otherwise just pass it on! */
338 chanrec::JoinUser(this->Instance, u, params[1].c_str(), false, "", Instance->Time());
339 Utils->DoOneToAllButSender(prefix,"SVSJOIN",params,prefix);
344 bool TreeSocket::RemoteRehash(const std::string &prefix, std::deque<std::string> ¶ms)
346 if (params.size() < 1)
349 std::string servermask = params[0];
351 if (this->Instance->MatchText(this->Instance->Config->ServerName,servermask))
353 this->Instance->SNO->WriteToSnoMask('l',"Remote rehash initiated by \002"+prefix+"\002.");
354 this->Instance->RehashServer();
355 Utils->ReadConfiguration(false);
356 InitializeDisabledCommands(Instance->Config->DisabledCommands, Instance);
358 Utils->DoOneToAllButSender(prefix,"REHASH",params,prefix);
362 bool TreeSocket::RemoteKill(const std::string &prefix, std::deque<std::string> ¶ms)
364 if (params.size() != 2)
367 std::string nick = params[0];
368 userrec* u = this->Instance->FindNick(prefix);
369 userrec* who = this->Instance->FindNick(nick);
373 /* Prepend kill source, if we don't have one */
374 std::string sourceserv = prefix;
377 sourceserv = u->server;
379 if (*(params[1].c_str()) != '[')
381 params[1] = "[" + sourceserv + "] Killed (" + params[1] +")";
383 std::string reason = params[1];
384 params[1] = ":" + params[1];
385 Utils->DoOneToAllButSender(prefix,"KILL",params,sourceserv);
386 who->Write(":%s KILL %s :%s (%s)", sourceserv.c_str(), who->nick, sourceserv.c_str(), reason.c_str());
387 userrec::QuitUser(this->Instance,who,reason);
392 bool TreeSocket::LocalPong(const std::string &prefix, std::deque<std::string> ¶ms)
394 if (params.size() < 1)
397 if (params.size() == 1)
399 TreeServer* ServerSource = Utils->FindServer(prefix);
402 ServerSource->SetPingFlag();
407 std::string forwardto = params[1];
408 if (forwardto == this->Instance->Config->ServerName)
411 * this is a PONG for us
412 * if the prefix is a user, check theyre local, and if they are,
413 * dump the PONG reply back to their fd. If its a server, do nowt.
414 * Services might want to send these s->s, but we dont need to yet.
416 userrec* u = this->Instance->FindNick(prefix);
419 u->WriteServ("PONG %s %s",params[0].c_str(),params[1].c_str());
424 // not for us, pass it on :)
425 Utils->DoOneToOne(prefix,"PONG",params,forwardto);
432 bool TreeSocket::MetaData(const std::string &prefix, std::deque<std::string> ¶ms)
434 if (params.size() < 2)
437 params.push_back("");
438 TreeServer* ServerSource = Utils->FindServer(prefix);
441 if (params[0] == "*")
443 FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_OTHER,NULL,params[1],params[2]));
445 else if (*(params[0].c_str()) == '#')
447 chanrec* c = this->Instance->FindChan(params[0]);
450 FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_CHANNEL,c,params[1],params[2]));
453 else if (*(params[0].c_str()) != '#')
455 userrec* u = this->Instance->FindNick(params[0]);
458 FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_USER,u,params[1],params[2]));
463 params[2] = ":" + params[2];
464 Utils->DoOneToAllButSender(prefix,"METADATA",params,prefix);
468 bool TreeSocket::ServerVersion(const std::string &prefix, std::deque<std::string> ¶ms)
470 if (params.size() < 1)
473 TreeServer* ServerSource = Utils->FindServer(prefix);
477 ServerSource->SetVersion(params[0]);
479 params[0] = ":" + params[0];
480 Utils->DoOneToAllButSender(prefix,"VERSION",params,prefix);
484 bool TreeSocket::ChangeHost(const std::string &prefix, std::deque<std::string> ¶ms)
486 if (params.size() < 1)
488 userrec* u = this->Instance->FindNick(prefix);
492 u->ChangeDisplayedHost(params[0].c_str());
493 Utils->DoOneToAllButSender(prefix,"FHOST",params,u->server);
498 bool TreeSocket::AddLine(const std::string &prefix, std::deque<std::string> ¶ms)
500 if (params.size() < 6)
502 bool propogate = false;
504 Utils->lines_to_apply = 0;
505 switch (*(params[0].c_str()))
508 propogate = Instance->XLines->add_zline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
509 Instance->XLines->zline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
511 Utils->lines_to_apply |= APPLY_ZLINES;
514 propogate = Instance->XLines->add_qline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
515 Instance->XLines->qline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
517 Utils->lines_to_apply |= APPLY_QLINES;
520 propogate = Instance->XLines->add_eline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
521 Instance->XLines->eline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
524 propogate = Instance->XLines->add_gline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
525 Instance->XLines->gline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
527 Utils->lines_to_apply |= APPLY_GLINES;
530 propogate = Instance->XLines->add_kline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
532 Utils->lines_to_apply |= APPLY_KLINES;
535 /* Just in case... */
536 this->Instance->SNO->WriteToSnoMask('x',"\2WARNING\2: Invalid xline type '"+params[0]+"' sent by server "+prefix+", ignored!");
540 /* Send it on its way */
543 if (atoi(params[4].c_str()))
545 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());
549 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());
551 params[5] = ":" + params[5];
552 Utils->DoOneToAllButSender(prefix,"ADDLINE",params,prefix);
556 Instance->XLines->apply_lines(Utils->lines_to_apply);
557 Utils->lines_to_apply = 0;
562 bool TreeSocket::ChangeName(const std::string &prefix, std::deque<std::string> ¶ms)
564 if (params.size() < 1)
566 userrec* u = this->Instance->FindNick(prefix);
569 u->ChangeName(params[0].c_str());
570 params[0] = ":" + params[0];
571 Utils->DoOneToAllButSender(prefix,"FNAME",params,u->server);
576 bool TreeSocket::Whois(const std::string &prefix, std::deque<std::string> ¶ms)
578 if (params.size() < 1)
580 userrec* u = this->Instance->FindNick(prefix);
583 // an incoming request
584 if (params.size() == 1)
586 userrec* x = this->Instance->FindNick(params[0]);
587 if ((x) && (IS_LOCAL(x)))
589 userrec* x = this->Instance->FindNick(params[0]);
592 snprintf(signon,MAXBUF,"%lu",(unsigned long)x->signon);
593 snprintf(idle,MAXBUF,"%lu",(unsigned long)abs((x->idle_lastmsg)-Instance->Time(true)));
594 std::deque<std::string> par;
595 par.push_back(prefix);
596 par.push_back(signon);
598 // ours, we're done, pass it BACK
599 Utils->DoOneToOne(params[0],"IDLE",par,u->server);
603 // not ours pass it on
604 Utils->DoOneToOne(prefix,"IDLE",params,x->server);
607 else if (params.size() == 3)
609 std::string who_did_the_whois = params[0];
610 userrec* who_to_send_to = this->Instance->FindNick(who_did_the_whois);
611 if ((who_to_send_to) && (IS_LOCAL(who_to_send_to)))
613 // an incoming reply to a whois we sent out
614 std::string nick_whoised = prefix;
615 unsigned long signon = atoi(params[1].c_str());
616 unsigned long idle = atoi(params[2].c_str());
617 if ((who_to_send_to) && (IS_LOCAL(who_to_send_to)))
618 do_whois(this->Instance,who_to_send_to,u,signon,idle,nick_whoised.c_str());
622 // not ours, pass it on
623 Utils->DoOneToOne(prefix,"IDLE",params,who_to_send_to->server);
630 bool TreeSocket::Push(const std::string &prefix, std::deque<std::string> ¶ms)
632 if (params.size() < 2)
634 userrec* u = this->Instance->FindNick(params[0]);
643 // continue the raw onwards
644 params[1] = ":" + params[1];
645 Utils->DoOneToOne(prefix,"PUSH",params,u->server);
650 bool TreeSocket::HandleSetTime(const std::string &prefix, std::deque<std::string> ¶ms)
652 if (!params.size() || !Utils->EnableTimeSync)
657 if ((params.size() == 2) && (params[1] == "FORCE"))
660 time_t them = atoi(params[0].c_str());
661 time_t us = Instance->Time(false);
663 time_t diff = them - us;
665 Utils->DoOneToAllButSender(prefix, "TIMESET", params, prefix);
667 if (force || (them != us))
669 time_t old = Instance->SetTimeDelta(diff);
670 Instance->Log(DEBUG, "TS (diff %d) from %s applied (old delta was %d)", diff, prefix.c_str(), old);
676 bool TreeSocket::Time(const std::string &prefix, std::deque<std::string> ¶ms)
678 // :source.server TIME remote.server sendernick
679 // :remote.server TIME source.server sendernick TS
680 if (params.size() == 2)
682 // someone querying our time?
683 if (this->Instance->Config->ServerName == params[0])
685 userrec* u = this->Instance->FindNick(params[1]);
688 params.push_back(ConvToStr(Instance->Time(false)));
690 Utils->DoOneToOne(this->Instance->Config->ServerName,"TIME",params,params[0]);
695 // not us, pass it on
696 userrec* u = this->Instance->FindNick(params[1]);
698 Utils->DoOneToOne(prefix,"TIME",params,params[0]);
701 else if (params.size() == 3)
703 // a response to a previous TIME
704 userrec* u = this->Instance->FindNick(params[1]);
705 if ((u) && (IS_LOCAL(u)))
707 time_t rawtime = atol(params[2].c_str());
708 struct tm * timeinfo;
709 timeinfo = localtime(&rawtime);
711 snprintf(tms,26,"%s",asctime(timeinfo));
713 u->WriteServ("391 %s %s :%s",u->nick,prefix.c_str(),tms);
718 Utils->DoOneToOne(prefix,"TIME",params,u->server);
724 bool TreeSocket::LocalPing(const std::string &prefix, std::deque<std::string> ¶ms)
726 if (params.size() < 1)
728 if (params.size() == 1)
730 std::string stufftobounce = params[0];
731 this->WriteLine(std::string(":")+this->Instance->Config->ServerName+" PONG "+stufftobounce);
736 std::string forwardto = params[1];
737 if (forwardto == this->Instance->Config->ServerName)
739 // this is a ping for us, send back PONG to the requesting server
740 params[1] = params[0];
741 params[0] = forwardto;
742 Utils->DoOneToOne(forwardto,"PONG",params,params[1]);
746 // not for us, pass it on :)
747 Utils->DoOneToOne(prefix,"PING",params,forwardto);
753 /** TODO: This creates a total mess of output and needs to really use irc::modestacker.
755 bool TreeSocket::RemoveStatus(const std::string &prefix, std::deque<std::string> ¶ms)
757 if (params.size() < 1)
759 chanrec* c = Instance->FindChan(params[0]);
762 for (char modeletter = 'A'; modeletter <= 'z'; modeletter++)
764 ModeHandler* mh = Instance->Modes->FindMode(modeletter, MODETYPE_CHANNEL);
772 bool TreeSocket::RemoteServer(const std::string &prefix, std::deque<std::string> ¶ms)
774 if (params.size() < 4)
776 std::string servername = params[0];
777 std::string password = params[1];
778 // hopcount is not used for a remote server, we calculate this ourselves
779 std::string description = params[3];
780 TreeServer* ParentOfThis = Utils->FindServer(prefix);
783 this->WriteLine("ERROR :Protocol error - Introduced remote server from unknown server "+prefix);
786 TreeServer* CheckDupe = Utils->FindServer(servername);
789 this->WriteLine("ERROR :Server "+servername+" already exists!");
790 this->Instance->SNO->WriteToSnoMask('l',"Server \2"+servername+"\2 being introduced from \2" + prefix + "\2 denied, already exists. Closing link with " + prefix);
793 TreeServer* Node = new TreeServer(this->Utils,this->Instance,servername,description,ParentOfThis,NULL);
794 ParentOfThis->AddChild(Node);
795 params[3] = ":" + params[3];
796 Utils->DoOneToAllButSender(prefix,"SERVER",params,prefix);
797 this->Instance->SNO->WriteToSnoMask('l',"Server \002"+prefix+"\002 introduced server \002"+servername+"\002 ("+description+")");
801 bool TreeSocket::Outbound_Reply_Server(std::deque<std::string> ¶ms)
803 if (params.size() < 4)
806 irc::string servername = params[0].c_str();
807 std::string sname = params[0];
808 std::string password = params[1];
809 int hops = atoi(params[2].c_str());
813 this->WriteLine("ERROR :Server too far away for authentication");
814 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
817 std::string description = params[3];
818 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
820 if ((x->Name == servername) && (x->RecvPass == password))
822 TreeServer* CheckDupe = Utils->FindServer(sname);
825 this->WriteLine("ERROR :Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
826 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
829 // Begin the sync here. this kickstarts the
830 // other side, waiting in WAIT_AUTH_2 state,
831 // into starting their burst, as it shows
833 this->LinkState = CONNECTED;
834 // we should add the details of this server now
835 // to the servers tree, as a child of the root
837 TreeServer* Node = new TreeServer(this->Utils,this->Instance,sname,description,Utils->TreeRoot,this);
838 Utils->TreeRoot->AddChild(Node);
839 params[3] = ":" + params[3];
840 Utils->DoOneToAllButSender(Utils->TreeRoot->GetName(),"SERVER",params,sname);
841 this->bursting = true;
846 this->WriteLine("ERROR :Invalid credentials");
847 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
851 bool TreeSocket::Inbound_Server(std::deque<std::string> ¶ms)
853 if (params.size() < 4)
855 irc::string servername = params[0].c_str();
856 std::string sname = params[0];
857 std::string password = params[1];
858 int hops = atoi(params[2].c_str());
862 this->WriteLine("ERROR :Server too far away for authentication");
863 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, server is too far away for authentication");
866 std::string description = params[3];
867 for (std::vector<Link>::iterator x = Utils->LinkBlocks.begin(); x < Utils->LinkBlocks.end(); x++)
869 if ((x->Name == servername) && (x->RecvPass == password))
871 TreeServer* CheckDupe = Utils->FindServer(sname);
874 this->WriteLine("ERROR :Server "+sname+" already exists on server "+CheckDupe->GetParent()->GetName()+"!");
875 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, already exists on server "+CheckDupe->GetParent()->GetName());
878 this->Instance->SNO->WriteToSnoMask('l',"Verified incoming server connection from \002"+sname+"\002["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] ("+description+")");
881 std::string name = InspSocketNameRequest((Module*)Utils->Creator, this->Hook).Send();
882 this->Instance->SNO->WriteToSnoMask('l',"Connection from \2"+sname+"\2["+(x->HiddenFromStats ? "<hidden>" : this->GetIP())+"] using transport \2"+name+"\2");
885 this->InboundServerName = sname;
886 this->InboundDescription = description;
887 // this is good. Send our details: Our server name and description and hopcount of 0,
888 // along with the sendpass from this block.
889 this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+x->SendPass+" 0 :"+this->Instance->Config->ServerDesc);
890 // move to the next state, we are now waiting for THEM.
891 this->LinkState = WAIT_AUTH_2;
895 this->WriteLine("ERROR :Invalid credentials");
896 this->Instance->SNO->WriteToSnoMask('l',"Server connection from \2"+sname+"\2 denied, invalid link credentials");
900 void TreeSocket::Split(const std::string &line, std::deque<std::string> &n)
903 irc::tokenstream tokens(line);
905 while ((param = tokens.GetToken()) != "")
910 bool TreeSocket::ProcessLine(std::string &line)
912 std::deque<std::string> params;
916 line = line.substr(0, line.find_first_of("\r\n"));
921 Instance->Log(DEBUG, "<- %s", line.c_str());
923 this->Split(line.c_str(),params);
925 if ((params[0][0] == ':') && (params.size() > 1))
927 prefix = params[0].substr(1);
930 command = params[0].c_str();
932 switch (this->LinkState)
937 // Waiting for SERVER command from remote server. Server initiating
938 // the connection sends the first SERVER command, listening server
939 // replies with theirs if its happy, then if the initiator is happy,
940 // it starts to send its net sync, which starts the merge, otherwise
941 // it sends an ERROR.
942 if (command == "PASS")
944 /* Silently ignored */
946 else if (command == "SERVER")
948 return this->Inbound_Server(params);
950 else if (command == "ERROR")
952 return this->Error(params);
954 else if (command == "USER")
956 this->WriteLine("ERROR :Client connections to this port are prohibited.");
959 else if (command == "CAPAB")
961 return this->Capab(params);
963 else if ((command == "U") || (command == "S"))
965 this->WriteLine("ERROR :Cannot use the old-style mesh linking protocol with m_spanningtree.so!");
970 std::string error("ERROR :Invalid command in negotiation phase: ");
971 error.append(command.c_str());
972 this->WriteLine(error);
977 // Waiting for start of other side's netmerge to say they liked our
979 if (command == "SERVER")
981 // cant do this, they sent it to us in the WAIT_AUTH_1 state!
985 else if ((command == "U") || (command == "S"))
987 this->WriteLine("ERROR :Cannot use the old-style mesh linking protocol with m_spanningtree.so!");
990 else if (command == "BURST")
992 if (params.size() && Utils->EnableTimeSync)
994 bool we_have_delta = (Instance->Time(false) != Instance->Time(true));
995 time_t them = atoi(params[0].c_str());
996 time_t delta = them - Instance->Time(false);
997 if ((delta < -300) || (delta > 300))
999 Instance->SNO->WriteToSnoMask('l',"\2ERROR\2: Your clocks are out by %d seconds (this is more than five minutes). Link aborted, \2PLEASE SYNC YOUR CLOCKS!\2",abs(delta));
1000 WriteLine("ERROR :Your clocks are out by "+ConvToStr(abs(delta))+" seconds (this is more than ten minutes). Link aborted, PLEASE SYNC YOUR CLOCKS!");
1003 else if ((delta < -30) || (delta > 30))
1005 Instance->SNO->WriteToSnoMask('l',"\2WARNING\2: Your clocks are out by %d seconds. Please consider synching your clocks.", abs(delta));
1008 if (!Utils->MasterTime && !we_have_delta)
1010 this->Instance->SetTimeDelta(delta);
1011 // Send this new timestamp to any other servers
1012 Utils->DoOneToMany(Utils->TreeRoot->GetName(), "TIMESET", params);
1015 this->LinkState = CONNECTED;
1016 Node = new TreeServer(this->Utils,this->Instance,InboundServerName,InboundDescription,Utils->TreeRoot,this);
1017 Utils->TreeRoot->AddChild(Node);
1019 params.push_back(InboundServerName);
1020 params.push_back("*");
1021 params.push_back("1");
1022 params.push_back(":"+InboundDescription);
1023 Utils->DoOneToAllButSender(Utils->TreeRoot->GetName(),"SERVER",params,InboundServerName);
1024 this->bursting = true;
1025 this->DoBurst(Node);
1027 else if (command == "ERROR")
1029 return this->Error(params);
1031 else if (command == "CAPAB")
1033 return this->Capab(params);
1038 this->WriteLine("ERROR :Internal error -- listening socket accepted its own descriptor!!!");
1042 if (command == "SERVER")
1044 // another server we connected to, which was in WAIT_AUTH_1 state,
1045 // has just sent us their credentials. If we get this far, theyre
1046 // happy with OUR credentials, and they are now in WAIT_AUTH_2 state.
1047 // if we're happy with this, we should send our netburst which
1048 // kickstarts the merge.
1049 return this->Outbound_Reply_Server(params);
1051 else if (command == "ERROR")
1053 return this->Error(params);
1057 // This is the 'authenticated' state, when all passwords
1058 // have been exchanged and anything past this point is taken
1063 std::string direction = prefix;
1064 userrec* t = this->Instance->FindNick(prefix);
1067 direction = t->server;
1069 TreeServer* route_back_again = Utils->BestRouteTo(direction);
1070 if ((!route_back_again) || (route_back_again->GetSocket() != this))
1072 if (route_back_again)
1073 Instance->Log(DEBUG,"Protocol violation: Fake direction in command '%s' from connection '%s'",line.c_str(),this->GetName().c_str());
1077 * When there is activity on the socket, reset the ping counter so
1078 * that we're not wasting bandwidth pinging an active server.
1080 route_back_again->SetNextPingTime(time(NULL) + 60);
1081 route_back_again->SetPingFlag();
1084 if (command == "SVSMODE")
1086 /* Services expects us to implement
1087 * SVSMODE. In inspircd its the same as
1092 std::string target = "";
1093 /* Yes, know, this is a mess. Its reasonably fast though as we're
1094 * working with std::string here.
1096 if ((command == "NICK") && (params.size() >= 8))
1098 return this->IntroduceClient(prefix,params);
1100 else if (command == "FJOIN")
1102 return this->ForceJoin(prefix,params);
1104 else if (command == "STATS")
1106 return this->Stats(prefix, params);
1108 else if (command == "MOTD")
1110 return this->Motd(prefix, params);
1112 else if (command == "MODULES")
1114 return this->Modules(prefix, params);
1116 else if (command == "ADMIN")
1118 return this->Admin(prefix, params);
1120 else if (command == "SERVER")
1122 return this->RemoteServer(prefix,params);
1124 else if (command == "ERROR")
1126 return this->Error(params);
1128 else if (command == "OPERTYPE")
1130 return this->OperType(prefix,params);
1132 else if (command == "FMODE")
1134 return this->ForceMode(prefix,params);
1136 else if (command == "KILL")
1138 return this->RemoteKill(prefix,params);
1140 else if (command == "FTOPIC")
1142 return this->ForceTopic(prefix,params);
1144 else if (command == "REHASH")
1146 return this->RemoteRehash(prefix,params);
1148 else if (command == "METADATA")
1150 return this->MetaData(prefix,params);
1152 else if (command == "REMSTATUS")
1154 return this->RemoveStatus(prefix,params);
1156 else if (command == "PING")
1159 * We just got a ping from a server that's bursting.
1160 * This can't be right, so set them to not bursting, and
1161 * apply their lines.
1165 this->bursting = false;
1166 Instance->XLines->apply_lines(Utils->lines_to_apply);
1167 Utils->lines_to_apply = 0;
1171 prefix = this->GetName();
1173 return this->LocalPing(prefix,params);
1175 else if (command == "PONG")
1178 * We just got a pong from a server that's bursting.
1179 * This can't be right, so set them to not bursting, and
1180 * apply their lines.
1184 this->bursting = false;
1185 Instance->XLines->apply_lines(Utils->lines_to_apply);
1186 Utils->lines_to_apply = 0;
1190 prefix = this->GetName();
1192 return this->LocalPong(prefix,params);
1194 else if (command == "VERSION")
1196 return this->ServerVersion(prefix,params);
1198 else if (command == "FHOST")
1200 return this->ChangeHost(prefix,params);
1202 else if (command == "FNAME")
1204 return this->ChangeName(prefix,params);
1206 else if (command == "ADDLINE")
1208 return this->AddLine(prefix,params);
1210 else if (command == "SVSNICK")
1214 prefix = this->GetName();
1216 return this->ForceNick(prefix,params);
1218 else if (command == "OPERQUIT")
1220 return this->OperQuit(prefix,params);
1222 else if (command == "RSQUIT")
1224 return this->RemoteSquit(prefix, params);
1226 else if (command == "IDLE")
1228 return this->Whois(prefix,params);
1230 else if (command == "PUSH")
1232 return this->Push(prefix,params);
1234 else if (command == "TIMESET")
1236 return this->HandleSetTime(prefix, params);
1238 else if (command == "TIME")
1240 return this->Time(prefix,params);
1242 else if ((command == "KICK") && (Utils->IsServer(prefix)))
1244 std::string sourceserv = this->myhost;
1245 if (params.size() == 3)
1247 userrec* user = this->Instance->FindNick(params[1]);
1248 chanrec* chan = this->Instance->FindChan(params[0]);
1251 if (!chan->ServerKickUser(user, params[2].c_str(), false))
1252 /* Yikes, the channels gone! */
1256 if (this->InboundServerName != "")
1258 sourceserv = this->InboundServerName;
1260 return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1262 else if (command == "SVSJOIN")
1266 prefix = this->GetName();
1268 return this->ServiceJoin(prefix,params);
1270 else if (command == "SQUIT")
1272 if (params.size() == 2)
1274 this->Squit(Utils->FindServer(params[0]),params[1]);
1278 else if (command == "OPERNOTICE")
1280 std::string sourceserv = this->myhost;
1281 if (this->InboundServerName != "")
1282 sourceserv = this->InboundServerName;
1283 if (params.size() >= 1)
1284 Instance->WriteOpers("*** From " + sourceserv + ": " + params[0]);
1285 return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params);
1287 else if (command == "MODENOTICE")
1289 std::string sourceserv = this->myhost;
1290 if (this->InboundServerName != "")
1291 sourceserv = this->InboundServerName;
1292 if (params.size() >= 2)
1294 Instance->WriteMode(params[0].c_str(), WM_AND, "*** From %s: %s", sourceserv.c_str(), params[1].c_str());
1296 return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params);
1298 else if (command == "SNONOTICE")
1300 std::string sourceserv = this->myhost;
1301 if (this->InboundServerName != "")
1302 sourceserv = this->InboundServerName;
1303 if (params.size() >= 2)
1305 Instance->SNO->WriteToSnoMask(*(params[0].c_str()), "From " + sourceserv + ": "+ params[1]);
1307 return Utils->DoOneToAllButSenderRaw(line, sourceserv, prefix, command, params);
1309 else if (command == "ENDBURST")
1311 this->bursting = false;
1312 Instance->XLines->apply_lines(Utils->lines_to_apply);
1313 Utils->lines_to_apply = 0;
1314 std::string sourceserv = this->myhost;
1315 if (this->InboundServerName != "")
1317 sourceserv = this->InboundServerName;
1319 this->Instance->SNO->WriteToSnoMask('l',"Received end of netburst from \2%s\2",sourceserv.c_str());
1321 Event rmode((char*)sourceserv.c_str(), (Module*)Utils->Creator, "new_server");
1322 rmode.Send(Instance);
1328 // not a special inter-server command.
1329 // Emulate the actual user doing the command,
1330 // this saves us having a huge ugly parser.
1331 userrec* who = this->Instance->FindNick(prefix);
1332 std::string sourceserv = this->myhost;
1333 if (this->InboundServerName != "")
1335 sourceserv = this->InboundServerName;
1337 if ((!who) && (command == "MODE"))
1339 if (Utils->IsServer(prefix))
1341 const char* modelist[127];
1342 for (size_t i = 0; i < params.size(); i++)
1343 modelist[i] = params[i].c_str();
1344 userrec* fake = new userrec(Instance);
1345 fake->SetFd(FD_MAGIC_NUMBER);
1346 this->Instance->SendMode(modelist, params.size(), fake);
1349 /* Hot potato! pass it on! */
1350 return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1355 if ((command == "NICK") && (params.size() > 0))
1357 /* On nick messages, check that the nick doesnt
1358 * already exist here. If it does, kill their copy,
1361 userrec* x = this->Instance->FindNick(params[0]);
1362 if ((x) && (x != who))
1364 std::deque<std::string> p;
1365 p.push_back(params[0]);
1366 p.push_back("Nickname collision ("+prefix+" -> "+params[0]+")");
1367 Utils->DoOneToMany(this->Instance->Config->ServerName,"KILL",p);
1369 p.push_back(prefix);
1370 p.push_back("Nickname collision");
1371 Utils->DoOneToMany(this->Instance->Config->ServerName,"KILL",p);
1372 userrec::QuitUser(this->Instance,x,"Nickname collision ("+prefix+" -> "+params[0]+")");
1373 userrec* y = this->Instance->FindNick(prefix);
1376 userrec::QuitUser(this->Instance,y,"Nickname collision");
1378 return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1382 target = who->server;
1383 const char* strparams[127];
1384 for (unsigned int q = 0; q < params.size(); q++)
1386 strparams[q] = params[q].c_str();
1388 switch (this->Instance->CallCommandHandler(command.c_str(), strparams, params.size(), who))
1391 this->WriteLine("ERROR :Unrecognised command '"+std::string(command.c_str())+"' -- possibly loaded mismatched modules");
1398 /* CMD_SUCCESS and CMD_USER_DELETED fall through here */
1404 // its not a user. Its either a server, or somethings screwed up.
1405 if (Utils->IsServer(prefix))
1406 target = this->Instance->Config->ServerName;
1410 return Utils->DoOneToAllButSenderRaw(line,sourceserv,prefix,command,params);
1419 std::string TreeSocket::GetName()
1421 std::string sourceserv = this->myhost;
1422 if (this->InboundServerName != "")
1424 sourceserv = this->InboundServerName;
1429 void TreeSocket::OnTimeout()
1431 if (this->LinkState == CONNECTING)
1433 this->Instance->SNO->WriteToSnoMask('l',"CONNECT: Connection to \002"+myhost+"\002 timed out.");
1434 Link* MyLink = Utils->FindLink(myhost);
1436 Utils->DoFailOver(MyLink);
1440 void TreeSocket::OnClose()
1442 // Connection closed.
1443 // If the connection is fully up (state CONNECTED)
1444 // then propogate a netsplit to all peers.
1445 std::string quitserver = this->myhost;
1446 if (this->InboundServerName != "")
1448 quitserver = this->InboundServerName;
1450 TreeServer* s = Utils->FindServer(quitserver);
1453 Squit(s,"Remote host closed the connection");
1456 if (quitserver != "")
1457 this->Instance->SNO->WriteToSnoMask('l',"Connection to '\2%s\2' failed.",quitserver.c_str());
1460 int TreeSocket::OnIncomingConnection(int newsock, char* ip)
1462 /* To prevent anyone from attempting to flood opers/DDoS by connecting to the server port,
1463 * or discovering if this port is the server port, we don't allow connections from any
1464 * IPs for which we don't have a link block.
1468 found = (std::find(Utils->ValidIPs.begin(), Utils->ValidIPs.end(), ip) != Utils->ValidIPs.end());
1471 for (vector<std::string>::iterator i = Utils->ValidIPs.begin(); i != Utils->ValidIPs.end(); i++)
1472 if (irc::sockets::MatchCIDR(ip, (*i).c_str()))
1477 this->Instance->SNO->WriteToSnoMask('l',"Server connection from %s denied (no link blocks with that IP address)", ip);
1483 TreeSocket* s = new TreeSocket(this->Utils, this->Instance, newsock, ip, this->Hook);
1484 s = s; /* Whinge whinge whinge, thats all GCC ever does. */