This commit is contained in:
Eric Mertens 2025-02-02 15:02:08 -08:00
parent 178d7dfcfe
commit 53771396ca
3 changed files with 27 additions and 27 deletions

View File

@ -63,7 +63,7 @@ auto configure_sasl(const Settings &settings) -> std::unique_ptr<SaslMechanism>
static auto start( static auto start(
boost::asio::io_context &io, boost::asio::io_context &io,
const Settings &settings, const Settings &settings,
std::shared_ptr<GithubWebhook> webhook std::shared_ptr<Webhooks> webhook
) -> void ) -> void
{ {
Ref<X509> tls_cert; Ref<X509> tls_cert;

View File

@ -64,7 +64,7 @@ static auto compute_signature(const std::string_view secret, const std::string_v
} }
static auto process_event( static auto process_event(
std::shared_ptr<GithubWebhook> self, std::shared_ptr<Webhooks> self,
const std::string_view notify_user, const std::string_view notify_user,
const std::string_view event, const std::string_view event,
const boost::json::value &json const boost::json::value &json
@ -102,12 +102,12 @@ static auto process_event(
} }
const auto message = std::string{event} + " on " + full_name; const auto message = std::string{event} + " on " + full_name;
self->add_event({settings.channel, message}); self->send_notice({settings.channel, message});
} }
template <class Body, class Allocator> template <class Body, class Allocator>
static auto handle_request( static auto handle_request(
std::shared_ptr<GithubWebhook> self, std::shared_ptr<Webhooks> self,
http::request<Body, http::basic_fields<Allocator>> &&req http::request<Body, http::basic_fields<Allocator>> &&req
) -> http::message_generator ) -> http::message_generator
{ {
@ -162,7 +162,7 @@ static auto handle_request(
return simple_response(http::status::ok, req.version(), req.keep_alive()); return simple_response(http::status::ok, req.version(), req.keep_alive());
} }
auto read_loop(tcp::socket socket, std::shared_ptr<GithubWebhook> self) -> boost::asio::awaitable<void> auto read_loop(tcp::socket socket, std::shared_ptr<Webhooks> self) -> boost::asio::awaitable<void>
{ {
beast::tcp_stream stream{std::move(socket)}; beast::tcp_stream stream{std::move(socket)};
beast::flat_buffer buffer; beast::flat_buffer buffer;
@ -200,7 +200,7 @@ auto read_loop(tcp::socket socket, std::shared_ptr<GithubWebhook> self) -> boost
auto accept_loop( auto accept_loop(
tcp::acceptor acceptor, tcp::acceptor acceptor,
std::shared_ptr<GithubWebhook> self std::shared_ptr<Webhooks> self
) -> boost::asio::awaitable<void> ) -> boost::asio::awaitable<void>
{ {
for (;;) for (;;)
@ -216,7 +216,7 @@ auto accept_loop(
auto spawn_webhook( auto spawn_webhook(
boost::asio::io_context &io, boost::asio::io_context &io,
const std::shared_ptr<GithubWebhook> webhook const std::shared_ptr<Webhooks> webhook
) -> boost::asio::awaitable<void> ) -> boost::asio::awaitable<void>
{ {
tcp::resolver resolver{io}; tcp::resolver resolver{io};
@ -240,7 +240,7 @@ auto spawn_webhook(
auto start_webhook( auto start_webhook(
boost::asio::io_context &io, boost::asio::io_context &io,
const char *webhook_settings_filename const char *webhook_settings_filename
) -> std::shared_ptr<GithubWebhook> ) -> std::shared_ptr<Webhooks>
{ {
std::ifstream webhook_settings_file{webhook_settings_filename}; std::ifstream webhook_settings_file{webhook_settings_filename};
if (!webhook_settings_file) if (!webhook_settings_file)
@ -252,12 +252,12 @@ auto start_webhook(
auto webhook_settings = toml::parse(webhook_settings_file); auto webhook_settings = toml::parse(webhook_settings_file);
WebhookSettings settings = WebhookSettings::from_toml(webhook_settings); WebhookSettings settings = WebhookSettings::from_toml(webhook_settings);
BOOST_LOG_TRIVIAL(info) << "Webhook settings: " << settings.to_toml(); BOOST_LOG_TRIVIAL(info) << "Webhook settings: " << settings.to_toml();
auto webhook = std::make_shared<GithubWebhook>(std::move(settings), webhook_settings_filename); auto webhook = std::make_shared<Webhooks>(std::move(settings), webhook_settings_filename);
boost::asio::co_spawn(io, spawn_webhook(io, webhook), report_error); boost::asio::co_spawn(io, spawn_webhook(io, webhook), report_error);
return webhook; return webhook;
} }
auto GithubWebhook::save_settings() const -> void auto Webhooks::save_settings() const -> void
{ {
std::ofstream webhook_settings_file{settings_file}; std::ofstream webhook_settings_file{settings_file};
if (!webhook_settings_file) if (!webhook_settings_file)
@ -383,7 +383,7 @@ auto WebhookSettings::to_toml() const -> toml::table
} }
// Either emit the event now or save it until a connection is set // Either emit the event now or save it until a connection is set
auto GithubWebhook::add_event(Notice notice) -> void auto Webhooks::send_notice(Notice notice) -> void
{ {
if (connection_) if (connection_)
{ {
@ -395,7 +395,7 @@ auto GithubWebhook::add_event(Notice notice) -> void
} }
} }
auto GithubWebhook::set_connection(std::shared_ptr<myirc::Connection> connection) -> void auto Webhooks::set_connection(std::shared_ptr<myirc::Connection> connection) -> void
{ {
connection_ = std::move(connection); connection_ = std::move(connection);
for (auto &&event : events_) for (auto &&event : events_)
@ -405,20 +405,20 @@ auto GithubWebhook::set_connection(std::shared_ptr<myirc::Connection> connection
events_.clear(); events_.clear();
} }
auto GithubWebhook::clear_connection() -> void auto Webhooks::clear_connection() -> void
{ {
connection_.reset(); connection_.reset();
} }
static auto reply_to(std::shared_ptr<GithubWebhook> webhooks, const myirc::Bot::Command &cmd, std::string message) -> void static auto reply_to(std::shared_ptr<Webhooks> webhooks, const myirc::Bot::Command &cmd, std::string message) -> void
{ {
if (cmd.target.starts_with("#")) if (cmd.target.starts_with("#"))
{ {
webhooks->add_event({std::string{cmd.target}, std::move(message)}); webhooks->send_notice({std::string{cmd.target}, std::move(message)});
} }
else else
{ {
webhooks->add_event({std::string{cmd.nick()}, std::move(message)}); webhooks->send_notice({std::string{cmd.nick()}, std::move(message)});
} }
} }
@ -427,8 +427,8 @@ static auto authorized_for_project(const ProjectSettings &project, const std::st
return project.authorized_accounts.find(std::string{nick}) != project.authorized_accounts.end(); return project.authorized_accounts.find(std::string{nick}) != project.authorized_accounts.end();
} }
std::map<std::string, void (*)(std::shared_ptr<GithubWebhook>, const myirc::Bot::Command &)> webhook_commands{ std::map<std::string, void (*)(std::shared_ptr<Webhooks>, const myirc::Bot::Command &)> webhook_commands{
{"add-credential", [](std::shared_ptr<GithubWebhook> webhooks, const myirc::Bot::Command &cmd) { {"add-credential", [](std::shared_ptr<Webhooks> webhooks, const myirc::Bot::Command &cmd) {
//if (cmd.oper.empty()) //if (cmd.oper.empty())
//{ //{
// return; // return;
@ -442,7 +442,7 @@ std::map<std::string, void (*)(std::shared_ptr<GithubWebhook>, const myirc::Bot:
reply_to(webhooks, cmd, "Added credential " + name); reply_to(webhooks, cmd, "Added credential " + name);
} }
}}, }},
{"drop-credential", [](std::shared_ptr<GithubWebhook> webhooks, const myirc::Bot::Command &cmd) { {"drop-credential", [](std::shared_ptr<Webhooks> webhooks, const myirc::Bot::Command &cmd) {
//if (cmd.oper.empty()) //if (cmd.oper.empty())
//{ //{
// return; // return;
@ -456,7 +456,7 @@ std::map<std::string, void (*)(std::shared_ptr<GithubWebhook>, const myirc::Bot:
reply_to(webhooks, cmd, "Dropped credential " + name); reply_to(webhooks, cmd, "Dropped credential " + name);
} }
}}, }},
{"enable-project", [](std::shared_ptr<GithubWebhook> webhooks, const myirc::Bot::Command &cmd) { {"enable-project", [](std::shared_ptr<Webhooks> webhooks, const myirc::Bot::Command &cmd) {
std::istringstream iss{std::string{cmd.arguments}}; std::istringstream iss{std::string{cmd.arguments}};
std::string name; std::string name;
if (iss >> name) if (iss >> name)
@ -481,7 +481,7 @@ std::map<std::string, void (*)(std::shared_ptr<GithubWebhook>, const myirc::Bot:
reply_to(webhooks, cmd, "Enabled project " + name); reply_to(webhooks, cmd, "Enabled project " + name);
} }
}}, }},
{"disable-project", [](std::shared_ptr<GithubWebhook> webhooks, const myirc::Bot::Command &cmd) { {"disable-project", [](std::shared_ptr<Webhooks> webhooks, const myirc::Bot::Command &cmd) {
//if (cmd.oper.empty()) //if (cmd.oper.empty())
//{ //{
// return; // return;
@ -493,7 +493,7 @@ std::map<std::string, void (*)(std::shared_ptr<GithubWebhook>, const myirc::Bot:
auto cursor = webhooks->settings_.projects.find(name); auto cursor = webhooks->settings_.projects.find(name);
if (cursor == webhooks->settings_.projects.end()) if (cursor == webhooks->settings_.projects.end())
{ {
webhooks->add_event({std::string{cmd.nick()}, "Unknown project " + name}); webhooks->send_notice({std::string{cmd.nick()}, "Unknown project " + name});
return; return;
} }

View File

@ -47,7 +47,7 @@ struct WebhookSettings {
static auto from_toml(const toml::table &v) -> WebhookSettings; static auto from_toml(const toml::table &v) -> WebhookSettings;
}; };
class GithubWebhook { class Webhooks {
public: public:
struct Notice { struct Notice {
std::string target; std::string target;
@ -66,19 +66,19 @@ private:
public: public:
WebhookSettings settings_; WebhookSettings settings_;
GithubWebhook(WebhookSettings settings, const char * settings_file) Webhooks(WebhookSettings settings, const char * settings_file)
: settings_(std::move(settings)) : settings_(std::move(settings))
, settings_file(settings_file) , settings_file(settings_file)
{ {
} }
// Either emit the event now or save it until a connection is set // Either emit the event now or save it until a connection is set
auto add_event(Notice event) -> void; auto send_notice(Notice event) -> void;
auto set_connection(std::shared_ptr<myirc::Connection> connection) -> void; auto set_connection(std::shared_ptr<myirc::Connection> connection) -> void;
auto clear_connection() -> void; auto clear_connection() -> void;
auto save_settings() const -> void; auto save_settings() const -> void;
}; };
auto start_webhook(boost::asio::io_context &io, const char *) -> std::shared_ptr<GithubWebhook>; auto start_webhook(boost::asio::io_context &io, const char *) -> std::shared_ptr<Webhooks>;
extern std::map<std::string, void(*)(std::shared_ptr<GithubWebhook>, const myirc::Bot::Command &)> webhook_commands; extern std::map<std::string, void(*)(std::shared_ptr<Webhooks>, const myirc::Bot::Command &)> webhook_commands;