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