1 ;;; mm-bodies.el --- Functions for decoding MIME things
3 ;; Copyright (C) 1998-2016 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 (defvar mm-uu-yenc-decode-function
)
31 (defvar mm-uu-decode-function
)
32 (defvar mm-uu-binhex-decode-function
)
34 ;; 8bit treatment gets any char except: 0x32 - 0x7f, LF, TAB, BEL,
35 ;; BS, vertical TAB, form feed, and ^_
37 ;; Note that CR is *not* included, as that would allow a non-paired CR
38 ;; in the body contrary to RFC 2822:
40 ;; - CR and LF MUST only occur together as CRLF; they MUST NOT
41 ;; appear independently in the body.
43 (defvar mm-7bit-chars
"\x20-\x7f\n\t\x7\x8\xb\xc\x1f")
45 (defcustom mm-body-charset-encoding-alist
46 '((iso-2022-jp .
7bit
)
47 (iso-2022-jp-2 .
7bit
)
48 ;; We MUST encode UTF-16 because it can contain \0's which is
49 ;; known to break servers.
50 ;; Note: UTF-16 variants are invalid for text parts [RFC 2781],
51 ;; so this can't happen :-/.
52 ;; PPS: Yes, it can happen if the user specifies UTF-16 in the MML
57 "Alist of MIME charsets to encodings.
58 Valid encodings are `7bit', `8bit', `quoted-printable' and `base64'."
59 :type
'(repeat (cons (symbol :tag
"charset")
60 (choice :tag
"encoding"
63 (const quoted-printable
)
67 (autoload 'message-options-get
"message")
68 (declare-function message-options-set
"message" (symbol value
))
70 (defun mm-encode-body (&optional charset
)
72 Should be called narrowed to the body that is to be encoded.
73 If there is more than one non-ASCII MULE charset in the body, then the
74 list of MULE charsets found is returned.
75 If CHARSET is non-nil, it is used as the MIME charset to encode the body.
76 If successful, the MIME charset is returned.
77 If no encoding was done, nil is returned."
78 (if (not (mm-multibyte-p))
79 ;; In the non-Mule case, we search for non-ASCII chars and
80 ;; return the value of `mail-parse-charset' if any are found.
83 (goto-char (point-min))
84 (if (re-search-forward "[^\x0-\x7f]" nil t
)
85 (or mail-parse-charset
86 (message-options-get 'mm-body-charset-encoding-alist
)
88 'mm-body-charset-encoding-alist
89 (read-coding-system "Charset used in the article: ")))
90 ;; The logic in `mml-generate-mime-1' confirms that it's OK
91 ;; to return nil here.
96 (encode-coding-region (point-min) (point-max)
97 (mm-charset-to-coding-system charset
))
99 (goto-char (point-min))
100 (let ((charsets (mm-find-mime-charset-region (point-min) (point-max)
106 ;; Too many charsets.
107 ((> (length charsets
) 1)
112 (setq charset
(car charsets
))
113 (encode-coding-region (point-min) (point-max)
114 (mm-charset-to-coding-system charset
))))
117 (defun mm-long-lines-p (length)
118 "Say whether any of the lines in the buffer is longer than LENGTH."
120 (goto-char (point-min))
122 (while (and (not (eobp))
123 (not (> (current-column) length
)))
126 (and (> (current-column) length
)
129 (defvar message-posting-charset
)
131 (defun mm-body-encoding (charset &optional encoding
)
132 "Do Content-Transfer-Encoding and return the encoding of the current buffer."
133 (when (stringp encoding
)
134 (setq encoding
(intern (downcase encoding
))))
135 (let ((bits (mm-body-7-or-8))
136 (longp (mm-long-lines-p 1000)))
140 (not (and mm-use-ultra-safe-encoding
141 (or (save-excursion (re-search-forward " $" nil t
))
142 (save-excursion (re-search-forward "^From " nil t
)))))
145 ((and (not mm-use-ultra-safe-encoding
)
147 (not (cdr (assq charset mm-body-charset-encoding-alist
)))
148 (or (eq t
(cdr message-posting-charset
))
149 (memq charset
(cdr message-posting-charset
))
150 (eq charset mail-parse-charset
)))
153 (let ((encoding (or encoding
154 (cdr (assq charset mm-body-charset-encoding-alist
))
156 (when mm-use-ultra-safe-encoding
157 (setq encoding
(mm-safer-encoding encoding
)))
158 (mm-encode-content-transfer-encoding encoding
"text/plain")
161 (defun mm-body-7-or-8 ()
162 "Say whether the body is 7bit or 8bit."
164 (goto-char (point-min))
165 (skip-chars-forward mm-7bit-chars
)
171 ;;; Functions for decoding
174 (defun mm-decode-content-transfer-encoding (encoding &optional type
)
175 "Decodes buffer encoded with ENCODING, returning success status.
176 If TYPE is `text/plain' CRLF->LF translation may occur."
178 (condition-case error
180 ((eq encoding
'quoted-printable
)
181 (quoted-printable-decode-region (point-min) (point-max))
183 ((eq encoding
'base64
)
184 (base64-decode-region
186 ;; Some mailers insert whitespace
187 ;; junk at the end which
188 ;; base64-decode-region dislikes.
189 ;; Also remove possible junk which could
190 ;; have been added by mailing list software.
192 (goto-char (point-min))
193 (while (re-search-forward "^[\t ]*\r?\n" nil t
)
194 (delete-region (match-beginning 0) (match-end 0)))
195 (goto-char (point-max))
196 (when (re-search-backward "^[\t ]*[A-Za-z0-9+/]+=*[\t ]*$"
200 ((memq encoding
'(nil 7bit
8bit binary
))
203 ((memq encoding
'(x-uuencode x-uue
))
205 (funcall mm-uu-decode-function
(point-min) (point-max))
207 ((eq encoding
'x-binhex
)
209 (funcall mm-uu-binhex-decode-function
(point-min) (point-max))
211 ((eq encoding
'x-yenc
)
213 (funcall mm-uu-yenc-decode-function
(point-min) (point-max))
215 ((functionp encoding
)
216 (funcall encoding
(point-min) (point-max))
219 (message "Unknown encoding %s; defaulting to 8bit" encoding
)))
221 (message "Error while decoding: %s" error
)
225 (memq encoding
'(base64 x-uuencode x-uue x-binhex x-yenc
))
226 (string-match "\\`text/" type
))
227 (goto-char (point-min))
228 (while (search-forward "\r\n" nil t
)
229 (replace-match "\n" t t
)))))
231 (defun mm-decode-body (charset &optional encoding type
)
232 "Decode the current article that has been encoded with ENCODING to CHARSET.
233 ENCODING is a MIME content transfer encoding.
234 CHARSET is the MIME charset with which to decode the data after transfer
235 decoding. If it is nil, default to `mail-parse-charset'."
236 (when (stringp charset
)
237 (setq charset
(intern (downcase charset
))))
238 (when (or (not charset
)
239 (eq 'gnus-all mail-parse-ignored-charsets
)
240 (memq 'gnus-all mail-parse-ignored-charsets
)
241 (memq charset mail-parse-ignored-charsets
))
242 (setq charset mail-parse-charset
))
245 (mm-decode-content-transfer-encoding encoding type
))
246 (when (not (eq charset
'gnus-decoded
))
247 (let ((coding-system (mm-charset-to-coding-system
248 ;; Allow overwrite using
249 ;; `mm-charset-override-alist'.
251 (if (and (not coding-system
)
252 (listp mail-parse-ignored-charsets
)
253 (memq 'gnus-unknown mail-parse-ignored-charsets
))
255 (mm-charset-to-coding-system mail-parse-charset
)))
256 (when (and charset coding-system
258 (or (not (eq coding-system
'ascii
))
259 (setq coding-system mail-parse-charset
)))
260 (decode-coding-region (point-min) (point-max) coding-system
))
261 (setq buffer-file-coding-system last-coding-system-used
)))))
263 (defun mm-decode-string (string charset
)
264 "Decode STRING with CHARSET."
265 (when (stringp charset
)
266 (setq charset
(intern (downcase charset
))))
267 (when (or (not charset
)
268 (eq 'gnus-all mail-parse-ignored-charsets
)
269 (memq 'gnus-all mail-parse-ignored-charsets
)
270 (memq charset mail-parse-ignored-charsets
))
271 (setq charset mail-parse-charset
))
273 (let ((coding-system (mm-charset-to-coding-system
275 ;; Allow overwrite using
276 ;; `mm-charset-override-alist'.
278 (if (and (not coding-system
)
279 (listp mail-parse-ignored-charsets
)
280 (memq 'gnus-unknown mail-parse-ignored-charsets
))
282 (mm-charset-to-coding-system mail-parse-charset
)))
283 (when (and charset coding-system
285 (or (not (eq coding-system
'ascii
))
286 (setq coding-system mail-parse-charset
)))
287 (decode-coding-string string coding-system
)))
292 ;;; mm-bodies.el ends here