Mercurial > emacs
comparison lisp/net/netrc.el @ 91085:880960b70474
Merge from emacs--devo--0
Revision: emacs@sv.gnu.org/emacs--unicode--0--patch-283
author | Miles Bader <miles@gnu.org> |
---|---|
date | Sun, 11 Nov 2007 00:56:44 +0000 |
parents | f55f9811f5d7 a3c27999decb |
children | 53108e6cea98 |
comparison
equal
deleted
inserted
replaced
91084:a4347a111894 | 91085:880960b70474 |
---|---|
30 ;; besides Gnus can use it. | 30 ;; besides Gnus can use it. |
31 | 31 |
32 ;;; Code: | 32 ;;; Code: |
33 | 33 |
34 ;;; | 34 ;;; |
35 ;;; .netrc and .authinforc parsing | 35 ;;; .netrc and .authinfo rc parsing |
36 ;;; | 36 ;;; |
37 | 37 |
38 (defalias 'netrc-point-at-eol | 38 (defalias 'netrc-point-at-eol |
39 (if (fboundp 'point-at-eol) | 39 (if (fboundp 'point-at-eol) |
40 'point-at-eol | 40 'point-at-eol |
41 'line-end-position)) | 41 'line-end-position)) |
42 ;; autoload encrypt | |
43 | |
44 (eval-and-compile | |
45 (autoload 'encrypt-find-model "encrypt") | |
46 (autoload 'encrypt-insert-file-contents "encrypt")) | |
47 | |
48 (defgroup netrc nil | |
49 "Netrc configuration." | |
50 :group 'comm) | |
51 | |
52 (defvar netrc-services-file "/etc/services" | |
53 "The name of the services file.") | |
42 | 54 |
43 (defun netrc-parse (file) | 55 (defun netrc-parse (file) |
44 "Parse FILE and return a list of all entries in the file." | 56 (interactive "fFile to Parse: ") |
57 "Parse FILE and return an list of all entries in the file." | |
45 (when (file-exists-p file) | 58 (when (file-exists-p file) |
46 (with-temp-buffer | 59 (with-temp-buffer |
47 (let ((tokens '("machine" "default" "login" | 60 (let ((tokens '("machine" "default" "login" |
48 "password" "account" "macdef" "force" | 61 "password" "account" "macdef" "force" |
49 "port")) | 62 "port")) |
63 (encryption-model (encrypt-find-model file)) | |
50 alist elem result pair) | 64 alist elem result pair) |
51 (insert-file-contents file) | 65 |
66 (if encryption-model | |
67 (encrypt-insert-file-contents file encryption-model) | |
68 (insert-file-contents file)) | |
69 | |
52 (goto-char (point-min)) | 70 (goto-char (point-min)) |
53 ;; Go through the file, line by line. | 71 ;; Go through the file, line by line. |
54 (while (not (eobp)) | 72 (while (not (eobp)) |
55 (narrow-to-region (point) (netrc-point-at-eol)) | 73 (narrow-to-region (point) (point-at-eol)) |
56 ;; For each line, get the tokens and values. | 74 ;; For each line, get the tokens and values. |
57 (while (not (eobp)) | 75 (while (not (eobp)) |
58 (skip-chars-forward "\t ") | 76 (skip-chars-forward "\t ") |
59 ;; Skip lines that begin with a "#". | 77 ;; Skip lines that begin with a "#". |
60 (if (eq (char-after) ?#) | 78 (if (eq (char-after) ?#) |
111 (push (car rest) result)) | 129 (push (car rest) result)) |
112 (pop rest))) | 130 (pop rest))) |
113 (when result | 131 (when result |
114 (setq result (nreverse result)) | 132 (setq result (nreverse result)) |
115 (while (and result | 133 (while (and result |
116 (not (equal (or port defaultport "nntp") | 134 (not (netrc-port-equal |
117 (or (netrc-get (car result) "port") | 135 (or port defaultport "nntp") |
118 defaultport "nntp")))) | 136 (or (netrc-get (car result) "port") |
137 defaultport "nntp")))) | |
119 (pop result)) | 138 (pop result)) |
120 (car result)))) | 139 (car result)))) |
140 | |
141 (defun netrc-machine-user-or-password (mode authinfo-file-or-list machines ports defaults) | |
142 "Get the user name or password according to MODE from AUTHINFO-FILE-OR-LIST. | |
143 Matches a machine from MACHINES and a port from PORTS, giving | |
144 default ports DEFAULTS to `netrc-machine'. | |
145 | |
146 MODE can be \"login\" or \"password\", suitable for passing to | |
147 `netrc-get'." | |
148 (let ((authinfo-list (if (stringp authinfo-file-or-list) | |
149 (netrc-parse authinfo-file-or-list) | |
150 authinfo-file-or-list)) | |
151 (ports (or ports '(nil))) | |
152 (defaults (or defaults '(nil))) | |
153 info) | |
154 (dolist (machine machines) | |
155 (dolist (default defaults) | |
156 (dolist (port ports) | |
157 (let ((alist (netrc-machine authinfo-list machine port default))) | |
158 (setq info (or (netrc-get alist mode) info)))))) | |
159 info)) | |
121 | 160 |
122 (defun netrc-get (alist type) | 161 (defun netrc-get (alist type) |
123 "Return the value of token TYPE from ALIST." | 162 "Return the value of token TYPE from ALIST." |
124 (cdr (assoc type alist))) | 163 (cdr (assoc type alist))) |
125 | 164 |
165 (defun netrc-port-equal (port1 port2) | |
166 (when (numberp port1) | |
167 (setq port1 (or (netrc-find-service-name port1) port1))) | |
168 (when (numberp port2) | |
169 (setq port2 (or (netrc-find-service-name port2) port2))) | |
170 (equal port1 port2)) | |
171 | |
172 (defun netrc-parse-services () | |
173 (when (file-exists-p netrc-services-file) | |
174 (let ((services nil)) | |
175 (with-temp-buffer | |
176 (insert-file-contents netrc-services-file) | |
177 (while (search-forward "#" nil t) | |
178 (delete-region (1- (point)) (point-at-eol))) | |
179 (goto-char (point-min)) | |
180 (while (re-search-forward | |
181 "^ *\\([^ \n\t]+\\)[ \t]+\\([0-9]+\\)/\\([^ \t\n]+\\)" nil t) | |
182 (push (list (match-string 1) (string-to-number (match-string 2)) | |
183 (intern (downcase (match-string 3)))) | |
184 services)) | |
185 (nreverse services))))) | |
186 | |
187 (defun netrc-find-service-name (number &optional type) | |
188 (let ((services (netrc-parse-services)) | |
189 service) | |
190 (setq type (or type 'tcp)) | |
191 (while (and (setq service (pop services)) | |
192 (not (and (= number (cadr service)) | |
193 (eq type (caddr service))))) | |
194 ) | |
195 (car service))) | |
196 | |
197 (defun netrc-find-service-number (name &optional type) | |
198 (let ((services (netrc-parse-services)) | |
199 service) | |
200 (setq type (or type 'tcp)) | |
201 (while (and (setq service (pop services)) | |
202 (not (and (string= name (car service)) | |
203 (eq type (caddr service))))) | |
204 ) | |
205 (cadr service))) | |
206 | |
126 (provide 'netrc) | 207 (provide 'netrc) |
127 | 208 |
128 ;;; arch-tag: af9929cc-2d12-482f-936e-eb4366f9fa55 | 209 ;;; arch-tag: af9929cc-2d12-482f-936e-eb4366f9fa55 |
129 ;;; netrc.el ends here | 210 ;;; netrc.el ends here |