Revert last change.
[emacs.git] / lisp / gnus / rfc2047.el
blobf171ba4e068fa124fd9ae4403d1873528f6ec7a6
1 ;;; rfc2047.el --- Functions for encoding and decoding rfc2047 messages
2 ;; Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
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.
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.
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.
23 ;;; Commentary:
25 ;; RFC 2047 is "MIME (Multipurpose Internet Mail Extensions) Part
26 ;; Three: Message Header Extensions for Non-ASCII Text".
28 ;;; Code:
30 (eval-when-compile (require 'cl))
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")
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.
49 The values can be:
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.")
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'.")
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.")
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.")
95 ;;;
96 ;;; Functions for encoding RFC2047 messages
97 ;;;
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)))
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)))))))
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))
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))
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)))))
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)))
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)))))
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 (if (looking-at " \t")
308 (insert "\n")
309 (insert "\n "))
310 (setq bol (1- (point)))
311 ;; Don't break before the first non-LWSP characters.
312 (skip-chars-forward " \t")
313 (forward-char 1))
314 (cond
315 ((eq (char-after) ?\n)
316 (forward-char 1)
317 (setq bol (point)
318 break nil
319 qword-break nil)
320 (skip-chars-forward " \t")
321 (unless (or (eobp) (eq (char-after) ?\n))
322 (forward-char 1)))
323 ((eq (char-after) ?\r)
324 (forward-char 1))
325 ((memq (char-after) '(? ?\t))
326 (skip-chars-forward " \t")
327 (setq break (1- (point))))
328 ((not break)
329 (if (not (looking-at "=\\?[^=]"))
330 (if (eq (char-after) ?=)
331 (forward-char 1)
332 (skip-chars-forward "^ \t\n\r="))
333 (setq qword-break (point))
334 (skip-chars-forward "^ \t\n\r")))
336 (skip-chars-forward "^ \t\n\r"))))
337 (when (and (or break qword-break) (> (- (point) bol) 76))
338 (goto-char (or break qword-break))
339 (setq break nil
340 qword-break nil)
341 (if (looking-at " \t")
342 (insert "\n")
343 (insert "\n "))
344 (setq bol (1- (point)))
345 ;; Don't break before the first non-LWSP characters.
346 (skip-chars-forward " \t")
347 (forward-char 1)))))
349 (defun rfc2047-unfold-region (b e)
350 "Unfold lines in the region."
351 (save-restriction
352 (narrow-to-region b e)
353 (goto-char (point-min))
354 (let ((bol (save-restriction
355 (widen)
356 (gnus-point-at-bol)))
357 (eol (gnus-point-at-eol))
358 leading)
359 (forward-line 1)
360 (while (not (eobp))
361 (looking-at "[ \t]*")
362 (setq leading (- (match-end 0) (match-beginning 0)))
363 (if (< (- (gnus-point-at-eol) bol leading) 76)
364 (progn
365 (goto-char eol)
366 (delete-region eol (progn
367 (skip-chars-forward "[ \t\n\r]+")
368 (1- (point)))))
369 (setq bol (gnus-point-at-bol)))
370 (setq eol (gnus-point-at-eol))
371 (forward-line 1)))))
373 (defun rfc2047-b-encode-region (b e)
374 "Base64-encode the header contained in region B to E."
375 (save-restriction
376 (narrow-to-region (goto-char b) e)
377 (while (not (eobp))
378 (base64-encode-region (point) (progn (end-of-line) (point)) t)
379 (if (and (bolp) (eolp))
380 (delete-backward-char 1))
381 (forward-line))))
383 (defun rfc2047-q-encode-region (b e)
384 "Quoted-printable-encode the header in region B to E."
385 (save-excursion
386 (save-restriction
387 (narrow-to-region (goto-char b) e)
388 (let ((alist rfc2047-q-encoding-alist)
389 (bol (save-restriction
390 (widen)
391 (gnus-point-at-bol))))
392 (while alist
393 (when (looking-at (caar alist))
394 (quoted-printable-encode-region b e nil (cdar alist))
395 (subst-char-in-region (point-min) (point-max) ? ?_)
396 (setq alist nil))
397 (pop alist))
398 ;; The size of QP encapsulation is about 20, so set limit to
399 ;; 56=76-20.
400 (unless (< (- (point-max) (point-min)) 56)
401 ;; Don't break if it could fit in one line.
402 ;; Let rfc2047-encode-region break it later.
403 (goto-char (1+ (point-min)))
404 (while (and (not (bobp)) (not (eobp)))
405 (goto-char (min (point-max) (+ 56 bol)))
406 (search-backward "=" (- (point) 2) t)
407 (unless (or (bobp) (eobp))
408 (insert "\n")
409 (setq bol (point)))))))))
412 ;;; Functions for decoding RFC2047 messages
415 (defvar rfc2047-encoded-word-regexp
416 "=\\?\\([^][\000-\040()<>@,\;:\\\"/?.=]+\\)\\?\\(B\\|Q\\)\\?\\([!->@-~ +]+\\)\\?=")
418 (defun rfc2047-decode-region (start end)
419 "Decode MIME-encoded words in region between START and END."
420 (interactive "r")
421 (let ((case-fold-search t)
422 b e)
423 (save-excursion
424 (save-restriction
425 (narrow-to-region start end)
426 (goto-char (point-min))
427 ;; Remove whitespace between encoded words.
428 (while (re-search-forward
429 (concat "\\(" rfc2047-encoded-word-regexp "\\)"
430 "\\(\n?[ \t]\\)+"
431 "\\(" rfc2047-encoded-word-regexp "\\)")
432 nil t)
433 (delete-region (goto-char (match-end 1)) (match-beginning 6)))
434 ;; Decode the encoded words.
435 (setq b (goto-char (point-min)))
436 (while (re-search-forward rfc2047-encoded-word-regexp nil t)
437 (setq e (match-beginning 0))
438 (insert (rfc2047-parse-and-decode
439 (prog1
440 (match-string 0)
441 (delete-region (match-beginning 0) (match-end 0)))))
442 (when (and (mm-multibyte-p)
443 mail-parse-charset
444 (not (eq mail-parse-charset 'gnus-decoded)))
445 (mm-decode-coding-region b e mail-parse-charset))
446 (setq b (point)))
447 (when (and (mm-multibyte-p)
448 mail-parse-charset
449 (not (eq mail-parse-charset 'us-ascii))
450 (not (eq mail-parse-charset 'gnus-decoded)))
451 (mm-decode-coding-region b (point-max) mail-parse-charset))
452 (rfc2047-unfold-region (point-min) (point-max))))))
454 (defun rfc2047-decode-string (string)
455 "Decode the quoted-printable-encoded STRING and return the results."
456 (let ((m (mm-multibyte-p)))
457 (with-temp-buffer
458 (when m
459 (mm-enable-multibyte))
460 (insert string)
461 (inline
462 (rfc2047-decode-region (point-min) (point-max)))
463 (buffer-string))))
465 (defun rfc2047-parse-and-decode (word)
466 "Decode WORD and return it if it is an encoded word.
467 Return WORD if not."
468 (if (not (string-match rfc2047-encoded-word-regexp word))
469 word
471 (condition-case nil
472 (rfc2047-decode
473 (match-string 1 word)
474 (upcase (match-string 2 word))
475 (match-string 3 word))
476 (error word))
477 word)))
479 (defun rfc2047-decode (charset encoding string)
480 "Decode STRING from the given MIME CHARSET in the given ENCODING.
481 Valid ENCODINGs are \"B\" and \"Q\".
482 If your Emacs implementation can't decode CHARSET, return nil."
483 (if (stringp charset)
484 (setq charset (intern (downcase charset))))
485 (if (or (not charset)
486 (eq 'gnus-all mail-parse-ignored-charsets)
487 (memq 'gnus-all mail-parse-ignored-charsets)
488 (memq charset mail-parse-ignored-charsets))
489 (setq charset mail-parse-charset))
490 (let ((cs (mm-charset-to-coding-system charset)))
491 (if (and (not cs) charset
492 (listp mail-parse-ignored-charsets)
493 (memq 'gnus-unknown mail-parse-ignored-charsets))
494 (setq cs (mm-charset-to-coding-system mail-parse-charset)))
495 (when cs
496 (when (and (eq cs 'ascii)
497 mail-parse-charset)
498 (setq cs mail-parse-charset))
499 ;; Ensure unibyte result in Emacs 20.
500 (let (default-enable-multibyte-characters)
501 (with-temp-buffer
502 (mm-decode-coding-string
503 (cond
504 ((equal "B" encoding)
505 (base64-decode-string string))
506 ((equal "Q" encoding)
507 (quoted-printable-decode-string
508 (mm-replace-chars-in-string string ?_ ? )))
509 (t (error "Invalid encoding: %s" encoding)))
510 cs))))))
512 (provide 'rfc2047)
514 ;;; rfc2047.el ends here