1 ;;; mm-encode.el --- Functions for encoding MIME things
2 ;; Copyright (C) 1998-2000 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
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)
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.
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.
30 (defvar mm-content-transfer-encoding-defaults
31 '(("text/x-patch" 8bit)
32 ("text/.*" qp-or-base64)
33 ("message/rfc822" 8bit)
34 ("application/emacs-lisp" 8bit)
35 ("application/x-patch" 8bit)
37 "Alist of regexps that match MIME types and their encodings.
38 If the encoding is `qp-or-base64', then either quoted-printable
39 or base64 will be used, depending on what is more efficient.")
41 (defvar mm-use-ultra-safe-encoding nil
42 "If non-nil, use encodings aimed at Procrustean bed survival.
44 This means that textual parts are encoded as quoted-printable if they
45 contain lines longer than 76 characters or starting with \"From \" in
46 the body. Non-7bit encodings (8bit, binary) are generally disallowed.
47 This is to reduce the probability that a broken MTA or MDA changes the
50 This variable should never be set directly, but bound before a call to
51 `mml-generate-mime' or similar functions.")
53 (defun mm-insert-rfc822-headers (charset encoding)
54 "Insert text/plain headers with CHARSET and ENCODING."
55 (insert "MIME-Version: 1.0\n")
56 (insert "Content-Type: text/plain; charset="
57 (mail-quote-string (downcase (symbol-name charset))) "\n")
58 (insert "Content-Transfer-Encoding: "
59 (downcase (symbol-name encoding)) "\n"))
61 (defun mm-insert-multipart-headers ()
62 "Insert multipart/mixed headers."
63 (let ((boundary "=-=-="))
64 (insert "MIME-Version: 1.0\n")
65 (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
69 (defun mm-default-file-encoding (file)
70 "Return a default encoding for FILE."
71 (if (not (string-match "\\.[^.]+$" file))
72 "application/octet-stream"
73 (mailcap-extension-to-mime (match-string 0 file))))
75 (defun mm-safer-encoding (encoding)
76 "Return a safer but similar encoding."
78 ((memq encoding '(7bit 8bit quoted-printable)) 'quoted-printable)
79 ;; The remaing encodings are binary and base64 (and perhaps some
80 ;; non-standard ones), which are both turned into base64.
83 (defun mm-encode-content-transfer-encoding (encoding &optional type)
85 ((eq encoding 'quoted-printable)
86 (quoted-printable-encode-region (point-min) (point-max) t))
87 ((eq encoding 'base64)
88 (when (equal type "text/plain")
89 (goto-char (point-min))
90 (while (search-forward "\n" nil t)
91 (replace-match "\r\n" t t)))
93 (base64-encode-region (point-min) (point-max))
95 (message "Error while decoding: %s" error)
97 ((memq encoding '(7bit 8bit binary))
103 ((functionp encoding)
104 (ignore-errors (funcall encoding (point-min) (point-max))))
106 (message "Unknown encoding %s; defaulting to 8bit" encoding))))
108 (defun mm-encode-buffer (type)
109 "Encode the buffer which contains data of TYPE.
110 The encoding used is returned."
111 (let* ((mime-type (if (stringp type) type (car type)))
113 (or (and (listp type)
114 (cadr (assq 'encoding type)))
115 (mm-content-transfer-encoding mime-type)))
116 (bits (mm-body-7-or-8)))
117 ;; We force buffers that are 7bit to be unencoded, no matter
118 ;; what the preferred encoding is.
119 (when (eq bits '7bit)
120 (setq encoding bits))
121 (mm-encode-content-transfer-encoding encoding mime-type)
124 (defun mm-insert-headers (type encoding &optional file)
125 "Insert headers for TYPE."
126 (insert "Content-Type: " type)
128 (insert ";\n\tname=\"" (file-name-nondirectory file) "\""))
130 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
131 (insert "Content-Disposition: inline")
133 (insert ";\n\tfilename=\"" (file-name-nondirectory file) "\""))
137 (defun mm-content-transfer-encoding (type)
138 "Return a CTE suitable for TYPE to encode the current buffer."
139 (let ((rules mm-content-transfer-encoding-defaults))
142 (when (string-match (caar rules) type)
145 (if (eq (cadar rules) 'qp-or-base64)
148 (if mm-use-ultra-safe-encoding
149 (mm-safer-encoding encoding)
153 (defun mm-qp-or-base64 ()
155 (let ((limit (min (point-max) (+ 2000 (point-min))))
157 (goto-char (point-min))
158 (skip-chars-forward "\x20-\x7f\r\n\t" limit)
159 (while (< (point) limit)
162 (skip-chars-forward "\x20-\x7f\r\n\t" limit))
163 (if (< (* 6 n8bit) (- limit (point-min)))
169 ;;; mm-encode.el ends here