1 ;;; mm-bodies.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998,99 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
28 (or (fboundp 'base64-decode-region)
30 (autoload 'binhex-decode-region "binhex"))
37 ;; 8bit treatment gets any char except: 0x32 - 0x7f, CR, LF, TAB, BEL,
38 ;; BS, vertical TAB, form feed, and ^_
39 (defvar mm-8bit-char-regexp "[^\x20-\x7f\r\n\t\x7\x8\xb\xc\x1f]")
41 (defvar mm-body-charset-encoding-alist nil
42 "Alist of MIME charsets to encodings.
43 Valid encodings are `7bit', `8bit', `quoted-printable' and `base64'.")
45 (defun mm-encode-body ()
47 Should be called narrowed to the body that is to be encoded.
48 If there is more than one non-ASCII MULE charset, then list of found
49 MULE charsets are returned.
50 If successful, the MIME charset is returned.
51 If no encoding was done, nil is returned."
52 (if (not (featurep 'mule))
53 ;; In the non-Mule case, we search for non-ASCII chars and
54 ;; return the value of `mm-default-charset' if any are found.
56 (goto-char (point-min))
57 (if (re-search-forward "[^\x0-\x7f]" nil t)
58 (or mail-parse-charset
59 (mm-read-charset "Charset used in the article: "))
60 ;; The logic in `mml-generate-mime-1' confirms that it's OK
61 ;; to return nil here.
64 (goto-char (point-min))
65 (let ((charsets (mm-find-mime-charset-region (point-min) (point-max)))
72 ((> (length charsets) 1)
76 (let ((charset (car charsets))
80 (not (mm-coding-system-equal
81 charset buffer-file-coding-system)))
83 (if (eq (char-charset (char-after)) 'ascii)
86 (narrow-to-region start (point))
87 (mm-encode-coding-region start (point) charset)
88 (goto-char (point-max)))
91 (setq start (point))))
94 (mm-encode-coding-region start (point) charset)
98 (defun mm-body-encoding (charset &optional encoding)
99 "Do Content-Transfer-Encoding and return the encoding of the current buffer."
100 (let ((bits (mm-body-7-or-8)))
104 ((eq charset mail-parse-charset)
107 (let ((encoding (or encoding
108 (cdr (assq charset mm-body-charset-encoding-alist))
110 (mm-encode-content-transfer-encoding encoding "text/plain")
113 (defun mm-body-7-or-8 ()
114 "Say whether the body is 7bit or 8bit."
116 ((not (featurep 'mule))
118 (goto-char (point-min))
119 (re-search-forward mm-8bit-char-regexp nil t))
124 (if (and (null (delq 'ascii
125 (mm-find-charset-region (point-min) (point-max))))
126 ;;!!!The following is necessary because the function
127 ;;!!!above seems to return the wrong result under
128 ;;!!!Emacs 20.3. Sometimes.
130 (goto-char (point-min))
131 (skip-chars-forward "\0-\177")
137 ;;; Functions for decoding
140 (defun mm-decode-content-transfer-encoding (encoding &optional type)
142 (condition-case error
144 ((eq encoding 'quoted-printable)
145 (quoted-printable-decode-region (point-min) (point-max)))
146 ((eq encoding 'base64)
147 (base64-decode-region (point-min)
148 ;; Some mailers insert whitespace
149 ;; junk at the end which
150 ;; base64-decode-region dislikes.
152 (goto-char (point-max))
153 (skip-chars-backward "\n\t ")
154 (delete-region (point) (point-max))
156 ((memq encoding '(7bit 8bit binary))
160 ((memq encoding '(x-uuencode x-uue))
161 (funcall mm-uu-decode-function (point-min) (point-max)))
162 ((eq encoding 'x-binhex)
163 (funcall mm-uu-binhex-decode-function (point-min) (point-max)))
164 ((functionp encoding)
165 (funcall encoding (point-min) (point-max)))
167 (message "Unknown encoding %s; defaulting to 8bit" encoding)))
169 (message "Error while decoding: %s" error)
172 (memq encoding '(base64 x-uuencode x-uue x-binhex))
173 (equal type "text/plain"))
174 (goto-char (point-min))
175 (while (search-forward "\r\n" nil t)
176 (replace-match "\n" t t)))))
178 (defun mm-decode-body (charset &optional encoding type)
179 "Decode the current article that has been encoded with ENCODING.
180 The characters in CHARSET should then be decoded."
181 (if (stringp charset)
182 (setq charset (intern (downcase charset))))
183 (if (or (not charset)
184 (eq 'gnus-all mail-parse-ignored-charsets)
185 (memq 'gnus-all mail-parse-ignored-charsets)
186 (memq charset mail-parse-ignored-charsets))
187 (setq charset mail-parse-charset))
190 (mm-decode-content-transfer-encoding encoding type))
191 (when (featurep 'mule)
192 (let ((mule-charset (mm-charset-to-coding-system charset)))
193 (if (and (not mule-charset)
194 (listp mail-parse-ignored-charsets)
195 (memq 'gnus-unknown mail-parse-ignored-charsets))
197 (mm-charset-to-coding-system mail-parse-charset)))
198 (when (and charset mule-charset
199 ;; buffer-file-coding-system
200 ;;Article buffer is nil coding system
203 (or (not (eq mule-charset 'ascii))
204 (setq mule-charset mail-parse-charset))
205 (not (eq mule-charset 'gnus-decoded)))
206 (mm-decode-coding-region (point-min) (point-max) mule-charset))))))
208 (defun mm-decode-string (string charset)
209 "Decode STRING with CHARSET."
210 (if (stringp charset)
211 (setq charset (intern (downcase charset))))
212 (if (or (not charset)
213 (eq 'gnus-all mail-parse-ignored-charsets)
214 (memq 'gnus-all mail-parse-ignored-charsets)
215 (memq charset mail-parse-ignored-charsets))
216 (setq charset mail-parse-charset))
218 (when (featurep 'mule)
219 (let ((mule-charset (mm-charset-to-coding-system charset)))
220 (if (and (not mule-charset)
221 (listp mail-parse-ignored-charsets)
222 (memq 'gnus-unknown mail-parse-ignored-charsets))
224 (mm-charset-to-coding-system mail-parse-charset)))
225 (when (and charset mule-charset
227 (or (not (eq mule-charset 'ascii))
228 (setq mule-charset mail-parse-charset)))
229 (mm-decode-coding-string string mule-charset))))
234 ;; mm-bodies.el ends here