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