summaryrefslogtreecommitdiff
path: root/toxcore/tox.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/tox.c')
-rw-r--r--toxcore/tox.c237
1 files changed, 131 insertions, 106 deletions
diff --git a/toxcore/tox.c b/toxcore/tox.c
index a97e52bc..494de478 100644
--- a/toxcore/tox.c
+++ b/toxcore/tox.c
@@ -24,7 +24,7 @@
24#include "Messenger.h" 24#include "Messenger.h"
25/* 25/*
26 * returns a FRIEND_ADDRESS_SIZE byte address to give to others. 26 * returns a FRIEND_ADDRESS_SIZE byte address to give to others.
27 * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] 27 * Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
28 * 28 *
29 */ 29 */
30void tox_getaddress(void *tox, uint8_t *address) 30void tox_getaddress(void *tox, uint8_t *address)
@@ -34,20 +34,20 @@ void tox_getaddress(void *tox, uint8_t *address)
34} 34}
35 35
36/* 36/*
37 * add a friend 37 * Add a friend.
38 * set the data that will be sent along with friend request 38 * Set the data that will be sent along with friend request.
39 * 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. 39 * 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.
40 * data is the data and length is the length 40 * data is the data and length is the length.
41 * returns the friend number if success 41 * returns the friend number if success.
42 * return FA_TOOLONG if message length is too long 42 * return FA_TOOLONG if message length is too long.
43 * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte) 43 * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte).
44 * return FAERR_OWNKEY if user's own key 44 * return FAERR_OWNKEY if user's own key.
45 * return FAERR_ALREADYSENT if friend request already sent or already a friend 45 * return FAERR_ALREADYSENT if friend request already sent or already a friend.
46 * return FAERR_UNKNOWN for unknown error 46 * return FAERR_UNKNOWN for unknown error.
47 * return FAERR_BADCHECKSUM if bad checksum in address 47 * return FAERR_BADCHECKSUM if bad checksum in address.
48 * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different 48 * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different.
49 * (the nospam for that friend was set to the new one) 49 * (the nospam for that friend was set to the new one).
50 * return FAERR_NOMEM if increasing the friend list size fails 50 * return FAERR_NOMEM if increasing the friend list size fails.
51 */ 51 */
52int tox_addfriend(void *tox, uint8_t *address, uint8_t *data, uint16_t length) 52int tox_addfriend(void *tox, uint8_t *address, uint8_t *data, uint16_t length)
53{ 53{
@@ -55,9 +55,10 @@ int tox_addfriend(void *tox, uint8_t *address, uint8_t *data, uint16_t length)
55 return m_addfriend(m, address, data, length); 55 return m_addfriend(m, address, data, length);
56} 56}
57 57
58/* add a friend without sending a friendrequest. 58/* Add a friend without sending a friendrequest.
59 returns the friend number if success 59 * returns the friend number if success.
60 return -1 if failure. */ 60 * return -1 if failure.
61 */
61int tox_addfriend_norequest(void *tox, uint8_t *client_id) 62int tox_addfriend_norequest(void *tox, uint8_t *client_id)
62{ 63{
63 Messenger *m = tox; 64 Messenger *m = tox;
@@ -65,48 +66,53 @@ int tox_addfriend_norequest(void *tox, uint8_t *client_id)
65} 66}
66 67
67/* return the friend id associated to that client id. 68/* return the friend id associated to that client id.
68 return -1 if no such friend */ 69 * return -1 if no such friend.
70 */
69int tox_getfriend_id(void *tox, uint8_t *client_id) 71int tox_getfriend_id(void *tox, uint8_t *client_id)
70{ 72{
71 Messenger *m = tox; 73 Messenger *m = tox;
72 return getfriend_id(m, client_id); 74 return getfriend_id(m, client_id);
73} 75}
74 76
75/* copies the public key associated to that friend id into client_id buffer. 77/* Copies the public key associated to that friend id into client_id buffer.
76 make sure that client_id is of size CLIENT_ID_SIZE. 78 * Make sure that client_id is of size CLIENT_ID_SIZE.
77 return 0 if success 79 * return 0 if success.
78 return -1 if failure */ 80 * return -1 if failure.
81 */
79int tox_getclient_id(void *tox, int friend_id, uint8_t *client_id) 82int tox_getclient_id(void *tox, int friend_id, uint8_t *client_id)
80{ 83{
81 Messenger *m = tox; 84 Messenger *m = tox;
82 return getclient_id(m, friend_id, client_id); 85 return getclient_id(m, friend_id, client_id);
83} 86}
84 87
85/* remove a friend */ 88/* Remove a friend. */
86int tox_delfriend(void *tox, int friendnumber) 89int tox_delfriend(void *tox, int friendnumber)
87{ 90{
88 Messenger *m = tox; 91 Messenger *m = tox;
89 return m_delfriend(m, friendnumber); 92 return m_delfriend(m, friendnumber);
90} 93}
91 94
92/* return 4 if friend is online 95/* return 4 if friend is online.
93 return 3 if friend is confirmed 96 * return 3 if friend is confirmed.
94 return 2 if the friend request was sent 97 * return 2 if the friend request was sent.
95 return 1 if the friend was added 98 * return 1 if the friend was added.
96 return 0 if there is no friend with that number */ 99 * return 0 if there is no friend with that number.
100 */
97int tox_friendstatus(void *tox, int friendnumber) 101int tox_friendstatus(void *tox, int friendnumber)
98{ 102{
99 Messenger *m = tox; 103 Messenger *m = tox;
100 return m_friendstatus(m, friendnumber); 104 return m_friendstatus(m, friendnumber);
101} 105}
102 106
103/* send a text chat message to an online friend 107/* Send a text chat message to an online friend.
104 returns the message id if packet was successfully put into the send queue 108 * returns the message id if packet was successfully put into the send queue.
105 return 0 if it was not 109 * return 0 if it was not.
106 you will want to retain the return value, it will be passed to your read receipt callback 110 *
107 if one is received. 111 * You will want to retain the return value, it will be passed to your read receipt callback
108 m_sendmessage_withid will send a message with the id of your choosing, 112 * if one is received.
109 however we can generate an id for you by calling plain m_sendmessage. */ 113 * m_sendmessage_withid will send a message with the id of your choosing,
114 * however we can generate an id for you by calling plain m_sendmessage.
115 */
110uint32_t tox_sendmessage(void *tox, int friendnumber, uint8_t *message, uint32_t length) 116uint32_t tox_sendmessage(void *tox, int friendnumber, uint8_t *message, uint32_t length)
111{ 117{
112 Messenger *m = tox; 118 Messenger *m = tox;
@@ -119,45 +125,47 @@ uint32_t tox_sendmessage_withid(void *tox, int friendnumber, uint32_t theid, uin
119 return m_sendmessage_withid(m, friendnumber, theid, message, length); 125 return m_sendmessage_withid(m, friendnumber, theid, message, length);
120} 126}
121 127
122/* send an action to an online friend 128/* Send an action to an online friend.
123 returns 1 if packet was successfully put into the send queue 129 * returns 1 if packet was successfully put into the send queue.
124 return 0 if it was not */ 130 * return 0 if it was not.
131 */
125int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length) 132int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length)
126{ 133{
127 Messenger *m = tox; 134 Messenger *m = tox;
128 return m_sendaction(m, friendnumber, action, length); 135 return m_sendaction(m, friendnumber, action, length);
129} 136}
130 137
131/* Set our nickname 138/* Set our nickname.
132 name must be a string of maximum MAX_NAME_LENGTH length. 139 * name must be a string of maximum MAX_NAME_LENGTH length.
133 length must be at least 1 byte 140 * length must be at least 1 byte.
134 length is the length of name with the NULL terminator 141 * length is the length of name with the NULL terminator.
135 return 0 if success 142 * return 0 if success.
136 return -1 if failure */ 143 * return -1 if failure.
144 */
137int tox_setname(void *tox, uint8_t *name, uint16_t length) 145int tox_setname(void *tox, uint8_t *name, uint16_t length)
138{ 146{
139 Messenger *m = tox; 147 Messenger *m = tox;
140 return setname(m, name, length); 148 return setname(m, name, length);
141} 149}
142 150
143/* 151/* Get your nickname.
144 Get your nickname. 152 * m - The messanger context to use.
145 m The messanger context to use. 153 * name - Pointer to a string for the name.
146 name Pointer to a string for the name. 154 * nlen - The length of the string buffer.
147 nlen The length of the string buffer. 155 * return length of the name.
148 returns Return the length of the name, 0 on error. 156 * return 0 on error.
149*/ 157 */
150uint16_t tox_getselfname(void *tox, uint8_t *name, uint16_t nlen) 158uint16_t tox_getselfname(void *tox, uint8_t *name, uint16_t nlen)
151{ 159{
152 Messenger *m = tox; 160 Messenger *m = tox;
153 return getself_name(m, name, nlen); 161 return getself_name(m, name, nlen);
154} 162}
155 163
156/* get name of friendnumber 164/* Get name of friendnumber and put it in name.
157 put it in name 165 * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
158 name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. 166 * return 0 if success.
159 return 0 if success 167 * return -1 if failure.
160 return -1 if failure */ 168 */
161int tox_getname(void *tox, int friendnumber, uint8_t *name) 169int tox_getname(void *tox, int friendnumber, uint8_t *name)
162{ 170{
163 Messenger *m = tox; 171 Messenger *m = tox;
@@ -179,18 +187,19 @@ int tox_set_userstatus(void *tox, USERSTATUS status)
179 return m_set_userstatus(m, status); 187 return m_set_userstatus(m, status);
180} 188}
181 189
182/* return the length of friendnumber's status message, 190/* return the length of friendnumber's status message, including null.
183 including null 191 * Pass it into malloc.
184 pass it into malloc */ 192 */
185int tox_get_statusmessage_size(void *tox, int friendnumber) 193int tox_get_statusmessage_size(void *tox, int friendnumber)
186{ 194{
187 Messenger *m = tox; 195 Messenger *m = tox;
188 return m_get_statusmessage_size(m, friendnumber); 196 return m_get_statusmessage_size(m, friendnumber);
189} 197}
190 198
191/* copy friendnumber's status message into buf, truncating if size is over maxlen 199/* Copy friendnumber's status message into buf, truncating if size is over maxlen.
192 get the size you need to allocate from m_get_statusmessage_size 200 * Get the size you need to allocate from m_get_statusmessage_size.
193 The self variant will copy our own status message. */ 201 * The self variant will copy our own status message.
202 */
194int tox_copy_statusmessage(void *tox, int friendnumber, uint8_t *buf, uint32_t maxlen) 203int tox_copy_statusmessage(void *tox, int friendnumber, uint8_t *buf, uint32_t maxlen)
195{ 204{
196 Messenger *m = tox; 205 Messenger *m = tox;
@@ -206,7 +215,8 @@ int tox_copy_self_statusmessage(void *tox, uint8_t *buf, uint32_t maxlen)
206/* Return one of USERSTATUS values. 215/* Return one of USERSTATUS values.
207 * Values unknown to your application should be represented as USERSTATUS_NONE. 216 * Values unknown to your application should be represented as USERSTATUS_NONE.
208 * As above, the self variant will return our own USERSTATUS. 217 * As above, the self variant will return our own USERSTATUS.
209 * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ 218 * If friendnumber is invalid, this shall return USERSTATUS_INVALID.
219 */
210USERSTATUS tox_get_userstatus(void *tox, int friendnumber) 220USERSTATUS tox_get_userstatus(void *tox, int friendnumber)
211{ 221{
212 Messenger *m = tox; 222 Messenger *m = tox;
@@ -221,7 +231,8 @@ USERSTATUS tox_get_selfuserstatus(void *tox)
221 231
222 232
223/* Sets whether we send read receipts for friendnumber. 233/* Sets whether we send read receipts for friendnumber.
224 * This function is not lazy, and it will fail if yesno is not (0 or 1).*/ 234 * This function is not lazy, and it will fail if yesno is not (0 or 1).
235 */
225void tox_set_sends_receipts(void *tox, int friendnumber, int yesno) 236void tox_set_sends_receipts(void *tox, int friendnumber, int yesno)
226{ 237{
227 Messenger *m = tox; 238 Messenger *m = tox;
@@ -229,8 +240,9 @@ void tox_set_sends_receipts(void *tox, int friendnumber, int yesno)
229} 240}
230 241
231 242
232/* set the function that will be executed when a friend request is received. 243/* Set the function that will be executed when a friend request is received.
233 function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ 244 * Function format is function(uint8_t * public_key, uint8_t * data, uint16_t length)
245 */
234void tox_callback_friendrequest(void *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata) 246void tox_callback_friendrequest(void *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata)
235{ 247{
236 Messenger *m = tox; 248 Messenger *m = tox;
@@ -238,8 +250,9 @@ void tox_callback_friendrequest(void *tox, void (*function)(uint8_t *, uint8_t *
238} 250}
239 251
240 252
241/* set the function that will be executed when a message from a friend is received. 253/* Set the function that will be executed when a message from a friend is received.
242 function format is: function(int friendnumber, uint8_t * message, uint32_t length) */ 254 * Function format is: function(int friendnumber, uint8_t * message, uint32_t length)
255 */
243void tox_callback_friendmessage(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), 256void tox_callback_friendmessage(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *),
244 void *userdata) 257 void *userdata)
245{ 258{
@@ -247,17 +260,19 @@ void tox_callback_friendmessage(void *tox, void (*function)(Messenger *tox, int,
247 m_callback_friendmessage(m, function, userdata); 260 m_callback_friendmessage(m, function, userdata);
248} 261}
249 262
250/* set the function that will be executed when an action from a friend is received. 263/* Set the function that will be executed when an action from a friend is received.
251 function format is: function(int friendnumber, uint8_t * action, uint32_t length) */ 264 * function format is: function(int friendnumber, uint8_t * action, uint32_t length)
265 */
252void tox_callback_action(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), void *userdata) 266void tox_callback_action(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), void *userdata)
253{ 267{
254 Messenger *m = tox; 268 Messenger *m = tox;
255 m_callback_action(m, function, userdata); 269 m_callback_action(m, function, userdata);
256} 270}
257 271
258/* set the callback for name changes 272/* Set the callback for name changes.
259 function(int friendnumber, uint8_t *newname, uint16_t length) 273 * function(int friendnumber, uint8_t *newname, uint16_t length)
260 you are not responsible for freeing newname */ 274 * You are not responsible for freeing newname.
275 */
261void tox_callback_namechange(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), 276void tox_callback_namechange(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *),
262 void *userdata) 277 void *userdata)
263{ 278{
@@ -265,9 +280,10 @@ void tox_callback_namechange(void *tox, void (*function)(Messenger *tox, int, ui
265 m_callback_namechange(m, function, userdata); 280 m_callback_namechange(m, function, userdata);
266} 281}
267 282
268/* set the callback for status message changes 283/* Set the callback for status message changes.
269 function(int friendnumber, uint8_t *newstatus, uint16_t length) 284 * function(int friendnumber, uint8_t *newstatus, uint16_t length)
270 you are not responsible for freeing newstatus */ 285 * You are not responsible for freeing newstatus.
286 */
271void tox_callback_statusmessage(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *), 287void tox_callback_statusmessage(void *tox, void (*function)(Messenger *tox, int, uint8_t *, uint16_t, void *),
272 void *userdata) 288 void *userdata)
273{ 289{
@@ -275,74 +291,83 @@ void tox_callback_statusmessage(void *tox, void (*function)(Messenger *tox, int,
275 m_callback_statusmessage(m, function, userdata); 291 m_callback_statusmessage(m, function, userdata);
276} 292}
277 293
278/* set the callback for status type changes 294/* Set the callback for status type changes.
279 function(int friendnumber, USERSTATUS kind) */ 295 * function(int friendnumber, USERSTATUS kind)
296 */
280void tox_callback_userstatus(void *tox, void (*function)(Messenger *tox, int, USERSTATUS, void *), void *userdata) 297void tox_callback_userstatus(void *tox, void (*function)(Messenger *tox, int, USERSTATUS, void *), void *userdata)
281{ 298{
282 Messenger *m = tox; 299 Messenger *m = tox;
283 m_callback_userstatus(m, function, userdata); 300 m_callback_userstatus(m, function, userdata);
284} 301}
285 302
286/* set the callback for read receipts 303/* Set the callback for read receipts.
287 function(int friendnumber, uint32_t receipt) 304 * function(int friendnumber, uint32_t receipt)
288 if you are keeping a record of returns from m_sendmessage, 305 *
289 receipt might be one of those values, and that means the message 306 * If you are keeping a record of returns from m_sendmessage;
290 has been received on the other side. since core doesn't 307 * receipt might be one of those values, meaning the message
291 track ids for you, receipt may not correspond to any message 308 * has been received on the other side.
292 in that case, you should discard it. */ 309 * Since core doesn't track ids for you, receipt may not correspond to any message.
310 * in that case, you should discard it.
311 */
293void tox_callback_read_receipt(void *tox, void (*function)(Messenger *tox, int, uint32_t, void *), void *userdata) 312void tox_callback_read_receipt(void *tox, void (*function)(Messenger *tox, int, uint32_t, void *), void *userdata)
294{ 313{
295 Messenger *m = tox; 314 Messenger *m = tox;
296 m_callback_read_receipt(m, function, userdata); 315 m_callback_read_receipt(m, function, userdata);
297} 316}
298 317
299/* set the callback for connection status changes 318/* Set the callback for connection status changes.
300 function(int friendnumber, uint8_t status) 319 * function(int friendnumber, uint8_t status)
301 status: 320 * Status:
302 0 -- friend went offline after being previously online 321 * 0 -- friend went offline after being previously online
303 1 -- friend went online 322 * 1 -- friend went online
304 note that this callback is not called when adding friends, thus the "after 323 *
305 being previously online" part. it's assumed that when adding friends, 324 * NOTE: this callback is not called when adding friends, thus the "after
306 their connection status is offline. */ 325 * being previously online" part. It's assumed that when adding friends,
326 * their connection status is offline.
327 */
307void tox_callback_connectionstatus(void *tox, void (*function)(Messenger *tox, int, uint8_t, void *), void *userdata) 328void tox_callback_connectionstatus(void *tox, void (*function)(Messenger *tox, int, uint8_t, void *), void *userdata)
308{ 329{
309 Messenger *m = tox; 330 Messenger *m = tox;
310 m_callback_connectionstatus(m, function, userdata); 331 m_callback_connectionstatus(m, function, userdata);
311} 332}
312 333
313/* Use this function to bootstrap the client 334/* Use this function to bootstrap the client.
314 Sends a get nodes request to the given node with ip port and public_key */ 335 * Sends a get nodes request to the given node with ip port and public_key.
336 */
315void tox_bootstrap(void *tox, IP_Port ip_port, uint8_t *public_key) 337void tox_bootstrap(void *tox, IP_Port ip_port, uint8_t *public_key)
316{ 338{
317 Messenger *m = tox; 339 Messenger *m = tox;
318 DHT_bootstrap(m->dht, ip_port, public_key); 340 DHT_bootstrap(m->dht, ip_port, public_key);
319} 341}
320 342
321/* returns 0 if we are not connected to the DHT 343/* returns 0 if we are not connected to the DHT.
322 returns 1 if we are */ 344 * returns 1 if we are.
345 */
323int tox_isconnected(void *tox) 346int tox_isconnected(void *tox)
324{ 347{
325 Messenger *m = tox; 348 Messenger *m = tox;
326 return DHT_isconnected(m->dht); 349 return DHT_isconnected(m->dht);
327} 350}
328 351
329/* run this at startup 352/* Run this at startup.
330 * returns allocated instance of tox on success 353 * returns allocated instance of tox on success.
331 * returns 0 if there are problems */ 354 * returns 0 if there are problems.
355 */
332void *tox_new(void) 356void *tox_new(void)
333{ 357{
334 return initMessenger(); 358 return initMessenger();
335} 359}
336 360
337/* run this before closing shop 361/* Run this before closing shop.
338 * free all datastructures */ 362 * Free all datastructures.
363 */
339void tox_kill(void *tox) 364void tox_kill(void *tox)
340{ 365{
341 Messenger *m = tox; 366 Messenger *m = tox;
342 cleanupMessenger(m); 367 cleanupMessenger(m);
343} 368}
344 369
345/* the main loop that needs to be run at least 20 times per second */ 370/* The main loop that needs to be run at least 20 times per second. */
346void tox_do(void *tox) 371void tox_do(void *tox)
347{ 372{
348 Messenger *m = tox; 373 Messenger *m = tox;
@@ -351,21 +376,21 @@ void tox_do(void *tox)
351 376
352/* SAVING AND LOADING FUNCTIONS: */ 377/* SAVING AND LOADING FUNCTIONS: */
353 378
354/* returns the size of the messenger data (for saving) */ 379/* returns the size of the messenger data (for saving). */
355uint32_t tox_size(void *tox) 380uint32_t tox_size(void *tox)
356{ 381{
357 Messenger *m = tox; 382 Messenger *m = tox;
358 return Messenger_size(m); 383 return Messenger_size(m);
359} 384}
360 385
361/* save the messenger in data (must be allocated memory of size Messenger_size()) */ 386/* Save the messenger in data (must be allocated memory of size Messenger_size()). */
362void tox_save(void *tox, uint8_t *data) 387void tox_save(void *tox, uint8_t *data)
363{ 388{
364 Messenger *m = tox; 389 Messenger *m = tox;
365 Messenger_save(m, data); 390 Messenger_save(m, data);
366} 391}
367 392
368/* load the messenger from data of size length */ 393/* Load the messenger from data of size length. */
369int tox_load(void *tox, uint8_t *data, uint32_t length) 394int tox_load(void *tox, uint8_t *data, uint32_t length)
370{ 395{
371 Messenger *m = tox; 396 Messenger *m = tox;