1 ;;; rfc2047.el --- functions for encoding and decoding rfc2047 messages
3 ;; Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; RFC 2047 is "MIME (Multipurpose Internet Mail Extensions) Part
28 ;; Three: Message Header Extensions for Non-ASCII Text".
34 (defvar message-posting-charset))
39 ;; Fixme: Avoid this (used for mail-parse-charset) mm dependence on gnus.
42 (autoload 'mm-body-7-or-8 "mm-bodies")
44 (defvar rfc2047-header-encoding-alist
45 '(("Newsgroups" . nil)
48 ("\\(Resent-\\)?\\(From\\|Cc\\|To\\|Bcc\\|\\(In-\\)?Reply-To\\|Sender\
49 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\)" . address-mime)
51 "*Header/encoding method alist.
52 The list is traversed sequentially. The keys can either be
57 1) nil, in which case no encoding is done;
58 2) `mime', in which case the header will be encoded according to RFC2047;
59 3) `address-mime', like `mime', but takes account of the rules for address
60 fields (where quoted strings and comments must be treated separately);
61 4) a charset, in which case it will be encoded as that charset;
62 5) `default', in which case the field will be encoded as the rest
65 (defvar rfc2047-charset-encoding-alist
89 "Alist of MIME charsets to RFC2047 encodings.
90 Valid encodings are nil, `Q' and `B'. These indicate binary (no) encoding,
91 quoted-printable and base64 respectively.")
93 (defvar rfc2047-encode-function-alist
94 '((Q . rfc2047-q-encode-string)
95 (B . rfc2047-b-encode-string)
97 "Alist of RFC2047 encodings to encoding functions.")
99 (defvar rfc2047-encode-encoded-words t
100 "Whether encoded words should be encoded again.")
103 ;;; Functions for encoding RFC2047 messages
106 (defun rfc2047-qp-or-base64 ()
107 "Return the type with which to encode the buffer.
108 This is either `base64' or `quoted-printable'."
110 (let ((limit (min (point-max) (+ 2000 (point-min))))
112 (goto-char (point-min))
113 (skip-chars-forward "\x20-\x7f\r\n\t" limit)
114 (while (< (point) limit)
117 (skip-chars-forward "\x20-\x7f\r\n\t" limit))
118 (if (or (< (* 6 n8bit) (- limit (point-min)))
119 ;; Don't base64, say, a short line with a single
120 ;; non-ASCII char when splitting parts by charset.
125 (defun rfc2047-narrow-to-field ()
126 "Narrow the buffer to the header on the current line."
132 (if (re-search-forward "^[^ \n\t]" nil t)
135 (goto-char (point-min)))
137 (defun rfc2047-field-value ()
138 "Return the value of the field at point."
141 (rfc2047-narrow-to-field)
142 (re-search-forward ":[ \t\n]*" nil t)
143 (buffer-substring-no-properties (point) (point-max)))))
145 (defvar rfc2047-encoding-type 'address-mime
146 "The type of encoding done by `rfc2047-encode-region'.
147 This should be dynamically bound around calls to
148 `rfc2047-encode-region' to either `mime' or `address-mime'. See
149 `rfc2047-header-encoding-alist', for definitions.")
151 (defun rfc2047-encode-message-header ()
152 "Encode the message header according to `rfc2047-header-encoding-alist'.
153 Should be called narrowed to the head of the message."
156 (goto-char (point-min))
157 (let (alist elem method)
160 (rfc2047-narrow-to-field)
161 (if (not (rfc2047-encodable-p))
163 (if (and (eq (mm-body-7-or-8) '8bit)
166 (car message-posting-charset)))
167 ;; 8 bit must be decoded.
168 (mm-encode-coding-region
169 (point-min) (point-max)
170 (mm-charset-to-coding-system
171 (car message-posting-charset))))
172 ;; No encoding necessary, but folding is nice
176 (goto-char (point-min))
177 (skip-chars-forward "^:")
178 (when (looking-at ": ")
182 ;; We found something that may perhaps be encoded.
184 alist rfc2047-header-encoding-alist)
185 (while (setq elem (pop alist))
186 (when (or (and (stringp (car elem))
187 (looking-at (car elem)))
191 (re-search-forward "^[^:]+: *" nil t)
193 ((eq method 'address-mime)
194 (rfc2047-encode-region (point) (point-max)))
196 (let ((rfc2047-encoding-type 'mime))
197 (rfc2047-encode-region (point) (point-max))))
198 ((eq method 'default)
199 (if (and (featurep 'mule)
200 (if (boundp 'default-enable-multibyte-characters)
201 default-enable-multibyte-characters)
203 (mm-encode-coding-region (point) (point-max)
204 mail-parse-charset)))
205 ;; We get this when CC'ing messsages to newsgroups with
206 ;; 8-bit names. The group name mail copy just got
207 ;; unconditionally encoded. Previously, it would ask
208 ;; whether to encode, which was quite confusing for the
209 ;; user. If the new behaviour is wrong, tell me. I have
210 ;; left the old code commented out below.
211 ;; -- Per Abrahamsen <abraham@dina.kvl.dk> Date: 2001-10-07.
212 ;; Modified by Dave Love, with the commented-out code changed
213 ;; in accordance with changes elsewhere.
215 (rfc2047-encode-region (point) (point-max)))
217 ;;; (if (or (message-options-get
218 ;;; 'rfc2047-encode-message-header-encode-any)
219 ;;; (message-options-set
220 ;;; 'rfc2047-encode-message-header-encode-any
222 ;;; "Some texts are not encoded. Encode anyway?")))
223 ;;; (rfc2047-encode-region (point-min) (point-max))
224 ;;; (error "Cannot send unencoded text")))
225 ((mm-coding-system-p method)
226 (if (and (featurep 'mule)
227 (if (boundp 'default-enable-multibyte-characters)
228 default-enable-multibyte-characters))
229 (mm-encode-coding-region (point) (point-max) method)))
232 (goto-char (point-max)))))))
234 ;; Fixme: This, and the require below may not be the Right Thing, but
235 ;; should be safe just before release. -- fx 2001-02-08
236 (eval-when-compile (defvar message-posting-charset))
238 (defun rfc2047-encodable-p ()
239 "Return non-nil if any characters in current buffer need encoding in headers.
240 The buffer may be narrowed."
241 (require 'message) ; for message-posting-charset
243 (mm-find-mime-charset-region (point-min) (point-max))))
244 (goto-char (point-min))
245 (or (and rfc2047-encode-encoded-words
247 (search-forward "=?" nil t)
248 (goto-char (point-min))))
250 (not (equal charsets (list (car message-posting-charset))))))))
252 ;; Use this syntax table when parsing into regions that may need
253 ;; encoding. Double quotes are string delimiters, backslash is
254 ;; character quoting, and all other RFC 2822 special characters are
255 ;; treated as punctuation so we can use forward-sexp/forward-word to
256 ;; skip to the end of regions appropriately. Nb. ietf-drums does
257 ;; things differently.
258 (defconst rfc2047-syntax-table
259 ;; (make-char-table 'syntax-table '(2)) only works in Emacs.
260 (let ((table (make-syntax-table)))
261 ;; The following is done to work for setting all elements of the table
262 ;; in Emacs 21 and 22 and XEmacs; it appears to be the cleanest way.
263 ;; Play safe and don't assume the form of the word syntax entry --
265 (if (fboundp 'set-char-table-range) ; Emacs
266 (funcall (intern "set-char-table-range")
267 table t (aref (standard-syntax-table) ?a))
268 (if (fboundp 'put-char-table)
269 (if (fboundp 'get-char-table) ; warning avoidance
270 (put-char-table t (get-char-table ?a (standard-syntax-table))
272 (modify-syntax-entry ?\\ "\\" table)
273 (modify-syntax-entry ?\" "\"" table)
274 (modify-syntax-entry ?\( "(" table)
275 (modify-syntax-entry ?\) ")" table)
276 (modify-syntax-entry ?\< "." table)
277 (modify-syntax-entry ?\> "." table)
278 (modify-syntax-entry ?\[ "." table)
279 (modify-syntax-entry ?\] "." table)
280 (modify-syntax-entry ?: "." table)
281 (modify-syntax-entry ?\; "." table)
282 (modify-syntax-entry ?, "." table)
283 (modify-syntax-entry ?@ "." table)
286 (defun rfc2047-encode-region (b e)
287 "Encode words in region B to E that need encoding.
288 By default, the region is treated as containing RFC2822 addresses.
289 Dynamically bind `rfc2047-encoding-type' to change that."
291 (narrow-to-region b e)
292 (let ((encodable-regexp (if rfc2047-encode-encoded-words
293 "[^\000-\177]+\\|=\\?"
295 start ; start of current token
297 ;; Whether there's an encoded word before the current token,
298 ;; either immediately or separated by space.
300 (orig-text (buffer-substring-no-properties b e)))
301 (if (eq 'mime rfc2047-encoding-type)
302 ;; Simple case. Continuous words in which all those contain
303 ;; non-ASCII characters are encoded collectively. Encoding
304 ;; ASCII words, including `Re:' used in Subject headers, is
305 ;; avoided for interoperability with non-MIME clients and
306 ;; for making it easy to find keywords.
308 (goto-char (point-min))
309 (while (progn (skip-chars-forward " \t\n")
312 (while (and (looking-at "[ \t\n]*\\([^ \t\n]+\\)")
314 (setq end (match-end 0))
315 (re-search-forward encodable-regexp end t)))
317 (if (> (point) start)
318 (rfc2047-encode start (point))
320 ;; `address-mime' case -- take care of quoted words, comments.
321 (with-syntax-table rfc2047-syntax-table
322 (goto-char (point-min))
323 (condition-case err ; in case of unbalanced quotes
324 ;; Look for rfc2822-style: sequences of atoms, quoted
325 ;; strings, specials, whitespace. (Specials mustn't be
329 (skip-chars-forward " \t\n")
332 ((not (char-after))) ; eob
334 ((eq ?\" (setq csyntax (char-syntax (char-after))))
338 ;; Does it need encoding?
340 (if (re-search-forward encodable-regexp end 'move)
341 ;; It needs encoding. Strip the quotes first,
342 ;; since encoded words can't occur in quotes.
345 (delete-backward-char 1)
349 ;; There was a preceding quoted word. We need
350 ;; to include any separating whitespace in this
351 ;; word to avoid it getting lost.
352 (skip-chars-backward " \t")
353 ;; A space is needed between the encoded words.
357 ;; Adjust the end position for the deleted quotes.
358 (rfc2047-encode start (- end 2))
359 (setq last-encoded t)) ; record that it was encoded
360 (setq last-encoded nil)))
362 ;; Skip other delimiters, but record that they've
363 ;; potentially separated quoted words.
365 (setq last-encoded nil))
367 (error "Unbalanced parentheses"))
369 ;; Look for the end of parentheses.
371 ;; Encode text as an unstructured field.
372 (let ((rfc2047-encoding-type 'mime))
373 (rfc2047-encode-region (1+ start) (1- (point))))
374 (skip-chars-forward ")"))
375 (t ; normal token/whitespace sequence
377 ;; Skip one ASCII word, or encode continuous words
378 ;; in which all those contain non-ASCII characters.
380 (while (not (or end (eobp)))
381 (when (looking-at "[\000-\177]+")
385 (while (and (or (re-search-forward
386 "[ \t\n]\\|\\Sw" end 'move)
388 (eq ?\\ (char-syntax (char-before))))
389 ;; Skip backslash-quoted characters.
392 (setq end (match-beginning 0))
393 (if rfc2047-encode-encoded-words
396 (when (search-forward "=?" end 'move)
397 (goto-char (match-beginning 0))
400 ;; Where the value nil of `end' means there may be
401 ;; text to have to be encoded following the point.
402 ;; Otherwise, the point reached to the end of ASCII
403 ;; words separated by whitespace or a special char.
405 (when (looking-at encodable-regexp)
406 (goto-char (setq begin (match-end 0)))
407 (while (and (looking-at "[ \t\n]+\\([^ \t\n]+\\)")
408 (setq end (match-end 0))
410 (while (re-search-forward
411 encodable-regexp end t))
414 (or (not (re-search-forward "\\Sw" end t))
416 (goto-char (match-beginning 0))
419 (when (looking-at "[^ \t\n]+")
420 (setq end (match-end 0))
421 (if (re-search-forward "\\Sw+" end t)
422 ;; There are special characters better
423 ;; to be encoded so that MTAs may parse
425 (cond ((= end (point)))
426 ((looking-at (concat "\\sw*\\("
431 (goto-char (1- (match-end 0)))
432 (unless (= (point) (match-beginning 0))
433 ;; Separate encodable text and
437 (skip-chars-forward " \t\n")
438 (if (and (looking-at "[^ \t\n]+")
439 (string-match encodable-regexp
442 (goto-char end)))))))
443 (skip-chars-backward " \t\n")
446 (if (re-search-forward encodable-regexp end 'move)
448 (unless (memq (char-before start) '(nil ?\t ? ))
451 (skip-chars-backward "^ \t\n")
452 (and (looking-at "\\Sw+")
453 (= (match-end 0) start)))
454 ;; Also encode bogus delimiters.
456 ;; Separate encodable text and delimiter.
459 (setq start (1+ start)
461 (rfc2047-encode start end)
462 (setq last-encoded t))
463 (setq last-encoded nil)))))
465 (if (or debug-on-quit debug-on-error)
466 (signal (car err) (cdr err))
467 (error "Invalid data for rfc2047 encoding: %s"
468 (mm-replace-in-string orig-text "[ \t\n]+" " "))))))))
469 (rfc2047-fold-region b (point))
470 (goto-char (point-max))))
472 (defun rfc2047-encode-string (string)
473 "Encode words in STRING.
474 By default, the string is treated as containing addresses (see
475 `rfc2047-encoding-type')."
476 (mm-with-multibyte-buffer
478 (rfc2047-encode-region (point-min) (point-max))
481 (defvar rfc2047-encode-max-chars 76
482 "Maximum characters of each header line that contain encoded-words.
483 If it is nil, encoded-words will not be folded. Too small value may
484 cause an error. Don't change this for no particular reason.")
486 (defun rfc2047-encode-1 (column string cs encoder start crest tail
488 "Subroutine used by `rfc2047-encode'."
489 (cond ((string-equal string "")
491 ((not rfc2047-encode-max-chars)
493 (funcall encoder (if cs
494 (mm-encode-coding-string string cs)
497 ((>= column rfc2047-encode-max-chars)
499 (cond ((string-match "\n[ \t]+\\'" eword)
500 ;; Reomove a superfluous empty line.
501 (setq eword (substring eword 0 (match-beginning 0))))
502 ((string-match "(+\\'" eword)
503 ;; Break the line before the open parenthesis.
504 (setq crest (concat crest (match-string 0 eword))
505 eword (substring eword 0 (match-beginning 0))))))
506 (rfc2047-encode-1 (length crest) string cs encoder start " " tail
507 (concat eword "\n" crest)))
510 (limit (1- (length string)))
515 (setq next (concat start
518 (mm-encode-coding-string
519 (substring string 0 (1+ index))
521 (substring string 0 (1+ index))))
523 len (+ column (length next)))
524 (if (> len rfc2047-encode-max-chars)
527 (if (or (< index limit)
528 (<= (+ len (or (string-match "\n" tail)
530 rfc2047-encode-max-chars))
533 (if (string-match "\\`)+" tail)
534 ;; Break the line after the close parenthesis.
535 (setq tail (concat (substring tail 0 (match-end 0))
537 (substring tail (match-end 0)))
543 (concat eword next tail)
546 (string-match "(+\\'" eword))
547 (setq crest (concat crest (match-string 0 eword))
548 eword (substring eword 0 (match-beginning 0)))
549 (setq eword (concat eword next)))
551 eword (concat eword next)))
552 (when (string-match "\n[ \t]+\\'" eword)
553 ;; Reomove a superfluous empty line.
554 (setq eword (substring eword 0 (match-beginning 0))))
555 (rfc2047-encode-1 (length crest) (substring string index)
556 cs encoder start " " tail
557 (concat eword "\n" crest)))))))
559 (defun rfc2047-encode (b e)
560 "Encode the word(s) in the region B to E.
561 Point moves to the end of the region."
562 (let ((mime-charset (or (mm-find-mime-charset-region b e) (list 'us-ascii)))
563 cs encoding tail crest eword)
564 (cond ((> (length mime-charset) 1)
565 (error "Can't rfc2047-encode `%s'"
566 (buffer-substring-no-properties b e)))
567 ((= (length mime-charset) 1)
568 (setq mime-charset (car mime-charset)
569 cs (mm-charset-to-coding-system mime-charset))
570 (unless (and (mm-multibyte-p)
571 (mm-coding-system-p cs))
574 (narrow-to-region b e)
576 (or (cdr (assq mime-charset
577 rfc2047-charset-encoding-alist))
578 ;; For the charsets that don't have a preferred
579 ;; encoding, choose the one that's shorter.
580 (if (eq (rfc2047-qp-or-base64) 'base64)
585 (skip-chars-forward "^ \t\n")
586 ;; `tail' may contain a close parenthesis.
587 (setq tail (buffer-substring-no-properties e (point)))
589 (setq b (point-marker)
590 e (set-marker (make-marker) e))
591 (rfc2047-fold-region (point-at-bol) b)
593 (skip-chars-backward "^ \t\n")
594 (unless (= 0 (skip-chars-backward " \t"))
595 ;; `crest' may contain whitespace and an open parenthesis.
596 (setq crest (buffer-substring-no-properties (point) b)))
597 (setq eword (rfc2047-encode-1
599 (mm-replace-in-string
600 (buffer-substring-no-properties b e)
601 "\n\\([ \t]?\\)" "\\1")
603 (or (cdr (assq encoding
604 rfc2047-encode-function-alist))
606 (concat "=?" (downcase (symbol-name mime-charset))
607 "?" (upcase (symbol-name encoding)) "?")
610 (delete-region (if (eq (aref eword 0) ?\n)
612 ;; The line was folded before encoding.
617 ;; `eword' contains `crest' and `tail'.
621 (unless (or (/= 0 (length tail))
623 (looking-at "[ \t\n)]"))
628 (defun rfc2047-fold-field ()
629 "Fold the current header field."
632 (rfc2047-narrow-to-field)
633 (rfc2047-fold-region (point-min) (point-max)))))
635 (defun rfc2047-fold-region (b e)
636 "Fold long lines in region B to E."
638 (narrow-to-region b e)
639 (goto-char (point-min))
643 (bol (save-restriction
647 (when (and (or break qword-break)
648 (> (- (point) bol) 76))
649 (goto-char (or break qword-break))
652 (skip-chars-backward " \t")
653 (if (looking-at "[ \t]")
656 (setq bol (1- (point)))
657 ;; Don't break before the first non-LWSP characters.
658 (skip-chars-forward " \t")
662 ((eq (char-after) ?\n)
667 (skip-chars-forward " \t")
668 (unless (or (eobp) (eq (char-after) ?\n))
670 ((eq (char-after) ?\r)
672 ((memq (char-after) '(? ?\t))
673 (skip-chars-forward " \t")
674 (unless first ;; Don't break just after the header name.
675 (setq break (point))))
677 (if (not (looking-at "=\\?[^=]"))
678 (if (eq (char-after) ?=)
680 (skip-chars-forward "^ \t\n\r="))
681 ;; Don't break at the start of the field.
682 (unless (= (point) b)
683 (setq qword-break (point)))
684 (skip-chars-forward "^ \t\n\r")))
686 (skip-chars-forward "^ \t\n\r")))
688 (when (and (or break qword-break)
689 (> (- (point) bol) 76))
690 (goto-char (or break qword-break))
693 (if (or (> 0 (skip-chars-backward " \t"))
694 (looking-at "[ \t]"))
697 (setq bol (1- (point)))
698 ;; Don't break before the first non-LWSP characters.
699 (skip-chars-forward " \t")
701 (forward-char 1))))))
703 (defun rfc2047-unfold-field ()
704 "Fold the current line."
707 (rfc2047-narrow-to-field)
708 (rfc2047-unfold-region (point-min) (point-max)))))
710 (defun rfc2047-unfold-region (b e)
711 "Unfold lines in region B to E."
713 (narrow-to-region b e)
714 (goto-char (point-min))
715 (let ((bol (save-restriction
718 (eol (point-at-eol)))
721 (if (and (looking-at "[ \t]")
722 (< (- (point-at-eol) bol) 76))
723 (delete-region eol (progn
725 (skip-chars-forward "\r\n")
727 (setq bol (point-at-bol)))
728 (setq eol (point-at-eol))
731 (defun rfc2047-b-encode-string (string)
732 "Base64-encode the header contained in STRING."
733 (base64-encode-string string t))
735 (defun rfc2047-q-encode-string (string)
736 "Quoted-printable-encode the header in STRING."
737 (mm-with-unibyte-buffer
739 (quoted-printable-encode-region
740 (point-min) (point-max) nil
741 ;; = (\075), _ (\137), ? (\077) are used in the encoded word.
742 ;; Avoid using 8bit characters.
743 ;; This list excludes `especials' (see the RFC2047 syntax),
744 ;; meaning that some characters in non-structured fields will
745 ;; get encoded when they con't need to be. The following is
746 ;; what it used to be.
747 ;;; ;; Equivalent to "^\000-\007\011\013\015-\037\200-\377=_?"
748 ;;; "\010\012\014\040-\074\076\100-\136\140-\177")
749 "-\b\n\f !#-'*+0-9A-Z\\^`-~\d")
750 (subst-char-in-region (point-min) (point-max) ? ?_)
753 (defun rfc2047-encode-parameter (param value)
754 "Return and PARAM=VALUE string encoded in the RFC2047-like style.
755 This is a replacement for the `rfc2231-encode-string' function.
757 When attaching files as MIME parts, we should use the RFC2231 encoding
758 to specify the file names containing non-ASCII characters. However,
759 many mail softwares don't support it in practice and recipients won't
760 be able to extract files with correct names. Instead, the RFC2047-like
761 encoding is acceptable generally. This function provides the very
762 RFC2047-like encoding, resigning to such a regrettable trend. To use
763 it, put the following line in your ~/.gnus.el file:
765 \(defalias 'mail-header-encode-parameter 'rfc2047-encode-parameter)
767 (let* ((rfc2047-encoding-type 'mime)
768 (rfc2047-encode-max-chars nil)
769 (string (rfc2047-encode-string value)))
770 (if (string-match (concat "[" ietf-drums-tspecials "]") string)
771 (format "%s=%S" param string)
772 (concat param "=" string))))
775 ;;; Functions for decoding RFC2047 messages
779 (defconst rfc2047-encoded-word-regexp
780 "=\\?\\([^][\000-\040()<>@,\;:*\\\"/?.=]+\\)\\(?:\\*[^?]+\\)?\
781 \\?\\(B\\|Q\\)\\?\\([!->@-~ ]*\\)\\?="))
783 (defvar rfc2047-quote-decoded-words-containing-tspecials nil
784 "If non-nil, quote decoded words containing special characters.")
786 (defvar rfc2047-allow-incomplete-encoded-text t
787 "*Non-nil means allow incomplete encoded-text in successive encoded-words.
788 Dividing of encoded-text in the place other than character boundaries
789 violates RFC2047 section 5, while we have a capability to decode it.
790 If it is non-nil, the decoder will decode B- or Q-encoding in each
791 encoded-word, concatenate them, and decode it by charset. Otherwise,
792 the decoder will fully decode each encoded-word before concatenating
795 (defun rfc2047-charset-to-coding-system (charset)
796 "Return coding-system corresponding to MIME CHARSET.
797 If your Emacs implementation can't decode CHARSET, return nil."
798 (when (stringp charset)
799 (setq charset (intern (downcase charset))))
800 (when (or (not charset)
801 (eq 'gnus-all mail-parse-ignored-charsets)
802 (memq 'gnus-all mail-parse-ignored-charsets)
803 (memq charset mail-parse-ignored-charsets))
804 (setq charset mail-parse-charset))
805 (let ((cs (mm-charset-to-coding-system charset)))
806 (cond ((eq cs 'ascii)
807 (setq cs (or (mm-charset-to-coding-system mail-parse-charset)
809 ((setq cs (mm-coding-system-p cs)))
811 (listp mail-parse-ignored-charsets)
812 (memq 'gnus-unknown mail-parse-ignored-charsets))
813 (setq cs (mm-charset-to-coding-system mail-parse-charset))))
818 (defun rfc2047-decode-encoded-words (words)
819 "Decode successive encoded-words in WORDS and return a decoded string.
820 Each element of WORDS looks like (CHARSET ENCODING ENCODED-TEXT
822 (let (word charset cs encoding text rest)
824 (setq word (pop words))
825 (if (and (or (setq cs (rfc2047-charset-to-coding-system
826 (setq charset (car word))))
828 (message "Unknown charset: %s" charset)
831 (cond ((char-equal ?B (nth 1 word))
832 (setq text (base64-decode-string
833 (rfc2047-pad-base64 (nth 2 word)))))
834 ((char-equal ?Q (nth 1 word))
835 (setq text (quoted-printable-decode-string
836 (mm-subst-char-in-string
837 ?_ ? (nth 2 word) t)))))
839 (message "%s" (error-message-string code))
841 (if (and rfc2047-allow-incomplete-encoded-text
843 ;; Concatenate text of which the charset is the same.
844 (setcdr (car rest) (concat (cdar rest) text))
845 (push (cons cs text) rest))
846 ;; Don't decode encoded-word.
847 (push (cons nil (nth 3 word)) rest)))
850 (or (and (setq cs (caar rest))
852 (mm-decode-coding-string (cdar rest) cs)
854 (message "%s" (error-message-string code))
856 (concat (when (cdr rest) " ")
859 (not (eq (string-to-char words) ? )))
865 ;; Fixme: This should decode in place, not cons intermediate strings.
866 ;; Also check whether it needs to worry about delimiting fields like
869 ;; In fact it's reported that (invalid) encoding of mailboxes in
870 ;; addr-specs is in use, so delimiting fields might help. Probably
871 ;; not decoding a word which isn't properly delimited is good enough
872 ;; and worthwhile (is it more correct or not?), e.g. something like
873 ;; `=?iso-8859-1?q?foo?=@'.
875 (defun rfc2047-decode-region (start end)
876 "Decode MIME-encoded words in region between START and END."
878 (let ((case-fold-search t)
879 (eword-regexp (eval-when-compile
880 ;; Ignore whitespace between encoded-words.
881 (concat "[\n\t ]*\\(" rfc2047-encoded-word-regexp
886 (narrow-to-region start end)
887 (goto-char (setq b start))
888 ;; Look for the encoded-words.
889 (while (setq match (re-search-forward eword-regexp nil t))
890 (setq e (match-beginning 1)
894 (push (list (match-string 2) ;; charset
895 (char-after (match-beginning 3)) ;; encoding
896 (match-string 4) ;; encoded-text
897 (match-string 1)) ;; encoded-word
899 ;; Look for the subsequent encoded-words.
900 (when (setq match (looking-at eword-regexp))
901 (goto-char (setq end (match-end 0)))))
902 ;; Replace the encoded-words with the decoded one.
903 (delete-region e end)
904 (insert (rfc2047-decode-encoded-words (nreverse words)))
906 (narrow-to-region e (point))
908 ;; Remove newlines between decoded words, though such
909 ;; things essentially must not be there.
910 (while (re-search-forward "[\n\r]+" nil t)
912 ;; Quote decoded words if there are special characters
913 ;; which might violate RFC2822.
914 (when (and rfc2047-quote-decoded-words-containing-tspecials
915 (let ((regexp (car (rassq
917 rfc2047-header-encoding-alist))))
922 (while (and (memq (char-after) '(? ?\t))
923 (zerop (forward-line -1))))
924 (looking-at regexp)))))
927 (skip-chars-forward " \t")
929 (setq quoted (eq (char-after) ?\"))
930 (goto-char (point-max))
931 (skip-chars-backward " \t")
932 (if (setq quoted (and quoted
933 (> (point) (1+ start))
934 (eq (char-before) ?\")))
937 (setq start (1+ start)
939 (setq end (point-marker)))
941 (while (search-forward "\"" end t)
944 (zerop (% (skip-chars-backward "\\\\") 2))
945 (goto-char (match-beginning 0)))
948 (when (and (not quoted)
952 (concat "[" ietf-drums-tspecials "]")
958 (set-marker end nil)))
959 (goto-char (point-max)))
960 (when (and (mm-multibyte-p)
962 (not (eq mail-parse-charset 'us-ascii))
963 (not (eq mail-parse-charset 'gnus-decoded)))
964 (mm-decode-coding-region b e mail-parse-charset))
966 (when (and (mm-multibyte-p)
968 (not (eq mail-parse-charset 'us-ascii))
969 (not (eq mail-parse-charset 'gnus-decoded)))
970 (mm-decode-coding-region b (point-max) mail-parse-charset))))))
972 (defun rfc2047-decode-string (string)
973 "Decode the quoted-printable-encoded STRING and return the results."
974 (let ((m (mm-multibyte-p)))
975 (if (string-match "=\\?" string)
977 ;; Fixme: This logic is wrong, but seems to be required by
978 ;; Gnus summary buffer generation. The value of `m' depends
979 ;; on the current buffer, not global multibyteness or that
980 ;; of the string. Also the string returned should always be
981 ;; multibyte in a multibyte session, i.e. the buffer should
982 ;; be multibyte before `buffer-string' is called.
984 (mm-enable-multibyte))
987 (rfc2047-decode-region (point-min) (point-max)))
989 ;; Fixme: As above, `m' here is inappropriate.
992 (not (eq mail-parse-charset 'us-ascii))
993 (not (eq mail-parse-charset 'gnus-decoded)))
994 ;; `decode-coding-string' in Emacs offers a third optional
995 ;; arg NOCOPY to avoid consing a new string if the decoding
996 ;; is "trivial". Unfortunately it currently doesn't
997 ;; consider anything else than a `nil' coding system
999 ;; `rfc2047-decode-string' is called multiple times for each
1000 ;; article during summary buffer generation, and we really
1001 ;; want to avoid unnecessary consing. So we bypass
1002 ;; `decode-coding-string' if the string is purely ASCII.
1003 (if (and (fboundp 'detect-coding-string)
1004 ;; string is purely ASCII
1005 (eq (detect-coding-string string t) 'undecided))
1007 (mm-decode-coding-string string mail-parse-charset))
1008 (mm-string-as-multibyte string)))))
1010 (defun rfc2047-pad-base64 (string)
1011 "Pad STRING to quartets."
1012 ;; Be more liberal to accept buggy base64 strings. If
1013 ;; base64-decode-string accepts buggy strings, this function could
1014 ;; be aliased to identity.
1015 (if (= 0 (mod (length string) 4))
1017 (when (string-match "=+$" string)
1018 (setq string (substring string 0 (match-beginning 0))))
1019 (case (mod (length string) 4)
1021 (1 string) ;; Error, don't pad it.
1022 (2 (concat string "=="))
1023 (3 (concat string "=")))))
1027 ;;; rfc2047.el ends here