xbot/snote_thread.cpp

139 lines
4.0 KiB
C++
Raw Normal View History

2023-11-25 20:09:20 -08:00
#include "snote_thread.hpp"
#include "irc_parse_thread.hpp"
#include "connection.hpp"
#include <cstring>
2023-11-26 21:16:56 -08:00
#include <cstdlib>
#include <stdexcept>
#include <utility>
#include <regex>
2023-11-25 20:09:20 -08:00
2023-11-26 21:16:56 -08:00
namespace {
2023-11-25 20:09:20 -08:00
2023-11-26 21:16:56 -08:00
struct SnotePattern
2023-11-25 20:09:20 -08:00
{
2023-11-26 21:16:56 -08:00
SnotePattern(SnoteTag tag, char const* expression, unsigned flags = 0)
: tag{tag}
, expression{expression}
, flags{flags}
, regex{expression, std::regex_constants::ECMAScript | std::regex_constants::optimize}
{
}
SnoteTag tag;
char const* expression;
unsigned flags;
std::regex regex;
};
SnotePattern const patterns[] =
{
{SnoteTag::ClientConnecting,
R"(^Client connecting: ([^ ]+) \(([^@ ]+)@([^) ]+)\) \[(.*)\] \{([^ ]*)\} <([^ ]*)> \[(.*)\]$)"},
{SnoteTag::ClientExiting,
R"(^Client exiting: ([^ ]+) \(([^@ ]+)@([^) ]+)\) \[(.*)\] \[(.*)\]$)"},
};
auto setup_database() -> std::unique_ptr<hs_database_t, SnoteThread::DbDeleter>
{
std::vector<char const*> expressions;
std::vector<unsigned> flags;
std::vector<unsigned> ids;
expressions.reserve(std::size(patterns));
flags.reserve(std::size(patterns));
ids.reserve(std::size(patterns));
unsigned id = 0;
for (auto const& pattern : patterns)
{
expressions.push_back(pattern.expression);
flags.push_back(pattern.flags);
ids.push_back(id++);
}
hs_database_t* db;
hs_compile_error *error;
switch (hs_compile_multi(expressions.data(), flags.data(), ids.data(), expressions.size(), HS_MODE_BLOCK, nullptr, &db, &error))
{
case HS_COMPILER_ERROR:
{
std::string msg = error->message;
hs_free_compile_error(error);
throw std::runtime_error{std::move(msg)};
}
case HS_SUCCESS:
break;
default:
abort();
}
return std::unique_ptr<hs_database_t, SnoteThread::DbDeleter>{db};
}
} // namespace
auto SnoteThread::start(Connection& connection) -> std::shared_ptr<SnoteThread>
{
auto thread = std::make_shared<SnoteThread>();
thread->db_ = setup_database();
hs_scratch_t* scratch = nullptr;
if (HS_SUCCESS != hs_alloc_scratch(thread->db_.get(), &scratch))
{
abort();
}
thread->scratch_ = std::unique_ptr<hs_scratch_t, ScratchDeleter>{scratch};
2023-11-25 20:09:20 -08:00
static char const* const prefix = "*** Notice -- ";
2023-11-26 21:16:56 -08:00
connection.add_listener<IrcMsgEvent>([&connection, thread](IrcMsgEvent& event)
2023-11-25 20:09:20 -08:00
{
2023-11-26 15:14:07 -08:00
auto& args = event.irc.args;
if (IrcCommand::NOTICE == event.command
&& "*" == args[0]
&& args[1].starts_with(prefix))
2023-11-25 20:09:20 -08:00
{
event.handled_ = true;
2023-11-26 21:16:56 -08:00
auto message = args[1].substr(strlen(prefix));
unsigned int match_id = -1;
auto const scan_result = hs_scan(thread->db_.get(), message.data(), message.size(), 0, thread->scratch_.get(),
[](unsigned int id, unsigned long long from, unsigned long long to, unsigned int flags, void *context) -> int
{
int* const match_id = static_cast<int*>(context);
*match_id = id;
return 1; // stop scanning
}
, &match_id);
if (scan_result != HS_SUCCESS && scan_result != HS_SCAN_TERMINATED)
{
abort();
}
if (match_id != -1)
{
auto& pattern = patterns[match_id];
std::match_results<std::string_view::const_iterator> results;
if (not std::regex_match(message.begin(), message.end(), results, pattern.regex))
{
// something went wrong - hyperscan disagrees with std::regex
abort();
}
std::vector<std::string_view> parts;
for (auto const sub : results)
{
parts.push_back(std::string_view{sub.first, sub.second});
}
connection.make_event<SnoteEvent>(pattern.tag, std::move(parts));
}
2023-11-25 20:09:20 -08:00
}
});
2023-11-26 21:16:56 -08:00
return thread;
2023-11-25 20:09:20 -08:00
}