1 ;;; mm-bodies.el --- Functions for decoding MIME things
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, 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, 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 :-/.
62 "Alist of MIME charsets to encodings.
63 Valid encodings are `7bit', `8bit', `quoted-printable' and `base64'."
64 :type '(repeat (cons (symbol :tag "charset")
65 (choice :tag "encoding"
68 (const quoted-printable)
72 (defun mm-encode-body (&optional charset)
74 Should be called narrowed to the body that is to be encoded.
75 If there is more than one non-ASCII MULE charset in the body, then the
76 list of MULE charsets found is returned.
77 If CHARSET is non-nil, it is used as the MIME charset to encode the body.
78 If successful, the MIME charset is returned.
79 If no encoding was done, nil is returned."
80 (if (not (mm-multibyte-p))
81 ;; In the non-Mule case, we search for non-ASCII chars and
82 ;; return the value of `mail-parse-charset' if any are found.
85 (goto-char (point-min))
86 (if (re-search-forward "[^\x0-\x7f]" nil t)
87 (or mail-parse-charset
88 (message-options-get 'mm-encody-body-charset)
90 'mm-encody-body-charset
91 (mm-read-coding-system "Charset used in the article: ")))
92 ;; The logic in `mml-generate-mime-1' confirms that it's OK
93 ;; to return nil here.
98 (mm-encode-coding-region (point-min) (point-max)
99 (mm-charset-to-coding-system charset))
101 (goto-char (point-min))
102 (let ((charsets (mm-find-mime-charset-region (point-min) (point-max)
108 ;; Too many charsets.
109 ((> (length charsets) 1)
114 (setq charset (car charsets))
115 (mm-encode-coding-region (point-min) (point-max)
116 (mm-charset-to-coding-system charset))))
119 (defun mm-long-lines-p (length)
120 "Say whether any of the lines in the buffer is longer than LENGTH."
122 (goto-char (point-min))
124 (while (and (not (eobp))
125 (not (> (current-column) length)))
128 (and (> (current-column) length)
131 (defvar message-posting-charset)
133 (defun mm-body-encoding (charset &optional encoding)
134 "Do Content-Transfer-Encoding and return the encoding of the current buffer."
135 (when (stringp encoding)
136 (setq encoding (intern (downcase encoding))))
137 (let ((bits (mm-body-7-or-8))
138 (longp (mm-long-lines-p 1000)))
142 (not (and mm-use-ultra-safe-encoding
143 (or (save-excursion (re-search-forward " $" nil t))
144 (save-excursion (re-search-forward "^From " nil t)))))
147 ((and (not mm-use-ultra-safe-encoding)
149 (not (cdr (assq charset mm-body-charset-encoding-alist)))
150 (or (eq t (cdr message-posting-charset))
151 (memq charset (cdr message-posting-charset))
152 (eq charset mail-parse-charset)))
155 (let ((encoding (or encoding
156 (cdr (assq charset mm-body-charset-encoding-alist))
158 (when mm-use-ultra-safe-encoding
159 (setq encoding (mm-safer-encoding encoding)))
160 (mm-encode-content-transfer-encoding encoding "text/plain")
163 (defun mm-body-7-or-8 ()
164 "Say whether the body is 7bit or 8bit."
166 (goto-char (point-min))
167 (skip-chars-forward mm-7bit-chars)
173 ;;; Functions for decoding
176 (eval-when-compile (defvar mm-uu-yenc-decode-function))
178 (defun mm-decode-content-transfer-encoding (encoding &optional type)
179 "Decodes buffer encoded with ENCODING, returning success status.
180 If TYPE is `text/plain' CRLF->LF translation may occur."
182 (condition-case error
184 ((eq encoding 'quoted-printable)
185 (quoted-printable-decode-region (point-min) (point-max))
187 ((eq encoding 'base64)
188 (base64-decode-region
190 ;; Some mailers insert whitespace
191 ;; junk at the end which
192 ;; base64-decode-region dislikes.
193 ;; Also remove possible junk which could
194 ;; have been added by mailing list software.
196 (goto-char (point-min))
197 (while (re-search-forward "^[\t ]*\r?\n" nil t)
198 (delete-region (match-beginning 0) (match-end 0)))
199 (goto-char (point-max))
200 (when (re-search-backward "^[A-Za-z0-9+/]+=*[\t ]*$" nil t)
203 ((memq encoding '(7bit 8bit binary))
209 ((memq encoding '(x-uuencode x-uue))
211 (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))
217 ((eq encoding 'x-yenc)
219 (funcall mm-uu-yenc-decode-function (point-min) (point-max))
221 ((functionp encoding)
222 (funcall encoding (point-min) (point-max))
225 (message "Unknown encoding %s; defaulting to 8bit" encoding)))
227 (message "Error while decoding: %s" error)
230 (memq encoding '(base64 x-uuencode x-uue x-binhex x-yenc))
231 (equal type "text/plain"))
232 (goto-char (point-min))
233 (while (search-forward "\r\n" nil t)
234 (replace-match "\n" t t)))))
236 (defun mm-decode-body (charset &optional encoding type)
237 "Decode the current article that has been encoded with ENCODING to CHARSET.
238 ENCODING is a MIME content transfer encoding.
239 CHARSET is the MIME charset with which to decode the data after transfer
240 decoding. If it is nil, default to `mail-parse-charset'."
241 (when (stringp charset)
242 (setq charset (intern (downcase charset))))
243 (when (or (not charset)
244 (eq 'gnus-all mail-parse-ignored-charsets)
245 (memq 'gnus-all mail-parse-ignored-charsets)
246 (memq charset mail-parse-ignored-charsets))
247 (setq charset mail-parse-charset))
250 (mm-decode-content-transfer-encoding encoding type))
251 (when (featurep 'mule) ; Fixme: Wrong test for unibyte session.
252 (let ((coding-system (mm-charset-to-coding-system charset)))
253 (if (and (not coding-system)
254 (listp mail-parse-ignored-charsets)
255 (memq 'gnus-unknown mail-parse-ignored-charsets))
257 (mm-charset-to-coding-system mail-parse-charset)))
258 (when (and charset coding-system
259 ;; buffer-file-coding-system
260 ;;Article buffer is nil coding system
263 (or (not (eq coding-system 'ascii))
264 (setq coding-system mail-parse-charset))
265 (not (eq coding-system 'gnus-decoded)))
266 (mm-decode-coding-region (point-min) (point-max)
268 (setq buffer-file-coding-system
269 (if (boundp 'last-coding-system-used)
270 (symbol-value 'last-coding-system-used)
273 (defun mm-decode-string (string charset)
274 "Decode STRING with CHARSET."
275 (when (stringp charset)
276 (setq charset (intern (downcase charset))))
277 (when (or (not charset)
278 (eq 'gnus-all mail-parse-ignored-charsets)
279 (memq 'gnus-all mail-parse-ignored-charsets)
280 (memq charset mail-parse-ignored-charsets))
281 (setq charset mail-parse-charset))
283 (when (featurep 'mule)
284 (let ((coding-system (mm-charset-to-coding-system charset)))
285 (if (and (not coding-system)
286 (listp mail-parse-ignored-charsets)
287 (memq 'gnus-unknown mail-parse-ignored-charsets))
289 (mm-charset-to-coding-system mail-parse-charset)))
290 (when (and charset coding-system
292 (or (not (eq coding-system 'ascii))
293 (setq coding-system mail-parse-charset)))
294 (mm-decode-coding-string string coding-system))))
299 ;;; arch-tag: 41104bb6-4443-4ca9-8d5c-ff87ecf27d8d
300 ;;; mm-bodies.el ends here