Merge flim-1_11_3.
[elisp/flim.git] / eword-decode.el
index 5280aeb..d85bce3 100644 (file)
@@ -4,7 +4,7 @@
 
 ;; Author: ENAMI Tsugutomo <enami@sys.ptg.sony.co.jp>
 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
-;; Maintainer: MORIOKA Tomohiko <morioka@jaist.ac.jp>
+;;         TANAKA Akira <akr@jaist.ac.jp>
 ;; Created: 1995/10/03
 ;; Original: 1992/07/20 ENAMI Tsugutomo's `mime.el'.
 ;;     Renamed: 1993/06/03 to tiny-mime.el
   "Encoded-word decoding"
   :group 'mime)
 
+(defcustom eword-max-size-to-decode 1000
+  "*Max size to decode header field."
+  :group 'eword-decode
+  :type '(choice (integer :tag "Limit (bytes)")
+                (const :tag "Don't limit" nil)))
+
 
 ;;; @ MIME encoded-word definition
 ;;;
 
-(defconst eword-encoded-text-regexp "[!->@-~]+")
+(eval-and-compile
+  (defconst eword-encoded-text-regexp "[!->@-~]+")
+  )
 (defconst eword-encoded-word-regexp
-  (concat (regexp-quote "=?")
-         "\\("
-         mime-charset-regexp
-         "\\)"
-         (regexp-quote "?")
-         "\\(B\\|Q\\)"
-         (regexp-quote "?")
-         "\\("
-         eword-encoded-text-regexp
-         "\\)"
-         (regexp-quote "?=")))
-
-
-;;; @@ Base64
-;;;
-
-(defconst base64-token-regexp "[A-Za-z0-9+/]")
-(defconst base64-token-padding-regexp "[A-Za-z0-9+/=]")
-
-(defconst eword-B-encoded-text-regexp
-  (concat "\\(\\("
-         base64-token-regexp
-         base64-token-regexp
-         base64-token-regexp
-         base64-token-regexp
-         "\\)*"
-         base64-token-regexp
-         base64-token-regexp
-         base64-token-padding-regexp
-         base64-token-padding-regexp
-          "\\)"))
-
-;; (defconst eword-B-encoding-and-encoded-text-regexp
-;;   (concat "\\(B\\)\\?" eword-B-encoded-text-regexp))
-
-
-;;; @@ Quoted-Printable
-;;;
-
-(defconst eword-Q-encoded-text-regexp
-  (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
-;; (defconst eword-Q-encoding-and-encoded-text-regexp
-;;   (concat "\\(Q\\)\\?" eword-Q-encoded-text-regexp))
+  (eval-when-compile
+    (concat (regexp-quote "=?")
+           "\\("
+           mime-charset-regexp
+           "\\)"
+           (regexp-quote "?")
+           "\\(B\\|Q\\)"
+           (regexp-quote "?")
+           "\\("
+           eword-encoded-text-regexp
+           "\\)"
+           (regexp-quote "?="))))
 
 
 ;;; @ for string
@@ -130,6 +107,130 @@ such as a version of Net$cape)."
     (concat dest string)
     ))
 
+(defun eword-decode-and-fold-structured-field
+  (string start-column &optional max-column must-unfold)
+  "Decode and fold (fill) STRING as structured field body.
+It decodes non us-ascii characters in FULL-NAME encoded as
+encoded-words or invalid \"raw\" string.  \"Raw\" non us-ascii
+characters are regarded as variable `default-mime-charset'.
+
+If an encoded-word is broken or your emacs implementation can not
+decode the charset included in it, it is not decoded.
+
+If MAX-COLUMN is omitted, `fill-column' is used.
+
+If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
+if there are in decoded encoded-words (generated by bad manner MUA
+such as a version of Net$cape)."
+  (if (and eword-max-size-to-decode
+          (> (length string) eword-max-size-to-decode))
+      string
+    (or max-column
+       (setq max-column fill-column))
+    (let ((c start-column)
+         (tokens (eword-lexical-analyze string must-unfold))
+         (result "")
+         token)
+      (while (and (setq token (car tokens))
+                 (setq tokens (cdr tokens)))
+       (let* ((type (car token)))
+         (if (eq type 'spaces)
+             (let* ((next-token (car tokens))
+                    (next-str (eword-decode-token next-token))
+                    (next-len (string-width next-str))
+                    (next-c (+ c next-len 1)))
+               (if (< next-c max-column)
+                   (setq result (concat result " " next-str)
+                         c next-c)
+                 (setq result (concat result "\n " next-str)
+                       c (1+ next-len)))
+               (setq tokens (cdr tokens))
+               )
+           (let* ((str (eword-decode-token token)))
+             (setq result (concat result str)
+                   c (+ c (string-width str)))
+             ))))
+      (if token
+         (concat result (eword-decode-token token))
+       result))))
+
+(defun eword-decode-and-unfold-structured-field (string)
+  "Decode and unfold STRING as structured field body.
+It decodes non us-ascii characters in FULL-NAME encoded as
+encoded-words or invalid \"raw\" string.  \"Raw\" non us-ascii
+characters are regarded as variable `default-mime-charset'.
+
+If an encoded-word is broken or your emacs implementation can not
+decode the charset included in it, it is not decoded."
+  (let ((tokens (eword-lexical-analyze string 'must-unfold))
+       (result ""))
+    (while tokens
+      (let* ((token (car tokens))
+            (type (car token)))
+       (setq tokens (cdr tokens))
+       (setq result
+             (if (eq type 'spaces)
+                 (concat result " ")
+               (concat result (eword-decode-token token))
+               ))))
+    result))
+
+(defun eword-decode-structured-field-body (string &optional must-unfold
+                                                 start-column max-column)
+  "Decode non us-ascii characters in STRING as structured field body.
+STRING is unfolded before decoding.
+
+It decodes non us-ascii characters in FULL-NAME encoded as
+encoded-words or invalid \"raw\" string.  \"Raw\" non us-ascii
+characters are regarded as variable `default-mime-charset'.
+
+If an encoded-word is broken or your emacs implementation can not
+decode the charset included in it, it is not decoded.
+
+If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
+if there are in decoded encoded-words (generated by bad manner MUA
+such as a version of Net$cape)."
+  (if start-column
+      ;; fold with max-column
+      (eword-decode-and-fold-structured-field
+       string start-column max-column must-unfold)
+    ;; Don't fold
+    (mapconcat (function eword-decode-token)
+              (eword-lexical-analyze string must-unfold)
+              "")
+    ))
+
+(defun eword-decode-unstructured-field-body (string &optional must-unfold)
+  "Decode non us-ascii characters in STRING as unstructured field body.
+STRING is unfolded before decoding.
+
+It decodes non us-ascii characters in FULL-NAME encoded as
+encoded-words or invalid \"raw\" string.  \"Raw\" non us-ascii
+characters are regarded as variable `default-mime-charset'.
+
+If an encoded-word is broken or your emacs implementation can not
+decode the charset included in it, it is not decoded.
+
+If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
+if there are in decoded encoded-words (generated by bad manner MUA
+such as a version of Net$cape)."
+  (eword-decode-string
+   (decode-mime-charset-string string default-mime-charset)
+   must-unfold))
+
+(defun eword-decode-and-unfold-unstructured-field (string)
+  "Decode and unfold STRING as unstructured field body.
+It decodes non us-ascii characters in FULL-NAME encoded as
+encoded-words or invalid \"raw\" string.  \"Raw\" non us-ascii
+characters are regarded as variable `default-mime-charset'.
+
+If an encoded-word is broken or your emacs implementation can not
+decode the charset included in it, it is not decoded."
+  (eword-decode-string
+   (decode-mime-charset-string (std11-unfold-string string)
+                              default-mime-charset)
+   'must-unfold))
+
 
 ;;; @ for region
 ;;;
@@ -180,13 +281,56 @@ Each field name must be symbol."
 (defcustom eword-decode-structured-field-list
   '(Reply-To Resent-Reply-To From Resent-From Sender Resent-Sender
             To Resent-To Cc Resent-Cc Bcc Resent-Bcc Dcc
+            Mail-Followup-To
             Mime-Version Content-Type Content-Transfer-Encoding
-            Content-Disposition)
+            Content-Disposition User-Agent)
   "*List of field-names to decode as structured field.
 Each field name must be symbol."
   :group 'eword-decode
   :type '(repeat symbol))
 
+(defun eword-decode-field-body
+  (field-body field-name &optional unfolded max-column)
+  "Decode FIELD-BODY as FIELD-NAME, and return the result.
+
+If UNFOLDED is non-nil, it is assumed that FIELD-BODY is
+already unfolded.
+
+If MAX-COLUMN is non-nil, the result is folded with MAX-COLUMN
+or `fill-column' if MAX-COLUMN is t.
+Otherwise, the result is unfolded.
+
+MIME encoded-word in FIELD-BODY is recognized according to
+`eword-decode-ignored-field-list',
+`eword-decode-structured-field-list' and FIELD-NAME.
+
+Non MIME encoded-word part in FILED-BODY is decoded with
+`default-mime-charset'."
+  (when (eq max-column t)
+    (setq max-column fill-column))
+  (let (field-name-symbol len)
+    (if (symbolp field-name)
+        (setq field-name-symbol field-name
+              len (1+ (string-width (symbol-name field-name))))
+      (setq field-name-symbol (intern (capitalize field-name))
+            len (1+ (string-width field-name))))
+    (if (memq field-name-symbol eword-decode-ignored-field-list)
+        ;; Don't decode
+        (if max-column
+            field-body
+          (std11-unfold-string field-body))
+      (if (memq field-name-symbol eword-decode-structured-field-list)
+          ;; Decode as structured field
+          (if max-column
+              (eword-decode-and-fold-structured-field
+               field-body len max-column t)
+            (eword-decode-and-unfold-structured-field field-body))
+        ;; Decode as unstructured field
+        (if max-column
+            (eword-decode-unstructured-field-body field-body len)
+          (eword-decode-unstructured-field-body
+           (std11-unfold-string field-body) len))))))
+
 (defun eword-decode-header (&optional code-conversion separator)
   "Decode MIME encoded-words in header fields.
 If CODE-CONVERSION is nil, it decodes only encoded-words.  If it is
@@ -254,83 +398,6 @@ If SEPARATOR is not nil, it is used as header separator."
            ))
       )))
 
-(defun eword-visible-field-p (field-name visible-fields invisible-fields)
-  (or (catch 'found
-       (while visible-fields
-         (let ((regexp (car visible-fields)))
-           (if (string-match regexp field-name)
-               (throw 'found t)
-             ))
-         (setq visible-fields (cdr visible-fields))
-         ))
-      (catch 'found
-       (while invisible-fields
-         (let ((regexp (car invisible-fields)))
-           (if (string-match regexp field-name)
-               (throw 'found nil)
-             ))
-         (setq invisible-fields (cdr invisible-fields))
-         )
-       t)))
-               
-(defun mime-insert-decoded-header (entity
-                                  &optional invisible-fields visible-fields
-                                  code-conversion)
-  "Insert before point a decoded header of ENTITY."
-  (let ((default-charset
-         (if code-conversion
-             (if (mime-charset-to-coding-system code-conversion)
-                 code-conversion
-               default-mime-charset))))
-    (save-restriction
-      (narrow-to-region (point)(point))
-      (let ((the-buf (current-buffer))
-           (src-buf (mime-entity-buffer entity))
-           (h-end (mime-entity-header-end entity))
-           beg p end field-name len field)
-       (save-excursion
-         (set-buffer src-buf)
-         (goto-char (mime-entity-header-start entity))
-         (save-restriction
-           (narrow-to-region (point) h-end)
-           (while (re-search-forward std11-field-head-regexp nil t)
-             (setq beg (match-beginning 0)
-                   p (match-end 0)
-                   field-name (buffer-substring beg (1- p))
-                   len (string-width field-name)
-                   end (std11-field-end))
-             (when (eword-visible-field-p field-name
-                                          visible-fields invisible-fields)
-               (setq field (intern (capitalize field-name)))
-               (save-excursion
-                 (set-buffer the-buf)
-                 (insert field-name)
-                 (insert ":")
-                 (cond ((memq field eword-decode-ignored-field-list)
-                        ;; Don't decode
-                        (insert-buffer-substring src-buf p end)
-                        )
-                       ((memq field eword-decode-structured-field-list)
-                        ;; Decode as structured field
-                        (let ((body (save-excursion
-                                      (set-buffer src-buf)
-                                      (buffer-substring p end)))
-                              (default-mime-charset default-charset))
-                          (insert (eword-decode-and-fold-structured-field
-                                   body (1+ len)))
-                          ))
-                       (t
-                        ;; Decode as unstructured field
-                        (let ((body (save-excursion
-                                      (set-buffer src-buf)
-                                      (buffer-substring p end)))
-                              (default-mime-charset default-charset))
-                          (insert (eword-decode-unstructured-field-body
-                                   body (1+ len)))
-                          )))
-                 (insert "\n")
-                 )))))))))
-
 
 ;;; @ encoded-word decoder
 ;;;
@@ -394,37 +461,19 @@ if there are in decoded encoded-text (generated by bad manner MUA such
 as a version of Net$cape)."
   (let ((cs (mime-charset-to-coding-system charset)))
     (if cs
-       (let ((dest
-               (cond
-                ((string-equal "B" encoding)
-                 (if (and (string-match eword-B-encoded-text-regexp string)
-                          (string-equal string (match-string 0 string)))
-                     (base64-decode-string string)
-                   (error "Invalid encoded-text %s" string)))
-                ((string-equal "Q" encoding)
-                 (if (and (string-match eword-Q-encoded-text-regexp string)
-                          (string-equal string (match-string 0 string)))
-                     (q-encoding-decode-string string)
-                   (error "Invalid encoded-text %s" string)))
-                (t
-                 (error "Invalid encoding %s" encoding)
-                 )))
-              )
-         (if dest
-             (progn
-               (setq dest (decode-coding-string dest cs))
-               (if must-unfold
-                   (mapconcat (function
-                               (lambda (chr)
-                                 (cond
-                                   ((eq chr ?\n) "")
-                                   ((eq chr ?\t) " ")
-                                   (t (char-to-string chr)))
-                                 ))
-                              (std11-unfold-string dest)
-                              "")
-                 dest)
-               ))))))
+       (let ((dest (encoded-text-decode-string string encoding)))
+         (when dest
+           (setq dest (decode-mime-charset-string dest charset))
+           (if must-unfold
+               (mapconcat (function
+                           (lambda (chr)
+                             (cond ((eq chr ?\n) "")
+                                   ((eq chr ?\t) " ")
+                                   (t (char-to-string chr)))
+                             ))
+                          (std11-unfold-string dest)
+                          "")
+             dest))))))
 
 
 ;;; @ lexical analyze
@@ -565,114 +614,6 @@ characters encoded as encoded-words or invalid \"raw\" format.
           (concat "(" (std11-wrap-as-quoted-pairs value '(?( ?))) ")"))
          (t value))))
 
-(defun eword-decode-and-fold-structured-field
-  (string start-column &optional max-column must-unfold)
-  "Decode and fold (fill) STRING as structured field body.
-It decodes non us-ascii characters in FULL-NAME encoded as
-encoded-words or invalid \"raw\" string.  \"Raw\" non us-ascii
-characters are regarded as variable `default-mime-charset'.
-
-If an encoded-word is broken or your emacs implementation can not
-decode the charset included in it, it is not decoded.
-
-If MAX-COLUMN is omitted, `fill-column' is used.
-
-If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
-if there are in decoded encoded-words (generated by bad manner MUA
-such as a version of Net$cape)."
-  (or max-column
-      (setq max-column fill-column))
-  (let ((c start-column)
-       (tokens (eword-lexical-analyze string must-unfold))
-       (result "")
-       token)
-    (while (and (setq token (car tokens))
-               (setq tokens (cdr tokens)))
-      (let* ((type (car token)))
-       (if (eq type 'spaces)
-           (let* ((next-token (car tokens))
-                  (next-str (eword-decode-token next-token))
-                  (next-len (string-width next-str))
-                  (next-c (+ c next-len 1)))
-             (if (< next-c max-column)
-                 (setq result (concat result " " next-str)
-                       c next-c)
-               (setq result (concat result "\n " next-str)
-                     c (1+ next-len)))
-             (setq tokens (cdr tokens))
-             )
-         (let* ((str (eword-decode-token token)))
-           (setq result (concat result str)
-                 c (+ c (string-width str)))
-           ))))
-    (if token
-       (concat result (eword-decode-token token))
-      result)))
-
-(defun eword-decode-and-unfold-structured-field (string)
-  "Decode and unfold STRING as structured field body.
-It decodes non us-ascii characters in FULL-NAME encoded as
-encoded-words or invalid \"raw\" string.  \"Raw\" non us-ascii
-characters are regarded as variable `default-mime-charset'.
-
-If an encoded-word is broken or your emacs implementation can not
-decode the charset included in it, it is not decoded."
-  (let ((tokens (eword-lexical-analyze string 'must-unfold))
-       (result ""))
-    (while tokens
-      (let* ((token (car tokens))
-            (type (car token)))
-       (setq tokens (cdr tokens))
-       (setq result
-             (if (eq type 'spaces)
-                 (concat result " ")
-               (concat result (eword-decode-token token))
-               ))))
-    result))
-
-(defun eword-decode-structured-field-body (string &optional must-unfold
-                                                 start-column max-column)
-  "Decode non us-ascii characters in STRING as structured field body.
-STRING is unfolded before decoding.
-
-It decodes non us-ascii characters in FULL-NAME encoded as
-encoded-words or invalid \"raw\" string.  \"Raw\" non us-ascii
-characters are regarded as variable `default-mime-charset'.
-
-If an encoded-word is broken or your emacs implementation can not
-decode the charset included in it, it is not decoded.
-
-If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
-if there are in decoded encoded-words (generated by bad manner MUA
-such as a version of Net$cape)."
-  (if start-column
-      ;; fold with max-column
-      (eword-decode-and-fold-structured-field
-       string start-column max-column must-unfold)
-    ;; Don't fold
-    (mapconcat (function eword-decode-token)
-              (eword-lexical-analyze string must-unfold)
-              "")
-    ))
-
-(defun eword-decode-unstructured-field-body (string &optional must-unfold)
-  "Decode non us-ascii characters in STRING as unstructured field body.
-STRING is unfolded before decoding.
-
-It decodes non us-ascii characters in FULL-NAME encoded as
-encoded-words or invalid \"raw\" string.  \"Raw\" non us-ascii
-characters are regarded as variable `default-mime-charset'.
-
-If an encoded-word is broken or your emacs implementation can not
-decode the charset included in it, it is not decoded.
-
-If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
-if there are in decoded encoded-words (generated by bad manner MUA
-such as a version of Net$cape)."
-  (eword-decode-string
-   (decode-mime-charset-string string default-mime-charset)
-   must-unfold))
-
 (defun eword-extract-address-components (string)
   "Extract full name and canonical address from STRING.
 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).