summaryrefslogtreecommitdiff
path: root/src/service.cpp
blob: cfa9a17c7646cccbe30f708111f69c6ea3d84ca9 (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
/*
 *
 * (C) 2014 Anope Team
 * Contact us at team@anope.org
 *
 * Please read COPYING and README for further details.
 *
 */


#include "services.h"
#include "service.h"

void Service::check()
{
	if (Services || Aliases)
		return;

	Services = new std::map<Anope::string, std::map<Anope::string, Service *> >();
	Aliases = new std::map<Anope::string, std::map<Anope::string, Anope::string> >;
}

Service *Service::FindService(const std::map<Anope::string, Service *> &services, const std::map<Anope::string, Anope::string> *aliases, const Anope::string &n)
{
	std::map<Anope::string, Service *>::const_iterator it = services.find(n);
	if (it != services.end())
		return it->second;

	if (aliases != NULL)
	{
		std::map<Anope::string, Anope::string>::const_iterator it2 = aliases->find(n);
		if (it2 != aliases->end())
			return FindService(services, aliases, it2->second);
	}

	return NULL;
}

Service *Service::FindService(const Anope::string &t, const Anope::string &n)
{
	check();

	std::map<Anope::string, std::map<Anope::string, Service *> >::const_iterator it = (*Services).find(t);
	if (it == (*Services).end())
		return NULL;

	std::map<Anope::string, std::map<Anope::string, Anope::string> >::const_iterator it2 = (*Aliases).find(t);
	if (it2 != (*Aliases).end())
		return FindService(it->second, &it2->second, n);

	return FindService(it->second, NULL, n);
}

std::vector<Anope::string> Service::GetServiceKeys(const Anope::string &t)
{
	check();

	std::vector<Anope::string> keys;
	std::map<Anope::string, std::map<Anope::string, Service *> >::iterator it = (*Services).find(t);
	if (it != (*Services).end())
		for (std::map<Anope::string, Service *>::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
			keys.push_back(it2->first);
	return keys;
}

void Service::AddAlias(const Anope::string &t, const Anope::string &n, const Anope::string &v)
{
	check();

	std::map<Anope::string, Anope::string> &smap = (*Aliases)[t];
	smap[n] = v;
}

void Service::DelAlias(const Anope::string &t, const Anope::string &n)
{
	check();

	std::map<Anope::string, Anope::string> &smap = (*Aliases)[t];
	smap.erase(n);
	if (smap.empty())
		(*Aliases).erase(t);
}

Service::Service(Module *o, const Anope::string &t, const Anope::string &n) : owner(o), type(t), name(n)
{
	this->Register();
}

Service::~Service()
{
	this->Unregister();
}

void Service::Register()
{
	check();

	std::map<Anope::string, Service *> &smap = (*Services)[this->type];
	if (smap.find(this->name) != smap.end())
		throw ModuleException("Service " + this->type + " with name " + this->name + " already exists");
	smap[this->name] = this;

	ReferenceBase::ResetAll();
}

void Service::Unregister()
{
	check();

	std::map<Anope::string, Service *> &smap = (*Services)[this->type];
	smap.erase(this->name);
	if (smap.empty())
		(*Services).erase(this->type);
}

ServiceAlias::ServiceAlias(const Anope::string &type, const Anope::string &from, const Anope::string &to) : t(type), f(from)
{
	Service::AddAlias(type, from, to);
}

ServiceAlias::~ServiceAlias()
{
	Service::DelAlias(t, f);
}