mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Separated StatusMessage from UserStatus
This commit is contained in:
parent
dda22a0167
commit
9801eceaa4
182
core/Messenger.c
182
core/Messenger.c
|
@ -32,10 +32,11 @@ typedef struct {
|
|||
uint8_t info[MAX_DATA_SIZE]; /* the data that is sent during the friend requests we do */
|
||||
uint8_t name[MAX_NAME_LENGTH];
|
||||
uint8_t name_sent; /* 0 if we didn't send our name to this friend 1 if we have. */
|
||||
uint8_t *userstatus;
|
||||
uint16_t userstatus_length;
|
||||
uint8_t *statusmessage;
|
||||
uint16_t statusmessage_length;
|
||||
uint8_t statusmessage_sent;
|
||||
USERSTATUS userstatus;
|
||||
uint8_t userstatus_sent;
|
||||
USERSTATUS_KIND userstatus_kind;
|
||||
uint16_t info_size; /* length of the info */
|
||||
} Friend;
|
||||
|
||||
|
@ -44,9 +45,9 @@ uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
|
|||
static uint8_t self_name[MAX_NAME_LENGTH];
|
||||
static uint16_t self_name_length;
|
||||
|
||||
static uint8_t *self_userstatus;
|
||||
static uint16_t self_userstatus_len;
|
||||
static USERSTATUS_KIND self_userstatus_kind;
|
||||
static uint8_t *self_statusmessage;
|
||||
static uint16_t self_statusmessage_len;
|
||||
static USERSTATUS self_userstatus;
|
||||
|
||||
#define MAX_NUM_FRIENDS 256
|
||||
|
||||
|
@ -123,9 +124,9 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
|
|||
friendlist[i].crypt_connection_id = -1;
|
||||
friendlist[i].friend_request_id = -1;
|
||||
memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
|
||||
friendlist[i].userstatus = calloc(1, 1);
|
||||
friendlist[i].userstatus_length = 1;
|
||||
friendlist[i].userstatus_kind = USERSTATUS_KIND_OFFLINE;
|
||||
friendlist[i].statusmessage = calloc(1, 1);
|
||||
friendlist[i].statusmessage_length = 1;
|
||||
friendlist[i].userstatus = USERSTATUS_NONE;
|
||||
memcpy(friendlist[i].info, data, length);
|
||||
friendlist[i].info_size = length;
|
||||
|
||||
|
@ -148,8 +149,9 @@ int m_addfriend_norequest(uint8_t * client_id)
|
|||
friendlist[i].crypt_connection_id = -1;
|
||||
friendlist[i].friend_request_id = -1;
|
||||
memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
|
||||
friendlist[i].userstatus = calloc(1, 1);
|
||||
friendlist[i].userstatus_length = 1;
|
||||
friendlist[i].statusmessage = calloc(1, 1);
|
||||
friendlist[i].statusmessage_length = 1;
|
||||
friendlist[i].userstatus = USERSTATUS_NONE;
|
||||
numfriends++;
|
||||
return i;
|
||||
}
|
||||
|
@ -167,7 +169,7 @@ int m_delfriend(int friendnumber)
|
|||
|
||||
DHT_delfriend(friendlist[friendnumber].client_id);
|
||||
crypto_kill(friendlist[friendnumber].crypt_connection_id);
|
||||
free(friendlist[friendnumber].userstatus);
|
||||
free(friendlist[friendnumber].statusmessage);
|
||||
memset(&friendlist[friendnumber], 0, sizeof(Friend));
|
||||
uint32_t i;
|
||||
|
||||
|
@ -272,33 +274,28 @@ int getname(int friendnumber, uint8_t * name)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int m_set_userstatus(USERSTATUS_KIND kind, uint8_t *status, uint16_t length)
|
||||
int m_set_statusmessage(uint8_t *status, uint16_t length)
|
||||
{
|
||||
if (length > MAX_USERSTATUS_LENGTH)
|
||||
if (length > MAX_STATUSMESSAGE_LENGTH)
|
||||
return -1;
|
||||
if (kind != USERSTATUS_KIND_RETAIN) {
|
||||
self_userstatus_kind = kind;
|
||||
}
|
||||
uint8_t *newstatus = calloc(length, 1);
|
||||
memcpy(newstatus, status, length);
|
||||
free(self_userstatus);
|
||||
self_userstatus = newstatus;
|
||||
self_userstatus_len = length;
|
||||
free(self_statusmessage);
|
||||
self_statusmessage = newstatus;
|
||||
self_statusmessage_len = length;
|
||||
|
||||
uint32_t i;
|
||||
for (i = 0; i < numfriends; ++i)
|
||||
friendlist[i].userstatus_sent = 0;
|
||||
friendlist[i].statusmessage_sent = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int m_set_userstatus_kind(USERSTATUS_KIND kind) {
|
||||
if (kind >= USERSTATUS_KIND_INVALID) {
|
||||
int m_set_userstatus(USERSTATUS status)
|
||||
{
|
||||
if (status >= USERSTATUS_INVALID) {
|
||||
return -1;
|
||||
}
|
||||
if (kind == USERSTATUS_KIND_RETAIN) {
|
||||
return 0;
|
||||
}
|
||||
self_userstatus_kind = kind;
|
||||
self_userstatus = status;
|
||||
uint32_t i;
|
||||
for (i = 0; i < numfriends; ++i)
|
||||
friendlist[i].userstatus_sent = 0;
|
||||
|
@ -306,72 +303,83 @@ int m_set_userstatus_kind(USERSTATUS_KIND kind) {
|
|||
}
|
||||
|
||||
/* return the size of friendnumber's user status
|
||||
guaranteed to be at most MAX_USERSTATUS_LENGTH */
|
||||
int m_get_userstatus_size(int friendnumber)
|
||||
guaranteed to be at most MAX_STATUSMESSAGE_LENGTH */
|
||||
int m_get_statusmessage_size(int friendnumber)
|
||||
{
|
||||
if (friendnumber >= numfriends || friendnumber < 0)
|
||||
return -1;
|
||||
return friendlist[friendnumber].userstatus_length;
|
||||
return friendlist[friendnumber].statusmessage_length;
|
||||
}
|
||||
|
||||
/* copy the user status of friendnumber into buf, truncating if needed to maxlen
|
||||
bytes, use m_get_userstatus_size to find out how much you need to allocate */
|
||||
int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen)
|
||||
bytes, use m_get_statusmessage_size to find out how much you need to allocate */
|
||||
int m_copy_statusmessage(int friendnumber, uint8_t * buf, uint32_t maxlen)
|
||||
{
|
||||
if (friendnumber >= numfriends || friendnumber < 0)
|
||||
return -1;
|
||||
memset(buf, 0, maxlen);
|
||||
memcpy(buf, friendlist[friendnumber].userstatus, MIN(maxlen, MAX_USERSTATUS_LENGTH) - 1);
|
||||
memcpy(buf, friendlist[friendnumber].statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int m_copy_self_userstatus(uint8_t * buf, uint32_t maxlen)
|
||||
int m_copy_self_statusmessage(uint8_t * buf, uint32_t maxlen)
|
||||
{
|
||||
memset(buf, 0, maxlen);
|
||||
memcpy(buf, self_userstatus, MIN(maxlen, MAX_USERSTATUS_LENGTH) - 1);
|
||||
memcpy(buf, self_statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
USERSTATUS_KIND m_get_userstatus_kind(int friendnumber) {
|
||||
if (friendnumber >= numfriends || friendnumber < 0)
|
||||
return USERSTATUS_KIND_INVALID;
|
||||
USERSTATUS_KIND uk = friendlist[friendnumber].userstatus_kind;
|
||||
if (uk >= USERSTATUS_KIND_INVALID) {
|
||||
uk = USERSTATUS_KIND_ONLINE;
|
||||
}
|
||||
return uk;
|
||||
}
|
||||
|
||||
USERSTATUS_KIND m_get_self_userstatus_kind(void) {
|
||||
return self_userstatus_kind;
|
||||
}
|
||||
|
||||
static int send_userstatus(int friendnumber, uint8_t * status, uint16_t length)
|
||||
USERSTATUS m_get_userstatus(int friendnumber)
|
||||
{
|
||||
uint8_t *thepacket = malloc(length + 2);
|
||||
memcpy(thepacket + 2, status, length);
|
||||
thepacket[0] = PACKET_ID_USERSTATUS;
|
||||
thepacket[1] = self_userstatus_kind;
|
||||
int written = write_cryptpacket(friendlist[friendnumber].crypt_connection_id, thepacket, length + 2);
|
||||
if (friendnumber >= numfriends || friendnumber < 0)
|
||||
return USERSTATUS_INVALID;
|
||||
USERSTATUS status = friendlist[friendnumber].userstatus;
|
||||
if (status >= USERSTATUS_INVALID) {
|
||||
status = USERSTATUS_NONE;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
USERSTATUS m_get_self_userstatus(void)
|
||||
{
|
||||
return self_userstatus;
|
||||
}
|
||||
|
||||
static int send_statusmessage(int friendnumber, uint8_t * status, uint16_t length)
|
||||
{
|
||||
uint8_t *thepacket = malloc(length + 1);
|
||||
memcpy(thepacket + 1, status, length);
|
||||
thepacket[0] = PACKET_ID_STATUSMESSAGE;
|
||||
int written = write_cryptpacket(friendlist[friendnumber].crypt_connection_id, thepacket, length + 1);
|
||||
free(thepacket);
|
||||
return written;
|
||||
}
|
||||
|
||||
static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t length)
|
||||
static int send_userstatus(int friendnumber, USERSTATUS status)
|
||||
{
|
||||
uint8_t *thepacket = malloc(1 + 1);
|
||||
memcpy(thepacket + 1, &status, 1);
|
||||
thepacket[0] = PACKET_ID_USERSTATUS;
|
||||
int written = write_cryptpacket(friendlist[friendnumber].crypt_connection_id, thepacket, 1 + 1);
|
||||
free(thepacket);
|
||||
return written;
|
||||
}
|
||||
|
||||
static int set_friend_statusmessage(int friendnumber, uint8_t * status, uint16_t length)
|
||||
{
|
||||
if (friendnumber >= numfriends || friendnumber < 0)
|
||||
return -1;
|
||||
uint8_t *newstatus = calloc(length, 1);
|
||||
memcpy(newstatus, status, length);
|
||||
free(friendlist[friendnumber].userstatus);
|
||||
friendlist[friendnumber].userstatus = newstatus;
|
||||
friendlist[friendnumber].userstatus_length = length;
|
||||
free(friendlist[friendnumber].statusmessage);
|
||||
friendlist[friendnumber].statusmessage = newstatus;
|
||||
friendlist[friendnumber].statusmessage_length = length;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void set_friend_userstatus_kind(int friendnumber, USERSTATUS_KIND k)
|
||||
static void set_friend_userstatus(int friendnumber, USERSTATUS status)
|
||||
{
|
||||
friendlist[friendnumber].userstatus_kind = k;
|
||||
friendlist[friendnumber].userstatus = status;
|
||||
}
|
||||
|
||||
/* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t);
|
||||
|
@ -400,12 +408,20 @@ void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t))
|
|||
friend_namechange_isset = 1;
|
||||
}
|
||||
|
||||
static void (*friend_statuschange)(int, USERSTATUS_KIND, uint8_t *, uint16_t);
|
||||
static uint8_t friend_statuschange_isset = 0;
|
||||
void m_callback_userstatus(void (*function)(int, USERSTATUS_KIND, uint8_t *, uint16_t))
|
||||
static void (*friend_statusmessagechange)(int, uint8_t *, uint16_t);
|
||||
static uint8_t friend_statusmessagechange_isset = 0;
|
||||
void m_callback_statusmessage(void (*function)(int, uint8_t *, uint16_t))
|
||||
{
|
||||
friend_statuschange = function;
|
||||
friend_statuschange_isset = 1;
|
||||
friend_statusmessagechange = function;
|
||||
friend_statusmessagechange_isset = 1;
|
||||
}
|
||||
|
||||
static void (*friend_userstatuschange)(int, USERSTATUS);
|
||||
static uint8_t friend_userstatuschange_isset = 0;
|
||||
void m_callback_userstatus(void (*function)(int, USERSTATUS))
|
||||
{
|
||||
friend_userstatuschange = function;
|
||||
friend_userstatuschange_isset = 1;
|
||||
}
|
||||
|
||||
#define PORT 33445
|
||||
|
@ -413,7 +429,7 @@ void m_callback_userstatus(void (*function)(int, USERSTATUS_KIND, uint8_t *, uin
|
|||
int initMessenger(void)
|
||||
{
|
||||
new_keys();
|
||||
m_set_userstatus(USERSTATUS_KIND_ONLINE, (uint8_t*)"Online", sizeof("Online"));
|
||||
m_set_statusmessage((uint8_t*)"Online", sizeof("Online"));
|
||||
initNetCrypto();
|
||||
IP ip;
|
||||
ip.i = 0;
|
||||
|
@ -468,8 +484,12 @@ static void doFriends(void)
|
|||
if (m_sendname(i, self_name, self_name_length))
|
||||
friendlist[i].name_sent = 1;
|
||||
}
|
||||
if (friendlist[i].statusmessage_sent == 0) {
|
||||
if (send_statusmessage(i, self_statusmessage, self_statusmessage_len))
|
||||
friendlist[i].statusmessage_sent = 1;
|
||||
}
|
||||
if (friendlist[i].userstatus_sent == 0) {
|
||||
if (send_userstatus(i, self_userstatus, self_userstatus_len))
|
||||
if (send_userstatus(i, self_userstatus))
|
||||
friendlist[i].userstatus_sent = 1;
|
||||
}
|
||||
len = read_cryptpacket(friendlist[i].crypt_connection_id, temp);
|
||||
|
@ -484,18 +504,20 @@ static void doFriends(void)
|
|||
friendlist[i].name[len - 2] = 0; /* make sure the NULL terminator is present. */
|
||||
break;
|
||||
}
|
||||
case PACKET_ID_STATUSMESSAGE: {
|
||||
uint8_t *status = calloc(MIN(len - 1, MAX_STATUSMESSAGE_LENGTH), 1);
|
||||
memcpy(status, temp + 1, MIN(len - 1, MAX_STATUSMESSAGE_LENGTH));
|
||||
if (friend_statusmessagechange_isset)
|
||||
friend_statusmessagechange(i, status, MIN(len - 1, MAX_STATUSMESSAGE_LENGTH));
|
||||
set_friend_statusmessage(i, status, MIN(len - 1, MAX_STATUSMESSAGE_LENGTH));
|
||||
free(status);
|
||||
break;
|
||||
}
|
||||
case PACKET_ID_USERSTATUS: {
|
||||
if (len > 2) {
|
||||
uint8_t *status = calloc(MIN(len - 2, MAX_USERSTATUS_LENGTH), 1);
|
||||
memcpy(status, temp + 2, MIN(len - 2, MAX_USERSTATUS_LENGTH));
|
||||
if (friend_statuschange_isset)
|
||||
friend_statuschange(i, temp[1], status, MIN(len - 2, MAX_USERSTATUS_LENGTH));
|
||||
set_friend_userstatus(i, status, MIN(len - 2, MAX_USERSTATUS_LENGTH));
|
||||
free(status);
|
||||
} else if (friend_statuschange_isset) {
|
||||
friend_statuschange(i, temp[1], friendlist[i].userstatus, friendlist[i].userstatus_length);
|
||||
}
|
||||
set_friend_userstatus_kind(i, temp[1]);
|
||||
USERSTATUS status = *(temp + 1);
|
||||
if (friend_userstatuschange_isset)
|
||||
friend_userstatuschange(i, status);
|
||||
set_friend_userstatus(i, status);
|
||||
break;
|
||||
}
|
||||
case PACKET_ID_MESSAGE: {
|
||||
|
@ -641,7 +663,7 @@ int Messenger_load(uint8_t * data, uint32_t length)
|
|||
if(temp[i].status != 0) {
|
||||
int fnum = m_addfriend_norequest(temp[i].client_id);
|
||||
setfriendname(fnum, temp[i].name);
|
||||
/* set_friend_userstatus(fnum, temp[i].userstatus, temp[i].userstatus_length); */
|
||||
/* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */
|
||||
}
|
||||
}
|
||||
free(temp);
|
||||
|
|
|
@ -36,10 +36,11 @@ extern "C" {
|
|||
#endif
|
||||
|
||||
#define MAX_NAME_LENGTH 128
|
||||
#define MAX_USERSTATUS_LENGTH 128
|
||||
#define MAX_STATUSMESSAGE_LENGTH 128
|
||||
|
||||
#define PACKET_ID_NICKNAME 48
|
||||
#define PACKET_ID_USERSTATUS 49
|
||||
#define PACKET_ID_STATUSMESSAGE 49
|
||||
#define PACKET_ID_USERSTATUS 50
|
||||
#define PACKET_ID_MESSAGE 64
|
||||
|
||||
/* status definitions */
|
||||
|
@ -57,24 +58,18 @@ extern "C" {
|
|||
#define FAERR_ALREADYSENT -4
|
||||
#define FAERR_UNKNOWN -5
|
||||
|
||||
/* don't assume MAX_USERSTATUS_LENGTH will stay at 128, it may be increased
|
||||
/* don't assume MAX_STATUSMESSAGE_LENGTH will stay at 128, it may be increased
|
||||
to an absurdly large number later */
|
||||
|
||||
/* USERSTATUS_KIND
|
||||
* Represents the different kinds of userstatus
|
||||
* someone can have.
|
||||
* More on this later... */
|
||||
/* USERSTATUS
|
||||
* Represents userstatuses someone can have. */
|
||||
|
||||
typedef enum {
|
||||
USERSTATUS_KIND_RETAIN = (uint8_t)0, /* This is a special value that must not be returned by
|
||||
* m_get_userstatus_kind. You can pass it into m_set_userstatus
|
||||
* to keep the current USERSTATUS_KIND. */
|
||||
USERSTATUS_KIND_ONLINE, /* Recommended representation: Green. */
|
||||
USERSTATUS_KIND_AWAY, /* Recommended representation: Orange, or yellow. */
|
||||
USERSTATUS_KIND_BUSY, /* Recommended representation: Red. */
|
||||
USERSTATUS_KIND_OFFLINE, /* Recommended representation: Grey, semi-transparent. */
|
||||
USERSTATUS_KIND_INVALID,
|
||||
} USERSTATUS_KIND;
|
||||
USERSTATUS_NONE,
|
||||
USERSTATUS_AWAY,
|
||||
USERSTATUS_BUSY,
|
||||
USERSTATUS_INVALID
|
||||
} USERSTATUS;
|
||||
|
||||
/*
|
||||
* add a friend
|
||||
|
@ -144,26 +139,26 @@ int getname(int friendnumber, uint8_t *name);
|
|||
/* set our user status
|
||||
you are responsible for freeing status after
|
||||
returns 0 on success, -1 on failure */
|
||||
int m_set_userstatus(USERSTATUS_KIND kind, uint8_t *status, uint16_t length);
|
||||
int m_set_userstatus_kind(USERSTATUS_KIND kind);
|
||||
int m_set_statusmessage(uint8_t *status, uint16_t length);
|
||||
int m_set_userstatus(USERSTATUS status);
|
||||
|
||||
/* return the length of friendnumber's user status,
|
||||
/* return the length of friendnumber's status message,
|
||||
including null
|
||||
pass it into malloc */
|
||||
int m_get_userstatus_size(int friendnumber);
|
||||
int m_get_statusmessage_size(int friendnumber);
|
||||
|
||||
/* copy friendnumber's userstatus into buf, truncating if size is over maxlen
|
||||
get the size you need to allocate from m_get_userstatus_size
|
||||
The self variant will copy our own userstatus. */
|
||||
int m_copy_userstatus(int friendnumber, uint8_t *buf, uint32_t maxlen);
|
||||
int m_copy_self_userstatus(uint8_t *buf, uint32_t maxlen);
|
||||
/* copy friendnumber's status message into buf, truncating if size is over maxlen
|
||||
get the size you need to allocate from m_get_statusmessage_size
|
||||
The self variant will copy our own status message. */
|
||||
int m_copy_statusmessage(int friendnumber, uint8_t *buf, uint32_t maxlen);
|
||||
int m_copy_self_statusmessage(uint8_t *buf, uint32_t maxlen);
|
||||
|
||||
/* Return one of USERSTATUS_KIND values, except USERSTATUS_KIND_RETAIN.
|
||||
* Values unknown to your application should be represented as USERSTATUS_KIND_ONLINE.
|
||||
* As above, the self variant will return our own USERSTATUS_KIND.
|
||||
* If friendnumber is invalid, this shall return USERSTATUS_KIND_INVALID. */
|
||||
USERSTATUS_KIND m_get_userstatus_kind(int friendnumber);
|
||||
USERSTATUS_KIND m_get_self_userstatus_kind(void);
|
||||
/* Return one of USERSTATUS values.
|
||||
* Values unknown to your application should be represented as USERSTATUS_NONE.
|
||||
* As above, the self variant will return our own USERSTATUS.
|
||||
* If friendnumber is invalid, this shall return USERSTATUS_INVALID. */
|
||||
USERSTATUS m_get_userstatus(int friendnumber);
|
||||
USERSTATUS m_get_self_userstatus(void);
|
||||
|
||||
/* set the function that will be executed when a friend request is received.
|
||||
function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
|
||||
|
@ -178,10 +173,10 @@ void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t));
|
|||
you are not responsible for freeing newname */
|
||||
void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t));
|
||||
|
||||
/* set the callback for user status changes
|
||||
function(int friendnumber, USERSTATUS_KIND kind, uint8_t *newstatus, uint16_t length)
|
||||
/* set the callback for status message changes
|
||||
function(int friendnumber, uint8_t *newstatus, uint16_t length)
|
||||
you are not responsible for freeing newstatus */
|
||||
void m_callback_userstatus(void (*function)(int, USERSTATUS_KIND, uint8_t *, uint16_t));
|
||||
void m_callback_statusmessage(void (*function)(int, uint8_t *, uint16_t));
|
||||
|
||||
/* run this at startup
|
||||
returns 0 if no connection problems
|
||||
|
|
Loading…
Reference in New Issue
Block a user