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/chat_commands.c b/src/chat_commands.c index 5d4f43b4c..9e2ef9e68 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -20,6 +20,7 @@ * */ +#include #include #include @@ -335,6 +336,112 @@ 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; + } + + // make tmp_dir if it does not exist + /// 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) { + 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.\n", err); + } + // end make tmpdir if it does not exist + + + 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; + + // 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); + + 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: + + 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); 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 e3562b30b..72d1e7cf9 100644 --- a/src/execute.c +++ b/src/execute.c @@ -91,6 +91,7 @@ static struct cmd_func chat_commands[] = { #ifdef GAMES { "/play", cmd_game_join }, #endif + { "/fopen", cmd_fopen }, { "/savefile", cmd_savefile }, { "/sendfile", cmd_sendfile }, #ifdef AUDIO