1 ;;; mm-bodies.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998 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)
36 (defun mm-encode-body ()
38 Should be called narrowed to the body that is to be encoded.
39 If there is more than one non-ASCII MULE charset, then list of found
40 MULE charsets are returned.
41 If successful, the MIME charset is returned.
42 If no encoding was done, nil is returned."
44 (goto-char (point-min))
46 (delq 'ascii (find-charset-region (point-min) (point-max))))
53 ((> (length charsets) 1)
58 (mm-mime-charset (car charsets) (point-min) (point-max)))
62 (not (mm-coding-system-equal
63 mime-charset buffer-file-coding-system)))
65 (if (eq (char-charset (char-after)) 'ascii)
68 (narrow-to-region start (point))
69 (mm-encode-coding-region start (point) mime-charset)
70 (goto-char (point-max)))
73 (setq start (point))))
76 (mm-encode-coding-region start (point) mime-charset)
80 (defun mm-body-encoding ()
81 "Return the encoding of the current buffer."
83 (null (delq 'ascii (find-charset-region (point-min) (point-max))))
84 ;;;!!!The following is necessary because the function
85 ;;;!!!above seems to return the wrong result under Emacs 20.3.
88 (goto-char (point-min))
89 (skip-chars-forward "\0-\177")
95 ;;; Functions for decoding
98 (defun mm-decode-content-transfer-encoding (encoding &optional type)
100 ((eq encoding 'quoted-printable)
101 (quoted-printable-decode-region (point-min) (point-max)))
102 ((eq encoding 'base64)
105 (base64-decode-region (point-min) (point-max))
107 (when (equal type "text/plain")
108 (goto-char (point-min))
109 (while (search-forward "\r\n" nil t)
110 (replace-match "\n" t t)))))
111 ((memq encoding '(7bit 8bit binary))
115 ((eq encoding 'x-uuencode)
117 (uudecode-decode-region (point-min) (point-max))
119 ((functionp encoding)
121 (funcall encoding (point-min) (point-max))
124 (message "Unknown encoding %s; defaulting to 8bit" encoding)
127 (defun mm-decode-body (charset &optional encoding)
128 "Decode the current article that has been encoded with ENCODING.
129 The characters in CHARSET should then be decoded."
130 (setq charset (or charset rfc2047-default-charset))
133 (mm-decode-content-transfer-encoding encoding))
134 (when (featurep 'mule)
137 (setq mule-charset (mm-charset-to-coding-system charset))
138 buffer-file-coding-system
139 enable-multibyte-characters
140 (or (not (eq mule-charset 'ascii))
141 (setq mule-charset rfc2047-default-charset)))
142 (mm-decode-coding-region (point-min) (point-max) mule-charset))))))
146 ;; mm-bodies.el ends here