2013-07-07 10:28:15 +08:00
|
|
|
/* Messenger.c
|
2013-07-26 09:45:56 +08:00
|
|
|
*
|
|
|
|
* An implementation of a simple text chat only messenger on the tox network core.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Tox project All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Tox.
|
|
|
|
*
|
|
|
|
* Tox is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tox is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
2013-07-27 20:43:36 +08:00
|
|
|
*
|
2013-07-26 09:45:56 +08:00
|
|
|
*/
|
2013-07-07 10:28:15 +08:00
|
|
|
|
|
|
|
#include "Messenger.h"
|
2013-08-12 21:19:25 +08:00
|
|
|
|
2013-07-19 01:56:50 +08:00
|
|
|
#define MIN(a,b) (((a)<(b))?(a):(b))
|
2013-07-27 20:43:36 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
static void set_friend_status(Messenger *m, int friendnumber, uint8_t status);
|
|
|
|
static int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length);
|
2013-08-08 13:46:27 +08:00
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* 1 if we are online
|
|
|
|
0 if we are offline
|
|
|
|
static uint8_t online; */
|
2013-07-17 07:02:44 +08:00
|
|
|
|
2013-08-09 00:13:29 +08:00
|
|
|
/* set the size of the friend list to numfriends
|
2013-08-08 22:49:36 +08:00
|
|
|
return -1 if realloc fails */
|
2013-08-17 01:11:09 +08:00
|
|
|
int realloc_friendlist(Messenger *m, uint32_t num)
|
|
|
|
{
|
2013-08-18 22:27:03 +08:00
|
|
|
if (num == 0) {
|
|
|
|
free(m->friendlist);
|
|
|
|
m->friendlist = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
2013-08-18 16:48:36 +08:00
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
Friend *newfriendlist = realloc(m->friendlist, num * sizeof(Friend));
|
2013-08-18 16:48:36 +08:00
|
|
|
|
2013-08-18 22:27:03 +08:00
|
|
|
if (newfriendlist == NULL)
|
2013-08-08 22:49:36 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friendlist = newfriendlist;
|
2013-08-08 22:49:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* return the friend id associated to that public key.
|
|
|
|
return -1 if no such friend */
|
2013-08-11 11:24:11 +08:00
|
|
|
int getfriend_id(Messenger *m, uint8_t *client_id)
|
2013-07-10 08:25:52 +08:00
|
|
|
{
|
|
|
|
uint32_t i;
|
2013-07-27 06:17:24 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
for (i = 0; i < m->numfriends; ++i) {
|
|
|
|
if (m->friendlist[i].status > 0)
|
|
|
|
if (memcmp(client_id, m->friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0)
|
2013-07-10 08:25:52 +08:00
|
|
|
return i;
|
2013-07-27 11:07:25 +08:00
|
|
|
}
|
2013-07-27 06:17:24 +08:00
|
|
|
|
2013-07-10 08:25:52 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* copies the public key associated to that friend id into client_id buffer.
|
|
|
|
make sure that client_id is of size CLIENT_ID_SIZE.
|
|
|
|
return 0 if success
|
|
|
|
return -1 if failure. */
|
2013-08-11 11:24:11 +08:00
|
|
|
int getclient_id(Messenger *m, int friend_id, uint8_t *client_id)
|
2013-07-14 05:44:43 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
if (friend_id >= m->numfriends || friend_id < 0)
|
2013-07-14 05:44:43 +08:00
|
|
|
return -1;
|
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
if (m->friendlist[friend_id].status > 0) {
|
|
|
|
memcpy(client_id, m->friendlist[friend_id].client_id, CLIENT_ID_SIZE);
|
2013-07-14 05:44:43 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-07-27 06:17:24 +08:00
|
|
|
|
2013-07-14 05:44:43 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2013-08-14 13:27:10 +08:00
|
|
|
/*
|
2013-08-14 07:07:59 +08:00
|
|
|
* returns a uint16_t that represents the checksum of address of length len
|
2013-08-14 13:27:10 +08:00
|
|
|
*
|
2013-08-14 07:07:59 +08:00
|
|
|
* TODO: Another checksum algorithm might be better.
|
|
|
|
*/
|
|
|
|
static uint16_t address_checksum(uint8_t *address, uint32_t len)
|
|
|
|
{
|
|
|
|
uint8_t checksum[2] = {0};
|
|
|
|
uint16_t check;
|
|
|
|
uint32_t i;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
for (i = 0; i < len; ++i)
|
2013-08-14 07:07:59 +08:00
|
|
|
checksum[i % 2] ^= address[i];
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-14 07:07:59 +08:00
|
|
|
memcpy(&check, checksum, sizeof(check));
|
|
|
|
return check;
|
|
|
|
}
|
2013-07-14 05:44:43 +08:00
|
|
|
|
2013-08-13 21:32:31 +08:00
|
|
|
/*
|
|
|
|
* returns a FRIEND_ADDRESS_SIZE byte address to give to others.
|
|
|
|
* format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
|
2013-08-14 13:27:10 +08:00
|
|
|
*
|
2013-08-13 21:32:31 +08:00
|
|
|
*/
|
|
|
|
void getaddress(Messenger *m, uint8_t *address)
|
|
|
|
{
|
2013-08-21 00:08:55 +08:00
|
|
|
memcpy(address, m->net_crypto->self_public_key, crypto_box_PUBLICKEYBYTES);
|
|
|
|
uint32_t nospam = get_nospam(&(m->fr));
|
2013-08-13 21:32:31 +08:00
|
|
|
memcpy(address + crypto_box_PUBLICKEYBYTES, &nospam, sizeof(nospam));
|
2013-08-14 07:07:59 +08:00
|
|
|
uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
|
|
|
|
memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(nospam), &checksum, sizeof(checksum));
|
2013-08-13 21:32:31 +08:00
|
|
|
}
|
|
|
|
|
2013-08-02 03:27:08 +08:00
|
|
|
/*
|
|
|
|
* add a friend
|
|
|
|
* set the data that will be sent along with friend request
|
2013-08-14 06:30:14 +08:00
|
|
|
* address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum.
|
2013-08-02 03:27:08 +08:00
|
|
|
* data is the data and length is the length
|
|
|
|
* returns the friend number if success
|
2013-08-03 00:35:34 +08:00
|
|
|
* return FA_TOOLONG if message length is too long
|
|
|
|
* return FAERR_NOMESSAGE if no message (message length must be >= 1 byte)
|
|
|
|
* return FAERR_OWNKEY if user's own key
|
|
|
|
* return FAERR_ALREADYSENT if friend request already sent or already a friend
|
2013-08-06 03:30:07 +08:00
|
|
|
* return FAERR_UNKNOWN for unknown error
|
2013-08-14 07:07:59 +08:00
|
|
|
* return FAERR_BADCHECKSUM if bad checksum in address
|
2013-08-14 13:27:10 +08:00
|
|
|
* return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different
|
2013-08-14 07:07:59 +08:00
|
|
|
* (the nospam for that friend was set to the new one)
|
2013-08-15 21:34:27 +08:00
|
|
|
* return FAERR_NOMEM if increasing the friend list size fails
|
2013-08-02 03:27:08 +08:00
|
|
|
*/
|
2013-08-13 21:32:31 +08:00
|
|
|
int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2013-08-06 03:30:07 +08:00
|
|
|
if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES
|
2013-08-17 01:11:09 +08:00
|
|
|
- crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES
|
|
|
|
+ crypto_box_ZEROBYTES))
|
2013-08-03 00:33:11 +08:00
|
|
|
return FAERR_TOOLONG;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-13 21:32:31 +08:00
|
|
|
uint8_t client_id[crypto_box_PUBLICKEYBYTES];
|
|
|
|
memcpy(client_id, address, crypto_box_PUBLICKEYBYTES);
|
2013-08-14 07:07:59 +08:00
|
|
|
uint16_t check, checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum));
|
|
|
|
memcpy(&check, address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), sizeof(check));
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-14 07:07:59 +08:00
|
|
|
if (check != checksum)
|
|
|
|
return FAERR_BADCHECKSUM;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-02 03:27:08 +08:00
|
|
|
if (length < 1)
|
2013-08-03 00:33:11 +08:00
|
|
|
return FAERR_NOMESSAGE;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-21 00:08:55 +08:00
|
|
|
if (memcmp(client_id, m->net_crypto->self_public_key, crypto_box_PUBLICKEYBYTES) == 0)
|
2013-08-03 00:33:11 +08:00
|
|
|
return FAERR_OWNKEY;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-14 05:25:52 +08:00
|
|
|
int friend_id = getfriend_id(m, client_id);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-14 05:25:52 +08:00
|
|
|
if (friend_id != -1) {
|
|
|
|
uint32_t nospam;
|
2013-08-14 07:07:59 +08:00
|
|
|
memcpy(&nospam, address + crypto_box_PUBLICKEYBYTES, sizeof(nospam));
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
if (m->friendlist[friend_id].friendrequest_nospam == nospam)
|
2013-08-14 05:25:52 +08:00
|
|
|
return FAERR_ALREADYSENT;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-14 05:25:52 +08:00
|
|
|
m->friendlist[friend_id].friendrequest_nospam = nospam;
|
2013-08-14 07:07:59 +08:00
|
|
|
return FAERR_SETNEWNOSPAM;
|
2013-08-14 05:25:52 +08:00
|
|
|
}
|
2013-08-02 03:27:08 +08:00
|
|
|
|
2013-08-08 22:49:36 +08:00
|
|
|
/* resize the friend list if necessary */
|
2013-08-15 21:34:27 +08:00
|
|
|
if (realloc_friendlist(m, m->numfriends + 1) != 0)
|
|
|
|
return FAERR_NOMEM;
|
2013-08-08 22:49:36 +08:00
|
|
|
|
2013-08-17 23:16:08 +08:00
|
|
|
memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend));
|
|
|
|
|
2013-07-10 08:25:52 +08:00
|
|
|
uint32_t i;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
for (i = 0; i <= m->numfriends; ++i) {
|
|
|
|
if (m->friendlist[i].status == NOFRIEND) {
|
2013-08-21 00:08:55 +08:00
|
|
|
DHT_addfriend(m->dht, client_id);
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friendlist[i].status = FRIEND_ADDED;
|
|
|
|
m->friendlist[i].crypt_connection_id = -1;
|
2013-08-17 06:57:00 +08:00
|
|
|
m->friendlist[i].friendrequest_lastsent = 0;
|
|
|
|
m->friendlist[i].friendrequest_timeout = FRIENDREQUEST_TIMEOUT;
|
2013-08-11 11:24:11 +08:00
|
|
|
memcpy(m->friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
|
|
|
|
m->friendlist[i].statusmessage = calloc(1, 1);
|
|
|
|
m->friendlist[i].statusmessage_length = 1;
|
|
|
|
m->friendlist[i].userstatus = USERSTATUS_NONE;
|
|
|
|
memcpy(m->friendlist[i].info, data, length);
|
|
|
|
m->friendlist[i].info_size = length;
|
|
|
|
m->friendlist[i].message_id = 0;
|
|
|
|
m->friendlist[i].receives_read_receipts = 1; /* default: YES */
|
2013-08-13 21:32:31 +08:00
|
|
|
memcpy(&(m->friendlist[i].friendrequest_nospam), address + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t));
|
2013-08-11 11:24:11 +08:00
|
|
|
|
2013-08-17 23:16:08 +08:00
|
|
|
if (m->numfriends == i)
|
|
|
|
++ m->numfriends;
|
|
|
|
|
2013-07-10 08:25:52 +08:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-03 00:35:34 +08:00
|
|
|
return FAERR_UNKNOWN;
|
2013-07-07 10:28:15 +08:00
|
|
|
}
|
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
int m_addfriend_norequest(Messenger *m, uint8_t *client_id)
|
2013-07-10 01:20:48 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
if (getfriend_id(m, client_id) != -1)
|
2013-07-10 08:25:52 +08:00
|
|
|
return -1;
|
2013-08-08 22:49:36 +08:00
|
|
|
|
|
|
|
/* resize the friend list if necessary */
|
2013-08-15 21:34:27 +08:00
|
|
|
if (realloc_friendlist(m, m->numfriends + 1) != 0)
|
|
|
|
return FAERR_NOMEM;
|
2013-08-08 22:49:36 +08:00
|
|
|
|
2013-08-17 23:16:08 +08:00
|
|
|
memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend));
|
|
|
|
|
2013-07-10 08:25:52 +08:00
|
|
|
uint32_t i;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
for (i = 0; i <= m->numfriends; ++i) {
|
2013-08-17 01:11:09 +08:00
|
|
|
if (m->friendlist[i].status == NOFRIEND) {
|
2013-08-21 00:08:55 +08:00
|
|
|
DHT_addfriend(m->dht, client_id);
|
2013-08-17 06:57:00 +08:00
|
|
|
m->friendlist[i].status = FRIEND_CONFIRMED;
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friendlist[i].crypt_connection_id = -1;
|
2013-08-17 06:57:00 +08:00
|
|
|
m->friendlist[i].friendrequest_lastsent = 0;
|
2013-08-11 11:24:11 +08:00
|
|
|
memcpy(m->friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
|
|
|
|
m->friendlist[i].statusmessage = calloc(1, 1);
|
|
|
|
m->friendlist[i].statusmessage_length = 1;
|
|
|
|
m->friendlist[i].userstatus = USERSTATUS_NONE;
|
|
|
|
m->friendlist[i].message_id = 0;
|
|
|
|
m->friendlist[i].receives_read_receipts = 1; /* default: YES */
|
2013-08-17 23:16:08 +08:00
|
|
|
|
|
|
|
if (m->numfriends == i)
|
|
|
|
++ m->numfriends;
|
|
|
|
|
2013-07-10 08:25:52 +08:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-10 08:25:52 +08:00
|
|
|
return -1;
|
2013-07-10 01:20:48 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* remove a friend
|
|
|
|
return 0 if success
|
|
|
|
return -1 if failure */
|
2013-08-11 11:24:11 +08:00
|
|
|
int m_delfriend(Messenger *m, int friendnumber)
|
2013-07-10 08:25:52 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
if (friendnumber >= m->numfriends || friendnumber < 0)
|
2013-07-10 08:25:52 +08:00
|
|
|
return -1;
|
|
|
|
|
2013-08-21 00:08:55 +08:00
|
|
|
DHT_delfriend(m->dht, m->friendlist[friendnumber].client_id);
|
2013-08-20 11:54:28 +08:00
|
|
|
crypto_kill(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id);
|
2013-08-11 11:24:11 +08:00
|
|
|
free(m->friendlist[friendnumber].statusmessage);
|
|
|
|
memset(&(m->friendlist[friendnumber]), 0, sizeof(Friend));
|
2013-07-10 08:25:52 +08:00
|
|
|
uint32_t i;
|
2013-08-01 06:01:59 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
for (i = m->numfriends; i != 0; --i) {
|
2013-08-17 01:11:09 +08:00
|
|
|
if (m->friendlist[i - 1].status != NOFRIEND)
|
2013-07-10 08:25:52 +08:00
|
|
|
break;
|
2013-07-27 11:07:25 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
m->numfriends = i;
|
2013-08-15 21:34:27 +08:00
|
|
|
|
2013-08-17 23:16:08 +08:00
|
|
|
if (realloc_friendlist(m, m->numfriends) != 0)
|
2013-08-15 21:34:27 +08:00
|
|
|
return FAERR_NOMEM;
|
2013-08-01 06:01:59 +08:00
|
|
|
|
2013-07-10 08:25:52 +08:00
|
|
|
return 0;
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|
|
|
|
|
2013-08-03 00:35:34 +08:00
|
|
|
/* return FRIEND_ONLINE if friend is online
|
|
|
|
return FRIEND_CONFIRMED if friend is confirmed
|
|
|
|
return FRIEND_REQUESTED if the friend request was sent
|
|
|
|
return FRIEND_ADDED if the friend was added
|
|
|
|
return NOFRIEND if there is no friend with that number */
|
2013-08-11 11:24:11 +08:00
|
|
|
int m_friendstatus(Messenger *m, int friendnumber)
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
if (friendnumber < 0 || friendnumber >= m->numfriends)
|
2013-08-03 00:33:11 +08:00
|
|
|
return NOFRIEND;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
return m->friendlist[friendnumber].status;
|
2013-07-07 10:28:15 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* send a text chat message to an online friend
|
2013-08-08 00:53:52 +08:00
|
|
|
return the message id if packet was successfully put into the send queue
|
2013-07-20 11:00:10 +08:00
|
|
|
return 0 if it was not */
|
2013-08-11 11:24:11 +08:00
|
|
|
uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length)
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
if (friendnumber < 0 || friendnumber >= m->numfriends)
|
2013-07-10 01:20:48 +08:00
|
|
|
return 0;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
uint32_t msgid = ++m->friendlist[friendnumber].message_id;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-08 01:06:07 +08:00
|
|
|
if (msgid == 0)
|
|
|
|
msgid = 1; /* otherwise, false error */
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
if (m_sendmessage_withid(m, friendnumber, msgid, message, length)) {
|
2013-08-11 20:15:07 +08:00
|
|
|
return msgid;
|
|
|
|
}
|
2013-08-11 11:24:11 +08:00
|
|
|
|
2013-08-11 20:15:07 +08:00
|
|
|
return 0;
|
2013-08-08 00:53:52 +08:00
|
|
|
}
|
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length)
|
2013-08-08 00:53:52 +08:00
|
|
|
{
|
2013-08-10 07:16:51 +08:00
|
|
|
if (length >= (MAX_DATA_SIZE - sizeof(theid)))
|
|
|
|
return 0;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-09 08:50:25 +08:00
|
|
|
uint8_t temp[MAX_DATA_SIZE];
|
2013-08-08 06:14:35 +08:00
|
|
|
theid = htonl(theid);
|
2013-08-10 03:58:08 +08:00
|
|
|
memcpy(temp, &theid, sizeof(theid));
|
|
|
|
memcpy(temp + sizeof(theid), message, length);
|
2013-08-11 11:24:11 +08:00
|
|
|
return write_cryptpacket_id(m, friendnumber, PACKET_ID_MESSAGE, temp, length + sizeof(theid));
|
2013-07-07 10:28:15 +08:00
|
|
|
}
|
|
|
|
|
2013-08-09 03:00:30 +08:00
|
|
|
/* send an action to an online friend
|
|
|
|
return 1 if packet was successfully put into the send queue
|
|
|
|
return 0 if it was not */
|
2013-08-11 11:24:11 +08:00
|
|
|
int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length)
|
2013-08-09 03:00:30 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
return write_cryptpacket_id(m, friendnumber, PACKET_ID_ACTION, action, length);
|
2013-08-09 03:00:30 +08:00
|
|
|
}
|
|
|
|
|
2013-08-06 03:30:07 +08:00
|
|
|
/* send a name packet to friendnumber
|
2013-07-28 09:08:06 +08:00
|
|
|
length is the length with the NULL terminator*/
|
2013-08-17 01:11:09 +08:00
|
|
|
static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t length)
|
2013-07-18 23:47:27 +08:00
|
|
|
{
|
2013-08-17 01:11:09 +08:00
|
|
|
if (length > MAX_NAME_LENGTH || length == 0)
|
2013-07-28 09:08:06 +08:00
|
|
|
return 0;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
return write_cryptpacket_id(m, friendnumber, PACKET_ID_NICKNAME, name, length);
|
2013-07-18 23:47:27 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* set the name of a friend
|
|
|
|
return 0 if success
|
|
|
|
return -1 if failure */
|
2013-08-17 01:11:09 +08:00
|
|
|
static int setfriendname(Messenger *m, int friendnumber, uint8_t *name)
|
2013-07-18 23:47:27 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
if (friendnumber >= m->numfriends || friendnumber < 0)
|
2013-07-18 23:47:27 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
memcpy(m->friendlist[friendnumber].name, name, MAX_NAME_LENGTH);
|
2013-07-18 23:47:27 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* Set our nickname
|
|
|
|
name must be a string of maximum MAX_NAME_LENGTH length.
|
2013-07-28 09:08:06 +08:00
|
|
|
length must be at least 1 byte
|
|
|
|
length is the length of name with the NULL terminator
|
2013-07-20 11:00:10 +08:00
|
|
|
return 0 if success
|
|
|
|
return -1 if failure */
|
2013-08-17 01:11:09 +08:00
|
|
|
int setname(Messenger *m, uint8_t *name, uint16_t length)
|
2013-07-18 23:47:27 +08:00
|
|
|
{
|
2013-07-28 09:08:06 +08:00
|
|
|
if (length > MAX_NAME_LENGTH || length == 0)
|
2013-07-18 23:47:27 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
memcpy(m->name, name, length);
|
|
|
|
m->name_length = length;
|
2013-07-18 23:47:27 +08:00
|
|
|
uint32_t i;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
for (i = 0; i < m->numfriends; ++i)
|
|
|
|
m->friendlist[i].name_sent = 0;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-18 23:47:27 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-31 02:30:33 +08:00
|
|
|
/* get our nickname
|
2013-08-06 03:30:07 +08:00
|
|
|
put it in name
|
2013-07-31 02:32:18 +08:00
|
|
|
name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
|
2013-07-31 02:30:33 +08:00
|
|
|
return the length of the name */
|
2013-08-11 21:24:47 +08:00
|
|
|
uint16_t getself_name(Messenger *m, uint8_t *name, uint16_t nlen)
|
2013-07-30 23:09:09 +08:00
|
|
|
{
|
2013-08-11 21:24:47 +08:00
|
|
|
uint16_t len;
|
|
|
|
|
|
|
|
if (name == NULL || nlen == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = MIN(nlen, m->name_length);
|
2013-08-11 11:24:11 +08:00
|
|
|
memcpy(name, m->name, m->name_length);
|
2013-08-11 21:24:47 +08:00
|
|
|
|
|
|
|
return len;
|
2013-08-06 03:30:07 +08:00
|
|
|
}
|
2013-07-30 23:09:09 +08:00
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* get name of friendnumber
|
|
|
|
put it in name
|
|
|
|
name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
|
|
|
|
return 0 if success
|
|
|
|
return -1 if failure */
|
2013-08-17 01:11:09 +08:00
|
|
|
int getname(Messenger *m, int friendnumber, uint8_t *name)
|
2013-07-18 23:47:27 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
if (friendnumber >= m->numfriends || friendnumber < 0)
|
2013-07-18 23:47:27 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
memcpy(name, m->friendlist[friendnumber].name, MAX_NAME_LENGTH);
|
2013-07-18 23:47:27 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-07-07 10:28:15 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length)
|
2013-07-19 01:56:50 +08:00
|
|
|
{
|
2013-08-08 05:01:31 +08:00
|
|
|
if (length > MAX_STATUSMESSAGE_LENGTH)
|
2013-07-19 01:56:50 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
memcpy(m->statusmessage, status, length);
|
|
|
|
m->statusmessage_length = length;
|
2013-07-19 01:56:50 +08:00
|
|
|
|
|
|
|
uint32_t i;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
for (i = 0; i < m->numfriends; ++i)
|
|
|
|
m->friendlist[i].statusmessage_sent = 0;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-19 01:56:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
int m_set_userstatus(Messenger *m, USERSTATUS status)
|
2013-08-08 05:01:31 +08:00
|
|
|
{
|
|
|
|
if (status >= USERSTATUS_INVALID) {
|
2013-08-06 03:30:07 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
m->userstatus = status;
|
2013-08-06 03:30:07 +08:00
|
|
|
uint32_t i;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
for (i = 0; i < m->numfriends; ++i)
|
|
|
|
m->friendlist[i].userstatus_sent = 0;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-06 03:30:07 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* return the size of friendnumber's user status
|
2013-08-08 05:01:31 +08:00
|
|
|
guaranteed to be at most MAX_STATUSMESSAGE_LENGTH */
|
2013-08-11 11:24:11 +08:00
|
|
|
int m_get_statusmessage_size(Messenger *m, int friendnumber)
|
2013-07-19 01:56:50 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
if (friendnumber >= m->numfriends || friendnumber < 0)
|
2013-07-19 01:56:50 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
return m->friendlist[friendnumber].statusmessage_length;
|
2013-07-19 01:56:50 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* copy the user status of friendnumber into buf, truncating if needed to maxlen
|
2013-08-08 05:01:31 +08:00
|
|
|
bytes, use m_get_statusmessage_size to find out how much you need to allocate */
|
2013-08-17 01:11:09 +08:00
|
|
|
int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen)
|
2013-07-19 01:56:50 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
if (friendnumber >= m->numfriends || friendnumber < 0)
|
2013-07-19 01:56:50 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-19 02:13:29 +08:00
|
|
|
memset(buf, 0, maxlen);
|
2013-08-11 11:24:11 +08:00
|
|
|
memcpy(buf, m->friendlist[friendnumber].statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1);
|
2013-07-19 01:56:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen)
|
2013-08-06 03:30:07 +08:00
|
|
|
{
|
|
|
|
memset(buf, 0, maxlen);
|
2013-08-11 11:24:11 +08:00
|
|
|
memcpy(buf, m->statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1);
|
2013-08-06 03:30:07 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
USERSTATUS m_get_userstatus(Messenger *m, int friendnumber)
|
2013-08-08 05:01:31 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
if (friendnumber >= m->numfriends || friendnumber < 0)
|
2013-08-08 05:01:31 +08:00
|
|
|
return USERSTATUS_INVALID;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
USERSTATUS status = m->friendlist[friendnumber].userstatus;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-08 05:01:31 +08:00
|
|
|
if (status >= USERSTATUS_INVALID) {
|
|
|
|
status = USERSTATUS_NONE;
|
2013-08-06 03:30:07 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-08 05:01:31 +08:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
USERSTATUS m_get_self_userstatus(Messenger *m)
|
2013-08-08 05:01:31 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
return m->userstatus;
|
2013-08-06 03:30:07 +08:00
|
|
|
}
|
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
static int send_statusmessage(Messenger *m, int friendnumber, uint8_t *status, uint16_t length)
|
2013-08-08 05:01:31 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
return write_cryptpacket_id(m, friendnumber, PACKET_ID_STATUSMESSAGE, status, length);
|
2013-08-06 03:30:07 +08:00
|
|
|
}
|
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
static int send_userstatus(Messenger *m, int friendnumber, USERSTATUS status)
|
2013-07-19 01:56:50 +08:00
|
|
|
{
|
2013-08-10 08:32:51 +08:00
|
|
|
uint8_t stat = status;
|
2013-08-11 11:24:11 +08:00
|
|
|
return write_cryptpacket_id(m, friendnumber, PACKET_ID_USERSTATUS, &stat, sizeof(stat));
|
2013-07-19 01:56:50 +08:00
|
|
|
}
|
|
|
|
|
2013-08-19 19:00:59 +08:00
|
|
|
static int send_ping(Messenger *m, int friendnumber)
|
|
|
|
{
|
|
|
|
m->friendlist[friendnumber].ping_lastsent = unix_time();
|
|
|
|
return write_cryptpacket_id(m, friendnumber, PACKET_ID_PING, 0, 0);
|
|
|
|
}
|
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t *status, uint16_t length)
|
2013-07-19 01:56:50 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
if (friendnumber >= m->numfriends || friendnumber < 0)
|
2013-07-19 01:56:50 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-19 01:56:50 +08:00
|
|
|
uint8_t *newstatus = calloc(length, 1);
|
|
|
|
memcpy(newstatus, status, length);
|
2013-08-11 11:24:11 +08:00
|
|
|
free(m->friendlist[friendnumber].statusmessage);
|
|
|
|
m->friendlist[friendnumber].statusmessage = newstatus;
|
|
|
|
m->friendlist[friendnumber].statusmessage_length = length;
|
2013-07-19 01:56:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-07-26 16:02:17 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
static void set_friend_userstatus(Messenger *m, int friendnumber, USERSTATUS status)
|
2013-08-06 03:30:07 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friendlist[friendnumber].userstatus = status;
|
2013-08-06 03:30:07 +08:00
|
|
|
}
|
|
|
|
|
2013-08-08 01:06:07 +08:00
|
|
|
/* Sets whether we send read receipts for friendnumber. */
|
2013-08-11 11:24:11 +08:00
|
|
|
void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno)
|
2013-08-08 01:06:07 +08:00
|
|
|
{
|
2013-08-08 06:14:35 +08:00
|
|
|
if (yesno != 0 || yesno != 1)
|
2013-08-08 01:06:07 +08:00
|
|
|
return;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
if (friendnumber >= m->numfriends || friendnumber < 0)
|
2013-08-08 01:06:07 +08:00
|
|
|
return;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friendlist[friendnumber].receives_read_receipts = yesno;
|
2013-08-08 01:06:07 +08:00
|
|
|
}
|
|
|
|
|
2013-08-14 13:27:10 +08:00
|
|
|
/* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); */
|
2013-07-20 11:00:10 +08:00
|
|
|
/* set the function that will be executed when a friend request is received. */
|
2013-08-17 01:11:09 +08:00
|
|
|
void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata)
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2013-08-21 00:08:55 +08:00
|
|
|
callback_friendrequest(&(m->fr), function, userdata);
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* set the function that will be executed when a message from a friend is received. */
|
2013-08-17 01:11:09 +08:00
|
|
|
void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *),
|
|
|
|
void *userdata)
|
2013-07-09 08:50:25 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friend_message = function;
|
2013-08-12 20:23:46 +08:00
|
|
|
m->friend_message_userdata = userdata;
|
2013-07-07 10:28:15 +08:00
|
|
|
}
|
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *), void *userdata)
|
2013-08-09 03:00:30 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friend_action = function;
|
2013-08-12 20:23:46 +08:00
|
|
|
m->friend_action_userdata = userdata;
|
2013-08-09 03:00:30 +08:00
|
|
|
}
|
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *),
|
|
|
|
void *userdata)
|
2013-07-19 01:56:50 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friend_namechange = function;
|
2013-08-12 20:23:46 +08:00
|
|
|
m->friend_namechange_userdata = userdata;
|
2013-07-19 01:56:50 +08:00
|
|
|
}
|
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void *),
|
|
|
|
void *userdata)
|
2013-07-19 01:56:50 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friend_statusmessagechange = function;
|
2013-08-12 20:23:46 +08:00
|
|
|
m->friend_statuschange_userdata = userdata;
|
2013-08-08 05:01:31 +08:00
|
|
|
}
|
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void *), void *userdata)
|
2013-08-08 05:01:31 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friend_userstatuschange = function;
|
2013-08-12 20:23:46 +08:00
|
|
|
m->friend_userstatuschange_userdata = userdata;
|
2013-07-19 01:56:50 +08:00
|
|
|
}
|
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void *), void *userdata)
|
2013-08-08 00:53:52 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
m->read_receipt = function;
|
2013-08-12 20:23:46 +08:00
|
|
|
m->read_receipt_userdata = userdata;
|
2013-08-08 00:53:52 +08:00
|
|
|
}
|
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void *), void *userdata)
|
2013-08-08 13:46:27 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friend_connectionstatuschange = function;
|
2013-08-12 20:23:46 +08:00
|
|
|
m->friend_connectionstatuschange_userdata = userdata;
|
2013-08-12 02:57:47 +08:00
|
|
|
}
|
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
static void check_friend_connectionstatus(Messenger *m, int friendnumber, uint8_t status)
|
2013-08-12 02:57:47 +08:00
|
|
|
{
|
2013-08-14 13:27:10 +08:00
|
|
|
if (!m->friend_connectionstatuschange)
|
2013-08-12 02:57:47 +08:00
|
|
|
return;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-12 02:57:47 +08:00
|
|
|
if (status == NOFRIEND)
|
|
|
|
return;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
const uint8_t was_connected = m->friendlist[friendnumber].status == FRIEND_ONLINE;
|
2013-08-12 04:56:02 +08:00
|
|
|
const uint8_t is_connected = status == FRIEND_ONLINE;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-12 02:57:47 +08:00
|
|
|
if (is_connected != was_connected)
|
2013-08-12 20:23:46 +08:00
|
|
|
m->friend_connectionstatuschange(m, friendnumber, is_connected, m->friend_connectionstatuschange_userdata);
|
2013-08-08 13:46:27 +08:00
|
|
|
}
|
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
void set_friend_status(Messenger *m, int friendnumber, uint8_t status)
|
2013-08-08 13:46:27 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
check_friend_connectionstatus(m, friendnumber, status);
|
|
|
|
m->friendlist[friendnumber].status = status;
|
2013-08-08 13:46:27 +08:00
|
|
|
}
|
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length)
|
2013-08-10 03:58:08 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
if (friendnumber < 0 || friendnumber >= m->numfriends)
|
2013-08-10 04:28:12 +08:00
|
|
|
return 0;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
if (length >= MAX_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE)
|
2013-08-10 04:28:12 +08:00
|
|
|
return 0;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-10 03:58:08 +08:00
|
|
|
uint8_t packet[length + 1];
|
|
|
|
packet[0] = packet_id;
|
2013-08-19 19:00:59 +08:00
|
|
|
|
|
|
|
if (length != 0)
|
|
|
|
memcpy(packet + 1, data, length);
|
|
|
|
|
2013-08-20 11:54:28 +08:00
|
|
|
return write_cryptpacket(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id, packet, length + 1);
|
2013-08-10 03:58:08 +08:00
|
|
|
}
|
|
|
|
|
2013-08-13 06:47:15 +08:00
|
|
|
/*Interval in seconds between LAN discovery packet sending*/
|
|
|
|
#define LAN_DISCOVERY_INTERVAL 60
|
2013-07-07 10:28:15 +08:00
|
|
|
#define PORT 33445
|
2013-08-13 06:47:15 +08:00
|
|
|
|
|
|
|
/*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/
|
2013-08-21 02:47:32 +08:00
|
|
|
static void LANdiscovery(Messenger *m)
|
2013-08-13 06:47:15 +08:00
|
|
|
{
|
2013-08-21 02:47:32 +08:00
|
|
|
if (m->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) {
|
|
|
|
send_LANdiscovery(htons(PORT), m->net_crypto);
|
|
|
|
m->last_LANdiscovery = unix_time();
|
|
|
|
}
|
2013-08-13 06:47:15 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* run this at startup */
|
2013-08-17 01:11:09 +08:00
|
|
|
Messenger *initMessenger(void)
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2013-08-11 11:24:11 +08:00
|
|
|
Messenger *m = calloc(1, sizeof(Messenger));
|
2013-08-21 07:37:05 +08:00
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
if ( ! m )
|
2013-08-21 03:53:15 +08:00
|
|
|
return NULL;
|
2013-08-21 07:37:05 +08:00
|
|
|
|
2013-07-07 10:28:15 +08:00
|
|
|
IP ip;
|
|
|
|
ip.i = 0;
|
2013-08-20 11:54:28 +08:00
|
|
|
m->net = new_networking(ip, PORT);
|
2013-08-21 07:37:05 +08:00
|
|
|
|
2013-08-20 11:54:28 +08:00
|
|
|
if (m->net == NULL) {
|
|
|
|
free(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-08-21 07:37:05 +08:00
|
|
|
|
2013-08-20 11:54:28 +08:00
|
|
|
m->net_crypto = new_net_crypto(m->net);
|
2013-08-21 07:37:05 +08:00
|
|
|
|
2013-08-20 11:54:28 +08:00
|
|
|
if (m->net_crypto == NULL) {
|
2013-08-21 00:08:55 +08:00
|
|
|
kill_networking(m->net);
|
|
|
|
free(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-08-21 07:37:05 +08:00
|
|
|
|
2013-08-21 00:08:55 +08:00
|
|
|
m->dht = new_DHT(m->net_crypto);
|
2013-08-21 07:37:05 +08:00
|
|
|
|
2013-08-21 00:08:55 +08:00
|
|
|
if (m->dht == NULL) {
|
|
|
|
kill_net_crypto(m->net_crypto);
|
|
|
|
kill_networking(m->net);
|
2013-08-20 11:54:28 +08:00
|
|
|
free(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-08-21 07:37:05 +08:00
|
|
|
|
2013-08-20 11:54:28 +08:00
|
|
|
new_keys(m->net_crypto);
|
|
|
|
m_set_statusmessage(m, (uint8_t *)"Online", sizeof("Online"));
|
2013-07-26 09:00:31 +08:00
|
|
|
|
2013-08-21 00:08:55 +08:00
|
|
|
friendreq_init(&(m->fr), m->net_crypto);
|
|
|
|
LANdiscovery_init(m->dht);
|
|
|
|
set_nospam(&(m->fr), random_int());
|
2013-08-10 07:54:43 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* run this before closing shop */
|
2013-08-17 01:11:09 +08:00
|
|
|
void cleanupMessenger(Messenger *m)
|
|
|
|
{
|
2013-08-14 13:35:15 +08:00
|
|
|
/* FIXME TODO ideally cleanupMessenger will mirror initMessenger
|
|
|
|
* this requires the other modules to expose cleanup functions
|
|
|
|
*/
|
2013-08-21 00:08:55 +08:00
|
|
|
kill_DHT(m->dht);
|
2013-08-20 11:54:28 +08:00
|
|
|
kill_net_crypto(m->net_crypto);
|
2013-08-21 00:08:55 +08:00
|
|
|
kill_networking(m->net);
|
2013-08-14 13:35:15 +08:00
|
|
|
free(m->friendlist);
|
2013-08-11 11:24:11 +08:00
|
|
|
free(m);
|
2013-07-07 10:28:15 +08:00
|
|
|
}
|
|
|
|
|
2013-07-27 11:07:25 +08:00
|
|
|
//TODO: make this function not suck.
|
2013-08-11 11:24:11 +08:00
|
|
|
void doFriends(Messenger *m)
|
2013-07-27 20:43:36 +08:00
|
|
|
{
|
|
|
|
/* TODO: add incoming connections and some other stuff. */
|
2013-07-09 08:50:25 +08:00
|
|
|
uint32_t i;
|
|
|
|
int len;
|
|
|
|
uint8_t temp[MAX_DATA_SIZE];
|
2013-08-19 19:00:59 +08:00
|
|
|
uint64_t temp_time = unix_time();
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
for (i = 0; i < m->numfriends; ++i) {
|
|
|
|
if (m->friendlist[i].status == FRIEND_ADDED) {
|
2013-08-21 07:37:05 +08:00
|
|
|
int fr = send_friendrequest(m->dht, m->friendlist[i].client_id, m->friendlist[i].friendrequest_nospam,
|
|
|
|
m->friendlist[i].info,
|
2013-08-17 01:11:09 +08:00
|
|
|
m->friendlist[i].info_size);
|
|
|
|
|
2013-08-17 06:57:00 +08:00
|
|
|
if (fr >= 0) {
|
2013-08-11 11:24:11 +08:00
|
|
|
set_friend_status(m, i, FRIEND_REQUESTED);
|
2013-08-19 19:00:59 +08:00
|
|
|
m->friendlist[i].friendrequest_lastsent = temp_time;
|
2013-08-17 06:57:00 +08:00
|
|
|
}
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
if (m->friendlist[i].status == FRIEND_REQUESTED
|
|
|
|
|| m->friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */
|
2013-08-11 11:24:11 +08:00
|
|
|
if (m->friendlist[i].status == FRIEND_REQUESTED) {
|
2013-08-17 06:57:00 +08:00
|
|
|
/* If we didn't connect to friend after successfully sending him a friend request the request is deemed
|
|
|
|
unsuccessful so we set the status back to FRIEND_ADDED and try again.*/
|
2013-08-19 19:00:59 +08:00
|
|
|
if (m->friendlist[i].friendrequest_lastsent + m->friendlist[i].friendrequest_timeout < temp_time) {
|
2013-08-17 06:57:00 +08:00
|
|
|
set_friend_status(m, i, FRIEND_ADDED);
|
|
|
|
/* Double the default timeout everytime if friendrequest is assumed to have been
|
|
|
|
sent unsuccessfully. */
|
|
|
|
m->friendlist[i].friendrequest_timeout *= 2;
|
2013-07-23 05:02:32 +08:00
|
|
|
}
|
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-21 00:08:55 +08:00
|
|
|
IP_Port friendip = DHT_getfriendip(m->dht, m->friendlist[i].client_id);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-20 11:54:28 +08:00
|
|
|
switch (is_cryptoconnected(m->net_crypto, m->friendlist[i].crypt_connection_id)) {
|
2013-08-17 01:11:09 +08:00
|
|
|
case 0:
|
|
|
|
if (friendip.ip.i > 1)
|
2013-08-20 11:54:28 +08:00
|
|
|
m->friendlist[i].crypt_connection_id = crypto_connect(m->net_crypto, m->friendlist[i].client_id, friendip);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: /* Connection is established */
|
|
|
|
set_friend_status(m, i, FRIEND_ONLINE);
|
|
|
|
m->friendlist[i].name_sent = 0;
|
|
|
|
m->friendlist[i].userstatus_sent = 0;
|
|
|
|
m->friendlist[i].statusmessage_sent = 0;
|
2013-08-19 19:00:59 +08:00
|
|
|
m->friendlist[i].ping_lastrecv = temp_time;
|
2013-08-17 01:11:09 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
2013-08-20 11:54:28 +08:00
|
|
|
crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id);
|
2013-08-17 01:11:09 +08:00
|
|
|
m->friendlist[i].crypt_connection_id = -1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2013-07-10 01:20:48 +08:00
|
|
|
}
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
while (m->friendlist[i].status == FRIEND_ONLINE) { /* friend is online */
|
|
|
|
if (m->friendlist[i].name_sent == 0) {
|
|
|
|
if (m_sendname(m, i, m->name, m->name_length))
|
|
|
|
m->friendlist[i].name_sent = 1;
|
2013-07-27 11:07:25 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
if (m->friendlist[i].statusmessage_sent == 0) {
|
|
|
|
if (send_statusmessage(m, i, m->statusmessage, m->statusmessage_length))
|
|
|
|
m->friendlist[i].statusmessage_sent = 1;
|
2013-08-08 05:01:31 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
if (m->friendlist[i].userstatus_sent == 0) {
|
|
|
|
if (send_userstatus(m, i, m->userstatus))
|
|
|
|
m->friendlist[i].userstatus_sent = 1;
|
2013-07-27 11:07:25 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-19 19:00:59 +08:00
|
|
|
if (m->friendlist[i].ping_lastsent + FRIEND_PING_INTERVAL < temp_time) {
|
|
|
|
send_ping(m, i);
|
|
|
|
}
|
|
|
|
|
2013-08-20 11:54:28 +08:00
|
|
|
len = read_cryptpacket(m->net_crypto, m->friendlist[i].crypt_connection_id, temp);
|
2013-08-10 04:56:11 +08:00
|
|
|
uint8_t packet_id = temp[0];
|
2013-08-17 01:11:09 +08:00
|
|
|
uint8_t *data = temp + 1;
|
2013-08-10 04:56:11 +08:00
|
|
|
int data_length = len - 1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-27 11:07:25 +08:00
|
|
|
if (len > 0) {
|
2013-08-10 04:56:11 +08:00
|
|
|
switch (packet_id) {
|
2013-08-19 19:00:59 +08:00
|
|
|
case PACKET_ID_PING: {
|
|
|
|
m->friendlist[i].ping_lastrecv = temp_time;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
case PACKET_ID_NICKNAME: {
|
|
|
|
if (data_length >= MAX_NAME_LENGTH || data_length == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (m->friend_namechange)
|
|
|
|
m->friend_namechange(m, i, data, data_length, m->friend_namechange_userdata);
|
|
|
|
|
|
|
|
memcpy(m->friendlist[i].name, data, data_length);
|
|
|
|
m->friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */
|
2013-07-19 01:56:50 +08:00
|
|
|
break;
|
2013-08-17 01:11:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case PACKET_ID_STATUSMESSAGE: {
|
|
|
|
if (data_length == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
uint8_t *status = calloc(MIN(data_length, MAX_STATUSMESSAGE_LENGTH), 1);
|
|
|
|
memcpy(status, data, MIN(data_length, MAX_STATUSMESSAGE_LENGTH));
|
|
|
|
|
|
|
|
if (m->friend_statusmessagechange)
|
|
|
|
m->friend_statusmessagechange(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH),
|
|
|
|
m->friend_statuschange_userdata);
|
|
|
|
|
|
|
|
set_friend_statusmessage(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH));
|
|
|
|
free(status);
|
2013-08-08 08:23:48 +08:00
|
|
|
break;
|
2013-08-17 01:11:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case PACKET_ID_USERSTATUS: {
|
|
|
|
if (data_length != 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
USERSTATUS status = data[0];
|
|
|
|
|
|
|
|
if (m->friend_userstatuschange)
|
|
|
|
m->friend_userstatuschange(m, i, status, m->friend_userstatuschange_userdata);
|
|
|
|
|
|
|
|
set_friend_userstatus(m, i, status);
|
2013-08-08 08:23:48 +08:00
|
|
|
break;
|
2013-08-08 00:53:52 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
case PACKET_ID_MESSAGE: {
|
|
|
|
uint8_t *message_id = data;
|
|
|
|
uint8_t message_id_length = 4;
|
|
|
|
uint8_t *message = data + message_id_length;
|
|
|
|
uint16_t message_length = data_length - message_id_length;
|
|
|
|
|
|
|
|
if (m->friendlist[i].receives_read_receipts) {
|
|
|
|
write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m->friend_message)
|
|
|
|
(*m->friend_message)(m, i, message, message_length, m->friend_message_userdata);
|
|
|
|
|
2013-08-08 00:53:52 +08:00
|
|
|
break;
|
2013-08-17 01:11:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case PACKET_ID_ACTION: {
|
|
|
|
if (m->friend_action)
|
|
|
|
(*m->friend_action)(m, i, data, data_length, m->friend_action_userdata);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PACKET_ID_RECEIPT: {
|
|
|
|
uint32_t msgid;
|
|
|
|
|
|
|
|
if (data_length < sizeof(msgid))
|
|
|
|
break;
|
|
|
|
|
|
|
|
memcpy(&msgid, data, sizeof(msgid));
|
|
|
|
msgid = ntohl(msgid);
|
|
|
|
|
|
|
|
if (m->read_receipt)
|
|
|
|
(*m->read_receipt)(m, i, msgid, m->read_receipt_userdata);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2013-07-27 20:43:36 +08:00
|
|
|
}
|
|
|
|
} else {
|
2013-08-21 07:37:05 +08:00
|
|
|
if (is_cryptoconnected(m->net_crypto,
|
|
|
|
m->friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */
|
2013-08-20 11:54:28 +08:00
|
|
|
crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id);
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friendlist[i].crypt_connection_id = -1;
|
|
|
|
set_friend_status(m, i, FRIEND_CONFIRMED);
|
2013-07-27 20:43:36 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-27 20:43:36 +08:00
|
|
|
break;
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|
2013-08-19 19:00:59 +08:00
|
|
|
|
|
|
|
if (m->friendlist[i].ping_lastrecv + FRIEND_CONNECTION_TIMEOUT < temp_time) {
|
|
|
|
/* if we stopped recieving ping packets kill it */
|
2013-08-20 11:54:28 +08:00
|
|
|
crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id);
|
2013-08-19 19:00:59 +08:00
|
|
|
m->friendlist[i].crypt_connection_id = -1;
|
|
|
|
set_friend_status(m, i, FRIEND_CONFIRMED);
|
|
|
|
}
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
void doInbound(Messenger *m)
|
2013-07-10 01:20:48 +08:00
|
|
|
{
|
|
|
|
uint8_t secret_nonce[crypto_box_NONCEBYTES];
|
|
|
|
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
|
|
|
|
uint8_t session_key[crypto_box_PUBLICKEYBYTES];
|
2013-08-20 11:54:28 +08:00
|
|
|
int inconnection = crypto_inbound(m->net_crypto, public_key, secret_nonce, session_key);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-27 11:07:25 +08:00
|
|
|
if (inconnection != -1) {
|
2013-08-11 11:24:11 +08:00
|
|
|
int friend_id = getfriend_id(m, public_key);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-27 11:07:25 +08:00
|
|
|
if (friend_id != -1) {
|
2013-08-20 11:54:28 +08:00
|
|
|
crypto_kill(m->net_crypto, m->friendlist[friend_id].crypt_connection_id);
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friendlist[friend_id].crypt_connection_id =
|
2013-08-20 11:54:28 +08:00
|
|
|
accept_crypto_inbound(m->net_crypto, inconnection, public_key, secret_nonce, session_key);
|
2013-07-27 20:43:36 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
set_friend_status(m, friend_id, FRIEND_CONFIRMED);
|
2013-07-10 01:20:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-20 11:54:28 +08:00
|
|
|
/* the main loop that needs to be run at least 20 times per second. */
|
2013-08-11 11:24:11 +08:00
|
|
|
void doMessenger(Messenger *m)
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2013-08-20 11:54:28 +08:00
|
|
|
networking_poll(m->net);
|
2013-08-11 11:24:11 +08:00
|
|
|
|
2013-08-21 00:08:55 +08:00
|
|
|
do_DHT(m->dht);
|
2013-08-20 11:54:28 +08:00
|
|
|
do_net_crypto(m->net_crypto);
|
2013-08-11 11:24:11 +08:00
|
|
|
doInbound(m);
|
|
|
|
doFriends(m);
|
2013-08-21 02:47:32 +08:00
|
|
|
LANdiscovery(m);
|
2013-08-10 07:30:18 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* returns the size of the messenger data (for saving) */
|
2013-08-11 11:24:11 +08:00
|
|
|
uint32_t Messenger_size(Messenger *m)
|
2013-07-18 00:07:19 +08:00
|
|
|
{
|
2013-08-17 17:59:28 +08:00
|
|
|
return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
|
2013-08-17 23:16:08 +08:00
|
|
|
+ sizeof(uint32_t) // nospam
|
|
|
|
+ sizeof(uint32_t) // DHT size
|
2013-08-21 00:08:55 +08:00
|
|
|
+ DHT_size(m->dht) // DHT itself
|
2013-08-17 23:16:08 +08:00
|
|
|
+ sizeof(uint32_t) // Friendlist size
|
|
|
|
+ sizeof(Friend) * m->numfriends // Friendlist itself
|
|
|
|
+ sizeof(uint16_t) // Own nickname length
|
|
|
|
+ m->name_length // Own nickname
|
|
|
|
;
|
2013-07-18 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* save the messenger in data of size Messenger_size() */
|
2013-08-11 11:24:11 +08:00
|
|
|
void Messenger_save(Messenger *m, uint8_t *data)
|
2013-07-18 00:07:19 +08:00
|
|
|
{
|
2013-08-20 11:54:28 +08:00
|
|
|
save_keys(m->net_crypto, data);
|
2013-07-18 00:07:19 +08:00
|
|
|
data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
|
2013-08-21 00:08:55 +08:00
|
|
|
uint32_t nospam = get_nospam(&(m->fr));
|
2013-08-14 05:25:52 +08:00
|
|
|
memcpy(data, &nospam, sizeof(nospam));
|
|
|
|
data += sizeof(nospam);
|
2013-08-21 00:08:55 +08:00
|
|
|
uint32_t size = DHT_size(m->dht);
|
2013-07-18 00:07:19 +08:00
|
|
|
memcpy(data, &size, sizeof(size));
|
|
|
|
data += sizeof(size);
|
2013-08-21 00:08:55 +08:00
|
|
|
DHT_save(m->dht, data);
|
2013-07-18 00:07:19 +08:00
|
|
|
data += size;
|
2013-08-11 11:24:11 +08:00
|
|
|
size = sizeof(Friend) * m->numfriends;
|
2013-07-18 00:07:19 +08:00
|
|
|
memcpy(data, &size, sizeof(size));
|
|
|
|
data += sizeof(size);
|
2013-08-11 11:24:11 +08:00
|
|
|
memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends);
|
2013-08-17 17:59:28 +08:00
|
|
|
data += size;
|
|
|
|
uint16_t small_size = m->name_length;
|
|
|
|
memcpy(data, &small_size, sizeof(small_size));
|
|
|
|
data += sizeof(small_size);
|
|
|
|
memcpy(data, m->name, small_size);
|
2013-07-18 00:07:19 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* load the messenger from data of size length. */
|
2013-08-17 01:11:09 +08:00
|
|
|
int Messenger_load(Messenger *m, uint8_t *data, uint32_t length)
|
2013-07-18 00:07:19 +08:00
|
|
|
{
|
2013-07-27 11:07:25 +08:00
|
|
|
if (length == ~0)
|
2013-07-18 00:07:19 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-14 05:25:52 +08:00
|
|
|
if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3)
|
2013-07-18 00:07:19 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-14 05:25:52 +08:00
|
|
|
length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3;
|
2013-08-20 11:54:28 +08:00
|
|
|
load_keys(m->net_crypto, data);
|
2013-07-18 00:07:19 +08:00
|
|
|
data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
|
2013-08-14 05:25:52 +08:00
|
|
|
uint32_t nospam;
|
|
|
|
memcpy(&nospam, data, sizeof(nospam));
|
2013-08-21 00:08:55 +08:00
|
|
|
set_nospam(&(m->fr), nospam);
|
2013-08-14 05:25:52 +08:00
|
|
|
data += sizeof(nospam);
|
2013-07-18 00:07:19 +08:00
|
|
|
uint32_t size;
|
|
|
|
memcpy(&size, data, sizeof(size));
|
|
|
|
data += sizeof(size);
|
2013-07-27 20:43:36 +08:00
|
|
|
|
2013-07-27 11:07:25 +08:00
|
|
|
if (length < size)
|
2013-07-18 00:07:19 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-18 00:07:19 +08:00
|
|
|
length -= size;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-21 00:08:55 +08:00
|
|
|
if (DHT_load(m->dht, data, size) == -1)
|
2013-07-18 00:07:19 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-18 00:07:19 +08:00
|
|
|
data += size;
|
|
|
|
memcpy(&size, data, sizeof(size));
|
|
|
|
data += sizeof(size);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-17 17:59:28 +08:00
|
|
|
if (length < size || size % sizeof(Friend) != 0)
|
2013-07-18 00:07:19 +08:00
|
|
|
return -1;
|
2013-07-27 20:43:36 +08:00
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
Friend *temp = malloc(size);
|
2013-07-18 00:07:19 +08:00
|
|
|
memcpy(temp, data, size);
|
2013-07-27 20:43:36 +08:00
|
|
|
|
2013-07-18 00:07:19 +08:00
|
|
|
uint16_t num = size / sizeof(Friend);
|
2013-07-27 20:43:36 +08:00
|
|
|
|
2013-07-18 00:07:19 +08:00
|
|
|
uint32_t i;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-27 11:07:25 +08:00
|
|
|
for (i = 0; i < num; ++i) {
|
2013-08-17 01:11:09 +08:00
|
|
|
if (temp[i].status >= 3) {
|
2013-08-11 11:24:11 +08:00
|
|
|
int fnum = m_addfriend_norequest(m, temp[i].client_id);
|
|
|
|
setfriendname(m, fnum, temp[i].name);
|
2013-08-08 05:01:31 +08:00
|
|
|
/* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */
|
2013-08-16 08:56:22 +08:00
|
|
|
} 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);
|
2013-07-19 05:10:26 +08:00
|
|
|
}
|
2013-07-18 00:07:19 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-18 15:23:50 +08:00
|
|
|
free(temp);
|
2013-08-17 17:59:28 +08:00
|
|
|
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);
|
|
|
|
|
2013-07-18 00:07:19 +08:00
|
|
|
return 0;
|
|
|
|
}
|