2012-04-19 20:58:29 +02:00
|
|
|
/*
|
|
|
|
* InspIRCd -- Internet Relay Chat Daemon
|
2008-09-05 17:37:02 +00:00
|
|
|
*
|
2020-01-11 22:02:47 +00:00
|
|
|
* Copyright (C) 2013 Sadie Powell <sadie@witchery.services>
|
|
|
|
* Copyright (C) 2013 Attila Molnar <attilamolnar@hush.com>
|
|
|
|
* Copyright (C) 2012 Robby <robby@chatbelgie.be>
|
|
|
|
* Copyright (C) 2010 Craig Edwards <brain@inspircd.org>
|
|
|
|
* Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
|
2012-04-19 20:58:29 +02:00
|
|
|
* Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
|
|
|
|
* Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
|
2008-09-05 17:37:02 +00:00
|
|
|
*
|
2012-04-19 20:58:29 +02:00
|
|
|
* 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.
|
2008-09-05 17:37:02 +00:00
|
|
|
*
|
2012-04-19 20:58:29 +02:00
|
|
|
* 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/>.
|
2008-09-05 17:37:02 +00:00
|
|
|
*/
|
|
|
|
|
2012-04-19 20:58:29 +02:00
|
|
|
|
2013-04-02 20:12:15 +01:00
|
|
|
#pragma once
|
2008-09-05 17:37:02 +00:00
|
|
|
|
|
|
|
#include "inspircd.h"
|
|
|
|
|
2020-07-28 16:43:59 +01:00
|
|
|
namespace Regex
|
2008-09-05 17:37:02 +00:00
|
|
|
{
|
2020-07-28 16:43:59 +01:00
|
|
|
class Engine;
|
|
|
|
class EngineReference;
|
|
|
|
class Exception;
|
|
|
|
class Pattern;
|
|
|
|
template<typename> class SimpleEngine;
|
2008-09-05 17:37:02 +00:00
|
|
|
|
2020-07-28 16:43:59 +01:00
|
|
|
/** A shared pointer to a regex pattern. */
|
|
|
|
typedef std::shared_ptr<Pattern> PatternPtr;
|
2008-09-05 17:37:02 +00:00
|
|
|
|
2020-07-28 16:43:59 +01:00
|
|
|
/** The options to use when matching a pattern. */
|
|
|
|
enum PatternOptions : uint8_t
|
|
|
|
{
|
|
|
|
/** No special matching options apply. */
|
|
|
|
OPT_NONE = 0,
|
|
|
|
|
|
|
|
/** The pattern is case insensitive. */
|
|
|
|
OPT_CASE_INSENSITIVE = 1,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/** The base class for regular expression engines. */
|
|
|
|
class Regex::Engine
|
|
|
|
: public DataProvider
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
/** Initializes a new instance of the Regex::Engine class.
|
|
|
|
* @param Creator The module which created this instance.
|
|
|
|
* @param Name The name of this regular expression engine.
|
|
|
|
*/
|
|
|
|
Engine(Module* Creator, const std::string& Name)
|
|
|
|
: DataProvider(Creator, "regex/" + Name)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
/** Compiles a regular expression pattern.
|
|
|
|
* @param pattern The pattern to compile.
|
|
|
|
* @param options One or more options to use when matching the pattern.
|
|
|
|
* @return A shared pointer to an instance of the Regex::Pattern class.
|
|
|
|
*/
|
|
|
|
virtual PatternPtr Create(const std::string& pattern, uint8_t options = Regex::OPT_NONE) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**The base class for simple regular expression engines. */
|
|
|
|
template<typename PatternClass>
|
|
|
|
class Regex::SimpleEngine final
|
|
|
|
: public Regex::Engine
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** @copydoc Regex::Engine::Engine */
|
|
|
|
SimpleEngine(Module* Creator, const std::string& Name)
|
|
|
|
: Regex::Engine(Creator, Name)
|
|
|
|
{
|
|
|
|
}
|
2008-09-05 17:37:02 +00:00
|
|
|
|
2020-07-28 16:43:59 +01:00
|
|
|
/** @copydoc Regex::Engine::Create */
|
|
|
|
PatternPtr Create(const std::string& pattern, uint8_t options) override
|
|
|
|
{
|
|
|
|
return std::make_shared<PatternClass>(pattern, options);
|
|
|
|
}
|
|
|
|
};
|
2008-09-05 17:37:02 +00:00
|
|
|
|
2020-07-28 16:43:59 +01:00
|
|
|
/** A dynamic reference to an instance of the Regex::Engine class. */
|
|
|
|
class Regex::EngineReference final
|
|
|
|
: public dynamic_reference_nocheck<Engine>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** Initializes a new instance of the Regex::EngineReference class.
|
|
|
|
* @param Creator The module which created this instance.
|
|
|
|
* @param Name The name of the regular expression engine to reference.
|
|
|
|
*/
|
|
|
|
EngineReference(Module* Creator, const std::string& Name = "")
|
|
|
|
: dynamic_reference_nocheck<Engine>(Creator, Name.empty() ? "regex" : "regex/" + Name)
|
|
|
|
{
|
|
|
|
}
|
2008-09-05 18:58:55 +00:00
|
|
|
|
2020-07-28 16:43:59 +01:00
|
|
|
/** Sets the name of the engine this reference is configured with.
|
|
|
|
* @param engine The name of the engine to refer to.
|
|
|
|
*/
|
|
|
|
void SetEngine(const std::string& engine)
|
2008-09-05 18:58:55 +00:00
|
|
|
{
|
2020-07-28 16:43:59 +01:00
|
|
|
SetProvider(engine.empty() ? "regex" : "regex/" + engine);
|
2008-09-05 18:58:55 +00:00
|
|
|
}
|
2008-09-05 17:37:02 +00:00
|
|
|
};
|
|
|
|
|
2020-07-28 16:43:59 +01:00
|
|
|
/** The exception which is thrown when a regular expression fails to compile. */
|
|
|
|
class Regex::Exception final
|
|
|
|
: public ModuleException
|
2008-09-05 17:37:02 +00:00
|
|
|
{
|
2009-11-16 17:59:06 +00:00
|
|
|
public:
|
2020-07-28 16:43:59 +01:00
|
|
|
/** Initializes a new instance of the Regex::Exception class.
|
|
|
|
* @param regex A regular expression which failed to compile.
|
|
|
|
* @param error The error which occurred whilst compiling the regular expression.
|
|
|
|
*/
|
|
|
|
Exception(const std::string& regex, const std::string& error)
|
|
|
|
: ModuleException("Error in regex '" + regex + "': " + error)
|
|
|
|
{
|
|
|
|
}
|
2009-02-14 21:14:36 +00:00
|
|
|
|
2020-07-28 16:43:59 +01:00
|
|
|
/** Initializes a new instance of the Regex::Exception class.
|
|
|
|
* @param regex A regular expression which failed to compile.
|
|
|
|
* @param error The error which occurred whilst compiling the regular expression.
|
|
|
|
* @param offset The offset at which the errror occurred.
|
|
|
|
*/
|
|
|
|
Exception(const std::string& regex, const std::string& error, int offset)
|
|
|
|
: ModuleException("Error in regex '" + regex + "' at offset " + ConvToStr(offset) + ": " + error)
|
|
|
|
{
|
|
|
|
}
|
2008-09-05 17:37:02 +00:00
|
|
|
};
|
2013-08-27 07:29:13 +01:00
|
|
|
|
2020-07-28 16:43:59 +01:00
|
|
|
/** Represents a compiled regular expression pattern. */
|
|
|
|
class Regex::Pattern
|
2013-08-27 07:29:13 +01:00
|
|
|
{
|
2020-07-28 16:43:59 +01:00
|
|
|
private:
|
|
|
|
/** The options used when matching this pattern. */
|
|
|
|
const uint8_t optionflags;
|
|
|
|
|
|
|
|
/** The pattern as a string. */
|
|
|
|
const std::string patternstr;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/** Initializes a new instance of the Pattern class.
|
|
|
|
* @param Pattern The pattern as a string.
|
|
|
|
* @param Options The options used when matching this pattern.
|
|
|
|
*/
|
|
|
|
Pattern(const std::string& pattern, uint8_t options)
|
|
|
|
: optionflags(options)
|
|
|
|
, patternstr(pattern)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-27 07:29:13 +01:00
|
|
|
public:
|
2020-07-28 16:43:59 +01:00
|
|
|
/** Destroys an instance of the Pattern class. */
|
|
|
|
virtual ~Pattern() = default;
|
|
|
|
|
|
|
|
/** Retrieves the options used when matching this pattern. */
|
|
|
|
uint8_t GetOptions() const { return optionflags; }
|
|
|
|
|
|
|
|
/** Retrieves the pattern as a string. */
|
|
|
|
const std::string& GetPattern() const { return patternstr; }
|
2013-08-27 07:29:13 +01:00
|
|
|
|
2020-07-28 16:43:59 +01:00
|
|
|
/** Attempts to match this pattern against the specified text.
|
|
|
|
* @param text The text to match against.
|
|
|
|
* @return If the text matched the pattern then true; otherwise, false.
|
|
|
|
*/
|
|
|
|
virtual bool IsMatch(const std::string& text) = 0;
|
2013-08-27 07:29:13 +01:00
|
|
|
};
|