add on_chat layer
This commit is contained in:
parent
7cd92ececb
commit
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));
|
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;
|
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(' ');
|
const auto cmdstart = message.find_first_not_of(' ');
|
||||||
if (cmdstart == message.npos) return;
|
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 oper;
|
||||||
std::string_view account;
|
std::string_view account;
|
||||||
for (auto [key, value] : msg.tags)
|
for (auto [key, value] : chat.tags)
|
||||||
{
|
{
|
||||||
if (key == "account")
|
if (key == "account")
|
||||||
{
|
{
|
||||||
@ -45,8 +46,8 @@ auto Bot::process_command(std::string_view message, const IrcMsg &msg) -> void
|
|||||||
}
|
}
|
||||||
|
|
||||||
sig_command({
|
sig_command({
|
||||||
.source = msg.args[0],
|
.source = chat.source,
|
||||||
.target = msg.args[1],
|
.target = chat.target,
|
||||||
.oper = oper,
|
.oper = oper,
|
||||||
.account = account,
|
.account = account,
|
||||||
.command = command,
|
.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];
|
if (self_->is_my_nick(chat.target))
|
||||||
const auto message = msg.args[1];
|
|
||||||
if (self_->is_my_nick(target))
|
|
||||||
{
|
{
|
||||||
process_command(message, msg);
|
process_command(chat.message, chat);
|
||||||
} else if (self_->is_channel(target)) {
|
}
|
||||||
const auto colon = message.find(':');
|
else if (self_->is_channel(chat.target))
|
||||||
if (colon == message.npos) return;
|
{
|
||||||
if (not self_->is_my_nick(message.substr(0, colon))) return;
|
const auto colon = chat.message.find(':');
|
||||||
process_command(message.substr(colon+1), msg);
|
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_{'!'}
|
, command_prefix_{'!'}
|
||||||
{}
|
{}
|
||||||
|
|
||||||
auto on_ircmsg(IrcCommand, const IrcMsg &) -> void;
|
auto on_chat(const Chat &) -> void;
|
||||||
auto process_command(std::string_view message, const IrcMsg &msg) -> void;
|
auto process_command(std::string_view message, const Chat &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;
|
||||||
|
33
client.cpp
33
client.cpp
@ -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(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]))
|
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))
|
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 Client::start(Connection &connection) -> std::shared_ptr<Client>
|
||||||
{
|
{
|
||||||
auto thread = std::make_shared<Client>(connection);
|
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) {
|
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;
|
||||||
@ -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
|
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 && 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
|
auto Client::is_channel(std::string_view name) const -> bool
|
||||||
|
14
client.hpp
14
client.hpp
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
|
#include <span>
|
||||||
|
|
||||||
struct Connection;
|
struct Connection;
|
||||||
struct IrcMsg;
|
struct IrcMsg;
|
||||||
@ -16,6 +17,15 @@ 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.
|
||||||
*
|
*
|
||||||
@ -34,6 +44,7 @@ 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_;
|
||||||
@ -49,15 +60,18 @@ 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_{"+@"}
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user