135b974992579d0f82c6c82fcf77020a91c76363
[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       (autoload 'base64-decode-region "base64" nil t)))
30 (require 'mm-util)
31 (require 'rfc2047)
32 (require 'qp)
33
34 (defun mm-encode-body ()
35   "Encode a body.
36 Should be called narrowed to the body that is to be encoded.
37 If there is more than one non-ASCII MULE charset, then list of found
38 MULE charsets are returned.
39 If successful, the MIME charset is returned.
40 If no encoding was done, nil is returned."
41   (save-excursion
42     (goto-char (point-min))
43     (let ((charsets
44            (delq 'ascii (find-charset-region (point-min) (point-max))))
45           charset)
46       (cond
47        ;; No encoding.
48        ((null charsets)
49         nil)
50        ;; Too many charsets.
51        ((> (length charsets) 1)
52         charsets)
53        ;; We encode.
54        (t
55         (let ((mime-charset 
56                (mm-mime-charset (car charsets) (point-min) (point-max)))
57               start)
58           (when (or t
59                     ;; We always decode.
60                     (not (mm-coding-system-equal
61                           mime-charset buffer-file-coding-system)))
62             (while (not (eobp))
63               (if (eq (char-charset (following-char)) 'ascii)
64                   (when start
65                     (save-restriction
66                       (narrow-to-region start (point))
67                       (mm-encode-coding-region start (point) mime-charset)
68                       (goto-char (point-max)))
69                     (setq start nil))
70                 (unless start
71                   (setq start (point))))
72               (forward-char 1))
73             (when start
74               (mm-encode-coding-region start (point) mime-charset)
75               (setq start nil)))
76           mime-charset))))))
77
78 (defun mm-body-encoding ()
79   "Return the encoding of the current buffer."
80   (if (and  
81        (null (delq 'ascii (find-charset-region (point-min) (point-max))))
82        ;;;!!!The following is necessary because the function
83        ;;;!!!above seems to return the wrong result under Emacs 20.3.
84        ;;;!!!Sometimes.
85        (save-excursion
86          (goto-char (point-min))
87          (skip-chars-forward "\0-\177")
88          (eobp)))
89       '7bit
90     '8bit))
91
92 ;;;
93 ;;; Functions for decoding
94 ;;;
95
96 (defun mm-decode-content-transfer-encoding (encoding)
97   (cond
98    ((eq encoding 'quoted-printable)
99     (quoted-printable-decode-region (point-min) (point-max)))
100    ((eq encoding 'base64)
101     (condition-case ()
102         (base64-decode-region (point-min) (point-max))
103       (error nil)))
104    ((memq encoding '(7bit 8bit binary))
105     )
106    ((null encoding)
107     )
108    (t
109     (error "Can't decode encoding %s" encoding))))
110
111 (defun mm-decode-body (charset encoding)
112   "Decode the current article that has been encoded with ENCODING.
113 The characters in CHARSET should then be decoded."
114   (setq charset (or charset rfc2047-default-charset))
115   (save-excursion
116     (when encoding
117       (mm-decode-content-transfer-encoding encoding))
118     (when (featurep 'mule)
119       (let (mule-charset)
120         (when (and charset
121                    (setq mule-charset (mm-charset-to-coding-system charset))
122                    buffer-file-coding-system
123                    (or (not (eq mule-charset 'ascii))
124                        (setq mule-charset rfc2047-default-charset)))
125           (mm-decode-coding-region (point-min) (point-max) mule-charset))))))
126
127 (provide 'mm-bodies)
128
129 ;; mm-bodies.el ends here