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