diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ccc105..78e7441 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,8 +31,6 @@ pkg_check_modules(OPUS IMPORTED_TARGET opus) file(GLOB VOID_TEAMSPEAK_SOURCES CONFIGURE_DEPENDS "src/*.cpp") add_library(VoidTeamspeak ${VOID_TEAMSPEAK_SOURCES}) add_library(Void::TeamSpeak ALIAS VoidTeamspeak) -# One-release source compatibility for Echoion consumers. -add_library(Echoion::Protocol ALIAS VoidTeamspeak) target_compile_features(VoidTeamspeak PUBLIC cxx_std_26) target_include_directories(VoidTeamspeak PUBLIC diff --git a/README.md b/README.md index 40d6c2d..4ba9eab 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,8 @@ official GUI client or depending on its proprietary ClientLib. The public API follows the concepts of the original TeamSpeak Client SDK while using RAII, strong IDs, chrono durations, spans, typed events, and -`Void::Result`. The compatibility implementation currently lives below the -modern facade and will be removed after the Echoion migration release. +`Void::Result`. The lower-level implementation lives under +`Void::TeamSpeak::Protocol`; application code should use the owning facade. ## Public API diff --git a/SDK_PARITY.md b/SDK_PARITY.md index 68200b2..3ccf39b 100644 --- a/SDK_PARITY.md +++ b/SDK_PARITY.md @@ -17,9 +17,9 @@ does not reproduce the proprietary C ABI. | Independent voice timelines | Implemented | one `Client` per timeline | | Voice receive callback | Implemented | `Events::voice` | | File download | Implemented | `download` | -| Typed server/channel/client notifications | Partial | typed command event plus raw request fallback | -| Permissions and channel groups | Partial | raw request fallback | -| File upload/delete | Planned | raw request fallback | +| Typed server/channel/client notifications | Partial | common client/channel/text events typed; batched command fallback | +| Permissions and channel groups | Implemented | typed lists and channel-group assignment | +| File upload/delete | Implemented | `upload`, `deleteFile` | | Audio preprocessing/device GUI | Not applicable | client-local SDK behavior | | GUI contacts, hotkeys, overlays | Not applicable | client-local SDK behavior | diff --git a/include/Void/TeamSpeak/Client.hpp b/include/Void/TeamSpeak/Client.hpp index 3ea78fb..f6412b3 100644 --- a/include/Void/TeamSpeak/Client.hpp +++ b/include/Void/TeamSpeak/Client.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -27,10 +28,15 @@ namespace Void::TeamSpeak }; struct CommandResponse { - using Record = std::map; + using Record = CommandFields; std::vector records; }; + struct CommandEvent { + std::string name; + std::vector records; + }; + struct Events { std::function connected; std::function disconnected; @@ -38,6 +44,12 @@ namespace Void::TeamSpeak std::function debug; std::function&)> command; + std::function commandBatch; + std::function clientEntered; + std::function clientLeft; + std::function clientMoved; + std::function textMessage; + std::function channelChanged; std::function)> voice; }; @@ -69,10 +81,19 @@ namespace Void::TeamSpeak [[nodiscard]] Void::Result::Result, Error> serverGroups( std::chrono::milliseconds timeout = std::chrono::seconds{3}); + [[nodiscard]] Void::Result::Result, Error> + channelGroups( + std::chrono::milliseconds timeout = std::chrono::seconds{3}); + [[nodiscard]] Void::Result::Result, Error> + permissions( + std::chrono::milliseconds timeout = std::chrono::seconds{3}); [[nodiscard]] Void::Result::Result addToServerGroup(ServerGroupId group, ClientDatabaseId client); [[nodiscard]] Void::Result::Result removeFromServerGroup(ServerGroupId group, ClientDatabaseId client); + [[nodiscard]] Void::Result::Result + setChannelGroup(ChannelGroupId group, ChannelId channel, + ClientDatabaseId client); [[nodiscard]] Void::Result::Result sendText(TextTarget target, std::string_view message); [[nodiscard]] Void::Result::Result @@ -85,6 +106,14 @@ namespace Void::TeamSpeak ChannelId channel = {}, std::string_view channelPassword = {}, std::chrono::milliseconds timeout = std::chrono::seconds{10}); + [[nodiscard]] Void::Result::Result + upload(const std::filesystem::path& source, + std::string_view remotePath, ChannelId channel = {}, + std::string_view channelPassword = {}, bool overwrite = false, + std::chrono::milliseconds timeout = std::chrono::seconds{10}); + [[nodiscard]] Void::Result::Result + deleteFile(std::string_view remotePath, ChannelId channel = {}, + std::string_view channelPassword = {}); [[nodiscard]] Void::Result::Result sendVoice(std::span opusFrame); diff --git a/include/Void/TeamSpeak/Command.hpp b/include/Void/TeamSpeak/Command.hpp new file mode 100644 index 0000000..5d0eeef --- /dev/null +++ b/include/Void/TeamSpeak/Command.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include +#include +#include + +namespace Void::TeamSpeak +{ + using CommandFields = std::map; + + // Build a TeamSpeak command with protocol-safe escaping for every value. + [[nodiscard]] std::string buildCommand( + std::string_view name, const CommandFields& fields = {}); +} diff --git a/include/Void/TeamSpeak/Identity.hpp b/include/Void/TeamSpeak/Identity.hpp index b9c5ba5..e44e622 100644 --- a/include/Void/TeamSpeak/Identity.hpp +++ b/include/Void/TeamSpeak/Identity.hpp @@ -1,10 +1,10 @@ #pragma once -#include +#include namespace Void::TeamSpeak { // Modern public name; the implementation remains source-compatible during // the extraction release. - using Identity = Echoion::Protocol::Identity; + using Identity = Void::TeamSpeak::Protocol::Identity; } diff --git a/include/echoion/protocol/Command.hpp b/include/Void/TeamSpeak/Protocol/Command.hpp similarity index 93% rename from include/echoion/protocol/Command.hpp rename to include/Void/TeamSpeak/Protocol/Command.hpp index 9238eba..ae94710 100644 --- a/include/echoion/protocol/Command.hpp +++ b/include/Void/TeamSpeak/Protocol/Command.hpp @@ -7,7 +7,7 @@ #include #include -namespace Echoion::Protocol +namespace Void::TeamSpeak::Protocol { // Escape / unescape a single token per the TS3 rules @@ -33,4 +33,4 @@ namespace Echoion::Protocol // Parse a raw command / notify line into name + params (+ pipe groups). ParsedCommand parseCommand(const std::string& raw); -} // namespace Echoion::Protocol +} // namespace Void::TeamSpeak::Protocol diff --git a/include/echoion/protocol/Crypto.hpp b/include/Void/TeamSpeak/Protocol/Crypto.hpp similarity index 97% rename from include/echoion/protocol/Crypto.hpp rename to include/Void/TeamSpeak/Protocol/Crypto.hpp index c399691..8ba049a 100644 --- a/include/echoion/protocol/Crypto.hpp +++ b/include/Void/TeamSpeak/Protocol/Crypto.hpp @@ -15,7 +15,7 @@ #include #include -namespace Echoion::Protocol::Crypto +namespace Void::TeamSpeak::Protocol::Crypto { using Bytes = std::vector; @@ -85,4 +85,4 @@ namespace Echoion::Protocol::Crypto // base64(sha1(utf8(password))); empty string for empty password. std::string hashPassword(const std::string& password); -} // namespace Echoion::Protocol::Crypto +} // namespace Void::TeamSpeak::Protocol::Crypto diff --git a/include/echoion/protocol/Identity.hpp b/include/Void/TeamSpeak/Protocol/Identity.hpp similarity index 96% rename from include/echoion/protocol/Identity.hpp rename to include/Void/TeamSpeak/Protocol/Identity.hpp index 3d20cad..17cf6b2 100644 --- a/include/echoion/protocol/Identity.hpp +++ b/include/Void/TeamSpeak/Protocol/Identity.hpp @@ -11,7 +11,7 @@ #include #include -namespace Echoion::Protocol +namespace Void::TeamSpeak::Protocol { class Identity @@ -62,4 +62,4 @@ namespace Echoion::Protocol uint64_t keyOffset_ = 0; }; -} // namespace Echoion::Protocol +} // namespace Void::TeamSpeak::Protocol diff --git a/include/echoion/protocol/License.hpp b/include/Void/TeamSpeak/Protocol/License.hpp similarity index 94% rename from include/echoion/protocol/License.hpp rename to include/Void/TeamSpeak/Protocol/License.hpp index 4891655..1d34873 100644 --- a/include/echoion/protocol/License.hpp +++ b/include/Void/TeamSpeak/Protocol/License.hpp @@ -10,7 +10,7 @@ #include #include -namespace Echoion::Protocol +namespace Void::TeamSpeak::Protocol { using Bytes = std::vector; @@ -42,4 +42,4 @@ namespace Echoion::Protocol std::array getSharedSecret2(ByteView serverDerivedKey, ByteView tempPrivateKey); -} // namespace Echoion::Protocol +} // namespace Void::TeamSpeak::Protocol diff --git a/include/echoion/protocol/QuickLz.hpp b/include/Void/TeamSpeak/Protocol/QuickLz.hpp similarity index 88% rename from include/echoion/protocol/QuickLz.hpp rename to include/Void/TeamSpeak/Protocol/QuickLz.hpp index dda4429..3f1731b 100644 --- a/include/echoion/protocol/QuickLz.hpp +++ b/include/Void/TeamSpeak/Protocol/QuickLz.hpp @@ -8,7 +8,7 @@ #include #include -namespace Echoion::Protocol +namespace Void::TeamSpeak::Protocol { using Bytes = std::vector; @@ -20,4 +20,4 @@ namespace Echoion::Protocol // Throws std::runtime_error on malformed input or unsupported level. Bytes qlzDecompress(ByteView data); -} // namespace Echoion::Protocol +} // namespace Void::TeamSpeak::Protocol diff --git a/include/echoion/protocol/TsClient.hpp b/include/Void/TeamSpeak/Protocol/TsClient.hpp similarity index 93% rename from include/echoion/protocol/TsClient.hpp rename to include/Void/TeamSpeak/Protocol/TsClient.hpp index 284883a..c7aa308 100644 --- a/include/echoion/protocol/TsClient.hpp +++ b/include/Void/TeamSpeak/Protocol/TsClient.hpp @@ -8,8 +8,8 @@ // send methods are thread-safe (guarded by an internal mutex) so the audio // thread can call sendVoice() directly. Callbacks fire on the loop thread. -#include "echoion/protocol/Command.hpp" -#include "echoion/protocol/Identity.hpp" +#include "Void/TeamSpeak/Protocol/Command.hpp" +#include "Void/TeamSpeak/Protocol/Identity.hpp" #include #include @@ -26,7 +26,7 @@ #include #include -namespace Echoion::Protocol +namespace Void::TeamSpeak::Protocol { enum class PacketType : uint8_t { @@ -53,7 +53,7 @@ namespace Echoion::Protocol std::string host; uint16_t port = 9987; const Identity* identity = nullptr; // not owned - std::string nickname = "EchoionBot"; + std::string nickname = "VoidTeamspeak"; std::string serverPassword; std::string defaultChannel; // channel id or name std::string channelPassword; @@ -122,6 +122,11 @@ namespace Echoion::Protocol const std::string& remotePath, const std::string& destination, uint64_t channelId = 0, const std::string& channelPassword = {}, std::chrono::milliseconds timeout = std::chrono::seconds{10}); + CommandResult uploadFile( + const std::string& source, const std::string& remotePath, + uint64_t channelId = 0, const std::string& channelPassword = {}, + bool overwrite = false, + std::chrono::milliseconds timeout = std::chrono::seconds{10}); private: @@ -227,4 +232,4 @@ namespace Echoion::Protocol std::atomic transferId_{0}; }; -} // namespace Echoion::Protocol +} // namespace Void::TeamSpeak::Protocol diff --git a/include/echoion/protocol/Util.hpp b/include/Void/TeamSpeak/Protocol/Util.hpp similarity index 85% rename from include/echoion/protocol/Util.hpp rename to include/Void/TeamSpeak/Protocol/Util.hpp index 7a6125f..951f46b 100644 --- a/include/echoion/protocol/Util.hpp +++ b/include/Void/TeamSpeak/Protocol/Util.hpp @@ -7,7 +7,7 @@ #include #include -namespace Echoion::Protocol::Util +namespace Void::TeamSpeak::Protocol::Util { using Bytes = std::vector; @@ -21,4 +21,4 @@ namespace Echoion::Protocol::Util std::string toHex(ByteView data); Bytes fromHex(const std::string& hex); -} // namespace Echoion::Protocol::Util +} // namespace Void::TeamSpeak::Protocol::Util diff --git a/include/Void/TeamSpeak/TeamSpeak.hpp b/include/Void/TeamSpeak/TeamSpeak.hpp index 8b93c17..8b557f2 100644 --- a/include/Void/TeamSpeak/TeamSpeak.hpp +++ b/include/Void/TeamSpeak/TeamSpeak.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include diff --git a/include/Void/TeamSpeak/Types.hpp b/include/Void/TeamSpeak/Types.hpp index c4b9d72..fc2cf25 100644 --- a/include/Void/TeamSpeak/Types.hpp +++ b/include/Void/TeamSpeak/Types.hpp @@ -46,6 +46,17 @@ namespace Void::TeamSpeak std::string uniqueId; }; + struct ClientLeft { + ClientId id; + int reasonId{}; + std::string reasonMessage; + }; + + struct ClientMoved { + ClientId id; + ChannelId channel; + }; + struct Channel { ChannelId id; std::optional parent; @@ -59,12 +70,37 @@ namespace Void::TeamSpeak int type{}; }; + struct ChannelGroup { + ChannelGroupId id; + std::string name; + int type{}; + }; + + struct Permission { + PermissionId id; + std::string name; + std::string description; + }; + enum class TextTargetKind { Client = 1, Channel = 2, Server = 3 }; struct TextTarget { TextTargetKind kind{TextTargetKind::Client}; std::uint64_t id{}; }; + struct TextMessage { + TextTarget target; + ClientId invoker; + std::string invokerName; + std::string message; + }; + + enum class ChannelChangeKind { Created, Updated, Moved, Deleted }; + struct ChannelChanged { + ChannelChangeKind kind{ChannelChangeKind::Updated}; + Channel channel; + }; + struct Error { enum class Code { InvalidArgument, diff --git a/src/Client.cpp b/src/Client.cpp index cda01e5..4990dbd 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -1,8 +1,8 @@ #include #include -#include -#include +#include +#include #include #include @@ -11,9 +11,14 @@ namespace Void::TeamSpeak { + std::string buildCommand(std::string_view name, const CommandFields& fields) + { + return Void::TeamSpeak::Protocol::buildCommand(std::string{name}, fields); + } + class Client::Impl { public: - Echoion::Protocol::TsClient client; + Void::TeamSpeak::Protocol::TsClient client; Events events; }; @@ -24,9 +29,9 @@ namespace Void::TeamSpeak return Error{Error::Code::Protocol, std::move(message)}; } - ConnectionState convert(Echoion::Protocol::ClientState state) + ConnectionState convert(Void::TeamSpeak::Protocol::ClientState state) { - using Old = Echoion::Protocol::ClientState; + using Old = Void::TeamSpeak::Protocol::ClientState; switch (state) { case Old::Disconnected: return ConnectionState::Disconnected; case Old::Init: return ConnectionState::Initializing; @@ -37,7 +42,7 @@ namespace Void::TeamSpeak return ConnectionState::Disconnected; } - std::uint64_t number(const Echoion::Protocol::CommandParams& record, + std::uint64_t number(const Void::TeamSpeak::Protocol::CommandParams& record, std::string_view key) { const auto iterator = record.find(std::string{key}); @@ -49,7 +54,7 @@ namespace Void::TeamSpeak iterator->second.size() ? value : 0; } - std::string text(const Echoion::Protocol::CommandParams& record, + std::string text(const Void::TeamSpeak::Protocol::CommandParams& record, std::string_view key) { const auto iterator = record.find(std::string{key}); @@ -66,7 +71,7 @@ namespace Void::TeamSpeak { impl_->events = std::move(events); auto* impl = impl_.get(); - Echoion::Protocol::TsClientCallbacks callbacks; + Void::TeamSpeak::Protocol::TsClientCallbacks callbacks; callbacks.onConnected = [impl] { if (impl->events.connected) impl->events.connected(); }; callbacks.onDisconnected = [impl] { if (impl->events.disconnected) impl->events.disconnected(); }; callbacks.onError = [impl](const std::string& message) { @@ -75,8 +80,66 @@ namespace Void::TeamSpeak callbacks.onDebug = [impl](const std::string& message) { if (impl->events.debug) impl->events.debug(message); }; - callbacks.onCommand = [impl](const Echoion::Protocol::ParsedCommand& command) { + callbacks.onCommand = [impl](const Void::TeamSpeak::Protocol::ParsedCommand& command) { if (impl->events.command) impl->events.command(command.name, command.params); + if (impl->events.commandBatch) { + impl->events.commandBatch(CommandEvent{ + .name = command.name, + .records = command.groups.empty() + ? std::vector{command.params} + : command.groups}); + } + const auto& records = command.groups.empty() + ? std::vector{command.params} + : command.groups; + for (const auto& record : records) { + if (command.name == "notifycliententerview" && + impl->events.clientEntered) { + impl->events.clientEntered(RemoteClient{ + .id = ClientId{static_cast(number(record, "clid"))}, + .channel = ChannelId{number(record, "ctid")}, + .nickname = text(record, "client_nickname"), + .uniqueId = text(record, "client_unique_identifier")}); + } else if (command.name == "notifyclientleftview" && + impl->events.clientLeft) { + impl->events.clientLeft(ClientLeft{ + .id = ClientId{static_cast(number(record, "clid"))}, + .reasonId = static_cast(number(record, "reasonid")), + .reasonMessage = text(record, "reasonmsg")}); + } else if (command.name == "notifyclientmoved" && + impl->events.clientMoved) { + impl->events.clientMoved(ClientMoved{ + .id = ClientId{static_cast(number(record, "clid"))}, + .channel = ChannelId{number(record, "ctid")}}); + } else if (command.name == "notifytextmessage" && + impl->events.textMessage) { + impl->events.textMessage(TextMessage{ + .target = TextTarget{ + .kind = static_cast(number(record, "targetmode")), + .id = number(record, "target")}, + .invoker = ClientId{static_cast(number(record, "invokerid"))}, + .invokerName = text(record, "invokername"), + .message = text(record, "msg")}); + } else if (impl->events.channelChanged) { + std::optional kind; + if (command.name == "notifychannelcreated") kind = ChannelChangeKind::Created; + else if (command.name == "notifychanneledited") kind = ChannelChangeKind::Updated; + else if (command.name == "notifychannelmoved") kind = ChannelChangeKind::Moved; + else if (command.name == "notifychanneldeleted") kind = ChannelChangeKind::Deleted; + if (kind) { + const auto parent = number(record, "pid"); + impl->events.channelChanged(ChannelChanged{ + .kind = *kind, + .channel = Channel{ + .id = ChannelId{number(record, "cid")}, + .parent = parent == 0 ? std::nullopt + : std::optional{ChannelId{parent}}, + .name = text(record, "channel_name"), + .passwordProtected = number(record, + "channel_flag_password") != 0}}); + } + } + } }; callbacks.onVoice = [impl](std::span voice) { if (impl->events.voice) impl->events.voice(voice); @@ -90,7 +153,7 @@ namespace Void::TeamSpeak return Void::Result::fail(Error{Error::Code::InvalidArgument, "host and identity are required"}); } - Echoion::Protocol::TsClientOptions old; + Void::TeamSpeak::Protocol::TsClientOptions old; old.host = options.host; old.port = options.port; old.identity = options.identity.get(); @@ -173,13 +236,46 @@ namespace Void::TeamSpeak return result; } + Void::Result::Result, Error> + Client::channelGroups(std::chrono::milliseconds timeout) + { + auto response = request("channelgrouplist", timeout); + if (!response) return Void::Result::fail(response.error()); + std::vector result; + result.reserve(response->records.size()); + for (const auto& record : response->records) { + result.push_back(ChannelGroup{ + .id = ChannelGroupId{number(record, "cgid")}, + .name = text(record, "name"), + .type = static_cast(number(record, "type"))}); + } + return result; + } + + Void::Result::Result, Error> + Client::permissions(std::chrono::milliseconds timeout) + { + auto response = request("permissionlist", timeout); + if (!response) return Void::Result::fail(response.error()); + std::vector result; + result.reserve(response->records.size()); + for (const auto& record : response->records) { + result.push_back(Permission{ + .id = PermissionId{static_cast( + number(record, "permid"))}, + .name = text(record, "permname"), + .description = text(record, "permdesc")}); + } + return result; + } + Void::Result::Result Client::addToServerGroup(ServerGroupId group, ClientDatabaseId client) { - Echoion::Protocol::CommandParams params{ + Void::TeamSpeak::Protocol::CommandParams params{ {"sgid", std::to_string(group.value)}, {"cldbid", std::to_string(client.value)}}; - auto response = request(Echoion::Protocol::buildCommand( + auto response = request(Void::TeamSpeak::Protocol::buildCommand( "servergroupaddclient", params)); if (!response) return Void::Result::fail(response.error()); return {}; @@ -188,24 +284,35 @@ Client::addToServerGroup(ServerGroupId group, ClientDatabaseId client) Void::Result::Result Client::removeFromServerGroup(ServerGroupId group, ClientDatabaseId client) { - Echoion::Protocol::CommandParams params{ + Void::TeamSpeak::Protocol::CommandParams params{ {"sgid", std::to_string(group.value)}, {"cldbid", std::to_string(client.value)}}; - auto response = request(Echoion::Protocol::buildCommand( + auto response = request(Void::TeamSpeak::Protocol::buildCommand( "servergroupdelclient", params)); if (!response) return Void::Result::fail(response.error()); return {}; } + Void::Result::Result Client::setChannelGroup( + ChannelGroupId group, ChannelId channel, ClientDatabaseId client) + { + CommandFields fields{{"cgid", std::to_string(group.value)}, + {"cid", std::to_string(channel.value)}, + {"cldbid", std::to_string(client.value)}}; + auto response = request(buildCommand("setclientchannelgroup", fields)); + if (!response) return Void::Result::fail(response.error()); + return {}; + } + Void::Result::Result Client::sendText(TextTarget target, std::string_view message) { - Echoion::Protocol::CommandParams params; + Void::TeamSpeak::Protocol::CommandParams params; params["targetmode"] = std::to_string(static_cast(target.kind)); if (target.kind != TextTargetKind::Server) params["target"] = std::to_string(target.id); params["msg"] = std::string{message}; - auto response = request(Echoion::Protocol::buildCommand( + auto response = request(Void::TeamSpeak::Protocol::buildCommand( "sendtextmessage", params)); if (!response) return Void::Result::fail(response.error()); return {}; @@ -214,10 +321,10 @@ Client::removeFromServerGroup(ServerGroupId group, ClientDatabaseId client) Void::Result::Result Client::move(ClientId client, ChannelId channel) { - Echoion::Protocol::CommandParams params{ + Void::TeamSpeak::Protocol::CommandParams params{ {"clid", std::to_string(client.value)}, {"cid", std::to_string(channel.value)}}; - auto response = request(Echoion::Protocol::buildCommand("clientmove", params)); + auto response = request(Void::TeamSpeak::Protocol::buildCommand("clientmove", params)); if (!response) return Void::Result::fail(response.error()); return {}; } @@ -225,10 +332,10 @@ Client::removeFromServerGroup(ServerGroupId group, ClientDatabaseId client) Void::Result::Result Client::kick(ClientId client, std::string_view reason) { - Echoion::Protocol::CommandParams params{ + Void::TeamSpeak::Protocol::CommandParams params{ {"clid", std::to_string(client.value)}, {"reasonid", "5"}, {"reasonmsg", std::string{reason}}}; - auto response = request(Echoion::Protocol::buildCommand("clientkick", params)); + auto response = request(Void::TeamSpeak::Protocol::buildCommand("clientkick", params)); if (!response) return Void::Result::fail(response.error()); return {}; } @@ -244,6 +351,34 @@ Client::removeFromServerGroup(ServerGroupId group, ClientDatabaseId client) return destination; } + Void::Result::Result Client::upload( + const std::filesystem::path& source, std::string_view remotePath, + ChannelId channel, std::string_view password, bool overwrite, + std::chrono::milliseconds timeout) + { + if (remotePath.empty() || !std::filesystem::is_regular_file(source)) { + return Void::Result::fail(Error{Error::Code::InvalidArgument, + "upload source and remote path are required"}); + } + auto result = impl_->client.uploadFile(source.string(), + std::string{remotePath}, channel.value, std::string{password}, + overwrite, timeout); + if (!result.ok()) return Void::Result::fail(protocolError(result.errorMessage)); + return {}; + } + + Void::Result::Result Client::deleteFile( + std::string_view remotePath, ChannelId channel, std::string_view password) + { + if (remotePath.empty()) return Void::Result::fail( + Error{Error::Code::InvalidArgument, "remote path is required"}); + CommandFields fields{{"cid", std::to_string(channel.value)}, + {"cpw", std::string{password}}, {"name", std::string{remotePath}}}; + auto response = request(buildCommand("ftdeletefile", fields)); + if (!response) return Void::Result::fail(response.error()); + return {}; + } + Void::Result::Result Client::sendVoice(std::span frame) { diff --git a/src/Command.cpp b/src/Command.cpp index 6b77518..84ccf88 100644 --- a/src/Command.cpp +++ b/src/Command.cpp @@ -1,9 +1,9 @@ -#include "echoion/protocol/Command.hpp" +#include "Void/TeamSpeak/Protocol/Command.hpp" #include #include -namespace Echoion::Protocol +namespace Void::TeamSpeak::Protocol { std::string tsEscape(const std::string& str) @@ -181,4 +181,4 @@ namespace Echoion::Protocol return out; } -} // namespace Echoion::Protocol +} // namespace Void::TeamSpeak::Protocol diff --git a/src/Crypto.cpp b/src/Crypto.cpp index ae44a8f..317f754 100644 --- a/src/Crypto.cpp +++ b/src/Crypto.cpp @@ -1,5 +1,5 @@ -#include "echoion/protocol/Crypto.hpp" -#include "echoion/protocol/Util.hpp" +#include "Void/TeamSpeak/Protocol/Crypto.hpp" +#include "Void/TeamSpeak/Protocol/Util.hpp" #include #include @@ -7,7 +7,7 @@ #include #include -namespace Echoion::Protocol::Crypto +namespace Void::TeamSpeak::Protocol::Crypto { namespace { @@ -250,4 +250,4 @@ namespace Echoion::Protocol::Crypto return Util::base64Encode(ByteView(digest.data(), digest.size())); } -} // namespace Echoion::Protocol::Crypto +} // namespace Void::TeamSpeak::Protocol::Crypto diff --git a/src/Identity.cpp b/src/Identity.cpp index 899a69c..137d7cc 100644 --- a/src/Identity.cpp +++ b/src/Identity.cpp @@ -1,6 +1,6 @@ -#include "echoion/protocol/Identity.hpp" -#include "echoion/protocol/Crypto.hpp" -#include "echoion/protocol/Util.hpp" +#include "Void/TeamSpeak/Protocol/Identity.hpp" +#include "Void/TeamSpeak/Protocol/Crypto.hpp" +#include "Void/TeamSpeak/Protocol/Util.hpp" #include #include @@ -19,7 +19,7 @@ #include #include -namespace Echoion::Protocol +namespace Void::TeamSpeak::Protocol { namespace { @@ -672,4 +672,4 @@ namespace Echoion::Protocol return id; } -} // namespace Echoion::Protocol +} // namespace Void::TeamSpeak::Protocol diff --git a/src/License.cpp b/src/License.cpp index 735237a..ad77cf7 100644 --- a/src/License.cpp +++ b/src/License.cpp @@ -1,5 +1,5 @@ -#include "echoion/protocol/License.hpp" -#include "echoion/protocol/Crypto.hpp" +#include "Void/TeamSpeak/Protocol/License.hpp" +#include "Void/TeamSpeak/Protocol/Crypto.hpp" #include @@ -7,7 +7,7 @@ #include #include -namespace Echoion::Protocol +namespace Void::TeamSpeak::Protocol { namespace { @@ -168,4 +168,4 @@ namespace Echoion::Protocol return Crypto::sha512(ByteView(mul.data(), mul.size())); } -} // namespace Echoion::Protocol +} // namespace Void::TeamSpeak::Protocol diff --git a/src/QuickLz.cpp b/src/QuickLz.cpp index ceedc9f..32f8f64 100644 --- a/src/QuickLz.cpp +++ b/src/QuickLz.cpp @@ -1,9 +1,9 @@ -#include "echoion/protocol/QuickLz.hpp" +#include "Void/TeamSpeak/Protocol/QuickLz.hpp" #include #include -namespace Echoion::Protocol +namespace Void::TeamSpeak::Protocol { namespace { @@ -155,4 +155,4 @@ namespace Echoion::Protocol return dest; } -} // namespace Echoion::Protocol +} // namespace Void::TeamSpeak::Protocol diff --git a/src/TsClient.cpp b/src/TsClient.cpp index f7e5fbc..b299575 100644 --- a/src/TsClient.cpp +++ b/src/TsClient.cpp @@ -1,8 +1,8 @@ -#include "echoion/protocol/TsClient.hpp" -#include "echoion/protocol/Crypto.hpp" -#include "echoion/protocol/License.hpp" -#include "echoion/protocol/QuickLz.hpp" -#include "echoion/protocol/Util.hpp" +#include "Void/TeamSpeak/Protocol/TsClient.hpp" +#include "Void/TeamSpeak/Protocol/Crypto.hpp" +#include "Void/TeamSpeak/Protocol/License.hpp" +#include "Void/TeamSpeak/Protocol/QuickLz.hpp" +#include "Void/TeamSpeak/Protocol/Util.hpp" #include #include @@ -20,7 +20,7 @@ #include #include -namespace Echoion::Protocol +namespace Void::TeamSpeak::Protocol { namespace { @@ -344,7 +344,7 @@ namespace Echoion::Protocol // response-bearing command in flight so those records remain unambiguous. std::unique_lock serialLock(requestSerialMutex_); auto pending = std::make_shared(); - pending->returnCode = "echoion-" + std::to_string(++requestId_); + pending->returnCode = "voidteamspeak-" + std::to_string(++requestId_); { // Keep queue insertion and wire send in the same order when callers // issue requests concurrently. @@ -500,6 +500,93 @@ namespace Echoion::Protocol return {}; } + CommandResult TsClient::uploadFile( + const std::string& source, const std::string& remotePath, + uint64_t channelId, const std::string& channelPassword, bool overwrite, + std::chrono::milliseconds timeout) + { + std::ifstream input(source, std::ios::binary); + if (!input) return {{}, -1, "upload source is not readable"}; + std::error_code sizeError; + const auto size = std::filesystem::file_size(source, sizeError); + if (sizeError) return {{}, -1, "cannot determine upload size"}; + + CommandParams parameters{{"cid", std::to_string(channelId)}, + {"cpw", channelPassword}, {"name", remotePath}, + {"clientftfid", std::to_string(++transferId_)}, + {"size", std::to_string(size)}, {"overwrite", overwrite ? "1" : "0"}, + {"resume", "0"}}; + auto result = requestCommand(buildCommand("ftinitupload", parameters), timeout); + if (!result.ok() || result.records.empty()) { + if (result.ok()) return {{}, -1, + "file transfer returned no connection data"}; + return result; + } + const auto& record = result.records.front(); + auto value = [&record](std::string_view name) { + const auto entry = record.find(std::string{name}); + return entry == record.end() ? std::string{} : entry->second; + }; + const auto transferKey = value("ftkey"); + const auto port = value("port"); + if (transferKey.empty() || port.empty()) + return {{}, -1, "file transfer response is incomplete"}; + + addrinfo hints{}; + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + addrinfo* addresses = nullptr; + if (getaddrinfo(opts_.host.c_str(), port.c_str(), &hints, &addresses) != 0 || + !addresses) return {{}, -1, "cannot resolve file-transfer endpoint"}; + int socket = -1; + for (auto* address = addresses; address; address = address->ai_next) { + socket = ::socket(address->ai_family, address->ai_socktype, + address->ai_protocol); + if (socket >= 0 && ::connect(socket, address->ai_addr, + address->ai_addrlen) == 0) break; + if (socket >= 0) ::close(socket); + socket = -1; + } + freeaddrinfo(addresses); + if (socket < 0) return {{}, -1, + "cannot connect to file-transfer endpoint"}; + + timeval socketTimeout{}; + socketTimeout.tv_sec = static_cast( + std::max(1, timeout.count() / 1000)); + setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &socketTimeout, + sizeof(socketTimeout)); + auto sendAll = [socket](const char* data, std::size_t length) { + std::size_t sent = 0; + while (sent < length) { + const auto count = ::send(socket, data + sent, length - sent, 0); + if (count <= 0) return false; + sent += static_cast(count); + } + return true; + }; + if (!sendAll(transferKey.data(), transferKey.size())) { + ::close(socket); + return {{}, -1, "cannot start file upload"}; + } + std::array buffer{}; + uint64_t uploaded = 0; + while (input) { + input.read(buffer.data(), static_cast(buffer.size())); + const auto count = input.gcount(); + if (count > 0 && !sendAll(buffer.data(), static_cast(count))) { + ::close(socket); + return {{}, -1, "file upload was interrupted"}; + } + uploaded += static_cast(count); + } + ::shutdown(socket, SHUT_WR); + ::close(socket); + if (!input.eof() || uploaded != size) + return {{}, -1, "file upload size mismatch"}; + return {}; + } + void TsClient::sendVoice(std::span opus) { std::lock_guard lock(mu_); @@ -1183,4 +1270,4 @@ namespace Echoion::Protocol } } -} // namespace Echoion::Protocol +} // namespace Void::TeamSpeak::Protocol diff --git a/src/Util.cpp b/src/Util.cpp index 07eb3b0..266a622 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -1,9 +1,9 @@ -#include "echoion/protocol/Util.hpp" +#include "Void/TeamSpeak/Protocol/Util.hpp" #include #include -namespace Echoion::Protocol::Util +namespace Void::TeamSpeak::Protocol::Util { namespace { @@ -113,4 +113,4 @@ namespace Echoion::Protocol::Util return out; } -} // namespace Echoion::Protocol::Util +} // namespace Void::TeamSpeak::Protocol::Util diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e1ffe7b..94c5d00 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,12 +1,12 @@ -# Unit tests for EchoionProtocol. Uses a tiny header-only harness (check.hpp), +# Unit tests for VoidTeamspeak. Uses a tiny header-only harness (check.hpp), # no external test framework required. -file(GLOB ECHOIONPROTOCOL_TEST_SOURCES CONFIGURE_DEPENDS +file(GLOB VOID_TEAMSPEAK_TEST_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/test_*.cpp") -add_executable(echoion_protocol_tests ${ECHOIONPROTOCOL_TEST_SOURCES}) -target_link_libraries(echoion_protocol_tests PRIVATE Void::TeamSpeak) -target_include_directories(echoion_protocol_tests PRIVATE +add_executable(void_teamspeak_tests ${VOID_TEAMSPEAK_TEST_SOURCES}) +target_link_libraries(void_teamspeak_tests PRIVATE Void::TeamSpeak) +target_include_directories(void_teamspeak_tests PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}") -add_test(NAME echoion_protocol_tests COMMAND echoion_protocol_tests) +add_test(NAME void_teamspeak_tests COMMAND void_teamspeak_tests) diff --git a/tests/test_command.cpp b/tests/test_command.cpp index 6be30ab..943969e 100644 --- a/tests/test_command.cpp +++ b/tests/test_command.cpp @@ -1,7 +1,7 @@ #include "check.hpp" -#include "echoion/protocol/Command.hpp" +#include "Void/TeamSpeak/Protocol/Command.hpp" -using namespace Echoion::Protocol; +using namespace Void::TeamSpeak::Protocol; TEST_CASE(escape_roundtrip) { diff --git a/tests/test_crypto.cpp b/tests/test_crypto.cpp index 7a45ce2..ac60546 100644 --- a/tests/test_crypto.cpp +++ b/tests/test_crypto.cpp @@ -1,10 +1,10 @@ #include "check.hpp" -#include "echoion/protocol/Crypto.hpp" -#include "echoion/protocol/Util.hpp" +#include "Void/TeamSpeak/Protocol/Crypto.hpp" +#include "Void/TeamSpeak/Protocol/Util.hpp" #include -using namespace Echoion::Protocol; +using namespace Void::TeamSpeak::Protocol; using Crypto::ByteView; namespace diff --git a/tests/test_identity.cpp b/tests/test_identity.cpp index c0c7bc1..047955c 100644 --- a/tests/test_identity.cpp +++ b/tests/test_identity.cpp @@ -1,14 +1,14 @@ #include "check.hpp" -#include "echoion/protocol/Crypto.hpp" -#include "echoion/protocol/Identity.hpp" -#include "echoion/protocol/Util.hpp" +#include "Void/TeamSpeak/Protocol/Crypto.hpp" +#include "Void/TeamSpeak/Protocol/Identity.hpp" +#include "Void/TeamSpeak/Protocol/Util.hpp" #include #include #include #include -using namespace Echoion::Protocol; +using namespace Void::TeamSpeak::Protocol; TEST_CASE(identity_generate_uid_and_level) { @@ -44,7 +44,7 @@ TEST_CASE(identity_sign_verifiable_length) TEST_CASE(identity_save_load_roundtrip) { auto id = Identity::generate(0); - std::string path = "/tmp/echoion_identity_test.txt"; + std::string path = "/tmp/void_teamspeak_identity_test.txt"; id.save(path); auto loaded = Identity::load(path); CHECK_EQ(loaded.publicKeyString(), id.publicKeyString()); @@ -84,8 +84,8 @@ namespace TEST_CASE(identity_import_teamspeak_ini) { - const std::string exportPath = "/tmp/echoion_ts_identity.ini"; - const std::string nativePath = "/tmp/echoion_imported_identity.txt"; + const std::string exportPath = "/tmp/void_teamspeak_identity.ini"; + const std::string nativePath = "/tmp/void_teamspeak_imported_identity.txt"; { std::ofstream output(exportPath); output << "[Identity]\nidentity=\"" << teamSpeakExportForScalarOne(42) @@ -109,7 +109,7 @@ TEST_CASE(identity_import_teamspeak_ini) TEST_CASE(identity_import_rejects_malformed_export) { - const std::string path = "/tmp/echoion_bad_identity.ini"; + const std::string path = "/tmp/void_teamspeak_bad_identity.ini"; { std::ofstream output(path); output << "[Identity]\nidentity=not-a-private-key\n"; diff --git a/tests/test_modern_api.cpp b/tests/test_modern_api.cpp index 1d17dba..41f5b2e 100644 --- a/tests/test_modern_api.cpp +++ b/tests/test_modern_api.cpp @@ -2,36 +2,41 @@ #include +#include +#include + using namespace Void::TeamSpeak; -TEST_CASE(modern_library_creates_independent_clients) +static_assert(!std::is_copy_constructible_v); +static_assert(std::is_move_constructible_v); +static_assert(std::is_same_v); +static_assert(std::is_same_v); +static_assert(std::is_same_v); +static_assert(std::is_same_v().deleteFile("x")), + Void::Result::Result>); + +TEST_CASE(modern_build_command_escapes_fields) { - ClientLibrary library; - auto first = library.createClient(); - auto second = library.createClient(); - CHECK(first != nullptr); - CHECK(second != nullptr); - CHECK(first.get() != second.get()); - CHECK_EQ(first->state(), ConnectionState::Disconnected); - CHECK_EQ(second->state(), ConnectionState::Disconnected); + const auto command = buildCommand("sendtextmessage", { + {"msg", "hello world|all"}, {"target", "7"}}); + CHECK_EQ(command, + std::string("sendtextmessage msg=hello\\sworld\\pall target=7")); } -TEST_CASE(strong_ids_do_not_erase_target_types) +TEST_CASE(strong_ids_do_not_implicitly_convert) { - WhisperTargets targets{ - .channels = {ChannelId{42}}, - .clients = {ClientId{7}, ClientId{8}}, - }; + const ClientId client{42}; + const ChannelId channel{42}; + CHECK(client.value == 42); + CHECK(channel.value == 42); + CHECK(static_cast(client)); + CHECK(static_cast(channel)); +} + +TEST_CASE(whisper_targets_report_empty_state) +{ + WhisperTargets targets; + CHECK(targets.empty()); + targets.clients.push_back(ClientId{7}); CHECK(!targets.empty()); - CHECK_EQ(targets.channels.front().value, std::uint64_t{42}); - CHECK_EQ(targets.clients.size(), std::size_t{2}); - CHECK(ClientId{7} != ClientId{8}); -} - -TEST_CASE(connect_options_are_validated_before_network_io) -{ - Client client; - auto result = client.connect({}); - CHECK(!result.has_value()); - CHECK_EQ(result.error().code, Error::Code::InvalidArgument); } diff --git a/tests/test_quicklz.cpp b/tests/test_quicklz.cpp index 04044f8..c4b5dc8 100644 --- a/tests/test_quicklz.cpp +++ b/tests/test_quicklz.cpp @@ -1,7 +1,7 @@ #include "check.hpp" -#include "echoion/protocol/QuickLz.hpp" +#include "Void/TeamSpeak/Protocol/QuickLz.hpp" -using namespace Echoion::Protocol; +using namespace Void::TeamSpeak::Protocol; // QuickLZ level-1 "stored" (uncompressed) block: header byte with level=1 // (bits 2-3 = 01 -> 0x04), compressed bit (bit0) = 0, short size form