Commit | Line | Data |
---|---|---|
23f87bed | 1 | ;;; mm-encode.el --- Functions for encoding MIME things |
e84b4b86 TTN |
2 | |
3 | ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, | |
114f9c96 | 4 | ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
c113de23 GM |
5 | |
6 | ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> | |
7 | ;; MORIOKA Tomohiko <morioka@jaist.ac.jp> | |
8 | ;; This file is part of GNU Emacs. | |
9 | ||
5e809f55 | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
c113de23 | 11 | ;; it under the terms of the GNU General Public License as published by |
5e809f55 GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
c113de23 GM |
14 | |
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
5e809f55 | 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
c113de23 GM |
18 | ;; GNU General Public License for more details. |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
5e809f55 | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
c113de23 GM |
22 | |
23 | ;;; Commentary: | |
24 | ||
25 | ;;; Code: | |
26 | ||
b28ce55a | 27 | (eval-when-compile (require 'cl)) |
c113de23 | 28 | (require 'mail-parse) |
aa8f8277 | 29 | (autoload 'mailcap-extension-to-mime "mailcap") |
8abf1b22 GM |
30 | (autoload 'mm-body-7-or-8 "mm-bodies") |
31 | (autoload 'mm-long-lines-p "mm-bodies") | |
c113de23 | 32 | |
23f87bed | 33 | (defcustom mm-content-transfer-encoding-defaults |
c113de23 GM |
34 | '(("text/x-patch" 8bit) |
35 | ("text/.*" qp-or-base64) | |
36 | ("message/rfc822" 8bit) | |
23f87bed MB |
37 | ("application/emacs-lisp" qp-or-base64) |
38 | ("application/x-emacs-lisp" qp-or-base64) | |
39 | ("application/x-patch" qp-or-base64) | |
5be28abc | 40 | (".*" base64)) |
c113de23 GM |
41 | "Alist of regexps that match MIME types and their encodings. |
42 | If the encoding is `qp-or-base64', then either quoted-printable | |
23f87bed MB |
43 | or base64 will be used, depending on what is more efficient. |
44 | ||
45 | `qp-or-base64' has another effect. It will fold long lines so that | |
46 | MIME parts may not be broken by MTA. So do `quoted-printable' and | |
47 | `base64'. | |
48 | ||
49 | Note: It affects body encoding only when a part is a raw forwarded | |
50 | message (which will be made by `gnus-summary-mail-forward' with the | |
51 | arg 2 for example) or is neither the text/* type nor the message/* | |
52 | type. Even though in those cases, you can use the `encoding' MML tag | |
53 | to specify encoding of non-ASCII MIME parts." | |
54 | :type '(repeat (list (regexp :tag "MIME type") | |
55 | (choice :tag "encoding" | |
56 | (const 7bit) | |
57 | (const 8bit) | |
58 | (const qp-or-base64) | |
59 | (const quoted-printable) | |
60 | (const base64)))) | |
61 | :group 'mime) | |
c113de23 | 62 | |
54c72c31 KY |
63 | (defcustom mm-sign-option nil |
64 | "Option how to create signed parts. | |
65 | nil, use the default keys without asking; | |
66 | `guided', let you select signing keys from the menu." | |
029dda9c | 67 | :version "23.2" ;; No Gnus 0.12 |
54c72c31 KY |
68 | :type '(choice (item guided) |
69 | (item :tag "default" nil)) | |
70 | :group 'mime-security) | |
71 | ||
72 | (defcustom mm-encrypt-option nil | |
73 | "Option how to create encrypted parts. | |
74 | nil, use the default keys without asking; | |
75 | `guided', let you select recipients' keys from the menu." | |
029dda9c | 76 | :version "23.2" ;; No Gnus 0.12 |
54c72c31 KY |
77 | :type '(choice (item guided) |
78 | (item :tag "default" nil)) | |
79 | :group 'mime-security) | |
80 | ||
c113de23 GM |
81 | (defvar mm-use-ultra-safe-encoding nil |
82 | "If non-nil, use encodings aimed at Procrustean bed survival. | |
83 | ||
84 | This means that textual parts are encoded as quoted-printable if they | |
85 | contain lines longer than 76 characters or starting with \"From \" in | |
86 | the body. Non-7bit encodings (8bit, binary) are generally disallowed. | |
87 | This is to reduce the probability that a broken MTA or MDA changes the | |
88 | message. | |
89 | ||
90 | This variable should never be set directly, but bound before a call to | |
91 | `mml-generate-mime' or similar functions.") | |
92 | ||
93 | (defun mm-insert-rfc822-headers (charset encoding) | |
94 | "Insert text/plain headers with CHARSET and ENCODING." | |
95 | (insert "MIME-Version: 1.0\n") | |
96 | (insert "Content-Type: text/plain; charset=" | |
97 | (mail-quote-string (downcase (symbol-name charset))) "\n") | |
98 | (insert "Content-Transfer-Encoding: " | |
99 | (downcase (symbol-name encoding)) "\n")) | |
100 | ||
101 | (defun mm-insert-multipart-headers () | |
102 | "Insert multipart/mixed headers." | |
103 | (let ((boundary "=-=-=")) | |
104 | (insert "MIME-Version: 1.0\n") | |
105 | (insert "Content-Type: multipart/mixed; boundary=\"" boundary "\"\n") | |
106 | boundary)) | |
107 | ||
108 | (defun mm-default-file-encoding (file) | |
109 | "Return a default encoding for FILE." | |
110 | (if (not (string-match "\\.[^.]+$" file)) | |
111 | "application/octet-stream" | |
112 | (mailcap-extension-to-mime (match-string 0 file)))) | |
113 | ||
58a67d68 | 114 | (defun mm-safer-encoding (encoding &optional type) |
23f87bed | 115 | "Return an encoding similar to ENCODING but safer than it." |
c113de23 | 116 | (cond |
23f87bed | 117 | ((eq encoding '7bit) '7bit) ;; 7bit is considered safe. |
58a67d68 MB |
118 | ((memq encoding '(8bit quoted-printable)) |
119 | ;; According to RFC2046, 5.2.1, RFC822 Subtype, "quoted-printable" is not | |
120 | ;; a valid encoding for message/rfc822: | |
121 | ;; No encoding other than "7bit", "8bit", or "binary" is permitted for the | |
122 | ;; body of a "message/rfc822" entity. | |
123 | (if (string= type "message/rfc822") '8bit 'quoted-printable)) | |
8f688cb0 | 124 | ;; The remaining encodings are binary and base64 (and perhaps some |
c113de23 | 125 | ;; non-standard ones), which are both turned into base64. |
58a67d68 | 126 | (t (if (string= type "message/rfc822") 'binary 'base64)))) |
c113de23 GM |
127 | |
128 | (defun mm-encode-content-transfer-encoding (encoding &optional type) | |
23f87bed MB |
129 | "Encode the current buffer with ENCODING for MIME type TYPE. |
130 | ENCODING can be: nil (do nothing); one of `quoted-printable', `base64'; | |
131 | `7bit', `8bit' or `binary' (all do nothing); a function to do the encoding." | |
c113de23 GM |
132 | (cond |
133 | ((eq encoding 'quoted-printable) | |
23f87bed MB |
134 | ;; This used to try to make a multibyte buffer unibyte. That's |
135 | ;; completely wrong, since you'd get QP-encoded emacs-mule. If | |
136 | ;; this gets run on multibyte text it's an error that needs | |
137 | ;; fixing, and the encoding function will signal an error. | |
138 | ;; Likewise base64 below. | |
c113de23 GM |
139 | (quoted-printable-encode-region (point-min) (point-max) t)) |
140 | ((eq encoding 'base64) | |
4a2358e9 | 141 | (when (string-match "\\`text/" type) |
c113de23 GM |
142 | (goto-char (point-min)) |
143 | (while (search-forward "\n" nil t) | |
144 | (replace-match "\r\n" t t))) | |
23f87bed | 145 | (base64-encode-region (point-min) (point-max))) |
c113de23 GM |
146 | ((memq encoding '(7bit 8bit binary)) |
147 | ;; Do nothing. | |
148 | ) | |
149 | ((null encoding) | |
150 | ;; Do nothing. | |
151 | ) | |
23f87bed | 152 | ;; Fixme: Ignoring errors here looks bogus. |
c113de23 GM |
153 | ((functionp encoding) |
154 | (ignore-errors (funcall encoding (point-min) (point-max)))) | |
155 | (t | |
23f87bed | 156 | (error "Unknown encoding %s" encoding)))) |
c113de23 | 157 | |
de0bdfe7 KY |
158 | (defun mm-encode-buffer (type &optional encoding) |
159 | "Encode the buffer which contains data of MIME type TYPE by ENCODING. | |
23f87bed | 160 | TYPE is a string or a list of the components. |
de0bdfe7 KY |
161 | The optional ENCODING overrides the encoding determined according to |
162 | TYPE and `mm-content-transfer-encoding-defaults'. | |
c113de23 | 163 | The encoding used is returned." |
de0bdfe7 KY |
164 | (let ((mime-type (if (stringp type) type (car type)))) |
165 | (mm-encode-content-transfer-encoding | |
166 | (or encoding | |
167 | (setq encoding (or (and (listp type) | |
168 | (cadr (assq 'encoding type))) | |
169 | (mm-content-transfer-encoding mime-type)))) | |
170 | mime-type) | |
c113de23 GM |
171 | encoding)) |
172 | ||
173 | (defun mm-insert-headers (type encoding &optional file) | |
174 | "Insert headers for TYPE." | |
175 | (insert "Content-Type: " type) | |
176 | (when file | |
177 | (insert ";\n\tname=\"" (file-name-nondirectory file) "\"")) | |
178 | (insert "\n") | |
179 | (insert (format "Content-Transfer-Encoding: %s\n" encoding)) | |
180 | (insert "Content-Disposition: inline") | |
181 | (when file | |
182 | (insert ";\n\tfilename=\"" (file-name-nondirectory file) "\"")) | |
183 | (insert "\n") | |
184 | (insert "\n")) | |
185 | ||
186 | (defun mm-content-transfer-encoding (type) | |
187 | "Return a CTE suitable for TYPE to encode the current buffer." | |
188 | (let ((rules mm-content-transfer-encoding-defaults)) | |
189 | (catch 'found | |
190 | (while rules | |
191 | (when (string-match (caar rules) type) | |
192 | (throw 'found | |
a1506d29 | 193 | (let ((encoding |
c113de23 GM |
194 | (if (eq (cadr (car rules)) 'qp-or-base64) |
195 | (mm-qp-or-base64) | |
196 | (cadr (car rules))))) | |
197 | (if mm-use-ultra-safe-encoding | |
58a67d68 | 198 | (mm-safer-encoding encoding type) |
c113de23 GM |
199 | encoding)))) |
200 | (pop rules))))) | |
201 | ||
202 | (defun mm-qp-or-base64 () | |
23f87bed MB |
203 | "Return the type with which to encode the buffer. |
204 | This is either `base64' or `quoted-printable'." | |
205 | (if (equal mm-use-ultra-safe-encoding '(sign . "pgp")) | |
206 | ;; perhaps not always accurate? | |
207 | 'quoted-printable | |
208 | (save-excursion | |
209 | (let ((limit (min (point-max) (+ 2000 (point-min)))) | |
210 | (n8bit 0)) | |
211 | (goto-char (point-min)) | |
212 | (skip-chars-forward "\x20-\x7f\r\n\t" limit) | |
213 | (while (< (point) limit) | |
214 | (incf n8bit) | |
215 | (forward-char 1) | |
216 | (skip-chars-forward "\x20-\x7f\r\n\t" limit)) | |
217 | (if (or (< (* 6 n8bit) (- limit (point-min))) | |
218 | ;; Don't base64, say, a short line with a single | |
219 | ;; non-ASCII char when splitting parts by charset. | |
220 | (= n8bit 1)) | |
221 | 'quoted-printable | |
222 | 'base64))))) | |
c113de23 GM |
223 | |
224 | (provide 'mm-encode) | |
225 | ||
cbee283d | 226 | ;; arch-tag: 7d01bba4-d469-4851-952b-dc863f84ed66 |
c113de23 | 227 | ;;; mm-encode.el ends here |