- (interactive "fFile to write: ")
- (let* ((model (or model (encrypt-find-model file)))
- (method (nth 0 model))
- (cipher (nth 1 model))
- (password-key (format "encrypt-password-%s-%s %s"
- (symbol-name method) cipher file))
- (passphrase
- (password-read
- (format "%s password for cipher %s? "
- (symbol-name method) cipher)
- password-key))
- outdata)
-
- (cond
- ((eq method 'gpg)
- (setq outdata (encrypt-gpg-encode-buffer passphrase cipher)))
- ((eq method 'encrypt-xor)
- (setq outdata (encrypt-xor-encode-buffer passphrase cipher))))
-
- (if outdata
- (progn
- (gnus-message 9 "%s was encrypted with %s (cipher %s)"
- file (symbol-name method) cipher)
- (delete-region (point-min) (point-max))
- (goto-char (point-min))
- (insert outdata)
- ;; do not confirm overwrites
- (write-file file nil))
- ;; the decryption failed, alas
- (password-cache-remove password-key)
- (gnus-error 5 "%s was NOT encrypted with %s (cipher %s)"
- file (symbol-name method) cipher))))
+ (interactive "sFile to write: ")
+ (setq model (or model (encrypt-find-model file)))
+ (if model
+ (let* ((method (nth 0 model))
+ (cipher (nth 1 model))
+ (passphrase
+ (encrypt-get-passphrase-if-needed file method cipher))
+ (outdata
+ (cond
+ ((eq method 'gpg)
+ (encrypt-gpg-encode-buffer passphrase cipher))
+ ((eq method 'pgg)
+ (encrypt-pgg-encode-buffer))
+ ((eq method 'encrypt-xor)
+ (encrypt-xor-encode-buffer passphrase cipher)))))
+
+ (if outdata
+ (progn
+ (message "%s was encrypted with %s"
+ file
+ (encrypt-message-method-and-cipher method cipher))
+ (with-temp-buffer
+ (insert outdata)
+ ;; do not confirm overwrites
+ (write-file file nil)))
+ ;; the decryption failed, alas
+ (password-cache-remove (encrypt-password-key file method cipher))
+ (gnus-error 5 "%s was NOT encrypted with %s"
+ file
+ (encrypt-message-method-and-cipher method cipher))))
+ (gnus-error
+ 1
+ "%s has no associated encryption model! See encrypt-file-alist."
+ file)))
+
+(defun encrypt-password-key (file method cipher)
+ (format "encrypt-password-%s-%s %s" (symbol-name method) cipher file))
+
+(defun encrypt-get-passphrase-if-needed (file method cipher &optional add)
+ "Read the passphrase for FILE, METHOD, CIPHER if necessary."
+ (when (not (eq method 'pgg))
+ (let ((password-key (encrypt-password-key file method cipher))
+ (password-question
+ (format "password for %s (file %s)? "
+ (encrypt-message-method-and-cipher method cipher)
+ file)))
+ (if add
+ (password-read-and-add password-question password-key)
+ (password-read password-question password-key)))))
+
+
+(defun encrypt-message-method-and-cipher (method cipher)
+ (format "method %s%s"
+ (symbol-name method)
+ (if cipher (format " (cipher %s)" cipher) "")))