Mercurial > emacs
annotate lisp/net/netrc.el @ 104237:870c09c7e096
Move code that deals with adding/deleting
file/directory-local variables to files-x.el.
author | Juri Linkov <juri@jurta.org> |
---|---|
date | Wed, 12 Aug 2009 20:30:31 +0000 |
parents | d775b84fdd71 |
children | 1d1d5d9bd884 |
rev | line source |
---|---|
44810 | 1 ;;; netrc.el --- .netrc parsing functionality |
64701
34bd8e434dd7
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64085
diff
changeset
|
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
100908 | 3 ;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
44810 | 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 | |
94677
91e5880a36c1
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
12 ;; GNU Emacs is free software: you can redistribute it and/or modify |
44810 | 13 ;; it under the terms of the GNU General Public License as published by |
94677
91e5880a36c1
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
14 ;; the Free Software Foundation, either version 3 of the License, or |
91e5880a36c1
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
15 ;; (at your option) any later version. |
44810 | 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 | |
94677
91e5880a36c1
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
44810 | 20 ;; GNU General Public License for more details. |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
94677
91e5880a36c1
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
44810 | 24 |
25 ;;; Commentary: | |
26 | |
27 ;; Just the .netrc parsing functionality, abstracted so other packages | |
28 ;; besides Gnus can use it. | |
29 | |
30 ;;; Code: | |
31 | |
32 ;;; | |
85712
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
33 ;;; .netrc and .authinfo rc parsing |
44810 | 34 ;;; |
35 | |
87454 | 36 ;; use encrypt if loaded (encrypt-file-alist has to be set as well) |
95624
164c226d4a0d
Remove unnecessary eval-when-compiles and eval-and-compiles.
Glenn Morris <rgm@gnu.org>
parents:
95193
diff
changeset
|
37 (autoload 'encrypt-find-model "encrypt") |
164c226d4a0d
Remove unnecessary eval-when-compiles and eval-and-compiles.
Glenn Morris <rgm@gnu.org>
parents:
95193
diff
changeset
|
38 (autoload 'encrypt-insert-file-contents "encrypt") |
44810 | 39 (defalias 'netrc-point-at-eol |
40 (if (fboundp 'point-at-eol) | |
41 'point-at-eol | |
42 'line-end-position)) | |
95624
164c226d4a0d
Remove unnecessary eval-when-compiles and eval-and-compiles.
Glenn Morris <rgm@gnu.org>
parents:
95193
diff
changeset
|
43 (defvar encrypt-file-alist) |
87454 | 44 (eval-when-compile |
45 ;; This is unnecessary in the compiled version as it is a macro. | |
46 (if (fboundp 'bound-and-true-p) | |
47 (defalias 'netrc-bound-and-true-p 'bound-and-true-p) | |
48 (defmacro netrc-bound-and-true-p (var) | |
49 "Return the value of symbol VAR if it is bound, else nil." | |
50 `(and (boundp (quote ,var)) ,var)))) | |
85712
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
51 |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
52 (defgroup netrc nil |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
53 "Netrc configuration." |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
54 :group 'comm) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
55 |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
56 (defvar netrc-services-file "/etc/services" |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
57 "The name of the services file.") |
44810 | 58 |
59 (defun netrc-parse (file) | |
85712
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
60 (interactive "fFile to Parse: ") |
92694 | 61 "Parse FILE and return a list of all entries in the file." |
99506 | 62 (if (listp file) |
63 file | |
64 (when (file-exists-p file) | |
65 (with-temp-buffer | |
66 (let ((tokens '("machine" "default" "login" | |
67 "password" "account" "macdef" "force" | |
68 "port")) | |
69 (encryption-model (when (netrc-bound-and-true-p encrypt-file-alist) | |
70 (encrypt-find-model file))) | |
71 alist elem result pair) | |
72 (if encryption-model | |
73 (encrypt-insert-file-contents file encryption-model) | |
74 (insert-file-contents file)) | |
75 (goto-char (point-min)) | |
76 ;; Go through the file, line by line. | |
44810 | 77 (while (not (eobp)) |
99506 | 78 (narrow-to-region (point) (point-at-eol)) |
79 ;; For each line, get the tokens and values. | |
80 (while (not (eobp)) | |
81 (skip-chars-forward "\t ") | |
82 ;; Skip lines that begin with a "#". | |
83 (if (eq (char-after) ?#) | |
84 (goto-char (point-max)) | |
85 (unless (eobp) | |
86 (setq elem | |
87 (if (= (following-char) ?\") | |
88 (read (current-buffer)) | |
89 (buffer-substring | |
90 (point) (progn (skip-chars-forward "^\t ") | |
91 (point))))) | |
92 (cond | |
93 ((equal elem "macdef") | |
94 ;; We skip past the macro definition. | |
95 (widen) | |
96 (while (and (zerop (forward-line 1)) | |
97 (looking-at "$"))) | |
98 (narrow-to-region (point) (point))) | |
99 ((member elem tokens) | |
100 ;; Tokens that don't have a following value are ignored, | |
101 ;; except "default". | |
102 (when (and pair (or (cdr pair) | |
103 (equal (car pair) "default"))) | |
104 (push pair alist)) | |
105 (setq pair (list elem))) | |
106 (t | |
107 ;; Values that haven't got a preceding token are ignored. | |
108 (when pair | |
109 (setcdr pair elem) | |
110 (push pair alist) | |
111 (setq pair nil))))))) | |
112 (when alist | |
113 (push (nreverse alist) result)) | |
114 (setq alist nil | |
115 pair nil) | |
116 (widen) | |
117 (forward-line 1)) | |
118 (nreverse result)))))) | |
44810 | 119 |
120 (defun netrc-machine (list machine &optional port defaultport) | |
121 "Return the netrc values from LIST for MACHINE or for the default entry. | |
122 If PORT specified, only return entries with matching port tokens. | |
123 Entries without port tokens default to DEFAULTPORT." | |
124 (let ((rest list) | |
125 result) | |
126 (while list | |
127 (when (equal (cdr (assoc "machine" (car list))) machine) | |
128 (push (car list) result)) | |
129 (pop list)) | |
130 (unless result | |
131 ;; No machine name matches, so we look for default entries. | |
132 (while rest | |
133 (when (assoc "default" (car rest)) | |
134 (push (car rest) result)) | |
135 (pop rest))) | |
136 (when result | |
137 (setq result (nreverse result)) | |
138 (while (and result | |
85712
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
139 (not (netrc-port-equal |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
140 (or port defaultport "nntp") |
95193 | 141 ;; when port is not given in the netrc file, |
142 ;; it should mean "any port" | |
85712
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
143 (or (netrc-get (car result) "port") |
95193 | 144 defaultport port)))) |
44810 | 145 (pop result)) |
146 (car result)))) | |
147 | |
85712
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
148 (defun netrc-machine-user-or-password (mode authinfo-file-or-list machines ports defaults) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
149 "Get the user name or password according to MODE from AUTHINFO-FILE-OR-LIST. |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
150 Matches a machine from MACHINES and a port from PORTS, giving |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
151 default ports DEFAULTS to `netrc-machine'. |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
152 |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
153 MODE can be \"login\" or \"password\", suitable for passing to |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
154 `netrc-get'." |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
155 (let ((authinfo-list (if (stringp authinfo-file-or-list) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
156 (netrc-parse authinfo-file-or-list) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
157 authinfo-file-or-list)) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
158 (ports (or ports '(nil))) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
159 (defaults (or defaults '(nil))) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
160 info) |
101804 | 161 (if (listp mode) |
162 (setq info | |
163 (mapcar | |
164 (lambda (mode-element) | |
165 (netrc-machine-user-or-password | |
166 mode-element | |
167 authinfo-list | |
168 machines | |
169 ports | |
170 defaults)) | |
171 mode)) | |
172 (dolist (machine machines) | |
173 (dolist (default defaults) | |
174 (dolist (port ports) | |
175 (let ((alist (netrc-machine authinfo-list machine port default))) | |
176 (setq info (or (netrc-get alist mode) info))))))) | |
85712
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
177 info)) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
178 |
44810 | 179 (defun netrc-get (alist type) |
180 "Return the value of token TYPE from ALIST." | |
181 (cdr (assoc type alist))) | |
182 | |
85712
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
183 (defun netrc-port-equal (port1 port2) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
184 (when (numberp port1) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
185 (setq port1 (or (netrc-find-service-name port1) port1))) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
186 (when (numberp port2) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
187 (setq port2 (or (netrc-find-service-name port2) port2))) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
188 (equal port1 port2)) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
189 |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
190 (defun netrc-parse-services () |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
191 (when (file-exists-p netrc-services-file) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
192 (let ((services nil)) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
193 (with-temp-buffer |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
194 (insert-file-contents netrc-services-file) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
195 (while (search-forward "#" nil t) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
196 (delete-region (1- (point)) (point-at-eol))) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
197 (goto-char (point-min)) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
198 (while (re-search-forward |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
199 "^ *\\([^ \n\t]+\\)[ \t]+\\([0-9]+\\)/\\([^ \t\n]+\\)" nil t) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
200 (push (list (match-string 1) (string-to-number (match-string 2)) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
201 (intern (downcase (match-string 3)))) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
202 services)) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
203 (nreverse services))))) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
204 |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
205 (defun netrc-find-service-name (number &optional type) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
206 (let ((services (netrc-parse-services)) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
207 service) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
208 (setq type (or type 'tcp)) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
209 (while (and (setq service (pop services)) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
210 (not (and (= number (cadr service)) |
86963
d549ccffa35b
(top-level): Don't load `encrypt' features.
Glenn Morris <rgm@gnu.org>
parents:
85712
diff
changeset
|
211 (eq type (car (cddr service))))))) |
85712
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
212 (car service))) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
213 |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
214 (defun netrc-find-service-number (name &optional type) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
215 (let ((services (netrc-parse-services)) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
216 service) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
217 (setq type (or type 'tcp)) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
218 (while (and (setq service (pop services)) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
219 (not (and (string= name (car service)) |
86963
d549ccffa35b
(top-level): Don't load `encrypt' features.
Glenn Morris <rgm@gnu.org>
parents:
85712
diff
changeset
|
220 (eq type (car (cddr service))))))) |
85712
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
221 (cadr service))) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
222 |
44810 | 223 (provide 'netrc) |
224 | |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
92694
diff
changeset
|
225 ;; arch-tag: af9929cc-2d12-482f-936e-eb4366f9fa55 |
44810 | 226 ;;; netrc.el ends here |