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)
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.
27 (eval-when-compile (require 'cl))
30 '(unless (fboundp 'base64-decode-string)
38 (defvar rfc2047-header-encoding-alist
39 '(("Newsgroups" . nil)
41 ("\\(Resent-\\)?\\(From\\|Cc\\|To\\|Bcc\\|Reply-To\\|Sender\\)" .
44 "*Header/encoding method alist.
45 The list is traversed sequentially. The keys can either be
46 header regexps or `t'.
50 1) nil, in which case no encoding is done;
51 2) `mime', in which case the header will be encoded according to RFC2047;
52 3) a charset, in which case it will be encoded as that charset;
53 4) `default', in which case the field will be encoded as the rest
55 5) a string, like `mime', expect for using it as word-chars.")
57 (defvar rfc2047-charset-encoding-alist
78 "Alist of MIME charsets to RFC2047 encodings.
79 Valid encodings are nil, `Q' and `B'.")
81 (defvar rfc2047-encoding-function-alist
82 '((Q . rfc2047-q-encode-region)
83 (B . rfc2047-b-encode-region)
85 "Alist of RFC2047 encodings to encoding functions.")
87 (defvar rfc2047-q-encoding-alist
88 '(("\\(Resent-\\)?\\(From\\|Cc\\|To\\|Bcc\\|Reply-To\\|Sender\\):"
90 ;; = (\075), _ (\137), ? (\077) are used in the encoded word.
91 ;; Avoid using 8bit characters. Some versions of Emacs has bug!
92 ;; Equivalent to "^\000-\007\011\013\015-\037\200-\377=_?"
93 ("." . "\010\012\014\040-\074\076\100-\136\140-\177"))
94 "Alist of header regexps and valid Q characters.")
97 ;;; Functions for encoding RFC2047 messages
100 (defun rfc2047-narrow-to-field ()
101 "Narrow the buffer to the header on the current line."
107 (if (re-search-forward "^[^ \n\t]" nil t)
112 (goto-char (point-min)))
114 (defun rfc2047-encode-message-header ()
115 "Encode the message header according to `rfc2047-header-encoding-alist'.
116 Should be called narrowed to the head of the message."
119 (goto-char (point-min))
120 (let (alist elem method)
123 (rfc2047-narrow-to-field)
124 (if (not (rfc2047-encodable-p))
125 (if (and (eq (mm-body-7-or-8) '8bit)
128 (car message-posting-charset)))
129 ;; 8 bit must be decoded.
130 ;; Is message-posting-charset a coding system?
131 (mm-encode-coding-region
132 (point-min) (point-max)
133 (car message-posting-charset)))
134 ;; We found something that may perhaps be encoded.
136 alist rfc2047-header-encoding-alist)
137 (while (setq elem (pop alist))
138 (when (or (and (stringp (car elem))
139 (looking-at (car elem)))
145 (rfc2047-encode-region (point-min) (point-max) method))
147 (rfc2047-encode-region (point-min) (point-max)))
148 ((eq method 'default)
149 (if (and (featurep 'mule)
151 (mm-encode-coding-region (point-min) (point-max)
152 mail-parse-charset)))
155 (mm-find-charset-region (point-min)
157 (if (or (message-options-get
158 'rfc2047-encode-message-header-encode-any)
160 'rfc2047-encode-message-header-encode-any
162 "Some texts are not encoded. Encode anyway?")))
163 (rfc2047-encode-region (point-min) (point-max))
164 (error "Cannot send unencoded text."))))
165 ((mm-coding-system-p method)
167 (mm-encode-coding-region (point-min) (point-max) method)))
170 (goto-char (point-max)))))))
172 (defun rfc2047-encodable-p (&optional header)
173 "Say whether the current (narrowed) buffer contains characters that need encoding in headers."
177 (mm-find-charset-region (point-min) (point-max))))
178 (cs (list 'us-ascii (car message-posting-charset)))
181 (unless (memq (pop charsets) cs)
185 (defun rfc2047-dissect-region (b e &optional word-chars)
186 "Dissect the region between B and E into words."
188 ;; Anything except most CTLs, WSP
189 (setq word-chars "\010\012\014\041-\177"))
190 (let (mail-parse-mule-charset
194 (narrow-to-region b e)
195 (goto-char (point-min))
196 (skip-chars-forward "\000-\177")
199 (skip-chars-backward word-chars b)
200 (unless (eq b (point))
201 (push (cons (buffer-substring b (point)) nil) words))
204 (setq current (mm-charset-after))
206 (skip-chars-forward word-chars)
207 (while (and (not (eobp))
208 (eq (mm-charset-after) current))
210 (skip-chars-forward word-chars))
211 (unless (eq b (point))
212 (push (cons (buffer-substring b (point)) current) words))
214 (skip-chars-forward "\000-\177"))
215 (unless (eq b (point))
216 (push (cons (buffer-substring b (point)) nil) words)))
217 ;; merge adjacent words
218 (setq word (pop words))
223 (not (string-match "[^ \t]" (caar words))))
224 (if (eq (cdr (nth 1 words)) (cdr word))
226 (setq word (cons (concat
227 (car (nth 1 words)) (caar words)
232 (push (cons (concat (caar words) (car word)) (cdr word))
235 (setq word (pop words)))
237 (setq word (pop words))))
240 (defun rfc2047-encode-region (b e &optional word-chars)
241 "Encode all encodable words in REGION."
242 (let ((words (rfc2047-dissect-region b e word-chars)) word)
244 (narrow-to-region b e)
245 (delete-region (point-min) (point-max))
246 (while (setq word (pop words))
249 (rfc2047-fold-region (gnus-point-at-bol) (point))
250 (goto-char (point-max))
251 (if (> (- (point) (save-restriction
253 (gnus-point-at-bol))) 76)
255 ;; Insert blank between encoded words
256 (if (eq (char-before) ?=) (insert " "))
257 (rfc2047-encode (point)
258 (progn (insert (car word)) (point))
260 (rfc2047-fold-region (point-min) (point-max)))))
262 (defun rfc2047-encode-string (string &optional word-chars)
263 "Encode words in STRING."
266 (rfc2047-encode-region (point-min) (point-max) word-chars)
269 (defun rfc2047-encode (b e charset)
270 "Encode the word in the region with CHARSET."
271 (let* ((mime-charset (mm-mime-charset charset))
272 (encoding (or (cdr (assq mime-charset
273 rfc2047-charset-encoding-alist))
276 "=?" (downcase (symbol-name mime-charset)) "?"
277 (downcase (symbol-name encoding)) "?"))
280 (narrow-to-region b e)
281 (when (eq encoding 'B)
282 ;; break into lines before encoding
283 (goto-char (point-min))
285 (goto-char (min (point-max) (+ 15 (point))))
288 (if (and (mm-multibyte-p)
289 (mm-coding-system-p mime-charset))
290 (mm-encode-coding-region (point-min) (point-max) mime-charset))
291 (funcall (cdr (assq encoding rfc2047-encoding-function-alist))
292 (point-min) (point-max))
293 (goto-char (point-min))
303 (defun rfc2047-fold-region (b e)
304 "Fold the long lines in the region."
306 (narrow-to-region b e)
307 (goto-char (point-min))
310 (bol (save-restriction
312 (gnus-point-at-bol))))
314 (when (and (or break qword-break) (> (- (point) bol) 76))
315 (goto-char (or break qword-break))
319 (setq bol (1- (point)))
320 ;; Don't break before the first non-LWSP characters.
321 (skip-chars-forward " \t")
324 ((eq (char-after) ?\n)
329 (skip-chars-forward " \t")
330 (unless (or (eobp) (eq (char-after) ?\n))
332 ((eq (char-after) ?\r)
334 ((memq (char-after) '(? ?\t))
335 (skip-chars-forward " \t")
336 (setq break (1- (point))))
338 (if (not (looking-at "=\\?[^=]"))
339 (if (eq (char-after) ?=)
341 (skip-chars-forward "^ \t\n\r="))
342 (setq qword-break (point))
343 (skip-chars-forward "^ \t\n\r")))
345 (skip-chars-forward "^ \t\n\r"))))
346 (when (and (or break qword-break) (> (- (point) bol) 76))
347 (goto-char (or break qword-break))
351 (setq bol (1- (point)))
352 ;; Don't break before the first non-LWSP characters.
353 (skip-chars-forward " \t")
356 (defun rfc2047-unfold-region (b e)
357 "Fold the long lines in the region."
359 (narrow-to-region b e)
360 (goto-char (point-min))
361 (let ((bol (save-restriction
363 (gnus-point-at-bol)))
364 (eol (gnus-point-at-eol))
368 (looking-at "[ \t]*")
369 (setq leading (- (match-end 0) (match-beginning 0)))
370 (if (< (- (gnus-point-at-eol) bol leading) 76)
373 (delete-region eol (progn
374 (skip-chars-forward "[ \t\n\r]+")
376 (setq bol (gnus-point-at-bol)))
377 (setq eol (gnus-point-at-eol))
380 (defun rfc2047-b-encode-region (b e)
381 "Encode the header contained in REGION with the B encoding."
383 (narrow-to-region (goto-char b) e)
385 (base64-encode-region (point) (progn (end-of-line) (point)) t)
386 (if (and (bolp) (eolp))
387 (delete-backward-char 1))
390 (defun rfc2047-q-encode-region (b e)
391 "Encode the header contained in REGION with the Q encoding."
394 (narrow-to-region (goto-char b) e)
395 (let ((alist rfc2047-q-encoding-alist)
396 (bol (save-restriction
398 (gnus-point-at-bol))))
400 (when (looking-at (caar alist))
401 (quoted-printable-encode-region b e nil (cdar alist))
402 (subst-char-in-region (point-min) (point-max) ? ?_)
405 ;; The size of QP encapsulation is about 20, so set limit to
407 (unless (< (- (point-max) (point-min)) 56)
408 ;; Don't break if it could fit in one line.
409 ;; Let rfc2047-encode-region break it later.
410 (goto-char (1+ (point-min)))
411 (while (and (not (bobp)) (not (eobp)))
412 (goto-char (min (point-max) (+ 56 bol)))
413 (search-backward "=" (- (point) 2) t)
414 (unless (or (bobp) (eobp))
416 (setq bol (point)))))))))
419 ;;; Functions for decoding RFC2047 messages
422 (defvar rfc2047-encoded-word-regexp
423 "=\\?\\([^][\000-\040()<>@,\;:\\\"/?.=]+\\)\\?\\(B\\|Q\\)\\?\\([!->@-~ +]*\\)\\?=")
425 (defun rfc2047-decode-region (start end)
426 "Decode MIME-encoded words in region between START and END."
428 (let ((case-fold-search t)
432 (narrow-to-region start end)
433 (goto-char (point-min))
434 ;; Remove whitespace between encoded words.
435 (while (re-search-forward
436 (concat "\\(" rfc2047-encoded-word-regexp "\\)"
438 "\\(" rfc2047-encoded-word-regexp "\\)")
440 (delete-region (goto-char (match-end 1)) (match-beginning 6)))
441 ;; Decode the encoded words.
442 (setq b (goto-char (point-min)))
443 (while (re-search-forward rfc2047-encoded-word-regexp nil t)
444 (setq e (match-beginning 0))
445 (insert (rfc2047-parse-and-decode
448 (delete-region (match-beginning 0) (match-end 0)))))
449 (when (and (mm-multibyte-p)
451 (not (eq mail-parse-charset 'gnus-decoded)))
452 (mm-decode-coding-region b e mail-parse-charset))
454 (when (and (mm-multibyte-p)
456 (not (eq mail-parse-charset 'us-ascii))
457 (not (eq mail-parse-charset 'gnus-decoded)))
458 (mm-decode-coding-region b (point-max) mail-parse-charset))
459 (rfc2047-unfold-region (point-min) (point-max))))))
461 (defun rfc2047-decode-string (string)
462 "Decode the quoted-printable-encoded STRING and return the results."
463 (let ((m (mm-multibyte-p)))
466 (mm-enable-multibyte))
469 (rfc2047-decode-region (point-min) (point-max)))
472 (defun rfc2047-parse-and-decode (word)
473 "Decode WORD and return it if it is an encoded word.
475 (if (not (string-match rfc2047-encoded-word-regexp word))
480 (match-string 1 word)
481 (upcase (match-string 2 word))
482 (match-string 3 word))
486 (defun rfc2047-pad-base64 (string)
487 "Pad STRING to quartets."
488 ;; Be more liberal to accept buggy base64 strings. If
489 ;; base64-decode-string accepts buggy strings, this function could
490 ;; be aliased to identity.
491 (case (mod (length string) 4)
493 (1 string) ;; Error, don't pad it.
494 (2 (concat string "=="))
495 (3 (concat string "="))))
497 (defun rfc2047-decode (charset encoding string)
498 "Decode STRING that uses CHARSET with ENCODING.
499 Valid ENCODINGs are \"B\" and \"Q\".
500 If your Emacs implementation can't decode CHARSET, it returns nil."
501 (if (stringp charset)
502 (setq charset (intern (downcase charset))))
503 (if (or (not charset)
504 (eq 'gnus-all mail-parse-ignored-charsets)
505 (memq 'gnus-all mail-parse-ignored-charsets)
506 (memq charset mail-parse-ignored-charsets))
507 (setq charset mail-parse-charset))
508 (let ((cs (mm-charset-to-coding-system charset)))
509 (if (and (not cs) charset
510 (listp mail-parse-ignored-charsets)
511 (memq 'gnus-unknown mail-parse-ignored-charsets))
512 (setq cs (mm-charset-to-coding-system mail-parse-charset)))
514 (when (and (eq cs 'ascii)
516 (setq cs mail-parse-charset))
517 (mm-with-unibyte-current-buffer-mule4
518 ;; In Emacs Mule 4, decoding UTF-8 should be in unibyte mode.
519 (mm-decode-coding-string
521 ((equal "B" encoding)
522 (base64-decode-string
523 (rfc2047-pad-base64 string)))
524 ((equal "Q" encoding)
525 (quoted-printable-decode-string
526 (mm-replace-chars-in-string string ?_ ? )))
527 (t (error "Invalid encoding: %s" encoding)))
532 ;;; rfc2047.el ends here