summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-10-11 06:42:07 -0700
committerirungentoo <irungentoo@gmail.com>2013-10-11 06:42:07 -0700
commit31f5d7a8abacd045de1d55a0462d17d14095073c (patch)
tree31e93f067cf7f89b00d650f8f8fd7ba0fd640efb /toxcore
parent77ec51085828c8dbe764030cbae7242994e590de (diff)
parent571412f911326a988d0d84019ea6a4ee8b49b17d (diff)
Merge pull request #618 from nurupo/action-messages-with-id
Added message ids to action messages
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/Messenger.c45
-rw-r--r--toxcore/Messenger.h12
-rw-r--r--toxcore/tox.c18
-rw-r--r--toxcore/tox.h12
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
332 332
333/* Send an action to an online friend. 333/* Send an action to an online friend.
334 * 334 *
335 * return 1 if packet was successfully put into the send queue. 335 * return the message id if packet was successfully put into the send queue.
336 * return 0 if it was not. 336 * return 0 if it was not.
337 */ 337 */
338int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length) 338uint32_t m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length)
339{
340 if (friend_not_valid(m, friendnumber))
341 return 0;
342
343 uint32_t msgid = ++m->friendlist[friendnumber].message_id;
344
345 if (msgid == 0)
346 msgid = 1; // Otherwise, false error
347
348 if (m_sendaction_withid(m, friendnumber, msgid, action, length)) {
349 return msgid;
350 }
351
352 return 0;
353}
354
355uint32_t m_sendaction_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *action, uint32_t length)
339{ 356{
340 return write_cryptpacket_id(m, friendnumber, PACKET_ID_ACTION, action, length); 357 if (length >= (MAX_DATA_SIZE - sizeof(theid)))
358 return 0;
359
360 uint8_t temp[MAX_DATA_SIZE];
361 theid = htonl(theid);
362 memcpy(temp, &theid, sizeof(theid));
363 memcpy(temp + sizeof(theid), action, length);
364 return write_cryptpacket_id(m, friendnumber, PACKET_ID_ACTION, temp, length + sizeof(theid));
341} 365}
342 366
343/* Send a name packet to friendnumber. 367/* Send a name packet to friendnumber.
@@ -1470,13 +1494,22 @@ void doFriends(Messenger *m)
1470 } 1494 }
1471 1495
1472 case PACKET_ID_ACTION: { 1496 case PACKET_ID_ACTION: {
1473 if (data_length == 0) 1497 uint8_t *message_id = data;
1498 uint8_t message_id_length = 4;
1499
1500 if (data_length <= message_id_length)
1474 break; 1501 break;
1475 1502
1476 data[data_length - 1] = 0;/* Make sure the NULL terminator is present. */ 1503 uint8_t *action = data + message_id_length;
1504 uint16_t action_length = data_length - message_id_length;
1505
1506 action[action_length - 1] = 0;/* Make sure the NULL terminator is present. */
1477 1507
1508 if (m->friendlist[i].receives_read_receipts) {
1509 write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length);
1510 }
1478 if (m->friend_action) 1511 if (m->friend_action)
1479 (*m->friend_action)(m, i, data, data_length, m->friend_action_userdata); 1512 (*m->friend_action)(m, i, action, action_length, m->friend_action_userdata);
1480 1513
1481 break; 1514 break;
1482 } 1515 }
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);
267 * return the message id if packet was successfully put into the send queue. 267 * return the message id if packet was successfully put into the send queue.
268 * return 0 if it was not. 268 * return 0 if it was not.
269 * 269 *
270 * You will want to retain the return value, it will be passed to your read receipt callback 270 * You will want to retain the return value, it will be passed to your read_receipt callback
271 * if one is received. 271 * if one is received.
272 * m_sendmessage_withid will send a message with the id of your choosing, 272 * m_sendmessage_withid will send a message with the id of your choosing,
273 * however we can generate an id for you by calling plain m_sendmessage. 273 * 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
277 277
278/* Send an action to an online friend. 278/* Send an action to an online friend.
279 * 279 *
280 * return 1 if packet was successfully put into the send queue. 280 * return the message id if packet was successfully put into the send queue.
281 * return 0 if it was not. 281 * return 0 if it was not.
282 *
283 * You will want to retain the return value, it will be passed to your read_receipt callback
284 * if one is received.
285 * m_sendaction_withid will send an action message with the id of your choosing,
286 * however we can generate an id for you by calling plain m_sendaction.
282 */ 287 */
283int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length); 288uint32_t m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length);
289uint32_t m_sendaction_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *action, uint32_t length);
284 290
285/* Set the name and name_length of a friend. 291/* Set the name and name_length of a friend.
286 * name must be a string of maximum MAX_NAME_LENGTH length. 292 * 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)
132 * return the message id if packet was successfully put into the send queue. 132 * return the message id if packet was successfully put into the send queue.
133 * return 0 if it was not. 133 * return 0 if it was not.
134 * 134 *
135 * You will want to retain the return value, it will be passed to your read receipt callback 135 * You will want to retain the return value, it will be passed to your read_receipt callback
136 * if one is received. 136 * if one is received.
137 * m_sendmessage_withid will send a message with the id of your choosing, 137 * m_sendmessage_withid will send a message with the id of your choosing,
138 * however we can generate an id for you by calling plain m_sendmessage. 138 * 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
150} 150}
151 151
152/* Send an action to an online friend. 152/* Send an action to an online friend.
153 * return 1 if packet was successfully put into the send queue. 153 *
154 * return the message id if packet was successfully put into the send queue.
154 * return 0 if it was not. 155 * return 0 if it was not.
156 *
157 * You will want to retain the return value, it will be passed to your read_receipt callback
158 * if one is received.
159 * m_sendaction_withid will send an action message with the id of your choosing,
160 * however we can generate an id for you by calling plain m_sendaction.
155 */ 161 */
156int tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length) 162uint32_t tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length)
157{ 163{
158 Messenger *m = tox; 164 Messenger *m = tox;
159 return m_sendaction(m, friendnumber, action, length); 165 return m_sendaction(m, friendnumber, action, length);
160} 166}
161 167
168uint32_t tox_sendaction_withid(Tox *tox, int friendnumber, uint32_t theid, uint8_t *action, uint32_t length)
169{
170 Messenger *m = tox;
171 return m_sendaction_withid(m, friendnumber, theid, action, length);
172}
173
162/* Set friendnumber's nickname. 174/* Set friendnumber's nickname.
163 * name must be a string of maximum MAX_NAME_LENGTH length. 175 * name must be a string of maximum MAX_NAME_LENGTH length.
164 * length must be at least 1 byte. 176 * 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);
209 * return the message id if packet was successfully put into the send queue. 209 * return the message id if packet was successfully put into the send queue.
210 * return 0 if it was not. 210 * return 0 if it was not.
211 * 211 *
212 * You will want to retain the return value, it will be passed to your read receipt callback 212 * You will want to retain the return value, it will be passed to your read_receipt callback
213 * if one is received. 213 * if one is received.
214 * m_sendmessage_withid will send a message with the id of your choosing, 214 * m_sendmessage_withid will send a message with the id of your choosing,
215 * however we can generate an id for you by calling plain m_sendmessage. 215 * 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
219 219
220/* Send an action to an online friend. 220/* Send an action to an online friend.
221 * 221 *
222 * return 1 if packet was successfully put into the send queue. 222 * return the message id if packet was successfully put into the send queue.
223 * return 0 if it was not. 223 * return 0 if it was not.
224 *
225 * You will want to retain the return value, it will be passed to your read_receipt callback
226 * if one is received.
227 * m_sendaction_withid will send an action message with the id of your choosing,
228 * however we can generate an id for you by calling plain m_sendaction.
224 */ 229 */
225int tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length); 230uint32_t tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length);
231uint32_t tox_sendaction_withid(Tox *tox, int friendnumber, uint32_t theid, uint8_t *action, uint32_t length);
226 232
227/* Set friendnumber's nickname. 233/* Set friendnumber's nickname.
228 * name must be a string of maximum MAX_NAME_LENGTH length. 234 * name must be a string of maximum MAX_NAME_LENGTH length.