-#include "configreader.h"
-#include "users.h"
-#include "channels.h"
-#include "modules.h"
-#include "commands/cmd_whois.h"
-#include "commands/cmd_stats.h"
-#include "socket.h"
-#include "inspircd.h"
-#include "wildcard.h"
-#include "xline.h"
-#include "transport.h"
-#include "socketengine.h"
-
-#include "m_spanningtree/main.h"
-#include "m_spanningtree/utils.h"
-#include "m_spanningtree/treeserver.h"
-#include "m_spanningtree/link.h"
-#include "m_spanningtree/treesocket.h"
-#include "m_spanningtree/resolvers.h"
-#include "m_spanningtree/handshaketimer.h"
-
-/* $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 */
-
-int TreeSocket::WriteLine(std::string line)
-{
- Instance->Log(DEBUG, "-> %s", line.c_str());
- line.append("\r\n");
- return this->Write(line);
-}
-
-/* Handle ERROR command */
-bool TreeSocket::Error(std::deque<std::string> ¶ms)
-{
- if (params.size() < 1)
- return false;
- this->Instance->SNO->WriteToSnoMask('l',"ERROR from %s: %s",(InboundServerName != "" ? InboundServerName.c_str() : myhost.c_str()),params[0].c_str());
- /* we will return false to cause the socket to close. */
- return false;
-}
-
-/** remote MOTD. leet, huh? */
-bool TreeSocket::Motd(const std::string &prefix, std::deque<std::string> ¶ms)
-{
- if (params.size() > 0)
- {
- if (this->Instance->MatchText(this->Instance->Config->ServerName, params[0]))
- {
- /* It's for our server */
- string_list results;
- userrec* source = this->Instance->FindNick(prefix);
-
- if (source)
- {
- std::deque<std::string> par;
- par.push_back(prefix);
- par.push_back("");
-
- if (!Instance->Config->MOTD.size())
- {
- par[1] = std::string("::")+Instance->Config->ServerName+" 422 "+source->nick+" :Message of the day file is missing.";
- Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
- return true;
- }
-
- par[1] = std::string("::")+Instance->Config->ServerName+" 375 "+source->nick+" :"+Instance->Config->ServerName+" message of the day";
- Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
-
- for (unsigned int i = 0; i < Instance->Config->MOTD.size(); i++)
- {
- par[1] = std::string("::")+Instance->Config->ServerName+" 372 "+source->nick+" :- "+Instance->Config->MOTD[i];
- Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
- }
-
- par[1] = std::string("::")+Instance->Config->ServerName+" 376 "+source->nick+" End of message of the day.";
- Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
- }
- }
- else
- {
- /* Pass it on */
- userrec* source = this->Instance->FindNick(prefix);
- if (source)
- Utils->DoOneToOne(prefix, "MOTD", params, params[0]);
- }
- }
- return true;
-}
-
-/** remote ADMIN. leet, huh? */
-bool TreeSocket::Admin(const std::string &prefix, std::deque<std::string> ¶ms)
-{
- if (params.size() > 0)
- {
- if (this->Instance->MatchText(this->Instance->Config->ServerName, params[0]))
- {
- /* It's for our server */
- string_list results;
- userrec* source = this->Instance->FindNick(prefix);
- if (source)
- {
- std::deque<std::string> par;
- par.push_back(prefix);
- par.push_back("");
- par[1] = std::string("::")+Instance->Config->ServerName+" 256 "+source->nick+" :Administrative info for "+Instance->Config->ServerName;
- Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
- par[1] = std::string("::")+Instance->Config->ServerName+" 257 "+source->nick+" :Name - "+Instance->Config->AdminName;
- Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
- par[1] = std::string("::")+Instance->Config->ServerName+" 258 "+source->nick+" :Nickname - "+Instance->Config->AdminNick;
- Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
- par[1] = std::string("::")+Instance->Config->ServerName+" 258 "+source->nick+" :E-Mail - "+Instance->Config->AdminEmail;
- Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
- }
- }
- else
- {
- /* Pass it on */
- userrec* source = this->Instance->FindNick(prefix);
- if (source)
- Utils->DoOneToOne(prefix, "ADMIN", params, params[0]);
- }
- }
- return true;
-}
-
-bool TreeSocket::Stats(const std::string &prefix, std::deque<std::string> ¶ms)
-{
- /* Get the reply to a STATS query if it matches this servername,
- * and send it back as a load of PUSH queries
- */
- if (params.size() > 1)
- {
- if (this->Instance->MatchText(this->Instance->Config->ServerName, params[1]))
- {
- /* It's for our server */
- string_list results;
- userrec* source = this->Instance->FindNick(prefix);
- if (source)
- {
- std::deque<std::string> par;
- par.push_back(prefix);
- par.push_back("");
- DoStats(this->Instance, *(params[0].c_str()), source, results);
- for (size_t i = 0; i < results.size(); i++)
- {
- par[1] = "::" + results[i];
- Utils->DoOneToOne(this->Instance->Config->ServerName, "PUSH",par, source->server);
- }
- }
- }
- else
- {
- /* Pass it on */
- userrec* source = this->Instance->FindNick(prefix);
- if (source)
- Utils->DoOneToOne(prefix, "STATS", params, params[1]);
- }
- }
- return true;
-}
-
-
-/** Because the core won't let users or even SERVERS set +o,
- * we use the OPERTYPE command to do this.
- */
-bool TreeSocket::OperType(const std::string &prefix, std::deque<std::string> ¶ms)
-{
- if (params.size() != 1)
- return true;
- std::string opertype = params[0];
- userrec* u = this->Instance->FindNick(prefix);
- if (u)
- {
- u->modes[UM_OPERATOR] = 1;
- this->Instance->all_opers.push_back(u);
- strlcpy(u->oper,opertype.c_str(),NICKMAX-1);
- Utils->DoOneToAllButSender(u->nick,"OPERTYPE",params,u->server);
- 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()));
- }
- return true;
-}
-
-/** Because Andy insists that services-compatible servers must
- * implement SVSNICK and SVSJOIN, that's exactly what we do :p
- */
-bool TreeSocket::ForceNick(const std::string &prefix, std::deque<std::string> ¶ms)
-{
- if (params.size() < 3)
- return true;
-
- userrec* u = this->Instance->FindNick(params[0]);
-
- if (u)
- {
- Utils->DoOneToAllButSender(prefix,"SVSNICK",params,prefix);
- if (IS_LOCAL(u))
- {
- std::deque<std::string> par;
- par.push_back(params[1]);
- if (!u->ForceNickChange(params[1].c_str()))
- {
- userrec::QuitUser(this->Instance, u, "Nickname collision");
- return true;
- }
- u->age = atoi(params[2].c_str());
- }
- }
- return true;
-}
-
-/*
- * Remote SQUIT (RSQUIT). Routing works similar to SVSNICK: Route it to the server that the target is connected to locally,
- * then let that server do the dirty work (squit it!). Example:
- * 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
- */
-bool TreeSocket::RemoteSquit(const std::string &prefix, std::deque<std::string> ¶ms)
-{
- /* ok.. :w00t RSQUIT jupe.barafranca.com :reason here */
- if (params.size() < 2)
- return true;
-
- TreeServer* s = Utils->FindServerMask(params[0]);
-
- if (s)
- {
- if (s == Utils->TreeRoot)
- {
- this->Instance->SNO->WriteToSnoMask('l',"What the fuck, I recieved a remote SQUIT for myself? :< (from %s", prefix.c_str());
- return true;
- }
-
- TreeSocket* sock = s->GetSocket();
-
- if (sock)
- {
- /* it's locally connected, KILL IT! */
- 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());
- sock->Squit(s,"Server quit by " + prefix + ": " + params[1]);
- Instance->SE->DelFd(sock);
- sock->Close();
- delete sock;
- }
- else
- {
- /* route the rsquit */
- params[1] = ":" + params[1];
- Utils->DoOneToOne(prefix, "RSQUIT", params, params[0]);
- }
- }
- else
- {
- /* mother fucker! it doesn't exist */
- }
-
- return true;
-}
-
-bool TreeSocket::ServiceJoin(const std::string &prefix, std::deque<std::string> ¶ms)
-{
- if (params.size() < 2)
- return true;
-
- userrec* u = this->Instance->FindNick(params[0]);
-
- if (u)
- {
- /* only join if it's local, otherwise just pass it on! */
- if (IS_LOCAL(u))
- chanrec::JoinUser(this->Instance, u, params[1].c_str(), false);
- Utils->DoOneToAllButSender(prefix,"SVSJOIN",params,prefix);
- }
- return true;
-}
-
-bool TreeSocket::RemoteRehash(const std::string &prefix, std::deque<std::string> ¶ms)
-{
- if (params.size() < 1)
- return false;
-
- std::string servermask = params[0];
-
- if (this->Instance->MatchText(this->Instance->Config->ServerName,servermask))
- {
- this->Instance->SNO->WriteToSnoMask('l',"Remote rehash initiated by \002"+prefix+"\002.");
- this->Instance->RehashServer();
- Utils->ReadConfiguration(false);
- InitializeDisabledCommands(Instance->Config->DisabledCommands, Instance);
- }
- Utils->DoOneToAllButSender(prefix,"REHASH",params,prefix);
- return true;
-}
-
-bool TreeSocket::RemoteKill(const std::string &prefix, std::deque<std::string> ¶ms)
-{
- if (params.size() != 2)
- return true;
-
- std::string nick = params[0];
- userrec* u = this->Instance->FindNick(prefix);
- userrec* who = this->Instance->FindNick(nick);
-
- if (who)
- {
- /* Prepend kill source, if we don't have one */
- std::string sourceserv = prefix;
- if (u)
- {
- sourceserv = u->server;
- }
- if (*(params[1].c_str()) != '[')
- {
- params[1] = "[" + sourceserv + "] Killed (" + params[1] +")";
- }
- std::string reason = params[1];
- params[1] = ":" + params[1];
- Utils->DoOneToAllButSender(prefix,"KILL",params,sourceserv);
- who->Write(":%s KILL %s :%s (%s)", sourceserv.c_str(), who->nick, sourceserv.c_str(), reason.c_str());
- userrec::QuitUser(this->Instance,who,reason);
- }
- return true;
-}
-
-bool TreeSocket::LocalPong(const std::string &prefix, std::deque<std::string> ¶ms)
-{
- if (params.size() < 1)
- return true;
-
- if (params.size() == 1)
- {
- TreeServer* ServerSource = Utils->FindServer(prefix);
- if (ServerSource)
- {
- ServerSource->SetPingFlag();
- }
- }
- else
- {
- std::string forwardto = params[1];
- if (forwardto == this->Instance->Config->ServerName)
- {
- /*
- * this is a PONG for us
- * if the prefix is a user, check theyre local, and if they are,
- * dump the PONG reply back to their fd. If its a server, do nowt.
- * Services might want to send these s->s, but we dont need to yet.
- */
- userrec* u = this->Instance->FindNick(prefix);
- if (u)
- {
- u->WriteServ("PONG %s %s",params[0].c_str(),params[1].c_str());
- }
- }
- else
- {
- // not for us, pass it on :)
- Utils->DoOneToOne(prefix,"PONG",params,forwardto);
- }
- }
-
- return true;
-}
-
-bool TreeSocket::MetaData(const std::string &prefix, std::deque<std::string> ¶ms)
-{
- if (params.size() < 2)
- return true;
- else
- params.push_back("");
- TreeServer* ServerSource = Utils->FindServer(prefix);
- if (ServerSource)
- {
- if (params[0] == "*")
- {
- FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_OTHER,NULL,params[1],params[2]));
- }
- else if (*(params[0].c_str()) == '#')
- {
- chanrec* c = this->Instance->FindChan(params[0]);
- if (c)
- {
- FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_CHANNEL,c,params[1],params[2]));
- }
- }
- else if (*(params[0].c_str()) != '#')
- {
- userrec* u = this->Instance->FindNick(params[0]);
- if (u)
- {
- FOREACH_MOD_I(this->Instance,I_OnDecodeMetaData,OnDecodeMetaData(TYPE_USER,u,params[1],params[2]));
- }
- }
- }
-
- params[2] = ":" + params[2];
- Utils->DoOneToAllButSender(prefix,"METADATA",params,prefix);
- return true;
-}
-
-bool TreeSocket::ServerVersion(const std::string &prefix, std::deque<std::string> ¶ms)
-{
- if (params.size() < 1)
- return true;
-
- TreeServer* ServerSource = Utils->FindServer(prefix);
-
- if (ServerSource)
- {
- ServerSource->SetVersion(params[0]);
- }
- params[0] = ":" + params[0];
- Utils->DoOneToAllButSender(prefix,"VERSION",params,prefix);
- return true;
-}
-
-bool TreeSocket::ChangeHost(const std::string &prefix, std::deque<std::string> ¶ms)
-{
- if (params.size() < 1)
- return true;
- userrec* u = this->Instance->FindNick(prefix);
-
- if (u)
- {
- u->ChangeDisplayedHost(params[0].c_str());
- Utils->DoOneToAllButSender(prefix,"FHOST",params,u->server);
- }
- return true;
-}
-
-bool TreeSocket::AddLine(const std::string &prefix, std::deque<std::string> ¶ms)
-{
- if (params.size() < 6)
- return true;
- bool propogate = false;
- if (!this->bursting)
- Utils->lines_to_apply = 0;
- switch (*(params[0].c_str()))
- {
- case 'Z':
- propogate = Instance->XLines->add_zline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
- Instance->XLines->zline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
- if (propogate)
- Utils->lines_to_apply |= APPLY_ZLINES;
- break;
- case 'Q':
- propogate = Instance->XLines->add_qline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
- Instance->XLines->qline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
- if (propogate)
- Utils->lines_to_apply |= APPLY_QLINES;
- break;
- case 'E':
- propogate = Instance->XLines->add_eline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
- Instance->XLines->eline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
- break;
- case 'G':
- propogate = Instance->XLines->add_gline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
- Instance->XLines->gline_set_creation_time(params[1].c_str(), atoi(params[3].c_str()));
- if (propogate)
- Utils->lines_to_apply |= APPLY_GLINES;
- break;
- case 'K':
- propogate = Instance->XLines->add_kline(atoi(params[4].c_str()), params[2].c_str(), params[5].c_str(), params[1].c_str());
- if (propogate)
- Utils->lines_to_apply |= APPLY_KLINES;
- break;
- default:
- /* Just in case... */
- this->Instance->SNO->WriteToSnoMask('x',"\2WARNING\2: Invalid xline type '"+params[0]+"' sent by server "+prefix+", ignored!");
- propogate = false;
- break;
- }
- /* Send it on its way */
- if (propogate)
- {
- if (atoi(params[4].c_str()))
- {
- 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());
- }
- else
- {
- 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());
- }
- params[5] = ":" + params[5];
- Utils->DoOneToAllButSender(prefix,"ADDLINE",params,prefix);
- }
- if (!this->bursting)
- {
- Instance->XLines->apply_lines(Utils->lines_to_apply);
- Utils->lines_to_apply = 0;
- }
- return true;
-}
-
-bool TreeSocket::ChangeName(const std::string &prefix, std::deque<std::string> ¶ms)
-{
- if (params.size() < 1)
- return true;
- userrec* u = this->Instance->FindNick(prefix);
- if (u)
- {
- u->ChangeName(params[0].c_str());
- params[0] = ":" + params[0];
- Utils->DoOneToAllButSender(prefix,"FNAME",params,u->server);
- }
- return true;
-}
-
-bool TreeSocket::Whois(const std::string &prefix, std::deque<std::string> ¶ms)
-{
- if (params.size() < 1)
- return true;
- userrec* u = this->Instance->FindNick(prefix);
- if (u)
- {
- // an incoming request
- if (params.size() == 1)
- {
- userrec* x = this->Instance->FindNick(params[0]);
- if ((x) && (IS_LOCAL(x)))
- {
- userrec* x = this->Instance->FindNick(params[0]);
- char signon[MAXBUF];
- char idle[MAXBUF];
- snprintf(signon,MAXBUF,"%lu",(unsigned long)x->signon);
- snprintf(idle,MAXBUF,"%lu",(unsigned long)abs((x->idle_lastmsg)-Instance->Time(true)));
- std::deque<std::string> par;
- par.push_back(prefix);
- par.push_back(signon);
- par.push_back(idle);
- // ours, we're done, pass it BACK
- Utils->DoOneToOne(params[0],"IDLE",par,u->server);
- }
- else
- {
- // not ours pass it on
- Utils->DoOneToOne(prefix,"IDLE",params,x->server);
- }
- }
- else if (params.size() == 3)
- {
- std::string who_did_the_whois = params[0];
- userrec* who_to_send_to = this->Instance->FindNick(who_did_the_whois);
- if ((who_to_send_to) && (IS_LOCAL(who_to_send_to)))
- {
- // an incoming reply to a whois we sent out
- std::string nick_whoised = prefix;
- unsigned long signon = atoi(params[1].c_str());
- unsigned long idle = atoi(params[2].c_str());
- if ((who_to_send_to) && (IS_LOCAL(who_to_send_to)))
- do_whois(this->Instance,who_to_send_to,u,signon,idle,nick_whoised.c_str());
- }
- else
- {
- // not ours, pass it on
- Utils->DoOneToOne(prefix,"IDLE",params,who_to_send_to->server);
- }
- }
- }
- return true;
-}
-
-bool TreeSocket::Push(const std::string &prefix, std::deque<std::string> ¶ms)
-{
- if (params.size() < 2)
- return true;
- userrec* u = this->Instance->FindNick(params[0]);
- if (!u)
- return true;
- if (IS_LOCAL(u))
- {
- u->Write(params[1]);
- }
- else
- {
- // continue the raw onwards
- params[1] = ":" + params[1];
- Utils->DoOneToOne(prefix,"PUSH",params,u->server);
- }
- return true;
-}
-
-bool TreeSocket::HandleSetTime(const std::string &prefix, std::deque<std::string> ¶ms)
-{
- if (!params.size() || !Utils->EnableTimeSync)
- return true;
-
- bool force = false;
-
- if ((params.size() == 2) && (params[1] == "FORCE"))
- force = true;
-
- time_t rts = atoi(params[0].c_str());
- time_t us = Instance->Time(true);
-
- if (rts == us)
- {
- Utils->DoOneToAllButSender(prefix, "TIMESET", params, prefix);
- }
- else if (force || (rts < us))
- {
- int old = Instance->SetTimeDelta(rts - us);
- Instance->Log(DEBUG, "%s TS (diff %d) from %s applied (old delta was %d)", (force) ? "Forced" : "Lower", rts - us, prefix.c_str(), old);
-
- Utils->DoOneToAllButSender(prefix, "TIMESET", params, prefix);
- }
- else
- {
- Instance->Log(DEBUG, "Higher TS (diff %d) from %s overridden", us - rts, prefix.c_str());
-
- std::deque<std::string> oparams;
- oparams.push_back(ConvToStr(us));
-
- Utils->DoOneToMany(prefix, "TIMESET", oparams);
- }
-
- return true;
-}
-
-bool TreeSocket::Time(const std::string &prefix, std::deque<std::string> ¶ms)
-{
- // :source.server TIME remote.server sendernick
- // :remote.server TIME source.server sendernick TS
- if (params.size() == 2)
- {
- // someone querying our time?
- if (this->Instance->Config->ServerName == params[0])
- {
- userrec* u = this->Instance->FindNick(params[1]);
- if (u)
- {
- params.push_back(ConvToStr(Instance->Time(false)));
- params[0] = prefix;
- Utils->DoOneToOne(this->Instance->Config->ServerName,"TIME",params,params[0]);
- }
- }
- else
- {
- // not us, pass it on
- userrec* u = this->Instance->FindNick(params[1]);
- if (u)
- Utils->DoOneToOne(prefix,"TIME",params,params[0]);
- }
- }
- else if (params.size() == 3)
- {
- // a response to a previous TIME
- userrec* u = this->Instance->FindNick(params[1]);
- if ((u) && (IS_LOCAL(u)))
- {
- time_t rawtime = atol(params[2].c_str());
- struct tm * timeinfo;
- timeinfo = localtime(&rawtime);
- char tms[26];
- snprintf(tms,26,"%s",asctime(timeinfo));
- tms[24] = 0;
- u->WriteServ("391 %s %s :%s",u->nick,prefix.c_str(),tms);
- }
- else
- {
- if (u)
- Utils->DoOneToOne(prefix,"TIME",params,u->server);
- }
- }
- return true;
-}
-
-bool TreeSocket::LocalPing(const std::string &prefix, std::deque<std::string> ¶ms)
-{
- if (params.size() < 1)
- return true;
- if (params.size() == 1)
- {
- std::string stufftobounce = params[0];
- this->WriteLine(std::string(":")+this->Instance->Config->ServerName+" PONG "+stufftobounce);
- return true;
- }
- else
- {
- std::string forwardto = params[1];
- if (forwardto == this->Instance->Config->ServerName)
- {
- // this is a ping for us, send back PONG to the requesting server
- params[1] = params[0];
- params[0] = forwardto;
- Utils->DoOneToOne(forwardto,"PONG",params,params[1]);
- }
- else
- {
- // not for us, pass it on :)
- Utils->DoOneToOne(prefix,"PING",params,forwardto);
- }
- return true;
- }
-}
-
-/** TODO: This creates a total mess of output and needs to really use irc::modestacker.