]> code.delx.au - gnu-emacs/blob - lisp/gnus/mm-url.el
(mail-reply-buffer): Add defvar.
[gnu-emacs] / lisp / gnus / mm-url.el
1 ;;; mm-url.el --- a wrapper of url functions/commands for Gnus
2
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published
11 ;; by the Free Software Foundation; either version 2, or (at your
12 ;; option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
23
24 ;;; Commentary:
25
26 ;; Some codes are stolen from w3 and url packages. Some are moved from
27 ;; nnweb.
28
29 ;; TODO: Support POST, cookie.
30
31 ;;; Code:
32
33 (eval-when-compile (require 'cl))
34
35 (require 'mm-util)
36 (require 'gnus)
37
38 (eval-and-compile
39 (autoload 'executable-find "executable"))
40
41 (eval-when-compile
42 (if (featurep 'xemacs)
43 (require 'timer-funcs)
44 (require 'timer)))
45
46 (defgroup mm-url nil
47 "A wrapper of url package and external url command for Gnus."
48 :group 'gnus)
49
50 (defcustom mm-url-use-external (not
51 (condition-case nil
52 (require 'url)
53 (error nil)))
54 "*If non-nil, use external grab program `mm-url-program'."
55 :version "22.1"
56 :type 'boolean
57 :group 'mm-url)
58
59 (defvar mm-url-predefined-programs
60 '((wget "wget" "--user-agent=mm-url" "-q" "-O" "-")
61 (w3m "w3m" "-dump_source")
62 (lynx "lynx" "-source")
63 (curl "curl" "--silent")))
64
65 (defcustom mm-url-program
66 (cond
67 ((executable-find "wget") 'wget)
68 ((executable-find "w3m") 'w3m)
69 ((executable-find "lynx") 'lynx)
70 ((executable-find "curl") 'curl)
71 (t "GET"))
72 "The url grab program.
73 Likely values are `wget', `w3m', `lynx' and `curl'."
74 :version "22.1"
75 :type '(choice
76 (symbol :tag "wget" wget)
77 (symbol :tag "w3m" w3m)
78 (symbol :tag "lynx" lynx)
79 (symbol :tag "curl" curl)
80 (string :tag "other"))
81 :group 'mm-url)
82
83 (defcustom mm-url-arguments nil
84 "The arguments for `mm-url-program'."
85 :version "22.1"
86 :type '(repeat string)
87 :group 'mm-url)
88
89 \f
90 ;;; Internal variables
91
92 (defvar mm-url-package-name
93 (gnus-replace-in-string
94 (gnus-replace-in-string gnus-version " v.*$" "")
95 " " "-"))
96
97 (defvar mm-url-package-version gnus-version-number)
98
99 ;; Stolen from w3.
100 (defvar mm-url-html-entities
101 '(
102 ;;(excl . 33)
103 (quot . 34)
104 ;;(num . 35)
105 ;;(dollar . 36)
106 ;;(percent . 37)
107 (amp . 38)
108 (rsquo . 39) ; should be U+8217
109 ;;(apos . 39)
110 ;;(lpar . 40)
111 ;;(rpar . 41)
112 ;;(ast . 42)
113 ;;(plus . 43)
114 ;;(comma . 44)
115 ;;(period . 46)
116 ;;(colon . 58)
117 ;;(semi . 59)
118 (lt . 60)
119 ;;(equals . 61)
120 (gt . 62)
121 ;;(quest . 63)
122 ;;(commat . 64)
123 ;;(lsqb . 91)
124 ;;(rsqb . 93)
125 (uarr . 94) ; should be U+8593
126 ;;(lowbar . 95)
127 (lsquo . 96) ; should be U+8216
128 (lcub . 123)
129 ;;(verbar . 124)
130 (rcub . 125)
131 (tilde . 126)
132 (nbsp . 160)
133 (iexcl . 161)
134 (cent . 162)
135 (pound . 163)
136 (curren . 164)
137 (yen . 165)
138 (brvbar . 166)
139 (sect . 167)
140 (uml . 168)
141 (copy . 169)
142 (ordf . 170)
143 (laquo . 171)
144 (not . 172)
145 (shy . 173)
146 (reg . 174)
147 (macr . 175)
148 (deg . 176)
149 (plusmn . 177)
150 (sup2 . 178)
151 (sup3 . 179)
152 (acute . 180)
153 (micro . 181)
154 (para . 182)
155 (middot . 183)
156 (cedil . 184)
157 (sup1 . 185)
158 (ordm . 186)
159 (raquo . 187)
160 (frac14 . 188)
161 (frac12 . 189)
162 (frac34 . 190)
163 (iquest . 191)
164 (Agrave . 192)
165 (Aacute . 193)
166 (Acirc . 194)
167 (Atilde . 195)
168 (Auml . 196)
169 (Aring . 197)
170 (AElig . 198)
171 (Ccedil . 199)
172 (Egrave . 200)
173 (Eacute . 201)
174 (Ecirc . 202)
175 (Euml . 203)
176 (Igrave . 204)
177 (Iacute . 205)
178 (Icirc . 206)
179 (Iuml . 207)
180 (ETH . 208)
181 (Ntilde . 209)
182 (Ograve . 210)
183 (Oacute . 211)
184 (Ocirc . 212)
185 (Otilde . 213)
186 (Ouml . 214)
187 (times . 215)
188 (Oslash . 216)
189 (Ugrave . 217)
190 (Uacute . 218)
191 (Ucirc . 219)
192 (Uuml . 220)
193 (Yacute . 221)
194 (THORN . 222)
195 (szlig . 223)
196 (agrave . 224)
197 (aacute . 225)
198 (acirc . 226)
199 (atilde . 227)
200 (auml . 228)
201 (aring . 229)
202 (aelig . 230)
203 (ccedil . 231)
204 (egrave . 232)
205 (eacute . 233)
206 (ecirc . 234)
207 (euml . 235)
208 (igrave . 236)
209 (iacute . 237)
210 (icirc . 238)
211 (iuml . 239)
212 (eth . 240)
213 (ntilde . 241)
214 (ograve . 242)
215 (oacute . 243)
216 (ocirc . 244)
217 (otilde . 245)
218 (ouml . 246)
219 (divide . 247)
220 (oslash . 248)
221 (ugrave . 249)
222 (uacute . 250)
223 (ucirc . 251)
224 (uuml . 252)
225 (yacute . 253)
226 (thorn . 254)
227 (yuml . 255)
228
229 ;; Special handling of these
230 (frac56 . "5/6")
231 (frac16 . "1/6")
232 (frac45 . "4/5")
233 (frac35 . "3/5")
234 (frac25 . "2/5")
235 (frac15 . "1/5")
236 (frac23 . "2/3")
237 (frac13 . "1/3")
238 (frac78 . "7/8")
239 (frac58 . "5/8")
240 (frac38 . "3/8")
241 (frac18 . "1/8")
242
243 ;; The following 5 entities are not mentioned in the HTML 2.0
244 ;; standard, nor in any other HTML proposed standard of which I
245 ;; am aware. I am not even sure they are ISO entity names. ***
246 ;; Hence, some arrangement should be made to give a bad HTML
247 ;; message when they are seen.
248 (ndash . 45)
249 (mdash . 45)
250 (emsp . 32)
251 (ensp . 32)
252 (sim . 126)
253 (le . "<=")
254 (agr . "alpha")
255 (rdquo . "''")
256 (ldquo . "``")
257 (trade . "(TM)")
258 ;; To be done
259 ;; (shy . ????) ; soft hyphen
260 )
261 "*An assoc list of entity names and how to actually display them.")
262
263 (defconst mm-url-unreserved-chars
264 '(
265 ?a ?b ?c ?d ?e ?f ?g ?h ?i ?j ?k ?l ?m ?n ?o ?p ?q ?r ?s ?t ?u ?v ?w ?x ?y ?z
266 ?A ?B ?C ?D ?E ?F ?G ?H ?I ?J ?K ?L ?M ?N ?O ?P ?Q ?R ?S ?T ?U ?V ?W ?X ?Y ?Z
267 ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
268 ?- ?_ ?. ?! ?~ ?* ?' ?\( ?\))
269 "A list of characters that are _NOT_ reserved in the URL spec.
270 This is taken from RFC 2396.")
271
272 (defun mm-url-load-url ()
273 "Load `url-insert-file-contents'."
274 (unless (condition-case ()
275 (require 'url-handlers)
276 (error nil))
277 ;; w3-4.0pre0.46 or earlier version.
278 (require 'w3-vars)
279 (require 'url)))
280
281 ;;;###autoload
282 (defun mm-url-insert-file-contents (url)
283 "Insert file contents of URL.
284 If `mm-url-use-external' is non-nil, use `mm-url-program'."
285 (if mm-url-use-external
286 (progn
287 (if (string-match "^file:/+" url)
288 (insert-file-contents (substring url (1- (match-end 0))))
289 (mm-url-insert-file-contents-external url))
290 (goto-char (point-min))
291 (if (fboundp 'url-generic-parse-url)
292 (setq url-current-object
293 (url-generic-parse-url url)))
294 (list url (buffer-size)))
295 (mm-url-load-url)
296 (let ((name buffer-file-name)
297 (url-request-extra-headers (list (cons "Connection" "Close")))
298 (url-package-name (or mm-url-package-name
299 url-package-name))
300 (url-package-version (or mm-url-package-version
301 url-package-version))
302 result)
303 (setq result (url-insert-file-contents url))
304 (save-excursion
305 (goto-char (point-min))
306 (while (re-search-forward "\r 1000\r ?" nil t)
307 (replace-match "")))
308 (setq buffer-file-name name)
309 (if (and (fboundp 'url-generic-parse-url)
310 (listp result))
311 (setq url-current-object (url-generic-parse-url
312 (car result))))
313 result)))
314
315 ;;;###autoload
316 (defun mm-url-insert-file-contents-external (url)
317 "Insert file contents of URL using `mm-url-program'."
318 (let (program args)
319 (if (symbolp mm-url-program)
320 (let ((item (cdr (assq mm-url-program mm-url-predefined-programs))))
321 (setq program (car item)
322 args (append (cdr item) (list url))))
323 (setq program mm-url-program
324 args (append mm-url-arguments (list url))))
325 (unless (eq 0 (apply 'call-process program nil t nil args))
326 (error "Couldn't fetch %s" url))))
327
328 (defvar mm-url-timeout 30
329 "The number of seconds before timing out an URL fetch.")
330
331 (defvar mm-url-retries 10
332 "The number of retries after timing out when fetching an URL.")
333
334 (defun mm-url-insert (url &optional follow-refresh)
335 "Insert the contents from an URL in the current buffer.
336 If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
337 (let ((times mm-url-retries)
338 (done nil)
339 (first t)
340 result)
341 (while (and (not (zerop (decf times)))
342 (not done))
343 (with-timeout (mm-url-timeout)
344 (unless first
345 (message "Trying again (%s)..." (- mm-url-retries times)))
346 (setq first nil)
347 (if follow-refresh
348 (save-restriction
349 (narrow-to-region (point) (point))
350 (mm-url-insert-file-contents url)
351 (goto-char (point-min))
352 (when (re-search-forward
353 "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\"" nil t)
354 (let ((url (match-string 1)))
355 (delete-region (point-min) (point-max))
356 (setq result (mm-url-insert url t)))))
357 (setq result (mm-url-insert-file-contents url)))
358 (setq done t)))
359 result))
360
361 (defun mm-url-decode-entities ()
362 "Decode all HTML entities."
363 (goto-char (point-min))
364 (while (re-search-forward "&\\(#[0-9]+\\|[a-z]+\\);" nil t)
365 (let ((elem (if (eq (aref (match-string 1) 0) ?\#)
366 (let ((c
367 (string-to-number (substring
368 (match-string 1) 1))))
369 (if (mm-char-or-char-int-p c) c 32))
370 (or (cdr (assq (intern (match-string 1))
371 mm-url-html-entities))
372 ?#))))
373 (unless (stringp elem)
374 (setq elem (char-to-string elem)))
375 (replace-match elem t t))))
376
377 (defun mm-url-decode-entities-nbsp ()
378 "Decode all HTML entities and &nbsp; to a space."
379 (let ((mm-url-html-entities (cons '(nbsp . 32) mm-url-html-entities)))
380 (mm-url-decode-entities)))
381
382 (defun mm-url-decode-entities-string (string)
383 (with-temp-buffer
384 (insert string)
385 (mm-url-decode-entities)
386 (buffer-string)))
387
388 (defun mm-url-form-encode-xwfu (chunk)
389 "Escape characters in a string for application/x-www-form-urlencoded.
390 Blasphemous crap because someone didn't think %20 was good enough for encoding
391 spaces. Die Die Die."
392 ;; This will get rid of the 'attributes' specified by the file type,
393 ;; which are useless for an application/x-www-form-urlencoded form.
394 (if (consp chunk)
395 (setq chunk (cdr chunk)))
396
397 (mapconcat
398 (lambda (char)
399 (cond
400 ((= char ? ) "+")
401 ((memq char mm-url-unreserved-chars) (char-to-string char))
402 (t (upcase (format "%%%02x" char)))))
403 ;; Fixme: Should this actually be accepting multibyte? Is there a
404 ;; better way in XEmacs?
405 (if (featurep 'mule)
406 (encode-coding-string chunk
407 (if (fboundp 'find-coding-systems-string)
408 (car (find-coding-systems-string chunk))
409 buffer-file-coding-system))
410 chunk)
411 ""))
412
413 (defun mm-url-encode-www-form-urlencoded (pairs)
414 "Return PAIRS encoded for forms."
415 (mapconcat
416 (lambda (data)
417 (concat (mm-url-form-encode-xwfu (car data)) "="
418 (mm-url-form-encode-xwfu (cdr data))))
419 pairs "&"))
420
421 (defun mm-url-fetch-form (url pairs)
422 "Fetch a form from URL with PAIRS as the data using the POST method."
423 (mm-url-load-url)
424 (let ((url-request-data (mm-url-encode-www-form-urlencoded pairs))
425 (url-request-method "POST")
426 (url-request-extra-headers
427 '(("Content-type" . "application/x-www-form-urlencoded"))))
428 (url-insert-file-contents url)
429 (setq buffer-file-name nil))
430 t)
431
432 (defun mm-url-fetch-simple (url content)
433 (mm-url-load-url)
434 (let ((url-request-data content)
435 (url-request-method "POST")
436 (url-request-extra-headers
437 '(("Content-type" . "application/x-www-form-urlencoded"))))
438 (url-insert-file-contents url)
439 (setq buffer-file-name nil))
440 t)
441
442 (defun mm-url-remove-markup ()
443 "Remove all HTML markup, leaving just plain text."
444 (goto-char (point-min))
445 (while (search-forward "<!--" nil t)
446 (delete-region (match-beginning 0)
447 (or (search-forward "-->" nil t)
448 (point-max))))
449 (goto-char (point-min))
450 (while (re-search-forward "<[^>]+>" nil t)
451 (replace-match "" t t)))
452
453 (provide 'mm-url)
454
455 ;;; arch-tag: 0594f9b3-417c-48b0-adc2-5082e1e7917f
456 ;;; mm-url.el ends here