Mercurial > emacs
annotate lisp/gnus/mm-encode.el @ 49914:b4b6ede28a6f
(x_face_list_fonts): Fix int/Lisp_Object mixup.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Sat, 22 Feb 2003 22:20:00 +0000 |
parents | 0d8b17d428b5 |
children | 695cf19ef79e d7ddb3e565de |
rev | line source |
---|---|
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48588
diff
changeset
|
1 ;;; mm-encode.el --- functions for encoding MIME things |
48588 | 2 ;; Copyright (C) 1998, 1999, 2000, 2002 Free Software Foundation, Inc. |
31717 | 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 | |
32223
1d234b7994c8
Require CL. At least, for `incf'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
31717
diff
changeset
|
27 (eval-when-compile (require 'cl)) |
31717 | 28 (require 'mail-parse) |
29 (require 'mailcap) | |
33297 | 30 (eval-and-compile |
31 (autoload 'mm-body-7-or-8 "mm-bodies")) | |
31717 | 32 |
33 (defvar mm-content-transfer-encoding-defaults | |
34 '(("text/x-patch" 8bit) | |
35 ("text/.*" qp-or-base64) | |
36 ("message/rfc822" 8bit) | |
37 ("application/emacs-lisp" 8bit) | |
43420
33c54ecf6602
* mm-encode.el (mm-content-transfer-encoding-defaults): Set
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
38413
diff
changeset
|
38 ("application/x-emacs-lisp" 8bit) |
31717 | 39 ("application/x-patch" 8bit) |
43420
33c54ecf6602
* mm-encode.el (mm-content-transfer-encoding-defaults): Set
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
38413
diff
changeset
|
40 (".*" base64)) |
31717 | 41 "Alist of regexps that match MIME types and their encodings. |
42 If the encoding is `qp-or-base64', then either quoted-printable | |
43 or base64 will be used, depending on what is more efficient.") | |
44 | |
45 (defvar mm-use-ultra-safe-encoding nil | |
46 "If non-nil, use encodings aimed at Procrustean bed survival. | |
47 | |
48 This means that textual parts are encoded as quoted-printable if they | |
49 contain lines longer than 76 characters or starting with \"From \" in | |
50 the body. Non-7bit encodings (8bit, binary) are generally disallowed. | |
51 This is to reduce the probability that a broken MTA or MDA changes the | |
52 message. | |
53 | |
54 This variable should never be set directly, but bound before a call to | |
55 `mml-generate-mime' or similar functions.") | |
56 | |
57 (defun mm-insert-rfc822-headers (charset encoding) | |
58 "Insert text/plain headers with CHARSET and ENCODING." | |
59 (insert "MIME-Version: 1.0\n") | |
60 (insert "Content-Type: text/plain; charset=" | |
61 (mail-quote-string (downcase (symbol-name charset))) "\n") | |
62 (insert "Content-Transfer-Encoding: " | |
63 (downcase (symbol-name encoding)) "\n")) | |
64 | |
65 (defun mm-insert-multipart-headers () | |
66 "Insert multipart/mixed headers." | |
67 (let ((boundary "=-=-=")) | |
68 (insert "MIME-Version: 1.0\n") | |
69 (insert "Content-Type: multipart/mixed; boundary=\"" boundary "\"\n") | |
70 boundary)) | |
71 | |
72 (defun mm-default-file-encoding (file) | |
73 "Return a default encoding for FILE." | |
74 (if (not (string-match "\\.[^.]+$" file)) | |
75 "application/octet-stream" | |
76 (mailcap-extension-to-mime (match-string 0 file)))) | |
77 | |
78 (defun mm-safer-encoding (encoding) | |
79 "Return a safer but similar encoding." | |
80 (cond | |
81 ((memq encoding '(7bit 8bit quoted-printable)) 'quoted-printable) | |
48588 | 82 ;; The remaining encodings are binary and base64 (and perhaps some |
31717 | 83 ;; non-standard ones), which are both turned into base64. |
84 (t 'base64))) | |
85 | |
86 (defun mm-encode-content-transfer-encoding (encoding &optional type) | |
87 (cond | |
88 ((eq encoding 'quoted-printable) | |
89 (quoted-printable-encode-region (point-min) (point-max) t)) | |
90 ((eq encoding 'base64) | |
91 (when (equal type "text/plain") | |
92 (goto-char (point-min)) | |
93 (while (search-forward "\n" nil t) | |
94 (replace-match "\r\n" t t))) | |
95 (condition-case error | |
96 (base64-encode-region (point-min) (point-max)) | |
97 (error | |
98 (message "Error while decoding: %s" error) | |
99 nil))) | |
100 ((memq encoding '(7bit 8bit binary)) | |
101 ;; Do nothing. | |
102 ) | |
103 ((null encoding) | |
104 ;; Do nothing. | |
105 ) | |
106 ((functionp encoding) | |
107 (ignore-errors (funcall encoding (point-min) (point-max)))) | |
108 (t | |
109 (message "Unknown encoding %s; defaulting to 8bit" encoding)))) | |
110 | |
111 (defun mm-encode-buffer (type) | |
112 "Encode the buffer which contains data of TYPE. | |
113 The encoding used is returned." | |
114 (let* ((mime-type (if (stringp type) type (car type))) | |
115 (encoding | |
116 (or (and (listp type) | |
117 (cadr (assq 'encoding type))) | |
118 (mm-content-transfer-encoding mime-type))) | |
119 (bits (mm-body-7-or-8))) | |
120 ;; We force buffers that are 7bit to be unencoded, no matter | |
121 ;; what the preferred encoding is. | |
122 (when (eq bits '7bit) | |
123 (setq encoding bits)) | |
124 (mm-encode-content-transfer-encoding encoding mime-type) | |
125 encoding)) | |
126 | |
127 (defun mm-insert-headers (type encoding &optional file) | |
128 "Insert headers for TYPE." | |
129 (insert "Content-Type: " type) | |
130 (when file | |
131 (insert ";\n\tname=\"" (file-name-nondirectory file) "\"")) | |
132 (insert "\n") | |
133 (insert (format "Content-Transfer-Encoding: %s\n" encoding)) | |
134 (insert "Content-Disposition: inline") | |
135 (when file | |
136 (insert ";\n\tfilename=\"" (file-name-nondirectory file) "\"")) | |
137 (insert "\n") | |
138 (insert "\n")) | |
139 | |
140 (defun mm-content-transfer-encoding (type) | |
141 "Return a CTE suitable for TYPE to encode the current buffer." | |
142 (let ((rules mm-content-transfer-encoding-defaults)) | |
143 (catch 'found | |
144 (while rules | |
145 (when (string-match (caar rules) type) | |
146 (throw 'found | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48588
diff
changeset
|
147 (let ((encoding |
31717 | 148 (if (eq (cadr (car rules)) 'qp-or-base64) |
149 (mm-qp-or-base64) | |
150 (cadr (car rules))))) | |
151 (if mm-use-ultra-safe-encoding | |
152 (mm-safer-encoding encoding) | |
153 encoding)))) | |
154 (pop rules))))) | |
155 | |
156 (defun mm-qp-or-base64 () | |
157 (save-excursion | |
158 (let ((limit (min (point-max) (+ 2000 (point-min)))) | |
159 (n8bit 0)) | |
160 (goto-char (point-min)) | |
161 (skip-chars-forward "\x20-\x7f\r\n\t" limit) | |
162 (while (< (point) limit) | |
163 (incf n8bit) | |
164 (forward-char 1) | |
165 (skip-chars-forward "\x20-\x7f\r\n\t" limit)) | |
35149
f6220cdcf5d4
(mm-qp-or-base64): Don't base64 for the sake of a
Dave Love <fx@gnu.org>
parents:
33297
diff
changeset
|
166 (if (or (< (* 6 n8bit) (- limit (point-min))) |
f6220cdcf5d4
(mm-qp-or-base64): Don't base64 for the sake of a
Dave Love <fx@gnu.org>
parents:
33297
diff
changeset
|
167 ;; Don't base64, say, a short line with a single |
f6220cdcf5d4
(mm-qp-or-base64): Don't base64 for the sake of a
Dave Love <fx@gnu.org>
parents:
33297
diff
changeset
|
168 ;; non-ASCII char when splitting parts by charset. |
f6220cdcf5d4
(mm-qp-or-base64): Don't base64 for the sake of a
Dave Love <fx@gnu.org>
parents:
33297
diff
changeset
|
169 (= n8bit 1)) |
31717 | 170 'quoted-printable |
171 'base64)))) | |
172 | |
173 (provide 'mm-encode) | |
174 | |
175 ;;; mm-encode.el ends here |