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