1 ;;; rfc2047.el --- functions for encoding and decoding rfc2047 messages
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 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 3, 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.
41 (require 'rfc2045
) ;; rfc2045-encode-string
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
91 "Alist of MIME charsets to RFC2047 encodings.
92 Valid encodings are nil, `Q' and `B'. These indicate binary (no) encoding,
93 quoted-printable and base64 respectively.")
95 (defvar rfc2047-encode-function-alist
96 '((Q . rfc2047-q-encode-string
)
97 (B . rfc2047-b-encode-string
)
99 "Alist of RFC2047 encodings to encoding functions.")
101 (defvar rfc2047-encode-encoded-words t
102 "Whether encoded words should be encoded again.")
104 (defvar rfc2047-allow-irregular-q-encoded-words t
105 "*Whether to decode irregular Q-encoded words.")
107 (eval-and-compile ;; Necessary to hard code them in `rfc2047-decode-region'.
108 (defconst rfc2047-encoded-word-regexp
109 "=\\?\\([^][\000-\040()<>@,\;:*\\\"/?.=]+\\)\\(?:\\*[^?]+\\)?\\?\
110 \\(B\\?[+/0-9A-Za-z]*=*\
113 "Regexp that matches encoded word."
114 ;; The patterns for the B encoding and the Q encoding, i.e. the ones
115 ;; beginning with "B" and "Q" respectively, are restricted into only
116 ;; the characters that those encodings may generally use.
118 (defconst rfc2047-encoded-word-regexp-loose
119 "=\\?\\([^][\000-\040()<>@,\;:*\\\"/?.=]+\\)\\(?:\\*[^?]+\\)?\\?\
120 \\(B\\?[+/0-9A-Za-z]*=*\
121 \\|Q\\?\\(?:\\?+[ -<>@-~]\\)?\\(?:[ ->@-~]+\\?+[ -<>@-~]\\)*[ ->@-~]*\\?*\
123 "Regexp that matches encoded word allowing loose Q encoding."
124 ;; The pattern for the Q encoding, i.e. the one beginning with "Q",
126 ;; "Q\\?\\(\\?+[^\n=?]\\)?\\([^\n?]+\\?+[^\n=?]\\)*[^\n?]*\\?*"
127 ;; <--------1-------><----------2,3----------><--4--><-5->
129 ;; 1. After "Q?", allow "?"s that follow a character other than "=".
130 ;; 2. Allow "=" after "Q?"; it isn't regarded as the terminator.
131 ;; 3. In the middle of an encoded word, allow "?"s that follow a
132 ;; character other than "=".
133 ;; 4. Allow any characters other than "?" in the middle of an
135 ;; 5. At the end, allow "?"s.
139 ;;; Functions for encoding RFC2047 messages
142 (defun rfc2047-qp-or-base64 ()
143 "Return the type with which to encode the buffer.
144 This is either `base64' or `quoted-printable'."
146 (let ((limit (min (point-max) (+ 2000 (point-min))))
148 (goto-char (point-min))
149 (skip-chars-forward "\x20-\x7f\r\n\t" limit
)
150 (while (< (point) limit
)
153 (skip-chars-forward "\x20-\x7f\r\n\t" limit
))
154 (if (or (< (* 6 n8bit
) (- limit
(point-min)))
155 ;; Don't base64, say, a short line with a single
156 ;; non-ASCII char when splitting parts by charset.
161 (defun rfc2047-narrow-to-field ()
162 "Narrow the buffer to the header on the current line."
168 (if (re-search-forward "^[^ \n\t]" nil t
)
171 (goto-char (point-min)))
173 (defun rfc2047-field-value ()
174 "Return the value of the field at point."
177 (rfc2047-narrow-to-field)
178 (re-search-forward ":[ \t\n]*" nil t
)
179 (buffer-substring-no-properties (point) (point-max)))))
181 (defun rfc2047-quote-special-characters-in-quoted-strings (&optional
183 "Quote special characters with `\\'s in quoted strings.
184 Quoting will not be done in a quoted string if it contains characters
185 matching ENCODABLE-REGEXP or it is within parentheses."
186 (goto-char (point-min))
187 (let ((tspecials (concat "[" ietf-drums-tspecials
"]"))
190 (with-syntax-table (standard-syntax-table)
194 (eq (char-before) ?\
)))
196 (goto-char (point-max)))
198 (narrow-to-region start
(point))
200 (while (search-forward "\"" nil t
)
201 (setq beg
(match-beginning 0))
202 (unless (eq (char-before beg
) ?
\\)
208 (setq end
(1- (point)))
210 (if (and encodable-regexp
211 (re-search-forward encodable-regexp end t
))
214 (narrow-to-region beg end
)
215 (while (re-search-forward tspecials nil
'move
)
216 (if (eq (char-before) ?
\\)
217 (if (looking-at tspecials
) ;; Already quoted.
220 (goto-char (match-beginning 0))
226 (goto-char (point-max)))
228 (setq start
(point))))))
230 (defvar rfc2047-encoding-type
'address-mime
231 "The type of encoding done by `rfc2047-encode-region'.
232 This should be dynamically bound around calls to
233 `rfc2047-encode-region' to either `mime' or `address-mime'. See
234 `rfc2047-header-encoding-alist', for definitions.")
236 (defun rfc2047-encode-message-header ()
237 "Encode the message header according to `rfc2047-header-encoding-alist'.
238 Should be called narrowed to the head of the message."
241 (goto-char (point-min))
242 (let (alist elem method
)
245 (rfc2047-narrow-to-field)
247 alist rfc2047-header-encoding-alist
)
248 (while (setq elem
(pop alist
))
249 (when (or (and (stringp (car elem
))
250 (looking-at (car elem
)))
254 (if (not (rfc2047-encodable-p))
256 (when (eq method
'address-mime
)
257 (rfc2047-quote-special-characters-in-quoted-strings))
258 (if (and (eq (mm-body-7-or-8) '8bit
)
261 (car message-posting-charset
)))
262 ;; 8 bit must be decoded.
263 (mm-encode-coding-region
264 (point-min) (point-max)
265 (mm-charset-to-coding-system
266 (car message-posting-charset
))))
267 ;; No encoding necessary, but folding is nice
271 (goto-char (point-min))
272 (skip-chars-forward "^:")
273 (when (looking-at ": ")
277 ;; We found something that may perhaps be encoded.
278 (re-search-forward "^[^:]+: *" nil t
)
280 ((eq method
'address-mime
)
281 (rfc2047-encode-region (point) (point-max)))
283 (let ((rfc2047-encoding-type 'mime
))
284 (rfc2047-encode-region (point) (point-max))))
285 ((eq method
'default
)
286 (if (and (featurep 'mule
)
287 (if (boundp 'default-enable-multibyte-characters
)
288 default-enable-multibyte-characters
)
290 (mm-encode-coding-region (point) (point-max)
291 mail-parse-charset
)))
292 ;; We get this when CC'ing messsages to newsgroups with
293 ;; 8-bit names. The group name mail copy just got
294 ;; unconditionally encoded. Previously, it would ask
295 ;; whether to encode, which was quite confusing for the
296 ;; user. If the new behaviour is wrong, tell me. I have
297 ;; left the old code commented out below.
298 ;; -- Per Abrahamsen <abraham@dina.kvl.dk> Date: 2001-10-07.
299 ;; Modified by Dave Love, with the commented-out code changed
300 ;; in accordance with changes elsewhere.
302 (rfc2047-encode-region (point) (point-max)))
304 ;;; (if (or (message-options-get
305 ;;; 'rfc2047-encode-message-header-encode-any)
306 ;;; (message-options-set
307 ;;; 'rfc2047-encode-message-header-encode-any
309 ;;; "Some texts are not encoded. Encode anyway?")))
310 ;;; (rfc2047-encode-region (point-min) (point-max))
311 ;;; (error "Cannot send unencoded text")))
312 ((mm-coding-system-p method
)
313 (if (or (and (featurep 'mule
)
314 (if (boundp 'default-enable-multibyte-characters
)
315 default-enable-multibyte-characters
))
316 (featurep 'file-coding
))
317 (mm-encode-coding-region (point) (point-max) method
)))
320 (goto-char (point-max)))))))
322 ;; Fixme: This, and the require below may not be the Right Thing, but
323 ;; should be safe just before release. -- fx 2001-02-08
325 (defun rfc2047-encodable-p ()
326 "Return non-nil if any characters in current buffer need encoding in headers.
327 The buffer may be narrowed."
328 (require 'message
) ; for message-posting-charset
330 (mm-find-mime-charset-region (point-min) (point-max))))
331 (goto-char (point-min))
332 (or (and rfc2047-encode-encoded-words
334 (re-search-forward rfc2047-encoded-word-regexp nil t
)
335 (goto-char (point-min))))
337 (not (equal charsets
(list (car message-posting-charset
))))))))
339 ;; Use this syntax table when parsing into regions that may need
340 ;; encoding. Double quotes are string delimiters, backslash is
341 ;; character quoting, and all other RFC 2822 special characters are
342 ;; treated as punctuation so we can use forward-sexp/forward-word to
343 ;; skip to the end of regions appropriately. Nb. ietf-drums does
344 ;; things differently.
345 (defconst rfc2047-syntax-table
346 ;; (make-char-table 'syntax-table '(2)) only works in Emacs.
347 (let ((table (make-syntax-table)))
348 ;; The following is done to work for setting all elements of the table
349 ;; in Emacs 21-23 and XEmacs; it appears to be the cleanest way.
350 ;; Play safe and don't assume the form of the word syntax entry --
352 (if (fboundp 'set-char-table-range
) ; Emacs
353 (funcall (intern "set-char-table-range")
354 table t
(aref (standard-syntax-table) ?a
))
355 (if (fboundp 'put-char-table
)
356 (if (fboundp 'get-char-table
) ; warning avoidance
357 (put-char-table t
(get-char-table ?a
(standard-syntax-table))
359 (modify-syntax-entry ?
\\ "\\" table
)
360 (modify-syntax-entry ?
\" "\"" table
)
361 (modify-syntax-entry ?\
( "(" table
)
362 (modify-syntax-entry ?\
) ")" table
)
363 (modify-syntax-entry ?\
< "." table
)
364 (modify-syntax-entry ?\
> "." table
)
365 (modify-syntax-entry ?\
[ "." table
)
366 (modify-syntax-entry ?\
] "." table
)
367 (modify-syntax-entry ?
: "." table
)
368 (modify-syntax-entry ?\
; "." table)
369 (modify-syntax-entry ?
, "." table
)
370 (modify-syntax-entry ?
@ "." table
)
373 (defun rfc2047-encode-region (b e
)
374 "Encode words in region B to E that need encoding.
375 By default, the region is treated as containing RFC2822 addresses.
376 Dynamically bind `rfc2047-encoding-type' to change that."
378 (narrow-to-region b e
)
379 (let ((encodable-regexp (if rfc2047-encode-encoded-words
380 "[^\000-\177]+\\|=\\?"
382 start
; start of current token
384 ;; Whether there's an encoded word before the current token,
385 ;; either immediately or separated by space.
387 (orig-text (buffer-substring-no-properties b e
)))
388 (if (eq 'mime rfc2047-encoding-type
)
389 ;; Simple case. Continuous words in which all those contain
390 ;; non-ASCII characters are encoded collectively. Encoding
391 ;; ASCII words, including `Re:' used in Subject headers, is
392 ;; avoided for interoperability with non-MIME clients and
393 ;; for making it easy to find keywords.
395 (goto-char (point-min))
396 (while (progn (skip-chars-forward " \t\n")
399 (while (and (looking-at "[ \t\n]*\\([^ \t\n]+\\)")
401 (setq end
(match-end 0))
402 (re-search-forward encodable-regexp end t
)))
404 (if (> (point) start
)
405 (rfc2047-encode start
(point))
407 ;; `address-mime' case -- take care of quoted words, comments.
408 (rfc2047-quote-special-characters-in-quoted-strings encodable-regexp
)
409 (with-syntax-table rfc2047-syntax-table
410 (goto-char (point-min))
411 (condition-case err
; in case of unbalanced quotes
412 ;; Look for rfc2822-style: sequences of atoms, quoted
413 ;; strings, specials, whitespace. (Specials mustn't be
417 (skip-chars-forward " \t\n")
420 ((not (char-after))) ; eob
422 ((eq ?
\" (setq csyntax
(char-syntax (char-after))))
426 ;; Does it need encoding?
428 (if (re-search-forward encodable-regexp end
'move
)
429 ;; It needs encoding. Strip the quotes first,
430 ;; since encoded words can't occur in quotes.
433 (delete-backward-char 1)
437 ;; There was a preceding quoted word. We need
438 ;; to include any separating whitespace in this
439 ;; word to avoid it getting lost.
440 (skip-chars-backward " \t")
441 ;; A space is needed between the encoded words.
445 ;; Adjust the end position for the deleted quotes.
446 (rfc2047-encode start
(- end
2))
447 (setq last-encoded t
)) ; record that it was encoded
448 (setq last-encoded nil
)))
450 ;; Skip other delimiters, but record that they've
451 ;; potentially separated quoted words.
453 (setq last-encoded nil
))
455 (error "Unbalanced parentheses"))
457 ;; Look for the end of parentheses.
459 ;; Encode text as an unstructured field.
460 (let ((rfc2047-encoding-type 'mime
))
461 (rfc2047-encode-region (1+ start
) (1- (point))))
462 (skip-chars-forward ")"))
463 (t ; normal token/whitespace sequence
465 ;; Skip one ASCII word, or encode continuous words
466 ;; in which all those contain non-ASCII characters.
468 (while (not (or end
(eobp)))
469 (when (looking-at "[\000-\177]+")
473 (while (and (or (re-search-forward
474 "[ \t\n]\\|\\Sw" end
'move
)
476 (eq ?
\\ (char-syntax (char-before))))
477 ;; Skip backslash-quoted characters.
480 (setq end
(match-beginning 0))
481 (if rfc2047-encode-encoded-words
484 (when (search-forward "=?" end
'move
)
485 (goto-char (match-beginning 0))
488 ;; Where the value nil of `end' means there may be
489 ;; text to have to be encoded following the point.
490 ;; Otherwise, the point reached to the end of ASCII
491 ;; words separated by whitespace or a special char.
493 (when (looking-at encodable-regexp
)
494 (goto-char (setq begin
(match-end 0)))
495 (while (and (looking-at "[ \t\n]+\\([^ \t\n]+\\)")
496 (setq end
(match-end 0))
498 (while (re-search-forward
499 encodable-regexp end t
))
502 (or (not (re-search-forward "\\Sw" end t
))
504 (goto-char (match-beginning 0))
507 (when (looking-at "[^ \t\n]+")
508 (setq end
(match-end 0))
509 (if (re-search-forward "\\Sw+" end t
)
510 ;; There are special characters better
511 ;; to be encoded so that MTAs may parse
513 (cond ((= end
(point)))
514 ((looking-at (concat "\\sw*\\("
519 (goto-char (1- (match-end 0)))
520 (unless (= (point) (match-beginning 0))
521 ;; Separate encodable text and
525 (skip-chars-forward " \t\n")
526 (if (and (looking-at "[^ \t\n]+")
527 (string-match encodable-regexp
530 (goto-char end
)))))))
531 (skip-chars-backward " \t\n")
534 (if (re-search-forward encodable-regexp end
'move
)
536 (unless (memq (char-before start
) '(nil ?
\t ?
))
539 (skip-chars-backward "^ \t\n")
540 (and (looking-at "\\Sw+")
541 (= (match-end 0) start
)))
542 ;; Also encode bogus delimiters.
544 ;; Separate encodable text and delimiter.
547 (setq start
(1+ start
)
549 (rfc2047-encode start end
)
550 (setq last-encoded t
))
551 (setq last-encoded nil
)))))
553 (if (or debug-on-quit debug-on-error
)
554 (signal (car err
) (cdr err
))
555 (error "Invalid data for rfc2047 encoding: %s"
556 (mm-replace-in-string orig-text
"[ \t\n]+" " "))))))))
557 (rfc2047-fold-region b
(point))
558 (goto-char (point-max))))
560 (defun rfc2047-encode-string (string)
561 "Encode words in STRING.
562 By default, the string is treated as containing addresses (see
563 `rfc2047-encoding-type')."
564 (mm-with-multibyte-buffer
566 (rfc2047-encode-region (point-min) (point-max))
570 ;; 2. Syntax of encoded-words
572 ;; While there is no limit to the length of a multiple-line header
573 ;; field, each line of a header field that contains one or more
574 ;; 'encoded-word's is limited to 76 characters.
576 ;; In `rfc2047-encode-parameter' it is bound to nil, so don't defconst it.
577 (defvar rfc2047-encode-max-chars
76
578 "Maximum characters of each header line that contain encoded-words.
579 According to RFC 2047, it is 76. If it is nil, encoded-words
580 will not be folded. Too small value may cause an error. You
581 should not change this value.")
583 (defun rfc2047-encode-1 (column string cs encoder start crest tail
585 "Subroutine used by `rfc2047-encode'."
586 (cond ((string-equal string
"")
588 ((not rfc2047-encode-max-chars
)
590 (funcall encoder
(if cs
591 (mm-encode-coding-string string cs
)
594 ((>= column rfc2047-encode-max-chars
)
596 (cond ((string-match "\n[ \t]+\\'" eword
)
597 ;; Reomove a superfluous empty line.
598 (setq eword
(substring eword
0 (match-beginning 0))))
599 ((string-match "(+\\'" eword
)
600 ;; Break the line before the open parenthesis.
601 (setq crest
(concat crest
(match-string 0 eword
))
602 eword
(substring eword
0 (match-beginning 0))))))
603 (rfc2047-encode-1 (length crest
) string cs encoder start
" " tail
604 (concat eword
"\n" crest
)))
607 (limit (1- (length string
)))
612 (setq next
(concat start
615 (mm-encode-coding-string
616 (substring string
0 (1+ index
))
618 (substring string
0 (1+ index
))))
620 len
(+ column
(length next
)))
621 (if (> len rfc2047-encode-max-chars
)
624 (if (or (< index limit
)
625 (<= (+ len
(or (string-match "\n" tail
)
627 rfc2047-encode-max-chars
))
630 (if (string-match "\\`)+" tail
)
631 ;; Break the line after the close parenthesis.
632 (setq tail
(concat (substring tail
0 (match-end 0))
634 (substring tail
(match-end 0)))
640 (concat eword next tail
)
643 (string-match "(+\\'" eword
))
644 (setq crest
(concat crest
(match-string 0 eword
))
645 eword
(substring eword
0 (match-beginning 0)))
646 (setq eword
(concat eword next
)))
648 eword
(concat eword next
)))
649 (when (string-match "\n[ \t]+\\'" eword
)
650 ;; Reomove a superfluous empty line.
651 (setq eword
(substring eword
0 (match-beginning 0))))
652 (rfc2047-encode-1 (length crest
) (substring string index
)
653 cs encoder start
" " tail
654 (concat eword
"\n" crest
)))))))
656 (defun rfc2047-encode (b e
)
657 "Encode the word(s) in the region B to E.
658 Point moves to the end of the region."
659 (let ((mime-charset (or (mm-find-mime-charset-region b e
) (list 'us-ascii
)))
660 cs encoding tail crest eword
)
661 (cond ((> (length mime-charset
) 1)
662 (error "Can't rfc2047-encode `%s'"
663 (buffer-substring-no-properties b e
)))
664 ((= (length mime-charset
) 1)
665 (setq mime-charset
(car mime-charset
)
666 cs
(mm-charset-to-coding-system mime-charset
))
667 (unless (and (mm-multibyte-p)
668 (mm-coding-system-p cs
))
671 (narrow-to-region b e
)
673 (or (cdr (assq mime-charset
674 rfc2047-charset-encoding-alist
))
675 ;; For the charsets that don't have a preferred
676 ;; encoding, choose the one that's shorter.
677 (if (eq (rfc2047-qp-or-base64) 'base64
)
682 (skip-chars-forward "^ \t\n")
683 ;; `tail' may contain a close parenthesis.
684 (setq tail
(buffer-substring-no-properties e
(point)))
686 (setq b
(point-marker)
687 e
(set-marker (make-marker) e
))
688 (rfc2047-fold-region (point-at-bol) b
)
690 (skip-chars-backward "^ \t\n")
691 (unless (= 0 (skip-chars-backward " \t"))
692 ;; `crest' may contain whitespace and an open parenthesis.
693 (setq crest
(buffer-substring-no-properties (point) b
)))
694 (setq eword
(rfc2047-encode-1
696 (mm-replace-in-string
697 (buffer-substring-no-properties b e
)
698 "\n\\([ \t]?\\)" "\\1")
700 (or (cdr (assq encoding
701 rfc2047-encode-function-alist
))
703 (concat "=?" (downcase (symbol-name mime-charset
))
704 "?" (upcase (symbol-name encoding
)) "?")
707 (delete-region (if (eq (aref eword
0) ?
\n)
709 ;; The line was folded before encoding.
714 ;; `eword' contains `crest' and `tail'.
718 (unless (or (/= 0 (length tail
))
720 (looking-at "[ \t\n)]"))
725 (defun rfc2047-fold-field ()
726 "Fold the current header field."
729 (rfc2047-narrow-to-field)
730 (rfc2047-fold-region (point-min) (point-max)))))
732 (defun rfc2047-fold-region (b e
)
733 "Fold long lines in region B to E."
735 (narrow-to-region b e
)
736 (goto-char (point-min))
740 (bol (save-restriction
744 (when (and (or break qword-break
)
745 (> (- (point) bol
) 76))
746 (goto-char (or break qword-break
))
749 (skip-chars-backward " \t")
750 (if (looking-at "[ \t]")
753 (setq bol
(1- (point)))
754 ;; Don't break before the first non-LWSP characters.
755 (skip-chars-forward " \t")
759 ((eq (char-after) ?
\n)
764 (skip-chars-forward " \t")
765 (unless (or (eobp) (eq (char-after) ?
\n))
767 ((eq (char-after) ?
\r)
769 ((memq (char-after) '(? ?
\t))
770 (skip-chars-forward " \t")
771 (unless first
;; Don't break just after the header name.
772 (setq break
(point))))
774 (if (not (looking-at "=\\?[^=]"))
775 (if (eq (char-after) ?
=)
777 (skip-chars-forward "^ \t\n\r="))
778 ;; Don't break at the start of the field.
779 (unless (= (point) b
)
780 (setq qword-break
(point)))
781 (skip-chars-forward "^ \t\n\r")))
783 (skip-chars-forward "^ \t\n\r")))
785 (when (and (or break qword-break
)
786 (> (- (point) bol
) 76))
787 (goto-char (or break qword-break
))
790 (if (or (> 0 (skip-chars-backward " \t"))
791 (looking-at "[ \t]"))
794 (setq bol
(1- (point)))
795 ;; Don't break before the first non-LWSP characters.
796 (skip-chars-forward " \t")
798 (forward-char 1))))))
800 (defun rfc2047-unfold-field ()
801 "Fold the current line."
804 (rfc2047-narrow-to-field)
805 (rfc2047-unfold-region (point-min) (point-max)))))
807 (defun rfc2047-unfold-region (b e
)
808 "Unfold lines in region B to E."
810 (narrow-to-region b e
)
811 (goto-char (point-min))
812 (let ((bol (save-restriction
815 (eol (point-at-eol)))
818 (if (and (looking-at "[ \t]")
819 (< (- (point-at-eol) bol
) 76))
820 (delete-region eol
(progn
822 (skip-chars-forward "\r\n")
824 (setq bol
(point-at-bol)))
825 (setq eol
(point-at-eol))
828 (defun rfc2047-b-encode-string (string)
829 "Base64-encode the header contained in STRING."
830 (base64-encode-string string t
))
832 (defun rfc2047-q-encode-string (string)
833 "Quoted-printable-encode the header in STRING."
834 (mm-with-unibyte-buffer
836 (quoted-printable-encode-region
837 (point-min) (point-max) nil
838 ;; = (\075), _ (\137), ? (\077) are used in the encoded word.
839 ;; Avoid using 8bit characters.
840 ;; This list excludes `especials' (see the RFC2047 syntax),
841 ;; meaning that some characters in non-structured fields will
842 ;; get encoded when they con't need to be. The following is
843 ;; what it used to be.
844 ;;; ;; Equivalent to "^\000-\007\011\013\015-\037\200-\377=_?"
845 ;;; "\010\012\014\040-\074\076\100-\136\140-\177")
846 "-\b\n\f !#-'*+0-9A-Z\\^`-~\d")
847 (subst-char-in-region (point-min) (point-max) ? ?_
)
850 (defun rfc2047-encode-parameter (param value
)
851 "Return and PARAM=VALUE string encoded in the RFC2047-like style.
852 This is a replacement for the `rfc2231-encode-string' function.
854 When attaching files as MIME parts, we should use the RFC2231 encoding
855 to specify the file names containing non-ASCII characters. However,
856 many mail softwares don't support it in practice and recipients won't
857 be able to extract files with correct names. Instead, the RFC2047-like
858 encoding is acceptable generally. This function provides the very
859 RFC2047-like encoding, resigning to such a regrettable trend. To use
860 it, put the following line in your ~/.gnus.el file:
862 \(defalias 'mail-header-encode-parameter 'rfc2047-encode-parameter)
864 (let ((rfc2047-encoding-type 'mime
)
865 (rfc2047-encode-max-chars nil
))
866 (rfc2045-encode-string param
(rfc2047-encode-string value
))))
869 ;;; Functions for decoding RFC2047 messages
872 (defvar rfc2047-quote-decoded-words-containing-tspecials nil
873 "If non-nil, quote decoded words containing special characters.")
875 (defvar rfc2047-allow-incomplete-encoded-text t
876 "*Non-nil means allow incomplete encoded-text in successive encoded-words.
877 Dividing of encoded-text in the place other than character boundaries
878 violates RFC2047 section 5, while we have a capability to decode it.
879 If it is non-nil, the decoder will decode B- or Q-encoding in each
880 encoded-word, concatenate them, and decode it by charset. Otherwise,
881 the decoder will fully decode each encoded-word before concatenating
884 (defun rfc2047-strip-backslashes-in-quoted-strings ()
885 "Strip backslashes in quoted strings. `\\\"' remains."
886 (goto-char (point-min))
888 (with-syntax-table (standard-syntax-table)
889 (while (search-forward "\"" nil t
)
890 (unless (eq (char-before) ?
\\)
891 (setq beg
(match-end 0))
892 (goto-char (match-beginning 0))
897 (narrow-to-region beg
(1- (point)))
899 (while (search-forward "\\" nil
'move
)
900 (unless (memq (char-after) '(?
\"))
901 (delete-backward-char 1))
905 (goto-char beg
))))))))
907 (defun rfc2047-charset-to-coding-system (charset)
908 "Return coding-system corresponding to MIME CHARSET.
909 If your Emacs implementation can't decode CHARSET, return nil."
910 (when (stringp charset
)
911 (setq charset
(intern (downcase charset
))))
912 (when (or (not charset
)
913 (eq 'gnus-all mail-parse-ignored-charsets
)
914 (memq 'gnus-all mail-parse-ignored-charsets
)
915 (memq charset mail-parse-ignored-charsets
))
916 (setq charset mail-parse-charset
))
917 (let ((cs (mm-charset-to-coding-system charset
)))
918 (cond ((eq cs
'ascii
)
919 (setq cs
(or (mm-charset-to-coding-system mail-parse-charset
)
921 ((mm-coding-system-p cs
))
923 (listp mail-parse-ignored-charsets
)
924 (memq 'gnus-unknown mail-parse-ignored-charsets
))
925 (setq cs
(mm-charset-to-coding-system mail-parse-charset
))))
930 (defun rfc2047-decode-encoded-words (words)
931 "Decode successive encoded-words in WORDS and return a decoded string.
932 Each element of WORDS looks like (CHARSET ENCODING ENCODED-TEXT
934 (let (word charset cs encoding text rest
)
936 (setq word
(pop words
))
937 (if (and (setq cs
(rfc2047-charset-to-coding-system
938 (setq charset
(car word
))))
940 (cond ((char-equal ?B
(nth 1 word
))
941 (setq text
(base64-decode-string
942 (rfc2047-pad-base64 (nth 2 word
)))))
943 ((char-equal ?Q
(nth 1 word
))
944 (setq text
(quoted-printable-decode-string
945 (mm-subst-char-in-string
946 ?_ ?
(nth 2 word
) t
)))))
948 (message "%s" (error-message-string code
))
950 (if (and rfc2047-allow-incomplete-encoded-text
952 ;; Concatenate text of which the charset is the same.
953 (setcdr (car rest
) (concat (cdar rest
) text
))
954 (push (cons cs text
) rest
))
955 ;; Don't decode encoded-word.
956 (push (cons nil
(nth 3 word
)) rest
)))
959 (or (and (setq cs
(caar rest
))
961 (mm-decode-coding-string (cdar rest
) cs
)
963 (message "%s" (error-message-string code
))
965 (concat (when (cdr rest
) " ")
968 (not (eq (string-to-char words
) ?
)))
974 ;; Fixme: This should decode in place, not cons intermediate strings.
975 ;; Also check whether it needs to worry about delimiting fields like
978 ;; In fact it's reported that (invalid) encoding of mailboxes in
979 ;; addr-specs is in use, so delimiting fields might help. Probably
980 ;; not decoding a word which isn't properly delimited is good enough
981 ;; and worthwhile (is it more correct or not?), e.g. something like
982 ;; `=?iso-8859-1?q?foo?=@'.
984 (defun rfc2047-decode-region (start end
&optional address-mime
)
985 "Decode MIME-encoded words in region between START and END.
986 If ADDRESS-MIME is non-nil, strip backslashes which precede characters
987 other than `\"' and `\\' in quoted strings."
989 (let ((case-fold-search t
)
991 (if rfc2047-allow-irregular-q-encoded-words
993 (concat "[\n\t ]*\\(" rfc2047-encoded-word-regexp-loose
"\\)"))
995 (concat "[\n\t ]*\\(" rfc2047-encoded-word-regexp
"\\)"))))
999 (narrow-to-region start end
)
1001 (rfc2047-strip-backslashes-in-quoted-strings))
1002 (goto-char (setq b start
))
1003 ;; Look for the encoded-words.
1004 (while (setq match
(re-search-forward eword-regexp nil t
))
1005 (setq e
(match-beginning 1)
1009 (push (list (match-string 2) ;; charset
1010 (char-after (match-beginning 3)) ;; encoding
1011 (substring (match-string 3) 2) ;; encoded-text
1012 (match-string 1)) ;; encoded-word
1014 ;; Look for the subsequent encoded-words.
1015 (when (setq match
(looking-at eword-regexp
))
1016 (goto-char (setq end
(match-end 0)))))
1017 ;; Replace the encoded-words with the decoded one.
1018 (delete-region e end
)
1019 (insert (rfc2047-decode-encoded-words (nreverse words
)))
1021 (narrow-to-region e
(point))
1023 ;; Remove newlines between decoded words, though such
1024 ;; things essentially must not be there.
1025 (while (re-search-forward "[\n\r]+" nil t
)
1026 (replace-match " "))
1027 ;; Quote decoded words if there are special characters
1028 ;; which might violate RFC2822.
1029 (when (and rfc2047-quote-decoded-words-containing-tspecials
1030 (let ((regexp (car (rassq
1032 rfc2047-header-encoding-alist
))))
1037 (while (and (memq (char-after) '(? ?
\t))
1038 (zerop (forward-line -
1))))
1039 (looking-at regexp
)))))
1042 (skip-chars-forward " \t")
1043 (setq start
(point))
1044 (setq quoted
(eq (char-after) ?
\"))
1045 (goto-char (point-max))
1046 (skip-chars-backward " \t")
1047 (if (setq quoted
(and quoted
1048 (> (point) (1+ start
))
1049 (eq (char-before) ?
\")))
1052 (setq start
(1+ start
)
1053 end
(point-marker)))
1054 (setq end
(point-marker)))
1056 (while (search-forward "\"" end t
)
1059 (zerop (%
(skip-chars-backward "\\\\") 2))
1060 (goto-char (match-beginning 0)))
1063 (when (and (not quoted
)
1067 (concat "[" ietf-drums-tspecials
"]")
1073 (set-marker end nil
)))
1074 (goto-char (point-max)))
1075 (when (and (mm-multibyte-p)
1077 (not (eq mail-parse-charset
'us-ascii
))
1078 (not (eq mail-parse-charset
'gnus-decoded
)))
1079 (mm-decode-coding-region b e mail-parse-charset
))
1081 (when (and (mm-multibyte-p)
1083 (not (eq mail-parse-charset
'us-ascii
))
1084 (not (eq mail-parse-charset
'gnus-decoded
)))
1085 (mm-decode-coding-region b
(point-max) mail-parse-charset
))))))
1087 (defun rfc2047-decode-address-region (start end
)
1088 "Decode MIME-encoded words in region between START and END.
1089 Backslashes which precede characters other than `\"' and `\\' in quoted
1090 strings are stripped."
1091 (rfc2047-decode-region start end t
))
1093 (defun rfc2047-decode-string (string &optional address-mime
)
1094 "Decode MIME-encoded STRING and return the result.
1095 If ADDRESS-MIME is non-nil, strip backslashes which precede characters
1096 other than `\"' and `\\' in quoted strings."
1097 ;; (let ((m (mm-multibyte-p)))
1098 (if (string-match "=\\?" string
)
1100 ;; We used to only call mm-enable-multibyte if `m' is non-nil,
1101 ;; but this can't be the right criterion. Don't just revert this
1102 ;; change if it encounters a bug. Please help me fix it
1103 ;; right instead. --Stef
1104 ;; The string returned should always be multibyte in a multibyte
1105 ;; session, i.e. the buffer should be multibyte before
1106 ;; `buffer-string' is called.
1107 (mm-enable-multibyte)
1110 (rfc2047-decode-region (point-min) (point-max) address-mime
))
1115 (when (mm-multibyte-string-p string
)
1116 (mm-enable-multibyte))
1118 (rfc2047-strip-backslashes-in-quoted-strings)
1120 ;; Fixme: As above, `m' here is inappropriate.
1123 (not (eq mail-parse-charset
'us-ascii
))
1124 (not (eq mail-parse-charset
'gnus-decoded
)))
1125 ;; `decode-coding-string' in Emacs offers a third optional
1126 ;; arg NOCOPY to avoid consing a new string if the decoding
1127 ;; is "trivial". Unfortunately it currently doesn't
1128 ;; consider anything else than a `nil' coding system
1130 ;; `rfc2047-decode-string' is called multiple times for each
1131 ;; article during summary buffer generation, and we really
1132 ;; want to avoid unnecessary consing. So we bypass
1133 ;; `decode-coding-string' if the string is purely ASCII.
1134 (if (and (fboundp 'detect-coding-string
)
1135 ;; string is purely ASCII
1136 (eq (detect-coding-string string t
) 'undecided
))
1138 (mm-decode-coding-string string mail-parse-charset
))
1139 (mm-string-to-multibyte string
)))) ;; )
1141 (defun rfc2047-decode-address-string (string)
1142 "Decode MIME-encoded STRING and return the result.
1143 Backslashes which precede characters other than `\"' and `\\' in quoted
1144 strings are stripped."
1145 (rfc2047-decode-string string t
))
1147 (defun rfc2047-pad-base64 (string)
1148 "Pad STRING to quartets."
1149 ;; Be more liberal to accept buggy base64 strings. If
1150 ;; base64-decode-string accepts buggy strings, this function could
1151 ;; be aliased to identity.
1152 (if (= 0 (mod (length string
) 4))
1154 (when (string-match "=+$" string
)
1155 (setq string
(substring string
0 (match-beginning 0))))
1156 (case (mod (length string
) 4)
1158 (1 string
) ;; Error, don't pad it.
1159 (2 (concat string
"=="))
1160 (3 (concat string
"=")))))
1164 ;; arch-tag: a07fe3d4-22b5-4c4a-bd89-b1f82d5d36f6
1165 ;;; rfc2047.el ends here