diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 455494bc..36dadbcf 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -488,7 +488,8 @@ int m_friend_exists(const Messenger *m, int32_t friendnumber) int m_send_message_generic(Messenger *m, int32_t friendnumber, uint8_t type, const uint8_t *message, uint32_t length, uint32_t *message_id) { - if (type > MESSAGE_ACTION) { + /* MESSAGE_LAST itself is incorrect value */ + if (type >= MESSAGE_LAST) { return -5; } @@ -2213,7 +2214,8 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le } case PACKET_ID_MESSAGE: // fall-through - case PACKET_ID_ACTION: { + case PACKET_ID_ACTION: + case PACKET_ID_CORRECTION: { if (data_length == 0) { break; } diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 350fe564..a261a507 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -42,7 +42,9 @@ enum { MESSAGE_NORMAL, - MESSAGE_ACTION + MESSAGE_ACTION, + MESSAGE_CORRECTION, + MESSAGE_LAST }; /* NOTE: Packet ids below 24 must never be used. */ @@ -54,6 +56,7 @@ enum { #define PACKET_ID_TYPING 51 #define PACKET_ID_MESSAGE 64 #define PACKET_ID_ACTION (PACKET_ID_MESSAGE + MESSAGE_ACTION) /* 65 */ +#define PACKET_ID_CORRECTION (PACKET_ID_MESSAGE + MESSAGE_CORRECTION) /* 66 */ #define PACKET_ID_MSI 69 #define PACKET_ID_FILE_SENDREQUEST 80 #define PACKET_ID_FILE_CONTROL 81 diff --git a/toxcore/group.c b/toxcore/group.c index 0f2639c3..d0a1154e 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -1956,6 +1956,21 @@ int group_action_send(const Group_Chats *g_c, int groupnumber, const uint8_t *ac return ret; } +/* send a group correction message + * return 0 on success + * see: send_message_group() for error codes. + */ +int group_correction_send(const Group_Chats *g_c, int groupnumber, const uint8_t *action, uint16_t length) +{ + int ret = send_message_group(g_c, groupnumber, PACKET_ID_CORRECTION, action, length); + + if (ret > 0) { + return 0; + } + + return ret; +} + /* High level function to send custom lossy packets. * * return -1 on failure. @@ -2083,7 +2098,9 @@ static void handle_message_packet_group(Group_Chats *g_c, int groupnumber, const } break; - case PACKET_ID_MESSAGE: { + case PACKET_ID_MESSAGE: + case PACKET_ID_ACTION: + case PACKET_ID_CORRECTION: { if (msg_data_len == 0) { return; } @@ -2094,24 +2111,9 @@ static void handle_message_packet_group(Group_Chats *g_c, int groupnumber, const // TODO(irungentoo): if (g_c->message_callback) { - g_c->message_callback(g_c->m, groupnumber, index, 0, newmsg, msg_data_len, userdata); - } - - break; - } - - case PACKET_ID_ACTION: { - if (msg_data_len == 0) { - return; - } - - VLA(uint8_t, newmsg, msg_data_len + 1); - memcpy(newmsg, msg_data, msg_data_len); - newmsg[msg_data_len] = 0; - - // TODO(irungentoo): - if (g_c->message_callback) { - g_c->message_callback(g_c->m, groupnumber, index, 1, newmsg, msg_data_len, userdata); + uint8_t chat_message_id = message_id - PACKET_ID_MESSAGE; + g_c->message_callback(g_c->m, groupnumber, index, chat_message_id, + newmsg, msg_data_len, userdata); } break; diff --git a/toxcore/group.h b/toxcore/group.h index 2e014da3..6c0baebf 100644 --- a/toxcore/group.h +++ b/toxcore/group.h @@ -238,6 +238,12 @@ int group_message_send(const Group_Chats *g_c, int groupnumber, const uint8_t *m */ int group_action_send(const Group_Chats *g_c, int groupnumber, const uint8_t *action, uint16_t length); +/* send a group correction message + * return 0 on success + * see: send_message_group() for error codes. + */ +int group_correction_send(const Group_Chats *g_c, int groupnumber, const uint8_t *action, uint16_t length); + /* set the group's title, limited to MAX_NAME_LENGTH * return 0 on success * return -1 if groupnumber is invalid. diff --git a/toxcore/tox.api.h b/toxcore/tox.api.h index 63959207..7f3d8d4c 100644 --- a/toxcore/tox.api.h +++ b/toxcore/tox.api.h @@ -338,6 +338,11 @@ enum class MESSAGE_TYPE { * on IRC. */ ACTION, + /** + * Correction of the last message. With empty message body can be used to mark + * last message as deleted. + */ + CORRECTION, } diff --git a/toxcore/tox.h b/toxcore/tox.h index 567ce7e5..f4f3d066 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -371,6 +371,12 @@ typedef enum TOX_MESSAGE_TYPE { */ TOX_MESSAGE_TYPE_ACTION, + /** + * Correction of the last message. With empty message body can be used to mark + * last message as deleted. + */ + TOX_MESSAGE_TYPE_CORRECTION, + } TOX_MESSAGE_TYPE;