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