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