]> code.delx.au - gnu-emacs/blob - lisp/gnus/mm-bodies.el
Merge from emacs--devo--0
[gnu-emacs] / lisp / gnus / mm-bodies.el
1 ;;; mm-bodies.el --- Functions for decoding MIME things
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, 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, LF, TAB, BEL,
42 ;; BS, vertical TAB, form feed, and ^_
43 ;;
44 ;; Note that CR is *not* included, as that would allow a non-paired CR
45 ;; in the body contrary to RFC 2822:
46 ;;
47 ;; - CR and LF MUST only occur together as CRLF; they MUST NOT
48 ;; appear independently in the body.
49
50 (defvar mm-7bit-chars "\x20-\x7f\n\t\x7\x8\xb\xc\x1f")
51
52 (defcustom mm-body-charset-encoding-alist
53 '((iso-2022-jp . 7bit)
54 (iso-2022-jp-2 . 7bit)
55 ;; We MUST encode UTF-16 because it can contain \0's which is
56 ;; known to break servers.
57 ;; Note: UTF-16 variants are invalid for text parts [RFC 2781],
58 ;; so this can't happen :-/.
59 ;; PPS: Yes, it can happen if the user specifies UTF-16 in the MML
60 ;; markup. - jh.
61 (utf-16 . base64)
62 (utf-16be . base64)
63 (utf-16le . base64))
64 "Alist of MIME charsets to encodings.
65 Valid encodings are `7bit', `8bit', `quoted-printable' and `base64'."
66 :type '(repeat (cons (symbol :tag "charset")
67 (choice :tag "encoding"
68 (const 7bit)
69 (const 8bit)
70 (const quoted-printable)
71 (const base64))))
72 :group 'mime)
73
74 (defun mm-encode-body (&optional charset)
75 "Encode a body.
76 Should be called narrowed to the body that is to be encoded.
77 If there is more than one non-ASCII MULE charset in the body, then the
78 list of MULE charsets found is returned.
79 If CHARSET is non-nil, it is used as the MIME charset to encode the body.
80 If successful, the MIME charset is returned.
81 If no encoding was done, nil is returned."
82 (if (not (mm-multibyte-p))
83 ;; In the non-Mule case, we search for non-ASCII chars and
84 ;; return the value of `mail-parse-charset' if any are found.
85 (or charset
86 (save-excursion
87 (goto-char (point-min))
88 (if (re-search-forward "[^\x0-\x7f]" nil t)
89 (or mail-parse-charset
90 (message-options-get 'mm-encody-body-charset)
91 (message-options-set
92 'mm-encody-body-charset
93 (mm-read-coding-system "Charset used in the article: ")))
94 ;; The logic in `mml-generate-mime-1' confirms that it's OK
95 ;; to return nil here.
96 nil)))
97 (save-excursion
98 (if charset
99 (progn
100 (mm-encode-coding-region (point-min) (point-max)
101 (mm-charset-to-coding-system charset))
102 charset)
103 (goto-char (point-min))
104 (let ((charsets (mm-find-mime-charset-region (point-min) (point-max))))
105 (cond
106 ;; No encoding.
107 ((null charsets)
108 nil)
109 ;; Too many charsets.
110 ((> (length charsets) 1)
111 charsets)
112 ;; We encode.
113 (t
114 (prog1
115 (setq charset (car charsets))
116 (mm-encode-coding-region (point-min) (point-max)
117 (mm-charset-to-coding-system charset))))
118 ))))))
119
120 (defun mm-long-lines-p (length)
121 "Say whether any of the lines in the buffer is longer than LENGTH."
122 (save-excursion
123 (goto-char (point-min))
124 (end-of-line)
125 (while (and (not (eobp))
126 (not (> (current-column) length)))
127 (forward-line 1)
128 (end-of-line))
129 (and (> (current-column) length)
130 (current-column))))
131
132 (defvar message-posting-charset)
133
134 (defun mm-body-encoding (charset &optional encoding)
135 "Do Content-Transfer-Encoding and return the encoding of the current buffer."
136 (when (stringp encoding)
137 (setq encoding (intern (downcase encoding))))
138 (let ((bits (mm-body-7-or-8))
139 (longp (mm-long-lines-p 1000)))
140 (require 'message)
141 (cond
142 ((and (not longp)
143 (not (and mm-use-ultra-safe-encoding
144 (or (save-excursion (re-search-forward " $" nil t))
145 (save-excursion (re-search-forward "^From " nil t)))))
146 (eq bits '7bit))
147 bits)
148 ((and (not mm-use-ultra-safe-encoding)
149 (not longp)
150 (not (cdr (assq charset mm-body-charset-encoding-alist)))
151 (or (eq t (cdr message-posting-charset))
152 (memq charset (cdr message-posting-charset))
153 (eq charset mail-parse-charset)))
154 bits)
155 (t
156 (let ((encoding (or encoding
157 (cdr (assq charset mm-body-charset-encoding-alist))
158 (mm-qp-or-base64))))
159 (when mm-use-ultra-safe-encoding
160 (setq encoding (mm-safer-encoding encoding)))
161 (mm-encode-content-transfer-encoding encoding "text/plain")
162 encoding)))))
163
164 (defun mm-body-7-or-8 ()
165 "Say whether the body is 7bit or 8bit."
166 (if (save-excursion
167 (goto-char (point-min))
168 (skip-chars-forward mm-7bit-chars)
169 (eobp))
170 '7bit
171 '8bit))
172
173 ;;;
174 ;;; Functions for decoding
175 ;;;
176
177 (eval-when-compile (defvar mm-uu-yenc-decode-function))
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 type
232 (memq encoding '(base64 x-uuencode x-uue x-binhex x-yenc))
233 (string-match "\\`text/" type))
234 (goto-char (point-min))
235 (while (search-forward "\r\n" nil t)
236 (replace-match "\n" t t)))))
237
238 (defun mm-decode-body (charset &optional encoding type)
239 "Decode the current article that has been encoded with ENCODING to CHARSET.
240 ENCODING is a MIME content transfer encoding.
241 CHARSET is the MIME charset with which to decode the data after transfer
242 decoding. If it is nil, default to `mail-parse-charset'."
243 (when (stringp charset)
244 (setq charset (intern (downcase charset))))
245 (when (or (not charset)
246 (eq 'gnus-all mail-parse-ignored-charsets)
247 (memq 'gnus-all mail-parse-ignored-charsets)
248 (memq charset mail-parse-ignored-charsets))
249 (setq charset mail-parse-charset))
250 (save-excursion
251 (when encoding
252 (mm-decode-content-transfer-encoding encoding type))
253 (when (and (featurep 'mule) ;; Fixme: Wrong test for unibyte session.
254 (not (eq charset 'gnus-decoded)))
255 (let ((coding-system (mm-charset-to-coding-system
256 ;; Allow overwrite using
257 ;; `mm-charset-override-alist'.
258 charset nil t)))
259 (if (and (not coding-system)
260 (listp mail-parse-ignored-charsets)
261 (memq 'gnus-unknown mail-parse-ignored-charsets))
262 (setq coding-system
263 (mm-charset-to-coding-system mail-parse-charset)))
264 (when (and charset coding-system
265 ;; buffer-file-coding-system
266 ;;Article buffer is nil coding system
267 ;;in XEmacs
268 (mm-multibyte-p)
269 (or (not (eq coding-system 'ascii))
270 (setq coding-system mail-parse-charset)))
271 (mm-decode-coding-region (point-min) (point-max)
272 coding-system))
273 (setq buffer-file-coding-system
274 (if (boundp 'last-coding-system-used)
275 (symbol-value 'last-coding-system-used)
276 coding-system))))))
277
278 (defun mm-decode-string (string charset)
279 "Decode STRING with CHARSET."
280 (when (stringp charset)
281 (setq charset (intern (downcase charset))))
282 (when (or (not charset)
283 (eq 'gnus-all mail-parse-ignored-charsets)
284 (memq 'gnus-all mail-parse-ignored-charsets)
285 (memq charset mail-parse-ignored-charsets))
286 (setq charset mail-parse-charset))
287 (or
288 (when (featurep 'mule)
289 (let ((coding-system (mm-charset-to-coding-system
290 charset
291 ;; Allow overwrite using
292 ;; `mm-charset-override-alist'.
293 nil t)))
294 (if (and (not coding-system)
295 (listp mail-parse-ignored-charsets)
296 (memq 'gnus-unknown mail-parse-ignored-charsets))
297 (setq coding-system
298 (mm-charset-to-coding-system mail-parse-charset)))
299 (when (and charset coding-system
300 (mm-multibyte-p)
301 (or (not (eq coding-system 'ascii))
302 (setq coding-system mail-parse-charset)))
303 (mm-decode-coding-string string coding-system))))
304 string))
305
306 (provide 'mm-bodies)
307
308 ;;; arch-tag: 41104bb6-4443-4ca9-8d5c-ff87ecf27d8d
309 ;;; mm-bodies.el ends here