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