d4e05f07e69e1820a0c08f48bcef3eaa4a7faa11
[gnus] / lisp / mm-bodies.el
1 ;;; mm-bodies.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
7
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)
11 ;; any later version.
12
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.
17
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.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-and-compile
28   (or (fboundp  'base64-decode-region)
29       (require 'base64)))
30
31 (require 'mm-util)
32 (require 'rfc2047)
33 (require 'qp)
34 (require 'uudecode)
35
36 (defun mm-encode-body ()
37   "Encode a 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."
43   (save-excursion
44     (goto-char (point-min))
45     (let ((charsets
46            (delq 'ascii (mm-find-charset-region (point-min) (point-max))))
47           charset)
48       (cond
49        ;; No encoding.
50        ((null charsets)
51         nil)
52        ;; Too many charsets.
53        ((> (length charsets) 1)
54         charsets)
55        ;; We encode.
56        (t
57         (let ((mime-charset 
58                (mm-mime-charset (car charsets) (point-min) (point-max)))
59               start)
60           (when (or t
61                     ;; We always decode.
62                     (not (mm-coding-system-equal
63                           mime-charset buffer-file-coding-system)))
64             (while (not (eobp))
65               (if (eq (char-charset (char-after)) 'ascii)
66                   (when start
67                     (save-restriction
68                       (narrow-to-region start (point))
69                       (mm-encode-coding-region start (point) mime-charset)
70                       (goto-char (point-max)))
71                     (setq start nil))
72                 (unless start
73                   (setq start (point))))
74               (forward-char 1))
75             (when start
76               (mm-encode-coding-region start (point) mime-charset)
77               (setq start nil)))
78           mime-charset))))))
79
80 (defun mm-body-encoding ()
81   "Return the encoding of the current buffer."
82   (if (and  
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.
86        ;;;!!!Sometimes.
87        (save-excursion
88          (goto-char (point-min))
89          (skip-chars-forward "\0-\177")
90          (eobp)))
91       '7bit
92     '8bit))
93
94 ;;;
95 ;;; Functions for decoding
96 ;;;
97
98 (defun mm-decode-content-transfer-encoding (encoding &optional type)
99   (cond
100    ((eq encoding 'quoted-printable)
101     (quoted-printable-decode-region (point-min) (point-max)))
102    ((eq encoding 'base64)
103     (prog1
104         (condition-case ()
105             (base64-decode-region (point-min) (point-max))
106           (error nil))
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))
112     )
113    ((null encoding)
114     )
115    ((eq encoding 'x-uuencode)
116     (condition-case ()
117         (uudecode-decode-region (point-min) (point-max))
118       (error nil)))
119    ((functionp encoding)
120     (condition-case ()
121         (funcall encoding (point-min) (point-max))
122       (error nil)))
123    (t
124     (message "Unknown encoding %s; defaulting to 8bit" encoding))))
125
126 (defun mm-decode-body (charset &optional encoding type)
127   "Decode the current article that has been encoded with ENCODING.
128 The characters in CHARSET should then be decoded."
129   (setq charset (or charset rfc2047-default-charset))
130   (save-excursion
131     (when encoding
132       (mm-decode-content-transfer-encoding encoding type))
133     (when (featurep 'mule)
134       (let (mule-charset)
135         (when (and charset
136                    (setq mule-charset (mm-charset-to-coding-system charset))
137                    buffer-file-coding-system
138                    enable-multibyte-characters
139                    (or (not (eq mule-charset 'ascii))
140                        (setq mule-charset rfc2047-default-charset)))
141           (mm-decode-coding-region (point-min) (point-max) mule-charset))))))
142
143 (provide 'mm-bodies)
144
145 ;; mm-bodies.el ends here