1 ;;; rfc2047.el --- Functions for encoding and decoding rfc2047 messages
2 ;; Copyright (C) 1998 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.
28 (if (not (fboundp 'base64-encode-string))
33 (defvar rfc2047-default-charset 'iso-8859-1
34 "Default MIME charset -- does not need encoding.")
36 (defvar rfc2047-header-encoding-alist
37 '(("Newsgroups" . nil)
40 "*Header/encoding method alist.
41 The list is traversed sequentially. The keys can either be
42 header regexps or `t'.
46 1) nil, in which case no encoding is done;
47 2) `mime', in which case the header will be encoded according to RFC2047;
48 3) a charset, in which case it will be encoded as that charse;
49 4) `default', in which case the field will be encoded as the rest
52 (defvar rfc2047-charset-encoding-alist
71 "Alist of MIME charsets to RFC2047 encodings.
72 Valid encodings are nil, `Q' and `B'.")
74 (defvar rfc2047-encoding-function-alist
75 '((Q . rfc2047-q-encode-region)
76 (B . base64-encode-region)
78 "Alist of RFC2047 encodings to encoding functions.")
80 (defvar rfc2047-q-encoding-alist
81 '(("\\(From\\|Cc\\|To\\|Bcc\||Reply-To\\):" . "[^-A-Za-z0-9!*+/=_]")
82 ("." . "[\000-\007\013\015-\037\200-\377=_?]"))
83 "Alist of header regexps and valid Q characters.")
86 ;;; Functions for encoding RFC2047 messages
89 (defun rfc2047-narrow-to-field ()
90 "Narrow the buffer to the header on the current line."
96 (if (re-search-forward "^[^ \n\t]" nil t)
101 (goto-char (point-min)))
104 (defun rfc2047-encode-message-header ()
105 "Encode the message header according to `rfc2047-header-encoding-alist'.
106 Should be called narrowed to the head of the message."
108 (when (featurep 'mule)
110 (let ((alist rfc2047-header-encoding-alist)
114 (rfc2047-narrow-to-field)
115 (when (rfc2047-encodable-p)
116 ;; We found something that may perhaps be encoded.
117 (while (setq elem (pop alist))
118 (when (or (and (stringp (car elem))
119 (looking-at (car elem)))
126 (rfc2047-encode-region (point-min) (point-max)))
129 (goto-char (point-max))))))))
131 (defun rfc2047-encodable-p ()
132 "Say whether the current (narrowed) buffer contains characters that need encoding."
133 (let ((charsets (mapcar
134 'mm-mule-charset-to-mime-charset
135 (find-charset-region (point-min) (point-max))))
136 (cs (list 'us-ascii rfc2047-default-charset))
139 (unless (memq (pop charsets) cs)
143 (defun rfc2047-encode-region (b e)
144 "Encode all encodable words in REGION."
145 (let (prev c start qstart qprev qend)
148 (while (re-search-forward "[^ \t\n]+" nil t)
150 (narrow-to-region (match-beginning 0) (match-end 0))
151 (goto-char (setq start (point-min)))
154 (unless (eq (setq c (char-charset (following-char))) 'ascii)
159 (setq qstart (or qstart start)
164 ;(rfc2047-encode start (setq start (point)) prev)
167 (when (and (not prev) qstart)
168 (rfc2047-encode qstart qend qprev)
171 (rfc2047-encode qstart qend qprev)
172 (setq qstart nil)))))
174 (defun rfc2047-encode-string (string)
175 "Encode words in STRING."
178 (rfc2047-encode-region (point-min) (point-max))
181 (defun rfc2047-encode (b e charset)
182 "Encode the word in the region with CHARSET."
183 (let* ((mime-charset (mm-mule-charset-to-mime-charset charset))
184 (encoding (cdr (assq mime-charset
185 rfc2047-charset-encoding-alist)))
187 "=?" (downcase (symbol-name mime-charset)) "?"
188 (downcase (symbol-name encoding)) "?")))
190 (narrow-to-region b e)
191 (mm-encode-coding-region b e mime-charset)
192 (funcall (cdr (assq encoding rfc2047-encoding-function-alist))
193 (point-min) (point-max))
194 (goto-char (point-min))
196 (goto-char (point-max))
198 ;; Encoded words can't be more than 75 chars long, so we have to
199 ;; split the long ones up.
201 (while (> (current-column) 74)
204 (insert "?=\n " start)
207 (defun rfc2047-q-encode-region (b e)
208 "Encode the header contained in REGION with the Q encoding."
211 (narrow-to-region (goto-char b) e)
212 (let ((alist rfc2047-q-encoding-alist))
214 (when (looking-at (caar alist))
215 (quoted-printable-encode-region b e nil (cdar alist))
216 (subst-char-in-region (point-min) (point-max) ? ?_))
220 ;;; Functions for decoding RFC2047 messages
223 (defvar rfc2047-encoded-word-regexp
224 "=\\?\\([^][\000-\040()<>@,\;:\\\"/?.=]+\\)\\?\\(B\\|Q\\)\\?\\([!->@-~ ]+\\)\\?=")
227 (defun rfc2047-decode-region (start end)
228 "Decode MIME-encoded words in region between START and END."
230 (let ((case-fold-search t)
234 (narrow-to-region start end)
235 (goto-char (point-min))
236 ;; Remove whitespace between encoded words.
237 (while (re-search-forward
238 (concat "\\(" rfc2047-encoded-word-regexp "\\)"
240 "\\(" rfc2047-encoded-word-regexp "\\)")
242 (delete-region (goto-char (match-end 1)) (match-beginning 6)))
243 ;; Decode the encoded words.
244 (setq b (goto-char (point-min)))
245 (while (re-search-forward rfc2047-encoded-word-regexp nil t)
246 (setq e (match-beginning 0))
247 (insert (rfc2047-parse-and-decode
250 (delete-region (match-beginning 0) (match-end 0)))))
251 (mm-decode-coding-region b e rfc2047-default-charset)
253 (mm-decode-coding-region b (point-max) rfc2047-default-charset)))))
256 (defun rfc2047-decode-string (string)
257 "Decode the quoted-printable-encoded STRING and return the results."
259 (mm-enable-multibyte)
262 (rfc2047-decode-region (point-min) (point-max)))
265 (defun rfc2047-parse-and-decode (word)
266 "Decode WORD and return it if it is an encoded word.
268 (if (not (string-match rfc2047-encoded-word-regexp word))
273 (match-string 1 word)
274 (upcase (match-string 2 word))
275 (match-string 3 word))
279 (defun rfc2047-decode (charset encoding string)
280 "Decode STRING that uses CHARSET with ENCODING.
281 Valid ENCODINGs are \"B\" and \"Q\".
282 If your Emacs implementation can't decode CHARSET, it returns nil."
283 (let ((cs (mm-charset-to-coding-system charset)))
285 (mm-decode-coding-string
287 ((equal "B" encoding)
288 (if (fboundp 'base64-decode-string)
289 (base64-decode-string string)
290 (base64-decode string)))
291 ((equal "Q" encoding)
292 (quoted-printable-decode-string
293 (mm-replace-chars-in-string string ?_ ? )))
294 (t (error "Invalid encoding: %s" encoding)))
299 ;;; rfc2047.el ends here