wire up some callbacks

This commit is contained in:
Green Sky 2023-01-14 02:03:32 +01:00
parent b7915c55a4
commit 10571ae923
No known key found for this signature in database
7 changed files with 184 additions and 8 deletions

View File

@ -13,6 +13,9 @@ add_executable(tox_ngc_ft1_tool
./tox_utils.hpp ./tox_utils.hpp
./tox_utils.cpp ./tox_utils.cpp
./tox_callbacks.hpp
./tox_callbacks.cpp
./tox_client.hpp ./tox_client.hpp
./tox_client.cpp ./tox_client.cpp
) )

View File

@ -36,6 +36,14 @@ CommandLine::CommandLine(int argc, char** argv) {
return; return;
} }
profile_path = argv[++i]; profile_path = argv[++i];
} else if (arg_sv == "-N") {
if (i+1 >= argc) {
std::cerr << "-N missing <self_name> parameter!\n\n";
printHelp();
_should_exit = true;
return;
}
self_name = argv[++i];
} else if (arg_sv == "-a") { } else if (arg_sv == "-a") {
} else if (arg_sv == "-f") { } else if (arg_sv == "-f") {
if (i+1 >= argc) { if (i+1 >= argc) {
@ -61,12 +69,17 @@ CommandLine::CommandLine(int argc, char** argv) {
return; return;
} }
receive_id = argv[++i]; receive_id = argv[++i];
} else {
std::cerr << "unknown parameter '" << arg_sv << "' !\n\n";
printHelp();
_should_exit = true;
} }
} }
} }
void CommandLine::printHelp(void) { void CommandLine::printHelp(void) {
std::cout std::cout
<< "~~~ HELP ~~~\n"
<< " meta:\n" << " meta:\n"
<< " -v version info\n" << " -v version info\n"
<< " -V verbose\n" << " -V verbose\n"
@ -75,6 +88,7 @@ void CommandLine::printHelp(void) {
<< " connectivity:\n" << " connectivity:\n"
<< " -G <chat_id>\n" << " -G <chat_id>\n"
<< " -F profile.tox\n" << " -F profile.tox\n"
<< " -N <self_name> (defaults to 'tox_ngc_ft1_tool')\n"
<< " will print friend id at startup\n" << " will print friend id at startup\n"
<< " will autoaccept any invite\n" << " will autoaccept any invite\n"
<< " if no -F give, will not save profile.\n" << " if no -F give, will not save profile.\n"

View File

@ -10,6 +10,7 @@
// connectivity: // connectivity:
// -G <chat_id> // -G <chat_id>
// -F profile.tox // -F profile.tox
// -N <self_name>
// will print friend id at startup // will print friend id at startup
// will autoaccept any invite // will autoaccept any invite
// if no -F give, will not save profile. // if no -F give, will not save profile.
@ -41,6 +42,8 @@ struct CommandLine {
std::string chat_id; std::string chat_id;
// -F profile.tox // -F profile.tox
std::string profile_path; std::string profile_path;
// -N <self_name>
std::string self_name {"tox_ngc_tf1_tool"};
// transfer variant: // transfer variant:
// -a id1/sha128_single/sha128_info/sha256_single/sha256_info // -a id1/sha128_single/sha128_info/sha256_single/sha256_info

33
src/tox_callbacks.cpp Normal file
View File

@ -0,0 +1,33 @@
#include "./tox_callbacks.hpp"
#include "./tox_client.hpp"
#include <string_view>
#include <iostream>
// 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<ToxClient*>(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<ToxClient*>(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<ToxClient*>(user_data)->onToxFriendRequest(public_key, std::string_view{reinterpret_cast<const char*>(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<ToxClient*>(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<ToxClient*>(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<ToxClient*>(user_data)->onToxGroupInvite(friend_number, invite_data, length, std::string_view{reinterpret_cast<const char*>(group_name), group_name_length});
}

40
src/tox_callbacks.hpp Normal file
View File

@ -0,0 +1,40 @@
#pragma once
#include <tox/tox.h>
// 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);

View File

@ -1,6 +1,8 @@
#include "./tox_client.hpp" #include "./tox_client.hpp"
#include "./tox_utils.hpp" #include "./tox_utils.hpp"
#include "./tox_callbacks.hpp"
#include "toxcore/tox.h"
#include <vector> #include <vector>
#include <fstream> #include <fstream>
@ -9,6 +11,7 @@
#include <stdexcept> #include <stdexcept>
ToxClient::ToxClient(const CommandLine& cl) : ToxClient::ToxClient(const CommandLine& cl) :
_self_name(cl.self_name),
_tox_profile_path(cl.profile_path) _tox_profile_path(cl.profile_path)
{ {
TOX_ERR_OPTIONS_NEW err_opt_new; 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); assert(err_opt_new == TOX_ERR_OPTIONS_NEW::TOX_ERR_OPTIONS_NEW_OK);
// use cl for options // use cl for options
//tox_options_set_log_callback(options, log_cb); tox_options_set_log_callback(options, log_cb);
tox_options_set_local_discovery_enabled(options, false); tox_options_set_local_discovery_enabled(options, true);
tox_options_set_udp_enabled(options, true); tox_options_set_udp_enabled(options, true);
tox_options_set_hole_punching_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"}; 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<const uint8_t*>(_self_name.data()), _self_name.size(), nullptr);
_tox_profile_dirty = true; _tox_profile_dirty = true;
} }
@ -77,6 +117,37 @@ std::string ToxClient::getOwnAddress(void) const {
return bin2hex(self_addr); 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<uint8_t> 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<const uint8_t*>(_self_name.data()), _self_name.size(), nullptr, 0, nullptr);
_tox_profile_dirty = true;
}
void ToxClient::saveToxProfile(void) { void ToxClient::saveToxProfile(void) {
if (_tox_profile_path.empty()) { if (_tox_profile_path.empty()) {
return; return;

View File

@ -2,13 +2,15 @@
#include "./command_line.hpp" #include "./command_line.hpp"
#include <string_view>
#include <tox/tox.h> #include <tox/tox.h>
#include <ngc_ext.h> #include <ngc_ext.h>
#include <ngc_ft1.h> #include <ngc_ft1.h>
#include <string> #include <string>
struct ToxClient final { struct ToxClient {
public:
ToxClient(const CommandLine& cl); ToxClient(const CommandLine& cl);
void iterate(void); void iterate(void);
@ -17,6 +19,14 @@ struct ToxClient final {
std::string getOwnAddress(void) const; 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);
private: private:
void saveToxProfile(void); void saveToxProfile(void);
@ -25,6 +35,8 @@ struct ToxClient final {
NGC_EXT_CTX* _ext_ctx {nullptr}; NGC_EXT_CTX* _ext_ctx {nullptr};
NGC_FT1* _ft1_ctx {nullptr}; NGC_FT1* _ft1_ctx {nullptr};
std::string _self_name;
std::string _tox_profile_path; std::string _tox_profile_path;
bool _tox_profile_dirty {false}; // set in callbacks bool _tox_profile_dirty {false}; // set in callbacks
}; };