xbot/snote.hpp
2025-01-24 14:48:15 -08:00

72 lines
1.5 KiB
C++

#pragma once
#include "ircmsg.hpp"
#include <memory>
#include <optional>
#include <regex>
#include <string_view>
#include <utility>
#include <variant>
struct hs_database;
struct hs_scratch;
enum class SnoteTag
{
ClientConnecting,
ClientExiting,
RejectingKlined,
NickChange,
CreateChannel,
TemporaryKlineExpired,
PropagatedBanExpired,
DisconnectingKlined,
NewPropagatedKline,
NewTemporaryKline,
LoginAttempts,
PossibleFlooder,
Killed,
TooManyGlobalConnections,
SetVhostOnMarkedAccount,
};
class SnoteMatch
{
SnoteTag tag_;
std::variant<std::pair<std::regex const&, std::string_view>, std::match_results<std::string_view::const_iterator>> components_;
public:
SnoteMatch(SnoteTag tag, std::regex const& regex, std::string_view full)
: tag_{tag}
, components_{std::make_pair(std::ref(regex), full)}
{}
auto get_tag() -> SnoteTag { return tag_; }
auto get_results() -> std::match_results<std::string_view::const_iterator> const&;
};
struct SnoteCore
{
struct DbDeleter
{
auto operator()(hs_database * db) const -> void;
};
struct ScratchDeleter
{
auto operator()(hs_scratch * scratch) const -> void;
};
/// @brief Database of server notice patterns
std::unique_ptr<hs_database, DbDeleter> db_;
/// @brief HyperScan scratch space
std::unique_ptr<hs_scratch, ScratchDeleter> scratch_;
SnoteCore();
auto match(const IrcMsg &msg) -> std::optional<SnoteMatch>;
};
extern SnoteCore snoteCore;