Merge from emacs-24; up to 2012-12-06T01:39:03Z!monnier@iro.umontreal.ca
[bpt/emacs.git] / lisp / net / netrc.el
CommitLineData
97b913ad 1;;; netrc.el --- .netrc parsing functionality
ab422c4d 2;; Copyright (C) 1996-2013 Free Software Foundation, Inc.
97b913ad
RS
3
4;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5;; Keywords: news
88f4758e 6;;
97b913ad
RS
7;; Modularized by Ted Zlatanov <tzz@lifelogs.com>
8;; when it was part of Gnus.
9
10;; This file is part of GNU Emacs.
11
874a927a 12;; GNU Emacs is free software: you can redistribute it and/or modify
97b913ad 13;; it under the terms of the GNU General Public License as published by
874a927a
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
97b913ad
RS
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
874a927a 19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
97b913ad
RS
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
874a927a 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
97b913ad
RS
24
25;;; Commentary:
26
27;; Just the .netrc parsing functionality, abstracted so other packages
28;; besides Gnus can use it.
29
30;;; Code:
31
32;;;
01c52d31 33;;; .netrc and .authinfo rc parsing
97b913ad
RS
34;;;
35
01c52d31
MB
36(defgroup netrc nil
37 "Netrc configuration."
38 :group 'comm)
39
a9ec34f4
LMI
40(defcustom netrc-file "~/.authinfo"
41 "File where user credentials are stored."
2bed3f04 42 :version "24.1"
a9ec34f4
LMI
43 :type 'file
44 :group 'netrc)
45
01c52d31
MB
46(defvar netrc-services-file "/etc/services"
47 "The name of the services file.")
97b913ad 48
7410c270
G
49(defvar netrc-cache nil)
50
a9ec34f4 51(defun netrc-parse (&optional file)
01c52d31 52 (interactive "fFile to Parse: ")
8f7abae3 53 "Parse FILE and return a list of all entries in the file."
a9ec34f4
LMI
54 (unless file
55 (setq file netrc-file))
5b51650c 56 (if (listp file)
7410c270 57 ;; We got already parsed contents; just return it.
5b51650c
MB
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"))
5b51650c 64 alist elem result pair)
7410c270
G
65 (if (and netrc-cache
66 (equal (car netrc-cache) (nth 5 (file-attributes file))))
7410c270
G
67 (insert (base64-decode-string (rot13-string (cdr netrc-cache))))
68 (insert-file-contents file)
69 (when (string-match "\\.gpg\\'" file)
06b840e0 70 ;; Store the contents of the file heavily encrypted in memory.
7410c270
G
71 (setq netrc-cache (cons (nth 5 (file-attributes file))
72 (rot13-string
73 (base64-encode-string
74 (buffer-string)))))))
5b51650c
MB
75 (goto-char (point-min))
76 ;; Go through the file, line by line.
97b913ad 77 (while (not (eobp))
5b51650c
MB
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)))
106 (t
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))))))
97b913ad
RS
119
120(defun netrc-machine (list machine &optional port defaultport)
121 "Return the netrc values from LIST for MACHINE or for the default entry.
122If PORT specified, only return entries with matching port tokens.
123Entries 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))
6b958814
G
134 (let ((elem (car rest)))
135 (setq elem (delete (assoc "default" elem) elem))
136 (push elem result)))
97b913ad
RS
137 (pop rest)))
138 (when result
139 (setq result (nreverse result))
6b958814
G
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)))))
97b913ad 151
01c52d31
MB
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.
154Matches a machine from MACHINES and a port from PORTS, giving
155default ports DEFAULTS to `netrc-machine'.
156
157MODE 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)
3b36c17e 165 (if (listp mode)
c9fc72fa
LMI
166 (setq info
167 (mapcar
168 (lambda (mode-element)
3b36c17e
MB
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)))))))
01c52d31
MB
181 info))
182
97b913ad
RS
183(defun netrc-get (alist type)
184 "Return the value of token TYPE from ALIST."
185 (cdr (assoc type alist)))
186
01c52d31
MB
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))
193
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)))))
208
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))
cb11d614 215 (eq type (car (cddr service)))))))
01c52d31
MB
216 (car service)))
217
20a673b2 218;;;###autoload
a9ec34f4
LMI
219(defun netrc-credentials (machine &rest ports)
220 "Return a user name/password pair.
fa463103 221Port specifications will be prioritized in the order they are
a9ec34f4
LMI
222listed in the PORTS list."
223 (let ((list (netrc-parse))
224 found)
6b958814
G
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)))))
a9ec34f4
LMI
230 (when found
231 (list (cdr (assoc "login" found))
232 (cdr (assoc "password" found))))))
233
97b913ad
RS
234(provide 'netrc)
235
236;;; netrc.el ends here