1 ;;; mm-bodies.el --- Functions for decoding MIME things
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 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 3, 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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
31 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
37 (defvar mm-uu-yenc-decode-function
)
38 (defvar mm-uu-decode-function
)
39 (defvar mm-uu-binhex-decode-function
)
41 ;; 8bit treatment gets any char except: 0x32 - 0x7f, LF, TAB, BEL,
42 ;; BS, vertical TAB, form feed, and ^_
44 ;; Note that CR is *not* included, as that would allow a non-paired CR
45 ;; in the body contrary to RFC 2822:
47 ;; - CR and LF MUST only occur together as CRLF; they MUST NOT
48 ;; appear independently in the body.
50 (defvar mm-7bit-chars
"\x20-\x7f\n\t\x7\x8\xb\xc\x1f")
52 (defcustom mm-body-charset-encoding-alist
53 '((iso-2022-jp .
7bit
)
54 (iso-2022-jp-2 .
7bit
)
55 ;; We MUST encode UTF-16 because it can contain \0's which is
56 ;; known to break servers.
57 ;; Note: UTF-16 variants are invalid for text parts [RFC 2781],
58 ;; so this can't happen :-/.
59 ;; PPS: Yes, it can happen if the user specifies UTF-16 in the MML
64 "Alist of MIME charsets to encodings.
65 Valid encodings are `7bit', `8bit', `quoted-printable' and `base64'."
66 :type
'(repeat (cons (symbol :tag
"charset")
67 (choice :tag
"encoding"
70 (const quoted-printable
)
74 (autoload 'message-options-get
"message")
75 (declare-function message-options-set
"message" (symbol value
))
77 (defun mm-encode-body (&optional charset
)
79 Should be called narrowed to the body that is to be encoded.
80 If there is more than one non-ASCII MULE charset in the body, then the
81 list of MULE charsets found is returned.
82 If CHARSET is non-nil, it is used as the MIME charset to encode the body.
83 If successful, the MIME charset is returned.
84 If no encoding was done, nil is returned."
85 (if (not (mm-multibyte-p))
86 ;; In the non-Mule case, we search for non-ASCII chars and
87 ;; return the value of `mail-parse-charset' if any are found.
90 (goto-char (point-min))
91 (if (re-search-forward "[^\x0-\x7f]" nil t
)
92 (or mail-parse-charset
93 (message-options-get 'mm-encody-body-charset
)
95 'mm-encody-body-charset
96 (mm-read-coding-system "Charset used in the article: ")))
97 ;; The logic in `mml-generate-mime-1' confirms that it's OK
98 ;; to return nil here.
103 (mm-encode-coding-region (point-min) (point-max)
104 (mm-charset-to-coding-system charset
))
106 (goto-char (point-min))
107 (let ((charsets (mm-find-mime-charset-region (point-min) (point-max))))
112 ;; Too many charsets.
113 ((> (length charsets
) 1)
118 (setq charset
(car charsets
))
119 (mm-encode-coding-region (point-min) (point-max)
120 (mm-charset-to-coding-system charset
))))
123 (defun mm-long-lines-p (length)
124 "Say whether any of the lines in the buffer is longer than LENGTH."
126 (goto-char (point-min))
128 (while (and (not (eobp))
129 (not (> (current-column) length
)))
132 (and (> (current-column) length
)
135 (defvar message-posting-charset
)
137 (defun mm-body-encoding (charset &optional encoding
)
138 "Do Content-Transfer-Encoding and return the encoding of the current buffer."
139 (when (stringp encoding
)
140 (setq encoding
(intern (downcase encoding
))))
141 (let ((bits (mm-body-7-or-8))
142 (longp (mm-long-lines-p 1000)))
146 (not (and mm-use-ultra-safe-encoding
147 (or (save-excursion (re-search-forward " $" nil t
))
148 (save-excursion (re-search-forward "^From " nil t
)))))
151 ((and (not mm-use-ultra-safe-encoding
)
153 (not (cdr (assq charset mm-body-charset-encoding-alist
)))
154 (or (eq t
(cdr message-posting-charset
))
155 (memq charset
(cdr message-posting-charset
))
156 (eq charset mail-parse-charset
)))
159 (let ((encoding (or encoding
160 (cdr (assq charset mm-body-charset-encoding-alist
))
162 (when mm-use-ultra-safe-encoding
163 (setq encoding
(mm-safer-encoding encoding
)))
164 (mm-encode-content-transfer-encoding encoding
"text/plain")
167 (defun mm-body-7-or-8 ()
168 "Say whether the body is 7bit or 8bit."
170 (goto-char (point-min))
171 (skip-chars-forward mm-7bit-chars
)
177 ;;; Functions for decoding
180 (defun mm-decode-content-transfer-encoding (encoding &optional type
)
181 "Decodes buffer encoded with ENCODING, returning success status.
182 If TYPE is `text/plain' CRLF->LF translation may occur."
184 (condition-case error
186 ((eq encoding
'quoted-printable
)
187 (quoted-printable-decode-region (point-min) (point-max))
189 ((eq encoding
'base64
)
190 (base64-decode-region
192 ;; Some mailers insert whitespace
193 ;; junk at the end which
194 ;; base64-decode-region dislikes.
195 ;; Also remove possible junk which could
196 ;; have been added by mailing list software.
198 (goto-char (point-min))
199 (while (re-search-forward "^[\t ]*\r?\n" nil t
)
200 (delete-region (match-beginning 0) (match-end 0)))
201 (goto-char (point-max))
202 (when (re-search-backward "^[A-Za-z0-9+/]+=*[\t ]*$" nil t
)
205 ((memq encoding
'(7bit 8bit binary
))
211 ((memq encoding
'(x-uuencode x-uue
))
213 (funcall mm-uu-decode-function
(point-min) (point-max))
215 ((eq encoding
'x-binhex
)
217 (funcall mm-uu-binhex-decode-function
(point-min) (point-max))
219 ((eq encoding
'x-yenc
)
221 (funcall mm-uu-yenc-decode-function
(point-min) (point-max))
223 ((functionp encoding
)
224 (funcall encoding
(point-min) (point-max))
227 (message "Unknown encoding %s; defaulting to 8bit" encoding
)))
229 (message "Error while decoding: %s" error
)
233 (memq encoding
'(base64 x-uuencode x-uue x-binhex x-yenc
))
234 (string-match "\\`text/" type
))
235 (goto-char (point-min))
236 (while (search-forward "\r\n" nil t
)
237 (replace-match "\n" t t
)))))
239 (defun mm-decode-body (charset &optional encoding type
)
240 "Decode the current article that has been encoded with ENCODING to CHARSET.
241 ENCODING is a MIME content transfer encoding.
242 CHARSET is the MIME charset with which to decode the data after transfer
243 decoding. If it is nil, default to `mail-parse-charset'."
244 (when (stringp charset
)
245 (setq charset
(intern (downcase charset
))))
246 (when (or (not charset
)
247 (eq 'gnus-all mail-parse-ignored-charsets
)
248 (memq 'gnus-all mail-parse-ignored-charsets
)
249 (memq charset mail-parse-ignored-charsets
))
250 (setq charset mail-parse-charset
))
253 (mm-decode-content-transfer-encoding encoding type
))
254 (when (and (featurep 'mule
) ;; Fixme: Wrong test for unibyte session.
255 (not (eq charset
'gnus-decoded
)))
256 (let ((coding-system (mm-charset-to-coding-system
257 ;; Allow overwrite using
258 ;; `mm-charset-override-alist'.
260 (if (and (not coding-system
)
261 (listp mail-parse-ignored-charsets
)
262 (memq 'gnus-unknown mail-parse-ignored-charsets
))
264 (mm-charset-to-coding-system mail-parse-charset
)))
265 (when (and charset coding-system
266 ;; buffer-file-coding-system
267 ;;Article buffer is nil coding system
270 (or (not (eq coding-system
'ascii
))
271 (setq coding-system mail-parse-charset
)))
272 (mm-decode-coding-region (point-min) (point-max)
274 (setq buffer-file-coding-system
275 (if (boundp 'last-coding-system-used
)
276 (symbol-value 'last-coding-system-used
)
279 (defun mm-decode-string (string charset
)
280 "Decode STRING with CHARSET."
281 (when (stringp charset
)
282 (setq charset
(intern (downcase charset
))))
283 (when (or (not charset
)
284 (eq 'gnus-all mail-parse-ignored-charsets
)
285 (memq 'gnus-all mail-parse-ignored-charsets
)
286 (memq charset mail-parse-ignored-charsets
))
287 (setq charset mail-parse-charset
))
289 (when (featurep 'mule
)
290 (let ((coding-system (mm-charset-to-coding-system
292 ;; Allow overwrite using
293 ;; `mm-charset-override-alist'.
295 (if (and (not coding-system
)
296 (listp mail-parse-ignored-charsets
)
297 (memq 'gnus-unknown mail-parse-ignored-charsets
))
299 (mm-charset-to-coding-system mail-parse-charset
)))
300 (when (and charset coding-system
302 (or (not (eq coding-system
'ascii
))
303 (setq coding-system mail-parse-charset
)))
304 (mm-decode-coding-string string coding-system
))))
309 ;;; arch-tag: 41104bb6-4443-4ca9-8d5c-ff87ecf27d8d
310 ;;; mm-bodies.el ends here