(custom-face-value-create): If face name doesn't end with "face", add
[bpt/emacs.git] / lisp / gnus / mm-encode.el
CommitLineData
c113de23
GM
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
b28ce55a 27(eval-when-compile (require 'cl))
c113de23
GM
28(require 'mail-parse)
29(require 'mailcap)
30
31(defvar mm-content-transfer-encoding-defaults
32 '(("text/x-patch" 8bit)
33 ("text/.*" qp-or-base64)
34 ("message/rfc822" 8bit)
35 ("application/emacs-lisp" 8bit)
36 ("application/x-patch" 8bit)
37 (".*" qp-or-base64))
38 "Alist of regexps that match MIME types and their encodings.
39If the encoding is `qp-or-base64', then either quoted-printable
40or base64 will be used, depending on what is more efficient.")
41
42(defvar mm-use-ultra-safe-encoding nil
43 "If non-nil, use encodings aimed at Procrustean bed survival.
44
45This means that textual parts are encoded as quoted-printable if they
46contain lines longer than 76 characters or starting with \"From \" in
47the body. Non-7bit encodings (8bit, binary) are generally disallowed.
48This is to reduce the probability that a broken MTA or MDA changes the
49message.
50
51This variable should never be set directly, but bound before a call to
52`mml-generate-mime' or similar functions.")
53
54(defun mm-insert-rfc822-headers (charset encoding)
55 "Insert text/plain headers with CHARSET and ENCODING."
56 (insert "MIME-Version: 1.0\n")
57 (insert "Content-Type: text/plain; charset="
58 (mail-quote-string (downcase (symbol-name charset))) "\n")
59 (insert "Content-Transfer-Encoding: "
60 (downcase (symbol-name encoding)) "\n"))
61
62(defun mm-insert-multipart-headers ()
63 "Insert multipart/mixed headers."
64 (let ((boundary "=-=-="))
65 (insert "MIME-Version: 1.0\n")
66 (insert "Content-Type: multipart/mixed; boundary=\"" boundary "\"\n")
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.
110The 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 (cadr (car rules)) 'qp-or-base64)
146 (mm-qp-or-base64)
147 (cadr (car 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