Friend request callback now contains the Tox object.

This commit is contained in:
irungentoo 2014-03-16 13:24:39 -04:00
parent 95c8e9c2fb
commit 5babb281c0
12 changed files with 35 additions and 25 deletions

View File

@ -116,7 +116,7 @@ int parent_friend_request(DHT *dht)
return 0; 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); fputs("OK\nsending status to parent", stdout);
fflush(stdout); fflush(stdout);

View File

@ -19,12 +19,13 @@
#define c_sleep(x) usleep(1000*x) #define c_sleep(x) usleep(1000*x)
#endif #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) { 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; uint32_t messages_received;
@ -114,7 +115,8 @@ START_TEST(test_few_clients)
Tox *tox2 = tox_new(TOX_ENABLE_IPV6_DEFAULT); Tox *tox2 = tox_new(TOX_ENABLE_IPV6_DEFAULT);
Tox *tox3 = 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"); 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]; uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
tox_get_address(tox2, address); tox_get_address(tox2, address);
int test = tox_add_friend(tox3, address, (uint8_t *)"Gentoo", 7); 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"); printf("tox clients connected\n");
uint32_t to_compare = 974536; to_compare = 974536;
tox_callback_friend_message(tox3, print_message, &to_compare); tox_callback_friend_message(tox3, print_message, &to_compare);
tox_send_message(tox2, 0, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); 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) { for (i = 0; i < NUM_TOXES; ++i) {
toxes[i] = tox_new(TOX_ENABLE_IPV6_DEFAULT); toxes[i] = tox_new(TOX_ENABLE_IPV6_DEFAULT);
ck_assert_msg(toxes[i] != 0, "Failed to create tox instances %u", i); 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 { struct {

View File

@ -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 * */ * networking_requesthandler and so cannot take a Messenger * */
static Messenger *m; 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("Friend request received from: \n");
printf("ClientID: "); printf("ClientID: ");

View File

@ -862,7 +862,7 @@ void do_refresh()
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("[i] received friend request with message:");
new_lines((char *)data); new_lines((char *)data);

View File

@ -1064,7 +1064,7 @@ int av_terminate_session(av_session_t *_phone)
/****** AV HELPER FUNCTIONS ******/ /****** AV HELPER FUNCTIONS ******/
/* Auto accept friend request */ /* 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_session_t *_phone = _userdata;
av_allocate_friend (_phone, -1, 0); av_allocate_friend (_phone, -1, 0);

View File

@ -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); */ /* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); */
/* Set the function that will be executed when a friend request is received. */ /* 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. */ /* Set the function that will be executed when a message from a friend is received. */

View File

@ -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. /* 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) * 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. /* 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) * Function format is: function(int32_t friendnumber, uint8_t * message, uint32_t length)

View File

@ -72,11 +72,12 @@ uint32_t get_nospam(Friend_Requests *fr)
/* Set the function that will be executed when a friend request is received. */ /* 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 callback_friendrequest(Friend_Requests *fr, void (*function)(void *, uint8_t *, uint8_t *, uint16_t, void *),
void *userdata) void *object, void *userdata)
{ {
fr->handle_friendrequest = function; fr->handle_friendrequest = function;
fr->handle_friendrequest_isset = 1; fr->handle_friendrequest_isset = 1;
fr->handle_friendrequest_object = object;
fr->handle_friendrequest_userdata = userdata; fr->handle_friendrequest_userdata = userdata;
} }
/* Set the function used to check if a friend request should be displayed to the user or not. */ /* 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); memcpy(message, packet + 4, length - 4);
message[sizeof(message) - 1] = 0; /* Be sure the message is null terminated. */ 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; return 0;
} }

View File

@ -30,8 +30,9 @@
typedef struct { typedef struct {
uint32_t nospam; 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; uint8_t handle_friendrequest_isset;
void *handle_friendrequest_object;
void *handle_friendrequest_userdata; void *handle_friendrequest_userdata;
int (*filter_function)(uint8_t *, void *); 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. /* 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) * 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 callback_friendrequest(Friend_Requests *fr, void (*function)(void *, uint8_t *, uint8_t *, uint16_t, void *),
void *userdata); void *object, void *userdata);
/* Set the function used to check if a friend request should be displayed to the user or not. /* 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) * Function format is int function(uint8_t * public_key, void * userdata)

View File

@ -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. /* 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) * 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; Messenger *m = tox;
m_callback_friendrequest(m, function, userdata); m_callback_friendrequest(m, function, userdata);

View File

@ -228,7 +228,6 @@ int tox_get_name_size(Tox *tox, int32_t friendnumber);
int tox_get_self_name_size(Tox *tox); int tox_get_self_name_size(Tox *tox);
/* Set our user status. /* Set our user status.
* You are responsible for freeing status after.
* *
* userstatus must be one of TOX_USERSTATUS values. * 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. /* 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) * 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. /* 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) * Function format is: function(Tox *tox, int friendnumber, uint8_t * message, uint32_t length, void *userdata)