Merge pull request #438 from mkfifo/master

Messenger refactor - redid work from pull request 79
This commit is contained in:
irungentoo 2013-08-12 03:39:18 -07:00
commit 33e104f32f
12 changed files with 561 additions and 508 deletions

View File

@ -44,6 +44,8 @@
#define c_sleep(x) usleep(1000*x) #define c_sleep(x) usleep(1000*x)
#endif #endif
static Messenger *m;
uint8_t *parent_id = NULL; uint8_t *parent_id = NULL;
uint8_t *child_id = NULL; uint8_t *child_id = NULL;
@ -60,16 +62,16 @@ void do_tox(void)
dht_on = 0; 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)
{ {
puts("OK"); puts("OK");
request_flags |= SECOND_FLAG; 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)
{ {
puts("OK"); puts("OK");
request_flags |= FIRST_FLAG; request_flags |= FIRST_FLAG;
@ -84,7 +86,7 @@ int parent_friend_request(void)
fputs("Sending child request.", stdout); fputs("Sending child request.", stdout);
fflush(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 */ /* wait on the status change */
for(i = 0; i < WAIT_COUNT; i++) { for(i = 0; i < WAIT_COUNT; i++) {
@ -110,11 +112,11 @@ void child_got_request(uint8_t *public_key, uint8_t *data, uint16_t length)
{ {
fputs("OK\nsending status to parent", stdout); fputs("OK\nsending status to parent", stdout);
fflush(stdout); fflush(stdout);
m_addfriend_norequest(public_key); m_addfriend_norequest(m, public_key);
request_flags |= FIRST_FLAG; 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)
{ {
request_flags |= SECOND_FLAG; request_flags |= SECOND_FLAG;
} }
@ -168,12 +170,13 @@ int main(int argc, char *argv[])
int i = 0; int i = 0;
char *message = "Y-yes Mr. Watson?"; char *message = "Y-yes Mr. Watson?";
initMessenger(); m = initMessenger();
Messenger_save(child_id);
Messenger_save(m, child_id);
msync(child_id, crypto_box_PUBLICKEYBYTES, MS_SYNC); msync(child_id, crypto_box_PUBLICKEYBYTES, MS_SYNC);
m_callback_friendrequest(child_got_request); m_callback_friendrequest(m, child_got_request);
m_callback_statusmessage(child_got_statuschange); m_callback_statusmessage(m, child_got_statuschange);
/* wait on the friend request */ /* wait on the friend request */
while(!(request_flags & FIRST_FLAG)) while(!(request_flags & FIRST_FLAG))
@ -185,10 +188,12 @@ int main(int argc, char *argv[])
for(i = 0; i < 6; i++) { for(i = 0; i < 6; i++) {
/* send the message six times, just to be sure */ /* 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(); do_tox();
} }
cleanupMessenger(m);
return 0; return 0;
} }
@ -199,15 +204,16 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
m = initMessenger();
msync(parent_id, crypto_box_PUBLICKEYBYTES, MS_SYNC); msync(parent_id, crypto_box_PUBLICKEYBYTES, MS_SYNC);
m_callback_statusmessage(parent_confirm_status); m_callback_statusmessage(m, parent_confirm_status);
m_callback_friendmessage(parent_confirm_message); m_callback_friendmessage(m, parent_confirm_message);
/* hacky way to give the child time to set up */ /* hacky way to give the child time to set up */
c_sleep(50); c_sleep(50);
initMessenger(); Messenger_save(m, parent_id);
Messenger_save(parent_id);
if(parent_friend_request() == -1) if(parent_friend_request() == -1)
return -1; return -1;

View File

@ -34,6 +34,8 @@ unsigned char *bad_id = NULL;
int friend_id_num = 0; int friend_id_num = 0;
Messenger *m;
unsigned char * hex_string_to_bin(char hex_string[]) unsigned char * hex_string_to_bin(char hex_string[])
{ {
size_t len = strlen(hex_string); size_t len = strlen(hex_string);
@ -52,22 +54,22 @@ START_TEST(test_m_sendmesage)
int bad_len = MAX_DATA_SIZE; int bad_len = MAX_DATA_SIZE;
ck_assert(m_sendmessage(-1, (uint8_t *)message, good_len) == 0); ck_assert(m_sendmessage(m, -1, (uint8_t *)message, good_len) == 0);
ck_assert(m_sendmessage(REALLY_BIG_NUMBER, (uint8_t *)message, good_len) == 0); ck_assert(m_sendmessage(m, REALLY_BIG_NUMBER, (uint8_t *)message, good_len) == 0);
ck_assert(m_sendmessage(17, (uint8_t *)message, good_len) == 0); ck_assert(m_sendmessage(m, 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, friend_id_num, (uint8_t *)message, bad_len) == 0);
} }
END_TEST END_TEST
START_TEST(test_m_get_userstatus_size) START_TEST(test_m_get_userstatus_size)
{ {
int rc = 0; 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"); "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", "m_get_statusmessage_size did NOT catch the following argument: %d\n",
REALLY_BIG_NUMBER); 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 */ /* this WILL error if the original m_addfriend_norequest() failed */
ck_assert_msg((rc > 0 && rc <= MAX_STATUSMESSAGE_LENGTH), 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 good_length = strlen(status);
uint16_t bad_length = REALLY_BIG_NUMBER; 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", ck_abort_msg("m_set_userstatus did NOT catch the following length: %d\n",
REALLY_BIG_NUMBER); 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" 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); "MAX_STATUSMESSAGE_LENGTH: %d\n", good_length, MAX_STATUSMESSAGE_LENGTH);
} }
@ -95,9 +97,9 @@ END_TEST
START_TEST(test_m_friendstatus) 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"); "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", "m_friendstatus did NOT catch an argument of %d.\n",
REALLY_BIG_NUMBER); REALLY_BIG_NUMBER);
} }
@ -105,9 +107,9 @@ END_TEST
START_TEST(test_m_delfriend) 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"); "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", "m_delfriend did NOT catch the following number: %d\n",
REALLY_BIG_NUMBER); REALLY_BIG_NUMBER);
} }
@ -124,16 +126,16 @@ START_TEST(test_m_addfriend)
- crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES
+ crypto_box_ZEROBYTES + 100); + 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); 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 */ /* 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" 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" "(this can be caused by the error of m_addfriend_norequest in"
" the beginning of the suite)\n"); " 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); ck_abort_msg("m_addfriend did NOT catch the following length: %d\n", bad_len);
/* this should REALLY error */ /* this should REALLY error */
@ -152,10 +154,10 @@ START_TEST(test_setname)
int good_length = strlen(good_name); int good_length = strlen(good_name);
int bad_length = REALLY_BIG_NUMBER; 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", ck_abort_msg("setname() did NOT error on %d as a length argument!\n",
bad_length); 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"); ck_abort_msg("setname() did NOT return 0 on good arguments!\n");
} }
END_TEST END_TEST
@ -166,8 +168,8 @@ START_TEST(test_getself_name)
int len = strlen(nickname); int len = strlen(nickname);
char nick_check[len]; char nick_check[len];
setname((uint8_t *)nickname, len); setname(m, (uint8_t *)nickname, len);
getself_name((uint8_t *)nick_check); getself_name(m, (uint8_t *)nick_check);
ck_assert_msg((!STRINGS_EQUAL(nickname, nick_check)), ck_assert_msg((!STRINGS_EQUAL(nickname, nick_check)),
"getself_name failed to return the known name!\n" "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); good_id_b = hex_string_to_bin(good_id_b_str);
bad_id = hex_string_to_bin(bad_id_str); bad_id = hex_string_to_bin(bad_id_str);
m = initMessenger();
/* setup a default friend and friendnum */ /* 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" fputs("m_addfriend_norequest() failed on a valid ID!\n"
"this was CRITICAL to the test, and the build WILL fail.\n" "this was CRITICAL to the test, and the build WILL fail.\n"
"the tests will continue now...\n\n", stderr); "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" fputs("getfriend_id() failed on a valid ID!\n"
"this was CRITICAL to the test, and the build WILL fail.\n" "this was CRITICAL to the test, and the build WILL fail.\n"
"the tests will continue now...\n\n", stderr); "the tests will continue now...\n\n", stderr);
@ -276,5 +280,7 @@ int main(int argc, char *argv[])
free(good_id_b); free(good_id_b);
free(bad_id); free(bad_id);
cleanupMessenger(m);
return number_failed; return number_failed;
} }

View File

@ -24,40 +24,8 @@
#include "Messenger.h" #include "Messenger.h"
#define MIN(a,b) (((a)<(b))?(a):(b)) #define MIN(a,b) (((a)<(b))?(a):(b))
typedef struct { static void set_friend_status(Messenger *m, int friendnumber, uint8_t status);
uint8_t client_id[CLIENT_ID_SIZE]; static int write_cryptpacket_id(Messenger *m, int friendnumber, uint8_t packet_id, uint8_t *data, uint32_t length);
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);
/* 1 if we are online /* 1 if we are online
0 if we are offline 0 if we are offline
@ -65,24 +33,24 @@ static int write_cryptpacket_id(int friendnumber, uint8_t packet_id, uint8_t *da
/* set the size of the friend list to numfriends /* set the size of the friend list to numfriends
return -1 if realloc fails */ return -1 if realloc fails */
int realloc_friendlist(uint32_t num) { int realloc_friendlist(Messenger *m, uint32_t num) {
Friend *newfriendlist = realloc(friendlist, num*sizeof(Friend)); Friend *newfriendlist = realloc(m->friendlist, num*sizeof(Friend));
if (newfriendlist == NULL) if (newfriendlist == NULL)
return -1; return -1;
memset(&newfriendlist[num-1], 0, sizeof(Friend)); memset(&newfriendlist[num-1], 0, sizeof(Friend));
friendlist = newfriendlist; m->friendlist = newfriendlist;
return 0; return 0;
} }
/* return the friend id associated to that public key. /* return the friend id associated to that public key.
return -1 if no such friend */ 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; uint32_t i;
for (i = 0; i < numfriends; ++i) { for (i = 0; i < m->numfriends; ++i) {
if (friendlist[i].status > 0) if (m->friendlist[i].status > 0)
if (memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) if (memcmp(client_id, m->friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0)
return i; return i;
} }
@ -93,13 +61,13 @@ int getfriend_id(uint8_t *client_id)
make sure that client_id is of size CLIENT_ID_SIZE. make sure that client_id is of size CLIENT_ID_SIZE.
return 0 if success return 0 if success
return -1 if failure. */ 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; return -1;
if (friendlist[friend_id].status > 0) { if (m->friendlist[friend_id].status > 0) {
memcpy(client_id, friendlist[friend_id].client_id, CLIENT_ID_SIZE); memcpy(client_id, m->friendlist[friend_id].client_id, CLIENT_ID_SIZE);
return 0; return 0;
} }
@ -118,7 +86,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_ALREADYSENT if friend request already sent or already a friend
* return FAERR_UNKNOWN for unknown error * 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 if (length >= (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES
- crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES
@ -128,57 +96,57 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length)
return FAERR_NOMESSAGE; return FAERR_NOMESSAGE;
if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0)
return FAERR_OWNKEY; return FAERR_OWNKEY;
if (getfriend_id(client_id) != -1) if (getfriend_id(m, client_id) != -1)
return FAERR_ALREADYSENT; return FAERR_ALREADYSENT;
/* resize the friend list if necessary */ /* resize the friend list if necessary */
realloc_friendlist(numfriends + 1); realloc_friendlist(m, m->numfriends + 1);
uint32_t i; uint32_t i;
for (i = 0; i <= numfriends; ++i) { for (i = 0; i <= m->numfriends; ++i) {
if (friendlist[i].status == NOFRIEND) { if (m->friendlist[i].status == NOFRIEND) {
DHT_addfriend(client_id); DHT_addfriend(client_id);
friendlist[i].status = FRIEND_ADDED; m->friendlist[i].status = FRIEND_ADDED;
friendlist[i].crypt_connection_id = -1; m->friendlist[i].crypt_connection_id = -1;
friendlist[i].friend_request_id = -1; m->friendlist[i].friend_request_id = -1;
memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); memcpy(m->friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
friendlist[i].statusmessage = calloc(1, 1); m->friendlist[i].statusmessage = calloc(1, 1);
friendlist[i].statusmessage_length = 1; m->friendlist[i].statusmessage_length = 1;
friendlist[i].userstatus = USERSTATUS_NONE; m->friendlist[i].userstatus = USERSTATUS_NONE;
memcpy(friendlist[i].info, data, length); memcpy(m->friendlist[i].info, data, length);
friendlist[i].info_size = length; m->friendlist[i].info_size = length;
friendlist[i].message_id = 0; m->friendlist[i].message_id = 0;
friendlist[i].receives_read_receipts = 1; /* default: YES */ m->friendlist[i].receives_read_receipts = 1; /* default: YES */
++numfriends; ++ m->numfriends;
return i; return i;
} }
} }
return FAERR_UNKNOWN; 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; return -1;
/* resize the friend list if necessary */ /* resize the friend list if necessary */
realloc_friendlist(numfriends + 1); realloc_friendlist(m, m->numfriends + 1);
uint32_t i; uint32_t i;
for (i = 0; i <= numfriends; ++i) { for (i = 0; i <= m->numfriends; ++i) {
if(friendlist[i].status == NOFRIEND) { if(m->friendlist[i].status == NOFRIEND) {
DHT_addfriend(client_id); DHT_addfriend(client_id);
friendlist[i].status = FRIEND_REQUESTED; m->friendlist[i].status = FRIEND_REQUESTED;
friendlist[i].crypt_connection_id = -1; m->friendlist[i].crypt_connection_id = -1;
friendlist[i].friend_request_id = -1; m->friendlist[i].friend_request_id = -1;
memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); memcpy(m->friendlist[i].client_id, client_id, CLIENT_ID_SIZE);
friendlist[i].statusmessage = calloc(1, 1); m->friendlist[i].statusmessage = calloc(1, 1);
friendlist[i].statusmessage_length = 1; m->friendlist[i].statusmessage_length = 1;
friendlist[i].userstatus = USERSTATUS_NONE; m->friendlist[i].userstatus = USERSTATUS_NONE;
friendlist[i].message_id = 0; m->friendlist[i].message_id = 0;
friendlist[i].receives_read_receipts = 1; /* default: YES */ m->friendlist[i].receives_read_receipts = 1; /* default: YES */
++numfriends; ++ m->numfriends;
return i; return i;
} }
} }
@ -188,23 +156,23 @@ int m_addfriend_norequest(uint8_t * client_id)
/* remove a friend /* remove a friend
return 0 if success return 0 if success
return -1 if failure */ 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; return -1;
DHT_delfriend(friendlist[friendnumber].client_id); DHT_delfriend(m->friendlist[friendnumber].client_id);
crypto_kill(friendlist[friendnumber].crypt_connection_id); crypto_kill(m->friendlist[friendnumber].crypt_connection_id);
free(friendlist[friendnumber].statusmessage); free(m->friendlist[friendnumber].statusmessage);
memset(&friendlist[friendnumber], 0, sizeof(Friend)); memset(&(m->friendlist[friendnumber]), 0, sizeof(Friend));
uint32_t i; uint32_t i;
for (i = numfriends; i != 0; --i) { for (i = m->numfriends; i != 0; --i) {
if (friendlist[i-1].status != NOFRIEND) if (m->friendlist[i-1].status != NOFRIEND)
break; break;
} }
numfriends = i; m->numfriends = i;
realloc_friendlist(numfriends + 1); realloc_friendlist(m, m->numfriends + 1);
return 0; return 0;
} }
@ -214,31 +182,31 @@ int m_delfriend(int friendnumber)
return FRIEND_REQUESTED if the friend request was sent return FRIEND_REQUESTED if the friend request was sent
return FRIEND_ADDED if the friend was added return FRIEND_ADDED if the friend was added
return NOFRIEND if there is no friend with that number */ 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 NOFRIEND;
return friendlist[friendnumber].status; return m->friendlist[friendnumber].status;
} }
/* send a text chat message to an online friend /* send a text chat message to an online friend
return the message id if packet was successfully put into the send queue return the message id if packet was successfully put into the send queue
return 0 if it was not */ 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; return 0;
uint32_t msgid = ++friendlist[friendnumber].message_id; uint32_t msgid = ++m->friendlist[friendnumber].message_id;
if (msgid == 0) if (msgid == 0)
msgid = 1; /* otherwise, false error */ 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 msgid;
} }
return 0; 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))) if (length >= (MAX_DATA_SIZE - sizeof(theid)))
return 0; return 0;
@ -246,34 +214,34 @@ uint32_t m_sendmessage_withid(int friendnumber, uint32_t theid, uint8_t *message
theid = htonl(theid); theid = htonl(theid);
memcpy(temp, &theid, sizeof(theid)); memcpy(temp, &theid, sizeof(theid));
memcpy(temp + sizeof(theid), message, length); 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 /* send an action to an online friend
return 1 if packet was successfully put into the send queue return 1 if packet was successfully put into the send queue
return 0 if it was not */ 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 /* send a name packet to friendnumber
length is the length with the NULL terminator*/ 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) if(length > MAX_NAME_LENGTH || length == 0)
return 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 /* set the name of a friend
return 0 if success return 0 if success
return -1 if failure */ 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; return -1;
memcpy(friendlist[friendnumber].name, name, MAX_NAME_LENGTH); memcpy(m->friendlist[friendnumber].name, name, MAX_NAME_LENGTH);
return 0; return 0;
} }
@ -283,15 +251,15 @@ static int setfriendname(int friendnumber, uint8_t * name)
length is the length of name with the NULL terminator length is the length of name with the NULL terminator
return 0 if success return 0 if success
return -1 if failure */ 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) if (length > MAX_NAME_LENGTH || length == 0)
return -1; return -1;
memcpy(self_name, name, length); memcpy(m->name, name, length);
self_name_length = length; m->name_length = length;
uint32_t i; uint32_t i;
for (i = 0; i < numfriends; ++i) for (i = 0; i < m->numfriends; ++i)
friendlist[i].name_sent = 0; m->friendlist[i].name_sent = 0;
return 0; return 0;
} }
@ -299,10 +267,10 @@ int setname(uint8_t * name, uint16_t length)
put it in name put it in name
name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes. name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
return the length of the name */ 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); memcpy(name, m->name, m->name_length);
return self_name_length; return m->name_length;
} }
/* get name of friendnumber /* get name of friendnumber
@ -310,293 +278,288 @@ 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. name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH bytes.
return 0 if success return 0 if success
return -1 if failure */ 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; return -1;
memcpy(name, friendlist[friendnumber].name, MAX_NAME_LENGTH); memcpy(name, m->friendlist[friendnumber].name, MAX_NAME_LENGTH);
return 0; 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) if (length > MAX_STATUSMESSAGE_LENGTH)
return -1; return -1;
memcpy(self_statusmessage, status, length); memcpy(m->statusmessage, status, length);
self_statusmessage_length = length; m->statusmessage_length = length;
uint32_t i; uint32_t i;
for (i = 0; i < numfriends; ++i) for (i = 0; i < m->numfriends; ++i)
friendlist[i].statusmessage_sent = 0; m->friendlist[i].statusmessage_sent = 0;
return 0; return 0;
} }
int m_set_userstatus(USERSTATUS status) int m_set_userstatus(Messenger *m, USERSTATUS status)
{ {
if (status >= USERSTATUS_INVALID) { if (status >= USERSTATUS_INVALID) {
return -1; return -1;
} }
self_userstatus = status; m->userstatus = status;
uint32_t i; uint32_t i;
for (i = 0; i < numfriends; ++i) for (i = 0; i < m->numfriends; ++i)
friendlist[i].userstatus_sent = 0; m->friendlist[i].userstatus_sent = 0;
return 0; return 0;
} }
/* return the size of friendnumber's user status /* return the size of friendnumber's user status
guaranteed to be at most MAX_STATUSMESSAGE_LENGTH */ 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 -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 /* 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 */ 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; return -1;
memset(buf, 0, maxlen); 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; 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); 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; 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; return USERSTATUS_INVALID;
USERSTATUS status = friendlist[friendnumber].userstatus; USERSTATUS status = m->friendlist[friendnumber].userstatus;
if (status >= USERSTATUS_INVALID) { if (status >= USERSTATUS_INVALID) {
status = USERSTATUS_NONE; status = USERSTATUS_NONE;
} }
return status; 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; 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; return -1;
uint8_t *newstatus = calloc(length, 1); uint8_t *newstatus = calloc(length, 1);
memcpy(newstatus, status, length); memcpy(newstatus, status, length);
free(friendlist[friendnumber].statusmessage); free(m->friendlist[friendnumber].statusmessage);
friendlist[friendnumber].statusmessage = newstatus; m->friendlist[friendnumber].statusmessage = newstatus;
friendlist[friendnumber].statusmessage_length = length; m->friendlist[friendnumber].statusmessage_length = length;
return 0; 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. */ /* 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) if (yesno != 0 || yesno != 1)
return; return;
if (friendnumber >= numfriends || friendnumber < 0) if (friendnumber >= m->numfriends || friendnumber < 0)
return; 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 void (*friend_request)(uint8_t *, uint8_t *, uint16_t);
static uint8_t friend_request_isset = 0; */ static uint8_t friend_request_isset = 0; */
/* set the function that will be executed when a friend request is received. */ /* 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))
{ {
callback_friendrequest(function); callback_friendrequest(function);
} }
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. */ /* 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))
{ {
friend_message = function; m->friend_message = function;
friend_message_isset = 1; m->friend_message_isset = 1;
} }
static void (*friend_action)(int, uint8_t *, uint16_t); void m_callback_action(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t))
static uint8_t friend_action_isset = 0;
void m_callback_action(void (*function)(int, uint8_t *, uint16_t))
{ {
friend_action = function; m->friend_action = function;
friend_action_isset = 1; m->friend_action_isset = 1;
} }
static void (*friend_namechange)(int, uint8_t *, uint16_t); void m_callback_namechange(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t))
static uint8_t friend_namechange_isset = 0;
void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t))
{ {
friend_namechange = function; m->friend_namechange = function;
friend_namechange_isset = 1; m->friend_namechange_isset = 1;
} }
static void (*friend_statusmessagechange)(int, uint8_t *, uint16_t); void m_callback_statusmessage(Messenger *m, void (*function)(Messenger *m, int, uint8_t *, uint16_t))
static uint8_t friend_statusmessagechange_isset = 0;
void m_callback_statusmessage(void (*function)(int, uint8_t *, uint16_t))
{ {
friend_statusmessagechange = function; m->friend_statusmessagechange = function;
friend_statusmessagechange_isset = 1; m->friend_statusmessagechange_isset = 1;
} }
static void (*friend_userstatuschange)(int, USERSTATUS); void m_callback_userstatus(Messenger *m, void (*function)(Messenger *m, int, USERSTATUS))
static uint8_t friend_userstatuschange_isset = 0;
void m_callback_userstatus(void (*function)(int, USERSTATUS))
{ {
friend_userstatuschange = function; m->friend_userstatuschange = function;
friend_userstatuschange_isset = 1; m->friend_userstatuschange_isset = 1;
} }
static void (*read_receipt)(int, uint32_t); void m_callback_read_receipt(Messenger *m, void (*function)(Messenger *m, int, uint32_t))
static uint8_t read_receipt_isset = 0;
void m_callback_read_receipt(void (*function)(int, uint32_t))
{ {
read_receipt = function; m->read_receipt = function;
read_receipt_isset = 1; m->read_receipt_isset = 1;
} }
static void (*friend_connectionstatuschange)(int, uint8_t); void m_callback_connectionstatus(Messenger *m, void (*function)(Messenger *m, int, uint8_t))
static uint8_t friend_connectionstatuschange_isset = 0;
void m_callback_connectionstatus(void (*function)(int, uint8_t))
{ {
friend_connectionstatuschange = function; m->friend_connectionstatuschange = function;
friend_connectionstatuschange_isset = 1; m->friend_connectionstatuschange_isset = 1;
} }
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; return;
if (status == NOFRIEND) if (status == NOFRIEND)
return; 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; const uint8_t is_connected = status == FRIEND_ONLINE;
if (is_connected != was_connected) if (is_connected != was_connected)
friend_connectionstatuschange(friendnumber, is_connected); m->friend_connectionstatuschange(m, friendnumber, is_connected);
} }
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); check_friend_connectionstatus(m, friendnumber, status);
friendlist[friendnumber].status = 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; 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; return 0;
uint8_t packet[length + 1]; uint8_t packet[length + 1];
packet[0] = packet_id; packet[0] = packet_id;
memcpy(packet + 1, data, length); 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);
} }
#define PORT 33445 #define PORT 33445
/* run this at startup */ /* run this at startup */
int initMessenger(void) Messenger * initMessenger(void)
{ {
Messenger *m = calloc(1, sizeof(Messenger));
if( ! m )
return 0;
new_keys(); new_keys();
m_set_statusmessage((uint8_t*)"Online", sizeof("Online")); m_set_statusmessage(m, (uint8_t*)"Online", sizeof("Online"));
initNetCrypto(); initNetCrypto();
IP ip; IP ip;
ip.i = 0; ip.i = 0;
if(init_networking(ip,PORT) == -1) if(init_networking(ip,PORT) == -1)
return -1; return 0;
DHT_init(); DHT_init();
LosslessUDP_init(); LosslessUDP_init();
friendreq_init(); friendreq_init();
LANdiscovery_init(); LANdiscovery_init();
return 0; 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);
} }
//TODO: make this function not suck. //TODO: make this function not suck.
static void doFriends(void) void doFriends(Messenger *m)
{ {
/* TODO: add incoming connections and some other stuff. */ /* TODO: add incoming connections and some other stuff. */
uint32_t i; uint32_t i;
int len; int len;
uint8_t temp[MAX_DATA_SIZE]; uint8_t temp[MAX_DATA_SIZE];
for (i = 0; i < numfriends; ++i) { for (i = 0; i < m->numfriends; ++i) {
if (friendlist[i].status == FRIEND_ADDED) { if (m->friendlist[i].status == FRIEND_ADDED) {
int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); 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 */ 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) 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 (m->friendlist[i].status == FRIEND_REQUESTED || m->friendlist[i].status == FRIEND_CONFIRMED) { /* friend is not online */
if (friendlist[i].status == FRIEND_REQUESTED) { if (m->friendlist[i].status == FRIEND_REQUESTED) {
if (friendlist[i].friend_request_id + 10 < unix_time()) { /*I know this is hackish but it should work.*/ if (m->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); send_friendrequest(m->friendlist[i].client_id, m->friendlist[i].info, m->friendlist[i].info_size);
friendlist[i].friend_request_id = unix_time(); m->friendlist[i].friend_request_id = unix_time();
} }
} }
IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); IP_Port friendip = DHT_getfriendip(m->friendlist[i].client_id);
switch (is_cryptoconnected(friendlist[i].crypt_connection_id)) { switch (is_cryptoconnected(m->friendlist[i].crypt_connection_id)) {
case 0: case 0:
if (friendip.ip.i > 1) 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; break;
case 3: /* Connection is established */ case 3: /* Connection is established */
set_friend_status(i, FRIEND_ONLINE); set_friend_status(m, i, FRIEND_ONLINE);
friendlist[i].name_sent = 0; m->friendlist[i].name_sent = 0;
friendlist[i].userstatus_sent = 0; m->friendlist[i].userstatus_sent = 0;
friendlist[i].statusmessage_sent = 0; m->friendlist[i].statusmessage_sent = 0;
break; break;
case 4: case 4:
crypto_kill(friendlist[i].crypt_connection_id); crypto_kill(m->friendlist[i].crypt_connection_id);
friendlist[i].crypt_connection_id = -1; m->friendlist[i].crypt_connection_id = -1;
break; break;
default: default:
break; break;
} }
} }
while (friendlist[i].status == FRIEND_ONLINE) { /* friend is online */ while (m->friendlist[i].status == FRIEND_ONLINE) { /* friend is online */
if (friendlist[i].name_sent == 0) { if (m->friendlist[i].name_sent == 0) {
if (m_sendname(i, self_name, self_name_length)) if (m_sendname(m, i, m->name, m->name_length))
friendlist[i].name_sent = 1; m->friendlist[i].name_sent = 1;
} }
if (friendlist[i].statusmessage_sent == 0) { if (m->friendlist[i].statusmessage_sent == 0) {
if (send_statusmessage(i, self_statusmessage, self_statusmessage_length)) if (send_statusmessage(m, i, m->statusmessage, m->statusmessage_length))
friendlist[i].statusmessage_sent = 1; m->friendlist[i].statusmessage_sent = 1;
} }
if (friendlist[i].userstatus_sent == 0) { if (m->friendlist[i].userstatus_sent == 0) {
if (send_userstatus(i, self_userstatus)) if (send_userstatus(m, i, m->userstatus))
friendlist[i].userstatus_sent = 1; 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 packet_id = temp[0];
uint8_t* data = temp + 1; uint8_t* data = temp + 1;
int data_length = len - 1; int data_length = len - 1;
@ -605,10 +568,10 @@ static void doFriends(void)
case PACKET_ID_NICKNAME: { case PACKET_ID_NICKNAME: {
if (data_length >= MAX_NAME_LENGTH || data_length == 0) if (data_length >= MAX_NAME_LENGTH || data_length == 0)
break; break;
if(friend_namechange_isset) if(m->friend_namechange_isset)
friend_namechange(i, data, data_length); m->friend_namechange(m, i, data, data_length);
memcpy(friendlist[i].name, data, data_length); memcpy(m->friendlist[i].name, data, data_length);
friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */ m->friendlist[i].name[data_length - 1] = 0; /* make sure the NULL terminator is present. */
break; break;
} }
case PACKET_ID_STATUSMESSAGE: { case PACKET_ID_STATUSMESSAGE: {
@ -616,9 +579,9 @@ static void doFriends(void)
break; break;
uint8_t *status = calloc(MIN(data_length, MAX_STATUSMESSAGE_LENGTH), 1); uint8_t *status = calloc(MIN(data_length, MAX_STATUSMESSAGE_LENGTH), 1);
memcpy(status, data, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); memcpy(status, data, MIN(data_length, MAX_STATUSMESSAGE_LENGTH));
if (friend_statusmessagechange_isset) if (m->friend_statusmessagechange_isset)
friend_statusmessagechange(i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); m->friend_statusmessagechange(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH));
set_friend_statusmessage(i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH)); set_friend_statusmessage(m, i, status, MIN(data_length, MAX_STATUSMESSAGE_LENGTH));
free(status); free(status);
break; break;
} }
@ -626,9 +589,9 @@ static void doFriends(void)
if (data_length != 1) if (data_length != 1)
break; break;
USERSTATUS status = data[0]; USERSTATUS status = data[0];
if (friend_userstatuschange_isset) if (m->friend_userstatuschange_isset)
friend_userstatuschange(i, status); m->friend_userstatuschange(m, i, status);
set_friend_userstatus(i, status); set_friend_userstatus(m, i, status);
break; break;
} }
case PACKET_ID_MESSAGE: { case PACKET_ID_MESSAGE: {
@ -636,16 +599,16 @@ static void doFriends(void)
uint8_t message_id_length = 4; uint8_t message_id_length = 4;
uint8_t *message = data + message_id_length; uint8_t *message = data + message_id_length;
uint16_t message_length = data_length - message_id_length; uint16_t message_length = data_length - message_id_length;
if (friendlist[i].receives_read_receipts) { if (m->friendlist[i].receives_read_receipts) {
write_cryptpacket_id(i, PACKET_ID_RECEIPT, message_id, message_id_length); write_cryptpacket_id(m, i, PACKET_ID_RECEIPT, message_id, message_id_length);
} }
if (friend_message_isset) if (m->friend_message_isset)
(*friend_message)(i, message, message_length); (*m->friend_message)(m, i, message, message_length);
break; break;
} }
case PACKET_ID_ACTION: { case PACKET_ID_ACTION: {
if (friend_action_isset) if (m->friend_action_isset)
(*friend_action)(i, data, data_length); (*m->friend_action)(m, i, data, data_length);
break; break;
} }
case PACKET_ID_RECEIPT: { case PACKET_ID_RECEIPT: {
@ -654,16 +617,16 @@ static void doFriends(void)
break; break;
memcpy(&msgid, data, sizeof(msgid)); memcpy(&msgid, data, sizeof(msgid));
msgid = ntohl(msgid); msgid = ntohl(msgid);
if (read_receipt_isset) if (m->read_receipt_isset)
(*read_receipt)(i, msgid); (*m->read_receipt)(m, i, msgid);
break; break;
} }
} }
} else { } else {
if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ if (is_cryptoconnected(m->friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */
crypto_kill(friendlist[i].crypt_connection_id); crypto_kill(m->friendlist[i].crypt_connection_id);
friendlist[i].crypt_connection_id = -1; m->friendlist[i].crypt_connection_id = -1;
set_friend_status(i, FRIEND_CONFIRMED); set_friend_status(m, i, FRIEND_CONFIRMED);
} }
break; break;
} }
@ -671,20 +634,20 @@ static void doFriends(void)
} }
} }
static void doInbound(void) void doInbound(Messenger *m)
{ {
uint8_t secret_nonce[crypto_box_NONCEBYTES]; uint8_t secret_nonce[crypto_box_NONCEBYTES];
uint8_t public_key[crypto_box_PUBLICKEYBYTES]; uint8_t public_key[crypto_box_PUBLICKEYBYTES];
uint8_t session_key[crypto_box_PUBLICKEYBYTES]; uint8_t session_key[crypto_box_PUBLICKEYBYTES];
int inconnection = crypto_inbound(public_key, secret_nonce, session_key); int inconnection = crypto_inbound(public_key, secret_nonce, session_key);
if (inconnection != -1) { if (inconnection != -1) {
int friend_id = getfriend_id(public_key); int friend_id = getfriend_id(m, public_key);
if (friend_id != -1) { if (friend_id != -1) {
crypto_kill(friendlist[friend_id].crypt_connection_id); crypto_kill(m->friendlist[friend_id].crypt_connection_id);
friendlist[friend_id].crypt_connection_id = m->friendlist[friend_id].crypt_connection_id =
accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); 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);
} }
} }
} }
@ -695,7 +658,7 @@ static void doInbound(void)
static uint64_t last_LANdiscovery; static uint64_t last_LANdiscovery;
/*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/ /*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/
static void LANdiscovery(void) void LANdiscovery(Messenger *m)
{ {
if (last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { if (last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) {
send_LANdiscovery(htons(PORT)); send_LANdiscovery(htons(PORT));
@ -705,27 +668,27 @@ static void LANdiscovery(void)
/* the main loop that needs to be run at least 200 times per second. */ /* the main loop that needs to be run at least 200 times per second. */
void doMessenger(void) void doMessenger(Messenger *m)
{ {
networking_poll(); networking_poll();
doDHT(); doDHT();
doLossless_UDP(); doLossless_UDP();
doNetCrypto(); doNetCrypto();
doInbound(); doInbound(m);
doFriends(); doFriends(m);
LANdiscovery(); LANdiscovery(m);
} }
/* returns the size of the messenger data (for saving) */ /* 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 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() */ /* 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); save_keys(data);
data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
@ -734,14 +697,14 @@ void Messenger_save(uint8_t *data)
data += sizeof(size); data += sizeof(size);
DHT_save(data); DHT_save(data);
data += size; data += size;
size = sizeof(Friend) * numfriends; size = sizeof(Friend) * m->numfriends;
memcpy(data, &size, sizeof(size)); memcpy(data, &size, sizeof(size));
data += 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. */ /* 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) if (length == ~0)
return -1; return -1;
@ -773,11 +736,12 @@ int Messenger_load(uint8_t * data, uint32_t length)
uint32_t i; uint32_t i;
for (i = 0; i < num; ++i) { for (i = 0; i < num; ++i) {
if(temp[i].status != 0) { if(temp[i].status != 0) {
int fnum = m_addfriend_norequest(temp[i].client_id); int fnum = m_addfriend_norequest(m, temp[i].client_id);
setfriendname(fnum, temp[i].name); setfriendname(m, fnum, temp[i].name);
/* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */
} }
} }
free(temp); free(temp);
return 0; return 0;
} }

View File

@ -73,6 +73,58 @@ typedef enum {
USERSTATUS_INVALID USERSTATUS_INVALID
} USERSTATUS; } 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);
uint8_t friend_message_isset;
void (*friend_action)(struct Messenger *m, int, uint8_t *, uint16_t);
uint8_t friend_action_isset;
void (*friend_namechange)(struct Messenger *m, int, uint8_t *, uint16_t);
uint8_t friend_namechange_isset;
void (*friend_statusmessagechange)(struct Messenger *m, int, uint8_t *, uint16_t);
uint8_t friend_statusmessagechange_isset;
void (*friend_userstatuschange)(struct Messenger *m, int, USERSTATUS);
uint8_t friend_userstatuschange_isset;
void (*read_receipt)(struct Messenger *m, int, uint32_t);
uint8_t read_receipt_isset;
void (*friend_statuschange)(struct Messenger *m, int, uint8_t);
uint8_t friend_statuschange_isset;
void (*friend_connectionstatuschange)(struct Messenger *m, int, uint8_t);
uint8_t friend_connectionstatuschange_isset;
} Messenger;
/* /*
* add a friend * add a friend
* set the data that will be sent along with friend request * set the data that will be sent along with friend request
@ -85,33 +137,33 @@ typedef enum {
* return -4 if friend request already sent or already a friend * return -4 if friend request already sent or already a friend
* return -5 for unknown error * 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. /* add a friend without sending a friendrequest.
returns the friend number if success returns the friend number if success
return -1 if failure. */ 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 the friend id associated to that client id.
return -1 if no such friend */ 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. /* copies the public key associated to that friend id into client_id buffer.
make sure that client_id is of size CLIENT_ID_SIZE. make sure that client_id is of size CLIENT_ID_SIZE.
return 0 if success return 0 if success
return -1 if failure */ 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 */ /* remove a friend */
int m_delfriend(int friendnumber); int m_delfriend(Messenger *m, int friendnumber);
/* return 4 if friend is online /* return 4 if friend is online
return 3 if friend is confirmed return 3 if friend is confirmed
return 2 if the friend request was sent return 2 if the friend request was sent
return 1 if the friend was added return 1 if the friend was added
return 0 if there is no friend with that number */ 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 /* send a text chat message to an online friend
returns the message id if packet was successfully put into the send queue returns the message id if packet was successfully put into the send queue
@ -120,13 +172,13 @@ int m_friendstatus(int friendnumber);
if one is received. if one is received.
m_sendmessage_withid will send a message with the id of your choosing, 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. */ 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(Messenger *m, 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_withid(Messenger *m, int friendnumber, uint32_t theid, uint8_t *message, uint32_t length);
/* send an action to an online friend /* send an action to an online friend
returns 1 if packet was successfully put into the send queue returns 1 if packet was successfully put into the send queue
return 0 if it was not */ 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 /* Set our nickname
name must be a string of maximum MAX_NAME_LENGTH length. name must be a string of maximum MAX_NAME_LENGTH length.
@ -134,73 +186,73 @@ int m_sendaction(int friendnumber, uint8_t *action, uint32_t length);
length is the length of name with the NULL terminator length is the length of name with the NULL terminator
return 0 if success return 0 if success
return -1 if failure */ 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 /* get our nickname
put it in name put it in name
return the length of the 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 /* get name of friendnumber
put it in name put it in name
name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes.
return 0 if success return 0 if success
return -1 if failure */ return -1 if failure */
int getname(int friendnumber, uint8_t *name); int getname(Messenger *m, int friendnumber, uint8_t *name);
/* set our user status /* set our user status
you are responsible for freeing status after you are responsible for freeing status after
returns 0 on success, -1 on failure */ returns 0 on success, -1 on failure */
int m_set_statusmessage(uint8_t *status, uint16_t length); int m_set_statusmessage(Messenger *m, uint8_t *status, uint16_t length);
int m_set_userstatus(USERSTATUS status); int m_set_userstatus(Messenger *m, USERSTATUS status);
/* return the length of friendnumber's status message, /* return the length of friendnumber's status message,
including null including null
pass it into malloc */ 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 /* 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 get the size you need to allocate from m_get_statusmessage_size
The self variant will copy our own status message. */ The self variant will copy our own status message. */
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);
int m_copy_self_statusmessage(uint8_t *buf, uint32_t maxlen); int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen);
/* Return one of USERSTATUS values. /* Return one of USERSTATUS values.
* Values unknown to your application should be represented as USERSTATUS_NONE. * Values unknown to your application should be represented as USERSTATUS_NONE.
* As above, the self variant will return our own USERSTATUS. * As above, the self variant will return our own USERSTATUS.
* If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */
USERSTATUS m_get_userstatus(int friendnumber); USERSTATUS m_get_userstatus(Messenger *m, int friendnumber);
USERSTATUS m_get_self_userstatus(void); USERSTATUS m_get_self_userstatus(Messenger *m);
/* Sets whether we send read receipts for friendnumber. /* Sets whether we send read receipts for friendnumber.
* This function is not lazy, and it will fail if yesno is not (0 or 1).*/ * 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. /* 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) */ 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));
/* set the function that will be executed when a message from a friend is received. /* 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) */ 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));
/* set the function that will be executed when an action from a friend is received. /* 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) */ 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));
/* set the callback for name changes /* set the callback for name changes
function(int friendnumber, uint8_t *newname, uint16_t length) function(int friendnumber, uint8_t *newname, uint16_t length)
you are not responsible for freeing newname */ 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));
/* set the callback for status message changes /* set the callback for status message changes
function(int friendnumber, uint8_t *newstatus, uint16_t length) function(int friendnumber, uint8_t *newstatus, uint16_t length)
you are not responsible for freeing newstatus */ 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));
/* set the callback for status type changes /* set the callback for status type changes
function(int friendnumber, USERSTATUS kind) */ 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));
/* set the callback for read receipts /* set the callback for read receipts
function(int friendnumber, uint32_t receipt) function(int friendnumber, uint32_t receipt)
@ -209,7 +261,7 @@ void m_callback_userstatus(void (*function)(int, USERSTATUS));
has been received on the other side. since core doesn't has been received on the other side. since core doesn't
track ids for you, receipt may not correspond to any message track ids for you, receipt may not correspond to any message
in that case, you should discard it. */ 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));
/* set the callback for connection status changes /* set the callback for connection status changes
function(int friendnumber, uint8_t status) function(int friendnumber, uint8_t status)
@ -219,26 +271,30 @@ void m_callback_read_receipt(void (*function)(int, uint32_t));
note that this callback is not called when adding friends, thus the "after note that this callback is not called when adding friends, thus the "after
being previously online" part. it's assumed that when adding friends, being previously online" part. it's assumed that when adding friends,
their connection status is offline. */ 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));
/* run this at startup /* run this at startup
returns 0 if no connection problems * returns allocated instance of Messenger on success
returns -1 if there are problems */ * returns 0 if there are problems */
int initMessenger(void); 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 */ /* 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: */ /* SAVING AND LOADING FUNCTIONS: */
/* returns the size of the messenger data (for saving) */ /* 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()) */ /* 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 */ /* 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 #ifdef __cplusplus
} }

View File

@ -51,6 +51,10 @@
#endif #endif
/* 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 print_request(uint8_t * public_key, uint8_t * data, uint16_t length)
{ {
printf("Friend request received from: \n"); printf("Friend request received from: \n");
@ -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. //if the request contained the message of peace the person is obviously a friend so we add him.
{ {
printf("Friend request accepted.\n"); 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)
{ {
printf("Message with length %u received from %u: %s \n", length, friendnumber, string); 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[]) 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]); 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); exit(0);
} }
initMessenger();
m = initMessenger();
if( !m ){
fputs("Failed to allocate messenger datastructure\n", stderr);
exit(0);
}
if(argc > 3) { if(argc > 3) {
IP_Port bootstrap_ip_port; IP_Port bootstrap_ip_port;
bootstrap_ip_port.port = htons(atoi(argv[2])); bootstrap_ip_port.port = htons(atoi(argv[2]));
@ -100,12 +110,12 @@ int main(int argc, char *argv[])
int read; int read;
uint8_t buffer[128000]; uint8_t buffer[128000];
read = fread(buffer, 1, 128000, file); 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); fclose(file);
} }
m_callback_friendrequest(print_request); m_callback_friendrequest(m, print_request);
m_callback_friendmessage(print_message); m_callback_friendmessage(m, print_message);
printf("OUR ID: "); printf("OUR ID: ");
uint32_t i; uint32_t i;
@ -115,32 +125,33 @@ int main(int argc, char *argv[])
printf("%hhX",self_public_key[i]); printf("%hhX",self_public_key[i]);
} }
setname((uint8_t *)"Anon", 5); setname(m, (uint8_t *)"Anon", 5);
char temp_id[128]; char temp_id[128];
printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n"); printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n");
if(scanf("%s", temp_id) != 1) { if(scanf("%s", temp_id) != 1) {
return 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"); perror("Initialization");
while(1) { while(1) {
uint8_t name[128]; uint8_t name[128];
getname(num, name); getname(m, num, name);
printf("%s\n", name); printf("%s\n", name);
m_sendmessage(num, (uint8_t*)"Test", 5); m_sendmessage(m, num, (uint8_t*)"Test", 5);
doMessenger(); doMessenger(m);
c_sleep(30); c_sleep(30);
FILE *file = fopen("Save.bak", "wb"); FILE *file = fopen("Save.bak", "wb");
if ( file==NULL ){return 1;} if ( file==NULL ){return 1;}
uint8_t * buffer = malloc(Messenger_size()); uint8_t * buffer = malloc(Messenger_size(m));
Messenger_save(buffer); Messenger_save(m, buffer);
size_t write_result = fwrite(buffer, 1, Messenger_size(), file); size_t write_result = fwrite(buffer, 1, Messenger_size(m), file);
if (write_result < Messenger_size()) {return 1;} if (write_result < Messenger_size(m)) {return 1;}
free(buffer); free(buffer);
fclose(file); fclose(file);
} }
cleanupMessenger(m);
} }

View File

@ -85,12 +85,12 @@ void new_lines(char *line)
} }
void print_friendlist() void print_friendlist(Messenger *m)
{ {
char name[MAX_NAME_LENGTH]; char name[MAX_NAME_LENGTH];
int i = 0; int i = 0;
new_lines("[i] Friend List:"); 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 */ /* account for the longest name and the longest "base" string */
char fstring[MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")]; char fstring[MAX_NAME_LENGTH + strlen("[i] Friend: NULL\n\tid: ")];
@ -107,13 +107,13 @@ void print_friendlist()
new_lines("\tno friends! D:"); 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]; char name[MAX_NAME_LENGTH];
if (friendnum != -1) { if (friendnum != -1) {
getname(friendnum, (uint8_t*)name); getname(m, friendnum, (uint8_t*)name);
} else { } else {
getself_name((uint8_t*)name); getself_name(m, (uint8_t*)name);
} }
char *msg = malloc(100+strlen(message)+strlen(name)+1); char *msg = malloc(100+strlen(message)+strlen(name)+1);
@ -133,7 +133,7 @@ char *format_message(char *message, int friendnum)
return msg; return msg;
} }
void line_eval(char *line) void line_eval(Messenger *m, char *line)
{ {
if (line[0] == '/') { if (line[0] == '/') {
char inpt_command = line[1]; char inpt_command = line[1];
@ -148,7 +148,7 @@ void line_eval(char *line)
temp_id[i] = line[i+prompt_offset]; temp_id[i] = line[i+prompt_offset];
unsigned char *bin_string = hex_string_to_bin(temp_id); 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); free(bin_string);
char numstring[100]; char numstring[100];
switch (num) { switch (num) {
@ -175,7 +175,7 @@ void line_eval(char *line)
do_refresh(); do_refresh();
} }
else if (inpt_command == 'd') { else if (inpt_command == 'd') {
doMessenger(); doMessenger(m);
} }
else if (inpt_command == 'm') { //message command: /m friendnumber messsage else if (inpt_command == 'm') { //message command: /m friendnumber messsage
size_t len = strlen(line); size_t len = strlen(line);
@ -196,10 +196,10 @@ void line_eval(char *line)
} }
} }
int num = atoi(numstring); 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"); new_lines("[i] could not send message");
} else { } else {
new_lines(format_message(message, -1)); new_lines(format_message(m, message, -1));
} }
} }
else if (inpt_command == 'n') { else if (inpt_command == 'n') {
@ -211,13 +211,13 @@ void line_eval(char *line)
name[i-3] = line[i]; name[i-3] = line[i];
} }
name[i-3] = 0; name[i-3] = 0;
setname(name, i - 2); setname(m, name, i - 2);
char numstring[100]; char numstring[100];
sprintf(numstring, "[i] changed nick to %s", (char*)name); sprintf(numstring, "[i] changed nick to %s", (char*)name);
new_lines(numstring); new_lines(numstring);
} }
else if (inpt_command == 'l') { else if (inpt_command == 'l') {
print_friendlist(); print_friendlist(m);
} }
else if (inpt_command == 's') { else if (inpt_command == 's') {
uint8_t status[MAX_STATUSMESSAGE_LENGTH]; uint8_t status[MAX_STATUSMESSAGE_LENGTH];
@ -228,7 +228,7 @@ void line_eval(char *line)
status[i-3] = line[i]; status[i-3] = line[i];
} }
status[i-3] = 0; status[i-3] = 0;
m_set_statusmessage(status, strlen((char*)status) + 1); m_set_statusmessage(m, status, strlen((char*)status) + 1);
char numstring[100]; char numstring[100];
sprintf(numstring, "[i] changed status to %s", (char*)status); sprintf(numstring, "[i] changed status to %s", (char*)status);
new_lines(numstring); 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"); sprintf(numchar,"[i] you either didn't receive that request or you already accepted it");
new_lines(numchar); new_lines(numchar);
} else { } else {
int num = m_addfriend_norequest(pending_requests[numf].id); int num = m_addfriend_norequest(m, pending_requests[numf].id);
if (num != -1) { if (num != -1) {
pending_requests[numf].accepted = 1; pending_requests[numf].accepted = 1;
sprintf(numchar, "[i] friend request %u accepted", numf); sprintf(numchar, "[i] friend request %u accepted", numf);
@ -349,32 +349,32 @@ void print_request(uint8_t *public_key, uint8_t *data, uint16_t length)
do_refresh(); 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)
{ {
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)
{ {
char name[MAX_NAME_LENGTH]; char name[MAX_NAME_LENGTH];
if(getname(friendnumber, (uint8_t*)name) != -1) { if(getname(m, friendnumber, (uint8_t*)name) != -1) {
char msg[100+length]; char msg[100+length];
sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string);
new_lines(msg); 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)
{ {
char name[MAX_NAME_LENGTH]; 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]; char msg[100+length+strlen(name)+1];
sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string);
new_lines(msg); new_lines(msg);
} }
} }
void load_key(char *path) void load_key(Messenger *m, char *path)
{ {
FILE *data_file = fopen(path, "r"); FILE *data_file = fopen(path, "r");
int size = 0; int size = 0;
@ -390,13 +390,13 @@ void load_key(char *path)
fputs("[!] could not read data file! exiting...\n", stderr); fputs("[!] could not read data file! exiting...\n", stderr);
goto FILE_ERROR; goto FILE_ERROR;
} }
Messenger_load(data, size); Messenger_load(m, data, size);
} else { } else {
//else save new keys //else save new keys
int size = Messenger_size(); int size = Messenger_size(m);
uint8_t data[size]; uint8_t data[size];
Messenger_save(data); Messenger_save(m, data);
data_file = fopen(path, "w"); data_file = fopen(path, "w");
if(!data_file) { if(!data_file) {
@ -435,6 +435,7 @@ int main(int argc, char *argv[])
int i = 0; int i = 0;
char *filename = "data"; char *filename = "data";
char idstring[200] = {0}; char idstring[200] = {0};
Messenger *m;
if (argc < 4) { if (argc < 4) {
printf("[!] Usage: %s [IP] [port] [public_key] <keyfile>\n", argv[0]); printf("[!] Usage: %s [IP] [port] [public_key] <keyfile>\n", argv[0]);
@ -458,13 +459,18 @@ int main(int argc, char *argv[])
} }
} }
initMessenger(); m = initMessenger();
load_key(filename); if( !m ){
fputs("Failed to allocate Messenger datastructure", stderr);
exit(0);
}
m_callback_friendrequest(print_request); load_key(m, filename);
m_callback_friendmessage(print_message);
m_callback_namechange(print_nickchange); m_callback_friendrequest(m, print_request);
m_callback_statusmessage(print_statuschange); m_callback_friendmessage(m, print_message);
m_callback_namechange(m, print_nickchange);
m_callback_statusmessage(m, print_statuschange);
initscr(); initscr();
noecho(); noecho();
@ -494,7 +500,7 @@ int main(int argc, char *argv[])
on = 1; on = 1;
} }
doMessenger(); doMessenger(m);
c_sleep(1); c_sleep(1);
do_refresh(); do_refresh();
@ -504,7 +510,7 @@ int main(int argc, char *argv[])
getmaxyx(stdscr, y, x); getmaxyx(stdscr, y, x);
if (c == '\n') { if (c == '\n') {
line_eval(line); line_eval(m, line);
strcpy(line, ""); strcpy(line, "");
} else if (c == 8 || c == 127) { } else if (c == 8 || c == 127) {
line[strlen(line)-1] = '\0'; line[strlen(line)-1] = '\0';
@ -512,6 +518,7 @@ int main(int argc, char *argv[])
strcpy(line, appender(line, (char) c)); strcpy(line, appender(line, (char) c));
} }
} }
cleanupMessenger(m);
endwin(); endwin();
return 0; return 0;
} }

View File

@ -43,7 +43,7 @@
#define PUB_KEY_BYTES 32 #define PUB_KEY_BYTES 32
void new_lines(char *line); 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) ; void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) ;
int count_lines(char *string) ; int count_lines(char *string) ;
char *appender(char *str, const char c); char *appender(char *str, const char c);

View File

@ -29,9 +29,9 @@ extern int active_window;
extern void del_window(ToxWindow *w, int f_num); extern void del_window(ToxWindow *w, int f_num);
extern void fix_name(uint8_t *name); extern void fix_name(uint8_t *name);
void print_help(ChatContext *self); 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; ChatContext *ctx = (ChatContext*) self->x;
uint8_t nick[MAX_NAME_LENGTH] = {0}; 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) if (ctx->friendnum != num)
return; return;
getname(num, (uint8_t*) &nick); getname(m, num, (uint8_t*) &nick);
msg[len-1] = '\0'; msg[len-1] = '\0';
nick[MAX_NAME_LENGTH-1] = '\0'; nick[MAX_NAME_LENGTH-1] = '\0';
fix_name(msg); fix_name(msg);
@ -61,7 +61,7 @@ static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len)
beep(); 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; ChatContext *ctx = (ChatContext*) self->x;
time_t now; time_t now;
@ -117,7 +117,7 @@ int string_is_empty(char *string)
return rc; 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; ChatContext *ctx = (ChatContext*) self->x;
time_t now; time_t now;
@ -155,7 +155,7 @@ static void chat_onKey(ToxWindow *self, int key)
wmove(self->window, y2-CURS_Y_OFFSET, 0); wmove(self->window, y2-CURS_Y_OFFSET, 0);
wclrtobot(self->window); wclrtobot(self->window);
if (ctx->line[0] == '/') if (ctx->line[0] == '/')
execute(self, ctx, ctx->line, timeinfo); execute(self, ctx, m, ctx->line, timeinfo);
else { else {
if (!string_is_empty(ctx->line)) { if (!string_is_empty(ctx->line)) {
/* make sure the string has at least non-space character */ /* 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)); wattroff(ctx->history, COLOR_PAIR(1));
wprintw(ctx->history, "%s\n", ctx->line); 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)); wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, " * Failed to send message.\n"); wprintw(ctx->history, " * Failed to send message.\n");
wattroff(ctx->history, COLOR_PAIR(3)); 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")) { if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) {
wclear(self->window); wclear(self->window);
@ -210,14 +210,14 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo)
wattroff(ctx->history, COLOR_PAIR(2)); wattroff(ctx->history, COLOR_PAIR(2));
uint8_t selfname[MAX_NAME_LENGTH]; uint8_t selfname[MAX_NAME_LENGTH];
int len = getself_name(selfname); int len = getself_name(m, selfname);
char msg[MAX_STR_SIZE-len-4]; char msg[MAX_STR_SIZE-len-4];
snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t*) selfname, action); snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t*) selfname, action);
wattron(ctx->history, COLOR_PAIR(1)); wattron(ctx->history, COLOR_PAIR(1));
wprintw(ctx->history, msg); wprintw(ctx->history, msg);
wattroff(ctx->history, COLOR_PAIR(1)); 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)); wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, " * Failed to send action\n"); wprintw(ctx->history, " * Failed to send action\n");
wattroff(ctx->history, COLOR_PAIR(3)); wattroff(ctx->history, COLOR_PAIR(3));
@ -256,13 +256,13 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo)
msg = strchr(status, ' '); msg = strchr(status, ' ');
if (msg == NULL) { if (msg == NULL) {
m_set_userstatus(status_kind); m_set_userstatus(m, status_kind);
wprintw(ctx->history, "Status set to: %s\n", status_text); wprintw(ctx->history, "Status set to: %s\n", status_text);
} }
else { else {
msg++; msg++;
m_set_userstatus(status_kind); m_set_userstatus(m, status_kind);
m_set_statusmessage((uint8_t*) msg, strlen(msg)+1); m_set_statusmessage(m, ( uint8_t*) msg, strlen(msg)+1);
wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg); 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; return;
} }
nick++; 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); wprintw(ctx->history, "Nickname set to: %s\n", nick);
} }
@ -312,7 +312,7 @@ static void chat_onDraw(ToxWindow *self)
wrefresh(self->window); wrefresh(self->window);
} }
static void chat_onInit(ToxWindow *self) static void chat_onInit(ToxWindow *self, Messenger *m)
{ {
int x, y; int x, y;
ChatContext *ctx = (ChatContext*) self->x; ChatContext *ctx = (ChatContext*) self->x;
@ -342,7 +342,7 @@ void print_help(ChatContext *self)
wattroff(self->history, COLOR_PAIR(2)); wattroff(self->history, COLOR_PAIR(2));
} }
ToxWindow new_chat(int friendnum) ToxWindow new_chat(Messenger *m, int friendnum)
{ {
ToxWindow ret; ToxWindow ret;
memset(&ret, 0, sizeof(ret)); memset(&ret, 0, sizeof(ret));
@ -356,7 +356,7 @@ ToxWindow new_chat(int friendnum)
ret.onAction = &chat_onAction; ret.onAction = &chat_onAction;
uint8_t nick[MAX_NAME_LENGTH] = {0}; uint8_t nick[MAX_NAME_LENGTH] = {0};
getname(friendnum, (uint8_t*) &nick); getname(m, friendnum, (uint8_t*) &nick);
fix_name(nick); fix_name(nick);
snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum); snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum);

View File

@ -14,7 +14,7 @@
extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM]; extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM];
extern int add_window(ToxWindow w, int n); 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; extern int active_window;
@ -42,7 +42,7 @@ void fix_name(uint8_t *name)
*q = 0; *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) if (num >= num_friends)
return; 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) { for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
if (WINDOW_STATUS[i] == -1) { if (WINDOW_STATUS[i] == -1) {
WINDOW_STATUS[i] = num; WINDOW_STATUS[i] = num;
add_window(new_chat(num), i); add_window(new_chat(m, num), i);
active_window = i; active_window = i;
break; break;
} }
@ -82,20 +82,20 @@ void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t
fix_name(friends[num].status); fix_name(friends[num].status);
} }
int friendlist_onFriendAdded(int num) int friendlist_onFriendAdded(Messenger *m, int num)
{ {
if (num_friends == MAX_FRIENDS_NUM) if (num_friends == MAX_FRIENDS_NUM)
return -1; return -1;
friends[num_friends].num = num; 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].name, "unknown");
strcpy((char*) friends[num_friends].status, "unknown"); strcpy((char*) friends[num_friends].status, "unknown");
friends[num_friends++].chatwin = -1; friends[num_friends++].chatwin = -1;
return 0; 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 (key == KEY_UP) {
if (--num_selected < 0) if (--num_selected < 0)
@ -121,7 +121,7 @@ static void friendlist_onKey(ToxWindow *self, int key)
if (WINDOW_STATUS[i] == -1) { if (WINDOW_STATUS[i] == -1) {
WINDOW_STATUS[i] = num_selected; WINDOW_STATUS[i] = num_selected;
friends[num_selected].chatwin = 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; active_window = i;
break; break;
} }
@ -164,7 +164,7 @@ void disable_chatwin(int f_num)
friends[f_num].chatwin = -1; friends[f_num].chatwin = -1;
} }
static void friendlist_onInit(ToxWindow *self) static void friendlist_onInit(ToxWindow *self, Messenger *m)
{ {
} }

View File

@ -25,7 +25,7 @@
extern ToxWindow new_prompt(); extern ToxWindow new_prompt();
extern ToxWindow new_friendlist(); 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 void disable_chatwin(int f_num);
extern int add_req(uint8_t *public_key); // XXX extern int add_req(uint8_t *public_key); // XXX
extern unsigned char *hex_string_to_bin(char hex_string[]); extern unsigned char *hex_string_to_bin(char hex_string[]);
@ -40,6 +40,8 @@ char WINDOW_STATUS[MAX_WINDOW_SLOTS];
static ToxWindow windows[MAX_WINDOW_SLOTS]; static ToxWindow windows[MAX_WINDOW_SLOTS];
static ToxWindow* prompt; static ToxWindow* prompt;
static Messenger *m;
int w_num; int w_num;
int active_window; int active_window;
@ -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)
{ {
int i; int i;
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
if (windows[i].onMessage != NULL) 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)
{ {
int i; int i;
for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
if (windows[i].onAction != NULL) 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)
{ {
wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string); wprintw(prompt->window, "\n(nickchange) %d: %s\n", friendnumber, string);
int i; 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)
{ {
wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string); wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string);
int i; int i;
@ -103,7 +105,7 @@ void on_statuschange(int friendnumber, uint8_t *string, uint16_t length)
void on_friendadded(int friendnumber) void on_friendadded(int friendnumber)
{ {
friendlist_onFriendAdded(friendnumber); friendlist_onFriendAdded(m, friendnumber);
} }
/* CALLBACKS END */ /* CALLBACKS END */
@ -129,14 +131,14 @@ static void init_term()
static void init_tox() static void init_tox()
{ {
/* Init core */ /* Init core */
initMessenger(); m = initMessenger();
/* Callbacks */ /* Callbacks */
m_callback_friendrequest(on_request); m_callback_friendrequest(m, on_request);
m_callback_friendmessage(on_message); m_callback_friendmessage(m, on_message);
m_callback_namechange(on_nickchange); m_callback_namechange(m, on_nickchange);
m_callback_statusmessage(on_statuschange); m_callback_statusmessage(m, on_statuschange);
m_callback_action(on_action); m_callback_action(m, on_action);
} }
#define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */ #define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */
@ -211,7 +213,7 @@ int add_window(ToxWindow w, int n)
return -1; return -1;
windows[n] = w; windows[n] = w;
w.onInit(&w); w.onInit(&w, m);
w_num++; w_num++;
return n; return n;
} }
@ -257,7 +259,7 @@ static void do_tox()
dht_on = false; dht_on = false;
wprintw(prompt->window, "\nDHT disconnected.\n"); wprintw(prompt->window, "\nDHT disconnected.\n");
} }
doMessenger(); doMessenger(m);
} }
static void load_data(char *path) static void load_data(char *path)
@ -285,17 +287,17 @@ static void load_data(char *path)
endwin(); endwin();
exit(1); exit(1);
} }
Messenger_load(buf, len); Messenger_load(m, buf, len);
} }
else { else {
len = Messenger_size(); len = Messenger_size(m);
buf = malloc(len); buf = malloc(len);
if (buf == NULL) { if (buf == NULL) {
fprintf(stderr, "malloc() failed.\n"); fprintf(stderr, "malloc() failed.\n");
endwin(); endwin();
exit(1); exit(1);
} }
Messenger_save(buf); Messenger_save(m, buf);
fd = fopen(path, "w"); fd = fopen(path, "w");
if (fd == NULL) { if (fd == NULL) {
@ -473,7 +475,8 @@ int main(int argc, char *argv[])
if (ch == '\t' || ch == KEY_BTAB) if (ch == '\t' || ch == KEY_BTAB)
set_active_window(ch); set_active_window(ch);
else if (ch != ERR) else if (ch != ERR)
a->onKey(a, ch); a->onKey(a, m, ch);
} }
cleanupMessenger(m);
return 0; return 0;
} }

View File

@ -20,24 +20,24 @@ static char prompt_buf[MAX_STR_SIZE] = {0};
static int prompt_buf_pos = 0; static int prompt_buf_pos = 0;
/* commands */ /* commands */
void cmd_accept(ToxWindow *, char **); void cmd_accept(ToxWindow *, Messenger *m, char **);
void cmd_add(ToxWindow *, char **); void cmd_add(ToxWindow *, Messenger *m, char **);
void cmd_clear(ToxWindow *, char **); void cmd_clear(ToxWindow *, Messenger *m, char **);
void cmd_connect(ToxWindow *, char **); void cmd_connect(ToxWindow *, Messenger *m, char **);
void cmd_help(ToxWindow *, char **); void cmd_help(ToxWindow *, Messenger *m, char **);
void cmd_msg(ToxWindow *, char **); void cmd_msg(ToxWindow *, Messenger *m, char **);
void cmd_myid(ToxWindow *, char **); void cmd_myid(ToxWindow *, Messenger *m, char **);
void cmd_nick(ToxWindow *, char **); void cmd_nick(ToxWindow *, Messenger *m, char **);
void cmd_quit(ToxWindow *, char **); void cmd_quit(ToxWindow *, Messenger *m, char **);
void cmd_status(ToxWindow *, char **); void cmd_status(ToxWindow *, Messenger *m, char **);
void cmd_statusmsg(ToxWindow *, char **); void cmd_statusmsg(ToxWindow *, Messenger *m, char **);
#define NUM_COMMANDS 13 #define NUM_COMMANDS 13
static struct { static struct {
char *name; char *name;
int numargs; int numargs;
void (*func)(ToxWindow *, char **); void (*func)(ToxWindow *, Messenger *m, char **);
} commands[] = { } commands[] = {
{ "accept", 1, cmd_accept }, { "accept", 1, cmd_accept },
{ "add", 1, cmd_add }, { "add", 1, cmd_add },
@ -74,7 +74,7 @@ unsigned char *hex_string_to_bin(char hex_string[])
return val; return val;
} }
void cmd_accept(ToxWindow *self, char **args) void cmd_accept(ToxWindow *self, Messenger *m, char **args)
{ {
int num = atoi(args[1]); int num = atoi(args[1]);
if (num >= num_requests) { if (num >= num_requests) {
@ -82,7 +82,7 @@ void cmd_accept(ToxWindow *self, char **args)
return; return;
} }
num = m_addfriend_norequest(pending_requests[num]); num = m_addfriend_norequest(m, pending_requests[num]);
if (num == -1) if (num == -1)
wprintw(self->window, "Failed to add friend.\n"); wprintw(self->window, "Failed to add friend.\n");
else { 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]; uint8_t id_bin[KEY_SIZE_BYTES];
char xx[3]; char xx[3];
@ -121,7 +121,7 @@ void cmd_add(ToxWindow *self, char **args)
} }
id_bin[i] = x; 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) { switch (num) {
case FAERR_TOOLONG: case FAERR_TOOLONG:
wprintw(self->window, "Message is too long.\n"); 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); wclear(self->window);
} }
void cmd_connect(ToxWindow *self, char **args) void cmd_connect(ToxWindow *self, Messenger *m, char **args)
{ {
IP_Port dht; IP_Port dht;
char *ip = args[1]; char *ip = args[1];
@ -174,13 +174,13 @@ void cmd_connect(ToxWindow *self, char **args)
free(binary_string); free(binary_string);
} }
void cmd_quit(ToxWindow *self, char **args) void cmd_quit(ToxWindow *self, Messenger *m, char **args)
{ {
endwin(); endwin();
exit(0); exit(0);
} }
void cmd_help(ToxWindow *self, char **args) void cmd_help(ToxWindow *self, Messenger *m, char **args)
{ {
wclear(self->window); wclear(self->window);
wattron(self->window, COLOR_PAIR(2) | A_BOLD); wattron(self->window, COLOR_PAIR(2) | A_BOLD);
@ -205,17 +205,17 @@ void cmd_help(ToxWindow *self, char **args)
wattroff(self->window, COLOR_PAIR(2)); 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 *id = args[1];
char *msg = args[2]; 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"); wprintw(self->window, "Error occurred while sending message.\n");
else else
wprintw(self->window, "Message successfully sent.\n"); 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}; char id[KEY_SIZE_BYTES*2 + 1] = {0};
size_t i; size_t i;
@ -227,14 +227,14 @@ void cmd_myid(ToxWindow *self, char **args)
wprintw(self->window, "Your ID: %s\n", id); 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]; 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); 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 = args[1];
char *status_text; char *status_text;
@ -260,24 +260,24 @@ void cmd_status(ToxWindow *self, char **args)
char *msg = args[2]; char *msg = args[2];
if (msg == NULL) { if (msg == NULL) {
m_set_userstatus(status_kind); m_set_userstatus(m, status_kind);
wprintw(self->window, "Status set to: %s\n", status_text); wprintw(self->window, "Status set to: %s\n", status_text);
} }
else { else {
m_set_userstatus(status_kind); m_set_userstatus(m, status_kind);
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, %s\n", status_text, msg); 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]; 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); 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; int newlines = 0;
char cmd[MAX_STR_SIZE] = {0}; char cmd[MAX_STR_SIZE] = {0};
@ -347,7 +347,7 @@ static void execute(ToxWindow *self, char *u_cmd)
return; return;
} }
/* pass arguments to command function */ /* pass arguments to command function */
(commands[i].func)(self, cmdargs); (commands[i].func)(self, m, cmdargs);
return; return;
} }
} }
@ -356,7 +356,7 @@ static void execute(ToxWindow *self, char *u_cmd)
wprintw(self->window, "Invalid command.\n"); 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 */ /* Add printable characters to line */
if (isprint(key)) { if (isprint(key)) {
@ -380,7 +380,7 @@ static void prompt_onKey(ToxWindow *self, int key)
/* RETURN key: execute command */ /* RETURN key: execute command */
else if (key == '\n') { else if (key == '\n') {
wprintw(self->window, "\n"); wprintw(self->window, "\n");
execute(self, prompt_buf); execute(self, m, prompt_buf);
prompt_buf_pos = 0; prompt_buf_pos = 0;
prompt_buf[0] = 0; prompt_buf[0] = 0;
} }
@ -413,10 +413,10 @@ static void prompt_onDraw(ToxWindow *self)
wrefresh(self->window); wrefresh(self->window);
} }
static void prompt_onInit(ToxWindow *self) static void prompt_onInit(ToxWindow *self, Messenger *m)
{ {
scrollok(self->window, 1); scrollok(self->window, 1);
cmd_help(self, NULL); cmd_help(self, m, NULL);
wclrtoeol(self->window); wclrtoeol(self->window);
} }

View File

@ -17,14 +17,14 @@
typedef struct ToxWindow_ ToxWindow; typedef struct ToxWindow_ ToxWindow;
struct ToxWindow_ { struct ToxWindow_ {
void(*onKey)(ToxWindow*, int); void(*onKey)(ToxWindow*, Messenger*, int);
void(*onDraw)(ToxWindow*); void(*onDraw)(ToxWindow*);
void(*onInit)(ToxWindow*); void(*onInit)(ToxWindow*, Messenger*);
void(*onFriendRequest)(ToxWindow*, uint8_t*, uint8_t*, uint16_t); 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(*onNickChange)(ToxWindow*, int, uint8_t*, uint16_t);
void(*onStatusChange)(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]; char title[256];
void* x; void* x;