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