Import Oort Gnus v0.18.
[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
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 'mailcap)
31 (eval-and-compile
32   (autoload 'mm-body-7-or-8 "mm-bodies"))
33
34 (defcustom mm-content-transfer-encoding-defaults
35   '(("text/x-patch" 8bit)
36     ("text/.*" qp-or-base64)
37     ("message/rfc822" 8bit)
38     ("application/emacs-lisp" 8bit)
39     ("application/x-emacs-lisp" 8bit)
40     ("application/x-patch" 8bit)
41     (".*" base64))
42   "Alist of regexps that match MIME types and their encodings.
43 If the encoding is `qp-or-base64', then either quoted-printable
44 or base64 will be used, depending on what is more efficient."
45   :type '(repeat (list (regexp :tag "MIME type")
46                        (choice :tag "encoding"
47                                (const 7bit)
48                                (const 8bit)
49                                (const qp-or-base64)
50                                (const quoted-printable)
51                                (const base64))))
52   :group 'mime)
53
54 (defvar mm-use-ultra-safe-encoding nil
55   "If non-nil, use encodings aimed at Procrustean bed survival.
56
57 This means that textual parts are encoded as quoted-printable if they
58 contain lines longer than 76 characters or starting with \"From \" in
59 the body.  Non-7bit encodings (8bit, binary) are generally disallowed.
60 This is to reduce the probability that a broken MTA or MDA changes the
61 message.
62
63 This variable should never be set directly, but bound before a call to
64 `mml-generate-mime' or similar functions.")
65
66 (defun mm-insert-rfc822-headers (charset encoding)
67   "Insert text/plain headers with CHARSET and ENCODING."
68   (insert "MIME-Version: 1.0\n")
69   (insert "Content-Type: text/plain; charset="
70           (mail-quote-string (downcase (symbol-name charset))) "\n")
71   (insert "Content-Transfer-Encoding: "
72           (downcase (symbol-name encoding)) "\n"))
73
74 (defun mm-insert-multipart-headers ()
75   "Insert multipart/mixed headers."
76   (let ((boundary "=-=-="))
77     (insert "MIME-Version: 1.0\n")
78     (insert "Content-Type: multipart/mixed; boundary=\"" boundary "\"\n")
79     boundary))
80
81 (defun mm-default-file-encoding (file)
82   "Return a default encoding for FILE."
83   (if (not (string-match "\\.[^.]+$" file))
84       "application/octet-stream"
85     (mailcap-extension-to-mime (match-string 0 file))))
86
87 (defun mm-safer-encoding (encoding)
88   "Return a safer but similar encoding."
89   (cond
90    ((memq encoding '(7bit 8bit quoted-printable)) 'quoted-printable)
91    ;; The remaining encodings are binary and base64 (and perhaps some
92    ;; non-standard ones), which are both turned into base64.
93    (t 'base64)))
94
95 (defun mm-encode-content-transfer-encoding (encoding &optional type)
96   (cond
97    ((eq encoding 'quoted-printable)
98     (mm-with-unibyte-current-buffer-mule4
99       (quoted-printable-encode-region (point-min) (point-max) t)))
100    ((eq encoding 'base64)
101     (when (equal type "text/plain")
102       (goto-char (point-min))
103       (while (search-forward "\n" nil t)
104         (replace-match "\r\n" t t)))
105     (condition-case error
106         (base64-encode-region (point-min) (point-max))
107       (error
108        (message "Error while decoding: %s" error)
109        nil)))
110    ((memq encoding '(7bit 8bit binary))
111     ;; Do nothing.
112     )
113    ((null encoding)
114     ;; Do nothing.
115     )
116    ((functionp encoding)
117     (ignore-errors (funcall encoding (point-min) (point-max))))
118    (t
119     (message "Unknown encoding %s; treating it as 8bit" encoding))))
120
121 (defun mm-encode-buffer (type)
122   "Encode the buffer which contains data of TYPE.
123 The encoding used is returned."
124   (let* ((mime-type (if (stringp type) type (car type)))
125          (encoding
126           (or (and (listp type)
127                    (cadr (assq 'encoding type)))
128               (mm-content-transfer-encoding mime-type)))
129          (bits (mm-body-7-or-8)))
130     ;; We force buffers that are 7bit to be unencoded, no matter
131     ;; what the preferred encoding is.
132     ;; Only if the buffers don't contain lone lines.
133     (when (and (eq bits '7bit) (not (mm-long-lines-p 76)))
134       (setq encoding bits))
135     (mm-encode-content-transfer-encoding encoding mime-type)
136     encoding))
137
138 (defun mm-insert-headers (type encoding &optional file)
139   "Insert headers for TYPE."
140   (insert "Content-Type: " type)
141   (when file
142     (insert ";\n\tname=\"" (file-name-nondirectory file) "\""))
143   (insert "\n")
144   (insert (format "Content-Transfer-Encoding: %s\n" encoding))
145   (insert "Content-Disposition: inline")
146   (when file
147     (insert ";\n\tfilename=\"" (file-name-nondirectory file) "\""))
148   (insert "\n")
149   (insert "\n"))
150
151 (defun mm-content-transfer-encoding (type)
152   "Return a CTE suitable for TYPE to encode the current buffer."
153   (let ((rules mm-content-transfer-encoding-defaults))
154     (catch 'found
155       (while rules
156         (when (string-match (caar rules) type)
157           (throw 'found
158                  (let ((encoding
159                         (if (eq (cadr (car rules)) 'qp-or-base64)
160                             (mm-qp-or-base64)
161                           (cadr (car rules)))))
162                    (if mm-use-ultra-safe-encoding
163                        (mm-safer-encoding encoding)
164                      encoding))))
165         (pop rules)))))
166
167 (defun mm-qp-or-base64 ()
168   (if (equal mm-use-ultra-safe-encoding '(sign . "pgp"))
169       ;; perhaps not always accurate?
170       'quoted-printable
171     (save-excursion
172       (let ((limit (min (point-max) (+ 2000 (point-min))))
173             (n8bit 0))
174         (goto-char (point-min))
175         (skip-chars-forward "\x20-\x7f\r\n\t" limit)
176         (while (< (point) limit)
177           (incf n8bit)
178           (forward-char 1)
179           (skip-chars-forward "\x20-\x7f\r\n\t" limit))
180         (if (or (< (* 6 n8bit) (- limit (point-min)))
181                 ;; Don't base64, say, a short line with a single
182                 ;; non-ASCII char when splitting parts by charset.
183                 (= n8bit 1))
184             'quoted-printable
185           'base64)))))
186
187 (provide 'mm-encode)
188
189 ;;; mm-encode.el ends here