1 ;;; mm-url.el --- a wrapper of url functions/commands for Gnus
2 ;; Copyright (C) 2001, 2002 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 'executable-find "executable"))
41 "A wrapper of url package and external url command for Gnus."
44 (defcustom mm-url-use-external (not
48 "*If not-nil, use external grab program `mm-url-program'."
52 (defvar mm-url-predefined-programs
53 '((wget "wget" "-q" "-O" "-")
54 (w3m "w3m" "-dump_source")
55 (lynx "lynx" "-source")
58 (defcustom mm-url-program
60 ((executable-find "wget") 'wget)
61 ((executable-find "w3m") 'w3m)
62 ((executable-find "lynx") 'lynx)
63 ((executable-find "curl") 'curl)
65 "The url grab program."
67 (symbol :tag "wget" wget)
68 (symbol :tag "w3m" w3m)
69 (symbol :tag "lynx" lynx)
70 (symbol :tag "curl" curl)
71 (string :tag "other"))
74 (defcustom mm-url-arguments nil
75 "The arguments for `mm-url-program'."
76 :type '(repeat string)
80 ;;; Internal variables
82 (defvar mm-url-package-name
83 (gnus-replace-in-string
84 (gnus-replace-in-string gnus-version " v.*$" "")
87 (defvar mm-url-package-version gnus-version-number)
90 (defvar mm-url-html-entities
98 (rsquo . 39) ; should be U+8217
115 (uarr . 94) ; should be U+8593
117 (lsquo . 96) ; should be U+8216
219 ;; Special handling of these
233 ;; The following 5 entities are not mentioned in the HTML 2.0
234 ;; standard, nor in any other HTML proposed standard of which I
235 ;; am aware. I am not even sure they are ISO entity names. ***
236 ;; Hence, some arrangement should be made to give a bad HTML
237 ;; message when they are seen.
249 ;; (shy . ????) ; soft hyphen
251 "*An assoc list of entity names and how to actually display them.")
253 (defconst mm-url-unreserved-chars
255 ?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
256 ?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
257 ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
258 ?- ?_ ?. ?! ?~ ?* ?' ?\( ?\))
259 "A list of characters that are _NOT_ reserved in the URL spec.
260 This is taken from RFC 2396.")
262 (defun mm-url-load-url ()
263 "Load `url-insert-file-contents'."
264 (unless (condition-case ()
265 (require 'url-handlers)
267 ;; w3-4.0pre0.46 or earlier version.
271 (defun mm-url-insert-file-contents (url)
272 (if mm-url-use-external
273 (if (string-match "^file:/+" url)
274 (insert-file-contents (substring url (1- (match-end 0))))
275 (mm-url-insert-file-contents-external url))
277 (let ((name buffer-file-name)
278 (url-package-name (or mm-url-package-name
280 (url-package-version (or mm-url-package-version
281 url-package-version)))
283 (url-insert-file-contents url)
285 (goto-char (point-min))
286 (while (re-search-forward "\r 1000\r ?" nil t)
288 (setq buffer-file-name name)))))
290 (defun mm-url-insert-file-contents-external (url)
292 (if (symbolp mm-url-program)
293 (let ((item (cdr (assq mm-url-program mm-url-predefined-programs))))
294 (setq program (car item)
295 args (append (cdr item) (list url))))
296 (setq program mm-url-program
297 args (append mm-url-arguments (list url))))
298 (apply 'call-process program nil t nil args)))
300 (defun mm-url-insert (url &optional follow-refresh)
301 "Insert the contents from an URL in the current buffer.
302 If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
305 (narrow-to-region (point) (point))
306 (mm-url-insert-file-contents url)
307 (goto-char (point-min))
308 (when (re-search-forward
309 "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\"" nil t)
310 (let ((url (match-string 1)))
311 (delete-region (point-min) (point-max))
312 (mm-url-insert url t))))
313 (mm-url-insert-file-contents url)))
315 (defun mm-url-decode-entities ()
316 "Decode all HTML entities."
317 (goto-char (point-min))
318 (while (re-search-forward "&\\(#[0-9]+\\|[a-z]+\\);" nil t)
319 (let ((elem (if (eq (aref (match-string 1) 0) ?\#)
321 (string-to-number (substring
322 (match-string 1) 1))))
323 (if (mm-char-or-char-int-p c) c 32))
324 (or (cdr (assq (intern (match-string 1))
325 mm-url-html-entities))
327 (unless (stringp elem)
328 (setq elem (char-to-string elem)))
329 (replace-match elem t t))))
331 (defun mm-url-decode-entities-nbsp ()
332 "Decode all HTML entities and to a space."
333 (let ((mm-url-html-entities (cons '(nbsp . 32) mm-url-html-entities)))
334 (mm-url-decode-entities)))
336 (defun mm-url-decode-entities-string (string)
339 (mm-url-decode-entities)
340 (buffer-substring (point-min) (point-max))))
342 (defun mm-url-form-encode-xwfu (chunk)
343 "Escape characters in a string for application/x-www-form-urlencoded.
344 Blasphemous crap because someone didn't think %20 was good enough for encoding
345 spaces. Die Die Die."
346 ;; This will get rid of the 'attributes' specified by the file type,
347 ;; which are useless for an application/x-www-form-urlencoded form.
349 (setq chunk (cdr chunk)))
355 ((memq char mm-url-unreserved-chars) (char-to-string char))
356 (t (upcase (format "%%%02x" char)))))
357 ;; Fixme: Should this actually be accepting multibyte? Is there a
358 ;; better way in XEmacs?
360 (encode-coding-string chunk
361 (if (fboundp 'find-coding-systems-string)
362 (car (find-coding-systems-string chunk))
363 buffer-file-coding-system))
367 (defun mm-url-encode-www-form-urlencoded (pairs)
368 "Return PAIRS encoded for forms."
371 (concat (mm-url-form-encode-xwfu (car data)) "="
372 (mm-url-form-encode-xwfu (cdr data))))
375 (defun mm-url-fetch-form (url pairs)
376 "Fetch a form from URL with PAIRS as the data using the POST method."
378 (let ((url-request-data (mm-url-encode-www-form-urlencoded pairs))
379 (url-request-method "POST")
380 (url-request-extra-headers
381 '(("Content-type" . "application/x-www-form-urlencoded"))))
382 (url-insert-file-contents url)
383 (setq buffer-file-name nil))
386 (defun mm-url-fetch-simple (url content)
388 (let ((url-request-data content)
389 (url-request-method "POST")
390 (url-request-extra-headers
391 '(("Content-type" . "application/x-www-form-urlencoded"))))
392 (url-insert-file-contents url)
393 (setq buffer-file-name nil))
396 (defun mm-url-remove-markup ()
397 "Remove all HTML markup, leaving just plain text."
398 (goto-char (point-min))
399 (while (search-forward "<!--" nil t)
400 (delete-region (match-beginning 0)
401 (or (search-forward "-->" nil t)
403 (goto-char (point-min))
404 (while (re-search-forward "<[^>]+>" nil t)
405 (replace-match "" t t)))
409 ;;; mm-url.el ends here