diff options
| author | ilotterytea <iltsu@alright.party> | 2024-04-21 16:15:27 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-04-21 16:15:27 +0500 |
| commit | 88bc263e64b62995a57bfb8231bb10c6da6fa090 (patch) | |
| tree | 22db30ab5d1213d00833338f08906993d81b3441 /src/irc | |
| parent | a28e803f43bbf0f263df6ec4100bf0b1f1deb4bf (diff) | |
feat: message pool + raw() method
Diffstat (limited to 'src/irc')
| -rw-r--r-- | src/irc/client.cpp | 25 | ||||
| -rw-r--r-- | src/irc/client.hpp | 6 |
2 files changed, 26 insertions, 5 deletions
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 <ixwebsocket/IXWebSocket.h> #include <string> +#include <vector> #include "message.hpp" @@ -15,6 +16,8 @@ namespace bot { void run(); + void raw(const std::string &raw_message); + template <MessageType T> void on(typename MessageHandler<T>::fn function) { switch (T) { @@ -37,6 +40,9 @@ namespace bot { ix::WebSocket websocket; + bool is_connected = false; + std::vector<std::string> pool; + // Message handlers typename MessageHandler<MessageType::Privmsg>::fn onPrivmsg; }; |
