Import Oort Gnus v0.09.
[elisp/gnus.git-] / lisp / mm-encode.el
1 ;;; mm-encode.el --- Functions for encoding MIME things
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28 (require 'mail-parse)
29 (require 'mailcap)
30 (eval-and-compile
31   (autoload 'mm-body-7-or-8 "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" 8bit)
38     ("application/x-emacs-lisp" 8bit)
39     ("application/x-patch" 8bit)
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   :type '(repeat (list (regexp :tag "MIME type")
45                        (choice :tag "encoding"
46                                (const 7bit)
47                                (const 8bit)
48                                (const qp-or-base64)
49                                (const quoted-printable)
50                                (const base64))))
51   :group 'mime)
52
53 (defvar mm-use-ultra-safe-encoding nil
54   "If non-nil, use encodings aimed at Procrustean bed survival.
55
56 This means that textual parts are encoded as quoted-printable if they
57 contain lines longer than 76 characters or starting with \"From \" in
58 the body.  Non-7bit encodings (8bit, binary) are generally disallowed.
59 This is to reduce the probability that a broken MTA or MDA changes the
60 message.
61
62 This variable should never be set directly, but bound before a call to
63 `mml-generate-mime' or similar functions.")
64
65 (defun mm-insert-rfc822-headers (charset encoding)
66   "Insert text/plain headers with CHARSET and ENCODING."
67   (insert "MIME-Version: 1.0\n")
68   (insert "Content-Type: text/plain; charset="
69           (mail-quote-string (downcase (symbol-name charset))) "\n")
70   (insert "Content-Transfer-Encoding: "
71           (downcase (symbol-name encoding)) "\n"))
72
73 (defun mm-insert-multipart-headers ()
74   "Insert multipart/mixed headers."
75   (let ((boundary "=-=-="))
76     (insert "MIME-Version: 1.0\n")
77     (insert "Content-Type: multipart/mixed; boundary=\"" boundary "\"\n")
78     boundary))
79
80 (defun mm-default-file-encoding (file)
81   "Return a default encoding for FILE."
82   (if (not (string-match "\\.[^.]+$" file))
83       "application/octet-stream"
84     (mailcap-extension-to-mime (match-string 0 file))))
85
86 (defun mm-safer-encoding (encoding)
87   "Return a safer but similar encoding."
88   (cond
89    ((memq encoding '(7bit 8bit quoted-printable)) 'quoted-printable)
90    ;; The remaining encodings are binary and base64 (and perhaps some
91    ;; non-standard ones), which are both turned into base64.
92    (t 'base64)))
93
94 (defun mm-encode-content-transfer-encoding (encoding &optional type)
95   (cond
96    ((eq encoding 'quoted-printable)
97     (mm-with-unibyte-current-buffer-mule4
98       (quoted-printable-encode-region (point-min) (point-max) t)))
99    ((eq encoding 'base64)
100     (when (equal type "text/plain")
101       (goto-char (point-min))
102       (while (search-forward "\n" nil t)
103         (replace-match "\r\n" t t)))
104     (condition-case error
105         (base64-encode-region (point-min) (point-max))
106       (error
107        (message "Error while decoding: %s" error)
108        nil)))
109    ((memq encoding '(7bit 8bit binary))
110     ;; Do nothing.
111     )
112    ((null encoding)
113     ;; Do nothing.
114     )
115    ((functionp encoding)
116     (ignore-errors (funcall encoding (point-min) (point-max))))
117    (t
118     (message "Unknown encoding %s; treating it as 8bit" encoding))))
119
120 (defun mm-encode-buffer (type)
121   "Encode the buffer which contains data of TYPE.
122 The encoding used is returned."
123   (let* ((mime-type (if (stringp type) type (car type)))
124          (encoding
125           (or (and (listp type)
126                    (cadr (assq 'encoding type)))
127               (mm-content-transfer-encoding mime-type)))
128          (bits (mm-body-7-or-8)))
129     ;; We force buffers that are 7bit to be unencoded, no matter
130     ;; what the preferred encoding is.
131     ;; Only if the buffers don't contain lone lines.
132     (when (and (eq bits '7bit) (not (mm-long-lines-p 76)))
133       (setq encoding bits))
134     (mm-encode-content-transfer-encoding encoding mime-type)
135     encoding))
136
137 (defun mm-insert-headers (type encoding &optional file)
138   "Insert headers for TYPE."
139   (insert "Content-Type: " type)
140   (when file
141     (insert ";\n\tname=\"" (file-name-nondirectory file) "\""))
142   (insert "\n")
143   (insert (format "Content-Transfer-Encoding: %s\n" encoding))
144   (insert "Content-Disposition: inline")
145   (when file
146     (insert ";\n\tfilename=\"" (file-name-nondirectory file) "\""))
147   (insert "\n")
148   (insert "\n"))
149
150 (defun mm-content-transfer-encoding (type)
151   "Return a CTE suitable for TYPE to encode the current buffer."
152   (let ((rules mm-content-transfer-encoding-defaults))
153     (catch 'found
154       (while rules
155         (when (string-match (caar rules) type)
156           (throw 'found
157                  (let ((encoding
158                         (if (eq (cadr (car rules)) 'qp-or-base64)
159                             (mm-qp-or-base64)
160                           (cadr (car rules)))))
161                    (if mm-use-ultra-safe-encoding
162                        (mm-safer-encoding encoding)
163                      encoding))))
164         (pop rules)))))
165
166 (defun mm-qp-or-base64 ()
167   (save-excursion
168     (let ((limit (min (point-max) (+ 2000 (point-min))))
169           (n8bit 0))
170       (goto-char (point-min))
171       (skip-chars-forward "\x20-\x7f\r\n\t" limit)
172       (while (< (point) limit)
173         (incf n8bit)
174         (forward-char 1)
175         (skip-chars-forward "\x20-\x7f\r\n\t" limit))
176       (if (or (< (* 6 n8bit) (- limit (point-min)))
177               ;; Don't base64, say, a short line with a single
178               ;; non-ASCII char when splitting parts by charset.
179               (= n8bit 1))
180           'quoted-printable
181         'base64))))
182
183 (provide 'mm-encode)
184
185 ;;; mm-encode.el ends here