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