Skip to content

Commit

Permalink
Make the friend message callback stateless
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregory Mullen (grayhatter) committed Aug 19, 2016
1 parent 83d4857 commit 44f3b09
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion auto_tests/tox_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ START_TEST(test_few_clients)
ck_assert_msg(connected_t1, "Tox1 isn't connected. %u", connected_t1);
printf("tox clients connected took %llu seconds\n", time(NULL) - con_time);
to_compare = 974536;
tox_callback_friend_message(tox3, print_message, &to_compare);
tox_callback_friend_message(tox3, print_message);
uint8_t msgs[TOX_MAX_MESSAGE_LENGTH + 1];
memset(msgs, 'G', sizeof(msgs));
TOX_ERR_FRIEND_SEND_MESSAGE errm;
Expand Down
2 changes: 1 addition & 1 deletion testing/Messenger_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ int main(int argc, char *argv[])
}

m_callback_friendrequest(m, print_request, NULL);
m_callback_friendmessage(m, print_message, NULL);
m_callback_friendmessage(m, print_message);

printf("OUR ID: ");
uint32_t i;
Expand Down
2 changes: 1 addition & 1 deletion testing/irc_syncbot.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ Tox *init_tox(int argc, char *argv[])
exit(1);

tox_self_set_name(tox, (uint8_t *)IRC_NAME, sizeof(IRC_NAME) - 1, 0);
tox_callback_friend_message(tox, &callback_friend_message, 0);
tox_callback_friend_message(tox, &callback_friend_message);
tox_callback_group_invite(tox, &callback_group_invite, 0);
tox_callback_group_message(tox, &copy_groupmessage, 0);
tox_callback_group_action(tox, &copy_groupmessage, 0);
Expand Down
2 changes: 1 addition & 1 deletion testing/nTox.c
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,7 @@ int main(int argc, char *argv[])
save_data_file(m, filename);

tox_callback_friend_request(m, print_request, NULL);
tox_callback_friend_message(m, print_message, NULL);
tox_callback_friend_message(m, print_message);
tox_callback_friend_name(m, print_nickchange);
tox_callback_friend_status_message(m, print_statuschange);
tox_callback_group_invite(m, print_invite, NULL);
Expand Down
2 changes: 1 addition & 1 deletion testing/tox_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ int main(int argc, char *argv[])

Tox *tox = tox_new(0, 0);
tox_callback_friend_connection_status(tox, print_online, NULL);
tox_callback_friend_message(tox, print_message, master);
tox_callback_friend_message(tox, print_message);


uint16_t port = atoi(argv[argvoffset + 2]);
Expand Down
8 changes: 4 additions & 4 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -771,10 +771,9 @@ void m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, const

/* Set the function that will be executed when a message from a friend is received. */
void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, uint32_t, unsigned int, const uint8_t *,
size_t, void *), void *userdata)
size_t, void *))
{
m->friend_message = function;
m->friend_message_userdata = userdata;
}

void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, uint32_t, const uint8_t *, size_t, void *))
Expand Down Expand Up @@ -2007,8 +2006,9 @@ static int handle_packet(void *object, int i, uint8_t *temp, uint16_t len, void
message_terminated[message_length] = 0;
uint8_t type = packet_id - PACKET_ID_MESSAGE;

if (m->friend_message)
(*m->friend_message)(m, i, type, message_terminated, message_length, m->friend_message_userdata);
if (m->friend_message) {
(*m->friend_message)(m, i, type, message_terminated, message_length, userdata);
}

break;
}
Expand Down
3 changes: 1 addition & 2 deletions toxcore/Messenger.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ struct Messenger {
Node_format loaded_relays[NUM_SAVED_TCP_RELAYS]; // Relays loaded from config

void (*friend_message)(struct Messenger *m, uint32_t, unsigned int, const uint8_t *, size_t, void *);
void *friend_message_userdata;
void (*friend_namechange)(struct Messenger *m, uint32_t, const uint8_t *, size_t, void *);
void (*friend_statusmessagechange)(struct Messenger *m, uint32_t, const uint8_t *, size_t, void *);
void (*friend_userstatuschange)(struct Messenger *m, uint32_t, unsigned int, void *);
Expand Down Expand Up @@ -477,7 +476,7 @@ void m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, const
* Function format is: function(uint32_t friendnumber, unsigned int type, uint8_t * message, uint32_t length)
*/
void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, uint32_t, unsigned int, const uint8_t *,
size_t, void *), void *userdata);
size_t, void *));

/* Set the callback for name changes.
* Function(uint32_t friendnumber, uint8_t *newname, size_t length)
Expand Down
4 changes: 2 additions & 2 deletions toxcore/tox.c
Original file line number Diff line number Diff line change
Expand Up @@ -902,10 +902,10 @@ void tox_callback_friend_request(Tox *tox, tox_friend_request_cb *function, void
m_callback_friendrequest(m, function, user_data);
}

void tox_callback_friend_message(Tox *tox, tox_friend_message_cb *function, void *user_data)
void tox_callback_friend_message(Tox *tox, tox_friend_message_cb *function)
{
Messenger *m = tox;
m_callback_friendmessage(m, function, user_data);
m_callback_friendmessage(m, function);
}

bool tox_hash(uint8_t *hash, const uint8_t *data, size_t length)
Expand Down
2 changes: 1 addition & 1 deletion toxcore/tox.h
Original file line number Diff line number Diff line change
Expand Up @@ -1580,7 +1580,7 @@ typedef void tox_friend_message_cb(Tox *tox, uint32_t friend_number, TOX_MESSAGE
*
* This event is triggered when a message from a friend is received.
*/
void tox_callback_friend_message(Tox *tox, tox_friend_message_cb *callback, void *user_data);
void tox_callback_friend_message(Tox *tox, tox_friend_message_cb *callback);


/*******************************************************************************
Expand Down

0 comments on commit 44f3b09

Please sign in to comment.