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