diff options
Diffstat (limited to 'core/Messenger.c')
-rw-r--r-- | core/Messenger.c | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/core/Messenger.c b/core/Messenger.c index bf87a3e4..af4fd797 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -55,6 +55,8 @@ static Friend *friendlist; | |||
55 | static uint32_t numfriends; | 55 | static uint32_t numfriends; |
56 | 56 | ||
57 | 57 | ||
58 | static void set_friend_status(int friendnumber, uint8_t status); | ||
59 | |||
58 | /* 1 if we are online | 60 | /* 1 if we are online |
59 | 0 if we are offline | 61 | 0 if we are offline |
60 | static uint8_t online; */ | 62 | static uint8_t online; */ |
@@ -134,7 +136,7 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) | |||
134 | for (i = 0; i <= numfriends; ++i) { | 136 | for (i = 0; i <= numfriends; ++i) { |
135 | if (friendlist[i].status == NOFRIEND) { | 137 | if (friendlist[i].status == NOFRIEND) { |
136 | DHT_addfriend(client_id); | 138 | DHT_addfriend(client_id); |
137 | friendlist[i].status = FRIEND_ADDED; | 139 | set_friend_status(i, FRIEND_ADDED); |
138 | friendlist[i].crypt_connection_id = -1; | 140 | friendlist[i].crypt_connection_id = -1; |
139 | friendlist[i].friend_request_id = -1; | 141 | friendlist[i].friend_request_id = -1; |
140 | memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); | 142 | memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); |
@@ -165,7 +167,7 @@ int m_addfriend_norequest(uint8_t * client_id) | |||
165 | for (i = 0; i <= numfriends; ++i) { | 167 | for (i = 0; i <= numfriends; ++i) { |
166 | if(friendlist[i].status == NOFRIEND) { | 168 | if(friendlist[i].status == NOFRIEND) { |
167 | DHT_addfriend(client_id); | 169 | DHT_addfriend(client_id); |
168 | friendlist[i].status = FRIEND_REQUESTED; | 170 | set_friend_status(i, FRIEND_REQUESTED); |
169 | friendlist[i].crypt_connection_id = -1; | 171 | friendlist[i].crypt_connection_id = -1; |
170 | friendlist[i].friend_request_id = -1; | 172 | friendlist[i].friend_request_id = -1; |
171 | memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); | 173 | memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); |
@@ -500,6 +502,21 @@ void m_callback_read_receipt(void (*function)(int, uint32_t)) | |||
500 | read_receipt_isset = 1; | 502 | read_receipt_isset = 1; |
501 | } | 503 | } |
502 | 504 | ||
505 | static void (*friend_statuschange)(int, uint8_t); | ||
506 | static uint8_t friend_statuschange_isset = 0; | ||
507 | void m_callback_friend_status(void (*function)(int, uint8_t)) | ||
508 | { | ||
509 | friend_statuschange = function; | ||
510 | friend_statuschange_isset = 1; | ||
511 | } | ||
512 | |||
513 | static void set_friend_status(int friendnumber, uint8_t status) | ||
514 | { | ||
515 | if (friendlist[friendnumber].status != status && friend_statuschange_isset) | ||
516 | friend_statuschange(friendnumber, status); | ||
517 | friendlist[friendnumber].status = status; | ||
518 | } | ||
519 | |||
503 | #define PORT 33445 | 520 | #define PORT 33445 |
504 | /* run this at startup */ | 521 | /* run this at startup */ |
505 | int initMessenger(void) | 522 | int initMessenger(void) |
@@ -527,9 +544,9 @@ static void doFriends(void) | |||
527 | if (friendlist[i].status == FRIEND_ADDED) { | 544 | if (friendlist[i].status == FRIEND_ADDED) { |
528 | int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); | 545 | int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); |
529 | 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 */ | 546 | 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 */ |
530 | friendlist[i].status = FRIEND_REQUESTED; | 547 | set_friend_status(i, FRIEND_REQUESTED); |
531 | else if (fr > 0) | 548 | else if (fr > 0) |
532 | friendlist[i].status = FRIEND_REQUESTED; | 549 | set_friend_status(i, FRIEND_REQUESTED); |
533 | } | 550 | } |
534 | if (friendlist[i].status == FRIEND_REQUESTED || friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */ | 551 | if (friendlist[i].status == FRIEND_REQUESTED || friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */ |
535 | if (friendlist[i].status == FRIEND_REQUESTED) { | 552 | if (friendlist[i].status == FRIEND_REQUESTED) { |
@@ -545,7 +562,7 @@ static void doFriends(void) | |||
545 | friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); | 562 | friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); |
546 | break; | 563 | break; |
547 | case 3: /* Connection is established */ | 564 | case 3: /* Connection is established */ |
548 | friendlist[i].status = FRIEND_ONLINE; | 565 | set_friend_status(i, FRIEND_ONLINE); |
549 | break; | 566 | break; |
550 | case 4: | 567 | case 4: |
551 | crypto_kill(friendlist[i].crypt_connection_id); | 568 | crypto_kill(friendlist[i].crypt_connection_id); |
@@ -632,7 +649,7 @@ static void doFriends(void) | |||
632 | if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ | 649 | if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ |
633 | crypto_kill(friendlist[i].crypt_connection_id); | 650 | crypto_kill(friendlist[i].crypt_connection_id); |
634 | friendlist[i].crypt_connection_id = -1; | 651 | friendlist[i].crypt_connection_id = -1; |
635 | friendlist[i].status = FRIEND_CONFIRMED; | 652 | set_friend_status(i, FRIEND_CONFIRMED); |
636 | } | 653 | } |
637 | break; | 654 | break; |
638 | } | 655 | } |
@@ -653,7 +670,7 @@ static void doInbound(void) | |||
653 | friendlist[friend_id].crypt_connection_id = | 670 | friendlist[friend_id].crypt_connection_id = |
654 | accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); | 671 | accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); |
655 | 672 | ||
656 | friendlist[friend_id].status = FRIEND_CONFIRMED; | 673 | set_friend_status(friend_id, FRIEND_CONFIRMED); |
657 | } | 674 | } |
658 | } | 675 | } |
659 | } | 676 | } |