1 ;;; netrc.el --- .netrc parsing functionality
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 ;; 2005 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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, 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."
46 (defvar netrc-services-file "/etc/services"
47 "The name of the services file.")
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)
54 (let ((tokens '("machine" "default" "login"
55 "password" "account" "macdef" "force"
57 (encryption-model (encrypt-find-model file))
58 alist elem result pair)
61 (encrypt-insert-file-contents file encryption-model)
62 (insert-file-contents file))
64 (goto-char (point-min))
65 ;; Go through the file, line by line.
67 (narrow-to-region (point) (point-at-eol))
68 ;; For each line, get the tokens and values.
70 (skip-chars-forward "\t ")
71 ;; Skip lines that begin with a "#".
72 (if (eq (char-after) ?#)
73 (goto-char (point-max))
76 (if (= (following-char) ?\")
77 (read (current-buffer))
79 (point) (progn (skip-chars-forward "^\t ")
82 ((equal elem "macdef")
83 ;; We skip past the macro definition.
85 (while (and (zerop (forward-line 1))
87 (narrow-to-region (point) (point)))
89 ;; Tokens that don't have a following value are ignored,
91 (when (and pair (or (cdr pair)
92 (equal (car pair) "default")))
94 (setq pair (list elem)))
96 ;; Values that haven't got a preceding token are ignored.
100 (setq pair nil)))))))
102 (push (nreverse alist) result))
107 (nreverse result)))))
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."
116 (when (equal (cdr (assoc "machine" (car list))) machine)
117 (push (car list) result))
120 ;; No machine name matches, so we look for default entries.
122 (when (assoc "default" (car rest))
123 (push (car rest) result))
126 (setq result (nreverse result))
128 (not (netrc-port-equal
129 (or port defaultport "nntp")
130 (or (netrc-get (car result) "port")
131 defaultport "nntp"))))
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'.
140 MODE can be \"login\" or \"password\", suitable for passing to
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)))
148 (dolist (machine machines)
149 (dolist (default defaults)
151 (let ((alist (netrc-machine authinfo-list machine port default)))
152 (setq info (or (netrc-get alist mode) info))))))
155 (defun netrc-get (alist type)
156 "Return the value of token TYPE from ALIST."
157 (cdr (assoc type alist)))
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)))
166 (defun netrc-parse-services ()
167 (when (file-exists-p netrc-services-file)
168 (let ((services nil))
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))))
179 (nreverse services)))))
181 (defun netrc-find-service-name (number &optional type)
182 (let ((services (netrc-parse-services))
184 (setq type (or type 'tcp))
185 (while (and (setq service (pop services))
186 (not (and (= number (cadr service))
187 (eq type (caddr service)))))
191 (defun netrc-find-service-number (name &optional type)
192 (let ((services (netrc-parse-services))
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)))))
203 ;;; netrc.el ends here