From d6685d1c9aa52ce22eb1c2bb83b90b58d4835f95 Mon Sep 17 00:00:00 2001 From: irungentoo_trip Date: Wed, 29 Oct 2014 23:09:33 -0400 Subject: [PATCH] Added actions to group chats. --- toxcore/group.c | 39 +++++++++++++++++++++++++++++++++++++++ toxcore/group.h | 2 ++ toxcore/tox.c | 5 ++--- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/toxcore/group.c b/toxcore/group.c index 24f0b72e..0b77e013 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -893,6 +893,17 @@ void g_callback_group_message(Group_Chats *g_c, void (*function)(Messenger *m, i g_c->message_callback_userdata = userdata; } +/* Set the callback for group actions. + * + * Function(Group_Chats *g_c, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) + */ +void g_callback_group_action(Group_Chats *g_c, void (*function)(Messenger *m, int, int, const uint8_t *, uint16_t, + void *), void *userdata) +{ + g_c->action_callback = function; + g_c->action_callback_userdata = userdata; +} + /* Set callback function for peer name list changes. * * It gets called every time the name list changes(new peer/name, deleted peer) @@ -1390,6 +1401,19 @@ int group_message_send(const Group_Chats *g_c, int groupnumber, const uint8_t *m } } +/* send a group action + * return 0 on success + * return -1 on failure + */ +int group_action_send(const Group_Chats *g_c, int groupnumber, const uint8_t *action, uint16_t length) +{ + if (send_message_group(g_c, groupnumber, PACKET_ID_ACTION, action, length)) { + return 0; + } else { + return -1; + } +} + static void handle_message_packet_group(Group_Chats *g_c, int groupnumber, const uint8_t *data, uint16_t length, int close_index) { @@ -1485,6 +1509,21 @@ static void handle_message_packet_group(Group_Chats *g_c, int groupnumber, const break; } + case PACKET_ID_ACTION: { + if (msg_data_len == 0) + return; + + uint8_t newmsg[msg_data_len + 1]; + memcpy(newmsg, msg_data, msg_data_len); + newmsg[msg_data_len] = 0; + + //TODO + if (g_c->action_callback) + g_c->action_callback(g_c->m, groupnumber, index, newmsg, msg_data_len, g_c->action_callback_userdata); + + break; + } + default: return; } diff --git a/toxcore/group.h b/toxcore/group.h index 3764bf94..74abfa66 100644 --- a/toxcore/group.h +++ b/toxcore/group.h @@ -98,6 +98,8 @@ typedef struct { void *invite_callback_userdata; void (*message_callback)(Messenger *m, int, int, const uint8_t *, uint16_t, void *); void *message_callback_userdata; + void (*action_callback)(Messenger *m, int, int, const uint8_t *, uint16_t, void *); + void *action_callback_userdata; void (*peer_namelistchange)(Messenger *m, int, int, uint8_t, void *); void *group_namelistchange_userdata; } Group_Chats; diff --git a/toxcore/tox.c b/toxcore/tox.c index 19146744..9eef6157 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -581,7 +581,7 @@ void tox_callback_group_action(Tox *tox, void (*function)(Messenger *tox, int, i void *userdata) { Messenger *m = tox; - //m_callback_group_action(m, function, userdata); + g_callback_group_action(m->group_chat_object, function, userdata); } /* Set callback function for peer name list changes. @@ -668,8 +668,7 @@ int tox_group_message_send(Tox *tox, int groupnumber, const uint8_t *message, ui int tox_group_action_send(Tox *tox, int groupnumber, const uint8_t *action, uint16_t length) { Messenger *m = tox; - //return group_action_send(m, groupnumber, action, length); - return -1; + return group_action_send(m->group_chat_object, groupnumber, action, length); } /* Check if the current peernumber corresponds to ours.