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 (defvar mm-uu-decode-function)
31 (defvar mm-uu-binhex-decode-function))
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 ;; We MUST encode UTF-16 because it can contain \0's which is
45 ;; known to break servers.
46 ;; Note: UTF-16 variants are invalid for text parts [RFC 2781],
47 ;; so this can't happen :-/.
51 "Alist of MIME charsets to encodings.
52 Valid encodings are `7bit', `8bit', `quoted-printable' and `base64'."
53 :type '(repeat (cons (symbol :tag "charset")
54 (choice :tag "encoding"
57 (const quoted-printable)
61 (defun mm-encode-body (&optional charset)
63 Should be called narrowed to the body that is to be encoded.
64 If there is more than one non-ASCII MULE charset in the body, then the
65 list of MULE charsets found is returned.
66 If CHARSET is non-nil, it is used as the MIME charset to encode the body.
67 If successful, the MIME charset is returned.
68 If no encoding was done, nil is returned."
69 (if (not (mm-multibyte-p))
70 ;; In the non-Mule case, we search for non-ASCII chars and
71 ;; return the value of `mail-parse-charset' if any are found.
74 (goto-char (point-min))
75 (if (re-search-forward "[^\x0-\x7f]" nil t)
76 (or mail-parse-charset
77 (message-options-get 'mm-encody-body-charset)
79 'mm-encody-body-charset
80 (mm-read-coding-system "Charset used in the article: ")))
81 ;; The logic in `mml-generate-mime-1' confirms that it's OK
82 ;; to return nil here.
87 (mm-encode-coding-region (point-min) (point-max) charset)
89 (goto-char (point-min))
90 (let ((charsets (mm-find-mime-charset-region (point-min) (point-max)
97 ((> (length charsets) 1)
102 (setq charset (car charsets))
103 (mm-encode-coding-region (point-min) (point-max)
104 (mm-charset-to-coding-system charset))))
107 (defun mm-long-lines-p (length)
108 "Say whether any of the lines in the buffer is longer than LENGTH."
110 (goto-char (point-min))
112 (while (and (not (eobp))
113 (not (> (current-column) length)))
116 (and (> (current-column) length)
119 (defvar message-posting-charset)
121 (defun mm-body-encoding (charset &optional encoding)
122 "Do Content-Transfer-Encoding and return the encoding of the current buffer."
123 (when (stringp encoding)
124 (setq encoding (intern (downcase encoding))))
125 (let ((bits (mm-body-7-or-8))
126 (longp (mm-long-lines-p 1000)))
130 (not (and mm-use-ultra-safe-encoding
131 (save-excursion (re-search-forward "^From " nil t))))
134 ((and (not mm-use-ultra-safe-encoding)
136 (not (cdr (assq charset mm-body-charset-encoding-alist)))
137 (or (eq t (cdr message-posting-charset))
138 (memq charset (cdr message-posting-charset))
139 (eq charset mail-parse-charset)))
142 (let ((encoding (or encoding
143 (cdr (assq charset mm-body-charset-encoding-alist))
145 (when mm-use-ultra-safe-encoding
146 (setq encoding (mm-safer-encoding encoding)))
147 (mm-encode-content-transfer-encoding encoding "text/plain")
150 (defun mm-body-7-or-8 ()
151 "Say whether the body is 7bit or 8bit."
153 (goto-char (point-min))
154 (skip-chars-forward mm-7bit-chars)
160 ;;; Functions for decoding
163 (eval-when-compile (defvar mm-uu-yenc-decode-function))
165 (defun mm-decode-content-transfer-encoding (encoding &optional type)
166 "Decodes buffer encoded with ENCODING, returning success status.
167 If TYPE is `text/plain' CRLF->LF translation may occur."
169 (condition-case error
171 ((eq encoding 'quoted-printable)
172 (quoted-printable-decode-region (point-min) (point-max))
174 ((eq encoding 'base64)
175 (base64-decode-region
177 ;; Some mailers insert whitespace
178 ;; junk at the end which
179 ;; base64-decode-region dislikes.
180 ;; Also remove possible junk which could
181 ;; have been added by mailing list software.
183 (goto-char (point-min))
184 (while (re-search-forward "^[\t ]*\r?\n" nil t)
185 (delete-region (match-beginning 0) (match-end 0)))
186 (goto-char (point-max))
187 (when (re-search-backward "^[A-Za-z0-9+/]+=*[\t ]*$" nil t)
190 ((memq encoding '(7bit 8bit binary))
196 ((memq encoding '(x-uuencode x-uue))
198 (funcall mm-uu-decode-function (point-min) (point-max))
200 ((eq encoding 'x-binhex)
202 (funcall mm-uu-binhex-decode-function (point-min) (point-max))
204 ((eq encoding 'x-yenc)
206 (funcall mm-uu-yenc-decode-function (point-min) (point-max))
208 ((functionp encoding)
209 (funcall encoding (point-min) (point-max))
212 (message "Unknown encoding %s; defaulting to 8bit" encoding)))
214 (message "Error while decoding: %s" error)
217 (memq encoding '(base64 x-uuencode x-uue x-binhex x-yenc))
218 (equal type "text/plain"))
219 (goto-char (point-min))
220 (while (search-forward "\r\n" nil t)
221 (replace-match "\n" t t)))))
223 (defun mm-decode-body (charset &optional encoding type)
224 "Decode the current article that has been encoded with ENCODING to CHARSET.
225 ENCODING is a MIME content transfer encoding.
226 CHARSET is the MIME charset with which to decode the data after transfer
227 decoding. If it is nil, default to `mail-parse-charset'."
228 (when (stringp charset)
229 (setq charset (intern (downcase charset))))
230 (when (or (not charset)
231 (eq 'gnus-all mail-parse-ignored-charsets)
232 (memq 'gnus-all mail-parse-ignored-charsets)
233 (memq charset mail-parse-ignored-charsets))
234 (setq charset mail-parse-charset))
237 (mm-decode-content-transfer-encoding encoding type))
238 (when (featurep 'mule) ; Fixme: Wrong test for unibyte session.
239 (let ((coding-system (mm-charset-to-coding-system charset)))
240 (if (and (not coding-system)
241 (listp mail-parse-ignored-charsets)
242 (memq 'gnus-unknown mail-parse-ignored-charsets))
244 (mm-charset-to-coding-system mail-parse-charset)))
245 (when (and charset coding-system
246 ;; buffer-file-coding-system
247 ;;Article buffer is nil coding system
250 (or (not (eq coding-system 'ascii))
251 (setq coding-system mail-parse-charset))
252 (not (eq coding-system 'gnus-decoded)))
253 (mm-decode-coding-region (point-min) (point-max)
255 (setq buffer-file-coding-system
256 (if (boundp 'last-coding-system-used)
257 (symbol-value 'last-coding-system-used)
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