Fix doc.
[gnus] / lisp / mm-encode.el
1 ;;; mm-encode.el --- Functions for encoding MIME things
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
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
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
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
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28 (require 'mail-parse)
29 (require 'mailcap)
30 (autoload 'mm-body-7-or-8 "mm-bodies")
31 (autoload 'mm-long-lines-p "mm-bodies")
32
33 (defcustom mm-content-transfer-encoding-defaults
34   '(("text/x-patch" 8bit)
35     ("text/.*" qp-or-base64)
36     ("message/rfc822" 8bit)
37     ("application/emacs-lisp" qp-or-base64)
38     ("application/x-emacs-lisp" qp-or-base64)
39     ("application/x-patch" qp-or-base64)
40     (".*" base64))
41   "Alist of regexps that match MIME types and their encodings.
42 If the encoding is `qp-or-base64', then either quoted-printable
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)
62
63 (defvar mm-use-ultra-safe-encoding nil
64   "If non-nil, use encodings aimed at Procrustean bed survival.
65
66 This means that textual parts are encoded as quoted-printable if they
67 contain lines longer than 76 characters or starting with \"From \" in
68 the body.  Non-7bit encodings (8bit, binary) are generally disallowed.
69 This is to reduce the probability that a broken MTA or MDA changes the
70 message.
71
72 This variable should never be set directly, but bound before a call to
73 `mml-generate-mime' or similar functions.")
74
75 (defun mm-insert-rfc822-headers (charset encoding)
76   "Insert text/plain headers with CHARSET and ENCODING."
77   (insert "MIME-Version: 1.0\n")
78   (insert "Content-Type: text/plain; charset="
79           (mail-quote-string (downcase (symbol-name charset))) "\n")
80   (insert "Content-Transfer-Encoding: "
81           (downcase (symbol-name encoding)) "\n"))
82
83 (defun mm-insert-multipart-headers ()
84   "Insert multipart/mixed headers."
85   (let ((boundary "=-=-="))
86     (insert "MIME-Version: 1.0\n")
87     (insert "Content-Type: multipart/mixed; boundary=\"" boundary "\"\n")
88     boundary))
89
90 (defun mm-default-file-encoding (file)
91   "Return a default encoding for FILE."
92   (if (not (string-match "\\.[^.]+$" file))
93       "application/octet-stream"
94     (mailcap-extension-to-mime (match-string 0 file))))
95
96 (defun mm-safer-encoding (encoding &optional type)
97   "Return an encoding similar to ENCODING but safer than it."
98   (cond
99    ((eq encoding '7bit) '7bit) ;; 7bit is considered safe.
100    ((memq encoding '(8bit quoted-printable))
101     ;; According to RFC2046, 5.2.1, RFC822 Subtype, "quoted-printable" is not
102     ;; a valid encoding for message/rfc822:
103     ;; No encoding other than "7bit", "8bit", or "binary" is permitted for the
104     ;; body of a "message/rfc822" entity.
105     (if (string= type "message/rfc822") '8bit 'quoted-printable))
106    ;; The remaining encodings are binary and base64 (and perhaps some
107    ;; non-standard ones), which are both turned into base64.
108    (t (if (string= type "message/rfc822") 'binary 'base64))))
109
110 (defun mm-encode-content-transfer-encoding (encoding &optional type)
111   "Encode the current buffer with ENCODING for MIME type TYPE.
112 ENCODING can be: nil (do nothing); one of `quoted-printable', `base64';
113 `7bit', `8bit' or `binary' (all do nothing); a function to do the encoding."
114   (cond
115    ((eq encoding 'quoted-printable)
116     ;; This used to try to make a multibyte buffer unibyte.  That's
117     ;; completely wrong, since you'd get QP-encoded emacs-mule.  If
118     ;; this gets run on multibyte text it's an error that needs
119     ;; fixing, and the encoding function will signal an error.
120     ;; Likewise base64 below.
121     (quoted-printable-encode-region (point-min) (point-max) t))
122    ((eq encoding 'base64)
123     (when (string-match "\\`text/" type)
124       (goto-char (point-min))
125       (while (search-forward "\n" nil t)
126         (replace-match "\r\n" t t)))
127     (base64-encode-region (point-min) (point-max)))
128    ((memq encoding '(7bit 8bit binary))
129     ;; Do nothing.
130     )
131    ((null encoding)
132     ;; Do nothing.
133     )
134    ;; Fixme: Ignoring errors here looks bogus.
135    ((functionp encoding)
136     (ignore-errors (funcall encoding (point-min) (point-max))))
137    (t
138     (error "Unknown encoding %s" encoding))))
139
140 (defun mm-encode-buffer (type &optional encoding)
141   "Encode the buffer which contains data of MIME type TYPE by ENCODING.
142 TYPE is a string or a list of the components.
143 The optional ENCODING overrides the encoding determined according to
144 TYPE and `mm-content-transfer-encoding-defaults'.
145 The encoding used is returned."
146   (let ((mime-type (if (stringp type) type (car type))))
147     (mm-encode-content-transfer-encoding
148      (or encoding
149          (setq encoding (or (and (listp type)
150                                  (cadr (assq 'encoding type)))
151                             (mm-content-transfer-encoding mime-type))))
152      mime-type)
153     encoding))
154
155 (defun mm-insert-headers (type encoding &optional file)
156   "Insert headers for TYPE."
157   (insert "Content-Type: " type)
158   (when file
159     (insert ";\n\tname=\"" (file-name-nondirectory file) "\""))
160   (insert "\n")
161   (insert (format "Content-Transfer-Encoding: %s\n" encoding))
162   (insert "Content-Disposition: inline")
163   (when file
164     (insert ";\n\tfilename=\"" (file-name-nondirectory file) "\""))
165   (insert "\n")
166   (insert "\n"))
167
168 (defun mm-content-transfer-encoding (type)
169   "Return a CTE suitable for TYPE to encode the current buffer."
170   (let ((rules mm-content-transfer-encoding-defaults))
171     (catch 'found
172       (while rules
173         (when (string-match (caar rules) type)
174           (throw 'found
175                  (let ((encoding
176                         (if (eq (cadr (car rules)) 'qp-or-base64)
177                             (mm-qp-or-base64)
178                           (cadr (car rules)))))
179                    (if mm-use-ultra-safe-encoding
180                        (mm-safer-encoding encoding type)
181                      encoding))))
182         (pop rules)))))
183
184 (defun mm-qp-or-base64 ()
185   "Return the type with which to encode the buffer.
186 This is either `base64' or `quoted-printable'."
187   (if (equal mm-use-ultra-safe-encoding '(sign . "pgp"))
188       ;; perhaps not always accurate?
189       'quoted-printable
190     (save-excursion
191       (let ((limit (min (point-max) (+ 2000 (point-min))))
192             (n8bit 0))
193         (goto-char (point-min))
194         (skip-chars-forward "\x20-\x7f\r\n\t" limit)
195         (while (< (point) limit)
196           (incf n8bit)
197           (forward-char 1)
198           (skip-chars-forward "\x20-\x7f\r\n\t" limit))
199         (if (or (< (* 6 n8bit) (- limit (point-min)))
200                 ;; Don't base64, say, a short line with a single
201                 ;; non-ASCII char when splitting parts by charset.
202                 (= n8bit 1))
203             'quoted-printable
204           'base64)))))
205
206 (provide 'mm-encode)
207
208 ;; arch-tag: 7d01bba4-d469-4851-952b-dc863f84ed66
209 ;;; mm-encode.el ends here