*** empty log message ***
[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   (if (not (fboundp 'base64-encode-string))
29       (require 'base64)))
30 (require 'mm-util)
31 (require 'qp)
32
33 (defun mm-encode-body ()
34   "Encode a body.
35 Should be called narrowed to the body that is to be encoded.
36 If there is more than one non-ASCII MULE charset, then list of found
37 MULE charsets are returned.
38 If successful, the MIME charset is returned.
39 If no encoding was done, nil is returned."
40   (save-excursion
41     (goto-char (point-min))
42     (let ((charsets
43            (delq 'ascii (find-charset-region (point-min) (point-max))))
44           charset)
45       (cond
46        ;; No encoding.
47        ((null charsets)
48         nil)
49        ;; Too many charsets.
50        ((> (length charsets) 1)
51         charsets)
52        ;; We encode.
53        (t
54         (let ((mime-charset
55                (or
56                 (coding-system-get
57                  (get-charset-property (car charsets) 'prefered-coding-system)
58                  'mime-charset)
59                 (car (memq (car charsets)
60                            (find-coding-systems-region
61                             (point-min) (point-max))))))
62               start)
63           (when (or t
64                     ;; We always decode.
65                     (not (mm-coding-system-equal
66                           mime-charset buffer-file-coding-system)))
67             (while (not (eobp))
68               (if (eq (char-charset (following-char)) 'ascii)
69                   (when start
70                     (mm-encode-coding-region start (point) mime-charset)
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-body (charset encoding)
99   "Decode the current article that has been encoded with ENCODING.
100 The characters in CHARSET should then be decoded."
101   (setq charset (or charset rfc2047-default-charset))
102   (save-excursion
103     (when encoding
104       (cond
105        ((eq encoding 'quoted-printable)
106         (quoted-printable-decode-region (point-min) (point-max)))
107        ((eq encoding 'base64)
108         (condition-case ()
109             (base64-decode-region (point-min) (point-max))
110           (error nil)))
111        ((memq encoding '(7bit 8bit binary))
112         )
113        ((null encoding)
114         )
115        (t
116         (error "Can't decode encoding %s" encoding))))
117     (when (featurep 'mule)
118       (let (mule-charset)
119         (when (and charset
120                    (setq mule-charset (mm-charset-to-coding-system charset))
121                    buffer-file-coding-system
122                    ;;(not (mm-coding-system-equal
123                    ;;    buffer-file-coding-system mule-charset))
124                    )
125           (mm-decode-coding-region (point-min) (point-max) mule-charset))))))
126
127 (provide 'mm-bodies)
128
129 ;; mm-bodies.el ends here