1 ;;; tm-ew-d.el --- RFC 1522 based MIME encoded-word decoder for GNU Emacs
3 ;; Copyright (C) 1995,1996 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>
8 ;; Created: 1993/6/3 (1995/10/3 obsolete tiny-mime.el)
9 ;; Version: $Revision: 7.25 $
10 ;; Keywords: mail, news, MIME, RFC 1522, multilingual, encoded-word
12 ;; This file is part of tm (Tools for MIME).
14 ;; This program is free software; you can redistribute it and/or
15 ;; modify it under the terms of the GNU General Public License as
16 ;; published by the Free Software Foundation; either version 2, or (at
17 ;; your option) any later version.
19 ;; This program is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ;; General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this program; see the file COPYING. If not, write to
26 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
40 (defconst tm-ew-d/RCS-ID
41 "$Id: tm-ew-d.el,v 7.25 1996/09/13 02:39:01 morioka Exp $")
42 (defconst mime/eword-decoder-version (get-version-string tm-ew-d/RCS-ID))
45 ;;; @ MIME encoded-word definition
48 (defconst mime/encoded-text-regexp "[!->@-~]+")
49 (defconst mime/encoded-word-regexp (concat (regexp-quote "=?")
57 mime/encoded-text-regexp
65 (defun mime-eword/decode-string (str &optional unfolding)
66 (setq str (std11-unfold-string str))
67 (let ((dest "")(ew nil)
69 (while (and (string-match mime/encoded-word-regexp str)
70 (setq beg (match-beginning 0)
76 (string-match "^[ \t]+$" (substring str 0 beg))
78 (setq dest (concat dest (substring str 0 beg)))
83 (mime/decode-encoded-word
84 (substring str beg end) unfolding)
86 (setq str (substring str end))
96 (defun mime-eword/decode-region (beg end &optional unfolding must-unfold)
97 "Decode MIME encoded-words in region between BEG and END.
98 If UNFOLDING is not nil, it unfolds before decoding.
99 If MUST-UNFOLD is not nil, it unfolds encoded results. [tm-ew-d.el]"
103 (narrow-to-region beg end)
107 (goto-char (point-min))
108 (while (re-search-forward
109 (concat (regexp-quote "?=") "\\s +" (regexp-quote "=?"))
111 (replace-match "?==?")
113 (goto-char (point-min))
114 (let (charset encoding text)
115 (while (re-search-forward mime/encoded-word-regexp nil t)
116 (insert (mime/decode-encoded-word
118 (buffer-substring (match-beginning 0) (match-end 0))
119 (delete-region (match-beginning 0) (match-end 0))
125 ;;; @ for message header
128 (defun mime/decode-message-header ()
129 "Decode MIME encoded-words in message header. [tm-ew-d.el]"
133 (narrow-to-region (goto-char (point-min))
134 (progn (re-search-forward "^$" nil t) (point)))
135 (mime-eword/decode-region (point-min) (point-max) t)
138 (defun mime/unfolding ()
139 (goto-char (point-min))
141 (while (re-search-forward std11-field-head-regexp nil t)
142 (setq beg (match-beginning 0)
143 end (std11-field-end))
144 (setq field (buffer-substring beg end))
145 (if (string-match mime/encoded-word-regexp field)
147 (narrow-to-region (goto-char beg) end)
148 (while (re-search-forward "\n[ \t]+" nil t)
151 (goto-char (point-max))
156 ;;; @ encoded-word decoder
159 (defun mime/decode-encoded-word (word &optional unfolding)
160 (or (if (string-match mime/encoded-word-regexp word)
162 (substring word (match-beginning 1) (match-end 1))
166 (substring word (match-beginning 2) (match-end 2))
169 (substring word (match-beginning 3) (match-end 3))
171 (mime/decode-encoded-text charset encoding text unfolding)
176 ;;; @ encoded-text decoder
179 (defun mime/decode-encoded-text (charset encoding str &optional unfolding)
180 (let ((cs (mime-charset-to-coding-system charset)))
183 (cond ((string-equal "B" encoding)
184 (base64-decode-string str))
185 ((string-equal "Q" encoding)
186 (q-encoding-decode-string str))
187 (t (message "unknown encoding %s" encoding)
191 (setq dest (decode-coding-string dest cs))
200 (std11-unfold-string dest)
211 ;;; tm-ew-d.el ends here