Mercurial > emacs
annotate lisp/net/netrc.el @ 94693:9276b559433c
Standardize permissions notice.
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Tue, 06 May 2008 16:29:23 +0000 |
parents | 91e5880a36c1 |
children | d90ff358ee27 |
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, |
79714 | 3 ;; 2005, 2006, 2007, 2008 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) |
37 (eval-and-compile | |
38 (autoload 'encrypt-find-model "encrypt") | |
39 (autoload 'encrypt-insert-file-contents "encrypt")) | |
44810 | 40 (defalias 'netrc-point-at-eol |
41 (if (fboundp 'point-at-eol) | |
42 'point-at-eol | |
43 'line-end-position)) | |
87454 | 44 (eval-when-compile |
45 (defvar encrypt-file-alist) | |
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)))) | |
85712
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
52 |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
53 (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
|
54 "Netrc configuration." |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
55 :group 'comm) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
56 |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
57 (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
|
58 "The name of the services file.") |
44810 | 59 |
60 (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
|
61 (interactive "fFile to Parse: ") |
92694 | 62 "Parse FILE and return a list of all entries in the file." |
44810 | 63 (when (file-exists-p file) |
64 (with-temp-buffer | |
65 (let ((tokens '("machine" "default" "login" | |
66 "password" "account" "macdef" "force" | |
67 "port")) | |
87454 | 68 (encryption-model (when (netrc-bound-and-true-p encrypt-file-alist) |
69 (encrypt-find-model file))) | |
44810 | 70 alist elem result pair) |
87454 | 71 (if encryption-model |
72 (encrypt-insert-file-contents file encryption-model) | |
73 (insert-file-contents file)) | |
44810 | 74 (goto-char (point-min)) |
75 ;; Go through the file, line by line. | |
76 (while (not (eobp)) | |
85712
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
77 (narrow-to-region (point) (point-at-eol)) |
44810 | 78 ;; For each line, get the tokens and values. |
79 (while (not (eobp)) | |
80 (skip-chars-forward "\t ") | |
81 ;; Skip lines that begin with a "#". | |
82 (if (eq (char-after) ?#) | |
83 (goto-char (point-max)) | |
84 (unless (eobp) | |
85 (setq elem | |
86 (if (= (following-char) ?\") | |
87 (read (current-buffer)) | |
88 (buffer-substring | |
89 (point) (progn (skip-chars-forward "^\t ") | |
90 (point))))) | |
91 (cond | |
92 ((equal elem "macdef") | |
93 ;; We skip past the macro definition. | |
94 (widen) | |
95 (while (and (zerop (forward-line 1)) | |
96 (looking-at "$"))) | |
97 (narrow-to-region (point) (point))) | |
98 ((member elem tokens) | |
99 ;; Tokens that don't have a following value are ignored, | |
100 ;; except "default". | |
101 (when (and pair (or (cdr pair) | |
102 (equal (car pair) "default"))) | |
103 (push pair alist)) | |
104 (setq pair (list elem))) | |
105 (t | |
106 ;; Values that haven't got a preceding token are ignored. | |
107 (when pair | |
108 (setcdr pair elem) | |
109 (push pair alist) | |
110 (setq pair nil))))))) | |
111 (when alist | |
112 (push (nreverse alist) result)) | |
113 (setq alist nil | |
114 pair nil) | |
115 (widen) | |
116 (forward-line 1)) | |
117 (nreverse result))))) | |
118 | |
119 (defun netrc-machine (list machine &optional port defaultport) | |
120 "Return the netrc values from LIST for MACHINE or for the default entry. | |
121 If PORT specified, only return entries with matching port tokens. | |
122 Entries without port tokens default to DEFAULTPORT." | |
123 (let ((rest list) | |
124 result) | |
125 (while list | |
126 (when (equal (cdr (assoc "machine" (car list))) machine) | |
127 (push (car list) result)) | |
128 (pop list)) | |
129 (unless result | |
130 ;; No machine name matches, so we look for default entries. | |
131 (while rest | |
132 (when (assoc "default" (car rest)) | |
133 (push (car rest) result)) | |
134 (pop rest))) | |
135 (when result | |
136 (setq result (nreverse result)) | |
137 (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
|
138 (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
|
139 (or port defaultport "nntp") |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
140 (or (netrc-get (car result) "port") |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
141 defaultport "nntp")))) |
44810 | 142 (pop result)) |
143 (car result)))) | |
144 | |
85712
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
145 (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
|
146 "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
|
147 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
|
148 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
|
149 |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
150 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
|
151 `netrc-get'." |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
152 (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
|
153 (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
|
154 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
|
155 (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
|
156 (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
|
157 info) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
158 (dolist (machine machines) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
159 (dolist (default defaults) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
160 (dolist (port ports) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
161 (let ((alist (netrc-machine authinfo-list machine port default))) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
162 (setq info (or (netrc-get alist mode) info)))))) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
163 info)) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
164 |
44810 | 165 (defun netrc-get (alist type) |
166 "Return the value of token TYPE from ALIST." | |
167 (cdr (assoc type alist))) | |
168 | |
85712
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
169 (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
|
170 (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
|
171 (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
|
172 (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
|
173 (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
|
174 (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
|
175 |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
176 (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
|
177 (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
|
178 (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
|
179 (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
|
180 (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
|
181 (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
|
182 (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
|
183 (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
|
184 (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
|
185 "^ *\\([^ \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
|
186 (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
|
187 (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
|
188 services)) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
189 (nreverse services))))) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
190 |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
191 (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
|
192 (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
|
193 service) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
194 (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
|
195 (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
|
196 (not (and (= number (cadr service)) |
86963
d549ccffa35b
(top-level): Don't load `encrypt' features.
Glenn Morris <rgm@gnu.org>
parents:
85712
diff
changeset
|
197 (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
|
198 (car service))) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
199 |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
200 (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
|
201 (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
|
202 service) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
203 (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
|
204 (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
|
205 (not (and (string= name (car service)) |
86963
d549ccffa35b
(top-level): Don't load `encrypt' features.
Glenn Morris <rgm@gnu.org>
parents:
85712
diff
changeset
|
206 (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
|
207 (cadr service))) |
a3c27999decb
Update Gnus to No Gnus 0.7 from the Gnus CVS trunk
Miles Bader <miles@gnu.org>
parents:
78230
diff
changeset
|
208 |
44810 | 209 (provide 'netrc) |
210 | |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
92694
diff
changeset
|
211 ;; arch-tag: af9929cc-2d12-482f-936e-eb4366f9fa55 |
44810 | 212 ;;; netrc.el ends here |