diff options
Diffstat (limited to 'toxcore/Messenger.c')
-rw-r--r-- | toxcore/Messenger.c | 93 |
1 files changed, 48 insertions, 45 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 2bbeed41..621f7130 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -34,13 +34,9 @@ static uint8_t friend_not_valid(Messenger *m, int friendnumber) | |||
34 | return (unsigned int)friendnumber >= m->numfriends; | 34 | return (unsigned int)friendnumber >= m->numfriends; |
35 | } | 35 | } |
36 | 36 | ||
37 | /* return 1 if we are online. | ||
38 | * return 0 if we are offline. | ||
39 | * static uint8_t online; | ||
40 | */ | ||
41 | |||
42 | /* Set the size of the friend list to numfriends. | 37 | /* Set the size of the friend list to numfriends. |
43 | * return -1 if realloc fails. | 38 | * |
39 | * return -1 if realloc fails. | ||
44 | */ | 40 | */ |
45 | int realloc_friendlist(Messenger *m, uint32_t num) | 41 | int realloc_friendlist(Messenger *m, uint32_t num) |
46 | { | 42 | { |
@@ -59,8 +55,8 @@ int realloc_friendlist(Messenger *m, uint32_t num) | |||
59 | return 0; | 55 | return 0; |
60 | } | 56 | } |
61 | 57 | ||
62 | /* return the friend id associated to that public key. | 58 | /* return the friend id associated to that public key. |
63 | * return -1 if no such friend. | 59 | * return -1 if no such friend. |
64 | */ | 60 | */ |
65 | int getfriend_id(Messenger *m, uint8_t *client_id) | 61 | int getfriend_id(Messenger *m, uint8_t *client_id) |
66 | { | 62 | { |
@@ -77,8 +73,9 @@ int getfriend_id(Messenger *m, uint8_t *client_id) | |||
77 | 73 | ||
78 | /* Copies the public key associated to that friend id into client_id buffer. | 74 | /* Copies the public key associated to that friend id into client_id buffer. |
79 | * Make sure that client_id is of size CLIENT_ID_SIZE. | 75 | * Make sure that client_id is of size CLIENT_ID_SIZE. |
80 | * return 0 if success. | 76 | * |
81 | * return -1 if failure. | 77 | * return 0 if success. |
78 | * return -1 if failure. | ||
82 | */ | 79 | */ |
83 | int getclient_id(Messenger *m, int friend_id, uint8_t *client_id) | 80 | int getclient_id(Messenger *m, int friend_id, uint8_t *client_id) |
84 | { | 81 | { |
@@ -92,10 +89,9 @@ int getclient_id(Messenger *m, int friend_id, uint8_t *client_id) | |||
92 | 89 | ||
93 | return -1; | 90 | return -1; |
94 | } | 91 | } |
95 | /* | 92 | /* TODO: Another checksum algorithm might be better. |
96 | * return a uint16_t that represents the checksum of address of length len. | ||
97 | * | 93 | * |
98 | * TODO: Another checksum algorithm might be better. | 94 | * return a uint16_t that represents the checksum of address of length len. |
99 | */ | 95 | */ |
100 | static uint16_t address_checksum(uint8_t *address, uint32_t len) | 96 | static uint16_t address_checksum(uint8_t *address, uint32_t len) |
101 | { | 97 | { |
@@ -110,10 +106,9 @@ static uint16_t address_checksum(uint8_t *address, uint32_t len) | |||
110 | return check; | 106 | return check; |
111 | } | 107 | } |
112 | 108 | ||
113 | /* | 109 | /* Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] |
114 | * return FRIEND_ADDRESS_SIZE byte address to give to others. | ||
115 | * Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] | ||
116 | * | 110 | * |
111 | * return FRIEND_ADDRESS_SIZE byte address to give to others. | ||
117 | */ | 112 | */ |
118 | void getaddress(Messenger *m, uint8_t *address) | 113 | void getaddress(Messenger *m, uint8_t *address) |
119 | { | 114 | { |
@@ -130,16 +125,17 @@ void getaddress(Messenger *m, uint8_t *address) | |||
130 | * Address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. | 125 | * Address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. |
131 | * TODO: add checksum. | 126 | * TODO: add checksum. |
132 | * data is the data and length is the length. | 127 | * data is the data and length is the length. |
133 | * return the friend number if success. | 128 | * |
134 | * return FA_TOOLONG if message length is too long. | 129 | * return the friend number if success. |
135 | * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte). | 130 | * return FA_TOOLONG if message length is too long. |
136 | * return FAERR_OWNKEY if user's own key. | 131 | * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte). |
137 | * return FAERR_ALREADYSENT if friend request already sent or already a friend. | 132 | * return FAERR_OWNKEY if user's own key. |
138 | * return FAERR_UNKNOWN for unknown error. | 133 | * return FAERR_ALREADYSENT if friend request already sent or already a friend. |
139 | * return FAERR_BADCHECKSUM if bad checksum in address. | 134 | * return FAERR_UNKNOWN for unknown error. |
140 | * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different. | 135 | * return FAERR_BADCHECKSUM if bad checksum in address. |
141 | * (the nospam for that friend was set to the new one). | 136 | * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different. |
142 | * return FAERR_NOMEM if increasing the friend list size fails. | 137 | * (the nospam for that friend was set to the new one). |
138 | * return FAERR_NOMEM if increasing the friend list size fails. | ||
143 | */ | 139 | */ |
144 | int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) | 140 | int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) |
145 | { | 141 | { |
@@ -247,6 +243,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id) | |||
247 | } | 243 | } |
248 | 244 | ||
249 | /* Remove a friend. | 245 | /* Remove a friend. |
246 | * | ||
250 | * return 0 if success. | 247 | * return 0 if success. |
251 | * return -1 if failure. | 248 | * return -1 if failure. |
252 | */ | 249 | */ |
@@ -274,11 +271,11 @@ int m_delfriend(Messenger *m, int friendnumber) | |||
274 | return 0; | 271 | return 0; |
275 | } | 272 | } |
276 | 273 | ||
277 | /* return FRIEND_ONLINE if friend is online. | 274 | /* return FRIEND_ONLINE if friend is online. |
278 | * return FRIEND_CONFIRMED if friend is confirmed. | 275 | * return FRIEND_CONFIRMED if friend is confirmed. |
279 | * return FRIEND_REQUESTED if the friend request was sent. | 276 | * return FRIEND_REQUESTED if the friend request was sent. |
280 | * return FRIEND_ADDED if the friend was added. | 277 | * return FRIEND_ADDED if the friend was added. |
281 | * return NOFRIEND if there is no friend with that number. | 278 | * return NOFRIEND if there is no friend with that number. |
282 | */ | 279 | */ |
283 | int m_friendstatus(Messenger *m, int friendnumber) | 280 | int m_friendstatus(Messenger *m, int friendnumber) |
284 | { | 281 | { |
@@ -289,8 +286,9 @@ int m_friendstatus(Messenger *m, int friendnumber) | |||
289 | } | 286 | } |
290 | 287 | ||
291 | /* Send a text chat message to an online friend. | 288 | /* Send a text chat message to an online friend. |
292 | * return the message id if packet was successfully put into the send queue. | 289 | * |
293 | * return 0 if it was not. | 290 | * return the message id if packet was successfully put into the send queue. |
291 | * return 0 if it was not. | ||
294 | */ | 292 | */ |
295 | uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length) | 293 | uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length) |
296 | { | 294 | { |
@@ -322,8 +320,9 @@ uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, ui | |||
322 | } | 320 | } |
323 | 321 | ||
324 | /* Send an action to an online friend. | 322 | /* Send an action to an online friend. |
325 | * return 1 if packet was successfully put into the send queue. | 323 | * |
326 | * return 0 if it was not. | 324 | * return 1 if packet was successfully put into the send queue. |
325 | * return 0 if it was not. | ||
327 | */ | 326 | */ |
328 | int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length) | 327 | int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length) |
329 | { | 328 | { |
@@ -342,8 +341,9 @@ static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t le | |||
342 | } | 341 | } |
343 | 342 | ||
344 | /* Set the name of a friend. | 343 | /* Set the name of a friend. |
345 | * return 0 if success. | 344 | * |
346 | * return -1 if failure. | 345 | * return 0 if success. |
346 | * return -1 if failure. | ||
347 | */ | 347 | */ |
348 | static int setfriendname(Messenger *m, int friendnumber, uint8_t *name) | 348 | static int setfriendname(Messenger *m, int friendnumber, uint8_t *name) |
349 | { | 349 | { |
@@ -358,8 +358,9 @@ static int setfriendname(Messenger *m, int friendnumber, uint8_t *name) | |||
358 | * name must be a string of maximum MAX_NAME_LENGTH length. | 358 | * name must be a string of maximum MAX_NAME_LENGTH length. |
359 | * length must be at least 1 byte. | 359 | * length must be at least 1 byte. |
360 | * length is the length of name with the NULL terminator. | 360 | * length is the length of name with the NULL terminator. |
361 | * return 0 if success. | 361 | * |
362 | * return -1 if failure. | 362 | * return 0 if success. |
363 | * return -1 if failure. | ||
363 | */ | 364 | */ |
364 | int setname(Messenger *m, uint8_t *name, uint16_t length) | 365 | int setname(Messenger *m, uint8_t *name, uint16_t length) |
365 | { | 366 | { |
@@ -377,8 +378,9 @@ int setname(Messenger *m, uint8_t *name, uint16_t length) | |||
377 | } | 378 | } |
378 | 379 | ||
379 | /* Get our nickname and put it in name. | 380 | /* Get our nickname and put it in name. |
380 | * Name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. | 381 | * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. |
381 | * return the length of the name. | 382 | * |
383 | * return the length of the name. | ||
382 | */ | 384 | */ |
383 | uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen) | 385 | uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen) |
384 | { | 386 | { |
@@ -395,9 +397,10 @@ uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen) | |||
395 | } | 397 | } |
396 | 398 | ||
397 | /* Get name of friendnumber and put it in name. | 399 | /* Get name of friendnumber and put it in name. |
398 | * Name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. | 400 | * name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. |
399 | * return 0 if success. | 401 | * |
400 | * return -1 if failure. | 402 | * return 0 if success. |
403 | * return -1 if failure. | ||
401 | */ | 404 | */ |
402 | int getname(Messenger *m, int friendnumber, uint8_t *name) | 405 | int getname(Messenger *m, int friendnumber, uint8_t *name) |
403 | { | 406 | { |
@@ -924,7 +927,7 @@ void doMessenger(Messenger *m) | |||
924 | LANdiscovery(m); | 927 | LANdiscovery(m); |
925 | } | 928 | } |
926 | 929 | ||
927 | /* return the size of the messenger data (for saving) */ | 930 | /* return size of the messenger data (for saving) */ |
928 | uint32_t Messenger_size(Messenger *m) | 931 | uint32_t Messenger_size(Messenger *m) |
929 | { | 932 | { |
930 | return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES | 933 | return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES |