1 ;;; mm-url.el --- a wrapper of url functions/commands for Gnus
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005 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
11 ;; by the Free Software Foundation; either version 2, or (at your
12 ;; option) any later version.
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.
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.
26 ;; Some codes are stolen from w3 and url packages. Some are moved from
29 ;; TODO: Support POST, cookie.
33 (eval-when-compile (require 'cl
))
39 (autoload 'executable-find
"executable"))
42 (if (featurep 'xemacs
)
43 (require 'timer-funcs
)
46 (defvar url-current-object
)
47 (defvar url-package-name
)
48 (defvar url-package-version
)
51 "A wrapper of url package and external url command for Gnus."
54 (defcustom mm-url-use-external
(not
58 "*If non-nil, use external grab program `mm-url-program'."
63 (defvar mm-url-predefined-programs
64 '((wget "wget" "--user-agent=mm-url" "-q" "-O" "-")
65 (w3m "w3m" "-dump_source")
66 (lynx "lynx" "-source")
67 (curl "curl" "--silent" "--user-agent mm-url" "--location")))
69 (defcustom mm-url-program
71 ((executable-find "wget") 'wget
)
72 ((executable-find "w3m") 'w3m
)
73 ((executable-find "lynx") 'lynx
)
74 ((executable-find "curl") 'curl
)
76 "The url grab program.
77 Likely values are `wget', `w3m', `lynx' and `curl'."
80 (symbol :tag
"wget" wget
)
81 (symbol :tag
"w3m" w3m
)
82 (symbol :tag
"lynx" lynx
)
83 (symbol :tag
"curl" curl
)
84 (string :tag
"other"))
87 (defcustom mm-url-arguments nil
88 "The arguments for `mm-url-program'."
90 :type
'(repeat string
)
94 ;;; Internal variables
96 (defvar mm-url-package-name
97 (gnus-replace-in-string
98 (gnus-replace-in-string gnus-version
" v.*$" "")
101 (defvar mm-url-package-version gnus-version-number
)
104 (defvar mm-url-html-entities
112 (rsquo .
39) ; should be U+8217
129 (uarr .
94) ; should be U+8593
131 (lsquo .
96) ; should be U+8216
233 ;; Special handling of these
247 ;; The following 5 entities are not mentioned in the HTML 2.0
248 ;; standard, nor in any other HTML proposed standard of which I
249 ;; am aware. I am not even sure they are ISO entity names. ***
250 ;; Hence, some arrangement should be made to give a bad HTML
251 ;; message when they are seen.
263 ;; (shy . ????) ; soft hyphen
265 "*An assoc list of entity names and how to actually display them.")
267 (defconst mm-url-unreserved-chars
269 ?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
270 ?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
271 ?
0 ?
1 ?
2 ?
3 ?
4 ?
5 ?
6 ?
7 ?
8 ?
9
272 ?- ?_ ?. ?
! ?~ ?
* ?
' ?\
( ?\
))
273 "A list of characters that are _NOT_ reserved in the URL spec.
274 This is taken from RFC 2396.")
276 (defun mm-url-load-url ()
277 "Load `url-insert-file-contents'."
278 (unless (condition-case ()
279 (require 'url-handlers
)
281 ;; w3-4.0pre0.46 or earlier version.
286 (defun mm-url-insert-file-contents (url)
287 "Insert file contents of URL.
288 If `mm-url-use-external' is non-nil, use `mm-url-program'."
289 (if mm-url-use-external
291 (if (string-match "^file:/+" url
)
292 (insert-file-contents (substring url
(1- (match-end 0))))
293 (mm-url-insert-file-contents-external url
))
294 (goto-char (point-min))
295 (if (fboundp 'url-generic-parse-url
)
296 (setq url-current-object
297 (url-generic-parse-url url
)))
298 (list url
(buffer-size)))
300 (let ((name buffer-file-name
)
301 (url-request-extra-headers (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]+\\|[a-z]+[0-9]*\\);" nil t
)
369 (let ((elem (if (eq (aref (match-string 1) 0) ?\
#)
371 (string-to-number (substring
372 (match-string 1) 1))))
373 (if (mm-char-or-char-int-p c
) c
32))
374 (or (cdr (assq (intern (match-string 1))
375 mm-url-html-entities
))
377 (unless (stringp elem
)
378 (setq elem
(char-to-string elem
)))
379 (replace-match elem t t
))))
381 (defun mm-url-decode-entities-nbsp ()
382 "Decode all HTML entities and to a space."
383 (let ((mm-url-html-entities (cons '(nbsp .
32) mm-url-html-entities
)))
384 (mm-url-decode-entities)))
386 (defun mm-url-decode-entities-string (string)
389 (mm-url-decode-entities)
392 (defun mm-url-form-encode-xwfu (chunk)
393 "Escape characters in a string for application/x-www-form-urlencoded.
394 Blasphemous crap because someone didn't think %20 was good enough for encoding
395 spaces. Die Die Die."
396 ;; This will get rid of the 'attributes' specified by the file type,
397 ;; which are useless for an application/x-www-form-urlencoded form.
399 (setq chunk
(cdr chunk
)))
405 ((memq char mm-url-unreserved-chars
) (char-to-string char
))
406 (t (upcase (format "%%%02x" char
)))))
407 ;; Fixme: Should this actually be accepting multibyte? Is there a
408 ;; better way in XEmacs?
410 (encode-coding-string chunk
411 (if (fboundp 'find-coding-systems-string
)
412 (car (find-coding-systems-string chunk
))
413 buffer-file-coding-system
))
417 (defun mm-url-encode-www-form-urlencoded (pairs)
418 "Return PAIRS encoded for forms."
421 (concat (mm-url-form-encode-xwfu (car data
)) "="
422 (mm-url-form-encode-xwfu (cdr data
))))
425 (defun mm-url-fetch-form (url pairs
)
426 "Fetch a form from URL with PAIRS as the data using the POST method."
428 (let ((url-request-data (mm-url-encode-www-form-urlencoded pairs
))
429 (url-request-method "POST")
430 (url-request-extra-headers
431 '(("Content-type" .
"application/x-www-form-urlencoded"))))
432 (url-insert-file-contents url
)
433 (setq buffer-file-name nil
))
436 (defun mm-url-fetch-simple (url content
)
438 (let ((url-request-data content
)
439 (url-request-method "POST")
440 (url-request-extra-headers
441 '(("Content-type" .
"application/x-www-form-urlencoded"))))
442 (url-insert-file-contents url
)
443 (setq buffer-file-name nil
))
446 (defun mm-url-remove-markup ()
447 "Remove all HTML markup, leaving just plain text."
448 (goto-char (point-min))
449 (while (search-forward "<!--" nil t
)
450 (delete-region (match-beginning 0)
451 (or (search-forward "-->" nil t
)
453 (goto-char (point-min))
454 (while (re-search-forward "<[^>]+>" nil t
)
455 (replace-match "" t t
)))
459 ;;; arch-tag: 0594f9b3-417c-48b0-adc2-5082e1e7917f
460 ;;; mm-url.el ends here