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