diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 3cc2f851..a1812c69 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -706,9 +706,9 @@ uint8_t m_get_self_userstatus(const Messenger *m) uint64_t m_get_last_online(const Messenger *m, int32_t friendnumber) { if (friend_not_valid(m, friendnumber)) - return -1; + return UINT64_MAX; - return m->friendlist[friendnumber].ping_lastrecv; + return m->friendlist[friendnumber].last_seen_time; } int m_set_usertyping(Messenger *m, int32_t friendnumber, uint8_t is_typing) @@ -1890,7 +1890,6 @@ static void check_friend_request_timed_out(Messenger *m, uint32_t i, uint64_t t) static int handle_status(void *object, int i, uint8_t status) { - uint64_t temp_time = unix_time(); Messenger *m = object; if (status) { /* Went online. */ @@ -1899,7 +1898,6 @@ static int handle_status(void *object, int i, uint8_t status) m->friendlist[i].userstatus_sent = 0; m->friendlist[i].statusmessage_sent = 0; m->friendlist[i].user_istyping_sent = 0; - m->friendlist[i].ping_lastrecv = temp_time; } else { /* Went offline. */ if (m->friendlist[i].status == FRIEND_ONLINE) { set_friend_status(m, i, FRIEND_CONFIRMED); @@ -2244,6 +2242,8 @@ void do_friends(Messenger *m) check_friend_tcp_udp(m, i); do_receipts(m, i); do_reqchunk_filecb(m, i); + + m->friendlist[i].last_seen_time = (uint64_t) time(NULL); } } } @@ -2384,7 +2384,6 @@ void do_messenger(Messenger *m) LOGGER_TRACE("Friend num in DHT %u != friend num in msger %u\n", m->dht->num_friends, m->numfriends); } - uint32_t ping_lastrecv; Friend *msgfptr; DHT_Friend *dhtfptr; @@ -2397,14 +2396,9 @@ void do_messenger(Messenger *m) dhtfptr = &m->dht->friends_list[friend]; if (msgfptr) { - ping_lastrecv = lastdump - msgfptr->ping_lastrecv; - - if (ping_lastrecv > 999) - ping_lastrecv = 999; - - LOGGER_TRACE("F[%2u:%2u] <%s> [%03u] %s", + LOGGER_TRACE("F[%2u:%2u] <%s> %s", dht2m[friend], friend, msgfptr->name, - ping_lastrecv, ID2String(msgfptr->real_pk)); + ID2String(msgfptr->real_pk)); } else { LOGGER_TRACE("F[--:%2u] %s", friend, ID2String(dhtfptr->client_id)); } @@ -2460,7 +2454,7 @@ struct SAVED_FRIEND { uint16_t statusmessage_length; uint8_t userstatus; uint32_t friendrequest_nospam; - uint64_t ping_lastrecv; + uint64_t last_seen_time; }; static uint32_t saved_friendslist_size(const Messenger *m) @@ -2496,10 +2490,10 @@ static uint32_t friends_list_save(const Messenger *m, uint8_t *data) temp.statusmessage_length = htons(m->friendlist[i].statusmessage_length); temp.userstatus = m->friendlist[i].userstatus; - uint8_t lastonline[sizeof(uint64_t)]; - memcpy(lastonline, &m->friendlist[i].ping_lastrecv, sizeof(uint64_t)); - host_to_net(lastonline, sizeof(uint64_t)); - memcpy(&temp.ping_lastrecv, lastonline, sizeof(uint64_t)); + uint8_t last_seen_time[sizeof(uint64_t)]; + memcpy(last_seen_time, &m->friendlist[i].last_seen_time, sizeof(uint64_t)); + host_to_net(last_seen_time, sizeof(uint64_t)); + memcpy(&temp.last_seen_time, last_seen_time, sizeof(uint64_t)); } memcpy(data + num * sizeof(struct SAVED_FRIEND), &temp, sizeof(struct SAVED_FRIEND)); @@ -2532,10 +2526,10 @@ static int friends_list_load(Messenger *m, const uint8_t *data, uint32_t length) setfriendname(m, fnum, temp.name, ntohs(temp.name_length)); set_friend_statusmessage(m, fnum, temp.statusmessage, ntohs(temp.statusmessage_length)); set_friend_userstatus(m, fnum, temp.userstatus); - uint8_t lastonline[sizeof(uint64_t)]; - memcpy(lastonline, &temp.ping_lastrecv, sizeof(uint64_t)); - net_to_host(lastonline, sizeof(uint64_t)); - memcpy(&m->friendlist[fnum].ping_lastrecv, lastonline, sizeof(uint64_t)); + uint8_t last_seen_time[sizeof(uint64_t)]; + memcpy(last_seen_time, &temp.last_seen_time, sizeof(uint64_t)); + net_to_host(last_seen_time, sizeof(uint64_t)); + memcpy(&m->friendlist[fnum].last_seen_time, last_seen_time, sizeof(uint64_t)); } else if (temp.status != 0) { /* TODO: This is not a good way to do this. */ uint8_t address[FRIEND_ADDRESS_SIZE]; diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index d2a992f3..63fb2820 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -198,7 +198,7 @@ typedef struct { uint16_t info_size; // Length of the info. uint32_t message_id; // a semi-unique id used in read receipts. uint32_t friendrequest_nospam; // The nospam number used in the friend request. - uint64_t ping_lastrecv;//TODO remove + uint64_t last_seen_time; uint64_t share_relays_lastsent; uint8_t last_connection_udp_tcp; struct File_Transfers file_sending[MAX_CONCURRENT_FILE_PIPES]; @@ -460,8 +460,8 @@ uint8_t m_get_userstatus(const Messenger *m, int32_t friendnumber); uint8_t m_get_self_userstatus(const Messenger *m); -/* returns timestamp of last time friendnumber was seen online, or 0 if never seen. - * returns -1 on error. +/* returns timestamp of last time friendnumber was seen online or 0 if never seen. + * if friendnumber is invalid this function will return UINT64_MAX. */ uint64_t m_get_last_online(const Messenger *m, int32_t friendnumber); diff --git a/toxcore/tox.c b/toxcore/tox.c index 403ba83f..a9306efc 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -599,6 +599,20 @@ bool tox_friend_exists(const Tox *tox, uint32_t friend_number) return m_friend_exists(m, friend_number); } +uint64_t tox_friend_get_last_online(const Tox *tox, uint32_t friend_number, TOX_ERR_FRIEND_GET_LAST_ONLINE *error) +{ + const Messenger *m = tox; + uint64_t timestamp = m_get_last_online(m, friend_number); + + if (timestamp == UINT64_MAX) { + SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_GET_LAST_ONLINE_FRIEND_NOT_FOUND) + return UINT64_MAX; + } + + SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_GET_LAST_ONLINE_OK); + return timestamp; +} + size_t tox_self_get_friend_list_size(const Tox *tox) { const Messenger *m = tox; diff --git a/toxcore/tox.h b/toxcore/tox.h index 8dc49d0b..034b1d78 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -996,6 +996,21 @@ bool tox_friend_get_public_key(const Tox *tox, uint32_t friend_number, uint8_t * */ bool tox_friend_exists(const Tox *tox, uint32_t friend_number); +typedef enum TOX_ERR_FRIEND_GET_LAST_ONLINE { + TOX_ERR_FRIEND_GET_LAST_ONLINE_OK, + /** + * No friend with the given number exists on the friend list. + */ + TOX_ERR_FRIEND_GET_LAST_ONLINE_FRIEND_NOT_FOUND, +} TOX_ERR_FRIEND_GET_LAST_ONLINE; + +/** + * Return a unix-time timestamp of the last time the friend associated with a given + * friend number was seen online. This function will return UINT64_MAX on error. + * + * @param friend_number The friend number you want to query. + */ +uint64_t tox_friend_get_last_online(const Tox *tox, uint32_t friend_number, TOX_ERR_FRIEND_GET_LAST_ONLINE *error); /** * Return the number of friends on the friend list.