Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-07-12 02:34:34 +03:00
1 parent a7ffe73a6c
commit c1a11ffe29
29 files changed
+433 -123

No files matched your search

+155 -20
View File
@@ -1,8 +1,8 @@
#include <Void/TeamSpeak/Client.hpp>
#include <Void/TeamSpeak/Identity.hpp>
#include <echoion/protocol/TsClient.hpp>
#include <echoion/protocol/Command.hpp>
#include <Void/TeamSpeak/Protocol/TsClient.hpp>
#include <Void/TeamSpeak/Protocol/Command.hpp>
#include <exception>
#include <charconv>
@@ -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<CommandResponse::Record>{command.params}
: command.groups});
}
const auto& records = command.groups.empty()
? std::vector<CommandResponse::Record>{command.params}
: command.groups;
for (const auto& record : records) {
if (command.name == "notifycliententerview" &&
impl->events.clientEntered) {
impl->events.clientEntered(RemoteClient{
.id = ClientId{static_cast<std::uint16_t>(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<std::uint16_t>(number(record, "clid"))},
.reasonId = static_cast<int>(number(record, "reasonid")),
.reasonMessage = text(record, "reasonmsg")});
} else if (command.name == "notifyclientmoved" &&
impl->events.clientMoved) {
impl->events.clientMoved(ClientMoved{
.id = ClientId{static_cast<std::uint16_t>(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<TextTargetKind>(number(record, "targetmode")),
.id = number(record, "target")},
.invoker = ClientId{static_cast<std::uint16_t>(number(record, "invokerid"))},
.invokerName = text(record, "invokername"),
.message = text(record, "msg")});
} else if (impl->events.channelChanged) {
std::optional<ChannelChangeKind> 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<const std::uint8_t> 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<std::vector<ChannelGroup>, Error>
Client::channelGroups(std::chrono::milliseconds timeout)
{
auto response = request("channelgrouplist", timeout);
if (!response) return Void::Result::fail(response.error());
std::vector<ChannelGroup> 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<int>(number(record, "type"))});
}
return result;
}
Void::Result::Result<std::vector<Permission>, Error>
Client::permissions(std::chrono::milliseconds timeout)
{
auto response = request("permissionlist", timeout);
if (!response) return Void::Result::fail(response.error());
std::vector<Permission> result;
result.reserve(response->records.size());
for (const auto& record : response->records) {
result.push_back(Permission{
.id = PermissionId{static_cast<std::uint32_t>(
number(record, "permid"))},
.name = text(record, "permname"),
.description = text(record, "permdesc")});
}
return result;
}
Void::Result::Result<void, Error>
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<void, Error>
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<void, Error> 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<void, Error>
Client::sendText(TextTarget target, std::string_view message)
{
Echoion::Protocol::CommandParams params;
Void::TeamSpeak::Protocol::CommandParams params;
params["targetmode"] = std::to_string(static_cast<int>(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<void, Error>
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<void, Error>
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<void, Error> 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<void, Error> 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<void, Error>
Client::sendVoice(std::span<const std::uint8_t> frame)
{
+3 -3
View File
@@ -1,9 +1,9 @@
#include "echoion/protocol/Command.hpp"
#include "Void/TeamSpeak/Protocol/Command.hpp"
#include <sstream>
#include <stdexcept>
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
+4 -4
View File
@@ -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 <openssl/evp.h>
#include <openssl/sha.h>
@@ -7,7 +7,7 @@
#include <cstring>
#include <stdexcept>
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
+5 -5
View File
@@ -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 <openssl/bn.h>
#include <openssl/core_names.h>
@@ -19,7 +19,7 @@
#include <string_view>
#include <utility>
namespace Echoion::Protocol
namespace Void::TeamSpeak::Protocol
{
namespace
{
@@ -672,4 +672,4 @@ namespace Echoion::Protocol
return id;
}
} // namespace Echoion::Protocol
} // namespace Void::TeamSpeak::Protocol
+4 -4
View File
@@ -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 <sodium.h>
@@ -7,7 +7,7 @@
#include <mutex>
#include <stdexcept>
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
+3 -3
View File
@@ -1,9 +1,9 @@
#include "echoion/protocol/QuickLz.hpp"
#include "Void/TeamSpeak/Protocol/QuickLz.hpp"
#include <algorithm>
#include <stdexcept>
namespace Echoion::Protocol
namespace Void::TeamSpeak::Protocol
{
namespace
{
@@ -155,4 +155,4 @@ namespace Echoion::Protocol
return dest;
}
} // namespace Echoion::Protocol
} // namespace Void::TeamSpeak::Protocol
+95 -8
View File
@@ -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 <openssl/bn.h>
#include <openssl/rand.h>
@@ -20,7 +20,7 @@
#include <fstream>
#include <stdexcept>
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<std::mutex> serialLock(requestSerialMutex_);
auto pending = std::make_shared<PendingRequest>();
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<long>(
std::max<int64_t>(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<std::size_t>(count);
}
return true;
};
if (!sendAll(transferKey.data(), transferKey.size())) {
::close(socket);
return {{}, -1, "cannot start file upload"};
}
std::array<char, 81920> buffer{};
uint64_t uploaded = 0;
while (input) {
input.read(buffer.data(), static_cast<std::streamsize>(buffer.size()));
const auto count = input.gcount();
if (count > 0 && !sendAll(buffer.data(), static_cast<std::size_t>(count))) {
::close(socket);
return {{}, -1, "file upload was interrupted"};
}
uploaded += static_cast<uint64_t>(count);
}
::shutdown(socket, SHUT_WR);
::close(socket);
if (!input.eof() || uploaded != size)
return {{}, -1, "file upload size mismatch"};
return {};
}
void TsClient::sendVoice(std::span<const uint8_t> opus)
{
std::lock_guard<std::recursive_mutex> lock(mu_);
@@ -1183,4 +1270,4 @@ namespace Echoion::Protocol
}
}
} // namespace Echoion::Protocol
} // namespace Void::TeamSpeak::Protocol
+3 -3
View File
@@ -1,9 +1,9 @@
#include "echoion/protocol/Util.hpp"
#include "Void/TeamSpeak/Protocol/Util.hpp"
#include <array>
#include <stdexcept>
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