From a356251c8387de582646e42a4be2213d32e161b6 Mon Sep 17 00:00:00 2001 From: faumaray Date: Thu, 25 Jun 2026 21:49:52 +0300 Subject: [PATCH] Plugin: chunk long text replies so search/queue results arrive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit requestSendPrivateTextMsg rejects over-long messages, and sendTextMessage ignored its error return — so long replies (e.g. a 5-result search list) were silently dropped while short ones (!help, error messages) worked. Split messages into UTF-8-safe <=900-byte chunks on line boundaries and send each; log the TeamSpeak error code on failure instead of swallowing it. Co-Authored-By: Claude Opus 4.8 --- src/PluginInterface.cpp | 79 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 7 deletions(-) diff --git a/src/PluginInterface.cpp b/src/PluginInterface.cpp index 056751b..2b849fc 100644 --- a/src/PluginInterface.cpp +++ b/src/PluginInterface.cpp @@ -6,8 +6,66 @@ #include #include #include +#include namespace Echoion::Plugin { + namespace + { + // TeamSpeak rejects over-long text messages (long replies such as + // search results or the queue silently never arrive). Split into + // UTF-8-safe chunks on line boundaries so every line is delivered. + constexpr std::size_t kMaxMessageBytes = 900; + + std::vector chunkMessage(const std::string& message, + std::size_t limit) + { + std::vector chunks; + if (message.size() <= limit) { + chunks.push_back(message); + return chunks; + } + + std::string current; + auto flush = [&] { + if (!current.empty()) { + chunks.push_back(current); + current.clear(); + } + }; + + std::size_t pos = 0; + while (pos < message.size()) { + const std::size_t nl = message.find('\n', pos); + std::string line = (nl == std::string::npos) + ? message.substr(pos) + : message.substr(pos, nl - pos + 1); + pos = (nl == std::string::npos) ? message.size() : nl + 1; + + // Hard-split a single over-long line on a UTF-8 boundary. + while (line.size() > limit) { + std::size_t cut = limit; + while (cut > 0 && + (static_cast(line[cut]) & 0xC0) == + 0x80) { + --cut; + } + if (cut == 0) { + cut = limit; // pathological line; avoid an infinite loop + } + flush(); + chunks.push_back(line.substr(0, cut)); + line.erase(0, cut); + } + + if (current.size() + line.size() > limit) { + flush(); + } + current += line; + } + flush(); + return chunks; + } + } // namespace PluginInterface::PluginInterface() = default; @@ -357,14 +415,21 @@ namespace Echoion::Plugin "Plugin interface not initialized"); } - unsigned int error = ts3Functions_->requestSendPrivateTextMsg( - serverConnectionHandlerId_, message.c_str(), - static_cast(targetId), nullptr); + for (const auto& chunk : chunkMessage(message, kMaxMessageBytes)) { + unsigned int error = ts3Functions_->requestSendPrivateTextMsg( + serverConnectionHandlerId_, chunk.c_str(), + static_cast(targetId), nullptr); - if (error != ERROR_ok) { - return Api::fail( - Api::ErrorCode::NetworkError, - std::format("Failed to send text message: {}", error)); + if (error != ERROR_ok) { + Void::Log::warn( + "PluginInterface", "{}", + std::format("requestSendPrivateTextMsg failed (error {}) " + "for a {}-byte chunk", + error, chunk.size())); + return Api::fail( + Api::ErrorCode::NetworkError, + std::format("Failed to send text message: {}", error)); + } } return {};