From 9801eceaa4834798a9b879d353a455754e5fbf00 Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Wed, 7 Aug 2013 17:01:31 -0400 Subject: [PATCH 1/5] Separated StatusMessage from UserStatus --- core/Messenger.c | 182 ++++++++++++++++++++++++++--------------------- core/Messenger.h | 63 ++++++++-------- 2 files changed, 131 insertions(+), 114 deletions(-) diff --git a/core/Messenger.c b/core/Messenger.c index 4e994a15..3ad9097c 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -32,10 +32,11 @@ typedef struct { uint8_t info[MAX_DATA_SIZE]; /* the data that is sent during the friend requests we do */ uint8_t name[MAX_NAME_LENGTH]; uint8_t name_sent; /* 0 if we didn't send our name to this friend 1 if we have. */ - uint8_t *userstatus; - uint16_t userstatus_length; + uint8_t *statusmessage; + uint16_t statusmessage_length; + uint8_t statusmessage_sent; + USERSTATUS userstatus; uint8_t userstatus_sent; - USERSTATUS_KIND userstatus_kind; uint16_t info_size; /* length of the info */ } Friend; @@ -44,9 +45,9 @@ uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; static uint8_t self_name[MAX_NAME_LENGTH]; static uint16_t self_name_length; -static uint8_t *self_userstatus; -static uint16_t self_userstatus_len; -static USERSTATUS_KIND self_userstatus_kind; +static uint8_t *self_statusmessage; +static uint16_t self_statusmessage_len; +static USERSTATUS self_userstatus; #define MAX_NUM_FRIENDS 256 @@ -123,9 +124,9 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) friendlist[i].crypt_connection_id = -1; friendlist[i].friend_request_id = -1; memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); - friendlist[i].userstatus = calloc(1, 1); - friendlist[i].userstatus_length = 1; - friendlist[i].userstatus_kind = USERSTATUS_KIND_OFFLINE; + friendlist[i].statusmessage = calloc(1, 1); + friendlist[i].statusmessage_length = 1; + friendlist[i].userstatus = USERSTATUS_NONE; memcpy(friendlist[i].info, data, length); friendlist[i].info_size = length; @@ -148,8 +149,9 @@ int m_addfriend_norequest(uint8_t * client_id) friendlist[i].crypt_connection_id = -1; friendlist[i].friend_request_id = -1; memcpy(friendlist[i].client_id, client_id, CLIENT_ID_SIZE); - friendlist[i].userstatus = calloc(1, 1); - friendlist[i].userstatus_length = 1; + friendlist[i].statusmessage = calloc(1, 1); + friendlist[i].statusmessage_length = 1; + friendlist[i].userstatus = USERSTATUS_NONE; numfriends++; return i; } @@ -167,7 +169,7 @@ int m_delfriend(int friendnumber) DHT_delfriend(friendlist[friendnumber].client_id); crypto_kill(friendlist[friendnumber].crypt_connection_id); - free(friendlist[friendnumber].userstatus); + free(friendlist[friendnumber].statusmessage); memset(&friendlist[friendnumber], 0, sizeof(Friend)); uint32_t i; @@ -272,33 +274,28 @@ int getname(int friendnumber, uint8_t * name) return 0; } -int m_set_userstatus(USERSTATUS_KIND kind, uint8_t *status, uint16_t length) +int m_set_statusmessage(uint8_t *status, uint16_t length) { - if (length > MAX_USERSTATUS_LENGTH) + if (length > MAX_STATUSMESSAGE_LENGTH) return -1; - if (kind != USERSTATUS_KIND_RETAIN) { - self_userstatus_kind = kind; - } uint8_t *newstatus = calloc(length, 1); memcpy(newstatus, status, length); - free(self_userstatus); - self_userstatus = newstatus; - self_userstatus_len = length; + free(self_statusmessage); + self_statusmessage = newstatus; + self_statusmessage_len = length; uint32_t i; for (i = 0; i < numfriends; ++i) - friendlist[i].userstatus_sent = 0; + friendlist[i].statusmessage_sent = 0; return 0; } -int m_set_userstatus_kind(USERSTATUS_KIND kind) { - if (kind >= USERSTATUS_KIND_INVALID) { +int m_set_userstatus(USERSTATUS status) +{ + if (status >= USERSTATUS_INVALID) { return -1; } - if (kind == USERSTATUS_KIND_RETAIN) { - return 0; - } - self_userstatus_kind = kind; + self_userstatus = status; uint32_t i; for (i = 0; i < numfriends; ++i) friendlist[i].userstatus_sent = 0; @@ -306,72 +303,83 @@ int m_set_userstatus_kind(USERSTATUS_KIND kind) { } /* return the size of friendnumber's user status - guaranteed to be at most MAX_USERSTATUS_LENGTH */ -int m_get_userstatus_size(int friendnumber) + guaranteed to be at most MAX_STATUSMESSAGE_LENGTH */ +int m_get_statusmessage_size(int friendnumber) { if (friendnumber >= numfriends || friendnumber < 0) return -1; - return friendlist[friendnumber].userstatus_length; + return friendlist[friendnumber].statusmessage_length; } /* copy the user status of friendnumber into buf, truncating if needed to maxlen - bytes, use m_get_userstatus_size to find out how much you need to allocate */ -int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen) + bytes, use m_get_statusmessage_size to find out how much you need to allocate */ +int m_copy_statusmessage(int friendnumber, uint8_t * buf, uint32_t maxlen) { if (friendnumber >= numfriends || friendnumber < 0) return -1; memset(buf, 0, maxlen); - memcpy(buf, friendlist[friendnumber].userstatus, MIN(maxlen, MAX_USERSTATUS_LENGTH) - 1); + memcpy(buf, friendlist[friendnumber].statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1); return 0; } -int m_copy_self_userstatus(uint8_t * buf, uint32_t maxlen) +int m_copy_self_statusmessage(uint8_t * buf, uint32_t maxlen) { memset(buf, 0, maxlen); - memcpy(buf, self_userstatus, MIN(maxlen, MAX_USERSTATUS_LENGTH) - 1); + memcpy(buf, self_statusmessage, MIN(maxlen, MAX_STATUSMESSAGE_LENGTH) - 1); return 0; } -USERSTATUS_KIND m_get_userstatus_kind(int friendnumber) { - if (friendnumber >= numfriends || friendnumber < 0) - return USERSTATUS_KIND_INVALID; - USERSTATUS_KIND uk = friendlist[friendnumber].userstatus_kind; - if (uk >= USERSTATUS_KIND_INVALID) { - uk = USERSTATUS_KIND_ONLINE; - } - return uk; -} - -USERSTATUS_KIND m_get_self_userstatus_kind(void) { - return self_userstatus_kind; -} - -static int send_userstatus(int friendnumber, uint8_t * status, uint16_t length) +USERSTATUS m_get_userstatus(int friendnumber) { - uint8_t *thepacket = malloc(length + 2); - memcpy(thepacket + 2, status, length); - thepacket[0] = PACKET_ID_USERSTATUS; - thepacket[1] = self_userstatus_kind; - int written = write_cryptpacket(friendlist[friendnumber].crypt_connection_id, thepacket, length + 2); + if (friendnumber >= numfriends || friendnumber < 0) + return USERSTATUS_INVALID; + USERSTATUS status = friendlist[friendnumber].userstatus; + if (status >= USERSTATUS_INVALID) { + status = USERSTATUS_NONE; + } + return status; +} + +USERSTATUS m_get_self_userstatus(void) +{ + return self_userstatus; +} + +static int send_statusmessage(int friendnumber, uint8_t * status, uint16_t length) +{ + uint8_t *thepacket = malloc(length + 1); + memcpy(thepacket + 1, status, length); + thepacket[0] = PACKET_ID_STATUSMESSAGE; + int written = write_cryptpacket(friendlist[friendnumber].crypt_connection_id, thepacket, length + 1); free(thepacket); return written; } -static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t length) +static int send_userstatus(int friendnumber, USERSTATUS status) +{ + uint8_t *thepacket = malloc(1 + 1); + memcpy(thepacket + 1, &status, 1); + thepacket[0] = PACKET_ID_USERSTATUS; + int written = write_cryptpacket(friendlist[friendnumber].crypt_connection_id, thepacket, 1 + 1); + free(thepacket); + return written; +} + +static int set_friend_statusmessage(int friendnumber, uint8_t * status, uint16_t length) { if (friendnumber >= numfriends || friendnumber < 0) return -1; uint8_t *newstatus = calloc(length, 1); memcpy(newstatus, status, length); - free(friendlist[friendnumber].userstatus); - friendlist[friendnumber].userstatus = newstatus; - friendlist[friendnumber].userstatus_length = length; + free(friendlist[friendnumber].statusmessage); + friendlist[friendnumber].statusmessage = newstatus; + friendlist[friendnumber].statusmessage_length = length; return 0; } -static void set_friend_userstatus_kind(int friendnumber, USERSTATUS_KIND k) +static void set_friend_userstatus(int friendnumber, USERSTATUS status) { - friendlist[friendnumber].userstatus_kind = k; + friendlist[friendnumber].userstatus = status; } /* static void (*friend_request)(uint8_t *, uint8_t *, uint16_t); @@ -400,12 +408,20 @@ void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t)) friend_namechange_isset = 1; } -static void (*friend_statuschange)(int, USERSTATUS_KIND, uint8_t *, uint16_t); -static uint8_t friend_statuschange_isset = 0; -void m_callback_userstatus(void (*function)(int, USERSTATUS_KIND, uint8_t *, uint16_t)) +static void (*friend_statusmessagechange)(int, uint8_t *, uint16_t); +static uint8_t friend_statusmessagechange_isset = 0; +void m_callback_statusmessage(void (*function)(int, uint8_t *, uint16_t)) { - friend_statuschange = function; - friend_statuschange_isset = 1; + friend_statusmessagechange = function; + friend_statusmessagechange_isset = 1; +} + +static void (*friend_userstatuschange)(int, USERSTATUS); +static uint8_t friend_userstatuschange_isset = 0; +void m_callback_userstatus(void (*function)(int, USERSTATUS)) +{ + friend_userstatuschange = function; + friend_userstatuschange_isset = 1; } #define PORT 33445 @@ -413,7 +429,7 @@ void m_callback_userstatus(void (*function)(int, USERSTATUS_KIND, uint8_t *, uin int initMessenger(void) { new_keys(); - m_set_userstatus(USERSTATUS_KIND_ONLINE, (uint8_t*)"Online", sizeof("Online")); + m_set_statusmessage((uint8_t*)"Online", sizeof("Online")); initNetCrypto(); IP ip; ip.i = 0; @@ -468,8 +484,12 @@ static void doFriends(void) if (m_sendname(i, self_name, self_name_length)) friendlist[i].name_sent = 1; } + if (friendlist[i].statusmessage_sent == 0) { + if (send_statusmessage(i, self_statusmessage, self_statusmessage_len)) + friendlist[i].statusmessage_sent = 1; + } if (friendlist[i].userstatus_sent == 0) { - if (send_userstatus(i, self_userstatus, self_userstatus_len)) + if (send_userstatus(i, self_userstatus)) friendlist[i].userstatus_sent = 1; } len = read_cryptpacket(friendlist[i].crypt_connection_id, temp); @@ -484,18 +504,20 @@ static void doFriends(void) friendlist[i].name[len - 2] = 0; /* make sure the NULL terminator is present. */ break; } + case PACKET_ID_STATUSMESSAGE: { + uint8_t *status = calloc(MIN(len - 1, MAX_STATUSMESSAGE_LENGTH), 1); + memcpy(status, temp + 1, MIN(len - 1, MAX_STATUSMESSAGE_LENGTH)); + if (friend_statusmessagechange_isset) + friend_statusmessagechange(i, status, MIN(len - 1, MAX_STATUSMESSAGE_LENGTH)); + set_friend_statusmessage(i, status, MIN(len - 1, MAX_STATUSMESSAGE_LENGTH)); + free(status); + break; + } case PACKET_ID_USERSTATUS: { - if (len > 2) { - uint8_t *status = calloc(MIN(len - 2, MAX_USERSTATUS_LENGTH), 1); - memcpy(status, temp + 2, MIN(len - 2, MAX_USERSTATUS_LENGTH)); - if (friend_statuschange_isset) - friend_statuschange(i, temp[1], status, MIN(len - 2, MAX_USERSTATUS_LENGTH)); - set_friend_userstatus(i, status, MIN(len - 2, MAX_USERSTATUS_LENGTH)); - free(status); - } else if (friend_statuschange_isset) { - friend_statuschange(i, temp[1], friendlist[i].userstatus, friendlist[i].userstatus_length); - } - set_friend_userstatus_kind(i, temp[1]); + USERSTATUS status = *(temp + 1); + if (friend_userstatuschange_isset) + friend_userstatuschange(i, status); + set_friend_userstatus(i, status); break; } case PACKET_ID_MESSAGE: { @@ -641,7 +663,7 @@ int Messenger_load(uint8_t * data, uint32_t length) if(temp[i].status != 0) { int fnum = m_addfriend_norequest(temp[i].client_id); setfriendname(fnum, temp[i].name); - /* set_friend_userstatus(fnum, temp[i].userstatus, temp[i].userstatus_length); */ + /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ } } free(temp); diff --git a/core/Messenger.h b/core/Messenger.h index 8940aadd..aef652ff 100644 --- a/core/Messenger.h +++ b/core/Messenger.h @@ -36,10 +36,11 @@ extern "C" { #endif #define MAX_NAME_LENGTH 128 -#define MAX_USERSTATUS_LENGTH 128 +#define MAX_STATUSMESSAGE_LENGTH 128 #define PACKET_ID_NICKNAME 48 -#define PACKET_ID_USERSTATUS 49 +#define PACKET_ID_STATUSMESSAGE 49 +#define PACKET_ID_USERSTATUS 50 #define PACKET_ID_MESSAGE 64 /* status definitions */ @@ -57,24 +58,18 @@ extern "C" { #define FAERR_ALREADYSENT -4 #define FAERR_UNKNOWN -5 -/* don't assume MAX_USERSTATUS_LENGTH will stay at 128, it may be increased +/* don't assume MAX_STATUSMESSAGE_LENGTH will stay at 128, it may be increased to an absurdly large number later */ -/* USERSTATUS_KIND - * Represents the different kinds of userstatus - * someone can have. - * More on this later... */ +/* USERSTATUS + * Represents userstatuses someone can have. */ typedef enum { - USERSTATUS_KIND_RETAIN = (uint8_t)0, /* This is a special value that must not be returned by - * m_get_userstatus_kind. You can pass it into m_set_userstatus - * to keep the current USERSTATUS_KIND. */ - USERSTATUS_KIND_ONLINE, /* Recommended representation: Green. */ - USERSTATUS_KIND_AWAY, /* Recommended representation: Orange, or yellow. */ - USERSTATUS_KIND_BUSY, /* Recommended representation: Red. */ - USERSTATUS_KIND_OFFLINE, /* Recommended representation: Grey, semi-transparent. */ - USERSTATUS_KIND_INVALID, -} USERSTATUS_KIND; + USERSTATUS_NONE, + USERSTATUS_AWAY, + USERSTATUS_BUSY, + USERSTATUS_INVALID +} USERSTATUS; /* * add a friend @@ -144,26 +139,26 @@ int getname(int friendnumber, uint8_t *name); /* set our user status you are responsible for freeing status after returns 0 on success, -1 on failure */ -int m_set_userstatus(USERSTATUS_KIND kind, uint8_t *status, uint16_t length); -int m_set_userstatus_kind(USERSTATUS_KIND kind); +int m_set_statusmessage(uint8_t *status, uint16_t length); +int m_set_userstatus(USERSTATUS status); -/* return the length of friendnumber's user status, +/* return the length of friendnumber's status message, including null pass it into malloc */ -int m_get_userstatus_size(int friendnumber); +int m_get_statusmessage_size(int friendnumber); -/* copy friendnumber's userstatus into buf, truncating if size is over maxlen - get the size you need to allocate from m_get_userstatus_size - The self variant will copy our own userstatus. */ -int m_copy_userstatus(int friendnumber, uint8_t *buf, uint32_t maxlen); -int m_copy_self_userstatus(uint8_t *buf, uint32_t maxlen); +/* copy friendnumber's status message into buf, truncating if size is over maxlen + get the size you need to allocate from m_get_statusmessage_size + The self variant will copy our own status message. */ +int m_copy_statusmessage(int friendnumber, uint8_t *buf, uint32_t maxlen); +int m_copy_self_statusmessage(uint8_t *buf, uint32_t maxlen); -/* Return one of USERSTATUS_KIND values, except USERSTATUS_KIND_RETAIN. - * Values unknown to your application should be represented as USERSTATUS_KIND_ONLINE. - * As above, the self variant will return our own USERSTATUS_KIND. - * If friendnumber is invalid, this shall return USERSTATUS_KIND_INVALID. */ -USERSTATUS_KIND m_get_userstatus_kind(int friendnumber); -USERSTATUS_KIND m_get_self_userstatus_kind(void); +/* Return one of USERSTATUS values. + * Values unknown to your application should be represented as USERSTATUS_NONE. + * As above, the self variant will return our own USERSTATUS. + * If friendnumber is invalid, this shall return USERSTATUS_INVALID. */ +USERSTATUS m_get_userstatus(int friendnumber); +USERSTATUS m_get_self_userstatus(void); /* set the function that will be executed when a friend request is received. function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ @@ -178,10 +173,10 @@ void m_callback_friendmessage(void (*function)(int, uint8_t *, uint16_t)); you are not responsible for freeing newname */ void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t)); -/* set the callback for user status changes - function(int friendnumber, USERSTATUS_KIND kind, uint8_t *newstatus, uint16_t length) +/* set the callback for status message changes + function(int friendnumber, uint8_t *newstatus, uint16_t length) you are not responsible for freeing newstatus */ -void m_callback_userstatus(void (*function)(int, USERSTATUS_KIND, uint8_t *, uint16_t)); +void m_callback_statusmessage(void (*function)(int, uint8_t *, uint16_t)); /* run this at startup returns 0 if no connection problems From b1a2af23e521f1b21a1013c6894987f410700496 Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Wed, 7 Aug 2013 18:12:59 -0400 Subject: [PATCH 2/5] Modified clients to properly work with the changes --- testing/nTox.c | 8 ++++---- testing/nTox_win32.c | 14 +++++++------- testing/nTox_win32.h | 2 +- testing/toxic/chat.c | 2 +- testing/toxic/friendlist.c | 4 ++-- testing/toxic/main.c | 4 ++-- testing/toxic/prompt.c | 20 ++++++++------------ 7 files changed, 25 insertions(+), 29 deletions(-) diff --git a/testing/nTox.c b/testing/nTox.c index ecdba1e3..ee4d7de4 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -220,7 +220,7 @@ void line_eval(char *line) print_friendlist(); } else if (inpt_command == 's') { - uint8_t status[MAX_USERSTATUS_LENGTH]; + uint8_t status[MAX_STATUSMESSAGE_LENGTH]; int i = 0; size_t len = strlen(line); for (i = 3; i < len; i++) { @@ -228,7 +228,7 @@ void line_eval(char *line) status[i-3] = line[i]; } status[i-3] = 0; - m_set_userstatus(USERSTATUS_KIND_ONLINE, status, strlen((char*)status) + 1); + m_set_statusmessage(status, strlen((char*)status) + 1); char numstring[100]; sprintf(numstring, "[i] changed status to %s", (char*)status); new_lines(numstring); @@ -364,7 +364,7 @@ void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) } } -void print_statuschange(int friendnumber, USERSTATUS_KIND kind, uint8_t *string, uint16_t length) +void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) { char name[MAX_NAME_LENGTH]; if(getname(friendnumber, (uint8_t*)name) != -1) { @@ -464,7 +464,7 @@ int main(int argc, char *argv[]) m_callback_friendrequest(print_request); m_callback_friendmessage(print_message); m_callback_namechange(print_nickchange); - m_callback_userstatus(print_statuschange); + m_callback_statusmessage(print_statuschange); initscr(); noecho(); diff --git a/testing/nTox_win32.c b/testing/nTox_win32.c index e4489f13..d9d695a5 100644 --- a/testing/nTox_win32.c +++ b/testing/nTox_win32.c @@ -86,7 +86,7 @@ void print_nickchange(int friendnumber, uint8_t *string, uint16_t length) printf(msg); } -void print_statuschange(int friendnumber,USERSTATUS_KIND kind, uint8_t *string, uint16_t length) +void print_statuschange(int friendnumber, uint8_t *string, uint16_t length) { char name[MAX_NAME_LENGTH]; getname(friendnumber, (uint8_t*)name); @@ -263,7 +263,7 @@ void change_nickname() void change_status(int savetofile) { - uint8_t status[MAX_USERSTATUS_LENGTH]; + uint8_t status[MAX_STATUSMESSAGE_LENGTH]; int i = 0; size_t len = strlen(line); @@ -275,7 +275,7 @@ void change_status(int savetofile) } status[i-3] = 0; - m_set_userstatus(USERSTATUS_KIND_RETAIN, status, strlen((char*)status)); + m_set_statusmessage(status, strlen((char*)status)); char numstring[100]; sprintf(numstring, "\n[i] changed status to %s\n\n", (char*)status); printf(numstring); @@ -403,11 +403,11 @@ int main(int argc, char *argv[]) FILE* status_file = NULL; status_file = fopen("statusfile.txt", "r"); if(status_file) { - uint8_t status[MAX_USERSTATUS_LENGTH]; - while (fgets(line, MAX_USERSTATUS_LENGTH, status_file) != NULL) { + uint8_t status[MAX_STATUSMESSAGE_LENGTH]; + while (fgets(line, MAX_STATUSMESSAGE_LENGTH, status_file) != NULL) { sscanf(line, "%s", (char*)status); } - m_set_userstatus(USERSTATUS_KIND_RETAIN, status, strlen((char*)status)+1); + m_set_statusmessage(status, strlen((char*)status)+1); statusloaded = 1; printf("%s\n", status); fclose(status_file); @@ -416,7 +416,7 @@ int main(int argc, char *argv[]) m_callback_friendrequest(print_request); m_callback_friendmessage(print_message); m_callback_namechange(print_nickchange); - m_callback_userstatus(print_statuschange); + m_callback_statusmessae(print_statuschange); char idstring1[PUB_KEY_BYTES][5]; char idstring2[PUB_KEY_BYTES][5]; int i; diff --git a/testing/nTox_win32.h b/testing/nTox_win32.h index 36d5df1f..03641a5d 100644 --- a/testing/nTox_win32.h +++ b/testing/nTox_win32.h @@ -32,7 +32,7 @@ void do_header(); void print_message(int friendnumber, uint8_t * string, uint16_t length); void print_nickchange(int friendnumber, uint8_t *string, uint16_t length); -void print_statuschange(int friendnumber,USERSTATUS_KIND kind, uint8_t *string, uint16_t length); +void print_statuschange(int friendnumber, uint8_t *string, uint16_t length); void load_key(); void add_friend(); void list_friends(); diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index 20c01620..2563fa9c 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c @@ -162,7 +162,7 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd) return; } msg++; - m_set_userstatus(USERSTATUS_KIND_RETAIN, (uint8_t*) msg, strlen(msg)+1); + m_set_statusmessage((uint8_t*) msg, strlen(msg)+1); wprintw(ctx->history, "Status set to: %s\n", msg); } diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c index f03914e6..159217b1 100644 --- a/testing/toxic/friendlist.c +++ b/testing/toxic/friendlist.c @@ -20,7 +20,7 @@ extern int active_window; typedef struct { uint8_t name[MAX_NAME_LENGTH]; - uint8_t status[MAX_USERSTATUS_LENGTH]; + uint8_t status[MAX_STATUSMESSAGE_LENGTH]; int num; int chatwin; } friend_t; @@ -74,7 +74,7 @@ void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t le void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) { - if (len >= MAX_USERSTATUS_LENGTH || num >= num_friends) + if (len >= MAX_STATUSMESSAGE_LENGTH || num >= num_friends) return; memcpy((char*) &friends[num].status, (char*) str, len); diff --git a/testing/toxic/main.c b/testing/toxic/main.c index 8de76244..59333004 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -68,7 +68,7 @@ void on_nickchange(int friendnumber, uint8_t *string, uint16_t length) } } -void on_statuschange(int friendnumber, USERSTATUS_KIND kind, uint8_t *string, uint16_t length) +void on_statuschange(int friendnumber, uint8_t *string, uint16_t length) { wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string); int i; @@ -112,7 +112,7 @@ static void init_tox() m_callback_friendrequest(on_request); m_callback_friendmessage(on_message); m_callback_namechange(on_nickchange); - m_callback_userstatus(on_statuschange); + m_callback_statusmessage(on_statuschange); } void init_window_status() diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index d79d061f..01261cce 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c @@ -183,27 +183,22 @@ static void execute(ToxWindow *self, char *u_cmd) return; } status++; - USERSTATUS_KIND status_kind; + USERSTATUS status_kind; if (!strncmp(status, "online", strlen("online"))) { - status_kind = USERSTATUS_KIND_ONLINE; + status_kind = USERSTATUS_NONE; status_text = "ONLINE"; } else if (!strncmp(status, "away", strlen("away"))) { - status_kind = USERSTATUS_KIND_AWAY; + status_kind = USERSTATUS_AWAY; status_text = "AWAY"; } else if (!strncmp(status, "busy", strlen("busy"))) { - status_kind = USERSTATUS_KIND_BUSY; + status_kind = USERSTATUS_BUSY; status_text = "BUSY"; } - else if (!strncmp(status, "offline", strlen("offline"))) { - status_kind = USERSTATUS_KIND_OFFLINE; - status_text = "OFFLINE"; - } - else { wprintw(self->window, "Invalid status.\n"); @@ -212,12 +207,13 @@ static void execute(ToxWindow *self, char *u_cmd) msg = strchr(status, ' '); if (msg == NULL) { - m_set_userstatus_kind(status_kind); + m_set_userstatus(status_kind); wprintw(self->window, "Status set to: %s\n", status_text); } else { msg++; - m_set_userstatus(status_kind, (uint8_t*) msg, strlen(msg)+1); + m_set_userstatus(status_kind); + m_set_statusmessage((uint8_t*) msg, strlen(msg)+1); wprintw(self->window, "Status set to: %s, %s\n", status_text, msg); } } @@ -229,7 +225,7 @@ static void execute(ToxWindow *self, char *u_cmd) return; } msg++; - m_set_userstatus(USERSTATUS_KIND_RETAIN, (uint8_t*) msg, strlen(msg)+1); + m_set_statusmessage((uint8_t*) msg, strlen(msg)+1); wprintw(self->window, "Status set to: %s\n", msg); } From b6a3f2b403ba8a2ee28921420081225176fe2783 Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Wed, 7 Aug 2013 20:23:48 -0400 Subject: [PATCH 3/5] Added length checks --- core/Messenger.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/Messenger.c b/core/Messenger.c index f1d8b35e..b1050230 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -541,6 +541,8 @@ static void doFriends(void) break; } case PACKET_ID_STATUSMESSAGE: { + if (len < 2) + break; uint8_t *status = calloc(MIN(len - 1, MAX_STATUSMESSAGE_LENGTH), 1); memcpy(status, temp + 1, MIN(len - 1, MAX_STATUSMESSAGE_LENGTH)); if (friend_statusmessagechange_isset) @@ -550,6 +552,8 @@ static void doFriends(void) break; } case PACKET_ID_USERSTATUS: { + if (len != 2) + break; USERSTATUS status = *(temp + 1); if (friend_userstatuschange_isset) friend_userstatuschange(i, status); From 4779561ea3076baa50288f848fb929dc1e5b4025 Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Wed, 7 Aug 2013 20:24:06 -0400 Subject: [PATCH 4/5] Refactored an expression --- core/Messenger.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/Messenger.c b/core/Messenger.c index b1050230..3d991e63 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -554,7 +554,7 @@ static void doFriends(void) case PACKET_ID_USERSTATUS: { if (len != 2) break; - USERSTATUS status = *(temp + 1); + USERSTATUS status = temp[1]; if (friend_userstatuschange_isset) friend_userstatuschange(i, status); set_friend_userstatus(i, status); From 338da1fde705a6e52242eaf81cd0b4face01fac4 Mon Sep 17 00:00:00 2001 From: Maxim Biro Date: Wed, 7 Aug 2013 20:37:34 -0400 Subject: [PATCH 5/5] Fixed auto tests --- auto_tests/friends_test.c | 8 ++++---- auto_tests/messenger_test.c | 22 ++++++++++------------ 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/auto_tests/friends_test.c b/auto_tests/friends_test.c index 4f777ab5..11c6bf29 100755 --- a/auto_tests/friends_test.c +++ b/auto_tests/friends_test.c @@ -65,7 +65,7 @@ void parent_confirm_message(int num, uint8_t *data, uint16_t length) request_flags |= SECOND_FLAG; } -void parent_confirm_status(int num, USERSTATUS_KIND status, uint8_t *data, uint16_t length) +void parent_confirm_status(int num, uint8_t *data, uint16_t length) { puts("OK"); request_flags |= FIRST_FLAG; @@ -110,7 +110,7 @@ void child_got_request(uint8_t *public_key, uint8_t *data, uint16_t length) request_flags |= FIRST_FLAG; } -void child_got_statuschange(int friend_num, USERSTATUS_KIND status, uint8_t *string, uint16_t length) +void child_got_statuschange(int friend_num, uint8_t *string, uint16_t length) { request_flags |= SECOND_FLAG; } @@ -169,7 +169,7 @@ int main(int argc, char *argv[]) msync(child_id, crypto_box_PUBLICKEYBYTES, MS_SYNC); m_callback_friendrequest(child_got_request); - m_callback_userstatus(child_got_statuschange); + m_callback_statusmessage(child_got_statuschange); /* wait on the friend request */ while(!(request_flags & FIRST_FLAG)) @@ -196,7 +196,7 @@ int main(int argc, char *argv[]) } msync(parent_id, crypto_box_PUBLICKEYBYTES, MS_SYNC); - m_callback_userstatus(parent_confirm_status); + m_callback_statusmessage(parent_confirm_status); m_callback_friendmessage(parent_confirm_message); /* hacky way to give the child time to set up */ diff --git a/auto_tests/messenger_test.c b/auto_tests/messenger_test.c index deed498f..cc624ab6 100644 --- a/auto_tests/messenger_test.c +++ b/auto_tests/messenger_test.c @@ -62,16 +62,16 @@ END_TEST START_TEST(test_m_get_userstatus_size) { int rc = 0; - ck_assert_msg((m_get_userstatus_size(-1) == -1), - "m_get_userstatus_size did NOT catch an argument of -1"); - ck_assert_msg((m_get_userstatus_size(REALLY_BIG_NUMBER) == -1), - "m_get_userstatus_size did NOT catch the following argument: %d\n", + ck_assert_msg((m_get_statusmessage_size(-1) == -1), + "m_get_statusmessage_size did NOT catch an argument of -1"); + ck_assert_msg((m_get_statusmessage_size(REALLY_BIG_NUMBER) == -1), + "m_get_statusmessage_size did NOT catch the following argument: %d\n", REALLY_BIG_NUMBER); - rc = m_get_userstatus_size(friend_id_num); + rc = m_get_statusmessage_size(friend_id_num); /* this WILL error if the original m_addfriend_norequest() failed */ - ck_assert_msg((rc > 0 && rc <= MAX_USERSTATUS_LENGTH), - "m_get_userstatus_size is returning out of range values!\n" + ck_assert_msg((rc > 0 && rc <= MAX_STATUSMESSAGE_LENGTH), + "m_get_statusmessage_size is returning out of range values!\n" "(this can be caused by the error of m_addfriend_norequest" " in the beginning of the suite)\n"); } @@ -83,15 +83,13 @@ START_TEST(test_m_set_userstatus) uint16_t good_length = strlen(status); uint16_t bad_length = REALLY_BIG_NUMBER; - if(m_set_userstatus(USERSTATUS_KIND_ONLINE, - (uint8_t *)status, bad_length) != -1) + if(m_set_statusmessage((uint8_t *)status, bad_length) != -1) ck_abort_msg("m_set_userstatus did NOT catch the following length: %d\n", REALLY_BIG_NUMBER); - if((m_set_userstatus(USERSTATUS_KIND_RETAIN, - (uint8_t *)status, good_length)) != 0) + if((m_set_statusmessage((uint8_t *)status, good_length)) != 0) ck_abort_msg("m_set_userstatus did NOT return 0 on the following length: %d\n" - "MAX_USERSTATUS_LENGTH: %d\n", good_length, MAX_USERSTATUS_LENGTH); + "MAX_STATUSMESSAGE_LENGTH: %d\n", good_length, MAX_STATUSMESSAGE_LENGTH); } END_TEST