Added function to get the number of peers and list of names for group chats.

This commit is contained in:
irungentoo 2013-11-22 23:31:55 -05:00
parent 2079cbc2db
commit d2b56faded
7 changed files with 138 additions and 10 deletions

View File

@ -974,13 +974,25 @@ void print_invite(Tox *m, int friendnumber, uint8_t *group_public_key, void *use
tox_join_groupchat(m, friendnumber, group_public_key));
new_lines(msg);
}
void print_groupchatpeers(Tox *m, int groupnumber)
{
char msg[256];
int num = tox_group_number_peers(m, groupnumber);
if (num == -1)
return;
uint8_t names[num][TOX_MAX_NAME_LENGTH];
tox_group_copy_names(m, groupnumber, names, num);
uint32_t i;
for (i = 0; i < num; ++i)
new_lines(names[i]);
}
void print_groupmessage(Tox *m, int groupnumber, int peernumber, uint8_t *message, uint16_t length, void *userdata)
{
char msg[256 + length];
uint8_t name[TOX_MAX_NAME_LENGTH];
int len = tox_group_peername(m, groupnumber, peernumber, name);
//print_groupchatpeers(m, groupnumber);
if (len <= 0)
name[0] = 0;

View File

@ -707,6 +707,23 @@ int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint
/**********GROUP CHATS************/
/* return 1 if the groupnumber is not valid.
* return 0 if the groupnumber is valid.
*/
static uint8_t groupnumber_not_valid(Messenger *m, int groupnumber)
{
if ((unsigned int)groupnumber >= m->numchats)
return 1;
if (m->chats == NULL)
return 1;
if (m->chats[groupnumber] == NULL)
return 1;
return 0;
}
/* returns valid ip port of connected friend on success
* returns zeroed out IP_Port on failure
*/
@ -964,6 +981,7 @@ int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_
return -1;
}
/* send a group message
* return 0 on success
* return -1 on failure
@ -971,13 +989,7 @@ int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_
int group_message_send(Messenger *m, int groupnumber, uint8_t *message, uint32_t length)
{
if ((unsigned int)groupnumber >= m->numchats)
return -1;
if (m->chats == NULL)
return -1;
if (m->chats[groupnumber] == NULL)
if (groupnumber_not_valid(m, groupnumber))
return -1;
if (group_sendmessage(m->chats[groupnumber], message, length) > 0)
@ -986,6 +998,33 @@ int group_message_send(Messenger *m, int groupnumber, uint8_t *message, uint32_t
return -1;
}
/* Return the number of peers in the group chat on success.
* return -1 on failure
*/
int group_number_peers(Messenger *m, int groupnumber)
{
if (groupnumber_not_valid(m, groupnumber))
return -1;
return group_numpeers(m->chats[groupnumber]);
}
/* List all the peers in the group chat.
*
* Copies the names of the peers to the name[length][MAX_NICK_BYTES] array.
*
* returns the number of peers on success.
*
* return -1 on failure.
*/
int group_names(Messenger *m, int groupnumber, uint8_t names[][MAX_NICK_BYTES], uint16_t length)
{
if (groupnumber_not_valid(m, groupnumber))
return -1;
return group_client_names(m->chats[groupnumber], names, length);
}
static int handle_group(void *object, IP_Port source, uint8_t *packet, uint32_t length)
{
Messenger *m = object;

View File

@ -497,6 +497,21 @@ int join_groupchat(Messenger *m, int friendnumber, uint8_t *friend_group_public_
int group_message_send(Messenger *m, int groupnumber, uint8_t *message, uint32_t length);
/* Return the number of peers in the group chat on success.
* return -1 on failure
*/
int group_number_peers(Messenger *m, int groupnumber);
/* List all the peers in the group chat.
*
* Copies the names of the peers to the name[length][MAX_NICK_BYTES] array.
*
* returns the number of peers on success.
*
* return -1 on failure.
*/
int group_names(Messenger *m, int groupnumber, uint8_t names[][MAX_NICK_BYTES], uint16_t length);
/****************FILE SENDING*****************/

View File

@ -616,6 +616,19 @@ void callback_groupmessage(Group_Chat *chat, void (*function)(Group_Chat *chat,
chat->group_message_userdata = userdata;
}
uint32_t group_numpeers(Group_Chat *chat)
{
return chat->numpeers;
}
uint32_t group_client_names(Group_Chat *chat, uint8_t names[][MAX_NICK_BYTES], uint16_t length)
{
uint32_t i;
for (i = 0; i < chat->numpeers && i < length; ++i) {
group_peername(chat, i, names[i]);
}
return i;
}
Group_Chat *new_groupchat(Networking_Core *net)
{

View File

@ -128,6 +128,18 @@ uint32_t group_newpeer(Group_Chat *chat, uint8_t *client_id);
Group_Chat *new_groupchat(Networking_Core *net);
/* Return the number of peers in the group chat.
*/
uint32_t group_numpeers(Group_Chat *chat);
/* List all the peers in the group chat.
*
* Copies the names of the peers to the name[length][MAX_NICK_BYTES] array.
*
* returns the number of peers.
*/
uint32_t group_client_names(Group_Chat *chat, uint8_t names[][MAX_NICK_BYTES], uint16_t length);
/* Kill a group chat
*
* Frees the memory and everything.

View File

@ -477,6 +477,29 @@ int tox_group_message_send(Tox *tox, int groupnumber, uint8_t *message, uint32_t
return group_message_send(m, groupnumber, message, length);
}
/* Return the number of peers in the group chat on success.
* return -1 on failure
*/
int tox_group_number_peers(Tox *tox, int groupnumber)
{
Messenger *m = tox;
return group_number_peers(m, groupnumber);
}
/* List all the peers in the group chat.
*
* Copies the names of the peers to the name[length][MAX_NICK_BYTES] array.
*
* returns the number of peers on success.
*
* return -1 on failure.
*/
int tox_group_copy_names(Tox *tox, int groupnumber, uint8_t names[][TOX_MAX_NAME_LENGTH], uint16_t length)
{
Messenger *m = tox;
return group_names(m, groupnumber, names, length);
}
/* Return the number of chats in the instance m.
* You should use this to determine how much memory to allocate
* for copy_chatlist. */

View File

@ -398,13 +398,27 @@ int tox_invite_friend(Tox *tox, int friendnumber, int groupnumber);
*/
int tox_join_groupchat(Tox *tox, int friendnumber, uint8_t *friend_group_public_key);
/* send a group message
* return 0 on success
* return -1 on failure
*/
int tox_group_message_send(Tox *tox, int groupnumber, uint8_t *message, uint32_t length);
/* Return the number of peers in the group chat on success.
* return -1 on failure
*/
int tox_group_number_peers(Tox *tox, int groupnumber);
/* List all the peers in the group chat.
*
* Copies the names of the peers to the name[length][TOX_MAX_NAME_LENGTH] array.
*
* returns the number of peers on success.
*
* return -1 on failure.
*/
int tox_group_copy_names(Tox *tox, int groupnumber, uint8_t names[][TOX_MAX_NAME_LENGTH], uint16_t length);
/* Return the number of chats in the instance m.
* You should use this to determine how much memory to allocate
* for copy_chatlist. */