1 ;;; mm-encode.el --- Functions for encoding MIME things
2 ;; Copyright (C) 1998,99 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 (defun mm-insert-rfc822-headers (charset encoding)
42 "Insert text/plain headers with CHARSET and ENCODING."
43 (insert "MIME-Version: 1.0\n")
44 (insert "Content-Type: text/plain; charset="
45 (mail-quote-string (downcase (symbol-name charset))) "\n")
46 (insert "Content-Transfer-Encoding: "
47 (downcase (symbol-name encoding)) "\n"))
49 (defun mm-insert-multipart-headers ()
50 "Insert multipart/mixed headers."
51 (let ((boundary "=-=-="))
52 (insert "MIME-Version: 1.0\n")
53 (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
57 (defun mm-default-file-encoding (file)
58 "Return a default encoding for FILE."
59 (if (not (string-match "\\.[^.]+$" file))
60 "application/octet-stream"
61 (mailcap-extension-to-mime (match-string 0 file))))
63 (defun mm-encode-content-transfer-encoding (encoding &optional type)
65 ((eq encoding 'quoted-printable)
66 (quoted-printable-encode-region (point-min) (point-max) t))
67 ((eq encoding 'base64)
68 (when (equal type "text/plain")
69 (goto-char (point-min))
70 (while (search-forward "\n" nil t)
71 (replace-match "\r\n" t t)))
73 (base64-encode-region (point-min) (point-max))
75 (message "Error while decoding: %s" error)
77 ((memq encoding '(7bit 8bit binary))
84 (ignore-errors (funcall encoding (point-min) (point-max))))
86 (message "Unknown encoding %s; defaulting to 8bit" encoding))))
88 (defun mm-encode-buffer (type)
89 "Encode the buffer which contains data of TYPE.
90 The encoding used is returned."
91 (let* ((mime-type (if (stringp type) type (car type)))
94 (cadr (assq 'encoding type)))
95 (mm-content-transfer-encoding mime-type)))
96 (bits (mm-body-7-or-8)))
97 ;; We force buffers that are 7bit to be unencoded, no matter
98 ;; what the preferred encoding is.
100 (setq encoding bits))
101 (mm-encode-content-transfer-encoding encoding mime-type)
104 (defun mm-insert-headers (type encoding &optional file)
105 "Insert headers for TYPE."
106 (insert "Content-Type: " type)
108 (insert ";\n\tname=\"" (file-name-nondirectory file) "\""))
110 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
111 (insert "Content-Disposition: inline")
113 (insert ";\n\tfilename=\"" (file-name-nondirectory file) "\""))
117 (defun mm-content-transfer-encoding (type)
118 "Return a CTE suitable for TYPE to encode the current buffer."
119 (let ((rules mm-content-transfer-encoding-defaults))
122 (when (string-match (caar rules) type)
124 (if (eq (cadar rules) 'qp-or-base64)
129 (defun mm-qp-or-base64 ()
131 (let ((limit (min (point-max) (+ 2000 (point-min))))
133 (goto-char (point-min))
134 (skip-chars-forward "\x20-\x7f\r\n\t" limit)
135 (while (< (point) limit)
138 (skip-chars-forward "\x20-\x7f\r\n\t" limit))
139 (if (< (* 6 n8bit) (- limit (point-min)))
145 ;;; mm-encode.el ends here