Skip tests for json.c unless compiled with native JSON support.
[emacs.git] / lisp / net / netrc.el
blob46a93ee76b39ebd43f773d05132fbc0340b42bdc
1 ;;; netrc.el --- .netrc parsing functionality
2 ;; Copyright (C) 1996-2017 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6 ;;
7 ;; Modularized by Ted Zlatanov <tzz@lifelogs.com>
8 ;; when it was part of Gnus.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Just the .netrc parsing functionality, abstracted so other packages
28 ;; besides Gnus can use it.
30 ;;; Code:
32 ;;;
33 ;;; .netrc and .authinfo rc parsing
34 ;;;
36 (defgroup netrc nil
37 "Netrc configuration."
38 :group 'comm)
40 (defcustom netrc-file "~/.authinfo"
41 "File where user credentials are stored."
42 :version "24.1"
43 :type 'file
44 :group 'netrc)
46 (defvar netrc-services-file "/etc/services"
47 "The name of the services file.")
49 (defvar netrc-cache nil)
51 (defun netrc-parse (&optional file)
52 (interactive "fFile to Parse: ")
53 "Parse FILE and return a list of all entries in the file."
54 (unless file
55 (setq file netrc-file))
56 (if (listp file)
57 ;; We got already parsed contents; just return it.
58 file
59 (when (file-exists-p file)
60 (with-temp-buffer
61 (let ((tokens '("machine" "default" "login"
62 "password" "account" "macdef" "force"
63 "port"))
64 alist elem result pair)
65 (if (and netrc-cache
66 (equal (car netrc-cache) (nth 5 (file-attributes file))))
67 (insert (base64-decode-string (rot13-string (cdr netrc-cache))))
68 (insert-file-contents file)
69 (when (string-match "\\.gpg\\'" file)
70 ;; Store the contents of the file heavily encrypted in memory.
71 (setq netrc-cache (cons (nth 5 (file-attributes file))
72 (rot13-string
73 (base64-encode-string
74 (buffer-string)))))))
75 (goto-char (point-min))
76 ;; Go through the file, line by line.
77 (while (not (eobp))
78 (narrow-to-region (point) (point-at-eol))
79 ;; For each line, get the tokens and values.
80 (while (not (eobp))
81 (skip-chars-forward "\t ")
82 ;; Skip lines that begin with a "#".
83 (if (eq (char-after) ?#)
84 (goto-char (point-max))
85 (unless (eobp)
86 (setq elem
87 (if (= (following-char) ?\")
88 (read (current-buffer))
89 (buffer-substring
90 (point) (progn (skip-chars-forward "^\t ")
91 (point)))))
92 (cond
93 ((equal elem "macdef")
94 ;; We skip past the macro definition.
95 (widen)
96 (while (and (zerop (forward-line 1))
97 (looking-at "$")))
98 (narrow-to-region (point) (point)))
99 ((member elem tokens)
100 ;; Tokens that don't have a following value are ignored,
101 ;; except "default".
102 (when (and pair (or (cdr pair)
103 (equal (car pair) "default")))
104 (push pair alist))
105 (setq pair (list elem)))
107 ;; Values that haven't got a preceding token are ignored.
108 (when pair
109 (setcdr pair elem)
110 (push pair alist)
111 (setq pair nil)))))))
112 (when alist
113 (push (nreverse alist) result))
114 (setq alist nil
115 pair nil)
116 (widen)
117 (forward-line 1))
118 (nreverse result))))))
120 (defun netrc-machine (list machine &optional port defaultport)
121 "Return the netrc values from LIST for MACHINE or for the default entry.
122 If PORT specified, only return entries with matching port tokens.
123 Entries without port tokens default to DEFAULTPORT."
124 (let ((rest list)
125 result)
126 (while list
127 (when (equal (cdr (assoc "machine" (car list))) machine)
128 (push (car list) result))
129 (pop list))
130 (unless result
131 ;; No machine name matches, so we look for default entries.
132 (while rest
133 (when (assoc "default" (car rest))
134 (let ((elem (car rest)))
135 (setq elem (delete (assoc "default" elem) elem))
136 (push elem result)))
137 (pop rest)))
138 (when result
139 (setq result (nreverse result))
140 (if (not port)
141 (car result)
142 (while (and result
143 (not (netrc-port-equal
144 (or port defaultport "nntp")
145 ;; when port is not given in the netrc file,
146 ;; it should mean "any port"
147 (or (netrc-get (car result) "port")
148 defaultport port))))
149 (pop result))
150 (car result)))))
152 (defun netrc-machine-user-or-password (mode authinfo-file-or-list machines ports defaults)
153 "Get the user name or password according to MODE from AUTHINFO-FILE-OR-LIST.
154 Matches a machine from MACHINES and a port from PORTS, giving
155 default ports DEFAULTS to `netrc-machine'.
157 MODE can be \"login\" or \"password\", suitable for passing to
158 `netrc-get'."
159 (let ((authinfo-list (if (stringp authinfo-file-or-list)
160 (netrc-parse authinfo-file-or-list)
161 authinfo-file-or-list))
162 (ports (or ports '(nil)))
163 (defaults (or defaults '(nil)))
164 info)
165 (if (listp mode)
166 (setq info
167 (mapcar
168 (lambda (mode-element)
169 (netrc-machine-user-or-password
170 mode-element
171 authinfo-list
172 machines
173 ports
174 defaults))
175 mode))
176 (dolist (machine machines)
177 (dolist (default defaults)
178 (dolist (port ports)
179 (let ((alist (netrc-machine authinfo-list machine port default)))
180 (setq info (or (netrc-get alist mode) info)))))))
181 info))
183 (defun netrc-get (alist type)
184 "Return the value of token TYPE from ALIST."
185 (cdr (assoc type alist)))
187 (defun netrc-port-equal (port1 port2)
188 (when (numberp port1)
189 (setq port1 (or (netrc-find-service-name port1) port1)))
190 (when (numberp port2)
191 (setq port2 (or (netrc-find-service-name port2) port2)))
192 (equal port1 port2))
194 (defun netrc-parse-services ()
195 (when (file-exists-p netrc-services-file)
196 (let ((services nil))
197 (with-temp-buffer
198 (insert-file-contents netrc-services-file)
199 (while (search-forward "#" nil t)
200 (delete-region (1- (point)) (point-at-eol)))
201 (goto-char (point-min))
202 (while (re-search-forward
203 "^ *\\([^ \n\t]+\\)[ \t]+\\([0-9]+\\)/\\([^ \t\n]+\\)" nil t)
204 (push (list (match-string 1) (string-to-number (match-string 2))
205 (intern (downcase (match-string 3))))
206 services))
207 (nreverse services)))))
209 (defun netrc-find-service-name (number &optional type)
210 (let ((services (netrc-parse-services))
211 service)
212 (setq type (or type 'tcp))
213 (while (and (setq service (pop services))
214 (not (and (= number (cadr service))
215 (eq type (car (cddr service)))))))
216 (car service)))
218 ;;;###autoload
219 (defun netrc-credentials (machine &rest ports)
220 "Return a user name/password pair.
221 Port specifications will be prioritized in the order they are
222 listed in the PORTS list."
223 (let ((list (netrc-parse))
224 found)
225 (if (not ports)
226 (setq found (netrc-machine list machine))
227 (while (and ports
228 (not found))
229 (setq found (netrc-machine list machine (pop ports)))))
230 (when found
231 (list (cdr (assoc "login" found))
232 (cdr (assoc "password" found))))))
234 (provide 'netrc)
236 ;;; netrc.el ends here