From e5e4244d45ebef8090132f250636ef8d1f09b749 Mon Sep 17 00:00:00 2001 From: Horea Christian Date: Fri, 12 Jan 2024 20:16:10 -0500 Subject: [PATCH 1/8] Trying to add open function --- src/chat_commands.c | 81 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/src/chat_commands.c b/src/chat_commands.c index 5d4f43b4c..7af5c49d3 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -335,6 +335,87 @@ void cmd_game_join(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*a #endif // GAMES +void cmd_fopen(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) +{ + UNUSED_VAR(window); + + if (argc < 1) { + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "File ID required."); + return; + } + + long int idx = strtol(argv[1], NULL, 10); + + if ((idx == 0 && strcmp(argv[1], "0")) || idx < 0 || idx >= MAX_FILES) { + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending file transfers with ID %ld", idx); + return; + } + + FileTransfer *ft = get_file_transfer_struct_index(self->num, idx, FILE_TRANSFER_RECV); + + if (!ft) { + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending file transfers with ID %ld.", idx); + return; + } + + if (ft->state != FILE_TRANSFER_PENDING) { + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending file transfers with ID %ld.", idx); + return; + } + + if ((ft->file = fopen(ft->file_path, "a")) == NULL) { + const char *msg = "File transfer failed: Invalid download path."; + close_file_transfer(self, tox, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error); + return; + } + + Tox_Err_File_Control err; + tox_file_control(tox, self->num, ft->filenumber, TOX_FILE_CONTROL_RESUME, &err); + + if (err != TOX_ERR_FILE_CONTROL_OK) { + goto on_recv_error; + } + + xdg_open(ft); + + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Saving file [%ld] as: '%s'", idx, ft->file_path); + + const bool auto_accept_files = friend_get_auto_accept_files(self->num); + const uint32_t line_skip = auto_accept_files ? 4 : 2; + + char progline[MAX_STR_SIZE]; + init_progress_bar(progline); + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s", progline); + ft->line_id = self->chatwin->hst->line_end->id + line_skip; + ft->state = FILE_TRANSFER_STARTED; + + return; + +on_recv_error: + + switch (err) { + case TOX_ERR_FILE_CONTROL_FRIEND_NOT_FOUND: + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed: Friend not found."); + return; + + case TOX_ERR_FILE_CONTROL_FRIEND_NOT_CONNECTED: + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed: Friend is not online."); + return; + + case TOX_ERR_FILE_CONTROL_NOT_FOUND: + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed: Invalid filenumber."); + return; + + case TOX_ERR_FILE_CONTROL_SENDQ: + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed: Connection error."); + return; + + default: + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed (error %d)\n", err); + return; + } +} + void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); From 26832c34e526f4851e6ddffb587347ac6a9af217 Mon Sep 17 00:00:00 2001 From: Horea Christian Date: Fri, 12 Jan 2024 20:26:56 -0500 Subject: [PATCH 2/8] Writing xdg function --- src/chat_commands.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/chat_commands.c b/src/chat_commands.c index 7af5c49d3..53e9c2783 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -335,6 +335,23 @@ void cmd_game_join(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*a #endif // GAMES +void open_with_xdg(const char *filename) +{ + // Make the command + char command[MAX_STR_SIZE]; + snprintf(command, sizeof(command), "xdg-open %s", filename); + + // Call the command + int result = system(command); + + // Did it work? + if (result == -1) { + perror("Cannot open file.\n"); + } else { + printf("File opened.\n"); + } +} + void cmd_fopen(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) { UNUSED_VAR(window); @@ -376,7 +393,7 @@ void cmd_fopen(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv) goto on_recv_error; } - xdg_open(ft); + open_with_xdg(ft); line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Saving file [%ld] as: '%s'", idx, ft->file_path); From 6f99015dc8616f86fe703f499ea92387369eba80 Mon Sep 17 00:00:00 2001 From: Horea Christian Date: Fri, 12 Jan 2024 20:37:08 -0500 Subject: [PATCH 3/8] Adding command to more places --- src/chat.c | 3 ++- src/execute.c | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/chat.c b/src/chat.c index 2b9e909a4..f12c6bcf1 100644 --- a/src/chat.c +++ b/src/chat.c @@ -90,9 +90,10 @@ static const char *chat_cmd_list[] = { #ifdef QRCODE "/myqr", #endif /* QRCODE */ + "/fopen", "/nick", - "/note", "/nospam", + "/note", "/quit", "/savefile", "/sendfile", diff --git a/src/execute.c b/src/execute.c index e3562b30b..8a54704c5 100644 --- a/src/execute.c +++ b/src/execute.c @@ -92,6 +92,7 @@ static struct cmd_func chat_commands[] = { { "/play", cmd_game_join }, #endif { "/savefile", cmd_savefile }, + { "/fopen", cmd_fopen }, { "/sendfile", cmd_sendfile }, #ifdef AUDIO { "/call", cmd_call }, From 3940f2be0d8aded24991779b7bc42bdf861bc269 Mon Sep 17 00:00:00 2001 From: Horea Christian Date: Fri, 12 Jan 2024 20:40:16 -0500 Subject: [PATCH 4/8] Sorted new command and added in header file --- src/chat_commands.h | 3 ++- src/execute.c | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/chat_commands.h b/src/chat_commands.h index 58d82d97c..d968b5a42 100644 --- a/src/chat_commands.h +++ b/src/chat_commands.h @@ -30,9 +30,10 @@ void cmd_autoaccept_files(WINDOW *window, ToxWindow *self, Tox *tox, int argc, c void cmd_cancelfile(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_conference_invite(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_conference_join(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_fopen(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); +void cmd_game_join(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_group_accept(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_group_invite(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]); -void cmd_game_join(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_savefile(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); void cmd_sendfile(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]); diff --git a/src/execute.c b/src/execute.c index 8a54704c5..72d1e7cf9 100644 --- a/src/execute.c +++ b/src/execute.c @@ -91,8 +91,8 @@ static struct cmd_func chat_commands[] = { #ifdef GAMES { "/play", cmd_game_join }, #endif - { "/savefile", cmd_savefile }, { "/fopen", cmd_fopen }, + { "/savefile", cmd_savefile }, { "/sendfile", cmd_sendfile }, #ifdef AUDIO { "/call", cmd_call }, From d81eed0132e28b075fe70ee2acd22acd9caba322 Mon Sep 17 00:00:00 2001 From: Horea Christian Date: Fri, 12 Jan 2024 21:12:03 -0500 Subject: [PATCH 5/8] tmp dir (not yet used), and included everything in fopen function --- src/chat_commands.c | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/chat_commands.c b/src/chat_commands.c index 53e9c2783..4421ea693 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -20,6 +20,7 @@ * */ +#include #include #include @@ -335,22 +336,6 @@ void cmd_game_join(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*a #endif // GAMES -void open_with_xdg(const char *filename) -{ - // Make the command - char command[MAX_STR_SIZE]; - snprintf(command, sizeof(command), "xdg-open %s", filename); - - // Call the command - int result = system(command); - - // Did it work? - if (result == -1) { - perror("Cannot open file.\n"); - } else { - printf("File opened.\n"); - } -} void cmd_fopen(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE]) { @@ -393,7 +378,29 @@ void cmd_fopen(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv) goto on_recv_error; } - open_with_xdg(ft); + // make tmp_dir if it does not exist + /// don't need this for now + const char *tmp_dir = "/tmp/toxic-download-dir/"; + DIR* dir = opendir("/tmp/toxic-download-dir/"); + if (dir) { + closedir(dir); + } + + if (mkdir(tmp_dir, S_IRWXU) == -1) { + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Could not create tox download /tmp/ directory."); + } + // end make tmpdir if it does not exist + + // make and call xdg command + char command[MAX_STR_SIZE]; + snprintf(command, sizeof(command), "xdg-open %s", ft->file_path); + + int open_result = system(command); + + if (open_result == -1) { + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Could not open file."); + } + // end make and call xdg command line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Saving file [%ld] as: '%s'", idx, ft->file_path); From a9b10b0bfdfa11025a544d5c89f5a4296790e086 Mon Sep 17 00:00:00 2001 From: Horea Christian Date: Fri, 12 Jan 2024 21:14:58 -0500 Subject: [PATCH 6/8] Open after download, not before --- src/chat_commands.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/chat_commands.c b/src/chat_commands.c index 4421ea693..66786a1e4 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -379,7 +379,7 @@ void cmd_fopen(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv) } // make tmp_dir if it does not exist - /// don't need this for now + /// don't need this for now, prob should go somewhere else anyway. const char *tmp_dir = "/tmp/toxic-download-dir/"; DIR* dir = opendir("/tmp/toxic-download-dir/"); if (dir) { @@ -391,16 +391,6 @@ void cmd_fopen(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv) } // end make tmpdir if it does not exist - // make and call xdg command - char command[MAX_STR_SIZE]; - snprintf(command, sizeof(command), "xdg-open %s", ft->file_path); - - int open_result = system(command); - - if (open_result == -1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Could not open file."); - } - // end make and call xdg command line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Saving file [%ld] as: '%s'", idx, ft->file_path); @@ -413,6 +403,17 @@ void cmd_fopen(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv) ft->line_id = self->chatwin->hst->line_end->id + line_skip; ft->state = FILE_TRANSFER_STARTED; + // make and call xdg command + char command[MAX_STR_SIZE]; + snprintf(command, sizeof(command), "xdg-open %s", ft->file_path); + + int open_result = system(command); + + if (open_result == -1) { + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Could not open file."); + } + // end make and call xdg command + return; on_recv_error: From 856d5a1fabccff90a870b3b50d2470e5568f2f02 Mon Sep 17 00:00:00 2001 From: Horea Christian Date: Fri, 12 Jan 2024 21:19:41 -0500 Subject: [PATCH 7/8] Debugging why xdg doesn't launch --- src/chat_commands.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/chat_commands.c b/src/chat_commands.c index 66786a1e4..e66e1dcdc 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -406,6 +406,7 @@ void cmd_fopen(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv) // make and call xdg command char command[MAX_STR_SIZE]; snprintf(command, sizeof(command), "xdg-open %s", ft->file_path); + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "I am about to run the xdg command."); int open_result = system(command); From 8c78994cd8d14c8f9a4e91590cfd3ad122a5ae9a Mon Sep 17 00:00:00 2001 From: Horea Christian Date: Fri, 12 Jan 2024 21:24:59 -0500 Subject: [PATCH 8/8] =?UTF-8?q?Line=20brak=20maybe=20makes=20the=20error?= =?UTF-8?q?=20line=20clearer=20=F0=9F=A4=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chat_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/chat_commands.c b/src/chat_commands.c index e66e1dcdc..9e2ef9e68 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -387,7 +387,7 @@ void cmd_fopen(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv) } if (mkdir(tmp_dir, S_IRWXU) == -1) { - line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Could not create tox download /tmp/ directory."); + line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Could not create tox download /tmp/ directory.\n", err); } // end make tmpdir if it does not exist