#include "snote_thread.hpp" #include "irc_parse_thread.hpp" #include "ircmsg.hpp" #include "connection.hpp" #include "c_callback.hpp" #include #include #include #include #include #include #include namespace { struct SnotePattern { SnotePattern(SnoteTag tag, char const* expression, unsigned flags = 0) : tag{tag} , expression{expression} , regex{expression, std::regex_constants::ECMAScript | std::regex_constants::optimize} { } SnoteTag tag; char const* expression; std::regex regex; }; SnotePattern static const patterns[] = { {SnoteTag::ClientConnecting, R"(^Client connecting: ([^ ]+) \(([^@ ]+)@([^) ]+)\) \[(.*)\] \{([^ ]*)\} <([^ ]*)> \[(.*)\]$)"}, {SnoteTag::ClientExiting, R"(^Client exiting: ([^ ]+) \(([^@ ]+)@([^) ]+)\) \[(.*)\] \[(.*)\]$)"}, {SnoteTag::RejectingKlined, R"(^Rejecting K-Lined user ([^ ]+)\[([^@]+)@([^\]]+)\] \[([^\] ]+)\] \((.*)\)$)"}, {SnoteTag::NickChange, R"(^Nick change: From ([^ ]+) to ([^ ]+) \[([^@]+)@([^ ]+)\]$)"}, {SnoteTag::CreateChannel, R"(^([^ ]+) is creating new channel ([^ ]+)$)"}, {SnoteTag::TemporaryKlineExpired, R"(^Temporary K-line for \[([^ ]+)\] expired$)"}, {SnoteTag::PropagatedBanExpired, R"(^Propagated ban for \[([^ ]+)\] expired$)"}, {SnoteTag::DisconnectingKlined, R"(^Disconnecting K-Lined user ([^ ]+)\[([^@]+)@([^ ]+)\] \((.*)\)$)"}, {SnoteTag::NewPropagatedKline, R"(^([^ ]+)!([^ ]+)@([^ ]+)\{([^ ]+)\} added global ([^ ]+) min\. K-Line for \[([^ ]+)\] \[(.*)\]$)"}, {SnoteTag::NewTemporaryKline, R"(^([^ ]+)!([^ ]+)@([^ ]+)\{([^ ]+)\} added temporary ([^ ]+) min\. K-Line for \[([^ ]+)\] \[(.*)\]$)"}, {SnoteTag::LoginAttempts, "^Warning: \x02([^ ]+)\x02 failed login attempts to \x02([^ ]+)\x02\\. Last attempt received from \x02(.+)\x02.*$"}, {SnoteTag::PossibleFlooder, R"(^Possible Flooder ([^ ]+)\[([^ ]+)@[^ ]+\] on ([^ ]+) target: ([^ ]+)$)"}, {SnoteTag::Killed, R"(^Received KILL message for ([^ ]+)!([^ ]+)@([^ ]+)\. From ([^ ]+) Path: ([^ ]+) \((.*)\)$)"}, {SnoteTag::TooManyGlobalConnections, R"(^Too many global connections for ([^ ]+)\[([^ ]+)@([^ ]+)\] \[(.*)\]$)"}, {SnoteTag::SetVhostOnMarkedAccount, "^\x02([^ ]+)\x02 set vhost ([^ ]+) on the \x02MARKED\x02 account ([^ ]+).$"}, }; static auto setup_database() -> hs_database_t* { auto const n = std::size(patterns); std::vector expressions; std::vector flags(n, HS_FLAG_SINGLEMATCH); std::vector ids; expressions.reserve(n); ids.reserve(n); for (std::size_t i = 0; i < n; i++) { expressions.push_back(patterns[i].expression); ids.push_back(i); } hs_database_t* db; hs_compile_error *error; hs_platform_info_t *platform = nullptr; // target current platform switch (hs_compile_multi(expressions.data(), flags.data(), ids.data(), expressions.size(), HS_MODE_BLOCK, platform, &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 db; } } // namespace auto SnoteThread::start(Connection& connection) -> std::shared_ptr { auto thread = std::make_shared(); thread->db_.reset(setup_database()); hs_scratch_t* scratch = nullptr; if (HS_SUCCESS != hs_alloc_scratch(thread->db_.get(), &scratch)) { abort(); } thread->scratch_.reset(scratch); static char const* const prefix = "*** Notice -- "; connection.add_listener([&connection, thread](IrcMsgEvent& event) { auto& args = event.irc.args; if (IrcCommand::NOTICE == event.command && "*" == args[0] && args[1].starts_with(prefix)) { event.handled_ = true; auto const message = args[1].substr(strlen(prefix)); unsigned match_id; auto cb = [&match_id](unsigned id, unsigned long long, unsigned long long, unsigned) -> int { match_id = id; return 1; // stop scanning }; auto const scan_result = hs_scan( thread->db_.get(), message.data(), message.size(), 0, // no flags thread->scratch_.get(), CCallback::invoke, &cb ); switch (scan_result) { case HS_SUCCESS: BOOST_LOG_TRIVIAL(warning) << "Unknown snote: " << message; break; case HS_SCAN_TERMINATED: { auto& pattern = patterns[match_id]; connection.make_event(pattern.tag, pattern.regex, message); break; } default: abort(); } } }); return thread; } auto SnoteEvent::get_tag() const -> SnoteTag { return tag_; } auto SnoteEvent::get_results() -> std::match_results const& { if (auto results = std::get_if<1>(&components_)) { return *results; } auto& regex = std::get<0>(components_).first; auto message = std::get<0>(components_).second; auto& results = components_.emplace<1>(); if (not std::regex_match(message.begin(), message.end(), results, regex)) { // something went wrong - hyperscan disagrees with std::regex abort(); } return results; } auto SnoteThread::DbDeleter::operator()(hs_database_t * db) const -> void { if (HS_SUCCESS != hs_free_database(db)) { abort(); } } auto SnoteThread::ScratchDeleter::operator()(hs_scratch_t * scratch) const -> void { if (HS_SUCCESS != hs_free_scratch(scratch)) { abort(); } }