1 ;;; mm-url.el --- a wrapper of url functions/commands for Gnus
2 ;; Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published
10 ;; by the Free Software Foundation; either version 2, or (at your
11 ;; option) any later version.
13 ;; GNU Emacs is distributed in the hope that it will be useful, but
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ;; General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
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
))
38 (autoload 'executable-find
"executable"))
44 "A wrapper of url package and external url command for Gnus."
47 (defcustom mm-url-use-external
(not
51 "*If non-nil, use external grab program `mm-url-program'."
56 (defvar mm-url-predefined-programs
57 '((wget "wget" "--user-agent=mm-url" "-q" "-O" "-")
58 (w3m "w3m" "-dump_source")
59 (lynx "lynx" "-source")
62 (defcustom mm-url-program
64 ((executable-find "wget") 'wget
)
65 ((executable-find "w3m") 'w3m
)
66 ((executable-find "lynx") 'lynx
)
67 ((executable-find "curl") 'curl
)
69 "The url grab program.
70 Likely values are `wget', `w3m', `lynx' and `curl'."
73 (symbol :tag
"wget" wget
)
74 (symbol :tag
"w3m" w3m
)
75 (symbol :tag
"lynx" lynx
)
76 (symbol :tag
"curl" curl
)
77 (string :tag
"other"))
80 (defcustom mm-url-arguments nil
81 "The arguments for `mm-url-program'."
83 :type
'(repeat string
)
87 ;;; Internal variables
89 (defvar mm-url-package-name
90 (gnus-replace-in-string
91 (gnus-replace-in-string gnus-version
" v.*$" "")
94 (defvar mm-url-package-version gnus-version-number
)
97 (defvar mm-url-html-entities
105 (rsquo .
39) ; should be U+8217
122 (uarr .
94) ; should be U+8593
124 (lsquo .
96) ; should be U+8216
226 ;; Special handling of these
240 ;; The following 5 entities are not mentioned in the HTML 2.0
241 ;; standard, nor in any other HTML proposed standard of which I
242 ;; am aware. I am not even sure they are ISO entity names. ***
243 ;; Hence, some arrangement should be made to give a bad HTML
244 ;; message when they are seen.
256 ;; (shy . ????) ; soft hyphen
258 "*An assoc list of entity names and how to actually display them.")
260 (defconst mm-url-unreserved-chars
262 ?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
263 ?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
264 ?
0 ?
1 ?
2 ?
3 ?
4 ?
5 ?
6 ?
7 ?
8 ?
9
265 ?- ?_ ?. ?
! ?~ ?
* ?
' ?\
( ?\
))
266 "A list of characters that are _NOT_ reserved in the URL spec.
267 This is taken from RFC 2396.")
269 (defun mm-url-load-url ()
270 "Load `url-insert-file-contents'."
271 (unless (condition-case ()
272 (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 (list (cons "Connection" "Close")))
295 (url-package-name (or mm-url-package-name
297 (url-package-version (or mm-url-package-version
298 url-package-version
))
300 (setq result
(url-insert-file-contents url
))
302 (goto-char (point-min))
303 (while (re-search-forward "\r 1000\r ?" nil t
)
305 (setq buffer-file-name name
)
306 (if (and (fboundp 'url-generic-parse-url
)
308 (setq url-current-object
(url-generic-parse-url
313 (defun mm-url-insert-file-contents-external (url)
314 "Insert file contents of URL using `mm-url-program'."
316 (if (symbolp mm-url-program
)
317 (let ((item (cdr (assq mm-url-program mm-url-predefined-programs
))))
318 (setq program
(car item
)
319 args
(append (cdr item
) (list url
))))
320 (setq program mm-url-program
321 args
(append mm-url-arguments
(list url
))))
322 (unless (eq 0 (apply 'call-process program nil t nil args
))
323 (error "Couldn't fetch %s" url
))))
325 (defvar mm-url-timeout
30
326 "The number of seconds before timing out an URL fetch.")
328 (defvar mm-url-retries
10
329 "The number of retries after timing out when fetching an URL.")
331 (defun mm-url-insert (url &optional follow-refresh
)
332 "Insert the contents from an URL in the current buffer.
333 If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
334 (let ((times mm-url-retries
)
338 (while (and (not (zerop (decf times
)))
340 (with-timeout (mm-url-timeout)
342 (message "Trying again (%s)..." (- mm-url-retries times
)))
346 (narrow-to-region (point) (point))
347 (mm-url-insert-file-contents url
)
348 (goto-char (point-min))
349 (when (re-search-forward
350 "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\"" nil t
)
351 (let ((url (match-string 1)))
352 (delete-region (point-min) (point-max))
353 (setq result
(mm-url-insert url t
)))))
354 (setq result
(mm-url-insert-file-contents url
)))
358 (defun mm-url-decode-entities ()
359 "Decode all HTML entities."
360 (goto-char (point-min))
361 (while (re-search-forward "&\\(#[0-9]+\\|[a-z]+\\);" nil t
)
362 (let ((elem (if (eq (aref (match-string 1) 0) ?\
#)
364 (string-to-number (substring
365 (match-string 1) 1))))
366 (if (mm-char-or-char-int-p c
) c
32))
367 (or (cdr (assq (intern (match-string 1))
368 mm-url-html-entities
))
370 (unless (stringp elem
)
371 (setq elem
(char-to-string elem
)))
372 (replace-match elem t t
))))
374 (defun mm-url-decode-entities-nbsp ()
375 "Decode all HTML entities and to a space."
376 (let ((mm-url-html-entities (cons '(nbsp .
32) mm-url-html-entities
)))
377 (mm-url-decode-entities)))
379 (defun mm-url-decode-entities-string (string)
382 (mm-url-decode-entities)
385 (defun mm-url-form-encode-xwfu (chunk)
386 "Escape characters in a string for application/x-www-form-urlencoded.
387 Blasphemous crap because someone didn't think %20 was good enough for encoding
388 spaces. Die Die Die."
389 ;; This will get rid of the 'attributes' specified by the file type,
390 ;; which are useless for an application/x-www-form-urlencoded form.
392 (setq chunk
(cdr chunk
)))
398 ((memq char mm-url-unreserved-chars
) (char-to-string char
))
399 (t (upcase (format "%%%02x" char
)))))
400 ;; Fixme: Should this actually be accepting multibyte? Is there a
401 ;; better way in XEmacs?
403 (encode-coding-string chunk
404 (if (fboundp 'find-coding-systems-string
)
405 (car (find-coding-systems-string chunk
))
406 buffer-file-coding-system
))
410 (defun mm-url-encode-www-form-urlencoded (pairs)
411 "Return PAIRS encoded for forms."
414 (concat (mm-url-form-encode-xwfu (car data
)) "="
415 (mm-url-form-encode-xwfu (cdr data
))))
418 (defun mm-url-fetch-form (url pairs
)
419 "Fetch a form from URL with PAIRS as the data using the POST method."
421 (let ((url-request-data (mm-url-encode-www-form-urlencoded pairs
))
422 (url-request-method "POST")
423 (url-request-extra-headers
424 '(("Content-type" .
"application/x-www-form-urlencoded"))))
425 (url-insert-file-contents url
)
426 (setq buffer-file-name nil
))
429 (defun mm-url-fetch-simple (url content
)
431 (let ((url-request-data content
)
432 (url-request-method "POST")
433 (url-request-extra-headers
434 '(("Content-type" .
"application/x-www-form-urlencoded"))))
435 (url-insert-file-contents url
)
436 (setq buffer-file-name nil
))
439 (defun mm-url-remove-markup ()
440 "Remove all HTML markup, leaving just plain text."
441 (goto-char (point-min))
442 (while (search-forward "<!--" nil t
)
443 (delete-region (match-beginning 0)
444 (or (search-forward "-->" nil t
)
446 (goto-char (point-min))
447 (while (re-search-forward "<[^>]+>" nil t
)
448 (replace-match "" t t
)))
452 ;;; arch-tag: 0594f9b3-417c-48b0-adc2-5082e1e7917f
453 ;;; mm-url.el ends here