2013-07-07 10:28:15 +08:00
|
|
|
/* Messenger.h
|
2013-07-26 09:45:56 +08:00
|
|
|
*
|
|
|
|
* An implementation of a simple text chat only messenger on the tox network core.
|
|
|
|
*
|
|
|
|
* NOTE: All the text in the messages must be encoded using UTF-8
|
2013-07-27 20:43:36 +08:00
|
|
|
*
|
2013-07-26 09:45:56 +08:00
|
|
|
* 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 16:02:17 +08:00
|
|
|
|
2013-07-19 01:56:50 +08:00
|
|
|
#ifndef MESSENGER_H
|
|
|
|
#define MESSENGER_H
|
2013-07-07 10:28:15 +08:00
|
|
|
|
|
|
|
#include "net_crypto.h"
|
|
|
|
#include "DHT.h"
|
2013-07-23 02:52:42 +08:00
|
|
|
#include "friend_requests.h"
|
2013-07-26 22:24:56 +08:00
|
|
|
#include "LAN_discovery.h"
|
2013-07-07 10:28:15 +08:00
|
|
|
|
2013-07-23 06:06:24 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2013-07-19 01:56:50 +08:00
|
|
|
#define MAX_NAME_LENGTH 128
|
|
|
|
#define MAX_USERSTATUS_LENGTH 128
|
2013-07-19 04:58:03 +08:00
|
|
|
|
|
|
|
#define PACKET_ID_NICKNAME 48
|
|
|
|
#define PACKET_ID_USERSTATUS 49
|
|
|
|
#define PACKET_ID_MESSAGE 64
|
|
|
|
|
2013-08-03 00:33:11 +08:00
|
|
|
/* status definitions */
|
|
|
|
#define FRIEND_ONLINE 4
|
|
|
|
#define FRIEND_CONFIRMED 3
|
|
|
|
#define FRIEND_REQUESTED 2
|
|
|
|
#define FRIEND_ADDED 1
|
|
|
|
#define NOFRIEND 0
|
|
|
|
|
|
|
|
/* errors for m_addfriend
|
|
|
|
* FAERR - Friend Add Error */
|
|
|
|
#define FAERR_TOOLONG -1
|
|
|
|
#define FAERR_NOMESSAGE -2
|
|
|
|
#define FAERR_OWNKEY -3
|
|
|
|
#define FAERR_ALREADYSENT -4
|
2013-08-03 00:35:34 +08:00
|
|
|
#define FAERR_UNKNOWN -5
|
2013-08-03 00:33:11 +08:00
|
|
|
|
2013-07-27 20:48:50 +08:00
|
|
|
/* don't assume MAX_USERSTATUS_LENGTH will stay at 128, it may be increased
|
|
|
|
to an absurdly large number later */
|
|
|
|
|
2013-08-06 03:30:07 +08:00
|
|
|
/* USERSTATUS_KIND
|
|
|
|
* Represents the different kinds of userstatus
|
|
|
|
* someone can have.
|
|
|
|
* More on this later... */
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2013-08-02 08:40:58 +08:00
|
|
|
/*
|
|
|
|
* add a friend
|
|
|
|
* set the data that will be sent along with friend request
|
|
|
|
* client_id is the client id of the friend
|
|
|
|
* data is the data and length is the length
|
|
|
|
* returns the friend number if success
|
|
|
|
* return -1 if message length is too long
|
|
|
|
* return -2 if no message (message length must be >= 1 byte)
|
|
|
|
* return -3 if user's own key
|
|
|
|
* return -4 if friend request already sent or already a friend
|
2013-08-06 03:30:07 +08:00
|
|
|
* return -5 for unknown error
|
2013-08-02 08:40:58 +08:00
|
|
|
*/
|
2013-07-27 20:48:50 +08:00
|
|
|
int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length);
|
|
|
|
|
|
|
|
|
|
|
|
/* add a friend without sending a friendrequest.
|
|
|
|
returns the friend number if success
|
|
|
|
return -1 if failure. */
|
|
|
|
int m_addfriend_norequest(uint8_t *client_id);
|
|
|
|
|
|
|
|
/* return the friend id associated to that client id.
|
|
|
|
return -1 if no such friend */
|
|
|
|
int getfriend_id(uint8_t *client_id);
|
|
|
|
|
|
|
|
/* copies the public key associated to that friend id into client_id buffer.
|
|
|
|
make sure that client_id is of size CLIENT_ID_SIZE.
|
|
|
|
return 0 if success
|
|
|
|
return -1 if failure */
|
|
|
|
int getclient_id(int friend_id, uint8_t *client_id);
|
|
|
|
|
|
|
|
/* remove a friend */
|
|
|
|
int m_delfriend(int friendnumber);
|
|
|
|
|
|
|
|
/* return 4 if friend is online
|
|
|
|
return 3 if friend is confirmed
|
|
|
|
return 2 if the friend request was sent
|
|
|
|
return 1 if the friend was added
|
|
|
|
return 0 if there is no friend with that number */
|
|
|
|
int m_friendstatus(int friendnumber);
|
|
|
|
|
|
|
|
/* send a text chat message to an online friend
|
|
|
|
returns 1 if packet was successfully put into the send queue
|
|
|
|
return 0 if it was not */
|
|
|
|
int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length);
|
|
|
|
|
|
|
|
/* Set our nickname
|
2013-07-28 09:08:06 +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
|
|
|
|
return 0 if success
|
|
|
|
return -1 if failure */
|
2013-07-27 20:48:50 +08:00
|
|
|
int setname(uint8_t *name, uint16_t length);
|
|
|
|
|
2013-07-31 02:30:33 +08:00
|
|
|
/* get our nickname
|
2013-08-06 03:30:07 +08:00
|
|
|
put it in name
|
2013-07-31 02:30:33 +08:00
|
|
|
return the length of the name*/
|
|
|
|
uint16_t getself_name(uint8_t *name);
|
2013-07-30 23:09:09 +08:00
|
|
|
|
2013-07-27 20:48:50 +08:00
|
|
|
/* get name of friendnumber
|
|
|
|
put it in name
|
|
|
|
name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
|
|
|
|
return 0 if success
|
|
|
|
return -1 if failure */
|
|
|
|
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 */
|
2013-08-06 03:30:07 +08:00
|
|
|
int m_set_userstatus(USERSTATUS_KIND kind, uint8_t *status, uint16_t length);
|
|
|
|
int m_set_userstatus_kind(USERSTATUS_KIND kind);
|
2013-07-27 20:48:50 +08:00
|
|
|
|
|
|
|
/* return the length of friendnumber's user status,
|
|
|
|
including null
|
|
|
|
pass it into malloc */
|
|
|
|
int m_get_userstatus_size(int friendnumber);
|
|
|
|
|
|
|
|
/* copy friendnumber's userstatus into buf, truncating if size is over maxlen
|
2013-08-06 03:30:07 +08:00
|
|
|
get the size you need to allocate from m_get_userstatus_size
|
|
|
|
The self variant will copy our own userstatus. */
|
2013-07-27 20:48:50 +08:00
|
|
|
int m_copy_userstatus(int friendnumber, uint8_t *buf, uint32_t maxlen);
|
2013-08-06 03:30:07 +08:00
|
|
|
int m_copy_self_userstatus(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);
|
2013-07-27 20:48:50 +08:00
|
|
|
|
|
|
|
/* 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) */
|
|
|
|
void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
|
|
|
|
|
|
|
|
/* set the function that will be executed when a message from a friend is received.
|
|
|
|
function format is: function(int friendnumber, uint8_t * message, uint32_t length) */
|
|
|
|
void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t));
|
|
|
|
|
|
|
|
/* set the callback for name changes
|
|
|
|
function(int friendnumber, uint8_t *newname, uint16_t length)
|
|
|
|
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
|
2013-08-06 03:30:07 +08:00
|
|
|
function(int friendnumber, USERSTATUS_KIND kind, uint8_t *newstatus, uint16_t length)
|
2013-07-27 20:48:50 +08:00
|
|
|
you are not responsible for freeing newstatus */
|
2013-08-06 03:30:07 +08:00
|
|
|
void m_callback_userstatus(void (*function)(int, USERSTATUS_KIND, uint8_t *, uint16_t));
|
2013-07-27 20:48:50 +08:00
|
|
|
|
|
|
|
/* run this at startup
|
|
|
|
returns 0 if no connection problems
|
|
|
|
returns -1 if there are problems */
|
2013-08-05 21:57:17 +08:00
|
|
|
int initMessenger(void);
|
2013-07-27 20:48:50 +08:00
|
|
|
|
|
|
|
/* the main loop that needs to be run at least 200 times per second */
|
2013-08-05 21:57:17 +08:00
|
|
|
void doMessenger(void);
|
2013-07-27 20:48:50 +08:00
|
|
|
|
|
|
|
/* SAVING AND LOADING FUNCTIONS: */
|
|
|
|
|
|
|
|
/* returns the size of the messenger data (for saving) */
|
2013-08-05 21:57:17 +08:00
|
|
|
uint32_t Messenger_size(void);
|
2013-07-27 20:48:50 +08:00
|
|
|
|
|
|
|
/* save the messenger in data (must be allocated memory of size Messenger_size()) */
|
|
|
|
void Messenger_save(uint8_t *data);
|
|
|
|
|
|
|
|
/* load the messenger from data of size length */
|
|
|
|
int Messenger_load(uint8_t *data, uint32_t length);
|
2013-07-18 00:07:19 +08:00
|
|
|
|
2013-07-23 06:06:24 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-07-07 10:28:15 +08:00
|
|
|
#endif
|