Skip to content

Commit

Permalink
Friend request callback now contains the Tox object.
Browse files Browse the repository at this point in the history
  • Loading branch information
irungentoo committed Mar 16, 2014
1 parent 95c8e9c commit 5babb28
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 25 deletions.
2 changes: 1 addition & 1 deletion auto_tests/friends_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 9 additions & 6 deletions auto_tests/tox_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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"));

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion testing/Messenger_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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: ");
Expand Down
2 changes: 1 addition & 1 deletion testing/nTox.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion toxav/phone.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
3 changes: 2 additions & 1 deletion toxcore/Messenger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions toxcore/friend_requests.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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;
}

Expand Down
7 changes: 4 additions & 3 deletions toxcore/friend_requests.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion toxcore/tox.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions toxcore/tox.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5babb28

Please sign in to comment.