Remove arch-tags from all files, since these are no longer needed.
[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,
114f9c96 3;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
97b913ad
RS
4
5;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6;; Keywords: news
88f4758e 7;;
97b913ad
RS
8;; Modularized by Ted Zlatanov <tzz@lifelogs.com>
9;; when it was part of Gnus.
10
11;; This file is part of GNU Emacs.
12
874a927a 13;; GNU Emacs is free software: you can redistribute it and/or modify
97b913ad 14;; it under the terms of the GNU General Public License as published by
874a927a
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
97b913ad
RS
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
874a927a 20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
97b913ad
RS
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
874a927a 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
97b913ad
RS
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;;;
01c52d31 34;;; .netrc and .authinfo rc parsing
97b913ad
RS
35;;;
36
87035689 37;; use encrypt if loaded (encrypt-file-alist has to be set as well)
91ed8a56
GM
38(autoload 'encrypt-find-model "encrypt")
39(autoload 'encrypt-insert-file-contents "encrypt")
97b913ad
RS
40(defalias 'netrc-point-at-eol
41 (if (fboundp 'point-at-eol)
42 'point-at-eol
43 'line-end-position))
91ed8a56 44(defvar encrypt-file-alist)
87035689 45(eval-when-compile
87035689
MB
46 ;; This is unnecessary in the compiled version as it is a macro.
47 (if (fboundp 'bound-and-true-p)
48 (defalias 'netrc-bound-and-true-p 'bound-and-true-p)
49 (defmacro netrc-bound-and-true-p (var)
50 "Return the value of symbol VAR if it is bound, else nil."
51 `(and (boundp (quote ,var)) ,var))))
01c52d31
MB
52
53(defgroup netrc nil
54 "Netrc configuration."
55 :group 'comm)
56
57(defvar netrc-services-file "/etc/services"
58 "The name of the services file.")
97b913ad
RS
59
60(defun netrc-parse (file)
01c52d31 61 (interactive "fFile to Parse: ")
8f7abae3 62 "Parse FILE and return a list of all entries in the file."
5b51650c
MB
63 (if (listp file)
64 file
65 (when (file-exists-p file)
66 (with-temp-buffer
67 (let ((tokens '("machine" "default" "login"
68 "password" "account" "macdef" "force"
69 "port"))
70 (encryption-model (when (netrc-bound-and-true-p encrypt-file-alist)
71 (encrypt-find-model file)))
72 alist elem result pair)
73 (if encryption-model
74 (encrypt-insert-file-contents file encryption-model)
75 (insert-file-contents file))
76 (goto-char (point-min))
77 ;; Go through the file, line by line.
97b913ad 78 (while (not (eobp))
5b51650c
MB
79 (narrow-to-region (point) (point-at-eol))
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))))))
97b913ad
RS
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")
a368801c
MB
142 ;; when port is not given in the netrc file,
143 ;; it should mean "any port"
01c52d31 144 (or (netrc-get (car result) "port")
a368801c 145 defaultport port))))
97b913ad
RS
146 (pop result))
147 (car result))))
148
01c52d31
MB
149(defun netrc-machine-user-or-password (mode authinfo-file-or-list machines ports defaults)
150 "Get the user name or password according to MODE from AUTHINFO-FILE-OR-LIST.
151Matches a machine from MACHINES and a port from PORTS, giving
152default ports DEFAULTS to `netrc-machine'.
153
154MODE can be \"login\" or \"password\", suitable for passing to
155`netrc-get'."
156 (let ((authinfo-list (if (stringp authinfo-file-or-list)
157 (netrc-parse authinfo-file-or-list)
158 authinfo-file-or-list))
159 (ports (or ports '(nil)))
160 (defaults (or defaults '(nil)))
161 info)
3b36c17e
MB
162 (if (listp mode)
163 (setq info
164 (mapcar
165 (lambda (mode-element)
166 (netrc-machine-user-or-password
167 mode-element
168 authinfo-list
169 machines
170 ports
171 defaults))
172 mode))
173 (dolist (machine machines)
174 (dolist (default defaults)
175 (dolist (port ports)
176 (let ((alist (netrc-machine authinfo-list machine port default)))
177 (setq info (or (netrc-get alist mode) info)))))))
01c52d31
MB
178 info))
179
97b913ad
RS
180(defun netrc-get (alist type)
181 "Return the value of token TYPE from ALIST."
182 (cdr (assoc type alist)))
183
01c52d31
MB
184(defun netrc-port-equal (port1 port2)
185 (when (numberp port1)
186 (setq port1 (or (netrc-find-service-name port1) port1)))
187 (when (numberp port2)
188 (setq port2 (or (netrc-find-service-name port2) port2)))
189 (equal port1 port2))
190
191(defun netrc-parse-services ()
192 (when (file-exists-p netrc-services-file)
193 (let ((services nil))
194 (with-temp-buffer
195 (insert-file-contents netrc-services-file)
196 (while (search-forward "#" nil t)
197 (delete-region (1- (point)) (point-at-eol)))
198 (goto-char (point-min))
199 (while (re-search-forward
200 "^ *\\([^ \n\t]+\\)[ \t]+\\([0-9]+\\)/\\([^ \t\n]+\\)" nil t)
201 (push (list (match-string 1) (string-to-number (match-string 2))
202 (intern (downcase (match-string 3))))
203 services))
204 (nreverse services)))))
205
206(defun netrc-find-service-name (number &optional type)
207 (let ((services (netrc-parse-services))
208 service)
209 (setq type (or type 'tcp))
210 (while (and (setq service (pop services))
211 (not (and (= number (cadr service))
cb11d614 212 (eq type (car (cddr service)))))))
01c52d31
MB
213 (car service)))
214
215(defun netrc-find-service-number (name &optional type)
216 (let ((services (netrc-parse-services))
217 service)
218 (setq type (or type 'tcp))
219 (while (and (setq service (pop services))
220 (not (and (string= name (car service))
cb11d614 221 (eq type (car (cddr service)))))))
01c52d31
MB
222 (cadr service)))
223
97b913ad
RS
224(provide 'netrc)
225
226;;; netrc.el ends here