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