1 ;;; mm-bodies.el --- Functions for decoding MIME things
3 ;; Copyright (C) 1998, 1999, 2000, 2001
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 "Alist of MIME charsets to encodings.
49 Valid encodings are `7bit', `8bit', `quoted-printable' and `base64'."
50 :type '(repeat (cons (symbol :tag "charset")
51 (choice :tag "encoding"
54 (const quoted-printable)
58 (defun mm-encode-body (&optional charset)
60 Should be called narrowed to the body that is to be encoded.
61 If there is more than one non-ASCII MULE charset, then list of found
62 MULE charsets are returned.
63 If CHARSET is non-nil, it is used.
64 If successful, the MIME charset is returned.
65 If no encoding was done, nil is returned."
66 (if (not (mm-multibyte-p))
67 ;; In the non-Mule case, we search for non-ASCII chars and
68 ;; return the value of `mail-parse-charset' if any are found.
71 (goto-char (point-min))
72 (if (re-search-forward "[^\x0-\x7f]" nil t)
73 (or mail-parse-charset
74 (message-options-get 'mm-encody-body-charset)
76 'mm-encody-body-charset
77 (mm-read-charset "Charset used in the article: ")))
78 ;; The logic in `mml-generate-mime-1' confirms that it's OK
79 ;; to return nil here.
84 (mm-encode-coding-region (point-min) (point-max) charset)
86 (goto-char (point-min))
87 (let ((charsets (mm-find-mime-charset-region (point-min) (point-max)
95 ((> (length charsets) 1)
99 (setq charset (car charsets))
101 (if (eq (mm-charset-after) 'ascii)
104 (narrow-to-region start (point))
105 (mm-encode-coding-region
106 start (point) (mm-charset-to-coding-system charset))
107 (goto-char (point-max)))
110 (setq start (point))))
113 (mm-encode-coding-region start (point)
114 (mm-charset-to-coding-system charset))
118 (defun mm-long-lines-p (length)
119 "Say whether any of the lines in the buffer is longer than LINES."
121 (goto-char (point-min))
123 (while (and (not (eobp))
124 (not (> (current-column) length)))
127 (and (> (current-column) length)
130 (defvar message-posting-charset)
132 (defun mm-body-encoding (charset &optional encoding)
133 "Do Content-Transfer-Encoding and return the encoding of the current buffer."
134 (when (stringp encoding)
135 (setq encoding (intern (downcase encoding))))
136 (let ((bits (mm-body-7-or-8))
137 (longp (mm-long-lines-p 1000)))
140 ((and (not mm-use-ultra-safe-encoding)
144 ((and (not mm-use-ultra-safe-encoding)
146 (or (eq t (cdr message-posting-charset))
147 (memq charset (cdr message-posting-charset))
148 (eq charset mail-parse-charset)))
151 (let ((encoding (or encoding
152 (cdr (assq charset mm-body-charset-encoding-alist))
154 (when mm-use-ultra-safe-encoding
155 (setq encoding (mm-safer-encoding encoding)))
156 (mm-encode-content-transfer-encoding encoding "text/plain")
159 (defun mm-body-7-or-8 ()
160 "Say whether the body is 7bit or 8bit."
162 ((not (featurep 'mule))
164 (goto-char (point-min))
165 (skip-chars-forward mm-7bit-chars)
171 (if (and (null (delq 'ascii
172 (mm-find-charset-region (point-min) (point-max))))
173 ;;!!!The following is necessary because the function
174 ;;!!!above seems to return the wrong result under
175 ;;!!!Emacs 20.3. Sometimes.
177 (goto-char (point-min))
178 (skip-chars-forward mm-7bit-chars)
184 ;;; Functions for decoding
187 (defun mm-decode-content-transfer-encoding (encoding &optional type)
188 "Decodes buffer encoded with ENCODING, returning success status.
189 If TYPE is `text/plain' CRLF->LF translation may occur."
191 (condition-case error
193 ((eq encoding 'quoted-printable)
194 (quoted-printable-decode-region (point-min) (point-max))
196 ((eq encoding 'base64)
197 (base64-decode-region
199 ;; Some mailers insert whitespace
200 ;; junk at the end which
201 ;; base64-decode-region dislikes.
202 ;; Also remove possible junk which could
203 ;; have been added by mailing list software.
205 (goto-char (point-min))
206 (while (re-search-forward "^[\t ]*\r?\n" nil t)
207 (delete-region (match-beginning 0) (match-end 0)))
208 (goto-char (point-max))
209 (when (re-search-backward "^[A-Za-z0-9+/]+=*[\t ]*$" nil t)
212 ((memq encoding '(7bit 8bit binary))
218 ((memq encoding '(x-uuencode x-uue))
220 (funcall mm-uu-decode-function (point-min) (point-max))
222 ((eq encoding 'x-binhex)
224 (funcall mm-uu-binhex-decode-function (point-min) (point-max))
226 ((functionp encoding)
227 (funcall encoding (point-min) (point-max))
230 (message "Unknown encoding %s; defaulting to 8bit" encoding)))
232 (message "Error while decoding: %s" error)
235 (memq encoding '(base64 x-uuencode x-uue x-binhex))
236 (equal type "text/plain"))
237 (goto-char (point-min))
238 (while (search-forward "\r\n" nil t)
239 (replace-match "\n" t t)))))
241 (defun mm-decode-body (charset &optional encoding type)
242 "Decode the current article that has been encoded with ENCODING.
243 The characters in CHARSET should then be decoded."
244 (if (stringp charset)
245 (setq charset (intern (downcase charset))))
246 (if (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 (featurep 'mule)
255 (let ((coding-system (mm-charset-to-coding-system charset)))
256 (if (and (not coding-system)
257 (listp mail-parse-ignored-charsets)
258 (memq 'gnus-unknown mail-parse-ignored-charsets))
260 (mm-charset-to-coding-system mail-parse-charset)))
261 (when (and charset coding-system
262 ;; buffer-file-coding-system
263 ;;Article buffer is nil coding system
266 (or (not (eq coding-system 'ascii))
267 (setq coding-system mail-parse-charset))
268 (not (eq coding-system 'gnus-decoded)))
269 (mm-decode-coding-region (point-min) (point-max) coding-system))))))
271 (defun mm-decode-string (string charset)
272 "Decode STRING with CHARSET."
273 (when (stringp charset)
274 (setq charset (intern (downcase charset))))
275 (when (or (not charset)
276 (eq 'gnus-all mail-parse-ignored-charsets)
277 (memq 'gnus-all mail-parse-ignored-charsets)
278 (memq charset mail-parse-ignored-charsets))
279 (setq charset mail-parse-charset))
281 (when (featurep 'mule)
282 (let ((coding-system (mm-charset-to-coding-system charset)))
283 (if (and (not coding-system)
284 (listp mail-parse-ignored-charsets)
285 (memq 'gnus-unknown mail-parse-ignored-charsets))
287 (mm-charset-to-coding-system mail-parse-charset)))
288 (when (and charset coding-system
290 (or (not (eq coding-system 'ascii))
291 (setq coding-system mail-parse-charset)))
292 (mm-decode-coding-string string coding-system))))
297 ;;; mm-bodies.el ends here