1 ;;; eword-decode.el --- RFC 2047 based encoded-word decoder for GNU Emacs
3 ;; Copyright (C) 1995,1996,1997 Free Software Foundation, Inc.
5 ;; Author: ENAMI Tsugutomo <enami@sys.ptg.sony.co.jp>
6 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Maintainer: MORIOKA Tomohiko <morioka@jaist.ac.jp>
9 ;; Original: 1992/07/20 ENAMI Tsugutomo's `mime.el'.
10 ;; Renamed: 1993/06/03 to tiny-mime.el
11 ;; Renamed: 1995/10/03 from tiny-mime.el (split off encoder)
12 ;; Renamed: 1997/02/22 from tm-ew-d.el
13 ;; Version: $Revision: 0.14 $
14 ;; Keywords: encoded-word, MIME, multilingual, header, mail, news
16 ;; This file is part of SEMI (SEMI is Emacs MIME Interfaces).
18 ;; This program is free software; you can redistribute it and/or
19 ;; modify it under the terms of the GNU General Public License as
20 ;; published by the Free Software Foundation; either version 2, or (at
21 ;; your option) any later version.
23 ;; This program is distributed in the hope that it will be useful, but
24 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
25 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 ;; General Public License for more details.
28 ;; You should have received a copy of the GNU General Public License
29 ;; along with GNU Emacs; see the file COPYING. If not, write to the
30 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
31 ;; Boston, MA 02111-1307, USA.
43 (defconst eword-decode-RCS-ID
44 "$Id: eword-decode.el,v 0.14 1997-02-27 08:56:45 tmorioka Exp $")
45 (defconst eword-decode-version (get-version-string eword-decode-RCS-ID))
48 ;;; @ MIME encoded-word definition
51 (defconst eword-encoded-text-regexp "[!->@-~]+")
52 (defconst eword-encoded-word-regexp
53 (concat (regexp-quote "=?")
61 eword-encoded-text-regexp
69 (defconst base64-token-regexp "[A-Za-z0-9+/]")
70 (defconst base64-token-padding-regexp "[A-Za-z0-9+/=]")
72 (defconst eword-B-encoded-text-regexp
81 base64-token-padding-regexp
82 base64-token-padding-regexp
85 ;; (defconst eword-B-encoding-and-encoded-text-regexp
86 ;; (concat "\\(B\\)\\?" eword-B-encoded-text-regexp))
89 ;;; @@ Quoted-Printable
92 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
93 (defconst quoted-printable-octet-regexp
94 (concat "=[" quoted-printable-hex-chars
95 "][" quoted-printable-hex-chars "]"))
97 (defconst eword-Q-encoded-text-regexp
98 (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
99 ;; (defconst eword-Q-encoding-and-encoded-text-regexp
100 ;; (concat "\\(Q\\)\\?" eword-Q-encoded-text-regexp))
106 (defun eword-decode-string (string &optional must-unfold)
107 "Decode MIME encoded-words in STRING.
109 STRING is unfolded before decoding.
111 If an encoded-word is broken or your emacs implementation can not
112 decode the charset included in it, it is not decoded.
114 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
115 if there are in decoded encoded-words (generated by bad manner MUA
116 such as a version of Net$cape)."
117 (setq string (std11-unfold-string string))
118 (let ((dest "")(ew nil)
120 (while (and (string-match eword-encoded-word-regexp string)
121 (setq beg (match-beginning 0)
127 (string-match "^[ \t]+$" (substring string 0 beg))
129 (setq dest (concat dest (substring string 0 beg)))
134 (eword-decode-encoded-word
135 (substring string beg end) must-unfold)
137 (setq string (substring string end))
147 (defun eword-decode-region (start end &optional unfolding must-unfold)
148 "Decode MIME encoded-words in region between START and END.
150 If UNFOLDING is not nil, it unfolds before decoding.
152 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
153 if there are in decoded encoded-words (generated by bad manner MUA
154 such as a version of Net$cape)."
158 (narrow-to-region start end)
160 (eword-decode-unfold)
162 (goto-char (point-min))
163 (while (re-search-forward (concat "\\(" eword-encoded-word-regexp "\\)"
165 "\\(" eword-encoded-word-regexp "\\)")
167 (replace-match "\\1\\6")
168 (goto-char (point-min))
170 (while (re-search-forward eword-encoded-word-regexp nil t)
171 (insert (eword-decode-encoded-word
173 (buffer-substring (match-beginning 0) (match-end 0))
174 (delete-region (match-beginning 0) (match-end 0))
180 ;;; @ for message header
183 (defun eword-decode-header (&optional separator)
184 "Decode MIME encoded-words in header fields.
185 If SEPARATOR is not nil, it is used as header separator."
189 (std11-narrow-to-header separator)
190 (eword-decode-region (point-min) (point-max) t)
193 (defun eword-decode-unfold ()
194 (goto-char (point-min))
196 (while (re-search-forward std11-field-head-regexp nil t)
197 (setq beg (match-beginning 0)
198 end (std11-field-end))
199 (setq field (buffer-substring beg end))
200 (if (string-match eword-encoded-word-regexp field)
202 (narrow-to-region (goto-char beg) end)
203 (while (re-search-forward "\n\\([ \t]\\)" nil t)
204 (replace-match (match-string 1))
206 (goto-char (point-max))
211 ;;; @ encoded-word decoder
214 (defvar eword-warning-face nil "Face used for invalid encoded-word.")
216 (defun eword-decode-encoded-word (word &optional must-unfold)
217 "Decode WORD if it is an encoded-word.
219 If your emacs implementation can not decode the charset of WORD, it
220 returns WORD. Similarly the encoded-word is broken, it returns WORD.
222 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
223 if there are in decoded encoded-word (generated by bad manner MUA such
224 as a version of Net$cape)."
225 (or (if (string-match eword-encoded-word-regexp word)
227 (substring word (match-beginning 1) (match-end 1))
231 (substring word (match-beginning 2) (match-end 2))
234 (substring word (match-beginning 3) (match-end 3))
237 (eword-decode-encoded-text charset encoding text must-unfold)
240 (add-text-properties 0 (length word)
241 (and eword-warning-face
242 (list 'face eword-warning-face))
249 ;;; @ encoded-text decoder
252 (defun eword-decode-encoded-text (charset encoding string
253 &optional must-unfold)
254 "Decode STRING as an encoded-text.
256 If your emacs implementation can not decode CHARSET, it returns nil.
258 If ENCODING is not \"B\" or \"Q\", it occurs error.
259 So you should write error-handling code if you don't want break by errors.
261 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
262 if there are in decoded encoded-text (generated by bad manner MUA such
263 as a version of Net$cape)."
264 (let ((cs (mime-charset-to-coding-system charset)))
268 ((string-equal "B" encoding)
269 (if (and (string-match eword-B-encoded-text-regexp string)
270 (string-equal string (match-string 0 string)))
271 (base64-decode-string string)
272 (error "Invalid encoded-text %s" string)))
273 ((string-equal "Q" encoding)
274 (if (and (string-match eword-Q-encoded-text-regexp string)
275 (string-equal string (match-string 0 string)))
276 (q-encoding-decode-string string)
277 (error "Invalid encoded-text %s" string)))
279 (error "Invalid encoding %s" encoding)
284 (setq dest (decode-coding-string dest cs))
291 (t (char-to-string chr)))
293 (std11-unfold-string dest)
302 (provide 'eword-decode)
304 ;;; eword-decode.el ends here