1 ;;; mm-bodies.el --- Functions for decoding MIME things
3 ;; Copyright (C) 1998-2017 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 <https://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
)
71 "Encode whole buffer's contents.
72 Buffer's multibyteness will be turned off when encoding takes place.
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 enable-multibyte-characters
)
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.
98 (encode-coding-string (buffer-string)
99 (mm-charset-to-coding-system charset
))
101 (set-buffer-multibyte nil
)))
103 (goto-char (point-min))
104 (let ((charsets (mm-find-mime-charset-region (point-min) (point-max)
110 ;; Too many charsets.
111 ((> (length charsets
) 1)
116 (setq charset
(car charsets
))
119 (encode-coding-string (buffer-string)
120 (mm-charset-to-coding-system charset
))
122 (set-buffer-multibyte nil
)))))
125 (defun mm-long-lines-p (length)
126 "Say whether any of the lines in the buffer is longer than LENGTH."
128 (goto-char (point-min))
130 (while (and (not (eobp))
131 (not (> (current-column) length
)))
134 (and (> (current-column) length
)
137 (defvar message-posting-charset
)
139 (defun mm-body-encoding (charset &optional encoding
)
140 "Do Content-Transfer-Encoding and return the encoding of the current buffer."
141 (when (stringp encoding
)
142 (setq encoding
(intern (downcase encoding
))))
143 (let ((bits (mm-body-7-or-8))
144 (longp (mm-long-lines-p 1000)))
148 (not (and mm-use-ultra-safe-encoding
149 (or (save-excursion (re-search-forward " $" nil t
))
150 (save-excursion (re-search-forward "^From " nil t
)))))
153 ((and (not mm-use-ultra-safe-encoding
)
155 (not (cdr (assq charset mm-body-charset-encoding-alist
)))
156 (or (eq t
(cdr message-posting-charset
))
157 (memq charset
(cdr message-posting-charset
))
158 (eq charset mail-parse-charset
)))
161 (let ((encoding (or encoding
162 (cdr (assq charset mm-body-charset-encoding-alist
))
164 (when mm-use-ultra-safe-encoding
165 (setq encoding
(mm-safer-encoding encoding
)))
166 (mm-encode-content-transfer-encoding encoding
"text/plain")
169 (defun mm-body-7-or-8 ()
170 "Say whether the body is 7bit or 8bit."
172 (goto-char (point-min))
173 (skip-chars-forward mm-7bit-chars
)
179 ;;; Functions for decoding
182 (defun mm-decode-content-transfer-encoding (encoding &optional type
)
183 "Decodes buffer encoded with ENCODING, returning success status.
184 If TYPE is `text/plain' CRLF->LF translation may occur."
186 (condition-case error
188 ((eq encoding
'quoted-printable
)
189 (quoted-printable-decode-region (point-min) (point-max))
191 ((eq encoding
'base64
)
192 (base64-decode-region
194 ;; Some mailers insert whitespace
195 ;; junk at the end which
196 ;; base64-decode-region dislikes.
197 ;; Also remove possible junk which could
198 ;; have been added by mailing list software.
200 (goto-char (point-min))
201 (while (re-search-forward "^[\t ]*\r?\n" nil t
)
202 (delete-region (match-beginning 0) (match-end 0)))
203 (goto-char (point-max))
204 (when (re-search-backward "^[\t ]*[A-Za-z0-9+/]+=*[\t ]*$"
208 ((memq encoding
'(nil 7bit
8bit binary
))
211 ((memq encoding
'(x-uuencode x-uue
))
213 (funcall mm-uu-decode-function
(point-min) (point-max))
215 ((eq encoding
'x-binhex
)
217 (funcall mm-uu-binhex-decode-function
(point-min) (point-max))
219 ((eq encoding
'x-yenc
)
221 (funcall mm-uu-yenc-decode-function
(point-min) (point-max))
223 ((functionp encoding
)
224 (funcall encoding
(point-min) (point-max))
227 (message "Unknown encoding %s; defaulting to 8bit" encoding
)))
229 (message "Error while decoding: %s" error
)
233 (memq encoding
'(base64 x-uuencode x-uue x-binhex x-yenc
))
234 (string-match "\\`text/" type
))
235 (goto-char (point-min))
236 (while (search-forward "\r\n" nil t
)
237 (replace-match "\n" t t
)))))
239 (defun mm-decode-body (charset &optional encoding type
)
240 "Decode the current article that has been encoded with ENCODING to CHARSET.
241 ENCODING is a MIME content transfer encoding.
242 CHARSET is the MIME charset with which to decode the data after transfer
243 decoding. If it is nil, default to `mail-parse-charset'."
244 (when (stringp charset
)
245 (setq charset
(intern (downcase charset
))))
246 (when (or (not charset
)
247 (eq 'gnus-all mail-parse-ignored-charsets
)
248 (memq 'gnus-all mail-parse-ignored-charsets
)
249 (memq charset mail-parse-ignored-charsets
))
250 (setq charset mail-parse-charset
))
253 (mm-decode-content-transfer-encoding encoding type
))
254 (when (not (eq charset
'gnus-decoded
))
255 (let ((coding-system (mm-charset-to-coding-system
256 ;; Allow overwrite using
257 ;; `mm-charset-override-alist'.
259 (if (and (not coding-system
)
260 (listp mail-parse-ignored-charsets
)
261 (memq 'gnus-unknown mail-parse-ignored-charsets
))
263 (mm-charset-to-coding-system mail-parse-charset
)))
264 (when (and charset coding-system
266 (or (not (eq coding-system
'ascii
))
267 (setq coding-system mail-parse-charset
)))
268 (decode-coding-region (point-min) (point-max) coding-system
))
269 (setq buffer-file-coding-system last-coding-system-used
)))))
271 (defun mm-decode-string (string charset
)
272 "Decode STRING with CHARSET."
273 (when (stringp charset
)
274 (setq charset
(intern (downcase charset
))))
275 (when (or (not charset
)
276 (eq 'gnus-all mail-parse-ignored-charsets
)
277 (memq 'gnus-all mail-parse-ignored-charsets
)
278 (memq charset mail-parse-ignored-charsets
))
279 (setq charset mail-parse-charset
))
281 (let ((coding-system (mm-charset-to-coding-system
283 ;; Allow overwrite using
284 ;; `mm-charset-override-alist'.
286 (if (and (not coding-system
)
287 (listp mail-parse-ignored-charsets
)
288 (memq 'gnus-unknown mail-parse-ignored-charsets
))
290 (mm-charset-to-coding-system mail-parse-charset
)))
291 (when (and charset coding-system
293 (or (not (eq coding-system
'ascii
))
294 (setq coding-system mail-parse-charset
)))
295 (decode-coding-string string coding-system
)))
300 ;;; mm-bodies.el ends here