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

#include "services.h"
#include "modules.h"
#include "language.h"
#include "account.h"

#ifdef GETTEXT_FOUND
# include <libintl.h>
#endif

Module::Module(const Anope::string &modname, const Anope::string &, ModType modtype) : name(modname), type(modtype)
{
	this->handle = NULL;
	this->permanent = false;
	this->created = Anope::CurTime;
	this->SetVersion(Anope::Version());

	if (type & VENDOR)
		this->SetAuthor("Anope");
	else
	{
		/* Not vendor implies third */
		type |= THIRD;
		this->SetAuthor("Unknown");
	}

	if (ModuleManager::FindModule(this->name))
		throw CoreException("Module already exists!");
	
	if (Anope::NoThird && type & THIRD)
		throw ModuleException("Third party modules may not be loaded");

	ModuleManager::Modules.push_back(this);

#if GETTEXT_FOUND
	for (unsigned i = 0; i < Language::Languages.size(); ++i)
	{
		/* Remove .UTF-8 or any other suffix */
		Anope::string lang;
		sepstream(Language::Languages[i], '.').GetToken(lang);

		if (Anope::IsFile(Anope::LocaleDir + "/" + lang + "/LC_MESSAGES/" + modname + ".mo"))
		{
			if (!bindtextdomain(this->name.c_str(), Anope::LocaleDir.c_str()))
				Log() << "Error calling bindtextdomain, " << Anope::LastError();
			else
			{
				Log() << "Found language file " << lang << " for " << modname;
				Language::Domains.push_back(modname);
			}
			break;
		}
	}
#endif
}

Module::~Module()
{
	/* Detach all event hooks for this module */
	ModuleManager::DetachAll(this);
	IdentifyRequest::ModuleUnload(this);
	/* Clear any active timers this module has */
	TimerManager::DeleteTimersFor(this);

	std::list<Module *>::iterator it = std::find(ModuleManager::Modules.begin(), ModuleManager::Modules.end(), this);
	if (it != ModuleManager::Modules.end())
		ModuleManager::Modules.erase(it);

#if GETTEXT_FOUND
	std::vector<Anope::string>::iterator dit = std::find(Language::Domains.begin(), Language::Domains.end(), this->name);
	if (dit != Language::Domains.end())
		Language::Domains.erase(dit);
#endif
}

void Module::SetPermanent(bool state)
{
	this->permanent = state;
}

bool Module::GetPermanent() const
{
	return this->permanent;
}

void Module::SetVersion(const Anope::string &nversion)
{
	this->version = nversion;
}

void Module::SetAuthor(const Anope::string &nauthor)
{
	this->author = nauthor;
}

ModuleVersion::ModuleVersion(int maj, int min, int pa) : version_major(maj), version_minor(min), version_patch(pa)
{
}

int ModuleVersion::GetMajor() const
{
	return this->version_major;
}

int ModuleVersion::GetMinor() const
{
	return this->version_minor;
}

int ModuleVersion::GetPatch() const
{
	return this->version_patch;
}