;;; netrc.el --- .netrc parsing functionality
;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
;; Free Software Foundation, Inc.
;; Author: Lars Magne Ingebrigtsen <email@example.com>
;; Keywords: news
;; Modularized by Ted Zlatanov <firstname.lastname@example.org>
;; when it was part of Gnus.
;; This file is part of XEmacs.
;; XEmacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; XEmacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with XEmacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;; Just the .netrc parsing functionality, abstracted so other packages
;; besides Gnus can use it.
;;; .netrc and .authinforc parsing
(if (fboundp 'point-at-eol)
(defun netrc-parse (file)
"Parse FILE and return an list of all entries in the file."
(when (file-exists-p file)
(let ((tokens '("machine" "default" "login"
"password" "account" "macdef" "force"
alist elem result pair)
;; Go through the file, line by line.
(while (not (eobp))
(narrow-to-region (point) (netrc-point-at-eol))
;; For each line, get the tokens and values.
(while (not (eobp))
(skip-chars-forward "\t ")
;; Skip lines that begin with a "#".
(if (eq (char-after) ?#)
(if (= (following-char) ?\")
(point) (progn (skip-chars-forward "^\t ")
((equal elem "macdef")
;; We skip past the macro definition.
(while (and (zerop (forward-line 1))
(narrow-to-region (point) (point)))
((member elem tokens)
;; Tokens that don't have a following value are ignored,
;; except "default".
(when (and pair (or (cdr pair)
(equal (car pair) "default")))
(push pair alist))
(setq pair (list elem)))
;; Values that haven't got a preceding token are ignored.
(setcdr pair elem)
(push pair alist)
(setq pair nil)))))))
(push (nreverse alist) result))
(setq alist nil
(defun netrc-machine (list machine &optional port defaultport)
"Return the netrc values from LIST for MACHINE or for the default entry.
If PORT specified, only return entries with matching port tokens.
Entries without port tokens default to DEFAULTPORT."
(let ((rest list)
(when (equal (cdr (assoc "machine" (car list))) machine)
(push (car list) result))
;; No machine name matches, so we look for default entries.
(when (assoc "default" (car rest))
(push (car rest) result))
(setq result (nreverse result))
(while (and result
(not (equal (or port defaultport "nntp")
(or (netrc-get (car result) "port")
(defun netrc-get (alist type)
"Return the value of token TYPE from ALIST."
(cdr (assoc type alist)))
;;; netrc.el ends here