Subject: Re: bug#7487: 24.0.50; Gnus nnimap broken

From: Daiki Ueno <[email-masked]>
Date: Wed, 15 Dec 2010 11:06:42 +0900

Daiki Ueno <[email-masked]> writes:

> If you want to simply skip the key selection, I'd suggest to do:
>
> (make-local-variable 'epa-file-encrypt-to)
> (setq epa-file-encrypt-to nil)
>
> in the "authinfo.gpg" buffer (see epa-file-write-region).

Find below a proposed patch to auth-source.el.

BTW, I forgot to mention in the previous response, I think it overkill
to make epa-file to encrypt files with symmetric encryption by default,
because as long as a file is visited in a buffer, epa-file remembers the
last used encryption method (in epa-file-encrypt-to local variable),
which will be used on the next save-buffer.  So a user should see the
key selection UI only the first time she saves the buffer.

Default symmetric might be useful when Emacs does visit/save/kill-buffer
repeatedly, but I think it is a rare case.

=== modified file 'lisp/gnus/auth-source.el'
--- lisp/gnus/auth-source.el	2010-10-10 22:48:40 +0000
+++ lisp/gnus/auth-source.el	2010-12-15 01:32:00 +0000
@@ -159,6 +159,15 @@
                                                      (const :tag "Any" t)
                                                      (string :tag "Specific user name"))))))))
 
+(defcustom auth-source-gpg-encrypt-to t
+  "List of recipient keys that `authinfo.gpg' encrypted to.
+If the value is not a list, symmetric encryption will be used."
+  :group 'auth-source
+  :version "23.2" ;; No Gnus
+  :type '(choice (const :tag "Symmetric encryption" t)
+		 (repeat :tag "Recipient public keys"
+			 (string :tag "Recipient public key"))))
+
 ;; temp for debugging
 ;; (unintern 'auth-source-protocols)
 ;; (unintern 'auth-sources)
@@ -352,9 +361,28 @@
       ;; netrc interface.
       (when (y-or-n-p (format "Do you want to save this password in %s? "
                               source))
-        (netrc-store-data source host prot
-                          (or user (cdr (assoc "login" result)))
-                          (cdr (assoc "password" result))))))
+	;; the code below is almost same as `netrc-store-data' except
+	;; the `epa-file-encrypt-to' hack (see bug#7487).
+	(with-temp-buffer
+	  (when (file-exists-p source)
+	    (insert-file-contents source))
+	  (when auth-source-gpg-encrypt-to
+	    ;; making `epa-file-encrypt-to' local to this buffer lets
+	    ;; epa-file skip the key selection query (see the
+	    ;; `local-variable-p' check in `epa-file-write-region').
+	    (unless (local-variable-p 'epa-file-encrypt-to)
+	      (make-local-variable 'epa-file-encrypt-to))
+	    (if (listp auth-source-gpg-encrypt-to)
+		(setq epa-file-encrypt-to auth-source-gpg-encrypt-to)))
+	  (goto-char (point-max))
+	  (unless (bolp)
+	    (insert "\n"))
+	  (insert (format "machine %s login %s password %s port %s\n"
+			  host
+			  (or user (cdr (assoc "login" result)))
+			  (cdr (assoc "password" result))
+			  prot))
+	  (write-region (point-min) (point-max) source nil 'silent)))))
     (if (consp mode)
         (mapcar #'cdr result)
       (cdar result))))

Regards,
-- 
Daiki Ueno