From 88bc263e64b62995a57bfb8231bb10c6da6fa090 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sun, 21 Apr 2024 16:15:27 +0500 Subject: feat: message pool + raw() method --- src/irc/client.cpp | 25 ++++++++++++++++++++----- src/irc/client.hpp | 6 ++++++ 2 files changed, 26 insertions(+), 5 deletions(-) (limited to 'src/irc') diff --git a/src/irc/client.cpp b/src/irc/client.cpp index e08d243..2604186 100644 --- a/src/irc/client.cpp +++ b/src/irc/client.cpp @@ -63,11 +63,17 @@ void Client::run() { } case ix::WebSocketMessageType::Open: { std::cout << "Connected to Twitch IRC!\n"; + this->is_connected = true; this->authorize(); + for (const auto &msg : this->pool) { + this->websocket.send(msg); + } + this->pool.clear(); break; } case ix::WebSocketMessageType::Close: { std::cout << "Twitch IRC Connection closed!\n"; + this->is_connected = false; break; } default: { @@ -79,6 +85,15 @@ void Client::run() { this->websocket.run(); } +void Client::raw(const std::string &raw_message) { + std::string msg = raw_message + "\r\n"; + if (this->is_connected) { + this->websocket.send(msg); + } else { + this->pool.push_back(msg); + } +} + void Client::authorize() { if (this->username.empty() || this->password.empty()) { std::cout << "Bot username and password must be set!\n"; @@ -87,9 +102,9 @@ void Client::authorize() { std::cout << "Authorizing on Twitch IRC servers...\n"; - this->websocket.send("PASS " + this->password + "\r\n"); - this->websocket.send("NICK " + this->username + "\r\n"); - this->websocket.send("CAP REQ :twitch.tv/membership\r\n"); - this->websocket.send("CAP REQ :twitch.tv/commands\r\n"); - this->websocket.send("CAP REQ :twitch.tv/tags\r\n"); + this->raw("PASS " + this->password); + this->raw("NICK " + this->username); + this->raw("CAP REQ :twitch.tv/membership"); + this->raw("CAP REQ :twitch.tv/commands"); + this->raw("CAP REQ :twitch.tv/tags"); } diff --git a/src/irc/client.hpp b/src/irc/client.hpp index eec233c..0194750 100644 --- a/src/irc/client.hpp +++ b/src/irc/client.hpp @@ -3,6 +3,7 @@ #include #include +#include #include "message.hpp" @@ -15,6 +16,8 @@ namespace bot { void run(); + void raw(const std::string &raw_message); + template void on(typename MessageHandler::fn function) { switch (T) { @@ -37,6 +40,9 @@ namespace bot { ix::WebSocket websocket; + bool is_connected = false; + std::vector pool; + // Message handlers typename MessageHandler::fn onPrivmsg; }; -- cgit v1.2.3