Compare commits

..

No commits in common. "e7aba11d058de140df4e4188ecf83bd6d81287b5" and "7cd92ececb2d389ecea4a208f56dcf909b791465" have entirely different histories.

9 changed files with 31 additions and 158 deletions

35
bot.cpp
View File

@ -6,15 +6,14 @@ auto Bot::start(std::shared_ptr<Client> self) -> std::shared_ptr<Bot>
{ {
const auto thread = std::make_shared<Bot>(std::move(self)); const auto thread = std::make_shared<Bot>(std::move(self));
thread->self_->get_connection().sig_ircmsg.connect([thread](const auto cmd, auto &msg) {
thread->self_->sig_chat.connect([thread](auto &chat) { thread->on_ircmsg(cmd, msg);
thread->on_chat(chat);
}); });
return thread; return thread;
} }
auto Bot::process_command(std::string_view message, const Chat &chat) -> void auto Bot::process_command(std::string_view message, const IrcMsg &msg) -> void
{ {
const auto cmdstart = message.find_first_not_of(' '); const auto cmdstart = message.find_first_not_of(' ');
if (cmdstart == message.npos) return; if (cmdstart == message.npos) return;
@ -33,7 +32,7 @@ auto Bot::process_command(std::string_view message, const Chat &chat) -> void
std::string_view oper; std::string_view oper;
std::string_view account; std::string_view account;
for (auto [key, value] : chat.tags) for (auto [key, value] : msg.tags)
{ {
if (key == "account") if (key == "account")
{ {
@ -46,8 +45,8 @@ auto Bot::process_command(std::string_view message, const Chat &chat) -> void
} }
sig_command({ sig_command({
.source = chat.source, .source = msg.args[0],
.target = chat.target, .target = msg.args[1],
.oper = oper, .oper = oper,
.account = account, .account = account,
.command = command, .command = command,
@ -55,20 +54,20 @@ auto Bot::process_command(std::string_view message, const Chat &chat) -> void
}); });
} }
auto Bot::on_chat(const Chat &chat) -> void auto Bot::on_ircmsg(const IrcCommand cmd, const IrcMsg &msg) -> void
{ {
if (not chat.is_notice) if (cmd == IrcCommand::PRIVMSG)
{ {
if (self_->is_my_nick(chat.target)) const auto target = msg.args[0];
const auto message = msg.args[1];
if (self_->is_my_nick(target))
{ {
process_command(chat.message, chat); process_command(message, msg);
} } else if (self_->is_channel(target)) {
else if (self_->is_channel(chat.target)) const auto colon = message.find(':');
{ if (colon == message.npos) return;
const auto colon = chat.message.find(':'); if (not self_->is_my_nick(message.substr(0, colon))) return;
if (colon == chat.message.npos) return; process_command(message.substr(colon+1), msg);
if (not self_->is_my_nick(chat.message.substr(0, colon))) return;
process_command(chat.message.substr(colon + 1), chat);
} }
} }
} }

View File

@ -28,8 +28,8 @@ struct Bot : std::enable_shared_from_this<Bot>
, command_prefix_{'!'} , command_prefix_{'!'}
{} {}
auto on_chat(const Chat &) -> void; auto on_ircmsg(IrcCommand, const IrcMsg &) -> void;
auto process_command(std::string_view message, const Chat &msg) -> void; auto process_command(std::string_view message, const IrcMsg &msg) -> void;
static auto start(std::shared_ptr<Client>) -> std::shared_ptr<Bot>; static auto start(std::shared_ptr<Client>) -> std::shared_ptr<Bot>;
auto shutdown() -> void; auto shutdown() -> void;

View File

@ -37,7 +37,7 @@ auto Client::on_join(const IrcMsg &irc) -> void
{ {
if (is_my_mask(irc.source)) if (is_my_mask(irc.source))
{ {
channels_.insert(casemap(irc.args[0])); channels_.insert(std::string{irc.args[0]});
} }
} }
@ -45,7 +45,7 @@ auto Client::on_kick(const IrcMsg &irc) -> void
{ {
if (is_my_nick(irc.args[1])) if (is_my_nick(irc.args[1]))
{ {
channels_.erase(casemap(irc.args[0])); channels_.erase(std::string{irc.args[0]});
} }
} }
@ -53,7 +53,7 @@ auto Client::on_part(const IrcMsg &irc) -> void
{ {
if (is_my_mask(irc.source)) if (is_my_mask(irc.source))
{ {
channels_.erase(casemap(irc.args[0])); channels_.erase(std::string{irc.args[0]});
} }
} }
@ -118,25 +118,6 @@ auto Client::on_isupport(const IrcMsg &msg) -> void
} }
} }
auto Client::on_chat(bool notice, const IrcMsg &irc) -> void
{
char status_msg = '\0';
std::string_view target = irc.args[0];
if (not target.empty() && status_msg_.find(target[0]) != std::string::npos)
{
status_msg = target[0];
target = target.substr(1);
}
sig_chat({
.tags = irc.tags,
.is_notice = notice,
.status_msg = '\0',
.source = irc.source,
.target = irc.args[0],
.message = irc.args[1],
});
}
auto Client::start(Connection &connection) -> std::shared_ptr<Client> auto Client::start(Connection &connection) -> std::shared_ptr<Client>
{ {
auto thread = std::make_shared<Client>(connection); auto thread = std::make_shared<Client>(connection);
@ -144,12 +125,6 @@ auto Client::start(Connection &connection) -> std::shared_ptr<Client>
connection.sig_ircmsg.connect([thread](auto cmd, auto &msg) { connection.sig_ircmsg.connect([thread](auto cmd, auto &msg) {
switch (cmd) switch (cmd)
{ {
case IrcCommand::PRIVMSG:
thread->on_chat(false, msg);
break;
case IrcCommand::NOTICE:
thread->on_chat(true, msg);
break;
case IrcCommand::JOIN: case IrcCommand::JOIN:
thread->on_join(msg); thread->on_join(msg);
break; break;
@ -215,7 +190,7 @@ auto Client::is_my_nick(std::string_view nick) const -> bool
auto Client::is_my_mask(std::string_view mask) const -> bool auto Client::is_my_mask(std::string_view mask) const -> bool
{ {
const auto bang = mask.find('!'); const auto bang = mask.find('!');
return bang != std::string_view::npos && is_my_nick(mask.substr(0, bang)); return bang != std::string_view::npos && nickname_ == mask.substr(0, bang);
} }
auto Client::is_channel(std::string_view name) const -> bool auto Client::is_channel(std::string_view name) const -> bool

View File

@ -5,7 +5,6 @@
#include <string> #include <string>
#include <unordered_set> #include <unordered_set>
#include <span>
struct Connection; struct Connection;
struct IrcMsg; struct IrcMsg;
@ -17,15 +16,6 @@ enum class Casemap
Ascii, Ascii,
}; };
struct Chat {
std::span<const irctag> tags;
bool is_notice;
char status_msg;
std::string_view source;
std::string_view target;
std::string_view message;
};
/** /**
* @brief Thread to track this connection's identity, and IRC state. * @brief Thread to track this connection's identity, and IRC state.
* *
@ -44,7 +34,6 @@ class Client
Casemap casemap_; Casemap casemap_;
std::string channel_prefix_; std::string channel_prefix_;
std::string status_msg_;
std::unordered_map<std::string, std::string> caps_available_; std::unordered_map<std::string, std::string> caps_available_;
std::unordered_set<std::string> caps_; std::unordered_set<std::string> caps_;
@ -60,18 +49,15 @@ class Client
auto on_cap(const IrcMsg &irc) -> void; auto on_cap(const IrcMsg &irc) -> void;
auto on_authenticate(std::string_view) -> void; auto on_authenticate(std::string_view) -> void;
auto on_registered() -> void; auto on_registered() -> void;
auto on_chat(bool, const IrcMsg &irc) -> void;
public: public:
boost::signals2::signal<void()> sig_registered; boost::signals2::signal<void()> sig_registered;
boost::signals2::signal<void(const std::unordered_map<std::string, std::string> &)> sig_cap_ls; boost::signals2::signal<void(const std::unordered_map<std::string, std::string> &)> sig_cap_ls;
boost::signals2::signal<void(const Chat &)> sig_chat;
Client(Connection &connection) Client(Connection &connection)
: connection_{connection} : connection_{connection}
, casemap_{Casemap::Rfc1459} , casemap_{Casemap::Rfc1459}
, channel_prefix_{"#&"} , channel_prefix_{"#&"}
, status_msg_{"+@"}
{ {
} }

View File

@ -224,15 +224,6 @@ auto Connection::send_join(std::string_view channel) -> void
write_irc("JOIN", channel); write_irc("JOIN", channel);
} }
auto Connection::send_whois(std::string_view arg1, std::string_view arg2) -> void
{
if (arg2.empty()) {
write_irc("WHOIS", arg1);
} else {
write_irc("WHOIS", arg1, arg2);
}
}
auto Connection::on_authenticate(const std::string_view chunk) -> void auto Connection::on_authenticate(const std::string_view chunk) -> void
{ {
if (chunk != "+"sv) if (chunk != "+"sv)

View File

@ -24,15 +24,12 @@ public:
struct ConnectSettings struct ConnectSettings
{ {
using X509_Ref = Ref<X509, X509_up_ref, X509_free>;
using EVP_PKEY_Ref = Ref<EVP_PKEY, EVP_PKEY_up_ref, EVP_PKEY_free>;
bool tls; bool tls;
std::string host; std::string host;
std::uint16_t port; std::uint16_t port;
X509_Ref client_cert; Ref<X509, X509_up_ref, X509_free> client_cert;
EVP_PKEY_Ref client_key; Ref<EVP_PKEY, EVP_PKEY_up_ref, EVP_PKEY_free> client_key;
std::string verify; std::string verify;
std::string sni; std::string sni;
@ -107,8 +104,6 @@ public:
auto send_authenticate(std::string_view message) -> void; auto send_authenticate(std::string_view message) -> void;
auto send_authenticate_encoded(std::string_view message) -> void; auto send_authenticate_encoded(std::string_view message) -> void;
auto send_authenticate_abort() -> void; auto send_authenticate_abort() -> void;
auto send_whois(std::string_view, std::string_view = {}) -> void;
}; };
template <typename... Args> template <typename... Args>

View File

@ -1,86 +1,21 @@
#include "bot.hpp"
#include "c_callback.hpp"
#include "client.hpp"
#include "connection.hpp" #include "connection.hpp"
#include "registration.hpp"
#include "settings.hpp" #include "settings.hpp"
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/log/trivial.hpp> #include <boost/log/trivial.hpp>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <cstdio>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include "bot.hpp"
#include "client.hpp"
#include "registration.hpp"
using namespace std::literals; using namespace std::literals;
static auto log_openssl_errors(const std::string_view prefix) -> void auto start(boost::asio::io_context &io, const Settings &settings) -> void
{ {
auto err_cb = [prefix](const char *str, size_t len) -> int {
BOOST_LOG_TRIVIAL(error) << prefix << std::string_view{str, len};
return 0;
};
ERR_print_errors_cb(CCallback<decltype(err_cb)>::invoke, &err_cb);
}
static auto cert_from_file(const std::string &filename) -> ConnectSettings::X509_Ref
{
ConnectSettings::X509_Ref cert;
if (const auto fp = fopen(filename.c_str(), "r"))
{
cert = PEM_read_X509(fp, nullptr, nullptr, nullptr);
if (cert.get() == nullptr)
{
log_openssl_errors("Reading certificate: "sv);
}
fclose(fp);
}
else
{
const auto err = strerror(errno);
BOOST_LOG_TRIVIAL(error) << "Opening certificate: " << err;
}
return cert;
}
static auto key_from_file(const std::string &filename) -> ConnectSettings::EVP_PKEY_Ref
{
ConnectSettings::EVP_PKEY_Ref key;
if (const auto fp = fopen(filename.c_str(), "r"))
{
key = PEM_read_PrivateKey(fp, nullptr, nullptr, nullptr);
if (key.get() == nullptr)
{
log_openssl_errors("Reading private key: "sv);
}
fclose(fp);
}
else
{
const auto err = strerror(errno);
BOOST_LOG_TRIVIAL(error) << "Opening private key: " << err;
}
return key;
}
static auto start(boost::asio::io_context &io, const Settings &settings) -> void
{
ConnectSettings::X509_Ref cert;
if (settings.use_tls && not settings.tls_certfile.empty())
{
cert = cert_from_file(settings.tls_certfile);
}
ConnectSettings::EVP_PKEY_Ref key;
if (settings.use_tls && not settings.tls_keyfile.empty())
{
key = key_from_file(settings.tls_keyfile);
}
const auto connection = std::make_shared<Connection>(io); const auto connection = std::make_shared<Connection>(io);
const auto client = Client::start(*connection); const auto client = Client::start(*connection);
Registration::start(settings, client); Registration::start(settings, client);
@ -97,7 +32,6 @@ static auto start(boost::asio::io_context &io, const Settings &settings) -> void
client->sig_registered.connect([connection]() { client->sig_registered.connect([connection]() {
connection->send_join("##glguy"sv); connection->send_join("##glguy"sv);
connection->send_whois("bot"sv);
}); });
connection->sig_disconnect.connect( connection->sig_disconnect.connect(
@ -120,12 +54,10 @@ static auto start(boost::asio::io_context &io, const Settings &settings) -> void
.host = settings.host, .host = settings.host,
.port = settings.service, .port = settings.service,
.verify = settings.tls_hostname, .verify = settings.tls_hostname,
.client_cert = std::move(cert),
.client_key = std::move(key),
}); });
} }
static auto get_settings() -> Settings auto get_settings() -> Settings
{ {
if (auto config_stream = std::ifstream{"config.toml"}) if (auto config_stream = std::ifstream{"config.toml"})
{ {

View File

@ -18,8 +18,6 @@ auto Settings::from_stream(std::istream &in) -> Settings
.sasl_authzid = config["sasl_authzid"].value_or(std::string{}), .sasl_authzid = config["sasl_authzid"].value_or(std::string{}),
.sasl_password = config["sasl_password"].value_or(std::string{}), .sasl_password = config["sasl_password"].value_or(std::string{}),
.tls_hostname = config["tls_hostname"].value_or(std::string{}), .tls_hostname = config["tls_hostname"].value_or(std::string{}),
.tls_certfile = config["tls_certfile"].value_or(std::string{}),
.tls_keyfile = config["tls_keyfile"].value_or(std::string{}),
.use_tls = config["use_tls"].value_or(false), .use_tls = config["use_tls"].value_or(false),
}; };
} }

View File

@ -18,9 +18,6 @@ struct Settings
std::string sasl_password; std::string sasl_password;
std::string tls_hostname; std::string tls_hostname;
std::string tls_certfile;
std::string tls_keyfile;
bool use_tls; bool use_tls;
static auto from_stream(std::istream &in) -> Settings; static auto from_stream(std::istream &in) -> Settings;