Importing pgnus-0.57
[elisp/gnus.git-] / 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 ;; 8bit treatment gets any char except: 0x32 - 0x7f, CR, LF, TAB, BEL,
37 ;; BS, vertical TAB, form feed, and ^_
38 (defvar mm-8bit-char-regexp "[^\x20-\x7f\r\n\t\x7\x8\xb\xc\x1f]")
39
40 (defun mm-encode-body ()
41   "Encode a body.
42 Should be called narrowed to the body that is to be encoded.
43 If there is more than one non-ASCII MULE charset, then list of found
44 MULE charsets are returned.
45 If successful, the MIME charset is returned.
46 If no encoding was done, nil is returned."
47   (if (not (featurep 'mule))
48       ;; In the non-Mule case, we search for non-ASCII chars and
49       ;; return the value of `mm-default-charset' if any are found.
50       (save-excursion
51         (goto-char (point-min))
52         (if (re-search-forward "[^\x0-\x7f]" nil t)
53             mm-default-charset
54           ;; The logic in `mml-generate-mime-1' confirms that it's OK
55           ;; to return nil here.
56           nil))
57     (save-excursion
58       (goto-char (point-min))
59       (let ((charsets
60              (delq 'ascii (mm-find-charset-region (point-min) (point-max))))
61             charset)
62         (cond
63          ;; No encoding.
64          ((null charsets)
65           nil)
66          ;; Too many charsets.
67          ((> (length charsets) 1)
68           charsets)
69          ;; We encode.
70          (t
71           (let ((mime-charset 
72                  (mm-mime-charset (car charsets) (point-min) (point-max)))
73                 start)
74             (when (or t
75                       ;; We always decode.
76                       (not (mm-coding-system-equal
77                             mime-charset buffer-file-coding-system)))
78               (while (not (eobp))
79                 (if (eq (char-charset (char-after)) 'ascii)
80                     (when start
81                       (save-restriction
82                         (narrow-to-region start (point))
83                         (mm-encode-coding-region start (point) mime-charset)
84                         (goto-char (point-max)))
85                       (setq start nil))
86                   (unless start
87                     (setq start (point))))
88                 (forward-char 1))
89               (when start
90                 (mm-encode-coding-region start (point) mime-charset)
91                 (setq start nil)))
92             mime-charset)))))))
93
94 (defun mm-body-encoding ()
95   "Return the encoding of the current buffer."
96   (cond ((not (featurep 'mule))
97          (if (save-excursion
98                (goto-char (point-min))
99                (re-search-forward mm-8bit-char-regexp nil t))
100              '8bit
101            '7bit))
102         (t
103          ;; Mule version
104          (if (and (null (delq 'ascii (find-charset-region (point-min) (point-max))))
105                   ;;!!!The following is necessary because the function
106                   ;;!!!above seems to return the wrong result under
107                   ;;!!!Emacs 20.3.  Sometimes.
108                   (save-excursion
109                     (goto-char (point-min))
110                     (skip-chars-forward "\0-\177")
111                     (eobp)))
112              '7bit
113            '8bit))))
114
115 ;;;
116 ;;; Functions for decoding
117 ;;;
118
119 (defun mm-decode-content-transfer-encoding (encoding &optional type)
120   (cond
121    ((eq encoding 'quoted-printable)
122     (quoted-printable-decode-region (point-min) (point-max)))
123    ((eq encoding 'base64)
124     (prog1
125         (condition-case ()
126             (base64-decode-region (point-min) (point-max))
127           (error nil))
128       (when (equal type "text/plain")
129         (goto-char (point-min))
130         (while (search-forward "\r\n" nil t)
131           (replace-match "\n" t t)))))
132    ((memq encoding '(7bit 8bit binary))
133     )
134    ((null encoding)
135     )
136    ((eq encoding 'x-uuencode)
137     (condition-case ()
138         (uudecode-decode-region (point-min) (point-max))
139       (error nil)))
140    ((eq encoding 'x-binhex)
141     (condition-case ()
142         (binhex-decode-region (point-min) (point-max))
143       (error nil)))
144    ((functionp encoding)
145     (condition-case ()
146         (funcall encoding (point-min) (point-max))
147       (error nil)))
148    (t
149     (message "Unknown encoding %s; defaulting to 8bit" encoding))))
150
151 (defun mm-decode-body (charset &optional encoding type)
152   "Decode the current article that has been encoded with ENCODING.
153 The characters in CHARSET should then be decoded."
154   (setq charset (or charset rfc2047-default-charset))
155   (save-excursion
156     (when encoding
157       (mm-decode-content-transfer-encoding encoding type))
158     (when (featurep 'mule)
159       (let (mule-charset)
160         (when (and charset
161                    (setq mule-charset (mm-charset-to-coding-system charset))
162                    ;; buffer-file-coding-system 
163                                         ;Article buffer is nil coding system
164                                         ;in XEmacs
165                    enable-multibyte-characters
166                    (or (not (eq mule-charset 'ascii))
167                        (setq mule-charset rfc2047-default-charset)))
168           (mm-decode-coding-region (point-min) (point-max) mule-charset))))))
169
170 (provide 'mm-bodies)
171
172 ;; mm-bodies.el ends here