summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2015-02-20 17:24:24 -0500
committerirungentoo <irungentoo@gmail.com>2015-02-20 17:24:24 -0500
commit41446f61e36844255f443f0094bb707abfddf7ed (patch)
treed5f46680256fa6654882f8ad72639d01f74fdb20
parentaf881e820a3f4b3d8f629c1cfd192fed049254f7 (diff)
Implemented message send functions in public api.
Internal message functions now return better error codes.
-rw-r--r--toxcore/Messenger.c44
-rw-r--r--toxcore/Messenger.h24
-rw-r--r--toxcore/tox.c84
3 files changed, 128 insertions, 24 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index a9d5e6a1..e5298b31 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -366,7 +366,8 @@ static int do_receipts(Messenger *m, int32_t friendnumber)
366 while (receipts) { 366 while (receipts) {
367 struct Receipts *temp_r = receipts->next; 367 struct Receipts *temp_r = receipts->next;
368 368
369 if (cryptpacket_received(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, m->friendlist[friendnumber].friendcon_id), receipts->packet_num) == -1) 369 if (cryptpacket_received(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
370 m->friendlist[friendnumber].friendcon_id), receipts->packet_num) == -1)
370 break; 371 break;
371 372
372 if (m->read_receipt) 373 if (m->read_receipt)
@@ -450,15 +451,25 @@ int m_friend_exists(const Messenger *m, int32_t friendnumber)
450 return 1; 451 return 1;
451} 452}
452 453
453 454/* Send a packet_id message.
454static uint32_t send_message_generic(Messenger *m, int32_t friendnumber, const uint8_t *message, uint32_t length, 455 *
455 uint8_t packet_id) 456 * return -1 if friend not valid.
457 * return -2 if too large.
458 * return -3 if friend not online.
459 * return -4 if send failed (because queue is full).
460 * return 0 if success.
461 */
462static int send_message_generic(Messenger *m, int32_t friendnumber, const uint8_t *message, uint32_t length,
463 uint8_t packet_id, uint32_t *message_id)
456{ 464{
457 if (friend_not_valid(m, friendnumber)) 465 if (friend_not_valid(m, friendnumber))
458 return 0; 466 return -1;
459 467
460 if (length >= MAX_CRYPTO_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE) 468 if (length >= MAX_CRYPTO_DATA_SIZE)
461 return 0; 469 return -2;
470
471 if (m->friendlist[friendnumber].status != FRIEND_ONLINE)
472 return -3;
462 473
463 uint8_t packet[length + 1]; 474 uint8_t packet[length + 1];
464 packet[0] = packet_id; 475 packet[0] = packet_id;
@@ -466,10 +477,11 @@ static uint32_t send_message_generic(Messenger *m, int32_t friendnumber, const u
466 if (length != 0) 477 if (length != 0)
467 memcpy(packet + 1, message, length); 478 memcpy(packet + 1, message, length);
468 479
469 int64_t packet_num = write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, m->friendlist[friendnumber].friendcon_id), packet, length + 1, 0); 480 int64_t packet_num = write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
481 m->friendlist[friendnumber].friendcon_id), packet, length + 1, 0);
470 482
471 if (packet_num == -1) 483 if (packet_num == -1)
472 return 0; 484 return -4;
473 485
474 uint32_t msg_id = ++m->friendlist[friendnumber].message_id; 486 uint32_t msg_id = ++m->friendlist[friendnumber].message_id;
475 487
@@ -478,7 +490,11 @@ static uint32_t send_message_generic(Messenger *m, int32_t friendnumber, const u
478 } 490 }
479 491
480 add_receipt(m, friendnumber, packet_num, msg_id); 492 add_receipt(m, friendnumber, packet_num, msg_id);
481 return msg_id; 493
494 if (message_id)
495 *message_id = msg_id;
496
497 return 0;
482} 498}
483 499
484/* Send a text chat message to an online friend. 500/* Send a text chat message to an online friend.
@@ -486,9 +502,9 @@ static uint32_t send_message_generic(Messenger *m, int32_t friendnumber, const u
486 * return the message id if packet was successfully put into the send queue. 502 * return the message id if packet was successfully put into the send queue.
487 * return 0 if it was not. 503 * return 0 if it was not.
488 */ 504 */
489uint32_t m_sendmessage(Messenger *m, int32_t friendnumber, const uint8_t *message, uint32_t length) 505int m_sendmessage(Messenger *m, int32_t friendnumber, const uint8_t *message, uint32_t length, uint32_t *message_id)
490{ 506{
491 return send_message_generic(m, friendnumber, message, length, PACKET_ID_MESSAGE); 507 return send_message_generic(m, friendnumber, message, length, PACKET_ID_MESSAGE, message_id);
492} 508}
493 509
494/* Send an action to an online friend. 510/* Send an action to an online friend.
@@ -496,9 +512,9 @@ uint32_t m_sendmessage(Messenger *m, int32_t friendnumber, const uint8_t *messag
496 * return the message id if packet was successfully put into the send queue. 512 * return the message id if packet was successfully put into the send queue.
497 * return 0 if it was not. 513 * return 0 if it was not.
498 */ 514 */
499uint32_t m_sendaction(Messenger *m, int32_t friendnumber, const uint8_t *action, uint32_t length) 515int m_sendaction(Messenger *m, int32_t friendnumber, const uint8_t *action, uint32_t length, uint32_t *message_id)
500{ 516{
501 return send_message_generic(m, friendnumber, action, length, PACKET_ID_ACTION); 517 return send_message_generic(m, friendnumber, action, length, PACKET_ID_ACTION, message_id);
502} 518}
503 519
504/* Send a name packet to friendnumber. 520/* Send a name packet to friendnumber.
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index 0a613c0b..4e011a57 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -403,23 +403,27 @@ int m_friend_exists(const Messenger *m, int32_t friendnumber);
403 403
404/* Send a text chat message to an online friend. 404/* Send a text chat message to an online friend.
405 * 405 *
406 * return the message id if packet was successfully put into the send queue. 406 * return -1 if friend not valid.
407 * return 0 if it was not. 407 * return -2 if too large.
408 * return -3 if friend not online.
409 * return -4 if send failed (because queue is full).
410 * return 0 if success.
408 * 411 *
409 * You will want to retain the return value, it will be passed to your read_receipt callback 412 * the value in message_id will be passed to your read_receipt callback when the other receives the message.
410 * if one is received.
411 */ 413 */
412uint32_t m_sendmessage(Messenger *m, int32_t friendnumber, const uint8_t *message, uint32_t length); 414int m_sendmessage(Messenger *m, int32_t friendnumber, const uint8_t *message, uint32_t length, uint32_t *message_id);
413 415
414/* Send an action to an online friend. 416/* Send an action to an online friend.
415 * 417 *
416 * return the message id if packet was successfully put into the send queue. 418 * return -1 if friend not valid.
417 * return 0 if it was not. 419 * return -2 if too large.
420 * return -3 if friend not online.
421 * return -4 if send failed (because queue is full).
422 * return 0 if success.
418 * 423 *
419 * You will want to retain the return value, it will be passed to your read_receipt callback 424 * the value in message_id will be passed to your read_receipt callback when the other receives the message.
420 * if one is received.
421 */ 425 */
422uint32_t m_sendaction(Messenger *m, int32_t friendnumber, const uint8_t *action, uint32_t length); 426int m_sendaction(Messenger *m, int32_t friendnumber, const uint8_t *action, uint32_t length, uint32_t *message_id);
423 427
424/* Set the name and name_length of a friend. 428/* Set the name and name_length of a friend.
425 * name must be a string of maximum MAX_NAME_LENGTH length. 429 * name must be a string of maximum MAX_NAME_LENGTH length.
diff --git a/toxcore/tox.c b/toxcore/tox.c
index db100459..54850d69 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -708,3 +708,87 @@ void tox_callback_friend_typing(Tox *tox, tox_friend_typing_cb *function, void *
708 Messenger *m = tox; 708 Messenger *m = tox;
709 m_callback_typingchange(m, function, user_data); 709 m_callback_typingchange(m, function, user_data);
710} 710}
711
712bool tox_self_set_typing(Tox *tox, uint32_t friend_number, bool is_typing, TOX_ERR_SET_TYPING *error)
713{
714 Messenger *m = tox;
715
716 if (m_set_usertyping(m, friend_number, is_typing) == -1) {
717 SET_ERROR_PARAMETER(error, TOX_ERR_SET_TYPING_FRIEND_NOT_FOUND);
718 return 0;
719 }
720
721 SET_ERROR_PARAMETER(error, TOX_ERR_SET_TYPING_OK);
722 return 1;
723}
724
725static void set_message_error(int ret, TOX_ERR_SEND_MESSAGE *error)
726{
727 switch (ret) {
728 case 0:
729 SET_ERROR_PARAMETER(error, TOX_ERR_SEND_MESSAGE_OK);
730 break;
731
732 case -1:
733 SET_ERROR_PARAMETER(error, TOX_ERR_SEND_MESSAGE_FRIEND_NOT_FOUND);
734 break;
735
736 case -2:
737 SET_ERROR_PARAMETER(error, TOX_ERR_SEND_MESSAGE_TOO_LONG);
738 break;
739
740 case -3:
741 SET_ERROR_PARAMETER(error, TOX_ERR_SEND_MESSAGE_FRIEND_NOT_CONNECTED);
742 break;
743
744 case -4:
745 SET_ERROR_PARAMETER(error, TOX_ERR_SEND_MESSAGE_SENDQ);
746 break;
747 }
748}
749
750uint32_t tox_send_message(Tox *tox, uint32_t friend_number, const uint8_t *message, size_t length,
751 TOX_ERR_SEND_MESSAGE *error)
752{
753 if (!message) {
754 SET_ERROR_PARAMETER(error, TOX_ERR_SEND_MESSAGE_NULL);
755 return 0;
756 }
757
758 if (!length) {
759 SET_ERROR_PARAMETER(error, TOX_ERR_SEND_MESSAGE_EMPTY);
760 return 0;
761 }
762
763 Messenger *m = tox;
764 uint32_t message_id = 0;
765 set_message_error(m_sendmessage(m, friend_number, message, length, &message_id), error);
766 return message_id;
767}
768
769uint32_t tox_send_action(Tox *tox, uint32_t friend_number, const uint8_t *action, size_t length,
770 TOX_ERR_SEND_MESSAGE *error)
771{
772 if (!action) {
773 SET_ERROR_PARAMETER(error, TOX_ERR_SEND_MESSAGE_NULL);
774 return 0;
775 }
776
777 if (!length) {
778 SET_ERROR_PARAMETER(error, TOX_ERR_SEND_MESSAGE_EMPTY);
779 return 0;
780 }
781
782 Messenger *m = tox;
783 uint32_t message_id = 0;
784 set_message_error(m_sendaction(m, friend_number, action, length, &message_id), error);
785 return message_id;
786}
787
788void tox_callback_read_receipt(Tox *tox, tox_read_receipt_cb *function, void *user_data)
789{
790 Messenger *m = tox;
791 m_callback_read_receipt(m, function, user_data);
792}
793
794