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 'exec-installed-p "path-util"))
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'."
55 (defvar mm-url-predefined-programs
56 '((wget "wget" "--user-agent=mm-url" "-q" "-O" "-")
57 (w3m "w3m" "-dump_source")
58 (lynx "lynx" "-source")
61 (defcustom mm-url-program
63 ((exec-installed-p "wget") 'wget)
64 ((exec-installed-p "w3m") 'w3m)
65 ((exec-installed-p "lynx") 'lynx)
66 ((exec-installed-p "curl") 'curl)
68 "The url grab program.
69 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'."
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 ()
269 (require 'url-handlers)
271 ;; w3-4.0pre0.46 or earlier version.
276 (defun mm-url-insert-file-contents (url)
277 "Insert file contents of URL.
278 If `mm-url-use-external' is non-nil, use `mm-url-program'."
279 (if mm-url-use-external
281 (if (string-match "^file:/+" url)
282 (insert-file-contents (substring url (1- (match-end 0))))
283 (mm-url-insert-file-contents-external url))
284 (goto-char (point-min))
285 (if (fboundp 'url-generic-parse-url)
286 (setq url-current-object
287 (url-generic-parse-url url)))
288 (list url (buffer-size)))
290 (let ((name buffer-file-name)
291 (url-request-extra-headers (list (cons "Connection" "Close")))
292 (url-package-name (or mm-url-package-name
294 (url-package-version (or mm-url-package-version
295 url-package-version))
297 (setq result (url-insert-file-contents url))
299 (goto-char (point-min))
300 (while (re-search-forward "\r 1000\r ?" nil t)
302 (setq buffer-file-name name)
303 (if (and (fboundp 'url-generic-parse-url)
305 (setq url-current-object (url-generic-parse-url
310 (defun mm-url-insert-file-contents-external (url)
311 "Insert file contents of URL using `mm-url-program'."
313 (if (symbolp mm-url-program)
314 (let ((item (cdr (assq mm-url-program mm-url-predefined-programs))))
315 (setq program (car item)
316 args (append (cdr item) (list url))))
317 (setq program mm-url-program
318 args (append mm-url-arguments (list url))))
319 (unless (eq 0 (apply 'call-process program nil t nil args))
320 (error "Couldn't fetch %s" url))))
322 (defvar mm-url-timeout 30
323 "The number of seconds before timing out an URL fetch.")
325 (defvar mm-url-retries 10
326 "The number of retries after timing out when fetching an URL.")
328 (defun mm-url-insert (url &optional follow-refresh)
329 "Insert the contents from an URL in the current buffer.
330 If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
331 (let ((times mm-url-retries)
335 (while (and (not (zerop (decf times)))
337 (with-timeout (mm-url-timeout)
339 (message "Trying again (%s)..." (- mm-url-retries times)))
343 (narrow-to-region (point) (point))
344 (mm-url-insert-file-contents url)
345 (goto-char (point-min))
346 (when (re-search-forward
347 "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\"" nil t)
348 (let ((url (match-string 1)))
349 (delete-region (point-min) (point-max))
350 (setq result (mm-url-insert url t)))))
351 (setq result (mm-url-insert-file-contents url)))
355 (defun mm-url-decode-entities ()
356 "Decode all HTML entities."
357 (goto-char (point-min))
358 (while (re-search-forward "&\\(#[0-9]+\\|[a-z]+\\);" nil t)
359 (let ((elem (if (eq (aref (match-string 1) 0) ?\#)
361 (string-to-number (substring
362 (match-string 1) 1))))
363 (if (mm-char-or-char-int-p c) c 32))
364 (or (cdr (assq (intern (match-string 1))
365 mm-url-html-entities))
367 (unless (stringp elem)
368 (setq elem (char-to-string elem)))
369 (replace-match elem t t))))
371 (defun mm-url-decode-entities-nbsp ()
372 "Decode all HTML entities and to a space."
373 (let ((mm-url-html-entities (cons '(nbsp . 32) mm-url-html-entities)))
374 (mm-url-decode-entities)))
376 (defun mm-url-decode-entities-string (string)
379 (mm-url-decode-entities)
382 (defun mm-url-form-encode-xwfu (chunk)
383 "Escape characters in a string for application/x-www-form-urlencoded.
384 Blasphemous crap because someone didn't think %20 was good enough for encoding
385 spaces. Die Die Die."
386 ;; This will get rid of the 'attributes' specified by the file type,
387 ;; which are useless for an application/x-www-form-urlencoded form.
389 (setq chunk (cdr chunk)))
395 ((memq char mm-url-unreserved-chars) (char-to-string char))
396 (t (upcase (format "%%%02x" char)))))
397 ;; Fixme: Should this actually be accepting multibyte? Is there a
398 ;; better way in XEmacs?
400 (encode-coding-string chunk
401 (if (fboundp 'find-coding-systems-string)
402 (car (find-coding-systems-string chunk))
403 buffer-file-coding-system))
407 (defun mm-url-encode-www-form-urlencoded (pairs)
408 "Return PAIRS encoded for forms."
411 (concat (mm-url-form-encode-xwfu (car data)) "="
412 (mm-url-form-encode-xwfu (cdr data))))
415 (defun mm-url-fetch-form (url pairs)
416 "Fetch a form from URL with PAIRS as the data using the POST method."
418 (let ((url-request-data (mm-url-encode-www-form-urlencoded pairs))
419 (url-request-method "POST")
420 (url-request-extra-headers
421 '(("Content-type" . "application/x-www-form-urlencoded"))))
422 (url-insert-file-contents url)
423 (setq buffer-file-name nil))
426 (defun mm-url-fetch-simple (url content)
428 (let ((url-request-data content)
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-remove-markup ()
437 "Remove all HTML markup, leaving just plain text."
438 (goto-char (point-min))
439 (while (search-forward "<!--" nil t)
440 (delete-region (match-beginning 0)
441 (or (search-forward "-->" nil t)
443 (goto-char (point-min))
444 (while (re-search-forward "<[^>]+>" nil t)
445 (replace-match "" t t)))
449 ;;; mm-url.el ends here