Import Gnus v5.10.2.
[elisp/gnus.git-] / lisp / mm-bodies.el
1 ;;; mm-bodies.el --- Functions for decoding MIME things
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2003
4 ;;        Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-and-compile
30   (or (fboundp  'base64-decode-region)
31       (require 'base64)))
32
33 (eval-when-compile
34   (defvar mm-uu-decode-function)
35   (defvar mm-uu-binhex-decode-function))
36
37 (require 'mm-util)
38 (require 'rfc2047)
39 (require 'mm-encode)
40
41 ;; 8bit treatment gets any char except: 0x32 - 0x7f, CR, LF, TAB, BEL,
42 ;; BS, vertical TAB, form feed, and ^_
43 (defvar mm-7bit-chars "\x20-\x7f\r\n\t\x7\x8\xb\xc\x1f")
44
45 (defcustom mm-body-charset-encoding-alist
46   '((iso-2022-jp . 7bit)
47     (iso-2022-jp-2 . 7bit)
48     ;; We MUST encode UTF-16 because it can contain \0's which is
49     ;; known to break servers.
50     ;; Note: UTF-16 variants are invalid for text parts [RFC 2781],
51     ;; so this can't happen :-/.
52     (utf-16 . base64)
53     (utf-16be . base64)
54     (utf-16le . base64))
55   "Alist of MIME charsets to encodings.
56 Valid encodings are `7bit', `8bit', `quoted-printable' and `base64'."
57   :type '(repeat (cons (symbol :tag "charset")
58                        (choice :tag "encoding"
59                                (const 7bit)
60                                (const 8bit)
61                                (const quoted-printable)
62                                (const base64))))
63   :group 'mime)
64
65 (defun mm-encode-body (&optional charset)
66   "Encode a body.
67 Should be called narrowed to the body that is to be encoded.
68 If there is more than one non-ASCII MULE charset in the body, then the
69 list of MULE charsets found is returned.
70 If CHARSET is non-nil, it is used as the MIME charset to encode the body.
71 If successful, the MIME charset is returned.
72 If no encoding was done, nil is returned."
73   (if (not (mm-multibyte-p))
74       ;; In the non-Mule case, we search for non-ASCII chars and
75       ;; return the value of `mail-parse-charset' if any are found.
76       (or charset
77           (save-excursion
78             (goto-char (point-min))
79             (if (re-search-forward "[^\x0-\x7f]" nil t)
80                 (or mail-parse-charset
81                     (message-options-get 'mm-encody-body-charset)
82                     (message-options-set
83                      'mm-encody-body-charset
84                      (mm-read-coding-system "Charset used in the article: ")))
85               ;; The logic in `mml-generate-mime-1' confirms that it's OK
86               ;; to return nil here.
87               nil)))
88     (save-excursion
89       (if charset
90           (progn
91             (mm-encode-coding-region (point-min) (point-max) charset)
92             charset)
93         (goto-char (point-min))
94         (let ((charsets (mm-find-mime-charset-region (point-min) (point-max)
95                                                      mm-hack-charsets)))
96           (cond
97            ;; No encoding.
98            ((null charsets)
99             nil)
100            ;; Too many charsets.
101            ((> (length charsets) 1)
102             charsets)
103            ;; We encode.
104            (t
105             (prog1
106                 (setq charset (car charsets))
107               (mm-encode-coding-region (point-min) (point-max)
108                                        (mm-charset-to-coding-system charset))))
109            ))))))
110
111 (defun mm-long-lines-p (length)
112   "Say whether any of the lines in the buffer is longer than LINES."
113   (save-excursion
114     (goto-char (point-min))
115     (end-of-line)
116     (while (and (not (eobp))
117                 (not (> (current-column) length)))
118       (forward-line 1)
119       (end-of-line))
120     (and (> (current-column) length)
121          (current-column))))
122
123 (defvar message-posting-charset)
124
125 (defun mm-body-encoding (charset &optional encoding)
126   "Do Content-Transfer-Encoding and return the encoding of the current buffer."
127   (when (stringp encoding)
128     (setq encoding (intern (downcase encoding))))
129   (let ((bits (mm-body-7-or-8))
130         (longp (mm-long-lines-p 1000)))
131     (require 'message)
132     (cond
133      ((and (not mm-use-ultra-safe-encoding)
134            (not longp)
135            (eq bits '7bit))
136       bits)
137      ((and (not mm-use-ultra-safe-encoding)
138            (not longp)
139            (not (cdr (assq charset mm-body-charset-encoding-alist)))
140            (or (eq t (cdr message-posting-charset))
141                (memq charset (cdr message-posting-charset))
142                (eq charset mail-parse-charset)))
143       bits)
144      (t
145       (let ((encoding (or encoding
146                           (cdr (assq charset mm-body-charset-encoding-alist))
147                           (mm-qp-or-base64))))
148         (when mm-use-ultra-safe-encoding
149           (setq encoding (mm-safer-encoding encoding)))
150         (mm-encode-content-transfer-encoding encoding "text/plain")
151         encoding)))))
152
153 (defun mm-body-7-or-8 ()
154   "Say whether the body is 7bit or 8bit."
155   (if (save-excursion
156         (goto-char (point-min))
157         (skip-chars-forward mm-7bit-chars)
158         (eobp))
159       '7bit
160     '8bit))
161
162 ;;;
163 ;;; Functions for decoding
164 ;;;
165
166 (eval-when-compile (defvar mm-uu-yenc-decode-function))
167
168 (defun mm-decode-content-transfer-encoding (encoding &optional type)
169   "Decodes buffer encoded with ENCODING, returning success status.
170 If TYPE is `text/plain' CRLF->LF translation may occur."
171   (prog1
172       (condition-case error
173           (cond
174            ((eq encoding 'quoted-printable)
175             (quoted-printable-decode-region (point-min) (point-max))
176             t)
177            ((eq encoding 'base64)
178             (base64-decode-region
179              (point-min)
180              ;; Some mailers insert whitespace
181              ;; junk at the end which
182              ;; base64-decode-region dislikes.
183              ;; Also remove possible junk which could
184              ;; have been added by mailing list software.
185              (save-excursion
186                (goto-char (point-min))
187                (while (re-search-forward "^[\t ]*\r?\n" nil t)
188                  (delete-region (match-beginning 0) (match-end 0)))
189                (goto-char (point-max))
190                (when (re-search-backward "^[A-Za-z0-9+/]+=*[\t ]*$" nil t)
191                  (forward-line))
192                (point))))
193            ((memq encoding '(7bit 8bit binary))
194             ;; Do nothing.
195             t)
196            ((null encoding)
197             ;; Do nothing.
198             t)
199            ((memq encoding '(x-uuencode x-uue))
200             (require 'mm-uu)
201             (funcall mm-uu-decode-function (point-min) (point-max))
202             t)
203            ((eq encoding 'x-binhex)
204             (require 'mm-uu)
205             (funcall mm-uu-binhex-decode-function (point-min) (point-max))
206             t)
207            ((eq encoding 'x-yenc)
208             (require 'mm-uu)
209             (funcall mm-uu-yenc-decode-function (point-min) (point-max))
210             )
211            ((functionp encoding)
212             (funcall encoding (point-min) (point-max))
213             t)
214            (t
215             (message "Unknown encoding %s; defaulting to 8bit" encoding)))
216         (error
217          (message "Error while decoding: %s" error)
218          nil))
219     (when (and
220            (memq encoding '(base64 x-uuencode x-uue x-binhex x-yenc))
221            (equal type "text/plain"))
222       (goto-char (point-min))
223       (while (search-forward "\r\n" nil t)
224         (replace-match "\n" t t)))))
225
226 (defun mm-decode-body (charset &optional encoding type)
227   "Decode the current article that has been encoded with ENCODING to CHARSET.
228 ENCODING is a MIME content transfer encoding.
229 CHARSET is the MIME charset with which to decode the data after transfer
230 decoding.  If it is nil, default to `mail-parse-charset'."
231   (when (stringp charset)
232     (setq charset (intern (downcase charset))))
233   (when (or (not charset)
234             (eq 'gnus-all mail-parse-ignored-charsets)
235             (memq 'gnus-all mail-parse-ignored-charsets)
236             (memq charset mail-parse-ignored-charsets))
237     (setq charset mail-parse-charset))
238   (save-excursion
239     (when encoding
240       (mm-decode-content-transfer-encoding encoding type))
241     (when (featurep 'mule)  ; Fixme: Wrong test for unibyte session.
242       (let ((coding-system (mm-charset-to-coding-system charset)))
243         (if (and (not coding-system)
244                  (listp mail-parse-ignored-charsets)
245                  (memq 'gnus-unknown mail-parse-ignored-charsets))
246             (setq coding-system
247                   (mm-charset-to-coding-system mail-parse-charset)))
248         (when (and charset coding-system
249                    ;; buffer-file-coding-system
250                    ;;Article buffer is nil coding system
251                    ;;in XEmacs
252                    (mm-multibyte-p)
253                    (or (not (eq coding-system 'ascii))
254                        (setq coding-system mail-parse-charset))
255                    (not (eq coding-system 'gnus-decoded)))
256           (mm-decode-coding-region (point-min) (point-max)
257                                    coding-system))
258         (setq buffer-file-coding-system
259               (if (boundp 'last-coding-system-used)
260                   (symbol-value 'last-coding-system-used)
261                 coding-system))))))
262
263 (defun mm-decode-string (string charset)
264   "Decode STRING with CHARSET."
265   (when (stringp charset)
266     (setq charset (intern (downcase charset))))
267   (when (or (not charset)
268             (eq 'gnus-all mail-parse-ignored-charsets)
269             (memq 'gnus-all mail-parse-ignored-charsets)
270             (memq charset mail-parse-ignored-charsets))
271     (setq charset mail-parse-charset))
272   (or
273    (when (featurep 'mule)
274      (let ((coding-system (mm-charset-to-coding-system charset)))
275        (if (and (not coding-system)
276                 (listp mail-parse-ignored-charsets)
277                 (memq 'gnus-unknown mail-parse-ignored-charsets))
278            (setq coding-system
279                  (mm-charset-to-coding-system mail-parse-charset)))
280        (when (and charset coding-system
281                   (mm-multibyte-p)
282                   (or (not (eq coding-system 'ascii))
283                       (setq coding-system mail-parse-charset)))
284          (mm-decode-coding-string string coding-system))))
285    string))
286
287 (provide 'mm-bodies)
288
289 ;;; mm-bodies.el ends here