Importing Pterodactyl Gnus v0.95.
[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/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)))
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     )
79    ((null encoding)
80     )
81    ((functionp encoding)
82     (ignore-errors (funcall encoding (point-min) (point-max))))
83    (t
84     (message "Unknown encoding %s; defaulting to 8bit" encoding))))
85
86 (defun mm-encode-buffer (type)
87   "Encode the buffer which contains data of TYPE.
88 The encoding used is returned."
89   (let* ((mime-type (if (stringp type) type (car type)))
90          (encoding
91           (or (and (listp type)
92                    (cadr (assq 'encoding type)))
93               (mm-content-transfer-encoding mime-type)))
94          (bits (mm-body-7-or-8)))
95     ;; We force buffers that are 7bit to be unencoded, no matter
96     ;; what the preferred encoding is.
97     (when (eq bits '7bit)
98       (setq encoding bits))
99     (mm-encode-content-transfer-encoding encoding mime-type)
100     encoding))
101
102 (defun mm-insert-headers (type encoding &optional file)
103   "Insert headers for TYPE."
104   (insert "Content-Type: " type)
105   (when file
106     (insert ";\n\tname=\"" (file-name-nondirectory file) "\""))
107   (insert "\n")
108   (insert (format "Content-Transfer-Encoding: %s\n" encoding))
109   (insert "Content-Disposition: inline")
110   (when file
111     (insert ";\n\tfilename=\"" (file-name-nondirectory file) "\""))
112   (insert "\n")
113   (insert "\n"))
114
115 (defun mm-content-transfer-encoding (type)
116   "Return a CTE suitable for TYPE to encode the current buffer."
117   (let ((rules mm-content-transfer-encoding-defaults))
118     (catch 'found
119       (while rules
120         (when (string-match (caar rules) type)
121           (throw 'found
122                  (if (eq (cadar rules) 'qp-or-base64)
123                      (mm-qp-or-base64)
124                    (cadar rules))))
125         (pop rules)))))
126
127 (defun mm-qp-or-base64 ()
128   (save-excursion
129     (save-restriction
130       (narrow-to-region (point-min) (min (+ (point-min) 1000) (point-max)))
131       (goto-char (point-min))
132       (let ((8bit 0))
133         (cond
134          ((not (featurep 'mule))
135           (while (re-search-forward "[^\x00-\x7f]" nil t)
136             (incf 8bit)))
137          (t
138           ;; Mule version
139           (while (not (eobp))
140             (skip-chars-forward "\0-\177")
141             (unless (eobp)
142               (forward-char 1)
143               (incf 8bit)))))
144         (if (> (/ (* 8bit 1.0) (buffer-size)) 0.166)
145             'quoted-printable
146           'base64)))))
147
148 (provide 'mm-encode)
149
150 ;;; mm-encode.el ends here