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