1 ;;; mm-bodies.el --- Functions for decoding MIME things
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2003
4 ;; Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
30 (or (fboundp 'base64-decode-region)
34 (defvar mm-uu-decode-function)
35 (defvar mm-uu-binhex-decode-function))
41 ;; 8bit treatment gets any char except: 0x32 - 0x7f, CR, LF, TAB, BEL,
42 ;; BS, vertical TAB, form feed, and ^_
43 (defvar mm-7bit-chars "\x20-\x7f\r\n\t\x7\x8\xb\xc\x1f")
45 (defcustom mm-body-charset-encoding-alist
46 '((iso-2022-jp . 7bit)
47 (iso-2022-jp-2 . 7bit)
48 ;; We MUST encode UTF-16 because it can contain \0's which is
49 ;; known to break servers.
53 "Alist of MIME charsets to encodings.
54 Valid encodings are `7bit', `8bit', `quoted-printable' and `base64'."
55 :type '(repeat (cons (symbol :tag "charset")
56 (choice :tag "encoding"
59 (const quoted-printable)
63 (defun mm-encode-body (&optional charset)
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 CHARSET is non-nil, it is used.
69 If successful, the MIME charset is returned.
70 If no encoding was done, nil is returned."
71 (if (not (mm-multibyte-p))
72 ;; In the non-Mule case, we search for non-ASCII chars and
73 ;; return the value of `mail-parse-charset' if any are found.
76 (goto-char (point-min))
77 (if (re-search-forward "[^\x0-\x7f]" nil t)
78 (or mail-parse-charset
79 (message-options-get 'mm-encody-body-charset)
81 'mm-encody-body-charset
82 (mm-read-charset "Charset used in the article: ")))
83 ;; The logic in `mml-generate-mime-1' confirms that it's OK
84 ;; to return nil here.
89 (mm-encode-coding-region (point-min) (point-max) charset)
91 (goto-char (point-min))
92 (let ((charsets (mm-find-mime-charset-region (point-min) (point-max)
99 ((> (length charsets) 1)
104 (setq charset (car charsets))
105 (mm-encode-coding-region (point-min) (point-max)
106 (mm-charset-to-coding-system charset))))
109 (defun mm-long-lines-p (length)
110 "Say whether any of the lines in the buffer is longer than LINES."
112 (goto-char (point-min))
114 (while (and (not (eobp))
115 (not (> (current-column) length)))
118 (and (> (current-column) length)
121 (defvar message-posting-charset)
123 (defun mm-body-encoding (charset &optional encoding)
124 "Do Content-Transfer-Encoding and return the encoding of the current buffer."
125 (when (stringp encoding)
126 (setq encoding (intern (downcase encoding))))
127 (let ((bits (mm-body-7-or-8))
128 (longp (mm-long-lines-p 1000)))
131 ((and (not mm-use-ultra-safe-encoding)
135 ((and (not mm-use-ultra-safe-encoding)
137 (not (cdr (assq charset mm-body-charset-encoding-alist)))
138 (or (eq t (cdr message-posting-charset))
139 (memq charset (cdr message-posting-charset))
140 (eq charset mail-parse-charset)))
143 (let ((encoding (or encoding
144 (cdr (assq charset mm-body-charset-encoding-alist))
146 (when mm-use-ultra-safe-encoding
147 (setq encoding (mm-safer-encoding encoding)))
148 (mm-encode-content-transfer-encoding encoding "text/plain")
151 (defun mm-body-7-or-8 ()
152 "Say whether the body is 7bit or 8bit."
154 ((not (featurep 'mule))
156 (goto-char (point-min))
157 (skip-chars-forward mm-7bit-chars)
163 (if (and (null (delq 'ascii
164 (mm-find-charset-region (point-min) (point-max))))
165 ;;!!!The following is necessary because the function
166 ;;!!!above seems to return the wrong result under
167 ;;!!!Emacs 20.3. Sometimes.
169 (goto-char (point-min))
170 (skip-chars-forward mm-7bit-chars)
176 ;;; Functions for decoding
179 (defun mm-decode-content-transfer-encoding (encoding &optional type)
180 "Decodes buffer encoded with ENCODING, returning success status.
181 If TYPE is `text/plain' CRLF->LF translation may occur."
183 (condition-case error
185 ((eq encoding 'quoted-printable)
186 (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))
214 ((eq encoding 'x-binhex)
216 (funcall mm-uu-binhex-decode-function (point-min) (point-max))
218 ((eq encoding 'x-yenc)
220 (funcall mm-uu-yenc-decode-function (point-min) (point-max))
222 ((functionp encoding)
223 (funcall encoding (point-min) (point-max))
226 (message "Unknown encoding %s; defaulting to 8bit" encoding)))
228 (message "Error while decoding: %s" error)
231 (memq encoding '(base64 x-uuencode x-uue x-binhex x-yenc))
232 (equal type "text/plain"))
233 (goto-char (point-min))
234 (while (search-forward "\r\n" nil t)
235 (replace-match "\n" t t)))))
237 (defun mm-decode-body (charset &optional encoding type force)
238 "Decode the current article that has been encoded with ENCODING.
239 The characters in CHARSET should then be decoded. If FORCE is non-nil
240 use the supplied charset unconditionally."
241 (let ((charset-supplied charset))
242 (when (stringp charset)
243 (setq charset (intern (downcase charset))))
244 (when (or (not charset)
245 (eq 'gnus-all mail-parse-ignored-charsets)
246 (memq 'gnus-all mail-parse-ignored-charsets)
247 (memq charset mail-parse-ignored-charsets))
248 (setq charset mail-parse-charset
249 charset-supplied nil))
252 (mm-decode-content-transfer-encoding encoding type))
253 (when (featurep 'mule)
254 (let ((coding-system (mm-charset-to-coding-system charset)))
255 (if (and (not coding-system)
256 (listp mail-parse-ignored-charsets)
257 (memq 'gnus-unknown mail-parse-ignored-charsets))
259 (mm-charset-to-coding-system mail-parse-charset)))
260 (when (and charset coding-system
261 ;; buffer-file-coding-system
262 ;;Article buffer is nil coding system
265 (or (not (eq coding-system 'ascii))
266 (setq coding-system mail-parse-charset))
267 (not (eq coding-system 'gnus-decoded)))
269 ;; If a charset was supplied, then use the
270 ;; supplied charset unconditionally.
272 (mm-decode-coding-region (point-min) (point-max)
274 ;; Otherwise allow Emacs to auto-detect the charset.
275 (mm-decode-coding-region-safely (point-min) (point-max)
277 (setq buffer-file-coding-system
278 (if (boundp 'last-coding-system-used)
279 (symbol-value 'last-coding-system-used)
282 (defun mm-decode-coding-region-safely (start end coding-system)
283 "Decode region between START and END with CODING-SYSTEM.
284 If CODING-SYSTEM is not a valid coding system for the text, let Emacs
285 decide which coding system to use."
286 (let* ((orig (buffer-substring start end))
289 (narrow-to-region start end)
290 (mm-decode-coding-region (point-min) (point-max) coding-system)
291 (setq charsets (find-charset-region (point-min) (point-max)))
292 (when (or (memq 'eight-bit-control charsets)
293 (memq 'eight-bit-graphic charsets))
294 (delete-region (point-min) (point-max))
296 (mm-decode-coding-region (point-min) (point-max) 'undecided)))))
298 (defun mm-decode-string (string charset)
299 "Decode STRING with CHARSET."
300 (when (stringp charset)
301 (setq charset (intern (downcase charset))))
302 (when (or (not charset)
303 (eq 'gnus-all mail-parse-ignored-charsets)
304 (memq 'gnus-all mail-parse-ignored-charsets)
305 (memq charset mail-parse-ignored-charsets))
306 (setq charset mail-parse-charset))
308 (when (featurep 'mule)
309 (let ((coding-system (mm-charset-to-coding-system charset)))
310 (if (and (not coding-system)
311 (listp mail-parse-ignored-charsets)
312 (memq 'gnus-unknown mail-parse-ignored-charsets))
314 (mm-charset-to-coding-system mail-parse-charset)))
315 (when (and charset coding-system
317 (or (not (eq coding-system 'ascii))
318 (setq coding-system mail-parse-charset)))
319 (mm-decode-coding-string string coding-system))))
324 ;;; mm-bodies.el ends here