From 10571ae923bff376cb18607872305c2ccbbb9a78 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Sat, 14 Jan 2023 02:03:32 +0100 Subject: [PATCH] wire up some callbacks --- src/CMakeLists.txt | 3 ++ src/command_line.cpp | 16 ++++++++- src/command_line.hpp | 3 ++ src/tox_callbacks.cpp | 33 +++++++++++++++++++ src/tox_callbacks.hpp | 40 +++++++++++++++++++++++ src/tox_client.cpp | 75 +++++++++++++++++++++++++++++++++++++++++-- src/tox_client.hpp | 22 ++++++++++--- 7 files changed, 184 insertions(+), 8 deletions(-) create mode 100644 src/tox_callbacks.cpp create mode 100644 src/tox_callbacks.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0fc181c..e0a1374 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,6 +13,9 @@ add_executable(tox_ngc_ft1_tool ./tox_utils.hpp ./tox_utils.cpp + ./tox_callbacks.hpp + ./tox_callbacks.cpp + ./tox_client.hpp ./tox_client.cpp ) diff --git a/src/command_line.cpp b/src/command_line.cpp index ff1a29f..d4ed932 100644 --- a/src/command_line.cpp +++ b/src/command_line.cpp @@ -36,6 +36,14 @@ CommandLine::CommandLine(int argc, char** argv) { return; } profile_path = argv[++i]; + } else if (arg_sv == "-N") { + if (i+1 >= argc) { + std::cerr << "-N missing parameter!\n\n"; + printHelp(); + _should_exit = true; + return; + } + self_name = argv[++i]; } else if (arg_sv == "-a") { } else if (arg_sv == "-f") { if (i+1 >= argc) { @@ -61,13 +69,18 @@ CommandLine::CommandLine(int argc, char** argv) { return; } receive_id = argv[++i]; + } else { + std::cerr << "unknown parameter '" << arg_sv << "' !\n\n"; + printHelp(); + _should_exit = true; } } } void CommandLine::printHelp(void) { std::cout - << "meta:\n" + << "~~~ HELP ~~~\n" + << " meta:\n" << " -v version info\n" << " -V verbose\n" << " -h help\n" @@ -75,6 +88,7 @@ void CommandLine::printHelp(void) { << " connectivity:\n" << " -G \n" << " -F profile.tox\n" + << " -N (defaults to 'tox_ngc_ft1_tool')\n" << " will print friend id at startup\n" << " will autoaccept any invite\n" << " if no -F give, will not save profile.\n" diff --git a/src/command_line.hpp b/src/command_line.hpp index 952a77d..14ddce5 100644 --- a/src/command_line.hpp +++ b/src/command_line.hpp @@ -10,6 +10,7 @@ // connectivity: // -G // -F profile.tox +// -N // will print friend id at startup // will autoaccept any invite // if no -F give, will not save profile. @@ -41,6 +42,8 @@ struct CommandLine { std::string chat_id; // -F profile.tox std::string profile_path; + // -N + std::string self_name {"tox_ngc_tf1_tool"}; // transfer variant: // -a id1/sha128_single/sha128_info/sha256_single/sha256_info diff --git a/src/tox_callbacks.cpp b/src/tox_callbacks.cpp new file mode 100644 index 0000000..4cfeb4b --- /dev/null +++ b/src/tox_callbacks.cpp @@ -0,0 +1,33 @@ +#include "./tox_callbacks.hpp" + +#include "./tox_client.hpp" + +#include +#include + +// logging +void log_cb(Tox*, TOX_LOG_LEVEL level, const char *file, uint32_t line, const char *func, const char *message, void *user_data) { + //ToxClient* client = static_cast(user_data); + std::cerr << "TOX " << level << " " << file << ":" << line << "(" << func << ") " << message << "\n"; +} + +// self +void self_connection_status_cb(Tox*, TOX_CONNECTION connection_status, void *user_data) { + static_cast(user_data)->onToxSelfConnectionStatus(connection_status); +} + +void friend_request_cb(Tox*, const uint8_t *public_key, const uint8_t *message, size_t length, void *user_data) { + static_cast(user_data)->onToxFriendRequest(public_key, std::string_view{reinterpret_cast(message), length}); +} +void group_custom_packet_cb(Tox*, uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length, void *user_data) { + static_cast(user_data)->onToxGroupCustomPacket(group_number, peer_id, data, length); +} + +void group_custom_private_packet_cb(Tox*, uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length, void *user_data) { + static_cast(user_data)->onToxGroupCustomPrivatePacket(group_number, peer_id, data, length); +} + +void group_invite_cb(Tox*, uint32_t friend_number, const uint8_t *invite_data, size_t length, const uint8_t *group_name, size_t group_name_length, void *user_data) { + static_cast(user_data)->onToxGroupInvite(friend_number, invite_data, length, std::string_view{reinterpret_cast(group_name), group_name_length}); +} + diff --git a/src/tox_callbacks.hpp b/src/tox_callbacks.hpp new file mode 100644 index 0000000..473a37f --- /dev/null +++ b/src/tox_callbacks.hpp @@ -0,0 +1,40 @@ +#pragma once + +#include + +// logging +void log_cb(Tox *tox, TOX_LOG_LEVEL level, const char *file, uint32_t line, const char *func, const char *message, void *user_data); + +// self +void self_connection_status_cb(Tox *tox, TOX_CONNECTION connection_status, void *user_data); + +// friend +//static void friend_name_cb(Tox *tox, uint32_t friend_number, const uint8_t *name, size_t length, void *user_data); +//static void friend_status_message_cb(Tox *tox, uint32_t friend_number, const uint8_t *message, size_t length, void *user_data); +//static void friend_status_cb(Tox *tox, uint32_t friend_number, TOX_USER_STATUS status, void *user_data); +//static void friend_connection_status_cb(Tox *tox, uint32_t friend_number, TOX_CONNECTION connection_status, void *user_data); +//static void friend_typing_cb(Tox *tox, uint32_t friend_number, bool is_typing, void *user_data); +//static void friend_read_receipt_cb(Tox *tox, uint32_t friend_number, uint32_t message_id, void *user_data); +void friend_request_cb(Tox *tox, const uint8_t *public_key, const uint8_t *message, size_t length, void *user_data); +//static void friend_message_cb(Tox *tox, uint32_t friend_number, TOX_MESSAGE_TYPE type, const uint8_t *message, size_t length, void *user_data); + +// ngc +//static void group_peer_name_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *name, size_t length, void *user_data); +//static void group_peer_status_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_User_Status status, void *user_data); +//static void group_topic_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *topic, size_t length, void *user_data); +//static void group_privacy_state_cb(Tox *tox, uint32_t group_number, Tox_Group_Privacy_State privacy_state, void *user_data); +//static void group_voice_state_cb(Tox *tox, uint32_t group_number, Tox_Group_Voice_State voice_state, void *user_data); +//static void group_topic_lock_cb(Tox *tox, uint32_t group_number, Tox_Group_Topic_Lock topic_lock, void *user_data); +//static void group_peer_limit_cb(Tox *tox, uint32_t group_number, uint32_t peer_limit, void *user_data); +//static void group_password_cb(Tox *tox, uint32_t group_number, const uint8_t *password, size_t length, void *user_data); +//static void group_message_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Message_Type type, const uint8_t *message, size_t length, uint32_t message_id, void *user_data); +//static void group_private_message_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Message_Type type, const uint8_t *message, size_t length, void *user_data); +void group_custom_packet_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length, void *user_data); +void group_custom_private_packet_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length, void *user_data); +void group_invite_cb(Tox *tox, uint32_t friend_number, const uint8_t *invite_data, size_t length, const uint8_t *group_name, size_t group_name_length, void *user_data); +//static void group_peer_join_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, void *user_data); +//static void group_peer_exit_cb(Tox *tox, uint32_t group_number, uint32_t peer_id, Tox_Group_Exit_Type exit_type, const uint8_t *name, size_t name_length, const uint8_t *part_message, size_t length, void *user_data); +//static void group_self_join_cb(Tox *tox, uint32_t group_number, void *user_data); +//static void group_join_fail_cb(Tox *tox, uint32_t group_number, Tox_Group_Join_Fail fail_type, void *user_data); +//static void group_moderation_cb(Tox *tox, uint32_t group_number, uint32_t source_peer_id, uint32_t target_peer_id, Tox_Group_Mod_Event mod_type, void *user_data); + diff --git a/src/tox_client.cpp b/src/tox_client.cpp index 4cadb46..873d95b 100644 --- a/src/tox_client.cpp +++ b/src/tox_client.cpp @@ -1,6 +1,8 @@ #include "./tox_client.hpp" #include "./tox_utils.hpp" +#include "./tox_callbacks.hpp" +#include "toxcore/tox.h" #include #include @@ -9,6 +11,7 @@ #include ToxClient::ToxClient(const CommandLine& cl) : + _self_name(cl.self_name), _tox_profile_path(cl.profile_path) { TOX_ERR_OPTIONS_NEW err_opt_new; @@ -16,8 +19,8 @@ ToxClient::ToxClient(const CommandLine& cl) : assert(err_opt_new == TOX_ERR_OPTIONS_NEW::TOX_ERR_OPTIONS_NEW_OK); // use cl for options - //tox_options_set_log_callback(options, log_cb); - tox_options_set_local_discovery_enabled(options, false); + tox_options_set_log_callback(options, log_cb); + tox_options_set_local_discovery_enabled(options, true); tox_options_set_udp_enabled(options, true); tox_options_set_hole_punching_enabled(options, true); @@ -57,6 +60,43 @@ ToxClient::ToxClient(const CommandLine& cl) : throw std::runtime_error{"tox failed"}; } +#define CALLBACK_REG(x) tox_callback_##x(_tox, x##_cb) + CALLBACK_REG(self_connection_status); + + //CALLBACK_REG(friend_name); + //CALLBACK_REG(friend_status_message); + //CALLBACK_REG(friend_status); + //CALLBACK_REG(friend_connection_status); + //CALLBACK_REG(friend_typing); + //CALLBACK_REG(friend_read_receipt); + CALLBACK_REG(friend_request); + //CALLBACK_REG(friend_message); + + //CALLBACK_REG(file_recv_control); + //CALLBACK_REG(file_chunk_request); + //CALLBACK_REG(file_recv); + //CALLBACK_REG(file_recv_chunk); + + //CALLBACK_REG(conference_invite); + //CALLBACK_REG(conference_connected); + //CALLBACK_REG(conference_message); + //CALLBACK_REG(conference_title); + //CALLBACK_REG(conference_peer_name); + //CALLBACK_REG(conference_peer_list_changed); + + //CALLBACK_REG(friend_lossy_packet); + //CALLBACK_REG(friend_lossless_packet); + + CALLBACK_REG(group_custom_packet); + CALLBACK_REG(group_custom_private_packet); + CALLBACK_REG(group_invite); +#undef CALLBACK_REG + + if (_self_name.empty()) { + _self_name = "tox_ngc_ft1_tool"; + } + tox_self_set_name(_tox, reinterpret_cast(_self_name.data()), _self_name.size(), nullptr); + _tox_profile_dirty = true; } @@ -77,6 +117,37 @@ std::string ToxClient::getOwnAddress(void) const { return bin2hex(self_addr); } +void ToxClient::onToxSelfConnectionStatus(TOX_CONNECTION connection_status) { + std::cout << "TCL self status: "; + switch (connection_status) { + case TOX_CONNECTION::TOX_CONNECTION_NONE: std::cout << "offline\n"; break; + case TOX_CONNECTION::TOX_CONNECTION_TCP: std::cout << "TCP-relayed\n"; break; + case TOX_CONNECTION::TOX_CONNECTION_UDP: std::cout << "UDP-direct\n"; break; + } + _tox_profile_dirty = true; +} + +void ToxClient::onToxFriendRequest(const uint8_t* public_key, std::string_view message) { + std::vector key(public_key, public_key + TOX_PUBLIC_KEY_SIZE); + std::cout << "TCL adding friend " << bin2hex(key) << " (" << message << ")\n"; + + tox_friend_add_norequest(_tox, public_key, nullptr); + _tox_profile_dirty = true; +} + +void ToxClient::onToxGroupCustomPacket(uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length) { +} + +void ToxClient::onToxGroupCustomPrivatePacket(uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length) { +} + +void ToxClient::onToxGroupInvite(uint32_t friend_number, const uint8_t* invite_data, size_t invite_length, std::string_view group_name) { + std::cout << "TCL accepting group invite (" << group_name << ")\n"; + + tox_group_invite_accept(_tox, friend_number, invite_data, invite_length, reinterpret_cast(_self_name.data()), _self_name.size(), nullptr, 0, nullptr); + _tox_profile_dirty = true; +} + void ToxClient::saveToxProfile(void) { if (_tox_profile_path.empty()) { return; diff --git a/src/tox_client.hpp b/src/tox_client.hpp index ad5c736..5cee71b 100644 --- a/src/tox_client.hpp +++ b/src/tox_client.hpp @@ -2,20 +2,30 @@ #include "./command_line.hpp" +#include #include #include #include #include -struct ToxClient final { - ToxClient(const CommandLine& cl); +struct ToxClient { + public: + ToxClient(const CommandLine& cl); - void iterate(void); + void iterate(void); - void setToxProfilePath(const std::string& new_path) { _tox_profile_path = new_path; } + void setToxProfilePath(const std::string& new_path) { _tox_profile_path = new_path; } + + std::string getOwnAddress(void) const; + + public: // tox callbacks + void onToxSelfConnectionStatus(TOX_CONNECTION connection_status); + void onToxFriendRequest(const uint8_t* public_key, std::string_view message); + void onToxGroupCustomPacket(uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length); + void onToxGroupCustomPrivatePacket(uint32_t group_number, uint32_t peer_id, const uint8_t *data, size_t length); + void onToxGroupInvite(uint32_t friend_number, const uint8_t* invite_data, size_t invite_length, std::string_view group_name); - std::string getOwnAddress(void) const; private: void saveToxProfile(void); @@ -25,6 +35,8 @@ struct ToxClient final { NGC_EXT_CTX* _ext_ctx {nullptr}; NGC_FT1* _ft1_ctx {nullptr}; + std::string _self_name; + std::string _tox_profile_path; bool _tox_profile_dirty {false}; // set in callbacks };