1 ;;; netrc.el --- .netrc parsing functionality
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Modularizer: Ted Zlatanov <tzz@lifelogs.com>
9 ;; This file is part of GNU Emacs.
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)
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.
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.
28 ;; Just the .netrc parsing functionality, abstracted so other packages
29 ;; besides Gnus can use it.
34 ;;; .netrc and .authinfo rc parsing
39 (autoload 'encrypt-find-model "encrypt")
40 (autoload 'encrypt-insert-file-contents "encrypt"))
43 "Netrc configuration.")
45 (defvar netrc-services-file "/etc/services"
46 "The name of the services file.")
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)
53 (let ((tokens '("machine" "default" "login"
54 "password" "account" "macdef" "force"
56 (encryption-model (encrypt-find-model file))
57 alist elem result pair)
60 (encrypt-insert-file-contents file encryption-model)
61 (insert-file-contents file))
63 (goto-char (point-min))
64 ;; Go through the file, line by line.
66 (narrow-to-region (point) (point-at-eol))
67 ;; For each line, get the tokens and values.
69 (skip-chars-forward "\t ")
70 ;; Skip lines that begin with a "#".
71 (if (eq (char-after) ?#)
72 (goto-char (point-max))
75 (if (= (following-char) ?\")
76 (read (current-buffer))
78 (point) (progn (skip-chars-forward "^\t ")
81 ((equal elem "macdef")
82 ;; We skip past the macro definition.
84 (while (and (zerop (forward-line 1))
86 (narrow-to-region (point) (point)))
88 ;; Tokens that don't have a following value are ignored,
90 (when (and pair (or (cdr pair)
91 (equal (car pair) "default")))
93 (setq pair (list elem)))
95 ;; Values that haven't got a preceding token are ignored.
101 (push (nreverse alist) result))
106 (nreverse result)))))
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."
115 (when (equal (cdr (assoc "machine" (car list))) machine)
116 (push (car list) result))
119 ;; No machine name matches, so we look for default entries.
121 (when (assoc "default" (car rest))
122 (push (car rest) result))
125 (setq result (nreverse result))
127 (not (netrc-port-equal
128 (or port defaultport "nntp")
129 (or (netrc-get (car result) "port")
130 defaultport "nntp"))))
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'.
139 MODE can be \"login\" or \"password\", suitable for passing to
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)))
147 (dolist (machine machines)
148 (dolist (default defaults)
150 (let ((alist (netrc-machine authinfo-list machine port default)))
151 (setq info (or (netrc-get alist mode) info))))))
154 (defun netrc-get (alist type)
155 "Return the value of token TYPE from ALIST."
156 (cdr (assoc type alist)))
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)))
165 (defun netrc-parse-services ()
166 (when (file-exists-p netrc-services-file)
167 (let ((services nil))
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))))
178 (nreverse services)))))
180 (defun netrc-find-service-name (number &optional type)
181 (let ((services (netrc-parse-services))
183 (setq type (or type 'tcp))
184 (while (and (setq service (pop services))
185 (not (and (= number (cadr service))
186 (eq type (caddr service)))))
190 (defun netrc-find-service-number (name &optional type)
191 (let ((services (netrc-parse-services))
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)))))
202 ;;; netrc.el ends here