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 (rfc2047-fold-region (point-min) (point-max)))
142 ((eq method 'default)
143 (if (and (featurep 'mule)
145 (mm-encode-coding-region (point-min) (point-max)
146 mail-parse-charset)))
147 ((mm-coding-system-p method)
149 (mm-encode-coding-region (point-min) (point-max) method)))
152 (goto-char (point-max)))))))
154 (defun rfc2047-encodable-p (&optional header)
155 "Say whether the current (narrowed) buffer contains characters that need encoding in headers."
159 (mm-find-charset-region (point-min) (point-max))))
160 (cs (list 'us-ascii (car message-posting-charset)))
163 (unless (memq (pop charsets) cs)
167 (defun rfc2047-dissect-region (b e)
168 "Dissect the region between B and E into words."
169 (let ((all-specials (concat ietf-drums-tspecials " \t\n\r"))
170 (special-list (mapcar 'identity ietf-drums-tspecials))
171 (blank-list '(? ?\t ?\n ?\r))
172 words current cs state mail-parse-mule-charset)
174 (narrow-to-region b e)
175 (goto-char (point-min))
176 (skip-chars-forward all-specials)
182 (if (not (eq (setq cs (mm-charset-after)) 'ascii))
187 ((memq (char-after) special-list)
189 ((memq (char-after) blank-list))
194 (if (not (eq (setq cs (mm-charset-after)) 'ascii))
195 (setq current cs)))))
198 ((memq (char-after) special-list)
200 (push (list b (point) current) words)
202 ((memq (char-after) blank-list)
206 (push (list b (point) current) words)
209 ((or (eq (setq cs (mm-charset-after)) 'ascii)
214 (push (list b (point) current) words)
219 (skip-chars-forward all-specials)))
221 (push (list b (point) current) words)))
224 (defun rfc2047-encode-region (b e)
225 "Encode all encodable words in REGION."
226 (let ((words (rfc2047-dissect-region b e))
227 beg end current word)
228 (while (setq word (pop words))
229 (if (and (eq (nth 2 word) current)
230 (eq beg (nth 1 word)))
231 (setq beg (nth 0 word))
233 (if (and (eq beg (nth 1 word)) (nth 2 word))
235 ;; There might be a bug in Emacs Mule.
236 ;; A space must be inserted before encoding.
239 (rfc2047-encode (1+ beg) (1+ end) current))
240 (rfc2047-encode beg end current)))
241 (setq current (nth 2 word)
245 (rfc2047-encode beg end current))))
247 (defun rfc2047-encode-string (string)
248 "Encode words in STRING."
251 (rfc2047-encode-region (point-min) (point-max))
254 (defun rfc2047-encode (b e charset)
255 "Encode the word in the region with CHARSET."
256 (let* ((mime-charset (mm-mime-charset charset))
257 (encoding (or (cdr (assq mime-charset
258 rfc2047-charset-encoding-alist))
261 "=?" (downcase (symbol-name mime-charset)) "?"
262 (downcase (symbol-name encoding)) "?"))
265 (narrow-to-region b e)
266 (when (eq encoding 'B)
267 ;; break into lines before encoding
268 (goto-char (point-min))
270 (goto-char (min (point-max) (+ 15 (point))))
273 (if (and (mm-multibyte-p)
274 (mm-coding-system-p mime-charset))
275 (mm-encode-coding-region (point-min) (point-max) mime-charset))
276 (funcall (cdr (assq encoding rfc2047-encoding-function-alist))
277 (point-min) (point-max))
278 (goto-char (point-min))
288 (defun rfc2047-fold-region (b e)
289 "Fold the long lines in the region."
291 (narrow-to-region b e)
292 (goto-char (point-min))
296 ((memq (char-after) '(? ?\t))
297 (setq break (point)))
300 (setq break (point)))
303 (> (- (point) (gnus-point-at-bol)) 76))
308 (forward-char 1))))))
310 (defun rfc2047-b-encode-region (b e)
311 "Encode the header contained in REGION with the B encoding."
313 (narrow-to-region (goto-char b) e)
315 (base64-encode-region (point) (progn (end-of-line) (point)) t)
316 (if (and (bolp) (eolp))
317 (delete-backward-char 1))
320 (defun rfc2047-q-encode-region (b e)
321 "Encode the header contained in REGION with the Q encoding."
324 (narrow-to-region (goto-char b) e)
325 (let ((alist rfc2047-q-encoding-alist))
327 (when (looking-at (caar alist))
328 (quoted-printable-encode-region b e nil (cdar alist))
329 (subst-char-in-region (point-min) (point-max) ? ?_)
332 (goto-char (point-min))
334 (goto-char (min (point-max) (+ 64 (point))))
335 (search-backward "=" (- (point) 2) t)
340 ;;; Functions for decoding RFC2047 messages
343 (defvar rfc2047-encoded-word-regexp
344 "=\\?\\([^][\000-\040()<>@,\;:\\\"/?.=]+\\)\\?\\(B\\|Q\\)\\?\\([!->@-~ +]+\\)\\?=")
346 (defun rfc2047-decode-region (start end)
347 "Decode MIME-encoded words in region between START and END."
349 (let ((case-fold-search t)
353 (narrow-to-region start end)
354 (goto-char (point-min))
355 ;; Remove whitespace between encoded words.
356 (while (re-search-forward
357 (concat "\\(" rfc2047-encoded-word-regexp "\\)"
359 "\\(" rfc2047-encoded-word-regexp "\\)")
361 (delete-region (goto-char (match-end 1)) (match-beginning 6)))
362 ;; Decode the encoded words.
363 (setq b (goto-char (point-min)))
364 (while (re-search-forward rfc2047-encoded-word-regexp nil t)
365 (setq e (match-beginning 0))
366 (insert (rfc2047-parse-and-decode
369 (delete-region (match-beginning 0) (match-end 0)))))
370 (when (and (mm-multibyte-p)
372 (not (eq mail-parse-charset 'gnus-decoded)))
373 (mm-decode-coding-region b e mail-parse-charset))
375 (when (and (mm-multibyte-p)
377 (not (eq mail-parse-charset 'us-ascii))
378 (not (eq mail-parse-charset 'gnus-decoded)))
379 (mm-decode-coding-region b (point-max) mail-parse-charset))))))
381 (defun rfc2047-decode-string (string)
382 "Decode the quoted-printable-encoded STRING and return the results."
383 (let ((m (mm-multibyte-p)))
386 (mm-enable-multibyte))
389 (rfc2047-decode-region (point-min) (point-max)))
392 (defun rfc2047-parse-and-decode (word)
393 "Decode WORD and return it if it is an encoded word.
395 (if (not (string-match rfc2047-encoded-word-regexp word))
400 (match-string 1 word)
401 (upcase (match-string 2 word))
402 (match-string 3 word))
406 (defun rfc2047-decode (charset encoding string)
407 "Decode STRING that uses CHARSET with ENCODING.
408 Valid ENCODINGs are \"B\" and \"Q\".
409 If your Emacs implementation can't decode CHARSET, it returns nil."
410 (if (stringp charset)
411 (setq charset (intern (downcase charset))))
412 (if (or (not charset)
413 (eq 'gnus-all mail-parse-ignored-charsets)
414 (memq 'gnus-all mail-parse-ignored-charsets)
415 (memq charset mail-parse-ignored-charsets))
416 (setq charset mail-parse-charset))
417 (let ((cs (mm-charset-to-coding-system charset)))
418 (if (and (not cs) charset
419 (listp mail-parse-ignored-charsets)
420 (memq 'gnus-unknown mail-parse-ignored-charsets))
421 (setq cs (mm-charset-to-coding-system mail-parse-charset)))
423 (when (and (eq cs 'ascii)
425 (setq cs mail-parse-charset))
426 (mm-decode-coding-string
428 ((equal "B" encoding)
429 (base64-decode-string string))
430 ((equal "Q" encoding)
431 (quoted-printable-decode-string
432 (mm-replace-chars-in-string string ?_ ? )))
433 (t (error "Invalid encoding: %s" encoding)))
438 ;;; rfc2047.el ends here