summaryrefslogtreecommitdiff
path: root/src/modules/extra/m_regex_stdlib.cpp
blob: 8e7bd0da25f99375ca7ab2b5304dc44c89805aa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * InspIRCd -- Internet Relay Chat Daemon
 *
 *   Copyright (C) 2012 ChrisTX <chris@rev-crew.info>
 *
 * This file is part of InspIRCd.  InspIRCd is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, version 2.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "inspircd.h"
#include "modules/regex.h"
#include <regex>

/* $CompileFlags: -std=c++11 */

class StdRegex : public Regex
{
	std::regex regexcl;

 public:
	StdRegex(const std::string& rx, std::regex::flag_type fltype) : Regex(rx)
	{
		try{
			regexcl.assign(rx, fltype | std::regex::optimize);
		}
		catch(std::regex_error rxerr)
		{
			throw RegexException(rx, rxerr.what());
		}
	}

	bool Matches(const std::string& text) CXX11_OVERRIDE
	{
		return std::regex_search(text, regexcl);
	}
};

class StdRegexFactory : public RegexFactory
{
 public:
	std::regex::flag_type regextype;
	StdRegexFactory(Module* m) : RegexFactory(m, "regex/stdregex") {}
	Regex* Create(const std::string& expr) CXX11_OVERRIDE
	{
		return new StdRegex(expr, regextype);
	}
};

class ModuleRegexStd : public Module
{
public:
	StdRegexFactory ref;
	ModuleRegexStd() : ref(this)
	{
	}

	Version GetVersion() CXX11_OVERRIDE
	{
		return Version("Regex Provider Module for std::regex", VF_VENDOR);
	}

	void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
	{
		ConfigTag* Conf = ServerInstance->Config->ConfValue("stdregex");
		std::string regextype = Conf->getString("type", "ecmascript");

		if(regextype == "bre")
			ref.regextype = std::regex::basic;
		else if(regextype == "ere")
			ref.regextype = std::regex::extended;
		else if(regextype == "awk")
			ref.regextype = std::regex::awk;
		else if(regextype == "grep")
			ref.regextype = std::regex::grep;
		else if(regextype == "egrep")
			ref.regextype = std::regex::egrep;
		else
		{
			if(regextype != "ecmascript")
				ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Non-existent regex engine '%s' specified. Falling back to ECMAScript.", regextype.c_str());
			ref.regextype = std::regex::ECMAScript;
		}
	}
};

MODULE_INIT(ModuleRegexStd)