413a7ae7168c3d1b1d718b2e5e5fe2daad1e580b
[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 (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"))
48
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"
54                     boundary))
55     boundary))
56
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))))
62
63 (defun mm-encode-content-transfer-encoding (encoding &optional type)
64   (cond
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)))
72     (condition-case error
73         (base64-encode-region (point-min) (point-max))
74       (error
75        (message "Error while decoding: %s" error)
76        nil)))
77    ((memq encoding '(7bit 8bit binary))
78     ;; Do nothing.
79     )
80    ((null encoding)
81     ;; Do nothing.
82     )
83    ((functionp encoding)
84     (ignore-errors (funcall encoding (point-min) (point-max))))
85    (t
86     (message "Unknown encoding %s; defaulting to 8bit" encoding))))
87
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)))
92          (encoding
93           (or (and (listp 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.
99     (when (eq bits '7bit)
100       (setq encoding bits))
101     (mm-encode-content-transfer-encoding encoding mime-type)
102     encoding))
103
104 (defun mm-insert-headers (type encoding &optional file)
105   "Insert headers for TYPE."
106   (insert "Content-Type: " type)
107   (when file
108     (insert ";\n\tname=\"" (file-name-nondirectory file) "\""))
109   (insert "\n")
110   (insert (format "Content-Transfer-Encoding: %s\n" encoding))
111   (insert "Content-Disposition: inline")
112   (when file
113     (insert ";\n\tfilename=\"" (file-name-nondirectory file) "\""))
114   (insert "\n")
115   (insert "\n"))
116
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))
120     (catch 'found
121       (while rules
122         (when (string-match (caar rules) type)
123           (throw 'found
124                  (if (eq (cadar rules) 'qp-or-base64)
125                      (mm-qp-or-base64)
126                    (cadar rules))))
127         (pop rules)))))
128
129 (defun mm-qp-or-base64 ()
130   (save-excursion
131     (let ((limit (min (point-max) (+ 2000 (point-min))))
132           (n8bit 0))
133       (goto-char (point-min))
134       (skip-chars-forward "\x20-\x7f\r\n\t" limit)
135       (while (< (point) limit)
136         (incf n8bit)
137         (forward-char 1)
138         (skip-chars-forward "\x20-\x7f\r\n\t" limit))
139       (if (< (* 6 n8bit) (- limit (point-min)))
140           'quoted-printable
141         'base64))))
142
143 (provide 'mm-encode)
144
145 ;;; mm-encode.el ends here