diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index c3f85beb..75210830 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -560,33 +560,48 @@ int m_set_userstatus(Messenger *m, uint8_t status) return 0; } +int m_unset_avatar(Messenger *m) +{ + if (m->avatar_data != NULL) + free(m->avatar_data); + + m->avatar_data = NULL; + m->avatar_data_length = 0; + m->avatar_format = AVATAR_FORMAT_NONE; + memset(m->avatar_hash, 0, AVATAR_HASH_LENGTH); + + uint32_t i; + + for (i = 0; i < m->numfriends; ++i) + m->friendlist[i].avatar_info_sent = 0; + + return 0; +} + int m_set_avatar(Messenger *m, uint8_t format, const uint8_t *data, uint32_t length) { - if (length > AVATAR_MAX_DATA_LENGTH) + if (format == AVATAR_FORMAT_NONE) { + m_unset_avatar(m); + return 0; + } + + if (length > AVATAR_MAX_DATA_LENGTH || length == 0) return -1; - if (format == AVATAR_FORMAT_NONE) { - free(m->avatar_data); - m->avatar_data = NULL; - m->avatar_data_length = 0; - m->avatar_format = format; - memset(m->avatar_hash, 0, AVATAR_HASH_LENGTH); - } else { - if (length == 0 || data == NULL) - return -1; + if (data == NULL) + return -1; - uint8_t *tmp = realloc(m->avatar_data, length); + uint8_t *tmp = realloc(m->avatar_data, length); - if (tmp == NULL) - return -1; + if (tmp == NULL) + return -1; - m->avatar_format = format; - m->avatar_data = tmp; - m->avatar_data_length = length; - memcpy(m->avatar_data, data, length); + m->avatar_format = format; + m->avatar_data = tmp; + m->avatar_data_length = length; + memcpy(m->avatar_data, data, length); - m_avatar_hash(m->avatar_hash, m->avatar_data, m->avatar_data_length); - } + m_avatar_hash(m->avatar_hash, m->avatar_data, m->avatar_data_length); uint32_t i; diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 38543b36..4a5a5ae7 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -496,6 +496,11 @@ uint8_t m_get_self_userstatus(const Messenger *m); */ int m_set_avatar(Messenger *m, uint8_t format, const uint8_t *data, uint32_t length); +/* Unsets the user avatar. + + returns 0 on success (currently always returns 0) */ +int m_unset_avatar(Messenger *m); + /* Get avatar data from the current user. * Copies the current user avatar data to the destination buffer and sets the image format * accordingly. diff --git a/toxcore/tox.c b/toxcore/tox.c index e709ee89..19e1c0a4 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -833,6 +833,12 @@ int tox_set_avatar(Tox *tox, uint8_t format, const uint8_t *data, uint32_t lengt return m_set_avatar(m, format, data, length); } +int tox_unset_avatar(Tox *tox) +{ + Messenger *m = tox; + return m_unset_avatar(m); +} + int tox_get_self_avatar(const Tox *tox, uint8_t *format, uint8_t *buf, uint32_t *length, uint32_t maxlen, uint8_t *hash) { const Messenger *m = tox; diff --git a/toxcore/tox.h b/toxcore/tox.h index 4cde9455..ccb5a83e 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -590,6 +590,10 @@ void tox_callback_avatar_data(Tox *tox, void (*function)(Tox *tox, int32_t, uint */ int tox_set_avatar(Tox *tox, uint8_t format, const uint8_t *data, uint32_t length); +/* Unsets the user avatar. + + returns 0 on success (currently always returns 0) */ +int tox_unset_avatar(Tox *tox); /* Get avatar data from the current user. * Copies the current user avatar data to the destination buffer and sets the image format