From 381ae769db0747de88ecd02decf7fbde44a4c6b9 Mon Sep 17 00:00:00 2001 From: Michael Rose Date: Tue, 13 Aug 2013 22:07:46 +0200 Subject: store messenger when friends are added --- testing/toxic/main.c | 113 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 68 insertions(+), 45 deletions(-) (limited to 'testing/toxic') diff --git a/testing/toxic/main.c b/testing/toxic/main.c index 40ff5d09..27a34e09 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -30,6 +30,8 @@ extern void disable_chatwin(int f_num); extern int add_req(uint8_t *public_key); // XXX extern unsigned char *hex_string_to_bin(char hex_string[]); +static int store_data(char*); + /* Holds status of chat windows */ char WINDOW_STATUS[MAX_WINDOW_SLOTS]; @@ -41,6 +43,7 @@ static ToxWindow windows[MAX_WINDOW_SLOTS]; static ToxWindow* prompt; static Messenger *m; +static char *DATA_FILE; int w_num; int active_window; @@ -105,7 +108,11 @@ void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t l void on_friendadded(int friendnumber) { - friendlist_onFriendAdded(m, friendnumber); + friendlist_onFriendAdded(m, friendnumber); + int st; + if ((st = store_data(DATA_FILE)) != 0) { + wprintw(prompt->window, "\nCould not store messenger, error code: %d\n", st); + } } /* CALLBACKS END */ @@ -279,61 +286,76 @@ static void do_tox() doMessenger(m); } -static void load_data(char *path) +/* + * Store Messenger data to path + * Return 0 Messenger stored successfully + * Return 1 malloc failed + * Return 2 fopen failed + * Return 3 fwrite failed + */ +static int store_data(char *path) { - FILE *fd; - size_t len; - uint8_t *buf; - - if ((fd = fopen(path, "r")) != NULL) { - fseek(fd, 0, SEEK_END); - len = ftell(fd); - fseek(fd, 0, SEEK_SET); + FILE *fd; + size_t len; + uint8_t *buf; - buf = malloc(len); - if (buf == NULL) { - fprintf(stderr, "malloc() failed.\n"); - fclose(fd); - endwin(); - exit(1); - } - if (fread(buf, len, 1, fd) != 1){ - fprintf(stderr, "fread() failed.\n"); - free(buf); - fclose(fd); - endwin(); - exit(1); - } - Messenger_load(m, buf, len); - } - else { len = Messenger_size(m); buf = malloc(len); if (buf == NULL) { - fprintf(stderr, "malloc() failed.\n"); - endwin(); - exit(1); + return 1; } Messenger_save(m, buf); fd = fopen(path, "w"); if (fd == NULL) { - fprintf(stderr, "fopen() failed.\n"); - free(buf); - endwin(); - exit(1); + return 2; } - if (fwrite(buf, len, 1, fd) != 1){ - fprintf(stderr, "fwrite() failed.\n"); - free(buf); - fclose(fd); - endwin(); - exit(1); + if (fwrite(buf, len, 1, fd) != 1) { + return 3; + } + + free(buf); + fclose(fd); + + return 0; +} + +static void load_data(char *path) { + FILE *fd; + size_t len; + uint8_t *buf; + + if ((fd = fopen(path, "r")) != NULL) { + fseek(fd, 0, SEEK_END); + len = ftell(fd); + fseek(fd, 0, SEEK_SET); + + buf = malloc(len); + if (buf == NULL) { + fprintf(stderr, "malloc() failed.\n"); + fclose(fd); + endwin(); + exit(1); + } + if (fread(buf, len, 1, fd) != 1) { + fprintf(stderr, "fread() failed.\n"); + free(buf); + fclose(fd); + endwin(); + exit(1); + } + Messenger_load(m, buf, len); + free(buf); + fclose(fd); + } else { + int st; + if ((st = store_data(path)) != 0) { + fprintf(stderr, "storing messenger failed with error code: %d", st); + endwin(); + exit(1); + } } - } - free(buf); - fclose(fd); } static void draw_bar() @@ -419,7 +441,6 @@ int main(int argc, char *argv[]) int ch; ToxWindow* a; char *user_config_dir = get_user_config_dir(); - char *DATA_FILE; int config_err = create_user_config_dir(user_config_dir); if(config_err) { DATA_FILE = "data"; @@ -457,7 +478,6 @@ int main(int argc, char *argv[]) if(f_loadfromfile) load_data(DATA_FILE); - free(DATA_FILE); if (f_flag == -1) { attron(COLOR_PAIR(3) | A_BOLD); @@ -490,6 +510,9 @@ int main(int argc, char *argv[]) else if (ch != ERR) a->onKey(a, m, ch); } + cleanupMessenger(m); + free(DATA_FILE); + return 0; } -- cgit v1.2.3 From 382094a6e85a4ebb05b88c3ce1e0255b52061bb1 Mon Sep 17 00:00:00 2001 From: Michael Rose Date: Wed, 14 Aug 2013 09:31:19 +0200 Subject: fix memory leak --- testing/toxic/main.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'testing/toxic') diff --git a/testing/toxic/main.c b/testing/toxic/main.c index 27a34e09..fd3c732f 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -308,10 +308,13 @@ static int store_data(char *path) fd = fopen(path, "w"); if (fd == NULL) { + free(buf); return 2; } if (fwrite(buf, len, 1, fd) != 1) { + free(buf); + fclose(fd); return 3; } -- cgit v1.2.3 From bfa624139a3d4b7ef582c13bb571c6b95b6141c8 Mon Sep 17 00:00:00 2001 From: Michael Rose Date: Wed, 14 Aug 2013 11:37:58 +0200 Subject: populate friends in toxic on startup --- testing/toxic/main.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'testing/toxic') diff --git a/testing/toxic/main.c b/testing/toxic/main.c index fd3c732f..40b70ba5 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -286,6 +286,15 @@ static void do_tox() doMessenger(m); } +static void populate_friends() +{ + wprintw(prompt->window, "Populating friends...\n"); + for (int i = 0; i < m->numfriends; i++) { + wprintw(prompt->window, "Added friend %d\n", i); + friendlist_onFriendAdded(m, i); + } +} + /* * Store Messenger data to path * Return 0 Messenger stored successfully @@ -321,6 +330,8 @@ static int store_data(char *path) free(buf); fclose(fd); + wprintw(prompt->window, "Messenger stored\n"); + return 0; } @@ -348,7 +359,9 @@ static void load_data(char *path) { endwin(); exit(1); } - Messenger_load(m, buf, len); + if (Messenger_load(m, buf, len) != 0) { + fprintf(stderr, "Problem while loading messenger"); + } free(buf); fclose(fd); } else { @@ -479,8 +492,10 @@ int main(int argc, char *argv[]) init_windows(); init_window_status(); - if(f_loadfromfile) - load_data(DATA_FILE); + if(f_loadfromfile) { + load_data(DATA_FILE); + populate_friends(); + } if (f_flag == -1) { attron(COLOR_PAIR(3) | A_BOLD); -- cgit v1.2.3 From 5c7245fecdbb83cd6833e0fad3ea5a035d37a1e4 Mon Sep 17 00:00:00 2001 From: Michael Rose Date: Wed, 14 Aug 2013 11:59:11 +0200 Subject: C99 fix --- testing/toxic/main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'testing/toxic') diff --git a/testing/toxic/main.c b/testing/toxic/main.c index 40b70ba5..e9d285cc 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -289,7 +289,8 @@ static void do_tox() static void populate_friends() { wprintw(prompt->window, "Populating friends...\n"); - for (int i = 0; i < m->numfriends; i++) { + uint32_t i; + for (i = 0; i < m->numfriends; i++) { wprintw(prompt->window, "Added friend %d\n", i); friendlist_onFriendAdded(m, i); } -- cgit v1.2.3 From 76bfefff6f312024595fba8fd8115bbf93084946 Mon Sep 17 00:00:00 2001 From: Michael Rose Date: Thu, 15 Aug 2013 12:11:48 +0200 Subject: Merge fix --- testing/toxic/main.c | 114 +++++++++++++++++++++++++++++------------------- testing/toxic/prompt.c | 8 ++-- testing/toxic/windows.c | 6 +++ testing/toxic/windows.h | 1 + 4 files changed, 81 insertions(+), 48 deletions(-) (limited to 'testing/toxic') diff --git a/testing/toxic/main.c b/testing/toxic/main.c index aa025a1a..4d52469a 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -25,6 +25,8 @@ #include "prompt.h" #include "friendlist.h" +/* Export for use in Callbacks */ +char *DATA_FILE = NULL; void on_window_resize(int sig) { @@ -145,67 +147,90 @@ static void do_tox(Messenger *m, ToxWindow * prompt) doMessenger(m); } -static void load_data(Messenger *m, char *path) +/* + * Store Messenger to given location + * Return 0 stored successfully + * Return 1 malloc failed + * Return 2 opening path failed + * Return 3 fwrite failed + */ +int store_data(Messenger *m, char *path) { - FILE *fd; - size_t len; - uint8_t *buf; - - if ((fd = fopen(path, "r")) != NULL) { - fseek(fd, 0, SEEK_END); - len = ftell(fd); - fseek(fd, 0, SEEK_SET); + FILE *fd; + size_t len; + uint8_t *buf; - buf = malloc(len); - if (buf == NULL) { - fprintf(stderr, "malloc() failed.\n"); - fclose(fd); - endwin(); - exit(1); - } - if (fread(buf, len, 1, fd) != 1){ - fprintf(stderr, "fread() failed.\n"); - free(buf); - fclose(fd); - endwin(); - exit(1); - } - Messenger_load(m, buf, len); - } - else { len = Messenger_size(m); buf = malloc(len); if (buf == NULL) { - fprintf(stderr, "malloc() failed.\n"); - endwin(); - exit(1); + return 1; } Messenger_save(m, buf); fd = fopen(path, "w"); if (fd == NULL) { - fprintf(stderr, "fopen() failed.\n"); - free(buf); - endwin(); - exit(1); + free(buf); + return 2; } - if (fwrite(buf, len, 1, fd) != 1){ - fprintf(stderr, "fwrite() failed.\n"); - free(buf); - fclose(fd); - endwin(); - exit(1); + if (fwrite(buf, len, 1, fd) != 1) { + free(buf); + fclose(fd); + return 3; + } + + free(buf); + fclose(fd); + return 0; +} + +static void load_data(Messenger *m, char *path) +{ + FILE *fd; + size_t len; + uint8_t *buf; + + if ((fd = fopen(path, "r")) != NULL) { + fseek(fd, 0, SEEK_END); + len = ftell(fd); + fseek(fd, 0, SEEK_SET); + + buf = malloc(len); + if (buf == NULL) { + fprintf(stderr, "malloc() failed.\n"); + fclose(fd); + endwin(); + exit(1); + } + if (fread(buf, len, 1, fd) != 1) { + fprintf(stderr, "fread() failed.\n"); + free(buf); + fclose(fd); + endwin(); + exit(1); + } + Messenger_load(m, buf, len); + + uint32_t i; + for (i = 0; i < m->numfriends; i++) { + on_friendadded(m, i); + } + + free(buf); + fclose(fd); + } else { + int st; + if ((st = store_data(m, path)) != 0) { + fprintf(stderr, "Store messenger failed with return code: %d\n", st); + endwin(); + exit(1); + } } - } - free(buf); - fclose(fd); } int main(int argc, char *argv[]) { char *user_config_dir = get_user_config_dir(); - char *DATA_FILE = NULL; int config_err = 0; int f_loadfromfile = 1; @@ -246,7 +271,6 @@ int main(int argc, char *argv[]) if(f_loadfromfile) load_data(m, DATA_FILE); - free(DATA_FILE); if (f_flag == -1) { attron(COLOR_PAIR(3) | A_BOLD); @@ -268,6 +292,8 @@ int main(int argc, char *argv[]) /* Draw */ draw_active_window(m); } + cleanupMessenger(m); + free(DATA_FILE); return 0; } diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index b84773af..1d0e0144 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c @@ -16,7 +16,7 @@ uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX uint8_t num_requests=0; // XXX -static friendAddedFn *on_friendadded; +static friendAddedFn *on_friendadded_cb; static char prompt_buf[MAX_STR_SIZE] = {0}; static int prompt_buf_pos = 0; @@ -88,7 +88,7 @@ void cmd_accept(ToxWindow *self, Messenger *m, char **args) wprintw(self->window, "Failed to add friend.\n"); else { wprintw(self->window, "Friend accepted as: %d.\n", num); - on_friendadded(m, num); + on_friendadded_cb(m, num); } } @@ -152,7 +152,7 @@ void cmd_add(ToxWindow *self, Messenger *m, char **args) break; default: wprintw(self->window, "Friend added as %d.\n", num); - on_friendadded(m, num); + on_friendadded_cb(m, num); break; } } @@ -436,7 +436,7 @@ static void prompt_onInit(ToxWindow *self, Messenger *m) ToxWindow new_prompt(friendAddedFn *f) { - on_friendadded = f; + on_friendadded_cb = f; ToxWindow ret; memset(&ret, 0, sizeof(ret)); ret.onKey = &prompt_onKey; diff --git a/testing/toxic/windows.c b/testing/toxic/windows.c index 6de50741..15caddf7 100644 --- a/testing/toxic/windows.c +++ b/testing/toxic/windows.c @@ -3,6 +3,9 @@ #include "dhtstatus.h" #include "windows.h" +extern char *DATA_FILE; +extern int store_data(Messenger *m, char *path); + /* Holds status of chat windows */ char WINDOW_STATUS[MAX_WINDOW_SLOTS]; @@ -74,6 +77,9 @@ void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t l void on_friendadded(Messenger *m, int friendnumber) { friendlist_onFriendAdded(m, friendnumber); + if (store_data(m, DATA_FILE) != 0) { + wprintw(prompt->window, "\nCould not store Messenger data\n"); + } } /* CALLBACKS END */ diff --git a/testing/toxic/windows.h b/testing/toxic/windows.h index 893ccf6f..9f4d74a2 100644 --- a/testing/toxic/windows.h +++ b/testing/toxic/windows.h @@ -47,6 +47,7 @@ void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); +void on_friendadded(Messenger *m, int friendnumber); void init_window_status(); ToxWindow * init_windows(); void draw_active_window(Messenger * m); -- cgit v1.2.3