/* load the messenger from data of size length. */ int Messenger_load(uint8_t * data, uint32_t length) { if (length == ~0) return -1; if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2) return -1; length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2; load_keys(data); data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; uint32_t size; memcpy(&size, data, sizeof(size)); data += sizeof(size); if (length < size) return -1; length -= size; if (DHT_load(data, size) == -1) return -1; data += size; memcpy(&size, data, sizeof(size)); data += sizeof(size); if (length != size || length % sizeof(Friend) != 0) return -1; Friend * temp = malloc(size); memcpy(temp, data, size); uint16_t num = size / sizeof(Friend); uint32_t i; for (i = 0; i < num; ++i) { if(temp[i].status != 0) { int fnum = m_addfriend_norequest(temp[i].client_id); setfriendname(fnum, temp[i].name); /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ } } free(temp); return 0; }
/* 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); }
/* load the messenger from data of size length. */ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length) { if (length == ~0) return -1; if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3) return -1; length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3; load_keys(data); data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; uint32_t nospam; memcpy(&nospam, data, sizeof(nospam)); set_nospam(nospam); data += sizeof(nospam); uint32_t size; memcpy(&size, data, sizeof(size)); data += sizeof(size); if (length < size) return -1; length -= size; if (DHT_load(data, size) == -1) return -1; data += size; memcpy(&size, data, sizeof(size)); data += sizeof(size); if (length < size || size % sizeof(Friend) != 0) return -1; Friend *temp = malloc(size); memcpy(temp, data, size); uint16_t num = size / sizeof(Friend); uint32_t i; for (i = 0; i < num; ++i) { if (temp[i].status >= 3) { int fnum = m_addfriend_norequest(m, temp[i].client_id); setfriendname(m, fnum, temp[i].name); /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ } else if (temp[i].status != 0) { /* TODO: this is not a good way to do this. */ uint8_t address[FRIEND_ADDRESS_SIZE]; memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES); memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t)); uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum)); m_addfriend(m, address, temp[i].info, temp[i].info_size); } } free(temp); data += size; length -= size; uint16_t small_size; if (length < sizeof(small_size)) return -1; memcpy(&small_size, data, sizeof(small_size)); data += sizeof(small_size); length -= sizeof(small_size); if (length != small_size) return -1; setname(m, data, small_size); return 0; }