anope

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

m_regex_posix.cpp (1765B)

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