1 ;;; mm-bodies.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998,99 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
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)
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.
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.
28 (or (fboundp 'base64-decode-region)
30 (autoload 'binhex-decode-region "binhex"))
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]")
41 (defvar mm-body-charset-encoding-alist
43 (iso-8859-1 . quoted-printable)
44 (iso-8859-2 . quoted-printable)
45 (iso-8859-3 . quoted-printable)
46 (iso-8859-4 . quoted-printable)
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)
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'.")
63 (defun mm-encode-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.
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.
82 (goto-char (point-min))
83 (let ((charsets (mm-find-mime-charset-region (point-min) (point-max)))
90 ((> (length charsets) 1)
94 (let ((charset (car charsets))
98 (not (mm-coding-system-equal
99 charset buffer-file-coding-system)))
101 (if (eq (char-charset (char-after)) 'ascii)
104 (narrow-to-region start (point))
105 (mm-encode-coding-region start (point) charset)
106 (goto-char (point-max)))
109 (setq start (point))))
112 (mm-encode-coding-region start (point) charset)
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)))
122 ((eq charset mail-parse-charset)
125 (let ((encoding (or (cdr (assq charset mm-body-charset-encoding-alist))
127 (mm-encode-content-transfer-encoding encoding "text/plain")
130 (defun mm-body-7-or-8 ()
131 "Say whether the body is 7bit or 8bit."
133 ((not (featurep 'mule))
135 (goto-char (point-min))
136 (re-search-forward mm-8bit-char-regexp nil t))
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.
147 (goto-char (point-min))
148 (skip-chars-forward "\0-\177")
154 ;;; Functions for decoding
157 (defun mm-decode-content-transfer-encoding (encoding &optional type)
159 (condition-case error
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.
169 (goto-char (point-max))
170 (skip-chars-backward "\n\t ")
172 ((memq encoding '(7bit 8bit binary))
176 ((memq encoding '(x-uuencode x-uue))
177 (funcall mm-uu-decode-function (point-min) (point-max)))
178 ((eq encoding 'x-binhex)
179 (funcall mm-uu-binhex-decode-function (point-min) (point-max)))
180 ((functionp encoding)
181 (funcall encoding (point-min) (point-max)))
183 (message "Unknown encoding %s; defaulting to 8bit" encoding)))
185 (message "Error while decoding: %s" error)
188 (memq encoding '(base64 x-uuencode x-uue x-binhex))
189 (equal type "text/plain"))
190 (goto-char (point-min))
191 (while (search-forward "\r\n" nil t)
192 (replace-match "\n" t t)))))
194 (defun mm-decode-body (charset &optional encoding type)
195 "Decode the current article that has been encoded with ENCODING.
196 The characters in CHARSET should then be decoded."
197 (if (stringp charset)
198 (setq charset (intern (downcase charset))))
199 (if (or (not charset) (memq charset mail-parse-ignored-charsets))
200 (setq charset mail-parse-charset))
203 (mm-decode-content-transfer-encoding encoding type))
204 (when (featurep 'mule)
207 (setq mule-charset (mm-charset-to-coding-system charset))
208 ;; buffer-file-coding-system
209 ;;Article buffer is nil coding system
211 enable-multibyte-characters
212 (or (not (eq mule-charset 'ascii))
213 (setq mule-charset mail-parse-charset)))
214 (mm-decode-coding-region (point-min) (point-max) mule-charset))))))
216 (defun mm-decode-string (string charset)
217 "Decode STRING with CHARSET."
218 (if (stringp charset)
219 (setq charset (intern (downcase charset))))
220 (if (or (not charset) (memq charset mail-parse-ignored-charsets))
221 (setq charset mail-parse-charset))
223 (when (featurep 'mule)
226 (setq mule-charset (mm-charset-to-coding-system charset))
227 enable-multibyte-characters
228 (or (not (eq mule-charset 'ascii))
229 (setq mule-charset mail-parse-charset)))
230 (mm-decode-coding-string string mule-charset))))
235 ;; mm-bodies.el ends here