(auth-source-netrc-saver): If the user says "don't ask again, save the choice via...
[gnus] / lisp / auth-source.el
index 21df923..3bbad49 100644 (file)
@@ -218,7 +218,7 @@ If the value is a function, debug messages are logged by calling
           (function :tag "Function that takes arguments like `message'")
           (const :tag "Don't log anything" nil)))
 
-(defcustom auth-sources '("~/.authinfo.gpg" "~/.authinfo" "~/.netrc")
+(defcustom auth-sources '("~/.authinfo" "~/.authinfo.gpg" "~/.netrc")
   "List of authentication sources.
 
 The default will get login and password information from
@@ -715,6 +715,8 @@ Returns the deleted entries."
       (equal collection value)
       (member value collection)))
 
+(defvar auth-source-netrc-cache nil)
+
 (defun auth-source-forget-all-cached ()
   "Forget all cached auth-source data."
   (interactive)
@@ -723,7 +725,8 @@ Returns the deleted entries."
         when (string-match (concat "^" auth-source-magic)
                            (symbol-name sym))
         ;; remove that key
-        do (password-cache-remove (symbol-name sym))))
+        do (password-cache-remove (symbol-name sym)))
+  (setq auth-source-netrc-cache nil))
 
 (defun auth-source-remember (spec found)
   "Remember FOUND search results for SPEC."
@@ -825,8 +828,6 @@ while \(:host t) would find all host entries."
 
 ;;; Backend specific parsing: netrc/authinfo backend
 
-(defvar auth-source-netrc-cache nil)
-
 ;;; (auth-source-netrc-parse "~/.authinfo.gpg")
 (defun* auth-source-netrc-parse (&rest
                                  spec
@@ -980,7 +981,9 @@ Note that the MAX parameter is used so we can exit the parse early."
          (,(if (boundp 'find-file-hook) 'find-file-hook 'find-file-hooks)
           ',(remove
              'epa-file-find-file-hook
-             ,(if (boundp 'find-file-hook) 'find-file-hook 'find-file-hooks)))
+             (if (boundp 'find-file-hook)
+                (symbol-value 'find-file-hook)
+              (symbol-value 'find-file-hooks))))
          (auto-mode-alist
           ',(if (boundp 'epa-file-auto-mode-alist-entry)
                 (remove (symbol-value 'epa-file-auto-mode-alist-entry)
@@ -1154,6 +1157,9 @@ See `auth-source-search' for details on SPEC."
          ;; we know (because of an assertion in auth-source-search) that the
          ;; :create parameter is either t or a list (which includes nil)
          (create-extra (if (eq t create) nil create))
+        (current-data (car (auth-source-search :max 1
+                                               :host host
+                                               :port port)))
          (required (append base-required create-extra))
          (file (oref backend source))
          (add "")
@@ -1188,7 +1194,9 @@ See `auth-source-search' for details on SPEC."
     (dolist (r required)
       (let* ((data (aget valist r))
              ;; take the first element if the data is a list
-             (data (auth-source-netrc-element-or-first data))
+             (data (or (auth-source-netrc-element-or-first data)
+                      (plist-get current-data
+                                 (intern (format ":%s" r) obarray))))
              ;; this is the default to be offered
              (given-default (aget auth-source-creation-defaults r))
              ;; the default supplementals are simple:
@@ -1291,7 +1299,7 @@ See `auth-source-search' for details on SPEC."
           (let ((printer (lambda ()
                            ;; append the key (the symbol name of r)
                            ;; and the value in r
-                           (format "%s%s %S"
+                           (format "%s%s %s"
                                    ;; prepend a space
                                    (if (zerop (length add)) "" " ")
                                    ;; remap auth-source tokens to netrc
@@ -1301,8 +1309,9 @@ See `auth-source-search' for details on SPEC."
                                      (secret "password")
                                      (port   "port") ; redundant but clearer
                                      (t (symbol-name r)))
-                                   ;; the value will be printed in %S format
-                                   data))))
+                                  (if (string-match "[\" ]" data)
+                                      (format "%S" data)
+                                    data)))))
             (setq add (concat add (funcall printer)))))))
 
     (plist-put
@@ -1364,9 +1373,10 @@ Respects `auth-source-save-behavior'.  Uses
                       (help-mode))))
               (?n (setq add ""
                         done t))
-              (?N (setq add ""
-                        done t
-                        auth-source-save-behavior nil))
+              (?N
+              (setq add ""
+                    done t)
+              (customize-save-variable 'auth-source-save-behavior nil))
               (?e (setq add (read-string "Line to add: " add)))
               (t nil)))