// Anope IRC Services <https://www.anope.org/>
//
// Copyright (C) 2003-2026 Anope Contributors
// Copyright (C) 2008-2011 Robin Burchell <w00t@inspircd.org>
//
// 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 "anope.h"
#include "opertype.h"
#include "config.h"

std::vector<Oper *> Oper::opers;

Oper::Oper(const Anope::string &n, OperType *o) : name(n), ot(o)
{
	opers.push_back(this);
}

Oper::~Oper()
{
	auto it = std::find(opers.begin(), opers.end(), this);
	if (it != opers.end())
		opers.erase(it);
}

Oper *Oper::Find(const Anope::string &name)
{
	for (auto *o : opers)
	{
		if (o->name.equals_ci(name))
			return o;
	}

	return NULL;
}

OperType *OperType::Find(const Anope::string &name)
{
	for (auto *ot : Config->MyOperTypes)
	{
		if (ot->GetName() == name)
			return ot;
	}

	return NULL;
}

OperType::OperType(const Anope::string &nname) : name(nname)
{
}

bool OperType::HasCommand(const Anope::string &cmdstr) const
{
	for (const auto &command : this->commands)
	{
		if (!command.find('~') && Anope::Match(cmdstr, command.substr(1)))
			return false;
		else if (Anope::Match(cmdstr, command))
			return true;
	}
	for (auto *ot : this->inheritances)
	{
		if (ot->HasCommand(cmdstr))
			return true;
	}

	return false;
}

bool OperType::HasPriv(const Anope::string &privstr) const
{
	for (const auto &priv : this->privs)
	{
		if (!priv.find('~') && Anope::Match(privstr, priv.substr(1)))
			return false;
		else if (Anope::Match(privstr, priv))
			return true;
	}
	for (auto *ot : this->inheritances)
	{
		if (ot->HasPriv(privstr))
			return true;
	}

	return false;
}

void OperType::AddCommand(const Anope::string &cmdstr)
{
	this->commands.push_back(cmdstr);
}

void OperType::AddPriv(const Anope::string &privstr)
{
	this->privs.push_back(privstr);
}

const Anope::string &OperType::GetName() const
{
	return this->name;
}

void OperType::Inherits(OperType *ot)
{
	if (ot != this)
		this->inheritances.insert(ot);
}

std::list<Anope::string> OperType::GetCommands() const
{
	std::list<Anope::string> cmd_list = this->commands;
	for (auto *ot : this->inheritances)
	{
		for (const auto &cmd : ot->GetCommands())
			cmd_list.push_back(cmd);
	}
	return cmd_list;
}

std::list<Anope::string> OperType::GetPrivs() const
{
	std::list<Anope::string> priv_list = this->privs;
	for (auto *ot : this->inheritances)
	{
		for (const auto &priv : ot->GetPrivs())
			priv_list.push_back(priv);
	}
	return priv_list;
}
