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.
29 '(if (not (fboundp 'base64-encode-string))
34 (defvar rfc2047-default-charset 'iso-8859-1
35 "Default MIME charset -- does not need encoding.")
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 charse;
50 4) `default', in which case the field will be encoded as the rest
53 (defvar rfc2047-charset-encoding-alist
72 "Alist of MIME charsets to RFC2047 encodings.
73 Valid encodings are nil, `Q' and `B'.")
75 (defvar rfc2047-encoding-function-alist
76 '((Q . rfc2047-q-encode-region)
77 (B . rfc2047-b-encode-region)
79 "Alist of RFC2047 encodings to encoding functions.")
81 (defvar rfc2047-q-encoding-alist
82 '(("\\(From\\|Cc\\|To\\|Bcc\||Reply-To\\):" . "-A-Za-z0-9!*+/=_")
83 ("." . "^\000-\007\013\015-\037\200-\377=_?"))
84 "Alist of header regexps and valid Q characters.")
87 ;;; Functions for encoding RFC2047 messages
90 (defun rfc2047-narrow-to-field ()
91 "Narrow the buffer to the header on the current line."
97 (if (re-search-forward "^[^ \n\t]" nil t)
102 (goto-char (point-min)))
105 (defun rfc2047-encode-message-header ()
106 "Encode the message header according to `rfc2047-header-encoding-alist'.
107 Should be called narrowed to the head of the message."
109 (when (featurep 'mule)
111 (let ((alist rfc2047-header-encoding-alist)
115 (rfc2047-narrow-to-field)
116 (when (rfc2047-encodable-p)
117 ;; We found something that may perhaps be encoded.
118 (while (setq elem (pop alist))
119 (when (or (and (stringp (car elem))
120 (looking-at (car elem)))
127 (rfc2047-encode-region (point-min) (point-max)))
130 (goto-char (point-max))))))))
132 (defun rfc2047-encodable-p ()
133 "Say whether the current (narrowed) buffer contains characters that need encoding."
134 (let ((charsets (mapcar
135 'mm-mule-charset-to-mime-charset
136 (find-charset-region (point-min) (point-max))))
137 (cs (list 'us-ascii rfc2047-default-charset))
140 (unless (memq (pop charsets) cs)
144 (defun rfc2047-dissect-region (b e)
145 "Dissect the region between B and E."
148 (narrow-to-region b e)
149 (goto-char (point-min))
150 (while (re-search-forward "[^ \t\n]+" nil t)
152 (list (match-beginning 0) (match-end 0)
155 (find-charset-region (match-beginning 0)
160 (defun rfc2047-encode-region (b e)
161 "Encode all encodable words in REGION."
162 (let ((words (rfc2047-dissect-region b e))
163 beg end current word)
164 (while (setq word (pop words))
165 (if (equal (nth 2 word) current)
166 (setq beg (nth 0 word))
168 (rfc2047-encode beg end current))
169 (setq current (nth 2 word)
173 (rfc2047-encode beg end current))))
175 (defun rfc2047-encode-string (string)
176 "Encode words in STRING."
179 (rfc2047-encode-region (point-min) (point-max))
182 (defun rfc2047-encode (b e charset)
183 "Encode the word in the region with CHARSET."
185 (mm-mime-charset charset b e))
186 (encoding (or (cdr (assq mime-charset
187 rfc2047-charset-encoding-alist))
190 "=?" (downcase (symbol-name mime-charset)) "?"
191 (downcase (symbol-name encoding)) "?")))
193 (narrow-to-region b e)
194 (mm-encode-coding-region b e mime-charset)
195 (funcall (cdr (assq encoding rfc2047-encoding-function-alist))
196 (point-min) (point-max))
197 (goto-char (point-min))
199 (goto-char (point-max))
201 ;; Encoded words can't be more than 75 chars long, so we have to
202 ;; split the long ones up.
204 (while (> (current-column) 74)
207 (insert "?=\n " start)
210 (defun rfc2047-b-encode-region (b e)
211 "Encode the header contained in REGION with the B encoding."
212 (base64-encode-region b e t))
214 (defun rfc2047-q-encode-region (b e)
215 "Encode the header contained in REGION with the Q encoding."
218 (narrow-to-region (goto-char b) e)
219 (let ((alist rfc2047-q-encoding-alist))
221 (when (looking-at (caar alist))
222 (quoted-printable-encode-region b e nil (cdar alist))
223 (subst-char-in-region (point-min) (point-max) ? ?_))
227 ;;; Functions for decoding RFC2047 messages
230 (defvar rfc2047-encoded-word-regexp
231 "=\\?\\([^][\000-\040()<>@,\;:\\\"/?.=]+\\)\\?\\(B\\|Q\\)\\?\\([!->@-~ ]+\\)\\?=")
234 (defun rfc2047-decode-region (start end)
235 "Decode MIME-encoded words in region between START and END."
237 (let ((case-fold-search t)
241 (narrow-to-region start end)
242 (goto-char (point-min))
243 ;; Remove whitespace between encoded words.
244 (while (re-search-forward
245 (concat "\\(" rfc2047-encoded-word-regexp "\\)"
247 "\\(" rfc2047-encoded-word-regexp "\\)")
249 (delete-region (goto-char (match-end 1)) (match-beginning 6)))
250 ;; Decode the encoded words.
251 (setq b (goto-char (point-min)))
252 (while (re-search-forward rfc2047-encoded-word-regexp nil t)
253 (setq e (match-beginning 0))
254 (insert (rfc2047-parse-and-decode
257 (delete-region (match-beginning 0) (match-end 0)))))
258 (when (mm-multibyte-p)
259 (mm-decode-coding-region b e rfc2047-default-charset))
261 (when (mm-multibyte-p)
262 (mm-decode-coding-region b (point-max) rfc2047-default-charset))))))
265 (defun rfc2047-decode-string (string)
266 "Decode the quoted-printable-encoded STRING and return the results."
267 (let ((m (mm-multibyte-p)))
270 (mm-enable-multibyte))
273 (rfc2047-decode-region (point-min) (point-max)))
276 (defun rfc2047-parse-and-decode (word)
277 "Decode WORD and return it if it is an encoded word.
279 (if (not (string-match rfc2047-encoded-word-regexp word))
284 (match-string 1 word)
285 (upcase (match-string 2 word))
286 (match-string 3 word))
290 (defun rfc2047-decode (charset encoding string)
291 "Decode STRING that uses CHARSET with ENCODING.
292 Valid ENCODINGs are \"B\" and \"Q\".
293 If your Emacs implementation can't decode CHARSET, it returns nil."
294 (let ((cs (mm-charset-to-coding-system charset)))
296 (mm-decode-coding-string
298 ((equal "B" encoding)
299 (if (fboundp 'base64-decode-string)
300 (base64-decode-string string)
301 (base64-decode string)))
302 ((equal "Q" encoding)
303 (quoted-printable-decode-string
304 (mm-replace-chars-in-string string ?_ ? )))
305 (t (error "Invalid encoding: %s" encoding)))
310 ;;; rfc2047.el ends here