diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index d8cd950d..f125c264 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -352,13 +352,16 @@ static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t le * return 0 if success. * return -1 if failure. */ -static int setfriendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t len) +int setfriendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t length) { if (friend_not_valid(m, friendnumber)) return -1; - m->friendlist[friendnumber].name_length = len; - memcpy(m->friendlist[friendnumber].name, name, len); + if (length > MAX_NAME_LENGTH || length == 0) + return -1; + + m->friendlist[friendnumber].name_length = length; + memcpy(m->friendlist[friendnumber].name, name, length); return 0; } diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 2b29896c..0ff14de4 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -246,6 +246,16 @@ uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, ui */ int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length); +/* Set the name and name_length of a friend. + * name must be a string of maximum MAX_NAME_LENGTH length. + * length must be at least 1 byte. + * length is the length of name with the NULL terminator. + * + * return 0 if success. + * return -1 if failure. + */ +int setfriendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t length); + /* Set our nickname. * name must be a string of maximum MAX_NAME_LENGTH length. * length must be at least 1 byte. diff --git a/toxcore/tox.c b/toxcore/tox.c index fa53e693..1d7118be 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -153,6 +153,20 @@ int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length return m_sendaction(m, friendnumber, action, length); } +/* Set friendnumber's nickname. + * name must be a string of maximum MAX_NAME_LENGTH length. + * length must be at least 1 byte. + * length is the length of name with the NULL terminator. + * + * return 0 if success. + * return -1 if failure. + */ +int tox_setfriendname(void *tox, int friendnumber, uint8_t *name, uint16_t length) +{ + Messenger *m = tox; + return setfriendname(m, friendnumber, name, length); +} + /* Set our nickname. * name must be a string of maximum MAX_NAME_LENGTH length. * length must be at least 1 byte. diff --git a/toxcore/tox.h b/toxcore/tox.h index 9c810418..db3621dc 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -214,6 +214,16 @@ uint32_t tox_sendmessage_withid(Tox *tox, int friendnumber, uint32_t theid, uint */ int tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length); +/* Set friendnumber's nickname. + * name must be a string of maximum MAX_NAME_LENGTH length. + * length must be at least 1 byte. + * length is the length of name with the NULL terminator. + * + * return 0 if success. + * return -1 if failure. + */ +int tox_setfriendname(void *tox, int friendnumber, uint8_t *name, uint16_t length); + /* Set our nickname. * name must be a string of maximum MAX_NAME_LENGTH length. * length must be at least 1 byte.