Add some commentary.
[bpt/emacs.git] / 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 . Q)
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 (cn-gb . B)
74 (cn-gb-2312 . B)
75 (euc-kr . B)
76 (iso-2022-jp-2 . B)
77 (iso-2022-int-1 . B))
78 "Alist of MIME charsets to RFC2047 encodings.
79 Valid encodings are nil, `Q' and `B'.")
80
81 (defvar rfc2047-encoding-function-alist
82 '((Q . rfc2047-q-encode-region)
83 (B . rfc2047-b-encode-region)
84 (nil . ignore))
85 "Alist of RFC2047 encodings to encoding functions.")
86
87 (defvar rfc2047-q-encoding-alist
88 '(("\\(From\\|Cc\\|To\\|Bcc\||Reply-To\\):" . "-A-Za-z0-9!*+/")
89 ;; = (\075), _ (\137), ? (\077) are used in the encoded word.
90 ;; Avoid using 8bit characters. Some versions of Emacs has bug!
91 ;; Equivalent to "^\000-\007\011\013\015-\037\200-\377=_?"
92 ("." . "\010\012\014\040-\074\076\100-\136\140-\177"))
93 "Alist of header regexps and valid Q characters.")
94
95 ;;;
96 ;;; Functions for encoding RFC2047 messages
97 ;;;
98
99 (defun rfc2047-narrow-to-field ()
100 "Narrow the buffer to the header on the current line."
101 (beginning-of-line)
102 (narrow-to-region
103 (point)
104 (progn
105 (forward-line 1)
106 (if (re-search-forward "^[^ \n\t]" nil t)
107 (progn
108 (beginning-of-line)
109 (point))
110 (point-max))))
111 (goto-char (point-min)))
112
113 (defun rfc2047-encode-message-header ()
114 "Encode the message header according to `rfc2047-header-encoding-alist'.
115 Should be called narrowed to the head of the message."
116 (interactive "*")
117 (save-excursion
118 (goto-char (point-min))
119 (let (alist elem method)
120 (while (not (eobp))
121 (save-restriction
122 (rfc2047-narrow-to-field)
123 (if (not (rfc2047-encodable-p))
124 (if (and (eq (mm-body-7-or-8) '8bit)
125 (mm-multibyte-p)
126 (mm-coding-system-p
127 (car message-posting-charset)))
128 ;; 8 bit must be decoded.
129 ;; Is message-posting-charset a coding system?
130 (mm-encode-coding-region
131 (point-min) (point-max)
132 (car message-posting-charset)))
133 ;; We found something that may perhaps be encoded.
134 (setq method nil
135 alist rfc2047-header-encoding-alist)
136 (while (setq elem (pop alist))
137 (when (or (and (stringp (car elem))
138 (looking-at (car elem)))
139 (eq (car elem) t))
140 (setq alist nil
141 method (cdr elem))))
142 (cond
143 ((eq method 'mime)
144 (rfc2047-encode-region (point-min) (point-max)))
145 ((eq method 'default)
146 (if (and (featurep 'mule)
147 (if (boundp 'default-enable-multibyte-characters)
148 default-enable-multibyte-characters)
149 mail-parse-charset)
150 (mm-encode-coding-region (point-min) (point-max)
151 mail-parse-charset)))
152 ((mm-coding-system-p method)
153 (if (and (featurep 'mule)
154 (if (boundp 'default-enable-multibyte-characters)
155 default-enable-multibyte-characters))
156 (mm-encode-coding-region (point-min) (point-max) method)))
157 ;; Hm.
158 (t)))
159 (goto-char (point-max)))))))
160
161 (defun rfc2047-encodable-p ()
162 "Return non-nil if any characters in current buffer need encoding in headers.
163 The buffer may be narrowed."
164 (let ((charsets
165 (mapcar
166 'mm-mime-charset
167 (mm-find-charset-region (point-min) (point-max))))
168 (cs (list 'us-ascii (car message-posting-charset)))
169 found)
170 (while charsets
171 (unless (memq (pop charsets) cs)
172 (setq found t)))
173 found))
174
175 (defun rfc2047-dissect-region (b e)
176 "Dissect the region between B and E into words."
177 (let ((word-chars "-A-Za-z0-9!*+/")
178 ;; Not using ietf-drums-specials-token makes life simple.
179 mail-parse-mule-charset
180 words point current
181 result word)
182 (save-restriction
183 (narrow-to-region b e)
184 (goto-char (point-min))
185 (skip-chars-forward "\000-\177")
186 (while (not (eobp))
187 (setq point (point))
188 (skip-chars-backward word-chars b)
189 (unless (eq b (point))
190 (push (cons (buffer-substring b (point)) nil) words))
191 (setq b (point))
192 (goto-char point)
193 (setq current (mm-charset-after))
194 (forward-char 1)
195 (skip-chars-forward word-chars)
196 (while (and (not (eobp))
197 (eq (mm-charset-after) current))
198 (forward-char 1)
199 (skip-chars-forward word-chars))
200 (unless (eq b (point))
201 (push (cons (buffer-substring b (point)) current) words))
202 (setq b (point))
203 (skip-chars-forward "\000-\177"))
204 (unless (eq b (point))
205 (push (cons (buffer-substring b (point)) nil) words)))
206 ;; merge adjacent words
207 (setq word (pop words))
208 (while word
209 (if (and (cdr word)
210 (caar words)
211 (not (cdar words))
212 (not (string-match "[^ \t]" (caar words))))
213 (if (eq (cdr (nth 1 words)) (cdr word))
214 (progn
215 (setq word (cons (concat
216 (car (nth 1 words)) (caar words)
217 (car word))
218 (cdr word)))
219 (pop words)
220 (pop words))
221 (push (cons (concat (caar words) (car word)) (cdr word))
222 result)
223 (pop words)
224 (setq word (pop words)))
225 (push word result)
226 (setq word (pop words))))
227 result))
228
229 (defun rfc2047-encode-region (b e)
230 "Encode all encodable words in region."
231 (let ((words (rfc2047-dissect-region b e)) word)
232 (save-restriction
233 (narrow-to-region b e)
234 (delete-region (point-min) (point-max))
235 (while (setq word (pop words))
236 (if (not (cdr word))
237 (insert (car word))
238 (rfc2047-fold-region (gnus-point-at-bol) (point))
239 (goto-char (point-max))
240 (if (> (- (point) (save-restriction
241 (widen)
242 (gnus-point-at-bol))) 76)
243 (insert "\n "))
244 ;; Insert blank between encoded words
245 (if (eq (char-before) ?=) (insert " "))
246 (rfc2047-encode (point)
247 (progn (insert (car word)) (point))
248 (cdr word))))
249 (rfc2047-fold-region (point-min) (point-max)))))
250
251 (defun rfc2047-encode-string (string)
252 "Encode words in STRING."
253 (with-temp-buffer
254 (insert string)
255 (rfc2047-encode-region (point-min) (point-max))
256 (buffer-string)))
257
258 (defun rfc2047-encode (b e charset)
259 "Encode the word in the region B to E with CHARSET."
260 (let* ((mime-charset (mm-mime-charset charset))
261 (encoding (or (cdr (assq mime-charset
262 rfc2047-charset-encoding-alist))
263 'B))
264 (start (concat
265 "=?" (downcase (symbol-name mime-charset)) "?"
266 (downcase (symbol-name encoding)) "?"))
267 (first t))
268 (save-restriction
269 (narrow-to-region b e)
270 (when (eq encoding 'B)
271 ;; break into lines before encoding
272 (goto-char (point-min))
273 (while (not (eobp))
274 (goto-char (min (point-max) (+ 15 (point))))
275 (unless (eobp)
276 (insert "\n"))))
277 (if (and (mm-multibyte-p)
278 (mm-coding-system-p mime-charset))
279 (mm-encode-coding-region (point-min) (point-max) mime-charset))
280 (funcall (cdr (assq encoding rfc2047-encoding-function-alist))
281 (point-min) (point-max))
282 (goto-char (point-min))
283 (while (not (eobp))
284 (unless first
285 (insert " "))
286 (setq first nil)
287 (insert start)
288 (end-of-line)
289 (insert "?=")
290 (forward-line 1)))))
291
292 (defun rfc2047-fold-region (b e)
293 "Fold long lines in the region."
294 (save-restriction
295 (narrow-to-region b e)
296 (goto-char (point-min))
297 (let ((break nil)
298 (qword-break nil)
299 (bol (save-restriction
300 (widen)
301 (gnus-point-at-bol))))
302 (while (not (eobp))
303 (when (and (or break qword-break) (> (- (point) bol) 76))
304 (goto-char (or break qword-break))
305 (setq break nil
306 qword-break nil)
307 (insert "\n ")
308 (setq bol (1- (point)))
309 ;; Don't break before the first non-LWSP characters.
310 (skip-chars-forward " \t")
311 (forward-char 1))
312 (cond
313 ((eq (char-after) ?\n)
314 (forward-char 1)
315 (setq bol (point)
316 break nil
317 qword-break nil)
318 (skip-chars-forward " \t")
319 (unless (or (eobp) (eq (char-after) ?\n))
320 (forward-char 1)))
321 ((eq (char-after) ?\r)
322 (forward-char 1))
323 ((memq (char-after) '(? ?\t))
324 (skip-chars-forward " \t")
325 (setq break (1- (point))))
326 ((not break)
327 (if (not (looking-at "=\\?[^=]"))
328 (if (eq (char-after) ?=)
329 (forward-char 1)
330 (skip-chars-forward "^ \t\n\r="))
331 (setq qword-break (point))
332 (skip-chars-forward "^ \t\n\r")))
333 (t
334 (skip-chars-forward "^ \t\n\r"))))
335 (when (and (or break qword-break) (> (- (point) bol) 76))
336 (goto-char (or break qword-break))
337 (setq break nil
338 qword-break nil)
339 (insert "\n ")
340 (setq bol (1- (point)))
341 ;; Don't break before the first non-LWSP characters.
342 (skip-chars-forward " \t")
343 (forward-char 1)))))
344
345 (defun rfc2047-unfold-region (b e)
346 "Unfold lines in the region."
347 (save-restriction
348 (narrow-to-region b e)
349 (goto-char (point-min))
350 (let ((bol (save-restriction
351 (widen)
352 (gnus-point-at-bol)))
353 (eol (gnus-point-at-eol))
354 leading)
355 (forward-line 1)
356 (while (not (eobp))
357 (looking-at "[ \t]*")
358 (setq leading (- (match-end 0) (match-beginning 0)))
359 (if (< (- (gnus-point-at-eol) bol leading) 76)
360 (progn
361 (goto-char eol)
362 (delete-region eol (progn
363 (skip-chars-forward "[ \t\n\r]+")
364 (1- (point)))))
365 (setq bol (gnus-point-at-bol)))
366 (setq eol (gnus-point-at-eol))
367 (forward-line 1)))))
368
369 (defun rfc2047-b-encode-region (b e)
370 "Base64-encode the header contained in region B to E."
371 (save-restriction
372 (narrow-to-region (goto-char b) e)
373 (while (not (eobp))
374 (base64-encode-region (point) (progn (end-of-line) (point)) t)
375 (if (and (bolp) (eolp))
376 (delete-backward-char 1))
377 (forward-line))))
378
379 (defun rfc2047-q-encode-region (b e)
380 "Quoted-printable-encode the header in region B to E."
381 (save-excursion
382 (save-restriction
383 (narrow-to-region (goto-char b) e)
384 (let ((alist rfc2047-q-encoding-alist)
385 (bol (save-restriction
386 (widen)
387 (gnus-point-at-bol))))
388 (while alist
389 (when (looking-at (caar alist))
390 (quoted-printable-encode-region b e nil (cdar alist))
391 (subst-char-in-region (point-min) (point-max) ? ?_)
392 (setq alist nil))
393 (pop alist))
394 ;; The size of QP encapsulation is about 20, so set limit to
395 ;; 56=76-20.
396 (unless (< (- (point-max) (point-min)) 56)
397 ;; Don't break if it could fit in one line.
398 ;; Let rfc2047-encode-region break it later.
399 (goto-char (1+ (point-min)))
400 (while (and (not (bobp)) (not (eobp)))
401 (goto-char (min (point-max) (+ 56 bol)))
402 (search-backward "=" (- (point) 2) t)
403 (unless (or (bobp) (eobp))
404 (insert "\n")
405 (setq bol (point)))))))))
406
407 ;;;
408 ;;; Functions for decoding RFC2047 messages
409 ;;;
410
411 (defvar rfc2047-encoded-word-regexp
412 "=\\?\\([^][\000-\040()<>@,\;:\\\"/?.=]+\\)\\?\\(B\\|Q\\)\\?\\([!->@-~ +]+\\)\\?=")
413
414 (defun rfc2047-decode-region (start end)
415 "Decode MIME-encoded words in region between START and END."
416 (interactive "r")
417 (let ((case-fold-search t)
418 b e)
419 (save-excursion
420 (save-restriction
421 (narrow-to-region start end)
422 (goto-char (point-min))
423 ;; Remove whitespace between encoded words.
424 (while (re-search-forward
425 (concat "\\(" rfc2047-encoded-word-regexp "\\)"
426 "\\(\n?[ \t]\\)+"
427 "\\(" rfc2047-encoded-word-regexp "\\)")
428 nil t)
429 (delete-region (goto-char (match-end 1)) (match-beginning 6)))
430 ;; Decode the encoded words.
431 (setq b (goto-char (point-min)))
432 (while (re-search-forward rfc2047-encoded-word-regexp nil t)
433 (setq e (match-beginning 0))
434 (insert (rfc2047-parse-and-decode
435 (prog1
436 (match-string 0)
437 (delete-region (match-beginning 0) (match-end 0)))))
438 (when (and (mm-multibyte-p)
439 mail-parse-charset
440 (not (eq mail-parse-charset 'gnus-decoded)))
441 (mm-decode-coding-region b e mail-parse-charset))
442 (setq b (point)))
443 (when (and (mm-multibyte-p)
444 mail-parse-charset
445 (not (eq mail-parse-charset 'us-ascii))
446 (not (eq mail-parse-charset 'gnus-decoded)))
447 (mm-decode-coding-region b (point-max) mail-parse-charset))
448 (rfc2047-unfold-region (point-min) (point-max))))))
449
450 (defun rfc2047-decode-string (string)
451 "Decode the quoted-printable-encoded STRING and return the results."
452 (let ((m (mm-multibyte-p)))
453 (with-temp-buffer
454 (when m
455 (mm-enable-multibyte))
456 (insert string)
457 (inline
458 (rfc2047-decode-region (point-min) (point-max)))
459 (buffer-string))))
460
461 (defun rfc2047-parse-and-decode (word)
462 "Decode WORD and return it if it is an encoded word.
463 Return WORD if not."
464 (if (not (string-match rfc2047-encoded-word-regexp word))
465 word
466 (or
467 (condition-case nil
468 (rfc2047-decode
469 (match-string 1 word)
470 (upcase (match-string 2 word))
471 (match-string 3 word))
472 (error word))
473 word)))
474
475 (defun rfc2047-decode (charset encoding string)
476 "Decode STRING from the given MIME CHARSET in the given ENCODING.
477 Valid ENCODINGs are \"B\" and \"Q\".
478 If your Emacs implementation can't decode CHARSET, return nil."
479 (if (stringp charset)
480 (setq charset (intern (downcase charset))))
481 (if (or (not charset)
482 (eq 'gnus-all mail-parse-ignored-charsets)
483 (memq 'gnus-all mail-parse-ignored-charsets)
484 (memq charset mail-parse-ignored-charsets))
485 (setq charset mail-parse-charset))
486 (let ((cs (mm-charset-to-coding-system charset)))
487 (if (and (not cs) charset
488 (listp mail-parse-ignored-charsets)
489 (memq 'gnus-unknown mail-parse-ignored-charsets))
490 (setq cs (mm-charset-to-coding-system mail-parse-charset)))
491 (when cs
492 (when (and (eq cs 'ascii)
493 mail-parse-charset)
494 (setq cs mail-parse-charset))
495 ;; Ensure unibyte result in Emacs 20.
496 (let (default-enable-multibyte-characters)
497 (with-temp-buffer
498 (mm-decode-coding-string
499 (cond
500 ((equal "B" encoding)
501 (base64-decode-string string))
502 ((equal "Q" encoding)
503 (quoted-printable-decode-string
504 (mm-replace-chars-in-string string ?_ ? )))
505 (t (error "Invalid encoding: %s" encoding)))
506 cs))))))
507
508 (provide 'rfc2047)
509
510 ;;; rfc2047.el ends here