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.
32 (eval-when-compile (require 'cl))
39 (autoload 'exec-installed-p "path-util")
40 ;;(autoload 'url-insert-file-contents "url-handlers")
41 (autoload 'url-insert-file-contents "url")
45 "A wrapper of url package and external url command for Gnus."
48 (defcustom mm-url-use-external nil
49 "*If not-nil, use external grab program `mm-url-program'."
53 (defvar mm-url-predefined-programs
54 '((wget "wget" "-q" "-O" "-")
55 (lynx "lynx" "-source")
58 (defcustom mm-url-program
60 ((exec-installed-p "wget") 'wget)
61 ((exec-installed-p "lynx") 'lynx)
62 ((exec-installed-p "curl") 'curl)
64 "The url grab program."
66 (symbol :tag "wget" wget)
67 (symbol :tag "lynx" lynx)
68 (symbol :tag "curl" curl)
69 (string :tag "other"))
72 (defcustom mm-url-arguments nil
73 "The arguments for `mm-url-program'."
74 :type '(repeat string)
78 (defvar mm-url-html-entities
86 (rsquo . 39) ; should be U+8217
103 (uarr . 94) ; should be U+8593
105 (lsquo . 96) ; should be U+8216
207 ;; Special handling of these
221 ;; The following 5 entities are not mentioned in the HTML 2.0
222 ;; standard, nor in any other HTML proposed standard of which I
223 ;; am aware. I am not even sure they are ISO entity names. ***
224 ;; Hence, some arrangement should be made to give a bad HTML
225 ;; message when they are seen.
237 ;; (shy . ????) ; soft hyphen
239 "*An assoc list of entity names and how to actually display them.")
241 (defconst mm-url-unreserved-chars
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 ?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
245 ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
246 ?- ?_ ?. ?! ?~ ?* ?' ?\( ?\))
247 "A list of characters that are _NOT_ reserved in the URL spec.
248 This is taken from RFC 2396.")
250 (defun mm-url-insert-file-contents (url)
251 (if mm-url-use-external
252 (if (string-match "^file:/+" url)
253 (insert-file-contents (substring url (1- (match-end 0))))
254 (mm-url-insert-file-contents-external url))
255 ;;(require 'url-handlers)
256 (let ((name buffer-file-name))
258 (url-insert-file-contents url)
259 (setq buffer-file-name name)))))
261 (defun mm-url-insert-file-contents-external (url)
263 (if (symbolp mm-url-program)
264 (let ((item (cdr (assq mm-url-program mm-url-predefined-programs))))
265 (setq program (car item)
266 args (append (cdr item) (list url))))
267 (setq program mm-url-program
268 args (append mm-url-arguments (list url))))
269 (apply 'call-process program nil t nil args)))
271 (defun mm-url-insert (url &optional follow-refresh)
272 "Insert the contents from an URL in the current buffer.
273 If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
276 (narrow-to-region (point) (point))
277 (mm-url-insert-file-contents url)
278 (goto-char (point-min))
279 (when (re-search-forward
280 "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\"" nil t)
281 (let ((url (match-string 1)))
282 (delete-region (point-min) (point-max))
283 (mm-url-insert url t))))
284 (mm-url-insert-file-contents url)))
286 (defun mm-url-decode-entities ()
287 "Decode all HTML entities."
288 (goto-char (point-min))
289 (while (re-search-forward "&\\(#[0-9]+\\|[a-z]+\\);" nil t)
290 (let ((elem (if (eq (aref (match-string 1) 0) ?\#)
292 (string-to-number (substring
293 (match-string 1) 1))))
294 (if (mm-char-or-char-int-p c) c 32))
295 (or (cdr (assq (intern (match-string 1))
296 mm-url-html-entities))
298 (unless (stringp elem)
299 (setq elem (char-to-string elem)))
300 (replace-match elem t t))))
302 (defun mm-url-decode-entities-nbsp ()
303 "Decode all HTML entities and to a space."
304 (let ((mm-url-html-entities (cons '(nbsp . 32) mm-url-html-entities)))
305 (mm-url-decode-entities)))
307 (defun mm-url-decode-entities-string (string)
310 (mm-url-decode-entities)
311 (buffer-substring (point-min) (point-max))))
313 (defun mm-url-form-encode-xwfu (chunk)
314 "Escape characters in a string for application/x-www-form-urlencoded.
315 Blasphemous crap because someone didn't think %20 was good enough for encoding
316 spaces. Die Die Die."
317 ;; This will get rid of the 'attributes' specified by the file type,
318 ;; which are useless for an application/x-www-form-urlencoded form.
320 (setq chunk (cdr chunk)))
326 ((memq char mm-url-unreserved-chars) (char-to-string char))
327 (t (upcase (format "%%%02x" char)))))
328 ;; Fixme: Should this actually be accepting multibyte? Is there a
329 ;; better way in XEmacs?
331 (encode-coding-string chunk
332 (if (fboundp 'find-coding-systems-string)
333 (car (find-coding-systems-string chunk))
334 buffer-file-coding-system))
338 (defun mm-url-encode-www-form-urlencoded (pairs)
339 "Return PAIRS encoded for forms."
342 (concat (mm-url-form-encode-xwfu (car data)) "="
343 (mm-url-form-encode-xwfu (cdr data))))
346 (defun mm-url-fetch-form (url pairs)
347 "Fetch a form from URL with PAIRS as the data using the POST method."
348 ;;(require 'url-handlers)
349 (let ((url-request-data (mm-url-encode-www-form-urlencoded pairs))
350 (url-request-method "POST")
351 (url-request-extra-headers
352 '(("Content-type" . "application/x-www-form-urlencoded"))))
353 (url-insert-file-contents url)
354 (setq buffer-file-name nil))
357 (defun mm-url-fetch-simple (url content)
358 ;;(require 'url-handlers)
359 (let ((url-request-data content)
360 (url-request-method "POST")
361 (url-request-extra-headers
362 '(("Content-type" . "application/x-www-form-urlencoded"))))
363 (url-insert-file-contents url)
364 (setq buffer-file-name nil))
367 (defun mm-url-remove-markup ()
368 "Remove all HTML markup, leaving just plain text."
369 (goto-char (point-min))
370 (while (search-forward "<!--" nil t)
371 (delete-region (match-beginning 0)
372 (or (search-forward "-->" nil t)
374 (goto-char (point-min))
375 (while (re-search-forward "<[^>]+>" nil t)
376 (replace-match "" t t)))
380 ;;; mm-url.el ends here