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