xbot/driver/web.hpp

101 lines
2.3 KiB
C++
Raw Normal View History

2025-02-01 11:04:33 -08:00
#pragma once
2025-02-01 20:57:57 -08:00
#include <myirc/connection.hpp>
#include <toml++/toml.hpp>
#include <boost/asio.hpp>
#include <boost/signals2.hpp>
#include <memory>
#include <map>
#include <set>
struct ProjectSettings {
// *** Administrative settings ***
// IRC channel to announce to
std::string channel;
// name extracted from notify/$user
std::string credential_name;
// Authorized accounts can edit the event list
std::set<std::string> authorized_accounts;
// *** User settings ***
// Events to announce
std::set<std::string> events;
// Whether to announce events
bool enabled;
auto to_toml() const -> toml::table;
static auto from_toml(const toml::table &v) -> ProjectSettings;
};
struct WebhookSettings {
std::string host;
std::string service;
std::map<std::string, std::string> credentials;
std::map<std::string, ProjectSettings> projects;
auto to_toml() const -> toml::table;
static auto from_toml(const toml::table &v) -> WebhookSettings;
};
struct WebhookEvent {
std::string channel;
std::string message;
};
class GithubWebhook {
// IRC connection to announce on; could be empty
std::shared_ptr<myirc::Connection> connection_;
// Buffered events in case connection was inactive when event was received
std::vector<WebhookEvent> events_;
// Actually write the event to the connection.
// Only call when there is a connection.
auto write_event(WebhookEvent event) -> void;
public:
WebhookSettings settings_;
GithubWebhook(WebhookSettings settings)
: settings_(std::move(settings))
{
}
// Either emit the event now or save it until a connection is set
auto add_event(WebhookEvent event) -> void
{
if (connection_) {
write_event(std::move(event));
} else {
events_.emplace_back(std::move(event));
}
}
auto set_connection(std::shared_ptr<myirc::Connection> connection) -> void
{
connection_ = std::move(connection);
for (auto &&event : events_)
{
write_event(event);
}
events_.clear();
}
auto clear_connection() -> void
{
connection_.reset();
}
};
auto start_webhook(boost::asio::io_context &io, const char *) -> std::shared_ptr<GithubWebhook>;