Fixing user interface problems

This commit is contained in:
2026-01-17 07:39:23 -04:00
parent 2e2f78720e
commit 1e017d81b7
6 changed files with 89 additions and 35 deletions

View File

@@ -10,12 +10,12 @@
#include <dirent.h>
#include <time.h>
#include "main.h"
#include "microtar/microtar.h"
#include "microtar.h"
// Suppress warnings from miniz header
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#include "miniz/miniz.h"
#include "miniz.h"
#pragma GCC diagnostic pop
////////////////////////////////////////////////////////////////////////////////

View File

@@ -1,10 +1,12 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "main.h"
// Global state variables
static char current_pads_dir[512] = DEFAULT_PADS_DIR;
static char current_pads_dir[512] = "";
static int is_interactive_mode = 0;
static int pads_dir_initialized = 0;
// Terminal dimensions (moved from ui.c to state.c for global access)
static int terminal_width = 80; // Default fallback width
@@ -13,6 +15,18 @@ static int terminal_height = 24; // Default fallback height
// Getters and setters for global state
const char* get_current_pads_dir(void) {
// Initialize pads directory on first access if not already set
if (!pads_dir_initialized && strlen(current_pads_dir) == 0) {
char* home_dir = getenv("HOME");
if (home_dir) {
snprintf(current_pads_dir, sizeof(current_pads_dir), "%s/.otp/pads", home_dir);
} else {
// Fallback to relative path if HOME is not set
strncpy(current_pads_dir, DEFAULT_PADS_DIR, sizeof(current_pads_dir) - 1);
}
current_pads_dir[sizeof(current_pads_dir) - 1] = '\0';
pads_dir_initialized = 1;
}
return current_pads_dir;
}
@@ -20,6 +34,7 @@ void set_current_pads_dir(const char* dir) {
if (dir) {
strncpy(current_pads_dir, dir, sizeof(current_pads_dir) - 1);
current_pads_dir[sizeof(current_pads_dir) - 1] = '\0';
pads_dir_initialized = 1;
}
}

View File

@@ -319,7 +319,33 @@ int handle_decrypt_menu(void) {
// Remove newline
input_line[strcspn(input_line, "\n")] = 0;
if (strlen(input_line) == 0) {
// Trim leading whitespace to handle pasted content better
char* trimmed_input = input_line;
while (*trimmed_input == ' ' || *trimmed_input == '\t') {
trimmed_input++;
}
// Check for ASCII armor FIRST, before checking for empty input
// This handles cases where pasted text starts with the header
if (strncmp(trimmed_input, "-----BEGIN OTP MESSAGE-----", 27) == 0) {
// Looks like ASCII armor - collect the full message
char full_message[MAX_INPUT_SIZE * 4] = {0};
strcat(full_message, input_line);
strcat(full_message, "\n");
printf("Continue pasting the message (end with -----END OTP MESSAGE-----):\n");
char line[MAX_LINE_LENGTH];
while (fgets(line, sizeof(line), stdin)) {
strncat(full_message, line, sizeof(full_message) - strlen(full_message) - 1);
if (strstr(line, "-----END OTP MESSAGE-----")) {
break;
}
}
return decrypt_text(NULL, full_message);
}
else if (strlen(trimmed_input) == 0) {
// Empty input - launch file manager to browse for files
char selected_file[512];
if (launch_file_manager(get_files_directory(), selected_file, sizeof(selected_file)) != 0) {
@@ -382,31 +408,13 @@ int handle_decrypt_menu(void) {
return decrypt_file(selected_file, output_file);
}
}
else if (strncmp(input_line, "-----BEGIN OTP MESSAGE-----", 27) == 0) {
// Looks like ASCII armor - collect the full message
char full_message[MAX_INPUT_SIZE * 4] = {0};
strcat(full_message, input_line);
strcat(full_message, "\n");
printf("Continue pasting the message (end with -----END OTP MESSAGE-----):\n");
char line[MAX_LINE_LENGTH];
while (fgets(line, sizeof(line), stdin)) {
strncat(full_message, line, sizeof(full_message) - strlen(full_message) - 1);
if (strstr(line, "-----END OTP MESSAGE-----")) {
break;
}
}
return decrypt_text(NULL, full_message);
}
else {
// Check if it looks like a file path
if (access(input_line, R_OK) == 0) {
if (access(trimmed_input, R_OK) == 0) {
// It's a valid file - decrypt it with enhanced input for output filename
char temp_default[512];
char default_output[512];
strncpy(temp_default, input_line, sizeof(temp_default) - 1);
strncpy(temp_default, trimmed_input, sizeof(temp_default) - 1);
temp_default[sizeof(temp_default) - 1] = '\0';
// Remove common encrypted extensions to get a better default
@@ -441,7 +449,7 @@ int handle_decrypt_menu(void) {
}
// Check if it's a directory archive
if (strstr(input_line, ".tar.gz.otp") || strstr(input_line, ".tar.otp")) {
if (strstr(trimmed_input, ".tar.gz.otp") || strstr(trimmed_input, ".tar.otp")) {
// It's a directory archive - extract to directory
char extract_dir[512];
strncpy(extract_dir, output_file, sizeof(extract_dir) - 1);
@@ -453,9 +461,9 @@ int handle_decrypt_menu(void) {
if (ext) *ext = '\0';
printf("Extracting directory archive to: %s/\n", extract_dir);
return decrypt_and_extract_directory(input_line, extract_dir);
return decrypt_and_extract_directory(trimmed_input, extract_dir);
} else {
return decrypt_file(input_line, output_file);
return decrypt_file(trimmed_input, output_file);
}
} else {
printf("Input not recognized as ASCII armor or valid file path.\n");

View File

@@ -19,6 +19,7 @@
// Global variables for preferences
static char default_pad_path[1024] = "";
static char pads_directory[1024] = "";
void show_progress(uint64_t current, uint64_t total, time_t start_time) {
time_t now = time(NULL);
@@ -506,6 +507,11 @@ int load_preferences(void) {
if (strcmp(key, "default_pad") == 0) {
strncpy(default_pad_path, value, sizeof(default_pad_path) - 1);
default_pad_path[sizeof(default_pad_path) - 1] = '\0';
} else if (strcmp(key, "pads_directory") == 0) {
strncpy(pads_directory, value, sizeof(pads_directory) - 1);
pads_directory[sizeof(pads_directory) - 1] = '\0';
// Apply the pads directory from config
set_current_pads_dir(pads_directory);
}
}
}
@@ -1109,6 +1115,12 @@ int save_preferences(void) {
fprintf(file, "# OTP Preferences File\n");
fprintf(file, "# This file is automatically generated and updated by the OTP program\n\n");
// Save pads directory
const char* current_pads = get_current_pads_dir();
if (current_pads && strlen(current_pads) > 0) {
fprintf(file, "pads_directory=%s\n", current_pads);
}
if (strlen(default_pad_path) > 0) {
fprintf(file, "default_pad=%s\n", default_pad_path);
}