]> code.delx.au - gnu-emacs/blob - lisp/gnus/mm-bodies.el
(mode-popup-menu): Add defvar.
[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 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 (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)
99 (mm-charset-to-coding-system charset))
100 charset)
101 (goto-char (point-min))
102 (let ((charsets (mm-find-mime-charset-region (point-min) (point-max)
103 mm-hack-charsets)))
104 (cond
105 ;; No encoding.
106 ((null charsets)
107 nil)
108 ;; Too many charsets.
109 ((> (length charsets) 1)
110 charsets)
111 ;; We encode.
112 (t
113 (prog1
114 (setq charset (car charsets))
115 (mm-encode-coding-region (point-min) (point-max)
116 (mm-charset-to-coding-system charset))))
117 ))))))
118
119 (defun mm-long-lines-p (length)
120 "Say whether any of the lines in the buffer is longer than LENGTH."
121 (save-excursion
122 (goto-char (point-min))
123 (end-of-line)
124 (while (and (not (eobp))
125 (not (> (current-column) length)))
126 (forward-line 1)
127 (end-of-line))
128 (and (> (current-column) length)
129 (current-column))))
130
131 (defvar message-posting-charset)
132
133 (defun mm-body-encoding (charset &optional encoding)
134 "Do Content-Transfer-Encoding and return the encoding of the current buffer."
135 (when (stringp encoding)
136 (setq encoding (intern (downcase encoding))))
137 (let ((bits (mm-body-7-or-8))
138 (longp (mm-long-lines-p 1000)))
139 (require 'message)
140 (cond
141 ((and (not longp)
142 (not (and mm-use-ultra-safe-encoding
143 (or (save-excursion (re-search-forward " $" nil t))
144 (save-excursion (re-search-forward "^From " nil t)))))
145 (eq bits '7bit))
146 bits)
147 ((and (not mm-use-ultra-safe-encoding)
148 (not longp)
149 (not (cdr (assq charset mm-body-charset-encoding-alist)))
150 (or (eq t (cdr message-posting-charset))
151 (memq charset (cdr message-posting-charset))
152 (eq charset mail-parse-charset)))
153 bits)
154 (t
155 (let ((encoding (or encoding
156 (cdr (assq charset mm-body-charset-encoding-alist))
157 (mm-qp-or-base64))))
158 (when mm-use-ultra-safe-encoding
159 (setq encoding (mm-safer-encoding encoding)))
160 (mm-encode-content-transfer-encoding encoding "text/plain")
161 encoding)))))
162
163 (defun mm-body-7-or-8 ()
164 "Say whether the body is 7bit or 8bit."
165 (if (save-excursion
166 (goto-char (point-min))
167 (skip-chars-forward mm-7bit-chars)
168 (eobp))
169 '7bit
170 '8bit))
171
172 ;;;
173 ;;; Functions for decoding
174 ;;;
175
176 (eval-when-compile (defvar mm-uu-yenc-decode-function))
177
178 (defun mm-decode-content-transfer-encoding (encoding &optional type)
179 "Decodes buffer encoded with ENCODING, returning success status.
180 If TYPE is `text/plain' CRLF->LF translation may occur."
181 (prog1
182 (condition-case error
183 (cond
184 ((eq encoding 'quoted-printable)
185 (quoted-printable-decode-region (point-min) (point-max))
186 t)
187 ((eq encoding 'base64)
188 (base64-decode-region
189 (point-min)
190 ;; Some mailers insert whitespace
191 ;; junk at the end which
192 ;; base64-decode-region dislikes.
193 ;; Also remove possible junk which could
194 ;; have been added by mailing list software.
195 (save-excursion
196 (goto-char (point-min))
197 (while (re-search-forward "^[\t ]*\r?\n" nil t)
198 (delete-region (match-beginning 0) (match-end 0)))
199 (goto-char (point-max))
200 (when (re-search-backward "^[A-Za-z0-9+/]+=*[\t ]*$" nil t)
201 (forward-line))
202 (point))))
203 ((memq encoding '(7bit 8bit binary))
204 ;; Do nothing.
205 t)
206 ((null encoding)
207 ;; Do nothing.
208 t)
209 ((memq encoding '(x-uuencode x-uue))
210 (require 'mm-uu)
211 (funcall mm-uu-decode-function (point-min) (point-max))
212 t)
213 ((eq encoding 'x-binhex)
214 (require 'mm-uu)
215 (funcall mm-uu-binhex-decode-function (point-min) (point-max))
216 t)
217 ((eq encoding 'x-yenc)
218 (require 'mm-uu)
219 (funcall mm-uu-yenc-decode-function (point-min) (point-max))
220 )
221 ((functionp encoding)
222 (funcall encoding (point-min) (point-max))
223 t)
224 (t
225 (message "Unknown encoding %s; defaulting to 8bit" encoding)))
226 (error
227 (message "Error while decoding: %s" error)
228 nil))
229 (when (and
230 (memq encoding '(base64 x-uuencode x-uue x-binhex x-yenc))
231 (equal type "text/plain"))
232 (goto-char (point-min))
233 (while (search-forward "\r\n" nil t)
234 (replace-match "\n" t t)))))
235
236 (defun mm-decode-body (charset &optional encoding type)
237 "Decode the current article that has been encoded with ENCODING to CHARSET.
238 ENCODING is a MIME content transfer encoding.
239 CHARSET is the MIME charset with which to decode the data after transfer
240 decoding. If it is nil, default to `mail-parse-charset'."
241 (when (stringp charset)
242 (setq charset (intern (downcase charset))))
243 (when (or (not charset)
244 (eq 'gnus-all mail-parse-ignored-charsets)
245 (memq 'gnus-all mail-parse-ignored-charsets)
246 (memq charset mail-parse-ignored-charsets))
247 (setq charset mail-parse-charset))
248 (save-excursion
249 (when encoding
250 (mm-decode-content-transfer-encoding encoding type))
251 (when (featurep 'mule) ; Fixme: Wrong test for unibyte session.
252 (let ((coding-system (mm-charset-to-coding-system charset)))
253 (if (and (not coding-system)
254 (listp mail-parse-ignored-charsets)
255 (memq 'gnus-unknown mail-parse-ignored-charsets))
256 (setq coding-system
257 (mm-charset-to-coding-system mail-parse-charset)))
258 (when (and charset coding-system
259 ;; buffer-file-coding-system
260 ;;Article buffer is nil coding system
261 ;;in XEmacs
262 (mm-multibyte-p)
263 (or (not (eq coding-system 'ascii))
264 (setq coding-system mail-parse-charset))
265 (not (eq coding-system 'gnus-decoded)))
266 (mm-decode-coding-region (point-min) (point-max)
267 coding-system))
268 (setq buffer-file-coding-system
269 (if (boundp 'last-coding-system-used)
270 (symbol-value 'last-coding-system-used)
271 coding-system))))))
272
273 (defun mm-decode-string (string charset)
274 "Decode STRING with CHARSET."
275 (when (stringp charset)
276 (setq charset (intern (downcase charset))))
277 (when (or (not charset)
278 (eq 'gnus-all mail-parse-ignored-charsets)
279 (memq 'gnus-all mail-parse-ignored-charsets)
280 (memq charset mail-parse-ignored-charsets))
281 (setq charset mail-parse-charset))
282 (or
283 (when (featurep 'mule)
284 (let ((coding-system (mm-charset-to-coding-system charset)))
285 (if (and (not coding-system)
286 (listp mail-parse-ignored-charsets)
287 (memq 'gnus-unknown mail-parse-ignored-charsets))
288 (setq coding-system
289 (mm-charset-to-coding-system mail-parse-charset)))
290 (when (and charset coding-system
291 (mm-multibyte-p)
292 (or (not (eq coding-system 'ascii))
293 (setq coding-system mail-parse-charset)))
294 (mm-decode-coding-string string coding-system))))
295 string))
296
297 (provide 'mm-bodies)
298
299 ;;; arch-tag: 41104bb6-4443-4ca9-8d5c-ff87ecf27d8d
300 ;;; mm-bodies.el ends here