forked from JFreegman/toxic
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Trying to add BETTER fopen function #407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
TheChymera
wants to merge
1
commit into
TokTok:master
Choose a base branch
from
TheChymera:fopen2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−3
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,9 +7,14 @@ | |
| */ | ||
|
|
||
| #include "chat_commands.h" | ||
|
|
||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <stdio.h> | ||
| #include <dirent.h> | ||
| #include <unistd.h> | ||
| #include <sys/types.h> | ||
| #include <sys/wait.h> | ||
| #include <fcntl.h> // for open(), O_RDWR | ||
|
|
||
| #include "chat.h" | ||
| #include "conference.h" | ||
|
|
@@ -24,6 +29,23 @@ | |
|
|
||
| extern FriendsList Friends; | ||
|
|
||
| void run_detached(const char *cmd, const char *arg); | ||
| void run_detached(const char *cmd, const char *arg) { | ||
| if (fork() == 0) { | ||
| setsid(); | ||
| int fd = open("/dev/null", O_RDWR); | ||
| if (fd != -1) { | ||
| dup2(fd, STDIN_FILENO); | ||
| dup2(fd, STDOUT_FILENO); | ||
| dup2(fd, STDERR_FILENO); | ||
| if (fd > 2) close(fd); | ||
| } | ||
| execlp(cmd, cmd, arg, (char *)NULL); | ||
| _exit(127); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| void cmd_autoaccept_files(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) | ||
| { | ||
| UNUSED_VAR(window); | ||
|
|
@@ -582,6 +604,88 @@ | |
| } | ||
| } | ||
|
|
||
| line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s", errmsg); | ||
| line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "%s",errmsg); | ||
| tox_file_control(tox, self->num, filenum, TOX_FILE_CONTROL_CANCEL, NULL); | ||
| } | ||
|
|
||
| void cmd_fopen(WINDOW *window, ToxWindow *self, Toxic *toxic, int argc, char (*argv)[MAX_STR_SIZE]) | ||
| { | ||
| UNUSED_VAR(window); | ||
| const Client_Config *c_config = toxic->c_config; | ||
| Tox *tox = toxic->tox; | ||
|
|
||
| if (argc < 1) { | ||
| line_info_add(self, c_config, 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, c_config, 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, c_config, 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, c_config, 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, toxic, 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; | ||
| } | ||
|
|
||
| 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, c_config, 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; | ||
|
|
||
| run_detached("xdg-open", ft->file_path); | ||
|
|
||
| return; | ||
|
|
||
| on_recv_error: | ||
|
|
||
| switch (err) { | ||
| case TOX_ERR_FILE_CONTROL_FRIEND_NOT_FOUND: | ||
| line_info_add(self, c_config, 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, c_config, 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, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed: Invalid filenumber."); | ||
| return; | ||
|
|
||
| case TOX_ERR_FILE_CONTROL_SENDQ: | ||
| line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed: Connection error."); | ||
| return; | ||
|
|
||
| default: | ||
| line_info_add(self, c_config, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed (error %d)\n", err); | ||
| return; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This switch should be extracted to its own function (instead of goto). Also the default case string has an extra new line. |
||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it really make sense to start an ft and immediately open the in-progress file transfer?