aboutsummaryrefslogtreecommitdiff
/*
 * InspIRCd -- Internet Relay Chat Daemon
 *
 *   Copyright (C) 2023-2026 Sadie Powell <sadie@witchery.services>
 *
 * This file is part of InspIRCd.  InspIRCd is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, version 2.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "inspircd.h"
#include "modules/stats.h"
#include "modules/whois.h"
#include "timeutils.h"

class HelpOp final
	: public SimpleUserMode
{
public:
	std::vector<User*> helpers;

	HelpOp(const WeakModulePtr& mod)
		: SimpleUserMode(mod, "helpop", 'h', true)
	{
	}

	bool OnModeChange(User* source, User* dest, Channel* channel, Modes::Change& change) override
	{
		if (!SimpleUserMode::OnModeChange(source, dest, channel, change))
			return false;

		if (change.adding)
			helpers.push_back(dest);
		else
			std::erase(helpers, dest);

		return true;
	}
};

class ModuleHelpMode final
	: public Module
	, public Stats::EventListener
	, public Whois::EventListener
{
private:
	bool ignorehideoper;
	HelpOp helpop;
	UserModeReference hideoper;
	bool markhelpers;

	// <helpchan:prefix>
	using HelpChanMap = insp::flat_map<std::string, std::vector<ChanModeReference>>;
	HelpChanMap helpchans;

public:
	ModuleHelpMode()
		: Module(VF_VENDOR, "Adds user mode h (helpop) which marks a user as being available for help.")
		, Stats::EventListener(weak_from_this(), 50)
		, Whois::EventListener(weak_from_this())
		, helpop(weak_from_this())
		, hideoper(weak_from_this(), "hideoper")
	{
	}

	void ReadConfig(ConfigStatus& status) override
	{
		HelpChanMap newhelpchans;
		for (const auto& [_, tag] : ServerInstance->Config->ConfTags("helpchan"))
		{
			const auto name = tag->getString("name");
			if (name.empty())
				throw ModuleException(weak_from_this(), "<helpchan:name> must not be empty at " + tag->source.str());

			auto& newhelpchan = newhelpchans[name];
			for (const auto modechr : tag->getString("prefix", "o", 1))
			{
				auto* mh = ServerInstance->Modes.FindPrefixMode(modechr);
				if (mh)
					newhelpchan.push_back(ChanModeReference(weak_from_this(), mh->service_name));
			}
		}
		std::swap(helpchans, newhelpchans);

		const auto& tag = ServerInstance->Config->ConfValue("helpmode");
		ignorehideoper = tag->getBool("ignorehideoper");
		markhelpers = tag->getBool("markhelpers", true);
	}

	ModResult OnStats(Stats::Context& stats) override
	{
		if (stats.GetSymbol() != 'P')
			return MOD_RES_PASSTHRU;

		for (auto* helper : helpop.helpers)
		{
			if (helper->server->IsService())
				continue; // Ignore services.

			if (helper->IsOper() && (!ignorehideoper || !helper->IsModeSet(hideoper)))
				continue; // Ignore opers.

			std::string extra;
			if (helper->IsAway())
			{
				const std::string awayperiod = Duration::ToLongString(ServerInstance->Time() - helper->away->time, true);
				const std::string awaytime = Time::ToString(helper->away->time);

				extra = FMT::format(": away for {} [since {}] ({})", awayperiod, awaytime, helper->away->message);
			}

			auto* lhelper = helper->AsLocal();
			if (lhelper)
			{
				const std::string idleperiod = Duration::ToLongString(ServerInstance->Time() - lhelper->idle_lastmsg, true);
				const std::string idletime = Time::ToString(lhelper->idle_lastmsg);

				extra += FMT::format("{} idle for {} [since {}]",  extra.empty() ? ':' : ',', idleperiod, idletime);
			}

			stats.AddGenericRow(FMT::format("\x02{}\x02{} ({}){}", helper->nick, markhelpers ? " [helper]" : "",
				helper->GetUserHost(), extra));
		}

		// Allow the core to add normal opers.
		return MOD_RES_PASSTHRU;
	}

	ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, PrefixMode::Set& privs, const std::string& keygiven, bool override) override
	{
		if (!user->IsModeSet(helpop))
			return MOD_RES_PASSTHRU;

		for (auto& [helpchan, prefixes] : helpchans)
		{
			if (!InspIRCd::Match(cname, helpchan))
				continue;

			for (auto& prefix : prefixes)
			{
				if (prefix && prefix->IsPrefixMode())
					privs.insert(prefix->IsPrefixMode());
			}
			break;
		}
		return MOD_RES_PASSTHRU;
	}

	void OnUserQuit(User* user, const std::string& message, const std::string& opermessage) override
	{
		if (user->IsModeSet(helpop))
			std::erase(helpop.helpers, user);
	}

	void OnWhois(Whois::Context& whois) override
	{
		if (whois.GetTarget()->IsModeSet(helpop))
			whois.SendLine(RPL_WHOISHELPOP, "is available for help.");
	}
};

MODULE_INIT(ModuleHelpMode)