2ee6bf2eb3b81e9a613706dcde46c3e39597eef5
[elisp/gnus.git-] / lisp / mm-bodies.el
1 ;;; mm-bodies.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998,99 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   (autoload 'binhex-decode-region "binhex"))
31
32 (require 'mm-util)
33 (require 'rfc2047)
34 (require 'qp)
35 (require 'uudecode)
36
37 ;; 8bit treatment gets any char except: 0x32 - 0x7f, CR, LF, TAB, BEL,
38 ;; BS, vertical TAB, form feed, and ^_
39 (defvar mm-7bit-chars "\x20-\x7f\r\n\t\x7\x8\xb\xc\x1f")
40
41 (defvar mm-body-charset-encoding-alist nil
42   "Alist of MIME charsets to encodings.
43 Valid encodings are `7bit', `8bit', `quoted-printable' and `base64'.")
44
45 (defun mm-encode-body ()
46   "Encode a body.
47 Should be called narrowed to the body that is to be encoded.
48 If there is more than one non-ASCII MULE charset, then list of found
49 MULE charsets are returned.
50 If successful, the MIME charset is returned.
51 If no encoding was done, nil is returned."
52   (if (not (featurep 'mule))
53       ;; In the non-Mule case, we search for non-ASCII chars and
54       ;; return the value of `mm-default-charset' if any are found.
55       (save-excursion
56         (goto-char (point-min))
57         (if (re-search-forward "[^\x0-\x7f]" nil t)
58             (or mail-parse-charset
59                 (mm-read-charset "Charset used in the article: "))
60           ;; The logic in `mml-generate-mime-1' confirms that it's OK
61           ;; to return nil here.
62           nil))
63     (save-excursion
64       (goto-char (point-min))
65       (let ((charsets (mm-find-mime-charset-region (point-min) (point-max)))
66             charset)
67         (cond
68          ;; No encoding.
69          ((null charsets)
70           nil)
71          ;; Too many charsets.
72          ((> (length charsets) 1)
73           charsets)
74          ;; We encode.
75          (t
76           (let ((charset (car charsets))
77                 start)
78             (when (or t
79                       ;; We always decode.
80                       (not (mm-coding-system-equal
81                             charset buffer-file-coding-system)))
82               (while (not (eobp))
83                 (if (eq (mm-charset-after) 'ascii)
84                     (when start
85                       (save-restriction
86                         (narrow-to-region start (point))
87                         (mm-encode-coding-region start (point) charset)
88                         (goto-char (point-max)))
89                       (setq start nil))
90                   (unless start
91                     (setq start (point))))
92                 (forward-char 1))
93               (when start
94                 (mm-encode-coding-region start (point) charset)
95                 (setq start nil)))
96             charset)))))))
97
98 (defun mm-body-encoding (charset &optional encoding)
99   "Do Content-Transfer-Encoding and return the encoding of the current buffer."
100   (let ((bits (mm-body-7-or-8)))
101     (cond
102      ((eq bits '7bit)
103       bits)
104      ((and (not mm-use-ultra-safe-encoding)
105            (or (eq t (cdr message-posting-charset))
106                (memq charset (cdr message-posting-charset))
107                (eq charset mail-parse-charset)))
108       bits)
109      (t
110       (let ((encoding (or encoding
111                           (cdr (assq charset mm-body-charset-encoding-alist))
112                           (mm-qp-or-base64))))
113         (when mm-use-ultra-safe-encoding
114           (setq encoding (mm-safer-encoding encoding)))
115         (mm-encode-content-transfer-encoding encoding "text/plain")
116         encoding)))))
117
118 (defun mm-body-7-or-8 ()
119   "Say whether the body is 7bit or 8bit."
120   (cond
121    ((not (featurep 'mule))
122     (if (save-excursion
123           (goto-char (point-min))
124           (skip-chars-forward mm-7bit-chars)
125           (eobp))
126         '7bit
127       '8bit))
128    (t
129     ;; Mule version
130     (if (and (null (delq 'ascii
131                          (mm-find-charset-region (point-min) (point-max))))
132              ;;!!!The following is necessary because the function
133              ;;!!!above seems to return the wrong result under
134              ;;!!!Emacs 20.3.  Sometimes.
135              (save-excursion
136                (goto-char (point-min))
137                (skip-chars-forward mm-7bit-chars)
138                (eobp)))
139         '7bit
140       '8bit))))
141
142 ;;;
143 ;;; Functions for decoding
144 ;;;
145
146 (defun mm-decode-content-transfer-encoding (encoding &optional type)
147   (prog1
148       (condition-case error
149           (cond
150            ((eq encoding 'quoted-printable)
151             (quoted-printable-decode-region (point-min) (point-max)))
152            ((eq encoding 'base64)
153             (base64-decode-region
154              (point-min)
155              ;; Some mailers insert whitespace
156              ;; junk at the end which
157              ;; base64-decode-region dislikes.
158              (save-excursion
159                (goto-char (point-max))
160                (skip-chars-backward "\n\t ")
161                (delete-region (point) (point-max))
162                (point))))
163            ((memq encoding '(7bit 8bit binary))
164             ;; Do nothing.
165             )
166            ((null encoding)
167             ;; Do nothing.
168             )
169            ((memq encoding '(x-uuencode x-uue))
170             (funcall mm-uu-decode-function (point-min) (point-max)))
171            ((eq encoding 'x-binhex)
172             (funcall mm-uu-binhex-decode-function (point-min) (point-max)))
173            ((functionp encoding)
174             (funcall encoding (point-min) (point-max)))
175            (t
176             (message "Unknown encoding %s; defaulting to 8bit" encoding)))
177         (error
178          (message "Error while decoding: %s" error)
179          nil))
180     (when (and
181            (memq encoding '(base64 x-uuencode x-uue x-binhex))
182            (equal type "text/plain"))
183       (goto-char (point-min))
184       (while (search-forward "\r\n" nil t)
185         (replace-match "\n" t t)))))
186
187 (defun mm-decode-body (charset &optional encoding type)
188   "Decode the current article that has been encoded with ENCODING.
189 The characters in CHARSET should then be decoded."
190   (if (stringp charset)
191       (setq charset (intern (downcase charset))))
192   (if (or (not charset) 
193           (eq 'gnus-all mail-parse-ignored-charsets)
194           (memq 'gnus-all mail-parse-ignored-charsets)
195           (memq charset mail-parse-ignored-charsets))
196       (setq charset mail-parse-charset))
197   (save-excursion
198     (when encoding
199       (mm-decode-content-transfer-encoding encoding type))
200     (when (featurep 'mule)
201       (let ((mule-charset (mm-charset-to-coding-system charset)))
202         (if (and (not mule-charset)
203                  (listp mail-parse-ignored-charsets)
204                  (memq 'gnus-unknown mail-parse-ignored-charsets))
205             (setq mule-charset 
206                   (mm-charset-to-coding-system mail-parse-charset)))
207         (when (and charset mule-charset
208                    ;; buffer-file-coding-system
209                    ;;Article buffer is nil coding system
210                    ;;in XEmacs
211                    (mm-multibyte-p)
212                    (or (not (eq mule-charset 'ascii))
213                        (setq mule-charset mail-parse-charset))
214                    (not (eq mule-charset 'gnus-decoded)))
215           (mm-decode-coding-region (point-min) (point-max) mule-charset))))))
216
217 (defun mm-decode-string (string charset)
218   "Decode STRING with CHARSET."
219   (if (stringp charset)
220       (setq charset (intern (downcase charset))))
221   (if (or (not charset) 
222           (eq 'gnus-all mail-parse-ignored-charsets)
223           (memq 'gnus-all mail-parse-ignored-charsets)
224           (memq charset mail-parse-ignored-charsets))
225       (setq charset mail-parse-charset))
226   (or
227    (when (featurep 'mule)
228      (let ((mule-charset (mm-charset-to-coding-system charset)))
229        (if (and (not mule-charset)
230                 (listp mail-parse-ignored-charsets)
231                 (memq 'gnus-unknown mail-parse-ignored-charsets))
232            (setq mule-charset 
233                  (mm-charset-to-coding-system mail-parse-charset)))
234        (when (and charset mule-charset
235                   (mm-multibyte-p)
236                   (or (not (eq mule-charset 'ascii))
237                       (setq mule-charset mail-parse-charset)))
238          (mm-decode-coding-string string mule-charset))))
239    string))
240
241 (provide 'mm-bodies)
242
243 ;; mm-bodies.el ends here