Group chats now have types.

This commit is contained in:
irungentoo 2014-11-10 17:57:47 -05:00
parent 5715a94061
commit 97742ed1e9
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
4 changed files with 55 additions and 22 deletions

View File

@ -629,11 +629,13 @@ static int add_conn_to_groupchat(Group_Chats *g_c, int friendcon_id, int groupnu
}
/* Creates a new groupchat and puts it in the chats array.
*
* type is one of GROUPCHAT_TYPE_*
*
* return group number on success.
* return -1 on failure.
*/
int add_groupchat(Group_Chats *g_c)
int add_groupchat(Group_Chats *g_c, uint8_t type)
{
int groupnumber = create_group_chat(g_c);
@ -644,7 +646,8 @@ int add_groupchat(Group_Chats *g_c)
g->status = GROUPCHAT_STATUS_CONNECTED;
g->number_joined = -1;
new_symmetric_key(g->identifier);
new_symmetric_key(g->identifier + 1);
g->identifier[0] = type;
g->peer_number = 0; /* Founder is peer 0. */
memcpy(g->real_pk, g_c->m->net_crypto->self_public_key, crypto_box_PUBLICKEYBYTES);
int peer_index = addpeer(g_c, groupnumber, g->real_pk, g_c->m->dht->self_public_key, 0);
@ -845,15 +848,20 @@ int invite_friend(Group_Chats *g_c, int32_t friendnumber, int groupnumber)
static unsigned int send_peer_query(Group_Chats *g_c, int friendcon_id, uint16_t group_num);
/* Join a group (you need to have been invited first.)
*
* expected_type is the groupchat type we expect the chat we are joining is.
*
* returns group number on success
* returns -1 on failure.
*/
int join_groupchat(Group_Chats *g_c, int32_t friendnumber, const uint8_t *data, uint16_t length)
int join_groupchat(Group_Chats *g_c, int32_t friendnumber, uint8_t expected_type, const uint8_t *data, uint16_t length)
{
if (length != sizeof(uint16_t) + GROUP_IDENTIFIER_LENGTH)
return -1;
if (data[sizeof(uint16_t)] != expected_type)
return -1;
int friendcon_id = getfriendcon_id(g_c->m, friendnumber);
if (friendcon_id == -1)
@ -899,12 +907,12 @@ int join_groupchat(Group_Chats *g_c, int32_t friendnumber, const uint8_t *data,
/* Set the callback for group invites.
*
* Function(Group_Chats *g_c, int32_t friendnumber, uint8_t *data, uint16_t length, void *userdata)
* Function(Group_Chats *g_c, int32_t friendnumber, uint8_t type, uint8_t *data, uint16_t length, void *userdata)
*
* data of length is what needs to be passed to join_groupchat().
*/
void g_callback_group_invite(Group_Chats *g_c, void (*function)(Messenger *m, int32_t, const uint8_t *, uint16_t,
void *), void *userdata)
void g_callback_group_invite(Group_Chats *g_c, void (*function)(Messenger *m, int32_t, uint8_t, const uint8_t *,
uint16_t, void *), void *userdata)
{
g_c->invite_callback = function;
g_c->invite_callback_userdata = userdata;
@ -1086,7 +1094,8 @@ static void handle_friend_invite_packet(Messenger *m, int32_t friendnumber, cons
if (groupnumber == -1) {
if (g_c->invite_callback)
g_c->invite_callback(m, friendnumber, invite_data, invite_length, g_c->invite_callback_userdata);
g_c->invite_callback(m, friendnumber, *(invite_data + sizeof(uint16_t)), invite_data, invite_length,
g_c->invite_callback_userdata);
return;
}
@ -2002,6 +2011,7 @@ void kill_groupchats(Group_Chats *g_c)
m_callback_group_invite(g_c->m, NULL);
g_c->m->group_chat_object = 0;
free(g_c->av_object);
free(g_c);
}

View File

@ -33,6 +33,11 @@ enum {
GROUPCHAT_STATUS_CONNECTED
};
enum {
GROUPCHAT_TYPE_TEXT,
GROUPCHAT_TYPE_AV
};
#define MAX_LOSSY_COUNT 256
typedef struct {
@ -55,7 +60,7 @@ typedef struct {
#define DESIRED_CLOSE_CONNECTIONS 4
#define MAX_GROUP_CONNECTIONS 16
#define GROUP_IDENTIFIER_LENGTH crypto_box_KEYBYTES /* So we can use new_symmetric_key(...) to fill it */
#define GROUP_IDENTIFIER_LENGTH (1 + crypto_box_KEYBYTES) /* crypto_box_KEYBYTES so we can use new_symmetric_key(...) to fill it */
enum {
GROUPCHAT_CLOSE_NONE,
@ -107,7 +112,7 @@ typedef struct {
Group_c *chats;
uint32_t num_chats;
void (*invite_callback)(Messenger *m, int32_t, const uint8_t *, uint16_t, void *);
void (*invite_callback)(Messenger *m, int32_t, uint8_t, const uint8_t *, uint16_t, void *);
void *invite_callback_userdata;
void (*message_callback)(Messenger *m, int, int, const uint8_t *, uint16_t, void *);
void *message_callback_userdata;
@ -119,16 +124,18 @@ typedef struct {
struct {
int (*function)(void *, int, int, void *, const uint8_t *, uint16_t);
} lossy_packethandlers[256];
void *av_object;
} Group_Chats;
/* Set the callback for group invites.
*
* Function(Group_Chats *g_c, int32_t friendnumber, uint8_t *data, uint16_t length, void *userdata)
* Function(Group_Chats *g_c, int32_t friendnumber, uint8_t type, uint8_t *data, uint16_t length, void *userdata)
*
* data of length is what needs to be passed to join_groupchat().
*/
void g_callback_group_invite(Group_Chats *g_c, void (*function)(Messenger *m, int32_t, const uint8_t *, uint16_t,
void *), void *userdata);
void g_callback_group_invite(Group_Chats *g_c, void (*function)(Messenger *m, int32_t, uint8_t, const uint8_t *,
uint16_t, void *), void *userdata);
/* Set the callback for group messages.
*
@ -158,11 +165,13 @@ void g_callback_group_namelistchange(Group_Chats *g_c, void (*function)(Messenge
void *userdata);
/* Creates a new groupchat and puts it in the chats array.
*
* type is one of GROUPCHAT_TYPE_*
*
* return group number on success.
* return -1 on failure.
*/
int add_groupchat(Group_Chats *g_c);
int add_groupchat(Group_Chats *g_c, uint8_t type);
/* Delete a groupchat from the chats array.
*
@ -186,11 +195,13 @@ int group_peername(const Group_Chats *g_c, int groupnumber, int peernumber, uint
int invite_friend(Group_Chats *g_c, int32_t friendnumber, int groupnumber);
/* Join a group (you need to have been invited first.)
*
* expected_type is the groupchat type we expect the chat we are joining is.
*
* returns group number on success
* returns -1 on failure.
*/
int join_groupchat(Group_Chats *g_c, int32_t friendnumber, const uint8_t *data, uint16_t length);
int join_groupchat(Group_Chats *g_c, int32_t friendnumber, uint8_t expected_type, const uint8_t *data, uint16_t length);
/* send a group message
* return 0 on success

View File

@ -551,12 +551,12 @@ int tox_send_lossless_packet(const Tox *tox, int32_t friendnumber, const uint8_t
/* Set the callback for group invites.
*
* Function(Tox *tox, int32_t friendnumber, uint8_t *data, uint16_t length, void *userdata)
* Function(Tox *tox, int32_t friendnumber, uint8_t type, uint8_t *data, uint16_t length, void *userdata)
*
* data of length is what needs to be passed to join_groupchat().
*/
void tox_callback_group_invite(Tox *tox, void (*function)(Messenger *tox, int32_t, const uint8_t *, uint16_t, void *),
void *userdata)
void tox_callback_group_invite(Tox *tox, void (*function)(Messenger *tox, int32_t, uint8_t, const uint8_t *, uint16_t,
void *), void *userdata)
{
Messenger *m = tox;
g_callback_group_invite(m->group_chat_object, function, userdata);
@ -603,7 +603,7 @@ void tox_callback_group_namelist_change(Tox *tox, void (*function)(Tox *tox, int
int tox_add_groupchat(Tox *tox)
{
Messenger *m = tox;
return add_groupchat(m->group_chat_object);
return add_groupchat(m->group_chat_object, GROUPCHAT_TYPE_TEXT);
}
/* Delete a groupchat from the chats array.
@ -648,7 +648,7 @@ int tox_invite_friend(Tox *tox, int32_t friendnumber, int groupnumber)
int tox_join_groupchat(Tox *tox, int32_t friendnumber, const uint8_t *data, uint16_t length)
{
Messenger *m = tox;
return join_groupchat(m->group_chat_object, friendnumber, data, length);
return join_groupchat(m->group_chat_object, friendnumber, GROUPCHAT_TYPE_TEXT, data, length);
}
/* send a group message

View File

@ -417,14 +417,26 @@ int tox_send_lossless_packet(const Tox *tox, int32_t friendnumber, const uint8_t
/**********GROUP CHAT FUNCTIONS: WARNING Group chats will be rewritten so this might change ************/
/* Group chat types for tox_callback_group_invite function.
*
* TOX_GROUPCHAT_TYPE_TEXT groupchats must be accepted with the tox_join_groupchat() function.
* The function to accept TOX_GROUPCHAT_TYPE_AV is in toxav.
*/
enum {
TOX_GROUPCHAT_TYPE_TEXT,
TOX_GROUPCHAT_TYPE_AV
};
/* Set the callback for group invites.
*
* Function(Tox *tox, int32_t friendnumber, const uint8_t *data, uint16_t length, void *userdata)
* Function(Tox *tox, int32_t friendnumber, uint8_t type, uint8_t *data, uint16_t length, void *userdata)
*
* data of length is what needs to be passed to join_groupchat().
*
* for what type means see the enum right above this comment.
*/
void tox_callback_group_invite(Tox *tox, void (*function)(Tox *tox, int32_t, const uint8_t *, uint16_t, void *),
void *userdata);
void tox_callback_group_invite(Tox *tox, void (*function)(Tox *tox, int32_t, uint8_t, const uint8_t *, uint16_t,
void *), void *userdata);
/* Set the callback for group messages.
*