diff --git a/other/analysis/gen-file.sh b/other/analysis/gen-file.sh index 89144eff..8c320acf 100644 --- a/other/analysis/gen-file.sh +++ b/other/analysis/gen-file.sh @@ -40,9 +40,6 @@ echo "#include " >> amalgamation.cc echo "#include " >> amalgamation.cc echo "#include " >> amalgamation.cc -echo "#define TOX_DEFINED" >> amalgamation.cc -echo "typedef struct Messenger Tox;" >> amalgamation.cc - put auto_tests/check_compat.h FIND_QUERY="find . '-(' -name '*.cc' -or -name '*.c' '-)'" diff --git a/toxav/toxav.c b/toxav/toxav.c index 57d14942..f26834b1 100644 --- a/toxav/toxav.c +++ b/toxav/toxav.c @@ -123,13 +123,16 @@ ToxAV *toxav_new(Tox *tox, TOXAV_ERR_NEW *error) { TOXAV_ERR_NEW rc = TOXAV_ERR_NEW_OK; ToxAV *av = nullptr; - Messenger *m = (Messenger *)tox; if (tox == nullptr) { rc = TOXAV_ERR_NEW_NULL; goto END; } + // TODO(iphydf): Don't rely on toxcore internals. + Messenger *m; + m = *(Messenger **)tox; + if (m->msi_packet) { rc = TOXAV_ERR_NEW_MULTIPLE; goto END; diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index cfa76172..7ff2c2b3 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -1985,7 +1985,7 @@ Messenger *new_messenger(Messenger_Options *options, unsigned int *error) return nullptr; } - logger_callback_log(m->log, options->log_callback, m, options->log_user_data); + logger_callback_log(m->log, options->log_callback, options->log_context, options->log_user_data); unsigned int net_err = 0; diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index cd872a2d..548d8654 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -61,6 +61,7 @@ typedef struct Messenger_Options { bool local_discovery_enabled; logger_cb *log_callback; + void *log_context; void *log_user_data; } Messenger_Options; diff --git a/toxcore/tox.c b/toxcore/tox.c index 750a52fa..bf84f022 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -29,13 +29,10 @@ #define _XOPEN_SOURCE 600 #endif -#ifndef TOX_DEFINED -#define TOX_DEFINED -typedef struct Messenger Tox; -#endif #include "tox.h" #include +#include #include #include "Messenger.h" @@ -78,6 +75,240 @@ typedef struct Messenger Tox; #error "TOX_MAX_STATUS_MESSAGE_LENGTH is assumed to be equal to MAX_STATUSMESSAGE_LENGTH" #endif +struct Tox { + Messenger *m; + tox_self_connection_status_cb *self_connection_status_callback; + tox_friend_name_cb *friend_name_callback; + tox_friend_status_message_cb *friend_status_message_callback; + tox_friend_status_cb *friend_status_callback; + tox_friend_connection_status_cb *friend_connection_status_callback; + tox_friend_typing_cb *friend_typing_callback; + tox_friend_read_receipt_cb *friend_read_receipt_callback; + tox_friend_request_cb *friend_request_callback; + tox_friend_message_cb *friend_message_callback; + tox_file_recv_control_cb *file_recv_control_callback; + tox_file_chunk_request_cb *file_chunk_request_callback; + tox_file_recv_cb *file_recv_callback; + tox_file_recv_chunk_cb *file_recv_chunk_callback; + tox_conference_invite_cb *conference_invite_callback; + tox_conference_message_cb *conference_message_callback; + tox_conference_title_cb *conference_title_callback; + tox_conference_peer_name_cb *conference_peer_name_callback; + tox_conference_peer_list_changed_cb *conference_peer_list_changed_callback; + tox_friend_lossy_packet_cb *friend_lossy_packet_callback; + tox_friend_lossless_packet_cb *friend_lossless_packet_callback; +}; + +struct Tox_Userdata { + Tox *tox; + void *user_data; +}; + +static void tox_self_connection_status_handler(Messenger *m, unsigned int connection_status, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->self_connection_status_callback != nullptr) { + tox_data->tox->self_connection_status_callback(tox_data->tox, (Tox_Connection)connection_status, tox_data->user_data); + } +} + +static void tox_friend_name_handler(Messenger *m, uint32_t friend_number, const uint8_t *name, size_t length, + void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->friend_name_callback != nullptr) { + tox_data->tox->friend_name_callback(tox_data->tox, friend_number, name, length, tox_data->user_data); + } +} + +static void tox_friend_status_message_handler(Messenger *m, uint32_t friend_number, const uint8_t *message, + size_t length, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->friend_status_message_callback != nullptr) { + tox_data->tox->friend_status_message_callback(tox_data->tox, friend_number, message, length, tox_data->user_data); + } +} + +static void tox_friend_status_handler(Messenger *m, uint32_t friend_number, unsigned int status, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->friend_status_callback != nullptr) { + tox_data->tox->friend_status_callback(tox_data->tox, friend_number, (Tox_User_Status)status, tox_data->user_data); + } +} + +static void tox_friend_connection_status_handler(Messenger *m, uint32_t friend_number, unsigned int connection_status, + void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->friend_connection_status_callback != nullptr) { + tox_data->tox->friend_connection_status_callback(tox_data->tox, friend_number, (Tox_Connection)connection_status, + tox_data->user_data); + } +} + +static void tox_friend_typing_handler(Messenger *m, uint32_t friend_number, bool is_typing, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->friend_typing_callback != nullptr) { + tox_data->tox->friend_typing_callback(tox_data->tox, friend_number, is_typing, tox_data->user_data); + } +} + +static void tox_friend_read_receipt_handler(Messenger *m, uint32_t friend_number, uint32_t message_id, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->friend_read_receipt_callback != nullptr) { + tox_data->tox->friend_read_receipt_callback(tox_data->tox, friend_number, message_id, tox_data->user_data); + } +} + +static void tox_friend_request_handler(Messenger *m, const uint8_t *public_key, const uint8_t *message, size_t length, + void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->friend_request_callback != nullptr) { + tox_data->tox->friend_request_callback(tox_data->tox, public_key, message, length, tox_data->user_data); + } +} + +static void tox_friend_message_handler(Messenger *m, uint32_t friend_number, unsigned int type, const uint8_t *message, + size_t length, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->friend_message_callback != nullptr) { + tox_data->tox->friend_message_callback(tox_data->tox, friend_number, (Tox_Message_Type)type, message, length, + tox_data->user_data); + } +} + +static void tox_file_recv_control_handler(Messenger *m, uint32_t friend_number, uint32_t file_number, + unsigned int control, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->file_recv_control_callback != nullptr) { + tox_data->tox->file_recv_control_callback(tox_data->tox, friend_number, file_number, (Tox_File_Control)control, + tox_data->user_data); + } +} + +static void tox_file_chunk_request_handler(Messenger *m, uint32_t friend_number, uint32_t file_number, + uint64_t position, size_t length, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->file_chunk_request_callback != nullptr) { + tox_data->tox->file_chunk_request_callback(tox_data->tox, friend_number, file_number, position, length, + tox_data->user_data); + } +} + +static void tox_file_recv_handler(Messenger *m, uint32_t friend_number, uint32_t file_number, uint32_t kind, + uint64_t file_size, const uint8_t *filename, size_t filename_length, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->file_recv_callback != nullptr) { + tox_data->tox->file_recv_callback(tox_data->tox, friend_number, file_number, kind, file_size, filename, filename_length, + tox_data->user_data); + } +} + +static void tox_file_recv_chunk_handler(Messenger *m, uint32_t friend_number, uint32_t file_number, uint64_t position, + const uint8_t *data, size_t length, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->file_recv_chunk_callback != nullptr) { + tox_data->tox->file_recv_chunk_callback(tox_data->tox, friend_number, file_number, position, data, length, + tox_data->user_data); + } +} + +static void tox_conference_invite_handler(Messenger *m, uint32_t friend_number, int type, const uint8_t *cookie, + size_t length, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->conference_invite_callback != nullptr) { + tox_data->tox->conference_invite_callback(tox_data->tox, friend_number, (Tox_Conference_Type)type, cookie, length, + tox_data->user_data); + } +} + +static void tox_conference_message_handler(Messenger *m, uint32_t conference_number, uint32_t peer_number, int type, + const uint8_t *message, size_t length, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->conference_message_callback != nullptr) { + tox_data->tox->conference_message_callback(tox_data->tox, conference_number, peer_number, (Tox_Message_Type)type, + message, length, tox_data->user_data); + } +} + +static void tox_conference_title_handler(Messenger *m, uint32_t conference_number, uint32_t peer_number, + const uint8_t *title, size_t length, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->conference_title_callback != nullptr) { + tox_data->tox->conference_title_callback(tox_data->tox, conference_number, peer_number, title, length, + tox_data->user_data); + } +} + +static void tox_conference_peer_name_handler(Messenger *m, uint32_t conference_number, uint32_t peer_number, + const uint8_t *name, size_t length, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->conference_peer_name_callback != nullptr) { + tox_data->tox->conference_peer_name_callback(tox_data->tox, conference_number, peer_number, name, length, + tox_data->user_data); + } +} + +static void tox_conference_peer_list_changed_handler(Messenger *m, uint32_t conference_number, void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->conference_peer_list_changed_callback != nullptr) { + tox_data->tox->conference_peer_list_changed_callback(tox_data->tox, conference_number, tox_data->user_data); + } +} + +static void tox_friend_lossy_packet_handler(Messenger *m, uint32_t friend_number, const uint8_t *data, size_t length, + void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->friend_lossy_packet_callback != nullptr) { + tox_data->tox->friend_lossy_packet_callback(tox_data->tox, friend_number, data, length, tox_data->user_data); + } +} + +static void tox_friend_lossless_packet_handler(Messenger *m, uint32_t friend_number, const uint8_t *data, size_t length, + void *user_data) +{ + struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data; + + if (tox_data->tox->friend_lossless_packet_callback != nullptr) { + tox_data->tox->friend_lossless_packet_callback(tox_data->tox, friend_number, data, length, tox_data->user_data); + } +} + bool tox_version_is_compatible(uint32_t major, uint32_t minor, uint32_t patch) { @@ -87,6 +318,13 @@ bool tox_version_is_compatible(uint32_t major, uint32_t minor, uint32_t patch) Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) { + Tox *tox = (Tox *)calloc(1, sizeof(Tox)); + + if (tox == nullptr) { + SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); + return nullptr; + } + Messenger_Options m_options = {0}; bool load_savedata_sk = false, load_savedata_tox = false; @@ -103,6 +341,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) case TOX_ERR_OPTIONS_NEW_MALLOC: SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); + free(tox); return nullptr; } } @@ -114,6 +353,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) if (tox_options_get_savedata_data(opts) == nullptr || tox_options_get_savedata_length(opts) == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); tox_options_free(default_options); + free(tox); return nullptr; } } @@ -122,6 +362,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) if (tox_options_get_savedata_length(opts) != TOX_SECRET_KEY_SIZE) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); tox_options_free(default_options); + free(tox); return nullptr; } @@ -130,12 +371,14 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) if (tox_options_get_savedata_length(opts) < TOX_ENC_SAVE_MAGIC_LENGTH) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); tox_options_free(default_options); + free(tox); return nullptr; } if (crypto_memcmp(tox_options_get_savedata_data(opts), TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_ENCRYPTED); tox_options_free(default_options); + free(tox); return nullptr; } @@ -151,6 +394,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) m_options.local_discovery_enabled = tox_options_get_local_discovery_enabled(opts); m_options.log_callback = (logger_cb *)tox_options_get_log_callback(opts); + m_options.log_context = tox; m_options.log_user_data = tox_options_get_log_user_data(opts); switch (tox_options_get_proxy_type(opts)) { @@ -169,6 +413,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) default: SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_TYPE); tox_options_free(default_options); + free(tox); return nullptr; } @@ -176,6 +421,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) if (tox_options_get_proxy_port(opts) == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_PORT); tox_options_free(default_options); + free(tox); return nullptr; } @@ -189,6 +435,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_HOST); // TODO(irungentoo): TOX_ERR_NEW_PROXY_NOT_FOUND if domain. tox_options_free(default_options); + free(tox); return nullptr; } @@ -196,8 +443,11 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) } unsigned int m_error; - Messenger *m = new_messenger(&m_options, &m_error); + Messenger *const m = new_messenger(&m_options, &m_error); + tox->m = m; + // TODO(iphydf): Clarify this code, check for NULL before new_groupchats, so + // new_groupchats can assume m is non-NULL. if (!new_groupchats(m)) { kill_messenger(m); @@ -210,6 +460,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) } tox_options_free(default_options); + free(tox); return nullptr; } @@ -223,8 +474,29 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) SET_ERROR_PARAMETER(error, TOX_ERR_NEW_OK); } + m_callback_namechange(m, tox_friend_name_handler); + m_callback_core_connection(m, tox_self_connection_status_handler); + m_callback_statusmessage(m, tox_friend_status_message_handler); + m_callback_userstatus(m, tox_friend_status_handler); + m_callback_connectionstatus(m, tox_friend_connection_status_handler); + m_callback_typingchange(m, tox_friend_typing_handler); + m_callback_read_receipt(m, tox_friend_read_receipt_handler); + m_callback_friendrequest(m, tox_friend_request_handler); + m_callback_friendmessage(m, tox_friend_message_handler); + callback_file_control(m, tox_file_recv_control_handler); + callback_file_reqchunk(m, tox_file_chunk_request_handler); + callback_file_sendrequest(m, tox_file_recv_handler); + callback_file_data(m, tox_file_recv_chunk_handler); + g_callback_group_invite((Group_Chats *)m->conferences_object, tox_conference_invite_handler); + g_callback_group_message((Group_Chats *)m->conferences_object, tox_conference_message_handler); + g_callback_group_title((Group_Chats *)m->conferences_object, tox_conference_title_handler); + g_callback_peer_name((Group_Chats *)m->conferences_object, tox_conference_peer_name_handler); + g_callback_peer_list_changed((Group_Chats *)m->conferences_object, tox_conference_peer_list_changed_handler); + custom_lossy_packet_registerhandler(m, tox_friend_lossy_packet_handler); + custom_lossless_packet_registerhandler(m, tox_friend_lossless_packet_handler); + tox_options_free(default_options); - return m; + return tox; } void tox_kill(Tox *tox) @@ -233,21 +505,22 @@ void tox_kill(Tox *tox) return; } - Messenger *m = tox; + Messenger *m = tox->m; kill_groupchats((Group_Chats *)m->conferences_object); kill_messenger(m); + free(tox); } size_t tox_get_savedata_size(const Tox *tox) { - const Messenger *m = tox; + const Messenger *m = tox->m; return messenger_size(m); } void tox_get_savedata(const Tox *tox, uint8_t *savedata) { if (savedata) { - const Messenger *m = tox; + const Messenger *m = tox->m; messenger_save(m, savedata); } } @@ -279,7 +552,7 @@ bool tox_bootstrap(Tox *tox, const char *host, uint16_t port, const uint8_t *pub for (i = 0; i < count; ++i) { root[i].port = net_htons(port); - Messenger *m = tox; + Messenger *m = tox->m; onion_add_bs_path_node(m->onion_c, root[i], public_key); dht_bootstrap(m->dht, root[i], public_key); } @@ -323,7 +596,7 @@ bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t for (i = 0; i < count; ++i) { root[i].port = net_htons(port); - Messenger *m = tox; + Messenger *m = tox->m; add_tcp_relay(m->net_crypto, root[i], public_key); } @@ -340,7 +613,7 @@ bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t Tox_Connection tox_self_get_connection_status(const Tox *tox) { - const Messenger *m = tox; + const Messenger *m = tox->m; unsigned int ret = onion_connection_status(m->onion_c); @@ -358,46 +631,46 @@ Tox_Connection tox_self_get_connection_status(const Tox *tox) void tox_callback_self_connection_status(Tox *tox, tox_self_connection_status_cb *callback) { - Messenger *m = tox; - m_callback_core_connection(m, (m_self_connection_status_cb *)callback); + tox->self_connection_status_callback = callback; } uint32_t tox_iteration_interval(const Tox *tox) { - const Messenger *m = tox; + const Messenger *m = tox->m; return messenger_run_interval(m); } void tox_iterate(Tox *tox, void *user_data) { - Messenger *m = tox; - do_messenger(m, user_data); - do_groupchats((Group_Chats *)m->conferences_object, user_data); + Messenger *m = tox->m; + struct Tox_Userdata tox_data = { tox, user_data }; + do_messenger(m, &tox_data); + do_groupchats((Group_Chats *)m->conferences_object, &tox_data); } void tox_self_get_address(const Tox *tox, uint8_t *address) { if (address) { - const Messenger *m = tox; + const Messenger *m = tox->m; getaddress(m, address); } } void tox_self_set_nospam(Tox *tox, uint32_t nospam) { - Messenger *m = tox; + Messenger *m = tox->m; set_nospam(m->fr, net_htonl(nospam)); } uint32_t tox_self_get_nospam(const Tox *tox) { - const Messenger *m = tox; + const Messenger *m = tox->m; return net_ntohl(get_nospam(m->fr)); } void tox_self_get_public_key(const Tox *tox, uint8_t *public_key) { - const Messenger *m = tox; + const Messenger *m = tox->m; if (public_key) { memcpy(public_key, nc_get_self_public_key(m->net_crypto), CRYPTO_PUBLIC_KEY_SIZE); @@ -406,7 +679,7 @@ void tox_self_get_public_key(const Tox *tox, uint8_t *public_key) void tox_self_get_secret_key(const Tox *tox, uint8_t *secret_key) { - const Messenger *m = tox; + const Messenger *m = tox->m; if (secret_key) { memcpy(secret_key, nc_get_self_secret_key(m->net_crypto), CRYPTO_SECRET_KEY_SIZE); @@ -420,7 +693,7 @@ bool tox_self_set_name(Tox *tox, const uint8_t *name, size_t length, Tox_Err_Set return 0; } - Messenger *m = tox; + Messenger *m = tox->m; if (setname(m, name, length) == 0) { // TODO(irungentoo): function to set different per group names? @@ -435,14 +708,14 @@ bool tox_self_set_name(Tox *tox, const uint8_t *name, size_t length, Tox_Err_Set size_t tox_self_get_name_size(const Tox *tox) { - const Messenger *m = tox; + const Messenger *m = tox->m; return m_get_self_name_size(m); } void tox_self_get_name(const Tox *tox, uint8_t *name) { if (name) { - const Messenger *m = tox; + const Messenger *m = tox->m; getself_name(m, name); } } @@ -454,7 +727,7 @@ bool tox_self_set_status_message(Tox *tox, const uint8_t *status_message, size_t return 0; } - Messenger *m = tox; + Messenger *m = tox->m; if (m_set_statusmessage(m, status_message, length) == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_OK); @@ -467,27 +740,27 @@ bool tox_self_set_status_message(Tox *tox, const uint8_t *status_message, size_t size_t tox_self_get_status_message_size(const Tox *tox) { - const Messenger *m = tox; + const Messenger *m = tox->m; return m_get_self_statusmessage_size(m); } void tox_self_get_status_message(const Tox *tox, uint8_t *status_message) { if (status_message) { - const Messenger *m = tox; + const Messenger *m = tox->m; m_copy_self_statusmessage(m, status_message); } } void tox_self_set_status(Tox *tox, Tox_User_Status status) { - Messenger *m = tox; + Messenger *m = tox->m; m_set_userstatus(m, status); } Tox_User_Status tox_self_get_status(const Tox *tox) { - const Messenger *m = tox; + const Messenger *m = tox->m; const uint8_t status = m_get_self_userstatus(m); return (Tox_User_Status)status; } @@ -538,7 +811,7 @@ uint32_t tox_friend_add(Tox *tox, const uint8_t *address, const uint8_t *message return UINT32_MAX; } - Messenger *m = tox; + Messenger *m = tox->m; int32_t ret = m_addfriend(m, address, message, length); if (ret >= 0) { @@ -557,7 +830,7 @@ uint32_t tox_friend_add_norequest(Tox *tox, const uint8_t *public_key, Tox_Err_F return UINT32_MAX; } - Messenger *m = tox; + Messenger *m = tox->m; int32_t ret = m_addfriend_norequest(m, public_key); if (ret >= 0) { @@ -571,7 +844,7 @@ uint32_t tox_friend_add_norequest(Tox *tox, const uint8_t *public_key, Tox_Err_F bool tox_friend_delete(Tox *tox, uint32_t friend_number, Tox_Err_Friend_Delete *error) { - Messenger *m = tox; + Messenger *m = tox->m; int ret = m_delfriend(m, friend_number); // TODO(irungentoo): handle if realloc fails? @@ -591,7 +864,7 @@ uint32_t tox_friend_by_public_key(const Tox *tox, const uint8_t *public_key, Tox return UINT32_MAX; } - const Messenger *m = tox; + const Messenger *m = tox->m; int32_t ret = getfriend_id(m, public_key); if (ret == -1) { @@ -610,7 +883,7 @@ bool tox_friend_get_public_key(const Tox *tox, uint32_t friend_number, uint8_t * return 0; } - const Messenger *m = tox; + const Messenger *m = tox->m; if (get_real_pk(m, friend_number, public_key) == -1) { SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_GET_PUBLIC_KEY_FRIEND_NOT_FOUND); @@ -623,13 +896,13 @@ bool tox_friend_get_public_key(const Tox *tox, uint32_t friend_number, uint8_t * bool tox_friend_exists(const Tox *tox, uint32_t friend_number) { - const Messenger *m = tox; + const Messenger *m = tox->m; return m_friend_exists(m, friend_number); } uint64_t tox_friend_get_last_online(const Tox *tox, uint32_t friend_number, Tox_Err_Friend_Get_Last_Online *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; uint64_t timestamp = m_get_last_online(m, friend_number); if (timestamp == UINT64_MAX) { @@ -643,14 +916,14 @@ uint64_t tox_friend_get_last_online(const Tox *tox, uint32_t friend_number, Tox_ size_t tox_self_get_friend_list_size(const Tox *tox) { - const Messenger *m = tox; + const Messenger *m = tox->m; return count_friendlist(m); } void tox_self_get_friend_list(const Tox *tox, uint32_t *friend_list) { if (friend_list) { - const Messenger *m = tox; + const Messenger *m = tox->m; // TODO(irungentoo): size parameter? copy_friendlist(m, friend_list, tox_self_get_friend_list_size(tox)); } @@ -658,7 +931,7 @@ void tox_self_get_friend_list(const Tox *tox, uint32_t *friend_list) size_t tox_friend_get_name_size(const Tox *tox, uint32_t friend_number, Tox_Err_Friend_Query *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = m_get_name_size(m, friend_number); if (ret == -1) { @@ -677,7 +950,7 @@ bool tox_friend_get_name(const Tox *tox, uint32_t friend_number, uint8_t *name, return 0; } - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = getname(m, friend_number, name); if (ret == -1) { @@ -691,13 +964,12 @@ bool tox_friend_get_name(const Tox *tox, uint32_t friend_number, uint8_t *name, void tox_callback_friend_name(Tox *tox, tox_friend_name_cb *callback) { - Messenger *m = tox; - m_callback_namechange(m, callback); + tox->friend_name_callback = callback; } size_t tox_friend_get_status_message_size(const Tox *tox, uint32_t friend_number, Tox_Err_Friend_Query *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = m_get_statusmessage_size(m, friend_number); if (ret == -1) { @@ -717,7 +989,7 @@ bool tox_friend_get_status_message(const Tox *tox, uint32_t friend_number, uint8 return 0; } - const Messenger *m = tox; + const Messenger *m = tox->m; // TODO(irungentoo): size parameter? int ret = m_copy_statusmessage(m, friend_number, status_message, m_get_statusmessage_size(m, friend_number)); @@ -732,13 +1004,12 @@ bool tox_friend_get_status_message(const Tox *tox, uint32_t friend_number, uint8 void tox_callback_friend_status_message(Tox *tox, tox_friend_status_message_cb *callback) { - Messenger *m = tox; - m_callback_statusmessage(m, callback); + tox->friend_status_message_callback = callback; } Tox_User_Status tox_friend_get_status(const Tox *tox, uint32_t friend_number, Tox_Err_Friend_Query *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = m_get_userstatus(m, friend_number); @@ -753,13 +1024,12 @@ Tox_User_Status tox_friend_get_status(const Tox *tox, uint32_t friend_number, To void tox_callback_friend_status(Tox *tox, tox_friend_status_cb *callback) { - Messenger *m = tox; - m_callback_userstatus(m, (m_friend_status_cb *)callback); + tox->friend_status_callback = callback; } Tox_Connection tox_friend_get_connection_status(const Tox *tox, uint32_t friend_number, Tox_Err_Friend_Query *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = m_get_friend_connectionstatus(m, friend_number); @@ -774,13 +1044,12 @@ Tox_Connection tox_friend_get_connection_status(const Tox *tox, uint32_t friend_ void tox_callback_friend_connection_status(Tox *tox, tox_friend_connection_status_cb *callback) { - Messenger *m = tox; - m_callback_connectionstatus(m, (m_friend_connection_status_cb *)callback); + tox->friend_connection_status_callback = callback; } bool tox_friend_get_typing(const Tox *tox, uint32_t friend_number, Tox_Err_Friend_Query *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = m_get_istyping(m, friend_number); if (ret == -1) { @@ -794,13 +1063,12 @@ bool tox_friend_get_typing(const Tox *tox, uint32_t friend_number, Tox_Err_Frien void tox_callback_friend_typing(Tox *tox, tox_friend_typing_cb *callback) { - Messenger *m = tox; - m_callback_typingchange(m, callback); + tox->friend_typing_callback = callback; } bool tox_self_set_typing(Tox *tox, uint32_t friend_number, bool typing, Tox_Err_Set_Typing *error) { - Messenger *m = tox; + Messenger *m = tox->m; if (m_set_usertyping(m, friend_number, typing) == -1) { SET_ERROR_PARAMETER(error, TOX_ERR_SET_TYPING_FRIEND_NOT_FOUND); @@ -855,7 +1123,7 @@ uint32_t tox_friend_send_message(Tox *tox, uint32_t friend_number, Tox_Message_T return 0; } - Messenger *m = tox; + Messenger *m = tox->m; uint32_t message_id = 0; set_message_error(m_send_message_generic(m, friend_number, type, message, length, &message_id), error); return message_id; @@ -863,20 +1131,17 @@ uint32_t tox_friend_send_message(Tox *tox, uint32_t friend_number, Tox_Message_T void tox_callback_friend_read_receipt(Tox *tox, tox_friend_read_receipt_cb *callback) { - Messenger *m = tox; - m_callback_read_receipt(m, callback); + tox->friend_read_receipt_callback = callback; } void tox_callback_friend_request(Tox *tox, tox_friend_request_cb *callback) { - Messenger *m = tox; - m_callback_friendrequest(m, callback); + tox->friend_request_callback = callback; } void tox_callback_friend_message(Tox *tox, tox_friend_message_cb *callback) { - Messenger *m = tox; - m_callback_friendmessage(m, (m_friend_message_cb *)callback); + tox->friend_message_callback = callback; } bool tox_hash(uint8_t *hash, const uint8_t *data, size_t length) @@ -892,7 +1157,7 @@ bool tox_hash(uint8_t *hash, const uint8_t *data, size_t length) bool tox_file_control(Tox *tox, uint32_t friend_number, uint32_t file_number, Tox_File_Control control, Tox_Err_File_Control *error) { - Messenger *m = tox; + Messenger *m = tox->m; int ret = file_control(m, friend_number, file_number, control); if (ret == 0) { @@ -941,7 +1206,7 @@ bool tox_file_control(Tox *tox, uint32_t friend_number, uint32_t file_number, To bool tox_file_seek(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, Tox_Err_File_Seek *error) { - Messenger *m = tox; + Messenger *m = tox->m; int ret = file_seek(m, friend_number, file_number, position); if (ret == 0) { @@ -982,8 +1247,7 @@ bool tox_file_seek(Tox *tox, uint32_t friend_number, uint32_t file_number, uint6 void tox_callback_file_recv_control(Tox *tox, tox_file_recv_control_cb *callback) { - Messenger *m = tox; - callback_file_control(m, (m_file_recv_control_cb *)callback); + tox->file_recv_control_callback = callback; } bool tox_file_get_file_id(const Tox *tox, uint32_t friend_number, uint32_t file_number, uint8_t *file_id, @@ -994,7 +1258,7 @@ bool tox_file_get_file_id(const Tox *tox, uint32_t friend_number, uint32_t file_ return 0; } - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = file_get_id(m, friend_number, file_number, file_id); if (ret == 0) { @@ -1027,7 +1291,7 @@ uint32_t tox_file_send(Tox *tox, uint32_t friend_number, uint32_t kind, uint64_t file_id = f_id; } - Messenger *m = tox; + Messenger *m = tox->m; long int file_num = new_filesender(m, friend_number, kind, file_size, file_id, filename, filename_length); if (file_num >= 0) { @@ -1060,7 +1324,7 @@ uint32_t tox_file_send(Tox *tox, uint32_t friend_number, uint32_t kind, uint64_t bool tox_file_send_chunk(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, const uint8_t *data, size_t length, Tox_Err_File_Send_Chunk *error) { - Messenger *m = tox; + Messenger *m = tox->m; int ret = file_data(m, friend_number, file_number, position, data, length); if (ret == 0) { @@ -1104,55 +1368,47 @@ bool tox_file_send_chunk(Tox *tox, uint32_t friend_number, uint32_t file_number, void tox_callback_file_chunk_request(Tox *tox, tox_file_chunk_request_cb *callback) { - Messenger *m = tox; - callback_file_reqchunk(m, callback); + tox->file_chunk_request_callback = callback; } void tox_callback_file_recv(Tox *tox, tox_file_recv_cb *callback) { - Messenger *m = tox; - callback_file_sendrequest(m, callback); + tox->file_recv_callback = callback; } void tox_callback_file_recv_chunk(Tox *tox, tox_file_recv_chunk_cb *callback) { - Messenger *m = tox; - callback_file_data(m, callback); + tox->file_recv_chunk_callback = callback; } void tox_callback_conference_invite(Tox *tox, tox_conference_invite_cb *callback) { - Messenger *m = tox; - g_callback_group_invite((Group_Chats *)m->conferences_object, (g_conference_invite_cb *)callback); + tox->conference_invite_callback = callback; } void tox_callback_conference_message(Tox *tox, tox_conference_message_cb *callback) { - Messenger *m = tox; - g_callback_group_message((Group_Chats *)m->conferences_object, (g_conference_message_cb *)callback); + tox->conference_message_callback = callback; } void tox_callback_conference_title(Tox *tox, tox_conference_title_cb *callback) { - Messenger *m = tox; - g_callback_group_title((Group_Chats *)m->conferences_object, callback); + tox->conference_title_callback = callback; } void tox_callback_conference_peer_name(Tox *tox, tox_conference_peer_name_cb *callback) { - Messenger *m = tox; - g_callback_peer_name((Group_Chats *)m->conferences_object, callback); + tox->conference_peer_name_callback = callback; } void tox_callback_conference_peer_list_changed(Tox *tox, tox_conference_peer_list_changed_cb *callback) { - Messenger *m = tox; - g_callback_peer_list_changed((Group_Chats *)m->conferences_object, callback); + tox->conference_peer_list_changed_callback = callback; } uint32_t tox_conference_new(Tox *tox, Tox_Err_Conference_New *error) { - Messenger *m = tox; + Messenger *m = tox->m; int ret = add_groupchat((Group_Chats *)m->conferences_object, GROUPCHAT_TYPE_TEXT); if (ret == -1) { @@ -1166,7 +1422,7 @@ uint32_t tox_conference_new(Tox *tox, Tox_Err_Conference_New *error) bool tox_conference_delete(Tox *tox, uint32_t conference_number, Tox_Err_Conference_Delete *error) { - Messenger *m = tox; + Messenger *m = tox->m; int ret = del_groupchat((Group_Chats *)m->conferences_object, conference_number); if (ret == -1) { @@ -1180,7 +1436,7 @@ bool tox_conference_delete(Tox *tox, uint32_t conference_number, Tox_Err_Confere uint32_t tox_conference_peer_count(const Tox *tox, uint32_t conference_number, Tox_Err_Conference_Peer_Query *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = group_number_peers((Group_Chats *)m->conferences_object, conference_number); if (ret == -1) { @@ -1195,7 +1451,7 @@ uint32_t tox_conference_peer_count(const Tox *tox, uint32_t conference_number, T size_t tox_conference_peer_get_name_size(const Tox *tox, uint32_t conference_number, uint32_t peer_number, Tox_Err_Conference_Peer_Query *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = group_peername_size((Group_Chats *)m->conferences_object, conference_number, peer_number); switch (ret) { @@ -1215,7 +1471,7 @@ size_t tox_conference_peer_get_name_size(const Tox *tox, uint32_t conference_num bool tox_conference_peer_get_name(const Tox *tox, uint32_t conference_number, uint32_t peer_number, uint8_t *name, Tox_Err_Conference_Peer_Query *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = group_peername((Group_Chats *)m->conferences_object, conference_number, peer_number, name); switch (ret) { @@ -1235,7 +1491,7 @@ bool tox_conference_peer_get_name(const Tox *tox, uint32_t conference_number, ui bool tox_conference_peer_get_public_key(const Tox *tox, uint32_t conference_number, uint32_t peer_number, uint8_t *public_key, Tox_Err_Conference_Peer_Query *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = group_peer_pubkey((Group_Chats *)m->conferences_object, conference_number, peer_number, public_key); switch (ret) { @@ -1255,7 +1511,7 @@ bool tox_conference_peer_get_public_key(const Tox *tox, uint32_t conference_numb bool tox_conference_peer_number_is_ours(const Tox *tox, uint32_t conference_number, uint32_t peer_number, Tox_Err_Conference_Peer_Query *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = group_peernumber_is_ours((Group_Chats *)m->conferences_object, conference_number, peer_number); switch (ret) { @@ -1279,7 +1535,7 @@ bool tox_conference_peer_number_is_ours(const Tox *tox, uint32_t conference_numb bool tox_conference_invite(Tox *tox, uint32_t friend_number, uint32_t conference_number, Tox_Err_Conference_Invite *error) { - Messenger *m = tox; + Messenger *m = tox->m; int ret = invite_friend((Group_Chats *)m->conferences_object, friend_number, conference_number); switch (ret) { @@ -1303,7 +1559,7 @@ bool tox_conference_invite(Tox *tox, uint32_t friend_number, uint32_t conference uint32_t tox_conference_join(Tox *tox, uint32_t friend_number, const uint8_t *cookie, size_t length, Tox_Err_Conference_Join *error) { - Messenger *m = tox; + Messenger *m = tox->m; int ret = join_groupchat((Group_Chats *)m->conferences_object, friend_number, GROUPCHAT_TYPE_TEXT, cookie, length); switch (ret) { @@ -1339,7 +1595,7 @@ uint32_t tox_conference_join(Tox *tox, uint32_t friend_number, const uint8_t *co bool tox_conference_send_message(Tox *tox, uint32_t conference_number, Tox_Message_Type type, const uint8_t *message, size_t length, Tox_Err_Conference_Send_Message *error) { - Messenger *m = tox; + Messenger *m = tox->m; int ret = 0; if (type == TOX_MESSAGE_TYPE_NORMAL) { @@ -1372,7 +1628,7 @@ bool tox_conference_send_message(Tox *tox, uint32_t conference_number, Tox_Messa size_t tox_conference_get_title_size(const Tox *tox, uint32_t conference_number, Tox_Err_Conference_Title *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = group_title_get_size((Group_Chats *)m->conferences_object, conference_number); switch (ret) { @@ -1392,7 +1648,7 @@ size_t tox_conference_get_title_size(const Tox *tox, uint32_t conference_number, bool tox_conference_get_title(const Tox *tox, uint32_t conference_number, uint8_t *title, Tox_Err_Conference_Title *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = group_title_get((Group_Chats *)m->conferences_object, conference_number, title); switch (ret) { @@ -1412,7 +1668,7 @@ bool tox_conference_get_title(const Tox *tox, uint32_t conference_number, uint8_ bool tox_conference_set_title(Tox *tox, uint32_t conference_number, const uint8_t *title, size_t length, Tox_Err_Conference_Title *error) { - Messenger *m = tox; + Messenger *m = tox->m; int ret = group_title_send((Group_Chats *)m->conferences_object, conference_number, title, length); switch (ret) { @@ -1435,13 +1691,13 @@ bool tox_conference_set_title(Tox *tox, uint32_t conference_number, const uint8_ size_t tox_conference_get_chatlist_size(const Tox *tox) { - const Messenger *m = tox; + const Messenger *m = tox->m; return count_chatlist((Group_Chats *)m->conferences_object); } void tox_conference_get_chatlist(const Tox *tox, uint32_t *chatlist) { - const Messenger *m = tox; + const Messenger *m = tox->m; size_t list_size = tox_conference_get_chatlist_size(tox); copy_chatlist((Group_Chats *)m->conferences_object, chatlist, list_size); } @@ -1449,7 +1705,7 @@ void tox_conference_get_chatlist(const Tox *tox, uint32_t *chatlist) Tox_Conference_Type tox_conference_get_type(const Tox *tox, uint32_t conference_number, Tox_Err_Conference_Get_Type *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; int ret = group_get_type((Group_Chats *)m->conferences_object, conference_number); if (ret == -1) { @@ -1463,8 +1719,7 @@ Tox_Conference_Type tox_conference_get_type(const Tox *tox, uint32_t conference_ bool tox_conference_get_id(const Tox *tox, uint32_t conference_number, uint8_t *id /* TOX_CONFERENCE_ID_SIZE bytes */) { - const Messenger *m = tox; - return conference_get_uid((Group_Chats *)m->conferences_object, conference_number, id); + return conference_get_uid((Group_Chats *)tox->m->conferences_object, conference_number, id); } // TODO(iphydf): Delete in 0.3.0. @@ -1480,8 +1735,7 @@ uint32_t tox_conference_by_id(const Tox *tox, const uint8_t *id, Tox_Err_Confere return UINT32_MAX; } - const Messenger *m = tox; - int32_t ret = conference_by_uid((Group_Chats *)m->conferences_object, id); + int32_t ret = conference_by_uid((Group_Chats *)tox->m->conferences_object, id); if (ret == -1) { SET_ERROR_PARAMETER(error, TOX_ERR_CONFERENCE_BY_ID_NOT_FOUND); @@ -1552,7 +1806,7 @@ bool tox_friend_send_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_ return 0; } - Messenger *m = tox; + Messenger *m = tox->m; if (length == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_EMPTY); @@ -1579,8 +1833,7 @@ bool tox_friend_send_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_ void tox_callback_friend_lossy_packet(Tox *tox, tox_friend_lossy_packet_cb *callback) { - Messenger *m = tox; - custom_lossy_packet_registerhandler(m, callback); + tox->friend_lossy_packet_callback = callback; } bool tox_friend_send_lossless_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, @@ -1591,7 +1844,7 @@ bool tox_friend_send_lossless_packet(Tox *tox, uint32_t friend_number, const uin return 0; } - Messenger *m = tox; + Messenger *m = tox->m; if (length == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_EMPTY); @@ -1611,21 +1864,20 @@ bool tox_friend_send_lossless_packet(Tox *tox, uint32_t friend_number, const uin void tox_callback_friend_lossless_packet(Tox *tox, tox_friend_lossless_packet_cb *callback) { - Messenger *m = tox; - custom_lossless_packet_registerhandler(m, callback); + tox->friend_lossless_packet_callback = callback; } void tox_self_get_dht_id(const Tox *tox, uint8_t *dht_id) { if (dht_id) { - const Messenger *m = tox; + const Messenger *m = tox->m; memcpy(dht_id, dht_get_self_public_key(m->dht), CRYPTO_PUBLIC_KEY_SIZE); } } uint16_t tox_self_get_udp_port(const Tox *tox, Tox_Err_Get_Port *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; uint16_t port = net_htons(net_port(m->net)); if (port) { @@ -1639,7 +1891,7 @@ uint16_t tox_self_get_udp_port(const Tox *tox, Tox_Err_Get_Port *error) uint16_t tox_self_get_tcp_port(const Tox *tox, Tox_Err_Get_Port *error) { - const Messenger *m = tox; + const Messenger *m = tox->m; if (m->tcp_server) { SET_ERROR_PARAMETER(error, TOX_ERR_GET_PORT_OK);