Mercurial > emacs
annotate lisp/gnus/mm-bodies.el @ 32359:93e229052aa9
*** empty log message ***
author | Dave Love <fx@gnu.org> |
---|---|
date | Tue, 10 Oct 2000 09:31:50 +0000 |
parents | cbbde5b20af5 |
children | 9373d6d073ed |
rev | line source |
---|---|
31717 | 1 ;;; mm-bodies.el --- Functions for decoding 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 | |
27 (eval-and-compile | |
28 (or (fboundp 'base64-decode-region) | |
32209
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
29 (require 'base64))) |
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
30 (eval-when-compile |
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
31 (require 'mm-uu)) |
31717 | 32 |
33 (require 'mm-util) | |
34 (require 'rfc2047) | |
32209
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
35 (require 'mm-encode) |
31717 | 36 |
37 ;; 8bit treatment gets any char except: 0x32 - 0x7f, CR, LF, TAB, BEL, | |
38 ;; BS, vertical TAB, form feed, and ^_ | |
39 (defvar mm-7bit-chars "\x20-\x7f\r\n\t\x7\x8\xb\xc\x1f") | |
40 | |
41 (defcustom mm-body-charset-encoding-alist | |
42 '((iso-2022-jp . 7bit) | |
43 (iso-2022-jp-2 . 7bit)) | |
44 "Alist of MIME charsets to encodings. | |
45 Valid encodings are `7bit', `8bit', `quoted-printable' and `base64'." | |
46 :type '(repeat (cons (symbol :tag "charset") | |
47 (choice :tag "encoding" | |
48 (const 7bit) | |
49 (const 8bit) | |
50 (const quoted-printable) | |
51 (const base64)))) | |
52 :group 'mime) | |
53 | |
54 (defun mm-encode-body () | |
55 "Encode a body. | |
56 Should be called narrowed to the body that is to be encoded. | |
57 If there is more than one non-ASCII MULE charset, then list of found | |
58 MULE charsets are returned. | |
59 If successful, the MIME charset is returned. | |
60 If no encoding was done, nil is returned." | |
61 (if (not (featurep 'mule)) | |
62 ;; In the non-Mule case, we search for non-ASCII chars and | |
63 ;; return the value of `mail-parse-charset' if any are found. | |
64 (save-excursion | |
65 (goto-char (point-min)) | |
66 (if (re-search-forward "[^\x0-\x7f]" nil t) | |
67 (or mail-parse-charset | |
68 (mm-read-charset "Charset used in the article: ")) | |
69 ;; The logic in `mml-generate-mime-1' confirms that it's OK | |
70 ;; to return nil here. | |
71 nil)) | |
72 (save-excursion | |
73 (goto-char (point-min)) | |
74 (let ((charsets (mm-find-mime-charset-region (point-min) (point-max))) | |
75 charset) | |
76 (cond | |
77 ;; No encoding. | |
78 ((null charsets) | |
79 nil) | |
80 ;; Too many charsets. | |
81 ((> (length charsets) 1) | |
82 charsets) | |
83 ;; We encode. | |
84 (t | |
85 (let ((charset (car charsets)) | |
86 start) | |
87 (when (or t | |
88 ;; We always decode. | |
89 (not (mm-coding-system-equal | |
90 charset buffer-file-coding-system))) | |
91 (while (not (eobp)) | |
92 (if (eq (mm-charset-after) 'ascii) | |
93 (when start | |
94 (save-restriction | |
95 (narrow-to-region start (point)) | |
32209
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
96 (mm-encode-coding-region |
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
97 start (point) (mm-charset-to-coding-system charset)) |
31717 | 98 (goto-char (point-max))) |
99 (setq start nil)) | |
100 (unless start | |
101 (setq start (point)))) | |
102 (forward-char 1)) | |
103 (when start | |
32209
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
104 (mm-encode-coding-region start (point) |
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
105 (mm-charset-to-coding-system charset)) |
31717 | 106 (setq start nil))) |
107 charset))))))) | |
108 | |
32209
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
109 (eval-when-compile (defvar message-posting-charset)) |
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
110 |
31717 | 111 (defun mm-body-encoding (charset &optional encoding) |
112 "Do Content-Transfer-Encoding and return the encoding of the current buffer." | |
113 (let ((bits (mm-body-7-or-8))) | |
32209
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
114 (require 'message) |
31717 | 115 (cond |
116 ((and (not mm-use-ultra-safe-encoding) (eq bits '7bit)) | |
117 bits) | |
118 ((and (not mm-use-ultra-safe-encoding) | |
119 (or (eq t (cdr message-posting-charset)) | |
120 (memq charset (cdr message-posting-charset)) | |
121 (eq charset mail-parse-charset))) | |
122 bits) | |
123 (t | |
124 (let ((encoding (or encoding | |
125 (cdr (assq charset mm-body-charset-encoding-alist)) | |
126 (mm-qp-or-base64)))) | |
127 (when mm-use-ultra-safe-encoding | |
128 (setq encoding (mm-safer-encoding encoding))) | |
129 (mm-encode-content-transfer-encoding encoding "text/plain") | |
130 encoding))))) | |
131 | |
132 (defun mm-body-7-or-8 () | |
133 "Say whether the body is 7bit or 8bit." | |
134 (cond | |
135 ((not (featurep 'mule)) | |
136 (if (save-excursion | |
137 (goto-char (point-min)) | |
138 (skip-chars-forward mm-7bit-chars) | |
139 (eobp)) | |
140 '7bit | |
141 '8bit)) | |
142 (t | |
143 ;; Mule version | |
144 (if (and (null (delq 'ascii | |
145 (mm-find-charset-region (point-min) (point-max)))) | |
146 ;;!!!The following is necessary because the function | |
147 ;;!!!above seems to return the wrong result under | |
148 ;;!!!Emacs 20.3. Sometimes. | |
149 (save-excursion | |
150 (goto-char (point-min)) | |
151 (skip-chars-forward mm-7bit-chars) | |
152 (eobp))) | |
153 '7bit | |
154 '8bit)))) | |
155 | |
156 ;;; | |
157 ;;; Functions for decoding | |
158 ;;; | |
159 | |
160 (defun mm-decode-content-transfer-encoding (encoding &optional type) | |
161 (prog1 | |
162 (condition-case error | |
163 (cond | |
164 ((eq encoding 'quoted-printable) | |
165 (quoted-printable-decode-region (point-min) (point-max))) | |
166 ((eq encoding 'base64) | |
167 (base64-decode-region | |
168 (point-min) | |
169 ;; Some mailers insert whitespace | |
170 ;; junk at the end which | |
171 ;; base64-decode-region dislikes. | |
172 ;; Also remove possible junk which could | |
173 ;; have been added by mailing list software. | |
174 (save-excursion | |
175 (goto-char (point-min)) | |
176 (while (re-search-forward "^[\t ]*\r?\n" nil t) | |
177 (delete-region (match-beginning 0) (match-end 0))) | |
178 (goto-char (point-max)) | |
179 (when (re-search-backward "^[A-Za-z0-9+/]+=*[\t ]*$" nil t) | |
180 (forward-line) | |
181 (delete-region (point) (point-max))) | |
182 (point-max)))) | |
183 ((memq encoding '(7bit 8bit binary)) | |
184 ;; Do nothing. | |
185 ) | |
186 ((null encoding) | |
187 ;; Do nothing. | |
188 ) | |
189 ((memq encoding '(x-uuencode x-uue)) | |
32209
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
190 (require 'mm-uu) |
31717 | 191 (funcall mm-uu-decode-function (point-min) (point-max))) |
192 ((eq encoding 'x-binhex) | |
32209
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
193 (require 'mm-uu) |
31717 | 194 (funcall mm-uu-binhex-decode-function (point-min) (point-max))) |
195 ((functionp encoding) | |
196 (funcall encoding (point-min) (point-max))) | |
197 (t | |
198 (message "Unknown encoding %s; defaulting to 8bit" encoding))) | |
199 (error | |
200 (message "Error while decoding: %s" error) | |
201 nil)) | |
202 (when (and | |
203 (memq encoding '(base64 x-uuencode x-uue x-binhex)) | |
204 (equal type "text/plain")) | |
205 (goto-char (point-min)) | |
206 (while (search-forward "\r\n" nil t) | |
207 (replace-match "\n" t t))))) | |
208 | |
209 (defun mm-decode-body (charset &optional encoding type) | |
210 "Decode the current article that has been encoded with ENCODING. | |
211 The characters in CHARSET should then be decoded." | |
212 (if (stringp charset) | |
213 (setq charset (intern (downcase charset)))) | |
214 (if (or (not charset) | |
215 (eq 'gnus-all mail-parse-ignored-charsets) | |
216 (memq 'gnus-all mail-parse-ignored-charsets) | |
217 (memq charset mail-parse-ignored-charsets)) | |
218 (setq charset mail-parse-charset)) | |
219 (save-excursion | |
220 (when encoding | |
221 (mm-decode-content-transfer-encoding encoding type)) | |
222 (when (featurep 'mule) | |
32209
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
223 (let ((coding-system (mm-charset-to-coding-system charset))) |
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
224 (if (and (not coding-system) |
31717 | 225 (listp mail-parse-ignored-charsets) |
226 (memq 'gnus-unknown mail-parse-ignored-charsets)) | |
32209
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
227 (setq coding-system |
31717 | 228 (mm-charset-to-coding-system mail-parse-charset))) |
32209
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
229 (when (and charset coding-system |
31717 | 230 ;; buffer-file-coding-system |
231 ;;Article buffer is nil coding system | |
232 ;;in XEmacs | |
233 (mm-multibyte-p) | |
32209
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
234 (or (not (eq coding-system 'ascii)) |
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
235 (setq coding-system mail-parse-charset)) |
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
236 (not (eq coding-system 'gnus-decoded))) |
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
237 (mm-decode-coding-region (point-min) (point-max) coding-system)))))) |
31717 | 238 |
239 (defun mm-decode-string (string charset) | |
240 "Decode STRING with CHARSET." | |
241 (when (stringp charset) | |
242 (setq charset (intern (downcase charset)))) | |
243 (when (or (not charset) | |
244 (eq 'gnus-all mail-parse-ignored-charsets) | |
245 (memq 'gnus-all mail-parse-ignored-charsets) | |
246 (memq charset mail-parse-ignored-charsets)) | |
247 (setq charset mail-parse-charset)) | |
248 (or | |
249 (when (featurep 'mule) | |
32209
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
250 (let ((coding-system (mm-charset-to-coding-system charset))) |
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
251 (if (and (not coding-system) |
31717 | 252 (listp mail-parse-ignored-charsets) |
253 (memq 'gnus-unknown mail-parse-ignored-charsets)) | |
32209
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
254 (setq coding-system |
31717 | 255 (mm-charset-to-coding-system mail-parse-charset))) |
32209
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
256 (when (and charset coding-system |
31717 | 257 (mm-multibyte-p) |
32209
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
258 (or (not (eq coding-system 'ascii)) |
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
259 (setq coding-system mail-parse-charset))) |
cbbde5b20af5
Require mm-uu, Don't require qp, uudecode.
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
260 (mm-decode-coding-string string coding-system)))) |
31717 | 261 string)) |
262 | |
263 (provide 'mm-bodies) | |
264 | |
265 ;; mm-bodies.el ends here |