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
|
|
|
|
2013-09-05 18:02:26 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-07-07 10:28:15 +08:00
|
|
|
#include "Messenger.h"
|
2013-11-15 02:05:36 +08:00
|
|
|
#include "assoc.h"
|
2013-10-02 22:38:54 +08:00
|
|
|
#include "network.h"
|
2013-09-13 14:50:46 +08:00
|
|
|
#include "util.h"
|
2013-08-12 21:19:25 +08:00
|
|
|
|
2013-11-15 02:05:36 +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-10-16 05:23:00 +08:00
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
static void set_friend_status(Messenger *m, int32_t friendnumber, uint8_t status);
|
|
|
|
static int write_cryptpacket_id(Messenger *m, int32_t friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length);
|
2013-08-08 13:46:27 +08:00
|
|
|
|
2013-09-02 21:55:37 +08:00
|
|
|
// friend_not_valid determines if the friendnumber passed is valid in the Messenger object
|
2014-02-22 00:38:04 +08:00
|
|
|
static uint8_t friend_not_valid(Messenger *m, int32_t friendnumber)
|
2013-09-02 21:55:37 +08:00
|
|
|
{
|
|
|
|
return (unsigned int)friendnumber >= m->numfriends;
|
|
|
|
}
|
2013-09-02 10:42:59 +08:00
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
static int add_online_friend(Messenger *m, int32_t friendnumber)
|
2014-02-15 10:16:31 +08:00
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
IP_Port temp_ip_port = get_friend_ipport(m, friendnumber);
|
|
|
|
|
|
|
|
if (temp_ip_port.port == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < m->numonline_friends; ++i) {
|
|
|
|
if (m->online_friendlist[i].friend_num == (uint32_t)friendnumber)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Online_Friend *temp;
|
|
|
|
temp = realloc(m->online_friendlist, sizeof(Online_Friend) * (m->numonline_friends + 1));
|
|
|
|
|
|
|
|
if (temp == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
m->online_friendlist = temp;
|
|
|
|
m->online_friendlist[m->numonline_friends].friend_num = friendnumber;
|
|
|
|
m->online_friendlist[m->numonline_friends].ip_port = temp_ip_port;
|
|
|
|
++m->numonline_friends;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
static int remove_online_friend(Messenger *m, int32_t friendnumber)
|
2014-02-15 10:16:31 +08:00
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
Online_Friend *temp;
|
|
|
|
|
|
|
|
for (i = 0; i < m->numonline_friends; ++i) {
|
|
|
|
/* Equal */
|
|
|
|
if (m->online_friendlist[i].friend_num == (uint32_t)friendnumber) {
|
|
|
|
--m->numonline_friends;
|
|
|
|
|
|
|
|
if (m->numonline_friends != i) {
|
|
|
|
memcpy( &m->online_friendlist[i],
|
|
|
|
&m->online_friendlist[m->numonline_friends],
|
|
|
|
sizeof(Online_Friend) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m->numonline_friends == 0) {
|
|
|
|
free(m->online_friendlist);
|
|
|
|
m->online_friendlist = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
temp = realloc(m->online_friendlist, sizeof(Online_Friend) * (m->numonline_friends));
|
|
|
|
|
|
|
|
if (temp == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
m->online_friendlist = temp;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Set the size of the friend list to numfriends.
|
2013-09-03 00:12:02 +08:00
|
|
|
*
|
|
|
|
* return -1 if realloc fails.
|
2013-08-30 05:17:51 +08:00
|
|
|
*/
|
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-09-03 00:12:02 +08:00
|
|
|
/* return the friend id associated to that public key.
|
|
|
|
* return -1 if no such friend.
|
2013-08-30 05:17:51 +08:00
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
int32_t 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)
|
2013-10-25 04:32:28 +08:00
|
|
|
if (id_equal(client_id, m->friendlist[i].client_id))
|
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-08-30 05:17:51 +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.
|
2013-09-03 00:12:02 +08:00
|
|
|
*
|
|
|
|
* return 0 if success.
|
|
|
|
* return -1 if failure.
|
2013-08-30 05:17:51 +08:00
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
int getclient_id(Messenger *m, int32_t friendnumber, uint8_t *client_id)
|
2013-07-14 05:44:43 +08:00
|
|
|
{
|
2014-02-22 00:38:04 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-07-14 05:44:43 +08:00
|
|
|
return -1;
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
if (m->friendlist[friendnumber].status > 0) {
|
|
|
|
memcpy(client_id, m->friendlist[friendnumber].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-09-03 00:12:02 +08:00
|
|
|
/* TODO: Another checksum algorithm might be better.
|
2013-08-14 13:27:10 +08:00
|
|
|
*
|
2013-09-03 00:12:02 +08:00
|
|
|
* return a uint16_t that represents the checksum of address of length len.
|
2013-08-14 07:07:59 +08:00
|
|
|
*/
|
|
|
|
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-09-03 00:12:02 +08:00
|
|
|
/* Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
|
2013-08-14 13:27:10 +08:00
|
|
|
*
|
2013-09-03 00:12:02 +08:00
|
|
|
* return FRIEND_ADDRESS_SIZE byte address to give to others.
|
2013-08-13 21:32:31 +08:00
|
|
|
*/
|
|
|
|
void getaddress(Messenger *m, uint8_t *address)
|
|
|
|
{
|
2013-10-25 04:32:28 +08:00
|
|
|
id_copy(address, m->net_crypto->self_public_key);
|
2013-08-21 00:08:55 +08:00
|
|
|
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
|
|
|
/*
|
2013-08-30 05:17:51 +08:00
|
|
|
* Add a friend.
|
|
|
|
* Set the data that will be sent along with friend request.
|
|
|
|
* Address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes.
|
|
|
|
* data is the data and length is the length.
|
2013-09-03 00:12:02 +08:00
|
|
|
*
|
|
|
|
* return the friend number if success.
|
|
|
|
* 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.
|
|
|
|
* return FAERR_UNKNOWN for unknown error.
|
|
|
|
* return FAERR_BADCHECKSUM if bad checksum in address.
|
|
|
|
* return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different.
|
|
|
|
* (the nospam for that friend was set to the new one).
|
|
|
|
* return FAERR_NOMEM if increasing the friend list size fails.
|
2013-08-02 03:27:08 +08:00
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
int32_t 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];
|
2013-10-25 04:32:28 +08:00
|
|
|
id_copy(client_id, address);
|
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-10-25 04:32:28 +08:00
|
|
|
if (id_equal(client_id, m->net_crypto->self_public_key))
|
2013-08-03 00:33:11 +08:00
|
|
|
return FAERR_OWNKEY;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
int32_t 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-30 05:17:51 +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));
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
int32_t onion_friendnum = onion_addfriend(m->onion_c, client_id);
|
2014-01-18 02:35:40 +08:00
|
|
|
|
|
|
|
if (onion_friendnum == -1)
|
|
|
|
return FAERR_UNKNOWN;
|
|
|
|
|
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) {
|
2014-01-18 02:35:40 +08:00
|
|
|
m->friendlist[i].onion_friendnum = onion_friendnum;
|
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-10-25 04:32:28 +08:00
|
|
|
id_copy(m->friendlist[i].client_id, client_id);
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friendlist[i].statusmessage = calloc(1, 1);
|
|
|
|
m->friendlist[i].statusmessage_length = 1;
|
|
|
|
m->friendlist[i].userstatus = USERSTATUS_NONE;
|
2014-02-17 05:33:37 +08:00
|
|
|
m->friendlist[i].is_typing = 0;
|
2013-08-11 11:24:11 +08:00
|
|
|
memcpy(m->friendlist[i].info, data, length);
|
|
|
|
m->friendlist[i].info_size = length;
|
|
|
|
m->friendlist[i].message_id = 0;
|
2013-08-30 05:17:51 +08:00
|
|
|
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)
|
2014-03-14 08:20:48 +08:00
|
|
|
++m->numfriends;
|
2013-08-17 23:16:08 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
int32_t 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
|
|
|
|
2013-08-30 05:17:51 +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-10-25 04:32:28 +08:00
|
|
|
if (id_equal(client_id, m->net_crypto->self_public_key))
|
2013-10-05 21:09:28 +08:00
|
|
|
return FAERR_OWNKEY;
|
|
|
|
|
2013-08-17 23:16:08 +08:00
|
|
|
memset(&(m->friendlist[m->numfriends]), 0, sizeof(Friend));
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
int32_t onion_friendnum = onion_addfriend(m->onion_c, client_id);
|
2014-01-18 02:35:40 +08:00
|
|
|
|
|
|
|
if (onion_friendnum == -1)
|
|
|
|
return FAERR_UNKNOWN;
|
|
|
|
|
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) {
|
2014-01-18 02:35:40 +08:00
|
|
|
m->friendlist[i].onion_friendnum = onion_friendnum;
|
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-10-25 04:32:28 +08:00
|
|
|
id_copy(m->friendlist[i].client_id, client_id);
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friendlist[i].statusmessage = calloc(1, 1);
|
|
|
|
m->friendlist[i].statusmessage_length = 1;
|
|
|
|
m->friendlist[i].userstatus = USERSTATUS_NONE;
|
2014-02-17 05:33:37 +08:00
|
|
|
m->friendlist[i].is_typing = 0;
|
2013-08-11 11:24:11 +08:00
|
|
|
m->friendlist[i].message_id = 0;
|
2013-08-30 05:17:51 +08:00
|
|
|
m->friendlist[i].receives_read_receipts = 1; /* Default: YES. */
|
2013-08-17 23:16:08 +08:00
|
|
|
|
|
|
|
if (m->numfriends == i)
|
2014-03-14 08:20:48 +08:00
|
|
|
++m->numfriends;
|
2013-08-17 23:16:08 +08:00
|
|
|
|
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-08-30 05:17:51 +08:00
|
|
|
/* Remove a friend.
|
2013-09-03 00:12:02 +08:00
|
|
|
*
|
2013-08-30 05:17:51 +08:00
|
|
|
* return 0 if success.
|
2013-08-30 05:55:58 +08:00
|
|
|
* return -1 if failure.
|
2013-08-30 05:17:51 +08:00
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
int m_delfriend(Messenger *m, int32_t friendnumber)
|
2013-07-10 08:25:52 +08:00
|
|
|
{
|
2013-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-07-10 08:25:52 +08:00
|
|
|
return -1;
|
|
|
|
|
2014-02-08 09:11:36 +08:00
|
|
|
if (m->friendlist[friendnumber].status == FRIEND_ONLINE)
|
2014-02-15 10:16:31 +08:00
|
|
|
remove_online_friend(m, friendnumber);
|
2014-02-08 09:11:36 +08:00
|
|
|
|
2014-01-18 02:35:40 +08:00
|
|
|
onion_delfriend(m->onion_c, m->friendlist[friendnumber].onion_friendnum);
|
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
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
int m_get_friend_connectionstatus(Messenger *m, int32_t friendnumber)
|
2013-09-08 04:05:16 +08:00
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return m->friendlist[friendnumber].status == FRIEND_ONLINE;
|
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
int m_friend_exists(Messenger *m, int32_t friendnumber)
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2013-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-09-08 04:05:16 +08:00
|
|
|
return 0;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-09-08 04:05:16 +08:00
|
|
|
return m->friendlist[friendnumber].status > NOFRIEND;
|
2013-07-07 10:28:15 +08:00
|
|
|
}
|
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Send a text chat message to an online friend.
|
2013-09-03 00:12:02 +08:00
|
|
|
*
|
|
|
|
* return the message id if packet was successfully put into the send queue.
|
|
|
|
* return 0 if it was not.
|
2013-08-30 05:17:51 +08:00
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
uint32_t m_sendmessage(Messenger *m, int32_t friendnumber, uint8_t *message, uint32_t length)
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2013-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
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)
|
2013-08-30 05:17:51 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
uint32_t m_sendmessage_withid(Messenger *m, int32_t 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-30 05:17:51 +08:00
|
|
|
/* Send an action to an online friend.
|
2013-09-03 00:12:02 +08:00
|
|
|
*
|
2013-10-11 10:27:51 +08:00
|
|
|
* return the message id if packet was successfully put into the send queue.
|
2013-09-03 00:12:02 +08:00
|
|
|
* return 0 if it was not.
|
2013-08-30 05:17:51 +08:00
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
uint32_t m_sendaction(Messenger *m, int32_t friendnumber, uint8_t *action, uint32_t length)
|
2013-10-11 10:27:51 +08:00
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
uint32_t msgid = ++m->friendlist[friendnumber].message_id;
|
|
|
|
|
|
|
|
if (msgid == 0)
|
|
|
|
msgid = 1; // Otherwise, false error
|
|
|
|
|
|
|
|
if (m_sendaction_withid(m, friendnumber, msgid, action, length)) {
|
|
|
|
return msgid;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
uint32_t m_sendaction_withid(Messenger *m, int32_t friendnumber, uint32_t theid, uint8_t *action, uint32_t length)
|
2013-08-09 03:00:30 +08:00
|
|
|
{
|
2013-10-11 10:27:51 +08:00
|
|
|
if (length >= (MAX_DATA_SIZE - sizeof(theid)))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
uint8_t temp[MAX_DATA_SIZE];
|
|
|
|
theid = htonl(theid);
|
|
|
|
memcpy(temp, &theid, sizeof(theid));
|
|
|
|
memcpy(temp + sizeof(theid), action, length);
|
|
|
|
return write_cryptpacket_id(m, friendnumber, PACKET_ID_ACTION, temp, length + sizeof(theid));
|
2013-08-09 03:00:30 +08:00
|
|
|
}
|
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Send a name packet to friendnumber.
|
|
|
|
* length is the length with the NULL terminator.
|
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
static int m_sendname(Messenger *m, int32_t 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-09-09 18:41:33 +08:00
|
|
|
/* Set the name and name_length of a friend.
|
2013-09-03 00:12:02 +08:00
|
|
|
*
|
|
|
|
* return 0 if success.
|
|
|
|
* return -1 if failure.
|
2013-08-30 05:17:51 +08:00
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
int setfriendname(Messenger *m, int32_t friendnumber, uint8_t *name, uint16_t length)
|
2013-07-18 23:47:27 +08:00
|
|
|
{
|
2013-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-07-18 23:47:27 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-09-24 03:40:25 +08:00
|
|
|
if (length > MAX_NAME_LENGTH || length == 0)
|
|
|
|
return -1;
|
|
|
|
|
2013-09-23 16:30:24 +08:00
|
|
|
m->friendlist[friendnumber].name_length = length;
|
|
|
|
memcpy(m->friendlist[friendnumber].name, name, length);
|
2013-07-18 23:47:27 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-20 11:00:10 +08:00
|
|
|
/* Set our nickname
|
2013-08-30 05:17:51 +08:00
|
|
|
* 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.
|
2013-09-03 00:12:02 +08:00
|
|
|
*
|
|
|
|
* return 0 if success.
|
|
|
|
* return -1 if failure.
|
2013-08-30 05:17:51 +08:00
|
|
|
*/
|
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-11-13 02:56:32 +08:00
|
|
|
for (i = 0; i < m->numchats; i++)
|
2013-11-13 09:59:32 +08:00
|
|
|
if (m->chats[i] != NULL)
|
|
|
|
set_nick(m->chats[i], name, length); /* TODO: remove this (group nicks should not be tied to the global one) */
|
2013-11-13 02:56:32 +08:00
|
|
|
|
2013-07-18 23:47:27 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Get our nickname and put it in name.
|
2013-09-03 00:12:02 +08:00
|
|
|
* name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
|
|
|
|
*
|
|
|
|
* return the length of the name.
|
2013-08-30 05:17:51 +08:00
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
uint16_t getself_name(Messenger *m, uint8_t *name)
|
2013-07-30 23:09:09 +08:00
|
|
|
{
|
2014-02-22 00:38:04 +08:00
|
|
|
if (name == NULL) {
|
2013-08-11 21:24:47 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
memcpy(name, m->name, m->name_length);
|
2013-08-11 21:24:47 +08:00
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
return m->name_length;
|
2013-08-06 03:30:07 +08:00
|
|
|
}
|
2013-07-30 23:09:09 +08:00
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Get name of friendnumber and put it in name.
|
2013-09-03 00:12:02 +08:00
|
|
|
* name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
|
|
|
|
*
|
2013-09-06 21:45:39 +08:00
|
|
|
* return length of name if success.
|
2013-09-03 00:12:02 +08:00
|
|
|
* return -1 if failure.
|
2013-08-30 05:17:51 +08:00
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
int getname(Messenger *m, int32_t friendnumber, uint8_t *name)
|
2013-07-18 23:47:27 +08:00
|
|
|
{
|
2013-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-07-18 23:47:27 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-09-06 21:45:39 +08:00
|
|
|
memcpy(name, m->friendlist[friendnumber].name, m->friendlist[friendnumber].name_length);
|
|
|
|
return m->friendlist[friendnumber].name_length;
|
2013-07-18 23:47:27 +08:00
|
|
|
}
|
2013-07-07 10:28:15 +08:00
|
|
|
|
2014-02-22 09:30:38 +08:00
|
|
|
int m_get_name_size(Messenger *m, int32_t friendnumber)
|
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return m->friendlist[friendnumber].name_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
int m_get_self_name_size(Messenger *m)
|
|
|
|
{
|
|
|
|
return m->name_length;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-23 23:38:20 +08:00
|
|
|
int m_set_userstatus(Messenger *m, uint8_t 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-08-30 05:17:51 +08:00
|
|
|
/* return the size of friendnumber's user status.
|
|
|
|
* Guaranteed to be at most MAX_STATUSMESSAGE_LENGTH.
|
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
int m_get_statusmessage_size(Messenger *m, int32_t friendnumber)
|
2013-07-19 01:56:50 +08:00
|
|
|
{
|
2013-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
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-08-30 05:17:51 +08:00
|
|
|
/* Copy the user status of friendnumber into buf, truncating if needed to maxlen
|
|
|
|
* bytes, use m_get_statusmessage_size to find out how much you need to allocate.
|
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
int m_copy_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *buf, uint32_t maxlen)
|
2013-07-19 01:56:50 +08:00
|
|
|
{
|
2013-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
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-09-08 01:27:44 +08:00
|
|
|
memcpy(buf, m->friendlist[friendnumber].statusmessage, MIN(maxlen, m->friendlist[friendnumber].statusmessage_length));
|
|
|
|
return MIN(maxlen, m->friendlist[friendnumber].statusmessage_length);
|
2013-07-19 01:56:50 +08:00
|
|
|
}
|
|
|
|
|
2014-02-22 09:30:38 +08:00
|
|
|
/* return the size of friendnumber's user status.
|
|
|
|
* Guaranteed to be at most MAX_STATUSMESSAGE_LENGTH.
|
|
|
|
*/
|
|
|
|
int m_get_self_statusmessage_size(Messenger *m)
|
|
|
|
{
|
|
|
|
return m->statusmessage_length;
|
|
|
|
}
|
|
|
|
|
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-09-08 01:27:44 +08:00
|
|
|
memcpy(buf, m->statusmessage, MIN(maxlen, m->statusmessage_length));
|
|
|
|
return MIN(maxlen, m->statusmessage_length);
|
2013-08-06 03:30:07 +08:00
|
|
|
}
|
|
|
|
|
2014-02-23 23:38:20 +08:00
|
|
|
uint8_t m_get_userstatus(Messenger *m, int32_t friendnumber)
|
2013-08-08 05:01:31 +08:00
|
|
|
{
|
2013-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-08-08 05:01:31 +08:00
|
|
|
return USERSTATUS_INVALID;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-02-23 23:38:20 +08:00
|
|
|
uint8_t 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;
|
|
|
|
}
|
|
|
|
|
2014-02-23 23:38:20 +08:00
|
|
|
uint8_t 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
|
|
|
}
|
|
|
|
|
2014-03-19 08:02:50 +08:00
|
|
|
uint64_t m_get_last_online(Messenger *m, int32_t friendnumber)
|
2014-03-14 08:20:48 +08:00
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return m->friendlist[friendnumber].ping_lastrecv;
|
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
int m_set_usertyping(Messenger *m, int32_t friendnumber, uint8_t is_typing)
|
2014-03-19 08:02:50 +08:00
|
|
|
|
2014-02-17 05:33:37 +08:00
|
|
|
{
|
2014-02-17 08:41:40 +08:00
|
|
|
if (is_typing != 0 && is_typing != 1) {
|
2014-02-17 05:33:37 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2014-02-19 02:49:17 +08:00
|
|
|
|
2014-02-17 05:33:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
2014-02-19 02:49:17 +08:00
|
|
|
|
2014-02-17 05:33:37 +08:00
|
|
|
m->friendlist[friendnumber].user_istyping = is_typing;
|
|
|
|
m->friendlist[friendnumber].user_istyping_sent = 0;
|
2014-02-19 02:49:17 +08:00
|
|
|
|
2014-02-17 05:33:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
int m_get_istyping(Messenger *m, int32_t friendnumber)
|
2014-02-17 05:33:37 +08:00
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
2014-02-19 02:49:17 +08:00
|
|
|
|
2014-02-17 05:33:37 +08:00
|
|
|
return m->friendlist[friendnumber].is_typing;
|
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
static int send_statusmessage(Messenger *m, int32_t 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
|
|
|
}
|
|
|
|
|
2014-02-23 23:38:20 +08:00
|
|
|
static int send_userstatus(Messenger *m, int32_t friendnumber, uint8_t status)
|
2013-07-19 01:56:50 +08:00
|
|
|
{
|
2014-02-23 23:38:20 +08:00
|
|
|
return write_cryptpacket_id(m, friendnumber, PACKET_ID_USERSTATUS, &status, sizeof(status));
|
2013-07-19 01:56:50 +08:00
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
static int send_user_istyping(Messenger *m, int32_t friendnumber, uint8_t is_typing)
|
2014-02-17 05:33:37 +08:00
|
|
|
{
|
|
|
|
uint8_t typing = is_typing;
|
|
|
|
return write_cryptpacket_id(m, friendnumber, PACKET_ID_TYPING, &typing, sizeof(typing));
|
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
static int send_ping(Messenger *m, int32_t friendnumber)
|
2013-08-19 19:00:59 +08:00
|
|
|
{
|
2013-09-24 21:57:49 +08:00
|
|
|
int ret = write_cryptpacket_id(m, friendnumber, PACKET_ID_PING, 0, 0);
|
2013-09-28 10:03:38 +08:00
|
|
|
|
2013-09-24 21:57:49 +08:00
|
|
|
if (ret == 1)
|
2013-09-28 10:03:38 +08:00
|
|
|
m->friendlist[friendnumber].ping_lastsent = unix_time();
|
|
|
|
|
2013-09-24 21:57:49 +08:00
|
|
|
return ret;
|
2013-08-19 19:00:59 +08:00
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
static int set_friend_statusmessage(Messenger *m, int32_t friendnumber, uint8_t *status, uint16_t length)
|
2013-07-19 01:56:50 +08:00
|
|
|
{
|
2013-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-07-19 01:56:50 +08:00
|
|
|
return -1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-02-23 23:23:55 +08:00
|
|
|
uint8_t *newstatus = calloc(length + 1, 1);
|
2013-07-19 01:56:50 +08:00
|
|
|
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
|
|
|
|
2014-02-23 23:38:20 +08:00
|
|
|
static void set_friend_userstatus(Messenger *m, int32_t friendnumber, uint8_t 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
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
static void set_friend_typing(Messenger *m, int32_t friendnumber, uint8_t is_typing)
|
2014-02-17 05:33:37 +08:00
|
|
|
{
|
|
|
|
m->friendlist[friendnumber].is_typing = is_typing;
|
|
|
|
}
|
|
|
|
|
2013-08-08 01:06:07 +08:00
|
|
|
/* Sets whether we send read receipts for friendnumber. */
|
2014-02-22 00:38:04 +08:00
|
|
|
void m_set_sends_receipts(Messenger *m, int32_t friendnumber, int yesno)
|
2013-08-08 01:06:07 +08:00
|
|
|
{
|
2013-09-23 03:36:45 +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-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
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-08-30 05:17:51 +08:00
|
|
|
/* Set the function that will be executed when a friend request is received. */
|
2014-03-17 01:24:39 +08:00
|
|
|
void m_callback_friendrequest(Messenger *m, void (*function)(Messenger *m, uint8_t *, uint8_t *, uint16_t, void *),
|
|
|
|
void *userdata)
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2014-03-19 08:30:45 +08:00
|
|
|
void (*handle_friendrequest)(void *, uint8_t *, uint8_t *, uint16_t, void *) = (void *)function;
|
2014-03-17 01:24:39 +08:00
|
|
|
callback_friendrequest(&(m->fr), handle_friendrequest, m, userdata);
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Set the function that will be executed when a message from a friend is received. */
|
2014-02-22 00:38:04 +08:00
|
|
|
void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *),
|
2013-08-17 01:11:09 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
void m_callback_action(Messenger *m, void (*function)(Messenger *m, int32_t, 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
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *),
|
2013-08-17 01:11:09 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *),
|
2013-08-17 01:11:09 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-23 23:38:20 +08:00
|
|
|
void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, 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
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
void m_callback_typingchange(Messenger *m, void(*function)(Messenger *m, int32_t, int, void *), void *userdata)
|
2014-02-17 05:33:37 +08:00
|
|
|
{
|
|
|
|
m->friend_typingchange = function;
|
|
|
|
m->friend_typingchange_userdata = userdata;
|
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int32_t, 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
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int32_t, 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
|
|
|
}
|
2014-02-16 02:10:53 +08:00
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
void m_callback_connectionstatus_internal_av(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, void *),
|
2014-02-16 02:10:53 +08:00
|
|
|
void *userdata)
|
|
|
|
{
|
|
|
|
m->friend_connectionstatuschange_internal = function;
|
|
|
|
m->friend_connectionstatuschange_internal_userdata = userdata;
|
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
static void break_files(Messenger *m, int32_t friendnumber);
|
|
|
|
static void check_friend_connectionstatus(Messenger *m, int32_t friendnumber, uint8_t status)
|
2013-08-12 02:57:47 +08:00
|
|
|
{
|
|
|
|
if (status == NOFRIEND)
|
|
|
|
return;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-09-27 17:24:33 +08:00
|
|
|
const uint8_t was_online = m->friendlist[friendnumber].status == FRIEND_ONLINE;
|
|
|
|
const uint8_t is_online = status == FRIEND_ONLINE;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2014-01-19 07:35:28 +08:00
|
|
|
onion_set_friend_online(m->onion_c, m->friendlist[friendnumber].onion_friendnum, is_online);
|
|
|
|
|
2013-10-02 04:48:32 +08:00
|
|
|
if (is_online != was_online) {
|
2014-02-08 08:43:21 +08:00
|
|
|
if (was_online) {
|
2013-10-02 04:48:32 +08:00
|
|
|
break_files(m, friendnumber);
|
2014-02-15 10:16:31 +08:00
|
|
|
remove_online_friend(m, friendnumber);
|
2014-02-08 08:43:21 +08:00
|
|
|
} else {
|
2014-02-15 10:16:31 +08:00
|
|
|
add_online_friend(m, friendnumber);
|
2014-02-08 08:43:21 +08:00
|
|
|
}
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2014-02-16 01:44:57 +08:00
|
|
|
if (m->friend_connectionstatuschange)
|
|
|
|
m->friend_connectionstatuschange(m, friendnumber, is_online, m->friend_connectionstatuschange_userdata);
|
2014-02-16 02:10:53 +08:00
|
|
|
|
|
|
|
if (m->friend_connectionstatuschange_internal)
|
|
|
|
m->friend_connectionstatuschange_internal(m, friendnumber, is_online,
|
|
|
|
m->friend_connectionstatuschange_internal_userdata);
|
2013-10-02 04:48:32 +08:00
|
|
|
}
|
2013-08-08 13:46:27 +08:00
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
void set_friend_status(Messenger *m, int32_t 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
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
int write_cryptpacket_id(Messenger *m, int32_t friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length)
|
2013-08-10 03:58:08 +08:00
|
|
|
{
|
2013-09-02 21:55:37 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
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-09-12 08:42:25 +08:00
|
|
|
/**********GROUP CHATS************/
|
|
|
|
|
2013-11-23 12:31:55 +08:00
|
|
|
/* return 1 if the groupnumber is not valid.
|
|
|
|
* return 0 if the groupnumber is valid.
|
|
|
|
*/
|
|
|
|
static uint8_t groupnumber_not_valid(Messenger *m, int groupnumber)
|
|
|
|
{
|
|
|
|
if ((unsigned int)groupnumber >= m->numchats)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (m->chats == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (m->chats[groupnumber] == NULL)
|
|
|
|
return 1;
|
2013-12-08 08:06:41 +08:00
|
|
|
|
2013-11-23 12:31:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-12 08:42:25 +08:00
|
|
|
/* returns valid ip port of connected friend on success
|
|
|
|
* returns zeroed out IP_Port on failure
|
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
IP_Port get_friend_ipport(Messenger *m, int32_t friendnumber)
|
2013-09-12 08:42:25 +08:00
|
|
|
{
|
|
|
|
IP_Port zero;
|
|
|
|
memset(&zero, 0, sizeof(zero));
|
|
|
|
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return zero;
|
|
|
|
|
|
|
|
int crypt_id = m->friendlist[friendnumber].crypt_connection_id;
|
|
|
|
|
2013-10-24 02:32:09 +08:00
|
|
|
if (is_cryptoconnected(m->net_crypto, crypt_id) != CRYPTO_CONN_ESTABLISHED)
|
2013-09-12 08:42:25 +08:00
|
|
|
return zero;
|
|
|
|
|
|
|
|
return connection_ip(m->net_crypto->lossless_udp, m->net_crypto->crypto_connections[crypt_id].number);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns the group number of the chat with public key group_public_key.
|
|
|
|
* returns -1 on failure.
|
|
|
|
*/
|
|
|
|
static int group_num(Messenger *m, uint8_t *group_public_key)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < m->numchats; ++i) {
|
2013-11-18 05:23:28 +08:00
|
|
|
if (m->chats[i] != NULL)
|
|
|
|
if (id_equal(m->chats[i]->self_public_key, group_public_key))
|
2013-11-22 08:33:02 +08:00
|
|
|
return i;
|
2013-09-12 08:42:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-09-13 07:26:30 +08:00
|
|
|
/* Set the callback for group invites.
|
|
|
|
*
|
2014-02-22 00:38:04 +08:00
|
|
|
* Function(Messenger *m, int32_t friendnumber, uint8_t *group_public_key, void *userdata)
|
2013-09-13 07:26:30 +08:00
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
void m_callback_group_invite(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, void *), void *userdata)
|
2013-09-13 07:26:30 +08:00
|
|
|
{
|
|
|
|
m->group_invite = function;
|
|
|
|
m->group_invite_userdata = userdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the callback for group messages.
|
|
|
|
*
|
2013-09-17 08:59:53 +08:00
|
|
|
* Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata)
|
2013-09-13 07:26:30 +08:00
|
|
|
*/
|
2013-09-17 08:59:53 +08:00
|
|
|
void m_callback_group_message(Messenger *m, void (*function)(Messenger *m, int, int, uint8_t *, uint16_t, void *),
|
2013-09-13 07:26:30 +08:00
|
|
|
void *userdata)
|
|
|
|
{
|
|
|
|
m->group_message = function;
|
|
|
|
m->group_message_userdata = userdata;
|
|
|
|
}
|
2013-11-24 06:19:02 +08:00
|
|
|
|
2013-12-14 09:27:25 +08:00
|
|
|
/* Set the callback for group actions.
|
|
|
|
*
|
2013-12-15 01:23:25 +08:00
|
|
|
* Function(Tox *tox, int groupnumber, int friendgroupnumber, uint8_t * message, uint16_t length, void *userdata)
|
2013-12-14 09:27:25 +08:00
|
|
|
*/
|
|
|
|
void m_callback_group_action(Messenger *m, void (*function)(Messenger *m, int, int, uint8_t *, uint16_t, void *),
|
|
|
|
void *userdata)
|
|
|
|
{
|
|
|
|
m->group_action = function;
|
|
|
|
m->group_action_userdata = userdata;
|
|
|
|
}
|
|
|
|
|
2013-11-24 06:19:02 +08:00
|
|
|
/* Set callback function for peer name list changes.
|
|
|
|
*
|
|
|
|
* It gets called every time the name list changes(new peer/name, deleted peer)
|
|
|
|
* Function(Tox *tox, int groupnumber, void *userdata)
|
|
|
|
*/
|
2013-12-08 08:06:41 +08:00
|
|
|
void m_callback_group_namelistchange(Messenger *m, void (*function)(Messenger *m, int, int, uint8_t, void *),
|
|
|
|
void *userdata)
|
2013-09-13 07:26:30 +08:00
|
|
|
{
|
2013-11-24 06:19:02 +08:00
|
|
|
m->group_namelistchange = function;
|
|
|
|
m->group_namelistchange_userdata = userdata;
|
|
|
|
}
|
2013-09-13 07:26:30 +08:00
|
|
|
|
2013-11-24 06:19:02 +08:00
|
|
|
static int get_chat_num(Messenger *m, Group_Chat *chat)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
2013-12-08 08:06:41 +08:00
|
|
|
|
2013-09-13 07:26:30 +08:00
|
|
|
for (i = 0; i < m->numchats; ++i) { //TODO: remove this
|
|
|
|
if (m->chats[i] == chat)
|
2013-11-24 06:19:02 +08:00
|
|
|
return i;
|
2013-09-13 07:26:30 +08:00
|
|
|
}
|
2013-12-08 08:06:41 +08:00
|
|
|
|
2013-11-24 06:19:02 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void group_message_function(Group_Chat *chat, int peer_number, uint8_t *message, uint16_t length, void *userdata)
|
|
|
|
{
|
|
|
|
Messenger *m = userdata;
|
|
|
|
int i = get_chat_num(m, chat);
|
2013-12-08 08:06:41 +08:00
|
|
|
|
2013-11-24 06:19:02 +08:00
|
|
|
if (i == -1)
|
|
|
|
return;
|
2013-09-13 07:26:30 +08:00
|
|
|
|
2014-02-23 23:23:55 +08:00
|
|
|
uint8_t message_terminated[length + 1];
|
|
|
|
memcpy(message_terminated, message, length);
|
|
|
|
message_terminated[length] = 0; /* Force NULL terminator */
|
2014-02-13 02:14:25 +08:00
|
|
|
|
2013-09-13 07:26:30 +08:00
|
|
|
if (m->group_message)
|
2014-02-23 23:23:55 +08:00
|
|
|
(*m->group_message)(m, i, peer_number, message_terminated, length, m->group_message_userdata);
|
2013-09-13 07:26:30 +08:00
|
|
|
}
|
|
|
|
|
2013-12-14 09:27:25 +08:00
|
|
|
static void group_action_function(Group_Chat *chat, int peer_number, uint8_t *action, uint16_t length, void *userdata)
|
|
|
|
{
|
|
|
|
Messenger *m = userdata;
|
|
|
|
int i = get_chat_num(m, chat);
|
|
|
|
|
|
|
|
if (i == -1)
|
|
|
|
return;
|
|
|
|
|
2014-02-23 23:23:55 +08:00
|
|
|
uint8_t action_terminated[length + 1];
|
|
|
|
memcpy(action_terminated, action, length);
|
|
|
|
action_terminated[length] = 0; /* Force NULL terminator */
|
2014-02-13 02:14:25 +08:00
|
|
|
|
2013-12-14 09:27:25 +08:00
|
|
|
if (m->group_action)
|
2014-02-23 23:23:55 +08:00
|
|
|
(*m->group_action)(m, i, peer_number, action_terminated, length, m->group_action_userdata);
|
2013-12-14 09:27:25 +08:00
|
|
|
}
|
|
|
|
|
2013-11-25 04:11:54 +08:00
|
|
|
static void group_namelistchange_function(Group_Chat *chat, int peer, uint8_t change, void *userdata)
|
2013-11-24 06:19:02 +08:00
|
|
|
{
|
|
|
|
Messenger *m = userdata;
|
|
|
|
int i = get_chat_num(m, chat);
|
2013-12-08 08:06:41 +08:00
|
|
|
|
2013-11-24 06:19:02 +08:00
|
|
|
if (i == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (m->group_namelistchange)
|
2013-11-25 04:11:54 +08:00
|
|
|
(*m->group_namelistchange)(m, i, peer, change, m->group_namelistchange_userdata);
|
2013-11-24 06:19:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-12 08:42:25 +08:00
|
|
|
/* Creates a new groupchat and puts it in the chats array.
|
|
|
|
*
|
|
|
|
* return group number on success.
|
|
|
|
* return -1 on failure.
|
|
|
|
*/
|
|
|
|
int add_groupchat(Messenger *m)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < m->numchats; ++i) {
|
|
|
|
if (m->chats[i] == NULL) {
|
|
|
|
Group_Chat *newchat = new_groupchat(m->net);
|
|
|
|
|
|
|
|
if (newchat == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2013-09-13 07:26:30 +08:00
|
|
|
callback_groupmessage(newchat, &group_message_function, m);
|
2013-12-14 09:27:25 +08:00
|
|
|
callback_groupaction(newchat, &group_action_function, m);
|
2013-11-24 06:19:02 +08:00
|
|
|
callback_namelistchange(newchat, &group_namelistchange_function, m);
|
2013-11-13 09:59:32 +08:00
|
|
|
/* TODO: remove this (group nicks should not be tied to the global one) */
|
|
|
|
set_nick(newchat, m->name, m->name_length);
|
2013-09-12 08:42:25 +08:00
|
|
|
m->chats[i] = newchat;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Group_Chat **temp;
|
|
|
|
temp = realloc(m->chats, sizeof(Group_Chat *) * (m->numchats + 1));
|
|
|
|
|
|
|
|
if (temp == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2013-12-09 22:30:35 +08:00
|
|
|
m->chats = temp;
|
2013-09-12 08:42:25 +08:00
|
|
|
temp[m->numchats] = new_groupchat(m->net);
|
|
|
|
|
|
|
|
if (temp[m->numchats] == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2013-09-13 07:26:30 +08:00
|
|
|
callback_groupmessage(temp[m->numchats], &group_message_function, m);
|
2013-12-14 09:27:25 +08:00
|
|
|
callback_groupaction(temp[m->numchats], &group_action_function, m);
|
2013-11-24 06:19:02 +08:00
|
|
|
callback_namelistchange(temp[m->numchats], &group_namelistchange_function, m);
|
2013-11-13 09:59:32 +08:00
|
|
|
/* TODO: remove this (group nicks should not be tied to the global one) */
|
|
|
|
set_nick(temp[m->numchats], m->name, m->name_length);
|
2013-09-12 08:42:25 +08:00
|
|
|
++m->numchats;
|
|
|
|
return (m->numchats - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete a groupchat from the chats array.
|
|
|
|
*
|
|
|
|
* return 0 on success.
|
|
|
|
* return -1 if failure.
|
|
|
|
*/
|
2013-09-13 07:26:30 +08:00
|
|
|
int del_groupchat(Messenger *m, int groupnumber)
|
2013-09-12 08:42:25 +08:00
|
|
|
{
|
|
|
|
if ((unsigned int)groupnumber >= m->numchats)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (m->chats == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (m->chats[groupnumber] == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
kill_groupchat(m->chats[groupnumber]);
|
|
|
|
m->chats[groupnumber] = NULL;
|
|
|
|
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for (i = m->numchats; i != 0; --i) {
|
|
|
|
if (m->chats[i - 1] != NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-09-17 18:52:02 +08:00
|
|
|
m->numchats = i;
|
|
|
|
|
2013-09-12 08:42:25 +08:00
|
|
|
if (i == 0) {
|
|
|
|
free(m->chats);
|
|
|
|
m->chats = NULL;
|
|
|
|
} else {
|
|
|
|
Group_Chat **temp = realloc(m->chats, sizeof(Group_Chat *) * i);
|
|
|
|
|
|
|
|
if (temp != NULL)
|
|
|
|
m->chats = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-17 08:59:53 +08:00
|
|
|
/* Copy the name of peernumber who is in groupnumber to name.
|
|
|
|
* name must be at least MAX_NICK_BYTES long.
|
2013-09-18 04:28:39 +08:00
|
|
|
*
|
2013-09-17 08:59:53 +08:00
|
|
|
* return length of name if success
|
|
|
|
* return -1 if failure
|
|
|
|
*/
|
|
|
|
int m_group_peername(Messenger *m, int groupnumber, int peernumber, uint8_t *name)
|
|
|
|
{
|
|
|
|
if ((unsigned int)groupnumber >= m->numchats)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (m->chats == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (m->chats[groupnumber] == NULL)
|
|
|
|
return -1;
|
2013-09-18 04:28:39 +08:00
|
|
|
|
2013-09-17 08:59:53 +08:00
|
|
|
return group_peername(m->chats[groupnumber], peernumber, name);
|
|
|
|
}
|
2013-11-04 06:39:49 +08:00
|
|
|
|
|
|
|
/* Store the fact that we invited a specific friend.
|
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
static void group_store_friendinvite(Messenger *m, int32_t friendnumber, int groupnumber)
|
2013-11-04 06:39:49 +08:00
|
|
|
{
|
|
|
|
/* Add 1 to the groupchat number because 0 (default value in invited_groups) is a valid groupchat number */
|
|
|
|
m->friendlist[friendnumber].invited_groups[m->friendlist[friendnumber].invited_groups_num % MAX_INVITED_GROUPS] =
|
|
|
|
groupnumber + 1;
|
|
|
|
++m->friendlist[friendnumber].invited_groups_num;
|
|
|
|
}
|
|
|
|
|
2013-09-13 07:26:30 +08:00
|
|
|
/* return 1 if that friend was invited to the group
|
|
|
|
* return 0 if the friend was not or error.
|
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
static uint8_t group_invited(Messenger *m, int32_t friendnumber, int groupnumber)
|
2013-09-13 07:26:30 +08:00
|
|
|
{
|
2013-11-04 06:39:49 +08:00
|
|
|
|
|
|
|
uint32_t i;
|
|
|
|
uint16_t num = MAX_INVITED_GROUPS;
|
|
|
|
|
|
|
|
if (MAX_INVITED_GROUPS > m->friendlist[friendnumber].invited_groups_num)
|
|
|
|
num = m->friendlist[friendnumber].invited_groups_num;
|
|
|
|
|
|
|
|
for (i = 0; i < num; ++i) {
|
|
|
|
if (m->friendlist[friendnumber].invited_groups[i] == groupnumber + 1) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2013-09-13 07:26:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* invite friendnumber to groupnumber
|
|
|
|
* return 0 on success
|
|
|
|
* return -1 on failure
|
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
int invite_friend(Messenger *m, int32_t friendnumber, int groupnumber)
|
2013-09-13 07:26:30 +08:00
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber) || (unsigned int)groupnumber >= m->numchats)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (m->chats == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (m->friendlist[friendnumber].status == NOFRIEND || m->chats[groupnumber] == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2013-11-04 06:39:49 +08:00
|
|
|
group_store_friendinvite(m, friendnumber, groupnumber);
|
|
|
|
|
2013-09-13 07:26:30 +08:00
|
|
|
if (write_cryptpacket_id(m, friendnumber, PACKET_ID_INVITE_GROUPCHAT, m->chats[groupnumber]->self_public_key,
|
|
|
|
crypto_box_PUBLICKEYBYTES) == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-12 08:42:25 +08:00
|
|
|
/* Join a group (you need to have been invited first.)
|
|
|
|
*
|
2013-09-13 07:26:30 +08:00
|
|
|
* returns group number on success
|
2013-09-12 08:42:25 +08:00
|
|
|
* returns -1 on failure.
|
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
int join_groupchat(Messenger *m, int32_t friendnumber, uint8_t *friend_group_public_key)
|
2013-09-12 08:42:25 +08:00
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
uint8_t data[crypto_box_PUBLICKEYBYTES * 2];
|
|
|
|
int groupnum = add_groupchat(m);
|
|
|
|
|
|
|
|
if (groupnum == -1)
|
|
|
|
return -1;
|
|
|
|
|
2013-10-25 04:32:28 +08:00
|
|
|
id_copy(data, friend_group_public_key);
|
|
|
|
id_copy(data + crypto_box_PUBLICKEYBYTES, m->chats[groupnum]->self_public_key);
|
2013-09-12 08:42:25 +08:00
|
|
|
|
|
|
|
if (write_cryptpacket_id(m, friendnumber, PACKET_ID_JOIN_GROUPCHAT, data, sizeof(data))) {
|
|
|
|
chat_bootstrap_nonlazy(m->chats[groupnum], get_friend_ipport(m, friendnumber),
|
|
|
|
friend_group_public_key); //TODO: check if ip returned is zero?
|
2013-09-13 07:26:30 +08:00
|
|
|
return groupnum;
|
2013-09-12 08:42:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-11-23 12:31:55 +08:00
|
|
|
|
2013-09-13 07:26:30 +08:00
|
|
|
/* send a group message
|
|
|
|
* return 0 on success
|
|
|
|
* return -1 on failure
|
|
|
|
*/
|
|
|
|
int group_message_send(Messenger *m, int groupnumber, uint8_t *message, uint32_t length)
|
|
|
|
{
|
2013-11-23 12:31:55 +08:00
|
|
|
if (groupnumber_not_valid(m, groupnumber))
|
2013-09-13 07:26:30 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (group_sendmessage(m->chats[groupnumber], message, length) > 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-12-14 09:27:25 +08:00
|
|
|
/* send a group action
|
|
|
|
* return 0 on success
|
|
|
|
* return -1 on failure
|
|
|
|
*/
|
|
|
|
int group_action_send(Messenger *m, int groupnumber, uint8_t *action, uint32_t length)
|
|
|
|
{
|
|
|
|
if (groupnumber_not_valid(m, groupnumber))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (group_sendaction(m->chats[groupnumber], action, length) > 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-11-23 12:31:55 +08:00
|
|
|
/* Return the number of peers in the group chat on success.
|
|
|
|
* return -1 on failure
|
|
|
|
*/
|
|
|
|
int group_number_peers(Messenger *m, int groupnumber)
|
|
|
|
{
|
|
|
|
if (groupnumber_not_valid(m, groupnumber))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return group_numpeers(m->chats[groupnumber]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* List all the peers in the group chat.
|
|
|
|
*
|
|
|
|
* Copies the names of the peers to the name[length][MAX_NICK_BYTES] array.
|
|
|
|
*
|
|
|
|
* returns the number of peers on success.
|
|
|
|
*
|
|
|
|
* return -1 on failure.
|
|
|
|
*/
|
|
|
|
int group_names(Messenger *m, int groupnumber, uint8_t names[][MAX_NICK_BYTES], uint16_t length)
|
|
|
|
{
|
|
|
|
if (groupnumber_not_valid(m, groupnumber))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return group_client_names(m->chats[groupnumber], names, length);
|
|
|
|
}
|
|
|
|
|
2013-09-12 08:42:25 +08:00
|
|
|
static int handle_group(void *object, IP_Port source, uint8_t *packet, uint32_t length)
|
|
|
|
{
|
|
|
|
Messenger *m = object;
|
|
|
|
|
|
|
|
if (length < crypto_box_PUBLICKEYBYTES + 1) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < m->numchats; ++i) {
|
|
|
|
if (m->chats[i] == NULL)
|
|
|
|
continue;
|
|
|
|
|
2013-10-25 04:32:28 +08:00
|
|
|
if (id_equal(packet + 1, m->chats[i]->self_public_key))
|
2013-09-12 08:42:25 +08:00
|
|
|
return handle_groupchatpacket(m->chats[i], source, packet, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_allgroupchats(Messenger *m)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < m->numchats; ++i) {
|
|
|
|
if (m->chats[i] != NULL)
|
|
|
|
do_groupchat(m->chats[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-30 21:35:17 +08:00
|
|
|
/****************FILE SENDING*****************/
|
|
|
|
|
|
|
|
|
|
|
|
/* Set the callback for file send requests.
|
|
|
|
*
|
2014-02-22 00:38:04 +08:00
|
|
|
* Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata)
|
2013-09-30 21:35:17 +08:00
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
void callback_file_sendrequest(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint64_t, uint8_t *,
|
|
|
|
uint16_t,
|
2013-09-30 21:35:17 +08:00
|
|
|
void *), void *userdata)
|
|
|
|
{
|
|
|
|
m->file_sendrequest = function;
|
|
|
|
m->file_sendrequest_userdata = userdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the callback for file control requests.
|
|
|
|
*
|
2014-02-22 00:38:04 +08:00
|
|
|
* Function(Tox *tox, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata)
|
2013-09-30 21:35:17 +08:00
|
|
|
*
|
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
void callback_file_control(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint8_t, uint8_t, uint8_t *,
|
2013-10-03 08:08:14 +08:00
|
|
|
uint16_t,
|
2013-09-30 21:35:17 +08:00
|
|
|
void *), void *userdata)
|
|
|
|
{
|
|
|
|
m->file_filecontrol = function;
|
|
|
|
m->file_filecontrol_userdata = userdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the callback for file data.
|
|
|
|
*
|
2014-02-22 00:38:04 +08:00
|
|
|
* Function(Tox *tox, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata)
|
2013-09-30 21:35:17 +08:00
|
|
|
*
|
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
void callback_file_data(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t, uint8_t *, uint16_t length,
|
|
|
|
void *),
|
2013-09-30 21:35:17 +08:00
|
|
|
void *userdata)
|
|
|
|
{
|
|
|
|
m->file_filedata = function;
|
|
|
|
m->file_filedata_userdata = userdata;
|
|
|
|
}
|
|
|
|
|
2013-10-02 22:28:08 +08:00
|
|
|
#define MAX_FILENAME_LENGTH 255
|
2013-09-30 21:35:17 +08:00
|
|
|
|
|
|
|
/* Send a file send request.
|
2013-10-02 22:28:08 +08:00
|
|
|
* Maximum filename length is 255 bytes.
|
2013-09-30 21:35:17 +08:00
|
|
|
* return 1 on success
|
|
|
|
* return 0 on failure
|
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
int file_sendrequest(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename,
|
2013-09-30 21:35:17 +08:00
|
|
|
uint16_t filename_length)
|
|
|
|
{
|
2013-10-02 04:48:32 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return 0;
|
|
|
|
|
2013-09-30 21:35:17 +08:00
|
|
|
if (filename_length > MAX_FILENAME_LENGTH)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
uint8_t packet[MAX_FILENAME_LENGTH + 1 + sizeof(filesize)];
|
|
|
|
packet[0] = filenumber;
|
2013-10-16 05:23:00 +08:00
|
|
|
host_to_net((uint8_t *)&filesize, sizeof(filesize));
|
2013-09-30 21:35:17 +08:00
|
|
|
memcpy(packet + 1, &filesize, sizeof(filesize));
|
|
|
|
memcpy(packet + 1 + sizeof(filesize), filename, filename_length);
|
|
|
|
return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_SENDREQUEST, packet,
|
|
|
|
1 + sizeof(filesize) + filename_length);
|
|
|
|
}
|
|
|
|
|
2013-10-02 04:48:32 +08:00
|
|
|
/* Send a file send request.
|
2013-10-02 22:28:08 +08:00
|
|
|
* Maximum filename length is 255 bytes.
|
2013-10-02 04:48:32 +08:00
|
|
|
* return file number on success
|
|
|
|
* return -1 on failure
|
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
int new_filesender(Messenger *m, int32_t friendnumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length)
|
2013-10-02 04:48:32 +08:00
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-11-29 23:03:05 +08:00
|
|
|
return -1;
|
2013-10-02 04:48:32 +08:00
|
|
|
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) {
|
2013-10-04 00:42:29 +08:00
|
|
|
if (m->friendlist[friendnumber].file_sending[i].status == FILESTATUS_NONE)
|
2013-10-02 04:48:32 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == MAX_CONCURRENT_FILE_PIPES)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (file_sendrequest(m, friendnumber, i, filesize, filename, filename_length) == 0)
|
|
|
|
return -1;
|
|
|
|
|
2013-10-04 00:42:29 +08:00
|
|
|
m->friendlist[friendnumber].file_sending[i].status = FILESTATUS_NOT_ACCEPTED;
|
2013-10-02 04:48:32 +08:00
|
|
|
m->friendlist[friendnumber].file_sending[i].size = filesize;
|
|
|
|
m->friendlist[friendnumber].file_sending[i].transferred = 0;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2013-09-30 21:35:17 +08:00
|
|
|
/* Send a file control request.
|
2013-10-03 08:08:14 +08:00
|
|
|
* send_receive is 0 if we want the control packet to target a sending file, 1 if it targets a receiving file.
|
2013-09-30 21:35:17 +08:00
|
|
|
*
|
2013-11-29 18:26:09 +08:00
|
|
|
* return 0 on success
|
|
|
|
* return -1 on failure
|
2013-09-30 21:35:17 +08:00
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
int file_control(Messenger *m, int32_t friendnumber, uint8_t send_receive, uint8_t filenumber, uint8_t message_id,
|
2013-10-03 08:08:14 +08:00
|
|
|
uint8_t *data, uint16_t length)
|
2013-09-30 21:35:17 +08:00
|
|
|
{
|
2013-10-03 08:08:14 +08:00
|
|
|
if (length > MAX_DATA_SIZE - 3)
|
2013-11-29 18:26:09 +08:00
|
|
|
return -1;
|
2013-09-30 21:35:17 +08:00
|
|
|
|
2013-10-02 04:48:32 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-11-29 18:26:09 +08:00
|
|
|
return -1;
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2013-10-05 22:55:28 +08:00
|
|
|
if (send_receive == 1) {
|
|
|
|
if (m->friendlist[friendnumber].file_receiving[filenumber].status == FILESTATUS_NONE)
|
2013-11-29 18:26:09 +08:00
|
|
|
return -1;
|
2013-10-05 22:55:28 +08:00
|
|
|
} else {
|
|
|
|
if (m->friendlist[friendnumber].file_sending[filenumber].status == FILESTATUS_NONE)
|
2013-11-29 18:26:09 +08:00
|
|
|
return -1;
|
2013-10-05 22:55:28 +08:00
|
|
|
}
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2013-10-03 08:08:14 +08:00
|
|
|
if (send_receive > 1)
|
2013-11-29 18:26:09 +08:00
|
|
|
return -1;
|
2013-10-03 08:08:14 +08:00
|
|
|
|
2013-09-30 21:35:17 +08:00
|
|
|
uint8_t packet[MAX_DATA_SIZE];
|
2013-10-03 08:08:14 +08:00
|
|
|
packet[0] = send_receive;
|
|
|
|
packet[1] = filenumber;
|
|
|
|
packet[2] = message_id;
|
2013-10-16 05:23:00 +08:00
|
|
|
uint64_t transferred = 0;
|
|
|
|
|
|
|
|
if (message_id == FILECONTROL_RESUME_BROKEN) {
|
|
|
|
if (length != sizeof(uint64_t))
|
2013-11-29 18:26:09 +08:00
|
|
|
return -1;
|
2013-10-16 05:23:00 +08:00
|
|
|
|
|
|
|
uint8_t remaining[sizeof(uint64_t)];
|
|
|
|
memcpy(remaining, data, sizeof(uint64_t));
|
|
|
|
host_to_net(remaining, sizeof(uint64_t));
|
|
|
|
memcpy(packet + 3, remaining, sizeof(uint64_t));
|
|
|
|
memcpy(&transferred, data, sizeof(uint64_t));
|
|
|
|
} else {
|
|
|
|
memcpy(packet + 3, data, length);
|
|
|
|
}
|
2013-10-03 08:08:14 +08:00
|
|
|
|
|
|
|
if (write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_CONTROL, packet, length + 3)) {
|
|
|
|
if (send_receive == 1)
|
|
|
|
switch (message_id) {
|
|
|
|
case FILECONTROL_ACCEPT:
|
2013-10-04 00:42:29 +08:00
|
|
|
m->friendlist[friendnumber].file_receiving[filenumber].status = FILESTATUS_TRANSFERRING;
|
2013-10-03 08:08:14 +08:00
|
|
|
break;
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2013-10-03 08:08:14 +08:00
|
|
|
case FILECONTROL_PAUSE:
|
2013-10-04 00:42:29 +08:00
|
|
|
m->friendlist[friendnumber].file_receiving[filenumber].status = FILESTATUS_PAUSED_BY_US;
|
2013-10-03 08:08:14 +08:00
|
|
|
break;
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2013-10-03 08:08:14 +08:00
|
|
|
case FILECONTROL_KILL:
|
|
|
|
case FILECONTROL_FINISHED:
|
2013-10-04 00:42:29 +08:00
|
|
|
m->friendlist[friendnumber].file_receiving[filenumber].status = FILESTATUS_NONE;
|
2013-10-03 08:08:14 +08:00
|
|
|
break;
|
2013-10-16 05:23:00 +08:00
|
|
|
|
|
|
|
case FILECONTROL_RESUME_BROKEN:
|
|
|
|
m->friendlist[friendnumber].file_receiving[filenumber].status = FILESTATUS_PAUSED_BY_OTHER;
|
|
|
|
m->friendlist[friendnumber].file_receiving[filenumber].transferred = transferred;
|
|
|
|
break;
|
2013-10-03 08:08:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
switch (message_id) {
|
|
|
|
case FILECONTROL_ACCEPT:
|
2013-10-04 00:42:29 +08:00
|
|
|
m->friendlist[friendnumber].file_sending[filenumber].status = FILESTATUS_TRANSFERRING;
|
2013-10-03 08:08:14 +08:00
|
|
|
break;
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2013-10-03 08:08:14 +08:00
|
|
|
case FILECONTROL_PAUSE:
|
2013-10-04 00:42:29 +08:00
|
|
|
m->friendlist[friendnumber].file_sending[filenumber].status = FILESTATUS_PAUSED_BY_US;
|
2013-10-03 08:08:14 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FILECONTROL_KILL:
|
|
|
|
case FILECONTROL_FINISHED:
|
2013-10-04 00:42:29 +08:00
|
|
|
m->friendlist[friendnumber].file_sending[filenumber].status = FILESTATUS_NONE;
|
2013-10-03 08:08:14 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-10-02 04:48:32 +08:00
|
|
|
|
|
|
|
return 0;
|
2013-11-29 18:26:09 +08:00
|
|
|
} else {
|
|
|
|
return -1;
|
2013-10-02 04:48:32 +08:00
|
|
|
}
|
2013-09-30 21:35:17 +08:00
|
|
|
}
|
|
|
|
|
2013-10-04 23:20:47 +08:00
|
|
|
#define MIN_SLOTS_FREE 4
|
2013-09-30 21:35:17 +08:00
|
|
|
/* Send file data.
|
|
|
|
*
|
2013-11-29 18:26:09 +08:00
|
|
|
* return 0 on success
|
|
|
|
* return -1 on failure
|
2013-09-30 21:35:17 +08:00
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
int file_data(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length)
|
2013-09-30 21:35:17 +08:00
|
|
|
{
|
|
|
|
if (length > MAX_DATA_SIZE - 1)
|
2013-11-29 18:26:09 +08:00
|
|
|
return -1;
|
2013-09-30 21:35:17 +08:00
|
|
|
|
2013-10-02 04:48:32 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
2013-11-29 18:26:09 +08:00
|
|
|
return -1;
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2013-10-04 00:42:29 +08:00
|
|
|
if (m->friendlist[friendnumber].file_sending[filenumber].status != FILESTATUS_TRANSFERRING)
|
2013-11-29 18:26:09 +08:00
|
|
|
return -1;
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2013-10-04 23:20:47 +08:00
|
|
|
/* Prevent file sending from filling up the entire buffer preventing messages from being sent. */
|
|
|
|
if (crypto_num_free_sendqueue_slots(m->net_crypto, m->friendlist[friendnumber].crypt_connection_id) < MIN_SLOTS_FREE)
|
2013-11-29 18:26:09 +08:00
|
|
|
return -1;
|
2013-10-04 23:20:47 +08:00
|
|
|
|
2013-09-30 21:35:17 +08:00
|
|
|
uint8_t packet[MAX_DATA_SIZE];
|
|
|
|
packet[0] = filenumber;
|
|
|
|
memcpy(packet + 1, data, length);
|
2013-10-02 04:48:32 +08:00
|
|
|
|
|
|
|
if (write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_DATA, packet, length + 1)) {
|
|
|
|
m->friendlist[friendnumber].file_sending[filenumber].transferred += length;
|
2013-11-29 18:26:09 +08:00
|
|
|
return 0;
|
2013-10-02 04:48:32 +08:00
|
|
|
}
|
|
|
|
|
2013-11-29 18:26:09 +08:00
|
|
|
return -1;
|
2013-10-02 04:48:32 +08:00
|
|
|
|
|
|
|
}
|
2013-10-02 06:59:33 +08:00
|
|
|
|
|
|
|
/* Give the number of bytes left to be sent/received.
|
|
|
|
*
|
|
|
|
* send_receive is 0 if we want the sending files, 1 if we want the receiving.
|
|
|
|
*
|
|
|
|
* return number of bytes remaining to be sent/received on success
|
|
|
|
* return 0 on failure
|
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
uint64_t file_dataremaining(Messenger *m, int32_t friendnumber, uint8_t filenumber, uint8_t send_receive)
|
2013-10-02 06:59:33 +08:00
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (send_receive == 0) {
|
2013-10-04 00:42:29 +08:00
|
|
|
if (m->friendlist[friendnumber].file_sending[filenumber].status == FILESTATUS_NONE)
|
2013-10-02 06:59:33 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return m->friendlist[friendnumber].file_sending[filenumber].size -
|
|
|
|
m->friendlist[friendnumber].file_sending[filenumber].transferred;
|
|
|
|
} else {
|
2013-10-04 00:42:29 +08:00
|
|
|
if (m->friendlist[friendnumber].file_receiving[filenumber].status == FILESTATUS_NONE)
|
2013-10-02 06:59:33 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return m->friendlist[friendnumber].file_receiving[filenumber].size -
|
|
|
|
m->friendlist[friendnumber].file_receiving[filenumber].transferred;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-02 04:48:32 +08:00
|
|
|
/* Run this when the friend disconnects.
|
|
|
|
* Sets all current file transfers to broken.
|
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
static void break_files(Messenger *m, int32_t friendnumber)
|
2013-10-02 04:48:32 +08:00
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_CONCURRENT_FILE_PIPES; ++i) {
|
2013-10-04 00:42:29 +08:00
|
|
|
if (m->friendlist[friendnumber].file_sending[i].status != FILESTATUS_NONE)
|
|
|
|
m->friendlist[friendnumber].file_sending[i].status = FILESTATUS_BROKEN;
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2013-10-04 00:42:29 +08:00
|
|
|
if (m->friendlist[friendnumber].file_receiving[i].status != FILESTATUS_NONE)
|
|
|
|
m->friendlist[friendnumber].file_receiving[i].status = FILESTATUS_BROKEN;
|
2013-10-02 04:48:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
static int handle_filecontrol(Messenger *m, int32_t friendnumber, uint8_t receive_send, uint8_t filenumber,
|
2013-10-03 08:08:14 +08:00
|
|
|
uint8_t message_id, uint8_t *data,
|
2013-10-02 04:48:32 +08:00
|
|
|
uint16_t length)
|
|
|
|
{
|
2013-10-16 05:23:00 +08:00
|
|
|
if (receive_send > 1)
|
2013-10-02 04:48:32 +08:00
|
|
|
return -1;
|
|
|
|
|
2013-10-16 05:23:00 +08:00
|
|
|
if (receive_send == 0) {
|
|
|
|
if (m->friendlist[friendnumber].file_receiving[filenumber].status == FILESTATUS_NONE) {
|
|
|
|
/* Tell the other to kill the file sending if we don't know this one. */
|
|
|
|
m->friendlist[friendnumber].file_receiving[filenumber].status = FILESTATUS_TEMPORARY;
|
|
|
|
file_control(m, friendnumber, !receive_send, filenumber, FILECONTROL_KILL, NULL, 0);
|
|
|
|
m->friendlist[friendnumber].file_receiving[filenumber].status = FILESTATUS_NONE;
|
2013-10-03 08:08:14 +08:00
|
|
|
return -1;
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2013-10-16 05:23:00 +08:00
|
|
|
}
|
|
|
|
|
2013-10-03 08:08:14 +08:00
|
|
|
switch (message_id) {
|
|
|
|
case FILECONTROL_ACCEPT:
|
2013-10-04 00:42:29 +08:00
|
|
|
if (m->friendlist[friendnumber].file_receiving[filenumber].status != FILESTATUS_PAUSED_BY_US) {
|
|
|
|
m->friendlist[friendnumber].file_receiving[filenumber].status = FILESTATUS_TRANSFERRING;
|
2013-10-03 08:08:14 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-10-02 04:48:32 +08:00
|
|
|
|
2013-10-03 08:08:14 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
case FILECONTROL_PAUSE:
|
2013-10-04 00:42:29 +08:00
|
|
|
if (m->friendlist[friendnumber].file_receiving[filenumber].status != FILESTATUS_PAUSED_BY_US) {
|
|
|
|
m->friendlist[friendnumber].file_receiving[filenumber].status = FILESTATUS_PAUSED_BY_OTHER;
|
2013-10-03 08:08:14 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
case FILECONTROL_KILL:
|
|
|
|
case FILECONTROL_FINISHED:
|
2013-10-04 00:42:29 +08:00
|
|
|
m->friendlist[friendnumber].file_receiving[filenumber].status = FILESTATUS_NONE;
|
2013-10-03 08:08:14 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
2013-10-16 05:23:00 +08:00
|
|
|
if (m->friendlist[friendnumber].file_sending[filenumber].status == FILESTATUS_NONE) {
|
|
|
|
/* Tell the other to kill the file sending if we don't know this one. */
|
|
|
|
m->friendlist[friendnumber].file_sending[filenumber].status = FILESTATUS_TEMPORARY;
|
|
|
|
file_control(m, friendnumber, !receive_send, filenumber, FILECONTROL_KILL, NULL, 0);
|
|
|
|
m->friendlist[friendnumber].file_sending[filenumber].status = FILESTATUS_NONE;
|
2013-10-03 08:08:14 +08:00
|
|
|
return -1;
|
2013-10-16 05:23:00 +08:00
|
|
|
}
|
2013-10-03 08:08:14 +08:00
|
|
|
|
|
|
|
switch (message_id) {
|
|
|
|
case FILECONTROL_ACCEPT:
|
2013-10-04 00:42:29 +08:00
|
|
|
if (m->friendlist[friendnumber].file_sending[filenumber].status != FILESTATUS_PAUSED_BY_US) {
|
|
|
|
m->friendlist[friendnumber].file_sending[filenumber].status = FILESTATUS_TRANSFERRING;
|
2013-10-03 08:08:14 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
case FILECONTROL_PAUSE:
|
2013-10-04 00:42:29 +08:00
|
|
|
if (m->friendlist[friendnumber].file_sending[filenumber].status != FILESTATUS_PAUSED_BY_US) {
|
|
|
|
m->friendlist[friendnumber].file_sending[filenumber].status = FILESTATUS_PAUSED_BY_OTHER;
|
|
|
|
}
|
|
|
|
|
2013-10-03 08:08:14 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case FILECONTROL_KILL:
|
|
|
|
case FILECONTROL_FINISHED:
|
2013-10-04 00:42:29 +08:00
|
|
|
m->friendlist[friendnumber].file_sending[filenumber].status = FILESTATUS_NONE;
|
2013-10-03 08:08:14 +08:00
|
|
|
return 0;
|
2013-10-16 05:23:00 +08:00
|
|
|
|
|
|
|
case FILECONTROL_RESUME_BROKEN: {
|
|
|
|
if (m->friendlist[friendnumber].file_sending[filenumber].status == FILESTATUS_BROKEN && length == sizeof(uint64_t)) {
|
|
|
|
m->friendlist[friendnumber].file_sending[filenumber].status = FILESTATUS_PAUSED_BY_US;
|
|
|
|
net_to_host(data, sizeof(uint64_t));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2013-10-03 08:08:14 +08:00
|
|
|
}
|
2013-10-02 04:48:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2013-09-30 21:35:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************/
|
|
|
|
|
2013-10-14 10:07:30 +08:00
|
|
|
/* Set the callback for msi packets.
|
|
|
|
*
|
|
|
|
* Function(Messenger *m, int friendnumber, uint8_t *data, uint16_t length, void *userdata)
|
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
void m_callback_msi_packet(Messenger *m, void (*function)(Messenger *m, int32_t, uint8_t *, uint16_t, void *),
|
2013-10-14 10:07:30 +08:00
|
|
|
void *userdata)
|
|
|
|
{
|
|
|
|
m->msi_packet = function;
|
|
|
|
m->msi_packet_userdata = userdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send an msi packet.
|
|
|
|
*
|
|
|
|
* return 1 on success
|
|
|
|
* return 0 on failure
|
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
int m_msi_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint16_t length)
|
2013-10-14 10:07:30 +08:00
|
|
|
{
|
|
|
|
return write_cryptpacket_id(m, friendnumber, PACKET_ID_MSI, data, length);
|
|
|
|
}
|
2013-09-12 08:42:25 +08:00
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
static int32_t friendnum_from_ip_port(Messenger *m, IP_Port ip_port)
|
2014-02-15 10:16:31 +08:00
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < m->numonline_friends; ++i) {
|
|
|
|
if (ipport_equal(&m->online_friendlist[i].ip_port, &ip_port))
|
|
|
|
return m->online_friendlist[i].friend_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int handle_custom_user_packet(void *object, IP_Port source, uint8_t *packet, uint32_t length)
|
|
|
|
{
|
|
|
|
Messenger *m = object;
|
2014-02-22 00:38:04 +08:00
|
|
|
int32_t friend_num = friendnum_from_ip_port(m, source);
|
2014-02-15 10:16:31 +08:00
|
|
|
|
|
|
|
if (friend_num == -1)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (m->friendlist[friend_num].packethandlers[packet[0] % TOTAL_USERPACKETS].function)
|
|
|
|
return m->friendlist[friend_num].packethandlers[packet[0] % TOTAL_USERPACKETS].function(
|
|
|
|
m->friendlist[friend_num].packethandlers[packet[0] % TOTAL_USERPACKETS].object, source, packet, length);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
int custom_user_packet_registerhandler(Messenger *m, int32_t friendnumber, uint8_t byte, packet_handler_callback cb,
|
2014-02-15 10:16:31 +08:00
|
|
|
void *object)
|
|
|
|
{
|
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (byte < NET_PACKET_CUSTOM_RANGE_START || byte >= NET_PACKET_CUSTOM_RANGE_END)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
m->friendlist[friendnumber].packethandlers[byte % TOTAL_USERPACKETS].function = cb;
|
|
|
|
m->friendlist[friendnumber].packethandlers[byte % TOTAL_USERPACKETS].object = object;
|
|
|
|
networking_registerhandler(m->net, byte, handle_custom_user_packet, m);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
int send_custom_user_packet(Messenger *m, int32_t friendnumber, uint8_t *data, uint32_t length)
|
2014-02-15 10:16:31 +08:00
|
|
|
{
|
2014-03-09 06:21:39 +08:00
|
|
|
if (friend_not_valid(m, friendnumber))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (m->friendlist[friendnumber].status != FRIEND_ONLINE)
|
|
|
|
return -1;
|
|
|
|
|
2014-02-15 10:16:31 +08:00
|
|
|
IP_Port ip_port = get_friend_ipport(m, friendnumber);
|
|
|
|
|
|
|
|
if (ip_port.port == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return sendpacket(m->net, ip_port, data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-28 03:18:39 +08:00
|
|
|
/* Function to filter out some friend requests*/
|
2013-12-03 01:20:37 +08:00
|
|
|
static int friend_already_added(uint8_t *client_id, void *data)
|
2013-11-28 03:18:39 +08:00
|
|
|
{
|
|
|
|
Messenger *m = data;
|
|
|
|
|
|
|
|
if (getfriend_id(m, client_id) == -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-08-30 05:17:51 +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()) {
|
2014-01-18 02:35:40 +08:00
|
|
|
send_LANdiscovery(htons(TOX_PORT_DEFAULT), m->dht);
|
2013-08-21 02:47:32 +08:00
|
|
|
m->last_LANdiscovery = unix_time();
|
|
|
|
}
|
2013-08-13 06:47:15 +08:00
|
|
|
}
|
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Run this at startup. */
|
2013-10-24 02:42:39 +08:00
|
|
|
Messenger *new_messenger(uint8_t ipv6enabled)
|
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-09-10 22:36:20 +08:00
|
|
|
IP ip;
|
|
|
|
ip_init(&ip, ipv6enabled);
|
2013-09-12 22:07:51 +08:00
|
|
|
m->net = new_networking(ip, TOX_PORT_DEFAULT);
|
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
|
|
|
|
2014-01-18 02:35:40 +08:00
|
|
|
m->onion = new_onion(m->dht);
|
|
|
|
m->onion_a = new_onion_announce(m->dht);
|
|
|
|
m->onion_c = new_onion_client(m->dht);
|
|
|
|
|
|
|
|
if (!(m->onion && m->onion_a && m->onion_c)) {
|
|
|
|
kill_onion(m->onion);
|
|
|
|
kill_onion_announce(m->onion_a);
|
|
|
|
kill_onion_client(m->onion_c);
|
|
|
|
kill_DHT(m->dht);
|
|
|
|
kill_net_crypto(m->net_crypto);
|
|
|
|
kill_networking(m->net);
|
|
|
|
free(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-08-20 11:54:28 +08:00
|
|
|
m_set_statusmessage(m, (uint8_t *)"Online", sizeof("Online"));
|
2013-07-26 09:00:31 +08:00
|
|
|
|
2014-01-18 02:35:40 +08:00
|
|
|
friendreq_init(&(m->fr), m->onion_c);
|
2013-08-21 00:08:55 +08:00
|
|
|
LANdiscovery_init(m->dht);
|
|
|
|
set_nospam(&(m->fr), random_int());
|
2013-11-28 03:18:39 +08:00
|
|
|
set_filter_function(&(m->fr), &friend_already_added, m);
|
|
|
|
|
2013-09-12 08:42:25 +08:00
|
|
|
networking_registerhandler(m->net, NET_PACKET_GROUP_CHATS, &handle_group, m);
|
2013-08-10 07:54:43 +08:00
|
|
|
|
2013-08-11 11:24:11 +08:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Run this before closing shop. */
|
2013-10-24 02:42:39 +08:00
|
|
|
void kill_messenger(Messenger *m)
|
2013-08-17 01:11:09 +08:00
|
|
|
{
|
2013-08-30 05:17:51 +08:00
|
|
|
/* FIXME TODO: ideally cleanupMessenger will mirror initMessenger.
|
|
|
|
* This requires the other modules to expose cleanup functions.
|
2013-08-14 13:35:15 +08:00
|
|
|
*/
|
2013-11-24 11:17:36 +08:00
|
|
|
uint32_t i, numchats = m->numchats;
|
2013-12-08 08:06:41 +08:00
|
|
|
|
2013-11-24 11:17:36 +08:00
|
|
|
for (i = 0; i < numchats; ++i)
|
|
|
|
del_groupchat(m, i);
|
|
|
|
|
2014-01-21 10:01:56 +08:00
|
|
|
kill_onion(m->onion);
|
|
|
|
kill_onion_announce(m->onion_a);
|
|
|
|
kill_onion_client(m->onion_c);
|
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-12-02 19:16:01 +08:00
|
|
|
/* Check for and handle a timed-out friend request. If the request has
|
|
|
|
* timed-out then the friend status is set back to FRIEND_ADDED.
|
|
|
|
* i: friendlist index of the timed-out friend
|
|
|
|
* t: time
|
|
|
|
*/
|
|
|
|
static void check_friend_request_timed_out(Messenger *m, uint32_t i, uint64_t t)
|
|
|
|
{
|
|
|
|
Friend *f = &m->friendlist[i];
|
|
|
|
|
|
|
|
if (f->friendrequest_lastsent + f->friendrequest_timeout < t) {
|
|
|
|
set_friend_status(m, i, FRIEND_ADDED);
|
|
|
|
/* Double the default timeout everytime if friendrequest is assumed
|
|
|
|
* to have been sent unsuccessfully.
|
|
|
|
*/
|
|
|
|
f->friendrequest_timeout *= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* TODO: Make this function not suck. */
|
2013-10-24 02:42:39 +08:00
|
|
|
void do_friends(Messenger *m)
|
2013-07-27 20:43:36 +08:00
|
|
|
{
|
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) {
|
2014-01-18 02:35:40 +08:00
|
|
|
int fr = send_friendrequest(m->onion_c, m->friendlist[i].client_id, m->friendlist[i].friendrequest_nospam,
|
2013-08-21 07:37:05 +08:00
|
|
|
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
|
2013-08-30 05:17:51 +08:00
|
|
|
|| 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
|
2013-08-30 05:17:51 +08:00
|
|
|
* unsuccessful so we set the status back to FRIEND_ADDED and try again.
|
|
|
|
*/
|
2013-12-05 03:48:27 +08:00
|
|
|
check_friend_request_timed_out(m, i, temp_time);
|
2013-07-23 05:02:32 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-09-10 22:36:20 +08:00
|
|
|
IP_Port friendip;
|
2014-01-18 02:35:40 +08:00
|
|
|
int friendok = onion_getfriendip(m->onion_c, m->friendlist[i].onion_friendnum, &friendip);
|
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-10-24 02:32:09 +08:00
|
|
|
case CRYPTO_CONN_NO_CONNECTION:
|
2013-09-10 22:36:20 +08:00
|
|
|
if (friendok == 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;
|
|
|
|
|
2013-10-24 02:32:09 +08:00
|
|
|
case CRYPTO_CONN_ESTABLISHED: /* Connection is established. */
|
2013-08-17 01:11:09 +08:00
|
|
|
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;
|
|
|
|
|
2013-10-24 02:32:09 +08:00
|
|
|
case CRYPTO_CONN_TIMED_OUT:
|
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-30 05:17:51 +08:00
|
|
|
while (m->friendlist[i].status == FRIEND_ONLINE) { /* friend is online. */
|
2013-08-11 11:24:11 +08:00
|
|
|
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
|
|
|
}
|
2014-02-19 02:49:17 +08:00
|
|
|
|
2014-02-17 05:33:37 +08:00
|
|
|
if (m->friendlist[i].user_istyping_sent == 0) {
|
|
|
|
if (send_user_istyping(m, i, m->friendlist[i].user_istyping))
|
|
|
|
m->friendlist[i].user_istyping_sent = 1;
|
|
|
|
}
|
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-17 01:11:09 +08:00
|
|
|
|
2013-07-27 11:07:25 +08:00
|
|
|
if (len > 0) {
|
2013-10-14 10:07:30 +08:00
|
|
|
uint8_t packet_id = temp[0];
|
|
|
|
uint8_t *data = temp + 1;
|
|
|
|
uint32_t data_length = len - 1;
|
|
|
|
|
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;
|
|
|
|
|
2013-11-11 22:25:43 +08:00
|
|
|
/* Make sure the NULL terminator is present. */
|
2014-02-23 23:23:55 +08:00
|
|
|
uint8_t data_terminated[data_length + 1];
|
|
|
|
memcpy(data_terminated, data, data_length);
|
|
|
|
data_terminated[data_length] = 0;
|
2013-09-06 21:45:39 +08:00
|
|
|
|
2013-11-11 22:25:43 +08:00
|
|
|
/* inform of namechange before we overwrite the old name */
|
2013-09-06 21:45:39 +08:00
|
|
|
if (m->friend_namechange)
|
2014-02-23 23:23:55 +08:00
|
|
|
m->friend_namechange(m, i, data_terminated, data_length, m->friend_namechange_userdata);
|
2013-11-11 22:25:43 +08:00
|
|
|
|
2014-02-23 23:23:55 +08:00
|
|
|
memcpy(m->friendlist[i].name, data_terminated, data_length + 1);
|
2013-11-11 22:25:43 +08:00
|
|
|
m->friendlist[i].name_length = data_length;
|
2013-09-06 21:45:39 +08:00
|
|
|
|
2013-07-19 01:56:50 +08:00
|
|
|
break;
|
2013-08-17 01:11:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case PACKET_ID_STATUSMESSAGE: {
|
2013-09-04 02:28:14 +08:00
|
|
|
if (data_length == 0 || data_length > MAX_STATUSMESSAGE_LENGTH)
|
2013-08-17 01:11:09 +08:00
|
|
|
break;
|
|
|
|
|
2014-02-23 23:23:55 +08:00
|
|
|
/* Make sure the NULL terminator is present. */
|
|
|
|
uint8_t data_terminated[data_length + 1];
|
|
|
|
memcpy(data_terminated, data, data_length);
|
|
|
|
data_terminated[data_length] = 0;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
if (m->friend_statusmessagechange)
|
2014-02-23 23:23:55 +08:00
|
|
|
m->friend_statusmessagechange(m, i, data_terminated, data_length,
|
2013-08-17 01:11:09 +08:00
|
|
|
m->friend_statuschange_userdata);
|
|
|
|
|
2014-02-23 23:23:55 +08:00
|
|
|
set_friend_statusmessage(m, i, data_terminated, data_length);
|
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];
|
|
|
|
|
2014-03-19 08:50:40 +08:00
|
|
|
if (status >= USERSTATUS_INVALID)
|
|
|
|
break;
|
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
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
|
|
|
}
|
2014-02-19 02:49:17 +08:00
|
|
|
|
2014-02-17 05:33:37 +08:00
|
|
|
case PACKET_ID_TYPING: {
|
|
|
|
if (data_length != 1)
|
|
|
|
break;
|
2014-02-19 02:49:17 +08:00
|
|
|
|
2014-02-17 05:33:37 +08:00
|
|
|
uint8_t typing = data[0];
|
2014-02-19 02:49:17 +08:00
|
|
|
|
2014-03-05 16:42:26 +08:00
|
|
|
set_friend_typing(m, i, typing);
|
|
|
|
|
2014-02-17 05:33:37 +08:00
|
|
|
if (m->friend_typingchange)
|
|
|
|
m->friend_typingchange(m, i, typing, m->friend_typingchange_userdata);
|
2014-03-06 22:55:50 +08:00
|
|
|
|
2014-02-19 02:49:17 +08:00
|
|
|
break;
|
2014-02-17 05:33:37 +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;
|
2013-09-04 02:28:14 +08:00
|
|
|
|
|
|
|
if (data_length <= message_id_length)
|
|
|
|
break;
|
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
uint8_t *message = data + message_id_length;
|
|
|
|
uint16_t message_length = data_length - message_id_length;
|
|
|
|
|
2014-02-23 23:23:55 +08:00
|
|
|
/* Make sure the NULL terminator is present. */
|
|
|
|
uint8_t message_terminated[message_length + 1];
|
|
|
|
memcpy(message_terminated, message, message_length);
|
|
|
|
message_terminated[message_length] = 0;
|
2013-09-04 02:28:14 +08:00
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
if (m->friendlist[i].receives_read_receipts) {
|
|
|
|
write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m->friend_message)
|
2014-02-23 23:23:55 +08:00
|
|
|
(*m->friend_message)(m, i, message_terminated, message_length, m->friend_message_userdata);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-08 00:53:52 +08:00
|
|
|
break;
|
2013-08-17 01:11:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case PACKET_ID_ACTION: {
|
2013-10-11 10:27:51 +08:00
|
|
|
uint8_t *message_id = data;
|
|
|
|
uint8_t message_id_length = 4;
|
|
|
|
|
|
|
|
if (data_length <= message_id_length)
|
2013-09-04 02:28:14 +08:00
|
|
|
break;
|
|
|
|
|
2013-10-11 10:27:51 +08:00
|
|
|
uint8_t *action = data + message_id_length;
|
|
|
|
uint16_t action_length = data_length - message_id_length;
|
|
|
|
|
2014-02-23 23:23:55 +08:00
|
|
|
/* Make sure the NULL terminator is present. */
|
|
|
|
uint8_t action_terminated[action_length + 1];
|
|
|
|
memcpy(action_terminated, action, action_length);
|
|
|
|
action_terminated[action_length] = 0;
|
2013-09-04 02:28:14 +08:00
|
|
|
|
2013-10-11 10:27:51 +08:00
|
|
|
if (m->friendlist[i].receives_read_receipts) {
|
|
|
|
write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length);
|
|
|
|
}
|
2013-10-14 10:07:30 +08:00
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
if (m->friend_action)
|
2014-02-23 23:23:55 +08:00
|
|
|
(*m->friend_action)(m, i, action_terminated, action_length, m->friend_action_userdata);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PACKET_ID_RECEIPT: {
|
|
|
|
uint32_t msgid;
|
|
|
|
|
2013-09-02 21:55:37 +08:00
|
|
|
if (data_length < sizeof(msgid))
|
2013-08-17 01:11:09 +08:00
|
|
|
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-09-13 07:26:30 +08:00
|
|
|
|
|
|
|
case PACKET_ID_INVITE_GROUPCHAT: {
|
|
|
|
if (data_length != crypto_box_PUBLICKEYBYTES)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (m->group_invite)
|
|
|
|
(*m->group_invite)(m, i, data, m->group_invite_userdata);
|
2013-09-28 10:03:38 +08:00
|
|
|
|
2013-09-22 14:50:14 +08:00
|
|
|
break;
|
2013-09-13 07:26:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case PACKET_ID_JOIN_GROUPCHAT: {
|
|
|
|
if (data_length != crypto_box_PUBLICKEYBYTES * 2)
|
|
|
|
break;
|
|
|
|
|
|
|
|
int groupnum = group_num(m, data);
|
|
|
|
|
|
|
|
if (groupnum == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!group_invited(m, i, groupnum))
|
|
|
|
break;
|
|
|
|
|
|
|
|
group_newpeer(m->chats[groupnum], data + crypto_box_PUBLICKEYBYTES);
|
2014-03-09 06:21:39 +08:00
|
|
|
/* This is just there to speedup joining. */
|
2013-11-22 08:33:02 +08:00
|
|
|
chat_bootstrap(m->chats[groupnum], get_friend_ipport(m, i), data + crypto_box_PUBLICKEYBYTES);
|
2013-09-22 14:50:14 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-09-28 10:03:38 +08:00
|
|
|
|
2013-09-30 21:35:17 +08:00
|
|
|
case PACKET_ID_FILE_SENDREQUEST: {
|
|
|
|
if (data_length < 1 + sizeof(uint64_t) + 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
uint8_t filenumber = data[0];
|
|
|
|
uint64_t filesize;
|
2013-10-16 05:23:00 +08:00
|
|
|
net_to_host(data + 1, sizeof(filesize));
|
2013-09-30 21:35:17 +08:00
|
|
|
memcpy(&filesize, data + 1, sizeof(filesize));
|
2013-10-04 00:42:29 +08:00
|
|
|
m->friendlist[i].file_receiving[filenumber].status = FILESTATUS_NOT_ACCEPTED;
|
2013-10-02 06:59:33 +08:00
|
|
|
m->friendlist[i].file_receiving[filenumber].size = filesize;
|
2013-10-02 21:42:28 +08:00
|
|
|
m->friendlist[i].file_receiving[filenumber].transferred = 0;
|
2013-10-04 00:42:29 +08:00
|
|
|
|
2014-02-23 23:23:55 +08:00
|
|
|
/* Force NULL terminate file name. */
|
|
|
|
uint8_t filename_terminated[data_length - 1 - sizeof(uint64_t) + 1];
|
|
|
|
memcpy(filename_terminated, data + 1 + sizeof(uint64_t), data_length - 1 - sizeof(uint64_t));
|
|
|
|
filename_terminated[data_length - 1 - sizeof(uint64_t)] = 0;
|
2014-02-13 02:14:25 +08:00
|
|
|
|
2013-09-30 21:35:17 +08:00
|
|
|
if (m->file_sendrequest)
|
2014-02-23 23:23:55 +08:00
|
|
|
(*m->file_sendrequest)(m, i, filenumber, filesize, filename_terminated, data_length - 1 - sizeof(uint64_t),
|
2013-09-30 21:35:17 +08:00
|
|
|
m->file_sendrequest_userdata);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PACKET_ID_FILE_CONTROL: {
|
2013-10-03 08:08:14 +08:00
|
|
|
if (data_length < 3)
|
2013-09-30 21:35:17 +08:00
|
|
|
break;
|
|
|
|
|
2013-10-03 08:08:14 +08:00
|
|
|
uint8_t send_receive = data[0];
|
|
|
|
uint8_t filenumber = data[1];
|
|
|
|
uint8_t control_type = data[2];
|
2013-09-30 21:35:17 +08:00
|
|
|
|
2013-10-03 08:08:14 +08:00
|
|
|
if (handle_filecontrol(m, i, send_receive, filenumber, control_type, data + 3, data_length - 3) == -1)
|
2013-10-02 04:48:32 +08:00
|
|
|
break;
|
|
|
|
|
2013-09-30 21:35:17 +08:00
|
|
|
if (m->file_filecontrol)
|
2013-10-03 08:08:14 +08:00
|
|
|
(*m->file_filecontrol)(m, i, send_receive, filenumber, control_type, data + 3, data_length - 3,
|
|
|
|
m->file_filecontrol_userdata);
|
2013-09-30 21:35:17 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PACKET_ID_FILE_DATA: {
|
|
|
|
if (data_length < 2)
|
|
|
|
break;
|
|
|
|
|
|
|
|
uint8_t filenumber = data[0];
|
|
|
|
|
2013-10-04 00:42:29 +08:00
|
|
|
if (m->friendlist[i].file_receiving[filenumber].status == FILESTATUS_NONE)
|
2013-10-02 04:48:32 +08:00
|
|
|
break;
|
|
|
|
|
2013-10-02 06:59:33 +08:00
|
|
|
m->friendlist[i].file_receiving[filenumber].transferred += (data_length - 1);
|
|
|
|
|
2013-09-30 21:35:17 +08:00
|
|
|
if (m->file_filedata)
|
|
|
|
(*m->file_filedata)(m, i, filenumber, data + 1, data_length - 1, m->file_filedata_userdata);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-10-14 10:07:30 +08:00
|
|
|
case PACKET_ID_MSI: {
|
|
|
|
if (data_length == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (m->msi_packet)
|
|
|
|
(*m->msi_packet)(m, i, data, data_length, m->msi_packet_userdata);
|
|
|
|
}
|
|
|
|
|
2013-09-22 14:50:14 +08:00
|
|
|
default: {
|
|
|
|
break;
|
2013-09-13 07:26:30 +08:00
|
|
|
}
|
2013-07-27 20:43:36 +08:00
|
|
|
}
|
|
|
|
} else {
|
2013-08-21 07:37:05 +08:00
|
|
|
if (is_cryptoconnected(m->net_crypto,
|
2013-10-24 02:32:09 +08:00
|
|
|
m->friendlist[i].crypt_connection_id) == CRYPTO_CONN_TIMED_OUT) { /* 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-09-29 05:19:32 +08:00
|
|
|
if (m->friendlist[i].ping_lastrecv + FRIEND_CONNECTION_TIMEOUT < temp_time) {
|
|
|
|
/* If we stopped recieving ping packets, kill it. */
|
|
|
|
crypto_kill(m->net_crypto, m->friendlist[i].crypt_connection_id);
|
|
|
|
m->friendlist[i].crypt_connection_id = -1;
|
|
|
|
set_friend_status(m, i, FRIEND_CONFIRMED);
|
|
|
|
}
|
2013-08-19 19:00:59 +08:00
|
|
|
|
2013-09-29 05:19:32 +08:00
|
|
|
break;
|
2013-08-19 19:00:59 +08:00
|
|
|
}
|
2013-07-09 08:50:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-24 02:42:39 +08:00
|
|
|
void do_inbound(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-09-22 07:27:53 +08:00
|
|
|
if (m_get_friend_connectionstatus(m, friend_id) == 1) {
|
2013-09-22 23:08:23 +08:00
|
|
|
kill_connection(m->net_crypto->lossless_udp, inconnection);
|
2013-09-22 07:27:53 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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);
|
2014-03-08 05:05:08 +08:00
|
|
|
} else {
|
|
|
|
kill_connection(m->net_crypto->lossless_udp, inconnection);
|
2013-07-10 01:20:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-13 14:50:46 +08:00
|
|
|
#ifdef LOGGING
|
2013-09-16 16:37:22 +08:00
|
|
|
#define DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS 60UL
|
2013-09-13 14:50:46 +08:00
|
|
|
static time_t lastdump = 0;
|
|
|
|
static char IDString[CLIENT_ID_SIZE * 2 + 1];
|
|
|
|
static char *ID2String(uint8_t *client_id)
|
|
|
|
{
|
2013-09-15 00:42:17 +08:00
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < CLIENT_ID_SIZE; i++)
|
2013-10-20 22:56:12 +08:00
|
|
|
sprintf(&IDString[i * 2], "%02X", client_id[i]);
|
2013-09-15 00:42:17 +08:00
|
|
|
|
|
|
|
IDString[CLIENT_ID_SIZE * 2] = 0;
|
|
|
|
return IDString;
|
2013-09-13 14:50:46 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* The main loop that needs to be run at least 20 times per second. */
|
2013-10-24 02:42:39 +08:00
|
|
|
void do_messenger(Messenger *m)
|
2013-07-07 10:28:15 +08:00
|
|
|
{
|
2013-10-25 04:32:28 +08:00
|
|
|
unix_time_update();
|
|
|
|
|
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);
|
2014-01-18 02:35:40 +08:00
|
|
|
do_onion_client(m->onion_c);
|
2013-10-24 02:42:39 +08:00
|
|
|
do_friends(m);
|
|
|
|
do_inbound(m);
|
2013-09-12 08:42:25 +08:00
|
|
|
do_allgroupchats(m);
|
2013-08-21 02:47:32 +08:00
|
|
|
LANdiscovery(m);
|
2013-09-13 14:50:46 +08:00
|
|
|
|
|
|
|
#ifdef LOGGING
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-11-06 22:25:23 +08:00
|
|
|
if (unix_time() > lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) {
|
2013-11-17 08:04:49 +08:00
|
|
|
loglog(" = = = = = = = = \n");
|
|
|
|
Assoc_status(m->dht->assoc);
|
|
|
|
|
|
|
|
if (m->numchats > 0) {
|
|
|
|
size_t c;
|
|
|
|
|
|
|
|
for (c = 0; c < m->numchats; c++) {
|
|
|
|
loglog("---------------- \n");
|
|
|
|
Assoc_status(m->chats[c]->assoc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-15 00:42:17 +08:00
|
|
|
loglog(" = = = = = = = = \n");
|
|
|
|
|
2013-11-06 22:25:23 +08:00
|
|
|
lastdump = unix_time();
|
2013-09-15 00:42:17 +08:00
|
|
|
uint32_t client, last_pinged;
|
|
|
|
|
|
|
|
for (client = 0; client < LCLIENT_LIST; client++) {
|
|
|
|
Client_data *cptr = &m->dht->close_clientlist[client];
|
2013-09-27 09:27:52 +08:00
|
|
|
IPPTsPng *assoc = NULL;
|
2013-09-28 01:50:42 +08:00
|
|
|
uint32_t a;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-09-28 01:50:42 +08:00
|
|
|
for (a = 0, assoc = &cptr->assoc4; a < 2; a++, assoc = &cptr->assoc6)
|
2013-10-20 22:56:12 +08:00
|
|
|
if (ip_isset(&assoc->ip_port.ip)) {
|
|
|
|
last_pinged = lastdump - assoc->last_pinged;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-10-20 22:56:12 +08:00
|
|
|
if (last_pinged > 999)
|
|
|
|
last_pinged = 999;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-10-20 22:56:12 +08:00
|
|
|
snprintf(logbuffer, sizeof(logbuffer), "C[%2u] %s:%u [%3u] %s\n",
|
|
|
|
client, ip_ntoa(&assoc->ip_port.ip), ntohs(assoc->ip_port.port),
|
|
|
|
last_pinged, ID2String(cptr->client_id));
|
|
|
|
loglog(logbuffer);
|
|
|
|
}
|
2013-09-15 00:42:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
loglog(" = = = = = = = = \n");
|
|
|
|
|
2013-12-03 01:20:37 +08:00
|
|
|
uint32_t friend, dhtfriend;
|
|
|
|
|
|
|
|
/* dht contains additional "friends" (requests) */
|
|
|
|
uint32_t num_dhtfriends = m->dht->num_friends;
|
|
|
|
int32_t m2dht[num_dhtfriends];
|
|
|
|
int32_t dht2m[num_dhtfriends];
|
|
|
|
|
|
|
|
for (friend = 0; friend < num_dhtfriends; friend++) {
|
|
|
|
m2dht[friend] = -1;
|
|
|
|
dht2m[friend] = -1;
|
|
|
|
|
|
|
|
if (friend >= m->numfriends)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (dhtfriend = 0; dhtfriend < m->dht->num_friends; dhtfriend++)
|
|
|
|
if (id_equal(m->friendlist[friend].client_id, m->dht->friends_list[dhtfriend].client_id)) {
|
|
|
|
m2dht[friend] = dhtfriend;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (friend = 0; friend < num_dhtfriends; friend++)
|
|
|
|
if (m2dht[friend] >= 0)
|
|
|
|
dht2m[m2dht[friend]] = friend;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
|
|
|
if (m->numfriends != m->dht->num_friends) {
|
|
|
|
sprintf(logbuffer, "Friend num in DHT %u != friend num in msger %u\n",
|
|
|
|
m->dht->num_friends, m->numfriends);
|
|
|
|
loglog(logbuffer);
|
|
|
|
}
|
|
|
|
|
2013-12-03 01:20:37 +08:00
|
|
|
uint32_t ping_lastrecv;
|
|
|
|
Friend *msgfptr;
|
|
|
|
DHT_Friend *dhtfptr;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-12-03 01:20:37 +08:00
|
|
|
for (friend = 0; friend < num_dhtfriends; friend++) {
|
|
|
|
if (dht2m[friend] >= 0)
|
|
|
|
msgfptr = &m->friendlist[dht2m[friend]];
|
|
|
|
else
|
|
|
|
msgfptr = NULL;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-12-03 01:20:37 +08:00
|
|
|
dhtfptr = &m->dht->friends_list[friend];
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-12-03 01:20:37 +08:00
|
|
|
if (msgfptr) {
|
|
|
|
ping_lastrecv = lastdump - msgfptr->ping_lastrecv;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-12-03 01:20:37 +08:00
|
|
|
if (ping_lastrecv > 999)
|
|
|
|
ping_lastrecv = 999;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-12-03 01:20:37 +08:00
|
|
|
snprintf(logbuffer, sizeof(logbuffer), "F[%2u:%2u] <%s> %02i [%03u] %s\n",
|
|
|
|
dht2m[friend], friend, msgfptr->name, msgfptr->crypt_connection_id,
|
|
|
|
ping_lastrecv, ID2String(msgfptr->client_id));
|
|
|
|
loglog(logbuffer);
|
|
|
|
} else {
|
|
|
|
snprintf(logbuffer, sizeof(logbuffer), "F[--:%2u] %s\n",
|
|
|
|
friend, ID2String(dhtfptr->client_id));
|
|
|
|
loglog(logbuffer);
|
|
|
|
}
|
2013-09-15 00:42:17 +08:00
|
|
|
|
|
|
|
for (client = 0; client < MAX_FRIEND_CLIENTS; client++) {
|
|
|
|
Client_data *cptr = &dhtfptr->client_list[client];
|
2013-09-27 09:27:52 +08:00
|
|
|
IPPTsPng *assoc = NULL;
|
2013-09-28 01:50:42 +08:00
|
|
|
uint32_t a;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-09-28 01:50:42 +08:00
|
|
|
for (a = 0, assoc = &cptr->assoc4; a < 2; a++, assoc = &cptr->assoc6)
|
2013-10-20 22:56:12 +08:00
|
|
|
if (ip_isset(&assoc->ip_port.ip)) {
|
|
|
|
last_pinged = lastdump - assoc->last_pinged;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-10-20 22:56:12 +08:00
|
|
|
if (last_pinged > 999)
|
|
|
|
last_pinged = 999;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-10-20 22:56:12 +08:00
|
|
|
snprintf(logbuffer, sizeof(logbuffer), "F[%2u] => C[%2u] %s:%u [%3u] %s\n",
|
|
|
|
friend, client, ip_ntoa(&assoc->ip_port.ip),
|
|
|
|
ntohs(assoc->ip_port.port), last_pinged,
|
|
|
|
ID2String(cptr->client_id));
|
|
|
|
loglog(logbuffer);
|
|
|
|
}
|
2013-09-15 00:42:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loglog(" = = = = = = = = \n");
|
|
|
|
}
|
|
|
|
|
2013-09-13 14:50:46 +08:00
|
|
|
#endif
|
2013-08-10 07:30:18 +08:00
|
|
|
}
|
|
|
|
|
2013-10-02 22:38:54 +08:00
|
|
|
/*
|
2013-10-05 18:53:54 +08:00
|
|
|
* functions to avoid excessive polling
|
2013-10-02 22:38:54 +08:00
|
|
|
*/
|
2014-03-08 10:12:46 +08:00
|
|
|
size_t wait_data_size()
|
2013-10-05 18:53:54 +08:00
|
|
|
{
|
2014-03-08 10:12:46 +08:00
|
|
|
return networking_wait_data_size();
|
2013-10-05 18:53:54 +08:00
|
|
|
}
|
|
|
|
|
2014-03-08 10:12:46 +08:00
|
|
|
int wait_prepare_messenger(Messenger *m, uint8_t *data)
|
2013-10-02 22:38:54 +08:00
|
|
|
{
|
2014-03-08 10:12:46 +08:00
|
|
|
return networking_wait_prepare(m->net, sendqueue_total(m->net_crypto->lossless_udp), data);
|
|
|
|
}
|
|
|
|
|
|
|
|
int wait_execute_messenger(uint8_t *data, long seconds, long microseconds)
|
|
|
|
{
|
|
|
|
return networking_wait_execute(data, seconds, microseconds);
|
|
|
|
}
|
2013-10-02 22:38:54 +08:00
|
|
|
|
2014-03-08 10:12:46 +08:00
|
|
|
int wait_cleanup_messenger(Messenger *m, uint8_t *data)
|
2013-10-06 22:37:02 +08:00
|
|
|
{
|
2014-03-08 10:12:46 +08:00
|
|
|
return networking_wait_cleanup(m->net, data);
|
2013-10-06 22:37:02 +08:00
|
|
|
}
|
|
|
|
|
2013-09-14 00:05:11 +08:00
|
|
|
/* new messenger format for load/save, more robust and forward compatible */
|
|
|
|
|
2014-03-06 22:55:50 +08:00
|
|
|
#define MESSENGER_STATE_COOKIE_GLOBAL_OLD 0x15ed1b1e
|
|
|
|
#define MESSENGER_STATE_COOKIE_GLOBAL 0x15ed1b1f
|
2013-09-14 00:05:11 +08:00
|
|
|
|
|
|
|
#define MESSENGER_STATE_COOKIE_TYPE 0x01ce
|
2014-03-07 04:35:52 +08:00
|
|
|
#define MESSENGER_STATE_TYPE_NOSPAMKEYS 1
|
|
|
|
#define MESSENGER_STATE_TYPE_DHT 2
|
|
|
|
#define MESSENGER_STATE_TYPE_FRIENDS 3
|
|
|
|
#define MESSENGER_STATE_TYPE_NAME 4
|
|
|
|
#define MESSENGER_STATE_TYPE_STATUSMESSAGE 5
|
|
|
|
#define MESSENGER_STATE_TYPE_STATUS 6
|
2013-09-14 00:05:11 +08:00
|
|
|
|
2014-03-06 22:55:50 +08:00
|
|
|
struct SAVED_FRIEND {
|
|
|
|
uint8_t status;
|
|
|
|
uint8_t client_id[CLIENT_ID_SIZE];
|
|
|
|
uint8_t info[MAX_DATA_SIZE]; // the data that is sent during the friend requests we do.
|
|
|
|
uint16_t info_size; // Length of the info.
|
|
|
|
uint8_t name[MAX_NAME_LENGTH];
|
|
|
|
uint16_t name_length;
|
|
|
|
uint8_t statusmessage[MAX_STATUSMESSAGE_LENGTH];
|
|
|
|
uint16_t statusmessage_length;
|
|
|
|
uint8_t userstatus;
|
|
|
|
uint32_t friendrequest_nospam;
|
2014-03-14 08:20:48 +08:00
|
|
|
uint64_t ping_lastrecv;
|
2014-03-06 22:55:50 +08:00
|
|
|
};
|
|
|
|
|
2014-03-16 17:09:36 +08:00
|
|
|
/* for backwards compatibility with last version of SAVED_FRIEND.
|
|
|
|
must never be bigger than SAVED_FRIEND. */
|
|
|
|
struct SAVED_FRIEND_OLD {
|
|
|
|
uint8_t status;
|
|
|
|
uint8_t client_id[CLIENT_ID_SIZE];
|
|
|
|
uint8_t info[MAX_DATA_SIZE];
|
|
|
|
uint16_t info_size;
|
|
|
|
uint8_t name[MAX_NAME_LENGTH];
|
|
|
|
uint16_t name_length;
|
|
|
|
uint8_t statusmessage[MAX_STATUSMESSAGE_LENGTH];
|
|
|
|
uint16_t statusmessage_length;
|
|
|
|
uint8_t userstatus;
|
|
|
|
uint32_t friendrequest_nospam;
|
|
|
|
};
|
|
|
|
|
2014-03-06 22:55:50 +08:00
|
|
|
static uint32_t saved_friendslist_size(Messenger *m)
|
|
|
|
{
|
|
|
|
return count_friendlist(m) * sizeof(struct SAVED_FRIEND);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t friends_list_save(Messenger *m, uint8_t *data)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
uint32_t num = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < m->numfriends; i++) {
|
|
|
|
if (m->friendlist[i].status > 0) {
|
|
|
|
struct SAVED_FRIEND temp;
|
|
|
|
memset(&temp, 0, sizeof(struct SAVED_FRIEND));
|
|
|
|
temp.status = m->friendlist[i].status;
|
|
|
|
memcpy(temp.client_id, m->friendlist[i].client_id, CLIENT_ID_SIZE);
|
|
|
|
|
|
|
|
if (temp.status < 3) {
|
|
|
|
memcpy(temp.info, m->friendlist[i].info, m->friendlist[i].info_size);
|
|
|
|
temp.info_size = htons(m->friendlist[i].info_size);
|
|
|
|
temp.friendrequest_nospam = m->friendlist[i].friendrequest_nospam;
|
|
|
|
} else {
|
|
|
|
memcpy(temp.name, m->friendlist[i].name, m->friendlist[i].name_length);
|
|
|
|
temp.name_length = htons(m->friendlist[i].name_length);
|
|
|
|
memcpy(temp.statusmessage, m->friendlist[i].statusmessage, m->friendlist[i].statusmessage_length);
|
|
|
|
temp.statusmessage_length = htons(m->friendlist[i].statusmessage_length);
|
|
|
|
temp.userstatus = m->friendlist[i].userstatus;
|
2014-03-14 08:20:48 +08:00
|
|
|
|
|
|
|
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));
|
2014-03-06 22:55:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(data + num * sizeof(struct SAVED_FRIEND), &temp, sizeof(struct SAVED_FRIEND));
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return num * sizeof(struct SAVED_FRIEND);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int friends_list_load(Messenger *m, uint8_t *data, uint32_t length)
|
|
|
|
{
|
2014-03-16 17:09:36 +08:00
|
|
|
int old_data = 0;
|
|
|
|
|
|
|
|
if (length % sizeof(struct SAVED_FRIEND) != 0) {
|
|
|
|
if (length % sizeof(struct SAVED_FRIEND_OLD) != 0)
|
|
|
|
return -1;
|
2014-03-06 22:55:50 +08:00
|
|
|
|
2014-03-16 17:09:36 +08:00
|
|
|
old_data = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if old data file is being used, offset size of current SAVED_FRIEND struct with size of old one */
|
|
|
|
uint32_t diff = old_data ? sizeof(struct SAVED_FRIEND) - sizeof(struct SAVED_FRIEND_OLD) : 0;
|
|
|
|
uint32_t struct_size = sizeof(struct SAVED_FRIEND) - diff;
|
|
|
|
uint32_t num = length / struct_size;
|
2014-03-06 22:55:50 +08:00
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < num; ++i) {
|
|
|
|
struct SAVED_FRIEND temp;
|
2014-03-16 17:09:36 +08:00
|
|
|
memcpy(&temp, data + i * struct_size, struct_size);
|
2014-03-06 22:55:50 +08:00
|
|
|
|
|
|
|
if (temp.status >= 3) {
|
|
|
|
int fnum = m_addfriend_norequest(m, temp.client_id);
|
|
|
|
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);
|
2014-03-14 08:20:48 +08:00
|
|
|
|
2014-03-16 17:09:36 +08:00
|
|
|
if (!old_data) {
|
|
|
|
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));
|
|
|
|
}
|
2014-03-06 22:55:50 +08:00
|
|
|
} else if (temp.status != 0) {
|
|
|
|
/* TODO: This is not a good way to do this. */
|
|
|
|
uint8_t address[FRIEND_ADDRESS_SIZE];
|
|
|
|
id_copy(address, temp.client_id);
|
|
|
|
memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp.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));
|
2014-03-14 03:10:59 +08:00
|
|
|
m_addfriend(m, address, temp.info, ntohs(temp.info_size));
|
2014-03-06 22:55:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2013-09-14 00:05:11 +08:00
|
|
|
/* return size of the messenger data (for saving) */
|
2013-10-24 02:42:39 +08:00
|
|
|
uint32_t messenger_size(Messenger *m)
|
2013-09-14 00:05:11 +08:00
|
|
|
{
|
2013-09-14 16:43:09 +08:00
|
|
|
uint32_t size32 = sizeof(uint32_t), sizesubhead = size32 * 2;
|
2013-09-14 00:05:11 +08:00
|
|
|
return size32 * 2 // global cookie
|
2013-09-17 07:08:57 +08:00
|
|
|
+ sizesubhead + sizeof(uint32_t) + crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
|
2013-09-16 23:22:01 +08:00
|
|
|
+ sizesubhead + DHT_size(m->dht) // DHT
|
2014-03-06 22:55:50 +08:00
|
|
|
+ sizesubhead + saved_friendslist_size(m) // Friendlist itself.
|
2013-09-16 23:22:01 +08:00
|
|
|
+ sizesubhead + m->name_length // Own nickname.
|
2014-03-07 04:35:52 +08:00
|
|
|
+ sizesubhead + m->statusmessage_length // status message
|
|
|
|
+ sizesubhead + 1 // status
|
2013-09-16 23:22:01 +08:00
|
|
|
;
|
2013-09-14 00:05:11 +08:00
|
|
|
}
|
|
|
|
|
2013-09-14 16:43:09 +08:00
|
|
|
static uint8_t *z_state_save_subheader(uint8_t *data, uint32_t len, uint16_t type)
|
2013-09-14 00:05:11 +08:00
|
|
|
{
|
2013-09-14 16:43:09 +08:00
|
|
|
uint32_t *data32 = (uint32_t *)data;
|
|
|
|
data32[0] = len;
|
|
|
|
data32[1] = (MESSENGER_STATE_COOKIE_TYPE << 16) | type;
|
|
|
|
data += sizeof(uint32_t) * 2;
|
2013-09-14 00:05:11 +08:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2014-03-06 22:55:50 +08:00
|
|
|
|
2013-09-14 00:05:11 +08:00
|
|
|
/* Save the messenger in data of size Messenger_size(). */
|
2013-10-24 02:42:39 +08:00
|
|
|
void messenger_save(Messenger *m, uint8_t *data)
|
2013-09-14 00:05:11 +08:00
|
|
|
{
|
2013-09-14 16:43:09 +08:00
|
|
|
uint32_t len;
|
2013-09-14 00:05:11 +08:00
|
|
|
uint16_t type;
|
|
|
|
uint32_t *data32, size32 = sizeof(uint32_t);
|
|
|
|
|
|
|
|
data32 = (uint32_t *)data;
|
|
|
|
data32[0] = 0;
|
|
|
|
data32[1] = MESSENGER_STATE_COOKIE_GLOBAL;
|
|
|
|
data += size32 * 2;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
assert(sizeof(get_nospam(&(m->fr))) == sizeof(uint32_t));
|
|
|
|
#endif
|
|
|
|
len = size32 + crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
|
|
|
|
type = MESSENGER_STATE_TYPE_NOSPAMKEYS;
|
|
|
|
data = z_state_save_subheader(data, len, type);
|
|
|
|
*(uint32_t *)data = get_nospam(&(m->fr));
|
|
|
|
save_keys(m->net_crypto, data + size32);
|
|
|
|
data += len;
|
|
|
|
|
|
|
|
len = DHT_size(m->dht);
|
|
|
|
type = MESSENGER_STATE_TYPE_DHT;
|
|
|
|
data = z_state_save_subheader(data, len, type);
|
|
|
|
DHT_save(m->dht, data);
|
|
|
|
data += len;
|
|
|
|
|
2014-03-06 22:55:50 +08:00
|
|
|
len = saved_friendslist_size(m);
|
2013-09-14 00:05:11 +08:00
|
|
|
type = MESSENGER_STATE_TYPE_FRIENDS;
|
|
|
|
data = z_state_save_subheader(data, len, type);
|
2014-03-06 22:55:50 +08:00
|
|
|
friends_list_save(m, data);
|
2013-09-14 00:05:11 +08:00
|
|
|
data += len;
|
|
|
|
|
|
|
|
len = m->name_length;
|
|
|
|
type = MESSENGER_STATE_TYPE_NAME;
|
|
|
|
data = z_state_save_subheader(data, len, type);
|
|
|
|
memcpy(data, m->name, len);
|
|
|
|
data += len;
|
2014-03-07 04:35:52 +08:00
|
|
|
|
|
|
|
len = m->statusmessage_length;
|
|
|
|
type = MESSENGER_STATE_TYPE_STATUSMESSAGE;
|
|
|
|
data = z_state_save_subheader(data, len, type);
|
|
|
|
memcpy(data, m->statusmessage, len);
|
|
|
|
data += len;
|
|
|
|
|
|
|
|
len = 1;
|
|
|
|
type = MESSENGER_STATE_TYPE_STATUS;
|
|
|
|
data = z_state_save_subheader(data, len, type);
|
|
|
|
*data = m->userstatus;
|
|
|
|
data += len;
|
2013-09-14 00:05:11 +08:00
|
|
|
}
|
|
|
|
|
2014-03-06 22:55:50 +08:00
|
|
|
static int messenger_load_state_callback_old(void *outer, uint8_t *data, uint32_t length, uint16_t type)
|
2013-09-14 00:05:11 +08:00
|
|
|
{
|
|
|
|
Messenger *m = outer;
|
2013-09-16 23:22:01 +08:00
|
|
|
|
|
|
|
switch (type) {
|
2013-09-14 00:05:11 +08:00
|
|
|
case MESSENGER_STATE_TYPE_NOSPAMKEYS:
|
|
|
|
if (length == crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t)) {
|
|
|
|
set_nospam(&(m->fr), *(uint32_t *)data);
|
|
|
|
load_keys(m->net_crypto, &data[sizeof(uint32_t)]);
|
2014-01-18 04:46:09 +08:00
|
|
|
#ifdef ENABLE_ASSOC_DHT
|
2013-11-15 02:05:36 +08:00
|
|
|
|
|
|
|
if (m->dht->assoc)
|
2013-11-17 08:04:49 +08:00
|
|
|
Assoc_self_client_id_changed(m->dht->assoc, m->net_crypto->self_public_key);
|
2014-01-18 04:46:09 +08:00
|
|
|
|
|
|
|
#endif
|
2013-09-16 23:22:01 +08:00
|
|
|
} else
|
2013-09-14 00:05:11 +08:00
|
|
|
return -1; /* critical */
|
2013-09-16 23:22:01 +08:00
|
|
|
|
2013-09-14 00:05:11 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MESSENGER_STATE_TYPE_DHT:
|
2013-11-11 06:57:24 +08:00
|
|
|
DHT_load(m->dht, data, length);
|
2013-09-14 00:05:11 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MESSENGER_STATE_TYPE_FRIENDS:
|
|
|
|
if (!(length % sizeof(Friend))) {
|
|
|
|
uint16_t num = length / sizeof(Friend);
|
|
|
|
Friend *friends = (Friend *)data;
|
|
|
|
uint32_t i;
|
2013-09-16 23:22:01 +08:00
|
|
|
|
2013-09-14 00:05:11 +08:00
|
|
|
for (i = 0; i < num; ++i) {
|
|
|
|
if (friends[i].status >= 3) {
|
|
|
|
int fnum = m_addfriend_norequest(m, friends[i].client_id);
|
|
|
|
setfriendname(m, fnum, friends[i].name, friends[i].name_length);
|
|
|
|
/* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */
|
|
|
|
} else if (friends[i].status != 0) {
|
|
|
|
/* TODO: This is not a good way to do this. */
|
|
|
|
uint8_t address[FRIEND_ADDRESS_SIZE];
|
2013-10-25 04:32:28 +08:00
|
|
|
id_copy(address, friends[i].client_id);
|
2013-09-14 00:05:11 +08:00
|
|
|
memcpy(address + crypto_box_PUBLICKEYBYTES, &(friends[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, friends[i].info, friends[i].info_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-16 23:22:01 +08:00
|
|
|
|
2013-09-14 00:05:11 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MESSENGER_STATE_TYPE_NAME:
|
|
|
|
if ((length > 0) && (length < MAX_NAME_LENGTH)) {
|
|
|
|
setname(m, data, length);
|
|
|
|
}
|
2013-09-16 23:22:01 +08:00
|
|
|
|
2013-09-14 00:05:11 +08:00
|
|
|
break;
|
|
|
|
|
2013-09-16 23:22:01 +08:00
|
|
|
#ifdef DEBUG
|
2013-09-18 04:28:39 +08:00
|
|
|
|
2013-09-17 04:18:35 +08:00
|
|
|
default:
|
2013-09-14 00:05:11 +08:00
|
|
|
fprintf(stderr, "Load state: contains unrecognized part (len %u, type %u)\n",
|
2013-09-16 23:22:01 +08:00
|
|
|
length, type);
|
2013-09-17 07:08:57 +08:00
|
|
|
break;
|
2013-09-16 23:22:01 +08:00
|
|
|
#endif
|
2013-09-14 00:05:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-06 22:55:50 +08:00
|
|
|
static int messenger_load_state_callback(void *outer, uint8_t *data, uint32_t length, uint16_t type)
|
|
|
|
{
|
|
|
|
Messenger *m = outer;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case MESSENGER_STATE_TYPE_NOSPAMKEYS:
|
|
|
|
if (length == crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t)) {
|
|
|
|
set_nospam(&(m->fr), *(uint32_t *)data);
|
|
|
|
load_keys(m->net_crypto, &data[sizeof(uint32_t)]);
|
|
|
|
#ifdef ENABLE_ASSOC_DHT
|
|
|
|
|
|
|
|
if (m->dht->assoc)
|
|
|
|
Assoc_self_client_id_changed(m->dht->assoc, m->net_crypto->self_public_key);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
} else
|
|
|
|
return -1; /* critical */
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MESSENGER_STATE_TYPE_DHT:
|
|
|
|
DHT_load(m->dht, data, length);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MESSENGER_STATE_TYPE_FRIENDS:
|
|
|
|
friends_list_load(m, data, length);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MESSENGER_STATE_TYPE_NAME:
|
|
|
|
if ((length > 0) && (length < MAX_NAME_LENGTH)) {
|
|
|
|
setname(m, data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2014-03-07 04:35:52 +08:00
|
|
|
case MESSENGER_STATE_TYPE_STATUSMESSAGE:
|
|
|
|
if ((length > 0) && (length < MAX_STATUSMESSAGE_LENGTH)) {
|
|
|
|
m_set_statusmessage(m, data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MESSENGER_STATE_TYPE_STATUS:
|
|
|
|
if (length == 1) {
|
|
|
|
m_set_userstatus(m, *data);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2014-03-06 22:55:50 +08:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Load state: contains unrecognized part (len %u, type %u)\n",
|
|
|
|
length, type);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-14 00:05:11 +08:00
|
|
|
/* Load the messenger from data of size length. */
|
2013-10-24 02:42:39 +08:00
|
|
|
int messenger_load(Messenger *m, uint8_t *data, uint32_t length)
|
2013-09-14 00:05:11 +08:00
|
|
|
{
|
|
|
|
uint32_t cookie_len = 2 * sizeof(uint32_t);
|
2013-09-16 23:22:01 +08:00
|
|
|
|
2013-09-14 16:43:09 +08:00
|
|
|
if (length < cookie_len)
|
|
|
|
return -1;
|
2013-09-14 00:05:11 +08:00
|
|
|
|
2013-09-14 16:43:09 +08:00
|
|
|
uint32_t *data32 = (uint32_t *)data;
|
2013-09-16 23:22:01 +08:00
|
|
|
|
2013-09-14 16:43:09 +08:00
|
|
|
if (!data32[0] && (data32[1] == MESSENGER_STATE_COOKIE_GLOBAL))
|
|
|
|
return load_state(messenger_load_state_callback, m, data + cookie_len,
|
|
|
|
length - cookie_len, MESSENGER_STATE_COOKIE_TYPE);
|
2014-03-06 22:55:50 +08:00
|
|
|
|
|
|
|
else if (!data32[0] && (data32[1] == MESSENGER_STATE_COOKIE_GLOBAL_OLD))
|
|
|
|
return load_state(messenger_load_state_callback_old, m, data + cookie_len,
|
|
|
|
length - cookie_len, MESSENGER_STATE_COOKIE_TYPE);
|
2013-09-14 16:43:09 +08:00
|
|
|
else /* old state file */
|
2013-10-28 08:25:31 +08:00
|
|
|
return -1;
|
2013-09-14 00:05:11 +08:00
|
|
|
}
|
|
|
|
|
2013-12-22 11:35:27 +08:00
|
|
|
/* return the size of data to pass to messenger_save_encrypted(...)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
uint32_t messenger_size_encrypted(Messenger *m)
|
|
|
|
{
|
|
|
|
return messenger_size(m) + crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the messenger, encrypting the data with key of length key_length
|
|
|
|
*
|
|
|
|
* return 0 on success.
|
|
|
|
* return -1 on failure.
|
|
|
|
*/
|
|
|
|
int messenger_save_encrypted(Messenger *m, uint8_t *data, uint8_t *key, uint16_t key_length)
|
|
|
|
{
|
|
|
|
uint32_t m_size = messenger_size(m);
|
|
|
|
uint8_t *plain_messenger = malloc(m_size);
|
|
|
|
|
|
|
|
if (plain_messenger == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
messenger_save(m, plain_messenger);
|
|
|
|
|
|
|
|
/* Hash the key with SHA256 to get a 32byte key. */
|
|
|
|
uint8_t hash[crypto_hash_sha256_BYTES];
|
|
|
|
crypto_hash_sha256(hash, key, key_length);
|
|
|
|
random_nonce(data);
|
|
|
|
encrypt_data_symmetric(hash, data, plain_messenger, m_size, data + crypto_secretbox_NONCEBYTES);
|
|
|
|
|
|
|
|
memset(plain_messenger, 0, m_size);
|
|
|
|
free(plain_messenger);
|
|
|
|
memset(hash, 0, crypto_hash_sha256_BYTES);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the messenger from data of size length encrypted with key of key_length.
|
|
|
|
*
|
|
|
|
* return 0 on success.
|
|
|
|
* return -1 on failure.
|
|
|
|
*/
|
|
|
|
int messenger_load_encrypted(Messenger *m, uint8_t *data, uint32_t length, uint8_t *key, uint16_t key_length)
|
|
|
|
{
|
|
|
|
if (length <= crypto_secretbox_MACBYTES + crypto_secretbox_NONCEBYTES)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
uint8_t *plain_messenger = malloc(length);
|
|
|
|
|
|
|
|
if (plain_messenger == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Hash the key with SHA256 to get a 32byte key. */
|
|
|
|
uint8_t hash[crypto_hash_sha256_BYTES];
|
|
|
|
crypto_hash_sha256(hash, key, key_length);
|
|
|
|
int len = decrypt_data_symmetric(hash, data, data + crypto_secretbox_NONCEBYTES, length - crypto_secretbox_NONCEBYTES,
|
|
|
|
plain_messenger);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((uint32_t)len == length - crypto_secretbox_NONCEBYTES - crypto_secretbox_MACBYTES) {
|
|
|
|
ret = messenger_load(m, plain_messenger, length - crypto_secretbox_NONCEBYTES - crypto_secretbox_MACBYTES);
|
|
|
|
} else {
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(plain_messenger, 0, length);
|
|
|
|
free(plain_messenger);
|
|
|
|
memset(hash, 0, crypto_hash_sha256_BYTES);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-09-22 05:47:30 +08:00
|
|
|
/* Return the number of friends in the instance m.
|
|
|
|
* You should use this to determine how much memory to allocate
|
|
|
|
* for copy_friendlist. */
|
2013-09-23 02:24:38 +08:00
|
|
|
uint32_t count_friendlist(Messenger *m)
|
2013-09-22 05:47:30 +08:00
|
|
|
{
|
2013-09-23 02:24:38 +08:00
|
|
|
uint32_t ret = 0;
|
2013-09-22 05:47:30 +08:00
|
|
|
uint32_t i;
|
2013-09-23 02:24:38 +08:00
|
|
|
|
2013-09-22 05:47:30 +08:00
|
|
|
for (i = 0; i < m->numfriends; i++) {
|
|
|
|
if (m->friendlist[i].status > 0) {
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
2013-09-23 02:24:38 +08:00
|
|
|
|
2013-09-22 05:47:30 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-02-08 08:43:21 +08:00
|
|
|
/* Return the number of online friends in the instance m. */
|
|
|
|
uint32_t get_num_online_friends(Messenger *m)
|
|
|
|
{
|
|
|
|
return m->numonline_friends;
|
|
|
|
}
|
|
|
|
|
2013-09-22 05:47:30 +08:00
|
|
|
/* Copy a list of valid friend IDs into the array out_list.
|
2013-09-23 02:24:38 +08:00
|
|
|
* If out_list is NULL, returns 0.
|
2013-09-22 05:47:30 +08:00
|
|
|
* Otherwise, returns the number of elements copied.
|
|
|
|
* If the array was too small, the contents
|
|
|
|
* of out_list will be truncated to list_size. */
|
2014-02-22 00:38:04 +08:00
|
|
|
uint32_t copy_friendlist(Messenger *m, int32_t *out_list, uint32_t list_size)
|
2013-09-22 05:47:30 +08:00
|
|
|
{
|
|
|
|
if (!out_list)
|
2013-09-23 02:24:38 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-09-22 05:47:30 +08:00
|
|
|
if (m->numfriends == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-09-23 02:24:38 +08:00
|
|
|
|
2013-09-22 05:47:30 +08:00
|
|
|
uint32_t i;
|
2013-09-23 02:24:38 +08:00
|
|
|
uint32_t ret = 0;
|
|
|
|
|
2013-09-22 05:47:30 +08:00
|
|
|
for (i = 0; i < m->numfriends; i++) {
|
2013-11-20 08:45:49 +08:00
|
|
|
if (ret >= list_size) {
|
2013-09-22 05:47:30 +08:00
|
|
|
break; /* Abandon ship */
|
|
|
|
}
|
2013-09-23 02:24:38 +08:00
|
|
|
|
2013-09-22 05:47:30 +08:00
|
|
|
if (m->friendlist[i].status > 0) {
|
2013-11-20 08:45:49 +08:00
|
|
|
out_list[ret] = i;
|
2013-09-22 05:47:30 +08:00
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
2013-09-23 02:24:38 +08:00
|
|
|
|
2013-09-22 05:47:30 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-09-05 05:45:36 +08:00
|
|
|
/* Allocate and return a list of valid friend id's. List must be freed by the
|
|
|
|
* caller.
|
|
|
|
*
|
|
|
|
* retun 0 if success.
|
|
|
|
* return -1 if failure.
|
|
|
|
*/
|
2014-02-22 00:38:04 +08:00
|
|
|
int get_friendlist(Messenger *m, int32_t **out_list, uint32_t *out_list_length)
|
2013-09-05 05:45:36 +08:00
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
*out_list_length = 0;
|
|
|
|
|
|
|
|
if (m->numfriends == 0) {
|
|
|
|
*out_list = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-22 00:38:04 +08:00
|
|
|
*out_list = malloc(m->numfriends * sizeof(int32_t));
|
2013-09-05 05:45:36 +08:00
|
|
|
|
|
|
|
if (*out_list == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < m->numfriends; i++) {
|
|
|
|
if (m->friendlist[i].status > 0) {
|
|
|
|
(*out_list)[i] = i;
|
|
|
|
(*out_list_length)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-19 12:10:07 +08:00
|
|
|
/* Return the number of chats in the instance m.
|
2013-11-19 12:05:35 +08:00
|
|
|
* You should use this to determine how much memory to allocate
|
2013-11-19 12:10:07 +08:00
|
|
|
* for copy_chatlist. */
|
2013-11-19 12:05:35 +08:00
|
|
|
uint32_t count_chatlist(Messenger *m)
|
|
|
|
{
|
|
|
|
uint32_t ret = 0;
|
|
|
|
uint32_t i;
|
2013-11-22 08:33:02 +08:00
|
|
|
|
2013-11-19 12:05:35 +08:00
|
|
|
for (i = 0; i < m->numchats; i++) {
|
2013-11-20 03:44:26 +08:00
|
|
|
if (m->chats[i]) {
|
|
|
|
ret++;
|
|
|
|
}
|
2013-11-19 12:05:35 +08:00
|
|
|
}
|
2013-11-22 08:33:02 +08:00
|
|
|
|
2013-11-19 12:05:35 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy a list of valid chat IDs into the array out_list.
|
|
|
|
* If out_list is NULL, returns 0.
|
|
|
|
* Otherwise, returns the number of elements copied.
|
|
|
|
* If the array was too small, the contents
|
|
|
|
* of out_list will be truncated to list_size. */
|
|
|
|
uint32_t copy_chatlist(Messenger *m, int *out_list, uint32_t list_size)
|
|
|
|
{
|
|
|
|
if (!out_list)
|
|
|
|
return 0;
|
2013-11-22 08:33:02 +08:00
|
|
|
|
2013-11-19 12:05:35 +08:00
|
|
|
if (m->numchats == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-22 08:33:02 +08:00
|
|
|
|
2013-11-19 12:05:35 +08:00
|
|
|
uint32_t i;
|
|
|
|
uint32_t ret = 0;
|
2013-11-22 08:33:02 +08:00
|
|
|
|
2013-11-19 12:05:35 +08:00
|
|
|
for (i = 0; i < m->numchats; i++) {
|
2013-11-20 08:45:49 +08:00
|
|
|
if (ret >= list_size) {
|
2013-11-19 12:05:35 +08:00
|
|
|
break; /* Abandon ship */
|
|
|
|
}
|
2013-11-22 08:33:02 +08:00
|
|
|
|
2013-11-20 03:44:26 +08:00
|
|
|
if (m->chats[i]) {
|
2013-11-20 08:45:49 +08:00
|
|
|
out_list[ret] = i;
|
2013-11-20 03:44:26 +08:00
|
|
|
ret++;
|
|
|
|
}
|
2013-11-19 12:05:35 +08:00
|
|
|
}
|
2013-11-22 08:33:02 +08:00
|
|
|
|
2013-11-19 12:05:35 +08:00
|
|
|
return ret;
|
|
|
|
}
|