mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
New API done and tested.
Some stuff needs to be cleaned a bit though.
This commit is contained in:
parent
bcb87139a2
commit
9d3a8d94f2
|
@ -144,8 +144,9 @@ static void at_shutdown(void)
|
|||
returns NULL if there are problems */
|
||||
Networking_Core *new_networking(IP ip, uint16_t port)
|
||||
{
|
||||
if(at_startup() != 0)
|
||||
if (at_startup() != 0)
|
||||
return NULL;
|
||||
|
||||
/* initialize our socket */
|
||||
Networking_Core *temp = calloc(1, sizeof(Networking_Core));
|
||||
|
||||
|
@ -215,43 +216,3 @@ void kill_networking(Networking_Core *net)
|
|||
free(net);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
resolve_addr():
|
||||
address should represent IPv4 or a hostname with A record
|
||||
|
||||
returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
|
||||
returns 0 on failure
|
||||
|
||||
TODO: Fix ipv6 support
|
||||
*/
|
||||
uint32_t resolve_addr(const char *address)
|
||||
{
|
||||
struct addrinfo *server = NULL;
|
||||
struct addrinfo hints;
|
||||
int rc;
|
||||
uint32_t addr;
|
||||
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_INET; // IPv4 only right now.
|
||||
hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses.
|
||||
|
||||
rc = getaddrinfo(address, "echo", &hints, &server);
|
||||
|
||||
// Lookup failed.
|
||||
if (rc != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// IPv4 records only..
|
||||
if (server->ai_family != AF_INET) {
|
||||
freeaddrinfo(server);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
addr = ((struct sockaddr_in *)server->ai_addr)->sin_addr.s_addr;
|
||||
|
||||
freeaddrinfo(server);
|
||||
return addr;
|
||||
}
|
||||
|
|
|
@ -151,16 +151,6 @@ Networking_Core *new_networking(IP ip, uint16_t port);
|
|||
/* function to cleanup networking stuff(doesn't do much right now) */
|
||||
void kill_networking(Networking_Core *net);
|
||||
|
||||
/*
|
||||
resolve_addr():
|
||||
address should represent IPv4 or a hostname with A record
|
||||
|
||||
returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
|
||||
returns 0 on failure
|
||||
|
||||
TODO: Fix ipv6 support
|
||||
*/
|
||||
uint32_t resolve_addr(const char *address);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
146
core/tox.h
146
core/tox.h
|
@ -24,34 +24,53 @@
|
|||
#ifndef TOX_H
|
||||
#define TOX_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define MAX_NAME_LENGTH 128
|
||||
#define MAX_STATUSMESSAGE_LENGTH 128
|
||||
#define TOX_MAX_NAME_LENGTH 128
|
||||
#define TOX_MAX_STATUSMESSAGE_LENGTH 128
|
||||
#define TOX_CLIENT_ID_SIZE 32
|
||||
|
||||
#define FRIEND_ADDRESS_SIZE (crypto_box_PUBLICKEYBYTES + sizeof(uint32_t) + sizeof(uint16_t))
|
||||
#define TOX_FRIEND_ADDRESS_SIZE (TOX_CLIENT_ID_SIZE + sizeof(uint32_t) + sizeof(uint16_t))
|
||||
|
||||
|
||||
typedef union {
|
||||
uint8_t c[4];
|
||||
uint16_t s[2];
|
||||
uint32_t i;
|
||||
} IP;
|
||||
|
||||
typedef struct {
|
||||
IP ip;
|
||||
uint16_t port;
|
||||
/* not used for anything right now */
|
||||
uint16_t padding;
|
||||
} IP_Port;
|
||||
|
||||
/* status definitions */
|
||||
#define FRIEND_ONLINE 4
|
||||
#define FRIEND_CONFIRMED 3
|
||||
#define FRIEND_REQUESTED 2
|
||||
#define FRIEND_ADDED 1
|
||||
#define NOFRIEND 0
|
||||
enum {
|
||||
TOX_NOFRIEND,
|
||||
TOX_FRIEND_ADDED,
|
||||
TOX_FRIEND_REQUESTED,
|
||||
TOX_FRIEND_CONFIRMED,
|
||||
TOX_FRIEND_ONLINE,
|
||||
};
|
||||
|
||||
/* errors for m_addfriend
|
||||
* FAERR - Friend Add Error */
|
||||
#define FAERR_TOOLONG -1
|
||||
#define FAERR_NOMESSAGE -2
|
||||
#define FAERR_OWNKEY -3
|
||||
#define FAERR_ALREADYSENT -4
|
||||
#define FAERR_UNKNOWN -5
|
||||
#define FAERR_BADCHECKSUM -6
|
||||
#define FAERR_SETNEWNOSPAM -7
|
||||
#define FAERR_NOMEM -8
|
||||
|
||||
enum {
|
||||
TOX_FAERR_TOOLONG = -1,
|
||||
TOX_FAERR_NOMESSAGE = -2,
|
||||
TOX_FAERR_OWNKEY = -3,
|
||||
TOX_FAERR_ALREADYSENT = -4,
|
||||
TOX_FAERR_UNKNOWN = -5,
|
||||
TOX_FAERR_BADCHECKSUM = -6,
|
||||
TOX_FAERR_SETNEWNOSPAM = -7,
|
||||
TOX_FAERR_NOMEM = -8
|
||||
};
|
||||
/* USERSTATUS
|
||||
* Represents userstatuses someone can have. */
|
||||
|
||||
|
@ -63,12 +82,14 @@ typedef enum {
|
|||
}
|
||||
TOX_USERSTATUS;
|
||||
|
||||
typedef void Tox;
|
||||
|
||||
/*
|
||||
* returns a FRIEND_ADDRESS_SIZE byte address to give to others.
|
||||
* format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)]
|
||||
*
|
||||
*/
|
||||
void tox_getaddress(void *tox, uint8_t *address);
|
||||
void tox_getaddress(Tox *tox, uint8_t *address);
|
||||
|
||||
/*
|
||||
* add a friend
|
||||
|
@ -76,43 +97,43 @@ void tox_getaddress(void *tox, uint8_t *address);
|
|||
* address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum.
|
||||
* 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
|
||||
* return -5 for unknown error
|
||||
* return -6 if bad checksum in address
|
||||
* return -7 if the friend was already there but the nospam was different
|
||||
* 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 -8 if increasing the friend list size fails
|
||||
* return FAERR_NOMEM if increasing the friend list size fails
|
||||
*/
|
||||
int tox_addfriend(void *tox, uint8_t *address, uint8_t *data, uint16_t length);
|
||||
int tox_addfriend(Tox *tox, uint8_t *address, uint8_t *data, uint16_t length);
|
||||
|
||||
|
||||
/* add a friend without sending a friendrequest.
|
||||
returns the friend number if success
|
||||
return -1 if failure. */
|
||||
int tox_addfriend_norequest(void *tox, uint8_t *client_id);
|
||||
int tox_addfriend_norequest(Tox *tox, uint8_t *client_id);
|
||||
|
||||
/* return the friend id associated to that client id.
|
||||
return -1 if no such friend */
|
||||
int tox_getfriend_id(void *tox, uint8_t *client_id);
|
||||
int tox_getfriend_id(Tox *tox, 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 tox_getclient_id(void *tox, int friend_id, uint8_t *client_id);
|
||||
int tox_getclient_id(Tox *tox, int friend_id, uint8_t *client_id);
|
||||
|
||||
/* remove a friend */
|
||||
int tox_delfriend(void *tox, int friendnumber);
|
||||
int tox_delfriend(Tox *tox, 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 tox_friendstatus(void *tox, int friendnumber);
|
||||
int tox_friendstatus(Tox *tox, int friendnumber);
|
||||
|
||||
/* send a text chat message to an online friend
|
||||
returns the message id if packet was successfully put into the send queue
|
||||
|
@ -121,13 +142,13 @@ int tox_friendstatus(void *tox, int friendnumber);
|
|||
if one is received.
|
||||
m_sendmessage_withid will send a message with the id of your choosing,
|
||||
however we can generate an id for you by calling plain m_sendmessage. */
|
||||
uint32_t tox_sendmessage(void *tox, int friendnumber, uint8_t *message, uint32_t length);
|
||||
uint32_t tox_sendmessage_withid(void *tox, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length);
|
||||
uint32_t tox_sendmessage(Tox *tox, int friendnumber, uint8_t *message, uint32_t length);
|
||||
uint32_t tox_sendmessage_withid(Tox *tox, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length);
|
||||
|
||||
/* send an action to an online friend
|
||||
returns 1 if packet was successfully put into the send queue
|
||||
return 0 if it was not */
|
||||
int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length);
|
||||
int tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length);
|
||||
|
||||
/* Set our nickname
|
||||
name must be a string of maximum MAX_NAME_LENGTH length.
|
||||
|
@ -135,7 +156,7 @@ int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length
|
|||
length is the length of name with the NULL terminator
|
||||
return 0 if success
|
||||
return -1 if failure */
|
||||
int tox_setname(void *tox, uint8_t *name, uint16_t length);
|
||||
int tox_setname(Tox *tox, uint8_t *name, uint16_t length);
|
||||
|
||||
/*
|
||||
Get your nickname.
|
||||
|
@ -144,71 +165,71 @@ int tox_setname(void *tox, uint8_t *name, uint16_t length);
|
|||
nlen The length of the string buffer.
|
||||
returns Return the length of the name, 0 on error.
|
||||
*/
|
||||
uint16_t tox_getselfname(void *tox, uint8_t *name, uint16_t nlen);
|
||||
uint16_t tox_getselfname(Tox *tox, uint8_t *name, uint16_t nlen);
|
||||
|
||||
/* 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 tox_getname(void *tox, int friendnumber, uint8_t *name);
|
||||
int tox_getname(Tox *tox, int friendnumber, uint8_t *name);
|
||||
|
||||
/* set our user status
|
||||
you are responsible for freeing status after
|
||||
returns 0 on success, -1 on failure */
|
||||
int tox_set_statusmessage(void *tox, uint8_t *status, uint16_t length);
|
||||
int tox_set_userstatus(void *tox, USERSTATUS status);
|
||||
int tox_set_statusmessage(Tox *tox, uint8_t *status, uint16_t length);
|
||||
int tox_set_userstatus(Tox *tox, TOX_USERSTATUS status);
|
||||
|
||||
/* return the length of friendnumber's status message,
|
||||
including null
|
||||
pass it into malloc */
|
||||
int tox_get_statusmessage_size(void *tox, int friendnumber);
|
||||
int tox_get_statusmessage_size(Tox *tox, int friendnumber);
|
||||
|
||||
/* 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 tox_copy_statusmessage(void *tox, int friendnumber, uint8_t *buf, uint32_t maxlen);
|
||||
int tox_copy_self_statusmessage(void *tox, uint8_t *buf, uint32_t maxlen);
|
||||
int tox_copy_statusmessage(Tox *tox, int friendnumber, uint8_t *buf, uint32_t maxlen);
|
||||
int tox_copy_self_statusmessage(Tox *tox, uint8_t *buf, uint32_t maxlen);
|
||||
|
||||
/* 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. */
|
||||
TOX_USERSTATUS tox_get_userstatus(void *tox, int friendnumber);
|
||||
TOX_USERSTATUS tox_get_selfuserstatus(void *tox);
|
||||
TOX_USERSTATUS tox_get_userstatus(Tox *tox, int friendnumber);
|
||||
TOX_USERSTATUS tox_get_selfuserstatus(Tox *tox);
|
||||
|
||||
/* Sets whether we send read receipts for friendnumber.
|
||||
* This function is not lazy, and it will fail if yesno is not (0 or 1).*/
|
||||
void tox_set_sends_receipts(void *tox, int friendnumber, int yesno);
|
||||
void tox_set_sends_receipts(Tox *tox, int friendnumber, int yesno);
|
||||
|
||||
/* 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 tox_callback_friendrequest(void *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata);
|
||||
void tox_callback_friendrequest(Tox *tox, void (*function)(uint8_t *, uint8_t *, uint16_t, void *), void *userdata);
|
||||
|
||||
/* 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 tox_callback_friendmessage(void *tox, void (*function)(void *tox, int, uint8_t *, uint16_t, void *),
|
||||
void tox_callback_friendmessage(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *),
|
||||
void *userdata);
|
||||
|
||||
/* set the function that will be executed when an action from a friend is received.
|
||||
function format is: function(int friendnumber, uint8_t * action, uint32_t length) */
|
||||
void tox_callback_action(void *tox, void (*function)(void *tox, int, uint8_t *, uint16_t, void *), void *userdata);
|
||||
void tox_callback_action(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *), void *userdata);
|
||||
|
||||
/* set the callback for name changes
|
||||
function(int friendnumber, uint8_t *newname, uint16_t length)
|
||||
you are not responsible for freeing newname */
|
||||
void tox_callback_namechange(void *tox, void (*function)(void *tox, int, uint8_t *, uint16_t, void *),
|
||||
void tox_callback_namechange(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *),
|
||||
void *userdata);
|
||||
|
||||
/* set the callback for status message changes
|
||||
function(int friendnumber, uint8_t *newstatus, uint16_t length)
|
||||
you are not responsible for freeing newstatus */
|
||||
void tox_callback_statusmessage(void *tox, void (*function)(void *tox, int, uint8_t *, uint16_t, void *),
|
||||
void tox_callback_statusmessage(Tox *tox, void (*function)(Tox *tox, int, uint8_t *, uint16_t, void *),
|
||||
void *userdata);
|
||||
|
||||
/* set the callback for status type changes
|
||||
function(int friendnumber, USERSTATUS kind) */
|
||||
void tox_callback_userstatus(void *tox, void (*function)(void *tox, int, USERSTATUS, void *), void *userdata);
|
||||
void tox_callback_userstatus(Tox *tox, void (*function)(Tox *tox, int, TOX_USERSTATUS, void *), void *userdata);
|
||||
|
||||
/* set the callback for read receipts
|
||||
function(int friendnumber, uint32_t receipt)
|
||||
|
@ -217,7 +238,7 @@ void tox_callback_userstatus(void *tox, void (*function)(void *tox, int, USERSTA
|
|||
has been received on the other side. since core doesn't
|
||||
track ids for you, receipt may not correspond to any message
|
||||
in that case, you should discard it. */
|
||||
void tox_callback_read_receipt(void *tox, void (*function)(void *tox, int, uint32_t, void *), void *userdata);
|
||||
void tox_callback_read_receipt(Tox *tox, void (*function)(Tox *tox, int, uint32_t, void *), void *userdata);
|
||||
|
||||
/* set the callback for connection status changes
|
||||
function(int friendnumber, uint8_t status)
|
||||
|
@ -227,38 +248,39 @@ void tox_callback_read_receipt(void *tox, void (*function)(void *tox, int, uint3
|
|||
note that this callback is not called when adding friends, thus the "after
|
||||
being previously online" part. it's assumed that when adding friends,
|
||||
their connection status is offline. */
|
||||
void tox_callback_connectionstatus(void *tox, void (*function)(void *tox, int, uint8_t, void *), void *userdata);
|
||||
void tox_callback_connectionstatus(Tox *tox, void (*function)(Tox *tox, int, uint8_t, void *), void *userdata);
|
||||
|
||||
/* Use this function to bootstrap the client
|
||||
Sends a get nodes request to the given node with ip port and public_key */
|
||||
void tox_bootstrap(void *tox, IP_Port ip_port, uint8_t *public_key);
|
||||
void tox_bootstrap(Tox *tox, IP_Port ip_port, uint8_t *public_key);
|
||||
|
||||
/* returns 0 if we are not connected to the DHT
|
||||
returns 1 if we are */
|
||||
int tox_isconnected(void *tox);
|
||||
int tox_isconnected(Tox *tox);
|
||||
|
||||
/* run this at startup
|
||||
* returns allocated instance of tox on success
|
||||
* returns 0 if there are problems */
|
||||
void *tox_new(void);
|
||||
Tox *tox_new(void);
|
||||
|
||||
/* run this before closing shop
|
||||
* free all datastructures */
|
||||
void tox_kill(void *tox);
|
||||
void tox_kill(Tox *tox);
|
||||
|
||||
/* the main loop that needs to be run at least 20 times per second */
|
||||
void tox_do(void *tox);
|
||||
void tox_do(Tox *tox);
|
||||
|
||||
/* SAVING AND LOADING FUNCTIONS: */
|
||||
|
||||
/* returns the size of the messenger data (for saving) */
|
||||
uint32_t tox_size(void *tox);
|
||||
uint32_t tox_size(Tox *tox);
|
||||
|
||||
/* save the messenger in data (must be allocated memory of size Messenger_size()) */
|
||||
void tox_save(void *tox, uint8_t *data);
|
||||
void tox_save(Tox *tox, uint8_t *data);
|
||||
|
||||
/* load the messenger from data of size length */
|
||||
int tox_load(void *tox, uint8_t *data, uint32_t length);
|
||||
int tox_load(Tox *tox, uint8_t *data, uint32_t length);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -72,6 +72,47 @@ int b16_to_key(char b16_string[], uint8_t *bs_pubkey)
|
|||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
resolve_addr():
|
||||
address should represent IPv4 or a hostname with A record
|
||||
|
||||
returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
|
||||
returns 0 on failure
|
||||
|
||||
TODO: Fix ipv6 support
|
||||
*/
|
||||
|
||||
uint32_t resolve_addr(const char *address)
|
||||
{
|
||||
struct addrinfo *server = NULL;
|
||||
struct addrinfo hints;
|
||||
int rc;
|
||||
uint32_t addr;
|
||||
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_INET; // IPv4 only right now.
|
||||
hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses.
|
||||
|
||||
rc = getaddrinfo(address, "echo", &hints, &server);
|
||||
|
||||
// Lookup failed.
|
||||
if (rc != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// IPv4 records only..
|
||||
if (server->ai_family != AF_INET) {
|
||||
freeaddrinfo(server);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
addr = ((struct sockaddr_in *)server->ai_addr)->sin_addr.s_addr;
|
||||
|
||||
freeaddrinfo(server);
|
||||
return addr;
|
||||
}
|
||||
|
||||
/* This unction connects to all specified servers
|
||||
and connect to them.
|
||||
returns 1 if the connection to the DHT is up
|
||||
|
|
|
@ -43,22 +43,22 @@ char *help = "[i] commands:\n/f ID (to add friend)\n/m friendnumber message "
|
|||
int x, y;
|
||||
|
||||
typedef struct {
|
||||
uint8_t id[CLIENT_ID_SIZE];
|
||||
uint8_t id[TOX_CLIENT_ID_SIZE];
|
||||
uint8_t accepted;
|
||||
} Friend_request;
|
||||
|
||||
Friend_request pending_requests[256];
|
||||
uint8_t num_requests = 0;
|
||||
|
||||
void get_id(Messenger *m, char *data)
|
||||
void get_id(Tox *m, char *data)
|
||||
{
|
||||
sprintf(data, "[i] ID: ");
|
||||
int offset = strlen(data);
|
||||
int i = 0;
|
||||
uint8_t address[FRIEND_ADDRESS_SIZE];
|
||||
getaddress(m, address);
|
||||
uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
|
||||
tox_getaddress(m, address);
|
||||
|
||||
for (; i < FRIEND_ADDRESS_SIZE; i++) {
|
||||
for (; i < TOX_FRIEND_ADDRESS_SIZE; i++) {
|
||||
sprintf(data + 2 * i + offset, "%02X ", address[i]);
|
||||
}
|
||||
}
|
||||
|
@ -75,15 +75,15 @@ void new_lines(char *line)
|
|||
}
|
||||
|
||||
|
||||
void print_friendlist(Messenger *m)
|
||||
void print_friendlist(Tox *m)
|
||||
{
|
||||
char name[MAX_NAME_LENGTH];
|
||||
char name[TOX_MAX_NAME_LENGTH];
|
||||
int i = 0;
|
||||
new_lines("[i] Friend List:");
|
||||
|
||||
while (getname(m, i, (uint8_t *)name) != -1) {
|
||||
while (tox_getname(m, i, (uint8_t *)name) != -1) {
|
||||
/* account for the longest name and the longest "base" string */
|
||||
char fstring[MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")];
|
||||
char fstring[TOX_MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")];
|
||||
|
||||
if (strlen(name) <= 0) {
|
||||
sprintf(fstring, "[i] Friend: No Friend!\n\tid: %i", i);
|
||||
|
@ -99,14 +99,14 @@ void print_friendlist(Messenger *m)
|
|||
new_lines("\tno friends! D:");
|
||||
}
|
||||
|
||||
char *format_message(Messenger *m, char *message, int friendnum)
|
||||
char *format_message(Tox *m, char *message, int friendnum)
|
||||
{
|
||||
char name[MAX_NAME_LENGTH];
|
||||
char name[TOX_MAX_NAME_LENGTH];
|
||||
|
||||
if (friendnum != -1) {
|
||||
getname(m, friendnum, (uint8_t *)name);
|
||||
tox_getname(m, friendnum, (uint8_t *)name);
|
||||
} else {
|
||||
getself_name(m, (uint8_t *)name, sizeof(name));
|
||||
tox_getselfname(m, (uint8_t *)name, sizeof(name));
|
||||
}
|
||||
|
||||
char *msg = malloc(100 + strlen(message) + strlen(name) + 1);
|
||||
|
@ -129,7 +129,7 @@ char *format_message(Messenger *m, char *message, int friendnum)
|
|||
return msg;
|
||||
}
|
||||
|
||||
void line_eval(Messenger *m, char *line)
|
||||
void line_eval(Tox *m, char *line)
|
||||
{
|
||||
if (line[0] == '/') {
|
||||
char inpt_command = line[1];
|
||||
|
@ -146,28 +146,28 @@ void line_eval(Messenger *m, char *line)
|
|||
temp_id[i] = line[i + prompt_offset];
|
||||
|
||||
unsigned char *bin_string = hex_string_to_bin(temp_id);
|
||||
int num = m_addfriend(m, bin_string, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"));
|
||||
int num = tox_addfriend(m, bin_string, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"));
|
||||
free(bin_string);
|
||||
char numstring[100];
|
||||
|
||||
switch (num) {
|
||||
case FAERR_TOOLONG:
|
||||
case TOX_FAERR_TOOLONG:
|
||||
sprintf(numstring, "[i] Message is too long.");
|
||||
break;
|
||||
|
||||
case FAERR_NOMESSAGE:
|
||||
case TOX_FAERR_NOMESSAGE:
|
||||
sprintf(numstring, "[i] Please add a message to your request.");
|
||||
break;
|
||||
|
||||
case FAERR_OWNKEY:
|
||||
case TOX_FAERR_OWNKEY:
|
||||
sprintf(numstring, "[i] That appears to be your own ID.");
|
||||
break;
|
||||
|
||||
case FAERR_ALREADYSENT:
|
||||
case TOX_FAERR_ALREADYSENT:
|
||||
sprintf(numstring, "[i] Friend request already sent.");
|
||||
break;
|
||||
|
||||
case FAERR_UNKNOWN:
|
||||
case TOX_FAERR_UNKNOWN:
|
||||
sprintf(numstring, "[i] Undefined error when adding friend.");
|
||||
break;
|
||||
|
||||
|
@ -179,7 +179,7 @@ void line_eval(Messenger *m, char *line)
|
|||
new_lines(numstring);
|
||||
do_refresh();
|
||||
} else if (inpt_command == 'd') {
|
||||
doMessenger(m);
|
||||
tox_do(m);
|
||||
} else if (inpt_command == 'm') { //message command: /m friendnumber messsage
|
||||
size_t len = strlen(line);
|
||||
|
||||
|
@ -205,13 +205,13 @@ void line_eval(Messenger *m, char *line)
|
|||
|
||||
int num = atoi(numstring);
|
||||
|
||||
if (m_sendmessage(m, num, (uint8_t *) message, strlen(message) + 1) != 1) {
|
||||
if (tox_sendmessage(m, num, (uint8_t *) message, strlen(message) + 1) != 1) {
|
||||
new_lines("[i] could not send message");
|
||||
} else {
|
||||
new_lines(format_message(m, message, -1));
|
||||
}
|
||||
} else if (inpt_command == 'n') {
|
||||
uint8_t name[MAX_NAME_LENGTH];
|
||||
uint8_t name[TOX_MAX_NAME_LENGTH];
|
||||
int i = 0;
|
||||
size_t len = strlen(line);
|
||||
|
||||
|
@ -222,14 +222,14 @@ void line_eval(Messenger *m, char *line)
|
|||
}
|
||||
|
||||
name[i - 3] = 0;
|
||||
setname(m, name, i - 2);
|
||||
tox_setname(m, name, i - 2);
|
||||
char numstring[100];
|
||||
sprintf(numstring, "[i] changed nick to %s", (char *)name);
|
||||
new_lines(numstring);
|
||||
} else if (inpt_command == 'l') {
|
||||
print_friendlist(m);
|
||||
} else if (inpt_command == 's') {
|
||||
uint8_t status[MAX_STATUSMESSAGE_LENGTH];
|
||||
uint8_t status[TOX_MAX_STATUSMESSAGE_LENGTH];
|
||||
int i = 0;
|
||||
size_t len = strlen(line);
|
||||
|
||||
|
@ -240,7 +240,7 @@ void line_eval(Messenger *m, char *line)
|
|||
}
|
||||
|
||||
status[i - 3] = 0;
|
||||
m_set_statusmessage(m, status, strlen((char *)status) + 1);
|
||||
tox_set_statusmessage(m, status, strlen((char *)status) + 1);
|
||||
char numstring[100];
|
||||
sprintf(numstring, "[i] changed status to %s", (char *)status);
|
||||
new_lines(numstring);
|
||||
|
@ -252,7 +252,7 @@ void line_eval(Messenger *m, char *line)
|
|||
sprintf(numchar, "[i] you either didn't receive that request or you already accepted it");
|
||||
new_lines(numchar);
|
||||
} else {
|
||||
int num = m_addfriend_norequest(m, pending_requests[numf].id);
|
||||
int num = tox_addfriend_norequest(m, pending_requests[numf].id);
|
||||
|
||||
if (num != -1) {
|
||||
pending_requests[numf].accepted = 1;
|
||||
|
@ -364,40 +364,40 @@ void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *us
|
|||
char numchar[100];
|
||||
sprintf(numchar, "[i] accept request with /a %u", num_requests);
|
||||
new_lines(numchar);
|
||||
memcpy(pending_requests[num_requests].id, public_key, CLIENT_ID_SIZE);
|
||||
memcpy(pending_requests[num_requests].id, public_key, TOX_CLIENT_ID_SIZE);
|
||||
pending_requests[num_requests].accepted = 0;
|
||||
++num_requests;
|
||||
do_refresh();
|
||||
}
|
||||
|
||||
void print_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
|
||||
void print_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
|
||||
{
|
||||
new_lines(format_message(m, (char *)string, friendnumber));
|
||||
}
|
||||
|
||||
void print_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
|
||||
void print_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
|
||||
{
|
||||
char name[MAX_NAME_LENGTH];
|
||||
char name[TOX_MAX_NAME_LENGTH];
|
||||
|
||||
if (getname(m, friendnumber, (uint8_t *)name) != -1) {
|
||||
if (tox_getname(m, friendnumber, (uint8_t *)name) != -1) {
|
||||
char msg[100 + length];
|
||||
sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string);
|
||||
new_lines(msg);
|
||||
}
|
||||
}
|
||||
|
||||
void print_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
|
||||
void print_statuschange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
|
||||
{
|
||||
char name[MAX_NAME_LENGTH];
|
||||
char name[TOX_MAX_NAME_LENGTH];
|
||||
|
||||
if (getname(m, friendnumber, (uint8_t *)name) != -1) {
|
||||
if (tox_getname(m, friendnumber, (uint8_t *)name) != -1) {
|
||||
char msg[100 + length + strlen(name) + 1];
|
||||
sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string);
|
||||
new_lines(msg);
|
||||
}
|
||||
}
|
||||
|
||||
void load_key(Messenger *m, char *path)
|
||||
void load_key(Tox *m, char *path)
|
||||
{
|
||||
FILE *data_file = fopen(path, "r");
|
||||
int size = 0;
|
||||
|
@ -415,13 +415,13 @@ void load_key(Messenger *m, char *path)
|
|||
goto FILE_ERROR;
|
||||
}
|
||||
|
||||
Messenger_load(m, data, size);
|
||||
tox_load(m, data, size);
|
||||
|
||||
} else {
|
||||
//else save new keys
|
||||
int size = Messenger_size(m);
|
||||
int size = tox_size(m);
|
||||
uint8_t data[size];
|
||||
Messenger_save(m, data);
|
||||
tox_save(m, data);
|
||||
data_file = fopen(path, "w");
|
||||
|
||||
if (!data_file) {
|
||||
|
@ -463,7 +463,7 @@ int main(int argc, char *argv[])
|
|||
int i = 0;
|
||||
char *filename = "data";
|
||||
char idstring[200] = {0};
|
||||
Messenger *m;
|
||||
Tox *m;
|
||||
|
||||
if (argc < 4) {
|
||||
printf("[!] Usage: %s [IP] [port] [public_key] <keyfile>\n", argv[0]);
|
||||
|
@ -487,7 +487,7 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
}
|
||||
|
||||
m = initMessenger();
|
||||
m = tox_new();
|
||||
|
||||
if ( !m ) {
|
||||
fputs("Failed to allocate Messenger datastructure", stderr);
|
||||
|
@ -496,10 +496,10 @@ int main(int argc, char *argv[])
|
|||
|
||||
load_key(m, filename);
|
||||
|
||||
m_callback_friendrequest(m, print_request, NULL);
|
||||
m_callback_friendmessage(m, print_message, NULL);
|
||||
m_callback_namechange(m, print_nickchange, NULL);
|
||||
m_callback_statusmessage(m, print_statuschange, NULL);
|
||||
tox_callback_friendrequest(m, print_request, NULL);
|
||||
tox_callback_friendmessage(m, print_message, NULL);
|
||||
tox_callback_namechange(m, print_nickchange, NULL);
|
||||
tox_callback_statusmessage(m, print_statuschange, NULL);
|
||||
|
||||
initscr();
|
||||
noecho();
|
||||
|
@ -521,17 +521,17 @@ int main(int argc, char *argv[])
|
|||
exit(1);
|
||||
|
||||
unsigned char *binary_string = hex_string_to_bin(argv[3]);
|
||||
DHT_bootstrap(m->dht, bootstrap_ip_port, binary_string);
|
||||
tox_bootstrap(m, bootstrap_ip_port, binary_string);
|
||||
free(binary_string);
|
||||
nodelay(stdscr, TRUE);
|
||||
|
||||
while (true) {
|
||||
if (on == 0 && DHT_isconnected(m->dht)) {
|
||||
if (on == 0 && tox_isconnected(m)) {
|
||||
new_lines("[i] connected to DHT\n[i] define username with /n");
|
||||
on = 1;
|
||||
}
|
||||
|
||||
doMessenger(m);
|
||||
tox_do(m);
|
||||
c_sleep(1);
|
||||
do_refresh();
|
||||
|
||||
|
@ -552,7 +552,7 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
}
|
||||
|
||||
cleanupMessenger(m);
|
||||
tox_kill(m);
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -35,15 +35,55 @@
|
|||
#include <arpa/inet.h>
|
||||
#include <sys/types.h>
|
||||
#include <netdb.h>
|
||||
#include "../core/Messenger.h"
|
||||
#include "../core/network.h"
|
||||
#include "../core/tox.h"
|
||||
|
||||
#define STRING_LENGTH 256
|
||||
#define HISTORY 50
|
||||
#define PUB_KEY_BYTES 32
|
||||
|
||||
/*
|
||||
resolve_addr():
|
||||
address should represent IPv4 or a hostname with A record
|
||||
|
||||
returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
|
||||
returns 0 on failure
|
||||
|
||||
TODO: Fix ipv6 support
|
||||
*/
|
||||
|
||||
uint32_t resolve_addr(const char *address)
|
||||
{
|
||||
struct addrinfo *server = NULL;
|
||||
struct addrinfo hints;
|
||||
int rc;
|
||||
uint32_t addr;
|
||||
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_INET; // IPv4 only right now.
|
||||
hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses.
|
||||
|
||||
rc = getaddrinfo(address, "echo", &hints, &server);
|
||||
|
||||
// Lookup failed.
|
||||
if (rc != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// IPv4 records only..
|
||||
if (server->ai_family != AF_INET) {
|
||||
freeaddrinfo(server);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
addr = ((struct sockaddr_in *)server->ai_addr)->sin_addr.s_addr;
|
||||
|
||||
freeaddrinfo(server);
|
||||
return addr;
|
||||
}
|
||||
|
||||
void new_lines(char *line);
|
||||
void line_eval(Messenger *m, char *line);
|
||||
void line_eval(Tox *m, char *line);
|
||||
void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) ;
|
||||
int count_lines(char *string) ;
|
||||
char *appender(char *str, const char c);
|
||||
|
|
Loading…
Reference in New Issue
Block a user