Convert consecutive FSF copyright years to ranges.
[gnus] / lisp / mm-encode.el
1 ;;; mm-encode.el --- Functions for encoding MIME things
2
3 ;; Copyright (C) 1998-2011 Free Software Foundation, Inc.
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
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.
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
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
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/>.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
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")
31
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.
43
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)
54
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)
63
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)
72
73 (defvar mm-use-ultra-safe-encoding nil
74   "If non-nil, use encodings aimed at Procrustean bed survival.
75
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.
81
82 This 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
106 (defun mm-safer-encoding (encoding &optional type)
107   "Return an encoding similar to ENCODING but safer than it."
108   (cond
109    ((eq encoding '7bit) '7bit) ;; 7bit is considered safe.
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))
116    ;; The remaining encodings are binary and base64 (and perhaps some
117    ;; non-standard ones), which are both turned into base64.
118    (t (if (string= type "message/rfc822") 'binary 'base64))))
119
120 (defun mm-encode-content-transfer-encoding (encoding &optional type)
121   "Encode the current buffer with ENCODING for MIME type TYPE.
122 ENCODING can be: nil (do nothing); one of `quoted-printable', `base64';
123 `7bit', `8bit' or `binary' (all do nothing); a function to do the encoding."
124   (cond
125    ((eq encoding 'quoted-printable)
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.
131     (quoted-printable-encode-region (point-min) (point-max) t))
132    ((eq encoding 'base64)
133     (when (string-match "\\`text/" type)
134       (goto-char (point-min))
135       (while (search-forward "\n" nil t)
136         (replace-match "\r\n" t t)))
137     (base64-encode-region (point-min) (point-max)))
138    ((memq encoding '(7bit 8bit binary))
139     ;; Do nothing.
140     )
141    ((null encoding)
142     ;; Do nothing.
143     )
144    ;; Fixme: Ignoring errors here looks bogus.
145    ((functionp encoding)
146     (ignore-errors (funcall encoding (point-min) (point-max))))
147    (t
148     (error "Unknown encoding %s" encoding))))
149
150 (defun mm-encode-buffer (type &optional encoding)
151   "Encode the buffer which contains data of MIME type TYPE by ENCODING.
152 TYPE is a string or a list of the components.
153 The optional ENCODING overrides the encoding determined according to
154 TYPE and `mm-content-transfer-encoding-defaults'.
155 The encoding used is returned."
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)
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
185                  (let ((encoding
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
190                        (mm-safer-encoding encoding type)
191                      encoding))))
192         (pop rules)))))
193
194 (defun mm-qp-or-base64 ()
195   "Return the type with which to encode the buffer.
196 This 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)))))
215
216 (provide 'mm-encode)
217
218 ;;; mm-encode.el ends here