From b9c6c5dbc916a58c6345d49f4ae98680ddb6ca21 Mon Sep 17 00:00:00 2001 From: faumaray Date: Fri, 26 Jun 2026 03:05:38 +0300 Subject: [PATCH] commands: fix result/queue formatting for TeamSpeak markdown MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The reply lists rendered badly: durations written as [0:00] were parsed as Markdown link references (brackets stripped), and titles containing Markdown punctuation broke the surrounding [title](url) link so it showed raw. Add shared escapeMarkdown()/markdownLink() helpers (Command.hpp) and reformat search, play, nowplaying and queue replies to: N. [escaped title](shortUrl) · duration Titles/nicknames are Markdown-escaped so '[', '*', '_', etc. can't break the link; the duration follows a middot separator instead of [..]. Also guard a possible nullopt duration deref in the queue list. Co-Authored-By: Claude Opus 4.8 --- include/echoion/core/commands/Command.hpp | 43 +++++++++++++++++++ .../core/commands/NowPlayingCommand.hpp | 21 ++++----- include/echoion/core/commands/PlayCommand.hpp | 31 ++++++------- .../echoion/core/commands/QueueCommand.hpp | 39 ++++++++--------- .../echoion/core/commands/SearchCommand.hpp | 21 ++++----- 5 files changed, 91 insertions(+), 64 deletions(-) diff --git a/include/echoion/core/commands/Command.hpp b/include/echoion/core/commands/Command.hpp index fa642bd..e57aa9b 100644 --- a/include/echoion/core/commands/Command.hpp +++ b/include/echoion/core/commands/Command.hpp @@ -5,8 +5,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -35,6 +37,47 @@ namespace Echoion::Core return "Utility"; } + // Middot separator (U+00B7) used between a track link and its duration. + inline constexpr std::string_view kDot = "\xc2\xb7"; + + // Escape Markdown punctuation in dynamic text (track titles, etc.) so it + // cannot break an enclosing [text](url) link or apply stray formatting. + inline std::string escapeMarkdown(std::string_view text) + { + std::string out; + out.reserve(text.size() + 8); + for (char c : text) { + switch (c) { + case '\\': + case '`': + case '*': + case '_': + case '[': + case ']': + case '(': + case ')': + case '~': + case '|': + out += '\\'; + [[fallthrough]]; + default: + out += c; + } + } + return out; + } + + // Render `text` as a Markdown link to `url` (escaping the text). When `url` + // is empty/absent the escaped text is returned without a link. + inline std::string markdownLink(std::string_view text, + const std::optional& url) + { + if (url.has_value() && !url->empty()) { + return std::format("[{}]({})", escapeMarkdown(text), *url); + } + return escapeMarkdown(text); + } + /** * @brief Context passed to every command execution. * Wraps all dependencies required to perform an action. diff --git a/include/echoion/core/commands/NowPlayingCommand.hpp b/include/echoion/core/commands/NowPlayingCommand.hpp index c82ec80..87744cb 100644 --- a/include/echoion/core/commands/NowPlayingCommand.hpp +++ b/include/echoion/core/commands/NowPlayingCommand.hpp @@ -44,33 +44,30 @@ namespace Echoion::Core displayText = std::format("{} by {}", trackInfo.title, trackInfo.artist); } - std::string linkText = - trackInfo.shortenedUrl.has_value() && - !trackInfo.shortenedUrl->empty() - ? std::format("[{}]({})", displayText, - *trackInfo.shortenedUrl) - : displayText; - std::string msg = - std::format("\xf0\x9f\x8e\xb5 {}", linkText); + std::string msg = std::format( + "\xf0\x9f\x8e\xb5 {}", + markdownLink(displayText, trackInfo.shortenedUrl)); - // Show current position / total duration + // Show current position / total duration after a middot + // (no [] — TeamSpeak parses it as a link reference). auto playbackState = manager->getPlaybackState(); if (playbackState && playbackState->currentTrack.has_value() && trackInfo.duration.has_value() && trackInfo.duration->count() > 0) { auto position = playbackState->position; - msg += std::format(" [{}/{}]", + msg += std::format(" {} {}/{}", kDot, formatDuration(position), formatDuration(*trackInfo.duration)); } else if (trackInfo.duration.has_value() && trackInfo.duration->count() > 0) { - msg += std::format(" [{}]", + msg += std::format(" {} {}", kDot, formatDuration(*trackInfo.duration)); } if (!track.addedBy.empty()) { - msg += std::format(" (requested by {})", track.addedBy); + msg += std::format(" (requested by {})", + escapeMarkdown(track.addedBy)); } ctx.reply(msg); diff --git a/include/echoion/core/commands/PlayCommand.hpp b/include/echoion/core/commands/PlayCommand.hpp index 4c63f5d..09c9f7b 100644 --- a/include/echoion/core/commands/PlayCommand.hpp +++ b/include/echoion/core/commands/PlayCommand.hpp @@ -98,13 +98,9 @@ namespace Echoion::Core displayText = std::format("{} by {}", track.title, track.artist); } - std::string linkText = - track.shortenedUrl.has_value() && - !track.shortenedUrl->empty() - ? std::format("[{}]({})", displayText, - *track.shortenedUrl) - : displayText; - ctx.reply(std::format("Added to queue: {}", linkText)); + ctx.reply(std::format( + "Added to queue: {}", + markdownLink(displayText, track.shortenedUrl))); return; } @@ -112,10 +108,9 @@ namespace Echoion::Core manager->storePendingSearch(ctx.clientId, searchResult->tracks); - std::string msg = - std::format("\xf0\x9f\x94\x8d Multiple results " - "found. Use {}select :\n", - ctx.prefix); + std::string msg = std::format( + "Multiple results \xe2\x80\x94 use {}select :\n\n", + ctx.prefix); for (size_t i = 0; i < searchResult->tracks.size() && i < 5; ++i) { const auto& track = searchResult->tracks[i]; @@ -124,15 +119,13 @@ namespace Echoion::Core displayText = std::format("{} by {}", track.title, track.artist); } - std::string linkText = - track.shortenedUrl.has_value() && - !track.shortenedUrl->empty() - ? std::format("[{}]({})", displayText, - *track.shortenedUrl) - : displayText; - msg += std::format("{}. {}", i + 1, linkText); + // [escaped title](url) · duration (no [] around the + // duration: TeamSpeak parses [..] as a link reference). + msg += std::format( + "{}. {}", i + 1, + markdownLink(displayText, track.shortenedUrl)); if (track.duration.has_value()) { - msg += std::format(" [{}]", + msg += std::format(" {} {}", kDot, formatDuration(*track.duration)); } msg += "\n"; diff --git a/include/echoion/core/commands/QueueCommand.hpp b/include/echoion/core/commands/QueueCommand.hpp index 29f6870..5987385 100644 --- a/include/echoion/core/commands/QueueCommand.hpp +++ b/include/echoion/core/commands/QueueCommand.hpp @@ -72,18 +72,19 @@ namespace Echoion::Core const auto& track = current->value(); const auto& trackInfo = track.track; - // Build time display: [current/total] or [total] + // Build time display: · current/total or · total + // (no [] — TeamSpeak parses it as a link reference). std::string timeStr; if (trackInfo.duration.has_value() && trackInfo.duration->count() > 0) { if (state) { timeStr = std::format( - " [{}/{}]", + " {} {}/{}", kDot, formatDuration(state->position), formatDuration(*trackInfo.duration)); } else { timeStr = std::format( - " [{}]", + " {} {}", kDot, formatDuration(*trackInfo.duration)); } } @@ -93,13 +94,10 @@ namespace Echoion::Core displayText = std::format( "{} by {}", trackInfo.title, trackInfo.artist); } - std::string linkText = - trackInfo.shortenedUrl.has_value() && - !trackInfo.shortenedUrl->empty() - ? std::format("[{}]({}){}", displayText, - *trackInfo.shortenedUrl, timeStr) - : displayText + timeStr; - msg << std::format("\n**Now Playing:** {}\n", linkText); + msg << std::format( + "\n**Now Playing:** {}{}\n", + markdownLink(displayText, trackInfo.shortenedUrl), + timeStr); } // Queue @@ -119,17 +117,18 @@ namespace Echoion::Core std::format("{} by {}", trackInfo.title, trackInfo.artist); } - std::string linkText = - trackInfo.shortenedUrl.has_value() && - !trackInfo.shortenedUrl->empty() - ? std::format( - "[{}]({}) [{}]", displayText, - *trackInfo.shortenedUrl, - formatDuration(*trackInfo.duration)) - : displayText; - msg << std::format("{}. {}", i + 1, linkText); + msg << std::format( + "{}. {}", i + 1, + markdownLink(displayText, + trackInfo.shortenedUrl)); + if (trackInfo.duration.has_value()) { + msg << std::format( + " {} {}", kDot, + formatDuration(*trackInfo.duration)); + } if (!entry.addedBy.empty()) { - msg << std::format(" (by {})", entry.addedBy); + msg << std::format( + " (by {})", escapeMarkdown(entry.addedBy)); } msg << "\n"; } diff --git a/include/echoion/core/commands/SearchCommand.hpp b/include/echoion/core/commands/SearchCommand.hpp index 238e5e3..e76dc45 100644 --- a/include/echoion/core/commands/SearchCommand.hpp +++ b/include/echoion/core/commands/SearchCommand.hpp @@ -53,29 +53,24 @@ namespace Echoion::Core manager->storePendingSearch(ctx.clientId, result->tracks); std::stringstream msg; - msg << "\xf0\x9f\x94\x8d Search results for \"" << ctx.args - << "\":\n\n"; + msg << "Search results for \"" << ctx.args << "\":\n\n"; for (size_t i = 0; i < result->tracks.size() && i < 5; ++i) { const auto& track = result->tracks[i]; - // Format as [Title by Artist](shortenedUrl) with - // duration std::string displayText = track.title; if (!track.artist.empty()) { displayText = std::format("{} by {}", track.title, track.artist); } - std::string linkText = - track.shortenedUrl.has_value() && - !track.shortenedUrl->empty() - ? std::format("[{}]({})", displayText, - *track.shortenedUrl) - : displayText; - msg << std::format("{}. {}", i + 1, linkText); + // [escaped title](url) · duration (no [] around the + // duration: TeamSpeak parses [..] as a link reference). + msg << std::format( + "{}. {}", i + 1, + markdownLink(displayText, track.shortenedUrl)); if (track.duration.has_value()) { - msg << std::format(" [{}]", - formatDuration(*track.duration)); + msg << " " << kDot << " " + << formatDuration(*track.duration); } msg << "\n"; }