Compare commits
2 Commits
7cd92ececb
...
e7aba11d05
Author | SHA1 | Date | |
---|---|---|---|
e7aba11d05 | |||
41b1148005 |
35
bot.cpp
35
bot.cpp
@ -6,14 +6,15 @@ auto Bot::start(std::shared_ptr<Client> self) -> std::shared_ptr<Bot>
|
||||
{
|
||||
const auto thread = std::make_shared<Bot>(std::move(self));
|
||||
|
||||
thread->self_->get_connection().sig_ircmsg.connect([thread](const auto cmd, auto &msg) {
|
||||
thread->on_ircmsg(cmd, msg);
|
||||
|
||||
thread->self_->sig_chat.connect([thread](auto &chat) {
|
||||
thread->on_chat(chat);
|
||||
});
|
||||
|
||||
return thread;
|
||||
}
|
||||
|
||||
auto Bot::process_command(std::string_view message, const IrcMsg &msg) -> void
|
||||
auto Bot::process_command(std::string_view message, const Chat &chat) -> void
|
||||
{
|
||||
const auto cmdstart = message.find_first_not_of(' ');
|
||||
if (cmdstart == message.npos) return;
|
||||
@ -32,7 +33,7 @@ auto Bot::process_command(std::string_view message, const IrcMsg &msg) -> void
|
||||
|
||||
std::string_view oper;
|
||||
std::string_view account;
|
||||
for (auto [key, value] : msg.tags)
|
||||
for (auto [key, value] : chat.tags)
|
||||
{
|
||||
if (key == "account")
|
||||
{
|
||||
@ -45,8 +46,8 @@ auto Bot::process_command(std::string_view message, const IrcMsg &msg) -> void
|
||||
}
|
||||
|
||||
sig_command({
|
||||
.source = msg.args[0],
|
||||
.target = msg.args[1],
|
||||
.source = chat.source,
|
||||
.target = chat.target,
|
||||
.oper = oper,
|
||||
.account = account,
|
||||
.command = command,
|
||||
@ -54,20 +55,20 @@ auto Bot::process_command(std::string_view message, const IrcMsg &msg) -> void
|
||||
});
|
||||
}
|
||||
|
||||
auto Bot::on_ircmsg(const IrcCommand cmd, const IrcMsg &msg) -> void
|
||||
auto Bot::on_chat(const Chat &chat) -> void
|
||||
{
|
||||
if (cmd == IrcCommand::PRIVMSG)
|
||||
if (not chat.is_notice)
|
||||
{
|
||||
const auto target = msg.args[0];
|
||||
const auto message = msg.args[1];
|
||||
if (self_->is_my_nick(target))
|
||||
if (self_->is_my_nick(chat.target))
|
||||
{
|
||||
process_command(message, msg);
|
||||
} else if (self_->is_channel(target)) {
|
||||
const auto colon = message.find(':');
|
||||
if (colon == message.npos) return;
|
||||
if (not self_->is_my_nick(message.substr(0, colon))) return;
|
||||
process_command(message.substr(colon+1), msg);
|
||||
process_command(chat.message, chat);
|
||||
}
|
||||
else if (self_->is_channel(chat.target))
|
||||
{
|
||||
const auto colon = chat.message.find(':');
|
||||
if (colon == chat.message.npos) return;
|
||||
if (not self_->is_my_nick(chat.message.substr(0, colon))) return;
|
||||
process_command(chat.message.substr(colon + 1), chat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
4
bot.hpp
4
bot.hpp
@ -28,8 +28,8 @@ struct Bot : std::enable_shared_from_this<Bot>
|
||||
, command_prefix_{'!'}
|
||||
{}
|
||||
|
||||
auto on_ircmsg(IrcCommand, const IrcMsg &) -> void;
|
||||
auto process_command(std::string_view message, const IrcMsg &msg) -> void;
|
||||
auto on_chat(const Chat &) -> void;
|
||||
auto process_command(std::string_view message, const Chat &msg) -> void;
|
||||
static auto start(std::shared_ptr<Client>) -> std::shared_ptr<Bot>;
|
||||
|
||||
auto shutdown() -> void;
|
||||
|
33
client.cpp
33
client.cpp
@ -37,7 +37,7 @@ auto Client::on_join(const IrcMsg &irc) -> void
|
||||
{
|
||||
if (is_my_mask(irc.source))
|
||||
{
|
||||
channels_.insert(std::string{irc.args[0]});
|
||||
channels_.insert(casemap(irc.args[0]));
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,7 +45,7 @@ auto Client::on_kick(const IrcMsg &irc) -> void
|
||||
{
|
||||
if (is_my_nick(irc.args[1]))
|
||||
{
|
||||
channels_.erase(std::string{irc.args[0]});
|
||||
channels_.erase(casemap(irc.args[0]));
|
||||
}
|
||||
}
|
||||
|
||||
@ -53,7 +53,7 @@ auto Client::on_part(const IrcMsg &irc) -> void
|
||||
{
|
||||
if (is_my_mask(irc.source))
|
||||
{
|
||||
channels_.erase(std::string{irc.args[0]});
|
||||
channels_.erase(casemap(irc.args[0]));
|
||||
}
|
||||
}
|
||||
|
||||
@ -118,6 +118,25 @@ 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 thread = std::make_shared<Client>(connection);
|
||||
@ -125,6 +144,12 @@ auto Client::start(Connection &connection) -> std::shared_ptr<Client>
|
||||
connection.sig_ircmsg.connect([thread](auto cmd, auto &msg) {
|
||||
switch (cmd)
|
||||
{
|
||||
case IrcCommand::PRIVMSG:
|
||||
thread->on_chat(false, msg);
|
||||
break;
|
||||
case IrcCommand::NOTICE:
|
||||
thread->on_chat(true, msg);
|
||||
break;
|
||||
case IrcCommand::JOIN:
|
||||
thread->on_join(msg);
|
||||
break;
|
||||
@ -190,7 +215,7 @@ auto Client::is_my_nick(std::string_view nick) const -> bool
|
||||
auto Client::is_my_mask(std::string_view mask) const -> bool
|
||||
{
|
||||
const auto bang = mask.find('!');
|
||||
return bang != std::string_view::npos && nickname_ == mask.substr(0, bang);
|
||||
return bang != std::string_view::npos && is_my_nick(mask.substr(0, bang));
|
||||
}
|
||||
|
||||
auto Client::is_channel(std::string_view name) const -> bool
|
||||
|
14
client.hpp
14
client.hpp
@ -5,6 +5,7 @@
|
||||
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
#include <span>
|
||||
|
||||
struct Connection;
|
||||
struct IrcMsg;
|
||||
@ -16,6 +17,15 @@ enum class Casemap
|
||||
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.
|
||||
*
|
||||
@ -34,6 +44,7 @@ class Client
|
||||
|
||||
Casemap casemap_;
|
||||
std::string channel_prefix_;
|
||||
std::string status_msg_;
|
||||
|
||||
std::unordered_map<std::string, std::string> caps_available_;
|
||||
std::unordered_set<std::string> caps_;
|
||||
@ -49,15 +60,18 @@ class Client
|
||||
auto on_cap(const IrcMsg &irc) -> void;
|
||||
auto on_authenticate(std::string_view) -> void;
|
||||
auto on_registered() -> void;
|
||||
auto on_chat(bool, const IrcMsg &irc) -> void;
|
||||
|
||||
public:
|
||||
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 Chat &)> sig_chat;
|
||||
|
||||
Client(Connection &connection)
|
||||
: connection_{connection}
|
||||
, casemap_{Casemap::Rfc1459}
|
||||
, channel_prefix_{"#&"}
|
||||
, status_msg_{"+@"}
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -224,6 +224,15 @@ auto Connection::send_join(std::string_view channel) -> void
|
||||
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
|
||||
{
|
||||
if (chunk != "+"sv)
|
||||
|
@ -24,12 +24,15 @@ public:
|
||||
|
||||
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;
|
||||
std::string host;
|
||||
std::uint16_t port;
|
||||
|
||||
Ref<X509, X509_up_ref, X509_free> client_cert;
|
||||
Ref<EVP_PKEY, EVP_PKEY_up_ref, EVP_PKEY_free> client_key;
|
||||
X509_Ref client_cert;
|
||||
EVP_PKEY_Ref client_key;
|
||||
std::string verify;
|
||||
std::string sni;
|
||||
|
||||
@ -104,6 +107,8 @@ public:
|
||||
auto send_authenticate(std::string_view message) -> void;
|
||||
auto send_authenticate_encoded(std::string_view message) -> void;
|
||||
auto send_authenticate_abort() -> void;
|
||||
auto send_whois(std::string_view, std::string_view = {}) -> void;
|
||||
|
||||
};
|
||||
|
||||
template <typename... Args>
|
||||
|
80
main.cpp
80
main.cpp
@ -1,21 +1,86 @@
|
||||
#include "bot.hpp"
|
||||
#include "c_callback.hpp"
|
||||
#include "client.hpp"
|
||||
#include "connection.hpp"
|
||||
#include "registration.hpp"
|
||||
#include "settings.hpp"
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/log/trivial.hpp>
|
||||
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/pem.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
#include "bot.hpp"
|
||||
#include "client.hpp"
|
||||
#include "registration.hpp"
|
||||
|
||||
using namespace std::literals;
|
||||
|
||||
auto start(boost::asio::io_context &io, const Settings &settings) -> void
|
||||
static auto log_openssl_errors(const std::string_view prefix) -> 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 client = Client::start(*connection);
|
||||
Registration::start(settings, client);
|
||||
@ -32,6 +97,7 @@ auto start(boost::asio::io_context &io, const Settings &settings) -> void
|
||||
|
||||
client->sig_registered.connect([connection]() {
|
||||
connection->send_join("##glguy"sv);
|
||||
connection->send_whois("bot"sv);
|
||||
});
|
||||
|
||||
connection->sig_disconnect.connect(
|
||||
@ -54,10 +120,12 @@ auto start(boost::asio::io_context &io, const Settings &settings) -> void
|
||||
.host = settings.host,
|
||||
.port = settings.service,
|
||||
.verify = settings.tls_hostname,
|
||||
.client_cert = std::move(cert),
|
||||
.client_key = std::move(key),
|
||||
});
|
||||
}
|
||||
|
||||
auto get_settings() -> Settings
|
||||
static auto get_settings() -> Settings
|
||||
{
|
||||
if (auto config_stream = std::ifstream{"config.toml"})
|
||||
{
|
||||
|
@ -18,6 +18,8 @@ auto Settings::from_stream(std::istream &in) -> Settings
|
||||
.sasl_authzid = config["sasl_authzid"].value_or(std::string{}),
|
||||
.sasl_password = config["sasl_password"].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),
|
||||
};
|
||||
}
|
||||
|
@ -18,6 +18,9 @@ struct Settings
|
||||
std::string sasl_password;
|
||||
|
||||
std::string tls_hostname;
|
||||
std::string tls_certfile;
|
||||
std::string tls_keyfile;
|
||||
|
||||
bool use_tls;
|
||||
|
||||
static auto from_stream(std::istream &in) -> Settings;
|
||||
|
Loading…
x
Reference in New Issue
Block a user