/* prints a progress bar for file transfers. if friendnum is -1 we're sending the file, otherwise we're receiving. */ void print_progress_bar(ToxWindow *self, double bps, double pct_done, uint32_t line_id) { if (bps < 0 || pct_done < 0 || pct_done > 100) return; char msg[MAX_STR_SIZE]; bytes_convert_str(msg, sizeof(msg), bps); strcat(msg, "/s ["); int n = pct_done / (100 / NUM_PROG_MARKS); int i, j; for (i = 0; i < n; ++i) strcat(msg, "#"); for (j = i; j < NUM_PROG_MARKS; ++j) strcat(msg, "-"); strcat(msg, "] "); char pctstr[16]; const char *frmt = pct_done == 100 ? "%.f%%" : "%.1f%%"; snprintf(pctstr, sizeof(pctstr), frmt, pct_done); strcat(msg, pctstr); line_info_set(self, line_id, msg); }
void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { if (max_file_senders_index >= (MAX_FILES - 1)) { const char *errmsg = "Please wait for some of your outgoing file transfers to complete."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); return; } if (argc < 1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File path required."); return; } if (argv[1][0] != '\"') { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File path must be enclosed in quotes."); return; } /* remove opening and closing quotes */ char path[MAX_STR_SIZE]; snprintf(path, sizeof(path), "%s", &argv[1][1]); int path_len = strlen(path) - 1; path[path_len] = '\0'; if (path_len >= MAX_STR_SIZE) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File path exceeds character limit."); return; } FILE *file_to_send = fopen(path, "r"); if (file_to_send == NULL) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File not found."); return; } off_t filesize = file_size(path); if (filesize == -1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File corrupt."); fclose(file_to_send); return; } char filename[MAX_STR_SIZE] = {0}; get_file_name(filename, sizeof(filename), path); int namelen = strlen(filename); int filenum = tox_new_file_sender(m, self->num, filesize, (const uint8_t *) filename, namelen); if (filenum == -1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Error sending file."); fclose(file_to_send); return; } int i; for (i = 0; i < MAX_FILES; ++i) { if (!file_senders[i].active) { memcpy(file_senders[i].filename, filename, namelen + 1); file_senders[i].active = true; file_senders[i].toxwin = self; file_senders[i].file = file_to_send; file_senders[i].filenum = filenum; file_senders[i].friendnum = self->num; file_senders[i].timestamp = get_unix_time(); file_senders[i].size = filesize; file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1, tox_file_data_size(m, self->num), file_to_send); char sizestr[32]; bytes_convert_str(sizestr, sizeof(sizestr), filesize); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Sending file [%d]: '%s' (%s)", filenum, filename, sizestr); ++num_active_file_senders; if (i == max_file_senders_index) ++max_file_senders_index; reset_file_sender_queue(); return; } } }
void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { const char *errmsg = NULL; if (argc < 1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File path required."); return; } if (argv[1][0] != '\"') { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File path must be enclosed in quotes."); return; } /* remove opening and closing quotes */ char path[MAX_STR_SIZE]; snprintf(path, sizeof(path), "%s", &argv[1][1]); int path_len = strlen(path) - 1; path[path_len] = '\0'; if (path_len >= MAX_STR_SIZE) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File path exceeds character limit."); return; } FILE *file_to_send = fopen(path, "r"); if (file_to_send == NULL) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File not found."); return; } off_t filesize = file_size(path); if (filesize == 0) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid file."); fclose(file_to_send); return; } char file_name[TOX_MAX_FILENAME_LENGTH]; size_t namelen = get_file_name(file_name, sizeof(file_name), path); TOX_ERR_FILE_SEND err; uint32_t filenum = tox_file_send(m, self->num, TOX_FILE_KIND_DATA, (uint64_t) filesize, NULL, (uint8_t *) file_name, namelen, &err); if (err != TOX_ERR_FILE_SEND_OK) goto on_send_error; struct FileTransfer *ft = new_file_transfer(self, self->num, filenum, FILE_TRANSFER_SEND, TOX_FILE_KIND_DATA); if (!ft) { err = TOX_ERR_FILE_SEND_TOO_MANY; goto on_send_error; } memcpy(ft->file_name, file_name, namelen + 1); ft->file = file_to_send; ft->file_size = filesize; tox_file_get_file_id(m, self->num, filenum, ft->file_id, NULL); char sizestr[32]; bytes_convert_str(sizestr, sizeof(sizestr), filesize); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Sending file [%d]: '%s' (%s)", filenum, file_name, sizestr); return; on_send_error: switch (err) { case TOX_ERR_FILE_SEND_FRIEND_NOT_FOUND: errmsg = "File transfer failed: Invalid friend."; break; case TOX_ERR_FILE_SEND_FRIEND_NOT_CONNECTED: errmsg = "File transfer failed: Friend is offline."; break; case TOX_ERR_FILE_SEND_NAME_TOO_LONG: errmsg = "File transfer failed: Filename is too long."; break; case TOX_ERR_FILE_SEND_TOO_MANY: errmsg = "File transfer failed: Too many concurrent file transfers."; break; default: errmsg = "File transfer failed."; break; } line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", errmsg); tox_file_control(m, self->num, filenum, TOX_FILE_CONTROL_CANCEL, NULL); fclose(file_to_send); }