2001-07-13 12: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               start)
87           (cond
88            ;; No encoding.
89            ((null charsets)
90             nil)
91            ;; Too many charsets.
92            ((> (length charsets) 1)
93             charsets)
94            ;; We encode.
95            (t
96             (setq charset (car charsets))
97             (while (not (eobp))
98               (if (eq (mm-charset-after) 'ascii)
99                   (when start
100                     (save-restriction
101                       (narrow-to-region start (point))
102                       (mm-encode-coding-region
103                        start (point) (mm-charset-to-coding-system charset))
104                       (goto-char (point-max)))
105                     (setq start nil))
106                 (unless start
107                   (setq start (point))))
108               (forward-char 1))
109             (when start
110               (mm-encode-coding-region start (point)
111                                        (mm-charset-to-coding-system charset))
112               (setq start nil))
113             charset)))))))
114
115 (defun mm-long-lines-p (length)
116   "Say whether any of the lines in the buffer is longer than LINES."
117   (save-excursion
118     (goto-char (point-min))
119     (end-of-line)
120     (while (and (not (eobp))
121                 (not (> (current-column) length)))
122       (forward-line 1)
123       (end-of-line))
124     (and (> (current-column) length)
125          (current-column))))
126
127 (defvar message-posting-charset)
128
129 (defun mm-body-encoding (charset &optional encoding)
130   "Do Content-Transfer-Encoding and return the encoding of the current buffer."
131   (let ((bits (mm-body-7-or-8))
132         (longp (mm-long-lines-p 1000)))
133     (require 'message)
134     (cond
135      ((and (not mm-use-ultra-safe-encoding)
136            (not longp)
137            (eq bits '7bit))
138       bits)
139      ((and (not mm-use-ultra-safe-encoding)
140            (not longp)
141            (or (eq t (cdr message-posting-charset))
142                (memq charset (cdr message-posting-charset))
143                (eq charset mail-parse-charset)))
144       bits)
145      (t
146       (let ((encoding (or encoding
147                           (cdr (assq charset mm-body-charset-encoding-alist))
148                           (mm-qp-or-base64))))
149         (when mm-use-ultra-safe-encoding
150           (setq encoding (mm-safer-encoding encoding)))
151         (mm-encode-content-transfer-encoding encoding "text/plain")
152         encoding)))))
153
154 (defun mm-body-7-or-8 ()
155   "Say whether the body is 7bit or 8bit."
156   (cond
157    ((not (featurep 'mule))
158     (if (save-excursion
159           (goto-char (point-min))
160           (skip-chars-forward mm-7bit-chars)
161           (eobp))
162         '7bit
163       '8bit))
164    (t
165     ;; Mule version
166     (if (and (null (delq 'ascii
167                          (mm-find-charset-region (point-min) (point-max))))
168              ;;!!!The following is necessary because the function
169              ;;!!!above seems to return the wrong result under
170              ;;!!!Emacs 20.3.  Sometimes.
171              (save-excursion
172                (goto-char (point-min))
173                (skip-chars-forward mm-7bit-chars)
174                (eobp)))
175         '7bit
176       '8bit))))
177
178 ;;;
179 ;;; Functions for decoding
180 ;;;
181
182 (defun mm-decode-content-transfer-encoding (encoding &optional type)
183   (prog1
184       (condition-case error
185           (cond
186            ((eq encoding 'quoted-printable)
187             (quoted-printable-decode-region (point-min) (point-max)))
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             )
207            ((null encoding)
208             ;; Do nothing.
209             )
210            ((memq encoding '(x-uuencode x-uue))
211             (require 'mm-uu)
212             (funcall mm-uu-decode-function (point-min) (point-max)))
213            ((eq encoding 'x-binhex)
214             (require 'mm-uu)
215             (funcall mm-uu-binhex-decode-function (point-min) (point-max)))
216            ((functionp encoding)
217             (funcall encoding (point-min) (point-max)))
218            (t
219             (message "Unknown encoding %s; defaulting to 8bit" encoding)))
220         (error
221          (message "Error while decoding: %s" error)
222          nil))
223     (when (and
224            (memq encoding '(base64 x-uuencode x-uue x-binhex))
225            (equal type "text/plain"))
226       (goto-char (point-min))
227       (while (search-forward "\r\n" nil t)
228         (replace-match "\n" t t)))))
229
230 (defun mm-decode-body (charset &optional encoding type)
231   "Decode the current article that has been encoded with ENCODING.
232 The characters in CHARSET should then be decoded."
233   (if (stringp charset)
234       (setq charset (intern (downcase charset))))
235   (if (or (not charset) 
236           (eq 'gnus-all mail-parse-ignored-charsets)
237           (memq 'gnus-all mail-parse-ignored-charsets)
238           (memq charset mail-parse-ignored-charsets))
239       (setq charset mail-parse-charset))
240   (save-excursion
241     (when encoding
242       (mm-decode-content-transfer-encoding encoding type))
243     (when (featurep 'mule)
244       (let ((coding-system (mm-charset-to-coding-system charset)))
245         (if (and (not coding-system)
246                  (listp mail-parse-ignored-charsets)
247                  (memq 'gnus-unknown mail-parse-ignored-charsets))
248             (setq coding-system 
249                   (mm-charset-to-coding-system mail-parse-charset)))
250         (when (and charset coding-system
251                    ;; buffer-file-coding-system
252                    ;;Article buffer is nil coding system
253                    ;;in XEmacs
254                    (mm-multibyte-p)
255                    (or (not (eq coding-system 'ascii))
256                        (setq coding-system mail-parse-charset))
257                    (not (eq coding-system 'gnus-decoded)))
258           (mm-decode-coding-region (point-min) (point-max) 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