1 ;;; mm-url.el --- a wrapper of url functions/commands for Gnus
2 ;; Copyright (C) 2001 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.
35 (eval-when-compile (require 'cl))
38 (autoload 'url-insert-file-contents "url-handlers"))
41 "A wrapper of url package and external url command for Gnus."
44 (defcustom mm-url-use-external (not
46 (require 'url-handlers)
48 "*If not-nil, use external grab program `mm-url-program'."
52 (defvar mm-url-predefined-programs
53 '((wget "wget" "-q" "-O" "-")
54 (lynx "lynx" "-source")
57 (defcustom mm-url-program
59 ((executable-find "wget") 'wget)
60 ((executable-find "lynx") 'lynx)
61 ((executable-find "curl") 'curl)
63 "The url grab program."
65 (symbol :tag "wget" wget)
66 (symbol :tag "lynx" lynx)
67 (symbol :tag "curl" curl)
68 (string :tag "other"))
71 (defcustom mm-url-arguments nil
72 "The arguments for `mm-url-program'."
73 :type '(repeat string)
77 (defvar mm-url-html-entities
85 (rsquo . 39) ; should be U+8217
102 (uarr . 94) ; should be U+8593
104 (lsquo . 96) ; should be U+8216
206 ;; Special handling of these
220 ;; The following 5 entities are not mentioned in the HTML 2.0
221 ;; standard, nor in any other HTML proposed standard of which I
222 ;; am aware. I am not even sure they are ISO entity names. ***
223 ;; Hence, some arrangement should be made to give a bad HTML
224 ;; message when they are seen.
236 ;; (shy . ????) ; soft hyphen
238 "*An assoc list of entity names and how to actually display them.")
240 (defconst mm-url-unreserved-chars
242 ?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
243 ?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
244 ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
245 ?- ?_ ?. ?! ?~ ?* ?' ?\( ?\))
246 "A list of characters that are _NOT_ reserved in the URL spec.
247 This is taken from RFC 2396.")
249 (defun mm-url-insert-file-contents (url)
250 (if mm-url-use-external
251 (mm-url-insert-file-contents-external url)
252 (url-insert-file-contents url)))
254 (defun mm-url-insert-file-contents-external (url)
256 (if (symbolp mm-url-program)
257 (let ((item (cdr (assq mm-url-program mm-url-predefined-programs))))
258 (setq program (car item)
259 args (append (cdr item) (list url))))
260 (setq program mm-url-program
261 args (append mm-url-arguments (list url))))
262 (apply 'call-process program nil t nil args)))
264 (defun mm-url-insert (url &optional follow-refresh)
265 "Insert the contents from an URL in the current buffer.
266 If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
267 (let ((name buffer-file-name))
270 (narrow-to-region (point) (point))
271 (mm-url-insert-file-contents url)
272 (goto-char (point-min))
273 (when (re-search-forward
274 "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\"" nil t)
275 (let ((url (match-string 1)))
276 (delete-region (point-min) (point-max))
277 (mm-url-insert url t))))
278 (mm-url-insert-file-contents url))
279 (setq buffer-file-name name)))
281 (defun mm-url-decode-entities ()
282 "Decode all HTML entities."
283 (goto-char (point-min))
284 (while (re-search-forward "&\\(#[0-9]+\\|[a-z]+\\);" nil t)
285 (let ((elem (if (eq (aref (match-string 1) 0) ?\#)
287 (string-to-number (substring
288 (match-string 1) 1))))
289 (if (mm-char-or-char-int-p c) c 32))
290 (or (cdr (assq (intern (match-string 1))
291 mm-url-html-entities))
293 (unless (stringp elem)
294 (setq elem (char-to-string elem)))
295 (replace-match elem t t))))
297 (defun mm-url-decode-entities-string (string)
300 (mm-url-decode-entities)
301 (buffer-substring (point-min) (point-max))))
303 (defun mm-url-form-encode-xwfu (chunk)
304 "Escape characters in a string for application/x-www-form-urlencoded.
305 Blasphemous crap because someone didn't think %20 was good enough for encoding
306 spaces. Die Die Die."
307 ;; This will get rid of the 'attributes' specified by the file type,
308 ;; which are useless for an application/x-www-form-urlencoded form.
310 (setq chunk (cdr chunk)))
316 ((memq char mm-url-unreserved-chars) (char-to-string char))
317 (t (upcase (format "%%%02x" char)))))
318 ;; Fixme: Should this actually be accepting multibyte? Is there a
319 ;; better way in XEmacs?
321 (encode-coding-string chunk
322 (if (fboundp 'find-coding-systems-string)
323 (car (find-coding-systems-string chunk))
324 buffer-file-coding-system))
330 ;;; mm-url.el ends here