anope

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

m_regex_pcre.cpp (1794B)

      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: pcre */
     10 /* RequiredWindowsLibraries: libpcre */
     11 
     12 #include "module.h"
     13 #include <pcre.h>
     14 
     15 class PCRERegex : public Regex
     16 {
     17 	pcre *regex;
     18 
     19  public:
     20 	PCRERegex(const Anope::string &expr) : Regex(expr)
     21 	{
     22 		const char *error;
     23 		int erroffset;
     24 		this->regex = pcre_compile(expr.c_str(), PCRE_CASELESS, &error, &erroffset, NULL);
     25 		if (!this->regex)
     26 			throw RegexException("Error in regex " + expr + " at offset " + stringify(erroffset) + ": " + error);
     27 	}
     28 
     29 	~PCRERegex()
     30 	{
     31 		pcre_free(this->regex);
     32 	}
     33 
     34 	bool Matches(const Anope::string &str)
     35 	{
     36 		return pcre_exec(this->regex, NULL, str.c_str(), str.length(), 0, 0, NULL, 0) > -1;
     37 	}
     38 };
     39 
     40 class PCRERegexProvider : public RegexProvider
     41 {
     42  public:
     43 	PCRERegexProvider(Module *creator) : RegexProvider(creator, "regex/pcre") { }
     44 
     45 	Regex *Compile(const Anope::string &expression) anope_override
     46 	{
     47 		return new PCRERegex(expression);
     48 	}
     49 };
     50 
     51 class ModuleRegexPCRE : public Module
     52 {
     53 	PCRERegexProvider pcre_regex_provider;
     54 
     55  public:
     56 	ModuleRegexPCRE(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, EXTRA | VENDOR),
     57 		pcre_regex_provider(this)
     58 	{
     59 		this->SetPermanent(true);
     60 	}
     61 
     62 	~ModuleRegexPCRE()
     63 	{
     64 		for (std::list<XLineManager *>::iterator it = XLineManager::XLineManagers.begin(); it != XLineManager::XLineManagers.end(); ++it)
     65 		{
     66 			XLineManager *xlm = *it;
     67 			const std::vector<XLine *> &xlines = xlm->GetList();
     68 
     69 			for (unsigned int i = 0; i < xlines.size(); ++i)
     70 			{
     71 				XLine *x = xlines[i];
     72 
     73 				if (x->regex && dynamic_cast<PCRERegex *>(x->regex))
     74 				{
     75 					delete x->regex;
     76 					x->regex = NULL;
     77 				}
     78 			}
     79 		}
     80 	}
     81 };
     82 
     83 MODULE_INIT(ModuleRegexPCRE)