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