1 ;;; mm-encode.el --- Functions for encoding MIME things
2 ;; Copyright (C) 1998 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.
28 (require 'gnus-mailcap)
30 (defvar mm-content-transfer-encoding-defaults
31 '(("text/.*" quoted-printable)
33 "Alist of regexps that match MIME types and their encodings.")
35 (defun mm-insert-rfc822-headers (charset encoding)
36 "Insert text/plain headers with CHARSET and ENCODING."
37 (insert "MIME-Version: 1.0\n")
38 (insert "Content-Type: text/plain; charset="
39 (mail-quote-string (downcase (symbol-name charset))) "\n")
40 (insert "Content-Transfer-Encoding: "
41 (downcase (symbol-name encoding)) "\n"))
43 (defun mm-insert-multipart-headers ()
44 "Insert multipart/mixed headers."
45 (let ((boundary "=-=-="))
46 (insert "MIME-Version: 1.0\n")
47 (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
51 (defun mm-default-file-encoding (file)
52 "Return a default encoding for FILE."
53 (if (not (string-match "\\.[^.]+$" file))
54 "application/octet-stream"
55 (mailcap-extension-to-mime (match-string 0 file))))
57 (defun mm-encode-content-transfer-encoding (encoding &optional type)
59 ((eq encoding 'quoted-printable)
60 (quoted-printable-encode-region (point-min) (point-max)))
61 ((eq encoding 'base64)
62 (when (equal type "text/plain")
63 (goto-char (point-min))
64 (while (search-forward "\n" nil t)
65 (replace-match "\r\n" t t)))
67 (base64-encode-region (point-min) (point-max))
69 ((memq encoding '(7bit 8bit binary))
73 ;;((eq encoding 'x-uuencode)
75 ;; (uudecode-encode-region (point-min) (point-max))
79 (funcall encoding (point-min) (point-max))
82 (message "Unknown encoding %s; defaulting to 8bit" encoding))))
84 (defun mm-encode-buffer (type)
85 "Encode the buffer which contains data of TYPE.
86 The encoding used is returned."
87 (let* ((mime-type (if (stringp type) type (car type)))
90 (cadr (assq 'encoding type)))
91 (mm-content-transfer-encoding mime-type))))
92 (mm-encode-content-transfer-encoding encoding mime-type)
95 (defun mm-insert-headers (type encoding &optional file)
96 "Insert headers for TYPE."
97 (insert "Content-Type: " type)
99 (insert ";\n\tname=\"" (file-name-nondirectory file) "\""))
101 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
102 (insert "Content-Disposition: inline")
104 (insert ";\n\tfilename=\"" (file-name-nondirectory file) "\""))
108 (defun mm-content-transfer-encoding (type)
109 "Return a CTE suitable for TYPE."
110 (let ((rules mm-content-transfer-encoding-defaults))
113 (when (string-match (caar rules) type)
114 (throw 'found (cadar rules)))
119 ;;; mm-encode.el ends here