Indent.
[gnus] / lisp / netrc.el
1 ;;; netrc.el --- .netrc parsing functionality
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Modularizer: Ted Zlatanov <tzz@lifelogs.com>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
28 ;; Just the .netrc parsing functionality, abstracted so other packages
29 ;; besides Gnus can use it.
30
31 ;;; Code:
32
33 ;;;
34 ;;; .netrc and .authinforc parsing
35 ;;;
36
37 (eval-and-compile
38   (defalias 'netrc-point-at-eol
39     (if (fboundp 'point-at-eol)
40         'point-at-eol
41       'line-end-position)))
42
43 (defun netrc-parse (file)
44   "Parse FILE and return an list of all entries in the file."
45   (when (file-exists-p file)
46     (with-temp-buffer
47       (let ((tokens '("machine" "default" "login"
48                       "password" "account" "macdef" "force"
49                       "port"))
50             alist elem result pair)
51         (insert-file-contents file)
52         (goto-char (point-min))
53         ;; Go through the file, line by line.
54         (while (not (eobp))
55           (narrow-to-region (point) (netrc-point-at-eol))
56           ;; For each line, get the tokens and values.
57           (while (not (eobp))
58             (skip-chars-forward "\t ")
59             ;; Skip lines that begin with a "#".
60             (if (eq (char-after) ?#)
61                 (goto-char (point-max))
62               (unless (eobp)
63                 (setq elem
64                       (if (= (following-char) ?\")
65                           (read (current-buffer))
66                         (buffer-substring
67                          (point) (progn (skip-chars-forward "^\t ")
68                                         (point)))))
69                 (cond
70                  ((equal elem "macdef")
71                   ;; We skip past the macro definition.
72                   (widen)
73                   (while (and (zerop (forward-line 1))
74                               (looking-at "$")))
75                   (narrow-to-region (point) (point)))
76                  ((member elem tokens)
77                   ;; Tokens that don't have a following value are ignored,
78                   ;; except "default".
79                   (when (and pair (or (cdr pair)
80                                       (equal (car pair) "default")))
81                     (push pair alist))
82                   (setq pair (list elem)))
83                  (t
84                   ;; Values that haven't got a preceding token are ignored.
85                   (when pair
86                     (setcdr pair elem)
87                     (push pair alist)
88                     (setq pair nil)))))))
89           (when alist
90             (push (nreverse alist) result))
91           (setq alist nil
92                 pair nil)
93           (widen)
94           (forward-line 1))
95         (nreverse result)))))
96
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."
101   (let ((rest list)
102         result)
103     (while list
104       (when (equal (cdr (assoc "machine" (car list))) machine)
105         (push (car list) result))
106       (pop list))
107     (unless result
108       ;; No machine name matches, so we look for default entries.
109       (while rest
110         (when (assoc "default" (car rest))
111           (push (car rest) result))
112         (pop rest)))
113     (when result
114       (setq result (nreverse result))
115       (while (and result
116                   (not (equal (or port defaultport "nntp")
117                               (or (netrc-get (car result) "port")
118                                   defaultport "nntp"))))
119         (pop result))
120       (car result))))
121
122 (defun netrc-get (alist type)
123   "Return the value of token TYPE from ALIST."
124   (cdr (assoc type alist)))
125
126 (provide 'netrc)
127
128 ;;; netrc.el ends here