diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index aa575382..021d93f7 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -781,6 +781,17 @@ void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, m->group_message_userdata = userdata; } +/* Set the callback for group actions. + * + * Function(Messenger *m, int friendnumber, uint8_t *group_public_key, void *userdata) + */ +void m_callback_group_action(Messenger *m, void (*function)(Messenger *m, int, int, uint8_t *, uint16_t, void *), + void *userdata) +{ + m->group_action = function; + m->group_action_userdata = userdata; +} + /* Set callback function for peer name list changes. * * It gets called every time the name list changes(new peer/name, deleted peer) @@ -817,6 +828,18 @@ static void group_message_function(Group_Chat *chat, int peer_number, uint8_t *m (*m->group_message)(m, i, peer_number, message, length, m->group_message_userdata); } +static void group_action_function(Group_Chat *chat, int peer_number, uint8_t *action, uint16_t length, void *userdata) +{ + Messenger *m = userdata; + int i = get_chat_num(m, chat); + + if (i == -1) + return; + + if (m->group_action) + (*m->group_action)(m, i, peer_number, action, length, m->group_action_userdata); +} + static void group_namelistchange_function(Group_Chat *chat, int peer, uint8_t change, void *userdata) { Messenger *m = userdata; @@ -847,6 +870,7 @@ int add_groupchat(Messenger *m) return -1; callback_groupmessage(newchat, &group_message_function, m); + callback_groupaction(newchat, &group_action_function, m); callback_namelistchange(newchat, &group_namelistchange_function, m); /* TODO: remove this (group nicks should not be tied to the global one) */ set_nick(newchat, m->name, m->name_length); @@ -868,6 +892,7 @@ int add_groupchat(Messenger *m) return -1; callback_groupmessage(temp[m->numchats], &group_message_function, m); + callback_groupaction(temp[m->numchats], &group_action_function, m); callback_namelistchange(temp[m->numchats], &group_namelistchange_function, m); /* TODO: remove this (group nicks should not be tied to the global one) */ set_nick(temp[m->numchats], m->name, m->name_length); @@ -1025,7 +1050,6 @@ int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_ * return 0 on success * return -1 on failure */ - int group_message_send(Messenger *m, int groupnumber, uint8_t *message, uint32_t length) { if (groupnumber_not_valid(m, groupnumber)) @@ -1037,6 +1061,21 @@ int group_message_send(Messenger *m, int groupnumber, uint8_t *message, uint32_t return -1; } +/* send a group action + * return 0 on success + * return -1 on failure + */ +int group_action_send(Messenger *m, int groupnumber, uint8_t *action, uint32_t length) +{ + if (groupnumber_not_valid(m, groupnumber)) + return -1; + + if (group_sendaction(m->chats[groupnumber], action, length) > 0) + return 0; + + return -1; +} + /* Return the number of peers in the group chat on success. * return -1 on failure */ diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 579516f8..61d7f86c 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -198,6 +198,8 @@ typedef struct Messenger { void *group_invite_userdata; void (*group_message)(struct Messenger *m, int, int, uint8_t *, uint16_t, void *); void *group_message_userdata; + void (*group_action)(struct Messenger *m, int, int, uint8_t *, uint16_t, void *); + void *group_action_userdata; void (*group_namelistchange)(struct Messenger *m, int, int, uint8_t, void *); void *group_namelistchange_userdata; @@ -457,6 +459,13 @@ void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int, u void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, int, uint8_t *, uint16_t, void *), void *userdata); +/* Set the callback for group actions. + * + * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) + */ +void m_callback_group_action(Messenger *m, void (*function)(Messenger *m, int, int, uint8_t *, uint16_t, void *), + void *userdata); + /* Set callback function for peer name list changes. * * It gets called every time the name list changes(new peer/name, deleted peer) @@ -504,9 +513,14 @@ int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_ * return 0 on success * return -1 on failure */ - int group_message_send(Messenger *m, int groupnumber, uint8_t *message, uint32_t length); +/* send a group action + * return 0 on success + * return -1 on failure + */ +int group_action_send(Messenger *m, int groupnumber, uint8_t *action, uint32_t length); + /* Return the number of peers in the group chat on success. * return -1 on failure */ diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index 90edeb40..e46b527d 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -521,6 +521,12 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len) break; + case GROUP_CHAT_ACTION: /* if message is a peer action */ + if (chat->group_action != NULL) + (*chat->group_action)(chat, peernum, contents, contents_len, chat->group_action_userdata); + + break; + default: handled = 0; break; @@ -607,6 +613,11 @@ uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length) return send_data(chat, message, length, GROUP_CHAT_CHAT_MESSAGE); //TODO: better return values? } +uint32_t group_sendaction(Group_Chat *chat, uint8_t *action, uint32_t length) +{ + return send_data(chat, action, length, GROUP_CHAT_ACTION); +} + /* * Send id/nick combo to the group. * @@ -644,6 +655,13 @@ void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, chat->group_message_userdata = userdata; } +void callback_groupaction(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *), + void *userdata) +{ + chat->group_action = function; + chat->group_action_userdata = userdata; +} + void callback_namelistchange(Group_Chat *chat, void (*function)(Group_Chat *chat, int peer, uint8_t change, void *), void *userdata) { diff --git a/toxcore/group_chats.h b/toxcore/group_chats.h index a43173ab..5c3c28c6 100644 --- a/toxcore/group_chats.h +++ b/toxcore/group_chats.h @@ -63,6 +63,8 @@ typedef struct Group_Chat { uint32_t message_number; void (*group_message)(struct Group_Chat *m, int, uint8_t *, uint16_t, void *); void *group_message_userdata; + void (*group_action)(struct Group_Chat *m, int, uint8_t *, uint16_t, void *); + void *group_action_userdata; void (*peer_namelistchange)(struct Group_Chat *m, int peer, uint8_t change, void *); void *group_namelistchange_userdata; @@ -80,6 +82,7 @@ typedef struct Group_Chat { #define GROUP_CHAT_QUIT 24 #define GROUP_CHAT_PEER_NICK 48 #define GROUP_CHAT_CHAT_MESSAGE 64 +#define GROUP_CHAT_ACTION 63 /* Copy the name of peernum to name. * name must be at least MAX_NICK_BYTES long. @@ -96,6 +99,15 @@ int group_peername(Group_Chat *chat, int peernum, uint8_t *name); */ void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *), void *userdata); + +/* + * Set callback function for actions. + * + * format of function is: function(Group_Chat *chat, peer number, action, action length, userdata) + */ +void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *), + void *userdata); + /* * Set callback function for peer name list changes. * @@ -119,6 +131,13 @@ void callback_namelistchange(Group_Chat *chat, void (*function)(Group_Chat *chat */ uint32_t group_sendmessage(Group_Chat *chat, uint8_t *message, uint32_t length); +/* + * Send an action to the group. + * + * returns the number of peers it has sent it to. + */ +uint32_t group_sendaction(Group_Chat *chat, uint8_t *action, uint32_t length); + /* * Set our nick for this group. * diff --git a/toxcore/tox.c b/toxcore/tox.c index c0d1da3b..fd1d9765 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -405,6 +405,7 @@ void tox_callback_group_invite(Tox *tox, void (*function)(Messenger *tox, int, u Messenger *m = tox; m_callback_group_invite(m, function, userdata); } + /* Set the callback for group messages. * * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) @@ -416,12 +417,22 @@ void tox_callback_group_message(Tox *tox, void (*function)(Messenger *tox, int, m_callback_group_message(m, function, userdata); } +/* Set the callback for group actions. + * + * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * action, uint16_t length, void *userdata) + */ +void tox_callback_group_action(Tox *tox, void (*function)(Messenger *tox, int, int, uint8_t *, uint16_t, void *), + void *userdata) +{ + Messenger *m = tox; + m_callback_group_action(m, function, userdata); +} + /* Set callback function for peer name list changes. * * It gets called every time the name list changes(new peer/name, deleted peer) * Function(Tox *tox, int groupnumber, void *userdata) */ - void tox_callback_group_namelist_change(Tox *tox, void (*function)(Tox *tox, int, int, uint8_t, void *), void *userdata) { Messenger *m = tox; @@ -490,6 +501,16 @@ int tox_group_message_send(Tox *tox, int groupnumber, uint8_t *message, uint32_t return group_message_send(m, groupnumber, message, length); } +/* send a group action + * return 0 on success + * return -1 on failure + */ +int tox_group_action_send(Tox *tox, int groupnumber, uint8_t *action, uint32_t length) +{ + Messenger *m = tox; + return group_action_send(m, groupnumber, action, length); +} + /* Return the number of peers in the group chat on success. * return -1 on failure */ diff --git a/toxcore/tox.h b/toxcore/tox.h index 1a335d2b..f8784aa6 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -363,6 +363,13 @@ void tox_callback_group_invite(Tox *tox, void (*function)(Tox *tox, int, uint8_t void tox_callback_group_message(Tox *tox, void (*function)(Tox *tox, int, int, uint8_t *, uint16_t, void *), void *userdata); +/* Set the callback for group actions. + * + * Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * action, uint16_t length, void *userdata) + */ +void tox_callback_group_action(Tox *tox, void (*function)(Tox *tox, int, int, uint8_t *, uint16_t, void *), + void *userdata); + /* Set callback function for peer name list changes. * * It gets called every time the name list changes(new peer/name, deleted peer) @@ -418,6 +425,12 @@ int tox_join_groupchat(Tox *tox, int friendnumber, uint8_t *friend_group_public_ */ int tox_group_message_send(Tox *tox, int groupnumber, uint8_t *message, uint32_t length); +/* send a group action + * return 0 on success + * return -1 on failure + */ +int tox_group_action_send(Tox *tox, int groupnumber, uint8_t *action, uint32_t length); + /* Return the number of peers in the group chat on success. * return -1 on failure */