1 ;;; mm-url.el --- a wrapper of url functions/commands for Gnus
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; This file is part of GNU Emacs.
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 by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; Some codes are stolen from w3 and url packages. Some are moved from
27 ;; TODO: Support POST, cookie.
31 (eval-when-compile (require 'cl
))
36 (defvar url-current-object
)
37 (defvar url-package-name
)
38 (defvar url-package-version
)
41 "A wrapper of url package and external url command for Gnus."
44 (defcustom mm-url-use-external
(not
48 "*If non-nil, use external grab program `mm-url-program'."
53 (defvar mm-url-predefined-programs
54 '((wget "wget" "--user-agent=mm-url" "-q" "-O" "-")
55 (w3m "w3m" "-dump_source")
56 (lynx "lynx" "-source")
57 (curl "curl" "--silent" "--user-agent" "mm-url" "--location")))
59 (defcustom mm-url-program
61 ((executable-find "wget") 'wget
)
62 ((executable-find "w3m") 'w3m
)
63 ((executable-find "lynx") 'lynx
)
64 ((executable-find "curl") 'curl
)
66 "The url grab program.
67 Likely values are `wget', `w3m', `lynx' and `curl'."
70 (symbol :tag
"wget" wget
)
71 (symbol :tag
"w3m" w3m
)
72 (symbol :tag
"lynx" lynx
)
73 (symbol :tag
"curl" curl
)
74 (string :tag
"other"))
77 (defcustom mm-url-arguments nil
78 "The arguments for `mm-url-program'."
80 :type
'(repeat string
)
84 ;;; Internal variables
86 (defvar mm-url-package-name
87 (gnus-replace-in-string
88 (gnus-replace-in-string gnus-version
" v.*$" "")
91 (defvar mm-url-package-version gnus-version-number
)
94 (defvar mm-url-html-entities
102 (rsquo .
39) ; should be U+8217
119 (uarr .
94) ; should be U+8593
121 (lsquo .
96) ; should be U+8216
223 ;; Special handling of these
237 ;; The following 5 entities are not mentioned in the HTML 2.0
238 ;; standard, nor in any other HTML proposed standard of which I
239 ;; am aware. I am not even sure they are ISO entity names. ***
240 ;; Hence, some arrangement should be made to give a bad HTML
241 ;; message when they are seen.
253 ;; (shy . ????) ; soft hyphen
255 "*An assoc list of entity names and how to actually display them.")
257 (defconst mm-url-unreserved-chars
259 ?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
260 ?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
261 ?
0 ?
1 ?
2 ?
3 ?
4 ?
5 ?
6 ?
7 ?
8 ?
9
262 ?- ?_ ?. ?
! ?~ ?
* ?
' ?\
( ?\
))
263 "A list of characters that are _NOT_ reserved in the URL spec.
264 This is taken from RFC 2396.")
266 (defun mm-url-load-url ()
267 "Load `url-insert-file-contents'."
268 (unless (condition-case ()
270 (require 'url-handlers
)
274 ;; w3-4.0pre0.46 or earlier version.
279 (defun mm-url-insert-file-contents (url)
280 "Insert file contents of URL.
281 If `mm-url-use-external' is non-nil, use `mm-url-program'."
282 (if mm-url-use-external
284 (if (string-match "^file:/+" url
)
285 (insert-file-contents (substring url
(1- (match-end 0))))
286 (mm-url-insert-file-contents-external url
))
287 (goto-char (point-min))
288 (if (fboundp 'url-generic-parse-url
)
289 (setq url-current-object
290 (url-generic-parse-url url
)))
291 (list url
(buffer-size)))
293 (let ((name buffer-file-name
)
294 (url-request-extra-headers
295 ;; ISTM setting a Connection header was a workaround for
296 ;; older versions of url included with w3, but it does more
297 ;; harm than good with the one shipped with Emacs. --ansel
298 (if (not (and (boundp 'url-version
)
299 (equal url-version
"Emacs")))
300 (list (cons "Connection" "Close"))))
301 (url-package-name (or mm-url-package-name
303 (url-package-version (or mm-url-package-version
304 url-package-version
))
306 (setq result
(url-insert-file-contents url
))
308 (goto-char (point-min))
309 (while (re-search-forward "\r 1000\r ?" nil t
)
311 (setq buffer-file-name name
)
312 (if (and (fboundp 'url-generic-parse-url
)
314 (setq url-current-object
(url-generic-parse-url
319 (defun mm-url-insert-file-contents-external (url)
320 "Insert file contents of URL using `mm-url-program'."
322 (if (symbolp mm-url-program
)
323 (let ((item (cdr (assq mm-url-program mm-url-predefined-programs
))))
324 (setq program
(car item
)
325 args
(append (cdr item
) (list url
))))
326 (setq program mm-url-program
327 args
(append mm-url-arguments
(list url
))))
328 (unless (eq 0 (apply 'call-process program nil t nil args
))
329 (error "Couldn't fetch %s" url
))))
331 (defvar mm-url-timeout
30
332 "The number of seconds before timing out an URL fetch.")
334 (defvar mm-url-retries
10
335 "The number of retries after timing out when fetching an URL.")
337 (defun mm-url-insert (url &optional follow-refresh
)
338 "Insert the contents from an URL in the current buffer.
339 If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
340 (let ((times mm-url-retries
)
344 (while (and (not (zerop (decf times
)))
346 (with-timeout (mm-url-timeout)
348 (message "Trying again (%s)..." (- mm-url-retries times
)))
352 (narrow-to-region (point) (point))
353 (mm-url-insert-file-contents url
)
354 (goto-char (point-min))
355 (when (re-search-forward
356 "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\"" nil t
)
357 (let ((url (match-string 1)))
358 (delete-region (point-min) (point-max))
359 (setq result
(mm-url-insert url t
)))))
360 (setq result
(mm-url-insert-file-contents url
)))
364 (defun mm-url-decode-entities ()
365 "Decode all HTML entities."
366 (goto-char (point-min))
367 (while (re-search-forward "&\\(#[0-9]+\\|[a-z]+[0-9]*\\);" nil t
)
368 (let ((elem (if (eq (aref (match-string 1) 0) ?\
#)
370 (string-to-number (substring
371 (match-string 1) 1))))
372 (if (mm-char-or-char-int-p c
) c
32))
373 (or (cdr (assq (intern (match-string 1))
374 mm-url-html-entities
))
376 (unless (stringp elem
)
377 (setq elem
(char-to-string elem
)))
378 (replace-match elem t t
))))
380 (defun mm-url-decode-entities-nbsp ()
381 "Decode all HTML entities and to a space."
382 (let ((mm-url-html-entities (cons '(nbsp .
32) mm-url-html-entities
)))
383 (mm-url-decode-entities)))
385 (defun mm-url-decode-entities-string (string)
388 (mm-url-decode-entities)
391 (defun mm-url-form-encode-xwfu (chunk)
392 "Escape characters in a string for application/x-www-form-urlencoded.
393 Blasphemous crap because someone didn't think %20 was good enough for encoding
394 spaces. Die Die Die."
395 ;; This will get rid of the 'attributes' specified by the file type,
396 ;; which are useless for an application/x-www-form-urlencoded form.
398 (setq chunk
(cdr chunk
)))
404 ((memq char mm-url-unreserved-chars
) (char-to-string char
))
405 (t (upcase (format "%%%02x" char
)))))
406 ;; Fixme: Should this actually be accepting multibyte? Is there a
407 ;; better way in XEmacs?
409 (encode-coding-string chunk
410 (if (fboundp 'find-coding-systems-string
)
411 (car (find-coding-systems-string chunk
))
412 buffer-file-coding-system
))
416 (defun mm-url-encode-www-form-urlencoded (pairs)
417 "Return PAIRS encoded for forms."
420 (concat (mm-url-form-encode-xwfu (car data
)) "="
421 (mm-url-form-encode-xwfu (cdr data
))))
424 (defun mm-url-fetch-form (url pairs
)
425 "Fetch a form from URL with PAIRS as the data using the POST method."
427 (let ((url-request-data (mm-url-encode-www-form-urlencoded pairs
))
428 (url-request-method "POST")
429 (url-request-extra-headers
430 '(("Content-type" .
"application/x-www-form-urlencoded"))))
431 (url-insert-file-contents url
)
432 (setq buffer-file-name nil
))
435 (defun mm-url-fetch-simple (url content
)
437 (let ((url-request-data content
)
438 (url-request-method "POST")
439 (url-request-extra-headers
440 '(("Content-type" .
"application/x-www-form-urlencoded"))))
441 (url-insert-file-contents url
)
442 (setq buffer-file-name nil
))
445 (defun mm-url-remove-markup ()
446 "Remove all HTML markup, leaving just plain text."
447 (goto-char (point-min))
448 (while (search-forward "<!--" nil t
)
449 (delete-region (match-beginning 0)
450 (or (search-forward "-->" nil t
)
452 (goto-char (point-min))
453 (while (re-search-forward "<[^>]+>" nil t
)
454 (replace-match "" t t
)))
458 ;; arch-tag: 0594f9b3-417c-48b0-adc2-5082e1e7917f
459 ;;; mm-url.el ends here