added groupchat actions

This commit is contained in:
Jfreegman 2013-12-13 20:27:25 -05:00
parent 4a5136f431
commit b92cdcf184
6 changed files with 127 additions and 3 deletions

View File

@ -781,6 +781,17 @@ void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int,
m->group_message_userdata = userdata; 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. /* Set callback function for peer name list changes.
* *
* It gets called every time the name list changes(new peer/name, deleted peer) * 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); (*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) static void group_namelistchange_function(Group_Chat *chat, int peer, uint8_t change, void *userdata)
{ {
Messenger *m = userdata; Messenger *m = userdata;
@ -847,6 +870,7 @@ int add_groupchat(Messenger *m)
return -1; return -1;
callback_groupmessage(newchat, &group_message_function, m); callback_groupmessage(newchat, &group_message_function, m);
callback_groupaction(newchat, &group_action_function, m);
callback_namelistchange(newchat, &group_namelistchange_function, m); callback_namelistchange(newchat, &group_namelistchange_function, m);
/* TODO: remove this (group nicks should not be tied to the global one) */ /* TODO: remove this (group nicks should not be tied to the global one) */
set_nick(newchat, m->name, m->name_length); set_nick(newchat, m->name, m->name_length);
@ -868,6 +892,7 @@ int add_groupchat(Messenger *m)
return -1; return -1;
callback_groupmessage(temp[m->numchats], &group_message_function, m); 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); callback_namelistchange(temp[m->numchats], &group_namelistchange_function, m);
/* TODO: remove this (group nicks should not be tied to the global one) */ /* TODO: remove this (group nicks should not be tied to the global one) */
set_nick(temp[m->numchats], m->name, m->name_length); 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 0 on success
* return -1 on failure * return -1 on failure
*/ */
int group_message_send(Messenger *m, int groupnumber, uint8_t *message, uint32_t length) int group_message_send(Messenger *m, int groupnumber, uint8_t *message, uint32_t length)
{ {
if (groupnumber_not_valid(m, groupnumber)) 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; 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 the number of peers in the group chat on success.
* return -1 on failure * return -1 on failure
*/ */

View File

@ -198,6 +198,8 @@ typedef struct Messenger {
void *group_invite_userdata; void *group_invite_userdata;
void (*group_message)(struct Messenger *m, int, int, uint8_t *, uint16_t, void *); void (*group_message)(struct Messenger *m, int, int, uint8_t *, uint16_t, void *);
void *group_message_userdata; 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)(struct Messenger *m, int, int, uint8_t, void *);
void *group_namelistchange_userdata; 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 m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, int, uint8_t *, uint16_t, void *),
void *userdata); 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. /* Set callback function for peer name list changes.
* *
* It gets called every time the name list changes(new peer/name, deleted peer) * 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 0 on success
* return -1 on failure * return -1 on failure
*/ */
int group_message_send(Messenger *m, int groupnumber, uint8_t *message, uint32_t length); 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 the number of peers in the group chat on success.
* return -1 on failure * return -1 on failure
*/ */

View File

@ -521,6 +521,12 @@ static int handle_data(Group_Chat *chat, uint8_t *data, uint32_t len)
break; 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: default:
handled = 0; handled = 0;
break; 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? 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. * 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; 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 callback_namelistchange(Group_Chat *chat, void (*function)(Group_Chat *chat, int peer, uint8_t change, void *),
void *userdata) void *userdata)
{ {

View File

@ -63,6 +63,8 @@ typedef struct Group_Chat {
uint32_t message_number; uint32_t message_number;
void (*group_message)(struct Group_Chat *m, int, uint8_t *, uint16_t, void *); void (*group_message)(struct Group_Chat *m, int, uint8_t *, uint16_t, void *);
void *group_message_userdata; 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 (*peer_namelistchange)(struct Group_Chat *m, int peer, uint8_t change, void *);
void *group_namelistchange_userdata; void *group_namelistchange_userdata;
@ -80,6 +82,7 @@ typedef struct Group_Chat {
#define GROUP_CHAT_QUIT 24 #define GROUP_CHAT_QUIT 24
#define GROUP_CHAT_PEER_NICK 48 #define GROUP_CHAT_PEER_NICK 48
#define GROUP_CHAT_CHAT_MESSAGE 64 #define GROUP_CHAT_CHAT_MESSAGE 64
#define GROUP_CHAT_ACTION 63
/* Copy the name of peernum to name. /* Copy the name of peernum to name.
* name must be at least MAX_NICK_BYTES long. * 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 callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat, int, uint8_t *, uint16_t, void *),
void *userdata); 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. * 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); 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. * Set our nick for this group.
* *

View File

@ -405,6 +405,7 @@ void tox_callback_group_invite(Tox *tox, void (*function)(Messenger *tox, int, u
Messenger *m = tox; Messenger *m = tox;
m_callback_group_invite(m, function, userdata); m_callback_group_invite(m, function, userdata);
} }
/* Set the callback for group messages. /* Set the callback for group messages.
* *
* Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata) * 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); 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. /* Set callback function for peer name list changes.
* *
* It gets called every time the name list changes(new peer/name, deleted peer) * It gets called every time the name list changes(new peer/name, deleted peer)
* Function(Tox *tox, int groupnumber, void *userdata) * 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) void tox_callback_group_namelist_change(Tox *tox, void (*function)(Tox *tox, int, int, uint8_t, void *), void *userdata)
{ {
Messenger *m = tox; 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); 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 the number of peers in the group chat on success.
* return -1 on failure * return -1 on failure
*/ */

View File

@ -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 tox_callback_group_message(Tox *tox, void (*function)(Tox *tox, int, int, uint8_t *, uint16_t, void *),
void *userdata); 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. /* Set callback function for peer name list changes.
* *
* It gets called every time the name list changes(new peer/name, deleted peer) * 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); 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 the number of peers in the group chat on success.
* return -1 on failure * return -1 on failure
*/ */