2001-11-01 09:00:00 ShengHuo ZHU <zsh@cs.rochester.edu>
[gnus] / lisp / mm-bodies.el
1 ;;; mm-bodies.el --- Functions for decoding MIME things
2 ;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-and-compile
28   (or (fboundp  'base64-decode-region)
29       (require 'base64)))
30
31 (eval-when-compile
32   (defvar mm-uu-decode-function)
33   (defvar mm-uu-binhex-decode-function))
34
35 (require 'mm-util)
36 (require 'rfc2047)
37 (require 'mm-encode)
38
39 ;; 8bit treatment gets any char except: 0x32 - 0x7f, CR, LF, TAB, BEL,
40 ;; BS, vertical TAB, form feed, and ^_
41 (defvar mm-7bit-chars "\x20-\x7f\r\n\t\x7\x8\xb\xc\x1f")
42
43 (defcustom mm-body-charset-encoding-alist
44   '((iso-2022-jp . 7bit)
45     (iso-2022-jp-2 . 7bit))
46   "Alist of MIME charsets to encodings.
47 Valid encodings are `7bit', `8bit', `quoted-printable' and `base64'."
48   :type '(repeat (cons (symbol :tag "charset")
49                        (choice :tag "encoding"
50                                (const 7bit)
51                                (const 8bit)
52                                (const quoted-printable)
53                                (const base64))))
54   :group 'mime)
55
56 (defun mm-encode-body (&optional charset)
57   "Encode a body.
58 Should be called narrowed to the body that is to be encoded.
59 If there is more than one non-ASCII MULE charset, then list of found
60 MULE charsets are returned.
61 If CHARSET is non-nil, it is used.
62 If successful, the MIME charset is returned.
63 If no encoding was done, nil is returned."
64   (if (not (mm-multibyte-p))
65       ;; In the non-Mule case, we search for non-ASCII chars and
66       ;; return the value of `mail-parse-charset' if any are found.
67       (or charset
68           (save-excursion
69             (goto-char (point-min))
70             (if (re-search-forward "[^\x0-\x7f]" nil t)
71                 (or mail-parse-charset
72                     (message-options-get 'mm-encody-body-charset)
73                     (message-options-set
74                      'mm-encody-body-charset
75                      (mm-read-charset "Charset used in the article: ")))
76               ;; The logic in `mml-generate-mime-1' confirms that it's OK
77               ;; to return nil here.
78               nil)))
79     (save-excursion
80       (if charset
81           (progn
82             (mm-encode-coding-region (point-min) (point-max) charset)
83             charset)
84         (goto-char (point-min))
85         (let ((charsets (mm-find-mime-charset-region (point-min) (point-max)
86                                                      mm-hack-charsets))
87               start)
88           (cond
89            ;; No encoding.
90            ((null charsets)
91             nil)
92            ;; Too many charsets.
93            ((> (length charsets) 1)
94             charsets)
95            ;; We encode.
96            (t
97             (setq charset (car charsets))
98             (while (not (eobp))
99               (if (eq (mm-charset-after) 'ascii)
100                   (when start
101                     (save-restriction
102                       (narrow-to-region start (point))
103                       (mm-encode-coding-region
104                        start (point) (mm-charset-to-coding-system charset))
105                       (goto-char (point-max)))
106                     (setq start nil))
107                 (unless start
108                   (setq start (point))))
109               (forward-char 1))
110             (when start
111               (mm-encode-coding-region start (point)
112                                        (mm-charset-to-coding-system charset))
113               (setq start nil))
114             charset)))))))
115
116 (defun mm-long-lines-p (length)
117   "Say whether any of the lines in the buffer is longer than LINES."
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   (let ((bits (mm-body-7-or-8))
133         (longp (mm-long-lines-p 1000)))
134     (require 'message)
135     (cond
136      ((and (not mm-use-ultra-safe-encoding)
137            (not longp)
138            (eq bits '7bit))
139       bits)
140      ((and (not mm-use-ultra-safe-encoding)
141            (not longp)
142            (or (eq t (cdr message-posting-charset))
143                (memq charset (cdr message-posting-charset))
144                (eq charset mail-parse-charset)))
145       bits)
146      (t
147       (let ((encoding (or encoding
148                           (cdr (assq charset mm-body-charset-encoding-alist))
149                           (mm-qp-or-base64))))
150         (when mm-use-ultra-safe-encoding
151           (setq encoding (mm-safer-encoding encoding)))
152         (mm-encode-content-transfer-encoding encoding "text/plain")
153         encoding)))))
154
155 (defun mm-body-7-or-8 ()
156   "Say whether the body is 7bit or 8bit."
157   (cond
158    ((not (featurep 'mule))
159     (if (save-excursion
160           (goto-char (point-min))
161           (skip-chars-forward mm-7bit-chars)
162           (eobp))
163         '7bit
164       '8bit))
165    (t
166     ;; Mule version
167     (if (and (null (delq 'ascii
168                          (mm-find-charset-region (point-min) (point-max))))
169              ;;!!!The following is necessary because the function
170              ;;!!!above seems to return the wrong result under
171              ;;!!!Emacs 20.3.  Sometimes.
172              (save-excursion
173                (goto-char (point-min))
174                (skip-chars-forward mm-7bit-chars)
175                (eobp)))
176         '7bit
177       '8bit))))
178
179 ;;;
180 ;;; Functions for decoding
181 ;;;
182
183 (defun mm-decode-content-transfer-encoding (encoding &optional type)
184   "Decodes buffer encoded with ENCODING, returning success status.
185 If TYPE is `text/plain' CRLF->LF translation may occur."
186   (prog1
187       (condition-case error
188           (cond
189            ((eq encoding 'quoted-printable)
190             (quoted-printable-decode-region (point-min) (point-max))
191             t)
192            ((eq encoding 'base64)
193             (base64-decode-region
194              (point-min)
195              ;; Some mailers insert whitespace
196              ;; junk at the end which
197              ;; base64-decode-region dislikes.
198              ;; Also remove possible junk which could
199              ;; have been added by mailing list software.
200              (save-excursion
201                (goto-char (point-min))
202                (while (re-search-forward "^[\t ]*\r?\n" nil t)
203                  (delete-region (match-beginning 0) (match-end 0)))
204                (goto-char (point-max))
205                (when (re-search-backward "^[A-Za-z0-9+/]+=*[\t ]*$" nil t)
206                  (forward-line))
207                (point))))
208            ((memq encoding '(7bit 8bit binary))
209             ;; Do nothing.
210             t)
211            ((null encoding)
212             ;; Do nothing.
213             t)
214            ((memq encoding '(x-uuencode x-uue))
215             (require 'mm-uu)
216             (funcall mm-uu-decode-function (point-min) (point-max))
217             t)
218            ((eq encoding 'x-binhex)
219             (require 'mm-uu)
220             (funcall mm-uu-binhex-decode-function (point-min) (point-max))
221             t)
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))
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)
238   "Decode the current article that has been encoded with ENCODING.
239 The characters in CHARSET should then be decoded."
240   (if (stringp charset)
241       (setq charset (intern (downcase charset))))
242   (if (or (not charset)
243           (eq 'gnus-all mail-parse-ignored-charsets)
244           (memq 'gnus-all mail-parse-ignored-charsets)
245           (memq charset mail-parse-ignored-charsets))
246       (setq charset mail-parse-charset))
247   (save-excursion
248     (when encoding
249       (mm-decode-content-transfer-encoding encoding type))
250     (when (featurep 'mule)
251       (let ((coding-system (mm-charset-to-coding-system charset)))
252         (if (and (not coding-system)
253                  (listp mail-parse-ignored-charsets)
254                  (memq 'gnus-unknown mail-parse-ignored-charsets))
255             (setq coding-system
256                   (mm-charset-to-coding-system mail-parse-charset)))
257         (when (and charset coding-system
258                    ;; buffer-file-coding-system
259                    ;;Article buffer is nil coding system
260                    ;;in XEmacs
261                    (mm-multibyte-p)
262                    (or (not (eq coding-system 'ascii))
263                        (setq coding-system mail-parse-charset))
264                    (not (eq coding-system 'gnus-decoded)))
265           (mm-decode-coding-region (point-min) (point-max) coding-system))))))
266
267 (defun mm-decode-string (string charset)
268   "Decode STRING with CHARSET."
269   (when (stringp charset)
270     (setq charset (intern (downcase charset))))
271   (when (or (not charset)
272             (eq 'gnus-all mail-parse-ignored-charsets)
273             (memq 'gnus-all mail-parse-ignored-charsets)
274             (memq charset mail-parse-ignored-charsets))
275     (setq charset mail-parse-charset))
276   (or
277    (when (featurep 'mule)
278      (let ((coding-system (mm-charset-to-coding-system charset)))
279        (if (and (not coding-system)
280                 (listp mail-parse-ignored-charsets)
281                 (memq 'gnus-unknown mail-parse-ignored-charsets))
282            (setq coding-system
283                  (mm-charset-to-coding-system mail-parse-charset)))
284        (when (and charset coding-system
285                   (mm-multibyte-p)
286                   (or (not (eq coding-system 'ascii))
287                       (setq coding-system mail-parse-charset)))
288          (mm-decode-coding-string string coding-system))))
289    string))
290
291 (provide 'mm-bodies)
292
293 ;;; mm-bodies.el ends here