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)
32 (defvar mm-uu-decode-function)
33 (defvar mm-uu-binhex-decode-function))
39 ;; 8bit treatment gets any char except: 0x32 - 0x7f, CR, LF, TAB, BEL,
40 ;; BS, vertical TAB, form feed, and ^_
41 (defvar mm-7bit-chars "\x20-\x7f\r\n\t\x7\x8\xb\xc\x1f")
43 (defcustom mm-body-charset-encoding-alist
44 '((iso-2022-jp . 7bit)
45 (iso-2022-jp-2 . 7bit))
46 "Alist of MIME charsets to encodings.
47 Valid encodings are `7bit', `8bit', `quoted-printable' and `base64'."
48 :type '(repeat (cons (symbol :tag "charset")
49 (choice :tag "encoding"
52 (const quoted-printable)
56 (defun mm-encode-body (&optional charset)
58 Should be called narrowed to the body that is to be encoded.
59 If there is more than one non-ASCII MULE charset, then list of found
60 MULE charsets are returned.
61 If CHARSET is non-nil, it is used.
62 If successful, the MIME charset is returned.
63 If no encoding was done, nil is returned."
64 (if (not (mm-multibyte-p))
65 ;; In the non-Mule case, we search for non-ASCII chars and
66 ;; return the value of `mail-parse-charset' if any are found.
69 (goto-char (point-min))
70 (if (re-search-forward "[^\x0-\x7f]" nil t)
71 (or mail-parse-charset
72 (message-options-get 'mm-encody-body-charset)
74 'mm-encody-body-charset
75 (mm-read-charset "Charset used in the article: ")))
76 ;; The logic in `mml-generate-mime-1' confirms that it's OK
77 ;; to return nil here.
82 (mm-encode-coding-region (point-min) (point-max) charset)
84 (goto-char (point-min))
85 (let ((charsets (mm-find-mime-charset-region (point-min) (point-max)))
92 ((> (length charsets) 1)
96 (setq charset (car charsets))
98 (if (eq (mm-charset-after) 'ascii)
101 (narrow-to-region start (point))
102 (mm-encode-coding-region
103 start (point) (mm-charset-to-coding-system charset))
104 (goto-char (point-max)))
107 (setq start (point))))
110 (mm-encode-coding-region start (point)
111 (mm-charset-to-coding-system charset))
115 (defun mm-long-lines-p (length)
116 "Say whether any of the lines in the buffer is longer than LINES."
118 (goto-char (point-min))
120 (while (and (not (eobp))
121 (not (> (current-column) length)))
124 (and (> (current-column) length)
127 (defvar message-posting-charset)
129 (defun mm-body-encoding (charset &optional encoding)
130 "Do Content-Transfer-Encoding and return the encoding of the current buffer."
131 (let ((bits (mm-body-7-or-8))
132 (longp (mm-long-lines-p 1000)))
135 ((and (not mm-use-ultra-safe-encoding)
139 ((and (not mm-use-ultra-safe-encoding)
141 (or (eq t (cdr message-posting-charset))
142 (memq charset (cdr message-posting-charset))
143 (eq charset mail-parse-charset)))
146 (let ((encoding (or encoding
147 (cdr (assq charset mm-body-charset-encoding-alist))
149 (when mm-use-ultra-safe-encoding
150 (setq encoding (mm-safer-encoding encoding)))
151 (mm-encode-content-transfer-encoding encoding "text/plain")
154 (defun mm-body-7-or-8 ()
155 "Say whether the body is 7bit or 8bit."
157 ((not (featurep 'mule))
159 (goto-char (point-min))
160 (skip-chars-forward mm-7bit-chars)
166 (if (and (null (delq 'ascii
167 (mm-find-charset-region (point-min) (point-max))))
168 ;;!!!The following is necessary because the function
169 ;;!!!above seems to return the wrong result under
170 ;;!!!Emacs 20.3. Sometimes.
172 (goto-char (point-min))
173 (skip-chars-forward mm-7bit-chars)
179 ;;; Functions for decoding
182 (defun mm-decode-content-transfer-encoding (encoding &optional type)
184 (condition-case error
186 ((eq encoding 'quoted-printable)
187 (quoted-printable-decode-region (point-min) (point-max)))
188 ((eq encoding 'base64)
189 (base64-decode-region
191 ;; Some mailers insert whitespace
192 ;; junk at the end which
193 ;; base64-decode-region dislikes.
194 ;; Also remove possible junk which could
195 ;; have been added by mailing list software.
197 (goto-char (point-min))
198 (while (re-search-forward "^[\t ]*\r?\n" nil t)
199 (delete-region (match-beginning 0) (match-end 0)))
200 (goto-char (point-max))
201 (when (re-search-backward "^[A-Za-z0-9+/]+=*[\t ]*$" nil t)
204 ((memq encoding '(7bit 8bit binary))
210 ((memq encoding '(x-uuencode x-uue))
212 (funcall mm-uu-decode-function (point-min) (point-max)))
213 ((eq encoding 'x-binhex)
215 (funcall mm-uu-binhex-decode-function (point-min) (point-max)))
216 ((functionp encoding)
217 (funcall encoding (point-min) (point-max)))
219 (message "Unknown encoding %s; defaulting to 8bit" encoding)))
221 (message "Error while decoding: %s" error)
224 (memq encoding '(base64 x-uuencode x-uue x-binhex))
225 (equal type "text/plain"))
226 (goto-char (point-min))
227 (while (search-forward "\r\n" nil t)
228 (replace-match "\n" t t)))))
230 (defun mm-decode-body (charset &optional encoding type)
231 "Decode the current article that has been encoded with ENCODING.
232 The characters in CHARSET should then be decoded."
233 (if (stringp charset)
234 (setq charset (intern (downcase charset))))
235 (if (or (not charset)
236 (eq 'gnus-all mail-parse-ignored-charsets)
237 (memq 'gnus-all mail-parse-ignored-charsets)
238 (memq charset mail-parse-ignored-charsets))
239 (setq charset mail-parse-charset))
242 (mm-decode-content-transfer-encoding encoding type))
243 (when (featurep 'mule)
244 (let ((coding-system (mm-charset-to-coding-system charset)))
245 (if (and (not coding-system)
246 (listp mail-parse-ignored-charsets)
247 (memq 'gnus-unknown mail-parse-ignored-charsets))
249 (mm-charset-to-coding-system mail-parse-charset)))
250 (when (and charset coding-system
251 ;; buffer-file-coding-system
252 ;;Article buffer is nil coding system
255 (or (not (eq coding-system 'ascii))
256 (setq coding-system mail-parse-charset))
257 (not (eq coding-system 'gnus-decoded)))
258 (mm-decode-coding-region (point-min) (point-max) coding-system))))))
260 (defun mm-decode-string (string charset)
261 "Decode STRING with CHARSET."
262 (when (stringp charset)
263 (setq charset (intern (downcase charset))))
264 (when (or (not charset)
265 (eq 'gnus-all mail-parse-ignored-charsets)
266 (memq 'gnus-all mail-parse-ignored-charsets)
267 (memq charset mail-parse-ignored-charsets))
268 (setq charset mail-parse-charset))
270 (when (featurep 'mule)
271 (let ((coding-system (mm-charset-to-coding-system charset)))
272 (if (and (not coding-system)
273 (listp mail-parse-ignored-charsets)
274 (memq 'gnus-unknown mail-parse-ignored-charsets))
276 (mm-charset-to-coding-system mail-parse-charset)))
277 (when (and charset coding-system
279 (or (not (eq coding-system 'ascii))
280 (setq coding-system mail-parse-charset)))
281 (mm-decode-coding-string string coding-system))))
286 ;; mm-bodies.el ends here