summaryrefslogtreecommitdiff
path: root/toxcore/Messenger.h
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/Messenger.h')
-rw-r--r--toxcore/Messenger.h280
1 files changed, 156 insertions, 124 deletions
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h
index e808529f..b588fb4b 100644
--- a/toxcore/Messenger.h
+++ b/toxcore/Messenger.h
@@ -49,7 +49,7 @@ extern "C" {
49#define PACKET_ID_ACTION 63 49#define PACKET_ID_ACTION 63
50 50
51 51
52/* status definitions */ 52/* Status definitions. */
53enum { 53enum {
54 NOFRIEND, 54 NOFRIEND,
55 FRIEND_ADDED, 55 FRIEND_ADDED,
@@ -58,8 +58,9 @@ enum {
58 FRIEND_ONLINE, 58 FRIEND_ONLINE,
59}; 59};
60 60
61/* errors for m_addfriend 61/* Errors for m_addfriend
62 * FAERR - Friend Add Error */ 62 * FAERR - Friend Add Error
63 */
63enum { 64enum {
64 FAERR_TOOLONG = -1, 65 FAERR_TOOLONG = -1,
65 FAERR_NOMESSAGE = -2, 66 FAERR_NOMESSAGE = -2,
@@ -71,20 +72,22 @@ enum {
71 FAERR_NOMEM = -8 72 FAERR_NOMEM = -8
72}; 73};
73 74
74/* don't assume MAX_STATUSMESSAGE_LENGTH will stay at 128, it may be increased 75/* Don't assume MAX_STATUSMESSAGE_LENGTH will stay at 128, it may be increased
75 to an absurdly large number later */ 76 * to an absurdly large number later.
77 */
76 78
77/* Default start timeout in seconds between friend requests */ 79/* Default start timeout in seconds between friend requests. */
78#define FRIENDREQUEST_TIMEOUT 5; 80#define FRIENDREQUEST_TIMEOUT 5;
79 81
80/* interval between the sending of ping packets.*/ 82/* Interval between the sending of ping packets. */
81#define FRIEND_PING_INTERVAL 5 83#define FRIEND_PING_INTERVAL 5
82 84
83/* If no packets are recieved from friend in this time interval, kill the connection.*/ 85/* If no packets are recieved from friend in this time interval, kill the connection. */
84#define FRIEND_CONNECTION_TIMEOUT (FRIEND_PING_INTERVAL * 2) 86#define FRIEND_CONNECTION_TIMEOUT (FRIEND_PING_INTERVAL * 2)
85 87
86/* USERSTATUS 88/* USERSTATUS -
87 * Represents userstatuses someone can have. */ 89 * Represents userstatuses someone can have.
90 */
88 91
89typedef enum { 92typedef enum {
90 USERSTATUS_NONE, 93 USERSTATUS_NONE,
@@ -97,21 +100,21 @@ USERSTATUS;
97typedef struct { 100typedef struct {
98 uint8_t client_id[CLIENT_ID_SIZE]; 101 uint8_t client_id[CLIENT_ID_SIZE];
99 int crypt_connection_id; 102 int crypt_connection_id;
100 uint64_t friendrequest_lastsent; /* time at which the last friend request was sent. */ 103 uint64_t friendrequest_lastsent; // Time at which the last friend request was sent.
101 uint32_t friendrequest_timeout; /* The timeout between successful friendrequest sending attempts */ 104 uint32_t friendrequest_timeout; // The timeout between successful friendrequest sending attempts.
102 uint8_t status; /* 0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. */ 105 uint8_t status; // 0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online.
103 uint8_t info[MAX_DATA_SIZE]; /* the data that is sent during the friend requests we do */ 106 uint8_t info[MAX_DATA_SIZE]; // the data that is sent during the friend requests we do.
104 uint8_t name[MAX_NAME_LENGTH]; 107 uint8_t name[MAX_NAME_LENGTH];
105 uint8_t name_sent; /* 0 if we didn't send our name to this friend 1 if we have. */ 108 uint8_t name_sent; // 0 if we didn't send our name to this friend 1 if we have.
106 uint8_t *statusmessage; 109 uint8_t *statusmessage;
107 uint16_t statusmessage_length; 110 uint16_t statusmessage_length;
108 uint8_t statusmessage_sent; 111 uint8_t statusmessage_sent;
109 USERSTATUS userstatus; 112 USERSTATUS userstatus;
110 uint8_t userstatus_sent; 113 uint8_t userstatus_sent;
111 uint16_t info_size; /* length of the info */ 114 uint16_t info_size; // Length of the info.
112 uint32_t message_id; /* a semi-unique id used in read receipts */ 115 uint32_t message_id; // a semi-unique id used in read receipts.
113 uint8_t receives_read_receipts; /* shall we send read receipts to this person? */ 116 uint8_t receives_read_receipts; // shall we send read receipts to this person?
114 uint32_t friendrequest_nospam; /*The nospam number used in the friend request*/ 117 uint32_t friendrequest_nospam; // The nospam number used in the friend request.
115 uint64_t ping_lastrecv; 118 uint64_t ping_lastrecv;
116 uint64_t ping_lastsent; 119 uint64_t ping_lastsent;
117} Friend; 120} Friend;
@@ -156,192 +159,221 @@ typedef struct Messenger {
156} Messenger; 159} Messenger;
157 160
158/* 161/*
159 * returns a FRIEND_ADDRESS_SIZE byte address to give to others. 162 * return FRIEND_ADDRESS_SIZE byte address to give to others.
160 * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] 163 * Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
161 * 164 *
162 */ 165 */
163void getaddress(Messenger *m, uint8_t *address); 166void getaddress(Messenger *m, uint8_t *address);
164 167
165/* 168/*
166 * add a friend 169 * Add a friend.
167 * set the data that will be sent along with friend request 170 * Set the data that will be sent along with friend request.
168 * address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum. 171 * address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum.
169 * data is the data and length is the length 172 * data is the data and length is the length.
170 * returns the friend number if success 173 * returns the friend number if success.
171 * return -1 if message length is too long 174 * return -1 if message length is too long.
172 * return -2 if no message (message length must be >= 1 byte) 175 * return -2 if no message (message length must be >= 1 byte).
173 * return -3 if user's own key 176 * return -3 if user's own key.
174 * return -4 if friend request already sent or already a friend 177 * return -4 if friend request already sent or already a friend.
175 * return -5 for unknown error 178 * return -5 for unknown error.
176 * return -6 if bad checksum in address 179 * return -6 if bad checksum in address.
177 * return -7 if the friend was already there but the nospam was different 180 * return -7 if the friend was already there but the nospam was different.
178 * (the nospam for that friend was set to the new one) 181 * (the nospam for that friend was set to the new one).
179 * return -8 if increasing the friend list size fails 182 * return -8 if increasing the friend list size fails.
180 */ 183 */
181int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length); 184int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length);
182 185
183 186
184/* add a friend without sending a friendrequest. 187/* Add a friend without sending a friendrequest.
185 returns the friend number if success 188 * return the friend number if success.
186 return -1 if failure. */ 189 * return -1 if failure.
190 */
187int m_addfriend_norequest(Messenger *m, uint8_t *client_id); 191int m_addfriend_norequest(Messenger *m, uint8_t *client_id);
188 192
189/* return the friend id associated to that client id. 193/* return the friend id associated to that client id.
190 return -1 if no such friend */ 194 * return -1 if no such friend.
195 */
191int getfriend_id(Messenger *m, uint8_t *client_id); 196int getfriend_id(Messenger *m, uint8_t *client_id);
192 197
193/* copies the public key associated to that friend id into client_id buffer. 198/* Copies the public key associated to that friend id into client_id buffer.
194 make sure that client_id is of size CLIENT_ID_SIZE. 199 * Make sure that client_id is of size CLIENT_ID_SIZE.
195 return 0 if success 200 * return 0 if success
196 return -1 if failure */ 201 * return -1 if failure
202 */
197int getclient_id(Messenger *m, int friend_id, uint8_t *client_id); 203int getclient_id(Messenger *m, int friend_id, uint8_t *client_id);
198 204
199/* remove a friend */ 205/* Remove a friend. */
200int m_delfriend(Messenger *m, int friendnumber); 206int m_delfriend(Messenger *m, int friendnumber);
201 207
202/* return 4 if friend is online 208/* return 4 if friend is online.
203 return 3 if friend is confirmed 209 * return 3 if friend is confirmed.
204 return 2 if the friend request was sent 210 * return 2 if the friend request was sent.
205 return 1 if the friend was added 211 * return 1 if the friend was added.
206 return 0 if there is no friend with that number */ 212 * return 0 if there is no friend with that number.
213 */
207int m_friendstatus(Messenger *m, int friendnumber); 214int m_friendstatus(Messenger *m, int friendnumber);
208 215
209/* send a text chat message to an online friend 216/* Send a text chat message to an online friend.
210 returns the message id if packet was successfully put into the send queue 217 * return the message id if packet was successfully put into the send queue.
211 return 0 if it was not 218 * return 0 if it was not.
212 you will want to retain the return value, it will be passed to your read receipt callback 219 *
213 if one is received. 220 * You will want to retain the return value, it will be passed to your read receipt callback
214 m_sendmessage_withid will send a message with the id of your choosing, 221 * if one is received.
215 however we can generate an id for you by calling plain m_sendmessage. */ 222 * m_sendmessage_withid will send a message with the id of your choosing,
223 * however we can generate an id for you by calling plain m_sendmessage.
224 */
216uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length); 225uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length);
217uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length); 226uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length);
218 227
219/* send an action to an online friend 228/* Send an action to an online friend.
220 returns 1 if packet was successfully put into the send queue 229 * return 1 if packet was successfully put into the send queue.
221 return 0 if it was not */ 230 * return 0 if it was not.
231 */
222int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length); 232int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length);
223 233
224/* Set our nickname 234/* Set our nickname.
225 name must be a string of maximum MAX_NAME_LENGTH length. 235 * name must be a string of maximum MAX_NAME_LENGTH length.
226 length must be at least 1 byte 236 * length must be at least 1 byte.
227 length is the length of name with the NULL terminator 237 * length is the length of name with the NULL terminator.
228 return 0 if success 238 * return 0 if success.
229 return -1 if failure */ 239 * return -1 if failure.
240 */
230int setname(Messenger *m, uint8_t *name, uint16_t length); 241int setname(Messenger *m, uint8_t *name, uint16_t length);
231 242
232/* 243/*
233 Get your nickname. 244 * Get your nickname.
234 m The messanger context to use. 245 * m - The messanger context to use.
235 name Pointer to a string for the name. 246 * name - Pointer to a string for the name.
236 nlen The length of the string buffer. 247 * nlen - The length of the string buffer.
237 returns Return the length of the name, 0 on error. 248 * return length of the name.
238*/ 249 * return 0 on error.
250 */
239uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen); 251uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen);
240 252
241/* get name of friendnumber 253/* Get name of friendnumber and put it in name.
242 put it in name 254 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
243 name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. 255 * return 0 if success.
244 return 0 if success 256 * return -1 if failure.
245 return -1 if failure */ 257 */
246int getname(Messenger *m, int friendnumber, uint8_t *name); 258int getname(Messenger *m, int friendnumber, uint8_t *name);
247 259
248/* set our user status 260/* Set our user status.
249 you are responsible for freeing status after 261 * You are responsible for freeing status after.
250 returns 0 on success, -1 on failure */ 262 * returns 0 on success.
263 * returns -1 on failure.
264 */
251int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length); 265int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length);
252int m_set_userstatus(Messenger *m, USERSTATUS status); 266int m_set_userstatus(Messenger *m, USERSTATUS status);
253 267
254/* return the length of friendnumber's status message, 268/* return the length of friendnumber's status message,
255 including null 269 * including null.
256 pass it into malloc */ 270 * Pass it into malloc.
271 */
257int m_get_statusmessage_size(Messenger *m, int friendnumber); 272int m_get_statusmessage_size(Messenger *m, int friendnumber);
258 273
259/* copy friendnumber's status message into buf, truncating if size is over maxlen 274/* Copy friendnumber's status message into buf, truncating if size is over maxlen.
260 get the size you need to allocate from m_get_statusmessage_size 275 * Get the size you need to allocate from m_get_statusmessage_size.
261 The self variant will copy our own status message. */ 276 * The self variant will copy our own status message.
277 */
262int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen); 278int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen);
263int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen); 279int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen);
264 280
265/* Return one of USERSTATUS values. 281/* return one of USERSTATUS values.
266 * Values unknown to your application should be represented as USERSTATUS_NONE. 282 * Values unknown to your application should be represented as USERSTATUS_NONE.
267 * As above, the self variant will return our own USERSTATUS. 283 * As above, the self variant will return our own USERSTATUS.
268 * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ 284 * If friendnumber is invalid, this shall return USERSTATUS_INVALID.
285 */
269USERSTATUS m_get_userstatus(Messenger *m, int friendnumber); 286USERSTATUS m_get_userstatus(Messenger *m, int friendnumber);
270USERSTATUS m_get_self_userstatus(Messenger *m); 287USERSTATUS m_get_self_userstatus(Messenger *m);
271 288
272/* Sets whether we send read receipts for friendnumber. 289/* Sets whether we send read receipts for friendnumber.
273 * This function is not lazy, and it will fail if yesno is not (0 or 1).*/ 290 * This function is not lazy, and it will fail if yesno is not (0 or 1).
291 */
274void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno); 292void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno);
275 293
276/* set the function that will be executed when a friend request is received. 294/* Set the function that will be executed when a friend request is received.
277 function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ 295 * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length)
296 */
278void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata); 297void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata);
279 298
280/* set the function that will be executed when a message from a friend is received. 299/* Set the function that will be executed when a message from a friend is received.
281 function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ 300 * Function format is: function(int friendnumber, uint8_t * message, uint32_t length)
301 */
282void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), 302void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *),
283 void *userdata); 303 void *userdata);
284 304
285/* set the function that will be executed when an action from a friend is received. 305/* Set the function that will be executed when an action from a friend is received.
286 function format is: function(int friendnumber, uint8_t * action, uint32_t length) */ 306 * function format is: function(int friendnumber, uint8_t * action, uint32_t length)
307 */
287void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), void *userdata); 308void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), void *userdata);
288 309
289/* set the callback for name changes 310/* Set the callback for name changes.
290 function(int friendnumber, uint8_t *newname, uint16_t length) 311 * Function(int friendnumber, uint8_t *newname, uint16_t length)
291 you are not responsible for freeing newname */ 312 * You are not responsible for freeing newname.
313 */
292void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), 314void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *),
293 void *userdata); 315 void *userdata);
294 316
295/* set the callback for status message changes 317/* Set the callback for status message changes.
296 function(int friendnumber, uint8_t *newstatus, uint16_t length) 318 * function(int friendnumber, uint8_t *newstatus, uint16_t length)
297 you are not responsible for freeing newstatus */ 319 *
320 * You are not responsible for freeing newstatus
321 */
298void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), 322void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *),
299 void *userdata); 323 void *userdata);
300 324
301/* set the callback for status type changes 325/* Set the callback for status type changes.
302 function(int friendnumber, USERSTATUS kind) */ 326 * function(int friendnumber, USERSTATUS kind)
327 */
303void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void *), void *userdata); 328void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void *), void *userdata);
304 329
305/* set the callback for read receipts 330/* Set the callback for read receipts.
306 function(int friendnumber, uint32_t receipt) 331 * function(int friendnumber, uint32_t receipt)
307 if you are keeping a record of returns from m_sendmessage, 332 *
308 receipt might be one of those values, and that means the message 333 * If you are keeping a record of returns from m_sendmessage,
309 has been received on the other side. since core doesn't 334 * receipt might be one of those values, meaning the message
310 track ids for you, receipt may not correspond to any message 335 * has been received on the other side.
311 in that case, you should discard it. */ 336 * Since core doesn't track ids for you, receipt may not correspond to any message.
337 * In that case, you should discard it.
338 */
312void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void *), void *userdata); 339void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void *), void *userdata);
313 340
314/* set the callback for connection status changes 341/* Set the callback for connection status changes.
315 function(int friendnumber, uint8_t status) 342 * function(int friendnumber, uint8_t status)
316 status: 343 *
317 0 -- friend went offline after being previously online 344 * Status:
318 1 -- friend went online 345 * 0 -- friend went offline after being previously online.
319 note that this callback is not called when adding friends, thus the "after 346 * 1 -- friend went online.
320 being previously online" part. it's assumed that when adding friends, 347 *
321 their connection status is offline. */ 348 * Note that this callback is not called when adding friends, thus the "after
349 * being previously online" part.
350 * It's assumed that when adding friends, their connection status is offline.
351 */
322void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), void *userdata); 352void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), void *userdata);
323 353
324/* run this at startup 354/* Run this at startup.
325 * returns allocated instance of Messenger on success 355 * returns allocated instance of Messenger on success.
326 * returns 0 if there are problems */ 356 * returns 0 if there are problems.
357 */
327Messenger *initMessenger(void); 358Messenger *initMessenger(void);
328 359
329/* run this before closing shop 360/* Run this before closing shop
330 * free all datastructures */ 361 * Free all datastructures.
362 */
331void cleanupMessenger(Messenger *M); 363void cleanupMessenger(Messenger *M);
332 364
333/* the main loop that needs to be run at least 200 times per second */ 365/* The main loop that needs to be run at least 20 times per second. */
334void doMessenger(Messenger *m); 366void doMessenger(Messenger *m);
335 367
336/* SAVING AND LOADING FUNCTIONS: */ 368/* SAVING AND LOADING FUNCTIONS: */
337 369
338/* returns the size of the messenger data (for saving) */ 370/* return size of the messenger data (for saving). */
339uint32_t Messenger_size(Messenger *m); 371uint32_t Messenger_size(Messenger *m);
340 372
341/* save the messenger in data (must be allocated memory of size Messenger_size()) */ 373/* Save the messenger in data (must be allocated memory of size Messenger_size()) */
342void Messenger_save(Messenger *m, uint8_t *data); 374void Messenger_save(Messenger *m, uint8_t *data);
343 375
344/* load the messenger from data of size length */ 376/* Load the messenger from data of size length. */
345int Messenger_load(Messenger *m, uint8_t *data, uint32_t length); 377int Messenger_load(Messenger *m, uint8_t *data, uint32_t length);
346 378
347#ifdef __cplusplus 379#ifdef __cplusplus