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