From 571412f911326a988d0d84019ea6a4ee8b49b17d Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Thu, 10 Oct 2013 22:27:51 -0400 Subject: [PATCH] Added message ids to action messages --- toxcore/Messenger.c | 45 +++++++++++++++++++++++++++++++++++++++------ toxcore/Messenger.h | 12 +++++++++--- toxcore/tox.c | 18 +++++++++++++++--- toxcore/tox.h | 12 +++++++++--- 4 files changed, 72 insertions(+), 15 deletions(-) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 5a194780..7de14edf 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -332,12 +332,36 @@ uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, ui /* Send an action to an online friend. * - * return 1 if packet was successfully put into the send queue. + * return the message id if packet was successfully put into the send queue. * return 0 if it was not. */ -int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length) +uint32_t m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length) { - return write_cryptpacket_id(m, friendnumber, PACKET_ID_ACTION, action, length); + if (friend_not_valid(m, friendnumber)) + return 0; + + uint32_t msgid = ++m->friendlist[friendnumber].message_id; + + if (msgid == 0) + msgid = 1; // Otherwise, false error + + if (m_sendaction_withid(m, friendnumber, msgid, action, length)) { + return msgid; + } + + return 0; +} + +uint32_t m_sendaction_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *action, uint32_t length) +{ + if (length >= (MAX_DATA_SIZE - sizeof(theid))) + return 0; + + uint8_t temp[MAX_DATA_SIZE]; + theid = htonl(theid); + memcpy(temp, &theid, sizeof(theid)); + memcpy(temp + sizeof(theid), action, length); + return write_cryptpacket_id(m, friendnumber, PACKET_ID_ACTION, temp, length + sizeof(theid)); } /* Send a name packet to friendnumber. @@ -1470,13 +1494,22 @@ void doFriends(Messenger *m) } case PACKET_ID_ACTION: { - if (data_length == 0) + uint8_t *message_id = data; + uint8_t message_id_length = 4; + + if (data_length <= message_id_length) break; - data[data_length - 1] = 0;/* Make sure the NULL terminator is present. */ + uint8_t *action = data + message_id_length; + uint16_t action_length = data_length - message_id_length; + action[action_length - 1] = 0;/* Make sure the NULL terminator is present. */ + + if (m->friendlist[i].receives_read_receipts) { + write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length); + } if (m->friend_action) - (*m->friend_action)(m, i, data, data_length, m->friend_action_userdata); + (*m->friend_action)(m, i, action, action_length, m->friend_action_userdata); break; } diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 1182962b..65fa19a5 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -267,7 +267,7 @@ int m_friend_exists(Messenger *m, int friendnumber); * return the message id if packet was successfully put into the send queue. * return 0 if it was not. * - * You will want to retain the return value, it will be passed to your read receipt callback + * You will want to retain the return value, it will be passed to your read_receipt callback * if one is received. * m_sendmessage_withid will send a message with the id of your choosing, * however we can generate an id for you by calling plain m_sendmessage. @@ -277,10 +277,16 @@ uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, ui /* Send an action to an online friend. * - * return 1 if packet was successfully put into the send queue. + * return the message id if packet was successfully put into the send queue. * return 0 if it was not. + * + * You will want to retain the return value, it will be passed to your read_receipt callback + * if one is received. + * m_sendaction_withid will send an action message with the id of your choosing, + * however we can generate an id for you by calling plain m_sendaction. */ -int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length); +uint32_t m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length); +uint32_t m_sendaction_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *action, uint32_t length); /* Set the name and name_length of a friend. * name must be a string of maximum MAX_NAME_LENGTH length. diff --git a/toxcore/tox.c b/toxcore/tox.c index ded2da6d..8f4aef5b 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -132,7 +132,7 @@ int tox_friend_exists(Tox *tox, int friendnumber) * return the message id if packet was successfully put into the send queue. * return 0 if it was not. * - * You will want to retain the return value, it will be passed to your read receipt callback + * You will want to retain the return value, it will be passed to your read_receipt callback * if one is received. * m_sendmessage_withid will send a message with the id of your choosing, * however we can generate an id for you by calling plain m_sendmessage. @@ -150,15 +150,27 @@ uint32_t tox_sendmessage_withid(Tox *tox, int friendnumber, uint32_t theid, uint } /* Send an action to an online friend. - * return 1 if packet was successfully put into the send queue. + * + * return the message id if packet was successfully put into the send queue. * return 0 if it was not. + * + * You will want to retain the return value, it will be passed to your read_receipt callback + * if one is received. + * m_sendaction_withid will send an action message with the id of your choosing, + * however we can generate an id for you by calling plain m_sendaction. */ -int tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length) +uint32_t tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length) { Messenger *m = tox; return m_sendaction(m, friendnumber, action, length); } +uint32_t tox_sendaction_withid(Tox *tox, int friendnumber, uint32_t theid, uint8_t *action, uint32_t length) +{ + Messenger *m = tox; + return m_sendaction_withid(m, friendnumber, theid, action, length); +} + /* Set friendnumber's nickname. * name must be a string of maximum MAX_NAME_LENGTH length. * length must be at least 1 byte. diff --git a/toxcore/tox.h b/toxcore/tox.h index db4e6b57..39832073 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -209,7 +209,7 @@ int tox_friend_exists(Tox *tox, int friendnumber); * return the message id if packet was successfully put into the send queue. * return 0 if it was not. * - * You will want to retain the return value, it will be passed to your read receipt callback + * You will want to retain the return value, it will be passed to your read_receipt callback * if one is received. * m_sendmessage_withid will send a message with the id of your choosing, * however we can generate an id for you by calling plain m_sendmessage. @@ -219,10 +219,16 @@ uint32_t tox_sendmessage_withid(Tox *tox, int friendnumber, uint32_t theid, uint /* Send an action to an online friend. * - * return 1 if packet was successfully put into the send queue. + * return the message id if packet was successfully put into the send queue. * return 0 if it was not. + * + * You will want to retain the return value, it will be passed to your read_receipt callback + * if one is received. + * m_sendaction_withid will send an action message with the id of your choosing, + * however we can generate an id for you by calling plain m_sendaction. */ -int tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length); +uint32_t tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length); +uint32_t tox_sendaction_withid(Tox *tox, int friendnumber, uint32_t theid, uint8_t *action, uint32_t length); /* Set friendnumber's nickname. * name must be a string of maximum MAX_NAME_LENGTH length.