From 3db909d397c5f0052d4da41d32bbcc157e87e7f5 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Tue, 25 Nov 2025 15:17:43 +0100 Subject: [PATCH] add tox connect command --- src/tox_commands.cpp | 80 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/src/tox_commands.cpp b/src/tox_commands.cpp index 6357c5b..9b4c24f 100644 --- a/src/tox_commands.cpp +++ b/src/tox_commands.cpp @@ -12,7 +12,9 @@ #include #include -#include +#include + +#include void registerToxCommands( MessageCommandDispatcher& mcd, @@ -227,5 +229,81 @@ void registerToxCommands( }, "invite a tox friend to the same tox group" ); + + mcd.registerCommand( + "tox", "tox", + "connect", + [&](std::string_view params, Message3Handle m) -> bool { + const auto contact_from = m.get().c; + const auto split = MM::std_utils::split(params, " "); + + if (split.size() != 3) { + rmm.sendText( + contact_from, + "error, invalid number of params" + ); + return true; + } + + try { + const auto addr = split.at(0); + const auto port_sv = split.at(1); + const auto pubkey_sv = split.at(2); + + uint16_t port{0}; + { // port + auto result = std::from_chars(port_sv.data(), port_sv.data() + port_sv.size(), port); + if (result.ec != std::errc{}) { + rmm.sendText( + contact_from, + "error, invalid port" + ); + return true; + } + } + + // pubkey + const auto pubkey = hex2bin(pubkey_sv); + if (pubkey.size() != 32/*TOX_PUBLIC_KEY_SIZE*/) { + rmm.sendText( + contact_from, + "error, invalid id/pubkey" + ); + return true; + } + + std::string response; + { + Tox_Err_Bootstrap err = t.toxBootstrap(std::string{addr}, port, pubkey); + if (err != Tox_Err_Bootstrap::TOX_ERR_BOOTSTRAP_OK) { + response += "add udp node failed with " + std::to_string(err) + "\n"; + } else { + response += "add udp node succeeded\n"; + } + } + { + Tox_Err_Bootstrap err = t.toxAddTcpRelay(std::string{addr}, port, pubkey); + if (err != Tox_Err_Bootstrap::TOX_ERR_BOOTSTRAP_OK) { + response += "add tcp relay failed with " + std::to_string(err); + } else { + response += "add tcp relay succeeded"; + } + } + + rmm.sendText( + contact_from, + response + ); + return true; + } catch (...) { + rmm.sendText( + contact_from, + "error, unknown exception" + ); + return true; + } + }, + "connect to a DHT node. 'addr port pubkey'" + ); }