#include "Void/TeamSpeak/Protocol/Command.hpp" #include #include #include namespace Void::TeamSpeak::Protocol { std::string tsEscape(const std::string &str) { std::string out; out.reserve(str.size()); for (char c : str) { switch (c) { case '\\': out += "\\\\"; break; case '/': out += "\\/"; break; case ' ': out += "\\s"; break; case '|': out += "\\p"; break; case '\f': out += "\\f"; break; case '\n': out += "\\n"; break; case '\r': out += "\\r"; break; case '\t': out += "\\t"; break; case '\v': out += "\\v"; break; default: out.push_back(c); } } return out; } std::string tsUnescape(const std::string &str) { std::string out; out.reserve(str.size()); for (size_t i = 0; i < str.size(); ++i) { if (str[i] == '\\') { if (++i >= str.size()) { throw std::runtime_error("invalid escape sequence"); } switch (str[i]) { case '\\': out.push_back('\\'); break; case '/': out.push_back('/'); break; case 's': out.push_back(' '); break; case 'p': out.push_back('|'); break; case 'f': out.push_back('\f'); break; case 'n': out.push_back('\n'); break; case 'r': out.push_back('\r'); break; case 't': out.push_back('\t'); break; case 'v': out.push_back('\v'); break; default: throw std::runtime_error(std::string("unknown escape: \\") + str[i]); } } else { out.push_back(str[i]); } } return out; } std::string buildCommand(const std::string &name, const CommandParams ¶ms) { std::string cmd = tsEscape(name); for (const auto &[key, value] : params) { cmd += ' '; cmd += key; cmd += '='; cmd += tsEscape(value); } return cmd; } namespace { CommandParams parseGroup(const std::string &part) { CommandParams params; std::istringstream iss(part); std::string token; while (std::getline(iss, token, ' ')) { if (token.empty()) { continue; } auto eq = token.find('='); if (eq != std::string::npos) { params[token.substr(0, eq)] = tsUnescape(token.substr(eq + 1)); } else { params[token] = ""; } } return params; } } // namespace ParsedCommand parseCommand(const std::string &raw) { // Split on '|' into groups. std::vector parts; { std::string cur; for (char c : raw) { if (c == '|') { parts.push_back(cur); cur.clear(); } else { cur.push_back(c); } } parts.push_back(cur); } ParsedCommand out; std::string first{Void::Str::trim(parts[0])}; // Notifications and protocol commands start with a command name, while // ServerQuery-style data replies are bare key=value records. Preserve the // first field of those replies instead of mistaking it for a command name. std::string body; auto sp = first.find(' '); const auto firstToken = first.substr(0, sp); if (firstToken.find('=') != std::string::npos) { body = first; } else if (sp == std::string::npos) { out.name = tsUnescape(first); } else { out.name = tsUnescape(first.substr(0, sp)); body = first.substr(sp + 1); } out.params = parseGroup(body); if (parts.size() > 1) { out.groups.push_back(out.params); for (size_t g = 1; g < parts.size(); ++g) { out.groups.push_back(parseGroup(std::string{Void::Str::trim(parts[g])})); } } return out; } } // namespace Void::TeamSpeak::Protocol