From 69c467fa5f3a133f62b2ffa9ee321755a0bc0e18 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Thu, 20 Feb 2014 07:47:19 -0500 Subject: [PATCH 01/58] rm unnecessary file transfer limit --- src/main.c | 2 +- src/prompt.c | 2 +- src/toxic_windows.h | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index 3a68fbf..b8b3d47 100644 --- a/src/main.c +++ b/src/main.c @@ -407,7 +407,7 @@ static void do_file_senders(Tox *m) int pieces = 0; - while (pieces++ < MAX_PIECES_SEND) { + while (true) { if (tox_file_send_data(m, friendnum, filenum, file_senders[i].nextpiece, file_senders[i].piecelen) == -1) break; diff --git a/src/prompt.c b/src/prompt.c index 741b9e0..f93e32c 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -162,7 +162,7 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) else if (key == KEY_END) { /* END key: move cursor to end of line */ if (prt->pos != prt->len) prt->pos = prt->len; - } + } else if (key == KEY_LEFT) { if (prt->pos > 0) diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 3a3d62e..ae126db 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -149,7 +149,6 @@ struct PromptBuf { #define MAX_FILES 256 #define FILE_PIECE_SIZE 1024 #define TIMEOUT_FILESENDER 300 -#define MAX_PIECES_SEND 100 /* Max number of pieces to send per file per call to do_file_senders() */ typedef struct { FILE *file; From d9142eb355551a4aa94cad37ce534ceaf6429b0b Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Fri, 21 Feb 2014 00:45:29 -0500 Subject: [PATCH 02/58] restructure main toxic loop --- src/main.c | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/main.c b/src/main.c index b8b3d47..2f1515e 100644 --- a/src/main.c +++ b/src/main.c @@ -232,7 +232,7 @@ int init_connection(Tox *m) return 5; } -static void do_tox(Tox *m, ToxWindow *prompt) +static void do_connection(Tox *m, ToxWindow *prompt) { static int conn_try = 0; static int conn_err = 0; @@ -258,8 +258,6 @@ static void do_tox(Tox *m, ToxWindow *prompt) prep_prompt_win(); wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n"); } - - tox_do(m); } int f_loadfromfile; @@ -405,8 +403,6 @@ static void do_file_senders(Tox *m) continue; } - int pieces = 0; - while (true) { if (tox_file_send_data(m, friendnum, filenum, file_senders[i].nextpiece, file_senders[i].piecelen) == -1) @@ -432,21 +428,17 @@ static void do_file_senders(Tox *m) } } -/* This should only be called on exit */ -static void close_file_transfers(Tox *m) +void exit_toxic(Tox *m) { + store_data(m, DATA_FILE); + int i; for (i = 0; i < max_file_senders_index; ++i) { if (file_senders[i].active) fclose(file_senders[i].file); } -} -void exit_toxic(Tox *m) -{ - store_data(m, DATA_FILE); - close_file_transfers(m); free(DATA_FILE); free(SRVLIST_FILE); free(prompt->stb); @@ -456,6 +448,16 @@ void exit_toxic(Tox *m) exit(EXIT_SUCCESS); } +static void do_toxic(Tox *m, ToxWindow *prompt) +{ + do_connection(m, prompt); + do_file_senders(m); + draw_active_window(m); + + /* main toxcore loop */ + tox_do(m); +} + int main(int argc, char *argv[]) { char *user_config_dir = get_user_config_dir(); @@ -549,12 +551,8 @@ int main(int argc, char *argv[]) prompt_init_statusbar(prompt, m); sort_friendlist_index(m); - while (true) { - do_tox(m, prompt); - do_file_senders(m); - draw_active_window(m); - } + while (true) + do_toxic(m, prompt); - exit_toxic(m); return 0; } From 4294e39b49ded3f08d4fcaf8fce11655bddd9c0d Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Fri, 21 Feb 2014 01:57:13 -0500 Subject: [PATCH 03/58] check connection only once per call --- src/main.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main.c b/src/main.c index 2f1515e..465bb7b 100644 --- a/src/main.c +++ b/src/main.c @@ -238,20 +238,22 @@ static void do_connection(Tox *m, ToxWindow *prompt) static int conn_err = 0; static bool dht_on = false; - if (!dht_on && !tox_isconnected(m) && !(conn_try++ % 100)) { + bool is_connected = tox_isconnected(m); + + if (!dht_on && !is_connected && !(conn_try++ % 100)) { prep_prompt_win(); if (!conn_err) { wprintw(prompt->window, "Establishing connection...\n"); if ((conn_err = init_connection(m))) wprintw(prompt->window, "\nAuto-connect failed with error code %d\n", conn_err); } - } else if (!dht_on && tox_isconnected(m)) { + } else if (!dht_on && is_connected) { dht_on = true; prompt_update_connectionstatus(prompt, dht_on); prep_prompt_win(); wprintw(prompt->window, "\nDHT connected.\n"); - } else if (dht_on && !tox_isconnected(m)) { + } else if (dht_on && !is_connected) { dht_on = false; prompt_update_connectionstatus(prompt, dht_on); @@ -451,10 +453,10 @@ void exit_toxic(Tox *m) static void do_toxic(Tox *m, ToxWindow *prompt) { do_connection(m, prompt); - do_file_senders(m); draw_active_window(m); + do_file_senders(m); - /* main toxcore loop */ + /* main tox-core loop */ tox_do(m); } From a68b733d581259423bfde9b118022f80ba4751f3 Mon Sep 17 00:00:00 2001 From: Amit Aryeh Levy Date: Fri, 21 Feb 2014 01:25:49 -0800 Subject: [PATCH 04/58] umask such that stored files are u+rw only --- src/main.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main.c b/src/main.c index 465bb7b..3b9bcc1 100644 --- a/src/main.c +++ b/src/main.c @@ -470,6 +470,9 @@ int main(int argc, char *argv[]) int i = 0; int f_use_ipv4 = 0; + // Make sure all written files are read/writeable only by the current user. + umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + for (i = 0; i < argc; ++i) { if (argv[i] == NULL) break; From 2982dc6ddd54cc343527d4958d8f3e8c360013e6 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Fri, 21 Feb 2014 21:21:12 -0500 Subject: [PATCH 05/58] show pseudo-unique identifier in friend chat windows --- src/chat.c | 15 ++++++++++++--- src/friendlist.c | 1 + src/friendlist.h | 3 +++ src/main.c | 3 +++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/chat.c b/src/chat.c index efec9fa..c404a1e 100644 --- a/src/chat.c +++ b/src/chat.c @@ -547,9 +547,9 @@ static void chat_onDraw(ToxWindow *self, Tox *m) self->x = x2; /* Truncate note if it doesn't fit in statusbar */ - uint16_t maxlen = x2 - getcurx(statusbar->topline) - 4; + uint16_t maxlen = x2 - getcurx(statusbar->topline) - (KEY_IDENT_DIGITS * 2) - 6; if (statusbar->statusmsg_len > maxlen) { - statusbar->statusmsg[maxlen] = '\0'; + statusbar->statusmsg[maxlen-1] = '\0'; statusbar->statusmsg_len = maxlen; } @@ -559,7 +559,16 @@ static void chat_onDraw(ToxWindow *self, Tox *m) wattroff(statusbar->topline, A_BOLD); } - wprintw(statusbar->topline, "\n"); + wclrtoeol(statusbar->topline); + wmove(statusbar->topline, 0, x2 - (KEY_IDENT_DIGITS * 2) - 3); + wprintw(statusbar->topline, "{"); + + int i; + + for (i = 0; i < KEY_IDENT_DIGITS; ++i) + wprintw(statusbar->topline, "%x", friends[self->num].pub_key[i] & 0xff); + + wprintw(statusbar->topline, "}\n"); mvwhline(ctx->linewin, 0, 0, ACS_HLINE, x2); } diff --git a/src/friendlist.c b/src/friendlist.c index f569405..2e425d0 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -130,6 +130,7 @@ static void friendlist_onFriendAdded(ToxWindow *self, Tox *m, int num, bool sort friends[i].online = false; friends[i].status = TOX_USERSTATUS_NONE; friends[i].namelength = tox_get_name(m, num, friends[i].name); + tox_get_client_id(m, num, friends[i].pub_key); if (friends[i].namelength == -1 || friends[i].name[0] == '\0') { strcpy(friends[i].name, (uint8_t *) UNKNOWN_NAME); diff --git a/src/friendlist.h b/src/friendlist.h index 3abc781..0dd4a8f 100644 --- a/src/friendlist.h +++ b/src/friendlist.h @@ -3,12 +3,15 @@ #include "toxic_windows.h" +#define KEY_IDENT_DIGITS 2 + typedef struct { uint8_t name[TOX_MAX_NAME_LENGTH]; uint16_t namelength; uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH]; uint16_t statusmsg_len; uint8_t pending_groupchat[TOX_CLIENT_ID_SIZE]; + uint8_t pub_key[TOX_CLIENT_ID_SIZE]; int num; int chatwin; bool active; diff --git a/src/main.c b/src/main.c index 465bb7b..3b9bcc1 100644 --- a/src/main.c +++ b/src/main.c @@ -470,6 +470,9 @@ int main(int argc, char *argv[]) int i = 0; int f_use_ipv4 = 0; + // Make sure all written files are read/writeable only by the current user. + umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + for (i = 0; i < argc; ++i) { if (argv[i] == NULL) break; From 7e23afb57e38207a21b2a89d4295d8efefd8b1e8 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Fri, 21 Feb 2014 21:24:33 -0500 Subject: [PATCH 06/58] make friendlist offline statuses bold --- src/friendlist.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/friendlist.c b/src/friendlist.c index 2e425d0..7e22091 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -364,7 +364,11 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) wprintw(self->window, " (%s)\n", friends[f].statusmsg); } else { - wprintw(self->window, "[O]"); + wprintw(self->window, "["); + wattron(self->window, A_BOLD); + wprintw(self->window, "O"); + wattroff(self->window, A_BOLD); + wprintw(self->window, "]"); if (f_selected) wattron(self->window, A_BOLD); From a4cb56855819c6c5d1ecf18961fd388e1b014b52 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Fri, 21 Feb 2014 22:02:16 -0500 Subject: [PATCH 07/58] small fix --- src/chat.c | 4 ++-- src/friendlist.h | 2 -- src/toxic_windows.h | 1 + 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/chat.c b/src/chat.c index c404a1e..a62b94e 100644 --- a/src/chat.c +++ b/src/chat.c @@ -547,9 +547,9 @@ static void chat_onDraw(ToxWindow *self, Tox *m) self->x = x2; /* Truncate note if it doesn't fit in statusbar */ - uint16_t maxlen = x2 - getcurx(statusbar->topline) - (KEY_IDENT_DIGITS * 2) - 6; + uint16_t maxlen = x2 - getcurx(statusbar->topline) - (KEY_IDENT_DIGITS * 2) - 7; if (statusbar->statusmsg_len > maxlen) { - statusbar->statusmsg[maxlen-1] = '\0'; + statusbar->statusmsg[maxlen] = '\0'; statusbar->statusmsg_len = maxlen; } diff --git a/src/friendlist.h b/src/friendlist.h index 0dd4a8f..029c9a6 100644 --- a/src/friendlist.h +++ b/src/friendlist.h @@ -3,8 +3,6 @@ #include "toxic_windows.h" -#define KEY_IDENT_DIGITS 2 - typedef struct { uint8_t name[TOX_MAX_NAME_LENGTH]; uint16_t namelength; diff --git a/src/toxic_windows.h b/src/toxic_windows.h index ae126db..774b790 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -26,6 +26,7 @@ #define N_DEFAULT_WINS 2 /* number of permanent default windows */ #define CURS_Y_OFFSET 3 /* y-axis cursor offset for chat contexts */ #define CHATBOX_HEIGHT 4 +#define KEY_IDENT_DIGITS 2 /* number of hex digits to display for the pub-key based identifier */ #define EXIT_SUCCESS 0 #define EXIT_FAILURE 1 From e8a72146b0bfca41521392b8ba360caed6391250 Mon Sep 17 00:00:00 2001 From: kl4ng Date: Fri, 21 Feb 2014 23:59:11 -0500 Subject: [PATCH 08/58] added note about ncurses libraries --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 2afa965..65323f2 100644 --- a/README.md +++ b/README.md @@ -14,3 +14,7 @@ Then execute the configure script with ./configure (you may need to pass it the echo '/usr/local/lib/' | sudo tee -a /etc/ld.so.conf.d/locallib.conf sudo ldconfig ``` +If you dont already have it, you might want to install the ncurses libraries, on Debian: +``` +sudo apt-get install libncurses5-dev +``` From 77875d0ca6769353e80b527d1ff6d7e5972f5bc1 Mon Sep 17 00:00:00 2001 From: kl4ng Date: Sat, 22 Feb 2014 00:18:29 -0500 Subject: [PATCH 09/58] typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 65323f2..2297569 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Then execute the configure script with ./configure (you may need to pass it the echo '/usr/local/lib/' | sudo tee -a /etc/ld.so.conf.d/locallib.conf sudo ldconfig ``` -If you dont already have it, you might want to install the ncurses libraries, on Debian: +If you dont already have them, you might want to install the ncurses libraries, on Debian: ``` sudo apt-get install libncurses5-dev ``` From c7a2643ee03b882c755be9f37e234f29837fbf51 Mon Sep 17 00:00:00 2001 From: kl4ng Date: Sat, 22 Feb 2014 00:26:46 -0500 Subject: [PATCH 10/58] added other required package --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2297569..28fb244 100644 --- a/README.md +++ b/README.md @@ -16,5 +16,5 @@ sudo ldconfig ``` If you dont already have them, you might want to install the ncurses libraries, on Debian: ``` -sudo apt-get install libncurses5-dev +sudo apt-get install libncurses5-dev libncursesw5-dev ``` From fd3604be44dcb3ea35315af543cbd26231110bc1 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sat, 22 Feb 2014 01:04:40 -0500 Subject: [PATCH 11/58] small fix --- src/chat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chat.c b/src/chat.c index a62b94e..d89bfdd 100644 --- a/src/chat.c +++ b/src/chat.c @@ -566,7 +566,7 @@ static void chat_onDraw(ToxWindow *self, Tox *m) int i; for (i = 0; i < KEY_IDENT_DIGITS; ++i) - wprintw(statusbar->topline, "%x", friends[self->num].pub_key[i] & 0xff); + wprintw(statusbar->topline, "%02X", friends[self->num].pub_key[i] & 0xff); wprintw(statusbar->topline, "}\n"); mvwhline(ctx->linewin, 0, 0, ACS_HLINE, x2); From 61b2a7f0c9f7532163962403b570def494af33cb Mon Sep 17 00:00:00 2001 From: Sean Date: Sat, 22 Feb 2014 00:59:33 -0800 Subject: [PATCH 12/58] Use ssl --- configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 4fcd5d1..4948029 100644 --- a/configure.ac +++ b/configure.ac @@ -2,7 +2,7 @@ # Process this file with autoconf to produce a configure script. AC_PREREQ([2.65]) -AC_INIT([toxic], [0.2.4], [http://tox.im/]) +AC_INIT([toxic], [0.2.3], [https://tox.im/]) AC_CONFIG_AUX_DIR(configure_aux) AC_CONFIG_SRCDIR([src/main.c]) AC_CONFIG_HEADERS([config.h]) From 198f9af71d5d69a14dc878a7929062bf52bdf212 Mon Sep 17 00:00:00 2001 From: Sean Date: Sat, 22 Feb 2014 01:21:37 -0800 Subject: [PATCH 13/58] Fine --- configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 4948029..7bd3426 100644 --- a/configure.ac +++ b/configure.ac @@ -2,7 +2,7 @@ # Process this file with autoconf to produce a configure script. AC_PREREQ([2.65]) -AC_INIT([toxic], [0.2.3], [https://tox.im/]) +AC_INIT([toxic], [0.2.4.1], [https://tox.im/]) AC_CONFIG_AUX_DIR(configure_aux) AC_CONFIG_SRCDIR([src/main.c]) AC_CONFIG_HEADERS([config.h]) From 3960208fd6332816d529b7cdf2283c5b89a11363 Mon Sep 17 00:00:00 2001 From: Sean Date: Sat, 22 Feb 2014 01:25:10 -0800 Subject: [PATCH 14/58] Update configure.ac --- configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 7bd3426..6c88146 100644 --- a/configure.ac +++ b/configure.ac @@ -2,7 +2,7 @@ # Process this file with autoconf to produce a configure script. AC_PREREQ([2.65]) -AC_INIT([toxic], [0.2.4.1], [https://tox.im/]) +AC_INIT([toxic], [0.2.5], [https://tox.im/]) AC_CONFIG_AUX_DIR(configure_aux) AC_CONFIG_SRCDIR([src/main.c]) AC_CONFIG_HEADERS([config.h]) From aec16a22a454bf153bfa34890edb3dab01a050ec Mon Sep 17 00:00:00 2001 From: Sean Date: Sat, 22 Feb 2014 01:39:32 -0800 Subject: [PATCH 15/58] Update prompt.c --- src/prompt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/prompt.c b/src/prompt.c index f93e32c..efbecde 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -418,7 +418,7 @@ void prompt_init_statusbar(ToxWindow *self, Tox *m) snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick); /* temporary until statusmessage saving works */ - uint8_t *statusmsg = "Toxing on Toxic v.0.2.4"; + uint8_t *statusmsg = "Toxing on Toxic v.0.2.5-git"; m_set_statusmessage(m, statusmsg, strlen(statusmsg) + 1); snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); From 9e2fde8d84293201bcf122d2a7516bb5b9a560e7 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sat, 22 Feb 2014 04:46:35 -0500 Subject: [PATCH 16/58] make status message version not hard-coded --- src/prompt.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/prompt.c b/src/prompt.c index f93e32c..1b9ba3c 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -418,7 +418,12 @@ void prompt_init_statusbar(ToxWindow *self, Tox *m) snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick); /* temporary until statusmessage saving works */ - uint8_t *statusmsg = "Toxing on Toxic v.0.2.4"; + uint8_t ver[strlen(TOXICVER) + 1]; + uint8_t statusmsg[MAX_STR_SIZE]; + strcpy(ver, TOXICVER); + uint8_t *toxic_ver = strtok(ver, "_"); + snprintf(statusmsg, MAX_STR_SIZE, "Toxing on Toxic v.%s", toxic_ver); + m_set_statusmessage(m, statusmsg, strlen(statusmsg) + 1); snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); From da6ef159e6881a338ede4a047506e500e38fc352 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sat, 22 Feb 2014 04:50:47 -0500 Subject: [PATCH 17/58] null check just incase --- src/prompt.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/prompt.c b/src/prompt.c index 1b9ba3c..6d0a238 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -422,7 +422,11 @@ void prompt_init_statusbar(ToxWindow *self, Tox *m) uint8_t statusmsg[MAX_STR_SIZE]; strcpy(ver, TOXICVER); uint8_t *toxic_ver = strtok(ver, "_"); - snprintf(statusmsg, MAX_STR_SIZE, "Toxing on Toxic v.%s", toxic_ver); + + if (toxic_ver != NULL) + snprintf(statusmsg, MAX_STR_SIZE, "Toxing on Toxic v.%s", toxic_ver); + else + snprintf(statusmsg, MAX_STR_SIZE, "Toxing on Toxic hacker edition"); m_set_statusmessage(m, statusmsg, strlen(statusmsg) + 1); snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); From 352656230cbfea103cd2ddadef032cfb81613b2f Mon Sep 17 00:00:00 2001 From: Sean Date: Sat, 22 Feb 2014 01:51:30 -0800 Subject: [PATCH 18/58] HOW DO YOU FORGET ABOUT THIS FOR 6 MONTHS??? --- src/CMakeLists.txt | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 src/CMakeLists.txt diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt deleted file mode 100644 index 53316af..0000000 --- a/src/CMakeLists.txt +++ /dev/null @@ -1,23 +0,0 @@ -cmake_minimum_required(VERSION 2.6.0) -project(toxic C) - -execute_process(COMMAND git rev-list HEAD --count OUTPUT_VARIABLE COMMIT) -SET(GCC_COVERAGE_COMPILE_FLAGS '-DTOXICVER="0.1.1_r${COMMIT}"') -add_definitions(${GCC_COVERAGE_COMPILE_FLAGS}) -set(exe_name toxic) - -add_executable(${exe_name} - main.c - windows.c - prompt.c - friendlist.c - dhtstatus.c - chat.c - configdir.c) - -include_directories(${CURSES_INCLUDE_DIR}) - -target_link_libraries(${exe_name} - ${CURSES_LIBRARIES}) - -linkCoreLibraries(${exe_name}) From 2b707f1d808f06de5c641c55e487379e0b89ed1b Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sun, 23 Feb 2014 04:28:33 -0500 Subject: [PATCH 19/58] implemented typing status --- src/chat.c | 39 ++++++++++++++++++++++++++++++++++++++- src/friendlist.h | 1 + src/main.c | 1 + src/toxic_windows.h | 4 ++++ src/windows.c | 10 ++++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/chat.c b/src/chat.c index d89bfdd..6292f39 100644 --- a/src/chat.c +++ b/src/chat.c @@ -45,6 +45,14 @@ static const uint8_t chat_cmd_list[AC_NUM_CHAT_COMMANDS][MAX_CMDNAME_SIZE] = { { "/status" }, }; +static void set_typingstatus(ToxWindow *self, Tox *m, bool is_typing) +{ + ChatContext *ctx = self->chatwin; + + tox_set_user_is_typing(m, self->num, is_typing); + ctx->self_is_typing = is_typing; +} + static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len) { if (self->num != num) @@ -77,7 +85,22 @@ static void chat_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t st return; StatusBar *statusbar = self->stb; - statusbar->is_online = status == 1 ? true : false; + + if (status == 1) { + statusbar->is_online = true; + friends[num].is_typing = tox_get_is_typing(m, num); + } else { + statusbar->is_online = false; + friends[num].is_typing = false; + } +} + +static void chat_onTypingChange(ToxWindow *self, Tox *m, int num, int is_typing) +{ + if (self->num != num) + return; + + friends[num].is_typing = is_typing; } static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uint16_t len) @@ -416,6 +439,9 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) else wmove(self->window, y, x + MAX(1, wcwidth(key))); } + + if (!ctx->self_is_typing) + set_typingstatus(self, m, true); } /* RETURN key: Execute command or print line */ else if (key == '\n') { @@ -473,6 +499,9 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) reset_buf(ctx->line, &ctx->pos, &ctx->len); } } + + if (ctx->len <= 0 && ctx->self_is_typing) + set_typingstatus(self, m, false); } static void chat_onDraw(ToxWindow *self, Tox *m) @@ -523,9 +552,16 @@ static void chat_onDraw(ToxWindow *self, Tox *m) break; } + if (friends[self->num].is_typing) + wattron(statusbar->topline, COLOR_PAIR(YELLOW)); + wattron(statusbar->topline, A_BOLD); wprintw(statusbar->topline, " %s ", self->name); wattroff(statusbar->topline, A_BOLD); + + if (friends[self->num].is_typing) + wattroff(statusbar->topline, COLOR_PAIR(YELLOW)); + wattron(statusbar->topline, COLOR_PAIR(colour) | A_BOLD); wprintw(statusbar->topline, "[%s]", status_text); wattroff(statusbar->topline, COLOR_PAIR(colour) | A_BOLD); @@ -611,6 +647,7 @@ ToxWindow new_chat(Tox *m, int friendnum) ret.onInit = &chat_onInit; ret.onMessage = &chat_onMessage; ret.onConnectionChange = &chat_onConnectionChange; + ret.onTypingChange = & chat_onTypingChange; ret.onGroupInvite = &chat_onGroupInvite; ret.onNickChange = &chat_onNickChange; ret.onStatusChange = &chat_onStatusChange; diff --git a/src/friendlist.h b/src/friendlist.h index 029c9a6..0f29f3c 100644 --- a/src/friendlist.h +++ b/src/friendlist.h @@ -14,6 +14,7 @@ typedef struct { int chatwin; bool active; bool online; + bool is_typing; TOX_USERSTATUS status; struct FileReceiver file_receiver; } ToxicFriend; diff --git a/src/main.c b/src/main.c index 3b9bcc1..9c30b43 100644 --- a/src/main.c +++ b/src/main.c @@ -108,6 +108,7 @@ static Tox *init_tox(int ipv4) /* Callbacks */ tox_callback_connection_status(m, on_connectionchange, NULL); + tox_callback_typing_change(m, on_typing_change, NULL); tox_callback_friend_request(m, on_request, NULL); tox_callback_friend_message(m, on_message, NULL); tox_callback_name_change(m, on_nickchange, NULL); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 774b790..eec6ca9 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -84,6 +84,7 @@ struct ToxWindow { void(*onFileSendRequest)(ToxWindow *, Tox *, int, uint8_t, uint64_t, uint8_t *, uint16_t); void(*onFileControl)(ToxWindow *, Tox *, int, uint8_t, uint8_t, uint8_t, uint8_t *, uint16_t); void(*onFileData)(ToxWindow *, Tox *, int, uint8_t, uint8_t *, uint16_t); + void(*onTypingChange)(ToxWindow *, Tox *, int, int); char name[TOX_MAX_NAME_LENGTH]; int num; @@ -124,6 +125,8 @@ struct ChatContext { int hst_pos; int hst_tot; + bool self_is_typing; + WINDOW *history; WINDOW *linewin; WINDOW *sidebar; @@ -185,6 +188,7 @@ void on_group_namelistchange(Tox *m, int groupnumber, int peernumber, uint8_t ch void on_file_sendrequest(Tox *m, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *pathname, uint16_t pathname_length, void *userdata); void on_file_control(Tox *m, int friendnumber, uint8_t receive_send, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata); void on_file_data(Tox *m, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata); +void on_typing_change(Tox *m, int friendnumber, int is_typing, void *userdata); ToxWindow *init_windows(Tox *m); void draw_active_window(Tox *m); diff --git a/src/windows.c b/src/windows.c index 8e068f5..326b659 100644 --- a/src/windows.c +++ b/src/windows.c @@ -37,6 +37,16 @@ void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdat } } +void on_typing_change(Tox *m, int friendnumber, int is_typing, void *userdata) +{ + int i; + + for (i = 0; i < MAX_WINDOWS_NUM; ++i) { + if (windows[i].onTypingChange != NULL) + windows[i].onTypingChange(&windows[i], m, friendnumber, is_typing); + } +} + void on_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) { int i; From 9f309ecb96b316a675c01bc3bb4b2ca935ef7cbf Mon Sep 17 00:00:00 2001 From: Sean Date: Sun, 23 Feb 2014 01:37:43 -0800 Subject: [PATCH 20/58] Move to 0.2.6 --- configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 6c88146..9fe3e35 100644 --- a/configure.ac +++ b/configure.ac @@ -2,7 +2,7 @@ # Process this file with autoconf to produce a configure script. AC_PREREQ([2.65]) -AC_INIT([toxic], [0.2.5], [https://tox.im/]) +AC_INIT([toxic], [0.2.6], [https://tox.im/]) AC_CONFIG_AUX_DIR(configure_aux) AC_CONFIG_SRCDIR([src/main.c]) AC_CONFIG_HEADERS([config.h]) From 6be1c907d93f558f99fae5b0206573381b41fc62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Llu=C3=ADs=20Batlle=20i=20Rossell?= Date: Sun, 23 Feb 2014 09:49:41 +0000 Subject: [PATCH 21/58] Fallback to loading /usr/share/toxic/DHTservers. First try ~/.config/tox/DHTservers, and fallback to the /usr/share one if the former fails. This should allow people just starting toxic for the first time to connect to the DHT. --- src/main.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/main.c b/src/main.c index 9c30b43..7ea172c 100644 --- a/src/main.c +++ b/src/main.c @@ -146,11 +146,14 @@ static char servers[MAXSERVERS][SERVERLEN]; static uint16_t ports[MAXSERVERS]; static uint8_t keys[MAXSERVERS][TOX_CLIENT_ID_SIZE]; -int serverlist_load(void) +static int serverlist_load(const char *filename) { FILE *fp = NULL; - fp = fopen(SRVLIST_FILE, "r"); + if (!filename) + return 1; + + fp = fopen(filename, "r"); if (fp == NULL) return 1; @@ -213,9 +216,15 @@ int init_connection(Tox *m) */ if (!init_connection_serverlist_loaded) { init_connection_serverlist_loaded = 1; - int res = serverlist_load(); + int res = serverlist_load(SRVLIST_FILE); if (res) - return res; + { + // Fallback on the provided DHTServers in /usr/share, + // so new starts of toxic will connect to the DHT. + serverlist_load(PACKAGE_DATADIR "/DHTservers"); + if (res) + return res; + } if (!linecnt) return 4; @@ -509,19 +518,15 @@ int main(int argc, char *argv[]) } } - if (config_err) { - SRVLIST_FILE = strdup(PACKAGE_DATADIR "/DHTservers"); + SRVLIST_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("DHTservers") + 1); + if (SRVLIST_FILE != NULL) { + strcpy(SRVLIST_FILE, user_config_dir); + strcat(SRVLIST_FILE, CONFIGDIR); + strcat(SRVLIST_FILE, "DHTservers"); } else { - SRVLIST_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("DHTservers") + 1); - if (SRVLIST_FILE != NULL) { - strcpy(SRVLIST_FILE, user_config_dir); - strcat(SRVLIST_FILE, CONFIGDIR); - strcat(SRVLIST_FILE, "DHTservers"); - } else { - endwin(); - fprintf(stderr, "malloc() failed. Aborting...\n"); - exit(EXIT_FAILURE); - } + endwin(); + fprintf(stderr, "malloc() failed. Aborting...\n"); + exit(EXIT_FAILURE); } free(user_config_dir); From 93bcecde70abe34367d176ab07d5a222c96c05a1 Mon Sep 17 00:00:00 2001 From: Sean Date: Sun, 23 Feb 2014 02:11:51 -0800 Subject: [PATCH 22/58] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 28fb244..07ae487 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ The client formerly resided in the [Tox core repository](https://github.com/irungentoo/ProjectTox-Core) and is now available as a standalone program. It looks like [this](http://wiki.tox.im/images/b/b6/Ncursesclient1.png). +#Note: run git checkout 0.2.5, 0.2.6 segfaults due to a bug + To compile, first generate the configure script by running the ```autoreconf -i``` command. Then execute the configure script with ./configure (you may need to pass it the location of your dependency libraries, i.e.): From 5b9d3f6f62b7aab86b99a1d6b573e31f9e374884 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sun, 23 Feb 2014 05:15:48 -0500 Subject: [PATCH 23/58] fix segfault --- src/chat.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/chat.c b/src/chat.c index 6292f39..4e8c79b 100644 --- a/src/chat.c +++ b/src/chat.c @@ -497,11 +497,11 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) free(statusbar); } else { reset_buf(ctx->line, &ctx->pos, &ctx->len); + + if (ctx->len <= 0 && ctx->self_is_typing) + set_typingstatus(self, m, false); } } - - if (ctx->len <= 0 && ctx->self_is_typing) - set_typingstatus(self, m, false); } static void chat_onDraw(ToxWindow *self, Tox *m) From ad8f99dae469993753e32ebe8f70b2afcd99e760 Mon Sep 17 00:00:00 2001 From: Sean Date: Sun, 23 Feb 2014 02:19:22 -0800 Subject: [PATCH 24/58] Revert that --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 07ae487..28fb244 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,6 @@ The client formerly resided in the [Tox core repository](https://github.com/irungentoo/ProjectTox-Core) and is now available as a standalone program. It looks like [this](http://wiki.tox.im/images/b/b6/Ncursesclient1.png). -#Note: run git checkout 0.2.5, 0.2.6 segfaults due to a bug - To compile, first generate the configure script by running the ```autoreconf -i``` command. Then execute the configure script with ./configure (you may need to pass it the location of your dependency libraries, i.e.): From b018aa384e2f8dfe134db0e1abc4277f9d14b080 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sun, 23 Feb 2014 05:38:44 -0500 Subject: [PATCH 25/58] small fix and don't show typing alert for /commands --- src/chat.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/chat.c b/src/chat.c index 4e8c79b..1bd192c 100644 --- a/src/chat.c +++ b/src/chat.c @@ -312,6 +312,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) getyx(self->window, y, x); getmaxyx(self->window, y2, x2); int cur_len = 0; + bool close_win = false; if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key: Remove character behind pos */ if (ctx->pos > 0) { @@ -440,7 +441,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) wmove(self->window, y, x + MAX(1, wcwidth(key))); } - if (!ctx->self_is_typing) + if (!ctx->self_is_typing && ctx->line[0] != '/') set_typingstatus(self, m, true); } /* RETURN key: Execute command or print line */ @@ -453,7 +454,6 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) wclear(ctx->linewin); wmove(self->window, y2 - CURS_Y_OFFSET, 0); wclrtobot(self->window); - bool close_win = false; if (!string_is_empty(line)) add_line_to_hist(ctx->line, ctx->len, ctx->ln_history, &ctx->hst_tot, &ctx->hst_pos); @@ -497,11 +497,13 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) free(statusbar); } else { reset_buf(ctx->line, &ctx->pos, &ctx->len); - - if (ctx->len <= 0 && ctx->self_is_typing) - set_typingstatus(self, m, false); } } + + if (!close_win) { + if (ctx->len <= 0 && ctx->self_is_typing) + set_typingstatus(self, m, false); + } } static void chat_onDraw(ToxWindow *self, Tox *m) From 5187861b699c1f1b86af01d96516ab30458c9d26 Mon Sep 17 00:00:00 2001 From: kl4ng Date: Sun, 23 Feb 2014 14:22:45 -0500 Subject: [PATCH 26/58] down arrow returns empty string if at end of history --- src/toxic_strings.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/toxic_strings.c b/src/toxic_strings.c index 15cff71..11d6ad0 100644 --- a/src/toxic_strings.c +++ b/src/toxic_strings.c @@ -120,19 +120,21 @@ void add_line_to_hist(const wchar_t *buf, size_t len, wchar_t (*hst)[MAX_STR_SIZ } /* copies history item at hst_pos to buf. Sets pos and len to the len of the history item. - hst_pos is decremented or incremented depending on key_dir. */ + hst_pos is decremented or incremented depending on key_dir. + + resets buffer if at end of history */ void fetch_hist_item(wchar_t *buf, size_t *pos, size_t *len, wchar_t (*hst)[MAX_STR_SIZE], int hst_tot, int *hst_pos, int key_dir) { if (key_dir == LN_HIST_MV_UP) { if (--(*hst_pos) < 0) { - ++(*hst_pos); + (*hst_pos) = 0; beep(); } } else { if (++(*hst_pos) >= hst_tot) { - --(*hst_pos); - beep(); + (*hst_pos) = hst_tot; + reset_buf(buf, pos, len); return; } } From eb09fceed72c24e1e38b593d1840521f60476051 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 24 Feb 2014 19:18:43 -0500 Subject: [PATCH 27/58] fix bug and some cleanup --- src/main.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/main.c b/src/main.c index 7ea172c..a1f0ec7 100644 --- a/src/main.c +++ b/src/main.c @@ -189,10 +189,10 @@ static int serverlist_load(const char *filename) return 0; } -int init_connection_helper(Tox *m, int linenumber) +int init_connection_helper(Tox *m, int line) { - return tox_bootstrap_from_address(m, servers[linenumber], TOX_ENABLE_IPV6_DEFAULT, - ports[linenumber], keys[linenumber]); + return tox_bootstrap_from_address(m, servers[line], TOX_ENABLE_IPV6_DEFAULT, + ports[line], keys[line]); } /* Connects to a random DHT server listed in the DHTservers file @@ -204,7 +204,8 @@ int init_connection_helper(Tox *m, int linenumber) * 4: failed to resolve name to IP * 5: serverlist file contains no acceptable line */ -static int init_connection_serverlist_loaded = 0; +static bool srvlist_loaded = false; + int init_connection(Tox *m) { if (linecnt > 0) /* already loaded serverlist */ @@ -214,14 +215,14 @@ int init_connection(Tox *m) * - load the serverlist * - connect to "everyone" inside */ - if (!init_connection_serverlist_loaded) { - init_connection_serverlist_loaded = 1; + if (!srvlist_loaded) { + srvlist_loaded = true; int res = serverlist_load(SRVLIST_FILE); - if (res) - { - // Fallback on the provided DHTServers in /usr/share, - // so new starts of toxic will connect to the DHT. - serverlist_load(PACKAGE_DATADIR "/DHTservers"); + + if (res) { + /* Fallback on the provided DHTServers in /usr/share, + so new starts of toxic will connect to the DHT. */ + res = serverlist_load(PACKAGE_DATADIR "/DHTservers"); if (res) return res; } @@ -230,9 +231,10 @@ int init_connection(Tox *m) return 4; res = 6; - int linenumber; - for(linenumber = 0; linenumber < linecnt; linenumber++) - if (init_connection_helper(m, linenumber)) + int line; + + for(line = 0; line < linecnt; line++) + if (init_connection_helper(m, line)) res = 0; return res; From e7920d1da7041bc2035a8f9193ea191de57ab365 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 24 Feb 2014 19:41:02 -0500 Subject: [PATCH 28/58] fix connection error codes --- src/main.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main.c b/src/main.c index a1f0ec7..b00ed88 100644 --- a/src/main.c +++ b/src/main.c @@ -200,16 +200,15 @@ int init_connection_helper(Tox *m, int line) * return codes: * 1: failed to open server file * 2: no line of sufficient length in server file - * 3: (old, removed) failed to split a selected line in the server file - * 4: failed to resolve name to IP - * 5: serverlist file contains no acceptable line + * 3: failed to resolve name to IP + * 4: serverlist file contains no acceptable line */ static bool srvlist_loaded = false; int init_connection(Tox *m) { if (linecnt > 0) /* already loaded serverlist */ - return init_connection_helper(m, rand() % linecnt) ? 0 : 4; + return init_connection_helper(m, rand() % linecnt) ? 0 : 3; /* only once: * - load the serverlist @@ -220,17 +219,18 @@ int init_connection(Tox *m) int res = serverlist_load(SRVLIST_FILE); if (res) { - /* Fallback on the provided DHTServers in /usr/share, + /* Fallback on the provided DHTServers in /usr/share or /usr/local/share, so new starts of toxic will connect to the DHT. */ res = serverlist_load(PACKAGE_DATADIR "/DHTservers"); + if (res) return res; } if (!linecnt) - return 4; + return 2; - res = 6; + res = 3; int line; for(line = 0; line < linecnt; line++) @@ -241,7 +241,7 @@ int init_connection(Tox *m) } /* empty serverlist file */ - return 5; + return 4; } static void do_connection(Tox *m, ToxWindow *prompt) From 2d9f4facf795938b3a19ba65431bc5b3ddd824eb Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 24 Feb 2014 20:08:51 -0500 Subject: [PATCH 29/58] connect to limited number of nodes on init instead of all of them --- src/main.c | 9 ++++++--- src/misc_tools.h | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main.c b/src/main.c index b00ed88..448e22b 100644 --- a/src/main.c +++ b/src/main.c @@ -205,6 +205,8 @@ int init_connection_helper(Tox *m, int line) */ static bool srvlist_loaded = false; +#define NUM_INIT_NODES 5 + int init_connection(Tox *m) { if (linecnt > 0) /* already loaded serverlist */ @@ -231,10 +233,11 @@ int init_connection(Tox *m) return 2; res = 3; - int line; + int i; + int n = MIN(NUM_INIT_NODES, linecnt); - for(line = 0; line < linecnt; line++) - if (init_connection_helper(m, line)) + for(i = 0; i < n; ++i) + if (init_connection_helper(m, rand() % linecnt)) res = 0; return res; diff --git a/src/misc_tools.h b/src/misc_tools.h index a3f223c..0aa203f 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -2,8 +2,8 @@ * Toxic -- Tox Curses Client */ -// #define MIN(x, y) (((x) < (y)) ? (x) : (y)) - #define MAX(x, y) (((x) > (y)) ? (x) : (y)) +#define MIN(x, y) (((x) < (y)) ? (x) : (y)) +#define MAX(x, y) (((x) > (y)) ? (x) : (y)) /* convert a hex string to binary */ unsigned char *hex_string_to_bin(char hex_string[]); From b6613a015fce3213362b5a7fe9a636fd799f8a3b Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 25 Feb 2014 02:28:24 -0500 Subject: [PATCH 30/58] add license info to source files --- src/chat.c | 22 ++++++++++++++++++++-- src/chat.h | 22 ++++++++++++++++++++++ src/chat_commands.c | 22 ++++++++++++++++++++-- src/chat_commands.h | 22 ++++++++++++++++++++-- src/configdir.c | 14 ++++++++------ src/configdir.h | 14 ++++++++------ src/execute.c | 22 ++++++++++++++++++++-- src/execute.h | 22 ++++++++++++++++++++-- src/friendlist.c | 22 ++++++++++++++++++++-- src/friendlist.h | 22 ++++++++++++++++++++++ src/global_commands.c | 22 ++++++++++++++++++++-- src/global_commands.h | 22 ++++++++++++++++++++-- src/groupchat.c | 22 ++++++++++++++++++++-- src/groupchat.h | 22 ++++++++++++++++++++-- src/main.c | 29 ++++++++++++++++++++++++----- src/misc_tools.c | 22 ++++++++++++++++++++-- src/misc_tools.h | 22 ++++++++++++++++++++-- src/prompt.c | 22 ++++++++++++++++++++-- src/prompt.h | 22 ++++++++++++++++++++-- src/toxic_strings.c | 22 ++++++++++++++++++++-- src/toxic_strings.h | 22 ++++++++++++++++++++-- src/toxic_windows.h | 22 ++++++++++++++++++++-- src/windows.c | 22 ++++++++++++++++++++++ 23 files changed, 446 insertions(+), 51 deletions(-) diff --git a/src/chat.c b/src/chat.c index 1bd192c..a4d782b 100644 --- a/src/chat.c +++ b/src/chat.c @@ -1,5 +1,23 @@ -/* - * Toxic -- Tox Curses Client +/* chat.c + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * */ #ifdef HAVE_CONFIG_H diff --git a/src/chat.h b/src/chat.h index a7918df..1801d10 100644 --- a/src/chat.h +++ b/src/chat.h @@ -1,3 +1,25 @@ +/* chat.h + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * + */ + #ifndef CHAT_H_6489PZ13 #define CHAT_H_6489PZ13 diff --git a/src/chat_commands.c b/src/chat_commands.c index 8a9505a..9fdedb3 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -1,5 +1,23 @@ -/* - * Toxic -- Tox Curses Client +/* chat_commands.c + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * */ #ifdef HAVE_CONFIG_H diff --git a/src/chat_commands.h b/src/chat_commands.h index fd8059a..744af97 100644 --- a/src/chat_commands.h +++ b/src/chat_commands.h @@ -1,5 +1,23 @@ -/* - * Toxic -- Tox Curses Client +/* chat_commands.h + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * */ void cmd_chat_help(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); diff --git a/src/configdir.c b/src/configdir.c index 8f43bc9..d43b08c 100644 --- a/src/configdir.c +++ b/src/configdir.c @@ -1,20 +1,22 @@ -/* - * Copyright (C) 2013 Tox project All Rights Reserved. +/* configdir.c * - * This file is part of Tox. * - * Tox is free software: you can redistribute it and/or modify + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * - * Tox is distributed in the hope that it will be useful, + * Toxic is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with Tox. If not, see . + * along with Toxic. If not, see . * */ diff --git a/src/configdir.h b/src/configdir.h index e886e53..6a1a7eb 100644 --- a/src/configdir.h +++ b/src/configdir.h @@ -1,20 +1,22 @@ -/* - * Copyright (C) 2013 Tox project All Rights Reserved. +/* configdir.h * - * This file is part of Tox. * - * Tox is free software: you can redistribute it and/or modify + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * - * Tox is distributed in the hope that it will be useful, + * Toxic is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with Tox. If not, see . + * along with Toxic. If not, see . * */ diff --git a/src/execute.c b/src/execute.c index beac6bb..816ae7f 100644 --- a/src/execute.c +++ b/src/execute.c @@ -1,5 +1,23 @@ -/* - * Toxic -- Tox Curses Client +/* execute.c + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * */ #include diff --git a/src/execute.h b/src/execute.h index c720a33..e40f028 100644 --- a/src/execute.h +++ b/src/execute.h @@ -1,5 +1,23 @@ -/* - * Toxic -- Tox Curses Client +/* execute.h + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * */ #define MAX_NUM_ARGS 4 /* Includes command */ diff --git a/src/friendlist.c b/src/friendlist.c index 7e22091..d55c173 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -1,5 +1,23 @@ -/* - * Toxic -- Tox Curses Client +/* friendlist.c + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * */ #ifdef HAVE_CONFIG_H diff --git a/src/friendlist.h b/src/friendlist.h index 0f29f3c..a468273 100644 --- a/src/friendlist.h +++ b/src/friendlist.h @@ -1,3 +1,25 @@ +/* friendlist.h + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * + */ + #ifndef FRIENDLIST_H_53I41IM #define FRIENDLIST_H_53I41IM diff --git a/src/global_commands.c b/src/global_commands.c index 29af2d6..0649bac 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -1,5 +1,23 @@ -/* - * Toxic -- Tox Curses Client +/* global_commands.c + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * */ #ifdef HAVE_CONFIG_H diff --git a/src/global_commands.h b/src/global_commands.h index 57f16e3..ff64323 100644 --- a/src/global_commands.h +++ b/src/global_commands.h @@ -1,5 +1,23 @@ -/* - * Toxic -- Tox Curses Client +/* global_commands.h + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * */ void cmd_accept(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); diff --git a/src/groupchat.c b/src/groupchat.c index 5b6d624..694a5d0 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -1,5 +1,23 @@ -/* - * Toxic -- Tox Curses Client +/* groupchat.c + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * */ #ifdef HAVE_CONFIG_H diff --git a/src/groupchat.h b/src/groupchat.h index a3aa0a7..b14efb7 100644 --- a/src/groupchat.h +++ b/src/groupchat.h @@ -1,5 +1,23 @@ -/* - * Toxic -- Tox Curses Client +/* groupchat.h + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * */ #define SIDEBAR_WIDTH 16 diff --git a/src/main.c b/src/main.c index 448e22b..4ddeb36 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,23 @@ -/* - * Toxic -- Tox Curses Client +/* main.c + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * */ #ifdef HAVE_CONFIG_H @@ -47,8 +65,11 @@ /* Export for use in Callbacks */ char *DATA_FILE = NULL; char *SRVLIST_FILE = NULL; + ToxWindow *prompt = NULL; +static int f_loadfromfile; /* 1 if we want to load from/save the data file, 0 otherwise */ + FileSender file_senders[MAX_FILES]; uint8_t max_file_senders_index; @@ -277,8 +298,6 @@ static void do_connection(Tox *m, ToxWindow *prompt) } } -int f_loadfromfile; - /* * Store Messenger to given location * Return 0 stored successfully @@ -485,7 +504,7 @@ int main(int argc, char *argv[]) int i = 0; int f_use_ipv4 = 0; - // Make sure all written files are read/writeable only by the current user. + /* Make sure all written files are read/writeable only by the current user. */ umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); for (i = 0; i < argc; ++i) { diff --git a/src/misc_tools.c b/src/misc_tools.c index 395a25b..a037c47 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -1,5 +1,23 @@ -/* - * Toxic -- Tox Curses Client +/* misc_tools.c + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * */ #include diff --git a/src/misc_tools.h b/src/misc_tools.h index 0aa203f..4d711e9 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -1,5 +1,23 @@ -/* - * Toxic -- Tox Curses Client +/* misc_tools.h + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * */ #define MIN(x, y) (((x) < (y)) ? (x) : (y)) diff --git a/src/prompt.c b/src/prompt.c index 6d0a238..ca30e32 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -1,5 +1,23 @@ -/* - * Toxic -- Tox Curses Client +/* prompt.c + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * */ #ifdef HAVE_CONFIG_H diff --git a/src/prompt.h b/src/prompt.h index 7652503..96ca08a 100644 --- a/src/prompt.h +++ b/src/prompt.h @@ -1,5 +1,23 @@ -/* - * Toxic -- Tox Curses Client +/* prompt.h + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * */ #ifndef PROMPT_H_UZYGWFFL diff --git a/src/toxic_strings.c b/src/toxic_strings.c index 11d6ad0..78abaae 100644 --- a/src/toxic_strings.c +++ b/src/toxic_strings.c @@ -1,5 +1,23 @@ -/* - * Toxic -- Tox Curses Client +/* toxic_strings.c + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * */ #include diff --git a/src/toxic_strings.h b/src/toxic_strings.h index f97dd20..3e28c0f 100644 --- a/src/toxic_strings.h +++ b/src/toxic_strings.h @@ -1,5 +1,23 @@ -/* - * Toxic -- Tox Curses Client +/* toxic_strings.h + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * */ /* Adds char to buffer at pos */ diff --git a/src/toxic_windows.h b/src/toxic_windows.h index eec6ca9..67b28f0 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -1,5 +1,23 @@ -/* - * Toxic -- Tox Curses Client +/* toxic_windows.h + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * */ #ifndef _windows_h diff --git a/src/windows.c b/src/windows.c index 326b659..018da54 100644 --- a/src/windows.c +++ b/src/windows.c @@ -1,3 +1,25 @@ +/* windows.c + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * + */ + #ifdef HAVE_CONFIG_H #include "config.h" #endif From 6896292c49d4673e7af105462aa30b4c60990484 Mon Sep 17 00:00:00 2001 From: JFreegman Date: Tue, 25 Feb 2014 03:07:03 -0500 Subject: [PATCH 31/58] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 28fb244..adec50a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -## Toxic - console client for [Tox](http://tox.im) +## Toxic -The client formerly resided in the [Tox core repository](https://github.com/irungentoo/ProjectTox-Core) and is now available as a standalone program. It looks like [this](http://wiki.tox.im/images/b/b6/Ncursesclient1.png). +Toxic is an ncurses based instant messaging client for [Tox](http://tox.im) which formerly resided in the [Tox core repository](https://github.com/irungentoo/ProjectTox-Core) and is now available as a standalone program. It looks like [this](http://i.imgur.com/hL7WhVl.png). To compile, first generate the configure script by running the ```autoreconf -i``` command. @@ -14,7 +14,7 @@ Then execute the configure script with ./configure (you may need to pass it the echo '/usr/local/lib/' | sudo tee -a /etc/ld.so.conf.d/locallib.conf sudo ldconfig ``` -If you dont already have them, you might want to install the ncurses libraries, on Debian: +If you dont already have them, you may need to install the ncurses libraries. For Debian based systems: ``` sudo apt-get install libncurses5-dev libncursesw5-dev ``` From 831d8e5f24b6eaca6cb20a155948d8a6ad2767ad Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 26 Feb 2014 01:51:06 -0500 Subject: [PATCH 32/58] implement chat logging --- src/chat.c | 13 +++++- src/log.c | 100 ++++++++++++++++++++++++++++++++++++++++++++ src/log.h | 33 +++++++++++++++ src/toxic_strings.c | 4 +- src/toxic_windows.h | 12 ++++++ 5 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 src/log.c create mode 100644 src/log.h diff --git a/src/chat.c b/src/chat.c index a4d782b..7666215 100644 --- a/src/chat.c +++ b/src/chat.c @@ -94,6 +94,7 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1 } else wprintw(ctx->history, "%s\n", msg); + add_to_log_buf(msg, nick, ctx); alert_window(self, WINDOW_ALERT_1, true); } @@ -137,6 +138,7 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin wprintw(ctx->history, "* %s %s\n", nick, action); wattroff(ctx->history, COLOR_PAIR(YELLOW)); + add_to_log_buf(action, nick, ctx); alert_window(self, WINDOW_ALERT_1, true); } @@ -263,7 +265,6 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int num, uint8_t filenum, u uint8_t *filename = friends[num].file_receiver.filenames[filenum]; FILE *file_to_save = fopen(filename, "a"); - // we have a problem here, but don't let it segfault if (file_to_save == NULL) { wattron(ctx->history, COLOR_PAIR(RED)); wprintw(ctx->history, "* Error writing to file.\n"); @@ -319,6 +320,8 @@ static void send_action(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *acti wprintw(ctx->history, " * Failed to send action\n"); wattroff(ctx->history, COLOR_PAIR(RED)); } + + add_to_log_buf(action, selfname, ctx); } static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) @@ -478,6 +481,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) if (line[0] == '/') { if (close_win = !strcmp(line, "/close")) { + write_to_log(ctx); int f_num = self->num; delwin(ctx->linewin); delwin(statusbar->topline); @@ -503,6 +507,8 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) } else wprintw(ctx->history, "%s\n", line); + add_to_log_buf(line, selfname, ctx); + if (!statusbar->is_online || tox_send_message(m, self->num, line, strlen(line) + 1) == 0) { wattron(ctx->history, COLOR_PAIR(RED)); wprintw(ctx->history, " * Failed to send message.\n"); @@ -653,6 +659,11 @@ static void chat_onInit(ToxWindow *self, Tox *m) wprintw(ctx->history, "\n\n"); execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE); wmove(self->window, y2 - CURS_Y_OFFSET, 0); + + if (self->name) { + ctx->log.log_on = true; + init_logging_session(self->name, friends[self->num].pub_key, ctx); + } } ToxWindow new_chat(Tox *m, int friendnum) diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..0b127e8 --- /dev/null +++ b/src/log.c @@ -0,0 +1,100 @@ +/* ctx->log.c + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * + */ + +#include +#include +#include + +#include "configdir.h" +#include "toxic_windows.h" +#include "misc_tools.h" + +/* gets the log path by appending to the config dir the name and first 4 chars of key */ +void init_logging_session(uint8_t *name, uint8_t *key, ChatContext *ctx) +{ + if (!ctx->log.log_on) + return; + + char *user_config_dir = get_user_config_dir(); + int path_len = strlen(user_config_dir) + strlen(CONFIGDIR) + strlen(name) + (KEY_IDENT_DIGITS * 2) + 5; + + if (path_len > MAX_STR_SIZE) + return; + + uint8_t ident[KEY_IDENT_DIGITS*2+1]; + sprintf(&ident[0], "%02X", key[0] & 0xff); + sprintf(&ident[2], "%02X", key[2] & 0xff); + ident[KEY_IDENT_DIGITS*2+1] = '\0'; + + snprintf(ctx->log.log_path, MAX_STR_SIZE, "%s%s%s-%s.log", user_config_dir, CONFIGDIR, name, ident); + + FILE *logfile = fopen(ctx->log.log_path, "a"); + + if (logfile == NULL) { + ctx->log.log_on = false; + return; + } + + fprintf(logfile, "\n***NEW SESSION***\n\n"); + + fclose(logfile); + free(user_config_dir); +} + +/* writes contents from a chatcontext's log buffer to respective log file and resets log pos. + This is triggered when the log buffer is full, but may be forced. */ +void write_to_log(ChatContext *ctx) +{ + if (!ctx->log.log_on) + return; + + FILE *logfile = fopen(ctx->log.log_path, "a"); + + if (logfile == NULL) { + ctx->log.log_on = false; + return; + } + + int i; + + for (i = 0; i < ctx->log.pos; ++i) + fprintf(logfile, "%s", ctx->log.log_buf[i]); + + ctx->log.pos = 0; + fclose(logfile); +} + +/* Adds msg to log_buf with timestamp and name. + If buf is full, triggers write_to_log (which sets buf pos to 0) */ +void add_to_log_buf(uint8_t *msg, uint8_t *name, ChatContext *ctx) +{ + if (!ctx->log.log_on) + return; + + struct tm *tminfo = get_time(); + snprintf(ctx->log.log_buf[ctx->log.pos], MAX_LOG_LINE_SIZE, "%04d/%02d/%02d [%02d:%02d:%02d] %s: %s\n", + tminfo->tm_year + 1900, tminfo->tm_mon + 1, tminfo->tm_mday, + tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec, name, msg); + + if (++(ctx->log.pos) >= MAX_LOG_BUF_LINES) + write_to_log(ctx); +} diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..01734af --- /dev/null +++ b/src/log.h @@ -0,0 +1,33 @@ +/* log.h + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * + */ + +/* gets a log path by appending to the config dir the name and first 4 chars of the pub_key, + writes current date/time to log file. */ +void init_logging_session(uint8_t *name, uint8_t *pub_key, ChatContext *ctx); + +/* Adds msg to log_buf with timestamp and name. + If buf is full, triggers write_to_log (which sets buf pos to 0) */ +void add_to_log_buf(uint8_t *msg, uint8_t *name, ChatContext *ctx); + +/* writes contents from a chatcontext's log buffer to respective log file and resets log pos. + This is triggered automatically when the log buffer is full, but may be forced. */ +void write_to_log(ChatContext *ctx); diff --git a/src/toxic_strings.c b/src/toxic_strings.c index 78abaae..efebc03 100644 --- a/src/toxic_strings.c +++ b/src/toxic_strings.c @@ -146,12 +146,12 @@ void fetch_hist_item(wchar_t *buf, size_t *pos, size_t *len, wchar_t (*hst)[MAX_ { if (key_dir == LN_HIST_MV_UP) { if (--(*hst_pos) < 0) { - (*hst_pos) = 0; + *hst_pos = 0; beep(); } } else { if (++(*hst_pos) >= hst_tot) { - (*hst_pos) = hst_tot; + *hst_pos = hst_tot; reset_buf(buf, pos, len); return; } diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 67b28f0..89ffbba 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -131,6 +131,16 @@ struct StatusBar { bool is_online; }; +#define MAX_LOG_BUF_LINES 10 /* write log_buf contents to log file after this many lines */ +#define MAX_LOG_LINE_SIZE MAX_STR_SIZE + TOXIC_MAX_NAME_LENGTH + 24 /* extra room for time/date */ + +struct chatlog { + uint8_t log_path[MAX_STR_SIZE]; + uint8_t log_buf[MAX_LOG_BUF_LINES][MAX_LOG_LINE_SIZE]; + int pos; + bool log_on; +}; + #define MAX_LINE_HIST 128 /* chat and groupchat window/buffer holder */ @@ -145,6 +155,8 @@ struct ChatContext { bool self_is_typing; + struct chatlog log; + WINDOW *history; WINDOW *linewin; WINDOW *sidebar; From 5ff70657446c0773c7cfb6d59a084caedcdf0434 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 26 Feb 2014 03:51:26 -0500 Subject: [PATCH 33/58] basic logging for groupchats --- build/Makefile.am | 4 +++- src/chat.c | 8 ++++---- src/chat_commands.c | 2 +- src/global_commands.c | 2 +- src/groupchat.c | 8 ++++++++ src/groupchat.h | 1 + src/log.c | 6 ++++-- 7 files changed, 22 insertions(+), 9 deletions(-) diff --git a/build/Makefile.am b/build/Makefile.am index a670d9b..d38b8f5 100644 --- a/build/Makefile.am +++ b/build/Makefile.am @@ -25,7 +25,9 @@ toxic_SOURCES = $(top_srcdir)/src/main.c \ $(top_srcdir)/src/misc_tools.c \ $(top_srcdir)/src/misc_tools.h \ $(top_srcdir)/src/toxic_strings.c \ - $(top_srcdir)/src/toxic_strings.h + $(top_srcdir)/src/toxic_strings.h \ + $(top_srcdir)/src/log.c \ + $(top_srcdir)/src/log.h toxic_CFLAGS = -I$(top_srcdir) \ $(NCURSES_CFLAGS) \ diff --git a/src/chat.c b/src/chat.c index 7666215..29bcec1 100644 --- a/src/chat.c +++ b/src/chat.c @@ -319,9 +319,9 @@ static void send_action(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *acti wattron(ctx->history, COLOR_PAIR(RED)); wprintw(ctx->history, " * Failed to send action\n"); wattroff(ctx->history, COLOR_PAIR(RED)); + } else { + add_to_log_buf(action, selfname, ctx); } - - add_to_log_buf(action, selfname, ctx); } static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) @@ -507,12 +507,12 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) } else wprintw(ctx->history, "%s\n", line); - add_to_log_buf(line, selfname, ctx); - if (!statusbar->is_online || tox_send_message(m, self->num, line, strlen(line) + 1) == 0) { wattron(ctx->history, COLOR_PAIR(RED)); wprintw(ctx->history, " * Failed to send message.\n"); wattroff(ctx->history, COLOR_PAIR(RED)); + } else { + add_to_log_buf(line, selfname, ctx); } } diff --git a/src/chat_commands.c b/src/chat_commands.c index 9fdedb3..2fd35bf 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -108,7 +108,7 @@ void cmd_join_group(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*ar return; } - if (init_groupchat_win(prompt, m, groupnum) == -1) { + if (init_groupchat_win(prompt, m, groupnum, groupkey) == -1) { wprintw(window, "Group chat window failed to initialize.\n"); tox_del_groupchat(m, groupnum); return; diff --git a/src/global_commands.c b/src/global_commands.c index 0649bac..129b02c 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -211,7 +211,7 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg return; } - if (init_groupchat_win(prompt, m, groupnum) == -1) { + if (init_groupchat_win(prompt, m, groupnum, NULL) == -1) { wprintw(window, "Group chat window failed to initialize.\n"); tox_del_groupchat(m, groupnum); return; diff --git a/src/groupchat.c b/src/groupchat.c index 694a5d0..7fd238f 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -154,6 +154,8 @@ static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, int groupnum, int } else { wprintw(ctx->history, "%s\n", msg); } + + add_to_log_buf(msg, nick, ctx); } static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int peernum, uint8_t *action, @@ -188,6 +190,8 @@ static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int p wattron(ctx->history, COLOR_PAIR(YELLOW)); wprintw(ctx->history, "* %s %s\n", nick, action); wattroff(ctx->history, COLOR_PAIR(YELLOW)); + + add_to_log_buf(action, nick, ctx); } /* Puts two copies of peerlist in chat instance */ @@ -483,6 +487,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) if (line[0] == '/') { if (close_win = strcmp(line, "/close") == 0) { + write_to_log(ctx); set_active_window(0); int groupnum = self->num; delwin(ctx->linewin); @@ -575,6 +580,9 @@ static void groupchat_onInit(ToxWindow *self, Tox *m) print_groupchat_help(ctx); wmove(self->window, y-CURS_Y_OFFSET, 0); + + ctx->log.log_on = true; + init_logging_session(self->name, groupchats[self->num].groupkey, ctx); } ToxWindow new_group_chat(Tox *m, int groupnum) diff --git a/src/groupchat.h b/src/groupchat.h index b14efb7..ea282df 100644 --- a/src/groupchat.h +++ b/src/groupchat.h @@ -30,6 +30,7 @@ typedef struct { int side_pos; /* current position of the sidebar - used for scrolling up and down */ uint8_t *peer_names; uint8_t *oldpeer_names; + uint8_t groupkey[TOX_CLIENT_ID_SIZE]; } GroupChat; int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum); diff --git a/src/log.c b/src/log.c index 0b127e8..8204615 100644 --- a/src/log.c +++ b/src/log.c @@ -35,7 +35,8 @@ void init_logging_session(uint8_t *name, uint8_t *key, ChatContext *ctx) return; char *user_config_dir = get_user_config_dir(); - int path_len = strlen(user_config_dir) + strlen(CONFIGDIR) + strlen(name) + (KEY_IDENT_DIGITS * 2) + 5; + int path_len = strlen(user_config_dir) + strlen(CONFIGDIR) + strlen(name)\ + + (KEY_IDENT_DIGITS * 2) + 5; if (path_len > MAX_STR_SIZE) return; @@ -45,7 +46,8 @@ void init_logging_session(uint8_t *name, uint8_t *key, ChatContext *ctx) sprintf(&ident[2], "%02X", key[2] & 0xff); ident[KEY_IDENT_DIGITS*2+1] = '\0'; - snprintf(ctx->log.log_path, MAX_STR_SIZE, "%s%s%s-%s.log", user_config_dir, CONFIGDIR, name, ident); + snprintf(ctx->log.log_path, MAX_STR_SIZE, "%s%s%s-%s.log", + user_config_dir, CONFIGDIR, name, ident); FILE *logfile = fopen(ctx->log.log_path, "a"); From a61f5f6a6d474448a450564d8f11cd2badaf16ef Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 26 Feb 2014 05:23:11 -0500 Subject: [PATCH 34/58] properly close windows on exit --- src/chat.c | 57 ++++++++++++++++++++++++++------------------- src/chat.h | 1 + src/groupchat.c | 32 ++++++++++++++----------- src/groupchat.h | 1 + src/main.c | 1 + src/toxic_windows.h | 9 ++++++- src/windows.c | 14 +++++++++++ 7 files changed, 77 insertions(+), 38 deletions(-) diff --git a/src/chat.c b/src/chat.c index 29bcec1..66b0884 100644 --- a/src/chat.c +++ b/src/chat.c @@ -71,6 +71,24 @@ static void set_typingstatus(ToxWindow *self, Tox *m, bool is_typing) ctx->self_is_typing = is_typing; } +void kill_chat_window(ToxWindow *self) +{ + set_active_window(0); + ChatContext *ctx = self->chatwin; + StatusBar *statusbar = self->stb; + + write_to_log(ctx); + + int f_num = self->num; + delwin(ctx->linewin); + delwin(statusbar->topline); + del_window(self); + disable_chatwin(f_num); + + free(ctx); + free(statusbar); +} + static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len) { if (self->num != num) @@ -333,7 +351,6 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) getyx(self->window, y, x); getmaxyx(self->window, y2, x2); int cur_len = 0; - bool close_win = false; if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key: Remove character behind pos */ if (ctx->pos > 0) { @@ -480,17 +497,17 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) add_line_to_hist(ctx->line, ctx->len, ctx->ln_history, &ctx->hst_tot, &ctx->hst_pos); if (line[0] == '/') { - if (close_win = !strcmp(line, "/close")) { - write_to_log(ctx); - int f_num = self->num; - delwin(ctx->linewin); - delwin(statusbar->topline); - del_window(self); - disable_chatwin(f_num); - } else if (strncmp(line, "/me ", strlen("/me ")) == 0) + if (strcmp(line, "/close") == 0) { + if (ctx->self_is_typing) + set_typingstatus(self, m, false); + + kill_chat_window(self); + return; + } else if (strncmp(line, "/me ", strlen("/me ")) == 0) { send_action(self, ctx, m, line + strlen("/me ")); - else + } else { execute(ctx->history, self, m, line, CHAT_COMMAND_MODE); + } } else if (!string_is_empty(line)) { uint8_t selfname[TOX_MAX_NAME_LENGTH]; tox_get_self_name(m, selfname, TOX_MAX_NAME_LENGTH); @@ -516,18 +533,11 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) } } - if (close_win) { - free(ctx); - free(statusbar); - } else { - reset_buf(ctx->line, &ctx->pos, &ctx->len); - } + reset_buf(ctx->line, &ctx->pos, &ctx->len); } - if (!close_win) { - if (ctx->len <= 0 && ctx->self_is_typing) - set_typingstatus(self, m, false); - } + if (ctx->len <= 0 && ctx->self_is_typing) + set_typingstatus(self, m, false); } static void chat_onDraw(ToxWindow *self, Tox *m) @@ -660,10 +670,8 @@ static void chat_onInit(ToxWindow *self, Tox *m) execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE); wmove(self->window, y2 - CURS_Y_OFFSET, 0); - if (self->name) { - ctx->log.log_on = true; - init_logging_session(self->name, friends[self->num].pub_key, ctx); - } + ctx->log.log_on = true; + init_logging_session(self->name, friends[self->num].pub_key, ctx); } ToxWindow new_chat(Tox *m, int friendnum) @@ -672,6 +680,7 @@ ToxWindow new_chat(Tox *m, int friendnum) memset(&ret, 0, sizeof(ret)); ret.active = true; + ret.is_chat = true; ret.onKey = &chat_onKey; ret.onDraw = &chat_onDraw; diff --git a/src/chat.h b/src/chat.h index 1801d10..fac132a 100644 --- a/src/chat.h +++ b/src/chat.h @@ -25,6 +25,7 @@ #include "toxic_windows.h" +void kill_chat_window(ToxWindow *self); ToxWindow new_chat(Tox *m, int friendnum); #endif /* end of include guard: CHAT_H_6489PZ13 */ diff --git a/src/groupchat.c b/src/groupchat.c index 7fd238f..db20627 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -71,8 +71,19 @@ int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum) return -1; } -static void close_groupchatwin(Tox *m, int groupnum) +void kill_groupchat_window(ToxWindow *self) { + ChatContext *ctx = self->chatwin; + write_to_log(ctx); + + delwin(ctx->linewin); + del_window(self); + free(ctx); +} + +static void close_groupchat(ToxWindow *self, Tox *m, int groupnum) +{ + set_active_window(0); tox_del_groupchat(m, groupnum); free(groupchats[groupnum].peer_names); @@ -87,6 +98,8 @@ static void close_groupchatwin(Tox *m, int groupnum) } max_groupchat_index = i; + + kill_groupchat_window(self); } static void print_groupchat_help(ChatContext *ctx) @@ -480,19 +493,14 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) wclear(ctx->linewin); wmove(self->window, y2 - CURS_Y_OFFSET, 0); wclrtobot(self->window); - bool close_win = false; if (!string_is_empty(line)) add_line_to_hist(ctx->line, ctx->len, ctx->ln_history, &ctx->hst_tot, &ctx->hst_pos); if (line[0] == '/') { - if (close_win = strcmp(line, "/close") == 0) { - write_to_log(ctx); - set_active_window(0); - int groupnum = self->num; - delwin(ctx->linewin); - del_window(self); - close_groupchatwin(m, groupnum); + if (strcmp(line, "/close") == 0) { + close_groupchat(self, m, self->num); + return; } else if (strcmp(line, "/help") == 0) print_groupchat_help(ctx); else if (strncmp(line, "/me ", strlen("/me ")) == 0) @@ -507,10 +515,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) } } - if (close_win) - free(ctx); - else - reset_buf(ctx->line, &ctx->pos, &ctx->len); + reset_buf(ctx->line, &ctx->pos, &ctx->len); } } @@ -591,6 +596,7 @@ ToxWindow new_group_chat(Tox *m, int groupnum) memset(&ret, 0, sizeof(ret)); ret.active = true; + ret.is_groupchat = true; ret.onKey = &groupchat_onKey; ret.onDraw = &groupchat_onDraw; diff --git a/src/groupchat.h b/src/groupchat.h index ea282df..542510c 100644 --- a/src/groupchat.h +++ b/src/groupchat.h @@ -33,5 +33,6 @@ typedef struct { uint8_t groupkey[TOX_CLIENT_ID_SIZE]; } GroupChat; +void kill_groupchat_window(ToxWindow *self); int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum); ToxWindow new_group_chat(Tox *m, int groupnum); diff --git a/src/main.c b/src/main.c index 4ddeb36..21ce5a7 100644 --- a/src/main.c +++ b/src/main.c @@ -475,6 +475,7 @@ void exit_toxic(Tox *m) fclose(file_senders[i].file); } + kill_all_windows(); free(DATA_FILE); free(SRVLIST_FILE); free(prompt->stb); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 89ffbba..6eb100e 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -109,6 +109,10 @@ struct ToxWindow { bool active; int x; + /* window type identifiers */ + bool is_chat; + bool is_groupchat; + bool alert0; bool alert1; bool alert2; @@ -132,7 +136,7 @@ struct StatusBar { }; #define MAX_LOG_BUF_LINES 10 /* write log_buf contents to log file after this many lines */ -#define MAX_LOG_LINE_SIZE MAX_STR_SIZE + TOXIC_MAX_NAME_LENGTH + 24 /* extra room for time/date */ +#define MAX_LOG_LINE_SIZE MAX_STR_SIZE + TOXIC_MAX_NAME_LENGTH + 24 /* extra room for timestamp */ struct chatlog { uint8_t log_path[MAX_STR_SIZE]; @@ -226,4 +230,7 @@ int add_window(Tox *m, ToxWindow w); void del_window(ToxWindow *w); void set_active_window(int ch); int num_active_windows(void); + +/* closes all chat and groupchat windows (should only be called on shutdown) */ +void kill_all_windows(void); #endif diff --git a/src/windows.c b/src/windows.c index 018da54..e5dd40f 100644 --- a/src/windows.c +++ b/src/windows.c @@ -30,6 +30,7 @@ #include "friendlist.h" #include "prompt.h" #include "toxic_windows.h" +#include "groupchat.h" extern char *DATA_FILE; @@ -409,3 +410,16 @@ int num_active_windows(void) return count; } + +/* destroys all chat and groupchat windows (should only be called on shutdown) */ +void kill_all_windows(void) +{ + int i; + + for (i = 0; i < MAX_WINDOWS_NUM; ++i) { + if (windows[i].is_chat) + kill_chat_window(&windows[i]); + else if (windows[i].is_groupchat) + kill_groupchat_window(&windows[i]); + } +} From 8e4db369bc7457e2da7461ba2a1ee77fd57c6dcb Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 26 Feb 2014 06:35:19 -0500 Subject: [PATCH 35/58] log events --- src/chat.c | 8 ++++---- src/groupchat.c | 23 +++++++++++++++++++---- src/log.c | 17 ++++++++++++----- src/log.h | 6 +++++- src/toxic_windows.h | 2 +- 5 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/chat.c b/src/chat.c index 66b0884..1bdb565 100644 --- a/src/chat.c +++ b/src/chat.c @@ -112,7 +112,7 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1 } else wprintw(ctx->history, "%s\n", msg); - add_to_log_buf(msg, nick, ctx); + add_to_log_buf(msg, nick, ctx, false); alert_window(self, WINDOW_ALERT_1, true); } @@ -156,7 +156,7 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin wprintw(ctx->history, "* %s %s\n", nick, action); wattroff(ctx->history, COLOR_PAIR(YELLOW)); - add_to_log_buf(action, nick, ctx); + add_to_log_buf(action, nick, ctx, true); alert_window(self, WINDOW_ALERT_1, true); } @@ -338,7 +338,7 @@ static void send_action(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *acti wprintw(ctx->history, " * Failed to send action\n"); wattroff(ctx->history, COLOR_PAIR(RED)); } else { - add_to_log_buf(action, selfname, ctx); + add_to_log_buf(action, selfname, ctx, true); } } @@ -529,7 +529,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) wprintw(ctx->history, " * Failed to send message.\n"); wattroff(ctx->history, COLOR_PAIR(RED)); } else { - add_to_log_buf(line, selfname, ctx); + add_to_log_buf(line, selfname, ctx, false); } } diff --git a/src/groupchat.c b/src/groupchat.c index db20627..738fe89 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -168,7 +168,7 @@ static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, int groupnum, int wprintw(ctx->history, "%s\n", msg); } - add_to_log_buf(msg, nick, ctx); + add_to_log_buf(msg, nick, ctx, false); } static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int peernum, uint8_t *action, @@ -204,7 +204,7 @@ static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int p wprintw(ctx->history, "* %s %s\n", nick, action); wattroff(ctx->history, COLOR_PAIR(YELLOW)); - add_to_log_buf(action, nick, ctx); + add_to_log_buf(action, nick, ctx, true); } /* Puts two copies of peerlist in chat instance */ @@ -269,25 +269,36 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu ChatContext *ctx = self->chatwin; print_time(ctx->history); + uint8_t *event; + switch (change) { case TOX_CHAT_CHANGE_PEER_ADD: + event = "has joined the room"; + wattron(ctx->history, COLOR_PAIR(GREEN)); wattron(ctx->history, A_BOLD); wprintw(ctx->history, "* %s", peername); wattroff(ctx->history, A_BOLD); - wprintw(ctx->history, " has joined the room\n"); + wprintw(ctx->history, " %s\n", event); wattroff(ctx->history, COLOR_PAIR(GREEN)); + + add_to_log_buf(event, peername, ctx, true); break; + case TOX_CHAT_CHANGE_PEER_DEL: + event = "has left the room"; + wattron(ctx->history, A_BOLD); wprintw(ctx->history, "* %s", oldpeername); wattroff(ctx->history, A_BOLD); - wprintw(ctx->history, " has left the room\n"); + wprintw(ctx->history, " %s\n", event); if (groupchats[self->num].side_pos > 0) --groupchats[self->num].side_pos; + add_to_log_buf(event, peername, ctx, true); break; + case TOX_CHAT_CHANGE_PEER_NAME: wattron(ctx->history, COLOR_PAIR(MAGENTA)); wattron(ctx->history, A_BOLD); @@ -300,6 +311,10 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu wprintw(ctx->history, "%s\n", peername); wattroff(ctx->history, A_BOLD); wattroff(ctx->history, COLOR_PAIR(MAGENTA)); + + uint8_t tmp_event[TOXIC_MAX_NAME_LENGTH + 32]; + snprintf(tmp_event, sizeof(tmp_event), "is now known as %s", peername); + add_to_log_buf(tmp_event, peername, ctx, true); break; } diff --git a/src/log.c b/src/log.c index 8204615..3ed1aea 100644 --- a/src/log.c +++ b/src/log.c @@ -85,17 +85,24 @@ void write_to_log(ChatContext *ctx) fclose(logfile); } -/* Adds msg to log_buf with timestamp and name. - If buf is full, triggers write_to_log (which sets buf pos to 0) */ -void add_to_log_buf(uint8_t *msg, uint8_t *name, ChatContext *ctx) +/* Adds line/event to log_buf with timestamp and name. If buf is full, triggers write_to_log. + If event is true, formats line as an event, e.g. * name has gone offline */ +void add_to_log_buf(uint8_t *msg, uint8_t *name, ChatContext *ctx, bool event) { if (!ctx->log.log_on) return; + uint8_t name_frmt[TOXIC_MAX_NAME_LENGTH + 3]; + + if (event) + snprintf(name_frmt, sizeof(name_frmt), "* %s", name); + else + snprintf(name_frmt, sizeof(name_frmt), "%s:", name); + struct tm *tminfo = get_time(); - snprintf(ctx->log.log_buf[ctx->log.pos], MAX_LOG_LINE_SIZE, "%04d/%02d/%02d [%02d:%02d:%02d] %s: %s\n", + snprintf(ctx->log.log_buf[ctx->log.pos], MAX_LOG_LINE_SIZE, "%04d/%02d/%02d [%02d:%02d:%02d] %s %s\n", tminfo->tm_year + 1900, tminfo->tm_mon + 1, tminfo->tm_mday, - tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec, name, msg); + tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec, name_frmt, msg); if (++(ctx->log.pos) >= MAX_LOG_BUF_LINES) write_to_log(ctx); diff --git a/src/log.h b/src/log.h index 01734af..45b1c62 100644 --- a/src/log.h +++ b/src/log.h @@ -26,7 +26,11 @@ void init_logging_session(uint8_t *name, uint8_t *pub_key, ChatContext *ctx); /* Adds msg to log_buf with timestamp and name. If buf is full, triggers write_to_log (which sets buf pos to 0) */ -void add_to_log_buf(uint8_t *msg, uint8_t *name, ChatContext *ctx); +void add_line_log_buf(uint8_t *msg, uint8_t *name, ChatContext *ctx); + +/* Adds line/event to log_buf with timestamp and name. If buf is full, triggers write_to_log. + If event is true, formats line as an event, e.g. * name has gone offline */ +void add_to_log_buf(uint8_t *msg, uint8_t *name, ChatContext *ctx, bool event); /* writes contents from a chatcontext's log buffer to respective log file and resets log pos. This is triggered automatically when the log buffer is full, but may be forced. */ diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 6eb100e..889152c 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -136,7 +136,7 @@ struct StatusBar { }; #define MAX_LOG_BUF_LINES 10 /* write log_buf contents to log file after this many lines */ -#define MAX_LOG_LINE_SIZE MAX_STR_SIZE + TOXIC_MAX_NAME_LENGTH + 24 /* extra room for timestamp */ +#define MAX_LOG_LINE_SIZE MAX_STR_SIZE + TOXIC_MAX_NAME_LENGTH + 32 /* extra room for timestamp */ struct chatlog { uint8_t log_path[MAX_STR_SIZE]; From 817f763589d994276d8ed636ab99921a68fd12a2 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 26 Feb 2014 17:15:34 -0500 Subject: [PATCH 36/58] give groupchat logs unique names --- src/chat_commands.c | 2 +- src/groupchat.c | 2 +- src/groupchat.h | 1 - src/log.c | 31 ++++++++++++++++++++++--------- src/log.h | 5 ++--- 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/chat_commands.c b/src/chat_commands.c index 2fd35bf..9fdedb3 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -108,7 +108,7 @@ void cmd_join_group(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*ar return; } - if (init_groupchat_win(prompt, m, groupnum, groupkey) == -1) { + if (init_groupchat_win(prompt, m, groupnum) == -1) { wprintw(window, "Group chat window failed to initialize.\n"); tox_del_groupchat(m, groupnum); return; diff --git a/src/groupchat.c b/src/groupchat.c index 738fe89..fd1980f 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -602,7 +602,7 @@ static void groupchat_onInit(ToxWindow *self, Tox *m) wmove(self->window, y-CURS_Y_OFFSET, 0); ctx->log.log_on = true; - init_logging_session(self->name, groupchats[self->num].groupkey, ctx); + init_logging_session(self->name, NULL, ctx); } ToxWindow new_group_chat(Tox *m, int groupnum) diff --git a/src/groupchat.h b/src/groupchat.h index 542510c..5b079d5 100644 --- a/src/groupchat.h +++ b/src/groupchat.h @@ -30,7 +30,6 @@ typedef struct { int side_pos; /* current position of the sidebar - used for scrolling up and down */ uint8_t *peer_names; uint8_t *oldpeer_names; - uint8_t groupkey[TOX_CLIENT_ID_SIZE]; } GroupChat; void kill_groupchat_window(ToxWindow *self); diff --git a/src/log.c b/src/log.c index 3ed1aea..f8d2de8 100644 --- a/src/log.c +++ b/src/log.c @@ -28,23 +28,36 @@ #include "toxic_windows.h" #include "misc_tools.h" -/* gets the log path by appending to the config dir the name and first 4 chars of key */ +/* gets the log path by appending to the config dir the name and a pseudo-unique identity */ void init_logging_session(uint8_t *name, uint8_t *key, ChatContext *ctx) { if (!ctx->log.log_on) return; char *user_config_dir = get_user_config_dir(); - int path_len = strlen(user_config_dir) + strlen(CONFIGDIR) + strlen(name)\ - + (KEY_IDENT_DIGITS * 2) + 5; + int path_len = strlen(user_config_dir) + strlen(CONFIGDIR) + strlen(name); - if (path_len > MAX_STR_SIZE) + /* use first 4 digits of key as log ident. If no key use a timestamp */ + uint8_t ident[32]; + + if (key != NULL) { + path_len += (KEY_IDENT_DIGITS * 2 + 5); + + sprintf(&ident[0], "%02X", key[0] & 0xff); + sprintf(&ident[2], "%02X", key[2] & 0xff); + ident[KEY_IDENT_DIGITS*2+1] = '\0'; + } else { + struct tm *tminfo = get_time(); + snprintf(ident, 32, + "%04d-%02d-%02d-%02d:%02d:%02d", tminfo->tm_year+1900,tminfo->tm_mon+1, tminfo->tm_mday, + tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec); + path_len += strlen(ident) + 1; + } + + if (path_len > MAX_STR_SIZE) { + ctx->log.log_on = false; return; - - uint8_t ident[KEY_IDENT_DIGITS*2+1]; - sprintf(&ident[0], "%02X", key[0] & 0xff); - sprintf(&ident[2], "%02X", key[2] & 0xff); - ident[KEY_IDENT_DIGITS*2+1] = '\0'; + } snprintf(ctx->log.log_path, MAX_STR_SIZE, "%s%s%s-%s.log", user_config_dir, CONFIGDIR, name, ident); diff --git a/src/log.h b/src/log.h index 45b1c62..da50aa2 100644 --- a/src/log.h +++ b/src/log.h @@ -20,9 +20,8 @@ * */ -/* gets a log path by appending to the config dir the name and first 4 chars of the pub_key, - writes current date/time to log file. */ -void init_logging_session(uint8_t *name, uint8_t *pub_key, ChatContext *ctx); +/* gets the log path by appending to the config dir the name and a pseudo-unique identity */ +void init_logging_session(uint8_t *name, uint8_t *key, ChatContext *ctx) /* Adds msg to log_buf with timestamp and name. If buf is full, triggers write_to_log (which sets buf pos to 0) */ From 9b57c05648635edf28188379090f6d90d7f4fd4c Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 26 Feb 2014 19:00:13 -0500 Subject: [PATCH 37/58] add command to turn logs on/off --- src/chat.c | 33 ++++++++++++++++++++++++++++----- src/chat.h | 2 ++ src/execute.c | 1 + src/execute.h | 2 +- src/global_commands.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/global_commands.h | 1 + src/groupchat.c | 30 ++++++++++++++++++++++++++---- src/groupchat.h | 2 ++ src/prompt.c | 1 + src/prompt.h | 2 +- src/toxic_windows.h | 1 + 11 files changed, 106 insertions(+), 11 deletions(-) diff --git a/src/chat.c b/src/chat.c index 1bdb565..9f6065d 100644 --- a/src/chat.c +++ b/src/chat.c @@ -33,6 +33,7 @@ #include "misc_tools.h" #include "friendlist.h" #include "toxic_strings.h" +#include "chat.h" extern char *DATA_FILE; extern int store_data(Tox *m, char *path); @@ -40,7 +41,7 @@ extern int store_data(Tox *m, char *path); extern FileSender file_senders[MAX_FILES]; extern ToxicFriend friends[MAX_FRIENDS_NUM]; -#define AC_NUM_CHAT_COMMANDS 17 +#define AC_NUM_CHAT_COMMANDS 18 /* Array of chat command names used for tab completion. */ static const uint8_t chat_cmd_list[AC_NUM_CHAT_COMMANDS][MAX_CMDNAME_SIZE] = { @@ -54,6 +55,7 @@ static const uint8_t chat_cmd_list[AC_NUM_CHAT_COMMANDS][MAX_CMDNAME_SIZE] = { { "/help" }, { "/invite" }, { "/join" }, + { "/log" }, { "/myid" }, { "/nick" }, { "/note" }, @@ -77,7 +79,7 @@ void kill_chat_window(ToxWindow *self) ChatContext *ctx = self->chatwin; StatusBar *statusbar = self->stb; - write_to_log(ctx); + chat_disable_log(self); int f_num = self->num; delwin(ctx->linewin); @@ -89,6 +91,29 @@ void kill_chat_window(ToxWindow *self) free(statusbar); } +void chat_enable_log(ToxWindow *self) +{ + ChatContext *ctx = self->chatwin; + + if (ctx->log.log_on) + return; + + ctx->log.log_on = true; + + if (!ctx->log.log_path[0]) + init_logging_session(self->name, friends[self->num].pub_key, ctx); +} + +void chat_disable_log(ToxWindow *self) +{ + ChatContext *ctx = self->chatwin; + + if (ctx->log.log_on) { + write_to_log(ctx); + ctx->log.log_on = false; + } +} + static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len) { if (self->num != num) @@ -668,10 +693,8 @@ static void chat_onInit(ToxWindow *self, Tox *m) ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x2, y2-CHATBOX_HEIGHT, 0); wprintw(ctx->history, "\n\n"); execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE); + execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE); wmove(self->window, y2 - CURS_Y_OFFSET, 0); - - ctx->log.log_on = true; - init_logging_session(self->name, friends[self->num].pub_key, ctx); } ToxWindow new_chat(Tox *m, int friendnum) diff --git a/src/chat.h b/src/chat.h index fac132a..8c55234 100644 --- a/src/chat.h +++ b/src/chat.h @@ -25,6 +25,8 @@ #include "toxic_windows.h" +void chat_enable_log(ToxWindow *self); +void chat_disable_log(ToxWindow *self); void kill_chat_window(ToxWindow *self); ToxWindow new_chat(Tox *m, int friendnum); diff --git a/src/execute.c b/src/execute.c index 816ae7f..ce061fb 100644 --- a/src/execute.c +++ b/src/execute.c @@ -41,6 +41,7 @@ static struct cmd_func global_commands[] = { { "/exit", cmd_quit }, { "/groupchat", cmd_groupchat }, { "/help", cmd_prompt_help }, + { "/log", cmd_log }, { "/myid", cmd_myid }, { "/nick", cmd_nick }, { "/note", cmd_note }, diff --git a/src/execute.h b/src/execute.h index e40f028..f4d9b52 100644 --- a/src/execute.h +++ b/src/execute.h @@ -21,7 +21,7 @@ */ #define MAX_NUM_ARGS 4 /* Includes command */ -#define GLOBAL_NUM_COMMANDS 13 +#define GLOBAL_NUM_COMMANDS 14 #define CHAT_NUM_COMMANDS 5 enum { diff --git a/src/global_commands.c b/src/global_commands.c index 129b02c..981d6d1 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -220,6 +220,48 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg wprintw(window, "Group chat created as %d.\n", groupnum); } +void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) +{ + if (!self->is_chat && !self->is_groupchat) /* remove if prompt logging gets implemented */ + return; + + ChatContext *ctx = self->chatwin; + + if (argc == 0) { + uint8_t *s = ctx->log.log_on ? "enabled" : "disabled"; + wprintw(window, "Logging for this chat is currently %s\n", s); + return; + } + + uint8_t *swch = argv[1]; + + if (!strcmp(swch, "1") || !strcmp(swch, "on")) { + if (self->is_chat) + chat_enable_log(self); + else if (self->is_groupchat) + groupchat_enable_log(self); + + wprintw(window, "Logging "); + wattron(window, COLOR_PAIR(GREEN) | A_BOLD); + wprintw(window, "[on]\n"); + wattroff(window, COLOR_PAIR(GREEN) | A_BOLD); + return; + } else if (!strcmp(swch, "0") || !strcmp(swch, "off")) { + if (self->is_chat) + chat_disable_log(self); + else if (self->is_groupchat) + groupchat_disable_log(self); + + wprintw(window, "Logging "); + wattron(window, COLOR_PAIR(RED) | A_BOLD); + wprintw(window, "[off]\n"); + wattroff(window, COLOR_PAIR(RED) | A_BOLD); + return; + } + + wprintw(window, "Invalid option: Use 1 or 0 to enable or disable logging.\n"); +} + void cmd_myid(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { char id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1] = {0}; diff --git a/src/global_commands.h b/src/global_commands.h index ff64323..5b0a66b 100644 --- a/src/global_commands.h +++ b/src/global_commands.h @@ -25,6 +25,7 @@ void cmd_add(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]) void cmd_clear(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_connect(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_groupchat(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_log(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_myid(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_nick(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_note(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); diff --git a/src/groupchat.c b/src/groupchat.c index fd1980f..3abe7b9 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -74,8 +74,8 @@ int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum) void kill_groupchat_window(ToxWindow *self) { ChatContext *ctx = self->chatwin; - write_to_log(ctx); + groupchat_disable_log(self); delwin(ctx->linewin); del_window(self); free(ctx); @@ -102,6 +102,29 @@ static void close_groupchat(ToxWindow *self, Tox *m, int groupnum) kill_groupchat_window(self); } +void groupchat_enable_log(ToxWindow *self) +{ + ChatContext *ctx = self->chatwin; + + if (ctx->log.log_on) + return; + + ctx->log.log_on = true; + + if (!ctx->log.log_path[0]) + init_logging_session(self->name, NULL, ctx); +} + +void groupchat_disable_log(ToxWindow *self) +{ + ChatContext *ctx = self->chatwin; + + if (ctx->log.log_on) { + write_to_log(ctx); + ctx->log.log_on = false; + } +} + static void print_groupchat_help(ChatContext *ctx) { wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD); @@ -599,10 +622,9 @@ static void groupchat_onInit(ToxWindow *self, Tox *m) ctx->sidebar = subwin(self->window, y-CHATBOX_HEIGHT+1, SIDEBAR_WIDTH, 0, x-SIDEBAR_WIDTH); print_groupchat_help(ctx); - wmove(self->window, y-CURS_Y_OFFSET, 0); + execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE); - ctx->log.log_on = true; - init_logging_session(self->name, NULL, ctx); + wmove(self->window, y-CURS_Y_OFFSET, 0); } ToxWindow new_group_chat(Tox *m, int groupnum) diff --git a/src/groupchat.h b/src/groupchat.h index 5b079d5..87c6e4c 100644 --- a/src/groupchat.h +++ b/src/groupchat.h @@ -32,6 +32,8 @@ typedef struct { uint8_t *oldpeer_names; } GroupChat; +void groupchat_enable_log(ToxWindow *self); +void groupchat_disable_log(ToxWindow *self); void kill_groupchat_window(ToxWindow *self); int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum); ToxWindow new_group_chat(Tox *m, int groupnum); diff --git a/src/prompt.c b/src/prompt.c index ca30e32..a1b1e78 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -48,6 +48,7 @@ const uint8_t glob_cmd_list[AC_NUM_GLOB_COMMANDS][MAX_CMDNAME_SIZE] = { { "/groupchat" }, { "/help" }, { "/join" }, + { "/log" }, { "/myid" }, { "/nick" }, { "/note" }, diff --git a/src/prompt.h b/src/prompt.h index 96ca08a..51acd59 100644 --- a/src/prompt.h +++ b/src/prompt.h @@ -25,7 +25,7 @@ #define X_OFST 2 /* offset to account for prompt char */ -#define AC_NUM_GLOB_COMMANDS 14 +#define AC_NUM_GLOB_COMMANDS 15 ToxWindow new_prompt(void); void prep_prompt_win(void); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 889152c..7cd8e54 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -171,6 +171,7 @@ struct PromptBuf { wchar_t line[MAX_STR_SIZE]; size_t pos; size_t len; + bool at_bottom; /* true if line end is at bottom of window */ int orig_y; /* y axis point of line origin */ bool scroll; /* used for prompt window hack to determine when to scroll down */ From af5627b0502ecf9be47f06cc8a780b4c71708671 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 26 Feb 2014 19:02:22 -0500 Subject: [PATCH 38/58] update to 0.2.7 --- configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 9fe3e35..657fdea 100644 --- a/configure.ac +++ b/configure.ac @@ -2,7 +2,7 @@ # Process this file with autoconf to produce a configure script. AC_PREREQ([2.65]) -AC_INIT([toxic], [0.2.6], [https://tox.im/]) +AC_INIT([toxic], [0.2.7], [https://tox.im/]) AC_CONFIG_AUX_DIR(configure_aux) AC_CONFIG_SRCDIR([src/main.c]) AC_CONFIG_HEADERS([config.h]) From d83ef1d8bec698e954f4fac3e0e7309bd022562f Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 26 Feb 2014 19:45:11 -0500 Subject: [PATCH 39/58] update help messages --- src/chat_commands.c | 17 ++++++++++------- src/global_commands.c | 13 ++++++++----- src/groupchat.c | 18 +++++++++++------- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/chat_commands.c b/src/chat_commands.c index 9fdedb3..f866e42 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -30,6 +30,7 @@ #include "toxic_windows.h" #include "misc_tools.h" #include "friendlist.h" +#include "execute.h" extern ToxWindow *prompt; @@ -40,23 +41,25 @@ extern uint8_t max_file_senders_index; void cmd_chat_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { + if (argc == 1) { + if (!strcmp(argv[1], "global")) { + execute(window, self, m, "/help", GLOBAL_COMMAND_MODE); + return; + } + } + wattron(window, COLOR_PAIR(CYAN) | A_BOLD); wprintw(window, "Chat commands:\n"); wattroff(window, COLOR_PAIR(CYAN) | A_BOLD); - wprintw(window, " /status : Set your status with optional note\n"); - wprintw(window, " /note : Set a personal note\n"); - wprintw(window, " /nick : Set your nickname\n"); wprintw(window, " /invite : Invite friend to a group chat\n"); - wprintw(window, " /me : Do an action\n"); - wprintw(window, " /myid : Print your ID\n"); wprintw(window, " /join : Join a pending group chat\n"); - wprintw(window, " /clear : Clear the screen\n"); + wprintw(window, " /log : Enable/disable logging\n"); wprintw(window, " /close : Close the current chat window\n"); wprintw(window, " /sendfile : Send a file\n"); wprintw(window, " /savefile : Receive a file\n"); - wprintw(window, " /quit or /exit : Exit Toxic\n"); wprintw(window, " /help : Print this message again\n"); + wprintw(window, " /help global : Show a list of global commands\n"); wattron(window, COLOR_PAIR(CYAN) | A_BOLD); wprintw(window, " * Argument messages must be enclosed in quotation marks.\n\n"); diff --git a/src/global_commands.c b/src/global_commands.c index 981d6d1..964cb59 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -211,7 +211,7 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg return; } - if (init_groupchat_win(prompt, m, groupnum, NULL) == -1) { + if (init_groupchat_win(prompt, m, groupnum) == -1) { wprintw(window, "Group chat window failed to initialize.\n"); tox_del_groupchat(m, groupnum); return; @@ -228,8 +228,11 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX ChatContext *ctx = self->chatwin; if (argc == 0) { - uint8_t *s = ctx->log.log_on ? "enabled" : "disabled"; - wprintw(window, "Logging for this chat is currently %s\n", s); + if (ctx->log.log_on) + wprintw(window, "Logging for this chat is currently enabled. /log 0 to disable.\n"); + else + wprintw(window, "Logging for this chat is currently disabled. /log 1 to enable.\n"); + return; } @@ -343,14 +346,14 @@ void cmd_prompt_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*a wprintw(window, " /add : Add friend with optional message\n"); wprintw(window, " /accept : Accept friend request\n"); wprintw(window, " /connect : Manually connect to a DHT server\n"); - wprintw(window, " /status : Set your status with optional note\n"); + wprintw(window, " /status : Set status with optional note\n"); wprintw(window, " /note : Set a personal note\n"); wprintw(window, " /nick : Set your nickname\n"); wprintw(window, " /groupchat : Create a group chat\n"); wprintw(window, " /myid : Print your ID\n"); - wprintw(window, " /quit or /exit : Exit Toxic\n"); wprintw(window, " /help : Print this message again\n"); wprintw(window, " /clear : Clear the window\n"); + wprintw(window, " /quit or /exit : Exit Toxic\n"); wattron(window, COLOR_PAIR(CYAN) | A_BOLD); wprintw(window, " * Argument messages must be enclosed in quotation marks.\n"); diff --git a/src/groupchat.c b/src/groupchat.c index 3abe7b9..0cb67fe 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -136,11 +136,10 @@ static void print_groupchat_help(ChatContext *ctx) wprintw(ctx->history, " /note : Set a personal note\n"); wprintw(ctx->history, " /nick : Set your nickname\n"); wprintw(ctx->history, " /groupchat : Create a group chat\n"); - wprintw(ctx->history, " /myid : Print your ID\n"); - wprintw(ctx->history, " /clear : Clear the screen\n"); + wprintw(ctx->history, " /log : Enable/disable logging\n"); wprintw(ctx->history, " /close : Close the current group chat\n"); - wprintw(ctx->history, " /quit or /exit : Exit Toxic\n"); wprintw(ctx->history, " /help : Print this message again\n"); + wprintw(ctx->history, " /help global : Show a list of global commands\n"); wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD); wprintw(ctx->history, " * Argument messages must be enclosed in quotation marks.\n"); @@ -539,12 +538,17 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) if (strcmp(line, "/close") == 0) { close_groupchat(self, m, self->num); return; - } else if (strcmp(line, "/help") == 0) - print_groupchat_help(ctx); - else if (strncmp(line, "/me ", strlen("/me ")) == 0) + } else if (strcmp(line, "/help") == 0) { + if (strcmp(line, "help global") == 0) + execute(ctx->history, self, m, "/help", GLOBAL_COMMAND_MODE); + else + print_groupchat_help(ctx); + + } else if (strncmp(line, "/me ", strlen("/me ")) == 0) { send_group_action(self, ctx, m, line + strlen("/me ")); - else + } else { execute(ctx->history, self, m, line, GROUPCHAT_COMMAND_MODE); + } } else if (!string_is_empty(line)) { if (tox_group_message_send(m, self->num, line, strlen(line) + 1) == -1) { wattron(ctx->history, COLOR_PAIR(RED)); From 044b7310890909e1452ee78c193daf0af14e9161 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 26 Feb 2014 21:00:35 -0500 Subject: [PATCH 40/58] Fix bug --- src/groupchat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/groupchat.c b/src/groupchat.c index 0cb67fe..8fac073 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -318,7 +318,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu if (groupchats[self->num].side_pos > 0) --groupchats[self->num].side_pos; - add_to_log_buf(event, peername, ctx, true); + add_to_log_buf(event, oldpeername, ctx, true); break; case TOX_CHAT_CHANGE_PEER_NAME: From 6ee1f1ed0f3af65213ef0b765c88c733608c2b40 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 26 Feb 2014 21:30:27 -0500 Subject: [PATCH 41/58] fix --- src/groupchat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/groupchat.c b/src/groupchat.c index 8fac073..cc28e45 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -336,7 +336,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu uint8_t tmp_event[TOXIC_MAX_NAME_LENGTH + 32]; snprintf(tmp_event, sizeof(tmp_event), "is now known as %s", peername); - add_to_log_buf(tmp_event, peername, ctx, true); + add_to_log_buf(tmp_event, oldpeername, ctx, true); break; } From 46b046a2091793e19894d0be96a6f034fd9daca4 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Thu, 27 Feb 2014 18:55:18 -0500 Subject: [PATCH 42/58] make C-e and C-aa work like they do in bash and fix/format help messages --- src/chat.c | 4 ++-- src/chat_commands.c | 16 ++++++++-------- src/global_commands.c | 28 ++++++++++++++-------------- src/groupchat.c | 22 +++++++++++----------- src/log.h | 2 +- src/prompt.c | 4 ++-- src/toxic_windows.h | 2 ++ 7 files changed, 40 insertions(+), 38 deletions(-) diff --git a/src/chat.c b/src/chat.c index 9f6065d..df02d1b 100644 --- a/src/chat.c +++ b/src/chat.c @@ -414,14 +414,14 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) beep(); } - else if (key == KEY_HOME) { /* HOME key: Move cursor to beginning of line */ + else if (key == KEY_HOME || key == T_KEY_C_A) { /* HOME/C-a key: Move cursor to start of line */ if (ctx->pos > 0) { ctx->pos = 0; wmove(self->window, y2 - CURS_Y_OFFSET, 0); } } - else if (key == KEY_END) { /* END key: move cursor to end of line */ + else if (key == KEY_END || key == T_KEY_C_E) { /* END/C-e key: move cursor to end of line */ if (ctx->pos != ctx->len) { ctx->pos = ctx->len; mv_curs_end(self->window, MAX(0, wcswidth(ctx->line, (CHATBOX_HEIGHT-1)*x2)), y2, x2); diff --git a/src/chat_commands.c b/src/chat_commands.c index f866e42..18d8646 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -52,14 +52,14 @@ void cmd_chat_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg wprintw(window, "Chat commands:\n"); wattroff(window, COLOR_PAIR(CYAN) | A_BOLD); - wprintw(window, " /invite : Invite friend to a group chat\n"); - wprintw(window, " /join : Join a pending group chat\n"); - wprintw(window, " /log : Enable/disable logging\n"); - wprintw(window, " /close : Close the current chat window\n"); - wprintw(window, " /sendfile : Send a file\n"); - wprintw(window, " /savefile : Receive a file\n"); - wprintw(window, " /help : Print this message again\n"); - wprintw(window, " /help global : Show a list of global commands\n"); + wprintw(window, " /invite : Invite friend to a group chat\n"); + wprintw(window, " /join : Join a pending group chat\n"); + wprintw(window, " /log or : Enable/disable logging\n"); + wprintw(window, " /sendfile : Send a file\n"); + wprintw(window, " /savefile : Receive a file\n"); + wprintw(window, " /close : Close the current chat window\n"); + wprintw(window, " /help : Print this message again\n"); + wprintw(window, " /help global : Show a list of global commands\n"); wattron(window, COLOR_PAIR(CYAN) | A_BOLD); wprintw(window, " * Argument messages must be enclosed in quotation marks.\n\n"); diff --git a/src/global_commands.c b/src/global_commands.c index 964cb59..2466a08 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -229,9 +229,9 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX if (argc == 0) { if (ctx->log.log_on) - wprintw(window, "Logging for this chat is currently enabled. /log 0 to disable.\n"); + wprintw(window, "Logging for this chat is on. Type \"/log off\" to disable.\n"); else - wprintw(window, "Logging for this chat is currently disabled. /log 1 to enable.\n"); + wprintw(window, "Logging for this chat is off. Type \"/log on\" to enable.\n"); return; } @@ -262,7 +262,7 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX return; } - wprintw(window, "Invalid option: Use 1 or 0 to enable or disable logging.\n"); + wprintw(window, "Invalid option. Use \"/log on\" and \"/log off\" to toggle logging.\n"); } void cmd_myid(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) @@ -343,17 +343,17 @@ void cmd_prompt_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*a wprintw(window, "\n\nGlobal commands:\n"); wattroff(window, COLOR_PAIR(CYAN) | A_BOLD); - wprintw(window, " /add : Add friend with optional message\n"); - wprintw(window, " /accept : Accept friend request\n"); - wprintw(window, " /connect : Manually connect to a DHT server\n"); - wprintw(window, " /status : Set status with optional note\n"); - wprintw(window, " /note : Set a personal note\n"); - wprintw(window, " /nick : Set your nickname\n"); - wprintw(window, " /groupchat : Create a group chat\n"); - wprintw(window, " /myid : Print your ID\n"); - wprintw(window, " /help : Print this message again\n"); - wprintw(window, " /clear : Clear the window\n"); - wprintw(window, " /quit or /exit : Exit Toxic\n"); + wprintw(window, " /add : Add friend with optional message\n"); + wprintw(window, " /accept : Accept friend request\n"); + wprintw(window, " /connect : Manually connect to a DHT server\n"); + wprintw(window, " /status : Set status with optional note\n"); + wprintw(window, " /note : Set a personal note\n"); + wprintw(window, " /nick : Set your nickname\n"); + wprintw(window, " /groupchat : Create a group chat\n"); + wprintw(window, " /myid : Print your ID\n"); + wprintw(window, " /help : Print this message again\n"); + wprintw(window, " /clear : Clear the window\n"); + wprintw(window, " /quit or /exit : Exit Toxic\n"); wattron(window, COLOR_PAIR(CYAN) | A_BOLD); wprintw(window, " * Argument messages must be enclosed in quotation marks.\n"); diff --git a/src/groupchat.c b/src/groupchat.c index cc28e45..678657e 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -131,15 +131,15 @@ static void print_groupchat_help(ChatContext *ctx) wprintw(ctx->history, "Group chat commands:\n"); wattroff(ctx->history, COLOR_PAIR(CYAN) | A_BOLD); - wprintw(ctx->history, " /add : Add friend with optional message\n"); - wprintw(ctx->history, " /status : Set your status with optional note\n"); - wprintw(ctx->history, " /note : Set a personal note\n"); - wprintw(ctx->history, " /nick : Set your nickname\n"); - wprintw(ctx->history, " /groupchat : Create a group chat\n"); - wprintw(ctx->history, " /log : Enable/disable logging\n"); - wprintw(ctx->history, " /close : Close the current group chat\n"); - wprintw(ctx->history, " /help : Print this message again\n"); - wprintw(ctx->history, " /help global : Show a list of global commands\n"); + wprintw(ctx->history, " /add : Add friend with optional message\n"); + wprintw(ctx->history, " /status : Set your status with optional note\n"); + wprintw(ctx->history, " /note : Set a personal note\n"); + wprintw(ctx->history, " /nick : Set your nickname\n"); + wprintw(ctx->history, " /groupchat : Create a group chat\n"); + wprintw(ctx->history, " /log or : Enable/disable logging\n"); + wprintw(ctx->history, " /close : Close the current group chat\n"); + wprintw(ctx->history, " /help : Print this message again\n"); + wprintw(ctx->history, " /help global : Show a list of global commands\n"); wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD); wprintw(ctx->history, " * Argument messages must be enclosed in quotation marks.\n"); @@ -410,14 +410,14 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) beep(); } - else if (key == KEY_HOME) { /* HOME key: Move cursor to beginning of line */ + else if (key == KEY_HOME || key == T_KEY_C_A) { /* HOME/C-a key: Move cursor to start of line */ if (ctx->pos > 0) { ctx->pos = 0; wmove(self->window, y2 - CURS_Y_OFFSET, 0); } } - else if (key == KEY_END) { /* END key: move cursor to end of line */ + else if (key == KEY_END || key == T_KEY_C_E) { /* END/C-e key: move cursor to end of line */ if (ctx->pos != ctx->len) { ctx->pos = ctx->len; mv_curs_end(self->window, MAX(0, wcswidth(ctx->line, (CHATBOX_HEIGHT-1)*x2)), y2, x2); diff --git a/src/log.h b/src/log.h index da50aa2..0556e09 100644 --- a/src/log.h +++ b/src/log.h @@ -21,7 +21,7 @@ */ /* gets the log path by appending to the config dir the name and a pseudo-unique identity */ -void init_logging_session(uint8_t *name, uint8_t *key, ChatContext *ctx) +void init_logging_session(uint8_t *name, uint8_t *key, ChatContext *ctx); /* Adds msg to log_buf with timestamp and name. If buf is full, triggers write_to_log (which sets buf pos to 0) */ diff --git a/src/prompt.c b/src/prompt.c index a1b1e78..fb143ce 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -173,12 +173,12 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key) beep(); } - else if (key == KEY_HOME) { /* HOME key: Move cursor to beginning of line */ + else if (key == KEY_HOME || key == T_KEY_C_A) { /* HOME/C-a key: Move cursor to start of line */ if (prt->pos != 0) prt->pos = 0; } - else if (key == KEY_END) { /* END key: move cursor to end of line */ + else if (key == KEY_END || key == T_KEY_C_E) { /* END/C-e key: move cursor to end of line */ if (prt->pos != prt->len) prt->pos = prt->len; } diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 7cd8e54..ae58cab 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -54,6 +54,8 @@ #define T_KEY_DISCARD 0x15 /* ctrl-u */ #define T_KEY_NEXT 0x10 /* ctrl-p */ #define T_KEY_PREV 0x0F /* ctrl-o */ +#define T_KEY_C_E 0x05 /* ctrl-e */ +#define T_KEY_C_A 0x01 /* ctrl-a */ /* Curses foreground colours (background is black) */ enum { From 4fb82cceaab91ebf51184d1c37850e8a98cbd2fe Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Thu, 27 Feb 2014 23:33:00 -0500 Subject: [PATCH 43/58] save logging preference for friend chats and improve log command message --- src/chat.c | 18 ++++++++++-------- src/friendlist.h | 1 + src/global_commands.c | 34 +++++++++++++++++++++++++--------- src/groupchat.c | 3 --- src/log.c | 4 ++-- src/toxic_windows.h | 2 +- 6 files changed, 39 insertions(+), 23 deletions(-) diff --git a/src/chat.c b/src/chat.c index df02d1b..d89b35a 100644 --- a/src/chat.c +++ b/src/chat.c @@ -95,9 +95,6 @@ void chat_enable_log(ToxWindow *self) { ChatContext *ctx = self->chatwin; - if (ctx->log.log_on) - return; - ctx->log.log_on = true; if (!ctx->log.log_path[0]) @@ -108,10 +105,10 @@ void chat_disable_log(ToxWindow *self) { ChatContext *ctx = self->chatwin; - if (ctx->log.log_on) { + if (ctx->log.log_on) write_to_log(ctx); - ctx->log.log_on = false; - } + + ctx->log.log_on = false; } static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len) @@ -692,9 +689,14 @@ static void chat_onInit(ToxWindow *self, Tox *m) scrollok(ctx->history, 1); ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x2, y2-CHATBOX_HEIGHT, 0); wprintw(ctx->history, "\n\n"); - execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE); - execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE); wmove(self->window, y2 - CURS_Y_OFFSET, 0); + + execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE); + + if (friends[self->num].logging_on) + chat_enable_log(self); + + execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE); } ToxWindow new_chat(Tox *m, int friendnum) diff --git a/src/friendlist.h b/src/friendlist.h index a468273..09ffe62 100644 --- a/src/friendlist.h +++ b/src/friendlist.h @@ -37,6 +37,7 @@ typedef struct { bool active; bool online; bool is_typing; + bool logging_on; /* saves preference for friend irrespective of chat windows */ TOX_USERSTATUS status; struct FileReceiver file_receiver; } ToxicFriend; diff --git a/src/global_commands.c b/src/global_commands.c index 2466a08..fd8353c 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -222,16 +222,27 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { - if (!self->is_chat && !self->is_groupchat) /* remove if prompt logging gets implemented */ + if (!self->is_chat && !self->is_groupchat) { /* remove if prompt logging gets implemented */ + wprintw(window, "Invalid command.\n"); return; + } ChatContext *ctx = self->chatwin; if (argc == 0) { - if (ctx->log.log_on) - wprintw(window, "Logging for this chat is on. Type \"/log off\" to disable.\n"); - else - wprintw(window, "Logging for this chat is off. Type \"/log on\" to enable.\n"); + if (ctx->log.log_on) { + wprintw(window, "Logging for this chat is "); + wattron(window, COLOR_PAIR(GREEN) | A_BOLD); + wprintw(window, "[on]"); + wattroff(window, COLOR_PAIR(GREEN) | A_BOLD); + wprintw(window, ". Type \"/log off\" to disable.\n"); + } else { + wprintw(window, "Logging for this chat is "); + wattron(window, COLOR_PAIR(RED) | A_BOLD); + wprintw(window, "[off]"); + wattroff(window, COLOR_PAIR(RED) | A_BOLD); + wprintw(window, ". Type \"/log on\" to enable.\n"); + } return; } @@ -239,10 +250,13 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX uint8_t *swch = argv[1]; if (!strcmp(swch, "1") || !strcmp(swch, "on")) { - if (self->is_chat) + if (self->is_chat) { chat_enable_log(self); - else if (self->is_groupchat) + friends[self->num].logging_on = true; + } else if (self->is_groupchat) { groupchat_enable_log(self); + friends[self->num].logging_on = false; + } wprintw(window, "Logging "); wattron(window, COLOR_PAIR(GREEN) | A_BOLD); @@ -250,10 +264,12 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX wattroff(window, COLOR_PAIR(GREEN) | A_BOLD); return; } else if (!strcmp(swch, "0") || !strcmp(swch, "off")) { - if (self->is_chat) + if (self->is_chat) { chat_disable_log(self); - else if (self->is_groupchat) + friends[self->num].logging_on = false; + } else if (self->is_groupchat) { groupchat_disable_log(self); + } wprintw(window, "Logging "); wattron(window, COLOR_PAIR(RED) | A_BOLD); diff --git a/src/groupchat.c b/src/groupchat.c index 678657e..5df6178 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -106,9 +106,6 @@ void groupchat_enable_log(ToxWindow *self) { ChatContext *ctx = self->chatwin; - if (ctx->log.log_on) - return; - ctx->log.log_on = true; if (!ctx->log.log_path[0]) diff --git a/src/log.c b/src/log.c index f8d2de8..023eba6 100644 --- a/src/log.c +++ b/src/log.c @@ -48,7 +48,7 @@ void init_logging_session(uint8_t *name, uint8_t *key, ChatContext *ctx) ident[KEY_IDENT_DIGITS*2+1] = '\0'; } else { struct tm *tminfo = get_time(); - snprintf(ident, 32, + snprintf(ident, sizeof(ident), "%04d-%02d-%02d-%02d:%02d:%02d", tminfo->tm_year+1900,tminfo->tm_mon+1, tminfo->tm_mday, tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec); path_len += strlen(ident) + 1; @@ -69,7 +69,7 @@ void init_logging_session(uint8_t *name, uint8_t *key, ChatContext *ctx) return; } - fprintf(logfile, "\n***NEW SESSION***\n\n"); + fprintf(logfile, "\n*** NEW SESSION ***\n\n"); fclose(logfile); free(user_config_dir); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index ae58cab..fc0bbe2 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -144,7 +144,7 @@ struct chatlog { uint8_t log_path[MAX_STR_SIZE]; uint8_t log_buf[MAX_LOG_BUF_LINES][MAX_LOG_LINE_SIZE]; int pos; - bool log_on; + bool log_on; /* specific to current chat window */ }; #define MAX_LINE_HIST 128 From 1e503b1080ce6902294115e38192314188bf7093 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Thu, 27 Feb 2014 23:38:15 -0500 Subject: [PATCH 44/58] fix --- src/global_commands.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/global_commands.c b/src/global_commands.c index fd8353c..d3f2d4a 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -255,7 +255,6 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX friends[self->num].logging_on = true; } else if (self->is_groupchat) { groupchat_enable_log(self); - friends[self->num].logging_on = false; } wprintw(window, "Logging "); From 7109b8fa18f50866c66f60bb75edc055e98f0786 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sat, 1 Mar 2014 07:10:44 -0500 Subject: [PATCH 45/58] refactor logging functions to only handle chatlog pointers --- src/chat.c | 51 ++++++++++++++++--------------------- src/chat.h | 2 -- src/global_commands.c | 17 ++++++------- src/groupchat.c | 45 ++++++++++++++------------------- src/groupchat.h | 2 -- src/log.c | 58 +++++++++++++++++++++++++++---------------- src/log.h | 13 +++++----- src/misc_tools.c | 4 +-- src/misc_tools.h | 4 +-- src/toxic_windows.h | 2 +- 10 files changed, 95 insertions(+), 103 deletions(-) diff --git a/src/chat.c b/src/chat.c index d89b35a..3b4d3a9 100644 --- a/src/chat.c +++ b/src/chat.c @@ -33,7 +33,7 @@ #include "misc_tools.h" #include "friendlist.h" #include "toxic_strings.h" -#include "chat.h" +#include "log.h" extern char *DATA_FILE; extern int store_data(Tox *m, char *path); @@ -79,7 +79,7 @@ void kill_chat_window(ToxWindow *self) ChatContext *ctx = self->chatwin; StatusBar *statusbar = self->stb; - chat_disable_log(self); + log_disable(ctx->log); int f_num = self->num; delwin(ctx->linewin); @@ -87,30 +87,11 @@ void kill_chat_window(ToxWindow *self) del_window(self); disable_chatwin(f_num); + free(ctx->log); free(ctx); free(statusbar); } -void chat_enable_log(ToxWindow *self) -{ - ChatContext *ctx = self->chatwin; - - ctx->log.log_on = true; - - if (!ctx->log.log_path[0]) - init_logging_session(self->name, friends[self->num].pub_key, ctx); -} - -void chat_disable_log(ToxWindow *self) -{ - ChatContext *ctx = self->chatwin; - - if (ctx->log.log_on) - write_to_log(ctx); - - ctx->log.log_on = false; -} - static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len) { if (self->num != num) @@ -134,7 +115,7 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1 } else wprintw(ctx->history, "%s\n", msg); - add_to_log_buf(msg, nick, ctx, false); + add_to_log_buf(msg, nick, ctx->log, false); alert_window(self, WINDOW_ALERT_1, true); } @@ -178,7 +159,7 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin wprintw(ctx->history, "* %s %s\n", nick, action); wattroff(ctx->history, COLOR_PAIR(YELLOW)); - add_to_log_buf(action, nick, ctx, true); + add_to_log_buf(action, nick, ctx->log, true); alert_window(self, WINDOW_ALERT_1, true); } @@ -360,7 +341,7 @@ static void send_action(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *acti wprintw(ctx->history, " * Failed to send action\n"); wattroff(ctx->history, COLOR_PAIR(RED)); } else { - add_to_log_buf(action, selfname, ctx, true); + add_to_log_buf(action, selfname, ctx->log, true); } } @@ -551,7 +532,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) wprintw(ctx->history, " * Failed to send message.\n"); wattroff(ctx->history, COLOR_PAIR(RED)); } else { - add_to_log_buf(line, selfname, ctx, false); + add_to_log_buf(line, selfname, ctx->log, false); } } @@ -688,15 +669,25 @@ static void chat_onInit(ToxWindow *self, Tox *m) ctx->history = subwin(self->window, y2-CHATBOX_HEIGHT+1, x2, 0, 0); scrollok(ctx->history, 1); ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x2, y2-CHATBOX_HEIGHT, 0); - wprintw(ctx->history, "\n\n"); - wmove(self->window, y2 - CURS_Y_OFFSET, 0); - execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE); + ctx->log = malloc(sizeof(struct chatlog)); + + if (ctx->log == NULL) { + endwin(); + fprintf(stderr, "malloc() failed. Aborting...\n"); + exit(EXIT_FAILURE); + } + + memset(ctx->log, 0, sizeof(struct chatlog)); if (friends[self->num].logging_on) - chat_enable_log(self); + log_enable(ctx->log, self->name, friends[self->num].pub_key); + wprintw(ctx->history, "\n\n"); + execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE); execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE); + + wmove(self->window, y2 - CURS_Y_OFFSET, 0); } ToxWindow new_chat(Tox *m, int friendnum) diff --git a/src/chat.h b/src/chat.h index 8c55234..fac132a 100644 --- a/src/chat.h +++ b/src/chat.h @@ -25,8 +25,6 @@ #include "toxic_windows.h" -void chat_enable_log(ToxWindow *self); -void chat_disable_log(ToxWindow *self); void kill_chat_window(ToxWindow *self); ToxWindow new_chat(Tox *m, int friendnum); diff --git a/src/global_commands.c b/src/global_commands.c index d3f2d4a..8072157 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -230,7 +230,7 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX ChatContext *ctx = self->chatwin; if (argc == 0) { - if (ctx->log.log_on) { + if (ctx->log->log_on) { wprintw(window, "Logging for this chat is "); wattron(window, COLOR_PAIR(GREEN) | A_BOLD); wprintw(window, "[on]"); @@ -248,27 +248,26 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX } uint8_t *swch = argv[1]; + uint8_t *ident = NULL; if (!strcmp(swch, "1") || !strcmp(swch, "on")) { if (self->is_chat) { - chat_enable_log(self); friends[self->num].logging_on = true; - } else if (self->is_groupchat) { - groupchat_enable_log(self); + ident = friends[self->num].pub_key; } + log_enable(ctx->log, self->name, ident); + wprintw(window, "Logging "); wattron(window, COLOR_PAIR(GREEN) | A_BOLD); wprintw(window, "[on]\n"); wattroff(window, COLOR_PAIR(GREEN) | A_BOLD); return; } else if (!strcmp(swch, "0") || !strcmp(swch, "off")) { - if (self->is_chat) { - chat_disable_log(self); + if (self->is_chat) friends[self->num].logging_on = false; - } else if (self->is_groupchat) { - groupchat_disable_log(self); - } + + log_disable(ctx->log); wprintw(window, "Logging "); wattron(window, COLOR_PAIR(RED) | A_BOLD); diff --git a/src/groupchat.c b/src/groupchat.c index 5df6178..e6b3b30 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -34,6 +34,7 @@ #include "groupchat.h" #include "prompt.h" #include "toxic_strings.h" +#include "log.h" extern char *DATA_FILE; extern int store_data(Tox *m, char *path); @@ -75,9 +76,10 @@ void kill_groupchat_window(ToxWindow *self) { ChatContext *ctx = self->chatwin; - groupchat_disable_log(self); + log_disable(ctx->log); delwin(ctx->linewin); del_window(self); + free(ctx->log); free(ctx); } @@ -98,30 +100,9 @@ static void close_groupchat(ToxWindow *self, Tox *m, int groupnum) } max_groupchat_index = i; - kill_groupchat_window(self); } -void groupchat_enable_log(ToxWindow *self) -{ - ChatContext *ctx = self->chatwin; - - ctx->log.log_on = true; - - if (!ctx->log.log_path[0]) - init_logging_session(self->name, NULL, ctx); -} - -void groupchat_disable_log(ToxWindow *self) -{ - ChatContext *ctx = self->chatwin; - - if (ctx->log.log_on) { - write_to_log(ctx); - ctx->log.log_on = false; - } -} - static void print_groupchat_help(ChatContext *ctx) { wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD); @@ -187,7 +168,7 @@ static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, int groupnum, int wprintw(ctx->history, "%s\n", msg); } - add_to_log_buf(msg, nick, ctx, false); + add_to_log_buf(msg, nick, ctx->log, false); } static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int peernum, uint8_t *action, @@ -223,7 +204,7 @@ static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int p wprintw(ctx->history, "* %s %s\n", nick, action); wattroff(ctx->history, COLOR_PAIR(YELLOW)); - add_to_log_buf(action, nick, ctx, true); + add_to_log_buf(action, nick, ctx->log, true); } /* Puts two copies of peerlist in chat instance */ @@ -301,7 +282,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu wprintw(ctx->history, " %s\n", event); wattroff(ctx->history, COLOR_PAIR(GREEN)); - add_to_log_buf(event, peername, ctx, true); + add_to_log_buf(event, peername, ctx->log, true); break; case TOX_CHAT_CHANGE_PEER_DEL: @@ -315,7 +296,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu if (groupchats[self->num].side_pos > 0) --groupchats[self->num].side_pos; - add_to_log_buf(event, oldpeername, ctx, true); + add_to_log_buf(event, oldpeername, ctx->log, true); break; case TOX_CHAT_CHANGE_PEER_NAME: @@ -333,7 +314,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu uint8_t tmp_event[TOXIC_MAX_NAME_LENGTH + 32]; snprintf(tmp_event, sizeof(tmp_event), "is now known as %s", peername); - add_to_log_buf(tmp_event, oldpeername, ctx, true); + add_to_log_buf(tmp_event, oldpeername, ctx->log, true); break; } @@ -622,6 +603,16 @@ static void groupchat_onInit(ToxWindow *self, Tox *m) ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x, y-CHATBOX_HEIGHT, 0); ctx->sidebar = subwin(self->window, y-CHATBOX_HEIGHT+1, SIDEBAR_WIDTH, 0, x-SIDEBAR_WIDTH); + ctx->log = malloc(sizeof(struct chatlog)); + + if (ctx->log == NULL) { + endwin(); + fprintf(stderr, "malloc() failed. Aborting...\n"); + exit(EXIT_FAILURE); + } + + memset(ctx->log, 0, sizeof(struct chatlog)); + print_groupchat_help(ctx); execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE); diff --git a/src/groupchat.h b/src/groupchat.h index 87c6e4c..5b079d5 100644 --- a/src/groupchat.h +++ b/src/groupchat.h @@ -32,8 +32,6 @@ typedef struct { uint8_t *oldpeer_names; } GroupChat; -void groupchat_enable_log(ToxWindow *self); -void groupchat_disable_log(ToxWindow *self); void kill_groupchat_window(ToxWindow *self); int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum); ToxWindow new_group_chat(Tox *m, int groupnum); diff --git a/src/log.c b/src/log.c index 023eba6..14cb648 100644 --- a/src/log.c +++ b/src/log.c @@ -1,4 +1,4 @@ -/* ctx->log.c +/* log->c * * * Copyright (C) 2014 Toxic All Rights Reserved. @@ -29,9 +29,9 @@ #include "misc_tools.h" /* gets the log path by appending to the config dir the name and a pseudo-unique identity */ -void init_logging_session(uint8_t *name, uint8_t *key, ChatContext *ctx) +void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log) { - if (!ctx->log.log_on) + if (!log->log_on) return; char *user_config_dir = get_user_config_dir(); @@ -49,23 +49,23 @@ void init_logging_session(uint8_t *name, uint8_t *key, ChatContext *ctx) } else { struct tm *tminfo = get_time(); snprintf(ident, sizeof(ident), - "%04d-%02d-%02d-%02d:%02d:%02d", tminfo->tm_year+1900,tminfo->tm_mon+1, tminfo->tm_mday, - tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec); + "%04d-%02d-%02d[%02d:%02d:%02d]", tminfo->tm_year+1900,tminfo->tm_mon+1, tminfo->tm_mday, + tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec); path_len += strlen(ident) + 1; } if (path_len > MAX_STR_SIZE) { - ctx->log.log_on = false; + log->log_on = false; return; } - snprintf(ctx->log.log_path, MAX_STR_SIZE, "%s%s%s-%s.log", + snprintf(log->log_path, MAX_STR_SIZE, "%s%s%s-%s.log", user_config_dir, CONFIGDIR, name, ident); - FILE *logfile = fopen(ctx->log.log_path, "a"); + FILE *logfile = fopen(log->log_path, "a"); if (logfile == NULL) { - ctx->log.log_on = false; + log->log_on = false; return; } @@ -77,32 +77,32 @@ void init_logging_session(uint8_t *name, uint8_t *key, ChatContext *ctx) /* writes contents from a chatcontext's log buffer to respective log file and resets log pos. This is triggered when the log buffer is full, but may be forced. */ -void write_to_log(ChatContext *ctx) +void write_to_log(struct chatlog *log) { - if (!ctx->log.log_on) + if (!log->log_on) return; - FILE *logfile = fopen(ctx->log.log_path, "a"); + FILE *logfile = fopen(log->log_path, "a"); if (logfile == NULL) { - ctx->log.log_on = false; + log->log_on = false; return; } int i; - for (i = 0; i < ctx->log.pos; ++i) - fprintf(logfile, "%s", ctx->log.log_buf[i]); + for (i = 0; i < log->pos; ++i) + fprintf(logfile, "%s", log->log_buf[i]); - ctx->log.pos = 0; + log->pos = 0; fclose(logfile); } /* Adds line/event to log_buf with timestamp and name. If buf is full, triggers write_to_log. If event is true, formats line as an event, e.g. * name has gone offline */ -void add_to_log_buf(uint8_t *msg, uint8_t *name, ChatContext *ctx, bool event) +void add_to_log_buf(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event) { - if (!ctx->log.log_on) + if (!log->log_on) return; uint8_t name_frmt[TOXIC_MAX_NAME_LENGTH + 3]; @@ -113,10 +113,26 @@ void add_to_log_buf(uint8_t *msg, uint8_t *name, ChatContext *ctx, bool event) snprintf(name_frmt, sizeof(name_frmt), "%s:", name); struct tm *tminfo = get_time(); - snprintf(ctx->log.log_buf[ctx->log.pos], MAX_LOG_LINE_SIZE, "%04d/%02d/%02d [%02d:%02d:%02d] %s %s\n", + snprintf(log->log_buf[log->pos], MAX_LOG_LINE_SIZE, "%04d/%02d/%02d [%02d:%02d:%02d] %s %s\n", tminfo->tm_year + 1900, tminfo->tm_mon + 1, tminfo->tm_mday, tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec, name_frmt, msg); - if (++(ctx->log.pos) >= MAX_LOG_BUF_LINES) - write_to_log(ctx); + if (++(log->pos) >= MAX_LOG_BUF_LINES) + write_to_log(log); +} + +void log_enable(struct chatlog *log, uint8_t *name, uint8_t *key) +{ + log->log_on = true; + + if (!log->log_path[0]) + init_logging_session(name, key, log); +} + +void log_disable(struct chatlog *log) +{ + if (log->log_on) { + write_to_log(log); + log->log_on = false; + } } diff --git a/src/log.h b/src/log.h index 0556e09..3854f00 100644 --- a/src/log.h +++ b/src/log.h @@ -21,16 +21,15 @@ */ /* gets the log path by appending to the config dir the name and a pseudo-unique identity */ -void init_logging_session(uint8_t *name, uint8_t *key, ChatContext *ctx); - -/* Adds msg to log_buf with timestamp and name. - If buf is full, triggers write_to_log (which sets buf pos to 0) */ -void add_line_log_buf(uint8_t *msg, uint8_t *name, ChatContext *ctx); +void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log); /* Adds line/event to log_buf with timestamp and name. If buf is full, triggers write_to_log. If event is true, formats line as an event, e.g. * name has gone offline */ -void add_to_log_buf(uint8_t *msg, uint8_t *name, ChatContext *ctx, bool event); +void add_to_log_buf(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event); /* writes contents from a chatcontext's log buffer to respective log file and resets log pos. This is triggered automatically when the log buffer is full, but may be forced. */ -void write_to_log(ChatContext *ctx); +void write_to_log(struct chatlog *log); + +void log_enable(struct chatlog *log, uint8_t *name, uint8_t *key); +void log_disable(struct chatlog *log); \ No newline at end of file diff --git a/src/misc_tools.c b/src/misc_tools.c index a037c47..be41bc1 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -70,8 +70,8 @@ void print_time(WINDOW *window) wattroff(window,COLOR_PAIR(BLUE)); } -/* Returns 1 if the string is empty, 0 otherwise */ -int string_is_empty(char *string) +/* Returns true if the string is empty, false otherwise */ +bool string_is_empty(char *string) { return string[0] == '\0'; } diff --git a/src/misc_tools.h b/src/misc_tools.h index 4d711e9..af264b9 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -32,8 +32,8 @@ struct tm *get_time(void); /* Prints the time to given window */ void print_time(WINDOW *window); -/* Returns 1 if the string is empty, 0 otherwise */ -int string_is_empty(char *string); +/* Returns true if the string is empty, false otherwise */ +bool string_is_empty(char *string); /* convert a multibyte string to a wide character string (must provide buffer) */ int char_to_wcs_buf(wchar_t *buf, const uint8_t *string, size_t n); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index fc0bbe2..7466acf 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -161,7 +161,7 @@ struct ChatContext { bool self_is_typing; - struct chatlog log; + struct chatlog *log; WINDOW *history; WINDOW *linewin; From 7f38c3c6e7e7d0cd6091a4f152a15932995f2cd4 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sat, 1 Mar 2014 18:06:35 -0500 Subject: [PATCH 46/58] add prompt logging support --- src/chat.c | 2 +- src/global_commands.c | 43 ++++++++++++++++++++++++++----------------- src/log.c | 4 ++-- src/log.h | 4 ++-- src/main.c | 2 ++ src/prompt.c | 38 +++++++++++++++++++++++++++++++++----- src/toxic_windows.h | 2 ++ 7 files changed, 68 insertions(+), 27 deletions(-) diff --git a/src/chat.c b/src/chat.c index 3b4d3a9..5c8ef1b 100644 --- a/src/chat.c +++ b/src/chat.c @@ -681,7 +681,7 @@ static void chat_onInit(ToxWindow *self, Tox *m) memset(ctx->log, 0, sizeof(struct chatlog)); if (friends[self->num].logging_on) - log_enable(ctx->log, self->name, friends[self->num].pub_key); + log_enable(self->name, friends[self->num].pub_key, ctx->log); wprintw(ctx->history, "\n\n"); execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE); diff --git a/src/global_commands.c b/src/global_commands.c index 8072157..f15cc9e 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -222,22 +222,22 @@ void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*arg void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { - if (!self->is_chat && !self->is_groupchat) { /* remove if prompt logging gets implemented */ - wprintw(window, "Invalid command.\n"); - return; - } - - ChatContext *ctx = self->chatwin; - if (argc == 0) { - if (ctx->log->log_on) { - wprintw(window, "Logging for this chat is "); + bool on; + + if (self->is_chat || self->is_groupchat) + on = self->chatwin->log->log_on; + else if (self->is_prompt) + on = self->promptbuf->log->log_on; + + if (on) { + wprintw(window, "Logging for this window is "); wattron(window, COLOR_PAIR(GREEN) | A_BOLD); wprintw(window, "[on]"); wattroff(window, COLOR_PAIR(GREEN) | A_BOLD); wprintw(window, ". Type \"/log off\" to disable.\n"); } else { - wprintw(window, "Logging for this chat is "); + wprintw(window, "Logging for this window is "); wattron(window, COLOR_PAIR(RED) | A_BOLD); wprintw(window, "[off]"); wattroff(window, COLOR_PAIR(RED) | A_BOLD); @@ -248,26 +248,34 @@ void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX } uint8_t *swch = argv[1]; - uint8_t *ident = NULL; if (!strcmp(swch, "1") || !strcmp(swch, "on")) { + if (self->is_chat) { friends[self->num].logging_on = true; - ident = friends[self->num].pub_key; + log_enable(self->name, friends[self->num].pub_key, self->chatwin->log); + } else if (self->is_prompt) { + uint8_t myid[TOX_FRIEND_ADDRESS_SIZE]; + tox_get_address(m, myid); + log_enable(self->name, &myid, self->promptbuf->log); + } else if (self->is_groupchat) { + log_enable(self->name, NULL, self->chatwin->log); } - log_enable(ctx->log, self->name, ident); - wprintw(window, "Logging "); wattron(window, COLOR_PAIR(GREEN) | A_BOLD); wprintw(window, "[on]\n"); wattroff(window, COLOR_PAIR(GREEN) | A_BOLD); return; } else if (!strcmp(swch, "0") || !strcmp(swch, "off")) { - if (self->is_chat) + if (self->is_chat) { friends[self->num].logging_on = false; - - log_disable(ctx->log); + log_disable(self->chatwin->log); + } else if (self->is_prompt) { + log_disable(self->promptbuf->log); + } else if (self->is_groupchat) { + log_disable(self->chatwin->log); + } wprintw(window, "Logging "); wattron(window, COLOR_PAIR(RED) | A_BOLD); @@ -363,6 +371,7 @@ void cmd_prompt_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*a wprintw(window, " /status : Set status with optional note\n"); wprintw(window, " /note : Set a personal note\n"); wprintw(window, " /nick : Set your nickname\n"); + wprintw(window, " /log or : Enable/disable logging\n"); wprintw(window, " /groupchat : Create a group chat\n"); wprintw(window, " /myid : Print your ID\n"); wprintw(window, " /help : Print this message again\n"); diff --git a/src/log.c b/src/log.c index 14cb648..2a83db6 100644 --- a/src/log.c +++ b/src/log.c @@ -1,4 +1,4 @@ -/* log->c +/* log.c * * * Copyright (C) 2014 Toxic All Rights Reserved. @@ -121,7 +121,7 @@ void add_to_log_buf(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event write_to_log(log); } -void log_enable(struct chatlog *log, uint8_t *name, uint8_t *key) +void log_enable(uint8_t *name, uint8_t *key, struct chatlog *log) { log->log_on = true; diff --git a/src/log.h b/src/log.h index 3854f00..e4ab01c 100644 --- a/src/log.h +++ b/src/log.h @@ -31,5 +31,5 @@ void add_to_log_buf(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event This is triggered automatically when the log buffer is full, but may be forced. */ void write_to_log(struct chatlog *log); -void log_enable(struct chatlog *log, uint8_t *name, uint8_t *key); -void log_disable(struct chatlog *log); \ No newline at end of file +void log_enable(uint8_t *name, uint8_t *key, struct chatlog *log); +void log_disable(struct chatlog *log); diff --git a/src/main.c b/src/main.c index 21ce5a7..fd4679d 100644 --- a/src/main.c +++ b/src/main.c @@ -479,6 +479,8 @@ void exit_toxic(Tox *m) free(DATA_FILE); free(SRVLIST_FILE); free(prompt->stb); + log_disable(prompt->promptbuf->log); + free(prompt->promptbuf->log); free(prompt->promptbuf); tox_kill(m); endwin(); diff --git a/src/prompt.c b/src/prompt.c index fb143ce..9116123 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -360,6 +360,18 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) static void prompt_onInit(ToxWindow *self, Tox *m) { scrollok(self->window, true); + PromptBuf *prt = self->promptbuf; + + prt->log = malloc(sizeof(struct chatlog)); + + if (prt->log == NULL) { + endwin(); + fprintf(stderr, "malloc() failed. Aborting...\n"); + exit(EXIT_FAILURE); + } + + memset(prt->log, 0, sizeof(struct chatlog)); + execute(self->window, self, m, "/help", GLOBAL_COMMAND_MODE); wclrtoeol(self->window); } @@ -369,6 +381,7 @@ static void prompt_onConnectionChange(ToxWindow *self, Tox *m, int friendnum , u if (friendnum < 0) return; + PromptBuf *prt = self->promptbuf; prep_prompt_win(); uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; @@ -382,22 +395,29 @@ static void prompt_onConnectionChange(ToxWindow *self, Tox *m, int friendnum , u wprintw(self->window, "\n"); print_time(self->window); + uint8_t *msg; + if (status == 1) { + msg = "has come online\n"; wattron(self->window, COLOR_PAIR(GREEN)); wattron(self->window, A_BOLD); wprintw(self->window, "* %s ", nick); wattroff(self->window, A_BOLD); - wprintw(self->window, "has come online\n"); + wprintw(self->window, "%s", msg); wattroff(self->window, COLOR_PAIR(GREEN)); + add_to_log_buf(msg, nick, prt->log, true); alert_window(self, WINDOW_ALERT_2, false); } else { + msg = "has gone offline\n"; wattron(self->window, COLOR_PAIR(RED)); wattron(self->window, A_BOLD); wprintw(self->window, "* %s ", nick); wattroff(self->window, A_BOLD); - wprintw(self->window, "has gone offline\n"); + wprintw(self->window, "%s", msg); wattroff(self->window, COLOR_PAIR(RED)); + + add_to_log_buf(msg, nick, prt->log, true); } } @@ -405,16 +425,23 @@ static void prompt_onFriendRequest(ToxWindow *self, uint8_t *key, uint8_t *data, { // make sure message data is null-terminated data[length - 1] = 0; - + PromptBuf *prt = self->promptbuf; prep_prompt_win(); + wprintw(self->window, "\n"); print_time(self->window); - wprintw(self->window, "Friend request with the message: '%s'\n", data); + + uint8_t msg[MAX_STR_SIZE]; + snprintf(msg, sizeof(msg), "Friend request with the message '%s'\n", data); + wprintw(self->window, "%s", msg); + add_to_log_buf(msg, "", prt->log, true); int n = add_friend_request(key); if (n == -1) { - wprintw(self->window, "Friend request queue is full. Discarding request.\n"); + uint8_t *errmsg = "Friend request queue is full. Discarding request.\n"; + wprintw(self->window, "%s", errmsg); + add_to_log_buf(errmsg, "", prt->log, true); return; } @@ -460,6 +487,7 @@ ToxWindow new_prompt(void) memset(&ret, 0, sizeof(ret)); ret.active = true; + ret.is_prompt = true; ret.onKey = &prompt_onKey; ret.onDraw = &prompt_onDraw; diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 7466acf..d242d19 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -114,6 +114,7 @@ struct ToxWindow { /* window type identifiers */ bool is_chat; bool is_groupchat; + bool is_prompt; bool alert0; bool alert1; @@ -182,6 +183,7 @@ struct PromptBuf { int hst_pos; int hst_tot; + struct chatlog *log; WINDOW *linewin; }; From e41008bd4e92d7b652051694ddb7f35560584250 Mon Sep 17 00:00:00 2001 From: Sean Qureshi Date: Mon, 3 Mar 2014 07:55:10 -0800 Subject: [PATCH 47/58] Minor fixup --- misc/{DHTservers => DHTnodes} | 0 misc/Makefile.am | 2 +- src/global_commands.c | 2 +- src/main.c | 42 +++++++++++++++++------------------ 4 files changed, 23 insertions(+), 23 deletions(-) rename misc/{DHTservers => DHTnodes} (100%) diff --git a/misc/DHTservers b/misc/DHTnodes similarity index 100% rename from misc/DHTservers rename to misc/DHTnodes diff --git a/misc/Makefile.am b/misc/Makefile.am index c25600a..0113f9d 100644 --- a/misc/Makefile.am +++ b/misc/Makefile.am @@ -1 +1 @@ -dist_pkgdata_DATA = DHTservers +dist_pkgdata_DATA = DHTnodes diff --git a/src/global_commands.c b/src/global_commands.c index f15cc9e..80a2978 100644 --- a/src/global_commands.c +++ b/src/global_commands.c @@ -367,7 +367,7 @@ void cmd_prompt_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*a wprintw(window, " /add : Add friend with optional message\n"); wprintw(window, " /accept : Accept friend request\n"); - wprintw(window, " /connect : Manually connect to a DHT server\n"); + wprintw(window, " /connect : Manually connect to a DHT node\n"); wprintw(window, " /status : Set status with optional note\n"); wprintw(window, " /note : Set a personal note\n"); wprintw(window, " /nick : Set your nickname\n"); diff --git a/src/main.c b/src/main.c index fd4679d..5b71c3a 100644 --- a/src/main.c +++ b/src/main.c @@ -159,15 +159,15 @@ static Tox *init_tox(int ipv4) #define MINLINE 50 /* IP: 7 + port: 5 + key: 38 + spaces: 2 = 70. ! (& e.g. tox.im = 6) */ #define MAXLINE 256 /* Approx max number of chars in a sever line (name + port + key) */ -#define MAXSERVERS 50 -#define SERVERLEN (MAXLINE - TOX_CLIENT_ID_SIZE - 7) +#define MAXNODES 50 +#define NODELEN (MAXLINE - TOX_CLIENT_ID_SIZE - 7) static int linecnt = 0; -static char servers[MAXSERVERS][SERVERLEN]; -static uint16_t ports[MAXSERVERS]; -static uint8_t keys[MAXSERVERS][TOX_CLIENT_ID_SIZE]; +static char nodes[MAXNODES][NODELEN]; +static uint16_t ports[MAXNODES]; +static uint8_t keys[MAXNODES][TOX_CLIENT_ID_SIZE]; -static int serverlist_load(const char *filename) +static int nodelist_load(const char *filename) { FILE *fp = NULL; @@ -180,7 +180,7 @@ static int serverlist_load(const char *filename) return 1; char line[MAXLINE]; - while (fgets(line, sizeof(line), fp) && linecnt < MAXSERVERS) { + while (fgets(line, sizeof(line), fp) && linecnt < MAXNODES) { if (strlen(line) > MINLINE) { char *name = strtok(line, " "); char *port = strtok(NULL, " "); @@ -189,8 +189,8 @@ static int serverlist_load(const char *filename) if (name == NULL || port == NULL || key_ascii == NULL) continue; - strncpy(servers[linecnt], name, SERVERLEN); - servers[linecnt][SERVERLEN - 1] = 0; + strncpy(nodes[linecnt], name, NODELEN); + nodes[linecnt][NODELEN - 1] = 0; ports[linecnt] = htons(atoi(port)); uint8_t *key_binary = hex_string_to_bin(key_ascii); @@ -212,17 +212,17 @@ static int serverlist_load(const char *filename) int init_connection_helper(Tox *m, int line) { - return tox_bootstrap_from_address(m, servers[line], TOX_ENABLE_IPV6_DEFAULT, + return tox_bootstrap_from_address(m, nodes[line], TOX_ENABLE_IPV6_DEFAULT, ports[line], keys[line]); } -/* Connects to a random DHT server listed in the DHTservers file +/* Connects to a random DHT node listed in the DHTnodes file * * return codes: - * 1: failed to open server file - * 2: no line of sufficient length in server file + * 1: failed to open node file + * 2: no line of sufficient length in node file * 3: failed to resolve name to IP - * 4: serverlist file contains no acceptable line + * 4: nodelist file contains no acceptable line */ static bool srvlist_loaded = false; @@ -230,21 +230,21 @@ static bool srvlist_loaded = false; int init_connection(Tox *m) { - if (linecnt > 0) /* already loaded serverlist */ + if (linecnt > 0) /* already loaded nodelist */ return init_connection_helper(m, rand() % linecnt) ? 0 : 3; /* only once: - * - load the serverlist + * - load the nodelist * - connect to "everyone" inside */ if (!srvlist_loaded) { srvlist_loaded = true; - int res = serverlist_load(SRVLIST_FILE); + int res = nodelist_load(SRVLIST_FILE); if (res) { /* Fallback on the provided DHTServers in /usr/share or /usr/local/share, so new starts of toxic will connect to the DHT. */ - res = serverlist_load(PACKAGE_DATADIR "/DHTservers"); + res = nodelist_load(PACKAGE_DATADIR "/DHTnodes"); if (res) return res; @@ -264,7 +264,7 @@ int init_connection(Tox *m) return res; } - /* empty serverlist file */ + /* empty nodelist file */ return 4; } @@ -545,11 +545,11 @@ int main(int argc, char *argv[]) } } - SRVLIST_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("DHTservers") + 1); + SRVLIST_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("DHTnodes") + 1); if (SRVLIST_FILE != NULL) { strcpy(SRVLIST_FILE, user_config_dir); strcat(SRVLIST_FILE, CONFIGDIR); - strcat(SRVLIST_FILE, "DHTservers"); + strcat(SRVLIST_FILE, "DHTnodes"); } else { endwin(); fprintf(stderr, "malloc() failed. Aborting...\n"); From 24b763bce64b97cfb4dc017cffab5e0fba29ba51 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 3 Mar 2014 19:21:52 -0500 Subject: [PATCH 48/58] simplify logging --- src/chat.c | 8 +++--- src/groupchat.c | 10 +++---- src/log.c | 64 ++++++++++++++++++--------------------------- src/log.h | 14 +++++----- src/prompt.c | 16 ++++++------ src/toxic_windows.h | 7 +++-- 6 files changed, 52 insertions(+), 67 deletions(-) diff --git a/src/chat.c b/src/chat.c index 5c8ef1b..f459171 100644 --- a/src/chat.c +++ b/src/chat.c @@ -115,7 +115,7 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1 } else wprintw(ctx->history, "%s\n", msg); - add_to_log_buf(msg, nick, ctx->log, false); + write_to_log(msg, nick, ctx->log, false); alert_window(self, WINDOW_ALERT_1, true); } @@ -159,7 +159,7 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin wprintw(ctx->history, "* %s %s\n", nick, action); wattroff(ctx->history, COLOR_PAIR(YELLOW)); - add_to_log_buf(action, nick, ctx->log, true); + write_to_log(action, nick, ctx->log, true); alert_window(self, WINDOW_ALERT_1, true); } @@ -341,7 +341,7 @@ static void send_action(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *acti wprintw(ctx->history, " * Failed to send action\n"); wattroff(ctx->history, COLOR_PAIR(RED)); } else { - add_to_log_buf(action, selfname, ctx->log, true); + write_to_log(action, selfname, ctx->log, true); } } @@ -532,7 +532,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) wprintw(ctx->history, " * Failed to send message.\n"); wattroff(ctx->history, COLOR_PAIR(RED)); } else { - add_to_log_buf(line, selfname, ctx->log, false); + write_to_log(line, selfname, ctx->log, false); } } diff --git a/src/groupchat.c b/src/groupchat.c index e6b3b30..2f30ff9 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -168,7 +168,7 @@ static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, int groupnum, int wprintw(ctx->history, "%s\n", msg); } - add_to_log_buf(msg, nick, ctx->log, false); + write_to_log(msg, nick, ctx->log, false); } static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int peernum, uint8_t *action, @@ -204,7 +204,7 @@ static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int p wprintw(ctx->history, "* %s %s\n", nick, action); wattroff(ctx->history, COLOR_PAIR(YELLOW)); - add_to_log_buf(action, nick, ctx->log, true); + write_to_log(action, nick, ctx->log, true); } /* Puts two copies of peerlist in chat instance */ @@ -282,7 +282,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu wprintw(ctx->history, " %s\n", event); wattroff(ctx->history, COLOR_PAIR(GREEN)); - add_to_log_buf(event, peername, ctx->log, true); + write_to_log(event, peername, ctx->log, true); break; case TOX_CHAT_CHANGE_PEER_DEL: @@ -296,7 +296,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu if (groupchats[self->num].side_pos > 0) --groupchats[self->num].side_pos; - add_to_log_buf(event, oldpeername, ctx->log, true); + write_to_log(event, oldpeername, ctx->log, true); break; case TOX_CHAT_CHANGE_PEER_NAME: @@ -314,7 +314,7 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu uint8_t tmp_event[TOXIC_MAX_NAME_LENGTH + 32]; snprintf(tmp_event, sizeof(tmp_event), "is now known as %s", peername); - add_to_log_buf(tmp_event, oldpeername, ctx->log, true); + write_to_log(tmp_event, oldpeername, ctx->log, true); break; } diff --git a/src/log.c b/src/log.c index 2a83db6..77b76e7 100644 --- a/src/log.c +++ b/src/log.c @@ -28,7 +28,7 @@ #include "toxic_windows.h" #include "misc_tools.h" -/* gets the log path by appending to the config dir the name and a pseudo-unique identity */ +/* Creates/fetches log file by appending to the config dir the name and a pseudo-unique identity */ void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log) { if (!log->log_on) @@ -56,55 +56,36 @@ void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log) if (path_len > MAX_STR_SIZE) { log->log_on = false; + log->file = NULL; return; } - snprintf(log->log_path, MAX_STR_SIZE, "%s%s%s-%s.log", + uint8_t log_path[MAX_STR_SIZE]; + + snprintf(log_path, MAX_STR_SIZE, "%s%s%s-%s.log", user_config_dir, CONFIGDIR, name, ident); - FILE *logfile = fopen(log->log_path, "a"); + log->file = fopen(log_path, "a"); - if (logfile == NULL) { + if (log->file == NULL) { log->log_on = false; return; } - fprintf(logfile, "\n*** NEW SESSION ***\n\n"); - - fclose(logfile); + fprintf(log->file, "\n*** NEW SESSION ***\n\n"); free(user_config_dir); } -/* writes contents from a chatcontext's log buffer to respective log file and resets log pos. - This is triggered when the log buffer is full, but may be forced. */ -void write_to_log(struct chatlog *log) +void write_to_log(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event) { if (!log->log_on) return; - FILE *logfile = fopen(log->log_path, "a"); - - if (logfile == NULL) { + if (log->file == NULL) { log->log_on = false; return; } - int i; - - for (i = 0; i < log->pos; ++i) - fprintf(logfile, "%s", log->log_buf[i]); - - log->pos = 0; - fclose(logfile); -} - -/* Adds line/event to log_buf with timestamp and name. If buf is full, triggers write_to_log. - If event is true, formats line as an event, e.g. * name has gone offline */ -void add_to_log_buf(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event) -{ - if (!log->log_on) - return; - uint8_t name_frmt[TOXIC_MAX_NAME_LENGTH + 3]; if (event) @@ -113,26 +94,33 @@ void add_to_log_buf(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event snprintf(name_frmt, sizeof(name_frmt), "%s:", name); struct tm *tminfo = get_time(); - snprintf(log->log_buf[log->pos], MAX_LOG_LINE_SIZE, "%04d/%02d/%02d [%02d:%02d:%02d] %s %s\n", - tminfo->tm_year + 1900, tminfo->tm_mon + 1, tminfo->tm_mday, - tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec, name_frmt, msg); + fprintf(log->file,"%04d/%02d/%02d [%02d:%02d:%02d] %s %s\n", tminfo->tm_year+1900, + tminfo->tm_mon+1, tminfo->tm_mday, tminfo->tm_hour, tminfo->tm_min, + tminfo->tm_sec, name_frmt, msg); + + uint64_t curtime = (uint64_t) time(NULL); + + if (timed_out(log->lastflush, curtime, LOG_FLUSH_LIMIT)) + fflush(log->file); + + log->lastflush = curtime; - if (++(log->pos) >= MAX_LOG_BUF_LINES) - write_to_log(log); } void log_enable(uint8_t *name, uint8_t *key, struct chatlog *log) { log->log_on = true; - if (!log->log_path[0]) + if (log->file == NULL) init_logging_session(name, key, log); } void log_disable(struct chatlog *log) { - if (log->log_on) { - write_to_log(log); - log->log_on = false; + log->log_on = false; + + if (log->file != NULL) { + fclose(log->file); + log->file = NULL; } } diff --git a/src/log.h b/src/log.h index e4ab01c..71c9748 100644 --- a/src/log.h +++ b/src/log.h @@ -20,16 +20,14 @@ * */ -/* gets the log path by appending to the config dir the name and a pseudo-unique identity */ +/* Creates/fetches log file by appending to the config dir the name and a pseudo-unique identity */ void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log); -/* Adds line/event to log_buf with timestamp and name. If buf is full, triggers write_to_log. - If event is true, formats line as an event, e.g. * name has gone offline */ -void add_to_log_buf(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event); - -/* writes contents from a chatcontext's log buffer to respective log file and resets log pos. - This is triggered automatically when the log buffer is full, but may be forced. */ -void write_to_log(struct chatlog *log); +/* formats/writes line to log file */ +void write_to_log(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event); +/* enables logging for specified log and creates/fetches file if necessary */ void log_enable(uint8_t *name, uint8_t *key, struct chatlog *log); + +/* disables logging for specified log and closes file */ void log_disable(struct chatlog *log); diff --git a/src/prompt.c b/src/prompt.c index 9116123..bda06e0 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -398,26 +398,26 @@ static void prompt_onConnectionChange(ToxWindow *self, Tox *m, int friendnum , u uint8_t *msg; if (status == 1) { - msg = "has come online\n"; + msg = "has come online"; wattron(self->window, COLOR_PAIR(GREEN)); wattron(self->window, A_BOLD); wprintw(self->window, "* %s ", nick); wattroff(self->window, A_BOLD); - wprintw(self->window, "%s", msg); + wprintw(self->window, "%s\n", msg); wattroff(self->window, COLOR_PAIR(GREEN)); - add_to_log_buf(msg, nick, prt->log, true); + write_to_log(msg, nick, prt->log, true); alert_window(self, WINDOW_ALERT_2, false); } else { - msg = "has gone offline\n"; + msg = "has gone offline"; wattron(self->window, COLOR_PAIR(RED)); wattron(self->window, A_BOLD); wprintw(self->window, "* %s ", nick); wattroff(self->window, A_BOLD); - wprintw(self->window, "%s", msg); + wprintw(self->window, "%s\n", msg); wattroff(self->window, COLOR_PAIR(RED)); - add_to_log_buf(msg, nick, prt->log, true); + write_to_log(msg, nick, prt->log, true); } } @@ -434,14 +434,14 @@ static void prompt_onFriendRequest(ToxWindow *self, uint8_t *key, uint8_t *data, uint8_t msg[MAX_STR_SIZE]; snprintf(msg, sizeof(msg), "Friend request with the message '%s'\n", data); wprintw(self->window, "%s", msg); - add_to_log_buf(msg, "", prt->log, true); + write_to_log(msg, "", prt->log, true); int n = add_friend_request(key); if (n == -1) { uint8_t *errmsg = "Friend request queue is full. Discarding request.\n"; wprintw(self->window, "%s", errmsg); - add_to_log_buf(errmsg, "", prt->log, true); + write_to_log(errmsg, "", prt->log, true); return; } diff --git a/src/toxic_windows.h b/src/toxic_windows.h index d242d19..1e01527 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -138,12 +138,11 @@ struct StatusBar { bool is_online; }; -#define MAX_LOG_BUF_LINES 10 /* write log_buf contents to log file after this many lines */ -#define MAX_LOG_LINE_SIZE MAX_STR_SIZE + TOXIC_MAX_NAME_LENGTH + 32 /* extra room for timestamp */ +#define LOG_FLUSH_LIMIT 2 /* limits calls to fflush(logfile) to a max of one per LOG_FLUSH_LIMIT seconds */ struct chatlog { - uint8_t log_path[MAX_STR_SIZE]; - uint8_t log_buf[MAX_LOG_BUF_LINES][MAX_LOG_LINE_SIZE]; + FILE *file; + uint64_t lastflush; int pos; bool log_on; /* specific to current chat window */ }; From 13e67f4ce395dc2c30cf7ffcae74fa455fbeebea Mon Sep 17 00:00:00 2001 From: JFreegman Date: Tue, 4 Mar 2014 03:08:04 -0500 Subject: [PATCH 49/58] Update README.md --- README.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index adec50a..c022e33 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,16 @@ -## Toxic +# Toxic Toxic is an ncurses based instant messaging client for [Tox](http://tox.im) which formerly resided in the [Tox core repository](https://github.com/irungentoo/ProjectTox-Core) and is now available as a standalone program. It looks like [this](http://i.imgur.com/hL7WhVl.png). +## Installation +* Generate the configure script by running the ```autoreconf -i``` command. -To compile, first generate the configure script by running the ```autoreconf -i``` command. +* Execute the configure script with ```./configure``` (you may need to pass it the location of your dependency libraries, i.e.): +```./configure --prefix=/where/to/install --with-libtoxcore-headers=/path/to/ProjectTox-Core/core --with-libtoxcore-libs=/path/to/ProjectTox-Core/build/core --with-libsodium-headers=/path/to/libsodium/include/ --with-libsodium-libs=/path/to/sodiumtest/lib/ ``` -Then execute the configure script with ./configure (you may need to pass it the location of your dependency libraries, i.e.): -``` -./configure --prefix=/where/to/install --with-libtoxcore-headers=/path/to/ProjectTox-Core/core --with-libtoxcore-libs=/path/to/ProjectTox-Core/build/core --with-libsodium-headers=/path/to/libsodium/include/ --with-libsodium-libs=/path/to/sodiumtest/lib/ +* Compile and install the program with ```make && sudo make install``` -``` -*Note:* If your default prefix is /usr/local and you happen to get an error that says "error while loading shared libraries: libtoxcore.so.0: cannot open shared object file: No such file or directory", then you can try running ```sudo ldconfig```. If that doesn't fix it, run: +#### Notes +If your default prefix is /usr/local and you get the error: "error while loading shared libraries: libtoxcore.so.0: cannot open shared object file: No such file or directory", then you can try running ```sudo ldconfig```. If that doesn't fix it, run: ``` echo '/usr/local/lib/' | sudo tee -a /etc/ld.so.conf.d/locallib.conf sudo ldconfig From 87bd0f9b349a0e84aab5f12133f02363b9d1e464 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 4 Mar 2014 03:10:07 -0500 Subject: [PATCH 50/58] old server list wasn't being installed; t in DHTnodes in /usr/local/share/toxic --- src/log.c | 4 ++-- src/main.c | 37 ++++++------------------------------- src/toxic_windows.h | 2 +- 3 files changed, 9 insertions(+), 34 deletions(-) diff --git a/src/log.c b/src/log.c index 77b76e7..40a36c0 100644 --- a/src/log.c +++ b/src/log.c @@ -100,10 +100,10 @@ void write_to_log(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event) uint64_t curtime = (uint64_t) time(NULL); - if (timed_out(log->lastflush, curtime, LOG_FLUSH_LIMIT)) + if (timed_out(log->lastwrite, curtime, LOG_FLUSH_LIMIT)) fflush(log->file); - log->lastflush = curtime; + log->lastwrite = curtime; } diff --git a/src/main.c b/src/main.c index 5b71c3a..849408c 100644 --- a/src/main.c +++ b/src/main.c @@ -64,8 +64,6 @@ /* Export for use in Callbacks */ char *DATA_FILE = NULL; -char *SRVLIST_FILE = NULL; - ToxWindow *prompt = NULL; static int f_loadfromfile; /* 1 if we want to load from/save the data file, 0 otherwise */ @@ -167,14 +165,12 @@ static char nodes[MAXNODES][NODELEN]; static uint16_t ports[MAXNODES]; static uint8_t keys[MAXNODES][TOX_CLIENT_ID_SIZE]; -static int nodelist_load(const char *filename) +static int nodelist_load(char *filename) { - FILE *fp = NULL; - if (!filename) return 1; - fp = fopen(filename, "r"); + FILE *fp = fopen(filename, "r"); if (fp == NULL) return 1; @@ -239,19 +235,10 @@ int init_connection(Tox *m) */ if (!srvlist_loaded) { srvlist_loaded = true; - int res = nodelist_load(SRVLIST_FILE); + int res = nodelist_load(PACKAGE_DATADIR "/DHTnodes"); - if (res) { - /* Fallback on the provided DHTServers in /usr/share or /usr/local/share, - so new starts of toxic will connect to the DHT. */ - res = nodelist_load(PACKAGE_DATADIR "/DHTnodes"); - - if (res) - return res; - } - - if (!linecnt) - return 2; + if (linecnt < 1) + return res; res = 3; int i; @@ -477,7 +464,6 @@ void exit_toxic(Tox *m) kill_all_windows(); free(DATA_FILE); - free(SRVLIST_FILE); free(prompt->stb); log_disable(prompt->promptbuf->log); free(prompt->promptbuf->log); @@ -536,7 +522,7 @@ int main(int argc, char *argv[]) if (DATA_FILE != NULL) { strcpy(DATA_FILE, user_config_dir); strcat(DATA_FILE, CONFIGDIR); - strcat(DATA_FILE, "data"); + strcat(DATA_FILE, "data"); } else { endwin(); fprintf(stderr, "malloc() failed. Aborting...\n"); @@ -545,17 +531,6 @@ int main(int argc, char *argv[]) } } - SRVLIST_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("DHTnodes") + 1); - if (SRVLIST_FILE != NULL) { - strcpy(SRVLIST_FILE, user_config_dir); - strcat(SRVLIST_FILE, CONFIGDIR); - strcat(SRVLIST_FILE, "DHTnodes"); - } else { - endwin(); - fprintf(stderr, "malloc() failed. Aborting...\n"); - exit(EXIT_FAILURE); - } - free(user_config_dir); init_term(); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 1e01527..9f87880 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -142,7 +142,7 @@ struct StatusBar { struct chatlog { FILE *file; - uint64_t lastflush; + uint64_t lastwrite; int pos; bool log_on; /* specific to current chat window */ }; From 90393f1dba99ba31d353967e34462f2f3af1928f Mon Sep 17 00:00:00 2001 From: JFreegman Date: Tue, 4 Mar 2014 03:14:25 -0500 Subject: [PATCH 51/58] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c022e33..3318693 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Toxic is an ncurses based instant messaging client for [Tox](http://tox.im) whic * Generate the configure script by running the ```autoreconf -i``` command. * Execute the configure script with ```./configure``` (you may need to pass it the location of your dependency libraries, i.e.): -```./configure --prefix=/where/to/install --with-libtoxcore-headers=/path/to/ProjectTox-Core/core --with-libtoxcore-libs=/path/to/ProjectTox-Core/build/core --with-libsodium-headers=/path/to/libsodium/include/ --with-libsodium-libs=/path/to/sodiumtest/lib/ ``` +```./configure --prefix=/where/to/install --with-libtoxcore-headers=/path/to/ProjectTox-Core/toxcore --with-libtoxcore-libs=/path/to/ProjectTox-Core/build/toxcore --with-libsodium-headers=/path/to/libsodium/include/ --with-libsodium-libs=/path/to/sodiumtest/lib/ ``` * Compile and install the program with ```make && sudo make install``` From 41292c1ded2366fd290afb4ee546aacc33229d55 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 4 Mar 2014 03:10:07 -0500 Subject: [PATCH 52/58] old server list wasn't being installed; default to DHTnodes in /usr/local/share/toxic --- src/log.c | 4 ++-- src/main.c | 37 ++++++------------------------------- src/toxic_windows.h | 2 +- 3 files changed, 9 insertions(+), 34 deletions(-) diff --git a/src/log.c b/src/log.c index 77b76e7..40a36c0 100644 --- a/src/log.c +++ b/src/log.c @@ -100,10 +100,10 @@ void write_to_log(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event) uint64_t curtime = (uint64_t) time(NULL); - if (timed_out(log->lastflush, curtime, LOG_FLUSH_LIMIT)) + if (timed_out(log->lastwrite, curtime, LOG_FLUSH_LIMIT)) fflush(log->file); - log->lastflush = curtime; + log->lastwrite = curtime; } diff --git a/src/main.c b/src/main.c index 5b71c3a..849408c 100644 --- a/src/main.c +++ b/src/main.c @@ -64,8 +64,6 @@ /* Export for use in Callbacks */ char *DATA_FILE = NULL; -char *SRVLIST_FILE = NULL; - ToxWindow *prompt = NULL; static int f_loadfromfile; /* 1 if we want to load from/save the data file, 0 otherwise */ @@ -167,14 +165,12 @@ static char nodes[MAXNODES][NODELEN]; static uint16_t ports[MAXNODES]; static uint8_t keys[MAXNODES][TOX_CLIENT_ID_SIZE]; -static int nodelist_load(const char *filename) +static int nodelist_load(char *filename) { - FILE *fp = NULL; - if (!filename) return 1; - fp = fopen(filename, "r"); + FILE *fp = fopen(filename, "r"); if (fp == NULL) return 1; @@ -239,19 +235,10 @@ int init_connection(Tox *m) */ if (!srvlist_loaded) { srvlist_loaded = true; - int res = nodelist_load(SRVLIST_FILE); + int res = nodelist_load(PACKAGE_DATADIR "/DHTnodes"); - if (res) { - /* Fallback on the provided DHTServers in /usr/share or /usr/local/share, - so new starts of toxic will connect to the DHT. */ - res = nodelist_load(PACKAGE_DATADIR "/DHTnodes"); - - if (res) - return res; - } - - if (!linecnt) - return 2; + if (linecnt < 1) + return res; res = 3; int i; @@ -477,7 +464,6 @@ void exit_toxic(Tox *m) kill_all_windows(); free(DATA_FILE); - free(SRVLIST_FILE); free(prompt->stb); log_disable(prompt->promptbuf->log); free(prompt->promptbuf->log); @@ -536,7 +522,7 @@ int main(int argc, char *argv[]) if (DATA_FILE != NULL) { strcpy(DATA_FILE, user_config_dir); strcat(DATA_FILE, CONFIGDIR); - strcat(DATA_FILE, "data"); + strcat(DATA_FILE, "data"); } else { endwin(); fprintf(stderr, "malloc() failed. Aborting...\n"); @@ -545,17 +531,6 @@ int main(int argc, char *argv[]) } } - SRVLIST_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("DHTnodes") + 1); - if (SRVLIST_FILE != NULL) { - strcpy(SRVLIST_FILE, user_config_dir); - strcat(SRVLIST_FILE, CONFIGDIR); - strcat(SRVLIST_FILE, "DHTnodes"); - } else { - endwin(); - fprintf(stderr, "malloc() failed. Aborting...\n"); - exit(EXIT_FAILURE); - } - free(user_config_dir); init_term(); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 1e01527..9f87880 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -142,7 +142,7 @@ struct StatusBar { struct chatlog { FILE *file; - uint64_t lastflush; + uint64_t lastwrite; int pos; bool log_on; /* specific to current chat window */ }; From 2f473300cd806af5cbe4939fe32c4faefba929e6 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 5 Mar 2014 06:01:10 +0000 Subject: [PATCH 53/58] Fixing fallback from IPv6 to IPv4 --- src/main.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index 849408c..a8987fc 100644 --- a/src/main.c +++ b/src/main.c @@ -117,8 +117,10 @@ static Tox *init_tox(int ipv4) int ipv6 = !ipv4; Tox *m = tox_new(ipv6); - if (TOX_ENABLE_IPV6_DEFAULT && m == NULL) { - fprintf(stderr, "IPv6 didn't initialize, trying IPv4 only\n"); + // TOX_ENABLE_IPV6_DEFAULT is always 1. + // Fuck checking it, this *should* be doing ipv4 fallback, no? + if (ipv6 && m == NULL) { + fprintf(stderr, "IPv6 didn't initialize, trying IPv4\n"); m = tox_new(0); } From d1153f96caf42572e357959eb9e9742e7aff0548 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 5 Mar 2014 03:31:12 -0500 Subject: [PATCH 54/58] small refactor for incoming file transfers --- src/chat.c | 37 +++++++++++++++++-------------------- src/chat_commands.c | 12 ++++++++++-- src/log.c | 8 +++----- src/toxic_windows.h | 1 + 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/chat.c b/src/chat.c index f459171..c23d0ff 100644 --- a/src/chat.c +++ b/src/chat.c @@ -223,7 +223,7 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t fil strcat(filename, d); filename[len + strlen(d)] = '\0'; - if (count > 999999) { + if (count > 999) { wprintw(ctx->history, "Error saving file to disk.\n"); return; } @@ -237,6 +237,15 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t fil alert_window(self, WINDOW_ALERT_2, true); } +static void close_file_receiver(num, filenum) +{ + friends[num].file_receiver.pending[filenum] = false; + FILE *file = friends[num].file_receiver.files[filenum]; + + if (file != NULL) + fclose(file); +} + static void chat_onFileControl(ToxWindow *self, Tox *m, int num, uint8_t receive_send, uint8_t filenum, uint8_t control_type, uint8_t *data, uint16_t length) { @@ -245,6 +254,7 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int num, uint8_t receive ChatContext *ctx = self->chatwin; uint8_t *filename; + bool close_in_file = false; if (receive_send == 0) filename = friends[num].file_receiver.filenames[filenum]; @@ -255,20 +265,20 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int num, uint8_t receive case TOX_FILECONTROL_ACCEPT: wprintw(ctx->history, "File transfer for '%s' accepted.\n", filename); break; - case TOX_FILECONTROL_PAUSE: - // wprintw(ctx->history, "File transfer for '%s' paused.\n", filename); - break; + // case TOX_FILECONTROL_PAUSE: + // wprintw(ctx->history, "File transfer for '%s' paused.\n", filename); + // break; case TOX_FILECONTROL_KILL: wprintw(ctx->history, "File transfer for '%s' failed.\n", filename); if (receive_send == 0) - friends[num].file_receiver.pending[filenum] = false; + close_file_receiver(num, filenum); else close_file_sender(filenum); - break; case TOX_FILECONTROL_FINISHED: wprintw(ctx->history, "File transfer for '%s' complete.\n", filename); + close_file_receiver(num, filenum); break; } @@ -283,25 +293,12 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int num, uint8_t filenum, u ChatContext *ctx = self->chatwin; - uint8_t *filename = friends[num].file_receiver.filenames[filenum]; - FILE *file_to_save = fopen(filename, "a"); - - if (file_to_save == NULL) { - wattron(ctx->history, COLOR_PAIR(RED)); - wprintw(ctx->history, "* Error writing to file.\n"); - wattroff(ctx->history, COLOR_PAIR(RED)); - tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0); - return; - } - - if (fwrite(data, length, 1, file_to_save) != 1) { + if (fwrite(data, length, 1, friends[num].file_receiver.files[filenum]) != 1) { wattron(ctx->history, COLOR_PAIR(RED)); wprintw(ctx->history, "* Error writing to file.\n"); wattroff(ctx->history, COLOR_PAIR(RED)); tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0); } - - fclose(file_to_save); } static void chat_onGroupInvite(ToxWindow *self, Tox *m, int friendnumber, uint8_t *group_pub_key) diff --git a/src/chat_commands.c b/src/chat_commands.c index 18d8646..f69e081 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -139,10 +139,18 @@ void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv uint8_t *filename = friends[self->num].file_receiver.filenames[filenum]; - if (tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_ACCEPT, 0, 0) == 0) + if (tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_ACCEPT, 0, 0) == 0) { wprintw(window, "Accepted file transfer %u. Saving file as: '%s'\n", filenum, filename); - else + + if ((friends[self->num].file_receiver.files[filenum] = fopen(filename, "a")) == NULL) { + wattron(window, COLOR_PAIR(RED)); + wprintw(window, "* Error writing to file.\n"); + wattroff(window, COLOR_PAIR(RED)); + tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0); + } + } else { wprintw(window, "File transfer failed.\n"); + } friends[self->num].file_receiver.pending[filenum] = false; } diff --git a/src/log.c b/src/log.c index 40a36c0..0b41b43 100644 --- a/src/log.c +++ b/src/log.c @@ -56,7 +56,6 @@ void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log) if (path_len > MAX_STR_SIZE) { log->log_on = false; - log->file = NULL; return; } @@ -100,11 +99,10 @@ void write_to_log(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event) uint64_t curtime = (uint64_t) time(NULL); - if (timed_out(log->lastwrite, curtime, LOG_FLUSH_LIMIT)) + if (timed_out(log->lastwrite, curtime, LOG_FLUSH_LIMIT)) { fflush(log->file); - - log->lastwrite = curtime; - + log->lastwrite = curtime; + } } void log_enable(uint8_t *name, uint8_t *key, struct chatlog *log) diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 9f87880..23b36dc 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -206,6 +206,7 @@ typedef struct { struct FileReceiver { uint8_t filenames[MAX_FILES][MAX_STR_SIZE]; + FILE *files[MAX_FILES]; bool pending[MAX_FILES]; }; From 675c8fa89f3e1f62614ecd944ff00c4aecea7545 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 5 Mar 2014 03:37:07 -0500 Subject: [PATCH 55/58] fix --- src/chat.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/chat.c b/src/chat.c index c23d0ff..c0b45a8 100644 --- a/src/chat.c +++ b/src/chat.c @@ -237,7 +237,7 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t fil alert_window(self, WINDOW_ALERT_2, true); } -static void close_file_receiver(num, filenum) +static void close_file_receiver(int num, int filenum) { friends[num].file_receiver.pending[filenum] = false; FILE *file = friends[num].file_receiver.files[filenum]; @@ -254,7 +254,6 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int num, uint8_t receive ChatContext *ctx = self->chatwin; uint8_t *filename; - bool close_in_file = false; if (receive_send == 0) filename = friends[num].file_receiver.filenames[filenum]; From 2fcfa954aba7e5f7ac160c4a2fc9251b6badbbb0 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 5 Mar 2014 05:06:21 -0500 Subject: [PATCH 56/58] move file sender stuff to its own files --- build/Makefile.am | 4 +- src/chat.c | 8 ++-- src/file_senders.c | 109 ++++++++++++++++++++++++++++++++++++++++++++ src/file_senders.h | 26 +++++++++++ src/main.c | 80 +------------------------------- src/toxic_windows.h | 2 +- 6 files changed, 145 insertions(+), 84 deletions(-) create mode 100644 src/file_senders.c create mode 100644 src/file_senders.h diff --git a/build/Makefile.am b/build/Makefile.am index d38b8f5..2c889ca 100644 --- a/build/Makefile.am +++ b/build/Makefile.am @@ -27,7 +27,9 @@ toxic_SOURCES = $(top_srcdir)/src/main.c \ $(top_srcdir)/src/toxic_strings.c \ $(top_srcdir)/src/toxic_strings.h \ $(top_srcdir)/src/log.c \ - $(top_srcdir)/src/log.h + $(top_srcdir)/src/log.h \ + $(top_srcdir)/src/file_senders.c \ + $(top_srcdir)/src/file_senders.h toxic_CFLAGS = -I$(top_srcdir) \ $(NCURSES_CFLAGS) \ diff --git a/src/chat.c b/src/chat.c index c0b45a8..c165f84 100644 --- a/src/chat.c +++ b/src/chat.c @@ -237,7 +237,7 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t fil alert_window(self, WINDOW_ALERT_2, true); } -static void close_file_receiver(int num, int filenum) +static void chat_close_file_receiver(int num, uint8_t filenum) { friends[num].file_receiver.pending[filenum] = false; FILE *file = friends[num].file_receiver.files[filenum]; @@ -271,13 +271,13 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int num, uint8_t receive wprintw(ctx->history, "File transfer for '%s' failed.\n", filename); if (receive_send == 0) - close_file_receiver(num, filenum); + chat_close_file_receiver(num, filenum); else - close_file_sender(filenum); + chat_close_file_receiver(num, filenum); break; case TOX_FILECONTROL_FINISHED: wprintw(ctx->history, "File transfer for '%s' complete.\n", filename); - close_file_receiver(num, filenum); + chat_close_file_receiver(num, filenum); break; } diff --git a/src/file_senders.c b/src/file_senders.c new file mode 100644 index 0000000..d643bf4 --- /dev/null +++ b/src/file_senders.c @@ -0,0 +1,109 @@ +/* file_senders.c + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * + */ + +#include +#include +#include + +#include "toxic_windows.h" + +FileSender file_senders[MAX_FILES]; +uint8_t max_file_senders_index; + +static void close_file_sender(int i) +{ + fclose(file_senders[i].file); + memset(&file_senders[i], 0, sizeof(FileSender)); + + int j; + + for (j = max_file_senders_index; j > 0; --j) { + if (file_senders[j-1].active) + break; + } + + max_file_senders_index = j; +} + +/* Should only be called on exit */ +void close_all_file_senders(void) +{ + int i; + + for (i = 0; i < max_file_senders_index; ++i) { + if (file_senders[i].active) + fclose(file_senders[i].file); + } +} + +void do_file_senders(Tox *m) +{ + int i; + + for (i = 0; i < max_file_senders_index; ++i) { + if (!file_senders[i].active) + continue; + + uint8_t *pathname = file_senders[i].pathname; + uint8_t filenum = file_senders[i].filenum; + int friendnum = file_senders[i].friendnum; + FILE *fp = file_senders[i].file; + uint64_t current_time = (uint64_t) time(NULL); + + /* If file transfer has timed out kill transfer and send kill control */ + if (timed_out(file_senders[i].timestamp, current_time, TIMEOUT_FILESENDER)) { + ChatContext *ctx = file_senders[i].toxwin->chatwin; + + if (ctx != NULL) { + wprintw(ctx->history, "File transfer for '%s' timed out.\n", pathname); + alert_window(file_senders[i].toxwin, WINDOW_ALERT_2, true); + } + + tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_KILL, 0, 0); + close_file_sender(i); + continue; + } + + while (true) { + if (tox_file_send_data(m, friendnum, filenum, file_senders[i].nextpiece, + file_senders[i].piecelen) == -1) + break; + + file_senders[i].timestamp = current_time; + file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1, + tox_file_data_size(m, friendnum), fp); + + if (file_senders[i].piecelen == 0) { + ChatContext *ctx = file_senders[i].toxwin->chatwin; + + if (ctx != NULL) { + wprintw(ctx->history, "File '%s' successfuly sent.\n", pathname); + alert_window(file_senders[i].toxwin, WINDOW_ALERT_2, true); + } + + tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_FINISHED, 0, 0); + close_file_sender(i); + break; + } + } + } +} diff --git a/src/file_senders.h b/src/file_senders.h new file mode 100644 index 0000000..9ec1cdc --- /dev/null +++ b/src/file_senders.h @@ -0,0 +1,26 @@ +/* file_senders.h + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * + */ + +/* Should only be called on exit */ +void close_all_file_senders(void); + +void do_file_senders(Tox *m); diff --git a/src/main.c b/src/main.c index 849408c..4bbd615 100644 --- a/src/main.c +++ b/src/main.c @@ -57,6 +57,7 @@ #include "friendlist.h" #include "prompt.h" #include "misc_tools.h" +#include "file_senders.h" #ifndef PACKAGE_DATADIR #define PACKAGE_DATADIR "." @@ -68,9 +69,6 @@ ToxWindow *prompt = NULL; static int f_loadfromfile; /* 1 if we want to load from/save the data file, 0 otherwise */ -FileSender file_senders[MAX_FILES]; -uint8_t max_file_senders_index; - void on_window_resize(int sig) { endwin(); @@ -383,85 +381,11 @@ static void load_data(Tox *m, char *path) } } -void close_file_sender(int i) -{ - fclose(file_senders[i].file); - memset(&file_senders[i], 0, sizeof(FileSender)); - - int j; - - for (j = max_file_senders_index; j > 0; --j) { - if (file_senders[j-1].active) - break; - } - - max_file_senders_index = j; -} - -static void do_file_senders(Tox *m) -{ - int i; - - for (i = 0; i < max_file_senders_index; ++i) { - if (!file_senders[i].active) - continue; - - uint8_t *pathname = file_senders[i].pathname; - uint8_t filenum = file_senders[i].filenum; - int friendnum = file_senders[i].friendnum; - FILE *fp = file_senders[i].file; - uint64_t current_time = (uint64_t) time(NULL); - - /* If file transfer has timed out kill transfer and send kill control */ - if (timed_out(file_senders[i].timestamp, current_time, TIMEOUT_FILESENDER)) { - ChatContext *ctx = file_senders[i].toxwin->chatwin; - - if (ctx != NULL) { - wprintw(ctx->history, "File transfer for '%s' timed out.\n", pathname); - alert_window(file_senders[i].toxwin, WINDOW_ALERT_2, true); - } - - tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_KILL, 0, 0); - close_file_sender(i); - continue; - } - - while (true) { - if (tox_file_send_data(m, friendnum, filenum, file_senders[i].nextpiece, - file_senders[i].piecelen) == -1) - break; - - file_senders[i].timestamp = current_time; - file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1, - tox_file_data_size(m, friendnum), fp); - - if (file_senders[i].piecelen == 0) { - ChatContext *ctx = file_senders[i].toxwin->chatwin; - - if (ctx != NULL) { - wprintw(ctx->history, "File '%s' successfuly sent.\n", pathname); - alert_window(file_senders[i].toxwin, WINDOW_ALERT_2, true); - } - - tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_FINISHED, 0, 0); - close_file_sender(i); - break; - } - } - } -} - void exit_toxic(Tox *m) { store_data(m, DATA_FILE); - int i; - - for (i = 0; i < max_file_senders_index; ++i) { - if (file_senders[i].active) - fclose(file_senders[i].file); - } - + close_all_file_senders(); kill_all_windows(); free(DATA_FILE); free(prompt->stb); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 23b36dc..1e10a7b 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -236,6 +236,6 @@ void del_window(ToxWindow *w); void set_active_window(int ch); int num_active_windows(void); -/* closes all chat and groupchat windows (should only be called on shutdown) */ +/* cleans up all chat and groupchat windows (should only be called on shutdown) */ void kill_all_windows(void); #endif From 4b8de0d16d863963f99c2f351d5adbdc69dfc2a7 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 5 Mar 2014 08:01:12 -0500 Subject: [PATCH 57/58] speed up friendlist loading on startup --- src/friendlist.c | 2 +- src/friendlist.h | 2 ++ src/main.c | 19 +++++++++++-------- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/friendlist.c b/src/friendlist.c index d55c173..13059be 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -133,7 +133,7 @@ static void friendlist_onStatusMessageChange(ToxWindow *self, int num, uint8_t * friends[num].statusmsg_len = len; } -static void friendlist_onFriendAdded(ToxWindow *self, Tox *m, int num, bool sort) +void friendlist_onFriendAdded(ToxWindow *self, Tox *m, int num, bool sort) { if (max_friends_index < 0 || max_friends_index >= MAX_FRIENDS_NUM) return; diff --git a/src/friendlist.h b/src/friendlist.h index 09ffe62..23a3565 100644 --- a/src/friendlist.h +++ b/src/friendlist.h @@ -46,6 +46,8 @@ ToxWindow new_friendlist(void); void disable_chatwin(int f_num); int get_friendnum(uint8_t *name); +void friendlist_onFriendAdded(ToxWindow *self, Tox *m, int num, bool sort); + /* sorts friendlist_index first by connection status then alphabetically */ void sort_friendlist_index(Tox *m); diff --git a/src/main.c b/src/main.c index 4bbd615..4f4595c 100644 --- a/src/main.c +++ b/src/main.c @@ -283,6 +283,15 @@ static void do_connection(Tox *m, ToxWindow *prompt) } } +static void load_friendlist(Tox *m) +{ + int i; + uint32_t numfriends = tox_count_friendlist(m); + + for (i = 0; i < numfriends; ++i) + friendlist_onFriendAdded(NULL, m, i, false); +} + /* * Store Messenger to given location * Return 0 stored successfully @@ -361,12 +370,7 @@ static void load_data(Tox *m, char *path) } tox_load(m, buf, len); - - uint32_t i = 0; - uint8_t name[TOX_MAX_NAME_LENGTH]; - - while (tox_get_name(m, i, name) != -1) - on_friendadded(m, i++, false); + load_friendlist(m); free(buf); fclose(fd); @@ -384,12 +388,11 @@ static void load_data(Tox *m, char *path) void exit_toxic(Tox *m) { store_data(m, DATA_FILE); - close_all_file_senders(); kill_all_windows(); + log_disable(prompt->promptbuf->log); free(DATA_FILE); free(prompt->stb); - log_disable(prompt->promptbuf->log); free(prompt->promptbuf->log); free(prompt->promptbuf); tox_kill(m); From 2ae478d546c2089f07bd4cfdcfad19ed8b8cd24f Mon Sep 17 00:00:00 2001 From: "Michael R. Torres" Date: Wed, 5 Mar 2014 07:17:57 -0800 Subject: [PATCH 58/58] Fix fall-back from IPv6 to IPv4 Professionalism edits --- src/main.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index a8987fc..73defee 100644 --- a/src/main.c +++ b/src/main.c @@ -117,8 +117,10 @@ static Tox *init_tox(int ipv4) int ipv6 = !ipv4; Tox *m = tox_new(ipv6); - // TOX_ENABLE_IPV6_DEFAULT is always 1. - // Fuck checking it, this *should* be doing ipv4 fallback, no? + /* + * TOX_ENABLE_IPV6_DEFAULT is always 1. + * Checking it is redundant, this *should* be doing ipv4 fallback + */ if (ipv6 && m == NULL) { fprintf(stderr, "IPv6 didn't initialize, trying IPv4\n"); m = tox_new(0);