Merge from emacs--devo--0
[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 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
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
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
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 ;;;
33 ;;; .netrc and .authinfo rc parsing
34 ;;;
35
36 ;; use encrypt if loaded (encrypt-file-alist has to be set as well)
37 (autoload 'encrypt-find-model "encrypt")
38 (autoload 'encrypt-insert-file-contents "encrypt")
39 (defalias 'netrc-point-at-eol
40   (if (fboundp 'point-at-eol)
41       'point-at-eol
42     'line-end-position))
43 (defvar encrypt-file-alist)
44 (eval-when-compile
45   ;; This is unnecessary in the compiled version as it is a macro.
46   (if (fboundp 'bound-and-true-p)
47       (defalias 'netrc-bound-and-true-p 'bound-and-true-p)
48     (defmacro netrc-bound-and-true-p (var)
49       "Return the value of symbol VAR if it is bound, else nil."
50       `(and (boundp (quote ,var)) ,var))))
51
52 (defgroup netrc nil
53  "Netrc configuration."
54  :group 'comm)
55
56 (defvar netrc-services-file "/etc/services"
57   "The name of the services file.")
58
59 (defun netrc-parse (file)
60   (interactive "fFile to Parse: ")
61   "Parse FILE and return a list of all entries in the file."
62   (when (file-exists-p file)
63     (with-temp-buffer
64       (let ((tokens '("machine" "default" "login"
65                       "password" "account" "macdef" "force"
66                       "port"))
67             (encryption-model (when (netrc-bound-and-true-p encrypt-file-alist)
68                                 (encrypt-find-model file)))
69             alist elem result pair)
70         (if encryption-model
71             (encrypt-insert-file-contents file encryption-model)
72           (insert-file-contents file))
73         (goto-char (point-min))
74         ;; Go through the file, line by line.
75         (while (not (eobp))
76           (narrow-to-region (point) (point-at-eol))
77           ;; For each line, get the tokens and values.
78           (while (not (eobp))
79             (skip-chars-forward "\t ")
80             ;; Skip lines that begin with a "#".
81             (if (eq (char-after) ?#)
82                 (goto-char (point-max))
83               (unless (eobp)
84                 (setq elem
85                       (if (= (following-char) ?\")
86                           (read (current-buffer))
87                         (buffer-substring
88                          (point) (progn (skip-chars-forward "^\t ")
89                                         (point)))))
90                 (cond
91                  ((equal elem "macdef")
92                   ;; We skip past the macro definition.
93                   (widen)
94                   (while (and (zerop (forward-line 1))
95                               (looking-at "$")))
96                   (narrow-to-region (point) (point)))
97                  ((member elem tokens)
98                   ;; Tokens that don't have a following value are ignored,
99                   ;; except "default".
100                   (when (and pair (or (cdr pair)
101                                       (equal (car pair) "default")))
102                     (push pair alist))
103                   (setq pair (list elem)))
104                  (t
105                   ;; Values that haven't got a preceding token are ignored.
106                   (when pair
107                     (setcdr pair elem)
108                     (push pair alist)
109                     (setq pair nil)))))))
110           (when alist
111             (push (nreverse alist) result))
112           (setq alist nil
113                 pair nil)
114           (widen)
115           (forward-line 1))
116         (nreverse result)))))
117
118 (defun netrc-machine (list machine &optional port defaultport)
119   "Return the netrc values from LIST for MACHINE or for the default entry.
120 If PORT specified, only return entries with matching port tokens.
121 Entries without port tokens default to DEFAULTPORT."
122   (let ((rest list)
123         result)
124     (while list
125       (when (equal (cdr (assoc "machine" (car list))) machine)
126         (push (car list) result))
127       (pop list))
128     (unless result
129       ;; No machine name matches, so we look for default entries.
130       (while rest
131         (when (assoc "default" (car rest))
132           (push (car rest) result))
133         (pop rest)))
134     (when result
135       (setq result (nreverse result))
136       (while (and result
137                   (not (netrc-port-equal
138                         (or port defaultport "nntp")
139                         ;; when port is not given in the netrc file,
140                         ;; it should mean "any port"
141                         (or (netrc-get (car result) "port")
142                             defaultport port))))
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