summaryrefslogtreecommitdiff
path: root/src/opertype.cpp
blob: e7708f99f1e51bc6f7a83fc45721f95be85a8f8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 *
 * (C) 2008-2011 Robin Burchell <w00t@inspircd.org>
 * (C) 2008-2023 Anope Team <team@anope.org>
 *
 * Please read COPYING and README for further details.
 */

#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()
{
	std::vector<Oper *>::iterator 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);
}

const 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;
}

const 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;
}