]> code.delx.au - gnu-emacs/blob - lisp/gnus/rfc2047.el
(rfc2047-charset-encoding-alist): Use B for Hebrew.
[gnu-emacs] / lisp / gnus / rfc2047.el
1 ;;; rfc2047.el --- functions for encoding and decoding rfc2047 messages
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 ;; RFC 2047 is "MIME (Multipurpose Internet Mail Extensions) Part
26 ;; Three: Message Header Extensions for Non-ASCII Text".
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'qp)
33 (require 'mm-util)
34 (require 'ietf-drums)
35 (require 'mail-prsvr)
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")
40
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
47 header regexps or t.
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)
66 (iso-8859-8 . B)
67 (iso-8859-9 . Q)
68 (iso-8859-14 . Q)
69 (iso-8859-15 . Q)
70 (iso-2022-jp . B)
71 (iso-2022-kr . B)
72 (gb2312 . B)
73 (big5 . B)
74 (cn-big5 . B)
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.
81 Valid encodings are nil, `Q' and `B'. These indicate binary (no) encoding,
82 quoted-printable and base64 respectively.")
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
91 '(("\\(From\\|Cc\\|To\\|Bcc\||Reply-To\\):" . "-A-Za-z0-9!*+/")
92 ;; = (\075), _ (\137), ? (\077) are used in the encoded word.
93 ;; Avoid using 8bit characters.
94 ;; Equivalent to "^\000-\007\011\013\015-\037\200-\377=_?"
95 ("." . "\010\012\014\040-\074\076\100-\136\140-\177"))
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?
133 (mm-encode-coding-region
134 (point-min) (point-max)
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)
147 (rfc2047-encode-region (point-min) (point-max)))
148 ((eq method 'default)
149 (if (and (featurep 'mule)
150 (if (boundp 'default-enable-multibyte-characters)
151 default-enable-multibyte-characters)
152 mail-parse-charset)
153 (mm-encode-coding-region (point-min) (point-max)
154 mail-parse-charset)))
155 ((mm-coding-system-p method)
156 (if (and (featurep 'mule)
157 (if (boundp 'default-enable-multibyte-characters)
158 default-enable-multibyte-characters))
159 (mm-encode-coding-region (point-min) (point-max) method)))
160 ;; Hm.
161 (t)))
162 (goto-char (point-max)))))))
163
164 ;; Fixme: This, and the require below may not be the Right Thing, but
165 ;; should be safe just before release. -- fx 2001-02-08
166 (eval-when-compile (defvar message-posting-charset))
167
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."
171 (require 'message) ; for message-posting-charset
172 (let ((charsets
173 (mm-find-mime-charset-region (point-min) (point-max))))
174 (and charsets (not (equal charsets (list message-posting-charset))))))
175
176 (defun rfc2047-dissect-region (b e)
177 "Dissect the region between B and E into words."
178 (let ((word-chars "-A-Za-z0-9!*+/")
179 ;; Not using ietf-drums-specials-token makes life simple.
180 mail-parse-mule-charset
181 words point nonascii
182 result word)
183 (save-restriction
184 (narrow-to-region b e)
185 (goto-char (point-min))
186 (skip-chars-forward "\000-\177")
187 ;; Fixme: This loop used to check charsets when it found
188 ;; non-ASCII characters. That's removed, since it doesn't make
189 ;; much sense in Emacs 22 and doesn't seem necessary in Emacs
190 ;; 21, even. I'm not sure exactly what it should be doing, and
191 ;; it needs another look, especially for efficiency's sake. -- fx
192 (while (not (eobp))
193 (setq point (point)
194 nonascii nil)
195 (skip-chars-backward word-chars b)
196 (unless (eq b (point))
197 (push (cons (buffer-substring b (point)) nil) words))
198 (setq b (point)
199 nonascii t)
200 (goto-char point)
201 (forward-char 1)
202 (skip-chars-forward word-chars)
203 (while (not (eobp))
204 (forward-char 1)
205 (skip-chars-forward word-chars))
206 (unless (eq b (point))
207 (push (cons (buffer-substring b (point)) nonascii) words))
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))
234
235 (defun rfc2047-encode-region (b e)
236 "Encode all encodable words in region B to E."
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)
253 (progn (insert (car word)) (point)))))
254 (rfc2047-fold-region (point-min) (point-max)))))
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
263 (defun rfc2047-encode (b e)
264 "Encode the word in the region B to E."
265 (let* ((buff (current-buffer))
266 (mime-charset (with-temp-buffer
267 (insert-buffer-substring buff b e)
268 (mm-find-mime-charset-region b e)))
269 (cs (if (> (length mime-charset) 1)
270 (mm-charset-to-coding-system mime-charset)
271 (error "Can't encode word: %s" (buffer-substring b e))))
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)
289 (mm-coding-system-p cs))
290 (mm-encode-coding-region (point-min) (point-max) cs))
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)
304 "Fold long lines in region B to E."
305 (save-restriction
306 (narrow-to-region b e)
307 (goto-char (point-min))
308 (let ((break nil)
309 (qword-break nil)
310 (bol (save-restriction
311 (widen)
312 (gnus-point-at-bol))))
313 (while (not (eobp))
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)
318 (if (looking-at " \t")
319 (insert "\n")
320 (insert "\n "))
321 (setq bol (1- (point)))
322 ;; Don't break before the first non-LWSP characters.
323 (skip-chars-forward " \t")
324 (unless (eobp) (forward-char 1)))
325 (cond
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))
336 ((memq (char-after) '(? ?\t))
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)
352 (if (looking-at " \t")
353 (insert "\n")
354 (insert "\n "))
355 (setq bol (1- (point)))
356 ;; Don't break before the first non-LWSP characters.
357 (skip-chars-forward " \t")
358 (unless (eobp) (forward-char 1))))))
359
360 (defun rfc2047-unfold-region (b e)
361 "Unfold lines in region B to E."
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)))))
383
384 (defun rfc2047-b-encode-region (b e)
385 "Base64-encode the header contained in region B to E."
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)
395 "Quoted-printable-encode the header in region B to E."
396 (save-excursion
397 (save-restriction
398 (narrow-to-region (goto-char b) e)
399 (let ((alist rfc2047-q-encoding-alist)
400 (bol (save-restriction
401 (widen)
402 (gnus-point-at-bol))))
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))
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)))))))))
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)))
462 (mm-decode-coding-region b (point-max) mail-parse-charset))
463 (rfc2047-unfold-region (point-min) (point-max))))))
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)
491 "Decode STRING from the given MIME CHARSET in the given ENCODING.
492 Valid ENCODINGs are \"B\" and \"Q\".
493 If your Emacs implementation can't decode CHARSET, return nil."
494 (if (stringp charset)
495 (setq charset (intern (downcase charset))))
496 (if (or (not charset)
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)))
502 (if (and (not cs) charset
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))
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))))))
522
523 (provide 'rfc2047)
524
525 ;;; rfc2047.el ends here