(mm-use-ultra-safe-encoding): New variable.
[gnus] / lisp / mm-encode.el
1 ;;; mm-encode.el --- Functions for encoding MIME things 
2 ;; Copyright (C) 1998,99 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 (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
66                     boundary))
67     boundary))
68
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))))
74
75 (defun mm-safer-encoding (encoding)
76   "Return a safer but similar encoding."
77   (cond
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.
81    (t 'base64)))
82
83 (defun mm-encode-content-transfer-encoding (encoding &optional type)
84   (cond
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)))
92     (condition-case error
93         (base64-encode-region (point-min) (point-max))
94       (error
95        (message "Error while decoding: %s" error)
96        nil)))
97    ((memq encoding '(7bit 8bit binary))
98     ;; Do nothing.
99     )
100    ((null encoding)
101     ;; Do nothing.
102     )
103    ((functionp encoding)
104     (ignore-errors (funcall encoding (point-min) (point-max))))
105    (t
106     (message "Unknown encoding %s; defaulting to 8bit" encoding))))
107
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)))
112          (encoding
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)
122     encoding))
123
124 (defun mm-insert-headers (type encoding &optional file)
125   "Insert headers for TYPE."
126   (insert "Content-Type: " type)
127   (when file
128     (insert ";\n\tname=\"" (file-name-nondirectory file) "\""))
129   (insert "\n")
130   (insert (format "Content-Transfer-Encoding: %s\n" encoding))
131   (insert "Content-Disposition: inline")
132   (when file
133     (insert ";\n\tfilename=\"" (file-name-nondirectory file) "\""))
134   (insert "\n")
135   (insert "\n"))
136
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))
140     (catch 'found
141       (while rules
142         (when (string-match (caar rules) type)
143           (throw 'found
144                  (let ((encoding 
145                         (if (eq (cadar rules) 'qp-or-base64)
146                             (mm-qp-or-base64)
147                           (cadar rules))))
148                    (if mm-use-ultra-safe-encoding
149                        (mm-safer-encoding encoding)
150                      encoding))))
151         (pop rules)))))
152
153 (defun mm-qp-or-base64 ()
154   (save-excursion
155     (let ((limit (min (point-max) (+ 2000 (point-min))))
156           (n8bit 0))
157       (goto-char (point-min))
158       (skip-chars-forward "\x20-\x7f\r\n\t" limit)
159       (while (< (point) limit)
160         (incf n8bit)
161         (forward-char 1)
162         (skip-chars-forward "\x20-\x7f\r\n\t" limit))
163       (if (< (* 6 n8bit) (- limit (point-min)))
164           'quoted-printable
165         'base64))))
166
167 (provide 'mm-encode)
168
169 ;;; mm-encode.el ends here