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 (defun mm-encode-body ()
43 Should be called narrowed to the body that is to be encoded.
44 If there is more than one non-ASCII MULE charset, then list of found
45 MULE charsets are returned.
46 If successful, the MIME charset is returned.
47 If no encoding was done, nil is returned."
48 (if (not (featurep 'mule))
49 ;; In the non-Mule case, we search for non-ASCII chars and
50 ;; return the value of `mm-default-charset' if any are found.
52 (goto-char (point-min))
53 (if (re-search-forward "[^\x0-\x7f]" nil t)
54 (mm-read-charset "Charset used in the article: ")
55 ;; The logic in `mml-generate-mime-1' confirms that it's OK
56 ;; to return nil here.
59 (goto-char (point-min))
61 (delq 'ascii (mm-find-charset-region (point-min) (point-max))))
68 ((> (length charsets) 1)
73 (mm-mime-charset (car charsets) (point-min) (point-max)))
77 (not (mm-coding-system-equal
78 mime-charset buffer-file-coding-system)))
80 (if (eq (char-charset (char-after)) 'ascii)
83 (narrow-to-region start (point))
84 (mm-encode-coding-region start (point) mime-charset)
85 (goto-char (point-max)))
88 (setq start (point))))
91 (mm-encode-coding-region start (point) mime-charset)
95 (defun mm-body-encoding ()
96 "Return the encoding of the current buffer."
98 ((not (featurep 'mule))
100 (goto-char (point-min))
101 (re-search-forward mm-8bit-char-regexp nil t))
106 (if (and (null (delq 'ascii
107 (mm-find-charset-region (point-min) (point-max))))
108 ;;!!!The following is necessary because the function
109 ;;!!!above seems to return the wrong result under
110 ;;!!!Emacs 20.3. Sometimes.
112 (goto-char (point-min))
113 (skip-chars-forward "\0-\177")
119 ;;; Functions for decoding
122 (defun mm-decode-content-transfer-encoding (encoding &optional type)
124 (condition-case error
126 ((eq encoding 'quoted-printable)
127 (quoted-printable-decode-region (point-min) (point-max)))
128 ((eq encoding 'base64)
129 (base64-decode-region (point-min) (point-max)))
130 ((memq encoding '(7bit 8bit binary))
134 ((eq encoding 'x-uuencode)
135 (funcall mm-uu-decode-function (point-min) (point-max)))
136 ((eq encoding 'x-binhex)
137 (funcall mm-uu-binhex-decode-function (point-min) (point-max)))
138 ((functionp encoding)
139 (funcall encoding (point-min) (point-max)))
141 (message "Unknown encoding %s; defaulting to 8bit" encoding)))
143 (message "Error while decoding: %s" error)
146 (memq encoding '(base64 x-uuencode x-binhex))
147 (equal type "text/plain"))
148 (goto-char (point-min))
149 (while (search-forward "\r\n" nil t)
150 (replace-match "\n" t t)))))
152 (defun mm-decode-body (charset &optional encoding type)
153 "Decode the current article that has been encoded with ENCODING.
154 The characters in CHARSET should then be decoded."
155 (setq charset (or charset mail-parse-charset))
158 (mm-decode-content-transfer-encoding encoding type))
159 (when (featurep 'mule)
162 (setq mule-charset (mm-charset-to-coding-system charset))
163 ;; buffer-file-coding-system
164 ;Article buffer is nil coding system
166 enable-multibyte-characters
167 (or (not (eq mule-charset 'ascii))
168 (setq mule-charset mail-parse-charset)))
169 (mm-decode-coding-region (point-min) (point-max) mule-charset))))))
171 (defun mm-decode-string (string charset)
172 "Decode STRING with CHARSET."
173 (setq charset (or charset mail-parse-charset))
175 (when (featurep 'mule)
178 (setq mule-charset (mm-charset-to-coding-system charset))
179 enable-multibyte-characters
180 (or (not (eq mule-charset 'ascii))
181 (setq mule-charset mail-parse-charset)))
182 (mm-decode-coding-string string mule-charset))))
187 ;; mm-bodies.el ends here