* spam.el (spam-point-at-eol): Replace with point-at-eol.
[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 (defun netrc-parse (file)
38   "Parse FILE and return an list of all entries in the file."
39   (when (file-exists-p file)
40     (with-temp-buffer
41       (let ((tokens '("machine" "default" "login"
42                       "password" "account" "macdef" "force"
43                       "port"))
44             alist elem result pair)
45         (insert-file-contents file)
46         (goto-char (point-min))
47         ;; Go through the file, line by line.
48         (while (not (eobp))
49           (narrow-to-region (point) (point-at-eol))
50           ;; For each line, get the tokens and values.
51           (while (not (eobp))
52             (skip-chars-forward "\t ")
53             ;; Skip lines that begin with a "#".
54             (if (eq (char-after) ?#)
55                 (goto-char (point-max))
56               (unless (eobp)
57                 (setq elem
58                       (if (= (following-char) ?\")
59                           (read (current-buffer))
60                         (buffer-substring
61                          (point) (progn (skip-chars-forward "^\t ")
62                                         (point)))))
63                 (cond
64                  ((equal elem "macdef")
65                   ;; We skip past the macro definition.
66                   (widen)
67                   (while (and (zerop (forward-line 1))
68                               (looking-at "$")))
69                   (narrow-to-region (point) (point)))
70                  ((member elem tokens)
71                   ;; Tokens that don't have a following value are ignored,
72                   ;; except "default".
73                   (when (and pair (or (cdr pair)
74                                       (equal (car pair) "default")))
75                     (push pair alist))
76                   (setq pair (list elem)))
77                  (t
78                   ;; Values that haven't got a preceding token are ignored.
79                   (when pair
80                     (setcdr pair elem)
81                     (push pair alist)
82                     (setq pair nil)))))))
83           (when alist
84             (push (nreverse alist) result))
85           (setq alist nil
86                 pair nil)
87           (widen)
88           (forward-line 1))
89         (nreverse result)))))
90
91 (defun netrc-machine (list machine &optional port defaultport)
92   "Return the netrc values from LIST for MACHINE or for the default entry.
93 If PORT specified, only return entries with matching port tokens.
94 Entries without port tokens default to DEFAULTPORT."
95   (let ((rest list)
96         result)
97     (while list
98       (when (equal (cdr (assoc "machine" (car list))) machine)
99         (push (car list) result))
100       (pop list))
101     (unless result
102       ;; No machine name matches, so we look for default entries.
103       (while rest
104         (when (assoc "default" (car rest))
105           (push (car rest) result))
106         (pop rest)))
107     (when result
108       (setq result (nreverse result))
109       (while (and result
110                   (not (equal (or port defaultport "nntp")
111                               (or (netrc-get (car result) "port")
112                                   defaultport "nntp"))))
113         (pop result))
114       (car result))))
115
116 (defun netrc-get (alist type)
117   "Return the value of token TYPE from ALIST."
118   (cdr (assoc type alist)))
119
120 (provide 'netrc)
121
122 ;;; netrc.el ends here