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.
29 '(unless (fboundp 'base64-decode-string)
37 (defvar rfc2047-header-encoding-alist
38 '(("Newsgroups" . nil)
41 "*Header/encoding method alist.
42 The list is traversed sequentially. The keys can either be
43 header regexps or `t'.
47 1) nil, in which case no encoding is done;
48 2) `mime', in which case the header will be encoded according to RFC2047;
49 3) a charset, in which case it will be encoded as that charset;
50 4) `default', in which case the field will be encoded as the rest
53 (defvar rfc2047-charset-encoding-alist
74 "Alist of MIME charsets to RFC2047 encodings.
75 Valid encodings are nil, `Q' and `B'.")
77 (defvar rfc2047-encoding-function-alist
78 '((Q . rfc2047-q-encode-region)
79 (B . rfc2047-b-encode-region)
81 "Alist of RFC2047 encodings to encoding functions.")
83 (defvar rfc2047-q-encoding-alist
84 '(("\\(From\\|Cc\\|To\\|Bcc\||Reply-To\\):" . "-A-Za-z0-9!*+/")
85 ;; = (\075), _ (\137), ? (\077) are used in the encoded word.
86 ;; Avoid using 8bit characters. Some versions of Emacs has bug!
87 ;; Equivalent to "^\000-\007\011\013\015-\037\200-\377=_?"
88 ("." . "\010\012\014\040-\074\076\100-\136\140-\177"))
89 "Alist of header regexps and valid Q characters.")
92 ;;; Functions for encoding RFC2047 messages
95 (defun rfc2047-narrow-to-field ()
96 "Narrow the buffer to the header on the current line."
102 (if (re-search-forward "^[^ \n\t]" nil t)
107 (goto-char (point-min)))
109 (defun rfc2047-encode-message-header ()
110 "Encode the message header according to `rfc2047-header-encoding-alist'.
111 Should be called narrowed to the head of the message."
114 (goto-char (point-min))
115 (let (alist elem method)
118 (rfc2047-narrow-to-field)
119 (if (not (rfc2047-encodable-p))
120 (if (and (eq (mm-body-7-or-8) '8bit)
123 (car message-posting-charset)))
124 ;; 8 bit must be decoded.
125 ;; Is message-posting-charset a coding system?
126 (mm-encode-coding-region
127 (point-min) (point-max)
128 (car message-posting-charset)))
129 ;; We found something that may perhaps be encoded.
131 alist rfc2047-header-encoding-alist)
132 (while (setq elem (pop alist))
133 (when (or (and (stringp (car elem))
134 (looking-at (car elem)))
140 (rfc2047-encode-region (point-min) (point-max)))
141 ((eq method 'default)
142 (if (and (featurep 'mule)
144 (mm-encode-coding-region (point-min) (point-max)
145 mail-parse-charset)))
146 ((mm-coding-system-p method)
148 (mm-encode-coding-region (point-min) (point-max) method)))
151 (goto-char (point-max)))))))
153 (defun rfc2047-encodable-p (&optional header)
154 "Say whether the current (narrowed) buffer contains characters that need encoding in headers."
158 (mm-find-charset-region (point-min) (point-max))))
159 (cs (list 'us-ascii (car message-posting-charset)))
162 (unless (memq (pop charsets) cs)
166 (defun rfc2047-dissect-region (b e)
167 "Dissect the region between B and E into words."
168 (let ((word-chars "-A-Za-z0-9!*+/")
169 ;; Not using ietf-drums-specials-token makes life simple.
170 mail-parse-mule-charset
174 (narrow-to-region b e)
175 (goto-char (point-min))
176 (skip-chars-forward "\000-\177")
179 (skip-chars-backward word-chars b)
180 (unless (eq b (point))
181 (push (cons (buffer-substring b (point)) nil) words))
184 (setq current (mm-charset-after))
186 (skip-chars-forward word-chars)
187 (while (and (not (eobp))
188 (eq (mm-charset-after) current))
190 (skip-chars-forward word-chars))
191 (unless (eq b (point))
192 (push (cons (buffer-substring b (point)) current) words))
194 (skip-chars-forward "\000-\177"))
195 (unless (eq b (point))
196 (push (cons (buffer-substring b (point)) nil) words)))
197 ;; merge adjacent words
198 (setq word (pop words))
203 (not (string-match "[^ \t]" (caar words))))
204 (if (eq (cdr (nth 1 words)) (cdr word))
206 (setq word (cons (concat
207 (car (nth 1 words)) (caar words)
212 (push (cons (concat (caar words) (car word)) (cdr word))
215 (setq word (pop words)))
217 (setq word (pop words))))
220 (defun rfc2047-encode-region (b e)
221 "Encode all encodable words in REGION."
222 (let ((words (rfc2047-dissect-region b e)) word)
224 (narrow-to-region b e)
225 (delete-region (point-min) (point-max))
226 (while (setq word (pop words))
229 (rfc2047-fold-region (gnus-point-at-bol) (point))
230 (goto-char (point-max))
231 (if (> (- (point) (save-restriction
233 (gnus-point-at-bol))) 76)
235 ;; Insert blank between encoded words
236 (if (eq (char-before) ?=) (insert " "))
237 (rfc2047-encode (point)
238 (progn (insert (car word)) (point))
240 (rfc2047-fold-region (point-min) (point-max)))))
242 (defun rfc2047-encode-string (string)
243 "Encode words in STRING."
246 (rfc2047-encode-region (point-min) (point-max))
249 (defun rfc2047-encode (b e charset)
250 "Encode the word in the region with CHARSET."
251 (let* ((mime-charset (mm-mime-charset charset))
252 (encoding (or (cdr (assq mime-charset
253 rfc2047-charset-encoding-alist))
256 "=?" (downcase (symbol-name mime-charset)) "?"
257 (downcase (symbol-name encoding)) "?"))
260 (narrow-to-region b e)
261 (when (eq encoding 'B)
262 ;; break into lines before encoding
263 (goto-char (point-min))
265 (goto-char (min (point-max) (+ 15 (point))))
268 (if (and (mm-multibyte-p)
269 (mm-coding-system-p mime-charset))
270 (mm-encode-coding-region (point-min) (point-max) mime-charset))
271 (funcall (cdr (assq encoding rfc2047-encoding-function-alist))
272 (point-min) (point-max))
273 (goto-char (point-min))
283 (defun rfc2047-fold-region (b e)
284 "Fold the long lines in the region."
286 (narrow-to-region b e)
287 (goto-char (point-min))
290 (bol (save-restriction
292 (gnus-point-at-bol))))
294 (when (and (or break qword-break) (> (- (point) bol) 76))
295 (goto-char (or break qword-break))
299 (setq bol (1- (point)))
300 ;; Don't break before the first non-LWSP characters.
301 (skip-chars-forward " \t")
304 ((eq (char-after) ?\n)
309 (skip-chars-forward " \t")
310 (unless (or (eobp) (eq (char-after) ?\n))
312 ((eq (char-after) ?\r)
314 ((memq (char-after) '(? ?\t))
315 (skip-chars-forward " \t")
316 (setq break (1- (point))))
318 (if (not (looking-at "=\\?[^=]"))
319 (if (eq (char-after) ?=)
321 (skip-chars-forward "^ \t\n\r="))
322 (setq qword-break (point))
323 (skip-chars-forward "^ \t\n\r")))
325 (skip-chars-forward "^ \t\n\r"))))
326 (when (and (or break qword-break) (> (- (point) bol) 76))
327 (goto-char (or break qword-break))
331 (setq bol (1- (point)))
332 ;; Don't break before the first non-LWSP characters.
333 (skip-chars-forward " \t")
336 (defun rfc2047-unfold-region (b e)
337 "Fold the long lines in the region."
339 (narrow-to-region b e)
340 (goto-char (point-min))
341 (let ((bol (save-restriction
343 (gnus-point-at-bol)))
344 (eol (gnus-point-at-eol))
348 (looking-at "[ \t]*")
349 (setq leading (- (match-end 0) (match-beginning 0)))
350 (if (< (- (gnus-point-at-eol) bol leading) 76)
353 (delete-region eol (progn
354 (skip-chars-forward "[ \t\n\r]+")
356 (setq bol (gnus-point-at-bol)))
357 (setq eol (gnus-point-at-eol))
360 (defun rfc2047-b-encode-region (b e)
361 "Encode the header contained in REGION with the B encoding."
363 (narrow-to-region (goto-char b) e)
365 (base64-encode-region (point) (progn (end-of-line) (point)) t)
366 (if (and (bolp) (eolp))
367 (delete-backward-char 1))
370 (defun rfc2047-q-encode-region (b e)
371 "Encode the header contained in REGION with the Q encoding."
374 (narrow-to-region (goto-char b) e)
375 (let ((alist rfc2047-q-encoding-alist))
377 (when (looking-at (caar alist))
378 (quoted-printable-encode-region b e nil (cdar alist))
379 (subst-char-in-region (point-min) (point-max) ? ?_)
382 (goto-char (1+ (point-min)))
383 (while (and (not (bobp)) (not (eobp)))
384 (goto-char (min (point-max) (save-restriction
386 ;; THe QP encapsulation is about 20.
387 (+ 56 (gnus-point-at-bol)))))
388 (search-backward "=" (- (point) 2) t)
389 (unless (or (bobp) (eobp))
393 ;;; Functions for decoding RFC2047 messages
396 (defvar rfc2047-encoded-word-regexp
397 "=\\?\\([^][\000-\040()<>@,\;:\\\"/?.=]+\\)\\?\\(B\\|Q\\)\\?\\([!->@-~ +]+\\)\\?=")
399 (defun rfc2047-decode-region (start end)
400 "Decode MIME-encoded words in region between START and END."
402 (let ((case-fold-search t)
406 (narrow-to-region start end)
407 (goto-char (point-min))
408 ;; Remove whitespace between encoded words.
409 (while (re-search-forward
410 (concat "\\(" rfc2047-encoded-word-regexp "\\)"
412 "\\(" rfc2047-encoded-word-regexp "\\)")
414 (delete-region (goto-char (match-end 1)) (match-beginning 6)))
415 ;; Decode the encoded words.
416 (setq b (goto-char (point-min)))
417 (while (re-search-forward rfc2047-encoded-word-regexp nil t)
418 (setq e (match-beginning 0))
419 (insert (rfc2047-parse-and-decode
422 (delete-region (match-beginning 0) (match-end 0)))))
423 (when (and (mm-multibyte-p)
425 (not (eq mail-parse-charset 'gnus-decoded)))
426 (mm-decode-coding-region b e mail-parse-charset))
428 (when (and (mm-multibyte-p)
430 (not (eq mail-parse-charset 'us-ascii))
431 (not (eq mail-parse-charset 'gnus-decoded)))
432 (mm-decode-coding-region b (point-max) mail-parse-charset))
433 (rfc2047-unfold-region (point-min) (point-max))))))
435 (defun rfc2047-decode-string (string)
436 "Decode the quoted-printable-encoded STRING and return the results."
437 (let ((m (mm-multibyte-p)))
440 (mm-enable-multibyte))
443 (rfc2047-decode-region (point-min) (point-max)))
446 (defun rfc2047-parse-and-decode (word)
447 "Decode WORD and return it if it is an encoded word.
449 (if (not (string-match rfc2047-encoded-word-regexp word))
454 (match-string 1 word)
455 (upcase (match-string 2 word))
456 (match-string 3 word))
460 (defun rfc2047-decode (charset encoding string)
461 "Decode STRING that uses CHARSET with ENCODING.
462 Valid ENCODINGs are \"B\" and \"Q\".
463 If your Emacs implementation can't decode CHARSET, it returns nil."
464 (if (stringp charset)
465 (setq charset (intern (downcase charset))))
466 (if (or (not charset)
467 (eq 'gnus-all mail-parse-ignored-charsets)
468 (memq 'gnus-all mail-parse-ignored-charsets)
469 (memq charset mail-parse-ignored-charsets))
470 (setq charset mail-parse-charset))
471 (let ((cs (mm-charset-to-coding-system charset)))
472 (if (and (not cs) charset
473 (listp mail-parse-ignored-charsets)
474 (memq 'gnus-unknown mail-parse-ignored-charsets))
475 (setq cs (mm-charset-to-coding-system mail-parse-charset)))
477 (when (and (eq cs 'ascii)
479 (setq cs mail-parse-charset))
480 (mm-with-unibyte-current-buffer-mule4
481 ;; In Emacs Mule 4, decoding UTF-8 should be in unibyte mode.
482 (mm-decode-coding-string
484 ((equal "B" encoding)
485 (base64-decode-string string))
486 ((equal "Q" encoding)
487 (quoted-printable-decode-string
488 (mm-replace-chars-in-string string ?_ ? )))
489 (t (error "Invalid encoding: %s" encoding)))
494 ;;; rfc2047.el ends here