#include #include #include #include #include #include #include #include namespace Void::TeamSpeak { class Client::Impl { public: Echoion::Protocol::TsClient client; Events events; }; namespace { Error protocolError(std::string message) { return Error{Error::Code::Protocol, std::move(message)}; } ConnectionState convert(Echoion::Protocol::ClientState state) { using Old = Echoion::Protocol::ClientState; switch (state) { case Old::Disconnected: return ConnectionState::Disconnected; case Old::Init: return ConnectionState::Initializing; case Old::Handshake: return ConnectionState::Handshaking; case Old::Connected: return ConnectionState::Connected; case Old::Disconnecting: return ConnectionState::Disconnecting; } return ConnectionState::Disconnected; } std::uint64_t number(const Echoion::Protocol::CommandParams& record, std::string_view key) { const auto iterator = record.find(std::string{key}); if (iterator == record.end()) return 0; std::uint64_t value = 0; auto [end, error] = std::from_chars(iterator->second.data(), iterator->second.data() + iterator->second.size(), value); return error == std::errc{} && end == iterator->second.data() + iterator->second.size() ? value : 0; } std::string text(const Echoion::Protocol::CommandParams& record, std::string_view key) { const auto iterator = record.find(std::string{key}); return iterator == record.end() ? std::string{} : iterator->second; } } Client::Client() : impl_(std::make_unique()) {} Client::~Client() = default; Client::Client(Client&&) noexcept = default; Client& Client::operator=(Client&&) noexcept = default; void Client::setEvents(Events events) { impl_->events = std::move(events); auto* impl = impl_.get(); Echoion::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) { if (impl->events.error) impl->events.error(protocolError(message)); }; callbacks.onDebug = [impl](const std::string& message) { if (impl->events.debug) impl->events.debug(message); }; callbacks.onCommand = [impl](const Echoion::Protocol::ParsedCommand& command) { if (impl->events.command) impl->events.command(command.name, command.params); }; callbacks.onVoice = [impl](std::span voice) { if (impl->events.voice) impl->events.voice(voice); }; impl_->client.setCallbacks(std::move(callbacks)); } Void::Result::Result Client::connect(const ConnectOptions& options) { if (options.host.empty() || !options.identity) { return Void::Result::fail(Error{Error::Code::InvalidArgument, "host and identity are required"}); } Echoion::Protocol::TsClientOptions old; old.host = options.host; old.port = options.port; old.identity = options.identity.get(); old.nickname = options.nickname; old.serverPassword = options.serverPassword; old.defaultChannel = options.defaultChannel; old.channelPassword = options.channelPassword; try { impl_->client.connect(old); return {}; } catch (const std::exception& exception) { return Void::Result::fail(Error{Error::Code::Io, exception.what()}); } } void Client::disconnect() noexcept { impl_->client.disconnect(); } void Client::close() noexcept { impl_->client.forceClose(); } ConnectionState Client::state() const noexcept { return convert(impl_->client.state()); } ClientId Client::id() const noexcept { return ClientId{impl_->client.clientId()}; } Void::Result::Result Client::request(std::string_view command, std::chrono::milliseconds timeout) { auto result = impl_->client.requestCommand(std::string{command}, timeout); if (!result.ok()) { return Void::Result::fail(protocolError(result.errorMessage)); } return CommandResponse{std::move(result.records)}; } Void::Result::Result, Error> Client::clients(std::chrono::milliseconds timeout) { auto response = request("clientlist -uid", 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(RemoteClient{ .id = ClientId{static_cast(number(record, "clid"))}, .channel = ChannelId{number(record, "cid")}, .nickname = text(record, "client_nickname"), .uniqueId = text(record, "client_unique_identifier")}); } return result; } Void::Result::Result, Error> Client::channels(std::chrono::milliseconds timeout) { auto response = request("channellist -flags", timeout); if (!response) return Void::Result::fail(response.error()); std::vector result; result.reserve(response->records.size()); for (const auto& record : response->records) { const auto parent = number(record, "pid"); result.push_back(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}); } return result; } Void::Result::Result, Error> Client::serverGroups(std::chrono::milliseconds timeout) { auto response = request("servergrouplist", 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(ServerGroup{ .id = ServerGroupId{number(record, "sgid")}, .name = text(record, "name"), .type = static_cast(number(record, "type"))}); } return result; } Void::Result::Result Client::addToServerGroup(ServerGroupId group, ClientDatabaseId client) { Echoion::Protocol::CommandParams params{ {"sgid", std::to_string(group.value)}, {"cldbid", std::to_string(client.value)}}; auto response = request(Echoion::Protocol::buildCommand( "servergroupaddclient", params)); if (!response) return Void::Result::fail(response.error()); return {}; } Void::Result::Result Client::removeFromServerGroup(ServerGroupId group, ClientDatabaseId client) { Echoion::Protocol::CommandParams params{ {"sgid", std::to_string(group.value)}, {"cldbid", std::to_string(client.value)}}; auto response = request(Echoion::Protocol::buildCommand( "servergroupdelclient", params)); if (!response) return Void::Result::fail(response.error()); return {}; } Void::Result::Result Client::sendText(TextTarget target, std::string_view message) { Echoion::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( "sendtextmessage", params)); if (!response) return Void::Result::fail(response.error()); return {}; } Void::Result::Result Client::move(ClientId client, ChannelId channel) { Echoion::Protocol::CommandParams params{ {"clid", std::to_string(client.value)}, {"cid", std::to_string(channel.value)}}; auto response = request(Echoion::Protocol::buildCommand("clientmove", params)); if (!response) return Void::Result::fail(response.error()); return {}; } Void::Result::Result Client::kick(ClientId client, std::string_view reason) { Echoion::Protocol::CommandParams params{ {"clid", std::to_string(client.value)}, {"reasonid", "5"}, {"reasonmsg", std::string{reason}}}; auto response = request(Echoion::Protocol::buildCommand("clientkick", params)); if (!response) return Void::Result::fail(response.error()); return {}; } Void::Result::Result Client::download(std::string_view remotePath, const std::filesystem::path& destination, ChannelId channel, std::string_view password, std::chrono::milliseconds timeout) { auto result = impl_->client.downloadFile(std::string{remotePath}, destination.string(), channel.value, std::string{password}, timeout); if (!result.ok()) return Void::Result::fail(protocolError(result.errorMessage)); return destination; } Void::Result::Result Client::sendVoice(std::span frame) { if (state() != ConnectionState::Connected) return Void::Result::fail(Error{Error::Code::NotConnected, "not connected"}); impl_->client.sendVoice(frame); return {}; } Void::Result::Result Client::sendWhisper(std::span frame, const WhisperTargets& targets) { if (state() != ConnectionState::Connected) return Void::Result::fail(Error{Error::Code::NotConnected, "not connected"}); std::vector channels; std::vector clients; channels.reserve(targets.channels.size()); clients.reserve(targets.clients.size()); for (auto value : targets.channels) channels.push_back(value.value); for (auto value : targets.clients) clients.push_back(value.value); impl_->client.sendWhisper(frame, channels, clients); return {}; } void Client::stopVoice() noexcept { impl_->client.sendVoiceStop(); } }