6470ddfcb17ec8c9cd0df05dd6c3a76fa5cc73bb
[gnus] / 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-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) charset)
97             charset)
98         (goto-char (point-min))
99         (let ((charsets (mm-find-mime-charset-region (point-min) (point-max)
100                                                      mm-hack-charsets)))
101           (cond
102            ;; No encoding.
103            ((null charsets)
104             nil)
105            ;; Too many charsets.
106            ((> (length charsets) 1)
107             charsets)
108            ;; We encode.
109            (t
110             (prog1
111                 (setq charset (car charsets))
112               (mm-encode-coding-region (point-min) (point-max)
113                                        (mm-charset-to-coding-system charset))))
114            ))))))
115
116 (defun mm-long-lines-p (length)
117   "Say whether any of the lines in the buffer is longer than LENGTH."
118   (save-excursion
119     (goto-char (point-min))
120     (end-of-line)
121     (while (and (not (eobp))
122                 (not (> (current-column) length)))
123       (forward-line 1)
124       (end-of-line))
125     (and (> (current-column) length)
126          (current-column))))
127
128 (defvar message-posting-charset)
129
130 (defun mm-body-encoding (charset &optional encoding)
131   "Do Content-Transfer-Encoding and return the encoding of the current buffer."
132   (when (stringp encoding)
133     (setq encoding (intern (downcase encoding))))
134   (let ((bits (mm-body-7-or-8))
135         (longp (mm-long-lines-p 1000)))
136     (require 'message)
137     (cond
138      ((and (not longp)
139            (not (and mm-use-ultra-safe-encoding
140                      (save-excursion (re-search-forward "^From " nil t))))
141            (eq bits '7bit))
142       bits)
143      ((and (not mm-use-ultra-safe-encoding)
144            (not longp)
145            (not (cdr (assq charset mm-body-charset-encoding-alist)))
146            (or (eq t (cdr message-posting-charset))
147                (memq charset (cdr message-posting-charset))
148                (eq charset mail-parse-charset)))
149       bits)
150      (t
151       (let ((encoding (or encoding
152                           (cdr (assq charset mm-body-charset-encoding-alist))
153                           (mm-qp-or-base64))))
154         (when mm-use-ultra-safe-encoding
155           (setq encoding (mm-safer-encoding encoding)))
156         (mm-encode-content-transfer-encoding encoding "text/plain")
157         encoding)))))
158
159 (defun mm-body-7-or-8 ()
160   "Say whether the body is 7bit or 8bit."
161   (if (save-excursion
162         (goto-char (point-min))
163         (skip-chars-forward mm-7bit-chars)
164         (eobp))
165       '7bit
166     '8bit))
167
168 ;;;
169 ;;; Functions for decoding
170 ;;;
171
172 (eval-when-compile (defvar mm-uu-yenc-decode-function))
173
174 (defun mm-decode-content-transfer-encoding (encoding &optional type)
175   "Decodes buffer encoded with ENCODING, returning success status.
176 If TYPE is `text/plain' CRLF->LF translation may occur."
177   (prog1
178       (condition-case error
179           (cond
180            ((eq encoding 'quoted-printable)
181             (quoted-printable-decode-region (point-min) (point-max))
182             t)
183            ((eq encoding 'base64)
184             (base64-decode-region
185              (point-min)
186              ;; Some mailers insert whitespace
187              ;; junk at the end which
188              ;; base64-decode-region dislikes.
189              ;; Also remove possible junk which could
190              ;; have been added by mailing list software.
191              (save-excursion
192                (goto-char (point-min))
193                (while (re-search-forward "^[\t ]*\r?\n" nil t)
194                  (delete-region (match-beginning 0) (match-end 0)))
195                (goto-char (point-max))
196                (when (re-search-backward "^[A-Za-z0-9+/]+=*[\t ]*$" nil t)
197                  (forward-line))
198                (point))))
199            ((memq encoding '(7bit 8bit binary))
200             ;; Do nothing.
201             t)
202            ((null encoding)
203             ;; Do nothing.
204             t)
205            ((memq encoding '(x-uuencode x-uue))
206             (require 'mm-uu)
207             (funcall mm-uu-decode-function (point-min) (point-max))
208             t)
209            ((eq encoding 'x-binhex)
210             (require 'mm-uu)
211             (funcall mm-uu-binhex-decode-function (point-min) (point-max))
212             t)
213            ((eq encoding 'x-yenc)
214             (require 'mm-uu)
215             (funcall mm-uu-yenc-decode-function (point-min) (point-max))
216             )
217            ((functionp encoding)
218             (funcall encoding (point-min) (point-max))
219             t)
220            (t
221             (message "Unknown encoding %s; defaulting to 8bit" encoding)))
222         (error
223          (message "Error while decoding: %s" error)
224          nil))
225     (when (and
226            (memq encoding '(base64 x-uuencode x-uue x-binhex x-yenc))
227            (equal type "text/plain"))
228       (goto-char (point-min))
229       (while (search-forward "\r\n" nil t)
230         (replace-match "\n" t t)))))
231
232 (defun mm-decode-body (charset &optional encoding type)
233   "Decode the current article that has been encoded with ENCODING to CHARSET.
234 ENCODING is a MIME content transfer encoding.
235 CHARSET is the MIME charset with which to decode the data after transfer
236 decoding.  If it is nil, default to `mail-parse-charset'."
237   (when (stringp charset)
238     (setq charset (intern (downcase charset))))
239   (when (or (not charset)
240             (eq 'gnus-all mail-parse-ignored-charsets)
241             (memq 'gnus-all mail-parse-ignored-charsets)
242             (memq charset mail-parse-ignored-charsets))
243     (setq charset mail-parse-charset))
244   (save-excursion
245     (when encoding
246       (mm-decode-content-transfer-encoding encoding type))
247     (when (featurep 'mule)  ; Fixme: Wrong test for unibyte session.
248       (let ((coding-system (mm-charset-to-coding-system charset)))
249         (if (and (not coding-system)
250                  (listp mail-parse-ignored-charsets)
251                  (memq 'gnus-unknown mail-parse-ignored-charsets))
252             (setq coding-system
253                   (mm-charset-to-coding-system mail-parse-charset)))
254         (when (and charset coding-system
255                    ;; buffer-file-coding-system
256                    ;;Article buffer is nil coding system
257                    ;;in XEmacs
258                    (mm-multibyte-p)
259                    (or (not (eq coding-system 'ascii))
260                        (setq coding-system mail-parse-charset))
261                    (not (eq coding-system 'gnus-decoded)))
262           (mm-decode-coding-region (point-min) (point-max)
263                                    coding-system))
264         (setq buffer-file-coding-system
265               (if (boundp 'last-coding-system-used)
266                   (symbol-value 'last-coding-system-used)
267                 coding-system))))))
268
269 (defun mm-decode-string (string charset)
270   "Decode STRING with CHARSET."
271   (when (stringp charset)
272     (setq charset (intern (downcase charset))))
273   (when (or (not charset)
274             (eq 'gnus-all mail-parse-ignored-charsets)
275             (memq 'gnus-all mail-parse-ignored-charsets)
276             (memq charset mail-parse-ignored-charsets))
277     (setq charset mail-parse-charset))
278   (or
279    (when (featurep 'mule)
280      (let ((coding-system (mm-charset-to-coding-system charset)))
281        (if (and (not coding-system)
282                 (listp mail-parse-ignored-charsets)
283                 (memq 'gnus-unknown mail-parse-ignored-charsets))
284            (setq coding-system
285                  (mm-charset-to-coding-system mail-parse-charset)))
286        (when (and charset coding-system
287                   (mm-multibyte-p)
288                   (or (not (eq coding-system 'ascii))
289                       (setq coding-system mail-parse-charset)))
290          (mm-decode-coding-string string coding-system))))
291    string))
292
293 (provide 'mm-bodies)
294
295 ;;; mm-bodies.el ends here