* netrc.el: Bind encrypt-file-alist for Emacs 21 and XEmacs when compiling.
[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 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Modularizer: Ted Zlatanov <tzz@lifelogs.com>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
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 (eval-and-compile
39   (autoload 'encrypt-find-model "encrypt")
40   (autoload 'encrypt-insert-file-contents "encrypt"))
41 (defalias 'netrc-point-at-eol
42   (if (fboundp 'point-at-eol)
43       'point-at-eol
44     'line-end-position))
45 (eval-when-compile
46   (defvar encrypt-file-alist)
47   ;; This is unnecessary in the compiled version as it is a macro.
48   (if (fboundp 'bound-and-true-p)
49       (defalias 'netrc-bound-and-true-p 'bound-and-true-p)
50     (defmacro netrc-bound-and-true-p (var)
51       "Return the value of symbol VAR if it is bound, else nil."
52       `(and (boundp (quote ,var)) ,var))))
53
54 (defgroup netrc nil
55  "Netrc configuration."
56  :group 'comm)
57
58 (defvar netrc-services-file "/etc/services"
59   "The name of the services file.")
60
61 (defun netrc-parse (file)
62   (interactive "fFile to Parse: ")
63   "Parse FILE and return an list of all entries in the file."
64   (when (file-exists-p file)
65     (with-temp-buffer
66       (let ((tokens '("machine" "default" "login"
67                       "password" "account" "macdef" "force"
68                       "port"))
69             (encryption-model (when (netrc-bound-and-true-p encrypt-file-alist)
70                                 (encrypt-find-model file)))
71             alist elem result pair)
72         (if encryption-model
73             (encrypt-insert-file-contents file encryption-model)
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                         (or (netrc-get (car result) "port")
142                             defaultport "nntp"))))
143         (pop result))
144       (car result))))
145
146 (defun netrc-machine-user-or-password (mode authinfo-file-or-list machines ports defaults)
147   "Get the user name or password according to MODE from AUTHINFO-FILE-OR-LIST.
148 Matches a machine from MACHINES and a port from PORTS, giving
149 default ports DEFAULTS to `netrc-machine'.
150
151 MODE can be \"login\" or \"password\", suitable for passing to
152 `netrc-get'."
153   (let ((authinfo-list (if (stringp authinfo-file-or-list)
154                            (netrc-parse authinfo-file-or-list)
155                          authinfo-file-or-list))
156         (ports (or ports '(nil)))
157         (defaults (or defaults '(nil)))
158         info)
159     (dolist (machine machines)
160       (dolist (default defaults)
161         (dolist (port ports)
162           (let ((alist (netrc-machine authinfo-list machine port default)))
163           (setq info (or (netrc-get alist mode) info))))))
164     info))
165
166 (defun netrc-get (alist type)
167   "Return the value of token TYPE from ALIST."
168   (cdr (assoc type alist)))
169
170 (defun netrc-port-equal (port1 port2)
171   (when (numberp port1)
172     (setq port1 (or (netrc-find-service-name port1) port1)))
173   (when (numberp port2)
174     (setq port2 (or (netrc-find-service-name port2) port2)))
175   (equal port1 port2))
176
177 (defun netrc-parse-services ()
178   (when (file-exists-p netrc-services-file)
179     (let ((services nil))
180       (with-temp-buffer
181         (insert-file-contents netrc-services-file)
182         (while (search-forward "#" nil t)
183           (delete-region (1- (point)) (point-at-eol)))
184         (goto-char (point-min))
185         (while (re-search-forward
186                 "^ *\\([^ \n\t]+\\)[ \t]+\\([0-9]+\\)/\\([^ \t\n]+\\)" nil t)
187           (push (list (match-string 1) (string-to-number (match-string 2))
188                       (intern (downcase (match-string 3))))
189                 services))
190         (nreverse services)))))
191
192 (defun netrc-find-service-name (number &optional type)
193   (let ((services (netrc-parse-services))
194         service)
195     (setq type (or type 'tcp))
196     (while (and (setq service (pop services))
197                 (not (and (= number (cadr service))
198                           (eq type (car (cddr service)))))))
199     (car service)))
200
201 (defun netrc-find-service-number (name &optional type)
202   (let ((services (netrc-parse-services))
203         service)
204     (setq type (or type 'tcp))
205     (while (and (setq service (pop services))
206                 (not (and (string= name (car service))
207                           (eq type (car (cddr service)))))))
208     (cadr service)))
209
210 (provide 'netrc)
211
212 ;;; arch-tag: af9929cc-2d12-482f-936e-eb4366f9fa55
213 ;;; netrc.el ends here