1 ;;; netrc.el --- .netrc parsing functionality
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Modularized by Ted Zlatanov <tzz@lifelogs.com>
8 ;; when it was part of Gnus.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; Just the .netrc parsing functionality, abstracted so other packages
30 ;; besides Gnus can use it.
35 ;;; .netrc and .authinfo rc parsing
38 ;; use encrypt if loaded (encrypt-file-alist has to be set as well)
40 (autoload 'encrypt-find-model
"encrypt")
41 (autoload 'encrypt-insert-file-contents
"encrypt"))
42 (defalias 'netrc-point-at-eol
43 (if (fboundp 'point-at-eol
)
47 (defvar encrypt-file-alist
)
48 ;; This is unnecessary in the compiled version as it is a macro.
49 (if (fboundp 'bound-and-true-p
)
50 (defalias 'netrc-bound-and-true-p
'bound-and-true-p
)
51 (defmacro netrc-bound-and-true-p
(var)
52 "Return the value of symbol VAR if it is bound, else nil."
53 `(and (boundp (quote ,var
)) ,var
))))
56 "Netrc configuration."
59 (defvar netrc-services-file
"/etc/services"
60 "The name of the services file.")
62 (defun netrc-parse (file)
63 (interactive "fFile to Parse: ")
64 "Parse FILE and return an list of all entries in the file."
65 (when (file-exists-p file
)
67 (let ((tokens '("machine" "default" "login"
68 "password" "account" "macdef" "force"
70 (encryption-model (when (netrc-bound-and-true-p encrypt-file-alist
)
71 (encrypt-find-model file
)))
72 alist elem result pair
)
74 (encrypt-insert-file-contents file encryption-model
)
75 (insert-file-contents file
))
76 (goto-char (point-min))
77 ;; Go through the file, line by line.
79 (narrow-to-region (point) (point-at-eol))
80 ;; For each line, get the tokens and values.
82 (skip-chars-forward "\t ")
83 ;; Skip lines that begin with a "#".
84 (if (eq (char-after) ?
#)
85 (goto-char (point-max))
88 (if (= (following-char) ?
\")
89 (read (current-buffer))
91 (point) (progn (skip-chars-forward "^\t ")
94 ((equal elem
"macdef")
95 ;; We skip past the macro definition.
97 (while (and (zerop (forward-line 1))
99 (narrow-to-region (point) (point)))
100 ((member elem tokens
)
101 ;; Tokens that don't have a following value are ignored,
103 (when (and pair
(or (cdr pair
)
104 (equal (car pair
) "default")))
106 (setq pair
(list elem
)))
108 ;; Values that haven't got a preceding token are ignored.
112 (setq pair nil
)))))))
114 (push (nreverse alist
) result
))
119 (nreverse result
)))))
121 (defun netrc-machine (list machine
&optional port defaultport
)
122 "Return the netrc values from LIST for MACHINE or for the default entry.
123 If PORT specified, only return entries with matching port tokens.
124 Entries without port tokens default to DEFAULTPORT."
128 (when (equal (cdr (assoc "machine" (car list
))) machine
)
129 (push (car list
) result
))
132 ;; No machine name matches, so we look for default entries.
134 (when (assoc "default" (car rest
))
135 (push (car rest
) result
))
138 (setq result
(nreverse result
))
140 (not (netrc-port-equal
141 (or port defaultport
"nntp")
142 (or (netrc-get (car result
) "port")
143 defaultport
"nntp"))))
147 (defun netrc-machine-user-or-password (mode authinfo-file-or-list machines ports defaults
)
148 "Get the user name or password according to MODE from AUTHINFO-FILE-OR-LIST.
149 Matches a machine from MACHINES and a port from PORTS, giving
150 default ports DEFAULTS to `netrc-machine'.
152 MODE can be \"login\" or \"password\", suitable for passing to
154 (let ((authinfo-list (if (stringp authinfo-file-or-list
)
155 (netrc-parse authinfo-file-or-list
)
156 authinfo-file-or-list
))
157 (ports (or ports
'(nil)))
158 (defaults (or defaults
'(nil)))
160 (dolist (machine machines
)
161 (dolist (default defaults
)
163 (let ((alist (netrc-machine authinfo-list machine port default
)))
164 (setq info
(or (netrc-get alist mode
) info
))))))
167 (defun netrc-get (alist type
)
168 "Return the value of token TYPE from ALIST."
169 (cdr (assoc type alist
)))
171 (defun netrc-port-equal (port1 port2
)
172 (when (numberp port1
)
173 (setq port1
(or (netrc-find-service-name port1
) port1
)))
174 (when (numberp port2
)
175 (setq port2
(or (netrc-find-service-name port2
) port2
)))
178 (defun netrc-parse-services ()
179 (when (file-exists-p netrc-services-file
)
180 (let ((services nil
))
182 (insert-file-contents netrc-services-file
)
183 (while (search-forward "#" nil t
)
184 (delete-region (1- (point)) (point-at-eol)))
185 (goto-char (point-min))
186 (while (re-search-forward
187 "^ *\\([^ \n\t]+\\)[ \t]+\\([0-9]+\\)/\\([^ \t\n]+\\)" nil t
)
188 (push (list (match-string 1) (string-to-number (match-string 2))
189 (intern (downcase (match-string 3))))
191 (nreverse services
)))))
193 (defun netrc-find-service-name (number &optional type
)
194 (let ((services (netrc-parse-services))
196 (setq type
(or type
'tcp
))
197 (while (and (setq service
(pop services
))
198 (not (and (= number
(cadr service
))
199 (eq type
(car (cddr service
)))))))
202 (defun netrc-find-service-number (name &optional type
)
203 (let ((services (netrc-parse-services))
205 (setq type
(or type
'tcp
))
206 (while (and (setq service
(pop services
))
207 (not (and (string= name
(car service
))
208 (eq type
(car (cddr service
)))))))
213 ;;; arch-tag: af9929cc-2d12-482f-936e-eb4366f9fa55
214 ;;; netrc.el ends here