Show an article with a specified charset.
[gnus] / 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-8bit-char-regexp "[^\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 (char-charset (char-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)
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      ((eq charset mail-parse-charset)
105       bits)
106      (t
107       (let ((encoding (or (cdr (assq charset mm-body-charset-encoding-alist))
108                           (mm-qp-or-base64))))
109         (mm-encode-content-transfer-encoding encoding "text/plain")
110         encoding)))))
111
112 (defun mm-body-7-or-8 ()
113   "Say whether the body is 7bit or 8bit."
114   (cond
115    ((not (featurep 'mule))
116     (if (save-excursion
117           (goto-char (point-min))
118           (re-search-forward mm-8bit-char-regexp nil t))
119         '8bit
120       '7bit))
121    (t
122     ;; Mule version
123     (if (and (null (delq 'ascii
124                          (mm-find-charset-region (point-min) (point-max))))
125              ;;!!!The following is necessary because the function
126              ;;!!!above seems to return the wrong result under
127              ;;!!!Emacs 20.3.  Sometimes.
128              (save-excursion
129                (goto-char (point-min))
130                (skip-chars-forward "\0-\177")
131                (eobp)))
132         '7bit
133       '8bit))))
134
135 ;;;
136 ;;; Functions for decoding
137 ;;;
138
139 (defun mm-decode-content-transfer-encoding (encoding &optional type)
140   (prog1
141       (condition-case error
142           (cond
143            ((eq encoding 'quoted-printable)
144             (quoted-printable-decode-region (point-min) (point-max)))
145            ((eq encoding 'base64)
146             (base64-decode-region (point-min)
147                                   ;; Some mailers insert whitespace
148                                   ;; junk at the end which
149                                   ;; base64-decode-region dislikes.
150                                   (save-excursion
151                                     (goto-char (point-max))
152                                     (skip-chars-backward "\n\t ")
153                                     (delete-region (point) (point-max))
154                                     (point))))
155            ((memq encoding '(7bit 8bit binary))
156             )
157            ((null encoding)
158             )
159            ((memq encoding '(x-uuencode x-uue))
160             (funcall mm-uu-decode-function (point-min) (point-max)))
161            ((eq encoding 'x-binhex)
162             (funcall mm-uu-binhex-decode-function (point-min) (point-max)))
163            ((functionp encoding)
164             (funcall encoding (point-min) (point-max)))
165            (t
166             (message "Unknown encoding %s; defaulting to 8bit" encoding)))
167         (error
168          (message "Error while decoding: %s" error)
169          nil))
170     (when (and
171            (memq encoding '(base64 x-uuencode x-uue x-binhex))
172            (equal type "text/plain"))
173       (goto-char (point-min))
174       (while (search-forward "\r\n" nil t)
175         (replace-match "\n" t t)))))
176
177 (defun mm-decode-body (charset &optional encoding type)
178   "Decode the current article that has been encoded with ENCODING.
179 The characters in CHARSET should then be decoded."
180   (if (stringp charset)
181     (setq charset (intern (downcase charset))))
182   (if (or (not charset) 
183           (eq 'gnus-all mail-parse-ignored-charsets)
184           (memq 'gnus-all mail-parse-ignored-charsets)
185           (memq charset mail-parse-ignored-charsets))
186       (setq charset mail-parse-charset))
187   (save-excursion
188     (when encoding
189       (mm-decode-content-transfer-encoding encoding type))
190     (when (featurep 'mule)
191       (let ((mule-charset (mm-charset-to-coding-system charset)))
192         (if (and (not mule-charset)
193                  (listp mail-parse-ignored-charsets)
194                  (memq 'gnus-unknown mail-parse-ignored-charsets))
195             (setq mule-charset 
196                   (mm-charset-to-coding-system mail-parse-charset)))
197         (when (and charset mule-charset
198                    ;; buffer-file-coding-system
199                    ;;Article buffer is nil coding system
200                    ;;in XEmacs
201                    (mm-multibyte-p)
202                    (or (not (eq mule-charset 'ascii))
203                        (setq mule-charset mail-parse-charset))
204                    (not (eq mule-charset 'gnus-decoded)))
205           (mm-decode-coding-region (point-min) (point-max) mule-charset))))))
206
207 (defun mm-decode-string (string charset)
208   "Decode STRING with CHARSET."
209   (if (stringp charset)
210     (setq charset (intern (downcase charset))))
211   (if (or (not charset) 
212           (eq 'gnus-all mail-parse-ignored-charsets)
213           (memq 'gnus-all mail-parse-ignored-charsets)
214           (memq charset mail-parse-ignored-charsets))
215       (setq charset mail-parse-charset))
216   (or
217    (when (featurep 'mule)
218       (let ((mule-charset (mm-charset-to-coding-system charset)))
219         (if (and (not mule-charset)
220                  (listp mail-parse-ignored-charsets)
221                  (memq 'gnus-unknown mail-parse-ignored-charsets))
222             (setq mule-charset 
223                   (mm-charset-to-coding-system mail-parse-charset)))
224        (when (and charset mule-charset
225                   (mm-multibyte-p)
226                   (or (not (eq mule-charset 'ascii))
227                       (setq mule-charset mail-parse-charset)))
228          (mm-decode-coding-string string mule-charset))))
229    string))
230
231 (provide 'mm-bodies)
232
233 ;; mm-bodies.el ends here