1 ;;; mm-url.el --- a wrapper of url functions/commands for Gnus
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005,
4 ;; 2006 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
12 ;; by the Free Software Foundation; either version 2, or (at your
13 ;; option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; Some codes are stolen from w3 and url packages. Some are moved from
30 ;; TODO: Support POST, cookie.
34 (eval-when-compile (require 'cl))
36 (require 'nnheader) ;; for `mm-char-or-char-int-p'.
40 (autoload 'exec-installed-p "path-util"))
43 (if (featurep 'xemacs)
44 (require 'timer-funcs)
47 (defvar url-current-object)
48 (defvar url-package-name)
49 (defvar url-package-version)
52 "A wrapper of url package and external url command for Gnus."
55 (defcustom mm-url-use-external (not
59 "*If non-nil, use external grab program `mm-url-program'."
64 (defvar mm-url-predefined-programs
65 '((wget "wget" "--user-agent=mm-url" "-q" "-O" "-")
66 (w3m "w3m" "-dump_source")
67 (lynx "lynx" "-source")
68 (curl "curl" "--silent" "--user-agent mm-url" "--location")))
70 (defcustom mm-url-program
72 ((exec-installed-p "wget") 'wget)
73 ((exec-installed-p "w3m") 'w3m)
74 ((exec-installed-p "lynx") 'lynx)
75 ((exec-installed-p "curl") 'curl)
77 "The url grab program.
78 Likely values are `wget', `w3m', `lynx' and `curl'."
81 (symbol :tag "wget" wget)
82 (symbol :tag "w3m" w3m)
83 (symbol :tag "lynx" lynx)
84 (symbol :tag "curl" curl)
85 (string :tag "other"))
88 (defcustom mm-url-arguments nil
89 "The arguments for `mm-url-program'."
91 :type '(repeat string)
95 ;;; Internal variables
97 (defvar mm-url-package-name
98 (gnus-replace-in-string
99 (gnus-replace-in-string gnus-version " v.*$" "")
102 (defvar mm-url-package-version gnus-version-number)
105 (defvar mm-url-html-entities
113 (rsquo . 39) ; should be U+8217
130 (uarr . 94) ; should be U+8593
132 (lsquo . 96) ; should be U+8216
234 ;; Special handling of these
248 ;; The following 5 entities are not mentioned in the HTML 2.0
249 ;; standard, nor in any other HTML proposed standard of which I
250 ;; am aware. I am not even sure they are ISO entity names. ***
251 ;; Hence, some arrangement should be made to give a bad HTML
252 ;; message when they are seen.
264 ;; (shy . ????) ; soft hyphen
266 "*An assoc list of entity names and how to actually display them.")
268 (defconst mm-url-unreserved-chars
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 ?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
272 ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
273 ?- ?_ ?. ?! ?~ ?* ?' ?\( ?\))
274 "A list of characters that are _NOT_ reserved in the URL spec.
275 This is taken from RFC 2396.")
277 (defun mm-url-load-url ()
278 "Load `url-insert-file-contents'."
279 (unless (condition-case ()
281 (require 'url-handlers)
285 ;; w3-4.0pre0.46 or earlier version.
290 (defun mm-url-insert-file-contents (url)
291 "Insert file contents of URL.
292 If `mm-url-use-external' is non-nil, use `mm-url-program'."
293 (if mm-url-use-external
295 (if (string-match "^file:/+" url)
296 (insert-file-contents (substring url (1- (match-end 0))))
297 (mm-url-insert-file-contents-external url))
298 (goto-char (point-min))
299 (if (fboundp 'url-generic-parse-url)
300 (setq url-current-object
301 (url-generic-parse-url url)))
302 (list url (buffer-size)))
304 (let ((name buffer-file-name)
305 (url-request-extra-headers (list (cons "Connection" "Close")))
306 (url-package-name (or mm-url-package-name
308 (url-package-version (or mm-url-package-version
309 url-package-version))
311 (setq result (url-insert-file-contents url))
313 (goto-char (point-min))
314 (while (re-search-forward "\r 1000\r ?" nil t)
316 (setq buffer-file-name name)
317 (if (and (fboundp 'url-generic-parse-url)
319 (setq url-current-object (url-generic-parse-url
324 (defun mm-url-insert-file-contents-external (url)
325 "Insert file contents of URL using `mm-url-program'."
327 (if (symbolp mm-url-program)
328 (let ((item (cdr (assq mm-url-program mm-url-predefined-programs))))
329 (setq program (car item)
330 args (append (cdr item) (list url))))
331 (setq program mm-url-program
332 args (append mm-url-arguments (list url))))
333 (unless (eq 0 (apply 'call-process program nil t nil args))
334 (error "Couldn't fetch %s" url))))
336 (defvar mm-url-timeout 30
337 "The number of seconds before timing out an URL fetch.")
339 (defvar mm-url-retries 10
340 "The number of retries after timing out when fetching an URL.")
342 (defun mm-url-insert (url &optional follow-refresh)
343 "Insert the contents from an URL in the current buffer.
344 If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
345 (let ((times mm-url-retries)
349 (while (and (not (zerop (decf times)))
351 (with-timeout (mm-url-timeout)
353 (message "Trying again (%s)..." (- mm-url-retries times)))
357 (narrow-to-region (point) (point))
358 (mm-url-insert-file-contents url)
359 (goto-char (point-min))
360 (when (re-search-forward
361 "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\"" nil t)
362 (let ((url (match-string 1)))
363 (delete-region (point-min) (point-max))
364 (setq result (mm-url-insert url t)))))
365 (setq result (mm-url-insert-file-contents url)))
369 (defun mm-url-decode-entities ()
370 "Decode all HTML entities."
371 (goto-char (point-min))
372 (while (re-search-forward "&\\(#[0-9]+\\|[a-z]+[0-9]*\\);" nil t)
373 (let ((elem (if (eq (aref (match-string 1) 0) ?\#)
375 (string-to-number (substring
376 (match-string 1) 1))))
377 (if (mm-char-or-char-int-p c) c 32))
378 (or (cdr (assq (intern (match-string 1))
379 mm-url-html-entities))
381 (unless (stringp elem)
382 (setq elem (char-to-string elem)))
383 (replace-match elem t t))))
385 (defun mm-url-decode-entities-nbsp ()
386 "Decode all HTML entities and to a space."
387 (let ((mm-url-html-entities (cons '(nbsp . 32) mm-url-html-entities)))
388 (mm-url-decode-entities)))
390 (defun mm-url-decode-entities-string (string)
393 (mm-url-decode-entities)
396 (defun mm-url-form-encode-xwfu (chunk)
397 "Escape characters in a string for application/x-www-form-urlencoded.
398 Blasphemous crap because someone didn't think %20 was good enough for encoding
399 spaces. Die Die Die."
400 ;; This will get rid of the 'attributes' specified by the file type,
401 ;; which are useless for an application/x-www-form-urlencoded form.
403 (setq chunk (cdr chunk)))
409 ((memq char mm-url-unreserved-chars) (char-to-string char))
410 (t (upcase (format "%%%02x" char)))))
411 ;; Fixme: Should this actually be accepting multibyte? Is there a
412 ;; better way in XEmacs?
414 (encode-coding-string chunk
415 (if (fboundp 'find-coding-systems-string)
416 (car (find-coding-systems-string chunk))
417 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 (defun mm-url-fetch-form (url pairs)
430 "Fetch a form from URL with PAIRS as the data using the POST method."
432 (let ((url-request-data (mm-url-encode-www-form-urlencoded pairs))
433 (url-request-method "POST")
434 (url-request-extra-headers
435 '(("Content-type" . "application/x-www-form-urlencoded"))))
436 (url-insert-file-contents url)
437 (setq buffer-file-name nil))
440 (defun mm-url-fetch-simple (url content)
442 (let ((url-request-data content)
443 (url-request-method "POST")
444 (url-request-extra-headers
445 '(("Content-type" . "application/x-www-form-urlencoded"))))
446 (url-insert-file-contents url)
447 (setq buffer-file-name nil))
450 (defun mm-url-remove-markup ()
451 "Remove all HTML markup, leaving just plain text."
452 (goto-char (point-min))
453 (while (search-forward "<!--" nil t)
454 (delete-region (match-beginning 0)
455 (or (search-forward "-->" nil t)
457 (goto-char (point-min))
458 (while (re-search-forward "<[^>]+>" nil t)
459 (replace-match "" t t)))
463 ;;; mm-url.el ends here