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