Output alists with dotted pair notation in .dir-locals.el
[emacs.git] / lisp / gnus / mm-bodies.el
blobe292dac16fe66874e2e13efa1b567d17487a9602
1 ;;; mm-bodies.el --- Functions for decoding MIME things
3 ;; Copyright (C) 1998-2018 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/>.
22 ;;; Commentary:
24 ;;; Code:
26 (require 'mm-util)
27 (require 'rfc2047)
28 (require 'mm-encode)
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
53 ;; markup. - jh.
54 (utf-16 . base64)
55 (utf-16be . base64)
56 (utf-16le . base64))
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"
61 (const 7bit)
62 (const 8bit)
63 (const quoted-printable)
64 (const base64))))
65 :group 'mime)
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.
81 (or charset
82 (save-excursion
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)
87 (message-options-set
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.
92 nil)))
93 (save-excursion
94 (if charset
95 (progn
96 (insert
97 (prog1
98 (encode-coding-string (buffer-string)
99 (mm-charset-to-coding-system charset))
100 (erase-buffer)
101 (set-buffer-multibyte nil)))
102 charset)
103 (goto-char (point-min))
104 (let ((charsets (mm-find-mime-charset-region (point-min) (point-max)
105 mm-hack-charsets)))
106 (cond
107 ;; No encoding.
108 ((null charsets)
109 nil)
110 ;; Too many charsets.
111 ((> (length charsets) 1)
112 charsets)
113 ;; We encode.
115 (prog1
116 (setq charset (car charsets))
117 (insert
118 (prog1
119 (encode-coding-string (buffer-string)
120 (mm-charset-to-coding-system charset))
121 (erase-buffer)
122 (set-buffer-multibyte nil)))))
123 ))))))
125 (defun mm-long-lines-p (length)
126 "Say whether any of the lines in the buffer is longer than LENGTH."
127 (save-excursion
128 (goto-char (point-min))
129 (end-of-line)
130 (while (and (not (eobp))
131 (not (> (current-column) length)))
132 (forward-line 1)
133 (end-of-line))
134 (and (> (current-column) length)
135 (current-column))))
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)))
145 (require 'message)
146 (cond
147 ((and (not longp)
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)))))
151 (eq bits '7bit))
152 bits)
153 ((and (not mm-use-ultra-safe-encoding)
154 (not longp)
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)))
159 bits)
161 (let ((encoding (or encoding
162 (cdr (assq charset mm-body-charset-encoding-alist))
163 (mm-qp-or-base64))))
164 (when mm-use-ultra-safe-encoding
165 (setq encoding (mm-safer-encoding encoding)))
166 (mm-encode-content-transfer-encoding encoding "text/plain")
167 encoding)))))
169 (defun mm-body-7-or-8 ()
170 "Say whether the body is 7bit or 8bit."
171 (if (save-excursion
172 (goto-char (point-min))
173 (skip-chars-forward mm-7bit-chars)
174 (eobp))
175 '7bit
176 '8bit))
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."
185 (prog1
186 (condition-case error
187 (cond
188 ((eq encoding 'quoted-printable)
189 (quoted-printable-decode-region (point-min) (point-max))
191 ((eq encoding 'base64)
192 (base64-decode-region
193 (point-min)
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.
199 (save-excursion
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 ]*$"
205 nil t)
206 (forward-line))
207 (point))))
208 ((memq encoding '(nil 7bit 8bit binary))
209 ;; Do nothing.
211 ((memq encoding '(x-uuencode x-uue))
212 (require 'mm-uu)
213 (funcall mm-uu-decode-function (point-min) (point-max))
215 ((eq encoding 'x-binhex)
216 (require 'mm-uu)
217 (funcall mm-uu-binhex-decode-function (point-min) (point-max))
219 ((eq encoding 'x-yenc)
220 (require 'mm-uu)
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)))
228 (error
229 (message "Error while decoding: %s" error)
230 nil))
231 (when (and
232 type
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))
251 (save-excursion
252 (when encoding
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'.
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 enable-multibyte-characters
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
282 charset
283 ;; Allow overwrite using
284 ;; `mm-charset-override-alist'.
285 nil t)))
286 (if (and (not coding-system)
287 (listp mail-parse-ignored-charsets)
288 (memq 'gnus-unknown mail-parse-ignored-charsets))
289 (setq coding-system
290 (mm-charset-to-coding-system mail-parse-charset)))
291 (when (and charset coding-system
292 enable-multibyte-characters
293 (or (not (eq coding-system 'ascii))
294 (setq coding-system mail-parse-charset)))
295 (decode-coding-string string coding-system)))
296 string))
298 (provide 'mm-bodies)
300 ;;; mm-bodies.el ends here