From 5babb281c05a48c0b4bbfe3c6be1527f439beec2 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sun, 16 Mar 2014 13:24:39 -0400 Subject: [PATCH] Friend request callback now contains the Tox object. --- auto_tests/friends_test.c | 2 +- auto_tests/tox_test.c | 15 +++++++++------ testing/Messenger_test.c | 2 +- testing/nTox.c | 2 +- toxav/phone.c | 2 +- toxcore/Messenger.c | 6 ++++-- toxcore/Messenger.h | 3 ++- toxcore/friend_requests.c | 8 +++++--- toxcore/friend_requests.h | 7 ++++--- toxcore/network.c | 2 +- toxcore/tox.c | 3 ++- toxcore/tox.h | 8 ++++---- 12 files changed, 35 insertions(+), 25 deletions(-) diff --git a/auto_tests/friends_test.c b/auto_tests/friends_test.c index 5071319e..2448f97c 100644 --- a/auto_tests/friends_test.c +++ b/auto_tests/friends_test.c @@ -116,7 +116,7 @@ int parent_friend_request(DHT *dht) return 0; } -void child_got_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) +void child_got_request(Messenger *m, uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) { fputs("OK\nsending status to parent", stdout); fflush(stdout); diff --git a/auto_tests/tox_test.c b/auto_tests/tox_test.c index 7e74fba7..e8c10b7e 100644 --- a/auto_tests/tox_test.c +++ b/auto_tests/tox_test.c @@ -19,12 +19,13 @@ #define c_sleep(x) usleep(1000*x) #endif -void accept_friend_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) +void accept_friend_request(Tox *m, uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) { - Tox *t = userdata; + if (*((uint32_t *)userdata) != 974536) + return; if (length == 7 && memcmp("Gentoo", data, 7) == 0) { - tox_add_friend_norequest(t, public_key); + tox_add_friend_norequest(m, public_key); } } uint32_t messages_received; @@ -114,7 +115,8 @@ START_TEST(test_few_clients) Tox *tox2 = tox_new(TOX_ENABLE_IPV6_DEFAULT); Tox *tox3 = tox_new(TOX_ENABLE_IPV6_DEFAULT); ck_assert_msg(tox1 || tox2 || tox3, "Failed to create 3 tox instances"); - tox_callback_friend_request(tox2, accept_friend_request, tox2); + uint32_t to_compare = 974536; + tox_callback_friend_request(tox2, accept_friend_request, &to_compare); uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; tox_get_address(tox2, address); int test = tox_add_friend(tox3, address, (uint8_t *)"Gentoo", 7); @@ -140,7 +142,7 @@ START_TEST(test_few_clients) } printf("tox clients connected\n"); - uint32_t to_compare = 974536; + to_compare = 974536; tox_callback_friend_message(tox3, print_message, &to_compare); tox_send_message(tox2, 0, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); @@ -267,7 +269,8 @@ START_TEST(test_many_clients) for (i = 0; i < NUM_TOXES; ++i) { toxes[i] = tox_new(TOX_ENABLE_IPV6_DEFAULT); ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); - tox_callback_friend_request(toxes[i], accept_friend_request, toxes[i]); + uint32_t to_comp = 974536; + tox_callback_friend_request(toxes[i], accept_friend_request, &to_comp); } struct { diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index 895a23d9..b875f832 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c @@ -66,7 +66,7 @@ void print_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t len * networking_requesthandler and so cannot take a Messenger * */ static Messenger *m; -void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) +void print_request(Messenger *m, uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) { printf("Friend request received from: \n"); printf("ClientID: "); diff --git a/testing/nTox.c b/testing/nTox.c index 343db236..dc9d5113 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -862,7 +862,7 @@ void do_refresh() refresh(); } -void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) +void print_request(Tox *m, uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) { new_lines("[i] received friend request with message:"); new_lines((char *)data); diff --git a/toxav/phone.c b/toxav/phone.c index 98e97873..bceaad9f 100644 --- a/toxav/phone.c +++ b/toxav/phone.c @@ -1064,7 +1064,7 @@ int av_terminate_session(av_session_t *_phone) /****** AV HELPER FUNCTIONS ******/ /* Auto accept friend request */ -void av_friend_requ(uint8_t *_public_key, uint8_t *_data, uint16_t _length, void *_userdata) +void av_friend_requ(Tox *_messenger, uint8_t *_public_key, uint8_t *_data, uint16_t _length, void *_userdata) { av_session_t *_phone = _userdata; av_allocate_friend (_phone, -1, 0); diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index e48fa5fc..7ce6bb3c 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -721,9 +721,11 @@ void m_set_sends_receipts(Messenger *m, int32_t friendnumber, int yesno) /* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); */ /* Set the function that will be executed when a friend request is received. */ -void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata) +void m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, uint8_t *, uint8_t *, uint16_t, void *), + void *userdata) { - callback_friendrequest(&(m->fr), function, userdata); + void (*handle_friendrequest)(void *, uint8_t *, uint8_t *, uint16_t, void *) = function; + callback_friendrequest(&(m->fr), handle_friendrequest, m, userdata); } /* Set the function that will be executed when a message from a friend is received. */ diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 32b1f1b3..9e91ccee 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -437,7 +437,8 @@ void m_set_sends_receipts(Messenger *m, int32_t friendnumber, int yesno); /* Set the function that will be executed when a friend request is received. * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ -void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata); +void m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, uint8_t *, uint8_t *, uint16_t, void *), + void *userdata); /* Set the function that will be executed when a message from a friend is received. * Function format is: function(int32_t friendnumber, uint8_t * message, uint32_t length) diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c index 987b1a4a..7574a881 100644 --- a/toxcore/friend_requests.c +++ b/toxcore/friend_requests.c @@ -72,11 +72,12 @@ uint32_t get_nospam(Friend_Requests *fr) /* Set the function that will be executed when a friend request is received. */ -void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), - void *userdata) +void callback_friendrequest(Friend_Requests *fr, void (*function)(void *, uint8_t *, uint8_t *, uint16_t, void *), + void *object, void *userdata) { fr->handle_friendrequest = function; fr->handle_friendrequest_isset = 1; + fr->handle_friendrequest_object = object; fr->handle_friendrequest_userdata = userdata; } /* Set the function used to check if a friend request should be displayed to the user or not. */ @@ -145,7 +146,8 @@ static int friendreq_handlepacket(void *object, uint8_t *source_pubkey, uint8_t memcpy(message, packet + 4, length - 4); message[sizeof(message) - 1] = 0; /* Be sure the message is null terminated. */ - (*fr->handle_friendrequest)(source_pubkey, message, length - 4, fr->handle_friendrequest_userdata); + (*fr->handle_friendrequest)(fr->handle_friendrequest_object, source_pubkey, message, length - 4, + fr->handle_friendrequest_userdata); return 0; } diff --git a/toxcore/friend_requests.h b/toxcore/friend_requests.h index 732dc4a2..722c7431 100644 --- a/toxcore/friend_requests.h +++ b/toxcore/friend_requests.h @@ -30,8 +30,9 @@ typedef struct { uint32_t nospam; - void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t, void *); + void (*handle_friendrequest)(void *, uint8_t *, uint8_t *, uint16_t, void *); uint8_t handle_friendrequest_isset; + void *handle_friendrequest_object; void *handle_friendrequest_userdata; int (*filter_function)(uint8_t *, void *); @@ -57,8 +58,8 @@ uint32_t get_nospam(Friend_Requests *fr); /* Set the function that will be executed when a friend request for us is received. * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length, void * userdata) */ -void callback_friendrequest(Friend_Requests *fr, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), - void *userdata); +void callback_friendrequest(Friend_Requests *fr, void (*function)(void *, uint8_t *, uint8_t *, uint16_t, void *), + void *object, void *userdata); /* Set the function used to check if a friend request should be displayed to the user or not. * Function format is int function(uint8_t * public_key, void * userdata) diff --git a/toxcore/network.c b/toxcore/network.c index 35b55bf1..e2e0bff4 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -435,7 +435,7 @@ int networking_wait_cleanup(Networking_Core *net, uint8_t *data) if (s->send_fail_reset) { net->send_fail_eagain = 0; } - + return 1; } diff --git a/toxcore/tox.c b/toxcore/tox.c index aec0d902..9a5be0ad 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -353,7 +353,8 @@ uint32_t tox_get_friendlist(Tox *tox, int32_t *out_list, uint32_t list_size) /* Set the function that will be executed when a friend request is received. * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ -void tox_callback_friend_request(Tox *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata) +void tox_callback_friend_request(Tox *tox, void (*function)(Tox *tox, uint8_t *, uint8_t *, uint16_t, void *), + void *userdata) { Messenger *m = tox; m_callback_friendrequest(m, function, userdata); diff --git a/toxcore/tox.h b/toxcore/tox.h index 1c84c1cb..41f868dd 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -98,9 +98,9 @@ typedef struct Tox Tox; #endif /* NOTE: Strings in Tox are all UTF-8, (This means that there is no terminating NULL character.) - * + * * The exact buffer you send will be received at the other end without modification. - * + * * Do not treat Tox strings as C strings. */ @@ -228,7 +228,6 @@ int tox_get_name_size(Tox *tox, int32_t friendnumber); int tox_get_self_name_size(Tox *tox); /* Set our user status. - * You are responsible for freeing status after. * * userstatus must be one of TOX_USERSTATUS values. * @@ -300,7 +299,8 @@ uint32_t tox_get_friendlist(Tox *tox, int32_t *out_list, uint32_t list_size); /* Set the function that will be executed when a friend request is received. * Function format is function(Tox *tox, uint8_t * public_key, uint8_t * data, uint16_t length, void *userdata) */ -void tox_callback_friend_request(Tox *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata); +void tox_callback_friend_request(Tox *tox, void (*function)(Tox *tox, uint8_t *, uint8_t *, uint16_t, void *), + void *userdata); /* Set the function that will be executed when a message from a friend is received. * Function format is: function(Tox *tox, int friendnumber, uint8_t * message, uint32_t length, void *userdata)