* gnus-registry.el (gnus-registry-split-fancy-with-parent): try
[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 .authinfo rc parsing
35 ;;;
36
37 ;; autoload gnus-encrypt
38 (eval-and-compile
39   (autoload 'gnus-encrypt-find-model "gnus-encrypt")
40   (autoload 'gnus-encrypt-insert-file-contents "gnus-encrypt"))
41
42 (defgroup netrc nil
43  "Netrc configuration.")
44
45 (defvar netrc-services-file "/etc/services"
46   "The name of the services file.")
47
48 (defun netrc-parse (file)
49   (interactive "fFile to Parse: ")
50   "Parse FILE and return an list of all entries in the file."
51   (when (file-exists-p file)
52     (with-temp-buffer
53       (let ((tokens '("machine" "default" "login"
54                       "password" "account" "macdef" "force"
55                       "port"))
56             (encryption-model (gnus-encrypt-find-model file))
57             alist elem result pair)
58
59         (if encryption-model
60             (gnus-encrypt-insert-file-contents file encryption-model)
61           (insert-file-contents file))
62
63         (goto-char (point-min))
64         ;; Go through the file, line by line.
65         (while (not (eobp))
66           (narrow-to-region (point) (point-at-eol))
67           ;; For each line, get the tokens and values.
68           (while (not (eobp))
69             (skip-chars-forward "\t ")
70             ;; Skip lines that begin with a "#".
71             (if (eq (char-after) ?#)
72                 (goto-char (point-max))
73               (unless (eobp)
74                 (setq elem
75                       (if (= (following-char) ?\")
76                           (read (current-buffer))
77                         (buffer-substring
78                          (point) (progn (skip-chars-forward "^\t ")
79                                         (point)))))
80                 (cond
81                  ((equal elem "macdef")
82                   ;; We skip past the macro definition.
83                   (widen)
84                   (while (and (zerop (forward-line 1))
85                               (looking-at "$")))
86                   (narrow-to-region (point) (point)))
87                  ((member elem tokens)
88                   ;; Tokens that don't have a following value are ignored,
89                   ;; except "default".
90                   (when (and pair (or (cdr pair)
91                                       (equal (car pair) "default")))
92                     (push pair alist))
93                   (setq pair (list elem)))
94                  (t
95                   ;; Values that haven't got a preceding token are ignored.
96                   (when pair
97                     (setcdr pair elem)
98                     (push pair alist)
99                     (setq pair nil)))))))
100           (when alist
101             (push (nreverse alist) result))
102           (setq alist nil
103                 pair nil)
104           (widen)
105           (forward-line 1))
106         (nreverse result)))))
107
108 (defun netrc-machine (list machine &optional port defaultport)
109   "Return the netrc values from LIST for MACHINE or for the default entry.
110 If PORT specified, only return entries with matching port tokens.
111 Entries without port tokens default to DEFAULTPORT."
112   (let ((rest list)
113         result)
114     (while list
115       (when (equal (cdr (assoc "machine" (car list))) machine)
116         (push (car list) result))
117       (pop list))
118     (unless result
119       ;; No machine name matches, so we look for default entries.
120       (while rest
121         (when (assoc "default" (car rest))
122           (push (car rest) result))
123         (pop rest)))
124     (when result
125       (setq result (nreverse result))
126       (while (and result
127                   (not (netrc-port-equal
128                         (or port defaultport "nntp")
129                         (or (netrc-get (car result) "port")
130                             defaultport "nntp"))))
131         (pop result))
132       (car result))))
133
134 (defun netrc-get (alist type)
135   "Return the value of token TYPE from ALIST."
136   (cdr (assoc type alist)))
137
138 (defun netrc-port-equal (port1 port2)
139   (when (numberp port1)
140     (setq port1 (or (netrc-find-service-name port1) port1)))
141   (when (numberp port2)
142     (setq port2 (or (netrc-find-service-name port2) port2)))
143   (equal port1 port2))
144
145 (defun netrc-parse-services ()
146   (when (file-exists-p netrc-services-file)
147     (let ((services nil))
148       (with-temp-buffer
149         (insert-file-contents netrc-services-file)
150         (while (search-forward "#" nil t)
151           (delete-region (1- (point)) (line-end-position)))
152         (goto-char (point-min))
153         (while (re-search-forward
154                 "^ *\\([^ \n\t]+\\)[ \t]+\\([0-9]+\\)/\\([^ \t\n]+\\)" nil t)
155           (push (list (match-string 1) (string-to-number (match-string 2))
156                       (intern (downcase (match-string 3))))
157                 services))
158         (nreverse services)))))
159
160 (defun netrc-find-service-name (number &optional type)
161   (let ((services (netrc-parse-services))
162         service)
163     (setq type (or type 'tcp))
164     (while (and (setq service (pop services))
165                 (not (and (= number (cadr service))
166                           (eq type (caddr service)))))
167       )
168     (car service)))
169
170 (defun netrc-find-service-number (name &optional type)
171   (let ((services (netrc-parse-services))
172         service)
173     (setq type (or type 'tcp))
174     (while (and (setq service (pop services))
175                 (not (and (string= name (car service))
176                           (eq type (caddr service)))))
177       )
178     (cadr service)))
179
180 (provide 'netrc)
181
182 ;;; arch-tag: af9929cc-2d12-482f-936e-eb4366f9fa55
183 ;;; netrc.el ends here