Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / lisp / gnus / mm-encode.el
CommitLineData
23f87bed 1;;; mm-encode.el --- Functions for encoding MIME things
e84b4b86 2
73b0cd50 3;; Copyright (C) 1998-2011 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
100(defun mm-default-file-encoding (file)
101 "Return a default encoding for FILE."
102 (if (not (string-match "\\.[^.]+$" file))
103 "application/octet-stream"
104 (mailcap-extension-to-mime (match-string 0 file))))
105
58a67d68 106(defun mm-safer-encoding (encoding &optional type)
23f87bed 107 "Return an encoding similar to ENCODING but safer than it."
c113de23 108 (cond
23f87bed 109 ((eq encoding '7bit) '7bit) ;; 7bit is considered safe.
58a67d68
MB
110 ((memq encoding '(8bit quoted-printable))
111 ;; According to RFC2046, 5.2.1, RFC822 Subtype, "quoted-printable" is not
112 ;; a valid encoding for message/rfc822:
113 ;; No encoding other than "7bit", "8bit", or "binary" is permitted for the
114 ;; body of a "message/rfc822" entity.
115 (if (string= type "message/rfc822") '8bit 'quoted-printable))
8f688cb0 116 ;; The remaining encodings are binary and base64 (and perhaps some
c113de23 117 ;; non-standard ones), which are both turned into base64.
58a67d68 118 (t (if (string= type "message/rfc822") 'binary 'base64))))
c113de23
GM
119
120(defun mm-encode-content-transfer-encoding (encoding &optional type)
23f87bed
MB
121 "Encode the current buffer with ENCODING for MIME type TYPE.
122ENCODING can be: nil (do nothing); one of `quoted-printable', `base64';
123`7bit', `8bit' or `binary' (all do nothing); a function to do the encoding."
c113de23
GM
124 (cond
125 ((eq encoding 'quoted-printable)
23f87bed
MB
126 ;; This used to try to make a multibyte buffer unibyte. That's
127 ;; completely wrong, since you'd get QP-encoded emacs-mule. If
128 ;; this gets run on multibyte text it's an error that needs
129 ;; fixing, and the encoding function will signal an error.
130 ;; Likewise base64 below.
c113de23
GM
131 (quoted-printable-encode-region (point-min) (point-max) t))
132 ((eq encoding 'base64)
4a2358e9 133 (when (string-match "\\`text/" type)
c113de23
GM
134 (goto-char (point-min))
135 (while (search-forward "\n" nil t)
136 (replace-match "\r\n" t t)))
23f87bed 137 (base64-encode-region (point-min) (point-max)))
c113de23
GM
138 ((memq encoding '(7bit 8bit binary))
139 ;; Do nothing.
140 )
141 ((null encoding)
142 ;; Do nothing.
143 )
23f87bed 144 ;; Fixme: Ignoring errors here looks bogus.
c113de23
GM
145 ((functionp encoding)
146 (ignore-errors (funcall encoding (point-min) (point-max))))
147 (t
23f87bed 148 (error "Unknown encoding %s" encoding))))
c113de23 149
de0bdfe7
KY
150(defun mm-encode-buffer (type &optional encoding)
151 "Encode the buffer which contains data of MIME type TYPE by ENCODING.
23f87bed 152TYPE is a string or a list of the components.
de0bdfe7
KY
153The optional ENCODING overrides the encoding determined according to
154TYPE and `mm-content-transfer-encoding-defaults'.
c113de23 155The encoding used is returned."
de0bdfe7
KY
156 (let ((mime-type (if (stringp type) type (car type))))
157 (mm-encode-content-transfer-encoding
158 (or encoding
159 (setq encoding (or (and (listp type)
160 (cadr (assq 'encoding type)))
161 (mm-content-transfer-encoding mime-type))))
162 mime-type)
c113de23
GM
163 encoding))
164
165(defun mm-insert-headers (type encoding &optional file)
166 "Insert headers for TYPE."
167 (insert "Content-Type: " type)
168 (when file
169 (insert ";\n\tname=\"" (file-name-nondirectory file) "\""))
170 (insert "\n")
171 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
172 (insert "Content-Disposition: inline")
173 (when file
174 (insert ";\n\tfilename=\"" (file-name-nondirectory file) "\""))
175 (insert "\n")
176 (insert "\n"))
177
178(defun mm-content-transfer-encoding (type)
179 "Return a CTE suitable for TYPE to encode the current buffer."
180 (let ((rules mm-content-transfer-encoding-defaults))
181 (catch 'found
182 (while rules
183 (when (string-match (caar rules) type)
184 (throw 'found
a1506d29 185 (let ((encoding
c113de23
GM
186 (if (eq (cadr (car rules)) 'qp-or-base64)
187 (mm-qp-or-base64)
188 (cadr (car rules)))))
189 (if mm-use-ultra-safe-encoding
58a67d68 190 (mm-safer-encoding encoding type)
c113de23
GM
191 encoding))))
192 (pop rules)))))
193
194(defun mm-qp-or-base64 ()
23f87bed
MB
195 "Return the type with which to encode the buffer.
196This is either `base64' or `quoted-printable'."
197 (if (equal mm-use-ultra-safe-encoding '(sign . "pgp"))
198 ;; perhaps not always accurate?
199 'quoted-printable
200 (save-excursion
201 (let ((limit (min (point-max) (+ 2000 (point-min))))
202 (n8bit 0))
203 (goto-char (point-min))
204 (skip-chars-forward "\x20-\x7f\r\n\t" limit)
205 (while (< (point) limit)
206 (incf n8bit)
207 (forward-char 1)
208 (skip-chars-forward "\x20-\x7f\r\n\t" limit))
209 (if (or (< (* 6 n8bit) (- limit (point-min)))
210 ;; Don't base64, say, a short line with a single
211 ;; non-ASCII char when splitting parts by charset.
212 (= n8bit 1))
213 'quoted-printable
214 'base64)))))
c113de23
GM
215
216(provide 'mm-encode)
217
218;;; mm-encode.el ends here