summaryrefslogtreecommitdiff
path: root/modules/extra/m_regex_tre.cpp
blob: 8cfbd3b18cf64751356c695e79ba4a0a1e24962c (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
/* RequiredLibraries: tre */

#include "module.h"
#include <tre/regex.h>

class TRERegex : public Regex
{
	regex_t regbuf;

 public:
	TRERegex(const Anope::string &expr) : Regex(expr)
	{
		int err = regcomp(&this->regbuf, expr.c_str(), REG_EXTENDED | REG_NOSUB);
		if (err)
		{
			char buf[BUFSIZE];
			regerror(err, &this->regbuf, buf, sizeof(buf));
			regfree(&this->regbuf);
			throw RegexException("Error in regex " + expr + ": " + buf);
		}
	}

	~TRERegex()
	{
		regfree(&this->regbuf);
	}

	bool Matches(const Anope::string &str)
	{
		return regexec(&this->regbuf, str.c_str(), 0, NULL, 0) == 0;
	}
};

class TRERegexProvider : public RegexProvider
{
 public:
	TRERegexProvider(Module *creator) : RegexProvider(creator, "regex/tre") { }

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

class ModuleRegexTRE : public Module
{
	TRERegexProvider tre_regex_provider;

 public:
	ModuleRegexTRE(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, SUPPORTED),
		tre_regex_provider(this)
	{
		this->SetAuthor("Anope");
		this->SetPermanent(true);
	}
};

MODULE_INIT(ModuleRegexTRE)