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