1 ;;; netrc.el --- .netrc parsing functionality
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 ;; 2005, 2006, 2007 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 .authinforc parsing
38 (defalias 'netrc-point-at-eol
39 (if (fboundp 'point-at-eol
)
43 (defun netrc-parse (file)
44 "Parse FILE and return a list of all entries in the file."
45 (when (file-exists-p file
)
47 (let ((tokens '("machine" "default" "login"
48 "password" "account" "macdef" "force"
50 alist elem result pair
)
51 (insert-file-contents file
)
52 (goto-char (point-min))
53 ;; Go through the file, line by line.
55 (narrow-to-region (point) (netrc-point-at-eol))
56 ;; For each line, get the tokens and values.
58 (skip-chars-forward "\t ")
59 ;; Skip lines that begin with a "#".
60 (if (eq (char-after) ?
#)
61 (goto-char (point-max))
64 (if (= (following-char) ?
\")
65 (read (current-buffer))
67 (point) (progn (skip-chars-forward "^\t ")
70 ((equal elem
"macdef")
71 ;; We skip past the macro definition.
73 (while (and (zerop (forward-line 1))
75 (narrow-to-region (point) (point)))
77 ;; Tokens that don't have a following value are ignored,
79 (when (and pair
(or (cdr pair
)
80 (equal (car pair
) "default")))
82 (setq pair
(list elem
)))
84 ;; Values that haven't got a preceding token are ignored.
90 (push (nreverse alist
) result
))
97 (defun netrc-machine (list machine
&optional port defaultport
)
98 "Return the netrc values from LIST for MACHINE or for the default entry.
99 If PORT specified, only return entries with matching port tokens.
100 Entries without port tokens default to DEFAULTPORT."
104 (when (equal (cdr (assoc "machine" (car list
))) machine
)
105 (push (car list
) result
))
108 ;; No machine name matches, so we look for default entries.
110 (when (assoc "default" (car rest
))
111 (push (car rest
) result
))
114 (setq result
(nreverse result
))
116 (not (equal (or port defaultport
"nntp")
117 (or (netrc-get (car result
) "port")
118 defaultport
"nntp"))))
122 (defun netrc-get (alist type
)
123 "Return the value of token TYPE from ALIST."
124 (cdr (assoc type alist
)))
128 ;;; arch-tag: af9929cc-2d12-482f-936e-eb4366f9fa55
129 ;;; netrc.el ends here