From 8ca2d8ac930a382108556daa4df7d5bda5784a47 Mon Sep 17 00:00:00 2001 From: jfreegman Date: Mon, 4 May 2020 16:08:42 -0400 Subject: [PATCH 1/3] Add option for toxcore logging in stderr --- doc/toxic.1 | 5 +++++ doc/toxic.1.asc | 3 +++ src/toxic.c | 30 ++++++++++++++++++++++++++---- src/windows.h | 1 + 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/doc/toxic.1 b/doc/toxic.1 index fad14a931..caa3bab9a 100644 --- a/doc/toxic.1 +++ b/doc/toxic.1 @@ -78,6 +78,11 @@ instead of Show help message .RE .PP +\-l, \-\-logging +.RS 4 +Enable toxcore logging in stderr +.RE +.PP \-n, \-\-nodes nodes\-file .RS 4 Use specified diff --git a/doc/toxic.1.asc b/doc/toxic.1.asc index dcfc01a0f..5abd09c24 100644 --- a/doc/toxic.1.asc +++ b/doc/toxic.1.asc @@ -40,6 +40,9 @@ OPTIONS -h, --help:: Show help message +-l, --logging:: + Enable toxcore logging in stderr + -n, --nodes nodes-file:: Use specified 'nodes-file' for DHT bootstrap nodes instead of '~/.config/tox/DHTnodes.json' diff --git a/src/toxic.c b/src/toxic.c index b95ea118a..3ba4cb56c 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -219,6 +219,16 @@ void exit_toxic_err(const char *errmsg, int errcode) exit(EXIT_FAILURE); } +void cb_toxcore_logger(Tox *m, TOX_LOG_LEVEL level, const char *file, uint32_t line, const char *func, + const char *message, void *user_data) +{ + UNUSED_VAR(user_data); + UNUSED_VAR(file); + UNUSED_VAR(m); + + fprintf(stderr, "[%d] %u:%s() - %s\n", level, line, func, message); +} + static void init_term(void) { #if HAVE_WIDECHAR @@ -645,6 +655,10 @@ static void init_tox_options(struct Tox_Options *tox_opts) tox_options_set_proxy_type(tox_opts, arg_opts.proxy_type); tox_options_set_tcp_port(tox_opts, arg_opts.tcp_port); + if (arg_opts.logging) { + tox_options_set_log_callback(tox_opts, cb_toxcore_logger); + } + if (!tox_options_get_ipv6_enabled(tox_opts)) { queue_init_message("Forcing IPv4 connection"); } @@ -942,6 +956,7 @@ static void print_usage(void) fprintf(stderr, " -e, --encrypt-data Encrypt an unencrypted data file\n"); fprintf(stderr, " -f, --file Use specified data file\n"); fprintf(stderr, " -h, --help Show this message and exit\n"); + fprintf(stderr, " -l, --logging Enable toxcore logging in stderr\n"); fprintf(stderr, " -n, --nodes Use specified DHTnodes file\n"); fprintf(stderr, " -o, --noconnect Do not connect to the DHT network\n"); fprintf(stderr, " -p, --SOCKS5-proxy Use SOCKS5 proxy: Requires [IP] [port]\n"); @@ -978,6 +993,7 @@ static void parse_args(int argc, char *argv[]) {"default-locale", no_argument, 0, 'd'}, {"config", required_argument, 0, 'c'}, {"encrypt-data", no_argument, 0, 'e'}, + {"logging", no_argument, 0, 'l'}, {"nodes", required_argument, 0, 'n'}, {"help", no_argument, 0, 'h'}, {"noconnect", no_argument, 0, 'o'}, @@ -991,7 +1007,7 @@ static void parse_args(int argc, char *argv[]) {NULL, no_argument, NULL, 0}, }; - const char *opts_str = "4bdehotuxvc:f:n:r:p:P:T:"; + const char *opts_str = "4bdehlotuxvc:f:n:r:p:P:T:"; int opt, indexptr; long int port = 0; @@ -1055,6 +1071,12 @@ static void parse_args(int argc, char *argv[]) break; + case 'l': + arg_opts.debug = true; + arg_opts.logging = true; + queue_init_message("Toxcore logging enabled in stderr"); + break; + case 'n': snprintf(arg_opts.nodes_path, sizeof(arg_opts.nodes_path), "%s", optarg); break; @@ -1248,6 +1270,9 @@ void DnD_callback(const char *asdv, DropType dt) int main(int argc, char **argv) { + /* Make sure all written files are read/writeable only by the current user. */ + umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + parse_args(argc, argv); /* Use the -b flag to enable stderr */ @@ -1263,9 +1288,6 @@ int main(int argc, char **argv) queue_init_message("Warning: Using --unencrypt-data and --encrypt-data simultaneously has no effect"); } - /* Make sure all written files are read/writeable only by the current user. */ - umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); - init_default_data_files(); bool datafile_exists = file_exists(DATA_FILE); diff --git a/src/windows.h b/src/windows.h index 1c3074ad2..a4951e576 100644 --- a/src/windows.h +++ b/src/windows.h @@ -89,6 +89,7 @@ struct arg_opts { bool no_connect; bool encrypt_data; bool unencrypt_data; + bool logging; char nameserver_path[MAX_STR_SIZE]; char config_path[MAX_STR_SIZE]; From d26dd371c0cead239fee189bd46e31aab5a14f91 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Mon, 4 May 2020 20:12:07 +0000 Subject: [PATCH 2/3] Restyled by astyle --- src/toxic.c | 206 ++++++++++++++++++++++++++-------------------------- 1 file changed, 103 insertions(+), 103 deletions(-) diff --git a/src/toxic.c b/src/toxic.c index 3ba4cb56c..c364a548e 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -1013,148 +1013,148 @@ static void parse_args(int argc, char *argv[]) while ((opt = getopt_long(argc, argv, opts_str, long_opts, &indexptr)) != -1) { switch (opt) { - case '4': - arg_opts.use_ipv4 = 1; - break; + case '4': + arg_opts.use_ipv4 = 1; + break; - case 'b': - arg_opts.debug = 1; - queue_init_message("stderr enabled"); - break; + case 'b': + arg_opts.debug = 1; + queue_init_message("stderr enabled"); + break; - case 'c': - snprintf(arg_opts.config_path, sizeof(arg_opts.config_path), "%s", optarg); + case 'c': + snprintf(arg_opts.config_path, sizeof(arg_opts.config_path), "%s", optarg); - if (!file_exists(arg_opts.config_path)) { - queue_init_message("Config file not found"); - } + if (!file_exists(arg_opts.config_path)) { + queue_init_message("Config file not found"); + } - break; + break; - case 'd': - arg_opts.default_locale = 1; - queue_init_message("Using default POSIX locale"); - break; + case 'd': + arg_opts.default_locale = 1; + queue_init_message("Using default POSIX locale"); + break; - case 'e': - arg_opts.encrypt_data = 1; - break; + case 'e': + arg_opts.encrypt_data = 1; + break; - case 'f': - arg_opts.use_custom_data = 1; + case 'f': + arg_opts.use_custom_data = 1; - if (DATA_FILE) { - free(DATA_FILE); - } + if (DATA_FILE) { + free(DATA_FILE); + } - if (BLOCK_FILE) { - free(BLOCK_FILE); - } + if (BLOCK_FILE) { + free(BLOCK_FILE); + } - DATA_FILE = malloc(strlen(optarg) + 1); - strcpy(DATA_FILE, optarg); + DATA_FILE = malloc(strlen(optarg) + 1); + strcpy(DATA_FILE, optarg); - if (DATA_FILE == NULL) { - exit_toxic_err("failed in parse_args", FATALERR_MEMORY); - } + if (DATA_FILE == NULL) { + exit_toxic_err("failed in parse_args", FATALERR_MEMORY); + } - BLOCK_FILE = malloc(strlen(optarg) + strlen("-blocklist") + 1); + BLOCK_FILE = malloc(strlen(optarg) + strlen("-blocklist") + 1); - if (BLOCK_FILE == NULL) { - exit_toxic_err("failed in parse_args", FATALERR_MEMORY); - } + if (BLOCK_FILE == NULL) { + exit_toxic_err("failed in parse_args", FATALERR_MEMORY); + } - strcpy(BLOCK_FILE, optarg); - strcat(BLOCK_FILE, "-blocklist"); + strcpy(BLOCK_FILE, optarg); + strcat(BLOCK_FILE, "-blocklist"); - queue_init_message("Using '%s' data file", DATA_FILE); + queue_init_message("Using '%s' data file", DATA_FILE); - break; + break; - case 'l': - arg_opts.debug = true; - arg_opts.logging = true; - queue_init_message("Toxcore logging enabled in stderr"); - break; + case 'l': + arg_opts.debug = true; + arg_opts.logging = true; + queue_init_message("Toxcore logging enabled in stderr"); + break; - case 'n': - snprintf(arg_opts.nodes_path, sizeof(arg_opts.nodes_path), "%s", optarg); - break; + case 'n': + snprintf(arg_opts.nodes_path, sizeof(arg_opts.nodes_path), "%s", optarg); + break; - case 'o': - arg_opts.no_connect = 1; - queue_init_message("DHT disabled"); - break; + case 'o': + arg_opts.no_connect = 1; + queue_init_message("DHT disabled"); + break; - case 'p': - arg_opts.proxy_type = TOX_PROXY_TYPE_SOCKS5; - snprintf(arg_opts.proxy_address, sizeof(arg_opts.proxy_address), "%s", optarg); + case 'p': + arg_opts.proxy_type = TOX_PROXY_TYPE_SOCKS5; + snprintf(arg_opts.proxy_address, sizeof(arg_opts.proxy_address), "%s", optarg); - if (++optind > argc || argv[optind - 1][0] == '-') { - exit_toxic_err("Proxy error", FATALERR_PROXY); - } + if (++optind > argc || argv[optind - 1][0] == '-') { + exit_toxic_err("Proxy error", FATALERR_PROXY); + } - port = strtol(argv[optind - 1], NULL, 10); + port = strtol(argv[optind - 1], NULL, 10); - if (port <= 0 || port > MAX_PORT_RANGE) { - exit_toxic_err("Proxy error", FATALERR_PROXY); - } + if (port <= 0 || port > MAX_PORT_RANGE) { + exit_toxic_err("Proxy error", FATALERR_PROXY); + } - arg_opts.proxy_port = port; - break; + arg_opts.proxy_port = port; + break; - case 'P': - arg_opts.proxy_type = TOX_PROXY_TYPE_HTTP; - snprintf(arg_opts.proxy_address, sizeof(arg_opts.proxy_address), "%s", optarg); + case 'P': + arg_opts.proxy_type = TOX_PROXY_TYPE_HTTP; + snprintf(arg_opts.proxy_address, sizeof(arg_opts.proxy_address), "%s", optarg); - if (++optind > argc || argv[optind - 1][0] == '-') { - exit_toxic_err("Proxy error", FATALERR_PROXY); - } + if (++optind > argc || argv[optind - 1][0] == '-') { + exit_toxic_err("Proxy error", FATALERR_PROXY); + } - port = strtol(argv[optind - 1], NULL, 10); + port = strtol(argv[optind - 1], NULL, 10); - if (port <= 0 || port > MAX_PORT_RANGE) { - exit_toxic_err("Proxy error", FATALERR_PROXY); - } + if (port <= 0 || port > MAX_PORT_RANGE) { + exit_toxic_err("Proxy error", FATALERR_PROXY); + } - arg_opts.proxy_port = port; - break; + arg_opts.proxy_port = port; + break; - case 'r': - snprintf(arg_opts.nameserver_path, sizeof(arg_opts.nameserver_path), "%s", optarg); + case 'r': + snprintf(arg_opts.nameserver_path, sizeof(arg_opts.nameserver_path), "%s", optarg); - if (!file_exists(arg_opts.nameserver_path)) { - queue_init_message("nameserver list not found"); - } + if (!file_exists(arg_opts.nameserver_path)) { + queue_init_message("nameserver list not found"); + } - break; + break; - case 't': - arg_opts.force_tcp = 1; - break; + case 't': + arg_opts.force_tcp = 1; + break; - case 'T': - port = strtol(optarg, NULL, 10); + case 'T': + port = strtol(optarg, NULL, 10); - if (port <= 0 || port > MAX_PORT_RANGE) { - port = 14191; - } + if (port <= 0 || port > MAX_PORT_RANGE) { + port = 14191; + } - arg_opts.tcp_port = port; - break; + arg_opts.tcp_port = port; + break; - case 'u': - arg_opts.unencrypt_data = 1; - break; + case 'u': + arg_opts.unencrypt_data = 1; + break; - case 'v': - print_version(); - exit(EXIT_SUCCESS); + case 'v': + print_version(); + exit(EXIT_SUCCESS); - case 'h': - default: - print_usage(); - exit(EXIT_SUCCESS); + case 'h': + default: + print_usage(); + exit(EXIT_SUCCESS); } } } From dc3288308150224a73ff07f5285c19fa36749e69 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Mon, 4 May 2020 20:12:08 +0000 Subject: [PATCH 3/3] Restyled by clang-format --- src/toxic.c | 2007 +++++++++++++++++++++++++------------------------ src/windows.h | 281 +++---- 2 files changed, 1162 insertions(+), 1126 deletions(-) diff --git a/src/toxic.c b/src/toxic.c index c364a548e..cc18474b0 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -20,50 +20,50 @@ * */ +#include #include #include -#include -#include +#include +#include +#include +#include +#include +#include +#include #include #include -#include -#include -#include +#include +#include #include -#include -#include -#include -#include +#include #include #include -#include -#include -#include #include -#include +#include +#include #include #include #include +#include "audio_device.h" +#include "bootstrap.h" #include "configdir.h" -#include "toxic.h" -#include "windows.h" +#include "execute.h" +#include "file_transfers.h" #include "friendlist.h" -#include "prompt.h" -#include "misc_tools.h" #include "groupchat.h" -#include "file_transfers.h" #include "line_info.h" -#include "settings.h" #include "log.h" -#include "notify.h" -#include "audio_device.h" #include "message_queue.h" -#include "execute.h" -#include "term_mplex.h" +#include "misc_tools.h" #include "name_lookup.h" -#include "bootstrap.h" +#include "notify.h" +#include "prompt.h" +#include "settings.h" +#include "term_mplex.h" +#include "toxic.h" +#include "windows.h" #ifdef X11 #include "xtra.h" @@ -91,7 +91,7 @@ char *DATA_FILE = NULL; char *BLOCK_FILE = NULL; ToxWindow *prompt = NULL; -#define DATANAME "toxic_profile.tox" +#define DATANAME "toxic_profile.tox" #define BLOCKNAME "toxic_blocklist" #define MIN_PASSWORD_LEN 6 @@ -103,444 +103,439 @@ struct av_thread av_thread; struct arg_opts arg_opts; struct user_settings *user_settings = NULL; - static struct user_password { - bool data_is_encrypted; - char pass[MAX_PASSWORD_LEN + 1]; - int len; + bool data_is_encrypted; + char pass[MAX_PASSWORD_LEN + 1]; + int len; } user_password; static time_t last_signal_time; -static void catch_SIGINT(int sig) -{ - UNUSED_VAR(sig); +static void catch_SIGINT(int sig) { + UNUSED_VAR(sig); - time_t cur_time = get_unix_time(); + time_t cur_time = get_unix_time(); - if (difftime(cur_time, last_signal_time) <= 1) { - Winthread.sig_exit_toxic = 1; - } else { - last_signal_time = cur_time; - } + if (difftime(cur_time, last_signal_time) <= 1) { + Winthread.sig_exit_toxic = 1; + } else { + last_signal_time = cur_time; + } } -static void catch_SIGSEGV(int sig) -{ - UNUSED_VAR(sig); +static void catch_SIGSEGV(int sig) { + UNUSED_VAR(sig); - if (!freopen("/dev/tty", "w", stderr)) { // make sure stderr is enabled since we may have disabled it - fprintf(stderr, "Warning: Failed to enable stderr\n"); - } + if (!freopen("/dev/tty", "w", stderr)) { // make sure stderr is enabled since + // we may have disabled it + fprintf(stderr, "Warning: Failed to enable stderr\n"); + } - endwin(); - fprintf(stderr, "Caught SIGSEGV: Aborting toxic session.\n"); - exit(EXIT_FAILURE); + endwin(); + fprintf(stderr, "Caught SIGSEGV: Aborting toxic session.\n"); + exit(EXIT_FAILURE); } -static void flag_window_resize(int sig) -{ - UNUSED_VAR(sig); +static void flag_window_resize(int sig) { + UNUSED_VAR(sig); - Winthread.flag_resize = 1; + Winthread.flag_resize = 1; } -static void init_signal_catchers(void) -{ - signal(SIGWINCH, flag_window_resize); - signal(SIGINT, catch_SIGINT); - signal(SIGSEGV, catch_SIGSEGV); +static void init_signal_catchers(void) { + signal(SIGWINCH, flag_window_resize); + signal(SIGINT, catch_SIGINT); + signal(SIGSEGV, catch_SIGSEGV); } -void free_global_data(void) -{ - if (DATA_FILE) { - free(DATA_FILE); - DATA_FILE = NULL; - } - - if (BLOCK_FILE) { - free(BLOCK_FILE); - BLOCK_FILE = NULL; - } - - if (user_settings) { - free(user_settings); - user_settings = NULL; - } +void free_global_data(void) { + if (DATA_FILE) { + free(DATA_FILE); + DATA_FILE = NULL; + } + + if (BLOCK_FILE) { + free(BLOCK_FILE); + BLOCK_FILE = NULL; + } + + if (user_settings) { + free(user_settings); + user_settings = NULL; + } } -void exit_toxic_success(Tox *m) -{ - store_data(m, DATA_FILE); - memset(&user_password, 0, sizeof(struct user_password)); +void exit_toxic_success(Tox *m) { + store_data(m, DATA_FILE); + memset(&user_password, 0, sizeof(struct user_password)); - terminate_notify(); + terminate_notify(); - kill_all_file_transfers(m); - kill_all_windows(m); + kill_all_file_transfers(m); + kill_all_windows(m); #ifdef AUDIO #ifdef VIDEO - terminate_video(); + terminate_video(); #endif /* VIDEO */ - terminate_audio(); + terminate_audio(); #endif /* AUDIO */ #ifdef PYTHON - terminate_python(); + terminate_python(); #endif /* PYTHON */ - free_global_data(); - tox_kill(m); - endwin(); - curl_global_cleanup(); + free_global_data(); + tox_kill(m); + endwin(); + curl_global_cleanup(); #ifdef X11 - /* We have to terminate xtra last coz reasons - * Please don't call this anywhere else coz trust me - */ - terminate_xtra(); + /* We have to terminate xtra last coz reasons + * Please don't call this anywhere else coz trust me + */ + terminate_xtra(); #endif /* X11 */ - exit(EXIT_SUCCESS); + exit(EXIT_SUCCESS); } -void exit_toxic_err(const char *errmsg, int errcode) -{ - free_global_data(); +void exit_toxic_err(const char *errmsg, int errcode) { + free_global_data(); - if (!freopen("/dev/tty", "w", stderr)) { - fprintf(stderr, "Warning: Failed to open stderr\n"); - } + if (!freopen("/dev/tty", "w", stderr)) { + fprintf(stderr, "Warning: Failed to open stderr\n"); + } - endwin(); - fprintf(stderr, "Toxic session aborted with error code %d (%s)\n", errcode, errmsg); - exit(EXIT_FAILURE); + endwin(); + fprintf(stderr, "Toxic session aborted with error code %d (%s)\n", errcode, + errmsg); + exit(EXIT_FAILURE); } -void cb_toxcore_logger(Tox *m, TOX_LOG_LEVEL level, const char *file, uint32_t line, const char *func, - const char *message, void *user_data) -{ - UNUSED_VAR(user_data); - UNUSED_VAR(file); - UNUSED_VAR(m); +void cb_toxcore_logger(Tox *m, TOX_LOG_LEVEL level, const char *file, + uint32_t line, const char *func, const char *message, + void *user_data) { + UNUSED_VAR(user_data); + UNUSED_VAR(file); + UNUSED_VAR(m); - fprintf(stderr, "[%d] %u:%s() - %s\n", level, line, func, message); + fprintf(stderr, "[%d] %u:%s() - %s\n", level, line, func, message); } -static void init_term(void) -{ +static void init_term(void) { #if HAVE_WIDECHAR - if (!arg_opts.default_locale) { - if (setlocale(LC_ALL, "") == NULL) - exit_toxic_err("Could not set your locale, please check your locale settings or " - "disable unicode support with the -d flag.", FATALERR_LOCALE_NOT_SET); - } + if (!arg_opts.default_locale) { + if (setlocale(LC_ALL, "") == NULL) + exit_toxic_err( + "Could not set your locale, please check your locale settings or " + "disable unicode support with the -d flag.", + FATALERR_LOCALE_NOT_SET); + } #endif - initscr(); - cbreak(); - keypad(stdscr, 1); - noecho(); - nonl(); - timeout(100); - - if (has_colors()) { - short bg_color = COLOR_BLACK; - start_color(); - - if (user_settings->colour_theme == NATIVE_COLS) { - if (assume_default_colors(-1, -1) == OK) { - bg_color = -1; - } - } - - init_pair(0, COLOR_WHITE, COLOR_BLACK); - init_pair(1, COLOR_GREEN, bg_color); - init_pair(2, COLOR_CYAN, bg_color); - init_pair(3, COLOR_RED, bg_color); - init_pair(4, COLOR_BLUE, bg_color); - init_pair(5, COLOR_YELLOW, bg_color); - init_pair(6, COLOR_MAGENTA, bg_color); - init_pair(7, COLOR_BLACK, COLOR_BLACK); - init_pair(8, COLOR_BLACK, COLOR_WHITE); - } - - refresh(); + initscr(); + cbreak(); + keypad(stdscr, 1); + noecho(); + nonl(); + timeout(100); + + if (has_colors()) { + short bg_color = COLOR_BLACK; + start_color(); + + if (user_settings->colour_theme == NATIVE_COLS) { + if (assume_default_colors(-1, -1) == OK) { + bg_color = -1; + } + } + + init_pair(0, COLOR_WHITE, COLOR_BLACK); + init_pair(1, COLOR_GREEN, bg_color); + init_pair(2, COLOR_CYAN, bg_color); + init_pair(3, COLOR_RED, bg_color); + init_pair(4, COLOR_BLUE, bg_color); + init_pair(5, COLOR_YELLOW, bg_color); + init_pair(6, COLOR_MAGENTA, bg_color); + init_pair(7, COLOR_BLACK, COLOR_BLACK); + init_pair(8, COLOR_BLACK, COLOR_WHITE); + } + + refresh(); } static struct _init_messages { - char **msgs; - int num; + char **msgs; + int num; } init_messages; -/* One-time queue for messages created during init. Do not use after program init. */ -static void queue_init_message(const char *msg, ...) -{ - char frmt_msg[MAX_STR_SIZE] = {0}; +/* One-time queue for messages created during init. Do not use after program + * init. */ +static void queue_init_message(const char *msg, ...) { + char frmt_msg[MAX_STR_SIZE] = {0}; - va_list args; - va_start(args, msg); - vsnprintf(frmt_msg, sizeof(frmt_msg), msg, args); - va_end(args); + va_list args; + va_start(args, msg); + vsnprintf(frmt_msg, sizeof(frmt_msg), msg, args); + va_end(args); - int i = init_messages.num; - ++init_messages.num; + int i = init_messages.num; + ++init_messages.num; - char **new_msgs = realloc(init_messages.msgs, sizeof(char *) * init_messages.num); + char **new_msgs = + realloc(init_messages.msgs, sizeof(char *) * init_messages.num); - if (new_msgs == NULL) { - exit_toxic_err("Failed in queue_init_message", FATALERR_MEMORY); - } + if (new_msgs == NULL) { + exit_toxic_err("Failed in queue_init_message", FATALERR_MEMORY); + } - new_msgs[i] = malloc(MAX_STR_SIZE); + new_msgs[i] = malloc(MAX_STR_SIZE); - if (new_msgs[i] == NULL) { - exit_toxic_err("Failed in queue_init_message", FATALERR_MEMORY); - } + if (new_msgs[i] == NULL) { + exit_toxic_err("Failed in queue_init_message", FATALERR_MEMORY); + } - snprintf(new_msgs[i], MAX_STR_SIZE, "%s", frmt_msg); - init_messages.msgs = new_msgs; + snprintf(new_msgs[i], MAX_STR_SIZE, "%s", frmt_msg); + init_messages.msgs = new_msgs; } /* called after messages have been printed to prompt and are no longer needed */ -static void cleanup_init_messages(void) -{ - if (init_messages.num <= 0) { - return; - } +static void cleanup_init_messages(void) { + if (init_messages.num <= 0) { + return; + } - int i; + int i; - for (i = 0; i < init_messages.num; ++i) { - free(init_messages.msgs[i]); - } + for (i = 0; i < init_messages.num; ++i) { + free(init_messages.msgs[i]); + } - free(init_messages.msgs); + free(init_messages.msgs); } -static void print_init_messages(ToxWindow *toxwin) -{ - int i; +static void print_init_messages(ToxWindow *toxwin) { + int i; - for (i = 0; i < init_messages.num; ++i) { - line_info_add(toxwin, NULL, NULL, NULL, SYS_MSG, 0, 0, init_messages.msgs[i]); - } + for (i = 0; i < init_messages.num; ++i) { + line_info_add(toxwin, NULL, NULL, NULL, SYS_MSG, 0, 0, + init_messages.msgs[i]); + } } -static void load_friendlist(Tox *m) -{ - size_t i; - size_t numfriends = tox_self_get_friend_list_size(m); +static void load_friendlist(Tox *m) { + size_t i; + size_t numfriends = tox_self_get_friend_list_size(m); - for (i = 0; i < numfriends; ++i) { - friendlist_onFriendAdded(NULL, m, i, false); - } + for (i = 0; i < numfriends; ++i) { + friendlist_onFriendAdded(NULL, m, i, false); + } - sort_friendlist_index(); + sort_friendlist_index(); } -static void load_groups(Tox *m) -{ - size_t i; - size_t num_chats = tox_conference_get_chatlist_size(m); - uint32_t chatlist[num_chats]; +static void load_groups(Tox *m) { + size_t i; + size_t num_chats = tox_conference_get_chatlist_size(m); + uint32_t chatlist[num_chats]; - if (num_chats) { - tox_conference_get_chatlist(m, chatlist); - } + if (num_chats) { + tox_conference_get_chatlist(m, chatlist); + } - for (i = 0; i < num_chats; ++i) { - uint32_t groupnum = chatlist[i]; + for (i = 0; i < num_chats; ++i) { + uint32_t groupnum = chatlist[i]; - if (get_num_active_windows() >= MAX_WINDOWS_NUM) { - tox_conference_delete(m, groupnum, NULL); - continue; - } + if (get_num_active_windows() >= MAX_WINDOWS_NUM) { + tox_conference_delete(m, groupnum, NULL); + continue; + } - Tox_Err_Conference_Get_Type err; - Tox_Conference_Type type = tox_conference_get_type(m, groupnum, &err); + Tox_Err_Conference_Get_Type err; + Tox_Conference_Type type = tox_conference_get_type(m, groupnum, &err); - if (err != TOX_ERR_CONFERENCE_GET_TYPE_OK) { - tox_conference_delete(m, groupnum, NULL); - continue; - } + if (err != TOX_ERR_CONFERENCE_GET_TYPE_OK) { + tox_conference_delete(m, groupnum, NULL); + continue; + } - Tox_Err_Conference_Title t_err; - size_t length = tox_conference_get_title_size(m, groupnum, &t_err); - uint8_t title[MAX_STR_SIZE]; + Tox_Err_Conference_Title t_err; + size_t length = tox_conference_get_title_size(m, groupnum, &t_err); + uint8_t title[MAX_STR_SIZE]; - if (t_err != TOX_ERR_CONFERENCE_TITLE_OK || length >= sizeof(title)) { - length = 0; - } else { - tox_conference_get_title(m, groupnum, title, &t_err); + if (t_err != TOX_ERR_CONFERENCE_TITLE_OK || length >= sizeof(title)) { + length = 0; + } else { + tox_conference_get_title(m, groupnum, title, &t_err); - if (t_err != TOX_ERR_CONFERENCE_TITLE_OK) { - length = 0; - } - } + if (t_err != TOX_ERR_CONFERENCE_TITLE_OK) { + length = 0; + } + } - title[length] = 0; + title[length] = 0; - if (init_groupchat_win(m, groupnum, type, (const char *) title, length) == -1) { - tox_conference_delete(m, groupnum, NULL); - continue; - } + if (init_groupchat_win(m, groupnum, type, (const char *)title, length) == + -1) { + tox_conference_delete(m, groupnum, NULL); + continue; } + } } /* return length of password on success, 0 on failure */ -static int password_prompt(char *buf, int size) -{ - buf[0] = '\0'; - - /* disable terminal echo */ - struct termios oflags, nflags; - tcgetattr(fileno(stdin), &oflags); - nflags = oflags; - nflags.c_lflag &= ~ECHO; - nflags.c_lflag |= ECHONL; - - if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) { - return 0; - } +static int password_prompt(char *buf, int size) { + buf[0] = '\0'; - const char *p = fgets(buf, size, stdin); - int len = strlen(buf); + /* disable terminal echo */ + struct termios oflags, nflags; + tcgetattr(fileno(stdin), &oflags); + nflags = oflags; + nflags.c_lflag &= ~ECHO; + nflags.c_lflag |= ECHONL; - /* re-enable terminal echo */ - tcsetattr(fileno(stdin), TCSANOW, &oflags); + if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) { + return 0; + } - if (p == NULL || len <= 1) { - return 0; - } + const char *p = fgets(buf, size, stdin); + int len = strlen(buf); - /* eat overflowed stdin and return error */ - if (buf[--len] != '\n') { - int ch; + /* re-enable terminal echo */ + tcsetattr(fileno(stdin), TCSANOW, &oflags); - while ((ch = getchar()) != '\n' && ch > 0) { - } + if (p == NULL || len <= 1) { + return 0; + } + + /* eat overflowed stdin and return error */ + if (buf[--len] != '\n') { + int ch; - return 0; + while ((ch = getchar()) != '\n' && ch > 0) { } - buf[len] = '\0'; - return len; + return 0; + } + + buf[len] = '\0'; + return len; } /* Get the password from the eval command. * return length of password on success, 0 on failure */ -static int password_eval(char *buf, int size) -{ - buf[0] = '\0'; +static int password_eval(char *buf, int size) { + buf[0] = '\0'; - /* Run password_eval command */ - FILE *f = popen(user_settings->password_eval, "r"); + /* Run password_eval command */ + FILE *f = popen(user_settings->password_eval, "r"); - if (f == NULL) { - fprintf(stderr, "Executing password_eval failed\n"); - return 0; - } + if (f == NULL) { + fprintf(stderr, "Executing password_eval failed\n"); + return 0; + } - /* Get output from command */ - char *ret = fgets(buf, size, f); + /* Get output from command */ + char *ret = fgets(buf, size, f); - if (ret == NULL) { - fprintf(stderr, "Reading password from password_eval command failed\n"); - pclose(f); - return 0; - } + if (ret == NULL) { + fprintf(stderr, "Reading password from password_eval command failed\n"); + pclose(f); + return 0; + } - /* Get exit status */ - int status = pclose(f); + /* Get exit status */ + int status = pclose(f); - if (status != 0) { - fprintf(stderr, "password_eval command returned error %d\n", status); - return 0; - } + if (status != 0) { + fprintf(stderr, "password_eval command returned error %d\n", status); + return 0; + } - /* Removez whitespace or \n at end */ - int i, len = strlen(buf); + /* Removez whitespace or \n at end */ + int i, len = strlen(buf); - for (i = len - 1; i > 0 && isspace(buf[i]); i--) { - buf[i] = 0; - len--; - } + for (i = len - 1; i > 0 && isspace(buf[i]); i--) { + buf[i] = 0; + len--; + } - return len; + return len; } /* Ask user if they would like to encrypt the data file and set password */ -static void first_time_encrypt(const char *msg) -{ - char ch[256] = {0}; +static void first_time_encrypt(const char *msg) { + char ch[256] = {0}; - do { - clear_screen(); - printf("%s ", msg); - fflush(stdout); + do { + clear_screen(); + printf("%s ", msg); + fflush(stdout); - if (!strcasecmp(ch, "y\n") || !strcasecmp(ch, "n\n") || !strcasecmp(ch, "yes\n") - || !strcasecmp(ch, "no\n") || !strcasecmp(ch, "q\n")) { - break; - } + if (!strcasecmp(ch, "y\n") || !strcasecmp(ch, "n\n") || + !strcasecmp(ch, "yes\n") || !strcasecmp(ch, "no\n") || + !strcasecmp(ch, "q\n")) { + break; + } - } while (fgets(ch, sizeof(ch), stdin)); + } while (fgets(ch, sizeof(ch), stdin)); - printf("\n"); + printf("\n"); - if (ch[0] == 'q' || ch[0] == 'Q') { - exit(0); - } + if (ch[0] == 'q' || ch[0] == 'Q') { + exit(0); + } - if (ch[0] == 'y' || ch[0] == 'Y') { - int len = 0; - bool valid_password = false; - char passconfirm[MAX_PASSWORD_LEN + 1] = {0}; - - printf("Enter a new password (must be at least %d characters) ", MIN_PASSWORD_LEN); - - while (valid_password == false) { - fflush(stdout); // Flush all before user input - len = password_prompt(user_password.pass, sizeof(user_password.pass)); - user_password.len = len; - - if (strcasecmp(user_password.pass, "q") == 0) { - exit(0); - } - - if (string_is_empty(passconfirm) && (len < MIN_PASSWORD_LEN || len > MAX_PASSWORD_LEN)) { - printf("Password must be between %d and %d characters long. ", MIN_PASSWORD_LEN, MAX_PASSWORD_LEN); - continue; - } - - if (string_is_empty(passconfirm)) { - printf("Enter password again "); - snprintf(passconfirm, sizeof(passconfirm), "%s", user_password.pass); - continue; - } - - if (strcmp(user_password.pass, passconfirm) != 0) { - memset(passconfirm, 0, sizeof(passconfirm)); - memset(user_password.pass, 0, sizeof(user_password.pass)); - printf("Passwords don't match. Try again. "); - continue; - } - - valid_password = true; - } + if (ch[0] == 'y' || ch[0] == 'Y') { + int len = 0; + bool valid_password = false; + char passconfirm[MAX_PASSWORD_LEN + 1] = {0}; + + printf("Enter a new password (must be at least %d characters) ", + MIN_PASSWORD_LEN); - queue_init_message("Data file '%s' is encrypted", DATA_FILE); + while (valid_password == false) { + fflush(stdout); // Flush all before user input + len = password_prompt(user_password.pass, sizeof(user_password.pass)); + user_password.len = len; + + if (strcasecmp(user_password.pass, "q") == 0) { + exit(0); + } + + if (string_is_empty(passconfirm) && + (len < MIN_PASSWORD_LEN || len > MAX_PASSWORD_LEN)) { + printf("Password must be between %d and %d characters long. ", + MIN_PASSWORD_LEN, MAX_PASSWORD_LEN); + continue; + } + + if (string_is_empty(passconfirm)) { + printf("Enter password again "); + snprintf(passconfirm, sizeof(passconfirm), "%s", user_password.pass); + continue; + } + + if (strcmp(user_password.pass, passconfirm) != 0) { memset(passconfirm, 0, sizeof(passconfirm)); - user_password.data_is_encrypted = true; + memset(user_password.pass, 0, sizeof(user_password.pass)); + printf("Passwords don't match. Try again. "); + continue; + } + + valid_password = true; } - clear_screen(); + queue_init_message("Data file '%s' is encrypted", DATA_FILE); + memset(passconfirm, 0, sizeof(passconfirm)); + user_password.data_is_encrypted = true; + } + + clear_screen(); } /* Store Tox profile data to path. @@ -549,891 +544,917 @@ static void first_time_encrypt(const char *msg) * Return -1 on error. */ #define TEMP_PROFILE_EXT ".tmp" -int store_data(Tox *m, const char *path) -{ - if (path == NULL) { - return -1; - } +int store_data(Tox *m, const char *path) { + if (path == NULL) { + return -1; + } - char temp_path[strlen(path) + strlen(TEMP_PROFILE_EXT) + 1]; - snprintf(temp_path, sizeof(temp_path), "%s%s", path, TEMP_PROFILE_EXT); + char temp_path[strlen(path) + strlen(TEMP_PROFILE_EXT) + 1]; + snprintf(temp_path, sizeof(temp_path), "%s%s", path, TEMP_PROFILE_EXT); - FILE *fp = fopen(temp_path, "wb"); + FILE *fp = fopen(temp_path, "wb"); - if (fp == NULL) { - return -1; - } + if (fp == NULL) { + return -1; + } - size_t data_len = tox_get_savedata_size(m); - char *data = malloc(data_len * sizeof(char)); + size_t data_len = tox_get_savedata_size(m); + char *data = malloc(data_len * sizeof(char)); - if (data == NULL) { - fclose(fp); - return -1; - } + if (data == NULL) { + fclose(fp); + return -1; + } - tox_get_savedata(m, (uint8_t *) data); + tox_get_savedata(m, (uint8_t *)data); - if (user_password.data_is_encrypted && !arg_opts.unencrypt_data) { - size_t enc_len = data_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH; - char *enc_data = malloc(enc_len * sizeof(char)); + if (user_password.data_is_encrypted && !arg_opts.unencrypt_data) { + size_t enc_len = data_len + TOX_PASS_ENCRYPTION_EXTRA_LENGTH; + char *enc_data = malloc(enc_len * sizeof(char)); - if (enc_data == NULL) { - fclose(fp); - free(data); - return -1; - } + if (enc_data == NULL) { + fclose(fp); + free(data); + return -1; + } - Tox_Err_Encryption err; - tox_pass_encrypt((uint8_t *) data, data_len, (uint8_t *) user_password.pass, user_password.len, - (uint8_t *) enc_data, &err); + Tox_Err_Encryption err; + tox_pass_encrypt((uint8_t *)data, data_len, (uint8_t *)user_password.pass, + user_password.len, (uint8_t *)enc_data, &err); - if (err != TOX_ERR_ENCRYPTION_OK) { - fprintf(stderr, "tox_pass_encrypt() failed with error %d\n", err); - fclose(fp); - free(data); - free(enc_data); - return -1; - } + if (err != TOX_ERR_ENCRYPTION_OK) { + fprintf(stderr, "tox_pass_encrypt() failed with error %d\n", err); + fclose(fp); + free(data); + free(enc_data); + return -1; + } - if (fwrite(enc_data, enc_len, 1, fp) != 1) { - fprintf(stderr, "Failed to write profile data.\n"); - fclose(fp); - free(data); - free(enc_data); - return -1; - } + if (fwrite(enc_data, enc_len, 1, fp) != 1) { + fprintf(stderr, "Failed to write profile data.\n"); + fclose(fp); + free(data); + free(enc_data); + return -1; + } - free(enc_data); - } else { /* data will not be encrypted */ - if (fwrite(data, data_len, 1, fp) != 1) { - fprintf(stderr, "Failed to write profile data.\n"); - fclose(fp); - free(data); - return -1; - } + free(enc_data); + } else { /* data will not be encrypted */ + if (fwrite(data, data_len, 1, fp) != 1) { + fprintf(stderr, "Failed to write profile data.\n"); + fclose(fp); + free(data); + return -1; } + } - fclose(fp); - free(data); + fclose(fp); + free(data); - if (rename(temp_path, path) != 0) { - return -1; - } + if (rename(temp_path, path) != 0) { + return -1; + } - return 0; + return 0; +} + +static void init_tox_callbacks(Tox *m) { + tox_callback_self_connection_status(m, on_self_connection_status); + tox_callback_friend_connection_status(m, on_friend_connection_status); + tox_callback_friend_typing(m, on_friend_typing); + tox_callback_friend_request(m, on_friend_request); + tox_callback_friend_message(m, on_friend_message); + tox_callback_friend_name(m, on_friend_name); + tox_callback_friend_status(m, on_friend_status); + tox_callback_friend_status_message(m, on_friend_status_message); + tox_callback_friend_read_receipt(m, on_friend_read_receipt); + tox_callback_conference_invite(m, on_conference_invite); + tox_callback_conference_message(m, on_conference_message); + tox_callback_conference_peer_list_changed(m, on_conference_peer_list_changed); + tox_callback_conference_peer_name(m, on_conference_peer_name); + tox_callback_conference_title(m, on_conference_title); + tox_callback_file_recv(m, on_file_recv); + tox_callback_file_chunk_request(m, on_file_chunk_request); + tox_callback_file_recv_control(m, on_file_recv_control); + tox_callback_file_recv_chunk(m, on_file_recv_chunk); } -static void init_tox_callbacks(Tox *m) -{ - tox_callback_self_connection_status(m, on_self_connection_status); - tox_callback_friend_connection_status(m, on_friend_connection_status); - tox_callback_friend_typing(m, on_friend_typing); - tox_callback_friend_request(m, on_friend_request); - tox_callback_friend_message(m, on_friend_message); - tox_callback_friend_name(m, on_friend_name); - tox_callback_friend_status(m, on_friend_status); - tox_callback_friend_status_message(m, on_friend_status_message); - tox_callback_friend_read_receipt(m, on_friend_read_receipt); - tox_callback_conference_invite(m, on_conference_invite); - tox_callback_conference_message(m, on_conference_message); - tox_callback_conference_peer_list_changed(m, on_conference_peer_list_changed); - tox_callback_conference_peer_name(m, on_conference_peer_name); - tox_callback_conference_title(m, on_conference_title); - tox_callback_file_recv(m, on_file_recv); - tox_callback_file_chunk_request(m, on_file_chunk_request); - tox_callback_file_recv_control(m, on_file_recv_control); - tox_callback_file_recv_chunk(m, on_file_recv_chunk); +static void init_tox_options(struct Tox_Options *tox_opts) { + tox_options_default(tox_opts); + + tox_options_set_ipv6_enabled(tox_opts, !arg_opts.use_ipv4); + tox_options_set_udp_enabled(tox_opts, !arg_opts.force_tcp); + tox_options_set_proxy_type(tox_opts, arg_opts.proxy_type); + tox_options_set_tcp_port(tox_opts, arg_opts.tcp_port); + + if (arg_opts.logging) { + tox_options_set_log_callback(tox_opts, cb_toxcore_logger); + } + + if (!tox_options_get_ipv6_enabled(tox_opts)) { + queue_init_message("Forcing IPv4 connection"); + } + + if (tox_options_get_tcp_port(tox_opts)) { + queue_init_message("TCP relaying enabled on port %d", + tox_options_get_tcp_port(tox_opts)); + } + + if (tox_options_get_proxy_type(tox_opts) != TOX_PROXY_TYPE_NONE) { + tox_options_set_proxy_port(tox_opts, arg_opts.proxy_port); + tox_options_set_proxy_host(tox_opts, arg_opts.proxy_address); + const char *ps = + tox_options_get_proxy_type(tox_opts) == TOX_PROXY_TYPE_SOCKS5 ? "SOCKS5" + : "HTTP"; + + char tmp[sizeof(arg_opts.proxy_address) + MAX_STR_SIZE]; + snprintf(tmp, sizeof(tmp), "Using %s proxy %s : %d", ps, + arg_opts.proxy_address, arg_opts.proxy_port); + queue_init_message("%s", tmp); + } + + if (!tox_options_get_udp_enabled(tox_opts)) { + queue_init_message("UDP disabled"); + } else if (tox_options_get_proxy_type(tox_opts) != TOX_PROXY_TYPE_NONE) { + const char *msg = "WARNING: Using a proxy without disabling UDP may leak " + "your real IP address."; + queue_init_message("%s", msg); + msg = "Use the -t option to disable UDP."; + queue_init_message("%s", msg); + } } -static void init_tox_options(struct Tox_Options *tox_opts) -{ - tox_options_default(tox_opts); +/* Returns a new Tox object on success. + * If object fails to initialize the toxic process will terminate. + */ +static Tox *load_tox(char *data_path, struct Tox_Options *tox_opts, + Tox_Err_New *new_err) { + Tox *m = NULL; - tox_options_set_ipv6_enabled(tox_opts, !arg_opts.use_ipv4); - tox_options_set_udp_enabled(tox_opts, !arg_opts.force_tcp); - tox_options_set_proxy_type(tox_opts, arg_opts.proxy_type); - tox_options_set_tcp_port(tox_opts, arg_opts.tcp_port); + FILE *fp = fopen(data_path, "rb"); - if (arg_opts.logging) { - tox_options_set_log_callback(tox_opts, cb_toxcore_logger); - } + if (fp != NULL) { /* Data file exists */ + off_t len = file_size(data_path); - if (!tox_options_get_ipv6_enabled(tox_opts)) { - queue_init_message("Forcing IPv4 connection"); + if (len == 0) { + fclose(fp); + exit_toxic_err("failed in load_tox", FATALERR_FILEOP); } - if (tox_options_get_tcp_port(tox_opts)) { - queue_init_message("TCP relaying enabled on port %d", tox_options_get_tcp_port(tox_opts)); + char data[len]; + + if (fread(data, sizeof(data), 1, fp) != 1) { + fclose(fp); + exit_toxic_err("failed in load_tox", FATALERR_FILEOP); } - if (tox_options_get_proxy_type(tox_opts) != TOX_PROXY_TYPE_NONE) { - tox_options_set_proxy_port(tox_opts, arg_opts.proxy_port); - tox_options_set_proxy_host(tox_opts, arg_opts.proxy_address); - const char *ps = tox_options_get_proxy_type(tox_opts) == TOX_PROXY_TYPE_SOCKS5 ? "SOCKS5" : "HTTP"; + bool is_encrypted = tox_is_data_encrypted((uint8_t *)data); - char tmp[sizeof(arg_opts.proxy_address) + MAX_STR_SIZE]; - snprintf(tmp, sizeof(tmp), "Using %s proxy %s : %d", ps, arg_opts.proxy_address, arg_opts.proxy_port); - queue_init_message("%s", tmp); + /* attempt to encrypt an already encrypted data file */ + if (arg_opts.encrypt_data && is_encrypted) { + fclose(fp); + exit_toxic_err("failed in load_tox", FATALERR_ENCRYPT); } - if (!tox_options_get_udp_enabled(tox_opts)) { - queue_init_message("UDP disabled"); - } else if (tox_options_get_proxy_type(tox_opts) != TOX_PROXY_TYPE_NONE) { - const char *msg = "WARNING: Using a proxy without disabling UDP may leak your real IP address."; - queue_init_message("%s", msg); - msg = "Use the -t option to disable UDP."; - queue_init_message("%s", msg); + if (arg_opts.unencrypt_data && is_encrypted) { + queue_init_message("Data file '%s' has been unencrypted", data_path); + } else if (arg_opts.unencrypt_data) { + queue_init_message("Warning: passed --unencrypt-data option with " + "unencrypted data file '%s'", + data_path); } -} -/* Returns a new Tox object on success. - * If object fails to initialize the toxic process will terminate. - */ -static Tox *load_tox(char *data_path, struct Tox_Options *tox_opts, Tox_Err_New *new_err) -{ - Tox *m = NULL; + if (is_encrypted) { + if (!arg_opts.unencrypt_data) { + user_password.data_is_encrypted = true; + } - FILE *fp = fopen(data_path, "rb"); + size_t pwlen = 0; + int pweval = user_settings->password_eval[0]; - if (fp != NULL) { /* Data file exists */ - off_t len = file_size(data_path); + if (!pweval) { + clear_screen(); + printf("Enter password (q to quit) "); + } - if (len == 0) { - fclose(fp); - exit_toxic_err("failed in load_tox", FATALERR_FILEOP); - } + size_t plain_len = len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH; + char plain[plain_len]; - char data[len]; + while (true) { + fflush(stdout); // Flush before prompts so the user sees the + // question/message - if (fread(data, sizeof(data), 1, fp) != 1) { - fclose(fp); - exit_toxic_err("failed in load_tox", FATALERR_FILEOP); + if (pweval) { + pwlen = password_eval(user_password.pass, sizeof(user_password.pass)); + } else { + pwlen = + password_prompt(user_password.pass, sizeof(user_password.pass)); } - bool is_encrypted = tox_is_data_encrypted((uint8_t *) data); + user_password.len = pwlen; - /* attempt to encrypt an already encrypted data file */ - if (arg_opts.encrypt_data && is_encrypted) { - fclose(fp); - exit_toxic_err("failed in load_tox", FATALERR_ENCRYPT); + if (strcasecmp(user_password.pass, "q") == 0) { + fclose(fp); + exit(0); } - if (arg_opts.unencrypt_data && is_encrypted) { - queue_init_message("Data file '%s' has been unencrypted", data_path); - } else if (arg_opts.unencrypt_data) { - queue_init_message("Warning: passed --unencrypt-data option with unencrypted data file '%s'", data_path); + if (pwlen < MIN_PASSWORD_LEN) { + clear_screen(); + sleep(1); + printf("Invalid password. Try again. "); + pweval = 0; + continue; } - if (is_encrypted) { - if (!arg_opts.unencrypt_data) { - user_password.data_is_encrypted = true; - } - - size_t pwlen = 0; - int pweval = user_settings->password_eval[0]; - - if (!pweval) { - clear_screen(); - printf("Enter password (q to quit) "); - } - - size_t plain_len = len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH; - char plain[plain_len]; - - while (true) { - fflush(stdout); // Flush before prompts so the user sees the question/message - - if (pweval) { - pwlen = password_eval(user_password.pass, sizeof(user_password.pass)); - } else { - pwlen = password_prompt(user_password.pass, sizeof(user_password.pass)); - } - - user_password.len = pwlen; - - if (strcasecmp(user_password.pass, "q") == 0) { - fclose(fp); - exit(0); - } - - if (pwlen < MIN_PASSWORD_LEN) { - clear_screen(); - sleep(1); - printf("Invalid password. Try again. "); - pweval = 0; - continue; - } - - Tox_Err_Decryption pwerr; - tox_pass_decrypt((uint8_t *) data, len, (uint8_t *) user_password.pass, pwlen, - (uint8_t *) plain, &pwerr); - - if (pwerr == TOX_ERR_DECRYPTION_OK) { - tox_options_set_savedata_type(tox_opts, TOX_SAVEDATA_TYPE_TOX_SAVE); - tox_options_set_savedata_data(tox_opts, (uint8_t *) plain, plain_len); - - m = tox_new(tox_opts, new_err); - - if (m == NULL) { - fclose(fp); - return NULL; - } - - break; - } else if (pwerr == TOX_ERR_DECRYPTION_FAILED) { - clear_screen(); - sleep(1); - printf("Invalid password. Try again. "); - pweval = 0; - } else { - fclose(fp); - exit_toxic_err("tox_pass_decrypt() failed", pwerr); - } - } - } else { /* data is not encrypted */ - tox_options_set_savedata_type(tox_opts, TOX_SAVEDATA_TYPE_TOX_SAVE); - tox_options_set_savedata_data(tox_opts, (uint8_t *) data, len); - - m = tox_new(tox_opts, new_err); - - if (m == NULL) { - fclose(fp); - return NULL; - } + Tox_Err_Decryption pwerr; + tox_pass_decrypt((uint8_t *)data, len, (uint8_t *)user_password.pass, + pwlen, (uint8_t *)plain, &pwerr); + + if (pwerr == TOX_ERR_DECRYPTION_OK) { + tox_options_set_savedata_type(tox_opts, TOX_SAVEDATA_TYPE_TOX_SAVE); + tox_options_set_savedata_data(tox_opts, (uint8_t *)plain, plain_len); + + m = tox_new(tox_opts, new_err); + + if (m == NULL) { + fclose(fp); + return NULL; + } + + break; + } else if (pwerr == TOX_ERR_DECRYPTION_FAILED) { + clear_screen(); + sleep(1); + printf("Invalid password. Try again. "); + pweval = 0; + } else { + fclose(fp); + exit_toxic_err("tox_pass_decrypt() failed", pwerr); } + } + } else { /* data is not encrypted */ + tox_options_set_savedata_type(tox_opts, TOX_SAVEDATA_TYPE_TOX_SAVE); + tox_options_set_savedata_data(tox_opts, (uint8_t *)data, len); + + m = tox_new(tox_opts, new_err); + if (m == NULL) { fclose(fp); - } else { /* Data file does not/should not exist */ - if (file_exists(data_path)) { - exit_toxic_err("failed in load_tox", FATALERR_FILEOP); - } + return NULL; + } + } - tox_options_set_savedata_type(tox_opts, TOX_SAVEDATA_TYPE_NONE); + fclose(fp); + } else { /* Data file does not/should not exist */ + if (file_exists(data_path)) { + exit_toxic_err("failed in load_tox", FATALERR_FILEOP); + } - m = tox_new(tox_opts, new_err); + tox_options_set_savedata_type(tox_opts, TOX_SAVEDATA_TYPE_NONE); - if (m == NULL) { - return NULL; - } + m = tox_new(tox_opts, new_err); - if (store_data(m, data_path) == -1) { - exit_toxic_err("failed in load_tox", FATALERR_FILEOP); - } + if (m == NULL) { + return NULL; } - return m; + if (store_data(m, data_path) == -1) { + exit_toxic_err("failed in load_tox", FATALERR_FILEOP); + } + } + + return m; } -static Tox *load_toxic(char *data_path) -{ - Tox_Err_Options_New options_new_err; - struct Tox_Options *tox_opts = tox_options_new(&options_new_err); +static Tox *load_toxic(char *data_path) { + Tox_Err_Options_New options_new_err; + struct Tox_Options *tox_opts = tox_options_new(&options_new_err); - if (!tox_opts) { - exit_toxic_err("tox_options_new returned fatal error", options_new_err); - } + if (!tox_opts) { + exit_toxic_err("tox_options_new returned fatal error", options_new_err); + } - init_tox_options(tox_opts); + init_tox_options(tox_opts); - Tox_Err_New new_err; - Tox *m = load_tox(data_path, tox_opts, &new_err); + Tox_Err_New new_err; + Tox *m = load_tox(data_path, tox_opts, &new_err); - if (new_err == TOX_ERR_NEW_PORT_ALLOC && tox_options_get_ipv6_enabled(tox_opts)) { - queue_init_message("Falling back to ipv4"); - tox_options_set_ipv6_enabled(tox_opts, false); - m = load_tox(data_path, tox_opts, &new_err); - } + if (new_err == TOX_ERR_NEW_PORT_ALLOC && + tox_options_get_ipv6_enabled(tox_opts)) { + queue_init_message("Falling back to ipv4"); + tox_options_set_ipv6_enabled(tox_opts, false); + m = load_tox(data_path, tox_opts, &new_err); + } - if (!m) { - exit_toxic_err("tox_new returned fatal error", new_err); - } + if (!m) { + exit_toxic_err("tox_new returned fatal error", new_err); + } - if (new_err != TOX_ERR_NEW_OK) { - queue_init_message("tox_new returned non-fatal error %d", new_err); - } + if (new_err != TOX_ERR_NEW_OK) { + queue_init_message("tox_new returned non-fatal error %d", new_err); + } - init_tox_callbacks(m); - load_friendlist(m); - load_blocklist(BLOCK_FILE); + init_tox_callbacks(m); + load_friendlist(m); + load_blocklist(BLOCK_FILE); - if (tox_self_get_name_size(m) == 0) { - tox_self_set_name(m, (uint8_t *) "Toxic User", strlen("Toxic User"), NULL); - } + if (tox_self_get_name_size(m) == 0) { + tox_self_set_name(m, (uint8_t *)"Toxic User", strlen("Toxic User"), NULL); + } - tox_options_free(tox_opts); - return m; + tox_options_free(tox_opts); + return m; } -static void do_toxic(Tox *m) -{ - pthread_mutex_lock(&Winthread.lock); +static void do_toxic(Tox *m) { + pthread_mutex_lock(&Winthread.lock); - if (arg_opts.no_connect) { - pthread_mutex_unlock(&Winthread.lock); - return; - } - - tox_iterate(m, NULL); - do_tox_connection(m); + if (arg_opts.no_connect) { pthread_mutex_unlock(&Winthread.lock); + return; + } + + tox_iterate(m, NULL); + do_tox_connection(m); + pthread_mutex_unlock(&Winthread.lock); } #define INACTIVE_WIN_REFRESH_RATE 10 -void *thread_winref(void *data) -{ - Tox *m = (Tox *) data; +void *thread_winref(void *data) { + Tox *m = (Tox *)data; - uint8_t draw_count = 0; - init_signal_catchers(); + uint8_t draw_count = 0; + init_signal_catchers(); - while (true) { - draw_active_window(m); - draw_count++; + while (true) { + draw_active_window(m); + draw_count++; - if (Winthread.flag_resize) { - on_window_resize(); - Winthread.flag_resize = 0; - } else if (draw_count >= INACTIVE_WIN_REFRESH_RATE) { - refresh_inactive_windows(); - draw_count = 0; - } + if (Winthread.flag_resize) { + on_window_resize(); + Winthread.flag_resize = 0; + } else if (draw_count >= INACTIVE_WIN_REFRESH_RATE) { + refresh_inactive_windows(); + draw_count = 0; + } - if (Winthread.sig_exit_toxic) { - pthread_mutex_lock(&Winthread.lock); - exit_toxic_success(m); - } + if (Winthread.sig_exit_toxic) { + pthread_mutex_lock(&Winthread.lock); + exit_toxic_success(m); } + } } -void *thread_cqueue(void *data) -{ - Tox *m = (Tox *) data; +void *thread_cqueue(void *data) { + Tox *m = (Tox *)data; - while (true) { - pthread_mutex_lock(&Winthread.lock); + while (true) { + pthread_mutex_lock(&Winthread.lock); - size_t i; + size_t i; - for (i = 2; i < MAX_WINDOWS_NUM; ++i) { - ToxWindow *toxwin = get_window_ptr(i); + for (i = 2; i < MAX_WINDOWS_NUM; ++i) { + ToxWindow *toxwin = get_window_ptr(i); - if (toxwin != NULL && toxwin->is_chat - && get_friend_connection_status(toxwin->num) != TOX_CONNECTION_NONE) { - cqueue_try_send(toxwin, m); - } - } + if (toxwin != NULL && toxwin->is_chat && + get_friend_connection_status(toxwin->num) != TOX_CONNECTION_NONE) { + cqueue_try_send(toxwin, m); + } + } - pthread_mutex_unlock(&Winthread.lock); + pthread_mutex_unlock(&Winthread.lock); - usleep(4000); - } + usleep(4000); + } } #ifdef AUDIO -void *thread_av(void *data) -{ - ToxAV *av = (ToxAV *) data; +void *thread_av(void *data) { + ToxAV *av = (ToxAV *)data; - while (true) { - pthread_mutex_lock(&Winthread.lock); - toxav_iterate(av); - pthread_mutex_unlock(&Winthread.lock); + while (true) { + pthread_mutex_lock(&Winthread.lock); + toxav_iterate(av); + pthread_mutex_unlock(&Winthread.lock); - usleep(toxav_iteration_interval(av) * 1000); - } + usleep(toxav_iteration_interval(av) * 1000); + } } #endif /* AUDIO */ -static void print_usage(void) -{ - fprintf(stderr, "usage: toxic [OPTION] [FILE ...]\n"); - fprintf(stderr, " -4, --ipv4 Force IPv4 connection\n"); - fprintf(stderr, " -b, --debug Enable stderr for debugging\n"); - fprintf(stderr, " -c, --config Use specified config file\n"); - fprintf(stderr, " -d, --default-locale Use default POSIX locale\n"); - fprintf(stderr, " -e, --encrypt-data Encrypt an unencrypted data file\n"); - fprintf(stderr, " -f, --file Use specified data file\n"); - fprintf(stderr, " -h, --help Show this message and exit\n"); - fprintf(stderr, " -l, --logging Enable toxcore logging in stderr\n"); - fprintf(stderr, " -n, --nodes Use specified DHTnodes file\n"); - fprintf(stderr, " -o, --noconnect Do not connect to the DHT network\n"); - fprintf(stderr, " -p, --SOCKS5-proxy Use SOCKS5 proxy: Requires [IP] [port]\n"); - fprintf(stderr, " -P, --HTTP-proxy Use HTTP proxy: Requires [IP] [port]\n"); - fprintf(stderr, " -r, --namelist Use specified name lookup server list\n"); - fprintf(stderr, " -t, --force-tcp Force toxic to use a TCP connection (use with proxies)\n"); - fprintf(stderr, " -T, --tcp-server Act as a TCP relay server: Requires [port]\n"); - fprintf(stderr, " -u, --unencrypt-data Unencrypt an encrypted data file\n"); - fprintf(stderr, " -v, --version Print the version\n"); +static void print_usage(void) { + fprintf(stderr, "usage: toxic [OPTION] [FILE ...]\n"); + fprintf(stderr, " -4, --ipv4 Force IPv4 connection\n"); + fprintf(stderr, " -b, --debug Enable stderr for debugging\n"); + fprintf(stderr, " -c, --config Use specified config file\n"); + fprintf(stderr, " -d, --default-locale Use default POSIX locale\n"); + fprintf(stderr, + " -e, --encrypt-data Encrypt an unencrypted data file\n"); + fprintf(stderr, " -f, --file Use specified data file\n"); + fprintf(stderr, " -h, --help Show this message and exit\n"); + fprintf(stderr, + " -l, --logging Enable toxcore logging in stderr\n"); + fprintf(stderr, " -n, --nodes Use specified DHTnodes file\n"); + fprintf(stderr, + " -o, --noconnect Do not connect to the DHT network\n"); + fprintf( + stderr, + " -p, --SOCKS5-proxy Use SOCKS5 proxy: Requires [IP] [port]\n"); + fprintf(stderr, + " -P, --HTTP-proxy Use HTTP proxy: Requires [IP] [port]\n"); + fprintf(stderr, + " -r, --namelist Use specified name lookup server list\n"); + fprintf(stderr, " -t, --force-tcp Force toxic to use a TCP " + "connection (use with proxies)\n"); + fprintf(stderr, " -T, --tcp-server Act as a TCP relay server: " + "Requires [port]\n"); + fprintf(stderr, + " -u, --unencrypt-data Unencrypt an encrypted data file\n"); + fprintf(stderr, " -v, --version Print the version\n"); } -static void print_version(void) -{ - fprintf(stderr, "Toxic version %s\n", TOXICVER); - fprintf(stderr, "Toxcore version %d.%d.%d\n", tox_version_major(), tox_version_minor(), tox_version_patch()); +static void print_version(void) { + fprintf(stderr, "Toxic version %s\n", TOXICVER); + fprintf(stderr, "Toxcore version %d.%d.%d\n", tox_version_major(), + tox_version_minor(), tox_version_patch()); } -static void set_default_opts(void) -{ - memset(&arg_opts, 0, sizeof(struct arg_opts)); +static void set_default_opts(void) { + memset(&arg_opts, 0, sizeof(struct arg_opts)); - /* set any non-zero defaults here*/ - arg_opts.proxy_type = TOX_PROXY_TYPE_NONE; + /* set any non-zero defaults here*/ + arg_opts.proxy_type = TOX_PROXY_TYPE_NONE; } -static void parse_args(int argc, char *argv[]) -{ - set_default_opts(); - - static struct option long_opts[] = { - {"file", required_argument, 0, 'f'}, - {"ipv4", no_argument, 0, '4'}, - {"debug", no_argument, 0, 'b'}, - {"default-locale", no_argument, 0, 'd'}, - {"config", required_argument, 0, 'c'}, - {"encrypt-data", no_argument, 0, 'e'}, - {"logging", no_argument, 0, 'l'}, - {"nodes", required_argument, 0, 'n'}, - {"help", no_argument, 0, 'h'}, - {"noconnect", no_argument, 0, 'o'}, - {"namelist", required_argument, 0, 'r'}, - {"force-tcp", no_argument, 0, 't'}, - {"tcp-server", required_argument, 0, 'T'}, - {"SOCKS5-proxy", required_argument, 0, 'p'}, - {"HTTP-proxy", required_argument, 0, 'P'}, - {"unencrypt-data", no_argument, 0, 'u'}, - {"version", no_argument, 0, 'v'}, - {NULL, no_argument, NULL, 0}, - }; - - const char *opts_str = "4bdehlotuxvc:f:n:r:p:P:T:"; - int opt, indexptr; - long int port = 0; - - while ((opt = getopt_long(argc, argv, opts_str, long_opts, &indexptr)) != -1) { - switch (opt) { - case '4': - arg_opts.use_ipv4 = 1; - break; - - case 'b': - arg_opts.debug = 1; - queue_init_message("stderr enabled"); - break; - - case 'c': - snprintf(arg_opts.config_path, sizeof(arg_opts.config_path), "%s", optarg); - - if (!file_exists(arg_opts.config_path)) { - queue_init_message("Config file not found"); - } - - break; - - case 'd': - arg_opts.default_locale = 1; - queue_init_message("Using default POSIX locale"); - break; - - case 'e': - arg_opts.encrypt_data = 1; - break; - - case 'f': - arg_opts.use_custom_data = 1; - - if (DATA_FILE) { - free(DATA_FILE); - } +static void parse_args(int argc, char *argv[]) { + set_default_opts(); + + static struct option long_opts[] = { + {"file", required_argument, 0, 'f'}, + {"ipv4", no_argument, 0, '4'}, + {"debug", no_argument, 0, 'b'}, + {"default-locale", no_argument, 0, 'd'}, + {"config", required_argument, 0, 'c'}, + {"encrypt-data", no_argument, 0, 'e'}, + {"logging", no_argument, 0, 'l'}, + {"nodes", required_argument, 0, 'n'}, + {"help", no_argument, 0, 'h'}, + {"noconnect", no_argument, 0, 'o'}, + {"namelist", required_argument, 0, 'r'}, + {"force-tcp", no_argument, 0, 't'}, + {"tcp-server", required_argument, 0, 'T'}, + {"SOCKS5-proxy", required_argument, 0, 'p'}, + {"HTTP-proxy", required_argument, 0, 'P'}, + {"unencrypt-data", no_argument, 0, 'u'}, + {"version", no_argument, 0, 'v'}, + {NULL, no_argument, NULL, 0}, + }; + + const char *opts_str = "4bdehlotuxvc:f:n:r:p:P:T:"; + int opt, indexptr; + long int port = 0; + + while ((opt = getopt_long(argc, argv, opts_str, long_opts, &indexptr)) != + -1) { + switch (opt) { + case '4': + arg_opts.use_ipv4 = 1; + break; + + case 'b': + arg_opts.debug = 1; + queue_init_message("stderr enabled"); + break; + + case 'c': + snprintf(arg_opts.config_path, sizeof(arg_opts.config_path), "%s", + optarg); + + if (!file_exists(arg_opts.config_path)) { + queue_init_message("Config file not found"); + } + + break; + + case 'd': + arg_opts.default_locale = 1; + queue_init_message("Using default POSIX locale"); + break; + + case 'e': + arg_opts.encrypt_data = 1; + break; + + case 'f': + arg_opts.use_custom_data = 1; + + if (DATA_FILE) { + free(DATA_FILE); + } - if (BLOCK_FILE) { - free(BLOCK_FILE); - } + if (BLOCK_FILE) { + free(BLOCK_FILE); + } - DATA_FILE = malloc(strlen(optarg) + 1); - strcpy(DATA_FILE, optarg); + DATA_FILE = malloc(strlen(optarg) + 1); + strcpy(DATA_FILE, optarg); - if (DATA_FILE == NULL) { - exit_toxic_err("failed in parse_args", FATALERR_MEMORY); - } + if (DATA_FILE == NULL) { + exit_toxic_err("failed in parse_args", FATALERR_MEMORY); + } - BLOCK_FILE = malloc(strlen(optarg) + strlen("-blocklist") + 1); + BLOCK_FILE = malloc(strlen(optarg) + strlen("-blocklist") + 1); - if (BLOCK_FILE == NULL) { - exit_toxic_err("failed in parse_args", FATALERR_MEMORY); - } + if (BLOCK_FILE == NULL) { + exit_toxic_err("failed in parse_args", FATALERR_MEMORY); + } - strcpy(BLOCK_FILE, optarg); - strcat(BLOCK_FILE, "-blocklist"); + strcpy(BLOCK_FILE, optarg); + strcat(BLOCK_FILE, "-blocklist"); - queue_init_message("Using '%s' data file", DATA_FILE); + queue_init_message("Using '%s' data file", DATA_FILE); - break; + break; - case 'l': - arg_opts.debug = true; - arg_opts.logging = true; - queue_init_message("Toxcore logging enabled in stderr"); - break; + case 'l': + arg_opts.debug = true; + arg_opts.logging = true; + queue_init_message("Toxcore logging enabled in stderr"); + break; - case 'n': - snprintf(arg_opts.nodes_path, sizeof(arg_opts.nodes_path), "%s", optarg); - break; + case 'n': + snprintf(arg_opts.nodes_path, sizeof(arg_opts.nodes_path), "%s", optarg); + break; - case 'o': - arg_opts.no_connect = 1; - queue_init_message("DHT disabled"); - break; + case 'o': + arg_opts.no_connect = 1; + queue_init_message("DHT disabled"); + break; - case 'p': - arg_opts.proxy_type = TOX_PROXY_TYPE_SOCKS5; - snprintf(arg_opts.proxy_address, sizeof(arg_opts.proxy_address), "%s", optarg); + case 'p': + arg_opts.proxy_type = TOX_PROXY_TYPE_SOCKS5; + snprintf(arg_opts.proxy_address, sizeof(arg_opts.proxy_address), "%s", + optarg); - if (++optind > argc || argv[optind - 1][0] == '-') { - exit_toxic_err("Proxy error", FATALERR_PROXY); - } + if (++optind > argc || argv[optind - 1][0] == '-') { + exit_toxic_err("Proxy error", FATALERR_PROXY); + } - port = strtol(argv[optind - 1], NULL, 10); + port = strtol(argv[optind - 1], NULL, 10); - if (port <= 0 || port > MAX_PORT_RANGE) { - exit_toxic_err("Proxy error", FATALERR_PROXY); - } + if (port <= 0 || port > MAX_PORT_RANGE) { + exit_toxic_err("Proxy error", FATALERR_PROXY); + } - arg_opts.proxy_port = port; - break; + arg_opts.proxy_port = port; + break; - case 'P': - arg_opts.proxy_type = TOX_PROXY_TYPE_HTTP; - snprintf(arg_opts.proxy_address, sizeof(arg_opts.proxy_address), "%s", optarg); + case 'P': + arg_opts.proxy_type = TOX_PROXY_TYPE_HTTP; + snprintf(arg_opts.proxy_address, sizeof(arg_opts.proxy_address), "%s", + optarg); - if (++optind > argc || argv[optind - 1][0] == '-') { - exit_toxic_err("Proxy error", FATALERR_PROXY); - } + if (++optind > argc || argv[optind - 1][0] == '-') { + exit_toxic_err("Proxy error", FATALERR_PROXY); + } - port = strtol(argv[optind - 1], NULL, 10); + port = strtol(argv[optind - 1], NULL, 10); - if (port <= 0 || port > MAX_PORT_RANGE) { - exit_toxic_err("Proxy error", FATALERR_PROXY); - } + if (port <= 0 || port > MAX_PORT_RANGE) { + exit_toxic_err("Proxy error", FATALERR_PROXY); + } - arg_opts.proxy_port = port; - break; + arg_opts.proxy_port = port; + break; - case 'r': - snprintf(arg_opts.nameserver_path, sizeof(arg_opts.nameserver_path), "%s", optarg); + case 'r': + snprintf(arg_opts.nameserver_path, sizeof(arg_opts.nameserver_path), "%s", + optarg); - if (!file_exists(arg_opts.nameserver_path)) { - queue_init_message("nameserver list not found"); - } + if (!file_exists(arg_opts.nameserver_path)) { + queue_init_message("nameserver list not found"); + } - break; + break; - case 't': - arg_opts.force_tcp = 1; - break; + case 't': + arg_opts.force_tcp = 1; + break; - case 'T': - port = strtol(optarg, NULL, 10); + case 'T': + port = strtol(optarg, NULL, 10); - if (port <= 0 || port > MAX_PORT_RANGE) { - port = 14191; - } + if (port <= 0 || port > MAX_PORT_RANGE) { + port = 14191; + } - arg_opts.tcp_port = port; - break; + arg_opts.tcp_port = port; + break; - case 'u': - arg_opts.unencrypt_data = 1; - break; + case 'u': + arg_opts.unencrypt_data = 1; + break; - case 'v': - print_version(); - exit(EXIT_SUCCESS); + case 'v': + print_version(); + exit(EXIT_SUCCESS); - case 'h': - default: - print_usage(); - exit(EXIT_SUCCESS); - } + case 'h': + default: + print_usage(); + exit(EXIT_SUCCESS); } + } } -/* Looks for an old default profile data file and blocklist, and renames them to the new default names. +/* Looks for an old default profile data file and blocklist, and renames them to + * the new default names. * * Returns 0 on success. * Returns -1 on failure. */ #define OLD_DATA_NAME "data" #define OLD_DATA_BLOCKLIST_NAME "data-blocklist" -static int rename_old_profile(const char *user_config_dir) -{ - char old_data_file[strlen(user_config_dir) + strlen(CONFIGDIR) + strlen(OLD_DATA_NAME) + 1]; - snprintf(old_data_file, sizeof(old_data_file), "%s%s%s", user_config_dir, CONFIGDIR, OLD_DATA_NAME); +static int rename_old_profile(const char *user_config_dir) { + char old_data_file[strlen(user_config_dir) + strlen(CONFIGDIR) + + strlen(OLD_DATA_NAME) + 1]; + snprintf(old_data_file, sizeof(old_data_file), "%s%s%s", user_config_dir, + CONFIGDIR, OLD_DATA_NAME); - if (!file_exists(old_data_file)) { - return 0; - } + if (!file_exists(old_data_file)) { + return 0; + } - if (file_exists(DATA_FILE)) { - return 0; - } + if (file_exists(DATA_FILE)) { + return 0; + } - if (rename(old_data_file, DATA_FILE) != 0) { - return -1; - } + if (rename(old_data_file, DATA_FILE) != 0) { + return -1; + } - queue_init_message("Data file has been moved to %s", DATA_FILE); + queue_init_message("Data file has been moved to %s", DATA_FILE); - char old_data_blocklist[strlen(user_config_dir) + strlen(CONFIGDIR) + strlen(OLD_DATA_BLOCKLIST_NAME) + 1]; - snprintf(old_data_blocklist, sizeof(old_data_blocklist), "%s%s%s", user_config_dir, CONFIGDIR, OLD_DATA_BLOCKLIST_NAME); + char old_data_blocklist[strlen(user_config_dir) + strlen(CONFIGDIR) + + strlen(OLD_DATA_BLOCKLIST_NAME) + 1]; + snprintf(old_data_blocklist, sizeof(old_data_blocklist), "%s%s%s", + user_config_dir, CONFIGDIR, OLD_DATA_BLOCKLIST_NAME); - if (!file_exists(old_data_blocklist)) { - return 0; - } + if (!file_exists(old_data_blocklist)) { + return 0; + } - if (file_exists(BLOCK_FILE)) { - return 0; - } + if (file_exists(BLOCK_FILE)) { + return 0; + } - if (rename(old_data_blocklist, BLOCK_FILE) != 0) { - return -1; - } + if (rename(old_data_blocklist, BLOCK_FILE) != 0) { + return -1; + } - return 0; + return 0; } /* Initializes the default config directory and data files used by toxic. * * Exits the process with an error on failure. */ -static void init_default_data_files(void) -{ - if (arg_opts.use_custom_data) { - return; - } +static void init_default_data_files(void) { + if (arg_opts.use_custom_data) { + return; + } - char *user_config_dir = get_user_config_dir(); + char *user_config_dir = get_user_config_dir(); - if (user_config_dir == NULL) { - exit_toxic_err("failed in init_default_data_files()", FATALERR_FILEOP); - } + if (user_config_dir == NULL) { + exit_toxic_err("failed in init_default_data_files()", FATALERR_FILEOP); + } - int config_err = create_user_config_dirs(user_config_dir); + int config_err = create_user_config_dirs(user_config_dir); - if (config_err == -1) { - DATA_FILE = strdup(DATANAME); - BLOCK_FILE = strdup(BLOCKNAME); + if (config_err == -1) { + DATA_FILE = strdup(DATANAME); + BLOCK_FILE = strdup(BLOCKNAME); - if (DATA_FILE == NULL || BLOCK_FILE == NULL) { - exit_toxic_err("failed in init_default_data_files()", FATALERR_MEMORY); - } - } else { - DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen(DATANAME) + 1); - BLOCK_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen(BLOCKNAME) + 1); + if (DATA_FILE == NULL || BLOCK_FILE == NULL) { + exit_toxic_err("failed in init_default_data_files()", FATALERR_MEMORY); + } + } else { + DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + + strlen(DATANAME) + 1); + BLOCK_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + + strlen(BLOCKNAME) + 1); - if (DATA_FILE == NULL || BLOCK_FILE == NULL) { - exit_toxic_err("failed in init_default_data_files()", FATALERR_MEMORY); - } + if (DATA_FILE == NULL || BLOCK_FILE == NULL) { + exit_toxic_err("failed in init_default_data_files()", FATALERR_MEMORY); + } - strcpy(DATA_FILE, user_config_dir); - strcat(DATA_FILE, CONFIGDIR); - strcat(DATA_FILE, DATANAME); + strcpy(DATA_FILE, user_config_dir); + strcat(DATA_FILE, CONFIGDIR); + strcat(DATA_FILE, DATANAME); - strcpy(BLOCK_FILE, user_config_dir); - strcat(BLOCK_FILE, CONFIGDIR); - strcat(BLOCK_FILE, BLOCKNAME); - } + strcpy(BLOCK_FILE, user_config_dir); + strcat(BLOCK_FILE, CONFIGDIR); + strcat(BLOCK_FILE, BLOCKNAME); + } - /* For backwards compatibility with old toxic profile names. TODO: remove this some day */ - if (rename_old_profile(user_config_dir) == -1) { - queue_init_message("Warning: Profile backwards compatibility failed."); - } + /* For backwards compatibility with old toxic profile names. TODO: remove this + * some day */ + if (rename_old_profile(user_config_dir) == -1) { + queue_init_message("Warning: Profile backwards compatibility failed."); + } - free(user_config_dir); + free(user_config_dir); } // this doesn't do anything (yet) #ifdef X11 -void DnD_callback(const char *asdv, DropType dt) -{ - UNUSED_VAR(asdv); - UNUSED_VAR(dt); - // if (dt != DT_plain) - // return; - - // pthread_mutex_lock(&Winthread.lock); - // line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, asdv); - // pthread_mutex_unlock(&Winthread.lock); +void DnD_callback(const char *asdv, DropType dt) { + UNUSED_VAR(asdv); + UNUSED_VAR(dt); + // if (dt != DT_plain) + // return; + + // pthread_mutex_lock(&Winthread.lock); + // line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, asdv); + // pthread_mutex_unlock(&Winthread.lock); } #endif /* X11 */ -int main(int argc, char **argv) -{ - /* Make sure all written files are read/writeable only by the current user. */ - umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); +int main(int argc, char **argv) { + /* Make sure all written files are read/writeable only by the current user. */ + umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); - parse_args(argc, argv); + parse_args(argc, argv); - /* Use the -b flag to enable stderr */ - if (!arg_opts.debug) { - if (!freopen("/dev/null", "w", stderr)) { - fprintf(stderr, "Warning: failed to enable stderr\n"); - } + /* Use the -b flag to enable stderr */ + if (!arg_opts.debug) { + if (!freopen("/dev/null", "w", stderr)) { + fprintf(stderr, "Warning: failed to enable stderr\n"); } + } - if (arg_opts.encrypt_data && arg_opts.unencrypt_data) { - arg_opts.encrypt_data = 0; - arg_opts.unencrypt_data = 0; - queue_init_message("Warning: Using --unencrypt-data and --encrypt-data simultaneously has no effect"); - } + if (arg_opts.encrypt_data && arg_opts.unencrypt_data) { + arg_opts.encrypt_data = 0; + arg_opts.unencrypt_data = 0; + queue_init_message("Warning: Using --unencrypt-data and --encrypt-data " + "simultaneously has no effect"); + } - init_default_data_files(); + init_default_data_files(); - bool datafile_exists = file_exists(DATA_FILE); + bool datafile_exists = file_exists(DATA_FILE); - if (!datafile_exists && !arg_opts.unencrypt_data) { - first_time_encrypt("Creating new data file. Would you like to encrypt it? Y/n (q to quit)"); - } else if (arg_opts.encrypt_data) { - first_time_encrypt("Encrypt existing data file? Y/n (q to quit)"); - } + if (!datafile_exists && !arg_opts.unencrypt_data) { + first_time_encrypt("Creating new data file. Would you like to encrypt it? " + "Y/n (q to quit)"); + } else if (arg_opts.encrypt_data) { + first_time_encrypt("Encrypt existing data file? Y/n (q to quit)"); + } + /* init user_settings struct and load settings from conf file */ + user_settings = calloc(1, sizeof(struct user_settings)); - /* init user_settings struct and load settings from conf file */ - user_settings = calloc(1, sizeof(struct user_settings)); + if (user_settings == NULL) { + exit_toxic_err("failed in main", FATALERR_MEMORY); + } - if (user_settings == NULL) { - exit_toxic_err("failed in main", FATALERR_MEMORY); - } + const char *p = arg_opts.config_path[0] ? arg_opts.config_path : NULL; - const char *p = arg_opts.config_path[0] ? arg_opts.config_path : NULL; + if (settings_load(user_settings, p) == -1) { + queue_init_message("Failed to load user settings"); + } - if (settings_load(user_settings, p) == -1) { - queue_init_message("Failed to load user settings"); - } + int curl_init = curl_global_init(CURL_GLOBAL_ALL); + int nameserver_ret = name_lookup_init(curl_init); - int curl_init = curl_global_init(CURL_GLOBAL_ALL); - int nameserver_ret = name_lookup_init(curl_init); - - if (nameserver_ret == -1) { - queue_init_message("curl failed to initialize; name lookup service is disabled."); - } else if (nameserver_ret == -2) { - queue_init_message("Name lookup server list could not be found."); - } else if (nameserver_ret == -3) { - queue_init_message("Name lookup server list does not contain any valid entries."); - } + if (nameserver_ret == -1) { + queue_init_message( + "curl failed to initialize; name lookup service is disabled."); + } else if (nameserver_ret == -2) { + queue_init_message("Name lookup server list could not be found."); + } else if (nameserver_ret == -3) { + queue_init_message( + "Name lookup server list does not contain any valid entries."); + } #ifdef X11 - if (init_xtra(DnD_callback) == -1) { - queue_init_message("X failed to initialize"); - } + if (init_xtra(DnD_callback) == -1) { + queue_init_message("X failed to initialize"); + } #endif /* X11 */ - Tox *m = load_toxic(DATA_FILE); + Tox *m = load_toxic(DATA_FILE); - if (arg_opts.encrypt_data && !datafile_exists) { - arg_opts.encrypt_data = 0; - } + if (arg_opts.encrypt_data && !datafile_exists) { + arg_opts.encrypt_data = 0; + } - init_term(); + init_term(); - prompt = init_windows(m); - prompt_init_statusbar(prompt, m, !datafile_exists); - load_groups(m); + prompt = init_windows(m); + prompt_init_statusbar(prompt, m, !datafile_exists); + load_groups(m); - /* thread for ncurses stuff */ - if (pthread_mutex_init(&Winthread.lock, NULL) != 0) { - exit_toxic_err("failed in main", FATALERR_MUTEX_INIT); - } + /* thread for ncurses stuff */ + if (pthread_mutex_init(&Winthread.lock, NULL) != 0) { + exit_toxic_err("failed in main", FATALERR_MUTEX_INIT); + } - if (pthread_create(&Winthread.tid, NULL, thread_winref, (void *) m) != 0) { - exit_toxic_err("failed in main", FATALERR_THREAD_CREATE); - } + if (pthread_create(&Winthread.tid, NULL, thread_winref, (void *)m) != 0) { + exit_toxic_err("failed in main", FATALERR_THREAD_CREATE); + } - /* thread for message queue */ - if (pthread_create(&cqueue_thread.tid, NULL, thread_cqueue, (void *) m) != 0) { - exit_toxic_err("failed in main", FATALERR_THREAD_CREATE); - } + /* thread for message queue */ + if (pthread_create(&cqueue_thread.tid, NULL, thread_cqueue, (void *)m) != 0) { + exit_toxic_err("failed in main", FATALERR_THREAD_CREATE); + } #ifdef AUDIO - av = init_audio(prompt, m); + av = init_audio(prompt, m); #ifdef VIDEO - init_video(prompt, m); + init_video(prompt, m); #endif /* VIDEO */ - /* AV thread */ - if (pthread_create(&av_thread.tid, NULL, thread_av, (void *) av) != 0) { - exit_toxic_err("failed in main", FATALERR_THREAD_CREATE); - } + /* AV thread */ + if (pthread_create(&av_thread.tid, NULL, thread_av, (void *)av) != 0) { + exit_toxic_err("failed in main", FATALERR_THREAD_CREATE); + } - set_primary_device(input, user_settings->audio_in_dev); - set_primary_device(output, user_settings->audio_out_dev); + set_primary_device(input, user_settings->audio_in_dev); + set_primary_device(output, user_settings->audio_out_dev); #elif SOUND_NOTIFY - if (init_devices() == de_InternalError) { - queue_init_message("Failed to init audio devices"); - } + if (init_devices() == de_InternalError) { + queue_init_message("Failed to init audio devices"); + } #endif /* AUDIO */ #ifdef PYTHON - init_python(m); - invoke_autoruns(prompt->chatwin->history, prompt); + init_python(m); + invoke_autoruns(prompt->chatwin->history, prompt); #endif /* PYTHON */ - init_notify(60, user_settings->notification_timeout); - - /* screen/tmux auto-away timer */ - if (init_mplex_away_timer(m) == -1) { - queue_init_message("Failed to init mplex auto-away."); - } + init_notify(60, user_settings->notification_timeout); - int nodeslist_ret = load_DHT_nodeslist(); + /* screen/tmux auto-away timer */ + if (init_mplex_away_timer(m) == -1) { + queue_init_message("Failed to init mplex auto-away."); + } - if (nodeslist_ret != 0) { - queue_init_message("DHT nodeslist failed to load (error %d)", nodeslist_ret); - } + int nodeslist_ret = load_DHT_nodeslist(); - pthread_mutex_lock(&Winthread.lock); - print_init_messages(prompt); - pthread_mutex_unlock(&Winthread.lock); + if (nodeslist_ret != 0) { + queue_init_message("DHT nodeslist failed to load (error %d)", + nodeslist_ret); + } - cleanup_init_messages(); + pthread_mutex_lock(&Winthread.lock); + print_init_messages(prompt); + pthread_mutex_unlock(&Winthread.lock); - /* set user avatar from config file. if no path is supplied tox_unset_avatar is called */ - char avatarstr[PATH_MAX + 11]; - snprintf(avatarstr, sizeof(avatarstr), "/avatar %s", user_settings->avatar_path); - execute(prompt->chatwin->history, prompt, m, avatarstr, GLOBAL_COMMAND_MODE); + cleanup_init_messages(); - time_t last_save = get_unix_time(); + /* set user avatar from config file. if no path is supplied tox_unset_avatar + * is called */ + char avatarstr[PATH_MAX + 11]; + snprintf(avatarstr, sizeof(avatarstr), "/avatar %s", + user_settings->avatar_path); + execute(prompt->chatwin->history, prompt, m, avatarstr, GLOBAL_COMMAND_MODE); - while (true) { - do_toxic(m); + time_t last_save = get_unix_time(); - time_t cur_time = get_unix_time(); + while (true) { + do_toxic(m); - if (user_settings->autosave_freq > 0 && timed_out(last_save, user_settings->autosave_freq)) { - pthread_mutex_lock(&Winthread.lock); + time_t cur_time = get_unix_time(); - if (store_data(m, DATA_FILE) != 0) { - line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, RED, "WARNING: Failed to save to data file"); - } + if (user_settings->autosave_freq > 0 && + timed_out(last_save, user_settings->autosave_freq)) { + pthread_mutex_lock(&Winthread.lock); - pthread_mutex_unlock(&Winthread.lock); + if (store_data(m, DATA_FILE) != 0) { + line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, RED, + "WARNING: Failed to save to data file"); + } - last_save = cur_time; - } + pthread_mutex_unlock(&Winthread.lock); - usleep(tox_iteration_interval(m) * 1000); + last_save = cur_time; } - return 0; + usleep(tox_iteration_interval(m) * 1000); + } + + return 0; } diff --git a/src/windows.h b/src/windows.h index a4951e576..f18c5f7e7 100644 --- a/src/windows.h +++ b/src/windows.h @@ -24,9 +24,9 @@ #define WINDOWS_H #include -#include -#include #include +#include +#include #include @@ -38,27 +38,28 @@ #define MAX_WINDOWS_NUM 16 #define MAX_WINDOW_NAME_LENGTH 22 -#define CURS_Y_OFFSET 1 /* y-axis cursor offset for chat contexts */ +#define CURS_Y_OFFSET 1 /* y-axis cursor offset for chat contexts */ #define CHATBOX_HEIGHT 2 /* Curses foreground colours (background is black) */ typedef enum { - WHITE, - GREEN, - CYAN, - RED, - BLUE, - YELLOW, - MAGENTA, - BLACK, + WHITE, + GREEN, + CYAN, + RED, + BLUE, + YELLOW, + MAGENTA, + BLACK, } C_COLOURS; -/* tab alert types: lower types take priority (this relies on the order of C_COLOURS) */ +/* tab alert types: lower types take priority (this relies on the order of + * C_COLOURS) */ typedef enum { - WINDOW_ALERT_NONE = 0, - WINDOW_ALERT_0 = GREEN, - WINDOW_ALERT_1 = RED, - WINDOW_ALERT_2 = MAGENTA, + WINDOW_ALERT_NONE = 0, + WINDOW_ALERT_0 = GREEN, + WINDOW_ALERT_1 = RED, + WINDOW_ALERT_2 = MAGENTA, } WINDOW_ALERTS; /* Fixes text color problem on some terminals. @@ -66,40 +67,40 @@ typedef enum { /* #define URXVT_FIX */ struct Winthread { - pthread_t tid; - pthread_mutex_t lock; - volatile sig_atomic_t sig_exit_toxic; - volatile sig_atomic_t flag_resize; + pthread_t tid; + pthread_mutex_t lock; + volatile sig_atomic_t sig_exit_toxic; + volatile sig_atomic_t flag_resize; }; struct cqueue_thread { - pthread_t tid; + pthread_t tid; }; struct av_thread { - pthread_t tid; + pthread_t tid; }; struct arg_opts { - bool use_ipv4; - bool force_tcp; - bool debug; - bool default_locale; - bool use_custom_data; - bool no_connect; - bool encrypt_data; - bool unencrypt_data; - bool logging; - - char nameserver_path[MAX_STR_SIZE]; - char config_path[MAX_STR_SIZE]; - char nodes_path[MAX_STR_SIZE]; - - char proxy_address[256]; - uint8_t proxy_type; - uint16_t proxy_port; - - uint16_t tcp_port; + bool use_ipv4; + bool force_tcp; + bool debug; + bool default_locale; + bool use_custom_data; + bool no_connect; + bool encrypt_data; + bool unencrypt_data; + bool logging; + + char nameserver_path[MAX_STR_SIZE]; + char config_path[MAX_STR_SIZE]; + char nodes_path[MAX_STR_SIZE]; + + char proxy_address[256]; + uint8_t proxy_type; + uint16_t proxy_port; + + uint16_t tcp_port; }; typedef struct ToxWindow ToxWindow; @@ -109,87 +110,100 @@ typedef struct ChatContext ChatContext; typedef struct Help Help; struct ToxWindow { - /* ncurses */ - void(*onKey)(ToxWindow *, Tox *, wint_t, bool); - void(*onDraw)(ToxWindow *, Tox *); - void(*onInit)(ToxWindow *, Tox *); - - /* toxcore */ - void(*onFriendRequest)(ToxWindow *, Tox *, const char *, const char *, size_t); - void(*onFriendAdded)(ToxWindow *, Tox *, uint32_t, bool); - void(*onConnectionChange)(ToxWindow *, Tox *, uint32_t, Tox_Connection); - void(*onMessage)(ToxWindow *, Tox *, uint32_t, Tox_Message_Type, const char *, size_t); - void(*onNickChange)(ToxWindow *, Tox *, uint32_t, const char *, size_t); - void(*onStatusChange)(ToxWindow *, Tox *, uint32_t, Tox_User_Status); - void(*onStatusMessageChange)(ToxWindow *, uint32_t, const char *, size_t); - void(*onGroupMessage)(ToxWindow *, Tox *, uint32_t, uint32_t, Tox_Message_Type, const char *, size_t); - void(*onGroupInvite)(ToxWindow *, Tox *, int32_t, uint8_t, const char *, uint16_t); - void(*onGroupNameListChange)(ToxWindow *, Tox *, uint32_t); - void(*onGroupPeerNameChange)(ToxWindow *, Tox *, uint32_t, uint32_t, const char *, size_t); - void(*onGroupTitleChange)(ToxWindow *, Tox *, uint32_t, uint32_t, const char *, size_t); - void(*onFileChunkRequest)(ToxWindow *, Tox *, uint32_t, uint32_t, uint64_t, size_t); - void(*onFileRecvChunk)(ToxWindow *, Tox *, uint32_t, uint32_t, uint64_t, const char *, size_t); - void(*onFileControl)(ToxWindow *, Tox *, uint32_t, uint32_t, Tox_File_Control); - void(*onFileRecv)(ToxWindow *, Tox *, uint32_t, uint32_t, uint64_t, const char *, size_t); - void(*onTypingChange)(ToxWindow *, Tox *, uint32_t, bool); - void(*onReadReceipt)(ToxWindow *, Tox *, uint32_t, uint32_t); + /* ncurses */ + void (*onKey)(ToxWindow *, Tox *, wint_t, bool); + void (*onDraw)(ToxWindow *, Tox *); + void (*onInit)(ToxWindow *, Tox *); + + /* toxcore */ + void (*onFriendRequest)(ToxWindow *, Tox *, const char *, const char *, + size_t); + void (*onFriendAdded)(ToxWindow *, Tox *, uint32_t, bool); + void (*onConnectionChange)(ToxWindow *, Tox *, uint32_t, Tox_Connection); + void (*onMessage)(ToxWindow *, Tox *, uint32_t, Tox_Message_Type, + const char *, size_t); + void (*onNickChange)(ToxWindow *, Tox *, uint32_t, const char *, size_t); + void (*onStatusChange)(ToxWindow *, Tox *, uint32_t, Tox_User_Status); + void (*onStatusMessageChange)(ToxWindow *, uint32_t, const char *, size_t); + void (*onGroupMessage)(ToxWindow *, Tox *, uint32_t, uint32_t, + Tox_Message_Type, const char *, size_t); + void (*onGroupInvite)(ToxWindow *, Tox *, int32_t, uint8_t, const char *, + uint16_t); + void (*onGroupNameListChange)(ToxWindow *, Tox *, uint32_t); + void (*onGroupPeerNameChange)(ToxWindow *, Tox *, uint32_t, uint32_t, + const char *, size_t); + void (*onGroupTitleChange)(ToxWindow *, Tox *, uint32_t, uint32_t, + const char *, size_t); + void (*onFileChunkRequest)(ToxWindow *, Tox *, uint32_t, uint32_t, uint64_t, + size_t); + void (*onFileRecvChunk)(ToxWindow *, Tox *, uint32_t, uint32_t, uint64_t, + const char *, size_t); + void (*onFileControl)(ToxWindow *, Tox *, uint32_t, uint32_t, + Tox_File_Control); + void (*onFileRecv)(ToxWindow *, Tox *, uint32_t, uint32_t, uint64_t, + const char *, size_t); + void (*onTypingChange)(ToxWindow *, Tox *, uint32_t, bool); + void (*onReadReceipt)(ToxWindow *, Tox *, uint32_t, uint32_t); #ifdef AUDIO - void(*onInvite)(ToxWindow *, ToxAV *, uint32_t, int); - void(*onRinging)(ToxWindow *, ToxAV *, uint32_t, int); - void(*onStarting)(ToxWindow *, ToxAV *, uint32_t, int); - void(*onEnding)(ToxWindow *, ToxAV *, uint32_t, int); - void(*onError)(ToxWindow *, ToxAV *, uint32_t, int); - void(*onStart)(ToxWindow *, ToxAV *, uint32_t, int); - void(*onCancel)(ToxWindow *, ToxAV *, uint32_t, int); - void(*onReject)(ToxWindow *, ToxAV *, uint32_t, int); - void(*onEnd)(ToxWindow *, ToxAV *, uint32_t, int); - void(*onWriteDevice)(ToxWindow *, Tox *, uint32_t, int, const int16_t *, unsigned int, uint8_t, unsigned int); - - int device_selection[2]; /* -1 if not set, if set uses these selections instead of primary device */ - bool is_call; - int ringing_sound; + void (*onInvite)(ToxWindow *, ToxAV *, uint32_t, int); + void (*onRinging)(ToxWindow *, ToxAV *, uint32_t, int); + void (*onStarting)(ToxWindow *, ToxAV *, uint32_t, int); + void (*onEnding)(ToxWindow *, ToxAV *, uint32_t, int); + void (*onError)(ToxWindow *, ToxAV *, uint32_t, int); + void (*onStart)(ToxWindow *, ToxAV *, uint32_t, int); + void (*onCancel)(ToxWindow *, ToxAV *, uint32_t, int); + void (*onReject)(ToxWindow *, ToxAV *, uint32_t, int); + void (*onEnd)(ToxWindow *, ToxAV *, uint32_t, int); + void (*onWriteDevice)(ToxWindow *, Tox *, uint32_t, int, const int16_t *, + unsigned int, uint8_t, unsigned int); + + int device_selection[2]; /* -1 if not set, if set uses these selections + instead of primary device */ + bool is_call; + int ringing_sound; #ifdef VIDEO - int video_device_selection[2]; /* -1 if not set, if set uses these selections instead of primary video device */ + int video_device_selection[2]; /* -1 if not set, if set uses these selections + instead of primary video device */ #endif /* VIDEO */ #endif /* AUDIO */ - int active_box; /* For box notify */ + int active_box; /* For box notify */ - char name[TOXIC_MAX_NAME_LENGTH + 1]; - uint32_t num; /* corresponds to friendnumber in chat windows */ - uint8_t index; /* This window's index in the windows array */ - int x; + char name[TOXIC_MAX_NAME_LENGTH + 1]; + uint32_t num; /* corresponds to friendnumber in chat windows */ + uint8_t index; /* This window's index in the windows array */ + int x; - bool is_chat; - bool is_prompt; - bool is_friendlist; - bool is_groupchat; - int show_peerlist; /* used to toggle groupchat peerlist */ + bool is_chat; + bool is_prompt; + bool is_friendlist; + bool is_groupchat; + int show_peerlist; /* used to toggle groupchat peerlist */ - WINDOW_ALERTS alert; + WINDOW_ALERTS alert; - ChatContext *chatwin; - StatusBar *stb; - Help *help; + ChatContext *chatwin; + StatusBar *stb; + Help *help; - WINDOW *window; + WINDOW *window; }; /* statusbar info holder */ struct StatusBar { - WINDOW *topline; - char statusmsg[TOX_MAX_STATUS_MESSAGE_LENGTH + 1]; - size_t statusmsg_len; - char nick[TOXIC_MAX_NAME_LENGTH + 1]; - size_t nick_len; - Tox_User_Status status; - Tox_Connection connection; + WINDOW *topline; + char statusmsg[TOX_MAX_STATUS_MESSAGE_LENGTH + 1]; + size_t statusmsg_len; + char nick[TOXIC_MAX_NAME_LENGTH + 1]; + size_t nick_len; + Tox_User_Status status; + Tox_Connection connection; }; #ifdef AUDIO @@ -199,17 +213,17 @@ struct StatusBar { /* holds display info for audio calls */ struct infobox { - float vad_lvl; - bool in_is_muted; - bool out_is_muted; - bool hide; - bool active; + float vad_lvl; + bool in_is_muted; + bool out_is_muted; + bool hide; + bool active; - time_t lastupdate; - time_t starttime; - char timestr[TIME_STR_SIZE]; + time_t lastupdate; + time_t starttime; + char timestr[TIME_STR_SIZE]; - WINDOW *win; + WINDOW *win; }; #endif /* AUDIO */ @@ -217,38 +231,39 @@ struct infobox { /* chat and groupchat window/buffer holder */ struct ChatContext { - wchar_t line[MAX_STR_SIZE]; - int pos; - int len; - int start; /* the position to start printing line at */ + wchar_t line[MAX_STR_SIZE]; + int pos; + int len; + int start; /* the position to start printing line at */ - wchar_t ln_history[MAX_LINE_HIST][MAX_STR_SIZE]; /* history for input lines/commands */ - int hst_pos; - int hst_tot; + wchar_t ln_history[MAX_LINE_HIST] + [MAX_STR_SIZE]; /* history for input lines/commands */ + int hst_pos; + int hst_tot; - wchar_t yank[MAX_STR_SIZE]; /* contains last killed/discarded line */ - int yank_len; + wchar_t yank[MAX_STR_SIZE]; /* contains last killed/discarded line */ + int yank_len; - struct history *hst; - struct chatlog *log; - struct chat_queue *cqueue; + struct history *hst; + struct chatlog *log; + struct chat_queue *cqueue; #ifdef AUDIO - struct infobox infobox; + struct infobox infobox; #endif - uint8_t self_is_typing; - uint8_t pastemode; /* whether to translate \r to \n */ + uint8_t self_is_typing; + uint8_t pastemode; /* whether to translate \r to \n */ - WINDOW *history; - WINDOW *linewin; - WINDOW *sidebar; + WINDOW *history; + WINDOW *linewin; + WINDOW *sidebar; }; struct Help { - WINDOW *win; - int type; - bool active; + WINDOW *win; + int type; + bool active; }; ToxWindow *init_windows(Tox *m); @@ -257,7 +272,7 @@ int add_window(Tox *m, ToxWindow *w); void del_window(ToxWindow *w); void set_active_window_index(uint8_t index); int get_num_active_windows(void); -void kill_all_windows(Tox *m); /* should only be called on shutdown */ +void kill_all_windows(Tox *m); /* should only be called on shutdown */ void on_window_resize(void); void force_refresh(WINDOW *w); ToxWindow *get_window_ptr(size_t i);