Coverage Report

Created: 2024-01-26 01:52

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