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