Mercurial > emacs
annotate lisp/gnus/rfc2047.el @ 88978:32dd7c0ab007
(tar-extract): Fix previous change.
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Thu, 08 Aug 2002 13:04:00 +0000 |
parents | 4cda993e8f6e |
children | 56933b338052 |
rev | line source |
---|---|
38413
a26d9b55abb6
Some fixes to follow coding conventions in files from Gnus.
Pavel Janík <Pavel@Janik.cz>
parents:
35985
diff
changeset
|
1 ;;; rfc2047.el --- functions for encoding and decoding rfc2047 messages |
31717 | 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 | |
34674 | 25 ;; RFC 2047 is "MIME (Multipurpose Internet Mail Extensions) Part |
26 ;; Three: Message Header Extensions for Non-ASCII Text". | |
27 | |
31717 | 28 ;;; Code: |
29 | |
33304 | 30 (eval-when-compile (require 'cl)) |
31717 | 31 |
32 (require 'qp) | |
33 (require 'mm-util) | |
34 (require 'ietf-drums) | |
35 (require 'mail-prsvr) | |
33304 | 36 (require 'base64) |
37 ;; Fixme: Avoid this (for gnus-point-at-...) mm dependence on gnus. | |
38 (require 'gnus-util) | |
39 (autoload 'mm-body-7-or-8 "mm-bodies") | |
33127
eca95f9d7f05
(base64): Require unconditionally.
Dave Love <fx@gnu.org>
parents:
31764
diff
changeset
|
40 |
31717 | 41 (defvar rfc2047-header-encoding-alist |
42 '(("Newsgroups" . nil) | |
43 ("Message-ID" . nil) | |
44 (t . mime)) | |
45 "*Header/encoding method alist. | |
46 The list is traversed sequentially. The keys can either be | |
33304 | 47 header regexps or t. |
31717 | 48 |
49 The values can be: | |
50 | |
51 1) nil, in which case no encoding is done; | |
52 2) `mime', in which case the header will be encoded according to RFC2047; | |
53 3) a charset, in which case it will be encoded as that charset; | |
54 4) `default', in which case the field will be encoded as the rest | |
55 of the article.") | |
56 | |
57 (defvar rfc2047-charset-encoding-alist | |
58 '((us-ascii . nil) | |
59 (iso-8859-1 . Q) | |
60 (iso-8859-2 . Q) | |
61 (iso-8859-3 . Q) | |
62 (iso-8859-4 . Q) | |
63 (iso-8859-5 . B) | |
64 (koi8-r . B) | |
65 (iso-8859-7 . Q) | |
88920
4cda993e8f6e
(rfc2047-charset-encoding-alist): Use B for Hebrew.
Dave Love <fx@gnu.org>
parents:
88792
diff
changeset
|
66 (iso-8859-8 . B) |
31717 | 67 (iso-8859-9 . Q) |
33304 | 68 (iso-8859-14 . Q) |
69 (iso-8859-15 . Q) | |
31717 | 70 (iso-2022-jp . B) |
71 (iso-2022-kr . B) | |
72 (gb2312 . B) | |
35838
53eebdb81828
2001-02-01 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
35453
diff
changeset
|
73 (big5 . B) |
53eebdb81828
2001-02-01 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
35453
diff
changeset
|
74 (cn-big5 . B) |
31717 | 75 (cn-gb . B) |
76 (cn-gb-2312 . B) | |
77 (euc-kr . B) | |
78 (iso-2022-jp-2 . B) | |
79 (iso-2022-int-1 . B)) | |
80 "Alist of MIME charsets to RFC2047 encodings. | |
88920
4cda993e8f6e
(rfc2047-charset-encoding-alist): Use B for Hebrew.
Dave Love <fx@gnu.org>
parents:
88792
diff
changeset
|
81 Valid encodings are nil, `Q' and `B'. These indicate binary (no) encoding, |
4cda993e8f6e
(rfc2047-charset-encoding-alist): Use B for Hebrew.
Dave Love <fx@gnu.org>
parents:
88792
diff
changeset
|
82 quoted-printable and base64 respectively.") |
31717 | 83 |
84 (defvar rfc2047-encoding-function-alist | |
85 '((Q . rfc2047-q-encode-region) | |
86 (B . rfc2047-b-encode-region) | |
87 (nil . ignore)) | |
88 "Alist of RFC2047 encodings to encoding functions.") | |
89 | |
90 (defvar rfc2047-q-encoding-alist | |
33304 | 91 '(("\\(From\\|Cc\\|To\\|Bcc\||Reply-To\\):" . "-A-Za-z0-9!*+/") |
31764 | 92 ;; = (\075), _ (\137), ? (\077) are used in the encoded word. |
35985
b9c371244b90
(rfc2047-fold-region): Don't forward-char at EOB.
Dave Love <fx@gnu.org>
parents:
35838
diff
changeset
|
93 ;; Avoid using 8bit characters. |
31764 | 94 ;; Equivalent to "^\000-\007\011\013\015-\037\200-\377=_?" |
95 ("." . "\010\012\014\040-\074\076\100-\136\140-\177")) | |
31717 | 96 "Alist of header regexps and valid Q characters.") |
97 | |
98 ;;; | |
99 ;;; Functions for encoding RFC2047 messages | |
100 ;;; | |
101 | |
102 (defun rfc2047-narrow-to-field () | |
103 "Narrow the buffer to the header on the current line." | |
104 (beginning-of-line) | |
105 (narrow-to-region | |
106 (point) | |
107 (progn | |
108 (forward-line 1) | |
109 (if (re-search-forward "^[^ \n\t]" nil t) | |
110 (progn | |
111 (beginning-of-line) | |
112 (point)) | |
113 (point-max)))) | |
114 (goto-char (point-min))) | |
115 | |
116 (defun rfc2047-encode-message-header () | |
117 "Encode the message header according to `rfc2047-header-encoding-alist'. | |
118 Should be called narrowed to the head of the message." | |
119 (interactive "*") | |
120 (save-excursion | |
121 (goto-char (point-min)) | |
122 (let (alist elem method) | |
123 (while (not (eobp)) | |
124 (save-restriction | |
125 (rfc2047-narrow-to-field) | |
126 (if (not (rfc2047-encodable-p)) | |
127 (if (and (eq (mm-body-7-or-8) '8bit) | |
128 (mm-multibyte-p) | |
129 (mm-coding-system-p | |
130 (car message-posting-charset))) | |
131 ;; 8 bit must be decoded. | |
132 ;; Is message-posting-charset a coding system? | |
33304 | 133 (mm-encode-coding-region |
134 (point-min) (point-max) | |
31717 | 135 (car message-posting-charset))) |
136 ;; We found something that may perhaps be encoded. | |
137 (setq method nil | |
138 alist rfc2047-header-encoding-alist) | |
139 (while (setq elem (pop alist)) | |
140 (when (or (and (stringp (car elem)) | |
141 (looking-at (car elem))) | |
142 (eq (car elem) t)) | |
143 (setq alist nil | |
144 method (cdr elem)))) | |
145 (cond | |
146 ((eq method 'mime) | |
33304 | 147 (rfc2047-encode-region (point-min) (point-max))) |
31717 | 148 ((eq method 'default) |
149 (if (and (featurep 'mule) | |
33815
61c7f3065929
(rfc2047-encode-message-header): Don't encode if
Dave Love <fx@gnu.org>
parents:
33304
diff
changeset
|
150 (if (boundp 'default-enable-multibyte-characters) |
61c7f3065929
(rfc2047-encode-message-header): Don't encode if
Dave Love <fx@gnu.org>
parents:
33304
diff
changeset
|
151 default-enable-multibyte-characters) |
31717 | 152 mail-parse-charset) |
33304 | 153 (mm-encode-coding-region (point-min) (point-max) |
31717 | 154 mail-parse-charset))) |
155 ((mm-coding-system-p method) | |
33815
61c7f3065929
(rfc2047-encode-message-header): Don't encode if
Dave Love <fx@gnu.org>
parents:
33304
diff
changeset
|
156 (if (and (featurep 'mule) |
61c7f3065929
(rfc2047-encode-message-header): Don't encode if
Dave Love <fx@gnu.org>
parents:
33304
diff
changeset
|
157 (if (boundp 'default-enable-multibyte-characters) |
61c7f3065929
(rfc2047-encode-message-header): Don't encode if
Dave Love <fx@gnu.org>
parents:
33304
diff
changeset
|
158 default-enable-multibyte-characters)) |
31717 | 159 (mm-encode-coding-region (point-min) (point-max) method))) |
160 ;; Hm. | |
161 (t))) | |
162 (goto-char (point-max))))))) | |
163 | |
35985
b9c371244b90
(rfc2047-fold-region): Don't forward-char at EOB.
Dave Love <fx@gnu.org>
parents:
35838
diff
changeset
|
164 ;; Fixme: This, and the require below may not be the Right Thing, but |
b9c371244b90
(rfc2047-fold-region): Don't forward-char at EOB.
Dave Love <fx@gnu.org>
parents:
35838
diff
changeset
|
165 ;; should be safe just before release. -- fx 2001-02-08 |
b9c371244b90
(rfc2047-fold-region): Don't forward-char at EOB.
Dave Love <fx@gnu.org>
parents:
35838
diff
changeset
|
166 (eval-when-compile (defvar message-posting-charset)) |
b9c371244b90
(rfc2047-fold-region): Don't forward-char at EOB.
Dave Love <fx@gnu.org>
parents:
35838
diff
changeset
|
167 |
33304 | 168 (defun rfc2047-encodable-p () |
169 "Return non-nil if any characters in current buffer need encoding in headers. | |
170 The buffer may be narrowed." | |
35985
b9c371244b90
(rfc2047-fold-region): Don't forward-char at EOB.
Dave Love <fx@gnu.org>
parents:
35838
diff
changeset
|
171 (require 'message) ; for message-posting-charset |
31717 | 172 (let ((charsets |
88792
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
173 (mm-find-mime-charset-region (point-min) (point-max)))) |
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
174 (and charsets (not (equal charsets (list message-posting-charset)))))) |
31717 | 175 |
176 (defun rfc2047-dissect-region (b e) | |
177 "Dissect the region between B and E into words." | |
33304 | 178 (let ((word-chars "-A-Za-z0-9!*+/") |
179 ;; Not using ietf-drums-specials-token makes life simple. | |
180 mail-parse-mule-charset | |
88792
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
181 words point nonascii |
33304 | 182 result word) |
31717 | 183 (save-restriction |
184 (narrow-to-region b e) | |
185 (goto-char (point-min)) | |
33304 | 186 (skip-chars-forward "\000-\177") |
88792
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
187 ;; Fixme: This loop used to check charsets when it found |
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
188 ;; non-ASCII characters. That's removed, since it doesn't make |
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
189 ;; much sense in Emacs 22 and doesn't seem necessary in Emacs |
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
190 ;; 21, even. I'm not sure exactly what it should be doing, and |
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
191 ;; it needs another look, especially for efficiency's sake. -- fx |
31717 | 192 (while (not (eobp)) |
88792
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
193 (setq point (point) |
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
194 nonascii nil) |
33304 | 195 (skip-chars-backward word-chars b) |
196 (unless (eq b (point)) | |
197 (push (cons (buffer-substring b (point)) nil) words)) | |
88792
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
198 (setq b (point) |
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
199 nonascii t) |
33304 | 200 (goto-char point) |
201 (forward-char 1) | |
202 (skip-chars-forward word-chars) | |
88792
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
203 (while (not (eobp)) |
33304 | 204 (forward-char 1) |
205 (skip-chars-forward word-chars)) | |
206 (unless (eq b (point)) | |
88792
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
207 (push (cons (buffer-substring b (point)) nonascii) words)) |
33304 | 208 (setq b (point)) |
209 (skip-chars-forward "\000-\177")) | |
210 (unless (eq b (point)) | |
211 (push (cons (buffer-substring b (point)) nil) words))) | |
212 ;; merge adjacent words | |
213 (setq word (pop words)) | |
214 (while word | |
215 (if (and (cdr word) | |
216 (caar words) | |
217 (not (cdar words)) | |
218 (not (string-match "[^ \t]" (caar words)))) | |
219 (if (eq (cdr (nth 1 words)) (cdr word)) | |
220 (progn | |
221 (setq word (cons (concat | |
222 (car (nth 1 words)) (caar words) | |
223 (car word)) | |
224 (cdr word))) | |
225 (pop words) | |
226 (pop words)) | |
227 (push (cons (concat (caar words) (car word)) (cdr word)) | |
228 result) | |
229 (pop words) | |
230 (setq word (pop words))) | |
231 (push word result) | |
232 (setq word (pop words)))) | |
233 result)) | |
31717 | 234 |
235 (defun rfc2047-encode-region (b e) | |
35985
b9c371244b90
(rfc2047-fold-region): Don't forward-char at EOB.
Dave Love <fx@gnu.org>
parents:
35838
diff
changeset
|
236 "Encode all encodable words in region B to E." |
33304 | 237 (let ((words (rfc2047-dissect-region b e)) word) |
238 (save-restriction | |
239 (narrow-to-region b e) | |
240 (delete-region (point-min) (point-max)) | |
241 (while (setq word (pop words)) | |
242 (if (not (cdr word)) | |
243 (insert (car word)) | |
244 (rfc2047-fold-region (gnus-point-at-bol) (point)) | |
245 (goto-char (point-max)) | |
246 (if (> (- (point) (save-restriction | |
247 (widen) | |
248 (gnus-point-at-bol))) 76) | |
249 (insert "\n ")) | |
250 ;; Insert blank between encoded words | |
251 (if (eq (char-before) ?=) (insert " ")) | |
252 (rfc2047-encode (point) | |
88792
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
253 (progn (insert (car word)) (point))))) |
33304 | 254 (rfc2047-fold-region (point-min) (point-max))))) |
31717 | 255 |
256 (defun rfc2047-encode-string (string) | |
257 "Encode words in STRING." | |
258 (with-temp-buffer | |
259 (insert string) | |
260 (rfc2047-encode-region (point-min) (point-max)) | |
261 (buffer-string))) | |
262 | |
88792
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
263 (defun rfc2047-encode (b e) |
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
264 "Encode the word in the region B to E." |
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
265 (let* ((buff (current-buffer)) |
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
266 (mime-charset (with-temp-buffer |
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
267 (insert-buffer-substring buff b e) |
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
268 (mm-find-mime-charset-region b e))) |
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
269 (cs (if (> (length mime-charset) 1) |
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
270 (mm-charset-to-coding-system mime-charset) |
690e82320649
(rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents:
38413
diff
changeset
|
271 (error "Can't encode word: %s" (buffer-substring b e)))) |
31717 | 272 (encoding (or (cdr (assq mime-charset |
273 rfc2047-charset-encoding-alist)) | |
274 'B)) | |
275 (start (concat | |
276 "=?" (downcase (symbol-name mime-charset)) "?" | |
277 (downcase (symbol-name encoding)) "?")) | |
278 (first t)) | |
279 (save-restriction | |
280 (narrow-to-region b e) | |
281 (when (eq encoding 'B) | |
282 ;; break into lines before encoding | |
283 (goto-char (point-min)) | |
284 (while (not (eobp)) | |
285 (goto-char (min (point-max) (+ 15 (point)))) | |
286 (unless (eobp) | |
287 (insert "\n")))) | |
288 (if (and (mm-multibyte-p) | |
35838
53eebdb81828
2001-02-01 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
35453
diff
changeset
|
289 (mm-coding-system-p cs)) |
53eebdb81828
2001-02-01 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
35453
diff
changeset
|
290 (mm-encode-coding-region (point-min) (point-max) cs)) |
31717 | 291 (funcall (cdr (assq encoding rfc2047-encoding-function-alist)) |
292 (point-min) (point-max)) | |
293 (goto-char (point-min)) | |
294 (while (not (eobp)) | |
295 (unless first | |
296 (insert " ")) | |
297 (setq first nil) | |
298 (insert start) | |
299 (end-of-line) | |
300 (insert "?=") | |
301 (forward-line 1))))) | |
302 | |
303 (defun rfc2047-fold-region (b e) | |
35985
b9c371244b90
(rfc2047-fold-region): Don't forward-char at EOB.
Dave Love <fx@gnu.org>
parents:
35838
diff
changeset
|
304 "Fold long lines in region B to E." |
31717 | 305 (save-restriction |
306 (narrow-to-region b e) | |
307 (goto-char (point-min)) | |
33304 | 308 (let ((break nil) |
309 (qword-break nil) | |
310 (bol (save-restriction | |
311 (widen) | |
312 (gnus-point-at-bol)))) | |
31717 | 313 (while (not (eobp)) |
33304 | 314 (when (and (or break qword-break) (> (- (point) bol) 76)) |
315 (goto-char (or break qword-break)) | |
316 (setq break nil | |
317 qword-break nil) | |
35453
26726eff41ca
2001-01-21 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
34674
diff
changeset
|
318 (if (looking-at " \t") |
26726eff41ca
2001-01-21 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
34674
diff
changeset
|
319 (insert "\n") |
26726eff41ca
2001-01-21 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
34674
diff
changeset
|
320 (insert "\n ")) |
33304 | 321 (setq bol (1- (point))) |
322 ;; Don't break before the first non-LWSP characters. | |
323 (skip-chars-forward " \t") | |
35985
b9c371244b90
(rfc2047-fold-region): Don't forward-char at EOB.
Dave Love <fx@gnu.org>
parents:
35838
diff
changeset
|
324 (unless (eobp) (forward-char 1))) |
31717 | 325 (cond |
33304 | 326 ((eq (char-after) ?\n) |
327 (forward-char 1) | |
328 (setq bol (point) | |
329 break nil | |
330 qword-break nil) | |
331 (skip-chars-forward " \t") | |
332 (unless (or (eobp) (eq (char-after) ?\n)) | |
333 (forward-char 1))) | |
334 ((eq (char-after) ?\r) | |
335 (forward-char 1)) | |
31717 | 336 ((memq (char-after) '(? ?\t)) |
33304 | 337 (skip-chars-forward " \t") |
338 (setq break (1- (point)))) | |
339 ((not break) | |
340 (if (not (looking-at "=\\?[^=]")) | |
341 (if (eq (char-after) ?=) | |
342 (forward-char 1) | |
343 (skip-chars-forward "^ \t\n\r=")) | |
344 (setq qword-break (point)) | |
345 (skip-chars-forward "^ \t\n\r"))) | |
346 (t | |
347 (skip-chars-forward "^ \t\n\r")))) | |
348 (when (and (or break qword-break) (> (- (point) bol) 76)) | |
349 (goto-char (or break qword-break)) | |
350 (setq break nil | |
351 qword-break nil) | |
35453
26726eff41ca
2001-01-21 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
34674
diff
changeset
|
352 (if (looking-at " \t") |
26726eff41ca
2001-01-21 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
34674
diff
changeset
|
353 (insert "\n") |
26726eff41ca
2001-01-21 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents:
34674
diff
changeset
|
354 (insert "\n ")) |
33304 | 355 (setq bol (1- (point))) |
356 ;; Don't break before the first non-LWSP characters. | |
357 (skip-chars-forward " \t") | |
35985
b9c371244b90
(rfc2047-fold-region): Don't forward-char at EOB.
Dave Love <fx@gnu.org>
parents:
35838
diff
changeset
|
358 (unless (eobp) (forward-char 1)))))) |
33304 | 359 |
360 (defun rfc2047-unfold-region (b e) | |
35985
b9c371244b90
(rfc2047-fold-region): Don't forward-char at EOB.
Dave Love <fx@gnu.org>
parents:
35838
diff
changeset
|
361 "Unfold lines in region B to E." |
33304 | 362 (save-restriction |
363 (narrow-to-region b e) | |
364 (goto-char (point-min)) | |
365 (let ((bol (save-restriction | |
366 (widen) | |
367 (gnus-point-at-bol))) | |
368 (eol (gnus-point-at-eol)) | |
369 leading) | |
370 (forward-line 1) | |
371 (while (not (eobp)) | |
372 (looking-at "[ \t]*") | |
373 (setq leading (- (match-end 0) (match-beginning 0))) | |
374 (if (< (- (gnus-point-at-eol) bol leading) 76) | |
375 (progn | |
376 (goto-char eol) | |
377 (delete-region eol (progn | |
378 (skip-chars-forward "[ \t\n\r]+") | |
379 (1- (point))))) | |
380 (setq bol (gnus-point-at-bol))) | |
381 (setq eol (gnus-point-at-eol)) | |
382 (forward-line 1))))) | |
31717 | 383 |
384 (defun rfc2047-b-encode-region (b e) | |
33304 | 385 "Base64-encode the header contained in region B to E." |
31717 | 386 (save-restriction |
387 (narrow-to-region (goto-char b) e) | |
388 (while (not (eobp)) | |
389 (base64-encode-region (point) (progn (end-of-line) (point)) t) | |
390 (if (and (bolp) (eolp)) | |
391 (delete-backward-char 1)) | |
392 (forward-line)))) | |
393 | |
394 (defun rfc2047-q-encode-region (b e) | |
33304 | 395 "Quoted-printable-encode the header in region B to E." |
31717 | 396 (save-excursion |
397 (save-restriction | |
398 (narrow-to-region (goto-char b) e) | |
33304 | 399 (let ((alist rfc2047-q-encoding-alist) |
400 (bol (save-restriction | |
401 (widen) | |
402 (gnus-point-at-bol)))) | |
31717 | 403 (while alist |
404 (when (looking-at (caar alist)) | |
405 (quoted-printable-encode-region b e nil (cdar alist)) | |
406 (subst-char-in-region (point-min) (point-max) ? ?_) | |
407 (setq alist nil)) | |
408 (pop alist)) | |
33304 | 409 ;; The size of QP encapsulation is about 20, so set limit to |
410 ;; 56=76-20. | |
411 (unless (< (- (point-max) (point-min)) 56) | |
412 ;; Don't break if it could fit in one line. | |
413 ;; Let rfc2047-encode-region break it later. | |
414 (goto-char (1+ (point-min))) | |
415 (while (and (not (bobp)) (not (eobp))) | |
416 (goto-char (min (point-max) (+ 56 bol))) | |
417 (search-backward "=" (- (point) 2) t) | |
418 (unless (or (bobp) (eobp)) | |
419 (insert "\n") | |
420 (setq bol (point))))))))) | |
31717 | 421 |
422 ;;; | |
423 ;;; Functions for decoding RFC2047 messages | |
424 ;;; | |
425 | |
426 (defvar rfc2047-encoded-word-regexp | |
427 "=\\?\\([^][\000-\040()<>@,\;:\\\"/?.=]+\\)\\?\\(B\\|Q\\)\\?\\([!->@-~ +]+\\)\\?=") | |
428 | |
429 (defun rfc2047-decode-region (start end) | |
430 "Decode MIME-encoded words in region between START and END." | |
431 (interactive "r") | |
432 (let ((case-fold-search t) | |
433 b e) | |
434 (save-excursion | |
435 (save-restriction | |
436 (narrow-to-region start end) | |
437 (goto-char (point-min)) | |
438 ;; Remove whitespace between encoded words. | |
439 (while (re-search-forward | |
440 (concat "\\(" rfc2047-encoded-word-regexp "\\)" | |
441 "\\(\n?[ \t]\\)+" | |
442 "\\(" rfc2047-encoded-word-regexp "\\)") | |
443 nil t) | |
444 (delete-region (goto-char (match-end 1)) (match-beginning 6))) | |
445 ;; Decode the encoded words. | |
446 (setq b (goto-char (point-min))) | |
447 (while (re-search-forward rfc2047-encoded-word-regexp nil t) | |
448 (setq e (match-beginning 0)) | |
449 (insert (rfc2047-parse-and-decode | |
450 (prog1 | |
451 (match-string 0) | |
452 (delete-region (match-beginning 0) (match-end 0))))) | |
453 (when (and (mm-multibyte-p) | |
454 mail-parse-charset | |
455 (not (eq mail-parse-charset 'gnus-decoded))) | |
456 (mm-decode-coding-region b e mail-parse-charset)) | |
457 (setq b (point))) | |
458 (when (and (mm-multibyte-p) | |
459 mail-parse-charset | |
460 (not (eq mail-parse-charset 'us-ascii)) | |
461 (not (eq mail-parse-charset 'gnus-decoded))) | |
33304 | 462 (mm-decode-coding-region b (point-max) mail-parse-charset)) |
463 (rfc2047-unfold-region (point-min) (point-max)))))) | |
31717 | 464 |
465 (defun rfc2047-decode-string (string) | |
466 "Decode the quoted-printable-encoded STRING and return the results." | |
467 (let ((m (mm-multibyte-p))) | |
468 (with-temp-buffer | |
469 (when m | |
470 (mm-enable-multibyte)) | |
471 (insert string) | |
472 (inline | |
473 (rfc2047-decode-region (point-min) (point-max))) | |
474 (buffer-string)))) | |
475 | |
476 (defun rfc2047-parse-and-decode (word) | |
477 "Decode WORD and return it if it is an encoded word. | |
478 Return WORD if not." | |
479 (if (not (string-match rfc2047-encoded-word-regexp word)) | |
480 word | |
481 (or | |
482 (condition-case nil | |
483 (rfc2047-decode | |
484 (match-string 1 word) | |
485 (upcase (match-string 2 word)) | |
486 (match-string 3 word)) | |
487 (error word)) | |
488 word))) | |
489 | |
490 (defun rfc2047-decode (charset encoding string) | |
33304 | 491 "Decode STRING from the given MIME CHARSET in the given ENCODING. |
31717 | 492 Valid ENCODINGs are \"B\" and \"Q\". |
33304 | 493 If your Emacs implementation can't decode CHARSET, return nil." |
31717 | 494 (if (stringp charset) |
495 (setq charset (intern (downcase charset)))) | |
33304 | 496 (if (or (not charset) |
31717 | 497 (eq 'gnus-all mail-parse-ignored-charsets) |
498 (memq 'gnus-all mail-parse-ignored-charsets) | |
499 (memq charset mail-parse-ignored-charsets)) | |
500 (setq charset mail-parse-charset)) | |
501 (let ((cs (mm-charset-to-coding-system charset))) | |
33304 | 502 (if (and (not cs) charset |
31717 | 503 (listp mail-parse-ignored-charsets) |
504 (memq 'gnus-unknown mail-parse-ignored-charsets)) | |
505 (setq cs (mm-charset-to-coding-system mail-parse-charset))) | |
506 (when cs | |
507 (when (and (eq cs 'ascii) | |
508 mail-parse-charset) | |
509 (setq cs mail-parse-charset)) | |
33304 | 510 ;; Ensure unibyte result in Emacs 20. |
511 (let (default-enable-multibyte-characters) | |
512 (with-temp-buffer | |
513 (mm-decode-coding-string | |
514 (cond | |
515 ((equal "B" encoding) | |
516 (base64-decode-string string)) | |
517 ((equal "Q" encoding) | |
518 (quoted-printable-decode-string | |
519 (mm-replace-chars-in-string string ?_ ? ))) | |
520 (t (error "Invalid encoding: %s" encoding))) | |
521 cs)))))) | |
31717 | 522 |
523 (provide 'rfc2047) | |
524 | |
525 ;;; rfc2047.el ends here |