anope- supernets anope source code & configuration |
git clone git://git.acid.vegas/anope.git |
Log | Files | Refs | Archive | README |
m_regex_pcre2.cpp (2190B)
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: pcre2-8 */ 10 /* RequiredWindowsLibraries: libpcre2-8 */ 11 12 #include "module.h" 13 14 #define PCRE2_CODE_UNIT_WIDTH 8 15 #include <pcre2.h> 16 17 class PCRERegex : public Regex 18 { 19 pcre2_code *regex; 20 21 public: 22 PCRERegex(const Anope::string &expr) : Regex(expr) 23 { 24 int errcode; 25 PCRE2_SIZE erroffset; 26 this->regex = pcre2_compile(reinterpret_cast<PCRE2_SPTR8>(expr.c_str()), expr.length(), PCRE2_CASELESS, &errcode, &erroffset, NULL); 27 28 if (!this->regex) 29 { 30 PCRE2_UCHAR error[128]; 31 pcre2_get_error_message(errcode, error, sizeof error); 32 throw RegexException("Error in regex " + expr + " at offset " + stringify(erroffset) + ": " + reinterpret_cast<const char*>(error)); 33 } 34 } 35 36 ~PCRERegex() 37 { 38 pcre2_code_free(this->regex); 39 } 40 41 bool Matches(const Anope::string &str) 42 { 43 pcre2_match_data *unused = pcre2_match_data_create_from_pattern(this->regex, NULL); 44 int result = pcre2_match(regex, reinterpret_cast<PCRE2_SPTR8>(str.c_str()), str.length(), 0, 0, unused, NULL); 45 pcre2_match_data_free(unused); 46 return result >= 0; 47 } 48 }; 49 50 class PCRERegexProvider : public RegexProvider 51 { 52 public: 53 PCRERegexProvider(Module *creator) : RegexProvider(creator, "regex/pcre") { } 54 55 Regex *Compile(const Anope::string &expression) anope_override 56 { 57 return new PCRERegex(expression); 58 } 59 }; 60 61 class ModuleRegexPCRE : public Module 62 { 63 PCRERegexProvider pcre_regex_provider; 64 65 public: 66 ModuleRegexPCRE(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, EXTRA | VENDOR), 67 pcre_regex_provider(this) 68 { 69 this->SetPermanent(true); 70 } 71 72 ~ModuleRegexPCRE() 73 { 74 for (std::list<XLineManager *>::iterator it = XLineManager::XLineManagers.begin(); it != XLineManager::XLineManagers.end(); ++it) 75 { 76 XLineManager *xlm = *it; 77 const std::vector<XLine *> &xlines = xlm->GetList(); 78 79 for (unsigned int i = 0; i < xlines.size(); ++i) 80 { 81 XLine *x = xlines[i]; 82 83 if (x->regex && dynamic_cast<PCRERegex *>(x->regex)) 84 { 85 delete x->regex; 86 x->regex = NULL; 87 } 88 } 89 } 90 } 91 }; 92 93 MODULE_INIT(ModuleRegexPCRE)