*** 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       (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   (if (not (featurep 'mule))
44       'iso-8859-1
45     (save-excursion
46       (goto-char (point-min))
47       (let ((charsets
48              (delq 'ascii (mm-find-charset-region (point-min) (point-max))))
49             charset)
50         (cond
51          ;; No encoding.
52          ((null charsets)
53           nil)
54          ;; Too many charsets.
55          ((> (length charsets) 1)
56           charsets)
57          ;; We encode.
58          (t
59           (let ((mime-charset 
60                  (mm-mime-charset (car charsets) (point-min) (point-max)))
61                 start)
62             (when (or t
63                       ;; We always decode.
64                       (not (mm-coding-system-equal
65                             mime-charset buffer-file-coding-system)))
66               (while (not (eobp))
67                 (if (eq (char-charset (char-after)) 'ascii)
68                     (when start
69                       (save-restriction
70                         (narrow-to-region start (point))
71                         (mm-encode-coding-region start (point) mime-charset)
72                         (goto-char (point-max)))
73                       (setq start nil))
74                   (unless start
75                     (setq start (point))))
76                 (forward-char 1))
77               (when start
78                 (mm-encode-coding-region start (point) mime-charset)
79                 (setq start nil)))
80             mime-charset)))))))
81
82 (defun mm-body-encoding ()
83   "Return the encoding of the current buffer."
84   (if (and
85        (featurep 'mule)
86        (null (delq 'ascii (find-charset-region (point-min) (point-max))))
87        ;;;!!!The following is necessary because the function
88        ;;;!!!above seems to return the wrong result under Emacs 20.3.
89        ;;;!!!Sometimes.
90        (save-excursion
91          (goto-char (point-min))
92          (skip-chars-forward "\0-\177")
93          (eobp)))
94       '7bit
95     '8bit))
96
97 ;;;
98 ;;; Functions for decoding
99 ;;;
100
101 (defun mm-decode-content-transfer-encoding (encoding &optional type)
102   (cond
103    ((eq encoding 'quoted-printable)
104     (quoted-printable-decode-region (point-min) (point-max)))
105    ((eq encoding 'base64)
106     (prog1
107         (condition-case ()
108             (base64-decode-region (point-min) (point-max))
109           (error nil))
110       (when (equal type "text/plain")
111         (goto-char (point-min))
112         (while (search-forward "\r\n" nil t)
113           (replace-match "\n" t t)))))
114    ((memq encoding '(7bit 8bit binary))
115     )
116    ((null encoding)
117     )
118    ((eq encoding 'x-uuencode)
119     (condition-case ()
120         (uudecode-decode-region (point-min) (point-max))
121       (error nil)))
122    ((functionp encoding)
123     (condition-case ()
124         (funcall encoding (point-min) (point-max))
125       (error nil)))
126    (t
127     (message "Unknown encoding %s; defaulting to 8bit" encoding))))
128
129 (defun mm-decode-body (charset &optional encoding type)
130   "Decode the current article that has been encoded with ENCODING.
131 The characters in CHARSET should then be decoded."
132   (setq charset (or charset rfc2047-default-charset))
133   (save-excursion
134     (when encoding
135       (mm-decode-content-transfer-encoding encoding type))
136     (when (featurep 'mule)
137       (let (mule-charset)
138         (when (and charset
139                    (setq mule-charset (mm-charset-to-coding-system charset))
140                    buffer-file-coding-system
141                    enable-multibyte-characters
142                    (or (not (eq mule-charset 'ascii))
143                        (setq mule-charset rfc2047-default-charset)))
144           (mm-decode-coding-region (point-min) (point-max) mule-charset))))))
145
146 (provide 'mm-bodies)
147
148 ;; mm-bodies.el ends here