1 ;;; mm-encode.el --- Functions for encoding MIME things
2 ;; Copyright (C) 1998, 1999, 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.
28 (require 'gnus-mailcap)
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 "Content-Type: multipart/mixed; boundary=\"" boundary "\"\n")
68 (defun mm-default-file-encoding (file)
69 "Return a default encoding for FILE."
70 (if (not (string-match "\\.[^.]+$" file))
71 "application/octet-stream"
72 (mailcap-extension-to-mime (match-string 0 file))))
74 (defun mm-safer-encoding (encoding)
75 "Return a safer but similar encoding."
77 ((memq encoding '(7bit 8bit quoted-printable)) 'quoted-printable)
78 ;; The remaing encodings are binary and base64 (and perhaps some
79 ;; non-standard ones), which are both turned into base64.
82 (defun mm-encode-content-transfer-encoding (encoding &optional type)
84 ((eq encoding 'quoted-printable)
85 (quoted-printable-encode-region (point-min) (point-max) t))
86 ((eq encoding 'base64)
87 (when (equal type "text/plain")
88 (goto-char (point-min))
89 (while (search-forward "\n" nil t)
90 (replace-match "\r\n" t t)))
92 (base64-encode-region (point-min) (point-max))
94 (message "Error while decoding: %s" error)
96 ((memq encoding '(7bit 8bit binary))
102 ((functionp encoding)
103 (ignore-errors (funcall encoding (point-min) (point-max))))
105 (message "Unknown encoding %s; defaulting to 8bit" encoding))))
107 (defun mm-encode-buffer (type)
108 "Encode the buffer which contains data of TYPE.
109 The encoding used is returned."
110 (let* ((mime-type (if (stringp type) type (car type)))
112 (or (and (listp type)
113 (cadr (assq 'encoding type)))
114 (mm-content-transfer-encoding mime-type)))
115 (bits (mm-body-7-or-8)))
116 ;; We force buffers that are 7bit to be unencoded, no matter
117 ;; what the preferred encoding is.
118 (when (eq bits '7bit)
119 (setq encoding bits))
120 (mm-encode-content-transfer-encoding encoding mime-type)
123 (defun mm-insert-headers (type encoding &optional file)
124 "Insert headers for TYPE."
125 (insert "Content-Type: " type)
127 (insert ";\n\tname=\"" (file-name-nondirectory file) "\""))
129 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
130 (insert "Content-Disposition: inline")
132 (insert ";\n\tfilename=\"" (file-name-nondirectory file) "\""))
136 (defun mm-content-transfer-encoding (type)
137 "Return a CTE suitable for TYPE to encode the current buffer."
138 (let ((rules mm-content-transfer-encoding-defaults))
141 (when (string-match (caar rules) type)
144 (if (eq (cadr (car rules)) 'qp-or-base64)
146 (cadr (car rules)))))
147 (if mm-use-ultra-safe-encoding
148 (mm-safer-encoding encoding)
152 (defun mm-qp-or-base64 ()
154 (let ((limit (min (point-max) (+ 2000 (point-min))))
156 (goto-char (point-min))
157 (skip-chars-forward "\x20-\x7f\r\n\t" limit)
158 (while (< (point) limit)
161 (skip-chars-forward "\x20-\x7f\r\n\t" limit))
162 (if (< (* 6 n8bit) (- limit (point-min)))
168 ;;; mm-encode.el ends here