1 ;;; mm-encode.el --- Functions for encoding MIME things
2 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; This file is part of GNU Emacs.
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)
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.
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.
28 (eval-when-compile (require 'cl))
32 (autoload 'mm-body-7-or-8 "mm-bodies"))
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)
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"
50 (const quoted-printable)
54 (defvar mm-use-ultra-safe-encoding nil
55 "If non-nil, use encodings aimed at Procrustean bed survival.
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
63 This variable should never be set directly, but bound before a call to
64 `mml-generate-mime' or similar functions.")
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"))
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")
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))))
87 (defun mm-safer-encoding (encoding)
88 "Return a safer but similar encoding."
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.
95 (defun mm-encode-content-transfer-encoding (encoding &optional type)
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))
108 (message "Error while decoding: %s" error)
110 ((memq encoding '(7bit 8bit binary))
116 ((functionp encoding)
117 (ignore-errors (funcall encoding (point-min) (point-max))))
119 (message "Unknown encoding %s; treating it as 8bit" encoding))))
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)))
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)
138 (defun mm-insert-headers (type encoding &optional file)
139 "Insert headers for TYPE."
140 (insert "Content-Type: " type)
142 (insert ";\n\tname=\"" (file-name-nondirectory file) "\""))
144 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
145 (insert "Content-Disposition: inline")
147 (insert ";\n\tfilename=\"" (file-name-nondirectory file) "\""))
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))
156 (when (string-match (caar rules) type)
159 (if (eq (cadr (car rules)) 'qp-or-base64)
161 (cadr (car rules)))))
162 (if mm-use-ultra-safe-encoding
163 (mm-safer-encoding encoding)
167 (defun mm-qp-or-base64 ()
168 (if (equal mm-use-ultra-safe-encoding '(sign . "pgp"))
169 ;; perhaps not always accurate?
172 (let ((limit (min (point-max) (+ 2000 (point-min))))
174 (goto-char (point-min))
175 (skip-chars-forward "\x20-\x7f\r\n\t" limit)
176 (while (< (point) limit)
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.
189 ;;; mm-encode.el ends here