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'.")
83 (defvar rfc2047-encoding-function-alist
84 '((Q . rfc2047-q-encode-region
)
85 (B . rfc2047-b-encode-region
)
87 "Alist of RFC2047 encodings to encoding functions.")
89 (defvar rfc2047-q-encoding-alist
90 '(("\\(From\\|Cc\\|To\\|Bcc\||Reply-To\\):" .
"-A-Za-z0-9!*+/")
91 ;; = (\075), _ (\137), ? (\077) are used in the encoded word.
92 ;; Avoid using 8bit characters. Some versions of Emacs has bug!
93 ;; Equivalent to "^\000-\007\011\013\015-\037\200-\377=_?"
94 ("." .
"\010\012\014\040-\074\076\100-\136\140-\177"))
95 "Alist of header regexps and valid Q characters.")
98 ;;; Functions for encoding RFC2047 messages
101 (defun rfc2047-narrow-to-field ()
102 "Narrow the buffer to the header on the current line."
108 (if (re-search-forward "^[^ \n\t]" nil t
)
113 (goto-char (point-min)))
115 (defun rfc2047-encode-message-header ()
116 "Encode the message header according to `rfc2047-header-encoding-alist'.
117 Should be called narrowed to the head of the message."
120 (goto-char (point-min))
121 (let (alist elem method
)
124 (rfc2047-narrow-to-field)
125 (if (not (rfc2047-encodable-p))
126 (if (and (eq (mm-body-7-or-8) '8bit
)
129 (car message-posting-charset
)))
130 ;; 8 bit must be decoded.
131 ;; Is message-posting-charset a coding system?
132 (mm-encode-coding-region
133 (point-min) (point-max)
134 (car message-posting-charset
)))
135 ;; We found something that may perhaps be encoded.
137 alist rfc2047-header-encoding-alist
)
138 (while (setq elem
(pop alist
))
139 (when (or (and (stringp (car elem
))
140 (looking-at (car elem
)))
146 (rfc2047-encode-region (point-min) (point-max)))
147 ((eq method
'default
)
148 (if (and (featurep 'mule
)
149 (if (boundp 'default-enable-multibyte-characters
)
150 default-enable-multibyte-characters
)
152 (mm-encode-coding-region (point-min) (point-max)
153 mail-parse-charset
)))
154 ((mm-coding-system-p method
)
155 (if (and (featurep 'mule
)
156 (if (boundp 'default-enable-multibyte-characters
)
157 default-enable-multibyte-characters
))
158 (mm-encode-coding-region (point-min) (point-max) method
)))
161 (goto-char (point-max)))))))
163 (defun rfc2047-encodable-p ()
164 "Return non-nil if any characters in current buffer need encoding in headers.
165 The buffer may be narrowed."
169 (mm-find-charset-region (point-min) (point-max))))
170 (cs (list 'us-ascii
(car message-posting-charset
)))
173 (unless (memq (pop charsets
) cs
)
177 (defun rfc2047-dissect-region (b e
)
178 "Dissect the region between B and E into words."
179 (let ((word-chars "-A-Za-z0-9!*+/")
180 ;; Not using ietf-drums-specials-token makes life simple.
181 mail-parse-mule-charset
185 (narrow-to-region b e
)
186 (goto-char (point-min))
187 (skip-chars-forward "\000-\177")
190 (skip-chars-backward word-chars b
)
191 (unless (eq b
(point))
192 (push (cons (buffer-substring b
(point)) nil
) words
))
195 (setq current
(mm-charset-after))
197 (skip-chars-forward word-chars
)
198 (while (and (not (eobp))
199 (eq (mm-charset-after) current
))
201 (skip-chars-forward word-chars
))
202 (unless (eq b
(point))
203 (push (cons (buffer-substring b
(point)) current
) words
))
205 (skip-chars-forward "\000-\177"))
206 (unless (eq b
(point))
207 (push (cons (buffer-substring b
(point)) nil
) words
)))
208 ;; merge adjacent words
209 (setq word
(pop words
))
214 (not (string-match "[^ \t]" (caar words
))))
215 (if (eq (cdr (nth 1 words
)) (cdr word
))
217 (setq word
(cons (concat
218 (car (nth 1 words
)) (caar words
)
223 (push (cons (concat (caar words
) (car word
)) (cdr word
))
226 (setq word
(pop words
)))
228 (setq word
(pop words
))))
231 (defun rfc2047-encode-region (b e
)
232 "Encode all encodable words in region."
233 (let ((words (rfc2047-dissect-region b e
)) word
)
235 (narrow-to-region b e
)
236 (delete-region (point-min) (point-max))
237 (while (setq word
(pop words
))
240 (rfc2047-fold-region (gnus-point-at-bol) (point))
241 (goto-char (point-max))
242 (if (> (- (point) (save-restriction
244 (gnus-point-at-bol))) 76)
246 ;; Insert blank between encoded words
247 (if (eq (char-before) ?
=) (insert " "))
248 (rfc2047-encode (point)
249 (progn (insert (car word
)) (point))
251 (rfc2047-fold-region (point-min) (point-max)))))
253 (defun rfc2047-encode-string (string)
254 "Encode words in STRING."
257 (rfc2047-encode-region (point-min) (point-max))
260 (defun rfc2047-encode (b e charset
)
261 "Encode the word in the region B to E with CHARSET."
262 (let* ((mime-charset (mm-mime-charset charset
))
263 (cs (mm-charset-to-coding-system mime-charset
))
264 (encoding (or (cdr (assq mime-charset
265 rfc2047-charset-encoding-alist
))
268 "=?" (downcase (symbol-name mime-charset
)) "?"
269 (downcase (symbol-name encoding
)) "?"))
272 (narrow-to-region b e
)
273 (when (eq encoding
'B
)
274 ;; break into lines before encoding
275 (goto-char (point-min))
277 (goto-char (min (point-max) (+ 15 (point))))
280 (if (and (mm-multibyte-p)
281 (mm-coding-system-p cs
))
282 (mm-encode-coding-region (point-min) (point-max) cs
))
283 (funcall (cdr (assq encoding rfc2047-encoding-function-alist
))
284 (point-min) (point-max))
285 (goto-char (point-min))
295 (defun rfc2047-fold-region (b e
)
296 "Fold long lines in the region."
298 (narrow-to-region b e
)
299 (goto-char (point-min))
302 (bol (save-restriction
304 (gnus-point-at-bol))))
306 (when (and (or break qword-break
) (> (- (point) bol
) 76))
307 (goto-char (or break qword-break
))
310 (if (looking-at " \t")
313 (setq bol
(1- (point)))
314 ;; Don't break before the first non-LWSP characters.
315 (skip-chars-forward " \t")
318 ((eq (char-after) ?
\n)
323 (skip-chars-forward " \t")
324 (unless (or (eobp) (eq (char-after) ?
\n))
326 ((eq (char-after) ?
\r)
328 ((memq (char-after) '(? ?
\t))
329 (skip-chars-forward " \t")
330 (setq break
(1- (point))))
332 (if (not (looking-at "=\\?[^=]"))
333 (if (eq (char-after) ?
=)
335 (skip-chars-forward "^ \t\n\r="))
336 (setq qword-break
(point))
337 (skip-chars-forward "^ \t\n\r")))
339 (skip-chars-forward "^ \t\n\r"))))
340 (when (and (or break qword-break
) (> (- (point) bol
) 76))
341 (goto-char (or break qword-break
))
344 (if (looking-at " \t")
347 (setq bol
(1- (point)))
348 ;; Don't break before the first non-LWSP characters.
349 (skip-chars-forward " \t")
352 (defun rfc2047-unfold-region (b e
)
353 "Unfold lines in the region."
355 (narrow-to-region b e
)
356 (goto-char (point-min))
357 (let ((bol (save-restriction
359 (gnus-point-at-bol)))
360 (eol (gnus-point-at-eol))
364 (looking-at "[ \t]*")
365 (setq leading
(- (match-end 0) (match-beginning 0)))
366 (if (< (- (gnus-point-at-eol) bol leading
) 76)
369 (delete-region eol
(progn
370 (skip-chars-forward "[ \t\n\r]+")
372 (setq bol
(gnus-point-at-bol)))
373 (setq eol
(gnus-point-at-eol))
376 (defun rfc2047-b-encode-region (b e
)
377 "Base64-encode the header contained in region B to E."
379 (narrow-to-region (goto-char b
) e
)
381 (base64-encode-region (point) (progn (end-of-line) (point)) t
)
382 (if (and (bolp) (eolp))
383 (delete-backward-char 1))
386 (defun rfc2047-q-encode-region (b e
)
387 "Quoted-printable-encode the header in region B to E."
390 (narrow-to-region (goto-char b
) e
)
391 (let ((alist rfc2047-q-encoding-alist
)
392 (bol (save-restriction
394 (gnus-point-at-bol))))
396 (when (looking-at (caar alist
))
397 (quoted-printable-encode-region b e nil
(cdar alist
))
398 (subst-char-in-region (point-min) (point-max) ? ?_
)
401 ;; The size of QP encapsulation is about 20, so set limit to
403 (unless (< (- (point-max) (point-min)) 56)
404 ;; Don't break if it could fit in one line.
405 ;; Let rfc2047-encode-region break it later.
406 (goto-char (1+ (point-min)))
407 (while (and (not (bobp)) (not (eobp)))
408 (goto-char (min (point-max) (+ 56 bol
)))
409 (search-backward "=" (- (point) 2) t
)
410 (unless (or (bobp) (eobp))
412 (setq bol
(point)))))))))
415 ;;; Functions for decoding RFC2047 messages
418 (defvar rfc2047-encoded-word-regexp
419 "=\\?\\([^][\000-\040()<>@,\;:\\\"/?.=]+\\)\\?\\(B\\|Q\\)\\?\\([!->@-~ +]+\\)\\?=")
421 (defun rfc2047-decode-region (start end
)
422 "Decode MIME-encoded words in region between START and END."
424 (let ((case-fold-search t
)
428 (narrow-to-region start end
)
429 (goto-char (point-min))
430 ;; Remove whitespace between encoded words.
431 (while (re-search-forward
432 (concat "\\(" rfc2047-encoded-word-regexp
"\\)"
434 "\\(" rfc2047-encoded-word-regexp
"\\)")
436 (delete-region (goto-char (match-end 1)) (match-beginning 6)))
437 ;; Decode the encoded words.
438 (setq b
(goto-char (point-min)))
439 (while (re-search-forward rfc2047-encoded-word-regexp nil t
)
440 (setq e
(match-beginning 0))
441 (insert (rfc2047-parse-and-decode
444 (delete-region (match-beginning 0) (match-end 0)))))
445 (when (and (mm-multibyte-p)
447 (not (eq mail-parse-charset
'gnus-decoded
)))
448 (mm-decode-coding-region b e mail-parse-charset
))
450 (when (and (mm-multibyte-p)
452 (not (eq mail-parse-charset
'us-ascii
))
453 (not (eq mail-parse-charset
'gnus-decoded
)))
454 (mm-decode-coding-region b
(point-max) mail-parse-charset
))
455 (rfc2047-unfold-region (point-min) (point-max))))))
457 (defun rfc2047-decode-string (string)
458 "Decode the quoted-printable-encoded STRING and return the results."
459 (let ((m (mm-multibyte-p)))
462 (mm-enable-multibyte))
465 (rfc2047-decode-region (point-min) (point-max)))
468 (defun rfc2047-parse-and-decode (word)
469 "Decode WORD and return it if it is an encoded word.
471 (if (not (string-match rfc2047-encoded-word-regexp word
))
476 (match-string 1 word
)
477 (upcase (match-string 2 word
))
478 (match-string 3 word
))
482 (defun rfc2047-decode (charset encoding string
)
483 "Decode STRING from the given MIME CHARSET in the given ENCODING.
484 Valid ENCODINGs are \"B\" and \"Q\".
485 If your Emacs implementation can't decode CHARSET, return nil."
486 (if (stringp charset
)
487 (setq charset
(intern (downcase charset
))))
488 (if (or (not charset
)
489 (eq 'gnus-all mail-parse-ignored-charsets
)
490 (memq 'gnus-all mail-parse-ignored-charsets
)
491 (memq charset mail-parse-ignored-charsets
))
492 (setq charset mail-parse-charset
))
493 (let ((cs (mm-charset-to-coding-system charset
)))
494 (if (and (not cs
) charset
495 (listp mail-parse-ignored-charsets
)
496 (memq 'gnus-unknown mail-parse-ignored-charsets
))
497 (setq cs
(mm-charset-to-coding-system mail-parse-charset
)))
499 (when (and (eq cs
'ascii
)
501 (setq cs mail-parse-charset
))
502 ;; Ensure unibyte result in Emacs 20.
503 (let (default-enable-multibyte-characters)
505 (mm-decode-coding-string
507 ((equal "B" encoding
)
508 (base64-decode-string string
))
509 ((equal "Q" encoding
)
510 (quoted-printable-decode-string
511 (mm-replace-chars-in-string string ?_ ?
)))
512 (t (error "Invalid encoding: %s" encoding
)))
517 ;;; rfc2047.el ends here