Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7810e66114 | |||
| b4be05c34d |
70
otp.c
70
otp.c
@@ -134,18 +134,23 @@ int main(int argc, char* argv[]) {
|
||||
// Load preferences first
|
||||
load_preferences();
|
||||
|
||||
// Check for piped input first (before any output)
|
||||
int is_pipe_mode = (argc == 1 && has_stdin_data());
|
||||
|
||||
// Check for OTP thumb drive on startup
|
||||
char otp_drive_path[512];
|
||||
if (detect_otp_thumb_drive(otp_drive_path, sizeof(otp_drive_path))) {
|
||||
printf("Detected OTP thumb drive: %s\n", otp_drive_path);
|
||||
printf("Using as default pads directory for this session.\n\n");
|
||||
// Only show messages in interactive/command mode, not pipe mode
|
||||
if (!is_pipe_mode) {
|
||||
printf("Detected OTP thumb drive: %s\n", otp_drive_path);
|
||||
printf("Using as default pads directory for this session.\n\n");
|
||||
}
|
||||
strncpy(current_pads_dir, otp_drive_path, sizeof(current_pads_dir) - 1);
|
||||
current_pads_dir[sizeof(current_pads_dir) - 1] = '\0';
|
||||
}
|
||||
|
||||
// Check for piped input
|
||||
if (argc == 1 && has_stdin_data()) {
|
||||
// No arguments but has piped data - enter pipe mode for interactive pad selection
|
||||
if (is_pipe_mode) {
|
||||
// No arguments but has piped data - enter pipe mode
|
||||
char* piped_text = read_stdin_text();
|
||||
if (piped_text) {
|
||||
int result = pipe_mode(argc, argv, piped_text);
|
||||
@@ -2409,36 +2414,37 @@ int pipe_mode(int argc, char* argv[], const char* piped_text) {
|
||||
(void)argc; // Suppress unused parameter warning
|
||||
(void)argv; // Suppress unused parameter warning
|
||||
|
||||
printf("Piped text received: \"%s\"\n\n", piped_text);
|
||||
|
||||
// List available pads for selection
|
||||
int pad_count = list_available_pads();
|
||||
if (pad_count == 0) {
|
||||
printf("No pads available. Generate a pad first.\n");
|
||||
// Check if we have a default pad configured
|
||||
char* default_pad = get_default_pad_path();
|
||||
if (default_pad) {
|
||||
// Verify the default pad exists and extract checksum
|
||||
if (access(default_pad, R_OK) == 0) {
|
||||
// Extract checksum from pad filename
|
||||
char* filename = strrchr(default_pad, '/');
|
||||
if (!filename) filename = default_pad;
|
||||
else filename++; // Skip the '/'
|
||||
|
||||
// Extract checksum (remove .pad extension)
|
||||
if (strlen(filename) >= 68 && strstr(filename, ".pad")) {
|
||||
char pad_checksum[65];
|
||||
strncpy(pad_checksum, filename, 64);
|
||||
pad_checksum[64] = '\0';
|
||||
|
||||
free(default_pad);
|
||||
|
||||
// Encrypt using the default pad (silent mode)
|
||||
return encrypt_text(pad_checksum, piped_text);
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(stderr, "Error: Default pad not found or invalid: %s\n", default_pad);
|
||||
free(default_pad);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Reopen stdin from the controlling terminal for interactive input
|
||||
FILE* tty = fopen("/dev/tty", "r");
|
||||
if (!tty) {
|
||||
printf("Error: Cannot open terminal for input\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("\nEnter pad selection (number, checksum, or prefix): ");
|
||||
fflush(stdout);
|
||||
|
||||
char pad_input[MAX_HASH_LENGTH];
|
||||
if (!fgets(pad_input, sizeof(pad_input), tty)) {
|
||||
printf("Error: Failed to read pad selection\n");
|
||||
fclose(tty);
|
||||
return 1;
|
||||
}
|
||||
pad_input[strcspn(pad_input, "\n")] = 0;
|
||||
fclose(tty);
|
||||
|
||||
// Encrypt the piped text
|
||||
return encrypt_text(pad_input, piped_text);
|
||||
fprintf(stderr, "Error: No default pad configured for pipe mode\n");
|
||||
fprintf(stderr, "Configure a default pad in ~/.otp/otp.conf\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Preferences management functions implementation
|
||||
|
||||
Reference in New Issue
Block a user