Merge changes in Emacs21.
[gnus] / lisp / mm-bodies.el
1 ;;; mm-bodies.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
7
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 by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
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.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-and-compile
28   (or (fboundp  'base64-decode-region)
29       (require 'base64)))
30
31 (eval-when-compile
32   (defvar mm-uu-decode-function)
33   (defvar mm-uu-binhex-decode-function))
34
35 (require 'mm-util)
36 (require 'rfc2047)
37 (require 'mm-encode)
38
39 ;; 8bit treatment gets any char except: 0x32 - 0x7f, CR, LF, TAB, BEL,
40 ;; BS, vertical TAB, form feed, and ^_
41 (defvar mm-7bit-chars "\x20-\x7f\r\n\t\x7\x8\xb\xc\x1f")
42
43 (defcustom mm-body-charset-encoding-alist
44   '((iso-2022-jp . 7bit)
45     (iso-2022-jp-2 . 7bit))
46   "Alist of MIME charsets to encodings.
47 Valid encodings are `7bit', `8bit', `quoted-printable' and `base64'."
48   :type '(repeat (cons (symbol :tag "charset")
49                        (choice :tag "encoding"
50                                (const 7bit)
51                                (const 8bit)
52                                (const quoted-printable)
53                                (const base64))))
54   :group 'mime)
55
56 (defun mm-encode-body (&optional charset)
57   "Encode a body.
58 Should be called narrowed to the body that is to be encoded.
59 If there is more than one non-ASCII MULE charset, then list of found
60 MULE charsets are returned.
61 If CHARSET is non-nil, it is used.
62 If successful, the MIME charset is returned.
63 If no encoding was done, nil is returned."
64   (if (not (mm-multibyte-p))
65       ;; In the non-Mule case, we search for non-ASCII chars and
66       ;; return the value of `mail-parse-charset' if any are found.
67       (or charset
68           (save-excursion
69             (goto-char (point-min))
70             (if (re-search-forward "[^\x0-\x7f]" nil t)
71                 (or mail-parse-charset
72                     (message-options-get 'mm-encody-body-charset)
73                     (message-options-set 
74                      'mm-encody-body-charset
75                      (mm-read-charset "Charset used in the article: ")))
76               ;; The logic in `mml-generate-mime-1' confirms that it's OK
77               ;; to return nil here.
78               nil)))
79     (save-excursion
80       (if charset
81           (progn
82             (mm-encode-coding-region (point-min) (point-max) charset)
83             charset)
84         (goto-char (point-min))
85         (let ((charsets (mm-find-mime-charset-region (point-min) (point-max)))
86               start)
87           (cond
88            ;; No encoding.
89            ((null charsets)
90             nil)
91            ;; Too many charsets.
92            ((> (length charsets) 1)
93             charsets)
94            ;; We encode.
95            (t
96             (setq charset (car charsets))
97             (while (not (eobp))
98               (if (eq (mm-charset-after) 'ascii)
99                   (when start
100                     (save-restriction
101                       (narrow-to-region start (point))
102                       (mm-encode-coding-region
103                        start (point) (mm-charset-to-coding-system charset))
104                       (goto-char (point-max)))
105                     (setq start nil))
106                 (unless start
107                   (setq start (point))))
108               (forward-char 1))
109             (when start
110               (mm-encode-coding-region start (point)
111                                        (mm-charset-to-coding-system charset))
112               (setq start nil))
113             charset)))))))
114
115 (eval-when-compile (defvar message-posting-charset))
116
117 (defun mm-body-encoding (charset &optional encoding)
118   "Do Content-Transfer-Encoding and return the encoding of the current buffer."
119   (let ((bits (mm-body-7-or-8)))
120     (require 'message)
121     (cond
122      ((and (not mm-use-ultra-safe-encoding) (eq bits '7bit))
123       bits)
124      ((and (not mm-use-ultra-safe-encoding)
125            (or (eq t (cdr message-posting-charset))
126                (memq charset (cdr message-posting-charset))
127                (eq charset mail-parse-charset)))
128       bits)
129      (t
130       (let ((encoding (or encoding
131                           (cdr (assq charset mm-body-charset-encoding-alist))
132                           (mm-qp-or-base64))))
133         (when mm-use-ultra-safe-encoding
134           (setq encoding (mm-safer-encoding encoding)))
135         (mm-encode-content-transfer-encoding encoding "text/plain")
136         encoding)))))
137
138 (defun mm-body-7-or-8 ()
139   "Say whether the body is 7bit or 8bit."
140   (cond
141    ((not (featurep 'mule))
142     (if (save-excursion
143           (goto-char (point-min))
144           (skip-chars-forward mm-7bit-chars)
145           (eobp))
146         '7bit
147       '8bit))
148    (t
149     ;; Mule version
150     (if (and (null (delq 'ascii
151                          (mm-find-charset-region (point-min) (point-max))))
152              ;;!!!The following is necessary because the function
153              ;;!!!above seems to return the wrong result under
154              ;;!!!Emacs 20.3.  Sometimes.
155              (save-excursion
156                (goto-char (point-min))
157                (skip-chars-forward mm-7bit-chars)
158                (eobp)))
159         '7bit
160       '8bit))))
161
162 ;;;
163 ;;; Functions for decoding
164 ;;;
165
166 (defun mm-decode-content-transfer-encoding (encoding &optional type)
167   (prog1
168       (condition-case error
169           (cond
170            ((eq encoding 'quoted-printable)
171             (quoted-printable-decode-region (point-min) (point-max)))
172            ((eq encoding 'base64)
173             (base64-decode-region
174              (point-min)
175              ;; Some mailers insert whitespace
176              ;; junk at the end which
177              ;; base64-decode-region dislikes.
178              ;; Also remove possible junk which could
179              ;; have been added by mailing list software.
180              (save-excursion
181                (goto-char (point-min))
182                (while (re-search-forward "^[\t ]*\r?\n" nil t)
183                  (delete-region (match-beginning 0) (match-end 0)))
184                (goto-char (point-max))
185                (when (re-search-backward "^[A-Za-z0-9+/]+=*[\t ]*$" nil t)
186                  (forward-line)
187                  (delete-region (point) (point-max)))
188                (point-max))))
189            ((memq encoding '(7bit 8bit binary))
190             ;; Do nothing.
191             )
192            ((null encoding)
193             ;; Do nothing.
194             )
195            ((memq encoding '(x-uuencode x-uue))
196             (require 'mm-uu)
197             (funcall mm-uu-decode-function (point-min) (point-max)))
198            ((eq encoding 'x-binhex)
199             (require 'mm-uu)
200             (funcall mm-uu-binhex-decode-function (point-min) (point-max)))
201            ((functionp encoding)
202             (funcall encoding (point-min) (point-max)))
203            (t
204             (message "Unknown encoding %s; defaulting to 8bit" encoding)))
205         (error
206          (message "Error while decoding: %s" error)
207          nil))
208     (when (and
209            (memq encoding '(base64 x-uuencode x-uue x-binhex))
210            (equal type "text/plain"))
211       (goto-char (point-min))
212       (while (search-forward "\r\n" nil t)
213         (replace-match "\n" t t)))))
214
215 (defun mm-decode-body (charset &optional encoding type)
216   "Decode the current article that has been encoded with ENCODING.
217 The characters in CHARSET should then be decoded."
218   (if (stringp charset)
219       (setq charset (intern (downcase charset))))
220   (if (or (not charset) 
221           (eq 'gnus-all mail-parse-ignored-charsets)
222           (memq 'gnus-all mail-parse-ignored-charsets)
223           (memq charset mail-parse-ignored-charsets))
224       (setq charset mail-parse-charset))
225   (save-excursion
226     (when encoding
227       (mm-decode-content-transfer-encoding encoding type))
228     (when (featurep 'mule)
229       (let ((coding-system (mm-charset-to-coding-system charset)))
230         (if (and (not coding-system)
231                  (listp mail-parse-ignored-charsets)
232                  (memq 'gnus-unknown mail-parse-ignored-charsets))
233             (setq coding-system 
234                   (mm-charset-to-coding-system mail-parse-charset)))
235         (when (and charset coding-system
236                    ;; buffer-file-coding-system
237                    ;;Article buffer is nil coding system
238                    ;;in XEmacs
239                    (mm-multibyte-p)
240                    (or (not (eq coding-system 'ascii))
241                        (setq coding-system mail-parse-charset))
242                    (not (eq coding-system 'gnus-decoded)))
243           (mm-decode-coding-region (point-min) (point-max) coding-system))))))
244
245 (defun mm-decode-string (string charset)
246   "Decode STRING with CHARSET."
247   (when (stringp charset)
248     (setq charset (intern (downcase charset))))
249   (when (or (not charset) 
250             (eq 'gnus-all mail-parse-ignored-charsets)
251             (memq 'gnus-all mail-parse-ignored-charsets)
252             (memq charset mail-parse-ignored-charsets))
253     (setq charset mail-parse-charset))
254   (or
255    (when (featurep 'mule)
256      (let ((coding-system (mm-charset-to-coding-system charset)))
257        (if (and (not coding-system)
258                 (listp mail-parse-ignored-charsets)
259                 (memq 'gnus-unknown mail-parse-ignored-charsets))
260            (setq coding-system 
261                  (mm-charset-to-coding-system mail-parse-charset)))
262        (when (and charset coding-system
263                   (mm-multibyte-p)
264                   (or (not (eq coding-system 'ascii))
265                       (setq coding-system mail-parse-charset)))
266          (mm-decode-coding-string string coding-system))))
267    string))
268
269 (provide 'mm-bodies)
270
271 ;; mm-bodies.el ends here