1 ;;; mm-url.el --- a wrapper of url functions/commands for Gnus
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Some codes are stolen from w3 and url packages. Some are moved from
28 ;; TODO: Support POST, cookie.
32 (eval-when-compile (require 'cl
))
37 (defvar url-current-object
)
38 (defvar url-package-name
)
39 (defvar url-package-version
)
42 "A wrapper of url package and external url command for Gnus."
45 (defcustom mm-url-use-external
(not
49 "*If non-nil, use external grab program `mm-url-program'."
54 (defvar mm-url-predefined-programs
55 '((wget "wget" "--user-agent=mm-url" "-q" "-O" "-")
56 (w3m "w3m" "-dump_source")
57 (lynx "lynx" "-source")
58 (curl "curl" "--silent" "--user-agent" "mm-url" "--location")))
60 (defcustom mm-url-program
62 ((executable-find "wget") 'wget
)
63 ((executable-find "w3m") 'w3m
)
64 ((executable-find "lynx") 'lynx
)
65 ((executable-find "curl") 'curl
)
67 "The url grab program.
68 Likely values are `wget', `w3m', `lynx' and `curl'."
71 (symbol :tag
"wget" wget
)
72 (symbol :tag
"w3m" w3m
)
73 (symbol :tag
"lynx" lynx
)
74 (symbol :tag
"curl" curl
)
75 (string :tag
"other"))
78 (defcustom mm-url-arguments nil
79 "The arguments for `mm-url-program'."
81 :type
'(repeat string
)
85 ;;; Internal variables
87 (defvar mm-url-package-name
88 (gnus-replace-in-string
89 (gnus-replace-in-string gnus-version
" v.*$" "")
92 (defvar mm-url-package-version gnus-version-number
)
95 (defvar mm-url-html-entities
103 (rsquo .
39) ; should be U+8217
120 (uarr .
94) ; should be U+8593
122 (lsquo .
96) ; should be U+8216
224 ;; Special handling of these
238 ;; The following 5 entities are not mentioned in the HTML 2.0
239 ;; standard, nor in any other HTML proposed standard of which I
240 ;; am aware. I am not even sure they are ISO entity names. ***
241 ;; Hence, some arrangement should be made to give a bad HTML
242 ;; message when they are seen.
254 ;; (shy . ????) ; soft hyphen
256 "*An assoc list of entity names and how to actually display them.")
258 (defconst mm-url-unreserved-chars
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 ?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
262 ?
0 ?
1 ?
2 ?
3 ?
4 ?
5 ?
6 ?
7 ?
8 ?
9
263 ?- ?_ ?. ?
! ?~ ?
* ?
' ?\
( ?\
))
264 "A list of characters that are _NOT_ reserved in the URL spec.
265 This is taken from RFC 2396.")
267 (defun mm-url-load-url ()
268 "Load `url-insert-file-contents'."
269 (unless (condition-case ()
271 (require 'url-handlers
)
275 ;; w3-4.0pre0.46 or earlier version.
280 (defun mm-url-insert-file-contents (url)
281 "Insert file contents of URL.
282 If `mm-url-use-external' is non-nil, use `mm-url-program'."
283 (if mm-url-use-external
285 (if (string-match "^file:/+" url
)
286 (insert-file-contents (substring url
(1- (match-end 0))))
287 (mm-url-insert-file-contents-external url
))
288 (goto-char (point-min))
289 (if (fboundp 'url-generic-parse-url
)
290 (setq url-current-object
291 (url-generic-parse-url url
)))
292 (list url
(buffer-size)))
294 (let ((name buffer-file-name
)
295 (url-request-extra-headers
296 ;; ISTM setting a Connection header was a workaround for
297 ;; older versions of url included with w3, but it does more
298 ;; harm than good with the one shipped with Emacs. --ansel
299 (if (not (and (boundp 'url-version
)
300 (equal url-version
"Emacs")))
301 (list (cons "Connection" "Close"))))
302 (url-package-name (or mm-url-package-name
304 (url-package-version (or mm-url-package-version
305 url-package-version
))
307 (setq result
(url-insert-file-contents url
))
309 (goto-char (point-min))
310 (while (re-search-forward "\r 1000\r ?" nil t
)
312 (setq buffer-file-name name
)
313 (if (and (fboundp 'url-generic-parse-url
)
315 (setq url-current-object
(url-generic-parse-url
320 (defun mm-url-insert-file-contents-external (url)
321 "Insert file contents of URL using `mm-url-program'."
323 (if (symbolp mm-url-program
)
324 (let ((item (cdr (assq mm-url-program mm-url-predefined-programs
))))
325 (setq program
(car item
)
326 args
(append (cdr item
) (list url
))))
327 (setq program mm-url-program
328 args
(append mm-url-arguments
(list url
))))
329 (unless (eq 0 (apply 'call-process program nil t nil args
))
330 (error "Couldn't fetch %s" url
))))
332 (defvar mm-url-timeout
30
333 "The number of seconds before timing out an URL fetch.")
335 (defvar mm-url-retries
10
336 "The number of retries after timing out when fetching an URL.")
338 (defun mm-url-insert (url &optional follow-refresh
)
339 "Insert the contents from an URL in the current buffer.
340 If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
341 (let ((times mm-url-retries
)
345 (while (and (not (zerop (decf times
)))
347 (with-timeout (mm-url-timeout)
349 (message "Trying again (%s)..." (- mm-url-retries times
)))
353 (narrow-to-region (point) (point))
354 (mm-url-insert-file-contents url
)
355 (goto-char (point-min))
356 (when (re-search-forward
357 "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\"" nil t
)
358 (let ((url (match-string 1)))
359 (delete-region (point-min) (point-max))
360 (setq result
(mm-url-insert url t
)))))
361 (setq result
(mm-url-insert-file-contents url
)))
365 (defun mm-url-decode-entities ()
366 "Decode all HTML entities."
367 (goto-char (point-min))
368 (while (re-search-forward "&\\(#[0-9]+\\|#x[0-9a-f]+\\|[a-z]+[0-9]*\\);"
370 (let* ((entity (match-string 1))
371 (elem (if (eq (aref entity
0) ?\
#)
373 ;; Hex number: ㈒
374 (if (eq (aref entity
1) ?x
)
375 (string-to-number (substring entity
2)
377 ;; Decimal number: 
378 (string-to-number (substring entity
1)))))
379 (setq c
(or (cdr (assq c mm-extra-numeric-entities
))
381 (if (mm-char-or-char-int-p c
) c ?
#))
382 (or (cdr (assq (intern entity
)
383 mm-url-html-entities
))
385 (unless (stringp elem
)
386 (setq elem
(char-to-string elem
)))
387 (replace-match elem t t
))))
389 (defun mm-url-decode-entities-nbsp ()
390 "Decode all HTML entities and to a space."
391 (let ((mm-url-html-entities (cons '(nbsp .
32) mm-url-html-entities
)))
392 (mm-url-decode-entities)))
394 (defun mm-url-decode-entities-string (string)
397 (mm-url-decode-entities)
400 (defun mm-url-form-encode-xwfu (chunk)
401 "Escape characters in a string for application/x-www-form-urlencoded.
402 Blasphemous crap because someone didn't think %20 was good enough for encoding
403 spaces. Die Die Die."
404 ;; This will get rid of the 'attributes' specified by the file type,
405 ;; which are useless for an application/x-www-form-urlencoded form.
407 (setq chunk
(cdr chunk
)))
413 ((memq char mm-url-unreserved-chars
) (char-to-string char
))
414 (t (upcase (format "%%%02x" char
)))))
415 (mm-encode-coding-string chunk
416 (if (fboundp 'find-coding-systems-string
)
417 (car (find-coding-systems-string chunk
))
418 buffer-file-coding-system
))
421 (defun mm-url-encode-www-form-urlencoded (pairs)
422 "Return PAIRS encoded for forms."
425 (concat (mm-url-form-encode-xwfu (car data
)) "="
426 (mm-url-form-encode-xwfu (cdr data
))))
429 (autoload 'mml-compute-boundary
"mml")
431 (defun mm-url-encode-multipart-form-data (pairs &optional boundary
)
432 "Return PAIRS encoded in multipart/form-data."
435 ;; Get a good boundary
437 (setq boundary
(mml-compute-boundary '())))
441 ;; Start with the boundary
444 ;; Create name value pairs
447 ;; Delete any returned items that are empty
449 (mapcar (lambda (data)
455 "Content-Disposition: form-data; name=\""
457 "Content-Type: text/plain; charset=utf-8\r\n"
458 "Content-Transfer-Encoding: binary\r\n\r\n"
460 (cond ((stringp (cdr data
))
462 ((integerp (cdr data
))
463 (int-to-string (cdr data
))))
467 ;; use the boundary as a separator
468 (concat "--" boundary
"\r\n"))
470 ;; put a boundary at the end.
471 "--" boundary
"--\r\n"))
473 (defun mm-url-fetch-form (url pairs
)
474 "Fetch a form from URL with PAIRS as the data using the POST method."
476 (let ((url-request-data (mm-url-encode-www-form-urlencoded pairs
))
477 (url-request-method "POST")
478 (url-request-extra-headers
479 '(("Content-type" .
"application/x-www-form-urlencoded"))))
480 (url-insert-file-contents url
)
481 (setq buffer-file-name nil
))
484 (defun mm-url-fetch-simple (url content
)
486 (let ((url-request-data content
)
487 (url-request-method "POST")
488 (url-request-extra-headers
489 '(("Content-type" .
"application/x-www-form-urlencoded"))))
490 (url-insert-file-contents url
)
491 (setq buffer-file-name nil
))
494 (defun mm-url-remove-markup ()
495 "Remove all HTML markup, leaving just plain text."
496 (goto-char (point-min))
497 (while (search-forward "<!--" nil t
)
498 (delete-region (match-beginning 0)
499 (or (search-forward "-->" nil t
)
501 (goto-char (point-min))
502 (while (re-search-forward "<[^>]+>" nil t
)
503 (replace-match "" t t
)))
507 ;;; mm-url.el ends here