Anope IRC Services  Version 2.0
m_regex_tre.cpp
Go to the documentation of this file.
1 /* RequiredLibraries: tre */
2 
3 #include "module.h"
4 #include <tre/regex.h>
5 
6 class TRERegex : public Regex
7 {
8  regex_t regbuf;
9 
10  public:
11  TRERegex(const Anope::string &expr) : Regex(expr)
12  {
13  int err = regcomp(&this->regbuf, expr.c_str(), REG_EXTENDED | REG_NOSUB);
14  if (err)
15  {
16  char buf[BUFSIZE];
17  regerror(err, &this->regbuf, buf, sizeof(buf));
18  regfree(&this->regbuf);
19  throw RegexException("Error in regex " + expr + ": " + buf);
20  }
21  }
22 
24  {
25  regfree(&this->regbuf);
26  }
27 
28  bool Matches(const Anope::string &str)
29  {
30  return regexec(&this->regbuf, str.c_str(), 0, NULL, 0) == 0;
31  }
32 };
33 
35 {
36  public:
37  TRERegexProvider(Module *creator) : RegexProvider(creator, "regex/tre") { }
38 
40  {
41  return new TRERegex(expression);
42  }
43 };
44 
45 class ModuleRegexTRE : public Module
46 {
48 
49  public:
50  ModuleRegexTRE(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, EXTRA | VENDOR),
51  tre_regex_provider(this)
52  {
53  this->SetPermanent(true);
54  }
55 };
56 
#define BUFSIZE
Definition: services.h:18
TRERegexProvider tre_regex_provider
Definition: m_regex_tre.cpp:47
bool Matches(const Anope::string &str)
Definition: m_regex_tre.cpp:28
Definition: regexpr.h:28
regex_t regbuf
Definition: m_regex_tre.cpp:8
#define anope_override
Definition: services.h:56
#define MODULE_INIT(x)
Definition: modules.h:45
TRERegex(const Anope::string &expr)
Definition: m_regex_tre.cpp:11
Regex * Compile(const Anope::string &expression) anope_override
Definition: m_regex_tre.cpp:39
const char * c_str() const
Definition: anope.h:117
void SetPermanent(bool state)
Definition: module.cpp:84
Definition: modules.h:163
TRERegexProvider(Module *creator)
Definition: m_regex_tre.cpp:37
ModuleRegexTRE(const Anope::string &modname, const Anope::string &creator)
Definition: m_regex_tre.cpp:50