// Anope IRC Services <https://www.anope.org/>
//
// Copyright (C) 2003-2026 Anope Contributors
//
// Anope is free software. You can use, modify, and/or distribute it under the
// terms of version 2 of the GNU General Public License. See docs/LICENSE.txt
// for the complete terms of this license and docs/AUTHORS.txt for a list of
// contributors.
//
// Based on the original code of Epona by Lara
// Based on the original code of Services by Andy Church
//
// SPDX-License-Identifier: GPL-2.0-only

#include "services.h"
#include "modules.h"
#include "users.h"
#include "protocol.h"
#include "config.h"
#include "uplink.h"
#include "opertype.h"
#include "messages.h"
#include "servers.h"
#include "channels.h"
#include "numeric.h"

void Message::Away::Run(MessageSource &source, const std::vector<Anope::string> &params, const Anope::map<Anope::string> &tags)
{
	source.GetUser()->SetAway(params.empty() ? "" : params[0]);
}

void Message::Capab::Run(MessageSource &source, const std::vector<Anope::string> &params, const Anope::map<Anope::string> &tags)
{
	if (params.size() == 1)
	{
		spacesepstream sep(params[0]);
		Anope::string token;
		while (sep.GetToken(token))
			Servers::Capab.insert(token);
	}
	else
	{
		for (const auto &param : params)
			Servers::Capab.insert(param);
	}
}

void Message::Error::Run(MessageSource &source, const std::vector<Anope::string> &params, const Anope::map<Anope::string> &tags)
{
	Anope::QuitReason = "Received an error from the uplink: " + params.back();
	Anope::Quitting = true;
	Log(LOG_TERMINAL) << Anope::QuitReason;
}

Message::Ignore::Ignore(Module *creator, const Anope::string &mname)
	: IRCDMessage(creator, mname, 0)
{
	SetFlag(FLAG_SOFT_LIMIT);
}

void Message::Ignore::Run(MessageSource &source, const std::vector<Anope::string> &params, const Anope::map<Anope::string> &tags)
{
	Log(LOG_DEBUG_3) << "Intentionally ignoring " << name << " message";
}


void Message::Invite::Run(MessageSource &source, const std::vector<Anope::string> &params, const Anope::map<Anope::string> &tags)
{
	User *targ = User::Find(params[0]);
	Channel *c = Channel::Find(params[1]);

	if (!targ || targ->server != Me || !c || c->FindUser(targ))
		return;

	FOREACH_MOD(OnInvite, (source.GetUser(), c, targ));
}

void Message::Join::Run(MessageSource &source, const std::vector<Anope::string> &params, const Anope::map<Anope::string> &tags)
{
	User *user = source.GetUser();
	const Anope::string &channels = params[0];

	Anope::string channel;
	commasepstream sep(channels);

	while (sep.GetToken(channel))
	{
		/* Special case for /join 0 */
		if (channel == "0")
		{
			for (auto it = user->chans.begin(), it_end = user->chans.end(); it != it_end; )
			{
				auto *memb = it->second;
				Channel *c = memb->chan;
				++it;

				FOREACH_MOD(OnPrePartChannel, (user, c));
				memb->chan->DeleteUser(user);
				FOREACH_MOD(OnPartChannel, (user, c, c->name, ""));
			}
			continue;
		}

		std::list<SJoinUser> users;
		users.emplace_back(ChannelStatus(), user);

		Channel *chan = Channel::Find(channel);
		SJoin(source, channel, chan ? chan->created : Anope::CurTime, "", {}, users);
	}
}

void Message::Join::SJoin(MessageSource &source, const Anope::string &chan, time_t ts, const Anope::string &modes, const std::vector<Anope::string> &modeparams, const std::list<SJoinUser> &users)
{
	bool created;
	Channel *c = Channel::FindOrCreate(chan, created, ts ? ts : Anope::CurTime);
	bool keep_their_modes = true;

	if (created)
		c->syncing = true;
	/* Some IRCds do not include a TS */
	else if (!ts)
		;
	/* Our creation time is newer than what the server gave us, so reset the channel to the older time */
	else if (c->created > ts)
	{
		c->created = ts;
		c->Reset();
	}
	/* Their TS is newer, don't accept any modes from them */
	else if (ts > c->created)
		keep_their_modes = false;

	/* Update the modes for the channel */
	if (keep_their_modes && !modes.empty())
		/* If we are syncing, mlock is checked later in Channel::Sync. It is important to not check it here
		 * so that Channel::SetCorrectModes can correctly detect the presence of channel mode +r.
		 */
		c->SetModesInternal(source, modes, modeparams, ts, !c->syncing);

	for (const auto &[status, u] : users)
	{
		keep_their_modes = ts <= c->created; // OnJoinChannel can call modules which can modify this channel's ts

		if (c->FindUser(u))
			continue;

		/* Add the user to the channel */
		c->JoinUser(u, keep_their_modes ? &status : NULL);

		/* Check if the user is allowed to join */
		if (c->CheckKick(u))
			continue;

		/* Set whatever modes the user should have, and remove any that
		 * they aren't allowed to have (secureops etc).
		 */
		c->SetCorrectModes(u, true);

		FOREACH_MOD(OnJoinChannel, (u, c));
	}

	/* Channel is done syncing */
	if (c->syncing)
	{
		/* Sync the channel (mode lock, topic, etc) */
		/* the channel is synced when the netmerge is complete */
		Server *src = source.GetServer() ? source.GetServer() : Me;
		if (src && src->IsSynced())
		{
			c->Sync();

			if (c->CheckDelete())
				c->QueueForDeletion();
		}
	}
}

void Message::Kick::Run(MessageSource &source, const std::vector<Anope::string> &params, const Anope::map<Anope::string> &tags)
{
	const Anope::string &channel = params[0];
	const Anope::string &users = params[1];
	const Anope::string &reason = params.size() > 2 ? params[2] : "";

	Channel *c = Channel::Find(channel);
	if (!c)
		return;

	Anope::string user;
	commasepstream sep(users);

	while (sep.GetToken(user))
		c->KickInternal(source, user, reason);
}

void Message::Kill::Run(MessageSource &source, const std::vector<Anope::string> &params, const Anope::map<Anope::string> &tags)
{
	User *u = User::Find(params[0]);
	BotInfo *bi;

	if (!u)
		return;

	/* Recover if someone kills us. */
	if (u->server == Me && (bi = dynamic_cast<BotInfo *>(u)))
	{
		static time_t last_time = 0;

		if (last_time == Anope::CurTime)
			throw ProtocolException("Kill loop detected. Is Anope U:Lined?");

		last_time = Anope::CurTime;

		bi->OnKill();
	}
	else
		u->KillInternal(source, params[1]);
}

void Message::Mode::Run(MessageSource &source, const std::vector<Anope::string> &params, const Anope::map<Anope::string> &tags)
{
	if (IRCD->IsChannelValid(params[0]))
	{
		Channel *c = Channel::Find(params[0]);

		if (c)
			c->SetModesInternal(source, params[1], { params.begin() + 2, params.end() });
	}
	else
	{
		User *u = User::Find(params[0]);

		if (u)
			u->SetModesInternal(source, params[1], { params.begin() + 2, params.end() });
	}
}

/* XXX We should cache the file somewhere not open/read/close it on every request */
void Message::MOTD::Run(MessageSource &source, const std::vector<Anope::string> &params, const Anope::map<Anope::string> &tags)
{
	Server *s = Server::Find(params[0]);
	if (s != Me)
		return;

	auto motdfile = Anope::ExpandConfig(Config->GetBlock("serverinfo").Get<const Anope::string>("motd"));
	std::ifstream stream(motdfile.str());
	if (!stream.is_open())
	{
		IRCD->SendNumeric(ERR_NOSUCHNICK, source.GetSource(), "- MOTD file not readable!  Please contact your IRC administrator.");
		return;
	}

	IRCD->SendNumeric(RPL_MOTDSTART, source.GetSource(), "- " + s->GetName() + " Message of the Day");
	for (Anope::string line; std::getline(stream, line.str()); )
		IRCD->SendNumeric(RPL_MOTD, source.GetSource(), Anope::Format("- %s", line.c_str()));
	IRCD->SendNumeric(RPL_ENDOFMOTD, source.GetSource(), "End of /MOTD command.");
}

void Message::Notice::Run(MessageSource &source, const std::vector<Anope::string> &params, const Anope::map<Anope::string> &tags)
{
	Anope::string message = params[1];

	User *u = source.GetUser();

	/* ignore channel notices */
	if (!IRCD->IsChannelValid(params[0]))
	{
		BotInfo *bi = BotInfo::Find(params[0]);
		if (!bi)
			return;
		FOREACH_MOD(OnBotNotice, (u, bi, message, tags));
	}
}

void Message::Part::Run(MessageSource &source, const std::vector<Anope::string> &params, const Anope::map<Anope::string> &tags)
{
	User *u = source.GetUser();
	const Anope::string &reason = params.size() > 1 ? params[1] : "";

	Anope::string channel;
	commasepstream sep(params[0]);

	while (sep.GetToken(channel))
	{
		Channel *c = Channel::Find(channel);

		if (!c || !u->FindChannel(c))
			continue;

		Log(u, c, "part") << "Reason: " << (!reason.empty() ? reason : "No reason");
		FOREACH_MOD(OnPrePartChannel, (u, c));
		c->DeleteUser(u);
		FOREACH_MOD(OnPartChannel, (u, c, c->name, !reason.empty() ? reason : ""));
	}
}

void Message::Ping::Run(MessageSource &source, const std::vector<Anope::string> &params, const Anope::map<Anope::string> &tags)
{
	IRCD->SendPong(params.size() > 1 ? params[1] : Me->GetSID(), params[0]);
}

void Message::Privmsg::Run(MessageSource &source, const std::vector<Anope::string> &params, const Anope::map<Anope::string> &tags)
{
	const Anope::string &receiver = params[0];
	Anope::string message = params[1];

	User *u = source.GetUser();

	if (IRCD->IsChannelValid(receiver))
	{
		Channel *c = Channel::Find(receiver);
		if (c)
		{
			FOREACH_MOD(OnPrivmsg, (u, c, message, tags));
		}
	}
	else
	{
		/* If a server is specified (nick@server format), make sure it matches
		 * us, and strip it off. */
		Anope::string botname = receiver;
		size_t s = receiver.find('@');
		bool nick_only = false;
		if (s != Anope::string::npos)
		{
			Anope::string servername(receiver.begin() + s + 1, receiver.end());
			botname = botname.substr(0, s);
			nick_only = true;
			if (!servername.equals_ci(Me->GetName()))
			{
				Log(LOG_DEBUG) << "Received a " << name << " message for " << servername << " which is not " << Me->GetName() << ", ignoring.";
				return;
			}
		}

		BotInfo *bi = BotInfo::Find(botname, nick_only);
		if (bi)
		{
			Anope::string ctcpname, ctcpbody;
			if (Anope::ParseCTCP(message, ctcpname, ctcpbody))
			{
				auto ctcpit = bi->ctcps.find(ctcpname);
				if (ctcpit != bi->ctcps.end())
					ctcpit->second(bi, u, ctcpbody);
				return;
			}

			EventReturn MOD_RESULT;
			FOREACH_RESULT(OnBotPrivmsg, MOD_RESULT, (u, bi, message, tags));
			if (MOD_RESULT == EVENT_STOP)
				return;

			bi->OnMessage(u, message, tags);
		}
	}

	return;
}

void Message::Quit::Run(MessageSource &source, const std::vector<Anope::string> &params, const Anope::map<Anope::string> &tags)
{
	const Anope::string &reason = params[0];
	User *user = source.GetUser();

	Log(user, "quit") << "quit (Reason: " << (!reason.empty() ? reason : "no reason") << ")";

	user->Quit(reason);
}

void Message::SQuit::Run(MessageSource &source, const std::vector<Anope::string> &params, const Anope::map<Anope::string> &tags)
{
	Server *s = Server::Find(params[0]);

	if (!s)
	{
		Log(LOG_DEBUG) << "SQUIT for nonexistent server " << params[0];
		return;
	}

	if (s == Me)
	{
		if (Me->GetLinks().empty())
			return;

		s = Me->GetLinks().front();
	}

	s->Delete(s->GetName() + " " + s->GetUplink()->GetName());
}

void Message::Stats::Run(MessageSource &source, const std::vector<Anope::string> &params, const Anope::map<Anope::string> &tags)
{
	User *u = source.GetUser();

	switch (params[0][0])
	{
		case 'l':
			if (u->HasMode("OPER"))
			{
				IRCD->SendNumeric(RPL_STATSLINKINFO, source.GetSource(), "Server SendBuf SentBytes SentMsgs RecvBuf RecvBytes RecvMsgs ConnTime");
				IRCD->SendNumeric(RPL_STATSLINKINFO, source.GetSource(), Config->Uplinks[Anope::CurrentUplink].host, UplinkSock->WriteBufferLen(),
					TotalWritten, UplinkSock->sent_msgs, UplinkSock->ReadBufferLen(), TotalRead, UplinkSock->recv_msgs, Anope::CurTime - Anope::StartTime);
			}

			IRCD->SendNumeric(RPL_STATSLINKINFO, source.GetSource(), params[0][0], "End of /STATS report.");
			break;
		case 'o':
		case 'O':
			/* Check whether the user is an operator */
			if (!u->HasMode("OPER") && Config->GetBlock("options").Get<bool>("hidestatso"))
				IRCD->SendNumeric(RPL_STATSLINKINFO, source.GetSource(), params[0][0], "End of /STATS report.");
			else
			{
				for (auto *o : Oper::opers)
				{
					const NickAlias *na = NickAlias::Find(o->name);
					if (na)
						IRCD->SendNumeric(RPL_STATSOLINE, source.GetSource(), 'O', '*', '*', o->name, o->ot->GetName().replace_all_cs(" ", "_"), '0');
				}

				IRCD->SendNumeric(RPL_STATSLINKINFO, source.GetSource(), params[0][0], "End of /STATS report.");
			}

			break;
		case 'u':
		{
			long uptime = static_cast<long>(Anope::CurTime - Anope::StartTime);
			IRCD->SendNumeric(RPL_STATSUPTIME, source.GetSource(), Anope::Format("Services up %ld day%s, %02ld:%02ld:%02ld", uptime / 86400, uptime / 86400 == 1 ? "" : "s", (uptime / 3600) % 24, (uptime / 60) % 60, uptime % 60));
			IRCD->SendNumeric(RPL_STATSCONN, source.GetSource(), Anope::Format("Current users: %zu (%zu ops); maximum %zu", UserListByNick.size(), OperCount, MaxUserCount));
			IRCD->SendNumeric(RPL_STATSLINKINFO, source.GetSource(), params[0][0], "End of /STATS report.");
			break;
		} /* case 'u' */

		default:
			IRCD->SendNumeric(RPL_STATSLINKINFO, source.GetSource(), params[0][0], "End of /STATS report.");
	}

	return;
}

void Message::Time::Run(MessageSource &source, const std::vector<Anope::string> &params, const Anope::map<Anope::string> &tags)
{
	const auto *tm = localtime(&Anope::CurTime);
	char timebuf[64];
	strftime(timebuf, sizeof(timebuf), "%A, %d %B %Y @ %H:%M:%S %Z", tm);
	const auto timestr = Anope::Format("%s (%lu)", timebuf, Anope::CurTime);
	IRCD->SendNumeric(RPL_TIME, source.GetSource(), Me->GetName(), timestr);
}

void Message::Topic::Run(MessageSource &source, const std::vector<Anope::string> &params, const Anope::map<Anope::string> &tags)
{
	Channel *c = Channel::Find(params[0]);
	if (c)
		c->ChangeTopicInternal(source.GetUser(), source.GetSource(), params[1], Anope::CurTime);

	return;
}

void Message::Version::Run(MessageSource &source, const std::vector<Anope::string> &params, const Anope::map<Anope::string> &tags)
{
	Module *enc = ModuleManager::FindFirstOf(ENCRYPTION);
	IRCD->SendNumeric(RPL_VERSION, source.GetSource(), "Anope-" + Anope::Version(), Me->GetName(), Anope::Format("%s -(%s) -- %s",
		IRCD->GetProtocolName().c_str(), enc ? enc->name.c_str() : "(none)", Anope::VersionBuildString().c_str()));
}

void Message::Whois::Run(MessageSource &source, const std::vector<Anope::string> &params, const Anope::map<Anope::string> &tags)
{
	User *u = User::Find(params[0]);

	if (u && u->server == Me)
	{
		const BotInfo *bi = BotInfo::Find(u->GetUID());
		IRCD->SendNumeric(RPL_WHOISUSER, source.GetSource(), u->nick, u->GetIdent(), u->host, '*', u->realname);
		if (bi)
			IRCD->SendNumeric(RPL_WHOISREGNICK, source.GetSource(), bi->nick, "is a registered nick");
		IRCD->SendNumeric(RPL_WHOISSERVER, source.GetSource(), u->nick, Me->GetName(), Config->GetBlock("serverinfo").Get<const Anope::string>("description"));
		if (bi)
			IRCD->SendNumeric(RPL_WHOISIDLE, source.GetSource(), bi->nick, Anope::CurTime - bi->lastmsg, bi->signon, "seconds idle, signon time");
		IRCD->SendNumeric(RPL_WHOISOPERATOR, source.GetSource(), u->nick, "is a Network Service");
		IRCD->SendNumeric(RPL_ENDOFWHOIS, source.GetSource(), u->nick, "End of /WHOIS list.");
	}
	else
		IRCD->SendNumeric(ERR_NOSUCHNICK, source.GetSource(), params[0], "No such user.");
}
