diff options
Diffstat (limited to 'src/irc')
| -rw-r--r-- | src/irc/message.cpp | 30 | ||||
| -rw-r--r-- | src/irc/message.hpp | 98 |
2 files changed, 128 insertions, 0 deletions
diff --git a/src/irc/message.cpp b/src/irc/message.cpp new file mode 100644 index 0000000..123a0da --- /dev/null +++ b/src/irc/message.cpp @@ -0,0 +1,30 @@ +#include "message.hpp" + +#include <optional> +#include <string> +#include <vector> + +namespace RedpilledBot { + namespace IRC { + std::optional<MessageType> define_message_type(const std::string &msg) { + std::vector<std::string> parts = split_text(msg, ' '); + int i; + + if (msg[0] == '@') { + i = 2; + } else if (msg[0] == ':') { + i = 1; + } else { + return std::nullopt; + } + + if (parts[i] == "NOTICE") { + return MessageType::Notice; + } else if (parts[i] == "PRIVMSG") { + return MessageType::Privmsg; + } + + return std::nullopt; + } + } +} diff --git a/src/irc/message.hpp b/src/irc/message.hpp new file mode 100644 index 0000000..b0ae353 --- /dev/null +++ b/src/irc/message.hpp @@ -0,0 +1,98 @@ +#pragma once + +#include <optional> +#include <sstream> +#include <string> +#include <vector> + +#include "../utils/string.hpp" + +namespace RedpilledBot { + namespace IRC { + enum MessageType { Privmsg, Notice }; + std::optional<MessageType> define_message_type(const std::string &msg); + + struct MessageSender { + std::string login; + std::string display_name; + int id; + + // More fields will be here + }; + + struct MessageSource { + std::string login; + int id; + }; + + template <MessageType T> + struct Message; + + template <> + struct Message<MessageType::Privmsg> { + MessageSender sender; + MessageSource source; + std::string message; + }; + + template <MessageType T> + std::optional<Message<T>> parse_message(const std::string &msg) { + std::vector<std::string> parts = split_text(msg, ' '); + + if (T == MessageType::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 = 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 = join_vector(parts, ' '); + message.message = chat_message.substr(1, chat_message.length()); + + std::vector<std::string> tags_parts = 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); + } + } + + message.sender = sender; + message.source = source; + + return message; + } + + return std::nullopt; + } + + } +} |
