Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / lisp / gnus / mm-encode.el
CommitLineData
23f87bed 1;;; mm-encode.el --- Functions for encoding MIME things
e84b4b86 2
acaf905b 3;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
c113de23
GM
4
5;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7;; This file is part of GNU Emacs.
8
5e809f55 9;; GNU Emacs is free software: you can redistribute it and/or modify
c113de23 10;; it under the terms of the GNU General Public License as published by
5e809f55
GM
11;; the Free Software Foundation, either version 3 of the License, or
12;; (at your option) any later version.
c113de23
GM
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
5e809f55 16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c113de23
GM
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
5e809f55 20;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
c113de23
GM
21
22;;; Commentary:
23
24;;; Code:
25
b28ce55a 26(eval-when-compile (require 'cl))
c113de23 27(require 'mail-parse)
aa8f8277 28(autoload 'mailcap-extension-to-mime "mailcap")
8abf1b22
GM
29(autoload 'mm-body-7-or-8 "mm-bodies")
30(autoload 'mm-long-lines-p "mm-bodies")
c113de23 31
23f87bed 32(defcustom mm-content-transfer-encoding-defaults
c113de23
GM
33 '(("text/x-patch" 8bit)
34 ("text/.*" qp-or-base64)
35 ("message/rfc822" 8bit)
23f87bed
MB
36 ("application/emacs-lisp" qp-or-base64)
37 ("application/x-emacs-lisp" qp-or-base64)
38 ("application/x-patch" qp-or-base64)
5be28abc 39 (".*" base64))
c113de23
GM
40 "Alist of regexps that match MIME types and their encodings.
41If the encoding is `qp-or-base64', then either quoted-printable
23f87bed
MB
42or base64 will be used, depending on what is more efficient.
43
a3f57c41
G
44This list is only consulted when encoding MIME parts in the
45bodies -- not for the regular non-MIME-ish messages."
23f87bed
MB
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)
c113de23 54
54c72c31
KY
55(defcustom mm-sign-option nil
56 "Option how to create signed parts.
57nil, use the default keys without asking;
58`guided', let you select signing keys from the menu."
029dda9c 59 :version "23.2" ;; No Gnus 0.12
54c72c31
KY
60 :type '(choice (item guided)
61 (item :tag "default" nil))
62 :group 'mime-security)
63
64(defcustom mm-encrypt-option nil
65 "Option how to create encrypted parts.
66nil, use the default keys without asking;
67`guided', let you select recipients' keys from the menu."
029dda9c 68 :version "23.2" ;; No Gnus 0.12
54c72c31
KY
69 :type '(choice (item guided)
70 (item :tag "default" nil))
71 :group 'mime-security)
72
c113de23
GM
73(defvar mm-use-ultra-safe-encoding nil
74 "If non-nil, use encodings aimed at Procrustean bed survival.
75
76This means that textual parts are encoded as quoted-printable if they
77contain lines longer than 76 characters or starting with \"From \" in
78the body. Non-7bit encodings (8bit, binary) are generally disallowed.
79This is to reduce the probability that a broken MTA or MDA changes the
80message.
81
82This variable should never be set directly, but bound before a call to
83`mml-generate-mime' or similar functions.")
84
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"))
92
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))
99
c3760c17 100;;;###autoload
c113de23
GM
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))))
106
58a67d68 107(defun mm-safer-encoding (encoding &optional type)
23f87bed 108 "Return an encoding similar to ENCODING but safer than it."
c113de23 109 (cond
23f87bed 110 ((eq encoding '7bit) '7bit) ;; 7bit is considered safe.
58a67d68
MB
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))
8f688cb0 117 ;; The remaining encodings are binary and base64 (and perhaps some
c113de23 118 ;; non-standard ones), which are both turned into base64.
58a67d68 119 (t (if (string= type "message/rfc822") 'binary 'base64))))
c113de23
GM
120
121(defun mm-encode-content-transfer-encoding (encoding &optional type)
23f87bed
MB
122 "Encode the current buffer with ENCODING for MIME type TYPE.
123ENCODING can be: nil (do nothing); one of `quoted-printable', `base64';
124`7bit', `8bit' or `binary' (all do nothing); a function to do the encoding."
c113de23
GM
125 (cond
126 ((eq encoding 'quoted-printable)
23f87bed
MB
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.
c113de23
GM
132 (quoted-printable-encode-region (point-min) (point-max) t))
133 ((eq encoding 'base64)
4a2358e9 134 (when (string-match "\\`text/" type)
c113de23
GM
135 (goto-char (point-min))
136 (while (search-forward "\n" nil t)
137 (replace-match "\r\n" t t)))
23f87bed 138 (base64-encode-region (point-min) (point-max)))
c113de23
GM
139 ((memq encoding '(7bit 8bit binary))
140 ;; Do nothing.
141 )
142 ((null encoding)
143 ;; Do nothing.
144 )
23f87bed 145 ;; Fixme: Ignoring errors here looks bogus.
c113de23
GM
146 ((functionp encoding)
147 (ignore-errors (funcall encoding (point-min) (point-max))))
148 (t
23f87bed 149 (error "Unknown encoding %s" encoding))))
c113de23 150
de0bdfe7
KY
151(defun mm-encode-buffer (type &optional encoding)
152 "Encode the buffer which contains data of MIME type TYPE by ENCODING.
23f87bed 153TYPE is a string or a list of the components.
de0bdfe7
KY
154The optional ENCODING overrides the encoding determined according to
155TYPE and `mm-content-transfer-encoding-defaults'.
c113de23 156The encoding used is returned."
de0bdfe7
KY
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)
c113de23
GM
164 encoding))
165
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"))
178
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
a1506d29 186 (let ((encoding
c113de23
GM
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
58a67d68 191 (mm-safer-encoding encoding type)
c113de23
GM
192 encoding))))
193 (pop rules)))))
194
195(defun mm-qp-or-base64 ()
23f87bed
MB
196 "Return the type with which to encode the buffer.
197This 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)))))
c113de23
GM
216
217(provide 'mm-encode)
218
219;;; mm-encode.el ends here