1 ;;; mm-bodies.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998, 1999, 2000 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-7bit-chars "\x20-\x7f\r\n\t\x7\x8\xb\xc\x1f")
41 (defcustom mm-body-charset-encoding-alist
42 '((iso-2022-jp . 7bit)
43 (iso-2022-jp-2 . 7bit))
44 "Alist of MIME charsets to encodings.
45 Valid encodings are `7bit', `8bit', `quoted-printable' and `base64'."
46 :type '(repeat (cons (symbol :tag "charset")
47 (choice :tag "encoding"
50 (const quoted-printable)
54 (defun mm-encode-body ()
56 Should be called narrowed to the body that is to be encoded.
57 If there is more than one non-ASCII MULE charset, then list of found
58 MULE charsets are returned.
59 If successful, the MIME charset is returned.
60 If no encoding was done, nil is returned."
61 (if (not (featurep 'mule))
62 ;; In the non-Mule case, we search for non-ASCII chars and
63 ;; return the value of `mail-parse-charset' if any are found.
65 (goto-char (point-min))
66 (if (re-search-forward "[^\x0-\x7f]" nil t)
67 (or mail-parse-charset
68 (mm-read-charset "Charset used in the article: "))
69 ;; The logic in `mml-generate-mime-1' confirms that it's OK
70 ;; to return nil here.
73 (goto-char (point-min))
74 (let ((charsets (mm-find-mime-charset-region (point-min) (point-max)))
81 ((> (length charsets) 1)
85 (let ((charset (car charsets))
89 (not (mm-coding-system-equal
90 charset buffer-file-coding-system)))
92 (if (eq (mm-charset-after) 'ascii)
95 (narrow-to-region start (point))
96 (mm-encode-coding-region start (point) charset)
97 (goto-char (point-max)))
100 (setq start (point))))
103 (mm-encode-coding-region start (point) charset)
107 (defun mm-body-encoding (charset &optional encoding)
108 "Do Content-Transfer-Encoding and return the encoding of the current buffer."
109 (let ((bits (mm-body-7-or-8)))
111 ((and (not mm-use-ultra-safe-encoding) (eq bits '7bit))
113 ((and (not mm-use-ultra-safe-encoding)
114 (or (eq t (cdr message-posting-charset))
115 (memq charset (cdr message-posting-charset))
116 (eq charset mail-parse-charset)))
119 (let ((encoding (or encoding
120 (cdr (assq charset mm-body-charset-encoding-alist))
122 (when mm-use-ultra-safe-encoding
123 (setq encoding (mm-safer-encoding encoding)))
124 (mm-encode-content-transfer-encoding encoding "text/plain")
127 (defun mm-body-7-or-8 ()
128 "Say whether the body is 7bit or 8bit."
130 ((not (featurep 'mule))
132 (goto-char (point-min))
133 (skip-chars-forward mm-7bit-chars)
139 (if (and (null (delq 'ascii
140 (mm-find-charset-region (point-min) (point-max))))
141 ;;!!!The following is necessary because the function
142 ;;!!!above seems to return the wrong result under
143 ;;!!!Emacs 20.3. Sometimes.
145 (goto-char (point-min))
146 (skip-chars-forward mm-7bit-chars)
152 ;;; Functions for decoding
155 (defun mm-decode-content-transfer-encoding (encoding &optional type)
157 (condition-case error
159 ((eq encoding 'quoted-printable)
160 (quoted-printable-decode-region (point-min) (point-max)))
161 ((eq encoding 'base64)
162 (base64-decode-region
164 ;; Some mailers insert whitespace
165 ;; junk at the end which
166 ;; base64-decode-region dislikes.
167 ;; Also remove possible junk which could
168 ;; have been added by mailing list software.
170 (goto-char (point-min))
171 (while (re-search-forward "^[\t ]*\r?\n" nil t)
172 (delete-region (match-beginning 0) (match-end 0)))
173 (goto-char (point-max))
174 (when (re-search-backward "^[A-Za-z0-9+/]+=*[\t ]*$" nil t)
176 (delete-region (point) (point-max)))
178 ((memq encoding '(7bit 8bit binary))
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)))
191 (message "Unknown encoding %s; defaulting to 8bit" encoding)))
193 (message "Error while decoding: %s" error)
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)))))
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))
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))
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
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))))))
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))
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))
248 (mm-charset-to-coding-system mail-parse-charset)))
249 (when (and charset mule-charset
251 (or (not (eq mule-charset 'ascii))
252 (setq mule-charset mail-parse-charset)))
253 (mm-decode-coding-string string mule-charset))))
258 ;; mm-bodies.el ends here