make thread_check_current_buffer return bool
[emacs.git] / lisp / gnus / mm-encode.el
blob6b6313bb501e7ff257b37252b5748a8763097b84
1 ;;; mm-encode.el --- Functions for encoding MIME things
3 ;; Copyright (C) 1998-2013 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 (eval-when-compile (require 'cl))
27 (require 'mail-parse)
28 (autoload 'mailcap-extension-to-mime "mailcap")
29 (autoload 'mm-body-7-or-8 "mm-bodies")
30 (autoload 'mm-long-lines-p "mm-bodies")
32 (defcustom mm-content-transfer-encoding-defaults
33 '(("text/x-patch" 8bit)
34 ("text/.*" qp-or-base64)
35 ("message/rfc822" 8bit)
36 ("application/emacs-lisp" qp-or-base64)
37 ("application/x-emacs-lisp" qp-or-base64)
38 ("application/x-patch" qp-or-base64)
39 (".*" base64))
40 "Alist of regexps that match MIME types and their encodings.
41 If the encoding is `qp-or-base64', then either quoted-printable
42 or base64 will be used, depending on what is more efficient.
44 This list is only consulted when encoding MIME parts in the
45 bodies -- not for the regular non-MIME-ish messages."
46 :type '(repeat (list (regexp :tag "MIME type")
47 (choice :tag "encoding"
48 (const 7bit)
49 (const 8bit)
50 (const qp-or-base64)
51 (const quoted-printable)
52 (const base64))))
53 :group 'mime)
55 (defcustom mm-sign-option nil
56 "Option how to create signed parts.
57 nil, use the default keys without asking;
58 `guided', let you select signing keys from the menu."
59 :version "23.2" ;; No Gnus 0.12
60 :type '(choice (item guided)
61 (item :tag "default" nil))
62 :group 'mime-security)
64 (defcustom mm-encrypt-option nil
65 "Option how to create encrypted parts.
66 nil, use the default keys without asking;
67 `guided', let you select recipients' keys from the menu."
68 :version "23.2" ;; No Gnus 0.12
69 :type '(choice (item guided)
70 (item :tag "default" nil))
71 :group 'mime-security)
73 (defvar mm-use-ultra-safe-encoding nil
74 "If non-nil, use encodings aimed at Procrustean bed survival.
76 This means that textual parts are encoded as quoted-printable if they
77 contain lines longer than 76 characters or starting with \"From \" in
78 the body. Non-7bit encodings (8bit, binary) are generally disallowed.
79 This is to reduce the probability that a broken MTA or MDA changes the
80 message.
82 This variable should never be set directly, but bound before a call to
83 `mml-generate-mime' or similar functions.")
85 (defun mm-insert-rfc822-headers (charset encoding)
86 "Insert text/plain headers with CHARSET and ENCODING."
87 (insert "MIME-Version: 1.0\n")
88 (insert "Content-Type: text/plain; charset="
89 (mail-quote-string (downcase (symbol-name charset))) "\n")
90 (insert "Content-Transfer-Encoding: "
91 (downcase (symbol-name encoding)) "\n"))
93 (defun mm-insert-multipart-headers ()
94 "Insert multipart/mixed headers."
95 (let ((boundary "=-=-="))
96 (insert "MIME-Version: 1.0\n")
97 (insert "Content-Type: multipart/mixed; boundary=\"" boundary "\"\n")
98 boundary))
100 ;;;###autoload
101 (defun mm-default-file-encoding (file)
102 "Return a default encoding for FILE."
103 (if (not (string-match "\\.[^.]+$" file))
104 "application/octet-stream"
105 (mailcap-extension-to-mime (match-string 0 file))))
107 (defun mm-safer-encoding (encoding &optional type)
108 "Return an encoding similar to ENCODING but safer than it."
109 (cond
110 ((eq encoding '7bit) '7bit) ;; 7bit is considered safe.
111 ((memq encoding '(8bit quoted-printable))
112 ;; According to RFC2046, 5.2.1, RFC822 Subtype, "quoted-printable" is not
113 ;; a valid encoding for message/rfc822:
114 ;; No encoding other than "7bit", "8bit", or "binary" is permitted for the
115 ;; body of a "message/rfc822" entity.
116 (if (string= type "message/rfc822") '8bit 'quoted-printable))
117 ;; The remaining encodings are binary and base64 (and perhaps some
118 ;; non-standard ones), which are both turned into base64.
119 (t (if (string= type "message/rfc822") 'binary 'base64))))
121 (defun mm-encode-content-transfer-encoding (encoding &optional type)
122 "Encode the current buffer with ENCODING for MIME type TYPE.
123 ENCODING can be: nil (do nothing); one of `quoted-printable', `base64';
124 `7bit', `8bit' or `binary' (all do nothing); a function to do the encoding."
125 (cond
126 ((eq encoding 'quoted-printable)
127 ;; This used to try to make a multibyte buffer unibyte. That's
128 ;; completely wrong, since you'd get QP-encoded emacs-mule. If
129 ;; this gets run on multibyte text it's an error that needs
130 ;; fixing, and the encoding function will signal an error.
131 ;; Likewise base64 below.
132 (quoted-printable-encode-region (point-min) (point-max) t))
133 ((eq encoding 'base64)
134 (when (string-match "\\`text/" type)
135 (goto-char (point-min))
136 (while (search-forward "\n" nil t)
137 (replace-match "\r\n" t t)))
138 (base64-encode-region (point-min) (point-max)))
139 ((memq encoding '(7bit 8bit binary))
140 ;; Do nothing.
142 ((null encoding)
143 ;; Do nothing.
145 ;; Fixme: Ignoring errors here looks bogus.
146 ((functionp encoding)
147 (ignore-errors (funcall encoding (point-min) (point-max))))
149 (error "Unknown encoding %s" encoding))))
151 (defun mm-encode-buffer (type &optional encoding)
152 "Encode the buffer which contains data of MIME type TYPE by ENCODING.
153 TYPE is a string or a list of the components.
154 The optional ENCODING overrides the encoding determined according to
155 TYPE and `mm-content-transfer-encoding-defaults'.
156 The encoding used is returned."
157 (let ((mime-type (if (stringp type) type (car type))))
158 (mm-encode-content-transfer-encoding
159 (or encoding
160 (setq encoding (or (and (listp type)
161 (cadr (assq 'encoding type)))
162 (mm-content-transfer-encoding mime-type))))
163 mime-type)
164 encoding))
166 (defun mm-insert-headers (type encoding &optional file)
167 "Insert headers for TYPE."
168 (insert "Content-Type: " type)
169 (when file
170 (insert ";\n\tname=\"" (file-name-nondirectory file) "\""))
171 (insert "\n")
172 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
173 (insert "Content-Disposition: inline")
174 (when file
175 (insert ";\n\tfilename=\"" (file-name-nondirectory file) "\""))
176 (insert "\n")
177 (insert "\n"))
179 (defun mm-content-transfer-encoding (type)
180 "Return a CTE suitable for TYPE to encode the current buffer."
181 (let ((rules mm-content-transfer-encoding-defaults))
182 (catch 'found
183 (while rules
184 (when (string-match (caar rules) type)
185 (throw 'found
186 (let ((encoding
187 (if (eq (cadr (car rules)) 'qp-or-base64)
188 (mm-qp-or-base64)
189 (cadr (car rules)))))
190 (if mm-use-ultra-safe-encoding
191 (mm-safer-encoding encoding type)
192 encoding))))
193 (pop rules)))))
195 (defun mm-qp-or-base64 ()
196 "Return the type with which to encode the buffer.
197 This is either `base64' or `quoted-printable'."
198 (if (equal mm-use-ultra-safe-encoding '(sign . "pgp"))
199 ;; perhaps not always accurate?
200 'quoted-printable
201 (save-excursion
202 (let ((limit (min (point-max) (+ 2000 (point-min))))
203 (n8bit 0))
204 (goto-char (point-min))
205 (skip-chars-forward "\x20-\x7f\r\n\t" limit)
206 (while (< (point) limit)
207 (incf n8bit)
208 (forward-char 1)
209 (skip-chars-forward "\x20-\x7f\r\n\t" limit))
210 (if (or (< (* 6 n8bit) (- limit (point-min)))
211 ;; Don't base64, say, a short line with a single
212 ;; non-ASCII char when splitting parts by charset.
213 (= n8bit 1))
214 'quoted-printable
215 'base64)))))
217 (provide 'mm-encode)
219 ;;; mm-encode.el ends here