Function: auth-source-netrc-parse

auth-source-netrc-parse is a byte-compiled function defined in auth-source.el.gz.

Signature

(auth-source-netrc-parse &key FILE MAX HOST USER PORT REQUIRE &allow-other-keys)

Documentation

Parse FILE and return a list of all entries in the file.

Note that the MAX parameter is used so we can exit the parse early.

Source Code

;; Defined in /usr/src/emacs/lisp/auth-source.el.gz
;; (auth-source-netrc-parse :file "~/.authinfo.gpg")
(cl-defun auth-source-netrc-parse (&key file max host user port require
                                   &allow-other-keys)
  "Parse FILE and return a list of all entries in the file.
Note that the MAX parameter is used so we can exit the parse early."
  (if (listp file)
      ;; We got already parsed contents; just return it.
      file
    (when (file-exists-p file)
      (setq port (auth-source-ensure-strings port))
      (with-temp-buffer
        (let* ((max (or max 5000))       ; sanity check: default to stop at 5K
               (modified 0)
               (cached (cdr-safe (assoc file auth-source-netrc-cache)))
               (cached-mtime (plist-get cached :mtime))
               (cached-secrets (plist-get cached :secret))
               (check (lambda(alist)
                        (and alist
                             (auth-source-search-collection
                              host
                              (or
                               (auth-source--aget alist "machine")
                               (auth-source--aget alist "host")
                               t))
                             (auth-source-search-collection
                              user
                              (or
                               (auth-source--aget alist "login")
                               (auth-source--aget alist "account")
                               (auth-source--aget alist "user")
                               t))
                             (auth-source-search-collection
                              port
                              (or
                               (auth-source--aget alist "port")
                               (auth-source--aget alist "protocol")
                               t))
                             (or
                              ;; the required list of keys is nil, or
                              (null require)
                              ;; every element of require is in n (normalized)
                              (let ((n (nth 0 (auth-source-netrc-normalize
                                               (list alist) file))))
                                (cl-loop for req in require
                                         always (plist-get n req)))))))
               result)

          (if (and (functionp cached-secrets)
                   (equal cached-mtime
                          (file-attribute-modification-time
                           (file-attributes file))))
              (progn
                (auth-source-do-trivia
                 "auth-source-netrc-parse: using CACHED file data for %s"
                 file)
                (insert (funcall cached-secrets)))
            (insert-file-contents file)
            ;; cache all netrc files (used to be just .gpg files)
            ;; Store the contents of the file obfuscated in memory.
            (auth-source--aput
             auth-source-netrc-cache file
             (list :mtime (file-attribute-modification-time
                           (file-attributes file))
                   :secret (let ((v (auth-source--obfuscate (buffer-string))))
                             (lambda () (auth-source--deobfuscate v))))))
          (goto-char (point-min))
          (let ((entries (auth-source-netrc-parse-entries check max))
                alist)
            (while (setq alist (pop entries))
                (push (nreverse alist) result)))

          (when (< 0 modified)
            (when auth-source-gpg-encrypt-to
              ;; (see bug#7487) 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 (current-buffer))
                (make-local-variable 'epa-file-encrypt-to))
              (if (listp auth-source-gpg-encrypt-to)
                  (setq epa-file-encrypt-to auth-source-gpg-encrypt-to)))

            ;; ask AFTER we've successfully opened the file
            (when (y-or-n-p (format "Save file %s? (%d deletions)"
                                    file modified))
              (write-region (point-min) (point-max) file nil 'silent)
              (auth-source-do-debug
               "auth-source-netrc-parse: modified %d lines in %s"
               modified file)))

          (nreverse result))))))