Coverage Report

Created: 2026-10-24 08:16

/work/toxcore/group_chats.h
Line
Count
Source (jump to first uncovered line)
1
/* SPDX-License-Identifier: GPL-3.0-or-later
2
 * Copyright © 2016-2020 The TokTok team.
3
 * Copyright © 2015 Tox project.
4
 */
5
6
/**
7
 * An implementation of massive text only group chats.
8
 */
9
10
#ifndef C_TOXCORE_TOXCORE_GROUP_CHATS_H
11
#define C_TOXCORE_TOXCORE_GROUP_CHATS_H
12
13
#include <stdbool.h>
14
#include <stdint.h>
15
16
#include "TCP_connection.h"
17
#include "attributes.h"
18
#include "bin_pack.h"
19
#include "bin_unpack.h"
20
#include "crypto_core.h"
21
#include "group_announce.h"
22
#include "group_common.h"
23
#include "group_connection.h"
24
#include "logger.h"
25
#include "mem.h"
26
#include "network.h"
27
28
#ifdef __cplusplus
29
extern "C" {
30
#endif
31
32
103k
#define GC_PING_TIMEOUT 12
33
0
#define GC_SEND_IP_PORT_INTERVAL (GC_PING_TIMEOUT * 5)
34
20.3k
#define GC_CONFIRMED_PEER_TIMEOUT (GC_PING_TIMEOUT * 4 + 10)
35
14.4k
#define GC_UNCONFIRMED_PEER_TIMEOUT GC_PING_TIMEOUT
36
37
33
#define GC_JOIN_DATA_LENGTH (ENC_PUBLIC_KEY_SIZE + CHAT_ID_SIZE)
38
39
/** Group topic lock states. */
40
typedef enum Group_Topic_Lock {
41
    TL_ENABLED  = 0x00,  // Only the Founder and moderators may set the topic
42
    TL_DISABLED = 0x01,  // Anyone except Observers may set the topic
43
} Group_Topic_Lock;
44
45
/** Group moderation events. */
46
typedef enum Group_Moderation_Event {
47
    MV_KICK      = 0x00,  // A peer has been kicked
48
    MV_OBSERVER  = 0x01,  // A peer has been demoted to Observer
49
    MV_USER      = 0x02,  // A peer has been demoted or promoted to User
50
    MV_MOD       = 0x03,  // A peer has been promoted to or demoted from Moderator
51
} Group_Moderation_Event;
52
53
/** Messenger level group invite types */
54
typedef enum Group_Invite_Message_Type {
55
    GROUP_INVITE              = 0x00,  // Peer has initiated an invite
56
    GROUP_INVITE_ACCEPTED     = 0x01,  // Peer has accepted the invite
57
    GROUP_INVITE_CONFIRMATION = 0x02,  // Peer has confirmed the accepted invite
58
} Group_Invite_Message_Type;
59
60
/** Group join rejection types. */
61
typedef enum Group_Join_Rejected {
62
    GJ_GROUP_FULL       = 0x00,
63
    GJ_INVALID_PASSWORD = 0x01,
64
    GJ_INVITE_FAILED    = 0x02,
65
    GJ_INVALID          = 0x03,
66
} Group_Join_Rejected;
67
68
/** Group broadcast packet types */
69
typedef enum Group_Broadcast_Type {
70
    GM_STATUS          = 0x00,  // Peer changed their status
71
    GM_NICK            = 0x01,  // Peer changed their nickname
72
    GM_PLAIN_MESSAGE   = 0x02,  // Peer sent a normal message
73
    GM_ACTION_MESSAGE  = 0x03,  // Peer sent an action message
74
    GM_PRIVATE_MESSAGE = 0x04,  // Peer sent a private message
75
    GM_PEER_EXIT       = 0x05,  // Peer left the group
76
    GM_KICK_PEER       = 0x06,  // Peer was kicked from the group
77
    GM_SET_MOD         = 0x07,  // Peer was promoted to or demoted from Moderator role
78
    GM_SET_OBSERVER    = 0x08,  // Peer was demoted to or promoted from Observer role
79
} Group_Broadcast_Type;
80
81
/***
82
 * Group packet types.
83
 *
84
 * For a detailed spec, see docs/DHT_Group_Chats_Packet_Spec.md
85
 */
86
typedef enum Group_Packet_Type {
87
    /* lossy packets (ID 0 is reserved) */
88
    GP_PING                     = 0x01,
89
    GP_MESSAGE_ACK              = 0x02,
90
    GP_INVITE_RESPONSE_REJECT   = 0x03,
91
92
    /* lossless packets */
93
    GP_CUSTOM_PRIVATE_PACKET    = 0xee,
94
    GP_FRAGMENT                 = 0xef,
95
    GP_KEY_ROTATION             = 0xf0,
96
    GP_TCP_RELAYS               = 0xf1,
97
    GP_CUSTOM_PACKET            = 0xf2,
98
    GP_BROADCAST                = 0xf3,
99
    GP_PEER_INFO_REQUEST        = 0xf4,
100
    GP_PEER_INFO_RESPONSE       = 0xf5,
101
    GP_INVITE_REQUEST           = 0xf6,
102
    GP_INVITE_RESPONSE          = 0xf7,
103
    GP_SYNC_REQUEST             = 0xf8,
104
    GP_SYNC_RESPONSE            = 0xf9,
105
    GP_TOPIC                    = 0xfa,
106
    GP_SHARED_STATE             = 0xfb,
107
    GP_MOD_LIST                 = 0xfc,
108
    GP_SANCTIONS_LIST           = 0xfd,
109
    GP_FRIEND_INVITE            = 0xfe,
110
    GP_HS_RESPONSE_ACK          = 0xff,
111
} Group_Packet_Type;
112
113
/** Lossless message acknowledgement types. */
114
typedef enum Group_Message_Ack_Type {
115
    GR_ACK_RECV    = 0x00,  // indicates a message has been received
116
    GR_ACK_REQ     = 0x01,  // indicates a message needs to be re-sent
117
} Group_Message_Ack_Type;
118
119
/** @brief Returns the GC_Connection object associated with `peer_number`.
120
 * Returns null if peer_number does not designate a valid peer.
121
 */
122
GC_Connection *_Nullable get_gc_connection(const GC_Chat *_Nonnull chat, int peer_number);
123
124
/** @brief Returns the jenkins hash of a 32 byte public encryption key. */
125
uint32_t gc_get_pk_jenkins_hash(const uint8_t *_Nonnull public_key);
126
127
/** @brief Check if peer with the public encryption key is in peer list.
128
 *
129
 * Returns the peer number if peer is in the peer list.
130
 * Returns -1 if peer is not in the peer list.
131
 *
132
 * If `confirmed` is true the peer number will only be returned if the peer is confirmed.
133
 */
134
int get_peer_number_of_enc_pk(const GC_Chat *_Nonnull chat, const uint8_t *_Nonnull public_enc_key, bool confirmed);
135
136
/** @brief Encrypts `data` of size `length` using the peer's shared key and a new nonce.
137
 *
138
 * Adds encrypted header consisting of: packet type, message_id (only for lossless packets).
139
 * Adds plaintext header consisting of: packet identifier, self public encryption key, nonce.
140
 *
141
 * Return length of encrypted packet on success.
142
 * Return -1 if plaintext length is invalid.
143
 * Return -2 if malloc fails.
144
 * Return -3 if encryption fails.
145
 */
146
int group_packet_wrap(
147
    const Logger *_Nonnull log, const Memory *_Nonnull mem, const Random *_Nonnull rng, const uint8_t *_Nonnull self_pk, const uint8_t *_Nonnull shared_key, uint8_t *_Nonnull packet,
148
    uint16_t packet_size, const uint8_t *_Nullable data, uint16_t length, uint64_t message_id,
149
    uint8_t gp_packet_type, Net_Packet_Type net_packet_type);
150
/** @brief Returns the size of a wrapped/encrypted packet with a plain size of `length`.
151
 *
152
 * `packet_type` should be either NET_PACKET_GC_LOSSY or NET_PACKET_GC_LOSSLESS.
153
 */
154
uint16_t gc_get_wrapped_packet_size(uint16_t length, Net_Packet_Type packet_type);
155
156
/** @brief Sends a plain message or an action, depending on type.
157
 *
158
 * `length` must not exceed MAX_GC_MESSAGE_SIZE and must not be equal to zero.
159
 * `message_id` should either point to a uint32_t or be NULL.
160
 *
161
 * Returns 0 on success.
162
 * Returns -1 if the message is too long.
163
 * Returns -2 if the message pointer is NULL or length is zero.
164
 * Returns -3 if the message type is invalid.
165
 * Returns -4 if the sender does not have permission to speak.
166
 * Returns -5 if the packet fails to send.
167
 */
168
int gc_send_message(const GC_Chat *_Nonnull chat, const uint8_t *_Nonnull message, uint16_t length, uint8_t type,
169
                    uint32_t *_Nullable message_id);
170
/** @brief Sends a private message to peer_id.
171
 *
172
 * `length` must not exceed MAX_GC_MESSAGE_SIZE and must not be equal to zero.
173
 *
174
 * Returns 0 on success.
175
 * Returns -1 if the message is too long.
176
 * Returns -2 if the message pointer is NULL or length is zero.
177
 * Returns -3 if the peer_id is invalid.
178
 * Returns -4 if the message type is invalid.
179
 * Returns -5 if the sender has the observer role.
180
 * Returns -6 if the packet fails to send.
181
 */
182
int gc_send_private_message(const GC_Chat *_Nonnull chat, GC_Peer_Id peer_id, uint8_t type, const uint8_t *_Nonnull message,
183
                            uint16_t length, uint32_t *_Nullable message_id);
184
/** @brief Sends a custom packet to the group. If lossless is true, the packet will be lossless.
185
 *
186
 * `length` must not exceed MAX_GC_MESSAGE_SIZE and must not be equal to zero.
187
 *
188
 * Returns 0 on success.
189
 * Returns -1 if the message is too long.
190
 * Returns -2 if the message pointer is NULL or length is zero.
191
 * Returns -3 if the packet did not successfully send to any peer.
192
 */
193
int gc_send_custom_packet(const GC_Chat *_Nonnull chat, bool lossless, const uint8_t *_Nonnull data, uint16_t length);
194
195
/** @brief Sends a custom private packet to the peer designated by peer_id.
196
 *
197
 * `length` must not exceed MAX_GC_MESSAGE_SIZE and must not be equal to zero.
198
 *
199
 * @retval 0 on success.
200
 * @retval -1 if the message is too long.
201
 * @retval -2 if the message pointer is NULL or length is zero.
202
 * @retval -3 if the supplied peer_id does not designate a valid peer.
203
 * @retval -4 if the packet fails to send.
204
 */
205
int gc_send_custom_private_packet(const GC_Chat *_Nonnull chat, bool lossless, GC_Peer_Id peer_id, const uint8_t *_Nonnull message, uint16_t length);
206
207
/** @brief Sets ignore for peer_id.
208
 *
209
 * Returns 0 on success.
210
 * Returns -1 if the peer_id is invalid.
211
 * Returns -2 if the caller attempted to ignore himself.
212
 */
213
int gc_set_ignore(const GC_Chat *_Nonnull chat, GC_Peer_Id peer_id, bool ignore);
214
215
/** @brief Sets the group topic and broadcasts it to the group.
216
 *
217
 * If `length` is equal to zero the topic will be unset.
218
 *
219
 * Returns 0 on success.
220
 * Returns -1 if the topic is too long (must be `<= MAX_GC_TOPIC_SIZE`).
221
 * Returns -2 if the caller does not have the required permissions to set the topic.
222
 * Returns -3 if the packet cannot be created or signing fails.
223
 * Returns -4 if the packet fails
224
 */
225
int gc_set_topic(GC_Chat *_Nonnull chat, const uint8_t *_Nullable topic, uint16_t length);
226
/** @brief Copies the group topic to `topic`. If topic is null this function has no effect.
227
 *
228
 * Call `gc_get_topic_size` to determine the allocation size for the `topic` parameter.
229
 *
230
 * The data written to `topic` is equal to the data received by the last topic callback.
231
 */
232
void gc_get_topic(const GC_Chat *_Nonnull chat, uint8_t *_Nullable topic);
233
/** @brief Returns the topic length.
234
 *
235
 * The return value is equal to the `length` agument received by the last topic callback.
236
 */
237
uint16_t gc_get_topic_size(const GC_Chat *_Nonnull chat);
238
239
/** @brief Copies group name to `group_name`. If `group_name` is null this function has no effect.
240
 *
241
 * Call `gc_get_group_name_size` to determine the allocation size for the `group_name`
242
 * parameter.
243
 */
244
void gc_get_group_name(const GC_Chat *_Nonnull chat, uint8_t *_Nonnull group_name);
245
246
/** @brief Returns the group name length. */
247
uint16_t gc_get_group_name_size(const GC_Chat *_Nonnull chat);
248
249
/** @brief Copies the group password to password.
250
 *
251
 * If password is null this function has no effect.
252
 *
253
 * Call the `gc_get_password_size` function to determine the allocation size for
254
 * the `password` buffer.
255
 *
256
 * The data received is equal to the data received by the last password callback.
257
 */
258
void gc_get_password(const GC_Chat *_Nonnull chat, uint8_t *_Nonnull password);
259
260
/** @brief Returns the group password length. */
261
uint16_t gc_get_password_size(const GC_Chat *_Nonnull chat);
262
263
/** @brief Returns the group privacy state.
264
 *
265
 * The value returned is equal to the data receieved by the last privacy_state callback.
266
 */
267
Group_Privacy_State gc_get_privacy_state(const GC_Chat *_Nonnull chat);
268
269
/** @brief Returns the group topic lock state.
270
 *
271
 * The value returned is equal to the data received by the last last topic_lock callback.
272
 */
273
Group_Topic_Lock gc_get_topic_lock_state(const GC_Chat *_Nonnull chat);
274
275
/** @brief Returns the group voice state.
276
 *
277
 * The value returned is equal to the data received by the last voice_state callback.
278
 */
279
Group_Voice_State gc_get_voice_state(const GC_Chat *_Nonnull chat);
280
281
/** @brief Returns the group peer limit.
282
 *
283
 * The value returned is equal to the data receieved by the last peer_limit callback.
284
 */
285
uint16_t gc_get_max_peers(const GC_Chat *_Nonnull chat);
286
287
/** @brief Sets your own nick to `nick`.
288
 *
289
 * `length` cannot exceed MAX_GC_NICK_SIZE. if `length` is zero or `name` is a
290
 * null pointer the function call will fail.
291
 *
292
 * Returns 0 on success.
293
 * Returns -1 if group_number is invalid.
294
 * Returns -2 if the length is too long.
295
 * Returns -3 if the length is zero or nick is a NULL pointer.
296
 * Returns -4 if the packet fails to send.
297
 */
298
int gc_set_self_nick(const Messenger *_Nonnull m, int group_number, const uint8_t *_Nonnull nick, uint16_t length);
299
300
/** @brief Copies your own name to `nick`.
301
 *
302
 * If `nick` is null this function has no effect.
303
 */
304
void gc_get_self_nick(const GC_Chat *_Nonnull chat, uint8_t *_Nonnull nick);
305
306
/** @brief Return your own nick length.
307
 *
308
 * If no nick was set before calling this function it will return 0.
309
 */
310
uint16_t gc_get_self_nick_size(const GC_Chat *_Nonnull chat);
311
312
/** @brief Returns your own group role. */
313
Group_Role gc_get_self_role(const GC_Chat *_Nonnull chat);
314
315
/** @brief Return your own status. */
316
uint8_t gc_get_self_status(const GC_Chat *_Nonnull chat);
317
318
/** @brief Returns your own peer id. */
319
GC_Peer_Id gc_get_self_peer_id(const GC_Chat *_Nonnull chat);
320
321
/** @brief Copies self public key to `public_key`.
322
 *
323
 * If `public_key` is null this function has no effect.
324
 *
325
 * This key is permanently tied to our identity for `chat` until we explicitly
326
 * exit the group. This key is the only way for other peers to reliably identify
327
 * us across client restarts.
328
 */
329
void gc_get_self_public_key(const GC_Chat *_Nonnull chat, uint8_t *_Nullable public_key);
330
/** @brief Copies nick designated by `peer_id` to `name`.
331
 *
332
 * Call `gc_get_peer_nick_size` to determine the allocation size for the `name` parameter.
333
 *
334
 * The data written to `name` is equal to the data received by the last nick_change callback.
335
 *
336
 * Returns true on success.
337
 * Returns false if peer_id is invalid.
338
 */
339
bool gc_get_peer_nick(const GC_Chat *_Nonnull chat, GC_Peer_Id peer_id, uint8_t *_Nullable name);
340
/** @brief Returns the length of the nick for the peer designated by `peer_id`.
341
 * Returns -1 if peer_id is invalid.
342
 *
343
 * The value returned is equal to the `length` argument received by the last
344
 * nick_change callback.
345
 */
346
int gc_get_peer_nick_size(const GC_Chat *_Nonnull chat, GC_Peer_Id peer_id);
347
348
/** @brief Copies peer_id's public key to `public_key`.
349
 *
350
 * This key is permanently tied to the peer's identity for `chat` until they explicitly
351
 * exit the group. This key is the only way for to reliably identify the given peer
352
 * across client restarts.
353
 *
354
 * `public_key` shold have room for at least ENC_PUBLIC_KEY_SIZE bytes.
355
 *
356
 * Returns 0 on success.
357
 * Returns -1 if peer_id is invalid or doesn't correspond to a valid peer connection.
358
 * Returns -2 if `public_key` is null.
359
 */
360
int gc_get_peer_public_key_by_peer_id(const GC_Chat *_Nonnull chat, GC_Peer_Id peer_id, uint8_t *_Nullable public_key);
361
/** @brief Returns the length of the IP address for the peer designated by `peer_id`.
362
 * Returns -1 if peer_id is invalid.
363
 */
364
int gc_get_peer_ip_address_size(const GC_Chat *_Nonnull chat, GC_Peer_Id peer_id);
365
366
/** @brief Copies peer_id's IP address to `ip_addr`.
367
 *
368
 * If the peer is forcing TCP connections this will be a placeholder value indicating
369
 * that their real IP address is unknown to us.
370
 *
371
 * If `peer_id` designates ourself, it will write either our own IP address or a
372
 * placeholder value, depending on whether or not we're forcing TCP connections.
373
 *
374
 * `ip_addr` should have room for at least IP_NTOA_LEN bytes.
375
 *
376
 * Returns 0 on success.
377
 * Returns -1 if peer_id is invalid or doesn't correspond to a valid peer connection.
378
 * Returns -2 if `ip_addr` is null.
379
 */
380
int gc_get_peer_ip_address(const GC_Chat *_Nonnull chat, GC_Peer_Id peer_id, uint8_t *_Nullable ip_addr);
381
/** @brief Gets the connection status for peer associated with `peer_id`.
382
 *
383
 * If `peer_id` designates ourself, the return value indicates whether we're capable
384
 * of making UDP connections with other peers, or are limited to TCP connections.
385
 *
386
 * Returns 2 if we have a direct (UDP) connection with a peer.
387
 * Returns 1 if we have an indirect (TCP) connection with a peer.
388
 * Returns 0 if peer_id is invalid.
389
 *
390
 * Note: Return values must correspond to Tox_Connection enum in API.
391
 */
392
unsigned int gc_get_peer_connection_status(const GC_Chat *_Nonnull chat, GC_Peer_Id peer_id);
393
394
/** @brief Sets the caller's status to `status`.
395
 *
396
 * Returns 0 on success.
397
 * Returns -1 if the group_number is invalid.
398
 * Returns -2 if the packet failed to send.
399
 */
400
int gc_set_self_status(const Messenger *_Nonnull m, int group_number, Group_Peer_Status status);
401
402
/** @brief Returns the status of peer designated by `peer_id`.
403
 * Returns UINT8_MAX on failure.
404
 *
405
 * The status returned is equal to the last status received through the status_change
406
 * callback.
407
 */
408
uint8_t gc_get_status(const GC_Chat *_Nonnull chat, GC_Peer_Id peer_id);
409
410
/** @brief Returns the group role of peer designated by `peer_id`.
411
 * Returns UINT8_MAX on failure.
412
 *
413
 * The role returned is equal to the last role received through the moderation callback.
414
 */
415
uint8_t gc_get_role(const GC_Chat *_Nonnull chat, GC_Peer_Id peer_id);
416
417
/** @brief Sets the role of peer_id. role must be one of: GR_MODERATOR, GR_USER, GR_OBSERVER
418
 *
419
 * Returns 0 on success.
420
 * Returns -1 if the group_number is invalid.
421
 * Returns -2 if the peer_id is invalid.
422
 * Returns -3 if caller does not have sufficient permissions for the action.
423
 * Returns -4 if the role assignment is invalid.
424
 * Returns -5 if the role failed to be set.
425
 * Returns -6 if the caller attempted to kick himself.
426
 */
427
int gc_set_peer_role(const Messenger *_Nonnull m, int group_number, GC_Peer_Id peer_id, Group_Role new_role);
428
429
/** @brief Sets the group password and distributes the new shared state to the group.
430
 *
431
 * This function requires that the shared state be re-signed and will only work for the group founder.
432
 *
433
 * If `password` is null or `password_length` is 0 the password will be unset for the group.
434
 *
435
 * Returns 0 on success.
436
 * Returns -1 if the caller does not have sufficient permissions for the action.
437
 * Returns -2 if the password is too long.
438
 * Returns -3 if the packet failed to send.
439
 * Returns -4 if malloc failed.
440
 */
441
int gc_founder_set_password(GC_Chat *_Nonnull chat, const uint8_t *_Nullable password, uint16_t password_length);
442
/** @brief Sets the topic lock and distributes the new shared state to the group.
443
 *
444
 * When the topic lock is enabled, only the group founder and moderators may set the topic.
445
 * When disabled, all peers except those with the observer role may set the topic.
446
 *
447
 * This function requires that the shared state be re-signed and will only work for the group founder.
448
 *
449
 * Returns 0 on success.
450
 * Returns -1 if group_number is invalid.
451
 * Returns -2 if `topic_lock` is an invalid type.
452
 * Returns -3 if the caller does not have sufficient permissions for this action.
453
 * Returns -4 if the group is disconnected.
454
 * Returns -5 if the topic lock could not be set.
455
 * Returns -6 if the packet failed to send.
456
 */
457
int gc_founder_set_topic_lock(const Messenger *_Nonnull m, int group_number, Group_Topic_Lock new_lock_state);
458
459
/** @brief Sets the group privacy state and distributes the new shared state to the group.
460
 *
461
 * This function requires that the shared state be re-signed and will only work for the group founder.
462
 *
463
 * If an attempt is made to set the privacy state to the same state that the group is already
464
 * in, the function call will be successful and no action will be taken.
465
 *
466
 * Returns 0 on success.
467
 * Returns -1 if group_number is invalid.
468
 * Returns -2 if the caller does not have sufficient permissions for this action.
469
 * Returns -3 if the group is disconnected.
470
 * Returns -4 if the privacy state could not be set.
471
 * Returns -5 if the packet failed to send.
472
 */
473
int gc_founder_set_privacy_state(const Messenger *_Nonnull m, int group_number, Group_Privacy_State new_privacy_state);
474
475
/** @brief Sets the group voice state and distributes the new shared state to the group.
476
 *
477
 * This function requires that the shared state be re-signed and will only work for the group founder.
478
 *
479
 * If an attempt is made to set the voice state to the same state that the group is already
480
 * in, the function call will be successful and no action will be taken.
481
 *
482
 * Returns 0 on success.
483
 * Returns -1 if group_number is invalid.
484
 * Returns -2 if the caller does not have sufficient permissions for this action.
485
 * Returns -3 if the group is disconnected.
486
 * Returns -4 if the voice state could not be set.
487
 * Returns -5 if the packet failed to send.
488
 */
489
int gc_founder_set_voice_state(const Messenger *_Nonnull m, int group_number, Group_Voice_State new_voice_state);
490
491
/** @brief Sets the peer limit to maxpeers and distributes the new shared state to the group.
492
 *
493
 * This function requires that the shared state be re-signed and will only work for the group founder.
494
 *
495
 * Returns 0 on success.
496
 * Returns -1 if the caller does not have sufficient permissions for this action.
497
 * Returns -2 if the peer limit could not be set.
498
 * Returns -3 if the packet failed to send.
499
 */
500
int gc_founder_set_max_peers(GC_Chat *_Nonnull chat, uint16_t max_peers);
501
502
/** @brief Removes peer designated by `peer_id` from peer list and sends a broadcast instructing
503
 * all other peers to remove the peer from their peerlist as well.
504
 *
505
 * This function will not trigger the peer_exit callback for the caller.
506
 *
507
 * Returns 0 on success.
508
 * Returns -1 if the group_number is invalid.
509
 * Returns -2 if the peer_id is invalid.
510
 * Returns -3 if the caller does not have sufficient permissions for this action.
511
 * Returns -4 if the action failed.
512
 * Returns -5 if the packet failed to send.
513
 * Returns -6 if the caller attempted to kick himself.
514
 */
515
int gc_kick_peer(const Messenger *_Nonnull m, int group_number, GC_Peer_Id peer_id);
516
517
/** @brief Copies the chat_id to dest. If dest is null this function has no effect.
518
 *
519
 * `dest` should have room for at least CHAT_ID_SIZE bytes.
520
 */
521
void gc_get_chat_id(const GC_Chat *_Nonnull chat, uint8_t *_Nullable dest);
522
/** Group callbacks */
523
void gc_callback_message(const Messenger *_Nonnull m, gc_message_cb *_Nullable function);
524
void gc_callback_private_message(const Messenger *_Nonnull m, gc_private_message_cb *_Nullable function);
525
void gc_callback_custom_packet(const Messenger *_Nonnull m, gc_custom_packet_cb *_Nullable function);
526
void gc_callback_custom_private_packet(const Messenger *_Nonnull m,
527
                                       gc_custom_private_packet_cb *_Nullable function);
528
void gc_callback_moderation(const Messenger *_Nonnull m, gc_moderation_cb *_Nullable function);
529
void gc_callback_nick_change(const Messenger *_Nonnull m, gc_nick_change_cb *_Nullable function);
530
void gc_callback_status_change(const Messenger *_Nonnull m, gc_status_change_cb *_Nullable function);
531
void gc_callback_topic_change(const Messenger *_Nonnull m, gc_topic_change_cb *_Nullable function);
532
void gc_callback_peer_limit(const Messenger *_Nonnull m, gc_peer_limit_cb *_Nullable function);
533
void gc_callback_privacy_state(const Messenger *_Nonnull m, gc_privacy_state_cb *_Nullable function);
534
void gc_callback_topic_lock(const Messenger *_Nonnull m, gc_topic_lock_cb *_Nullable function);
535
void gc_callback_password(const Messenger *_Nonnull m, gc_password_cb *_Nullable function);
536
void gc_callback_peer_join(const Messenger *_Nonnull m, gc_peer_join_cb *_Nullable function);
537
void gc_callback_peer_exit(const Messenger *_Nonnull m, gc_peer_exit_cb *_Nullable function);
538
void gc_callback_self_join(const Messenger *_Nonnull m, gc_self_join_cb *_Nullable function);
539
void gc_callback_rejected(const Messenger *_Nonnull m, gc_rejected_cb *_Nullable function);
540
void gc_callback_voice_state(const Messenger *_Nonnull m, gc_voice_state_cb *_Nullable function);
541
542
/** @brief The main loop. Should be called with every Messenger iteration. */
543
void do_gc(GC_Session *_Nonnull c, void *_Nullable userdata);
544
/**
545
 * Make sure that DHT is initialized before calling this.
546
 * Returns a NULL pointer on failure.
547
 */
548
GC_Session *_Nullable new_dht_groupchats(Messenger *_Nonnull m);
549
/** @brief Cleans up groupchat structures and calls `gc_group_exit()` for every group chat */
550
void kill_dht_groupchats(GC_Session *_Nullable c);
551
/** @brief Loads a previously saved group and attempts to join it.
552
 *
553
 * `bu` is the packed group info.
554
 *
555
 * Returns group_number on success.
556
 * Returns -1 on failure.
557
 */
558
int gc_group_load(GC_Session *_Nonnull c, Bin_Unpack *_Nonnull bu);
559
560
/**
561
 * @brief Saves info from `chat` to `bp` in binary format.
562
 */
563
void gc_group_save(const GC_Chat *_Nonnull chat, Bin_Pack *_Nonnull bp);
564
565
/** @brief Creates a new group and adds it to the group sessions group array.
566
 *
567
 * The caller of this function has founder role privileges.
568
 *
569
 * The client should initiate its peer list with self info after calling this function, as
570
 * the peer_join callback will not be triggered.
571
 *
572
 * Return -1 if the nick or group name is too long.
573
 * Return -2 if the nick or group name is empty.
574
 * Return -3 if the the group object fails to initialize.
575
 * Return -4 if the group state fails to initialize.
576
 * Return -5 if the Messenger friend connection fails to initialize.
577
 */
578
int gc_group_add(GC_Session *_Nonnull c, Group_Privacy_State privacy_state, const uint8_t *_Nonnull group_name, uint16_t group_name_length, const uint8_t *_Nonnull nick, size_t nick_length);
579
580
/** @brief Joins a group designated by `chat_id`.
581
 *
582
 * This function creates a new GC_Chat object, adds it to the chats array, and sends a DHT
583
 * announcement to find peers in the group associated with `chat_id`. Once a peer has been
584
 * found a join attempt will be initiated.
585
 *
586
 * If the group is not password protected password should be set to NULL and password_length should be 0.
587
 *
588
 * Return group_number on success.
589
 * Return -1 if the group object fails to initialize.
590
 * Return -2 if chat_id is NULL.
591
 * Return -3 if nick is too long.
592
 * Return -4 if nick is empty or nick length is zero.
593
 * Return -5 if there is an error setting the group password.
594
 * Return -6 if the Messenger friend connection fails to initialize.
595
 */
596
int gc_group_join(GC_Session *_Nonnull c, const uint8_t *_Nonnull chat_id, const uint8_t *_Nonnull nick, size_t nick_length, const uint8_t *_Nullable passwd,
597
                  uint16_t passwd_len);
598
/** @brief Disconnects from all peers in a group but saves the group state for later use.
599
 *
600
 * Return true on sucess.
601
 * Return false if the group handler object or chat object is null.
602
 */
603
bool gc_disconnect_from_group(const GC_Session *_Nonnull c, GC_Chat *_Nonnull chat);
604
605
/** @brief Disconnects from all peers in a group and attempts to reconnect.
606
 *
607
 * All self state and credentials are retained.
608
 *
609
 * Returns 0 on success.
610
 * Returns -1 if the group handler object or chat object is null.
611
 * Returns -2 if the Messenger friend connection fails to initialize.
612
 */
613
int gc_rejoin_group(GC_Session *_Nonnull c, GC_Chat *_Nonnull chat, const uint8_t *_Nullable passwd, uint16_t passwd_len);
614
/** @brief Joins a group using the invite data received in a friend's group invite.
615
 *
616
 * The invite is only valid while the inviter is present in the group.
617
 *
618
 * Return group_number on success.
619
 * Return -1 if the invite data is malformed.
620
 * Return -2 if the group object fails to initialize.
621
 * Return -3 if nick is too long.
622
 * Return -4 if nick is empty or nick length is zero.
623
 * Return -5 if there is an error setting the password.
624
 * Return -6 if friend doesn't exist.
625
 * Return -7 if sending packet failed.
626
 */
627
int gc_accept_invite(GC_Session *_Nonnull c, int32_t friend_number, const uint8_t *_Nonnull data, uint16_t length, const uint8_t *_Nonnull nick,
628
                     size_t nick_length, const uint8_t *_Nullable passwd, uint16_t passwd_len);
629
typedef bool gc_send_group_invite_packet_cb(const Messenger *_Nonnull m, uint32_t friendnumber, const uint8_t *_Nonnull packet,
630
        uint16_t length);
631
632
/** @brief Invites friend designated by `friendnumber` to chat.
633
 * Packet includes: Type, chat_id, TCP node or packed IP_Port.
634
 *
635
 * Return 0 on success.
636
 * Return -1 if friendnumber does not exist.
637
 * Return -2 on failure to create the invite data.
638
 * Return -3 if the packet fails to send.
639
 */
640
int gc_invite_friend(const GC_Session *_Nonnull c, GC_Chat *_Nonnull chat, int32_t friend_number, gc_send_group_invite_packet_cb *_Nonnull callback);
641
642
/** @brief Leaves a group and sends an exit broadcast packet with an optional parting message.
643
 *
644
 * All group state is permanently lost, including keys and roles.
645
 *
646
 * Return 0 on success.
647
 * Return -1 if the parting message is too long.
648
 * Return -2 if the parting message failed to send.
649
 */
650
int gc_group_exit(GC_Session *_Nonnull c, GC_Chat *_Nonnull chat, const uint8_t *_Nullable message, uint16_t length);
651
/** @brief Returns true if `chat` is a valid group chat.
652
 *
653
 * A valid group chat constitutes an initialized chat instance with a non-zero shared state version.
654
 * The shared state version will be non-zero either if a peer has created the group, or if
655
 * they have ever successfully connected to the group.
656
 */
657
bool gc_group_is_valid(const GC_Chat *_Nonnull chat);
658
659
/** @brief Returns the number of active groups in `c`. */
660
uint32_t gc_count_groups(const GC_Session *_Nonnull c);
661
662
/** @brief Copy a list of valid group numbers into the array group_list. */
663
uint32_t gc_get_group_list(const GC_Session *_Nonnull c, uint32_t *_Nonnull group_list);
664
665
/** @brief Returns true if peer_number exists */
666
bool gc_peer_number_is_valid(const GC_Chat *_Nonnull chat, int peer_number);
667
668
/** @brief Return group_number's GC_Chat pointer on success
669
 * Return NULL on failure
670
 */
671
GC_Chat *_Nullable gc_get_group(const GC_Session *_Nonnull c, int group_number);
672
673
/** @brief Sends a lossy message acknowledgement to peer associated with `gconn`.
674
 *
675
 * If `type` is GR_ACK_RECV we send a read-receipt for read_id's packet. If `type` is GR_ACK_REQ
676
 * we send a request for the respective id's packet.
677
 *
678
 * Requests are limited to one per second per peer.
679
 *
680
 * @retval true on success.
681
 */
682
bool gc_send_message_ack(const GC_Chat *_Nonnull chat, GC_Connection *_Nonnull gconn, uint64_t message_id, Group_Message_Ack_Type type);
683
684
/** @brief Helper function for `handle_gc_lossless_packet()`.
685
 *
686
 * Note: This function may modify the peer list and change peer numbers.
687
 *
688
 * @retval true if packet is successfully handled.
689
 */
690
bool handle_gc_lossless_helper(const GC_Session *_Nonnull c, GC_Chat *_Nonnull chat, uint32_t peer_number, const uint8_t *_Nullable data,
691
                               uint16_t length, uint8_t packet_type, void *_Nullable userdata);
692
/** @brief Handles an invite accept packet.
693
 *
694
 * @retval true on success.
695
 */
696
bool handle_gc_invite_accepted_packet(const GC_Session *_Nonnull c, int friend_number, const uint8_t *_Nonnull data, uint16_t length);
697
698
/** @brief Return true if `chat_id` is not present in our group sessions array.
699
 *
700
 * `length` must be at least CHAT_ID_SIZE bytes in length.
701
 */
702
bool group_not_added(const GC_Session *_Nonnull c, const uint8_t *_Nonnull chat_id, uint32_t length);
703
704
/** @brief Handles an invite confirmed packet.
705
 *
706
 * Return 0 on success.
707
 * Return -1 if length is invalid.
708
 * Return -2 if data contains invalid chat_id.
709
 * Return -3 if data contains invalid peer info.
710
 * Return -4 if `friend_number` does not designate a valid friend.
711
 * Return -5 if data contains invalid connection info.
712
 */
713
int handle_gc_invite_confirmed_packet(const GC_Session *_Nonnull c, int friend_number, const uint8_t *_Nonnull data, uint16_t length);
714
715
/** @brief Returns the group designated by `public_key`.
716
 * Returns null if group does not exist.
717
 */
718
GC_Chat *_Nullable gc_get_group_by_public_key(const GC_Session *_Nonnull c, const uint8_t *_Nonnull public_key);
719
720
/** @brief Attempts to add peers from `announces` to our peer list and initiate an invite request.
721
 *
722
 * Returns the number of peers added on success.
723
 * Returns -1 on failure.
724
 */
725
int gc_add_peers_from_announces(GC_Chat *_Nonnull chat, const GC_Announce *_Nonnull announces, uint8_t gc_announces_count);
726
727
#ifdef __cplusplus
728
} /* extern "C" */
729
#endif
730
731
#endif /* C_TOXCORE_TOXCORE_GROUP_CHATS_H */