1 ;;; rfc2047.el --- Functions for encoding and decoding rfc2047 messages
2 ;; Copyright (C) 1998,99 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 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)))
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 (goto-char (point-min))
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))
128 (rfc2047-fold-region (point-min) (point-max)))
131 (goto-char (point-max)))))
132 (when mail-parse-charset
133 (encode-coding-region (point-min) (point-max)
134 mail-parse-charset)))))
136 (defun rfc2047-encodable-p ()
137 "Say whether the current (narrowed) buffer contains characters that need encoding."
141 (mm-find-charset-region (point-min) (point-max))))
142 (cs (list 'us-ascii mail-parse-charset))
145 (unless (memq (pop charsets) cs)
149 (defun rfc2047-dissect-region (b e)
150 "Dissect the region between B and E into words."
153 (narrow-to-region b e)
154 (goto-char (point-min))
155 (while (re-search-forward
156 (concat "[^" ietf-drums-tspecials " \t\n]+") nil t)
158 (list (match-beginning 0) (match-end 0)
159 (car (delq 'ascii (mm-find-charset-region
160 (match-beginning 0) (match-end 0)))))
164 (defun rfc2047-encode-region (b e)
165 "Encode all encodable words in REGION."
166 (let ((words (rfc2047-dissect-region b e))
167 beg end current word)
168 (while (setq word (pop words))
169 (if (equal (nth 2 word) current)
170 (setq beg (nth 0 word))
172 (rfc2047-encode beg end current))
173 (setq current (nth 2 word)
177 (rfc2047-encode beg end current))))
179 (defun rfc2047-encode-string (string)
180 "Encode words in STRING."
183 (rfc2047-encode-region (point-min) (point-max))
186 (defun rfc2047-encode (b e charset)
187 "Encode the word in the region with CHARSET."
188 (let* ((mime-charset (mm-mime-charset charset))
189 (encoding (or (cdr (assq mime-charset
190 rfc2047-charset-encoding-alist))
193 "=?" (downcase (symbol-name mime-charset)) "?"
194 (downcase (symbol-name encoding)) "?"))
197 (narrow-to-region b e)
198 (mm-encode-coding-region b e mime-charset)
199 (funcall (cdr (assq encoding rfc2047-encoding-function-alist))
200 (point-min) (point-max))
201 (goto-char (point-min))
211 (defun rfc2047-fold-region (b e)
212 "Fold the long lines in the region."
214 (narrow-to-region b e)
215 (goto-char (point-min))
219 ((memq (char-after) '(? ?\t))
220 (setq break (point)))
223 (setq break (point)))
224 ((and (looking-at "\\?=")
225 (> (- (point) (save-excursion (beginning-of-line) (point))) 76))
230 (forward-char 1))))))
232 (defun rfc2047-b-encode-region (b e)
233 "Encode the header contained in REGION with the B encoding."
234 (base64-encode-region b e t)
235 (goto-char (point-min))
237 (goto-char (min (point-max) (+ 64 (point))))
241 (defun rfc2047-q-encode-region (b e)
242 "Encode the header contained in REGION with the Q encoding."
245 (narrow-to-region (goto-char b) e)
246 (let ((alist rfc2047-q-encoding-alist))
248 (when (looking-at (caar alist))
249 (quoted-printable-encode-region b e nil (cdar alist))
250 (subst-char-in-region (point-min) (point-max) ? ?_)
253 (goto-char (point-min))
255 (goto-char (min (point-max) (+ 64 (point))))
256 (search-backward "=" (- (point) 2) t)
261 ;;; Functions for decoding RFC2047 messages
264 (defvar rfc2047-encoded-word-regexp
265 "=\\?\\([^][\000-\040()<>@,\;:\\\"/?.=]+\\)\\?\\(B\\|Q\\)\\?\\([!->@-~ +]+\\)\\?=")
267 (defun rfc2047-decode-region (start end)
268 "Decode MIME-encoded words in region between START and END."
270 (let ((case-fold-search t)
274 (narrow-to-region start end)
275 (goto-char (point-min))
276 ;; Remove whitespace between encoded words.
277 (while (re-search-forward
278 (concat "\\(" rfc2047-encoded-word-regexp "\\)"
280 "\\(" rfc2047-encoded-word-regexp "\\)")
282 (delete-region (goto-char (match-end 1)) (match-beginning 6)))
283 ;; Decode the encoded words.
284 (setq b (goto-char (point-min)))
285 (while (re-search-forward rfc2047-encoded-word-regexp nil t)
286 (setq e (match-beginning 0))
287 (insert (rfc2047-parse-and-decode
290 (delete-region (match-beginning 0) (match-end 0)))))
291 (when (and (mm-multibyte-p)
293 (mm-decode-coding-region b e mail-parse-charset))
295 (when (and (mm-multibyte-p)
297 (not (eq mail-parse-charset 'us-ascii)))
298 (mm-decode-coding-region b (point-max) mail-parse-charset))))))
300 (defun rfc2047-decode-string (string)
301 "Decode the quoted-printable-encoded STRING and return the results."
302 (let ((m (mm-multibyte-p)))
305 (mm-enable-multibyte))
308 (rfc2047-decode-region (point-min) (point-max)))
311 (defun rfc2047-parse-and-decode (word)
312 "Decode WORD and return it if it is an encoded word.
314 (if (not (string-match rfc2047-encoded-word-regexp word))
319 (match-string 1 word)
320 (upcase (match-string 2 word))
321 (match-string 3 word))
325 (defun rfc2047-decode (charset encoding string)
326 "Decode STRING that uses CHARSET with ENCODING.
327 Valid ENCODINGs are \"B\" and \"Q\".
328 If your Emacs implementation can't decode CHARSET, it returns nil."
329 (let ((cs (mm-charset-to-coding-system charset)))
331 (when (and (eq cs 'ascii)
333 (setq cs mail-parse-charset))
334 (mm-decode-coding-string
336 ((equal "B" encoding)
337 (base64-decode-string string))
338 ((equal "Q" encoding)
339 (quoted-printable-decode-string
340 (mm-replace-chars-in-string string ?_ ? )))
341 (t (error "Invalid encoding: %s" encoding)))
346 ;;; rfc2047.el ends here