summaryrefslogtreecommitdiff
path: root/toxcore/Messenger.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/Messenger.c')
-rw-r--r--toxcore/Messenger.c214
1 files changed, 115 insertions, 99 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index c5dd8a40..01b6b090 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -28,12 +28,14 @@
28static void set_friend_status(Messenger *m, int friendnumber, uint8_t status); 28static void set_friend_status(Messenger *m, int friendnumber, uint8_t status);
29static int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length); 29static int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length);
30 30
31/* 1 if we are online 31/* return 1 if we are online.
32 0 if we are offline 32 * return 0 if we are offline.
33 static uint8_t online; */ 33 * static uint8_t online;
34 */
34 35
35/* set the size of the friend list to numfriends 36/* Set the size of the friend list to numfriends.
36 return -1 if realloc fails */ 37 * return -1 if realloc fails.
38 */
37int realloc_friendlist(Messenger *m, uint32_t num) 39int realloc_friendlist(Messenger *m, uint32_t num)
38{ 40{
39 if (num == 0) { 41 if (num == 0) {
@@ -52,7 +54,8 @@ int realloc_friendlist(Messenger *m, uint32_t num)
52} 54}
53 55
54/* return the friend id associated to that public key. 56/* return the friend id associated to that public key.
55 return -1 if no such friend */ 57 * return -1 if no such friend.
58 */
56int getfriend_id(Messenger *m, uint8_t *client_id) 59int getfriend_id(Messenger *m, uint8_t *client_id)
57{ 60{
58 uint32_t i; 61 uint32_t i;
@@ -66,10 +69,11 @@ int getfriend_id(Messenger *m, uint8_t *client_id)
66 return -1; 69 return -1;
67} 70}
68 71
69/* copies the public key associated to that friend id into client_id buffer. 72/* Copies the public key associated to that friend id into client_id buffer.
70 make sure that client_id is of size CLIENT_ID_SIZE. 73 * Make sure that client_id is of size CLIENT_ID_SIZE.
71 return 0 if success 74 * return 0 if success.
72 return -1 if failure. */ 75 * return -1 if failure.
76 */
73int getclient_id(Messenger *m, int friend_id, uint8_t *client_id) 77int getclient_id(Messenger *m, int friend_id, uint8_t *client_id)
74{ 78{
75 if (friend_id >= m->numfriends || friend_id < 0) 79 if (friend_id >= m->numfriends || friend_id < 0)
@@ -83,7 +87,7 @@ int getclient_id(Messenger *m, int friend_id, uint8_t *client_id)
83 return -1; 87 return -1;
84} 88}
85/* 89/*
86 * returns a uint16_t that represents the checksum of address of length len 90 * return a uint16_t that represents the checksum of address of length len.
87 * 91 *
88 * TODO: Another checksum algorithm might be better. 92 * TODO: Another checksum algorithm might be better.
89 */ 93 */
@@ -101,8 +105,8 @@ static uint16_t address_checksum(uint8_t *address, uint32_t len)
101} 105}
102 106
103/* 107/*
104 * returns a FRIEND_ADDRESS_SIZE byte address to give to others. 108 * return FRIEND_ADDRESS_SIZE byte address to give to others.
105 * format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] 109 * Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
106 * 110 *
107 */ 111 */
108void getaddress(Messenger *m, uint8_t *address) 112void getaddress(Messenger *m, uint8_t *address)
@@ -115,20 +119,21 @@ void getaddress(Messenger *m, uint8_t *address)
115} 119}
116 120
117/* 121/*
118 * add a friend 122 * Add a friend.
119 * set the data that will be sent along with friend request 123 * Set the data that will be sent along with friend request.
120 * 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. 124 * Address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes.
121 * data is the data and length is the length 125 * TODO: add checksum.
122 * returns the friend number if success 126 * data is the data and length is the length.
123 * return FA_TOOLONG if message length is too long 127 * return the friend number if success.
124 * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte) 128 * return FA_TOOLONG if message length is too long.
125 * return FAERR_OWNKEY if user's own key 129 * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte).
126 * return FAERR_ALREADYSENT if friend request already sent or already a friend 130 * return FAERR_OWNKEY if user's own key.
127 * return FAERR_UNKNOWN for unknown error 131 * return FAERR_ALREADYSENT if friend request already sent or already a friend.
128 * return FAERR_BADCHECKSUM if bad checksum in address 132 * return FAERR_UNKNOWN for unknown error.
129 * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different 133 * return FAERR_BADCHECKSUM if bad checksum in address.
130 * (the nospam for that friend was set to the new one) 134 * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different.
131 * return FAERR_NOMEM if increasing the friend list size fails 135 * (the nospam for that friend was set to the new one).
136 * return FAERR_NOMEM if increasing the friend list size fails.
132 */ 137 */
133int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length) 138int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
134{ 139{
@@ -164,7 +169,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
164 return FAERR_SETNEWNOSPAM; 169 return FAERR_SETNEWNOSPAM;
165 } 170 }
166 171
167 /* resize the friend list if necessary */ 172 /* Resize the friend list if necessary. */
168 if (realloc_friendlist(m, m->numfriends + 1) != 0) 173 if (realloc_friendlist(m, m->numfriends + 1) != 0)
169 return FAERR_NOMEM; 174 return FAERR_NOMEM;
170 175
@@ -186,7 +191,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
186 memcpy(m->friendlist[i].info, data, length); 191 memcpy(m->friendlist[i].info, data, length);
187 m->friendlist[i].info_size = length; 192 m->friendlist[i].info_size = length;
188 m->friendlist[i].message_id = 0; 193 m->friendlist[i].message_id = 0;
189 m->friendlist[i].receives_read_receipts = 1; /* default: YES */ 194 m->friendlist[i].receives_read_receipts = 1; /* Default: YES. */
190 memcpy(&(m->friendlist[i].friendrequest_nospam), address + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t)); 195 memcpy(&(m->friendlist[i].friendrequest_nospam), address + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t));
191 196
192 if (m->numfriends == i) 197 if (m->numfriends == i)
@@ -204,7 +209,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id)
204 if (getfriend_id(m, client_id) != -1) 209 if (getfriend_id(m, client_id) != -1)
205 return -1; 210 return -1;
206 211
207 /* resize the friend list if necessary */ 212 /* Resize the friend list if necessary. */
208 if (realloc_friendlist(m, m->numfriends + 1) != 0) 213 if (realloc_friendlist(m, m->numfriends + 1) != 0)
209 return FAERR_NOMEM; 214 return FAERR_NOMEM;
210 215
@@ -223,7 +228,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id)
223 m->friendlist[i].statusmessage_length = 1; 228 m->friendlist[i].statusmessage_length = 1;
224 m->friendlist[i].userstatus = USERSTATUS_NONE; 229 m->friendlist[i].userstatus = USERSTATUS_NONE;
225 m->friendlist[i].message_id = 0; 230 m->friendlist[i].message_id = 0;
226 m->friendlist[i].receives_read_receipts = 1; /* default: YES */ 231 m->friendlist[i].receives_read_receipts = 1; /* Default: YES. */
227 232
228 if (m->numfriends == i) 233 if (m->numfriends == i)
229 ++ m->numfriends; 234 ++ m->numfriends;
@@ -235,9 +240,10 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id)
235 return -1; 240 return -1;
236} 241}
237 242
238/* remove a friend 243/* Remove a friend.
239 return 0 if success 244 * return 0 if success.
240 return -1 if failure */ 245 * return -1 if failure.
246 */
241int m_delfriend(Messenger *m, int friendnumber) 247int m_delfriend(Messenger *m, int friendnumber)
242{ 248{
243 if (friendnumber >= m->numfriends || friendnumber < 0) 249 if (friendnumber >= m->numfriends || friendnumber < 0)
@@ -262,11 +268,12 @@ int m_delfriend(Messenger *m, int friendnumber)
262 return 0; 268 return 0;
263} 269}
264 270
265/* return FRIEND_ONLINE if friend is online 271/* return FRIEND_ONLINE if friend is online.
266 return FRIEND_CONFIRMED if friend is confirmed 272 * return FRIEND_CONFIRMED if friend is confirmed.
267 return FRIEND_REQUESTED if the friend request was sent 273 * return FRIEND_REQUESTED if the friend request was sent.
268 return FRIEND_ADDED if the friend was added 274 * return FRIEND_ADDED if the friend was added.
269 return NOFRIEND if there is no friend with that number */ 275 * return NOFRIEND if there is no friend with that number.
276 */
270int m_friendstatus(Messenger *m, int friendnumber) 277int m_friendstatus(Messenger *m, int friendnumber)
271{ 278{
272 if (friendnumber < 0 || friendnumber >= m->numfriends) 279 if (friendnumber < 0 || friendnumber >= m->numfriends)
@@ -275,9 +282,10 @@ int m_friendstatus(Messenger *m, int friendnumber)
275 return m->friendlist[friendnumber].status; 282 return m->friendlist[friendnumber].status;
276} 283}
277 284
278/* send a text chat message to an online friend 285/* Send a text chat message to an online friend.
279 return the message id if packet was successfully put into the send queue 286 * return the message id if packet was successfully put into the send queue.
280 return 0 if it was not */ 287 * return 0 if it was not.
288 */
281uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length) 289uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length)
282{ 290{
283 if (friendnumber < 0 || friendnumber >= m->numfriends) 291 if (friendnumber < 0 || friendnumber >= m->numfriends)
@@ -286,7 +294,7 @@ uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_
286 uint32_t msgid = ++m->friendlist[friendnumber].message_id; 294 uint32_t msgid = ++m->friendlist[friendnumber].message_id;
287 295
288 if (msgid == 0) 296 if (msgid == 0)
289 msgid = 1; /* otherwise, false error */ 297 msgid = 1; // Otherwise, false error
290 298
291 if (m_sendmessage_withid(m, friendnumber, msgid, message, length)) { 299 if (m_sendmessage_withid(m, friendnumber, msgid, message, length)) {
292 return msgid; 300 return msgid;
@@ -307,16 +315,18 @@ uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, ui
307 return write_cryptpacket_id(m, friendnumber, PACKET_ID_MESSAGE, temp, length + sizeof(theid)); 315 return write_cryptpacket_id(m, friendnumber, PACKET_ID_MESSAGE, temp, length + sizeof(theid));
308} 316}
309 317
310/* send an action to an online friend 318/* Send an action to an online friend.
311 return 1 if packet was successfully put into the send queue 319 * return 1 if packet was successfully put into the send queue.
312 return 0 if it was not */ 320 * return 0 if it was not.
321 */
313int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length) 322int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length)
314{ 323{
315 return write_cryptpacket_id(m, friendnumber, PACKET_ID_ACTION, action, length); 324 return write_cryptpacket_id(m, friendnumber, PACKET_ID_ACTION, action, length);
316} 325}
317 326
318/* send a name packet to friendnumber 327/* Send a name packet to friendnumber.
319 length is the length with the NULL terminator*/ 328 * length is the length with the NULL terminator.
329 */
320static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t length) 330static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t length)
321{ 331{
322 if (length > MAX_NAME_LENGTH || length == 0) 332 if (length > MAX_NAME_LENGTH || length == 0)
@@ -325,9 +335,10 @@ static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t le
325 return write_cryptpacket_id(m, friendnumber, PACKET_ID_NICKNAME, name, length); 335 return write_cryptpacket_id(m, friendnumber, PACKET_ID_NICKNAME, name, length);
326} 336}
327 337
328/* set the name of a friend 338/* Set the name of a friend.
329 return 0 if success 339 * return 0 if success.
330 return -1 if failure */ 340 * return -1 if failure.
341 */
331static int setfriendname(Messenger *m, int friendnumber, uint8_t *name) 342static int setfriendname(Messenger *m, int friendnumber, uint8_t *name)
332{ 343{
333 if (friendnumber >= m->numfriends || friendnumber < 0) 344 if (friendnumber >= m->numfriends || friendnumber < 0)
@@ -338,11 +349,12 @@ static int setfriendname(Messenger *m, int friendnumber, uint8_t *name)
338} 349}
339 350
340/* Set our nickname 351/* Set our nickname
341 name must be a string of maximum MAX_NAME_LENGTH length. 352 * name must be a string of maximum MAX_NAME_LENGTH length.
342 length must be at least 1 byte 353 * length must be at least 1 byte.
343 length is the length of name with the NULL terminator 354 * length is the length of name with the NULL terminator.
344 return 0 if success 355 * return 0 if success.
345 return -1 if failure */ 356 * return -1 if failure.
357 */
346int setname(Messenger *m, uint8_t *name, uint16_t length) 358int setname(Messenger *m, uint8_t *name, uint16_t length)
347{ 359{
348 if (length > MAX_NAME_LENGTH || length == 0) 360 if (length > MAX_NAME_LENGTH || length == 0)
@@ -358,10 +370,10 @@ int setname(Messenger *m, uint8_t *name, uint16_t length)
358 return 0; 370 return 0;
359} 371}
360 372
361/* get our nickname 373/* Get our nickname and put it in name.
362 put it in name 374 * Name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
363 name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. 375 * return the length of the name.
364 return the length of the name */ 376 */
365uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen) 377uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen)
366{ 378{
367 uint16_t len; 379 uint16_t len;
@@ -376,11 +388,11 @@ uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen)
376 return len; 388 return len;
377} 389}
378 390
379/* get name of friendnumber 391/* Get name of friendnumber and put it in name.
380 put it in name 392 * 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. 393 * return 0 if success.
382 return 0 if success 394 * return -1 if failure.
383 return -1 if failure */ 395 */
384int getname(Messenger *m, int friendnumber, uint8_t *name) 396int getname(Messenger *m, int friendnumber, uint8_t *name)
385{ 397{
386 if (friendnumber >= m->numfriends || friendnumber < 0) 398 if (friendnumber >= m->numfriends || friendnumber < 0)
@@ -421,8 +433,9 @@ int m_set_userstatus(Messenger *m, USERSTATUS status)
421 return 0; 433 return 0;
422} 434}
423 435
424/* return the size of friendnumber's user status 436/* return the size of friendnumber's user status.
425 guaranteed to be at most MAX_STATUSMESSAGE_LENGTH */ 437 * Guaranteed to be at most MAX_STATUSMESSAGE_LENGTH.
438 */
426int m_get_statusmessage_size(Messenger *m, int friendnumber) 439int m_get_statusmessage_size(Messenger *m, int friendnumber)
427{ 440{
428 if (friendnumber >= m->numfriends || friendnumber < 0) 441 if (friendnumber >= m->numfriends || friendnumber < 0)
@@ -431,8 +444,9 @@ int m_get_statusmessage_size(Messenger *m, int friendnumber)
431 return m->friendlist[friendnumber].statusmessage_length; 444 return m->friendlist[friendnumber].statusmessage_length;
432} 445}
433 446
434/* copy the user status of friendnumber into buf, truncating if needed to maxlen 447/* Copy the user status of friendnumber into buf, truncating if needed to maxlen
435 bytes, use m_get_statusmessage_size to find out how much you need to allocate */ 448 * bytes, use m_get_statusmessage_size to find out how much you need to allocate.
449 */
436int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen) 450int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen)
437{ 451{
438 if (friendnumber >= m->numfriends || friendnumber < 0) 452 if (friendnumber >= m->numfriends || friendnumber < 0)
@@ -517,13 +531,13 @@ void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno)
517} 531}
518 532
519/* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); */ 533/* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); */
520/* set the function that will be executed when a friend request is received. */ 534/* Set the function that will be executed when a friend request is received. */
521void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata) 535void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata)
522{ 536{
523 callback_friendrequest(&(m->fr), function, userdata); 537 callback_friendrequest(&(m->fr), function, userdata);
524} 538}
525 539
526/* set the function that will be executed when a message from a friend is received. */ 540/* Set the function that will be executed when a message from a friend is received. */
527void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), 541void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *),
528 void *userdata) 542 void *userdata)
529{ 543{
@@ -607,11 +621,11 @@ int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint
607 return write_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, packet, length + 1); 621 return write_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, packet, length + 1);
608} 622}
609 623
610/*Interval in seconds between LAN discovery packet sending*/ 624/* Interval in seconds between LAN discovery packet sending. */
611#define LAN_DISCOVERY_INTERVAL 60 625#define LAN_DISCOVERY_INTERVAL 60
612#define PORT 33445 626#define PORT 33445
613 627
614/*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/ 628/* Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds. */
615static void LANdiscovery(Messenger *m) 629static void LANdiscovery(Messenger *m)
616{ 630{
617 if (m->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { 631 if (m->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) {
@@ -620,7 +634,7 @@ static void LANdiscovery(Messenger *m)
620 } 634 }
621} 635}
622 636
623/* run this at startup */ 637/* Run this at startup. */
624Messenger *initMessenger(void) 638Messenger *initMessenger(void)
625{ 639{
626 Messenger *m = calloc(1, sizeof(Messenger)); 640 Messenger *m = calloc(1, sizeof(Messenger));
@@ -664,11 +678,11 @@ Messenger *initMessenger(void)
664 return m; 678 return m;
665} 679}
666 680
667/* run this before closing shop */ 681/* Run this before closing shop. */
668void cleanupMessenger(Messenger *m) 682void cleanupMessenger(Messenger *m)
669{ 683{
670 /* FIXME TODO ideally cleanupMessenger will mirror initMessenger 684 /* FIXME TODO: ideally cleanupMessenger will mirror initMessenger.
671 * this requires the other modules to expose cleanup functions 685 * This requires the other modules to expose cleanup functions.
672 */ 686 */
673 kill_DHT(m->dht); 687 kill_DHT(m->dht);
674 kill_net_crypto(m->net_crypto); 688 kill_net_crypto(m->net_crypto);
@@ -677,10 +691,10 @@ void cleanupMessenger(Messenger *m)
677 free(m); 691 free(m);
678} 692}
679 693
680//TODO: make this function not suck. 694/* TODO: Make this function not suck. */
681void doFriends(Messenger *m) 695void doFriends(Messenger *m)
682{ 696{
683 /* TODO: add incoming connections and some other stuff. */ 697 /* TODO: Add incoming connections and some other stuff. */
684 uint32_t i; 698 uint32_t i;
685 int len; 699 int len;
686 uint8_t temp[MAX_DATA_SIZE]; 700 uint8_t temp[MAX_DATA_SIZE];
@@ -699,14 +713,16 @@ void doFriends(Messenger *m)
699 } 713 }
700 714
701 if (m->friendlist[i].status == FRIEND_REQUESTED 715 if (m->friendlist[i].status == FRIEND_REQUESTED
702 || m->friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */ 716 || m->friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online. */
703 if (m->friendlist[i].status == FRIEND_REQUESTED) { 717 if (m->friendlist[i].status == FRIEND_REQUESTED) {
704 /* If we didn't connect to friend after successfully sending him a friend request the request is deemed 718 /* If we didn't connect to friend after successfully sending him a friend request the request is deemed
705 unsuccessful so we set the status back to FRIEND_ADDED and try again.*/ 719 * unsuccessful so we set the status back to FRIEND_ADDED and try again.
720 */
706 if (m->friendlist[i].friendrequest_lastsent + m->friendlist[i].friendrequest_timeout < temp_time) { 721 if (m->friendlist[i].friendrequest_lastsent + m->friendlist[i].friendrequest_timeout < temp_time) {
707 set_friend_status(m, i, FRIEND_ADDED); 722 set_friend_status(m, i, FRIEND_ADDED);
708 /* Double the default timeout everytime if friendrequest is assumed to have been 723 /* Double the default timeout everytime if friendrequest is assumed to have been
709 sent unsuccessfully. */ 724 * sent unsuccessfully.
725 */
710 m->friendlist[i].friendrequest_timeout *= 2; 726 m->friendlist[i].friendrequest_timeout *= 2;
711 } 727 }
712 } 728 }
@@ -720,7 +736,7 @@ void doFriends(Messenger *m)
720 736
721 break; 737 break;
722 738
723 case 3: /* Connection is established */ 739 case 3: /* Connection is established. */
724 set_friend_status(m, i, FRIEND_ONLINE); 740 set_friend_status(m, i, FRIEND_ONLINE);
725 m->friendlist[i].name_sent = 0; 741 m->friendlist[i].name_sent = 0;
726 m->friendlist[i].userstatus_sent = 0; 742 m->friendlist[i].userstatus_sent = 0;
@@ -738,7 +754,7 @@ void doFriends(Messenger *m)
738 } 754 }
739 } 755 }
740 756
741 while (m->friendlist[i].status == FRIEND_ONLINE) { /* friend is online */ 757 while (m->friendlist[i].status == FRIEND_ONLINE) { /* friend is online. */
742 if (m->friendlist[i].name_sent == 0) { 758 if (m->friendlist[i].name_sent == 0) {
743 if (m_sendname(m, i, m->name, m->name_length)) 759 if (m_sendname(m, i, m->name, m->name_length))
744 m->friendlist[i].name_sent = 1; 760 m->friendlist[i].name_sent = 1;
@@ -778,7 +794,7 @@ void doFriends(Messenger *m)
778 m->friend_namechange(m, i, data, data_length, m->friend_namechange_userdata); 794 m->friend_namechange(m, i, data, data_length, m->friend_namechange_userdata);
779 795
780 memcpy(m->friendlist[i].name, data, data_length); 796 memcpy(m->friendlist[i].name, data, data_length);
781 m->friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */ 797 m->friendlist[i].name[data_length - 1] = 0; /* Make sure the NULL terminator is present. */
782 break; 798 break;
783 } 799 }
784 800
@@ -851,7 +867,7 @@ void doFriends(Messenger *m)
851 } 867 }
852 } else { 868 } else {
853 if (is_cryptoconnected(m->net_crypto, 869 if (is_cryptoconnected(m->net_crypto,
854 m->friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ 870 m->friendlist[i].crypt_connection_id) == 4) { /* If the connection timed out, kill it. */
855 crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id); 871 crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id);
856 m->friendlist[i].crypt_connection_id = -1; 872 m->friendlist[i].crypt_connection_id = -1;
857 set_friend_status(m, i, FRIEND_CONFIRMED); 873 set_friend_status(m, i, FRIEND_CONFIRMED);
@@ -861,7 +877,7 @@ void doFriends(Messenger *m)
861 } 877 }
862 878
863 if (m->friendlist[i].ping_lastrecv + FRIEND_CONNECTION_TIMEOUT < temp_time) { 879 if (m->friendlist[i].ping_lastrecv + FRIEND_CONNECTION_TIMEOUT < temp_time) {
864 /* if we stopped recieving ping packets kill it */ 880 /* If we stopped recieving ping packets, kill it. */
865 crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id); 881 crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id);
866 m->friendlist[i].crypt_connection_id = -1; 882 m->friendlist[i].crypt_connection_id = -1;
867 set_friend_status(m, i, FRIEND_CONFIRMED); 883 set_friend_status(m, i, FRIEND_CONFIRMED);
@@ -890,7 +906,7 @@ void doInbound(Messenger *m)
890 } 906 }
891} 907}
892 908
893/* the main loop that needs to be run at least 20 times per second. */ 909/* The main loop that needs to be run at least 20 times per second. */
894void doMessenger(Messenger *m) 910void doMessenger(Messenger *m)
895{ 911{
896 networking_poll(m->net); 912 networking_poll(m->net);
@@ -902,21 +918,21 @@ void doMessenger(Messenger *m)
902 LANdiscovery(m); 918 LANdiscovery(m);
903} 919}
904 920
905/* returns the size of the messenger data (for saving) */ 921/* return the size of the messenger data (for saving) */
906uint32_t Messenger_size(Messenger *m) 922uint32_t Messenger_size(Messenger *m)
907{ 923{
908 return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES 924 return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
909 + sizeof(uint32_t) // nospam 925 + sizeof(uint32_t) // nospam.
910 + sizeof(uint32_t) // DHT size 926 + sizeof(uint32_t) // DHT size.
911 + DHT_size(m->dht) // DHT itself 927 + DHT_size(m->dht) // DHT itself.
912 + sizeof(uint32_t) // Friendlist size 928 + sizeof(uint32_t) // Friendlist size.
913 + sizeof(Friend) * m->numfriends // Friendlist itself 929 + sizeof(Friend) * m->numfriends // Friendlist itself.
914 + sizeof(uint16_t) // Own nickname length 930 + sizeof(uint16_t) // Own nickname length.
915 + m->name_length // Own nickname 931 + m->name_length // Own nickname.
916 ; 932 ;
917} 933}
918 934
919/* save the messenger in data of size Messenger_size() */ 935/* Save the messenger in data of size Messenger_size(). */
920void Messenger_save(Messenger *m, uint8_t *data) 936void Messenger_save(Messenger *m, uint8_t *data)
921{ 937{
922 save_keys(m->net_crypto, data); 938 save_keys(m->net_crypto, data);
@@ -940,7 +956,7 @@ void Messenger_save(Messenger *m, uint8_t *data)
940 memcpy(data, m->name, small_size); 956 memcpy(data, m->name, small_size);
941} 957}
942 958
943/* load the messenger from data of size length. */ 959/* Load the messenger from data of size length. */
944int Messenger_load(Messenger *m, uint8_t *data, uint32_t length) 960int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
945{ 961{
946 if (length == ~0) 962 if (length == ~0)
@@ -988,7 +1004,7 @@ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
988 setfriendname(m, fnum, temp[i].name); 1004 setfriendname(m, fnum, temp[i].name);
989 /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ 1005 /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */
990 } else if (temp[i].status != 0) { 1006 } else if (temp[i].status != 0) {
991 /* TODO: this is not a good way to do this. */ 1007 /* TODO: This is not a good way to do this. */
992 uint8_t address[FRIEND_ADDRESS_SIZE]; 1008 uint8_t address[FRIEND_ADDRESS_SIZE];
993 memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES); 1009 memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES);
994 memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t)); 1010 memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t));