summaryrefslogtreecommitdiff
path: root/modules/extra/m_regex_pcre.cpp
blob: 7cbde87db1a82e399316102e13b55f9aa844c074 (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
/* RequiredLibraries: pcre */
/* RequiredWindowsLibraries: libpcre */

#include "module.h"
#include <pcre.h>

class PCRERegex : public Regex
{
	pcre *regex;

 public:
	PCRERegex(const Anope::string &expr) : Regex(expr)
	{
		const char *error;
		int erroffset;
		this->regex = pcre_compile(expr.c_str(), PCRE_CASELESS, &error, &erroffset, NULL);
		if (!this->regex)
			throw RegexException("Error in regex " + expr + " at offset " + stringify(erroffset) + ": " + error); 
	}

	~PCRERegex()
	{
		pcre_free(this->regex);
	}

	bool Matches(const Anope::string &str)
	{
		return pcre_exec(this->regex, NULL, str.c_str(), str.length(), 0, 0, NULL, 0) > -1;
	}
};

class PCRERegexProvider : public RegexProvider
{
 public:
	PCRERegexProvider(Module *creator) : RegexProvider(creator, "regex/pcre") { }

	Regex *Compile(const Anope::string &expression) anope_override
	{
		return new PCRERegex(expression);
	}
};

class ModuleRegexPCRE : public Module
{
	PCRERegexProvider pcre_regex_provider;

 public:
	ModuleRegexPCRE(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, EXTRA | VENDOR),
		pcre_regex_provider(this)
	{
		this->SetPermanent(true);
	}
};

MODULE_INIT(ModuleRegexPCRE)