anope

- supernets anope source code & configuration
git clone git://git.acid.vegas/anope.git
Log | Files | Refs | Archive | README

m_regex_tre.cpp (1734B)

      1 /*
      2  *
      3  * (C) 2012-2022 Anope Team
      4  * Contact us at team@anope.org
      5  *
      6  * Please read COPYING and README for further details.
      7  */
      8 
      9 /* RequiredLibraries: tre */
     10 
     11 #include "module.h"
     12 #include <tre/regex.h>
     13 
     14 class TRERegex : public Regex
     15 {
     16 	regex_t regbuf;
     17 
     18  public:
     19 	TRERegex(const Anope::string &expr) : Regex(expr)
     20 	{
     21 		int err = regcomp(&this->regbuf, expr.c_str(), REG_EXTENDED | REG_NOSUB);
     22 		if (err)
     23 		{
     24 			char buf[BUFSIZE];
     25 			regerror(err, &this->regbuf, buf, sizeof(buf));
     26 			regfree(&this->regbuf);
     27 			throw RegexException("Error in regex " + expr + ": " + buf);
     28 		}
     29 	}
     30 
     31 	~TRERegex()
     32 	{
     33 		regfree(&this->regbuf);
     34 	}
     35 
     36 	bool Matches(const Anope::string &str)
     37 	{
     38 		return regexec(&this->regbuf, str.c_str(), 0, NULL, 0) == 0;
     39 	}
     40 };
     41 
     42 class TRERegexProvider : public RegexProvider
     43 {
     44  public:
     45 	TRERegexProvider(Module *creator) : RegexProvider(creator, "regex/tre") { }
     46 
     47 	Regex *Compile(const Anope::string &expression) anope_override
     48 	{
     49 		return new TRERegex(expression);
     50 	}
     51 };
     52 
     53 class ModuleRegexTRE : public Module
     54 {
     55 	TRERegexProvider tre_regex_provider;
     56 
     57  public:
     58 	ModuleRegexTRE(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, EXTRA | VENDOR),
     59 		tre_regex_provider(this)
     60 	{
     61 		this->SetPermanent(true);
     62 	}
     63 
     64 	~ModuleRegexTRE()
     65 	{
     66 		for (std::list<XLineManager *>::iterator it = XLineManager::XLineManagers.begin(); it != XLineManager::XLineManagers.end(); ++it)
     67 		{
     68 			XLineManager *xlm = *it;
     69 			const std::vector<XLine *> &xlines = xlm->GetList();
     70 
     71 			for (unsigned int i = 0; i < xlines.size(); ++i)
     72 			{
     73 				XLine *x = xlines[i];
     74 
     75 				if (x->regex && dynamic_cast<TRERegex *>(x->regex))
     76 				{
     77 					delete x->regex;
     78 					x->regex = NULL;
     79 				}
     80 			}
     81 		}
     82 	}
     83 };
     84 
     85 MODULE_INIT(ModuleRegexTRE)