diff options
Diffstat (limited to 'core/Messenger.c')
-rw-r--r-- | core/Messenger.c | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/core/Messenger.c b/core/Messenger.c index 3d991e63..29cd9252 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -57,6 +57,8 @@ static Friend friendlist[MAX_NUM_FRIENDS]; | |||
57 | 57 | ||
58 | static uint32_t numfriends; | 58 | static uint32_t numfriends; |
59 | 59 | ||
60 | static void set_friend_status(int friendnumber, uint8_t status); | ||
61 | |||
60 | /* 1 if we are online | 62 | /* 1 if we are online |
61 | 0 if we are offline | 63 | 0 if we are offline |
62 | static uint8_t online; */ | 64 | static uint8_t online; */ |
@@ -122,7 +124,7 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) | |||
122 | for (i = 0; i <= numfriends && i <= MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */ | 124 | for (i = 0; i <= numfriends && i <= MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */ |
123 | if (friendlist[i].status == NOFRIEND) { | 125 | if (friendlist[i].status == NOFRIEND) { |
124 | DHT_addfriend(client_id); | 126 | DHT_addfriend(client_id); |
125 | friendlist[i].status = FRIEND_ADDED; | 127 | set_friend_status(i, FRIEND_ADDED); |
126 | friendlist[i].crypt_connection_id = -1; | 128 | friendlist[i].crypt_connection_id = -1; |
127 | friendlist[i].friend_request_id = -1; | 129 | friendlist[i].friend_request_id = -1; |
128 | memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); | 130 | memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); |
@@ -149,7 +151,7 @@ int m_addfriend_norequest(uint8_t * client_id) | |||
149 | for (i = 0; i <= numfriends && i <= MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */ | 151 | for (i = 0; i <= numfriends && i <= MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */ |
150 | if(friendlist[i].status == NOFRIEND) { | 152 | if(friendlist[i].status == NOFRIEND) { |
151 | DHT_addfriend(client_id); | 153 | DHT_addfriend(client_id); |
152 | friendlist[i].status = FRIEND_REQUESTED; | 154 | set_friend_status(i, FRIEND_REQUESTED); |
153 | friendlist[i].crypt_connection_id = -1; | 155 | friendlist[i].crypt_connection_id = -1; |
154 | friendlist[i].friend_request_id = -1; | 156 | friendlist[i].friend_request_id = -1; |
155 | memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); | 157 | memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); |
@@ -460,6 +462,23 @@ void m_callback_read_receipt(void (*function)(int, uint32_t)) | |||
460 | read_receipt_isset = 1; | 462 | read_receipt_isset = 1; |
461 | } | 463 | } |
462 | 464 | ||
465 | static void (*friend_status)(int, uint8_t); | ||
466 | static uint8_t friend_status_isset = 0; | ||
467 | void m_callback_friend_status(void (*function)(int, uint8_t)) | ||
468 | { | ||
469 | friend_status = function; | ||
470 | friend_status_isset = 1; | ||
471 | } | ||
472 | |||
473 | static void set_friend_status(int friendnumber, uint8_t status) | ||
474 | { | ||
475 | friendlist[friendnumber].status = status; | ||
476 | if (friendlist[friendnumber].status == status) | ||
477 | return; | ||
478 | if (friend_status_isset) | ||
479 | friend_status(friendnumber, status); | ||
480 | } | ||
481 | |||
463 | #define PORT 33445 | 482 | #define PORT 33445 |
464 | /* run this at startup */ | 483 | /* run this at startup */ |
465 | int initMessenger(void) | 484 | int initMessenger(void) |
@@ -487,9 +506,9 @@ static void doFriends(void) | |||
487 | if (friendlist[i].status == FRIEND_ADDED) { | 506 | if (friendlist[i].status == FRIEND_ADDED) { |
488 | int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); | 507 | int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); |
489 | if (fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case of packet loss */ | 508 | if (fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case of packet loss */ |
490 | friendlist[i].status = FRIEND_REQUESTED; | 509 | set_friend_status(i, FRIEND_REQUESTED); |
491 | else if (fr > 0) | 510 | else if (fr > 0) |
492 | friendlist[i].status = FRIEND_REQUESTED; | 511 | set_friend_status(i, FRIEND_REQUESTED); |
493 | } | 512 | } |
494 | if (friendlist[i].status == FRIEND_REQUESTED || friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */ | 513 | if (friendlist[i].status == FRIEND_REQUESTED || friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */ |
495 | if (friendlist[i].status == FRIEND_REQUESTED) { | 514 | if (friendlist[i].status == FRIEND_REQUESTED) { |
@@ -505,7 +524,7 @@ static void doFriends(void) | |||
505 | friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); | 524 | friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); |
506 | break; | 525 | break; |
507 | case 3: /* Connection is established */ | 526 | case 3: /* Connection is established */ |
508 | friendlist[i].status = FRIEND_ONLINE; | 527 | set_friend_status(i, FRIEND_ONLINE); |
509 | break; | 528 | break; |
510 | case 4: | 529 | case 4: |
511 | crypto_kill(friendlist[i].crypt_connection_id); | 530 | crypto_kill(friendlist[i].crypt_connection_id); |
@@ -587,7 +606,7 @@ static void doFriends(void) | |||
587 | if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ | 606 | if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ |
588 | crypto_kill(friendlist[i].crypt_connection_id); | 607 | crypto_kill(friendlist[i].crypt_connection_id); |
589 | friendlist[i].crypt_connection_id = -1; | 608 | friendlist[i].crypt_connection_id = -1; |
590 | friendlist[i].status = FRIEND_CONFIRMED; | 609 | set_friend_status(i, FRIEND_CONFIRMED); |
591 | } | 610 | } |
592 | break; | 611 | break; |
593 | } | 612 | } |
@@ -608,7 +627,7 @@ static void doInbound(void) | |||
608 | friendlist[friend_id].crypt_connection_id = | 627 | friendlist[friend_id].crypt_connection_id = |
609 | accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); | 628 | accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); |
610 | 629 | ||
611 | friendlist[friend_id].status = FRIEND_CONFIRMED; | 630 | set_friend_status(friend_id, FRIEND_CONFIRMED); |
612 | } | 631 | } |
613 | } | 632 | } |
614 | } | 633 | } |