Plugin: chunk long text replies so search/queue results arrive
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
9a61e01fda
commit
a356251c83
1 file changed
+72
-7
+72
-7
@@ -6,8 +6,66 @@
|
||||
#include <cstring>
|
||||
#include <format>
|
||||
#include <teamspeak/public_errors.h>
|
||||
#include <vector>
|
||||
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<std::string> chunkMessage(const std::string& message,
|
||||
std::size_t limit)
|
||||
{
|
||||
std::vector<std::string> 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<unsigned char>(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<anyID>(targetId), nullptr);
|
||||
for (const auto& chunk : chunkMessage(message, kMaxMessageBytes)) {
|
||||
unsigned int error = ts3Functions_->requestSendPrivateTextMsg(
|
||||
serverConnectionHandlerId_, chunk.c_str(),
|
||||
static_cast<anyID>(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 {};
|
||||
|
||||
Reference in new issue
Block a user