fbc2d7c4628cba9131e4d0227ddd3643c752c956
[elisp/gnus.git-] / 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/.*" quoted-printable)
32     ("message/rfc822" 8bit)
33     ("application/emacs-lisp" 8bit)
34     ("application/x-patch" 8bit)
35     (".*" base64))
36   "Alist of regexps that match MIME types and their encodings.")
37
38 (defun mm-insert-rfc822-headers (charset encoding)
39   "Insert text/plain headers with CHARSET and ENCODING."
40   (insert "MIME-Version: 1.0\n")
41   (insert "Content-Type: text/plain; charset="
42           (mail-quote-string (downcase (symbol-name charset))) "\n")
43   (insert "Content-Transfer-Encoding: "
44           (downcase (symbol-name encoding)) "\n"))
45
46 (defun mm-insert-multipart-headers ()
47   "Insert multipart/mixed headers."
48   (let ((boundary "=-=-="))
49     (insert "MIME-Version: 1.0\n")
50     (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
51                     boundary))
52     boundary))
53
54 (defun mm-default-file-encoding (file)
55   "Return a default encoding for FILE."
56   (if (not (string-match "\\.[^.]+$" file))
57       "application/octet-stream"
58     (mailcap-extension-to-mime (match-string 0 file))))
59
60 (defun mm-encode-content-transfer-encoding (encoding &optional type)
61   (cond
62    ((eq encoding 'quoted-printable)
63     (quoted-printable-encode-region (point-min) (point-max)))
64    ((eq encoding 'base64)
65     (when (equal type "text/plain")
66       (goto-char (point-min))
67       (while (search-forward "\n" nil t)
68         (replace-match "\r\n" t t)))
69     (condition-case error
70         (base64-encode-region (point-min) (point-max))
71       (error
72        (message "Error while decoding: %s" error)
73        nil)))
74    ((memq encoding '(7bit 8bit binary))
75     )
76    ((null encoding)
77     )
78    ((functionp encoding)
79     (ignore-errors (funcall encoding (point-min) (point-max))))
80    (t
81     (message "Unknown encoding %s; defaulting to 8bit" encoding))))
82
83 (defun mm-encode-buffer (type)
84   "Encode the buffer which contains data of TYPE.
85 The encoding used is returned."
86   (let* ((mime-type (if (stringp type) type (car type)))
87          (encoding
88           (or (and (listp type)
89                    (cadr (assq 'encoding type)))
90               (mm-content-transfer-encoding mime-type)))
91          (bits (mm-body-7-or-8)))
92     ;; We force buffers that are 7bit to be unencoded, no matter
93     ;; what the preferred encoding is.
94     (when (eq bits '7bit)
95       (setq encoding bits))
96     (mm-encode-content-transfer-encoding encoding mime-type)
97     encoding))
98
99 (defun mm-insert-headers (type encoding &optional file)
100   "Insert headers for TYPE."
101   (insert "Content-Type: " type)
102   (when file
103     (insert ";\n\tname=\"" (file-name-nondirectory file) "\""))
104   (insert "\n")
105   (insert (format "Content-Transfer-Encoding: %s\n" encoding))
106   (insert "Content-Disposition: inline")
107   (when file
108     (insert ";\n\tfilename=\"" (file-name-nondirectory file) "\""))
109   (insert "\n")
110   (insert "\n"))
111
112 (defun mm-content-transfer-encoding (type)
113   "Return a CTE suitable for TYPE."
114   (let ((rules mm-content-transfer-encoding-defaults))
115     (catch 'found
116       (while rules
117         (when (string-match (caar rules) type)
118           (throw 'found (cadar rules)))
119         (pop rules)))))
120
121 (provide 'mm-encode)
122
123 ;;; mm-encode.el ends here