diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index c1301767..3b09baa2 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -273,6 +273,9 @@ int m_delfriend(Messenger *m, int friendnumber) if (friend_not_valid(m, friendnumber)) return -1; + if (m->friendlist[friendnumber].status == FRIEND_ONLINE) + --m->numonline_friends; + onion_delfriend(m->onion_c, m->friendlist[friendnumber].onion_friendnum); crypto_kill(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id); free(m->friendlist[friendnumber].statusmessage); @@ -665,8 +668,12 @@ static void check_friend_connectionstatus(Messenger *m, int friendnumber, uint8_ onion_set_friend_online(m->onion_c, m->friendlist[friendnumber].onion_friendnum, is_online); if (is_online != was_online) { - if (was_online) + if (was_online) { break_files(m, friendnumber); + --m->numonline_friends; + } else { + ++m->numonline_friends; + } m->friend_connectionstatuschange(m, friendnumber, is_online, m->friend_connectionstatuschange_userdata); } @@ -2360,6 +2367,12 @@ uint32_t count_friendlist(Messenger *m) return ret; } +/* Return the number of online friends in the instance m. */ +uint32_t get_num_online_friends(Messenger *m) +{ + return m->numonline_friends; +} + /* Copy a list of valid friend IDs into the array out_list. * If out_list is NULL, returns 0. * Otherwise, returns the number of elements copied. diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index e09b2f30..40e857d1 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -178,6 +178,7 @@ typedef struct Messenger { Friend *friendlist; uint32_t numfriends; + uint32_t numonline_friends; Group_Chat **chats; uint32_t numchats; @@ -682,6 +683,9 @@ int messenger_load_encrypted(Messenger *m, uint8_t *data, uint32_t length, uint8 * for copy_friendlist. */ uint32_t count_friendlist(Messenger *m); +/* Return the number of online friends in the instance m. */ +uint32_t get_num_online_friends(Messenger *m); + /* Copy a list of valid friend IDs into the array out_list. * If out_list is NULL, returns 0. * Otherwise, returns the number of elements copied. diff --git a/toxcore/tox.c b/toxcore/tox.c index 04e412be..f4690080 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -289,6 +289,13 @@ uint32_t tox_count_friendlist(Tox *tox) return count_friendlist(m); } +/* Return the number of online friends in the instance m. */ +uint32_t tox_get_num_online_friends(Tox *tox) +{ + Messenger *m = tox; + return get_num_online_friends(m); +} + /* Copy a list of valid friend IDs into the array out_list. * If out_list is NULL, returns 0. * Otherwise, returns the number of elements copied. diff --git a/toxcore/tox.h b/toxcore/tox.h index f3118270..0597c435 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -284,6 +284,9 @@ void tox_set_sends_receipts(Tox *tox, int friendnumber, int yesno); * for copy_friendlist. */ uint32_t tox_count_friendlist(Tox *tox); +/* Return the number of online friends in the instance m. */ +uint32_t tox_get_num_online_friends(Tox *tox); + /* Copy a list of valid friend IDs into the array out_list. * If out_list is NULL, returns 0. * Otherwise, returns the number of elements copied.