* many files: Remove trailing whitespaces, replace spc+tab with
[gnus] / lisp / mm-url.el
index 39bf776..3e140ec 100644 (file)
@@ -1,5 +1,5 @@
 ;;; mm-url.el --- a wrapper of url functions/commands for Gnus
-;; Copyright (C) 2001 Free Software Foundation, Inc.
+;; Copyright (C) 2001, 2002 Free Software Foundation, Inc.
 
 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
 
 
 ;;; Code:
 
-(require 'mm-util)
-
 (eval-when-compile (require 'cl))
 
+(require 'mm-util)
+
 (eval-and-compile
-  (autoload 'url-insert-file-contents "url-handlers"))
+  (autoload 'executable-find "executable"))
 
 (defgroup mm-url nil
   "A wrapper of url package and external url command for Gnus."
@@ -42,7 +42,7 @@
 
 (defcustom mm-url-use-external (not
                                (condition-case nil
-                                   (require 'url-handlers)
+                                   (require 'url)
                                  (error nil)))
   "*If not-nil, use external grab program `mm-url-program'."
   :type 'boolean
 
 (defvar mm-url-predefined-programs
   '((wget "wget" "-q" "-O" "-")
+    (w3m  "w3m" "-dump_source")
     (lynx "lynx" "-source")
     (curl "curl")))
 
-(defcustom mm-url-program 
+(defcustom mm-url-program
   (cond
    ((executable-find "wget") 'wget)
+   ((executable-find "w3m") 'w3m)
    ((executable-find "lynx") 'lynx)
    ((executable-find "curl") 'curl)
    (t "GET"))
   "The url grab program."
-  :type '(choice 
+  :type '(choice
          (symbol :tag "wget" wget)
+         (symbol :tag "w3m" w3m)
          (symbol :tag "lynx" lynx)
          (symbol :tag "curl" curl)
          (string :tag "other"))
@@ -72,6 +75,9 @@
   :type '(repeat string)
   :group 'mm-url)
 
+\f
+;;; Internal variables
+
 ;; Stolen from w3.
 (defvar mm-url-html-entities
   '(
   "A list of characters that are _NOT_ reserved in the URL spec.
 This is taken from RFC 2396.")
 
+(defun mm-url-load-url ()
+  "Load `url-insert-file-contents'."
+  (unless (condition-case ()
+             (require 'url-handlers)
+           (error nil))
+    ;; w3-4.0pre0.46 or earlier version.
+    (require 'w3-vars)
+    (require 'url)))
+
 (defun mm-url-insert-file-contents (url)
   (if mm-url-use-external
-      (mm-url-insert-file-contents-external url)
-    (url-insert-file-contents url)))
+      (if (string-match "^file:/+" url)
+         (insert-file-contents (substring url (1- (match-end 0))))
+       (mm-url-insert-file-contents-external url))
+    (mm-url-load-url)
+    (let ((name buffer-file-name))
+      (prog1
+         (url-insert-file-contents url)
+       (setq buffer-file-name name)))))
 
 (defun mm-url-insert-file-contents-external (url)
   (let (program args)
@@ -263,19 +284,17 @@ This is taken from RFC 2396.")
 (defun mm-url-insert (url &optional follow-refresh)
   "Insert the contents from an URL in the current buffer.
 If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
-  (let ((name buffer-file-name))
-    (if follow-refresh
-       (save-restriction
-         (narrow-to-region (point) (point))
-         (mm-url-insert-file-contents url)
-         (goto-char (point-min))
-         (when (re-search-forward
-                "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\"" nil t)
-           (let ((url (match-string 1)))
-             (delete-region (point-min) (point-max))
-             (mm-url-insert url t))))
-      (mm-url-insert-file-contents url))
-    (setq buffer-file-name name)))
+  (if follow-refresh
+      (save-restriction
+       (narrow-to-region (point) (point))
+       (mm-url-insert-file-contents url)
+       (goto-char (point-min))
+       (when (re-search-forward
+              "<meta[ \t\r\n]*http-equiv=\"Refresh\"[^>]*URL=\\([^\"]+\\)\"" nil t)
+         (let ((url (match-string 1)))
+           (delete-region (point-min) (point-max))
+           (mm-url-insert url t))))
+    (mm-url-insert-file-contents url)))
 
 (defun mm-url-decode-entities ()
   "Decode all HTML entities."
@@ -293,6 +312,11 @@ If FOLLOW-REFRESH is non-nil, redirect refresh url in META."
        (setq elem (char-to-string elem)))
       (replace-match elem t t))))
 
+(defun mm-url-decode-entities-nbsp ()
+  "Decode all HTML entities and &nbsp; to a space."
+  (let ((mm-url-html-entities (cons '(nbsp . 32) mm-url-html-entities)))
+    (mm-url-decode-entities)))
+
 (defun mm-url-decode-entities-string (string)
   (with-temp-buffer
     (insert string)
@@ -324,6 +348,46 @@ spaces.  Die Die Die."
      chunk)
    ""))
 
+(defun mm-url-encode-www-form-urlencoded (pairs)
+  "Return PAIRS encoded for forms."
+  (mapconcat
+   (lambda (data)
+     (concat (mm-url-form-encode-xwfu (car data)) "="
+            (mm-url-form-encode-xwfu (cdr data))))
+   pairs "&"))
+
+(defun mm-url-fetch-form (url pairs)
+  "Fetch a form from URL with PAIRS as the data using the POST method."
+  (mm-url-load-url)
+  (let ((url-request-data (mm-url-encode-www-form-urlencoded pairs))
+       (url-request-method "POST")
+       (url-request-extra-headers
+        '(("Content-type" . "application/x-www-form-urlencoded"))))
+    (url-insert-file-contents url)
+    (setq buffer-file-name nil))
+  t)
+
+(defun mm-url-fetch-simple (url content)
+  (mm-url-load-url)
+  (let ((url-request-data content)
+       (url-request-method "POST")
+       (url-request-extra-headers
+        '(("Content-type" . "application/x-www-form-urlencoded"))))
+    (url-insert-file-contents url)
+    (setq buffer-file-name nil))
+  t)
+
+(defun mm-url-remove-markup ()
+  "Remove all HTML markup, leaving just plain text."
+  (goto-char (point-min))
+  (while (search-forward "<!--" nil t)
+    (delete-region (match-beginning 0)
+                  (or (search-forward "-->" nil t)
+                      (point-max))))
+  (goto-char (point-min))
+  (while (re-search-forward "<[^>]+>" nil t)
+    (replace-match "" t t)))
+
 (provide 'mm-url)
 
 ;;; mm-url.el ends here