1 ;;; mm-url.el --- a wrapper of url functions/commands for Gnus
2 ;; Copyright (C) 2001, 2002, 2003, 2004 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 (if (featurep 'xemacs)
39 (require 'timer-funcs)
43 "A wrapper of url package and external url command for Gnus."
46 (defcustom mm-url-use-external (not
50 "*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")
59 (curl "curl" "--silent")))
61 (defcustom mm-url-program
63 ((executable-find "wget") 'wget)
64 ((executable-find "w3m") 'w3m)
65 ((executable-find "lynx") 'lynx)
66 ((executable-find "curl") 'curl)
68 "The url grab program.
69 Likely values are `wget', `w3m', `lynx' and `curl'."
72 (symbol :tag "wget" wget)
73 (symbol :tag "w3m" w3m)
74 (symbol :tag "lynx" lynx)
75 (symbol :tag "curl" curl)
76 (string :tag "other"))
79 (defcustom mm-url-arguments nil
80 "The arguments for `mm-url-program'."
82 :type '(repeat string)
86 ;;; Internal variables
88 (defvar mm-url-package-name
89 (gnus-replace-in-string
90 (gnus-replace-in-string gnus-version " v.*$" "")
93 (defvar mm-url-package-version gnus-version-number)
96 (defvar mm-url-html-entities
104 (rsquo . 39) ; should be U+8217
121 (uarr . 94) ; should be U+8593
123 (lsquo . 96) ; should be U+8216
225 ;; Special handling of these
239 ;; The following 5 entities are not mentioned in the HTML 2.0
240 ;; standard, nor in any other HTML proposed standard of which I
241 ;; am aware. I am not even sure they are ISO entity names. ***
242 ;; Hence, some arrangement should be made to give a bad HTML
243 ;; message when they are seen.
255 ;; (shy . ????) ; soft hyphen
257 "*An assoc list of entity names and how to actually display them.")
259 (defconst mm-url-unreserved-chars
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 ?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 ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
264 ?- ?_ ?. ?! ?~ ?* ?' ?\( ?\))
265 "A list of characters that are _NOT_ reserved in the URL spec.
266 This is taken from RFC 2396.")
268 (defun mm-url-load-url ()
269 "Load `url-insert-file-contents'."
270 (unless (condition-case ()
271 (require 'url-handlers)
273 ;; w3-4.0pre0.46 or earlier version.
278 (defun mm-url-insert-file-contents (url)
279 "Insert file contents of URL.
280 If `mm-url-use-external' is non-nil, use `mm-url-program'."
281 (if mm-url-use-external
283 (if (string-match "^file:/+" url)
284 (insert-file-contents (substring url (1- (match-end 0))))
285 (mm-url-insert-file-contents-external url))
286 (goto-char (point-min))
287 (if (fboundp 'url-generic-parse-url)
288 (setq url-current-object
289 (url-generic-parse-url url)))
290 (list url (buffer-size)))
292 (let ((name buffer-file-name)
293 (url-request-extra-headers (list (cons "Connection" "Close")))
294 (url-package-name (or mm-url-package-name
296 (url-package-version (or mm-url-package-version
297 url-package-version))
299 (setq result (url-insert-file-contents url))
301 (goto-char (point-min))
302 (while (re-search-forward "\r 1000\r ?" nil t)
304 (setq buffer-file-name name)
305 (if (and (fboundp 'url-generic-parse-url)
307 (setq url-current-object (url-generic-parse-url
312 (defun mm-url-insert-file-contents-external (url)
313 "Insert file contents of URL using `mm-url-program'."
315 (if (symbolp mm-url-program)
316 (let ((item (cdr (assq mm-url-program mm-url-predefined-programs))))
317 (setq program (car item)
318 args (append (cdr item) (list url))))
319 (setq program mm-url-program
320 args (append mm-url-arguments (list url))))
321 (unless (eq 0 (apply 'call-process program nil t nil args))
322 (error "Couldn't fetch %s" url))))
324 (defvar mm-url-timeout 30
325 "The number of seconds before timing out an URL fetch.")
327 (defvar mm-url-retries 10
328 "The number of retries after timing out when fetching an URL.")
330 (defun mm-url-insert (url &optional follow-refresh)
331 "Insert the contents from an URL in the current buffer.
332 If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
333 (let ((times mm-url-retries)
337 (while (and (not (zerop (decf times)))
339 (with-timeout (mm-url-timeout)
341 (message "Trying again (%s)..." (- mm-url-retries times)))
345 (narrow-to-region (point) (point))
346 (mm-url-insert-file-contents url)
347 (goto-char (point-min))
348 (when (re-search-forward
349 "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\"" nil t)
350 (let ((url (match-string 1)))
351 (delete-region (point-min) (point-max))
352 (setq result (mm-url-insert url t)))))
353 (setq result (mm-url-insert-file-contents url)))
357 (defun mm-url-decode-entities ()
358 "Decode all HTML entities."
359 (goto-char (point-min))
360 (while (re-search-forward "&\\(#[0-9]+\\|[a-z]+\\);" nil t)
361 (let ((elem (if (eq (aref (match-string 1) 0) ?\#)
363 (string-to-number (substring
364 (match-string 1) 1))))
365 (if (mm-char-or-char-int-p c) c 32))
366 (or (cdr (assq (intern (match-string 1))
367 mm-url-html-entities))
369 (unless (stringp elem)
370 (setq elem (char-to-string elem)))
371 (replace-match elem t t))))
373 (defun mm-url-decode-entities-nbsp ()
374 "Decode all HTML entities and to a space."
375 (let ((mm-url-html-entities (cons '(nbsp . 32) mm-url-html-entities)))
376 (mm-url-decode-entities)))
378 (defun mm-url-decode-entities-string (string)
381 (mm-url-decode-entities)
384 (defun mm-url-form-encode-xwfu (chunk)
385 "Escape characters in a string for application/x-www-form-urlencoded.
386 Blasphemous crap because someone didn't think %20 was good enough for encoding
387 spaces. Die Die Die."
388 ;; This will get rid of the 'attributes' specified by the file type,
389 ;; which are useless for an application/x-www-form-urlencoded form.
391 (setq chunk (cdr chunk)))
397 ((memq char mm-url-unreserved-chars) (char-to-string char))
398 (t (upcase (format "%%%02x" char)))))
399 ;; Fixme: Should this actually be accepting multibyte? Is there a
400 ;; better way in XEmacs?
402 (encode-coding-string chunk
403 (if (fboundp 'find-coding-systems-string)
404 (car (find-coding-systems-string chunk))
405 buffer-file-coding-system))
409 (defun mm-url-encode-www-form-urlencoded (pairs)
410 "Return PAIRS encoded for forms."
413 (concat (mm-url-form-encode-xwfu (car data)) "="
414 (mm-url-form-encode-xwfu (cdr data))))
417 (defun mm-url-fetch-form (url pairs)
418 "Fetch a form from URL with PAIRS as the data using the POST method."
420 (let ((url-request-data (mm-url-encode-www-form-urlencoded pairs))
421 (url-request-method "POST")
422 (url-request-extra-headers
423 '(("Content-type" . "application/x-www-form-urlencoded"))))
424 (url-insert-file-contents url)
425 (setq buffer-file-name nil))
428 (defun mm-url-fetch-simple (url content)
430 (let ((url-request-data content)
431 (url-request-method "POST")
432 (url-request-extra-headers
433 '(("Content-type" . "application/x-www-form-urlencoded"))))
434 (url-insert-file-contents url)
435 (setq buffer-file-name nil))
438 (defun mm-url-remove-markup ()
439 "Remove all HTML markup, leaving just plain text."
440 (goto-char (point-min))
441 (while (search-forward "<!--" nil t)
442 (delete-region (match-beginning 0)
443 (or (search-forward "-->" nil t)
445 (goto-char (point-min))
446 (while (re-search-forward "<[^>]+>" nil t)
447 (replace-match "" t t)))
451 ;;; arch-tag: 0594f9b3-417c-48b0-adc2-5082e1e7917f
452 ;;; mm-url.el ends here