Do smarter auth-source creation if needed and still work without creation.
authorTed Zlatanov <tzz@lifelogs.com>
Wed, 16 Feb 2011 18:22:36 +0000 (12:22 -0600)
committerTed Zlatanov <tzz@lifelogs.com>
Wed, 16 Feb 2011 18:22:36 +0000 (12:22 -0600)
* nnimap.el (nnimap-credentials): Remove the `inhibit-create'
parameter.  Create entry if necessary by using :create t.
(nnimap-open-connection-1): Don't pass `inhibit-create'.

* auth-source.el (auth-source-netrc-create): Return a synthetic search
result when the user doesn't want to write to the file.
(auth-source-netrc-search): Expect a synthetic result and proceed
accordingly.

lisp/ChangeLog
lisp/auth-source.el
lisp/nnimap.el

index 8e0c467..2577934 100644 (file)
@@ -1,3 +1,14 @@
+2011-02-16  Teodor Zlatanov  <tzz@lifelogs.com>
+
+       * auth-source.el (auth-source-netrc-create): Return a synthetic search
+       result when the user doesn't want to write to the file.
+       (auth-source-netrc-search): Expect a synthetic result and proceed
+       accordingly.
+
+       * nnimap.el (nnimap-credentials): Remove the `inhibit-create'
+       parameter.  Create entry if necessary by using :create t.
+       (nnimap-open-connection-1): Don't pass `inhibit-create'.
+
 2011-02-15  Teodor Zlatanov  <tzz@lifelogs.com>
 
        * auth-source.el (auth-source-debug): Enable by default and don't
index a259c5c..29829eb 100644 (file)
@@ -808,14 +808,17 @@ See `auth-source-search' for details on SPEC."
     (when (and create
                (= 0 (length results)))
 
-      ;; create based on the spec
-      (apply (slot-value backend 'create-function) spec)
-      ;; turn off the :create key
-      (setq spec (plist-put spec :create nil))
-      ;; run the search again to get the updated data
-      ;; the result will be returned, even if the search fails
-      (setq results (apply 'auth-source-netrc-search spec)))
-
+      ;; create based on the spec and record the value
+      (setq results (or
+                     ;; if the user did not want to create the entry
+                     ;; in the file, it will be returned
+                     (apply (slot-value backend 'create-function) spec)
+                     ;; if not, we do the search again without :create
+                     ;; to get the updated data.
+
+                     ;; the result will be returned, even if the search fails
+                     (apply 'auth-source-netrc-search
+                            (plist-put spec :create nil)))))
     results))
 
 ;;; (auth-source-search :host "nonesuch" :type 'netrc :max 1 :create t)
@@ -833,7 +836,9 @@ See `auth-source-search' for details on SPEC."
          (file (oref backend source))
          (add "")
          ;; `valist' is an alist
-         valist)
+         valist
+         ;; `artificial' will be returned if no creation is needed
+         artificial)
 
     ;; only for base required elements (defined as function parameters):
     ;; fill in the valist with whatever data we may have from the search
@@ -902,6 +907,14 @@ See `auth-source-search' for details on SPEC."
                        nil nil default))
                      (t data))))
 
+        (when data
+          (setq artificial (plist-put artificial
+                                      (intern (concat ":" (symbol-name r)))
+                                      (if (eq r 'secret)
+                                          (lexical-let ((data data))
+                                            (lambda () data))
+                                        data))))
+
         ;; when r is not an empty string...
         (when (and (stringp data)
                    (< 0 (length data)))
@@ -935,14 +948,17 @@ See `auth-source-search' for details on SPEC."
       (goto-char (point-max))
 
       ;; ask AFTER we've successfully opened the file
-      (when (y-or-n-p (format "Add to file %s: line [%s]" file add))
-        (unless (bolp)
-          (insert "\n"))
-        (insert add "\n")
-        (write-region (point-min) (point-max) file nil 'silent)
-        (auth-source-do-debug
-         "auth-source-netrc-create: wrote 1 new line to %s"
-         file)))))
+      (if (y-or-n-p (format "Add to file %s: line [%s]" file add))
+          (progn
+            (unless (bolp)
+              (insert "\n"))
+            (insert add "\n")
+            (write-region (point-min) (point-max) file nil 'silent)
+            (auth-source-do-debug
+             "auth-source-netrc-create: wrote 1 new line to %s"
+             file)
+            nil)
+        (list artificial)))))
 
 ;;; Backend specific parsing: Secrets API backend
 
index a5a001f..4e220bc 100644 (file)
@@ -276,13 +276,11 @@ textual parts.")
     (push (current-buffer) nnimap-process-buffers)
     (current-buffer)))
 
-(defun nnimap-credentials (address ports &optional inhibit-create)
+(defun nnimap-credentials (address ports)
   (let* ((found (nth 0 (auth-source-search :max 1
                                            :host address
                                            :port ports
-                                           :create (if inhibit-create
-                                                       nil
-                                                     (null ports)))))
+                                           :create t)))
          (user (plist-get found :user))
          (secret (plist-get found :secret))
          (secret (if (functionp secret) (funcall secret) secret)))
@@ -389,7 +387,7 @@ textual parts.")
                                 (list
                                  (nnoo-current-server 'nnimap)
                                  nnimap-address)
-                                ports t))))
+                                ports))))
                  (setq nnimap-object nil)
                (let ((nnimap-inhibit-logging t))
                  (setq login-result