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 (if (featurep 'xemacs)
40 (require 'timer-funcs)
43 (defvar url-current-object)
44 (defvar url-package-name)
45 (defvar url-package-version)
48 "A wrapper of url package and external url command for Gnus."
51 (defcustom mm-url-use-external (not
55 "*If non-nil, use external grab program `mm-url-program'."
60 (defvar mm-url-predefined-programs
61 '((wget "wget" "--user-agent=mm-url" "-q" "-O" "-")
62 (w3m "w3m" "-dump_source")
63 (lynx "lynx" "-source")
64 (curl "curl" "--silent" "--user-agent mm-url" "--location")))
66 (defcustom mm-url-program
68 ((executable-find "wget") 'wget)
69 ((executable-find "w3m") 'w3m)
70 ((executable-find "lynx") 'lynx)
71 ((executable-find "curl") 'curl)
73 "The url grab program.
74 Likely values are `wget', `w3m', `lynx' and `curl'."
77 (symbol :tag "wget" wget)
78 (symbol :tag "w3m" w3m)
79 (symbol :tag "lynx" lynx)
80 (symbol :tag "curl" curl)
81 (string :tag "other"))
84 (defcustom mm-url-arguments nil
85 "The arguments for `mm-url-program'."
87 :type '(repeat string)
91 ;;; Internal variables
93 (defvar mm-url-package-name
94 (gnus-replace-in-string
95 (gnus-replace-in-string gnus-version " v.*$" "")
98 (defvar mm-url-package-version gnus-version-number)
101 (defvar mm-url-html-entities
109 (rsquo . 39) ; should be U+8217
126 (uarr . 94) ; should be U+8593
128 (lsquo . 96) ; should be U+8216
230 ;; Special handling of these
244 ;; The following 5 entities are not mentioned in the HTML 2.0
245 ;; standard, nor in any other HTML proposed standard of which I
246 ;; am aware. I am not even sure they are ISO entity names. ***
247 ;; Hence, some arrangement should be made to give a bad HTML
248 ;; message when they are seen.
260 ;; (shy . ????) ; soft hyphen
262 "*An assoc list of entity names and how to actually display them.")
264 (defconst mm-url-unreserved-chars
266 ?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
267 ?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
268 ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
269 ?- ?_ ?. ?! ?~ ?* ?' ?\( ?\))
270 "A list of characters that are _NOT_ reserved in the URL spec.
271 This is taken from RFC 2396.")
273 (defun mm-url-load-url ()
274 "Load `url-insert-file-contents'."
275 (unless (condition-case ()
276 (require 'url-handlers)
278 ;; w3-4.0pre0.46 or earlier version.
283 (defun mm-url-insert-file-contents (url)
284 "Insert file contents of URL.
285 If `mm-url-use-external' is non-nil, use `mm-url-program'."
286 (if mm-url-use-external
288 (if (string-match "^file:/+" url)
289 (insert-file-contents (substring url (1- (match-end 0))))
290 (mm-url-insert-file-contents-external url))
291 (goto-char (point-min))
292 (if (fboundp 'url-generic-parse-url)
293 (setq url-current-object
294 (url-generic-parse-url url)))
295 (list url (buffer-size)))
297 (let ((name buffer-file-name)
298 (url-request-extra-headers (list (cons "Connection" "Close")))
299 (url-package-name (or mm-url-package-name
301 (url-package-version (or mm-url-package-version
302 url-package-version))
304 (setq result (url-insert-file-contents url))
306 (goto-char (point-min))
307 (while (re-search-forward "\r 1000\r ?" nil t)
309 (setq buffer-file-name name)
310 (if (and (fboundp 'url-generic-parse-url)
312 (setq url-current-object (url-generic-parse-url
317 (defun mm-url-insert-file-contents-external (url)
318 "Insert file contents of URL using `mm-url-program'."
320 (if (symbolp mm-url-program)
321 (let ((item (cdr (assq mm-url-program mm-url-predefined-programs))))
322 (setq program (car item)
323 args (append (cdr item) (list url))))
324 (setq program mm-url-program
325 args (append mm-url-arguments (list url))))
326 (unless (eq 0 (apply 'call-process program nil t nil args))
327 (error "Couldn't fetch %s" url))))
329 (defvar mm-url-timeout 30
330 "The number of seconds before timing out an URL fetch.")
332 (defvar mm-url-retries 10
333 "The number of retries after timing out when fetching an URL.")
335 (defun mm-url-insert (url &optional follow-refresh)
336 "Insert the contents from an URL in the current buffer.
337 If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
338 (let ((times mm-url-retries)
342 (while (and (not (zerop (decf times)))
344 (with-timeout (mm-url-timeout)
346 (message "Trying again (%s)..." (- mm-url-retries times)))
350 (narrow-to-region (point) (point))
351 (mm-url-insert-file-contents url)
352 (goto-char (point-min))
353 (when (re-search-forward
354 "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\"" nil t)
355 (let ((url (match-string 1)))
356 (delete-region (point-min) (point-max))
357 (setq result (mm-url-insert url t)))))
358 (setq result (mm-url-insert-file-contents url)))
362 (defun mm-url-decode-entities ()
363 "Decode all HTML entities."
364 (goto-char (point-min))
365 (while (re-search-forward "&\\(#[0-9]+\\|[a-z]+[0-9]*\\);" nil t)
366 (let ((elem (if (eq (aref (match-string 1) 0) ?\#)
368 (string-to-number (substring
369 (match-string 1) 1))))
370 (if (mm-char-or-char-int-p c) c 32))
371 (or (cdr (assq (intern (match-string 1))
372 mm-url-html-entities))
374 (unless (stringp elem)
375 (setq elem (char-to-string elem)))
376 (replace-match elem t t))))
378 (defun mm-url-decode-entities-nbsp ()
379 "Decode all HTML entities and to a space."
380 (let ((mm-url-html-entities (cons '(nbsp . 32) mm-url-html-entities)))
381 (mm-url-decode-entities)))
383 (defun mm-url-decode-entities-string (string)
386 (mm-url-decode-entities)
389 (defun mm-url-form-encode-xwfu (chunk)
390 "Escape characters in a string for application/x-www-form-urlencoded.
391 Blasphemous crap because someone didn't think %20 was good enough for encoding
392 spaces. Die Die Die."
393 ;; This will get rid of the 'attributes' specified by the file type,
394 ;; which are useless for an application/x-www-form-urlencoded form.
396 (setq chunk (cdr chunk)))
402 ((memq char mm-url-unreserved-chars) (char-to-string char))
403 (t (upcase (format "%%%02x" char)))))
404 ;; Fixme: Should this actually be accepting multibyte? Is there a
405 ;; better way in XEmacs?
407 (encode-coding-string chunk
408 (if (fboundp 'find-coding-systems-string)
409 (car (find-coding-systems-string chunk))
410 buffer-file-coding-system))
414 (defun mm-url-encode-www-form-urlencoded (pairs)
415 "Return PAIRS encoded for forms."
418 (concat (mm-url-form-encode-xwfu (car data)) "="
419 (mm-url-form-encode-xwfu (cdr data))))
422 (defun mm-url-fetch-form (url pairs)
423 "Fetch a form from URL with PAIRS as the data using the POST method."
425 (let ((url-request-data (mm-url-encode-www-form-urlencoded pairs))
426 (url-request-method "POST")
427 (url-request-extra-headers
428 '(("Content-type" . "application/x-www-form-urlencoded"))))
429 (url-insert-file-contents url)
430 (setq buffer-file-name nil))
433 (defun mm-url-fetch-simple (url content)
435 (let ((url-request-data content)
436 (url-request-method "POST")
437 (url-request-extra-headers
438 '(("Content-type" . "application/x-www-form-urlencoded"))))
439 (url-insert-file-contents url)
440 (setq buffer-file-name nil))
443 (defun mm-url-remove-markup ()
444 "Remove all HTML markup, leaving just plain text."
445 (goto-char (point-min))
446 (while (search-forward "<!--" nil t)
447 (delete-region (match-beginning 0)
448 (or (search-forward "-->" nil t)
450 (goto-char (point-min))
451 (while (re-search-forward "<[^>]+>" nil t)
452 (replace-match "" t t)))
456 ;;; arch-tag: 0594f9b3-417c-48b0-adc2-5082e1e7917f
457 ;;; mm-url.el ends here