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