/work/toxcore/Messenger.h
Line | Count | Source |
1 | | /* SPDX-License-Identifier: GPL-3.0-or-later |
2 | | * Copyright © 2016-2018 The TokTok team. |
3 | | * Copyright © 2013 Tox project. |
4 | | */ |
5 | | |
6 | | /** |
7 | | * An implementation of a simple text chat only messenger on the tox network |
8 | | * core. |
9 | | */ |
10 | | #ifndef C_TOXCORE_TOXCORE_MESSENGER_H |
11 | | #define C_TOXCORE_TOXCORE_MESSENGER_H |
12 | | |
13 | | #include "TCP_server.h" |
14 | | #include "announce.h" |
15 | | #include "forwarding.h" |
16 | | #include "friend_connection.h" |
17 | | #include "friend_requests.h" |
18 | | #include "group_announce.h" |
19 | | #include "group_common.h" |
20 | | #include "logger.h" |
21 | | #include "net_crypto.h" |
22 | | #include "state.h" |
23 | | |
24 | 15.8k | #define MAX_NAME_LENGTH 128 |
25 | | /* TODO(irungentoo): this must depend on other variable. */ |
26 | 3.95k | #define MAX_STATUSMESSAGE_LENGTH 1007 |
27 | | /* Used for TCP relays in Messenger struct (may need to be `% 2 == 0`)*/ |
28 | 4.35k | #define NUM_SAVED_TCP_RELAYS 8 |
29 | | /* This cannot be bigger than 256 */ |
30 | 45.0M | #define MAX_CONCURRENT_FILE_PIPES 256 |
31 | | |
32 | | |
33 | 483 | #define FRIEND_ADDRESS_SIZE (CRYPTO_PUBLIC_KEY_SIZE + sizeof(uint32_t) + sizeof(uint16_t)) |
34 | | |
35 | | typedef enum Message_Type { |
36 | | MESSAGE_NORMAL, |
37 | | MESSAGE_ACTION, |
38 | | } Message_Type; |
39 | | |
40 | | // TODO(Jfreegman, Iphy): Remove this before merge |
41 | | #ifndef MESSENGER_DEFINED |
42 | | #define MESSENGER_DEFINED |
43 | | typedef struct Messenger Messenger; |
44 | | #endif /* MESSENGER_DEFINED */ |
45 | | |
46 | | // Returns the size of the data |
47 | | typedef uint32_t m_state_size_cb(const Messenger *m); |
48 | | |
49 | | // Returns the new pointer to data |
50 | | typedef uint8_t *m_state_save_cb(const Messenger *m, uint8_t *data); |
51 | | |
52 | | // Returns if there were any erros during loading |
53 | | typedef State_Load_Status m_state_load_cb(Messenger *m, const uint8_t *data, uint32_t length); |
54 | | |
55 | | typedef struct Messenger_State_Plugin { |
56 | | State_Type type; |
57 | | m_state_size_cb *size; |
58 | | m_state_save_cb *save; |
59 | | m_state_load_cb *load; |
60 | | } Messenger_State_Plugin; |
61 | | |
62 | | typedef struct Messenger_Options { |
63 | | bool ipv6enabled; |
64 | | bool udp_disabled; |
65 | | TCP_Proxy_Info proxy_info; |
66 | | uint16_t port_range[2]; |
67 | | uint16_t tcp_server_port; |
68 | | |
69 | | bool hole_punching_enabled; |
70 | | bool local_discovery_enabled; |
71 | | bool dht_announcements_enabled; |
72 | | |
73 | | logger_cb *log_callback; |
74 | | void *log_context; |
75 | | void *log_user_data; |
76 | | |
77 | | Messenger_State_Plugin *state_plugins; |
78 | | uint8_t state_plugins_length; |
79 | | } Messenger_Options; |
80 | | |
81 | | |
82 | | struct Receipts { |
83 | | uint32_t packet_num; |
84 | | uint32_t msg_id; |
85 | | struct Receipts *next; |
86 | | }; |
87 | | |
88 | | /** Status definitions. */ |
89 | | typedef enum Friend_Status { |
90 | | NOFRIEND, |
91 | | FRIEND_ADDED, |
92 | | FRIEND_REQUESTED, |
93 | | FRIEND_CONFIRMED, |
94 | | FRIEND_ONLINE, |
95 | | } Friend_Status; |
96 | | |
97 | | /** @brief Errors for m_addfriend |
98 | | * |
99 | | * FAERR - Friend Add Error |
100 | | */ |
101 | | typedef enum Friend_Add_Error { |
102 | | FAERR_TOOLONG = -1, |
103 | | FAERR_NOMESSAGE = -2, |
104 | | FAERR_OWNKEY = -3, |
105 | | FAERR_ALREADYSENT = -4, |
106 | | FAERR_BADCHECKSUM = -6, |
107 | | FAERR_SETNEWNOSPAM = -7, |
108 | | FAERR_NOMEM = -8, |
109 | | } Friend_Add_Error; |
110 | | |
111 | | |
112 | | /** Default start timeout in seconds between friend requests. */ |
113 | 176 | #define FRIENDREQUEST_TIMEOUT 5 |
114 | | |
115 | | typedef enum Connection_Status { |
116 | | CONNECTION_NONE, |
117 | | CONNECTION_TCP, |
118 | | CONNECTION_UDP, |
119 | | } Connection_Status; |
120 | | |
121 | | /** |
122 | | * Represents userstatuses someone can have. |
123 | | */ |
124 | | typedef enum Userstatus { |
125 | | USERSTATUS_NONE, |
126 | | USERSTATUS_AWAY, |
127 | | USERSTATUS_BUSY, |
128 | | USERSTATUS_INVALID, |
129 | | } Userstatus; |
130 | | |
131 | 24 | #define FILE_ID_LENGTH 32 |
132 | | |
133 | | struct File_Transfers { |
134 | | uint64_t size; |
135 | | uint64_t transferred; |
136 | | uint8_t status; /* 0 == no transfer, 1 = not accepted, 3 = transferring, 4 = broken, 5 = finished */ |
137 | | uint8_t paused; /* 0: not paused, 1 = paused by us, 2 = paused by other, 3 = paused by both. */ |
138 | | uint32_t last_packet_number; /* number of the last packet sent. */ |
139 | | uint64_t requested; /* total data requested by the request chunk callback */ |
140 | | uint8_t id[FILE_ID_LENGTH]; |
141 | | }; |
142 | | typedef enum Filestatus { |
143 | | FILESTATUS_NONE, |
144 | | FILESTATUS_NOT_ACCEPTED, |
145 | | FILESTATUS_TRANSFERRING, |
146 | | // FILESTATUS_BROKEN, |
147 | | FILESTATUS_FINISHED, |
148 | | } Filestatus; |
149 | | |
150 | | typedef enum File_Pause { |
151 | | FILE_PAUSE_NOT, |
152 | | FILE_PAUSE_US, |
153 | | FILE_PAUSE_OTHER, |
154 | | FILE_PAUSE_BOTH, |
155 | | } File_Pause; |
156 | | |
157 | | typedef enum Filecontrol { |
158 | | FILECONTROL_ACCEPT, |
159 | | FILECONTROL_PAUSE, |
160 | | FILECONTROL_KILL, |
161 | | FILECONTROL_SEEK, |
162 | | } Filecontrol; |
163 | | |
164 | | typedef enum Filekind { |
165 | | FILEKIND_DATA, |
166 | | FILEKIND_AVATAR, |
167 | | } Filekind; |
168 | | |
169 | | |
170 | | typedef void m_self_connection_status_cb(Messenger *m, Onion_Connection_Status connection_status, void *user_data); |
171 | | typedef void m_friend_status_cb(Messenger *m, uint32_t friend_number, unsigned int status, void *user_data); |
172 | | typedef void m_friend_connection_status_cb(Messenger *m, uint32_t friend_number, unsigned int connection_status, |
173 | | void *user_data); |
174 | | typedef void m_friend_message_cb(Messenger *m, uint32_t friend_number, unsigned int message_type, |
175 | | const uint8_t *message, size_t length, void *user_data); |
176 | | typedef void m_file_recv_control_cb(Messenger *m, uint32_t friend_number, uint32_t file_number, unsigned int control, |
177 | | void *user_data); |
178 | | typedef void m_friend_request_cb(Messenger *m, const uint8_t *public_key, const uint8_t *message, size_t length, |
179 | | void *user_data); |
180 | | typedef void m_friend_name_cb(Messenger *m, uint32_t friend_number, const uint8_t *name, size_t length, |
181 | | void *user_data); |
182 | | typedef void m_friend_status_message_cb(Messenger *m, uint32_t friend_number, const uint8_t *message, size_t length, |
183 | | void *user_data); |
184 | | typedef void m_friend_typing_cb(Messenger *m, uint32_t friend_number, bool is_typing, void *user_data); |
185 | | typedef void m_friend_read_receipt_cb(Messenger *m, uint32_t friend_number, uint32_t message_id, void *user_data); |
186 | | typedef void m_file_recv_cb(Messenger *m, uint32_t friend_number, uint32_t file_number, uint32_t kind, |
187 | | uint64_t file_size, const uint8_t *filename, size_t filename_length, void *user_data); |
188 | | typedef void m_file_chunk_request_cb(Messenger *m, uint32_t friend_number, uint32_t file_number, uint64_t position, |
189 | | size_t length, void *user_data); |
190 | | typedef void m_file_recv_chunk_cb(Messenger *m, uint32_t friend_number, uint32_t file_number, uint64_t position, |
191 | | const uint8_t *data, size_t length, void *user_data); |
192 | | typedef void m_friend_lossy_packet_cb(Messenger *m, uint32_t friend_number, uint8_t packet_id, const uint8_t *data, |
193 | | size_t length, void *user_data); |
194 | | typedef void m_friend_lossless_packet_cb(Messenger *m, uint32_t friend_number, uint8_t packet_id, const uint8_t *data, |
195 | | size_t length, void *user_data); |
196 | | typedef void m_friend_connectionstatuschange_internal_cb(Messenger *m, uint32_t friend_number, |
197 | | uint8_t connection_status, void *user_data); |
198 | | typedef void m_conference_invite_cb(Messenger *m, uint32_t friend_number, const uint8_t *cookie, uint16_t length, |
199 | | void *user_data); |
200 | | typedef void m_group_invite_cb(const Messenger *m, uint32_t friend_number, const uint8_t *invite_data, size_t length, |
201 | | const uint8_t *group_name, size_t group_name_length, void *user_data); |
202 | | typedef void m_msi_packet_cb(Messenger *m, uint32_t friend_number, const uint8_t *data, uint16_t length, |
203 | | void *user_data); |
204 | | typedef int m_lossy_rtp_packet_cb(Messenger *m, uint32_t friend_number, const uint8_t *data, uint16_t length, void *object); |
205 | | |
206 | | typedef struct RTP_Packet_Handler { |
207 | | m_lossy_rtp_packet_cb *function; |
208 | | void *object; |
209 | | } RTP_Packet_Handler; |
210 | | |
211 | | typedef struct Friend { |
212 | | uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE]; |
213 | | int friendcon_id; |
214 | | |
215 | | uint64_t friendrequest_lastsent; // Time at which the last friend request was sent. |
216 | | uint32_t friendrequest_timeout; // The timeout between successful friendrequest sending attempts. |
217 | | uint8_t status; // 0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. |
218 | | uint8_t info[MAX_FRIEND_REQUEST_DATA_SIZE]; // the data that is sent during the friend requests we do. |
219 | | uint8_t name[MAX_NAME_LENGTH]; |
220 | | uint16_t name_length; |
221 | | bool name_sent; // false if we didn't send our name to this friend, true if we have. |
222 | | uint8_t statusmessage[MAX_STATUSMESSAGE_LENGTH]; |
223 | | uint16_t statusmessage_length; |
224 | | bool statusmessage_sent; |
225 | | Userstatus userstatus; |
226 | | bool userstatus_sent; |
227 | | bool user_istyping; |
228 | | bool user_istyping_sent; |
229 | | bool is_typing; |
230 | | uint16_t info_size; // Length of the info. |
231 | | uint32_t message_id; // a semi-unique id used in read receipts. |
232 | | uint32_t friendrequest_nospam; // The nospam number used in the friend request. |
233 | | uint64_t last_seen_time; |
234 | | Connection_Status last_connection_udp_tcp; |
235 | | struct File_Transfers file_sending[MAX_CONCURRENT_FILE_PIPES]; |
236 | | uint32_t num_sending_files; |
237 | | struct File_Transfers file_receiving[MAX_CONCURRENT_FILE_PIPES]; |
238 | | |
239 | | RTP_Packet_Handler lossy_rtp_packethandlers[PACKET_ID_RANGE_LOSSY_AV_SIZE]; |
240 | | |
241 | | struct Receipts *receipts_start; |
242 | | struct Receipts *receipts_end; |
243 | | } Friend; |
244 | | |
245 | | struct Messenger { |
246 | | Logger *log; |
247 | | Mono_Time *mono_time; |
248 | | const Memory *mem; |
249 | | const Random *rng; |
250 | | const Network *ns; |
251 | | |
252 | | Networking_Core *net; |
253 | | Net_Crypto *net_crypto; |
254 | | DHT *dht; |
255 | | |
256 | | Forwarding *forwarding; |
257 | | Announcements *announce; |
258 | | |
259 | | Onion *onion; |
260 | | Onion_Announce *onion_a; |
261 | | Onion_Client *onion_c; |
262 | | |
263 | | Friend_Connections *fr_c; |
264 | | |
265 | | TCP_Server *tcp_server; |
266 | | Friend_Requests *fr; |
267 | | uint8_t name[MAX_NAME_LENGTH]; |
268 | | uint16_t name_length; |
269 | | |
270 | | uint8_t statusmessage[MAX_STATUSMESSAGE_LENGTH]; |
271 | | uint16_t statusmessage_length; |
272 | | |
273 | | Userstatus userstatus; |
274 | | |
275 | | Friend *friendlist; |
276 | | uint32_t numfriends; |
277 | | |
278 | | uint64_t lastdump; |
279 | | uint8_t is_receiving_file; |
280 | | |
281 | | GC_Session *group_handler; |
282 | | GC_Announces_List *group_announce; |
283 | | |
284 | | bool has_added_relays; // If the first connection has occurred in do_messenger |
285 | | |
286 | | uint16_t num_loaded_relays; |
287 | | Node_format loaded_relays[NUM_SAVED_TCP_RELAYS]; // Relays loaded from config |
288 | | |
289 | | m_friend_request_cb *friend_request; |
290 | | m_friend_message_cb *friend_message; |
291 | | m_friend_name_cb *friend_namechange; |
292 | | m_friend_status_message_cb *friend_statusmessagechange; |
293 | | m_friend_status_cb *friend_userstatuschange; |
294 | | m_friend_typing_cb *friend_typingchange; |
295 | | m_friend_read_receipt_cb *read_receipt; |
296 | | m_friend_connection_status_cb *friend_connectionstatuschange; |
297 | | m_friend_connectionstatuschange_internal_cb *friend_connectionstatuschange_internal; |
298 | | void *friend_connectionstatuschange_internal_userdata; |
299 | | |
300 | | struct Group_Chats *conferences_object; |
301 | | m_conference_invite_cb *conference_invite; |
302 | | |
303 | | m_group_invite_cb *group_invite; |
304 | | |
305 | | m_file_recv_cb *file_sendrequest; |
306 | | m_file_recv_control_cb *file_filecontrol; |
307 | | m_file_recv_chunk_cb *file_filedata; |
308 | | m_file_chunk_request_cb *file_reqchunk; |
309 | | |
310 | | m_msi_packet_cb *msi_packet; |
311 | | void *msi_packet_userdata; |
312 | | |
313 | | m_friend_lossy_packet_cb *lossy_packethandler; |
314 | | m_friend_lossless_packet_cb *lossless_packethandler; |
315 | | |
316 | | m_self_connection_status_cb *core_connection_change; |
317 | | Onion_Connection_Status last_connection_status; |
318 | | |
319 | | Messenger_Options options; |
320 | | }; |
321 | | |
322 | | /** |
323 | | * Determines if the friendnumber passed is valid in the Messenger object. |
324 | | * |
325 | | * @param friendnumber The index in the friend list. |
326 | | */ |
327 | | non_null() |
328 | | bool friend_is_valid(const Messenger *m, int32_t friendnumber); |
329 | | |
330 | | /** |
331 | | * Format: `[real_pk (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]` |
332 | | * |
333 | | * @param[out] address FRIEND_ADDRESS_SIZE byte address to give to others. |
334 | | */ |
335 | | non_null() |
336 | | void getaddress(const Messenger *m, uint8_t *address); |
337 | | |
338 | | /** |
339 | | * Add a friend. |
340 | | * |
341 | | * Set the data that will be sent along with friend request. |
342 | | * |
343 | | * @param address is the address of the friend (returned by getaddress of the friend |
344 | | * you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. |
345 | | * TODO(irungentoo): add checksum. |
346 | | * @param data is the data. |
347 | | * @param length is the length. |
348 | | * |
349 | | * @return the friend number if success. |
350 | | * @retval FA_TOOLONG if message length is too long. |
351 | | * @retval FAERR_NOMESSAGE if no message (message length must be >= 1 byte). |
352 | | * @retval FAERR_OWNKEY if user's own key. |
353 | | * @retval FAERR_ALREADYSENT if friend request already sent or already a friend. |
354 | | * @retval FAERR_BADCHECKSUM if bad checksum in address. |
355 | | * @retval FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different. |
356 | | * (the nospam for that friend was set to the new one). |
357 | | * @retval FAERR_NOMEM if increasing the friend list size fails. |
358 | | */ |
359 | | non_null() |
360 | | int32_t m_addfriend(Messenger *m, const uint8_t *address, const uint8_t *data, uint16_t length); |
361 | | |
362 | | |
363 | | /** @brief Add a friend without sending a friendrequest. |
364 | | * @return the friend number if success. |
365 | | * @retval -3 if user's own key. |
366 | | * @retval -4 if friend request already sent or already a friend. |
367 | | * @retval -6 if bad checksum in address. |
368 | | * @retval -8 if increasing the friend list size fails. |
369 | | */ |
370 | | non_null() |
371 | | int32_t m_addfriend_norequest(Messenger *m, const uint8_t *real_pk); |
372 | | |
373 | | /** @brief Initializes the friend connection and onion connection for a groupchat. |
374 | | * |
375 | | * @retval true on success. |
376 | | */ |
377 | | non_null() |
378 | | bool m_create_group_connection(Messenger *m, GC_Chat *chat); |
379 | | |
380 | | /* |
381 | | * Kills the friend connection for a groupchat. |
382 | | */ |
383 | | non_null() |
384 | | void m_kill_group_connection(Messenger *m, const GC_Chat *chat); |
385 | | |
386 | | /** @return the friend number associated to that public key. |
387 | | * @retval -1 if no such friend. |
388 | | */ |
389 | | non_null() |
390 | | int32_t getfriend_id(const Messenger *m, const uint8_t *real_pk); |
391 | | |
392 | | /** @brief Copies the public key associated to that friend id into real_pk buffer. |
393 | | * |
394 | | * Make sure that real_pk is of size CRYPTO_PUBLIC_KEY_SIZE. |
395 | | * |
396 | | * @retval 0 if success. |
397 | | * @retval -1 if failure. |
398 | | */ |
399 | | non_null() |
400 | | int get_real_pk(const Messenger *m, int32_t friendnumber, uint8_t *real_pk); |
401 | | |
402 | | /** @return friend connection id on success. |
403 | | * @retval -1 if failure. |
404 | | */ |
405 | | non_null() |
406 | | int getfriendcon_id(const Messenger *m, int32_t friendnumber); |
407 | | |
408 | | /** @brief Remove a friend. |
409 | | * |
410 | | * @retval 0 if success. |
411 | | * @retval -1 if failure. |
412 | | */ |
413 | | non_null() |
414 | | int m_delfriend(Messenger *m, int32_t friendnumber); |
415 | | |
416 | | /** @brief Checks friend's connection status. |
417 | | * |
418 | | * @retval CONNECTION_UDP (2) if friend is directly connected to us (Online UDP). |
419 | | * @retval CONNECTION_TCP (1) if friend is connected to us (Online TCP). |
420 | | * @retval CONNECTION_NONE (0) if friend is not connected to us (Offline). |
421 | | * @retval -1 on failure. |
422 | | */ |
423 | | non_null() |
424 | | int m_get_friend_connectionstatus(const Messenger *m, int32_t friendnumber); |
425 | | |
426 | | /** |
427 | | * Checks if there exists a friend with given friendnumber. |
428 | | * |
429 | | * @param friendnumber The index in the friend list. |
430 | | * |
431 | | * @retval true if friend exists. |
432 | | * @retval false if friend doesn't exist. |
433 | | */ |
434 | | non_null() |
435 | | bool m_friend_exists(const Messenger *m, int32_t friendnumber); |
436 | | |
437 | | /** @brief Send a message of type to an online friend. |
438 | | * |
439 | | * @retval -1 if friend not valid. |
440 | | * @retval -2 if too large. |
441 | | * @retval -3 if friend not online. |
442 | | * @retval -4 if send failed (because queue is full). |
443 | | * @retval -5 if bad type. |
444 | | * @retval 0 if success. |
445 | | * |
446 | | * The value in message_id will be passed to your read_receipt callback when the other receives the message. |
447 | | */ |
448 | | non_null(1, 4) nullable(6) |
449 | | int m_send_message_generic(Messenger *m, int32_t friendnumber, uint8_t type, const uint8_t *message, uint32_t length, |
450 | | uint32_t *message_id); |
451 | | |
452 | | |
453 | | /** @brief Set the name and name_length of a friend. |
454 | | * |
455 | | * name must be a string of maximum MAX_NAME_LENGTH length. |
456 | | * length must be at least 1 byte. |
457 | | * length is the length of name with the NULL terminator. |
458 | | * |
459 | | * @retval 0 if success. |
460 | | * @retval -1 if failure. |
461 | | */ |
462 | | non_null() |
463 | | int setfriendname(Messenger *m, int32_t friendnumber, const uint8_t *name, uint16_t length); |
464 | | |
465 | | /** @brief Set our nickname. |
466 | | * |
467 | | * name must be a string of maximum MAX_NAME_LENGTH length. |
468 | | * length must be at least 1 byte. |
469 | | * length is the length of name with the NULL terminator. |
470 | | * |
471 | | * @retval 0 if success. |
472 | | * @retval -1 if failure. |
473 | | */ |
474 | | non_null() |
475 | | int setname(Messenger *m, const uint8_t *name, uint16_t length); |
476 | | |
477 | | /** |
478 | | * @brief Get your nickname. |
479 | | * |
480 | | * m - The messenger context to use. |
481 | | * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. |
482 | | * |
483 | | * @return length of the name. |
484 | | * @retval 0 on error. |
485 | | */ |
486 | | non_null() |
487 | | uint16_t getself_name(const Messenger *m, uint8_t *name); |
488 | | |
489 | | /** @brief Get name of friendnumber and put it in name. |
490 | | * |
491 | | * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. |
492 | | * |
493 | | * @return length of name if success. |
494 | | * @retval -1 if failure. |
495 | | */ |
496 | | non_null() |
497 | | int getname(const Messenger *m, int32_t friendnumber, uint8_t *name); |
498 | | |
499 | | /** @return the length of name, including null on success. |
500 | | * @retval -1 on failure. |
501 | | */ |
502 | | non_null() int m_get_name_size(const Messenger *m, int32_t friendnumber); |
503 | | non_null() int m_get_self_name_size(const Messenger *m); |
504 | | |
505 | | /** @brief Set our user status. |
506 | | * You are responsible for freeing status after. |
507 | | * |
508 | | * @retval 0 on success. |
509 | | * @retval -1 on failure. |
510 | | */ |
511 | | non_null() int m_set_statusmessage(Messenger *m, const uint8_t *status, uint16_t length); |
512 | | non_null() int m_set_userstatus(Messenger *m, uint8_t status); |
513 | | |
514 | | /** |
515 | | * Guaranteed to be at most MAX_STATUSMESSAGE_LENGTH. |
516 | | * |
517 | | * @return the length of friendnumber's status message, including null on success. |
518 | | * @retval -1 on failure. |
519 | | */ |
520 | | non_null() int m_get_statusmessage_size(const Messenger *m, int32_t friendnumber); |
521 | | non_null() int m_get_self_statusmessage_size(const Messenger *m); |
522 | | |
523 | | /** @brief Copy friendnumber's status message into buf, truncating if size is over maxlen. |
524 | | * |
525 | | * Get the size you need to allocate from m_get_statusmessage_size. |
526 | | * The self variant will copy our own status message. |
527 | | * |
528 | | * @return the length of the copied data on success |
529 | | * @retval -1 on failure. |
530 | | */ |
531 | | non_null() int m_copy_statusmessage(const Messenger *m, int32_t friendnumber, uint8_t *buf, uint32_t maxlen); |
532 | | non_null() int m_copy_self_statusmessage(const Messenger *m, uint8_t *buf); |
533 | | |
534 | | /** @brief return one of Userstatus values. |
535 | | * |
536 | | * Values unknown to your application should be represented as USERSTATUS_NONE. |
537 | | * As above, the self variant will return our own Userstatus. |
538 | | * If friendnumber is invalid, this shall return USERSTATUS_INVALID. |
539 | | */ |
540 | | non_null() uint8_t m_get_userstatus(const Messenger *m, int32_t friendnumber); |
541 | | non_null() uint8_t m_get_self_userstatus(const Messenger *m); |
542 | | |
543 | | |
544 | | /** @brief returns timestamp of last time friendnumber was seen online or 0 if never seen. |
545 | | * if friendnumber is invalid this function will return UINT64_MAX. |
546 | | */ |
547 | | non_null() uint64_t m_get_last_online(const Messenger *m, int32_t friendnumber); |
548 | | |
549 | | /** @brief Set our typing status for a friend. |
550 | | * You are responsible for turning it on or off. |
551 | | * |
552 | | * @retval 0 on success. |
553 | | * @retval -1 on failure. |
554 | | */ |
555 | | non_null() |
556 | | int m_set_usertyping(Messenger *m, int32_t friendnumber, bool is_typing); |
557 | | |
558 | | /** @brief Get the typing status of a friend. |
559 | | * |
560 | | * @retval -1 if friend number is invalid. |
561 | | * @retval 0 if friend is not typing. |
562 | | * @retval 1 if friend is typing. |
563 | | */ |
564 | | non_null() |
565 | | int m_get_istyping(const Messenger *m, int32_t friendnumber); |
566 | | |
567 | | /** Set the function that will be executed when a friend request is received. */ |
568 | | non_null(1) nullable(2) |
569 | | void m_callback_friendrequest(Messenger *m, m_friend_request_cb *function); |
570 | | |
571 | | /** Set the function that will be executed when a message from a friend is received. */ |
572 | | non_null() void m_callback_friendmessage(Messenger *m, m_friend_message_cb *function); |
573 | | |
574 | | /** @brief Set the callback for name changes. |
575 | | * You are not responsible for freeing newname. |
576 | | */ |
577 | | non_null() void m_callback_namechange(Messenger *m, m_friend_name_cb *function); |
578 | | |
579 | | /** @brief Set the callback for status message changes. |
580 | | * |
581 | | * You are not responsible for freeing newstatus |
582 | | */ |
583 | | non_null() void m_callback_statusmessage(Messenger *m, m_friend_status_message_cb *function); |
584 | | |
585 | | /** @brief Set the callback for status type changes. */ |
586 | | non_null() void m_callback_userstatus(Messenger *m, m_friend_status_cb *function); |
587 | | |
588 | | /** @brief Set the callback for typing changes. */ |
589 | | non_null() void m_callback_typingchange(Messenger *m, m_friend_typing_cb *function); |
590 | | |
591 | | /** @brief Set the callback for read receipts. |
592 | | * |
593 | | * If you are keeping a record of returns from m_sendmessage, |
594 | | * receipt might be one of those values, meaning the message |
595 | | * has been received on the other side. |
596 | | * Since core doesn't track ids for you, receipt may not correspond to any message. |
597 | | * In that case, you should discard it. |
598 | | */ |
599 | | non_null() void m_callback_read_receipt(Messenger *m, m_friend_read_receipt_cb *function); |
600 | | |
601 | | /** @brief Set the callback for connection status changes. |
602 | | * |
603 | | * Status: |
604 | | * - 0: friend went offline after being previously online. |
605 | | * - 1: friend went online. |
606 | | * |
607 | | * Note that this callback is not called when adding friends, thus the |
608 | | * "after being previously online" part. |
609 | | * It's assumed that when adding friends, their connection status is offline. |
610 | | */ |
611 | | non_null() void m_callback_connectionstatus(Messenger *m, m_friend_connection_status_cb *function); |
612 | | |
613 | | /** Same as previous but for internal A/V core usage only */ |
614 | | non_null() void m_callback_connectionstatus_internal_av( |
615 | | Messenger *m, m_friend_connectionstatuschange_internal_cb *function, void *userdata); |
616 | | |
617 | | |
618 | | /** @brief Set the callback for typing changes. */ |
619 | | non_null() void m_callback_core_connection(Messenger *m, m_self_connection_status_cb *function); |
620 | | |
621 | | /*** CONFERENCES */ |
622 | | |
623 | | /** @brief Set the callback for conference invites. */ |
624 | | non_null(1) nullable(2) |
625 | | void m_callback_conference_invite(Messenger *m, m_conference_invite_cb *function); |
626 | | |
627 | | /* Set the callback for group invites. |
628 | | */ |
629 | | non_null(1) nullable(2) |
630 | | void m_callback_group_invite(Messenger *m, m_group_invite_cb *function); |
631 | | |
632 | | |
633 | | /** @brief Send a conference invite packet. |
634 | | * |
635 | | * return true on success |
636 | | * return false on failure |
637 | | */ |
638 | | non_null() |
639 | | bool send_conference_invite_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint16_t length); |
640 | | |
641 | | /* Send a group invite packet. |
642 | | * |
643 | | * WARNING: Return-value semantics are different than for |
644 | | * send_conference_invite_packet(). |
645 | | * |
646 | | * return true on success |
647 | | */ |
648 | | non_null() |
649 | | bool send_group_invite_packet(const Messenger *m, uint32_t friendnumber, const uint8_t *packet, uint16_t length); |
650 | | |
651 | | |
652 | | /*** FILE SENDING */ |
653 | | |
654 | | |
655 | | /** @brief Set the callback for file send requests. */ |
656 | | non_null() void callback_file_sendrequest(Messenger *m, m_file_recv_cb *function); |
657 | | |
658 | | |
659 | | /** @brief Set the callback for file control requests. */ |
660 | | non_null() void callback_file_control(Messenger *m, m_file_recv_control_cb *function); |
661 | | |
662 | | /** @brief Set the callback for file data. */ |
663 | | non_null() void callback_file_data(Messenger *m, m_file_recv_chunk_cb *function); |
664 | | |
665 | | /** @brief Set the callback for file request chunk. */ |
666 | | non_null() void callback_file_reqchunk(Messenger *m, m_file_chunk_request_cb *function); |
667 | | |
668 | | |
669 | | /** @brief Copy the file transfer file id to file_id |
670 | | * |
671 | | * @retval 0 on success. |
672 | | * @retval -1 if friend not valid. |
673 | | * @retval -2 if filenumber not valid |
674 | | */ |
675 | | non_null() |
676 | | int file_get_id(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uint8_t *file_id); |
677 | | |
678 | | /** @brief Send a file send request. |
679 | | * |
680 | | * Maximum filename length is 255 bytes. |
681 | | * |
682 | | * @return file number on success |
683 | | * @retval -1 if friend not found. |
684 | | * @retval -2 if filename length invalid. |
685 | | * @retval -3 if no more file sending slots left. |
686 | | * @retval -4 if could not send packet (friend offline). |
687 | | */ |
688 | | non_null() |
689 | | long int new_filesender(const Messenger *m, int32_t friendnumber, uint32_t file_type, uint64_t filesize, |
690 | | const uint8_t *file_id, const uint8_t *filename, uint16_t filename_length); |
691 | | |
692 | | /** @brief Send a file control request. |
693 | | * |
694 | | * @retval 0 on success |
695 | | * @retval -1 if friend not valid. |
696 | | * @retval -2 if friend not online. |
697 | | * @retval -3 if file number invalid. |
698 | | * @retval -4 if file control is bad. |
699 | | * @retval -5 if file already paused. |
700 | | * @retval -6 if resume file failed because it was only paused by the other. |
701 | | * @retval -7 if resume file failed because it wasn't paused. |
702 | | * @retval -8 if packet failed to send. |
703 | | */ |
704 | | non_null() |
705 | | int file_control(const Messenger *m, int32_t friendnumber, uint32_t filenumber, unsigned int control); |
706 | | |
707 | | /** @brief Send a seek file control request. |
708 | | * |
709 | | * @retval 0 on success |
710 | | * @retval -1 if friend not valid. |
711 | | * @retval -2 if friend not online. |
712 | | * @retval -3 if file number invalid. |
713 | | * @retval -4 if not receiving file. |
714 | | * @retval -5 if file status wrong. |
715 | | * @retval -6 if position bad. |
716 | | * @retval -8 if packet failed to send. |
717 | | */ |
718 | | non_null() |
719 | | int file_seek(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uint64_t position); |
720 | | |
721 | | /** @brief Send file data. |
722 | | * |
723 | | * @retval 0 on success |
724 | | * @retval -1 if friend not valid. |
725 | | * @retval -2 if friend not online. |
726 | | * @retval -3 if filenumber invalid. |
727 | | * @retval -4 if file transfer not transferring. |
728 | | * @retval -5 if bad data size. |
729 | | * @retval -6 if packet queue full. |
730 | | * @retval -7 if wrong position. |
731 | | */ |
732 | | non_null(1) nullable(5) |
733 | | int send_file_data(const Messenger *m, int32_t friendnumber, uint32_t filenumber, uint64_t position, |
734 | | const uint8_t *data, uint16_t length); |
735 | | |
736 | | /*** A/V related */ |
737 | | |
738 | | /** @brief Set the callback for msi packets. */ |
739 | | non_null(1) nullable(2, 3) |
740 | | void m_callback_msi_packet(Messenger *m, m_msi_packet_cb *function, void *userdata); |
741 | | |
742 | | /** @brief Send an msi packet. |
743 | | * |
744 | | * @retval true on success |
745 | | * @retval false on failure |
746 | | */ |
747 | | non_null() |
748 | | bool m_msi_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint16_t length); |
749 | | |
750 | | /** @brief Set handlers for lossy rtp packets. |
751 | | * |
752 | | * @retval -1 on failure. |
753 | | * @retval 0 on success. |
754 | | */ |
755 | | non_null(1) nullable(4, 5) |
756 | | int m_callback_rtp_packet(Messenger *m, int32_t friendnumber, uint8_t byte, |
757 | | m_lossy_rtp_packet_cb *function, void *object); |
758 | | |
759 | | /*** CUSTOM PACKETS */ |
760 | | |
761 | | /** @brief Set handlers for custom lossy packets. */ |
762 | | non_null() void custom_lossy_packet_registerhandler(Messenger *m, m_friend_lossy_packet_cb *lossy_packethandler); |
763 | | |
764 | | /** @brief High level function to send custom lossy packets. |
765 | | * |
766 | | * TODO(oxij): this name is confusing, because this function sends both av and custom lossy packets. |
767 | | * Meanwhile, m_handle_lossy_packet routes custom packets to custom_lossy_packet_registerhandler |
768 | | * as you would expect from its name. |
769 | | * |
770 | | * I.e. custom_lossy_packet_registerhandler's "custom lossy packet" and this "custom lossy packet" |
771 | | * are not the same set of packets. |
772 | | * |
773 | | * @retval -1 if friend invalid. |
774 | | * @retval -2 if length wrong. |
775 | | * @retval -3 if first byte invalid. |
776 | | * @retval -4 if friend offline. |
777 | | * @retval -5 if packet failed to send because of other error. |
778 | | * @retval 0 on success. |
779 | | */ |
780 | | non_null() |
781 | | int m_send_custom_lossy_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length); |
782 | | |
783 | | |
784 | | /** @brief Set handlers for custom lossless packets. */ |
785 | | non_null() |
786 | | void custom_lossless_packet_registerhandler(Messenger *m, m_friend_lossless_packet_cb *lossless_packethandler); |
787 | | |
788 | | /** @brief High level function to send custom lossless packets. |
789 | | * |
790 | | * @retval -1 if friend invalid. |
791 | | * @retval -2 if length wrong. |
792 | | * @retval -3 if first byte invalid. |
793 | | * @retval -4 if friend offline. |
794 | | * @retval -5 if packet failed to send because of other error. |
795 | | * @retval 0 on success. |
796 | | */ |
797 | | non_null() |
798 | | int send_custom_lossless_packet(const Messenger *m, int32_t friendnumber, const uint8_t *data, uint32_t length); |
799 | | |
800 | | /*** Messenger constructor/destructor/operations. */ |
801 | | |
802 | | typedef enum Messenger_Error { |
803 | | MESSENGER_ERROR_NONE, |
804 | | MESSENGER_ERROR_PORT, |
805 | | MESSENGER_ERROR_TCP_SERVER, |
806 | | MESSENGER_ERROR_OTHER, |
807 | | } Messenger_Error; |
808 | | |
809 | | /** @brief Run this at startup. |
810 | | * |
811 | | * @return allocated instance of Messenger on success. |
812 | | * @retval 0 if there are problems. |
813 | | * |
814 | | * if error is not NULL it will be set to one of the values in the enum above. |
815 | | */ |
816 | | non_null() |
817 | | Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *rng, const Network *ns, |
818 | | Messenger_Options *options, Messenger_Error *error); |
819 | | |
820 | | /** @brief Run this before closing shop. |
821 | | * |
822 | | * Free all datastructures. |
823 | | */ |
824 | | nullable(1) |
825 | | void kill_messenger(Messenger *m); |
826 | | |
827 | | /** @brief The main loop that needs to be run at least 20 times per second. */ |
828 | | non_null(1) nullable(2) |
829 | | void do_messenger(Messenger *m, void *userdata); |
830 | | |
831 | | /** |
832 | | * @brief Return the time in milliseconds before `do_messenger()` should be called again |
833 | | * for optimal performance. |
834 | | * |
835 | | * @return time (in ms) before the next `do_messenger()` needs to be run on success. |
836 | | */ |
837 | | non_null() |
838 | | uint32_t messenger_run_interval(const Messenger *m); |
839 | | |
840 | | /* SAVING AND LOADING FUNCTIONS: */ |
841 | | |
842 | | /** @brief Registers a state plugin for saving, loading, and getting the size of a section of the save. |
843 | | * |
844 | | * @retval true on success |
845 | | * @retval false on error |
846 | | */ |
847 | | non_null() |
848 | | bool m_register_state_plugin(Messenger *m, State_Type type, |
849 | | m_state_size_cb *size_callback, |
850 | | m_state_load_cb *load_callback, |
851 | | m_state_save_cb *save_callback); |
852 | | |
853 | | /** return size of the messenger data (for saving). */ |
854 | | non_null() |
855 | | uint32_t messenger_size(const Messenger *m); |
856 | | |
857 | | /** Save the messenger in data (must be allocated memory of size at least `Messenger_size()`) */ |
858 | | non_null() |
859 | | uint8_t *messenger_save(const Messenger *m, uint8_t *data); |
860 | | |
861 | | /** @brief Load a state section. |
862 | | * |
863 | | * @param data Data to load. |
864 | | * @param length Length of data. |
865 | | * @param type Type of section (`STATE_TYPE_*`). |
866 | | * @param status Result of loading section is stored here if the section is handled. |
867 | | * @return true iff section handled. |
868 | | */ |
869 | | non_null() |
870 | | bool messenger_load_state_section(Messenger *m, const uint8_t *data, uint32_t length, uint16_t type, |
871 | | State_Load_Status *status); |
872 | | |
873 | | /** @brief Return the number of friends in the instance m. |
874 | | * |
875 | | * You should use this to determine how much memory to allocate |
876 | | * for copy_friendlist. |
877 | | */ |
878 | | non_null() |
879 | | uint32_t count_friendlist(const Messenger *m); |
880 | | |
881 | | /** @brief Copy a list of valid friend IDs into the array out_list. |
882 | | * If out_list is NULL, returns 0. |
883 | | * Otherwise, returns the number of elements copied. |
884 | | * If the array was too small, the contents |
885 | | * of out_list will be truncated to list_size. |
886 | | */ |
887 | | non_null() |
888 | | uint32_t copy_friendlist(const Messenger *m, uint32_t *out_list, uint32_t list_size); |
889 | | |
890 | | non_null() |
891 | | bool m_is_receiving_file(Messenger *m); |
892 | | |
893 | | #endif /* C_TOXCORE_TOXCORE_MESSENGER_H */ |