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