diff options
| author | ilotterytea <iltsu@alright.party> | 2025-07-05 18:56:26 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-07-05 18:56:26 +0500 |
| commit | 0194f86ee840b9585fdc9a111b3d9521217b39bf (patch) | |
| tree | c05fea406458c5315457eb174a0bbc7a4c022e7a | |
| parent | e39d90442dfd6fb136cda23b0d013da98fb089e9 (diff) | |
feat: use new IRCMessage parser on WS messages
| -rw-r--r-- | bot/src/irc/client.cpp | 17 | ||||
| -rw-r--r-- | bot/src/irc/message.cpp | 17 | ||||
| -rw-r--r-- | bot/src/irc/message.hpp | 79 |
3 files changed, 31 insertions, 82 deletions
diff --git a/bot/src/irc/client.cpp b/bot/src/irc/client.cpp index 4f8fe6c..108d5c6 100644 --- a/bot/src/irc/client.cpp +++ b/bot/src/irc/client.cpp @@ -63,7 +63,12 @@ void Client::run() { }), line.end()); - std::optional<MessageType> type = define_message_type(line); + std::optional<IRCMessage> m = IRCMessage::from_string(line); + if (!m.has_value()) { + break; + } + + std::optional<MessageType> type = define_message_type(m->command); if (!type.has_value()) { break; @@ -73,7 +78,7 @@ void Client::run() { if (m_type == MessageType::Privmsg) { std::optional<Message<MessageType::Privmsg>> message = - parse_message<MessageType::Privmsg>(line); + parse_message<MessageType::Privmsg>(*m); if (message.has_value()) { this->onPrivmsg(message.value()); @@ -81,9 +86,11 @@ void Client::run() { } else if (m_type == MessageType::Ping) { // as the docs say, the message should be the same as the one // from the ping - std::string response_text = msg->str.substr(4, msg->str.size()); - - this->raw("PONG" + response_text); + std::string text; + if (!m->params.empty()) { + text = " :" + m->params.at(0); + } + this->raw("PONG" + text); } } diff --git a/bot/src/irc/message.cpp b/bot/src/irc/message.cpp index 43590e5..087cb57 100644 --- a/bot/src/irc/message.cpp +++ b/bot/src/irc/message.cpp @@ -6,22 +6,11 @@ namespace bot::irc { std::optional<MessageType> define_message_type(const std::string &msg) { - std::vector<std::string> parts = utils::string::split_text(msg, ' '); - int i; - - if (msg[0] == '@') { - i = 2; - } else if (msg[0] == ':') { - i = 1; - } else { - return std::nullopt; - } - - if (parts[i] == "NOTICE") { + if (msg == "NOTICE") { return MessageType::Notice; - } else if (parts[i] == "PRIVMSG") { + } else if (msg == "PRIVMSG") { return MessageType::Privmsg; - } else if (parts[i] == "PING") { + } else if (msg == "PING") { return MessageType::Ping; } diff --git a/bot/src/irc/message.hpp b/bot/src/irc/message.hpp index ae903ba..f8ce995 100644 --- a/bot/src/irc/message.hpp +++ b/bot/src/irc/message.hpp @@ -48,77 +48,30 @@ namespace bot { }; template <MessageType T> - std::optional<Message<T>> parse_message(const std::string &msg) { - std::vector<std::string> parts = utils::string::split_text(msg, ' '); - - if (T == MessageType::Privmsg) { + std::optional<Message<T>> parse_message(const IRCMessage &msg) { + if (T == MessageType::Privmsg && msg.command == "PRIVMSG") { MessageSender sender; MessageSource source; - Message<MessageType::Privmsg> message; - - std::string tags = parts[0]; - tags = tags.substr(1, tags.length()); - parts.erase(parts.begin()); - - std::string user = parts[0]; - user = user.substr(1, user.length()); - - std::vector<std::string> user_parts = - utils::string::split_text(user, '!'); - - sender.login = user_parts[0]; - - parts.erase(parts.begin(), parts.begin() + 2); - - std::string channel_login = parts[0]; - source.login = channel_login.substr(1, channel_login.length()); - - parts.erase(parts.begin()); - - std::string chat_message = utils::string::join_vector(parts, ' '); - message.message = chat_message.substr(1, chat_message.length()); - - std::vector<std::string> tags_parts = - utils::string::split_text(tags, ';'); - - for (const std::string &tag : tags_parts) { - std::istringstream iss(tag); - std::string key; - std::string value; - - std::getline(iss, key, '='); - std::getline(iss, value); - - if (key == "display-name") { - sender.display_name = value; - } else if (key == "room-id") { - source.id = std::stoi(value); - } else if (key == "user-id") { - sender.id = std::stoi(value); - } else if (key == "badges") { - std::vector<std::string> badges = - utils::string::split_text(value, ','); - - std::map<std::string, int> map; - - for (const auto &badge : badges) { - std::istringstream iss2(badge); - std::string name; - std::string value; - - std::getline(iss2, name, '/'); - std::getline(iss2, value); - - map.insert({name, std::stoi(value)}); - } + sender.login = msg.nick; + sender.display_name = msg.tags.at("display-name"); + sender.id = std::stoi(msg.tags.at("user-id")); + for (const std::string &badge : + utils::string::split_text(msg.tags.at("badges"), ',')) { + auto b = utils::string::split_text_n(badge, "/", 1); + sender.badges.insert_or_assign(b[0], std::stoi(b[1])); + } - sender.badges = map; - } + source.login = msg.params.front(); + if (source.login[0] == '#') { + source.login = source.login.substr(1); } + source.id = std::stoi(msg.tags.at("room-id")); + Message<MessageType::Privmsg> message; message.sender = sender; message.source = source; + message.message = msg.params.at(1); return message; } |
