Inhibit `epa-file-handler' in Tramp
[emacs.git] / lisp / gnus / mm-bodies.el
blob4a25c1486a8aad206fa018b7dd98037df54ce55b
1 ;;; mm-bodies.el --- Functions for decoding MIME things
3 ;; Copyright (C) 1998-2015 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/>.
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 a body.
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.
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 (mm-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 (mm-encode-coding-region (point-min) (point-max)
97 (mm-charset-to-coding-system charset))
98 charset)
99 (goto-char (point-min))
100 (let ((charsets (mm-find-mime-charset-region (point-min) (point-max)
101 mm-hack-charsets)))
102 (cond
103 ;; No encoding.
104 ((null charsets)
105 nil)
106 ;; Too many charsets.
107 ((> (length charsets) 1)
108 charsets)
109 ;; We encode.
111 (prog1
112 (setq charset (car charsets))
113 (mm-encode-coding-region (point-min) (point-max)
114 (mm-charset-to-coding-system charset))))
115 ))))))
117 (defun mm-long-lines-p (length)
118 "Say whether any of the lines in the buffer is longer than LENGTH."
119 (save-excursion
120 (goto-char (point-min))
121 (end-of-line)
122 (while (and (not (eobp))
123 (not (> (current-column) length)))
124 (forward-line 1)
125 (end-of-line))
126 (and (> (current-column) length)
127 (current-column))))
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)))
137 (require 'message)
138 (cond
139 ((and (not longp)
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)))))
143 (eq bits '7bit))
144 bits)
145 ((and (not mm-use-ultra-safe-encoding)
146 (not longp)
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)))
151 bits)
153 (let ((encoding (or encoding
154 (cdr (assq charset mm-body-charset-encoding-alist))
155 (mm-qp-or-base64))))
156 (when mm-use-ultra-safe-encoding
157 (setq encoding (mm-safer-encoding encoding)))
158 (mm-encode-content-transfer-encoding encoding "text/plain")
159 encoding)))))
161 (defun mm-body-7-or-8 ()
162 "Say whether the body is 7bit or 8bit."
163 (if (save-excursion
164 (goto-char (point-min))
165 (skip-chars-forward mm-7bit-chars)
166 (eobp))
167 '7bit
168 '8bit))
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."
177 (prog1
178 (condition-case error
179 (cond
180 ((eq encoding 'quoted-printable)
181 (quoted-printable-decode-region (point-min) (point-max))
183 ((eq encoding 'base64)
184 (base64-decode-region
185 (point-min)
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.
191 (save-excursion
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 ]*$"
197 nil t)
198 (forward-line))
199 (point))))
200 ((memq encoding '(nil 7bit 8bit binary))
201 ;; Do nothing.
203 ((memq encoding '(x-uuencode x-uue))
204 (require 'mm-uu)
205 (funcall mm-uu-decode-function (point-min) (point-max))
207 ((eq encoding 'x-binhex)
208 (require 'mm-uu)
209 (funcall mm-uu-binhex-decode-function (point-min) (point-max))
211 ((eq encoding 'x-yenc)
212 (require 'mm-uu)
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)))
220 (error
221 (message "Error while decoding: %s" error)
222 nil))
223 (when (and
224 type
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))
243 (save-excursion
244 (when encoding
245 (mm-decode-content-transfer-encoding encoding type))
246 (when (and (featurep 'mule) ;; Fixme: Wrong test for unibyte session.
247 (not (eq charset 'gnus-decoded)))
248 (let ((coding-system (mm-charset-to-coding-system
249 ;; Allow overwrite using
250 ;; `mm-charset-override-alist'.
251 charset nil t)))
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 (mm-decode-coding-region (point-min) (point-max)
265 coding-system))
266 (setq buffer-file-coding-system
267 (if (boundp 'last-coding-system-used)
268 (symbol-value 'last-coding-system-used)
269 coding-system))))))
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 (when (featurep 'mule)
282 (let ((coding-system (mm-charset-to-coding-system
283 charset
284 ;; Allow overwrite using
285 ;; `mm-charset-override-alist'.
286 nil t)))
287 (if (and (not coding-system)
288 (listp mail-parse-ignored-charsets)
289 (memq 'gnus-unknown mail-parse-ignored-charsets))
290 (setq coding-system
291 (mm-charset-to-coding-system mail-parse-charset)))
292 (when (and charset coding-system
293 (mm-multibyte-p)
294 (or (not (eq coding-system 'ascii))
295 (setq coding-system mail-parse-charset)))
296 (mm-decode-coding-string string coding-system))))
297 string))
299 (provide 'mm-bodies)
301 ;;; mm-bodies.el ends here