Implemented message send functions in public api.

Internal message functions now return better error codes.
This commit is contained in:
irungentoo 2015-02-20 17:24:24 -05:00
parent af881e820a
commit 41446f61e3
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
3 changed files with 128 additions and 24 deletions

View File

@ -366,7 +366,8 @@ static int do_receipts(Messenger *m, int32_t friendnumber)
while (receipts) { while (receipts) {
struct Receipts *temp_r = receipts->next; struct Receipts *temp_r = receipts->next;
if (cryptpacket_received(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, m->friendlist[friendnumber].friendcon_id), receipts->packet_num) == -1) if (cryptpacket_received(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
m->friendlist[friendnumber].friendcon_id), receipts->packet_num) == -1)
break; break;
if (m->read_receipt) if (m->read_receipt)
@ -450,15 +451,25 @@ int m_friend_exists(const Messenger *m, int32_t friendnumber)
return 1; return 1;
} }
/* Send a packet_id message.
static uint32_t send_message_generic(Messenger *m, int32_t friendnumber, const uint8_t *message, uint32_t length, *
uint8_t packet_id) * return -1 if friend not valid.
* return -2 if too large.
* return -3 if friend not online.
* return -4 if send failed (because queue is full).
* return 0 if success.
*/
static int send_message_generic(Messenger *m, int32_t friendnumber, const uint8_t *message, uint32_t length,
uint8_t packet_id, uint32_t *message_id)
{ {
if (friend_not_valid(m, friendnumber)) if (friend_not_valid(m, friendnumber))
return 0; return -1;
if (length >= MAX_CRYPTO_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE) if (length >= MAX_CRYPTO_DATA_SIZE)
return 0; return -2;
if (m->friendlist[friendnumber].status != FRIEND_ONLINE)
return -3;
uint8_t packet[length + 1]; uint8_t packet[length + 1];
packet[0] = packet_id; packet[0] = packet_id;
@ -466,10 +477,11 @@ static uint32_t send_message_generic(Messenger *m, int32_t friendnumber, const u
if (length != 0) if (length != 0)
memcpy(packet + 1, message, length); memcpy(packet + 1, message, length);
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); 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);
if (packet_num == -1) if (packet_num == -1)
return 0; return -4;
uint32_t msg_id = ++m->friendlist[friendnumber].message_id; uint32_t msg_id = ++m->friendlist[friendnumber].message_id;
@ -478,7 +490,11 @@ static uint32_t send_message_generic(Messenger *m, int32_t friendnumber, const u
} }
add_receipt(m, friendnumber, packet_num, msg_id); add_receipt(m, friendnumber, packet_num, msg_id);
return msg_id;
if (message_id)
*message_id = msg_id;
return 0;
} }
/* Send a text chat message to an online friend. /* 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
* return the message id 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. * return 0 if it was not.
*/ */
uint32_t m_sendmessage(Messenger *m, int32_t friendnumber, const uint8_t *message, uint32_t length) int m_sendmessage(Messenger *m, int32_t friendnumber, const uint8_t *message, uint32_t length, uint32_t *message_id)
{ {
return send_message_generic(m, friendnumber, message, length, PACKET_ID_MESSAGE); return send_message_generic(m, friendnumber, message, length, PACKET_ID_MESSAGE, message_id);
} }
/* Send an action to an online friend. /* Send an action to an online friend.
@ -496,9 +512,9 @@ uint32_t m_sendmessage(Messenger *m, int32_t friendnumber, const uint8_t *messag
* return the message id 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. * return 0 if it was not.
*/ */
uint32_t m_sendaction(Messenger *m, int32_t friendnumber, const uint8_t *action, uint32_t length) int m_sendaction(Messenger *m, int32_t friendnumber, const uint8_t *action, uint32_t length, uint32_t *message_id)
{ {
return send_message_generic(m, friendnumber, action, length, PACKET_ID_ACTION); return send_message_generic(m, friendnumber, action, length, PACKET_ID_ACTION, message_id);
} }
/* Send a name packet to friendnumber. /* Send a name packet to friendnumber.

View File

@ -403,23 +403,27 @@ int m_friend_exists(const Messenger *m, int32_t friendnumber);
/* Send a text chat message to an online friend. /* Send a text chat message to an online friend.
* *
* return the message id if packet was successfully put into the send queue. * return -1 if friend not valid.
* return 0 if it was not. * return -2 if too large.
* return -3 if friend not online.
* return -4 if send failed (because queue is full).
* return 0 if success.
* *
* You will want to retain the return value, it will be passed to your read_receipt callback * the value in message_id will be passed to your read_receipt callback when the other receives the message.
* if one is received.
*/ */
uint32_t m_sendmessage(Messenger *m, int32_t friendnumber, const uint8_t *message, uint32_t length); int m_sendmessage(Messenger *m, int32_t friendnumber, const uint8_t *message, uint32_t length, uint32_t *message_id);
/* Send an action to an online friend. /* Send an action to an online friend.
* *
* return the message id if packet was successfully put into the send queue. * return -1 if friend not valid.
* return 0 if it was not. * return -2 if too large.
* return -3 if friend not online.
* return -4 if send failed (because queue is full).
* return 0 if success.
* *
* You will want to retain the return value, it will be passed to your read_receipt callback * the value in message_id will be passed to your read_receipt callback when the other receives the message.
* if one is received.
*/ */
uint32_t m_sendaction(Messenger *m, int32_t friendnumber, const uint8_t *action, uint32_t length); int m_sendaction(Messenger *m, int32_t friendnumber, const uint8_t *action, uint32_t length, uint32_t *message_id);
/* Set the name and name_length of a friend. /* Set the name and name_length of a friend.
* name must be a string of maximum MAX_NAME_LENGTH length. * name must be a string of maximum MAX_NAME_LENGTH length.

View File

@ -708,3 +708,87 @@ void tox_callback_friend_typing(Tox *tox, tox_friend_typing_cb *function, void *
Messenger *m = tox; Messenger *m = tox;
m_callback_typingchange(m, function, user_data); m_callback_typingchange(m, function, user_data);
} }
bool tox_self_set_typing(Tox *tox, uint32_t friend_number, bool is_typing, TOX_ERR_SET_TYPING *error)
{
Messenger *m = tox;
if (m_set_usertyping(m, friend_number, is_typing) == -1) {
SET_ERROR_PARAMETER(error, TOX_ERR_SET_TYPING_FRIEND_NOT_FOUND);
return 0;
}
SET_ERROR_PARAMETER(error, TOX_ERR_SET_TYPING_OK);
return 1;
}
static void set_message_error(int ret, TOX_ERR_SEND_MESSAGE *error)
{
switch (ret) {
case 0:
SET_ERROR_PARAMETER(error, TOX_ERR_SEND_MESSAGE_OK);
break;
case -1:
SET_ERROR_PARAMETER(error, TOX_ERR_SEND_MESSAGE_FRIEND_NOT_FOUND);
break;
case -2:
SET_ERROR_PARAMETER(error, TOX_ERR_SEND_MESSAGE_TOO_LONG);
break;
case -3:
SET_ERROR_PARAMETER(error, TOX_ERR_SEND_MESSAGE_FRIEND_NOT_CONNECTED);
break;
case -4:
SET_ERROR_PARAMETER(error, TOX_ERR_SEND_MESSAGE_SENDQ);
break;
}
}
uint32_t tox_send_message(Tox *tox, uint32_t friend_number, const uint8_t *message, size_t length,
TOX_ERR_SEND_MESSAGE *error)
{
if (!message) {
SET_ERROR_PARAMETER(error, TOX_ERR_SEND_MESSAGE_NULL);
return 0;
}
if (!length) {
SET_ERROR_PARAMETER(error, TOX_ERR_SEND_MESSAGE_EMPTY);
return 0;
}
Messenger *m = tox;
uint32_t message_id = 0;
set_message_error(m_sendmessage(m, friend_number, message, length, &message_id), error);
return message_id;
}
uint32_t tox_send_action(Tox *tox, uint32_t friend_number, const uint8_t *action, size_t length,
TOX_ERR_SEND_MESSAGE *error)
{
if (!action) {
SET_ERROR_PARAMETER(error, TOX_ERR_SEND_MESSAGE_NULL);
return 0;
}
if (!length) {
SET_ERROR_PARAMETER(error, TOX_ERR_SEND_MESSAGE_EMPTY);
return 0;
}
Messenger *m = tox;
uint32_t message_id = 0;
set_message_error(m_sendaction(m, friend_number, action, length, &message_id), error);
return message_id;
}
void tox_callback_read_receipt(Tox *tox, tox_read_receipt_cb *function, void *user_data)
{
Messenger *m = tox;
m_callback_read_receipt(m, function, user_data);
}