1 ;;; rfc2047.el --- Functions for encoding and decoding rfc2047 messages
2 ;; Copyright (C) 1998, 1999, 2000, 2001 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)
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.
25 ;; RFC 2047 is "MIME (Multipurpose Internet Mail Extensions) Part
26 ;; Three: Message Header Extensions for Non-ASCII Text".
30 (eval-when-compile (require 'cl))
37 ;; Fixme: Avoid this (for gnus-point-at-...) mm dependence on gnus.
39 (autoload 'mm-body-7-or-8 "mm-bodies")
41 (defvar rfc2047-header-encoding-alist
42 '(("Newsgroups" . nil)
44 ("\\(Resent-\\)?\\(From\\|Cc\\|To\\|Bcc\\|Reply-To\\|Sender\\)" .
47 "*Header/encoding method alist.
48 The list is traversed sequentially. The keys can either be
53 1) nil, in which case no encoding is done;
54 2) `mime', in which case the header will be encoded according to RFC2047;
55 3) a charset, in which case it will be encoded as that charset;
56 4) `default', in which case the field will be encoded as the rest
58 5) a string, like `mime', expect for using it as word-chars.")
60 (defvar rfc2047-charset-encoding-alist
83 "Alist of MIME charsets to RFC2047 encodings.
84 Valid encodings are nil, `Q' and `B'.")
86 (defvar rfc2047-encoding-function-alist
87 '((Q . rfc2047-q-encode-region)
88 (B . rfc2047-b-encode-region)
90 "Alist of RFC2047 encodings to encoding functions.")
92 (defvar rfc2047-q-encoding-alist
93 '(("\\(Resent-\\)?\\(From\\|Cc\\|To\\|Bcc\\|Reply-To\\|Sender\\):"
95 ;; = (\075), _ (\137), ? (\077) are used in the encoded word.
96 ;; Avoid using 8bit characters.
97 ;; Equivalent to "^\000-\007\011\013\015-\037\200-\377=_?"
98 ("." . "\010\012\014\040-\074\076\100-\136\140-\177"))
99 "Alist of header regexps and valid Q characters.")
102 ;;; Functions for encoding RFC2047 messages
105 (defun rfc2047-narrow-to-field ()
106 "Narrow the buffer to the header on the current line."
112 (if (re-search-forward "^[^ \n\t]" nil t)
117 (goto-char (point-min)))
119 (defun rfc2047-encode-message-header ()
120 "Encode the message header according to `rfc2047-header-encoding-alist'.
121 Should be called narrowed to the head of the message."
124 (goto-char (point-min))
125 (let (alist elem method)
128 (rfc2047-narrow-to-field)
129 (if (not (rfc2047-encodable-p))
131 (if (and (eq (mm-body-7-or-8) '8bit)
134 (car message-posting-charset)))
135 ;; 8 bit must be decoded.
136 ;; Is message-posting-charset a coding system?
137 (mm-encode-coding-region
138 (point-min) (point-max)
139 (car message-posting-charset)))
140 ;; No encoding necessary, but folding is nice
141 (rfc2047-fold-region (save-excursion
142 (goto-char (point-min))
143 (skip-chars-forward "^:")
144 (and (looking-at ": ")
146 (point)) (point-max)))
147 ;; We found something that may perhaps be encoded.
149 alist rfc2047-header-encoding-alist)
150 (while (setq elem (pop alist))
151 (when (or (and (stringp (car elem))
152 (looking-at (car elem)))
158 (rfc2047-encode-region (point-min) (point-max) method))
160 (rfc2047-encode-region (point-min) (point-max)))
161 ((eq method 'default)
162 (if (and (featurep 'mule)
163 (if (boundp 'default-enable-multibyte-characters)
164 default-enable-multibyte-characters)
166 (mm-encode-coding-region (point-min) (point-max)
167 mail-parse-charset)))
170 (mm-find-charset-region (point-min)
172 (if (or (message-options-get
173 'rfc2047-encode-message-header-encode-any)
175 'rfc2047-encode-message-header-encode-any
177 "Some texts are not encoded. Encode anyway?")))
178 (rfc2047-encode-region (point-min) (point-max))
179 (error "Cannot send unencoded text"))))
180 ((mm-coding-system-p method)
181 (if (and (featurep 'mule)
182 (if (boundp 'default-enable-multibyte-characters)
183 default-enable-multibyte-characters))
184 (mm-encode-coding-region (point-min) (point-max) method)))
187 (goto-char (point-max)))))))
189 ;; Fixme: This, and the require below may not be the Right Thing, but
190 ;; should be safe just before release. -- fx 2001-02-08
191 (eval-when-compile (defvar message-posting-charset))
193 (defun rfc2047-encodable-p ()
194 "Return non-nil if any characters in current buffer need encoding in headers.
195 The buffer may be narrowed."
196 (require 'message) ; for message-posting-charset
200 (mm-find-charset-region (point-min) (point-max))))
201 (cs (list 'us-ascii (car message-posting-charset)))
204 (unless (memq (pop charsets) cs)
208 (defun rfc2047-dissect-region (b e &optional word-chars)
209 "Dissect the region between B and E into words."
211 ;; Anything except most CTLs, WSP
212 (setq word-chars "\010\012\014\041-\177"))
213 (let (mail-parse-mule-charset
217 (narrow-to-region b e)
218 (goto-char (point-min))
219 (skip-chars-forward "\000-\177")
222 (skip-chars-backward word-chars b)
223 (unless (eq b (point))
224 (push (cons (buffer-substring b (point)) nil) words))
227 (setq current (mm-charset-after))
229 (skip-chars-forward word-chars)
230 (while (and (not (eobp))
231 (eq (mm-charset-after) current))
233 (skip-chars-forward word-chars))
234 (unless (eq b (point))
235 (push (cons (buffer-substring b (point)) current) words))
237 (skip-chars-forward "\000-\177"))
238 (unless (eq b (point))
239 (push (cons (buffer-substring b (point)) nil) words)))
240 ;; merge adjacent words
241 (setq word (pop words))
246 (not (string-match "[^ \t]" (caar words))))
247 (if (eq (cdr (nth 1 words)) (cdr word))
249 (setq word (cons (concat
250 (car (nth 1 words)) (caar words)
255 (push (cons (concat (caar words) (car word)) (cdr word))
258 (setq word (pop words)))
260 (setq word (pop words))))
263 (defun rfc2047-encode-region (b e &optional word-chars)
264 "Encode all encodable words in region B to E."
265 (let ((words (rfc2047-dissect-region b e word-chars)) word)
267 (narrow-to-region b e)
268 (delete-region (point-min) (point-max))
269 (while (setq word (pop words))
272 (rfc2047-fold-region (gnus-point-at-bol) (point))
273 (goto-char (point-max))
274 (if (> (- (point) (save-restriction
276 (gnus-point-at-bol))) 76)
278 ;; Insert blank between encoded words
279 (if (eq (char-before) ?=) (insert " "))
280 (rfc2047-encode (point)
281 (progn (insert (car word)) (point))
283 (rfc2047-fold-region (point-min) (point-max)))))
285 (defun rfc2047-encode-string (string &optional word-chars)
286 "Encode words in STRING."
289 (rfc2047-encode-region (point-min) (point-max) word-chars)
292 (defun rfc2047-encode (b e charset)
293 "Encode the word in the region B to E with CHARSET."
294 (let* ((mime-charset (mm-mime-charset charset))
295 (cs (mm-charset-to-coding-system mime-charset))
296 (encoding (or (cdr (assq mime-charset
297 rfc2047-charset-encoding-alist))
300 "=?" (downcase (symbol-name mime-charset)) "?"
301 (downcase (symbol-name encoding)) "?"))
304 (narrow-to-region b e)
305 (when (eq encoding 'B)
306 ;; break into lines before encoding
307 (goto-char (point-min))
309 (goto-char (min (point-max) (+ 15 (point))))
312 (if (and (mm-multibyte-p)
313 (mm-coding-system-p cs))
314 (mm-encode-coding-region (point-min) (point-max) cs))
315 (funcall (cdr (assq encoding rfc2047-encoding-function-alist))
316 (point-min) (point-max))
317 (goto-char (point-min))
327 (defun rfc2047-fold-region (b e)
328 "Fold long lines in region B to E."
330 (narrow-to-region b e)
331 (goto-char (point-min))
334 (bol (save-restriction
336 (gnus-point-at-bol))))
338 (when (and (or break qword-break) (> (- (point) bol) 76))
339 (goto-char (or break qword-break))
342 (if (looking-at "[ \t]")
345 (setq bol (1- (point)))
346 ;; Don't break before the first non-LWSP characters.
347 (skip-chars-forward " \t")
348 (unless (eobp) (forward-char 1)))
350 ((eq (char-after) ?\n)
355 (skip-chars-forward " \t")
356 (unless (or (eobp) (eq (char-after) ?\n))
358 ((eq (char-after) ?\r)
360 ((memq (char-after) '(? ?\t))
361 (skip-chars-forward " \t")
362 (setq break (1- (point))))
364 (if (not (looking-at "=\\?[^=]"))
365 (if (eq (char-after) ?=)
367 (skip-chars-forward "^ \t\n\r="))
368 (setq qword-break (point))
369 (skip-chars-forward "^ \t\n\r")))
371 (skip-chars-forward "^ \t\n\r"))))
372 (when (and (or break qword-break) (> (- (point) bol) 76))
373 (goto-char (or break qword-break))
376 (if (looking-at "[ \t]")
379 (setq bol (1- (point)))
380 ;; Don't break before the first non-LWSP characters.
381 (skip-chars-forward " \t")
382 (unless (eobp) (forward-char 1))))))
384 (defun rfc2047-unfold-region (b e)
385 "Unfold lines in region B to E."
387 (narrow-to-region b e)
388 (goto-char (point-min))
389 (let ((bol (save-restriction
391 (gnus-point-at-bol)))
392 (eol (gnus-point-at-eol))
396 (looking-at "[ \t]*")
397 (setq leading (- (match-end 0) (match-beginning 0)))
398 (if (< (- (gnus-point-at-eol) bol leading) 76)
401 (delete-region eol (progn
402 (skip-chars-forward " \t\n\r")
404 (setq bol (gnus-point-at-bol)))
405 (setq eol (gnus-point-at-eol))
408 (defun rfc2047-b-encode-region (b e)
409 "Base64-encode the header contained in region B to E."
411 (narrow-to-region (goto-char b) e)
413 (base64-encode-region (point) (progn (end-of-line) (point)) t)
414 (if (and (bolp) (eolp))
415 (delete-backward-char 1))
418 (defun rfc2047-q-encode-region (b e)
419 "Quoted-printable-encode the header in region B to E."
422 (narrow-to-region (goto-char b) e)
423 (let ((alist rfc2047-q-encoding-alist)
424 (bol (save-restriction
426 (gnus-point-at-bol))))
428 (when (looking-at (caar alist))
429 (mm-with-unibyte-current-buffer-mule4
430 (quoted-printable-encode-region
431 (point-min) (point-max) nil (cdar alist)))
432 (subst-char-in-region (point-min) (point-max) ? ?_)
435 ;; The size of QP encapsulation is about 20, so set limit to
437 (unless (< (- (point-max) (point-min)) 56)
438 ;; Don't break if it could fit in one line.
439 ;; Let rfc2047-encode-region break it later.
440 (goto-char (1+ (point-min)))
441 (while (and (not (bobp)) (not (eobp)))
442 (goto-char (min (point-max) (+ 56 bol)))
443 (search-backward "=" (- (point) 2) t)
444 (unless (or (bobp) (eobp))
446 (setq bol (point)))))))))
449 ;;; Functions for decoding RFC2047 messages
452 (defvar rfc2047-encoded-word-regexp
453 "=\\?\\([^][\000-\040()<>@,\;:\\\"/?.=]+\\)\\?\\(B\\|Q\\)\\?\\([!->@-~ +]*\\)\\?=")
455 (defun rfc2047-decode-region (start end)
456 "Decode MIME-encoded words in region between START and END."
458 (let ((case-fold-search t)
462 (narrow-to-region start end)
463 (goto-char (point-min))
464 ;; Remove whitespace between encoded words.
465 (while (re-search-forward
466 (concat "\\(" rfc2047-encoded-word-regexp "\\)"
468 "\\(" rfc2047-encoded-word-regexp "\\)")
470 (delete-region (goto-char (match-end 1)) (match-beginning 6)))
471 ;; Decode the encoded words.
472 (setq b (goto-char (point-min)))
473 (while (re-search-forward rfc2047-encoded-word-regexp nil t)
474 (setq e (match-beginning 0))
475 (insert (rfc2047-parse-and-decode
478 (delete-region (match-beginning 0) (match-end 0)))))
479 (when (and (mm-multibyte-p)
481 (not (eq mail-parse-charset 'gnus-decoded)))
482 (mm-decode-coding-region b e mail-parse-charset))
484 (when (and (mm-multibyte-p)
486 (not (eq mail-parse-charset 'us-ascii))
487 (not (eq mail-parse-charset 'gnus-decoded)))
488 (mm-decode-coding-region b (point-max) mail-parse-charset))
489 (rfc2047-unfold-region (point-min) (point-max))))))
491 (defun rfc2047-decode-string (string)
492 "Decode the quoted-printable-encoded STRING and return the results."
493 (let ((m (mm-multibyte-p)))
496 (mm-enable-multibyte))
499 (rfc2047-decode-region (point-min) (point-max)))
502 (defun rfc2047-parse-and-decode (word)
503 "Decode WORD and return it if it is an encoded word.
505 (if (not (string-match rfc2047-encoded-word-regexp word))
510 (match-string 1 word)
511 (upcase (match-string 2 word))
512 (match-string 3 word))
516 (defun rfc2047-pad-base64 (string)
517 "Pad STRING to quartets."
518 ;; Be more liberal to accept buggy base64 strings. If
519 ;; base64-decode-string accepts buggy strings, this function could
520 ;; be aliased to identity.
521 (case (mod (length string) 4)
523 (1 string) ;; Error, don't pad it.
524 (2 (concat string "=="))
525 (3 (concat string "="))))
527 (defun rfc2047-decode (charset encoding string)
528 "Decode STRING from the given MIME CHARSET in the given ENCODING.
529 Valid ENCODINGs are \"B\" and \"Q\".
530 If your Emacs implementation can't decode CHARSET, return nil."
531 (if (stringp charset)
532 (setq charset (intern (downcase charset))))
533 (if (or (not charset)
534 (eq 'gnus-all mail-parse-ignored-charsets)
535 (memq 'gnus-all mail-parse-ignored-charsets)
536 (memq charset mail-parse-ignored-charsets))
537 (setq charset mail-parse-charset))
538 (let ((cs (mm-charset-to-coding-system charset)))
539 (if (and (not cs) charset
540 (listp mail-parse-ignored-charsets)
541 (memq 'gnus-unknown mail-parse-ignored-charsets))
542 (setq cs (mm-charset-to-coding-system mail-parse-charset)))
544 (when (and (eq cs 'ascii)
546 (setq cs mail-parse-charset))
547 (mm-with-unibyte-current-buffer-mule4
548 ;; In Emacs Mule 4, decoding UTF-8 should be in unibyte mode.
549 (mm-decode-coding-string
551 ((equal "B" encoding)
552 (base64-decode-string
553 (rfc2047-pad-base64 string)))
554 ((equal "Q" encoding)
555 (quoted-printable-decode-string
556 (mm-replace-chars-in-string string ?_ ? )))
557 (t (error "Invalid encoding: %s" encoding)))
562 ;;; rfc2047.el ends here