xbot/driver/web.hpp
2025-02-02 15:02:08 -08:00

85 lines
2.1 KiB
C++

#pragma once
#include <myirc/bot.hpp>
#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;
};
class Webhooks {
public:
struct Notice {
std::string target;
std::string message;
};
private:
// 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<Notice> events_;
const char * settings_file;
public:
WebhookSettings settings_;
Webhooks(WebhookSettings settings, const char * settings_file)
: settings_(std::move(settings))
, settings_file(settings_file)
{
}
// Either emit the event now or save it until a connection is set
auto send_notice(Notice event) -> void;
auto set_connection(std::shared_ptr<myirc::Connection> connection) -> void;
auto clear_connection() -> void;
auto save_settings() const -> void;
};
auto start_webhook(boost::asio::io_context &io, const char *) -> std::shared_ptr<Webhooks>;
extern std::map<std::string, void(*)(std::shared_ptr<Webhooks>, const myirc::Bot::Command &)> webhook_commands;