From c2dce960b8df7a428e6f9fb99843aa4964c3c59b Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 3 Jun 2014 02:02:24 -0400 Subject: [PATCH] add setting to allow specified download path & some fixes --- src/chat.c | 34 +++++++++++-- src/chat_commands.c | 2 +- src/misc_tools.c | 2 +- src/misc_tools.h | 5 +- src/settings.c | 118 +++++++++++++++++++++++++++++--------------- src/settings.h | 5 +- 6 files changed, 116 insertions(+), 50 deletions(-) diff --git a/src/chat.c b/src/chat.c index 603280d..7f32f30 100644 --- a/src/chat.c +++ b/src/chat.c @@ -228,15 +228,21 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t if (self->num != num) return; - uint8_t msg[MAX_STR_SIZE]; + uint8_t msg[MAX_STR_SIZE * 2]; uint8_t *errmsg; pathname[path_len] = '\0'; - uint8_t filename[MAX_STR_SIZE]; - get_file_name(pathname, filename); - snprintf(msg, sizeof(msg), "File transfer request for '%s' (%llu bytes).", filename, + /* holds the filename appended to the user specified path */ + uint8_t filename_path[MAX_STR_SIZE] = {0}; + + /* holds the lone filename */ + uint8_t filename_nopath[MAX_STR_SIZE]; + get_file_name(filename_nopath, pathname); + int len = strlen(filename_nopath); + + snprintf(msg, sizeof(msg), "File transfer request for '%s' (%llu bytes).", filename_nopath, (long long unsigned int)filesize); line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); @@ -246,10 +252,28 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t return; } + /* use specified path in config if possible */ + if (user_settings->download_path[0]) { + snprintf(filename_path, sizeof(filename_path), "%s%s", user_settings->download_path, filename_nopath); + len += strlen(user_settings->download_path); + } + + if (len >= sizeof(friends[num].file_receiver.filenames[filenum])) { + errmsg = "File name too long; discarding."; + line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); + return; + } + + uint8_t filename[MAX_STR_SIZE]; + + if (filename_path[0]) + strcpy(filename, filename_path); + else + strcpy(filename, filename_nopath); + /* Append a number to duplicate file names */ FILE *filecheck = NULL; int count = 1; - int len = strlen(filename); while ((filecheck = fopen(filename, "r"))) { filename[len] = '\0'; diff --git a/src/chat_commands.c b/src/chat_commands.c index afbbe27..a0eb2ab 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -246,7 +246,7 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv fseek(file_to_send, 0, SEEK_SET); uint8_t filename[MAX_STR_SIZE]; - get_file_name(path, filename); + get_file_name(filename, path); int filenum = tox_new_file_sender(m, self->num, filesize, filename, strlen(filename)); if (filenum == -1) { diff --git a/src/misc_tools.c b/src/misc_tools.c index 928c6b4..e401010 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -233,7 +233,7 @@ void mv_curs_end(WINDOW *w, size_t len, int max_y, int max_x) } /* gets base file name from path or original file name if no path is supplied */ -void get_file_name(uint8_t *pathname, uint8_t *namebuf) +void get_file_name(uint8_t *namebuf, uint8_t *pathname) { int idx = strlen(pathname) - 1; diff --git a/src/misc_tools.h b/src/misc_tools.h index b5b2a93..e1e7e45 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -63,9 +63,10 @@ void alert_window(ToxWindow *self, int type, bool is_beep); /* case-insensitive string compare function for use with qsort */ int qsort_strcasecmp_hlpr(const void *nick1, const void *nick2); -/* Returns true if nick is valid. A valid toxic nick: +/* Returns 1 if nick is valid, 0 if not. A valid toxic nick: - cannot be empty - cannot start with a space + - must not contain a forward slash (for logfile naming purposes) - must not contain contiguous spaces */ int valid_nick(uint8_t *nick); @@ -73,4 +74,4 @@ int valid_nick(uint8_t *nick); void mv_curs_end(WINDOW *w, size_t len, int max_y, int max_x); /* gets base file name from path or original file name if no path is supplied */ -void get_file_name(uint8_t *pathname, uint8_t *namebuf); +void get_file_name(uint8_t *namebuf, uint8_t *pathname); diff --git a/src/settings.c b/src/settings.c index 49b4f28..add9e47 100644 --- a/src/settings.c +++ b/src/settings.c @@ -29,17 +29,18 @@ #include "settings.h" #include "line_info.h" -static void uset_autolog(struct user_settings *s, int val); -static void uset_time(struct user_settings *s, int val); -static void uset_alerts(struct user_settings *s, int val); -static void uset_colours(struct user_settings *s, int val); -static void uset_ain_dev(struct user_settings *s, int val); -static void uset_aout_dev(struct user_settings *s, int val); -static void uset_hst_size(struct user_settings *s, int val); +static void uset_autolog(struct user_settings *s, const char *val); +static void uset_time(struct user_settings *s, const char *val); +static void uset_alerts(struct user_settings *s, const char *val); +static void uset_colours(struct user_settings *s, const char *val); +static void uset_ain_dev(struct user_settings *s, const char *val); +static void uset_aout_dev(struct user_settings *s, const char *val); +static void uset_hst_size(struct user_settings *s, const char *val); +static void uset_dwnld_path(struct user_settings *s, const char *val); struct { - const char *name; - void (*func)(struct user_settings *s, int val); + const char *key; + void (*func)(struct user_settings *s, const char *val); } user_settings_list[] = { { "autolog", uset_autolog }, { "time", uset_time }, @@ -48,63 +49,103 @@ struct { { "audio_in_dev", uset_ain_dev }, { "audio_out_dev", uset_aout_dev }, { "history_size", uset_hst_size }, + { "download_path", uset_dwnld_path }, }; -static void uset_autolog(struct user_settings *s, int val) +static void uset_autolog(struct user_settings *s, const char *val) { + int n = atoi(val); + /* default off if invalid value */ - s->autolog = val == AUTOLOG_ON ? AUTOLOG_ON : AUTOLOG_OFF; + s->autolog = n == AUTOLOG_ON ? AUTOLOG_ON : AUTOLOG_OFF; } -static void uset_time(struct user_settings *s, int val) +static void uset_time(struct user_settings *s, const char *val) { + int n = atoi(val); + /* default to 24 hour time if invalid value */ - s->time = val == TIME_12 ? TIME_12 : TIME_24; + s->time = n == TIME_12 ? TIME_12 : TIME_24; } -static void uset_alerts(struct user_settings *s, int val) +static void uset_alerts(struct user_settings *s, const char *val) { + int n = atoi(val); + /* alerts default on if invalid value */ - s->alerts = val == ALERTS_DISABLED ? ALERTS_DISABLED : ALERTS_ENABLED; + s->alerts = n == ALERTS_DISABLED ? ALERTS_DISABLED : ALERTS_ENABLED; } -static void uset_colours(struct user_settings *s, int val) +static void uset_colours(struct user_settings *s, const char *val) { + int n = atoi(val); + /* use default toxic colours if invalid value */ - s->colour_theme = val == NATIVE_COLS ? NATIVE_COLS : DFLT_COLS; + s->colour_theme = n == NATIVE_COLS ? NATIVE_COLS : DFLT_COLS; } -static void uset_ain_dev(struct user_settings *s, int val) +static void uset_ain_dev(struct user_settings *s, const char *val) { - if (val < 0 || val > MAX_DEVICES) - val = (long int) 0; + int n = atoi(val); - s->audio_in_dev = (long int) val; + if (n < 0 || n > MAX_DEVICES) + n = (long int) 0; + + s->audio_in_dev = (long int) n; } -static void uset_aout_dev(struct user_settings *s, int val) +static void uset_aout_dev(struct user_settings *s, const char *val) { - if (val < 0 || val > MAX_DEVICES) - val = (long int) 0; + int n = atoi(val); - s->audio_out_dev = (long int) val; + if (n < 0 || n > MAX_DEVICES) + n = (long int) 0; + + s->audio_out_dev = (long int) n; } -static void uset_hst_size(struct user_settings *s, int val) +static void uset_hst_size(struct user_settings *s, const char *val) { + int n = atoi(val); + /* if val is out of range use default history size */ - s->history_size = (val > MAX_HISTORY || val < MIN_HISTORY) ? DFLT_HST_SIZE : val; + s->history_size = (n > MAX_HISTORY || n < MIN_HISTORY) ? DFLT_HST_SIZE : n; +} + +static void uset_dwnld_path(struct user_settings *s, const char *val) +{ + memset(s->download_path, 0, sizeof(s->download_path)); + + if (val == NULL) + return; + + int len = strlen(val); + + if (len >= sizeof(s->download_path) - 2) /* leave room for null and '/' */ + return; + + FILE *fp = fopen(val, "r"); + + if (fp == NULL) + return; + + strcpy(s->download_path, val); + + if (val[len] != '/') + strcat(s->download_path, "/"); } static void set_default_settings(struct user_settings *s) { - uset_autolog(s, AUTOLOG_OFF); - uset_time(s, TIME_24); - uset_alerts(s, ALERTS_ENABLED); - uset_colours(s, DFLT_COLS); - uset_ain_dev(s, 0); - uset_aout_dev(s, 0); - uset_hst_size(s, DFLT_HST_SIZE); + /* see settings_values enum in settings.h for defaults */ + uset_autolog(s, "0"); + uset_time(s, "24"); + uset_alerts(s, "0"); + uset_colours(s, "0"); + uset_ain_dev(s, "0"); + uset_aout_dev(s, "0"); + uset_hst_size(s, "700"); + uset_dwnld_path(s, NULL); } int settings_load(struct user_settings *s, char *path) @@ -137,17 +178,16 @@ int settings_load(struct user_settings *s, char *path) if (line[0] == '#' || !line[0]) continue; - char *name = strtok(line, ":"); - char *val_s = strtok(NULL, ";"); + const char *key = strtok(line, ":"); + const char *val = strtok(NULL, ";"); - if (name == NULL || val_s == NULL) + if (key == NULL || val == NULL) continue; - int val = atoi(val_s); int i; for (i = 0; i < NUM_SETTINGS; ++i) { - if (!strcmp(user_settings_list[i].name, name)) { + if (!strcmp(user_settings_list[i].key, key)) { (user_settings_list[i].func)(s, val); break; } diff --git a/src/settings.h b/src/settings.h index a989030..83f05b3 100644 --- a/src/settings.h +++ b/src/settings.h @@ -20,7 +20,7 @@ * */ -#define NUM_SETTINGS 7 +#define NUM_SETTINGS 8 /* holds user setting values */ struct user_settings { @@ -31,6 +31,7 @@ struct user_settings { long int audio_in_dev; long int audio_out_dev; int history_size; /* int between MIN_HISTORY and MAX_HISTORY */ + char download_path[MAX_STR_SIZE]; }; enum { @@ -47,6 +48,6 @@ enum { DFLT_COLS = 0, DFLT_HST_SIZE = 700, -}; +} settings_values; int settings_load(struct user_settings *s, char *path);