Delete all trailing white space.
[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 ;; use encrypt if loaded (encrypt-file-alist has to be set as well)
38 (autoload 'encrypt-find-model "encrypt")
39 (autoload 'encrypt-insert-file-contents "encrypt")
40 (defalias 'netrc-point-at-eol
41   (if (fboundp 'point-at-eol)
42       'point-at-eol
43     'line-end-position))
44 (defvar encrypt-file-alist)
45 (eval-when-compile
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))))
52
53 (defgroup netrc nil
54  "Netrc configuration."
55  :group 'comm)
56
57 (defvar netrc-services-file "/etc/services"
58   "The name of the services file.")
59
60 (defun netrc-parse (file)
61   (interactive "fFile to Parse: ")
62   "Parse FILE and return a list of all entries in the file."
63   (if (listp file)
64       file
65     (when (file-exists-p file)
66       (with-temp-buffer
67         (let ((tokens '("machine" "default" "login"
68                         "password" "account" "macdef" "force"
69                         "port"))
70               (encryption-model (when (netrc-bound-and-true-p encrypt-file-alist)
71                                   (encrypt-find-model file)))
72               alist elem result pair)
73           (if encryption-model
74               (encrypt-insert-file-contents file encryption-model)
75             (insert-file-contents file))
76           (goto-char (point-min))
77           ;; Go through the file, line by line.
78           (while (not (eobp))
79             (narrow-to-region (point) (point-at-eol))
80             ;; For each line, get the tokens and values.
81             (while (not (eobp))
82               (skip-chars-forward "\t ")
83               ;; Skip lines that begin with a "#".
84               (if (eq (char-after) ?#)
85                   (goto-char (point-max))
86                 (unless (eobp)
87                   (setq elem
88                         (if (= (following-char) ?\")
89                             (read (current-buffer))
90                           (buffer-substring
91                            (point) (progn (skip-chars-forward "^\t ")
92                                           (point)))))
93                   (cond
94                    ((equal elem "macdef")
95                     ;; We skip past the macro definition.
96                     (widen)
97                     (while (and (zerop (forward-line 1))
98                                 (looking-at "$")))
99                     (narrow-to-region (point) (point)))
100                    ((member elem tokens)
101                     ;; Tokens that don't have a following value are ignored,
102                     ;; except "default".
103                     (when (and pair (or (cdr pair)
104                                         (equal (car pair) "default")))
105                       (push pair alist))
106                     (setq pair (list elem)))
107                    (t
108                     ;; Values that haven't got a preceding token are ignored.
109                     (when pair
110                       (setcdr pair elem)
111                       (push pair alist)
112                       (setq pair nil)))))))
113             (when alist
114               (push (nreverse alist) result))
115             (setq alist nil
116                   pair nil)
117             (widen)
118             (forward-line 1))
119           (nreverse result))))))
120
121 (defun netrc-machine (list machine &optional port defaultport)
122   "Return the netrc values from LIST for MACHINE or for the default entry.
123 If PORT specified, only return entries with matching port tokens.
124 Entries without port tokens default to DEFAULTPORT."
125   (let ((rest list)
126         result)
127     (while list
128       (when (equal (cdr (assoc "machine" (car list))) machine)
129         (push (car list) result))
130       (pop list))
131     (unless result
132       ;; No machine name matches, so we look for default entries.
133       (while rest
134         (when (assoc "default" (car rest))
135           (push (car rest) result))
136         (pop rest)))
137     (when result
138       (setq result (nreverse result))
139       (while (and result
140                   (not (netrc-port-equal
141                         (or port defaultport "nntp")
142                         ;; when port is not given in the netrc file,
143                         ;; it should mean "any port"
144                         (or (netrc-get (car result) "port")
145                             defaultport port))))
146         (pop result))
147       (car result))))
148
149 (defun netrc-machine-user-or-password (mode authinfo-file-or-list machines ports defaults)
150   "Get the user name or password according to MODE from AUTHINFO-FILE-OR-LIST.
151 Matches a machine from MACHINES and a port from PORTS, giving
152 default ports DEFAULTS to `netrc-machine'.
153
154 MODE can be \"login\" or \"password\", suitable for passing to
155 `netrc-get'."
156   (let ((authinfo-list (if (stringp authinfo-file-or-list)
157                            (netrc-parse authinfo-file-or-list)
158                          authinfo-file-or-list))
159         (ports (or ports '(nil)))
160         (defaults (or defaults '(nil)))
161         info)
162     (if (listp mode)
163         (setq info
164               (mapcar
165                (lambda (mode-element)
166                  (netrc-machine-user-or-password
167                   mode-element
168                   authinfo-list
169                   machines
170                   ports
171                   defaults))
172                mode))
173       (dolist (machine machines)
174         (dolist (default defaults)
175           (dolist (port ports)
176             (let ((alist (netrc-machine authinfo-list machine port default)))
177               (setq info (or (netrc-get alist mode) info)))))))
178     info))
179
180 (defun netrc-get (alist type)
181   "Return the value of token TYPE from ALIST."
182   (cdr (assoc type alist)))
183
184 (defun netrc-port-equal (port1 port2)
185   (when (numberp port1)
186     (setq port1 (or (netrc-find-service-name port1) port1)))
187   (when (numberp port2)
188     (setq port2 (or (netrc-find-service-name port2) port2)))
189   (equal port1 port2))
190
191 (defun netrc-parse-services ()
192   (when (file-exists-p netrc-services-file)
193     (let ((services nil))
194       (with-temp-buffer
195         (insert-file-contents netrc-services-file)
196         (while (search-forward "#" nil t)
197           (delete-region (1- (point)) (point-at-eol)))
198         (goto-char (point-min))
199         (while (re-search-forward
200                 "^ *\\([^ \n\t]+\\)[ \t]+\\([0-9]+\\)/\\([^ \t\n]+\\)" nil t)
201           (push (list (match-string 1) (string-to-number (match-string 2))
202                       (intern (downcase (match-string 3))))
203                 services))
204         (nreverse services)))))
205
206 (defun netrc-find-service-name (number &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 (= number (cadr service))
212                           (eq type (car (cddr service)))))))
213     (car service)))
214
215 (defun netrc-find-service-number (name &optional type)
216   (let ((services (netrc-parse-services))
217         service)
218     (setq type (or type 'tcp))
219     (while (and (setq service (pop services))
220                 (not (and (string= name (car service))
221                           (eq type (car (cddr service)))))))
222     (cadr service)))
223
224 (provide 'netrc)
225
226 ;;; netrc.el ends here