xbot/driver/web.hpp

77 lines
1.9 KiB
C++

#pragma once
#include <myirc/bot.hpp>
#include <myirc/client.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 {
// IRC connection to announce on; could be empty
std::shared_ptr<myirc::Client> client_;
const char * settings_filename_;
public:
WebhookSettings settings_;
Webhooks(const char * settings_filename)
: settings_filename_{settings_filename}
{
}
// Either emit the event now or save it until a connection is set
auto send_notice(std::string_view, std::string) -> void;
auto set_client(std::shared_ptr<myirc::Client> client) -> void;
auto clear_client() -> void;
auto save_settings() const -> void;
auto load_settings() -> void;
auto refresh_channels() 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;