31717
|
1 ;;; mm-encode.el --- Functions for encoding MIME things
|
|
2 ;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
|
|
3
|
|
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
|
|
5 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
|
|
6 ;; This file is part of GNU Emacs.
|
|
7
|
|
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)
|
|
11 ;; any later version.
|
|
12
|
|
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.
|
|
17
|
|
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.
|
|
22
|
|
23 ;;; Commentary:
|
|
24
|
|
25 ;;; Code:
|
|
26
|
|
27 (require 'mail-parse)
|
|
28 (require 'mailcap)
|
|
29
|
|
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)
|
|
36 (".*" qp-or-base64))
|
|
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.")
|
|
40
|
|
41 (defvar mm-use-ultra-safe-encoding nil
|
|
42 "If non-nil, use encodings aimed at Procrustean bed survival.
|
|
43
|
|
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
|
|
48 message.
|
|
49
|
|
50 This variable should never be set directly, but bound before a call to
|
|
51 `mml-generate-mime' or similar functions.")
|
|
52
|
|
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"))
|
|
60
|
|
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")
|
|
66 boundary))
|
|
67
|
|
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))))
|
|
73
|
|
74 (defun mm-safer-encoding (encoding)
|
|
75 "Return a safer but similar encoding."
|
|
76 (cond
|
|
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.
|
|
80 (t 'base64)))
|
|
81
|
|
82 (defun mm-encode-content-transfer-encoding (encoding &optional type)
|
|
83 (cond
|
|
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)))
|
|
91 (condition-case error
|
|
92 (base64-encode-region (point-min) (point-max))
|
|
93 (error
|
|
94 (message "Error while decoding: %s" error)
|
|
95 nil)))
|
|
96 ((memq encoding '(7bit 8bit binary))
|
|
97 ;; Do nothing.
|
|
98 )
|
|
99 ((null encoding)
|
|
100 ;; Do nothing.
|
|
101 )
|
|
102 ((functionp encoding)
|
|
103 (ignore-errors (funcall encoding (point-min) (point-max))))
|
|
104 (t
|
|
105 (message "Unknown encoding %s; defaulting to 8bit" encoding))))
|
|
106
|
|
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)))
|
|
111 (encoding
|
|
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)
|
|
121 encoding))
|
|
122
|
|
123 (defun mm-insert-headers (type encoding &optional file)
|
|
124 "Insert headers for TYPE."
|
|
125 (insert "Content-Type: " type)
|
|
126 (when file
|
|
127 (insert ";\n\tname=\"" (file-name-nondirectory file) "\""))
|
|
128 (insert "\n")
|
|
129 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
|
|
130 (insert "Content-Disposition: inline")
|
|
131 (when file
|
|
132 (insert ";\n\tfilename=\"" (file-name-nondirectory file) "\""))
|
|
133 (insert "\n")
|
|
134 (insert "\n"))
|
|
135
|
|
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))
|
|
139 (catch 'found
|
|
140 (while rules
|
|
141 (when (string-match (caar rules) type)
|
|
142 (throw 'found
|
|
143 (let ((encoding
|
|
144 (if (eq (cadr (car rules)) 'qp-or-base64)
|
|
145 (mm-qp-or-base64)
|
|
146 (cadr (car rules)))))
|
|
147 (if mm-use-ultra-safe-encoding
|
|
148 (mm-safer-encoding encoding)
|
|
149 encoding))))
|
|
150 (pop rules)))))
|
|
151
|
|
152 (defun mm-qp-or-base64 ()
|
|
153 (save-excursion
|
|
154 (let ((limit (min (point-max) (+ 2000 (point-min))))
|
|
155 (n8bit 0))
|
|
156 (goto-char (point-min))
|
|
157 (skip-chars-forward "\x20-\x7f\r\n\t" limit)
|
|
158 (while (< (point) limit)
|
|
159 (incf n8bit)
|
|
160 (forward-char 1)
|
|
161 (skip-chars-forward "\x20-\x7f\r\n\t" limit))
|
|
162 (if (< (* 6 n8bit) (- limit (point-min)))
|
|
163 'quoted-printable
|
|
164 'base64))))
|
|
165
|
|
166 (provide 'mm-encode)
|
|
167
|
|
168 ;;; mm-encode.el ends here
|