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