Split -request-update-info into -request-marks and -update-info.
[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 (defalias 'netrc-point-at-eol
38   (if (fboundp 'point-at-eol)
39       'point-at-eol
40     'line-end-position))
41 (eval-when-compile
42   ;; This is unnecessary in the compiled version as it is a macro.
43   (if (fboundp 'bound-and-true-p)
44       (defalias 'netrc-bound-and-true-p 'bound-and-true-p)
45     (defmacro netrc-bound-and-true-p (var)
46       "Return the value of symbol VAR if it is bound, else nil."
47       `(and (boundp (quote ,var)) ,var))))
48
49 (defgroup netrc nil
50  "Netrc configuration."
51  :group 'comm)
52
53 (defcustom netrc-file "~/.authinfo"
54   "File where user credentials are stored."
55   :type 'file
56   :group 'netrc)
57
58 (defvar netrc-services-file "/etc/services"
59   "The name of the services file.")
60
61 (defun netrc-parse (&optional file)
62   (interactive "fFile to Parse: ")
63   "Parse FILE and return a list of all entries in the file."
64   (unless file
65     (setq file netrc-file))
66   (if (listp file)
67       file
68     (when (file-exists-p file)
69       (with-temp-buffer
70         (let ((tokens '("machine" "default" "login"
71                         "password" "account" "macdef" "force"
72                         "port"))
73               alist elem result pair)
74           (insert-file-contents file)
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)))
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))))))
119
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           (push (car rest) result))
135         (pop rest)))
136     (when result
137       (setq result (nreverse result))
138       (while (and result
139                   (not (netrc-port-equal
140                         (or port defaultport "nntp")
141                         ;; when port is not given in the netrc file,
142                         ;; it should mean "any port"
143                         (or (netrc-get (car result) "port")
144                             defaultport port))))
145         (pop result))
146       (car result))))
147
148 (defun netrc-machine-user-or-password (mode authinfo-file-or-list machines ports defaults)
149   "Get the user name or password according to MODE from AUTHINFO-FILE-OR-LIST.
150 Matches a machine from MACHINES and a port from PORTS, giving
151 default ports DEFAULTS to `netrc-machine'.
152
153 MODE can be \"login\" or \"password\", suitable for passing to
154 `netrc-get'."
155   (let ((authinfo-list (if (stringp authinfo-file-or-list)
156                            (netrc-parse authinfo-file-or-list)
157                          authinfo-file-or-list))
158         (ports (or ports '(nil)))
159         (defaults (or defaults '(nil)))
160         info)
161     (if (listp mode)
162         (setq info
163               (mapcar
164                (lambda (mode-element)
165                  (netrc-machine-user-or-password
166                   mode-element
167                   authinfo-list
168                   machines
169                   ports
170                   defaults))
171                mode))
172       (dolist (machine machines)
173         (dolist (default defaults)
174           (dolist (port ports)
175             (let ((alist (netrc-machine authinfo-list machine port default)))
176               (setq info (or (netrc-get alist mode) info)))))))
177     info))
178
179 (defun netrc-get (alist type)
180   "Return the value of token TYPE from ALIST."
181   (cdr (assoc type alist)))
182
183 (defun netrc-port-equal (port1 port2)
184   (when (numberp port1)
185     (setq port1 (or (netrc-find-service-name port1) port1)))
186   (when (numberp port2)
187     (setq port2 (or (netrc-find-service-name port2) port2)))
188   (equal port1 port2))
189
190 (defun netrc-parse-services ()
191   (when (file-exists-p netrc-services-file)
192     (let ((services nil))
193       (with-temp-buffer
194         (insert-file-contents netrc-services-file)
195         (while (search-forward "#" nil t)
196           (delete-region (1- (point)) (point-at-eol)))
197         (goto-char (point-min))
198         (while (re-search-forward
199                 "^ *\\([^ \n\t]+\\)[ \t]+\\([0-9]+\\)/\\([^ \t\n]+\\)" nil t)
200           (push (list (match-string 1) (string-to-number (match-string 2))
201                       (intern (downcase (match-string 3))))
202                 services))
203         (nreverse services)))))
204
205 (defun netrc-find-service-name (number &optional type)
206   (let ((services (netrc-parse-services))
207         service)
208     (setq type (or type 'tcp))
209     (while (and (setq service (pop services))
210                 (not (and (= number (cadr service))
211                           (eq type (car (cddr service)))))))
212     (car service)))
213
214 (defun netrc-find-service-number (name &optional type)
215   (let ((services (netrc-parse-services))
216         service)
217     (setq type (or type 'tcp))
218     (while (and (setq service (pop services))
219                 (not (and (string= name (car service))
220                           (eq type (car (cddr service)))))))
221     (cadr service)))
222
223 ;;;###autoload
224 (defun netrc-credentials (machine &rest ports)
225   "Return a user name/password pair.
226 Port specifications will be prioritised in the order they are
227 listed in the PORTS list."
228   (let ((list (netrc-parse))
229         found)
230     (while (and ports
231                 (not found))
232       (setq found (netrc-machine list machine (pop ports))))
233     (when found
234       (list (cdr (assoc "login" found))
235             (cdr (assoc "password" found))))))
236
237 (provide 'netrc)
238
239 ;;; netrc.el ends here