autoload ("open_filter_process", "syncproc"); autoload ("close_filter_process", "syncproc"); custom_variable ("GPG_Encrypt_Program", "gpg -c --batch --quiet -o - --passphrase-fd 0"); custom_variable ("GPG_Decrypt_Program", "gpg --decrypt --batch -o - --passphrase-fd 0"); static define check_is_encrypted (file) { variable ext = path_extname (file); if (ext == ".gpg") return 0; return -1; } static define read_mini_hidden (prompt, default) { if (default != NULL) { prompt += sprintf (" [** buffer default **]", default); } prompt += ":"; flush (prompt); variable s = ""; forever { variable ch = getkey (); if (ch == '\r') break; s += char (ch); } if ((s == "") and (default != NULL)) s = default; return s; } static define get_pass_phrase (file, use_blocal_phrase, set_blocal_phrase, confirm_phrase) { variable default_pass_phrase = NULL; file = path_basename (file); if (use_blocal_phrase or set_blocal_phrase and (0 == blocal_var_exists ("_gpg_pass_phrase"))) create_blocal_var ("_gpg_pass_phrase"); if (use_blocal_phrase) default_pass_phrase = get_blocal_var ("_gpg_pass_phrase"); forever { variable p = read_mini_hidden (sprintf ("Passphrase for %s", file), default_pass_phrase); if (p == "") return NULL; if ((default_pass_phrase == NULL) and confirm_phrase) { if (p != read_mini_hidden ("Confirm Passphrase", NULL)) { flush ("Confirmation failed. Try again"); sleep (1); continue; } } if (set_blocal_phrase and (p != default_pass_phrase)) { if (1 == get_y_or_n ("Save passphrase as buffer-default")) set_blocal_var (p, "_gpg_pass_phrase"); } return p; } } static define _write_encrypted_region (file, append) { if (append) return 0; variable i = check_is_encrypted (file); if (i == -1) return 0; variable p = get_pass_phrase (file, 1, 1, 1); if (p == "") return 0; variable txt = bufsubstr (); variable cmd = sprintf ("%s > %s", GPG_Encrypt_Program, file); variable fp = popen (cmd, "w"); if (fp == NULL) verror ("%s failed", cmd); if (orelse {(-1 == fputs (p + "\n", fp))} {(-1 == fputs (txt, fp))} {(0 != pclose (fp))}) verror ("write to %s failed", cmd); return 1; } static define write_encrypted_region (file) { return _write_encrypted_region (file, 0); } static define _insert_encrypted_file (file, use_blocal_phrase, set_blocal_phrase, confirm_phrase) { variable i = check_is_encrypted (file); if (i == -1) return 0; if (1 != file_status (file)) return 0; variable pid = open_filter_process (["/bin/sh", "-c", GPG_Decrypt_Program + " " + file], "."); send_process (pid, get_pass_phrase (file, use_blocal_phrase, set_blocal_phrase, confirm_phrase)); send_process (pid, "\n"); () = close_filter_process (pid); return 1; } static define insert_encrypted_file (file) { return _insert_encrypted_file (file, 0, 0, 0); } static define read_encrypted_file (file) { if (_insert_encrypted_file (file, 0, 1, 0)) { %create_blocal_var ("Auto_Compression_Mode"); return 1; } return 0; } add_to_hook ("_jed_insert_file_hooks", &insert_encrypted_file); add_to_hook ("_jed_read_file_hooks", &read_encrypted_file); append_to_hook ("_jed_write_region_hooks", &write_encrypted_region); %append_to_hook ("_jed_append_region_hooks", &append_e); static define encrypted_set_mode_hook (ext) { variable i, file; (file,,,) = getbuf_info (); i = check_is_encrypted (file); if (i != -1) { file = file[[0:strlen(file)-strlen(ext)-2]]; mode_hook (file_type (file)); return 1; } return 0; } add_to_hook ("_jed_set_mode_hooks", &encrypted_set_mode_hook);