Skip to content

Commit 388942d

Browse files
zugziphydf
authored andcommitted
Expose offline conference peers in API
1 parent e532057 commit 388942d

File tree

8 files changed

+372
-59
lines changed

8 files changed

+372
-59
lines changed

toxav/groupav.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ int add_av_groupchat(const Logger *log, Tox *tox, Group_Chats *g_c, audio_data_c
473473
}
474474

475475
if (groupchat_enable_av(log, tox, g_c, groupnumber, audio_callback, userdata) == -1) {
476-
del_groupchat(g_c, groupnumber);
476+
del_groupchat(g_c, groupnumber, true);
477477
return -1;
478478
}
479479

@@ -495,7 +495,7 @@ int join_av_groupchat(const Logger *log, Tox *tox, Group_Chats *g_c, uint32_t fr
495495
}
496496

497497
if (groupchat_enable_av(log, tox, g_c, groupnumber, audio_callback, userdata) == -1) {
498-
del_groupchat(g_c, groupnumber);
498+
del_groupchat(g_c, groupnumber, true);
499499
return -1;
500500
}
501501

toxcore/group.c

Lines changed: 83 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ typedef enum Groupchat_Closest {
271271

272272
static int add_to_closest(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *real_pk, const uint8_t *temp_pk)
273273
{
274+
// TODO(zugz): this can be const. Similarly throughout the file.
274275
Group_c *g = get_group_c(g_c, groupnumber);
275276

276277
if (!g) {
@@ -1033,19 +1034,26 @@ int add_groupchat(Group_Chats *g_c, uint8_t type)
10331034
return groupnumber;
10341035
}
10351036

1036-
/* Delete a groupchat from the chats array.
1037+
static int group_leave(const Group_Chats *g_c, uint32_t groupnumber);
1038+
1039+
/* Delete a groupchat from the chats array, informing the group first as
1040+
* appropriate.
10371041
*
10381042
* return 0 on success.
10391043
* return -1 if groupnumber is invalid.
10401044
*/
1041-
int del_groupchat(Group_Chats *g_c, uint32_t groupnumber)
1045+
int del_groupchat(Group_Chats *g_c, uint32_t groupnumber, bool leave_permanently)
10421046
{
10431047
Group_c *g = get_group_c(g_c, groupnumber);
10441048

10451049
if (!g) {
10461050
return -1;
10471051
}
10481052

1053+
if (leave_permanently) {
1054+
group_leave(g_c, groupnumber);
1055+
}
1056+
10491057
for (uint32_t i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
10501058
if (g->close[i].type == GROUPCHAT_CLOSE_NONE) {
10511059
continue;
@@ -1071,82 +1079,115 @@ int del_groupchat(Group_Chats *g_c, uint32_t groupnumber)
10711079
return wipe_group_chat(g_c, groupnumber);
10721080
}
10731081

1074-
/* Copy the public key of peernumber who is in groupnumber to pk.
1075-
* pk must be CRYPTO_PUBLIC_KEY_SIZE long.
1082+
/* Copy the public key of (frozen, if frozen is true) peernumber who is in
1083+
* groupnumber to pk. pk must be CRYPTO_PUBLIC_KEY_SIZE long.
10761084
*
10771085
* return 0 on success
10781086
* return -1 if groupnumber is invalid.
10791087
* return -2 if peernumber is invalid.
10801088
*/
1081-
int group_peer_pubkey(const Group_Chats *g_c, uint32_t groupnumber, int peernumber, uint8_t *pk)
1089+
int group_peer_pubkey(const Group_Chats *g_c, uint32_t groupnumber, int peernumber, uint8_t *pk, bool frozen)
10821090
{
10831091
Group_c *g = get_group_c(g_c, groupnumber);
10841092

10851093
if (!g) {
10861094
return -1;
10871095
}
10881096

1089-
if ((uint32_t)peernumber >= g->numpeers) {
1097+
const Group_Peer *list = frozen ? g->frozen : g->group;
1098+
const uint32_t num = frozen ? g->numfrozen : g->numpeers;
1099+
1100+
if ((uint32_t)peernumber >= num) {
10901101
return -2;
10911102
}
10921103

1093-
memcpy(pk, g->group[peernumber].real_pk, CRYPTO_PUBLIC_KEY_SIZE);
1104+
memcpy(pk, list[peernumber].real_pk, CRYPTO_PUBLIC_KEY_SIZE);
10941105
return 0;
10951106
}
10961107

10971108
/*
1098-
* Return the size of peernumber's name.
1109+
* Return the size of (frozen, if frozen is true) peernumber's name.
10991110
*
11001111
* return -1 if groupnumber is invalid.
11011112
* return -2 if peernumber is invalid.
11021113
*/
1103-
int group_peername_size(const Group_Chats *g_c, uint32_t groupnumber, int peernumber)
1114+
int group_peername_size(const Group_Chats *g_c, uint32_t groupnumber, int peernumber, bool frozen)
11041115
{
11051116
Group_c *g = get_group_c(g_c, groupnumber);
11061117

11071118
if (!g) {
11081119
return -1;
11091120
}
11101121

1111-
if ((uint32_t)peernumber >= g->numpeers) {
1122+
const Group_Peer *list = frozen ? g->frozen : g->group;
1123+
const uint32_t num = frozen ? g->numfrozen : g->numpeers;
1124+
1125+
if ((uint32_t)peernumber >= num) {
11121126
return -2;
11131127
}
11141128

1115-
if (g->group[peernumber].nick_len == 0) {
1129+
if (list[peernumber].nick_len == 0) {
11161130
return 0;
11171131
}
11181132

1119-
return g->group[peernumber].nick_len;
1133+
return list[peernumber].nick_len;
11201134
}
11211135

1122-
/* Copy the name of peernumber who is in groupnumber to name.
1123-
* name must be at least MAX_NAME_LENGTH long.
1136+
/* Copy the name of (frozen, if frozen is true) peernumber who is in
1137+
* groupnumber to name. name must be at least MAX_NAME_LENGTH long.
11241138
*
11251139
* return length of name if success
11261140
* return -1 if groupnumber is invalid.
11271141
* return -2 if peernumber is invalid.
11281142
*/
1129-
int group_peername(const Group_Chats *g_c, uint32_t groupnumber, int peernumber, uint8_t *name)
1143+
int group_peername(const Group_Chats *g_c, uint32_t groupnumber, int peernumber, uint8_t *name, bool frozen)
11301144
{
11311145
Group_c *g = get_group_c(g_c, groupnumber);
11321146

11331147
if (!g) {
11341148
return -1;
11351149
}
11361150

1137-
if ((uint32_t)peernumber >= g->numpeers) {
1151+
const Group_Peer *list = frozen ? g->frozen : g->group;
1152+
const uint32_t num = frozen ? g->numfrozen : g->numpeers;
1153+
1154+
if ((uint32_t)peernumber >= num) {
11381155
return -2;
11391156
}
11401157

1141-
if (g->group[peernumber].nick_len == 0) {
1158+
if (list[peernumber].nick_len == 0) {
11421159
return 0;
11431160
}
11441161

1145-
memcpy(name, g->group[peernumber].nick, g->group[peernumber].nick_len);
1146-
return g->group[peernumber].nick_len;
1162+
memcpy(name, list[peernumber].nick, list[peernumber].nick_len);
1163+
return list[peernumber].nick_len;
11471164
}
11481165

1149-
/* List all the peers in the group chat.
1166+
/* Copy last active timestamp of frozennumber who is in groupnumber to
1167+
* last_active.
1168+
*
1169+
* return 0 on success.
1170+
* return -1 if groupnumber is invalid.
1171+
* return -2 if frozennumber is invalid.
1172+
*/
1173+
int group_frozen_last_active(const Group_Chats *g_c, uint32_t groupnumber, int peernumber,
1174+
uint64_t *last_active)
1175+
{
1176+
Group_c *g = get_group_c(g_c, groupnumber);
1177+
1178+
if (!g) {
1179+
return -1;
1180+
}
1181+
1182+
if ((uint32_t)peernumber >= g->numfrozen) {
1183+
return -2;
1184+
}
1185+
1186+
*last_active = g->frozen[peernumber].last_active;
1187+
return 0;
1188+
}
1189+
1190+
/* List all the (frozen, if frozen is true) peers in the group chat.
11501191
*
11511192
* Copies the names of the peers to the name[length][MAX_NAME_LENGTH] array.
11521193
*
@@ -1157,35 +1198,38 @@ int group_peername(const Group_Chats *g_c, uint32_t groupnumber, int peernumber,
11571198
* return -1 on failure.
11581199
*/
11591200
int group_names(const Group_Chats *g_c, uint32_t groupnumber, uint8_t names[][MAX_NAME_LENGTH], uint16_t lengths[],
1160-
uint16_t length)
1201+
uint16_t length, bool frozen)
11611202
{
11621203
Group_c *g = get_group_c(g_c, groupnumber);
11631204

11641205
if (!g) {
11651206
return -1;
11661207
}
11671208

1209+
const uint32_t num = frozen ? g->numfrozen : g->numpeers;
1210+
11681211
unsigned int i;
11691212

1170-
for (i = 0; i < g->numpeers && i < length; ++i) {
1171-
lengths[i] = group_peername(g_c, groupnumber, i, names[i]);
1213+
for (i = 0; i < num && i < length; ++i) {
1214+
lengths[i] = group_peername(g_c, groupnumber, i, names[i], frozen);
11721215
}
11731216

11741217
return i;
11751218
}
11761219

1177-
/* Return the number of peers in the group chat on success.
1220+
/* Return the number of (frozen, if frozen is true) peers in the group chat on
1221+
* success.
11781222
* return -1 if groupnumber is invalid.
11791223
*/
1180-
int group_number_peers(const Group_Chats *g_c, uint32_t groupnumber)
1224+
int group_number_peers(const Group_Chats *g_c, uint32_t groupnumber, bool frozen)
11811225
{
11821226
Group_c *g = get_group_c(g_c, groupnumber);
11831227

11841228
if (!g) {
11851229
return -1;
11861230
}
11871231

1188-
return g->numpeers;
1232+
return frozen ? g->numfrozen : g->numpeers;
11891233
}
11901234

11911235
/* return 1 if the peernumber corresponds to ours.
@@ -1597,18 +1641,18 @@ static int group_name_send(const Group_Chats *g_c, uint32_t groupnumber, const u
15971641
}
15981642

15991643
/* send message to announce leaving group
1600-
* return true on success
1601-
* return false on failure
1644+
* return 0 on success
1645+
* return -1 on failure
16021646
*/
1603-
bool group_leave(const Group_Chats *g_c, uint32_t groupnumber)
1647+
static int group_leave(const Group_Chats *g_c, uint32_t groupnumber)
16041648
{
16051649
Group_c *g = get_group_c(g_c, groupnumber);
16061650

16071651
if (!g) {
1608-
return false;
1652+
return -1;
16091653
}
16101654

1611-
return group_kill_peer_send(g_c, groupnumber, g->peer_number) == 0;
1655+
return group_kill_peer_send(g_c, groupnumber, g->peer_number);
16121656
}
16131657

16141658

@@ -2900,7 +2944,7 @@ void send_name_all_groups(Group_Chats *g_c)
29002944
}
29012945
}
29022946

2903-
#define SAVED_PEER_SIZE_CONSTANT (2 * CRYPTO_PUBLIC_KEY_SIZE + 2 + 1)
2947+
#define SAVED_PEER_SIZE_CONSTANT (2 * CRYPTO_PUBLIC_KEY_SIZE + sizeof(uint16_t) + sizeof(uint64_t) + 1)
29042948

29052949
static uint32_t saved_peer_size(const Group_Peer *peer)
29062950
{
@@ -2918,6 +2962,9 @@ static uint8_t *save_peer(const Group_Peer *peer, uint8_t *data)
29182962
host_to_lendian_bytes16(data, peer->peer_number);
29192963
data += sizeof(uint16_t);
29202964

2965+
host_to_lendian_bytes64(data, peer->last_active);
2966+
data += sizeof(uint64_t);
2967+
29212968
*data = peer->nick_len;
29222969
++data;
29232970

@@ -3098,6 +3145,9 @@ static State_Load_Status load_conferences(Group_Chats *g_c, const uint8_t *data,
30983145
lendian_bytes_to_host16(&peer->peer_number, data);
30993146
data += sizeof(uint16_t);
31003147

3148+
lendian_bytes_to_host64(&peer->last_active, data);
3149+
data += sizeof(uint64_t);
3150+
31013151
peer->nick_len = *data;
31023152
++data;
31033153

@@ -3189,7 +3239,7 @@ void do_groupchats(Group_Chats *g_c, void *userdata)
31893239
void kill_groupchats(Group_Chats *g_c)
31903240
{
31913241
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
3192-
del_groupchat(g_c, i);
3242+
del_groupchat(g_c, i, false);
31933243
}
31943244

31953245
m_callback_conference_invite(g_c->m, nullptr);

0 commit comments

Comments
 (0)