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.
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)
45 "*Header/encoding method alist.
46 The list is traversed sequentially. The keys can either 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
57 (defvar rfc2047-charset-encoding-alist
80 "Alist of MIME charsets to RFC2047 encodings.
81 Valid encodings are nil, `Q' and `B'. These indicate binary (no) encoding,
82 quoted-printable and base64 respectively.")
84 (defvar rfc2047-encoding-function-alist
85 '((Q . rfc2047-q-encode-region)
86 (B . rfc2047-b-encode-region)
88 "Alist of RFC2047 encodings to encoding functions.")
90 (defvar rfc2047-q-encoding-alist
91 '(("\\(From\\|Cc\\|To\\|Bcc\||Reply-To\\):" . "-A-Za-z0-9!*+/")
92 ;; = (\075), _ (\137), ? (\077) are used in the encoded word.
93 ;; Avoid using 8bit characters.
94 ;; Equivalent to "^\000-\007\011\013\015-\037\200-\377=_?"
95 ("." . "\010\012\014\040-\074\076\100-\136\140-\177"))
96 "Alist of header regexps and valid Q characters.")
99 ;;; Functions for encoding RFC2047 messages
102 (defun rfc2047-narrow-to-field ()
103 "Narrow the buffer to the header on the current line."
109 (if (re-search-forward "^[^ \n\t]" nil t)
114 (goto-char (point-min)))
116 (defun rfc2047-encode-message-header ()
117 "Encode the message header according to `rfc2047-header-encoding-alist'.
118 Should be called narrowed to the head of the message."
121 (goto-char (point-min))
122 (let (alist elem method)
125 (rfc2047-narrow-to-field)
126 (if (not (rfc2047-encodable-p))
127 (if (and (eq (mm-body-7-or-8) '8bit)
130 (car message-posting-charset)))
131 ;; 8 bit must be decoded.
132 ;; Is message-posting-charset a coding system?
133 (mm-encode-coding-region
134 (point-min) (point-max)
135 (car message-posting-charset)))
136 ;; We found something that may perhaps be encoded.
138 alist rfc2047-header-encoding-alist)
139 (while (setq elem (pop alist))
140 (when (or (and (stringp (car elem))
141 (looking-at (car elem)))
147 (rfc2047-encode-region (point-min) (point-max)))
148 ((eq method 'default)
149 (if (and (featurep 'mule)
150 (if (boundp 'default-enable-multibyte-characters)
151 default-enable-multibyte-characters)
153 (mm-encode-coding-region (point-min) (point-max)
154 mail-parse-charset)))
155 ((mm-coding-system-p method)
156 (if (and (featurep 'mule)
157 (if (boundp 'default-enable-multibyte-characters)
158 default-enable-multibyte-characters))
159 (mm-encode-coding-region (point-min) (point-max) method)))
162 (goto-char (point-max)))))))
164 ;; Fixme: This, and the require below may not be the Right Thing, but
165 ;; should be safe just before release. -- fx 2001-02-08
166 (eval-when-compile (defvar message-posting-charset))
168 (defun rfc2047-encodable-p ()
169 "Return non-nil if any characters in current buffer need encoding in headers.
170 The buffer may be narrowed."
171 (require 'message) ; for message-posting-charset
173 (mm-find-mime-charset-region (point-min) (point-max))))
174 (and charsets (not (equal charsets (list message-posting-charset))))))
176 (defun rfc2047-dissect-region (b e)
177 "Dissect the region between B and E into words."
178 (let ((word-chars "-A-Za-z0-9!*+/")
179 ;; Not using ietf-drums-specials-token makes life simple.
180 mail-parse-mule-charset
184 (narrow-to-region b e)
185 (goto-char (point-min))
186 (skip-chars-forward "\000-\177")
187 ;; Fixme: This loop used to check charsets when it found
188 ;; non-ASCII characters. That's removed, since it doesn't make
189 ;; much sense in Emacs 22 and doesn't seem necessary in Emacs
190 ;; 21, even. I'm not sure exactly what it should be doing, and
191 ;; it needs another look, especially for efficiency's sake. -- fx
195 (skip-chars-backward word-chars b)
196 (unless (eq b (point))
197 (push (cons (buffer-substring b (point)) nil) words))
202 (skip-chars-forward word-chars)
205 (skip-chars-forward word-chars))
206 (unless (eq b (point))
207 (push (cons (buffer-substring b (point)) nonascii) words))
209 (skip-chars-forward "\000-\177"))
210 (unless (eq b (point))
211 (push (cons (buffer-substring b (point)) nil) words)))
212 ;; merge adjacent words
213 (setq word (pop words))
218 (not (string-match "[^ \t]" (caar words))))
219 (if (eq (cdr (nth 1 words)) (cdr word))
221 (setq word (cons (concat
222 (car (nth 1 words)) (caar words)
227 (push (cons (concat (caar words) (car word)) (cdr word))
230 (setq word (pop words)))
232 (setq word (pop words))))
235 (defun rfc2047-encode-region (b e)
236 "Encode all encodable words in region B to E."
237 (let ((words (rfc2047-dissect-region b e)) word)
239 (narrow-to-region b e)
240 (delete-region (point-min) (point-max))
241 (while (setq word (pop words))
244 (rfc2047-fold-region (gnus-point-at-bol) (point))
245 (goto-char (point-max))
246 (if (> (- (point) (save-restriction
248 (gnus-point-at-bol))) 76)
250 ;; Insert blank between encoded words
251 (if (eq (char-before) ?=) (insert " "))
252 (rfc2047-encode (point)
253 (progn (insert (car word)) (point)))))
254 (rfc2047-fold-region (point-min) (point-max)))))
256 (defun rfc2047-encode-string (string)
257 "Encode words in STRING."
260 (rfc2047-encode-region (point-min) (point-max))
263 (defun rfc2047-encode (b e)
264 "Encode the word in the region B to E."
265 (let* ((buff (current-buffer))
266 (mime-charset (with-temp-buffer
267 (insert-buffer-substring buff b e)
268 (mm-find-mime-charset-region b e)))
269 (cs (if (> (length mime-charset) 1)
270 (mm-charset-to-coding-system mime-charset)
271 (error "Can't encode word: %s" (buffer-substring b e))))
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 cs))
290 (mm-encode-coding-region (point-min) (point-max) cs))
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 long lines in region B to E."
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))
318 (if (looking-at " \t")
321 (setq bol (1- (point)))
322 ;; Don't break before the first non-LWSP characters.
323 (skip-chars-forward " \t")
324 (unless (eobp) (forward-char 1)))
326 ((eq (char-after) ?\n)
331 (skip-chars-forward " \t")
332 (unless (or (eobp) (eq (char-after) ?\n))
334 ((eq (char-after) ?\r)
336 ((memq (char-after) '(? ?\t))
337 (skip-chars-forward " \t")
338 (setq break (1- (point))))
340 (if (not (looking-at "=\\?[^=]"))
341 (if (eq (char-after) ?=)
343 (skip-chars-forward "^ \t\n\r="))
344 (setq qword-break (point))
345 (skip-chars-forward "^ \t\n\r")))
347 (skip-chars-forward "^ \t\n\r"))))
348 (when (and (or break qword-break) (> (- (point) bol) 76))
349 (goto-char (or break qword-break))
352 (if (looking-at " \t")
355 (setq bol (1- (point)))
356 ;; Don't break before the first non-LWSP characters.
357 (skip-chars-forward " \t")
358 (unless (eobp) (forward-char 1))))))
360 (defun rfc2047-unfold-region (b e)
361 "Unfold lines in region B to E."
363 (narrow-to-region b e)
364 (goto-char (point-min))
365 (let ((bol (save-restriction
367 (gnus-point-at-bol)))
368 (eol (gnus-point-at-eol))
372 (looking-at "[ \t]*")
373 (setq leading (- (match-end 0) (match-beginning 0)))
374 (if (< (- (gnus-point-at-eol) bol leading) 76)
377 (delete-region eol (progn
378 (skip-chars-forward "[ \t\n\r]+")
380 (setq bol (gnus-point-at-bol)))
381 (setq eol (gnus-point-at-eol))
384 (defun rfc2047-b-encode-region (b e)
385 "Base64-encode the header contained in region B to E."
387 (narrow-to-region (goto-char b) e)
389 (base64-encode-region (point) (progn (end-of-line) (point)) t)
390 (if (and (bolp) (eolp))
391 (delete-backward-char 1))
394 (defun rfc2047-q-encode-region (b e)
395 "Quoted-printable-encode the header in region B to E."
398 (narrow-to-region (goto-char b) e)
399 (let ((alist rfc2047-q-encoding-alist)
400 (bol (save-restriction
402 (gnus-point-at-bol))))
404 (when (looking-at (caar alist))
405 (quoted-printable-encode-region b e nil (cdar alist))
406 (subst-char-in-region (point-min) (point-max) ? ?_)
409 ;; The size of QP encapsulation is about 20, so set limit to
411 (unless (< (- (point-max) (point-min)) 56)
412 ;; Don't break if it could fit in one line.
413 ;; Let rfc2047-encode-region break it later.
414 (goto-char (1+ (point-min)))
415 (while (and (not (bobp)) (not (eobp)))
416 (goto-char (min (point-max) (+ 56 bol)))
417 (search-backward "=" (- (point) 2) t)
418 (unless (or (bobp) (eobp))
420 (setq bol (point)))))))))
423 ;;; Functions for decoding RFC2047 messages
426 (defvar rfc2047-encoded-word-regexp
427 "=\\?\\([^][\000-\040()<>@,\;:\\\"/?.=]+\\)\\?\\(B\\|Q\\)\\?\\([!->@-~ +]+\\)\\?=")
429 (defun rfc2047-decode-region (start end)
430 "Decode MIME-encoded words in region between START and END."
432 (let ((case-fold-search t)
436 (narrow-to-region start end)
437 (goto-char (point-min))
438 ;; Remove whitespace between encoded words.
439 (while (re-search-forward
440 (concat "\\(" rfc2047-encoded-word-regexp "\\)"
442 "\\(" rfc2047-encoded-word-regexp "\\)")
444 (delete-region (goto-char (match-end 1)) (match-beginning 6)))
445 ;; Decode the encoded words.
446 (setq b (goto-char (point-min)))
447 (while (re-search-forward rfc2047-encoded-word-regexp nil t)
448 (setq e (match-beginning 0))
449 (insert (rfc2047-parse-and-decode
452 (delete-region (match-beginning 0) (match-end 0)))))
453 (when (and (mm-multibyte-p)
455 (not (eq mail-parse-charset 'gnus-decoded)))
456 (mm-decode-coding-region b e mail-parse-charset))
458 (when (and (mm-multibyte-p)
460 (not (eq mail-parse-charset 'us-ascii))
461 (not (eq mail-parse-charset 'gnus-decoded)))
462 (mm-decode-coding-region b (point-max) mail-parse-charset))
463 (rfc2047-unfold-region (point-min) (point-max))))))
465 (defun rfc2047-decode-string (string)
466 "Decode the quoted-printable-encoded STRING and return the results."
467 (let ((m (mm-multibyte-p)))
470 (mm-enable-multibyte))
473 (rfc2047-decode-region (point-min) (point-max)))
476 (defun rfc2047-parse-and-decode (word)
477 "Decode WORD and return it if it is an encoded word.
479 (if (not (string-match rfc2047-encoded-word-regexp word))
484 (match-string 1 word)
485 (upcase (match-string 2 word))
486 (match-string 3 word))
490 (defun rfc2047-decode (charset encoding string)
491 "Decode STRING from the given MIME CHARSET in the given ENCODING.
492 Valid ENCODINGs are \"B\" and \"Q\".
493 If your Emacs implementation can't decode CHARSET, return nil."
494 (if (stringp charset)
495 (setq charset (intern (downcase charset))))
496 (if (or (not charset)
497 (eq 'gnus-all mail-parse-ignored-charsets)
498 (memq 'gnus-all mail-parse-ignored-charsets)
499 (memq charset mail-parse-ignored-charsets))
500 (setq charset mail-parse-charset))
501 (let ((cs (mm-charset-to-coding-system charset)))
502 (if (and (not cs) charset
503 (listp mail-parse-ignored-charsets)
504 (memq 'gnus-unknown mail-parse-ignored-charsets))
505 (setq cs (mm-charset-to-coding-system mail-parse-charset)))
507 (when (and (eq cs 'ascii)
509 (setq cs mail-parse-charset))
510 ;; Ensure unibyte result in Emacs 20.
511 (let (default-enable-multibyte-characters)
513 (mm-decode-coding-string
515 ((equal "B" encoding)
516 (base64-decode-string string))
517 ((equal "Q" encoding)
518 (quoted-printable-decode-string
519 (mm-replace-chars-in-string string ?_ ? )))
520 (t (error "Invalid encoding: %s" encoding)))
525 ;;; rfc2047.el ends here