Fix up comment convention on the arch-tag lines.
[bpt/emacs.git] / lisp / net / netrc.el
CommitLineData
97b913ad 1;;; netrc.el --- .netrc parsing functionality
5fd6d89f 2;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
2f043267 3;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
97b913ad
RS
4
5;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6;; Keywords: news
7;; Modularized by Ted Zlatanov <tzz@lifelogs.com>
8;; when it was part of Gnus.
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
ceaeecb0 14;; the Free Software Foundation; either version 3, or (at your option)
97b913ad
RS
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
24;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25;; Boston, MA 02110-1301, USA.
97b913ad
RS
26
27;;; Commentary:
28
29;; Just the .netrc parsing functionality, abstracted so other packages
30;; besides Gnus can use it.
31
32;;; Code:
33
34;;;
01c52d31 35;;; .netrc and .authinfo rc parsing
97b913ad
RS
36;;;
37
87035689
MB
38;; use encrypt if loaded (encrypt-file-alist has to be set as well)
39(eval-and-compile
40 (autoload 'encrypt-find-model "encrypt")
41 (autoload 'encrypt-insert-file-contents "encrypt"))
97b913ad
RS
42(defalias 'netrc-point-at-eol
43 (if (fboundp 'point-at-eol)
44 'point-at-eol
45 'line-end-position))
87035689
MB
46(eval-when-compile
47 (defvar encrypt-file-alist)
48 ;; This is unnecessary in the compiled version as it is a macro.
49 (if (fboundp 'bound-and-true-p)
50 (defalias 'netrc-bound-and-true-p 'bound-and-true-p)
51 (defmacro netrc-bound-and-true-p (var)
52 "Return the value of symbol VAR if it is bound, else nil."
53 `(and (boundp (quote ,var)) ,var))))
01c52d31
MB
54
55(defgroup netrc nil
56 "Netrc configuration."
57 :group 'comm)
58
59(defvar netrc-services-file "/etc/services"
60 "The name of the services file.")
97b913ad
RS
61
62(defun netrc-parse (file)
01c52d31 63 (interactive "fFile to Parse: ")
8f7abae3 64 "Parse FILE and return a list of all entries in the file."
97b913ad
RS
65 (when (file-exists-p file)
66 (with-temp-buffer
67 (let ((tokens '("machine" "default" "login"
68 "password" "account" "macdef" "force"
69 "port"))
87035689
MB
70 (encryption-model (when (netrc-bound-and-true-p encrypt-file-alist)
71 (encrypt-find-model file)))
97b913ad 72 alist elem result pair)
87035689
MB
73 (if encryption-model
74 (encrypt-insert-file-contents file encryption-model)
75 (insert-file-contents file))
97b913ad
RS
76 (goto-char (point-min))
77 ;; Go through the file, line by line.
78 (while (not (eobp))
01c52d31 79 (narrow-to-region (point) (point-at-eol))
97b913ad
RS
80 ;; For each line, get the tokens and values.
81 (while (not (eobp))
82 (skip-chars-forward "\t ")
83 ;; Skip lines that begin with a "#".
84 (if (eq (char-after) ?#)
85 (goto-char (point-max))
86 (unless (eobp)
87 (setq elem
88 (if (= (following-char) ?\")
89 (read (current-buffer))
90 (buffer-substring
91 (point) (progn (skip-chars-forward "^\t ")
92 (point)))))
93 (cond
94 ((equal elem "macdef")
95 ;; We skip past the macro definition.
96 (widen)
97 (while (and (zerop (forward-line 1))
98 (looking-at "$")))
99 (narrow-to-region (point) (point)))
100 ((member elem tokens)
101 ;; Tokens that don't have a following value are ignored,
102 ;; except "default".
103 (when (and pair (or (cdr pair)
104 (equal (car pair) "default")))
105 (push pair alist))
106 (setq pair (list elem)))
107 (t
108 ;; Values that haven't got a preceding token are ignored.
109 (when pair
110 (setcdr pair elem)
111 (push pair alist)
112 (setq pair nil)))))))
113 (when alist
114 (push (nreverse alist) result))
115 (setq alist nil
116 pair nil)
117 (widen)
118 (forward-line 1))
119 (nreverse result)))))
120
121(defun netrc-machine (list machine &optional port defaultport)
122 "Return the netrc values from LIST for MACHINE or for the default entry.
123If PORT specified, only return entries with matching port tokens.
124Entries without port tokens default to DEFAULTPORT."
125 (let ((rest list)
126 result)
127 (while list
128 (when (equal (cdr (assoc "machine" (car list))) machine)
129 (push (car list) result))
130 (pop list))
131 (unless result
132 ;; No machine name matches, so we look for default entries.
133 (while rest
134 (when (assoc "default" (car rest))
135 (push (car rest) result))
136 (pop rest)))
137 (when result
138 (setq result (nreverse result))
139 (while (and result
01c52d31
MB
140 (not (netrc-port-equal
141 (or port defaultport "nntp")
142 (or (netrc-get (car result) "port")
143 defaultport "nntp"))))
97b913ad
RS
144 (pop result))
145 (car result))))
146
01c52d31
MB
147(defun netrc-machine-user-or-password (mode authinfo-file-or-list machines ports defaults)
148 "Get the user name or password according to MODE from AUTHINFO-FILE-OR-LIST.
149Matches a machine from MACHINES and a port from PORTS, giving
150default ports DEFAULTS to `netrc-machine'.
151
152MODE can be \"login\" or \"password\", suitable for passing to
153`netrc-get'."
154 (let ((authinfo-list (if (stringp authinfo-file-or-list)
155 (netrc-parse authinfo-file-or-list)
156 authinfo-file-or-list))
157 (ports (or ports '(nil)))
158 (defaults (or defaults '(nil)))
159 info)
160 (dolist (machine machines)
161 (dolist (default defaults)
162 (dolist (port ports)
163 (let ((alist (netrc-machine authinfo-list machine port default)))
164 (setq info (or (netrc-get alist mode) info))))))
165 info))
166
97b913ad
RS
167(defun netrc-get (alist type)
168 "Return the value of token TYPE from ALIST."
169 (cdr (assoc type alist)))
170
01c52d31
MB
171(defun netrc-port-equal (port1 port2)
172 (when (numberp port1)
173 (setq port1 (or (netrc-find-service-name port1) port1)))
174 (when (numberp port2)
175 (setq port2 (or (netrc-find-service-name port2) port2)))
176 (equal port1 port2))
177
178(defun netrc-parse-services ()
179 (when (file-exists-p netrc-services-file)
180 (let ((services nil))
181 (with-temp-buffer
182 (insert-file-contents netrc-services-file)
183 (while (search-forward "#" nil t)
184 (delete-region (1- (point)) (point-at-eol)))
185 (goto-char (point-min))
186 (while (re-search-forward
187 "^ *\\([^ \n\t]+\\)[ \t]+\\([0-9]+\\)/\\([^ \t\n]+\\)" nil t)
188 (push (list (match-string 1) (string-to-number (match-string 2))
189 (intern (downcase (match-string 3))))
190 services))
191 (nreverse services)))))
192
193(defun netrc-find-service-name (number &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 (= number (cadr service))
cb11d614 199 (eq type (car (cddr service)))))))
01c52d31
MB
200 (car service)))
201
202(defun netrc-find-service-number (name &optional type)
203 (let ((services (netrc-parse-services))
204 service)
205 (setq type (or type 'tcp))
206 (while (and (setq service (pop services))
207 (not (and (string= name (car service))
cb11d614 208 (eq type (car (cddr service)))))))
01c52d31
MB
209 (cadr service)))
210
97b913ad
RS
211(provide 'netrc)
212
cbee283d 213;; arch-tag: af9929cc-2d12-482f-936e-eb4366f9fa55
97b913ad 214;;; netrc.el ends here