mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Merge remote-tracking branch 'upstream/master' into timer
Conflicts: core/Messenger.c
This commit is contained in:
commit
be840b46b9
|
@ -44,6 +44,8 @@
|
|||
#define c_sleep(x) usleep(1000*x)
|
||||
#endif
|
||||
|
||||
static Messenger *m;
|
||||
|
||||
uint8_t *parent_id = NULL;
|
||||
uint8_t *child_id = NULL;
|
||||
|
||||
|
@ -60,16 +62,16 @@ void do_tox(void)
|
|||
dht_on = 0;
|
||||
}
|
||||
|
||||
doMessenger();
|
||||
doMessenger(m);
|
||||
}
|
||||
|
||||
void parent_confirm_message(int num, uint8_t *data, uint16_t length)
|
||||
void parent_confirm_message(Messenger *m, int num, uint8_t *data, uint16_t length, void* userdata)
|
||||
{
|
||||
puts("OK");
|
||||
request_flags |= SECOND_FLAG;
|
||||
}
|
||||
|
||||
void parent_confirm_status(int num, uint8_t *data, uint16_t length)
|
||||
void parent_confirm_status(Messenger *m, int num, uint8_t *data, uint16_t length, void* userdata)
|
||||
{
|
||||
puts("OK");
|
||||
request_flags |= FIRST_FLAG;
|
||||
|
@ -84,7 +86,7 @@ int parent_friend_request(void)
|
|||
fputs("Sending child request.", stdout);
|
||||
fflush(stdout);
|
||||
|
||||
m_addfriend(child_id, (uint8_t *)message, len);
|
||||
m_addfriend(m, child_id, (uint8_t *)message, len);
|
||||
|
||||
/* wait on the status change */
|
||||
for(i = 0; i < WAIT_COUNT; i++) {
|
||||
|
@ -106,15 +108,15 @@ int parent_friend_request(void)
|
|||
return 0;
|
||||
}
|
||||
|
||||
void child_got_request(uint8_t *public_key, uint8_t *data, uint16_t length)
|
||||
void child_got_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata)
|
||||
{
|
||||
fputs("OK\nsending status to parent", stdout);
|
||||
fflush(stdout);
|
||||
m_addfriend_norequest(public_key);
|
||||
m_addfriend_norequest(m, public_key);
|
||||
request_flags |= FIRST_FLAG;
|
||||
}
|
||||
|
||||
void child_got_statuschange(int friend_num, uint8_t *string, uint16_t length)
|
||||
void child_got_statuschange(Messenger *m, int friend_num, uint8_t *string, uint16_t length, void* userdata)
|
||||
{
|
||||
request_flags |= SECOND_FLAG;
|
||||
}
|
||||
|
@ -168,12 +170,13 @@ int main(int argc, char *argv[])
|
|||
int i = 0;
|
||||
char *message = "Y-yes Mr. Watson?";
|
||||
|
||||
initMessenger();
|
||||
Messenger_save(child_id);
|
||||
m = initMessenger();
|
||||
|
||||
Messenger_save(m, child_id);
|
||||
msync(child_id, crypto_box_PUBLICKEYBYTES, MS_SYNC);
|
||||
|
||||
m_callback_friendrequest(child_got_request);
|
||||
m_callback_statusmessage(child_got_statuschange);
|
||||
m_callback_friendrequest(m, child_got_request, NULL);
|
||||
m_callback_statusmessage(m, child_got_statuschange, NULL);
|
||||
|
||||
/* wait on the friend request */
|
||||
while(!(request_flags & FIRST_FLAG))
|
||||
|
@ -185,10 +188,12 @@ int main(int argc, char *argv[])
|
|||
|
||||
for(i = 0; i < 6; i++) {
|
||||
/* send the message six times, just to be sure */
|
||||
m_sendmessage(0, (uint8_t *)message, strlen(message));
|
||||
m_sendmessage(m, 0, (uint8_t *)message, strlen(message));
|
||||
do_tox();
|
||||
}
|
||||
|
||||
cleanupMessenger(m);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -199,15 +204,16 @@ int main(int argc, char *argv[])
|
|||
return -1;
|
||||
}
|
||||
|
||||
m = initMessenger();
|
||||
|
||||
msync(parent_id, crypto_box_PUBLICKEYBYTES, MS_SYNC);
|
||||
m_callback_statusmessage(parent_confirm_status);
|
||||
m_callback_friendmessage(parent_confirm_message);
|
||||
m_callback_statusmessage(m, parent_confirm_status, NULL);
|
||||
m_callback_friendmessage(m, parent_confirm_message, NULL);
|
||||
|
||||
/* hacky way to give the child time to set up */
|
||||
c_sleep(50);
|
||||
|
||||
initMessenger();
|
||||
Messenger_save(parent_id);
|
||||
Messenger_save(m, parent_id);
|
||||
|
||||
if(parent_friend_request() == -1)
|
||||
return -1;
|
||||
|
|
|
@ -34,6 +34,8 @@ unsigned char *bad_id = NULL;
|
|||
|
||||
int friend_id_num = 0;
|
||||
|
||||
Messenger *m;
|
||||
|
||||
unsigned char * hex_string_to_bin(char hex_string[])
|
||||
{
|
||||
size_t len = strlen(hex_string);
|
||||
|
@ -52,22 +54,22 @@ START_TEST(test_m_sendmesage)
|
|||
int bad_len = MAX_DATA_SIZE;
|
||||
|
||||
|
||||
ck_assert(m_sendmessage(-1, (uint8_t *)message, good_len) == 0);
|
||||
ck_assert(m_sendmessage(REALLY_BIG_NUMBER, (uint8_t *)message, good_len) == 0);
|
||||
ck_assert(m_sendmessage(17, (uint8_t *)message, good_len) == 0);
|
||||
ck_assert(m_sendmessage(friend_id_num, (uint8_t *)message, bad_len) == 0);
|
||||
ck_assert(m_sendmessage(m, -1, (uint8_t *)message, good_len) == 0);
|
||||
ck_assert(m_sendmessage(m, REALLY_BIG_NUMBER, (uint8_t *)message, good_len) == 0);
|
||||
ck_assert(m_sendmessage(m, 17, (uint8_t *)message, good_len) == 0);
|
||||
ck_assert(m_sendmessage(m, friend_id_num, (uint8_t *)message, bad_len) == 0);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(test_m_get_userstatus_size)
|
||||
{
|
||||
int rc = 0;
|
||||
ck_assert_msg((m_get_statusmessage_size(-1) == -1),
|
||||
ck_assert_msg((m_get_statusmessage_size(m, -1) == -1),
|
||||
"m_get_statusmessage_size did NOT catch an argument of -1");
|
||||
ck_assert_msg((m_get_statusmessage_size(REALLY_BIG_NUMBER) == -1),
|
||||
ck_assert_msg((m_get_statusmessage_size(m, REALLY_BIG_NUMBER) == -1),
|
||||
"m_get_statusmessage_size did NOT catch the following argument: %d\n",
|
||||
REALLY_BIG_NUMBER);
|
||||
rc = m_get_statusmessage_size(friend_id_num);
|
||||
rc = m_get_statusmessage_size(m, friend_id_num);
|
||||
|
||||
/* this WILL error if the original m_addfriend_norequest() failed */
|
||||
ck_assert_msg((rc > 0 && rc <= MAX_STATUSMESSAGE_LENGTH),
|
||||
|
@ -83,11 +85,11 @@ START_TEST(test_m_set_userstatus)
|
|||
uint16_t good_length = strlen(status);
|
||||
uint16_t bad_length = REALLY_BIG_NUMBER;
|
||||
|
||||
if(m_set_statusmessage((uint8_t *)status, bad_length) != -1)
|
||||
if(m_set_statusmessage(m, (uint8_t *)status, bad_length) != -1)
|
||||
ck_abort_msg("m_set_userstatus did NOT catch the following length: %d\n",
|
||||
REALLY_BIG_NUMBER);
|
||||
|
||||
if((m_set_statusmessage((uint8_t *)status, good_length)) != 0)
|
||||
if((m_set_statusmessage(m, (uint8_t *)status, good_length)) != 0)
|
||||
ck_abort_msg("m_set_userstatus did NOT return 0 on the following length: %d\n"
|
||||
"MAX_STATUSMESSAGE_LENGTH: %d\n", good_length, MAX_STATUSMESSAGE_LENGTH);
|
||||
}
|
||||
|
@ -95,9 +97,9 @@ END_TEST
|
|||
|
||||
START_TEST(test_m_friendstatus)
|
||||
{
|
||||
ck_assert_msg((m_friendstatus(-1) == NOFRIEND),
|
||||
ck_assert_msg((m_friendstatus(m, -1) == NOFRIEND),
|
||||
"m_friendstatus did NOT catch an argument of -1.\n");
|
||||
ck_assert_msg((m_friendstatus(REALLY_BIG_NUMBER) == NOFRIEND),
|
||||
ck_assert_msg((m_friendstatus(m, REALLY_BIG_NUMBER) == NOFRIEND),
|
||||
"m_friendstatus did NOT catch an argument of %d.\n",
|
||||
REALLY_BIG_NUMBER);
|
||||
}
|
||||
|
@ -105,9 +107,9 @@ END_TEST
|
|||
|
||||
START_TEST(test_m_delfriend)
|
||||
{
|
||||
ck_assert_msg((m_delfriend(-1) == -1),
|
||||
ck_assert_msg((m_delfriend(m, -1) == -1),
|
||||
"m_delfriend did NOT catch an argument of -1\n");
|
||||
ck_assert_msg((m_delfriend(REALLY_BIG_NUMBER) == -1),
|
||||
ck_assert_msg((m_delfriend(m, REALLY_BIG_NUMBER) == -1),
|
||||
"m_delfriend did NOT catch the following number: %d\n",
|
||||
REALLY_BIG_NUMBER);
|
||||
}
|
||||
|
@ -124,16 +126,16 @@ START_TEST(test_m_addfriend)
|
|||
- crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES
|
||||
+ crypto_box_ZEROBYTES + 100);
|
||||
|
||||
if(m_addfriend((uint8_t *)friend_id, (uint8_t *)good_data, really_bad_len) != FAERR_TOOLONG)
|
||||
if(m_addfriend(m, (uint8_t *)friend_id, (uint8_t *)good_data, really_bad_len) != FAERR_TOOLONG)
|
||||
ck_abort_msg("m_addfriend did NOT catch the following length: %d\n", really_bad_len);
|
||||
|
||||
/* this will error if the original m_addfriend_norequest() failed */
|
||||
if(m_addfriend((uint8_t *)friend_id, (uint8_t *)good_data, good_len) != FAERR_ALREADYSENT)
|
||||
if(m_addfriend(m, (uint8_t *)friend_id, (uint8_t *)good_data, good_len) != FAERR_ALREADYSENT)
|
||||
ck_abort_msg("m_addfriend did NOT catch adding a friend we already have.\n"
|
||||
"(this can be caused by the error of m_addfriend_norequest in"
|
||||
" the beginning of the suite)\n");
|
||||
|
||||
if(m_addfriend((uint8_t *)good_id_b, (uint8_t *)bad_data, bad_len) != FAERR_NOMESSAGE)
|
||||
if(m_addfriend(m, (uint8_t *)good_id_b, (uint8_t *)bad_data, bad_len) != FAERR_NOMESSAGE)
|
||||
ck_abort_msg("m_addfriend did NOT catch the following length: %d\n", bad_len);
|
||||
|
||||
/* this should REALLY error */
|
||||
|
@ -152,10 +154,10 @@ START_TEST(test_setname)
|
|||
int good_length = strlen(good_name);
|
||||
int bad_length = REALLY_BIG_NUMBER;
|
||||
|
||||
if(setname((uint8_t *)good_name, bad_length) != -1)
|
||||
if(setname(m, (uint8_t *)good_name, bad_length) != -1)
|
||||
ck_abort_msg("setname() did NOT error on %d as a length argument!\n",
|
||||
bad_length);
|
||||
if(setname((uint8_t *)good_name, good_length) != 0)
|
||||
if(setname(m, (uint8_t *)good_name, good_length) != 0)
|
||||
ck_abort_msg("setname() did NOT return 0 on good arguments!\n");
|
||||
}
|
||||
END_TEST
|
||||
|
@ -166,8 +168,8 @@ START_TEST(test_getself_name)
|
|||
int len = strlen(nickname);
|
||||
char nick_check[len];
|
||||
|
||||
setname((uint8_t *)nickname, len);
|
||||
getself_name((uint8_t *)nick_check);
|
||||
setname(m, (uint8_t *)nickname, len);
|
||||
getself_name(m, (uint8_t *)nick_check);
|
||||
|
||||
ck_assert_msg((!STRINGS_EQUAL(nickname, nick_check)),
|
||||
"getself_name failed to return the known name!\n"
|
||||
|
@ -256,13 +258,15 @@ int main(int argc, char *argv[])
|
|||
good_id_b = hex_string_to_bin(good_id_b_str);
|
||||
bad_id = hex_string_to_bin(bad_id_str);
|
||||
|
||||
m = initMessenger();
|
||||
|
||||
/* setup a default friend and friendnum */
|
||||
if(m_addfriend_norequest((uint8_t *)friend_id) < 0)
|
||||
if(m_addfriend_norequest(m, (uint8_t *)friend_id) < 0)
|
||||
fputs("m_addfriend_norequest() failed on a valid ID!\n"
|
||||
"this was CRITICAL to the test, and the build WILL fail.\n"
|
||||
"the tests will continue now...\n\n", stderr);
|
||||
|
||||
if((friend_id_num = getfriend_id((uint8_t *)friend_id)) < 0)
|
||||
if((friend_id_num = getfriend_id(m, (uint8_t *)friend_id)) < 0)
|
||||
fputs("getfriend_id() failed on a valid ID!\n"
|
||||
"this was CRITICAL to the test, and the build WILL fail.\n"
|
||||
"the tests will continue now...\n\n", stderr);
|
||||
|
@ -276,5 +280,7 @@ int main(int argc, char *argv[])
|
|||
free(good_id_b);
|
||||
free(bad_id);
|
||||
|
||||
cleanupMessenger(m);
|
||||
|
||||
return number_failed;
|
||||
}
|
||||
|
|
544
core/Messenger.c
544
core/Messenger.c
|
@ -26,40 +26,8 @@
|
|||
|
||||
#define MIN(a,b) (((a)<(b))?(a):(b))
|
||||
|
||||
typedef struct {
|
||||
uint8_t client_id[CLIENT_ID_SIZE];
|
||||
int crypt_connection_id;
|
||||
uint64_t friend_request_id; /* id of the friend request corresponding to the current friend request to the current friend. */
|
||||
uint8_t status; /* 0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. */
|
||||
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 *statusmessage;
|
||||
uint16_t statusmessage_length;
|
||||
uint8_t statusmessage_sent;
|
||||
USERSTATUS userstatus;
|
||||
uint8_t userstatus_sent;
|
||||
uint16_t info_size; /* length of the info */
|
||||
uint32_t message_id; /* a semi-unique id used in read receipts */
|
||||
uint8_t receives_read_receipts; /* shall we send read receipts to this person? */
|
||||
} Friend;
|
||||
|
||||
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_statusmessage[MAX_STATUSMESSAGE_LENGTH];
|
||||
static uint16_t self_statusmessage_length;
|
||||
|
||||
static USERSTATUS self_userstatus;
|
||||
|
||||
static Friend *friendlist;
|
||||
static uint32_t numfriends;
|
||||
|
||||
|
||||
static void set_friend_status(int friendnumber, uint8_t status);
|
||||
static int write_cryptpacket_id(int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length);
|
||||
static void set_friend_status(Messenger *m, int friendnumber, uint8_t status);
|
||||
static int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length);
|
||||
|
||||
/* 1 if we are online
|
||||
0 if we are offline
|
||||
|
@ -67,24 +35,24 @@ static int write_cryptpacket_id(int friendnumber, uint8_t packet_id, uint8_t *da
|
|||
|
||||
/* set the size of the friend list to numfriends
|
||||
return -1 if realloc fails */
|
||||
int realloc_friendlist(uint32_t num) {
|
||||
Friend *newfriendlist = realloc(friendlist, num*sizeof(Friend));
|
||||
int realloc_friendlist(Messenger *m, uint32_t num) {
|
||||
Friend *newfriendlist = realloc(m->friendlist, num*sizeof(Friend));
|
||||
if (newfriendlist == NULL)
|
||||
return -1;
|
||||
memset(&newfriendlist[num-1], 0, sizeof(Friend));
|
||||
friendlist = newfriendlist;
|
||||
m->friendlist = newfriendlist;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* return the friend id associated to that public key.
|
||||
return -1 if no such friend */
|
||||
int getfriend_id(uint8_t *client_id)
|
||||
int getfriend_id(Messenger *m, uint8_t *client_id)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < numfriends; ++i) {
|
||||
if (friendlist[i].status > 0)
|
||||
if (memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0)
|
||||
for (i = 0; i < m->numfriends; ++i) {
|
||||
if (m->friendlist[i].status > 0)
|
||||
if (memcmp(client_id, m->friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0)
|
||||
return i;
|
||||
}
|
||||
|
||||
|
@ -95,13 +63,13 @@ int getfriend_id(uint8_t *client_id)
|
|||
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)
|
||||
int getclient_id(Messenger *m, int friend_id, uint8_t *client_id)
|
||||
{
|
||||
if (friend_id >= numfriends || friend_id < 0)
|
||||
if (friend_id >= m->numfriends || friend_id < 0)
|
||||
return -1;
|
||||
|
||||
if (friendlist[friend_id].status > 0) {
|
||||
memcpy(client_id, friendlist[friend_id].client_id, CLIENT_ID_SIZE);
|
||||
if (m->friendlist[friend_id].status > 0) {
|
||||
memcpy(client_id, m->friendlist[friend_id].client_id, CLIENT_ID_SIZE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -120,7 +88,7 @@ int getclient_id(int friend_id, uint8_t *client_id)
|
|||
* return FAERR_ALREADYSENT if friend request already sent or already a friend
|
||||
* return FAERR_UNKNOWN for unknown error
|
||||
*/
|
||||
int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
|
||||
int m_addfriend(Messenger *m, uint8_t *client_id, uint8_t *data, uint16_t length)
|
||||
{
|
||||
if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES
|
||||
- crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES
|
||||
|
@ -130,57 +98,57 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
|
|||
return FAERR_NOMESSAGE;
|
||||
if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)
|
||||
return FAERR_OWNKEY;
|
||||
if (getfriend_id(client_id) != -1)
|
||||
if (getfriend_id(m, client_id) != -1)
|
||||
return FAERR_ALREADYSENT;
|
||||
|
||||
/* resize the friend list if necessary */
|
||||
realloc_friendlist(numfriends + 1);
|
||||
realloc_friendlist(m, m->numfriends + 1);
|
||||
|
||||
uint32_t i;
|
||||
for (i = 0; i <= numfriends; ++i) {
|
||||
if (friendlist[i].status == NOFRIEND) {
|
||||
for (i = 0; i <= m->numfriends; ++i) {
|
||||
if (m->friendlist[i].status == NOFRIEND) {
|
||||
DHT_addfriend(client_id);
|
||||
friendlist[i].status = FRIEND_ADDED;
|
||||
friendlist[i].crypt_connection_id = -1;
|
||||
friendlist[i].friend_request_id = -1;
|
||||
memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
|
||||
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;
|
||||
friendlist[i].message_id = 0;
|
||||
friendlist[i].receives_read_receipts = 1; /* default: YES */
|
||||
m->friendlist[i].status = FRIEND_ADDED;
|
||||
m->friendlist[i].crypt_connection_id = -1;
|
||||
m->friendlist[i].friend_request_id = -1;
|
||||
memcpy(m->friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
|
||||
m->friendlist[i].statusmessage = calloc(1, 1);
|
||||
m->friendlist[i].statusmessage_length = 1;
|
||||
m->friendlist[i].userstatus = USERSTATUS_NONE;
|
||||
memcpy(m->friendlist[i].info, data, length);
|
||||
m->friendlist[i].info_size = length;
|
||||
m->friendlist[i].message_id = 0;
|
||||
m->friendlist[i].receives_read_receipts = 1; /* default: YES */
|
||||
|
||||
++numfriends;
|
||||
++ m->numfriends;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return FAERR_UNKNOWN;
|
||||
}
|
||||
|
||||
int m_addfriend_norequest(uint8_t * client_id)
|
||||
int m_addfriend_norequest(Messenger *m, uint8_t * client_id)
|
||||
{
|
||||
if (getfriend_id(client_id) != -1)
|
||||
if (getfriend_id(m, client_id) != -1)
|
||||
return -1;
|
||||
|
||||
/* resize the friend list if necessary */
|
||||
realloc_friendlist(numfriends + 1);
|
||||
realloc_friendlist(m, m->numfriends + 1);
|
||||
|
||||
uint32_t i;
|
||||
for (i = 0; i <= numfriends; ++i) {
|
||||
if(friendlist[i].status == NOFRIEND) {
|
||||
for (i = 0; i <= m->numfriends; ++i) {
|
||||
if(m->friendlist[i].status == NOFRIEND) {
|
||||
DHT_addfriend(client_id);
|
||||
friendlist[i].status = FRIEND_REQUESTED;
|
||||
friendlist[i].crypt_connection_id = -1;
|
||||
friendlist[i].friend_request_id = -1;
|
||||
memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
|
||||
friendlist[i].statusmessage = calloc(1, 1);
|
||||
friendlist[i].statusmessage_length = 1;
|
||||
friendlist[i].userstatus = USERSTATUS_NONE;
|
||||
friendlist[i].message_id = 0;
|
||||
friendlist[i].receives_read_receipts = 1; /* default: YES */
|
||||
++numfriends;
|
||||
m->friendlist[i].status = FRIEND_REQUESTED;
|
||||
m->friendlist[i].crypt_connection_id = -1;
|
||||
m->friendlist[i].friend_request_id = -1;
|
||||
memcpy(m->friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
|
||||
m->friendlist[i].statusmessage = calloc(1, 1);
|
||||
m->friendlist[i].statusmessage_length = 1;
|
||||
m->friendlist[i].userstatus = USERSTATUS_NONE;
|
||||
m->friendlist[i].message_id = 0;
|
||||
m->friendlist[i].receives_read_receipts = 1; /* default: YES */
|
||||
++ m->numfriends;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
@ -190,23 +158,23 @@ int m_addfriend_norequest(uint8_t * client_id)
|
|||
/* remove a friend
|
||||
return 0 if success
|
||||
return -1 if failure */
|
||||
int m_delfriend(int friendnumber)
|
||||
int m_delfriend(Messenger *m, int friendnumber)
|
||||
{
|
||||
if (friendnumber >= numfriends || friendnumber < 0)
|
||||
if (friendnumber >= m->numfriends || friendnumber < 0)
|
||||
return -1;
|
||||
|
||||
DHT_delfriend(friendlist[friendnumber].client_id);
|
||||
crypto_kill(friendlist[friendnumber].crypt_connection_id);
|
||||
free(friendlist[friendnumber].statusmessage);
|
||||
memset(&friendlist[friendnumber], 0, sizeof(Friend));
|
||||
DHT_delfriend(m->friendlist[friendnumber].client_id);
|
||||
crypto_kill(m->friendlist[friendnumber].crypt_connection_id);
|
||||
free(m->friendlist[friendnumber].statusmessage);
|
||||
memset(&(m->friendlist[friendnumber]), 0, sizeof(Friend));
|
||||
uint32_t i;
|
||||
|
||||
for (i = numfriends; i != 0; --i) {
|
||||
if (friendlist[i-1].status != NOFRIEND)
|
||||
for (i = m->numfriends; i != 0; --i) {
|
||||
if (m->friendlist[i-1].status != NOFRIEND)
|
||||
break;
|
||||
}
|
||||
numfriends = i;
|
||||
realloc_friendlist(numfriends + 1);
|
||||
m->numfriends = i;
|
||||
realloc_friendlist(m, m->numfriends + 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -216,31 +184,31 @@ int m_delfriend(int friendnumber)
|
|||
return FRIEND_REQUESTED if the friend request was sent
|
||||
return FRIEND_ADDED if the friend was added
|
||||
return NOFRIEND if there is no friend with that number */
|
||||
int m_friendstatus(int friendnumber)
|
||||
int m_friendstatus(Messenger *m, int friendnumber)
|
||||
{
|
||||
if (friendnumber < 0 || friendnumber >= numfriends)
|
||||
if (friendnumber < 0 || friendnumber >= m->numfriends)
|
||||
return NOFRIEND;
|
||||
return friendlist[friendnumber].status;
|
||||
return m->friendlist[friendnumber].status;
|
||||
}
|
||||
|
||||
/* send a text chat message to an online friend
|
||||
return the message id if packet was successfully put into the send queue
|
||||
return 0 if it was not */
|
||||
uint32_t m_sendmessage(int friendnumber, uint8_t *message, uint32_t length)
|
||||
uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length)
|
||||
{
|
||||
if (friendnumber < 0 || friendnumber >= numfriends)
|
||||
if (friendnumber < 0 || friendnumber >= m->numfriends)
|
||||
return 0;
|
||||
uint32_t msgid = ++friendlist[friendnumber].message_id;
|
||||
uint32_t msgid = ++m->friendlist[friendnumber].message_id;
|
||||
if (msgid == 0)
|
||||
msgid = 1; /* otherwise, false error */
|
||||
if(m_sendmessage_withid(friendnumber, msgid, message, length)) {
|
||||
if(m_sendmessage_withid(m, friendnumber, msgid, message, length)) {
|
||||
return msgid;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t m_sendmessage_withid(int friendnumber, uint32_t theid, uint8_t *message, uint32_t length)
|
||||
uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length)
|
||||
{
|
||||
if (length >= (MAX_DATA_SIZE - sizeof(theid)))
|
||||
return 0;
|
||||
|
@ -248,34 +216,34 @@ uint32_t m_sendmessage_withid(int friendnumber, uint32_t theid, uint8_t *message
|
|||
theid = htonl(theid);
|
||||
memcpy(temp, &theid, sizeof(theid));
|
||||
memcpy(temp + sizeof(theid), message, length);
|
||||
return write_cryptpacket_id(friendnumber, PACKET_ID_MESSAGE, temp, length + sizeof(theid));
|
||||
return write_cryptpacket_id(m, friendnumber, PACKET_ID_MESSAGE, temp, length + sizeof(theid));
|
||||
}
|
||||
|
||||
/* send an action to an online friend
|
||||
return 1 if packet was successfully put into the send queue
|
||||
return 0 if it was not */
|
||||
int m_sendaction(int friendnumber, uint8_t *action, uint32_t length)
|
||||
int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length)
|
||||
{
|
||||
return write_cryptpacket_id(friendnumber, PACKET_ID_ACTION, action, length);
|
||||
return write_cryptpacket_id(m, friendnumber, PACKET_ID_ACTION, action, length);
|
||||
}
|
||||
|
||||
/* send a name packet to friendnumber
|
||||
length is the length with the NULL terminator*/
|
||||
static int m_sendname(int friendnumber, uint8_t * name, uint16_t length)
|
||||
static int m_sendname(Messenger *m, int friendnumber, uint8_t * name, uint16_t length)
|
||||
{
|
||||
if(length > MAX_NAME_LENGTH || length == 0)
|
||||
return 0;
|
||||
return write_cryptpacket_id(friendnumber, PACKET_ID_NICKNAME, name, length);
|
||||
return write_cryptpacket_id(m, friendnumber, PACKET_ID_NICKNAME, name, length);
|
||||
}
|
||||
|
||||
/* set the name of a friend
|
||||
return 0 if success
|
||||
return -1 if failure */
|
||||
static int setfriendname(int friendnumber, uint8_t * name)
|
||||
static int setfriendname(Messenger *m, int friendnumber, uint8_t * name)
|
||||
{
|
||||
if (friendnumber >= numfriends || friendnumber < 0)
|
||||
if (friendnumber >= m->numfriends || friendnumber < 0)
|
||||
return -1;
|
||||
memcpy(friendlist[friendnumber].name, name, MAX_NAME_LENGTH);
|
||||
memcpy(m->friendlist[friendnumber].name, name, MAX_NAME_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -285,15 +253,15 @@ static int setfriendname(int friendnumber, uint8_t * name)
|
|||
length is the length of name with the NULL terminator
|
||||
return 0 if success
|
||||
return -1 if failure */
|
||||
int setname(uint8_t * name, uint16_t length)
|
||||
int setname(Messenger *m, uint8_t * name, uint16_t length)
|
||||
{
|
||||
if (length > MAX_NAME_LENGTH || length == 0)
|
||||
return -1;
|
||||
memcpy(self_name, name, length);
|
||||
self_name_length = length;
|
||||
memcpy(m->name, name, length);
|
||||
m->name_length = length;
|
||||
uint32_t i;
|
||||
for (i = 0; i < numfriends; ++i)
|
||||
friendlist[i].name_sent = 0;
|
||||
for (i = 0; i < m->numfriends; ++i)
|
||||
m->friendlist[i].name_sent = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -301,10 +269,10 @@ int setname(uint8_t * name, uint16_t length)
|
|||
put it in name
|
||||
name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
|
||||
return the length of the name */
|
||||
uint16_t getself_name(uint8_t *name)
|
||||
uint16_t getself_name(Messenger *m, uint8_t *name)
|
||||
{
|
||||
memcpy(name, self_name, self_name_length);
|
||||
return self_name_length;
|
||||
memcpy(name, m->name, m->name_length);
|
||||
return m->name_length;
|
||||
}
|
||||
|
||||
/* get name of friendnumber
|
||||
|
@ -312,272 +280,298 @@ uint16_t getself_name(uint8_t *name)
|
|||
name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
|
||||
return 0 if success
|
||||
return -1 if failure */
|
||||
int getname(int friendnumber, uint8_t * name)
|
||||
int getname(Messenger *m, int friendnumber, uint8_t * name)
|
||||
{
|
||||
if (friendnumber >= numfriends || friendnumber < 0)
|
||||
if (friendnumber >= m->numfriends || friendnumber < 0)
|
||||
return -1;
|
||||
memcpy(name, friendlist[friendnumber].name, MAX_NAME_LENGTH);
|
||||
memcpy(name, m->friendlist[friendnumber].name, MAX_NAME_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int m_set_statusmessage(uint8_t *status, uint16_t length)
|
||||
int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length)
|
||||
{
|
||||
if (length > MAX_STATUSMESSAGE_LENGTH)
|
||||
return -1;
|
||||
memcpy(self_statusmessage, status, length);
|
||||
self_statusmessage_length = length;
|
||||
memcpy(m->statusmessage, status, length);
|
||||
m->statusmessage_length = length;
|
||||
|
||||
uint32_t i;
|
||||
for (i = 0; i < numfriends; ++i)
|
||||
friendlist[i].statusmessage_sent = 0;
|
||||
for (i = 0; i < m->numfriends; ++i)
|
||||
m->friendlist[i].statusmessage_sent = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int m_set_userstatus(USERSTATUS status)
|
||||
int m_set_userstatus(Messenger *m, USERSTATUS status)
|
||||
{
|
||||
if (status >= USERSTATUS_INVALID) {
|
||||
return -1;
|
||||
}
|
||||
self_userstatus = status;
|
||||
m->userstatus = status;
|
||||
uint32_t i;
|
||||
for (i = 0; i < numfriends; ++i)
|
||||
friendlist[i].userstatus_sent = 0;
|
||||
for (i = 0; i < m->numfriends; ++i)
|
||||
m->friendlist[i].userstatus_sent = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* return the size of friendnumber's user status
|
||||
guaranteed to be at most MAX_STATUSMESSAGE_LENGTH */
|
||||
int m_get_statusmessage_size(int friendnumber)
|
||||
int m_get_statusmessage_size(Messenger *m, int friendnumber)
|
||||
{
|
||||
if (friendnumber >= numfriends || friendnumber < 0)
|
||||
if (friendnumber >= m->numfriends || friendnumber < 0)
|
||||
return -1;
|
||||
return friendlist[friendnumber].statusmessage_length;
|
||||
return m->friendlist[friendnumber].statusmessage_length;
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
int m_copy_statusmessage(int friendnumber, uint8_t * buf, uint32_t maxlen)
|
||||
int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t * buf, uint32_t maxlen)
|
||||
{
|
||||
if (friendnumber >= numfriends || friendnumber < 0)
|
||||
if (friendnumber >= m->numfriends || friendnumber < 0)
|
||||
return -1;
|
||||
memset(buf, 0, maxlen);
|
||||
memcpy(buf, friendlist[friendnumber].statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1);
|
||||
memcpy(buf, m->friendlist[friendnumber].statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int m_copy_self_statusmessage(uint8_t * buf, uint32_t maxlen)
|
||||
int m_copy_self_statusmessage(Messenger *m, uint8_t * buf, uint32_t maxlen)
|
||||
{
|
||||
memset(buf, 0, maxlen);
|
||||
memcpy(buf, self_statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1);
|
||||
memcpy(buf, m->statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
USERSTATUS m_get_userstatus(int friendnumber)
|
||||
USERSTATUS m_get_userstatus(Messenger *m, int friendnumber)
|
||||
{
|
||||
if (friendnumber >= numfriends || friendnumber < 0)
|
||||
if (friendnumber >= m->numfriends || friendnumber < 0)
|
||||
return USERSTATUS_INVALID;
|
||||
USERSTATUS status = friendlist[friendnumber].userstatus;
|
||||
USERSTATUS status = m->friendlist[friendnumber].userstatus;
|
||||
if (status >= USERSTATUS_INVALID) {
|
||||
status = USERSTATUS_NONE;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
USERSTATUS m_get_self_userstatus(void)
|
||||
USERSTATUS m_get_self_userstatus(Messenger *m)
|
||||
{
|
||||
return self_userstatus;
|
||||
return m->userstatus;
|
||||
}
|
||||
|
||||
static int send_statusmessage(int friendnumber, uint8_t * status, uint16_t length)
|
||||
static int send_statusmessage(Messenger *m, int friendnumber, uint8_t * status, uint16_t length)
|
||||
{
|
||||
return write_cryptpacket_id(friendnumber, PACKET_ID_STATUSMESSAGE, status, length);
|
||||
return write_cryptpacket_id(m, friendnumber, PACKET_ID_STATUSMESSAGE, status, length);
|
||||
}
|
||||
|
||||
static int send_userstatus(int friendnumber, USERSTATUS status)
|
||||
static int send_userstatus(Messenger *m, int friendnumber, USERSTATUS status)
|
||||
{
|
||||
uint8_t stat = status;
|
||||
return write_cryptpacket_id(friendnumber, PACKET_ID_USERSTATUS, &stat, sizeof(stat));
|
||||
return write_cryptpacket_id(m, friendnumber, PACKET_ID_USERSTATUS, &stat, sizeof(stat));
|
||||
}
|
||||
|
||||
static int set_friend_statusmessage(int friendnumber, uint8_t * status, uint16_t length)
|
||||
static int set_friend_statusmessage(Messenger *m, int friendnumber, uint8_t * status, uint16_t length)
|
||||
{
|
||||
if (friendnumber >= numfriends || friendnumber < 0)
|
||||
if (friendnumber >= m->numfriends || friendnumber < 0)
|
||||
return -1;
|
||||
uint8_t *newstatus = calloc(length, 1);
|
||||
memcpy(newstatus, status, length);
|
||||
free(friendlist[friendnumber].statusmessage);
|
||||
friendlist[friendnumber].statusmessage = newstatus;
|
||||
friendlist[friendnumber].statusmessage_length = length;
|
||||
free(m->friendlist[friendnumber].statusmessage);
|
||||
m->friendlist[friendnumber].statusmessage = newstatus;
|
||||
m->friendlist[friendnumber].statusmessage_length = length;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void set_friend_userstatus(int friendnumber, USERSTATUS status)
|
||||
static void set_friend_userstatus(Messenger *m, int friendnumber, USERSTATUS status)
|
||||
{
|
||||
friendlist[friendnumber].userstatus = status;
|
||||
m->friendlist[friendnumber].userstatus = status;
|
||||
}
|
||||
|
||||
/* Sets whether we send read receipts for friendnumber. */
|
||||
void m_set_sends_receipts(int friendnumber, int yesno)
|
||||
void m_set_sends_receipts(Messenger *m, int friendnumber, int yesno)
|
||||
{
|
||||
if (yesno != 0 || yesno != 1)
|
||||
return;
|
||||
if (friendnumber >= numfriends || friendnumber < 0)
|
||||
if (friendnumber >= m->numfriends || friendnumber < 0)
|
||||
return;
|
||||
friendlist[friendnumber].receives_read_receipts = yesno;
|
||||
m->friendlist[friendnumber].receives_read_receipts = yesno;
|
||||
}
|
||||
|
||||
/* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t);
|
||||
static uint8_t friend_request_isset = 0; */
|
||||
/* set the function that will be executed when a friend request is received. */
|
||||
void m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
|
||||
void m_callback_friendrequest(Messenger *m, void (*function)(uint8_t *, uint8_t *, uint16_t, void*), void* userdata)
|
||||
{
|
||||
callback_friendrequest(function);
|
||||
callback_friendrequest(function, userdata);
|
||||
}
|
||||
|
||||
static void (*friend_message)(int, uint8_t *, uint16_t);
|
||||
static uint8_t friend_message_isset = 0;
|
||||
|
||||
/* set the function that will be executed when a message from a friend is received. */
|
||||
void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t))
|
||||
void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata)
|
||||
{
|
||||
friend_message = function;
|
||||
friend_message_isset = 1;
|
||||
m->friend_message = function;
|
||||
m->friend_message_isset = 1;
|
||||
m->friend_message_userdata = userdata;
|
||||
}
|
||||
|
||||
static void (*friend_action)(int, uint8_t *, uint16_t);
|
||||
static uint8_t friend_action_isset = 0;
|
||||
void m_callback_action(void (*function)(int, uint8_t *, uint16_t))
|
||||
void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata)
|
||||
{
|
||||
friend_action = function;
|
||||
friend_action_isset = 1;
|
||||
m->friend_action = function;
|
||||
m->friend_action_isset = 1;
|
||||
m->friend_action_userdata = userdata;
|
||||
}
|
||||
|
||||
static void (*friend_namechange)(int, uint8_t *, uint16_t);
|
||||
static uint8_t friend_namechange_isset = 0;
|
||||
void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t))
|
||||
void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata)
|
||||
{
|
||||
friend_namechange = function;
|
||||
friend_namechange_isset = 1;
|
||||
m->friend_namechange = function;
|
||||
m->friend_namechange_isset = 1;
|
||||
m->friend_namechange_userdata = userdata;
|
||||
}
|
||||
|
||||
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))
|
||||
void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata)
|
||||
{
|
||||
friend_statusmessagechange = function;
|
||||
friend_statusmessagechange_isset = 1;
|
||||
m->friend_statusmessagechange = function;
|
||||
m->friend_statusmessagechange_isset = 1;
|
||||
m->friend_statuschange_userdata = userdata;
|
||||
}
|
||||
|
||||
static void (*friend_userstatuschange)(int, USERSTATUS);
|
||||
static uint8_t friend_userstatuschange_isset = 0;
|
||||
void m_callback_userstatus(void (*function)(int, USERSTATUS))
|
||||
void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void*), void* userdata)
|
||||
{
|
||||
friend_userstatuschange = function;
|
||||
friend_userstatuschange_isset = 1;
|
||||
m->friend_userstatuschange = function;
|
||||
m->friend_userstatuschange_isset = 1;
|
||||
m->friend_userstatuschange_userdata = userdata;
|
||||
}
|
||||
|
||||
static void (*read_receipt)(int, uint32_t);
|
||||
static uint8_t read_receipt_isset = 0;
|
||||
void m_callback_read_receipt(void (*function)(int, uint32_t))
|
||||
void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void*), void* userdata)
|
||||
{
|
||||
read_receipt = function;
|
||||
read_receipt_isset = 1;
|
||||
m->read_receipt = function;
|
||||
m->read_receipt_isset = 1;
|
||||
m->read_receipt_userdata = userdata;
|
||||
}
|
||||
|
||||
static void (*friend_connectionstatuschange)(int, uint8_t);
|
||||
static uint8_t friend_connectionstatuschange_isset = 0;
|
||||
void m_callback_connectionstatus(void (*function)(int, uint8_t))
|
||||
void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void*), void* userdata)
|
||||
{
|
||||
friend_connectionstatuschange = function;
|
||||
friend_connectionstatuschange_isset = 1;
|
||||
m->friend_connectionstatuschange = function;
|
||||
m->friend_connectionstatuschange_isset = 1;
|
||||
m->friend_connectionstatuschange_userdata = userdata;
|
||||
}
|
||||
|
||||
static void check_friend_connectionstatus(int friendnumber, uint8_t status)
|
||||
static void check_friend_connectionstatus(Messenger *m, int friendnumber, uint8_t status)
|
||||
{
|
||||
if (!friend_connectionstatuschange_isset)
|
||||
if (!m->friend_connectionstatuschange_isset)
|
||||
return;
|
||||
if (status == NOFRIEND)
|
||||
return;
|
||||
const uint8_t was_connected = friendlist[friendnumber].status == FRIEND_ONLINE;
|
||||
const uint8_t was_connected = m->friendlist[friendnumber].status == FRIEND_ONLINE;
|
||||
const uint8_t is_connected = status == FRIEND_ONLINE;
|
||||
if (is_connected != was_connected)
|
||||
friend_connectionstatuschange(friendnumber, is_connected);
|
||||
m->friend_connectionstatuschange(m, friendnumber, is_connected, m->friend_connectionstatuschange_userdata);
|
||||
}
|
||||
|
||||
static void set_friend_status(int friendnumber, uint8_t status)
|
||||
void set_friend_status(Messenger *m, int friendnumber, uint8_t status)
|
||||
{
|
||||
check_friend_connectionstatus(friendnumber, status);
|
||||
friendlist[friendnumber].status = status;
|
||||
check_friend_connectionstatus(m, friendnumber, status);
|
||||
m->friendlist[friendnumber].status = status;
|
||||
}
|
||||
|
||||
static int write_cryptpacket_id(int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length)
|
||||
int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length)
|
||||
{
|
||||
if (friendnumber < 0 || friendnumber >= numfriends)
|
||||
if (friendnumber < 0 || friendnumber >= m->numfriends)
|
||||
return 0;
|
||||
if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != FRIEND_ONLINE)
|
||||
if (length >= MAX_DATA_SIZE || m->friendlist[friendnumber].status != FRIEND_ONLINE)
|
||||
return 0;
|
||||
uint8_t packet[length + 1];
|
||||
packet[0] = packet_id;
|
||||
memcpy(packet + 1, data, length);
|
||||
return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, packet, length + 1);
|
||||
return write_cryptpacket(m->friendlist[friendnumber].crypt_connection_id, packet, length + 1);
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
#define PORT 33445
|
||||
/* run this at startup */
|
||||
Messenger * initMessenger(void)
|
||||
{
|
||||
Messenger *m = calloc(1, sizeof(Messenger));
|
||||
if( ! m )
|
||||
return 0;
|
||||
|
||||
new_keys();
|
||||
m_set_statusmessage(m, (uint8_t*)"Online", sizeof("Online"));
|
||||
initNetCrypto();
|
||||
IP ip;
|
||||
ip.i = 0;
|
||||
|
||||
if(init_networking(ip,PORT) == -1)
|
||||
return 0;
|
||||
|
||||
DHT_init();
|
||||
LosslessUDP_init();
|
||||
friendreq_init();
|
||||
LANdiscovery_init();
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
/* run this before closing shop */
|
||||
void cleanupMessenger(Messenger *m){
|
||||
/* FIXME TODO it seems no one frees friendlist or all the elements status */
|
||||
free(m);
|
||||
}
|
||||
|
||||
>>>>>>> upstream/master
|
||||
//TODO: make this function not suck.
|
||||
static void doFriends(void)
|
||||
void doFriends(Messenger *m)
|
||||
{
|
||||
/* TODO: add incoming connections and some other stuff. */
|
||||
uint32_t i;
|
||||
int len;
|
||||
uint8_t temp[MAX_DATA_SIZE];
|
||||
for (i = 0; i < numfriends; ++i) {
|
||||
if (friendlist[i].status == FRIEND_ADDED) {
|
||||
int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size);
|
||||
for (i = 0; i < m->numfriends; ++i) {
|
||||
if (m->friendlist[i].status == FRIEND_ADDED) {
|
||||
int fr = send_friendrequest(m->friendlist[i].client_id, m->friendlist[i].info, m->friendlist[i].info_size);
|
||||
if (fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case of packet loss */
|
||||
set_friend_status(i, FRIEND_REQUESTED);
|
||||
set_friend_status(m, i, FRIEND_REQUESTED);
|
||||
else if (fr > 0)
|
||||
set_friend_status(i, FRIEND_REQUESTED);
|
||||
set_friend_status(m, i, FRIEND_REQUESTED);
|
||||
}
|
||||
if (friendlist[i].status == FRIEND_REQUESTED || friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */
|
||||
if (friendlist[i].status == FRIEND_REQUESTED) {
|
||||
if (friendlist[i].friend_request_id + 10 < unix_time()) { /*I know this is hackish but it should work.*/
|
||||
send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size);
|
||||
friendlist[i].friend_request_id = unix_time();
|
||||
if (m->friendlist[i].status == FRIEND_REQUESTED || m->friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */
|
||||
if (m->friendlist[i].status == FRIEND_REQUESTED) {
|
||||
if (m->friendlist[i].friend_request_id + 10 < unix_time()) { /*I know this is hackish but it should work.*/
|
||||
send_friendrequest(m->friendlist[i].client_id, m->friendlist[i].info, m->friendlist[i].info_size);
|
||||
m->friendlist[i].friend_request_id = unix_time();
|
||||
}
|
||||
}
|
||||
IP_Port friendip = DHT_getfriendip(friendlist[i].client_id);
|
||||
switch (is_cryptoconnected(friendlist[i].crypt_connection_id)) {
|
||||
IP_Port friendip = DHT_getfriendip(m->friendlist[i].client_id);
|
||||
switch (is_cryptoconnected(m->friendlist[i].crypt_connection_id)) {
|
||||
case 0:
|
||||
if (friendip.ip.i > 1)
|
||||
friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip);
|
||||
m->friendlist[i].crypt_connection_id = crypto_connect(m->friendlist[i].client_id, friendip);
|
||||
break;
|
||||
case 3: /* Connection is established */
|
||||
set_friend_status(i, FRIEND_ONLINE);
|
||||
friendlist[i].name_sent = 0;
|
||||
friendlist[i].userstatus_sent = 0;
|
||||
friendlist[i].statusmessage_sent = 0;
|
||||
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;
|
||||
break;
|
||||
case 4:
|
||||
crypto_kill(friendlist[i].crypt_connection_id);
|
||||
friendlist[i].crypt_connection_id = -1;
|
||||
crypto_kill(m->friendlist[i].crypt_connection_id);
|
||||
m->friendlist[i].crypt_connection_id = -1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
while (friendlist[i].status == FRIEND_ONLINE) { /* friend is online */
|
||||
if (friendlist[i].name_sent == 0) {
|
||||
if (m_sendname(i, self_name, self_name_length))
|
||||
friendlist[i].name_sent = 1;
|
||||
while (m->friendlist[i].status == FRIEND_ONLINE) { /* friend is online */
|
||||
if (m->friendlist[i].name_sent == 0) {
|
||||
if (m_sendname(m, i, m->name, m->name_length))
|
||||
m->friendlist[i].name_sent = 1;
|
||||
}
|
||||
if (friendlist[i].statusmessage_sent == 0) {
|
||||
if (send_statusmessage(i, self_statusmessage, self_statusmessage_length))
|
||||
friendlist[i].statusmessage_sent = 1;
|
||||
if (m->friendlist[i].statusmessage_sent == 0) {
|
||||
if (send_statusmessage(m, i, m->statusmessage, m->statusmessage_length))
|
||||
m->friendlist[i].statusmessage_sent = 1;
|
||||
}
|
||||
if (friendlist[i].userstatus_sent == 0) {
|
||||
if (send_userstatus(i, self_userstatus))
|
||||
friendlist[i].userstatus_sent = 1;
|
||||
if (m->friendlist[i].userstatus_sent == 0) {
|
||||
if (send_userstatus(m, i, m->userstatus))
|
||||
m->friendlist[i].userstatus_sent = 1;
|
||||
}
|
||||
len = read_cryptpacket(friendlist[i].crypt_connection_id, temp);
|
||||
len = read_cryptpacket(m->friendlist[i].crypt_connection_id, temp);
|
||||
uint8_t packet_id = temp[0];
|
||||
uint8_t* data = temp + 1;
|
||||
int data_length = len - 1;
|
||||
|
@ -586,10 +580,10 @@ static void doFriends(void)
|
|||
case PACKET_ID_NICKNAME: {
|
||||
if (data_length >= MAX_NAME_LENGTH || data_length == 0)
|
||||
break;
|
||||
if(friend_namechange_isset)
|
||||
friend_namechange(i, data, data_length);
|
||||
memcpy(friendlist[i].name, data, data_length);
|
||||
friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */
|
||||
if(m->friend_namechange_isset)
|
||||
m->friend_namechange(m, i, data, data_length, m->friend_namechange_userdata);
|
||||
memcpy(m->friendlist[i].name, data, data_length);
|
||||
m->friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */
|
||||
break;
|
||||
}
|
||||
case PACKET_ID_STATUSMESSAGE: {
|
||||
|
@ -597,9 +591,10 @@ static void doFriends(void)
|
|||
break;
|
||||
uint8_t *status = calloc(MIN(data_length, MAX_STATUSMESSAGE_LENGTH), 1);
|
||||
memcpy(status, data, MIN(data_length, MAX_STATUSMESSAGE_LENGTH));
|
||||
if (friend_statusmessagechange_isset)
|
||||
friend_statusmessagechange(i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH));
|
||||
set_friend_statusmessage(i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH));
|
||||
if (m->friend_statusmessagechange_isset)
|
||||
m->friend_statusmessagechange(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH),
|
||||
m->friend_statuschange_userdata);
|
||||
set_friend_statusmessage(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH));
|
||||
free(status);
|
||||
break;
|
||||
}
|
||||
|
@ -607,9 +602,9 @@ static void doFriends(void)
|
|||
if (data_length != 1)
|
||||
break;
|
||||
USERSTATUS status = data[0];
|
||||
if (friend_userstatuschange_isset)
|
||||
friend_userstatuschange(i, status);
|
||||
set_friend_userstatus(i, status);
|
||||
if (m->friend_userstatuschange_isset)
|
||||
m->friend_userstatuschange(m, i, status, m->friend_userstatuschange_userdata);
|
||||
set_friend_userstatus(m, i, status);
|
||||
break;
|
||||
}
|
||||
case PACKET_ID_MESSAGE: {
|
||||
|
@ -617,16 +612,16 @@ static void doFriends(void)
|
|||
uint8_t message_id_length = 4;
|
||||
uint8_t *message = data + message_id_length;
|
||||
uint16_t message_length = data_length - message_id_length;
|
||||
if (friendlist[i].receives_read_receipts) {
|
||||
write_cryptpacket_id(i, PACKET_ID_RECEIPT, message_id, message_id_length);
|
||||
if (m->friendlist[i].receives_read_receipts) {
|
||||
write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length);
|
||||
}
|
||||
if (friend_message_isset)
|
||||
(*friend_message)(i, message, message_length);
|
||||
if (m->friend_message_isset)
|
||||
(*m->friend_message)(m, i, message, message_length, m->friend_message_userdata);
|
||||
break;
|
||||
}
|
||||
case PACKET_ID_ACTION: {
|
||||
if (friend_action_isset)
|
||||
(*friend_action)(i, data, data_length);
|
||||
if (m->friend_action_isset)
|
||||
(*m->friend_action)(m, i, data, data_length, m->friend_action_userdata);
|
||||
break;
|
||||
}
|
||||
case PACKET_ID_RECEIPT: {
|
||||
|
@ -635,16 +630,16 @@ static void doFriends(void)
|
|||
break;
|
||||
memcpy(&msgid, data, sizeof(msgid));
|
||||
msgid = ntohl(msgid);
|
||||
if (read_receipt_isset)
|
||||
(*read_receipt)(i, msgid);
|
||||
if (m->read_receipt_isset)
|
||||
(*m->read_receipt)(m, i, msgid, m->read_receipt_userdata);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */
|
||||
crypto_kill(friendlist[i].crypt_connection_id);
|
||||
friendlist[i].crypt_connection_id = -1;
|
||||
set_friend_status(i, FRIEND_CONFIRMED);
|
||||
if (is_cryptoconnected(m->friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */
|
||||
crypto_kill(m->friendlist[i].crypt_connection_id);
|
||||
m->friendlist[i].crypt_connection_id = -1;
|
||||
set_friend_status(m, i, FRIEND_CONFIRMED);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -652,20 +647,20 @@ static void doFriends(void)
|
|||
}
|
||||
}
|
||||
|
||||
static void doInbound(void)
|
||||
void doInbound(Messenger *m)
|
||||
{
|
||||
uint8_t secret_nonce[crypto_box_NONCEBYTES];
|
||||
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
|
||||
uint8_t session_key[crypto_box_PUBLICKEYBYTES];
|
||||
int inconnection = crypto_inbound(public_key, secret_nonce, session_key);
|
||||
if (inconnection != -1) {
|
||||
int friend_id = getfriend_id(public_key);
|
||||
int friend_id = getfriend_id(m, public_key);
|
||||
if (friend_id != -1) {
|
||||
crypto_kill(friendlist[friend_id].crypt_connection_id);
|
||||
friendlist[friend_id].crypt_connection_id =
|
||||
crypto_kill(m->friendlist[friend_id].crypt_connection_id);
|
||||
m->friendlist[friend_id].crypt_connection_id =
|
||||
accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key);
|
||||
|
||||
set_friend_status(friend_id, FRIEND_CONFIRMED);
|
||||
set_friend_status(m, friend_id, FRIEND_CONFIRMED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -676,7 +671,11 @@ static void doInbound(void)
|
|||
#define LAN_DISCOVERY_INTERVAL 60
|
||||
|
||||
/*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/
|
||||
<<<<<<< HEAD
|
||||
static int LANdiscovery_timercallback(timer* t, void* ignore)
|
||||
=======
|
||||
void LANdiscovery(Messenger *m)
|
||||
>>>>>>> upstream/master
|
||||
{
|
||||
send_LANdiscovery(htons(PORT));
|
||||
timer_start(t, LAN_DISCOVERY_INTERVAL);
|
||||
|
@ -707,9 +706,10 @@ int initMessenger(void)
|
|||
}
|
||||
|
||||
/* the main loop that needs to be run at least 200 times per second. */
|
||||
void doMessenger(void)
|
||||
void doMessenger(Messenger *m)
|
||||
{
|
||||
networking_poll();
|
||||
<<<<<<< HEAD
|
||||
timer_poll();
|
||||
|
||||
doDHT();
|
||||
|
@ -717,17 +717,26 @@ void doMessenger(void)
|
|||
doNetCrypto();
|
||||
doInbound();
|
||||
doFriends();
|
||||
=======
|
||||
|
||||
doDHT();
|
||||
doLossless_UDP();
|
||||
doNetCrypto();
|
||||
doInbound(m);
|
||||
doFriends(m);
|
||||
LANdiscovery(m);
|
||||
>>>>>>> upstream/master
|
||||
}
|
||||
|
||||
/* returns the size of the messenger data (for saving) */
|
||||
uint32_t Messenger_size(void)
|
||||
uint32_t Messenger_size(Messenger *m)
|
||||
{
|
||||
return crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES
|
||||
+ sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * numfriends;
|
||||
+ sizeof(uint32_t) + DHT_size() + sizeof(uint32_t) + sizeof(Friend) * m->numfriends;
|
||||
}
|
||||
|
||||
/* save the messenger in data of size Messenger_size() */
|
||||
void Messenger_save(uint8_t *data)
|
||||
void Messenger_save(Messenger *m, uint8_t *data)
|
||||
{
|
||||
save_keys(data);
|
||||
data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
|
||||
|
@ -736,14 +745,14 @@ void Messenger_save(uint8_t *data)
|
|||
data += sizeof(size);
|
||||
DHT_save(data);
|
||||
data += size;
|
||||
size = sizeof(Friend) * numfriends;
|
||||
size = sizeof(Friend) * m->numfriends;
|
||||
memcpy(data, &size, sizeof(size));
|
||||
data += sizeof(size);
|
||||
memcpy(data, friendlist, sizeof(Friend) * numfriends);
|
||||
memcpy(data, m->friendlist, sizeof(Friend) * m->numfriends);
|
||||
}
|
||||
|
||||
/* load the messenger from data of size length. */
|
||||
int Messenger_load(uint8_t * data, uint32_t length)
|
||||
int Messenger_load(Messenger *m, uint8_t * data, uint32_t length)
|
||||
{
|
||||
if (length == ~0)
|
||||
return -1;
|
||||
|
@ -775,11 +784,12 @@ int Messenger_load(uint8_t * data, uint32_t length)
|
|||
uint32_t i;
|
||||
for (i = 0; i < num; ++i) {
|
||||
if(temp[i].status != 0) {
|
||||
int fnum = m_addfriend_norequest(temp[i].client_id);
|
||||
setfriendname(fnum, temp[i].name);
|
||||
int fnum = m_addfriend_norequest(m, temp[i].client_id);
|
||||
setfriendname(m, fnum, temp[i].name);
|
||||
/* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */
|
||||
}
|
||||
}
|
||||
free(temp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
134
core/Messenger.h
134
core/Messenger.h
|
@ -73,6 +73,66 @@ typedef enum {
|
|||
USERSTATUS_INVALID
|
||||
} USERSTATUS;
|
||||
|
||||
typedef struct {
|
||||
uint8_t client_id[CLIENT_ID_SIZE];
|
||||
int crypt_connection_id;
|
||||
uint64_t friend_request_id; /* id of the friend request corresponding to the current friend request to the current friend. */
|
||||
uint8_t status; /* 0 if no friend, 1 if added, 2 if friend request sent, 3 if confirmed friend, 4 if online. */
|
||||
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 *statusmessage;
|
||||
uint16_t statusmessage_length;
|
||||
uint8_t statusmessage_sent;
|
||||
USERSTATUS userstatus;
|
||||
uint8_t userstatus_sent;
|
||||
uint16_t info_size; /* length of the info */
|
||||
uint32_t message_id; /* a semi-unique id used in read receipts */
|
||||
uint8_t receives_read_receipts; /* shall we send read receipts to this person? */
|
||||
} Friend;
|
||||
|
||||
typedef struct Messenger {
|
||||
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
|
||||
|
||||
uint8_t name[MAX_NAME_LENGTH];
|
||||
uint16_t name_length;
|
||||
|
||||
uint8_t statusmessage[MAX_STATUSMESSAGE_LENGTH];
|
||||
uint16_t statusmessage_length;
|
||||
|
||||
USERSTATUS userstatus;
|
||||
|
||||
Friend *friendlist;
|
||||
uint32_t numfriends;
|
||||
|
||||
void (*friend_message)(struct Messenger *m, int, uint8_t *, uint16_t, void*);
|
||||
uint8_t friend_message_isset;
|
||||
void* friend_message_userdata;
|
||||
void (*friend_action)(struct Messenger *m, int, uint8_t *, uint16_t, void*);
|
||||
uint8_t friend_action_isset;
|
||||
void* friend_action_userdata;
|
||||
void (*friend_namechange)(struct Messenger *m, int, uint8_t *, uint16_t, void*);
|
||||
uint8_t friend_namechange_isset;
|
||||
void* friend_namechange_userdata;
|
||||
void (*friend_statusmessagechange)(struct Messenger *m, int, uint8_t *, uint16_t, void*);
|
||||
uint8_t friend_statusmessagechange_isset;
|
||||
void* friend_statusmessagechange_userdata;
|
||||
void (*friend_userstatuschange)(struct Messenger *m, int, USERSTATUS, void*);
|
||||
uint8_t friend_userstatuschange_isset;
|
||||
void* friend_userstatuschange_userdata;
|
||||
void (*read_receipt)(struct Messenger *m, int, uint32_t, void*);
|
||||
uint8_t read_receipt_isset;
|
||||
void* read_receipt_userdata;
|
||||
void (*friend_statuschange)(struct Messenger *m, int, uint8_t, void*);
|
||||
uint8_t friend_statuschange_isset;
|
||||
void* friend_statuschange_userdata;
|
||||
void (*friend_connectionstatuschange)(struct Messenger *m, int, uint8_t, void*);
|
||||
uint8_t friend_connectionstatuschange_isset;
|
||||
void* friend_connectionstatuschange_userdata;
|
||||
|
||||
|
||||
} Messenger;
|
||||
|
||||
/*
|
||||
* add a friend
|
||||
* set the data that will be sent along with friend request
|
||||
|
@ -85,33 +145,33 @@ typedef enum {
|
|||
* return -4 if friend request already sent or already a friend
|
||||
* return -5 for unknown error
|
||||
*/
|
||||
int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length);
|
||||
int m_addfriend(Messenger *m, 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);
|
||||
int m_addfriend_norequest(Messenger *m, 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);
|
||||
int getfriend_id(Messenger *m, 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);
|
||||
int getclient_id(Messenger *m, int friend_id, uint8_t *client_id);
|
||||
|
||||
/* remove a friend */
|
||||
int m_delfriend(int friendnumber);
|
||||
int m_delfriend(Messenger *m, 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);
|
||||
int m_friendstatus(Messenger *m, int friendnumber);
|
||||
|
||||
/* send a text chat message to an online friend
|
||||
returns the message id if packet was successfully put into the send queue
|
||||
|
@ -120,13 +180,13 @@ int m_friendstatus(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 m_sendmessage(int friendnumber, uint8_t *message, uint32_t length);
|
||||
uint32_t m_sendmessage_withid(int friendnumber, uint32_t theid, uint8_t *message, uint32_t length);
|
||||
uint32_t m_sendmessage(Messenger *m, int friendnumber, uint8_t *message, uint32_t length);
|
||||
uint32_t m_sendmessage_withid(Messenger *m, 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 m_sendaction(int friendnumber, uint8_t *action, uint32_t length);
|
||||
int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length);
|
||||
|
||||
/* Set our nickname
|
||||
name must be a string of maximum MAX_NAME_LENGTH length.
|
||||
|
@ -134,73 +194,73 @@ int m_sendaction(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 setname(uint8_t *name, uint16_t length);
|
||||
int setname(Messenger *m, uint8_t *name, uint16_t length);
|
||||
|
||||
/* get our nickname
|
||||
put it in name
|
||||
return the length of the name*/
|
||||
uint16_t getself_name(uint8_t *name);
|
||||
uint16_t getself_name(Messenger *m, uint8_t *name);
|
||||
|
||||
/* 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);
|
||||
int getname(Messenger *m, 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_statusmessage(uint8_t *status, uint16_t length);
|
||||
int m_set_userstatus(USERSTATUS status);
|
||||
int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length);
|
||||
int m_set_userstatus(Messenger *m, USERSTATUS status);
|
||||
|
||||
/* return the length of friendnumber's status message,
|
||||
including null
|
||||
pass it into malloc */
|
||||
int m_get_statusmessage_size(int friendnumber);
|
||||
int m_get_statusmessage_size(Messenger *m, 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 m_copy_statusmessage(int friendnumber, uint8_t *buf, uint32_t maxlen);
|
||||
int m_copy_self_statusmessage(uint8_t *buf, uint32_t maxlen);
|
||||
int m_copy_statusmessage(Messenger *m, int friendnumber, uint8_t *buf, uint32_t maxlen);
|
||||
int m_copy_self_statusmessage(Messenger *m, 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. */
|
||||
USERSTATUS m_get_userstatus(int friendnumber);
|
||||
USERSTATUS m_get_self_userstatus(void);
|
||||
USERSTATUS m_get_userstatus(Messenger *m, int friendnumber);
|
||||
USERSTATUS m_get_self_userstatus(Messenger *m);
|
||||
|
||||
/* 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 m_set_sends_receipts(int friendnumber, int yesno);
|
||||
void m_set_sends_receipts(Messenger *m, 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 m_callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
|
||||
void m_callback_friendrequest(Messenger *m, 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 m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t));
|
||||
void m_callback_friendmessage(Messenger *m, void (*function)(Messenger *m, 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 m_callback_action(void (*function)(int, uint8_t *, uint16_t));
|
||||
void m_callback_action(Messenger *m, void (*function)(Messenger *m, 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 m_callback_namechange(void (*function)(int, uint8_t *, uint16_t));
|
||||
void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, 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 m_callback_statusmessage(void (*function)(int, uint8_t *, uint16_t));
|
||||
void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t, void*), void* userdata);
|
||||
|
||||
/* set the callback for status type changes
|
||||
function(int friendnumber, USERSTATUS kind) */
|
||||
void m_callback_userstatus(void (*function)(int, USERSTATUS));
|
||||
void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS, void*), void* userdata);
|
||||
|
||||
/* set the callback for read receipts
|
||||
function(int friendnumber, uint32_t receipt)
|
||||
|
@ -209,7 +269,7 @@ void m_callback_userstatus(void (*function)(int, USERSTATUS));
|
|||
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 m_callback_read_receipt(void (*function)(int, uint32_t));
|
||||
void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t, void*), void* userdata);
|
||||
|
||||
/* set the callback for connection status changes
|
||||
function(int friendnumber, uint8_t status)
|
||||
|
@ -219,26 +279,30 @@ void m_callback_read_receipt(void (*function)(int, uint32_t));
|
|||
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 m_callback_connectionstatus(void (*function)(int, uint8_t));
|
||||
void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t, void*), void* userdata);
|
||||
|
||||
/* run this at startup
|
||||
returns 0 if no connection problems
|
||||
returns -1 if there are problems */
|
||||
int initMessenger(void);
|
||||
* returns allocated instance of Messenger on success
|
||||
* returns 0 if there are problems */
|
||||
Messenger * initMessenger(void);
|
||||
|
||||
/* run this before closing shop
|
||||
* free all datastructures */
|
||||
void cleanupMessenger(Messenger *M);
|
||||
|
||||
/* the main loop that needs to be run at least 200 times per second */
|
||||
void doMessenger(void);
|
||||
void doMessenger(Messenger *m);
|
||||
|
||||
/* SAVING AND LOADING FUNCTIONS: */
|
||||
|
||||
/* returns the size of the messenger data (for saving) */
|
||||
uint32_t Messenger_size(void);
|
||||
uint32_t Messenger_size(Messenger *m);
|
||||
|
||||
/* save the messenger in data (must be allocated memory of size Messenger_size()) */
|
||||
void Messenger_save(uint8_t *data);
|
||||
void Messenger_save(Messenger *m, uint8_t *data);
|
||||
|
||||
/* load the messenger from data of size length */
|
||||
int Messenger_load(uint8_t *data, uint32_t length);
|
||||
int Messenger_load(Messenger *m, uint8_t *data, uint32_t length);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
@ -57,14 +57,15 @@ int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length)
|
|||
return num;
|
||||
}
|
||||
|
||||
static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t);
|
||||
static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t, void*);
|
||||
static uint8_t handle_friendrequest_isset = 0;
|
||||
|
||||
static void* handle_friendrequest_userdata;
|
||||
/* set the function that will be executed when a friend request is received. */
|
||||
void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t))
|
||||
void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t, void*), void* userdata)
|
||||
{
|
||||
handle_friendrequest = function;
|
||||
handle_friendrequest_isset = 1;
|
||||
handle_friendrequest_userdata = userdata;
|
||||
}
|
||||
|
||||
|
||||
|
@ -121,7 +122,7 @@ static int friendreq_handlepacket(IP_Port source, uint8_t * packet, uint32_t len
|
|||
return 1;
|
||||
|
||||
addto_receivedlist(public_key);
|
||||
(*handle_friendrequest)(public_key, data, len);
|
||||
(*handle_friendrequest)(public_key, data, len, handle_friendrequest_userdata);
|
||||
} else { /* if request is not for us, try routing it. */
|
||||
if(route_packet(packet + 1, packet, length) == length)
|
||||
return 0;
|
||||
|
|
|
@ -37,7 +37,7 @@ int send_friendrequest(uint8_t *public_key, uint8_t *data, uint32_t length);
|
|||
|
||||
/* set the function that will be executed when a friend request for us is received.
|
||||
function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
|
||||
void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
|
||||
void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t, void*), void* userdata);
|
||||
|
||||
/* sets up friendreq packet handlers */
|
||||
void friendreq_init(void);
|
||||
|
|
|
@ -1,21 +1,21 @@
|
|||
/* Messenger test
|
||||
*
|
||||
*
|
||||
* This program adds a friend and accepts all friend requests with the proper message.
|
||||
*
|
||||
*
|
||||
* It tries sending a message to the added friend.
|
||||
*
|
||||
*
|
||||
* If it receives a message from a friend it replies back.
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
* This is how I compile it: gcc -O2 -Wall -D VANILLA_NACL -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../core/DHT.c ../nacl/build/${HOSTNAME%.*}/lib/amd64/{cpucycles.o,libnacl.a,randombytes.o} Messenger_test.c
|
||||
*
|
||||
*
|
||||
*
|
||||
* Command line arguments are the ip, port and public_key of a node (for bootstrapping).
|
||||
*
|
||||
*
|
||||
* EX: ./test 127.0.0.1 33445 CDCFD319CE3460824B33BE58FD86B8941C9585181D8FBD7C79C5721D7C2E9F7C
|
||||
*
|
||||
*
|
||||
* Or the argument can be the path to the save file.
|
||||
*
|
||||
*
|
||||
* EX: ./test Save.bak
|
||||
*
|
||||
* Copyright (C) 2013 Tox project All Rights Reserved.
|
||||
|
@ -34,7 +34,7 @@
|
|||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
#include "../core/Messenger.h"
|
||||
|
@ -51,7 +51,11 @@
|
|||
|
||||
#endif
|
||||
|
||||
void print_request(uint8_t * public_key, uint8_t * data, uint16_t length)
|
||||
/* FIXME needed as print_request has to match the interface expected by
|
||||
* networking_requesthandler and so cannot take a Messenger * */
|
||||
static Messenger *m;
|
||||
|
||||
void print_request(uint8_t * public_key, uint8_t * data, uint16_t length, void* userdata)
|
||||
{
|
||||
printf("Friend request received from: \n");
|
||||
printf("ClientID: ");
|
||||
|
@ -63,7 +67,7 @@ void print_request(uint8_t * public_key, uint8_t * data, uint16_t length)
|
|||
printf("%hhX", public_key[j]);
|
||||
}
|
||||
printf("\nOf length: %u with data: %s \n", length, data);
|
||||
|
||||
|
||||
if(length != sizeof("Install Gentoo"))
|
||||
{
|
||||
return;
|
||||
|
@ -72,14 +76,14 @@ void print_request(uint8_t * public_key, uint8_t * data, uint16_t length)
|
|||
//if the request contained the message of peace the person is obviously a friend so we add him.
|
||||
{
|
||||
printf("Friend request accepted.\n");
|
||||
m_addfriend_norequest(public_key);
|
||||
m_addfriend_norequest(m, public_key);
|
||||
}
|
||||
}
|
||||
|
||||
void print_message(int friendnumber, uint8_t * string, uint16_t length)
|
||||
void print_message(Messenger *m, int friendnumber, uint8_t * string, uint16_t length, void* userdata)
|
||||
{
|
||||
printf("Message with length %u received from %u: %s \n", length, friendnumber, string);
|
||||
m_sendmessage(friendnumber, (uint8_t*)"Test1", 6);
|
||||
m_sendmessage(m, friendnumber, (uint8_t*)"Test1", 6);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
|
@ -88,7 +92,13 @@ int main(int argc, char *argv[])
|
|||
printf("usage %s ip port public_key (of the DHT bootstrap node)\n or\n %s Save.bak\n", argv[0], argv[0]);
|
||||
exit(0);
|
||||
}
|
||||
initMessenger();
|
||||
|
||||
m = initMessenger();
|
||||
if( !m ){
|
||||
fputs("Failed to allocate messenger datastructure\n", stderr);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if(argc > 3) {
|
||||
IP_Port bootstrap_ip_port;
|
||||
bootstrap_ip_port.port = htons(atoi(argv[2]));
|
||||
|
@ -100,13 +110,13 @@ int main(int argc, char *argv[])
|
|||
int read;
|
||||
uint8_t buffer[128000];
|
||||
read = fread(buffer, 1, 128000, file);
|
||||
printf("Messenger loaded: %i\n", Messenger_load(buffer, read));
|
||||
printf("Messenger loaded: %i\n", Messenger_load(m, buffer, read));
|
||||
fclose(file);
|
||||
|
||||
|
||||
}
|
||||
m_callback_friendrequest(print_request);
|
||||
m_callback_friendmessage(print_message);
|
||||
|
||||
m_callback_friendrequest(m, print_request, NULL);
|
||||
m_callback_friendmessage(m, print_message, NULL);
|
||||
|
||||
printf("OUR ID: ");
|
||||
uint32_t i;
|
||||
for(i = 0; i < 32; i++) {
|
||||
|
@ -114,33 +124,34 @@ int main(int argc, char *argv[])
|
|||
printf("0");
|
||||
printf("%hhX",self_public_key[i]);
|
||||
}
|
||||
|
||||
setname((uint8_t *)"Anon", 5);
|
||||
|
||||
|
||||
setname(m, (uint8_t *)"Anon", 5);
|
||||
|
||||
char temp_id[128];
|
||||
printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n");
|
||||
if(scanf("%s", temp_id) != 1) {
|
||||
return 1;
|
||||
}
|
||||
int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
|
||||
|
||||
int num = m_addfriend(m, hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
|
||||
|
||||
perror("Initialization");
|
||||
|
||||
while(1) {
|
||||
uint8_t name[128];
|
||||
getname(num, name);
|
||||
getname(m, num, name);
|
||||
printf("%s\n", name);
|
||||
|
||||
m_sendmessage(num, (uint8_t*)"Test", 5);
|
||||
doMessenger();
|
||||
|
||||
m_sendmessage(m, num, (uint8_t*)"Test", 5);
|
||||
doMessenger(m);
|
||||
c_sleep(30);
|
||||
FILE *file = fopen("Save.bak", "wb");
|
||||
if ( file==NULL ){return 1;}
|
||||
uint8_t * buffer = malloc(Messenger_size());
|
||||
Messenger_save(buffer);
|
||||
size_t write_result = fwrite(buffer, 1, Messenger_size(), file);
|
||||
if (write_result < Messenger_size()) {return 1;}
|
||||
uint8_t * buffer = malloc(Messenger_size(m));
|
||||
Messenger_save(m, buffer);
|
||||
size_t write_result = fwrite(buffer, 1, Messenger_size(m), file);
|
||||
if (write_result < Messenger_size(m)) {return 1;}
|
||||
free(buffer);
|
||||
fclose(file);
|
||||
}
|
||||
}
|
||||
cleanupMessenger(m);
|
||||
}
|
||||
|
|
|
@ -85,12 +85,12 @@ void new_lines(char *line)
|
|||
}
|
||||
|
||||
|
||||
void print_friendlist()
|
||||
void print_friendlist(Messenger *m)
|
||||
{
|
||||
char name[MAX_NAME_LENGTH];
|
||||
int i = 0;
|
||||
new_lines("[i] Friend List:");
|
||||
while(getname(i, (uint8_t *)name) != -1) {
|
||||
while(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: ")];
|
||||
|
||||
|
@ -107,13 +107,13 @@ void print_friendlist()
|
|||
new_lines("\tno friends! D:");
|
||||
}
|
||||
|
||||
char *format_message(char *message, int friendnum)
|
||||
char *format_message(Messenger *m, char *message, int friendnum)
|
||||
{
|
||||
char name[MAX_NAME_LENGTH];
|
||||
if (friendnum != -1) {
|
||||
getname(friendnum, (uint8_t*)name);
|
||||
getname(m, friendnum, (uint8_t*)name);
|
||||
} else {
|
||||
getself_name((uint8_t*)name);
|
||||
getself_name(m, (uint8_t*)name);
|
||||
}
|
||||
char *msg = malloc(100+strlen(message)+strlen(name)+1);
|
||||
|
||||
|
@ -133,7 +133,7 @@ char *format_message(char *message, int friendnum)
|
|||
return msg;
|
||||
}
|
||||
|
||||
void line_eval(char *line)
|
||||
void line_eval(Messenger *m, char *line)
|
||||
{
|
||||
if (line[0] == '/') {
|
||||
char inpt_command = line[1];
|
||||
|
@ -148,7 +148,7 @@ void line_eval(char *line)
|
|||
temp_id[i] = line[i+prompt_offset];
|
||||
|
||||
unsigned char *bin_string = hex_string_to_bin(temp_id);
|
||||
int num = m_addfriend(bin_string, (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
|
||||
int num = m_addfriend(m, bin_string, (uint8_t*)"Install Gentoo", sizeof("Install Gentoo"));
|
||||
free(bin_string);
|
||||
char numstring[100];
|
||||
switch (num) {
|
||||
|
@ -175,7 +175,7 @@ void line_eval(char *line)
|
|||
do_refresh();
|
||||
}
|
||||
else if (inpt_command == 'd') {
|
||||
doMessenger();
|
||||
doMessenger(m);
|
||||
}
|
||||
else if (inpt_command == 'm') { //message command: /m friendnumber messsage
|
||||
size_t len = strlen(line);
|
||||
|
@ -196,10 +196,10 @@ void line_eval(char *line)
|
|||
}
|
||||
}
|
||||
int num = atoi(numstring);
|
||||
if (m_sendmessage(num, (uint8_t*) message, strlen(message) + 1) != 1) {
|
||||
if (m_sendmessage(m, num, (uint8_t*) message, strlen(message) + 1) != 1) {
|
||||
new_lines("[i] could not send message");
|
||||
} else {
|
||||
new_lines(format_message(message, -1));
|
||||
new_lines(format_message(m, message, -1));
|
||||
}
|
||||
}
|
||||
else if (inpt_command == 'n') {
|
||||
|
@ -211,13 +211,13 @@ void line_eval(char *line)
|
|||
name[i-3] = line[i];
|
||||
}
|
||||
name[i-3] = 0;
|
||||
setname(name, i - 2);
|
||||
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();
|
||||
print_friendlist(m);
|
||||
}
|
||||
else if (inpt_command == 's') {
|
||||
uint8_t status[MAX_STATUSMESSAGE_LENGTH];
|
||||
|
@ -228,7 +228,7 @@ void line_eval(char *line)
|
|||
status[i-3] = line[i];
|
||||
}
|
||||
status[i-3] = 0;
|
||||
m_set_statusmessage(status, strlen((char*)status) + 1);
|
||||
m_set_statusmessage(m, status, strlen((char*)status) + 1);
|
||||
char numstring[100];
|
||||
sprintf(numstring, "[i] changed status to %s", (char*)status);
|
||||
new_lines(numstring);
|
||||
|
@ -240,7 +240,7 @@ void line_eval(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(pending_requests[numf].id);
|
||||
int num = m_addfriend_norequest(m, pending_requests[numf].id);
|
||||
if (num != -1) {
|
||||
pending_requests[numf].accepted = 1;
|
||||
sprintf(numchar, "[i] friend request %u accepted", numf);
|
||||
|
@ -336,7 +336,7 @@ void do_refresh()
|
|||
refresh();
|
||||
}
|
||||
|
||||
void print_request(uint8_t *public_key, uint8_t *data, uint16_t length)
|
||||
void print_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata)
|
||||
{
|
||||
new_lines("[i] received friend request with message:");
|
||||
new_lines((char *)data);
|
||||
|
@ -349,32 +349,32 @@ void print_request(uint8_t *public_key, uint8_t *data, uint16_t length)
|
|||
do_refresh();
|
||||
}
|
||||
|
||||
void print_message(int friendnumber, uint8_t * string, uint16_t length)
|
||||
void print_message(Messenger *m, int friendnumber, uint8_t * string, uint16_t length, void* userdata)
|
||||
{
|
||||
new_lines(format_message((char*)string, friendnumber));
|
||||
new_lines(format_message(m, (char*)string, friendnumber));
|
||||
}
|
||||
|
||||
void print_nickchange(int friendnumber, uint8_t *string, uint16_t length)
|
||||
void print_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
|
||||
{
|
||||
char name[MAX_NAME_LENGTH];
|
||||
if(getname(friendnumber, (uint8_t*)name) != -1) {
|
||||
if(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(int friendnumber, uint8_t *string, uint16_t length)
|
||||
void print_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
|
||||
{
|
||||
char name[MAX_NAME_LENGTH];
|
||||
if(getname(friendnumber, (uint8_t*)name) != -1) {
|
||||
if(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(char *path)
|
||||
void load_key(Messenger *m, char *path)
|
||||
{
|
||||
FILE *data_file = fopen(path, "r");
|
||||
int size = 0;
|
||||
|
@ -390,13 +390,13 @@ void load_key(char *path)
|
|||
fputs("[!] could not read data file! exiting...\n", stderr);
|
||||
goto FILE_ERROR;
|
||||
}
|
||||
Messenger_load(data, size);
|
||||
Messenger_load(m, data, size);
|
||||
|
||||
} else {
|
||||
//else save new keys
|
||||
int size = Messenger_size();
|
||||
int size = Messenger_size(m);
|
||||
uint8_t data[size];
|
||||
Messenger_save(data);
|
||||
Messenger_save(m, data);
|
||||
data_file = fopen(path, "w");
|
||||
|
||||
if(!data_file) {
|
||||
|
@ -435,6 +435,7 @@ int main(int argc, char *argv[])
|
|||
int i = 0;
|
||||
char *filename = "data";
|
||||
char idstring[200] = {0};
|
||||
Messenger *m;
|
||||
|
||||
if (argc < 4) {
|
||||
printf("[!] Usage: %s [IP] [port] [public_key] <keyfile>\n", argv[0]);
|
||||
|
@ -458,13 +459,18 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
}
|
||||
|
||||
initMessenger();
|
||||
load_key(filename);
|
||||
m = initMessenger();
|
||||
if( !m ){
|
||||
fputs("Failed to allocate Messenger datastructure", stderr);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
m_callback_friendrequest(print_request);
|
||||
m_callback_friendmessage(print_message);
|
||||
m_callback_namechange(print_nickchange);
|
||||
m_callback_statusmessage(print_statuschange);
|
||||
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);
|
||||
|
||||
initscr();
|
||||
noecho();
|
||||
|
@ -494,7 +500,7 @@ int main(int argc, char *argv[])
|
|||
on = 1;
|
||||
}
|
||||
|
||||
doMessenger();
|
||||
doMessenger(m);
|
||||
c_sleep(1);
|
||||
do_refresh();
|
||||
|
||||
|
@ -504,7 +510,7 @@ int main(int argc, char *argv[])
|
|||
|
||||
getmaxyx(stdscr, y, x);
|
||||
if (c == '\n') {
|
||||
line_eval(line);
|
||||
line_eval(m, line);
|
||||
strcpy(line, "");
|
||||
} else if (c == 8 || c == 127) {
|
||||
line[strlen(line)-1] = '\0';
|
||||
|
@ -512,6 +518,7 @@ int main(int argc, char *argv[])
|
|||
strcpy(line, appender(line, (char) c));
|
||||
}
|
||||
}
|
||||
cleanupMessenger(m);
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/* nTox.h
|
||||
*
|
||||
*
|
||||
*Textual frontend for Tox.
|
||||
*
|
||||
* Copyright (C) 2013 Tox project All Rights Reserved.
|
||||
|
@ -18,7 +18,7 @@
|
|||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef NTOX_H
|
||||
|
@ -43,7 +43,7 @@
|
|||
#define PUB_KEY_BYTES 32
|
||||
|
||||
void new_lines(char *line);
|
||||
void line_eval(char *line);
|
||||
void line_eval(Messenger *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);
|
||||
|
|
|
@ -29,9 +29,9 @@ extern int active_window;
|
|||
extern void del_window(ToxWindow *w, int f_num);
|
||||
extern void fix_name(uint8_t *name);
|
||||
void print_help(ChatContext *self);
|
||||
void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo);
|
||||
void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd, struct tm *timeinfo);
|
||||
|
||||
static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len)
|
||||
static void chat_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *msg, uint16_t len)
|
||||
{
|
||||
ChatContext *ctx = (ChatContext*) self->x;
|
||||
uint8_t nick[MAX_NAME_LENGTH] = {0};
|
||||
|
@ -43,7 +43,7 @@ static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len)
|
|||
if (ctx->friendnum != num)
|
||||
return;
|
||||
|
||||
getname(num, (uint8_t*) &nick);
|
||||
getname(m, num, (uint8_t*) &nick);
|
||||
msg[len-1] = '\0';
|
||||
nick[MAX_NAME_LENGTH-1] = '\0';
|
||||
fix_name(msg);
|
||||
|
@ -61,7 +61,7 @@ static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len)
|
|||
beep();
|
||||
}
|
||||
|
||||
static void chat_onAction(ToxWindow *self, int num, uint8_t *action, uint16_t len)
|
||||
static void chat_onAction(ToxWindow *self, Messenger *m, int num, uint8_t *action, uint16_t len)
|
||||
{
|
||||
ChatContext *ctx = (ChatContext*) self->x;
|
||||
time_t now;
|
||||
|
@ -117,7 +117,7 @@ int string_is_empty(char *string)
|
|||
return rc;
|
||||
}
|
||||
|
||||
static void chat_onKey(ToxWindow *self, int key)
|
||||
static void chat_onKey(ToxWindow *self, Messenger *m, int key)
|
||||
{
|
||||
ChatContext *ctx = (ChatContext*) self->x;
|
||||
time_t now;
|
||||
|
@ -155,7 +155,7 @@ static void chat_onKey(ToxWindow *self, int key)
|
|||
wmove(self->window, y2-CURS_Y_OFFSET, 0);
|
||||
wclrtobot(self->window);
|
||||
if (ctx->line[0] == '/')
|
||||
execute(self, ctx, ctx->line, timeinfo);
|
||||
execute(self, ctx, m, ctx->line, timeinfo);
|
||||
else {
|
||||
if (!string_is_empty(ctx->line)) {
|
||||
/* make sure the string has at least non-space character */
|
||||
|
@ -167,7 +167,7 @@ static void chat_onKey(ToxWindow *self, int key)
|
|||
wattroff(ctx->history, COLOR_PAIR(1));
|
||||
wprintw(ctx->history, "%s\n", ctx->line);
|
||||
}
|
||||
if (m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) == 0) {
|
||||
if (m_sendmessage(m, ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) == 0) {
|
||||
wattron(ctx->history, COLOR_PAIR(3));
|
||||
wprintw(ctx->history, " * Failed to send message.\n");
|
||||
wattroff(ctx->history, COLOR_PAIR(3));
|
||||
|
@ -178,7 +178,7 @@ static void chat_onKey(ToxWindow *self, int key)
|
|||
}
|
||||
}
|
||||
|
||||
void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo)
|
||||
void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd, struct tm *timeinfo)
|
||||
{
|
||||
if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) {
|
||||
wclear(self->window);
|
||||
|
@ -210,14 +210,14 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo)
|
|||
wattroff(ctx->history, COLOR_PAIR(2));
|
||||
|
||||
uint8_t selfname[MAX_NAME_LENGTH];
|
||||
int len = getself_name(selfname);
|
||||
int len = getself_name(m, selfname);
|
||||
char msg[MAX_STR_SIZE-len-4];
|
||||
snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t*) selfname, action);
|
||||
|
||||
wattron(ctx->history, COLOR_PAIR(1));
|
||||
wprintw(ctx->history, msg);
|
||||
wattroff(ctx->history, COLOR_PAIR(1));
|
||||
if (m_sendaction(ctx->friendnum, (uint8_t*) msg, strlen(msg)+1) < 0) {
|
||||
if (m_sendaction(m, ctx->friendnum, (uint8_t*) msg, strlen(msg)+1) < 0) {
|
||||
wattron(ctx->history, COLOR_PAIR(3));
|
||||
wprintw(ctx->history, " * Failed to send action\n");
|
||||
wattroff(ctx->history, COLOR_PAIR(3));
|
||||
|
@ -256,13 +256,13 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo)
|
|||
|
||||
msg = strchr(status, ' ');
|
||||
if (msg == NULL) {
|
||||
m_set_userstatus(status_kind);
|
||||
m_set_userstatus(m, status_kind);
|
||||
wprintw(ctx->history, "Status set to: %s\n", status_text);
|
||||
}
|
||||
else {
|
||||
msg++;
|
||||
m_set_userstatus(status_kind);
|
||||
m_set_statusmessage((uint8_t*) msg, strlen(msg)+1);
|
||||
m_set_userstatus(m, status_kind);
|
||||
m_set_statusmessage(m, ( uint8_t*) msg, strlen(msg)+1);
|
||||
wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg);
|
||||
}
|
||||
}
|
||||
|
@ -275,7 +275,7 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo)
|
|||
return;
|
||||
}
|
||||
nick++;
|
||||
setname((uint8_t*) nick, strlen(nick)+1);
|
||||
setname(m, (uint8_t*) nick, strlen(nick)+1);
|
||||
wprintw(ctx->history, "Nickname set to: %s\n", nick);
|
||||
}
|
||||
|
||||
|
@ -312,7 +312,7 @@ static void chat_onDraw(ToxWindow *self)
|
|||
wrefresh(self->window);
|
||||
}
|
||||
|
||||
static void chat_onInit(ToxWindow *self)
|
||||
static void chat_onInit(ToxWindow *self, Messenger *m)
|
||||
{
|
||||
int x, y;
|
||||
ChatContext *ctx = (ChatContext*) self->x;
|
||||
|
@ -329,7 +329,7 @@ void print_help(ChatContext *self)
|
|||
wattron(self->history, COLOR_PAIR(2) | A_BOLD);
|
||||
wprintw(self->history, "Commands:\n");
|
||||
wattroff(self->history, A_BOLD);
|
||||
|
||||
|
||||
wprintw(self->history, " /status <type> <message> : Set your status\n");
|
||||
wprintw(self->history, " /nick <nickname> : Set your nickname\n");
|
||||
wprintw(self->history, " /me <action> : Do an action\n");
|
||||
|
@ -342,7 +342,7 @@ void print_help(ChatContext *self)
|
|||
wattroff(self->history, COLOR_PAIR(2));
|
||||
}
|
||||
|
||||
ToxWindow new_chat(int friendnum)
|
||||
ToxWindow new_chat(Messenger *m, int friendnum)
|
||||
{
|
||||
ToxWindow ret;
|
||||
memset(&ret, 0, sizeof(ret));
|
||||
|
@ -356,7 +356,7 @@ ToxWindow new_chat(int friendnum)
|
|||
ret.onAction = &chat_onAction;
|
||||
|
||||
uint8_t nick[MAX_NAME_LENGTH] = {0};
|
||||
getname(friendnum, (uint8_t*) &nick);
|
||||
getname(m, friendnum, (uint8_t*) &nick);
|
||||
fix_name(nick);
|
||||
|
||||
snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum);
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM];
|
||||
extern int add_window(ToxWindow w, int n);
|
||||
extern ToxWindow new_chat(int friendnum);
|
||||
extern ToxWindow new_chat(Messenger *m, int friendnum);
|
||||
|
||||
extern int active_window;
|
||||
|
||||
|
@ -42,7 +42,7 @@ void fix_name(uint8_t *name)
|
|||
*q = 0;
|
||||
}
|
||||
|
||||
void friendlist_onMessage(ToxWindow *self, int num, uint8_t *str, uint16_t len)
|
||||
void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str, uint16_t len)
|
||||
{
|
||||
if (num >= num_friends)
|
||||
return;
|
||||
|
@ -54,7 +54,7 @@ void friendlist_onMessage(ToxWindow *self, int num, uint8_t *str, uint16_t len)
|
|||
for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
if (WINDOW_STATUS[i] == -1) {
|
||||
WINDOW_STATUS[i] = num;
|
||||
add_window(new_chat(num), i);
|
||||
add_window(new_chat(m, num), i);
|
||||
active_window = i;
|
||||
break;
|
||||
}
|
||||
|
@ -82,20 +82,20 @@ void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t
|
|||
fix_name(friends[num].status);
|
||||
}
|
||||
|
||||
int friendlist_onFriendAdded(int num)
|
||||
int friendlist_onFriendAdded(Messenger *m, int num)
|
||||
{
|
||||
if (num_friends == MAX_FRIENDS_NUM)
|
||||
return -1;
|
||||
|
||||
friends[num_friends].num = num;
|
||||
getname(num, friends[num_friends].name);
|
||||
getname(m, num, friends[num_friends].name);
|
||||
strcpy((char*) friends[num_friends].name, "unknown");
|
||||
strcpy((char*) friends[num_friends].status, "unknown");
|
||||
friends[num_friends++].chatwin = -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void friendlist_onKey(ToxWindow *self, int key)
|
||||
static void friendlist_onKey(ToxWindow *self, Messenger *m, int key)
|
||||
{
|
||||
if (key == KEY_UP) {
|
||||
if (--num_selected < 0)
|
||||
|
@ -121,7 +121,7 @@ static void friendlist_onKey(ToxWindow *self, int key)
|
|||
if (WINDOW_STATUS[i] == -1) {
|
||||
WINDOW_STATUS[i] = num_selected;
|
||||
friends[num_selected].chatwin = num_selected;
|
||||
add_window(new_chat(num_selected), i);
|
||||
add_window(new_chat(m, num_selected), i);
|
||||
active_window = i;
|
||||
break;
|
||||
}
|
||||
|
@ -164,7 +164,7 @@ void disable_chatwin(int f_num)
|
|||
friends[f_num].chatwin = -1;
|
||||
}
|
||||
|
||||
static void friendlist_onInit(ToxWindow *self)
|
||||
static void friendlist_onInit(ToxWindow *self, Messenger *m)
|
||||
{
|
||||
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@
|
|||
extern ToxWindow new_prompt();
|
||||
extern ToxWindow new_friendlist();
|
||||
|
||||
extern int friendlist_onFriendAdded(int num);
|
||||
extern int friendlist_onFriendAdded(Messenger *m, int num);
|
||||
extern void disable_chatwin(int f_num);
|
||||
extern int add_req(uint8_t *public_key); // XXX
|
||||
extern unsigned char *hex_string_to_bin(char hex_string[]);
|
||||
|
@ -40,11 +40,13 @@ char WINDOW_STATUS[MAX_WINDOW_SLOTS];
|
|||
static ToxWindow windows[MAX_WINDOW_SLOTS];
|
||||
static ToxWindow* prompt;
|
||||
|
||||
static Messenger *m;
|
||||
|
||||
int w_num;
|
||||
int active_window;
|
||||
|
||||
/* CALLBACKS START */
|
||||
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length)
|
||||
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void* userdata)
|
||||
{
|
||||
int n = add_req(public_key);
|
||||
wprintw(prompt->window, "\nFriend request from:\n");
|
||||
|
@ -63,25 +65,25 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length)
|
|||
}
|
||||
}
|
||||
|
||||
void on_message(int friendnumber, uint8_t *string, uint16_t length)
|
||||
void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
if (windows[i].onMessage != NULL)
|
||||
windows[i].onMessage(&windows[i], friendnumber, string, length);
|
||||
windows[i].onMessage(&windows[i], m, friendnumber, string, length);
|
||||
}
|
||||
}
|
||||
|
||||
void on_action(int friendnumber, uint8_t *string, uint16_t length)
|
||||
void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
|
||||
if (windows[i].onAction != NULL)
|
||||
windows[i].onAction(&windows[i], friendnumber, string, length);
|
||||
windows[i].onAction(&windows[i], m, friendnumber, string, length);
|
||||
}
|
||||
}
|
||||
|
||||
void on_nickchange(int friendnumber, uint8_t *string, uint16_t length)
|
||||
void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
|
||||
{
|
||||
wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string);
|
||||
int i;
|
||||
|
@ -91,7 +93,7 @@ void on_nickchange(int friendnumber, uint8_t *string, uint16_t length)
|
|||
}
|
||||
}
|
||||
|
||||
void on_statuschange(int friendnumber, uint8_t *string, uint16_t length)
|
||||
void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata)
|
||||
{
|
||||
wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string);
|
||||
int i;
|
||||
|
@ -103,7 +105,7 @@ void on_statuschange(int friendnumber, uint8_t *string, uint16_t length)
|
|||
|
||||
void on_friendadded(int friendnumber)
|
||||
{
|
||||
friendlist_onFriendAdded(friendnumber);
|
||||
friendlist_onFriendAdded(m, friendnumber);
|
||||
}
|
||||
/* CALLBACKS END */
|
||||
|
||||
|
@ -129,14 +131,14 @@ static void init_term()
|
|||
static void init_tox()
|
||||
{
|
||||
/* Init core */
|
||||
initMessenger();
|
||||
m = initMessenger();
|
||||
|
||||
/* Callbacks */
|
||||
m_callback_friendrequest(on_request);
|
||||
m_callback_friendmessage(on_message);
|
||||
m_callback_namechange(on_nickchange);
|
||||
m_callback_statusmessage(on_statuschange);
|
||||
m_callback_action(on_action);
|
||||
m_callback_friendrequest(m, on_request, NULL);
|
||||
m_callback_friendmessage(m, on_message, NULL);
|
||||
m_callback_namechange(m, on_nickchange, NULL);
|
||||
m_callback_statusmessage(m, on_statuschange, NULL);
|
||||
m_callback_action(m, on_action, NULL);
|
||||
}
|
||||
|
||||
#define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */
|
||||
|
@ -157,8 +159,7 @@ int init_connection(void)
|
|||
char line[MAXLINE];
|
||||
int linecnt = 0;
|
||||
while (fgets(line, sizeof(line), fp) && linecnt < MAXSERVERS) {
|
||||
int len = strlen(line);
|
||||
if (len > MINLINE && len < MAXLINE)
|
||||
if (strlen(line) > MINLINE)
|
||||
strcpy(servers[linecnt++], line);
|
||||
}
|
||||
if (linecnt < 1) {
|
||||
|
@ -211,7 +212,7 @@ int add_window(ToxWindow w, int n)
|
|||
return -1;
|
||||
|
||||
windows[n] = w;
|
||||
w.onInit(&w);
|
||||
w.onInit(&w, m);
|
||||
w_num++;
|
||||
return n;
|
||||
}
|
||||
|
@ -237,7 +238,7 @@ static void init_windows()
|
|||
w_num = 0;
|
||||
int n_prompt = 0;
|
||||
int n_friendslist = 1;
|
||||
if (add_window(new_prompt(), n_prompt) == -1
|
||||
if (add_window(new_prompt(), n_prompt) == -1
|
||||
|| add_window(new_friendlist(), n_friendslist) == -1) {
|
||||
fprintf(stderr, "add_window() failed.\n");
|
||||
endwin();
|
||||
|
@ -248,16 +249,22 @@ static void init_windows()
|
|||
|
||||
static void do_tox()
|
||||
{
|
||||
static int conn_try = 0;
|
||||
static bool dht_on = false;
|
||||
if (!dht_on && DHT_isconnected()) {
|
||||
if (!dht_on && !DHT_isconnected() && !(conn_try++ % 100)) {
|
||||
init_connection();
|
||||
wprintw(prompt->window, "\nEstablishing connection...\n");
|
||||
}
|
||||
else if (!dht_on && DHT_isconnected()) {
|
||||
dht_on = true;
|
||||
wprintw(prompt->window, "\nDHT connected.\n");
|
||||
}
|
||||
else if (dht_on && !DHT_isconnected()) {
|
||||
dht_on = false;
|
||||
wprintw(prompt->window, "\nDHT disconnected.\n");
|
||||
wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n");
|
||||
init_connection();
|
||||
}
|
||||
doMessenger();
|
||||
doMessenger(m);
|
||||
}
|
||||
|
||||
static void load_data(char *path)
|
||||
|
@ -285,17 +292,17 @@ static void load_data(char *path)
|
|||
endwin();
|
||||
exit(1);
|
||||
}
|
||||
Messenger_load(buf, len);
|
||||
Messenger_load(m, buf, len);
|
||||
}
|
||||
else {
|
||||
len = Messenger_size();
|
||||
len = Messenger_size(m);
|
||||
buf = malloc(len);
|
||||
if (buf == NULL) {
|
||||
fprintf(stderr, "malloc() failed.\n");
|
||||
endwin();
|
||||
exit(1);
|
||||
}
|
||||
Messenger_save(buf);
|
||||
Messenger_save(m, buf);
|
||||
|
||||
fd = fopen(path, "w");
|
||||
if (fd == NULL) {
|
||||
|
@ -329,7 +336,7 @@ static void draw_bar()
|
|||
move(LINES - 1, 0);
|
||||
|
||||
attron(COLOR_PAIR(4) | A_BOLD);
|
||||
printw(" TOXIC " TOXICVER "|");
|
||||
printw(" TOXIC " TOXICVER "|");
|
||||
attroff(COLOR_PAIR(4) | A_BOLD);
|
||||
|
||||
int i;
|
||||
|
@ -440,10 +447,6 @@ int main(int argc, char *argv[])
|
|||
load_data(DATA_FILE);
|
||||
free(DATA_FILE);
|
||||
|
||||
int connected = init_connection();
|
||||
if (connected != 0)
|
||||
wprintw(prompt->window, "Auto-connect failed (error code %d)\n", connected);
|
||||
|
||||
if (f_flag == -1) {
|
||||
attron(COLOR_PAIR(3) | A_BOLD);
|
||||
wprintw(prompt->window, "You passed '-f' without giving an argument.\n"
|
||||
|
@ -473,7 +476,8 @@ int main(int argc, char *argv[])
|
|||
if (ch == '\t' || ch == KEY_BTAB)
|
||||
set_active_window(ch);
|
||||
else if (ch != ERR)
|
||||
a->onKey(a, ch);
|
||||
a->onKey(a, m, ch);
|
||||
}
|
||||
cleanupMessenger(m);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -20,24 +20,24 @@ static char prompt_buf[MAX_STR_SIZE] = {0};
|
|||
static int prompt_buf_pos = 0;
|
||||
|
||||
/* commands */
|
||||
void cmd_accept(ToxWindow *, char **);
|
||||
void cmd_add(ToxWindow *, char **);
|
||||
void cmd_clear(ToxWindow *, char **);
|
||||
void cmd_connect(ToxWindow *, char **);
|
||||
void cmd_help(ToxWindow *, char **);
|
||||
void cmd_msg(ToxWindow *, char **);
|
||||
void cmd_myid(ToxWindow *, char **);
|
||||
void cmd_nick(ToxWindow *, char **);
|
||||
void cmd_quit(ToxWindow *, char **);
|
||||
void cmd_status(ToxWindow *, char **);
|
||||
void cmd_statusmsg(ToxWindow *, char **);
|
||||
void cmd_accept(ToxWindow *, Messenger *m, char **);
|
||||
void cmd_add(ToxWindow *, Messenger *m, char **);
|
||||
void cmd_clear(ToxWindow *, Messenger *m, char **);
|
||||
void cmd_connect(ToxWindow *, Messenger *m, char **);
|
||||
void cmd_help(ToxWindow *, Messenger *m, char **);
|
||||
void cmd_msg(ToxWindow *, Messenger *m, char **);
|
||||
void cmd_myid(ToxWindow *, Messenger *m, char **);
|
||||
void cmd_nick(ToxWindow *, Messenger *m, char **);
|
||||
void cmd_quit(ToxWindow *, Messenger *m, char **);
|
||||
void cmd_status(ToxWindow *, Messenger *m, char **);
|
||||
void cmd_statusmsg(ToxWindow *, Messenger *m, char **);
|
||||
|
||||
#define NUM_COMMANDS 13
|
||||
|
||||
static struct {
|
||||
char *name;
|
||||
int numargs;
|
||||
void (*func)(ToxWindow *, char **);
|
||||
void (*func)(ToxWindow *, Messenger *m, char **);
|
||||
} commands[] = {
|
||||
{ "accept", 1, cmd_accept },
|
||||
{ "add", 1, cmd_add },
|
||||
|
@ -74,7 +74,7 @@ unsigned char *hex_string_to_bin(char hex_string[])
|
|||
return val;
|
||||
}
|
||||
|
||||
void cmd_accept(ToxWindow *self, char **args)
|
||||
void cmd_accept(ToxWindow *self, Messenger *m, char **args)
|
||||
{
|
||||
int num = atoi(args[1]);
|
||||
if (num >= num_requests) {
|
||||
|
@ -82,7 +82,7 @@ void cmd_accept(ToxWindow *self, char **args)
|
|||
return;
|
||||
}
|
||||
|
||||
num = m_addfriend_norequest(pending_requests[num]);
|
||||
num = m_addfriend_norequest(m, pending_requests[num]);
|
||||
if (num == -1)
|
||||
wprintw(self->window, "Failed to add friend.\n");
|
||||
else {
|
||||
|
@ -91,7 +91,7 @@ void cmd_accept(ToxWindow *self, char **args)
|
|||
}
|
||||
}
|
||||
|
||||
void cmd_add(ToxWindow *self, char **args)
|
||||
void cmd_add(ToxWindow *self, Messenger *m, char **args)
|
||||
{
|
||||
uint8_t id_bin[KEY_SIZE_BYTES];
|
||||
char xx[3];
|
||||
|
@ -121,7 +121,7 @@ void cmd_add(ToxWindow *self, char **args)
|
|||
}
|
||||
id_bin[i] = x;
|
||||
}
|
||||
int num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1);
|
||||
int num = m_addfriend(m, id_bin, (uint8_t*) msg, strlen(msg)+1);
|
||||
switch (num) {
|
||||
case FAERR_TOOLONG:
|
||||
wprintw(self->window, "Message is too long.\n");
|
||||
|
@ -145,12 +145,12 @@ void cmd_add(ToxWindow *self, char **args)
|
|||
}
|
||||
}
|
||||
|
||||
void cmd_clear(ToxWindow *self, char **args)
|
||||
void cmd_clear(ToxWindow *self, Messenger *m, char **args)
|
||||
{
|
||||
wclear(self->window);
|
||||
}
|
||||
|
||||
void cmd_connect(ToxWindow *self, char **args)
|
||||
void cmd_connect(ToxWindow *self, Messenger *m, char **args)
|
||||
{
|
||||
IP_Port dht;
|
||||
char *ip = args[1];
|
||||
|
@ -174,13 +174,13 @@ void cmd_connect(ToxWindow *self, char **args)
|
|||
free(binary_string);
|
||||
}
|
||||
|
||||
void cmd_quit(ToxWindow *self, char **args)
|
||||
void cmd_quit(ToxWindow *self, Messenger *m, char **args)
|
||||
{
|
||||
endwin();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void cmd_help(ToxWindow *self, char **args)
|
||||
void cmd_help(ToxWindow *self, Messenger *m, char **args)
|
||||
{
|
||||
wclear(self->window);
|
||||
wattron(self->window, COLOR_PAIR(2) | A_BOLD);
|
||||
|
@ -197,7 +197,7 @@ void cmd_help(ToxWindow *self, char **args)
|
|||
wprintw(self->window, " quit/exit : Exit program\n");
|
||||
wprintw(self->window, " help : Print this message again\n");
|
||||
wprintw(self->window, " clear : Clear this window\n");
|
||||
|
||||
|
||||
wattron(self->window, A_BOLD);
|
||||
wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n");
|
||||
wattroff(self->window, A_BOLD);
|
||||
|
@ -205,17 +205,17 @@ void cmd_help(ToxWindow *self, char **args)
|
|||
wattroff(self->window, COLOR_PAIR(2));
|
||||
}
|
||||
|
||||
void cmd_msg(ToxWindow *self, char **args)
|
||||
void cmd_msg(ToxWindow *self, Messenger *m, char **args)
|
||||
{
|
||||
char *id = args[1];
|
||||
char *msg = args[2];
|
||||
if (m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) == 0)
|
||||
if (m_sendmessage(m, atoi(id), (uint8_t*) msg, strlen(msg)+1) == 0)
|
||||
wprintw(self->window, "Error occurred while sending message.\n");
|
||||
else
|
||||
wprintw(self->window, "Message successfully sent.\n");
|
||||
}
|
||||
|
||||
void cmd_myid(ToxWindow *self, char **args)
|
||||
void cmd_myid(ToxWindow *self, Messenger *m, char **args)
|
||||
{
|
||||
char id[KEY_SIZE_BYTES*2 + 1] = {0};
|
||||
size_t i;
|
||||
|
@ -227,14 +227,14 @@ void cmd_myid(ToxWindow *self, char **args)
|
|||
wprintw(self->window, "Your ID: %s\n", id);
|
||||
}
|
||||
|
||||
void cmd_nick(ToxWindow *self, char **args)
|
||||
void cmd_nick(ToxWindow *self, Messenger *m, char **args)
|
||||
{
|
||||
char *nick = args[1];
|
||||
setname((uint8_t*) nick, strlen(nick)+1);
|
||||
setname(m, (uint8_t*) nick, strlen(nick)+1);
|
||||
wprintw(self->window, "Nickname set to: %s\n", nick);
|
||||
}
|
||||
|
||||
void cmd_status(ToxWindow *self, char **args)
|
||||
void cmd_status(ToxWindow *self, Messenger *m, char **args)
|
||||
{
|
||||
char *status = args[1];
|
||||
char *status_text;
|
||||
|
@ -260,24 +260,24 @@ void cmd_status(ToxWindow *self, char **args)
|
|||
|
||||
char *msg = args[2];
|
||||
if (msg == NULL) {
|
||||
m_set_userstatus(status_kind);
|
||||
m_set_userstatus(m, status_kind);
|
||||
wprintw(self->window, "Status set to: %s\n", status_text);
|
||||
}
|
||||
else {
|
||||
m_set_userstatus(status_kind);
|
||||
m_set_statusmessage((uint8_t*) msg, strlen(msg)+1);
|
||||
m_set_userstatus(m, status_kind);
|
||||
m_set_statusmessage(m, (uint8_t*) msg, strlen(msg)+1);
|
||||
wprintw(self->window, "Status set to: %s, %s\n", status_text, msg);
|
||||
}
|
||||
}
|
||||
|
||||
void cmd_statusmsg(ToxWindow *self, char **args)
|
||||
void cmd_statusmsg(ToxWindow *self, Messenger *m, char **args)
|
||||
{
|
||||
char *msg = args[1];
|
||||
m_set_statusmessage((uint8_t*) msg, strlen(msg)+1);
|
||||
m_set_statusmessage(m, (uint8_t*) msg, strlen(msg)+1);
|
||||
wprintw(self->window, "Status set to: %s\n", msg);
|
||||
}
|
||||
|
||||
static void execute(ToxWindow *self, char *u_cmd)
|
||||
static void execute(ToxWindow *self, Messenger *m, char *u_cmd)
|
||||
{
|
||||
int newlines = 0;
|
||||
char cmd[MAX_STR_SIZE] = {0};
|
||||
|
@ -341,13 +341,13 @@ static void execute(ToxWindow *self, char *u_cmd)
|
|||
return;
|
||||
}
|
||||
}
|
||||
/* check for excess arguments */
|
||||
/* check for excess arguments */
|
||||
if (strcmp(cmdargs[0], "add") && strlen(cmdargs[j]) != 0) {
|
||||
wprintw(self->window, "Invalid command: too many arguments to %s.\n", commands[i].name);
|
||||
return;
|
||||
}
|
||||
/* pass arguments to command function */
|
||||
(commands[i].func)(self, cmdargs);
|
||||
(commands[i].func)(self, m, cmdargs);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -356,7 +356,7 @@ static void execute(ToxWindow *self, char *u_cmd)
|
|||
wprintw(self->window, "Invalid command.\n");
|
||||
}
|
||||
|
||||
static void prompt_onKey(ToxWindow *self, int key)
|
||||
static void prompt_onKey(ToxWindow *self, Messenger *m, int key)
|
||||
{
|
||||
/* Add printable characters to line */
|
||||
if (isprint(key)) {
|
||||
|
@ -380,7 +380,7 @@ static void prompt_onKey(ToxWindow *self, int key)
|
|||
/* RETURN key: execute command */
|
||||
else if (key == '\n') {
|
||||
wprintw(self->window, "\n");
|
||||
execute(self, prompt_buf);
|
||||
execute(self, m, prompt_buf);
|
||||
prompt_buf_pos = 0;
|
||||
prompt_buf[0] = 0;
|
||||
}
|
||||
|
@ -413,10 +413,10 @@ static void prompt_onDraw(ToxWindow *self)
|
|||
wrefresh(self->window);
|
||||
}
|
||||
|
||||
static void prompt_onInit(ToxWindow *self)
|
||||
static void prompt_onInit(ToxWindow *self, Messenger *m)
|
||||
{
|
||||
scrollok(self->window, 1);
|
||||
cmd_help(self, NULL);
|
||||
cmd_help(self, m, NULL);
|
||||
wclrtoeol(self->window);
|
||||
}
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
#define KEY_SIZE_BYTES 32
|
||||
|
||||
/* number of permanent default windows */
|
||||
#define N_DEFAULT_WINS 2
|
||||
#define N_DEFAULT_WINS 2
|
||||
|
||||
/* maximum window slots for WINDOW_STATUS array */
|
||||
#define MAX_WINDOW_SLOTS N_DEFAULT_WINS+MAX_FRIENDS_NUM
|
||||
|
@ -17,14 +17,14 @@
|
|||
typedef struct ToxWindow_ ToxWindow;
|
||||
|
||||
struct ToxWindow_ {
|
||||
void(*onKey)(ToxWindow*, int);
|
||||
void(*onKey)(ToxWindow*, Messenger*, int);
|
||||
void(*onDraw)(ToxWindow*);
|
||||
void(*onInit)(ToxWindow*);
|
||||
void(*onInit)(ToxWindow*, Messenger*);
|
||||
void(*onFriendRequest)(ToxWindow*, uint8_t*, uint8_t*, uint16_t);
|
||||
void(*onMessage)(ToxWindow*, int, uint8_t*, uint16_t);
|
||||
void(*onMessage)(ToxWindow*, Messenger*, int, uint8_t*, uint16_t);
|
||||
void(*onNickChange)(ToxWindow*, int, uint8_t*, uint16_t);
|
||||
void(*onStatusChange)(ToxWindow*, int, uint8_t*, uint16_t);
|
||||
void(*onAction)(ToxWindow*, int, uint8_t*, uint16_t);
|
||||
void(*onAction)(ToxWindow*, Messenger*, int, uint8_t*, uint16_t);
|
||||
char title[256];
|
||||
|
||||
void* x;
|
||||
|
|
Loading…
Reference in New Issue
Block a user