74
|
1 ;;; tm-ew-d.el --- RFC 2047 based encoded-word decoder for GNU Emacs
|
4
|
2
|
70
|
3 ;; Copyright (C) 1995,1996 Free Software Foundation, Inc.
|
4
|
4
|
|
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: 1995/10/03
|
|
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)
|
80
|
12 ;; Version: $Revision: 1.2 $
|
74
|
13 ;; Keywords: encoded-word, MIME, multilingual, header, mail, news
|
4
|
14
|
|
15 ;; This file is part of tm (Tools for MIME).
|
|
16
|
|
17 ;; This program is free software; you can redistribute it and/or
|
|
18 ;; modify it under the terms of the GNU General Public License as
|
|
19 ;; published by the Free Software Foundation; either version 2, or (at
|
|
20 ;; your option) any later version.
|
|
21
|
|
22 ;; This program is distributed in the hope that it will be useful, but
|
|
23 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
25 ;; General Public License for more details.
|
|
26
|
|
27 ;; You should have received a copy of the GNU General Public License
|
|
28 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
29 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
30 ;; Boston, MA 02111-1307, USA.
|
|
31
|
|
32 ;;; Code:
|
|
33
|
|
34 (require 'emu)
|
|
35 (require 'std11)
|
|
36 (require 'mel)
|
|
37 (require 'tm-def)
|
80
|
38 (require 'tl-str)
|
4
|
39
|
|
40
|
|
41 ;;; @ version
|
|
42 ;;;
|
|
43
|
|
44 (defconst tm-ew-d/RCS-ID
|
80
|
45 "$Id: tm-ew-d.el,v 1.2 1997/01/11 20:14:11 steve Exp $")
|
4
|
46 (defconst mime/eword-decoder-version (get-version-string tm-ew-d/RCS-ID))
|
|
47
|
|
48
|
|
49 ;;; @ MIME encoded-word definition
|
|
50 ;;;
|
|
51
|
|
52 (defconst mime/encoded-text-regexp "[!->@-~]+")
|
|
53 (defconst mime/encoded-word-regexp (concat (regexp-quote "=?")
|
|
54 "\\("
|
|
55 mime/charset-regexp
|
|
56 "\\)"
|
|
57 (regexp-quote "?")
|
|
58 "\\(B\\|Q\\)"
|
|
59 (regexp-quote "?")
|
|
60 "\\("
|
|
61 mime/encoded-text-regexp
|
|
62 "\\)"
|
|
63 (regexp-quote "?=")))
|
|
64
|
|
65
|
|
66 ;;; @ for string
|
|
67 ;;;
|
|
68
|
|
69 (defun mime-eword/decode-string (string &optional must-unfold)
|
|
70 "Decode MIME encoded-words in STRING.
|
|
71
|
|
72 STRING is unfolded before decoding.
|
|
73
|
|
74 If an encoded-word is broken or your emacs implementation can not
|
|
75 decode the charset included in it, it is not decoded.
|
|
76
|
|
77 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
|
|
78 if there are in decoded encoded-words (generated by bad manner MUA
|
|
79 such as a version of Net$cape). [tm-ew-d.el]"
|
|
80 (setq string (std11-unfold-string string))
|
|
81 (let ((dest "")(ew nil)
|
|
82 beg end)
|
|
83 (while (and (string-match mime/encoded-word-regexp string)
|
|
84 (setq beg (match-beginning 0)
|
|
85 end (match-end 0))
|
|
86 )
|
|
87 (if (> beg 0)
|
|
88 (if (not
|
|
89 (and (eq ew t)
|
|
90 (string-match "^[ \t]+$" (substring string 0 beg))
|
|
91 ))
|
|
92 (setq dest (concat dest (substring string 0 beg)))
|
|
93 )
|
|
94 )
|
|
95 (setq dest
|
|
96 (concat dest
|
|
97 (mime/decode-encoded-word
|
|
98 (substring string beg end) must-unfold)
|
|
99 ))
|
|
100 (setq string (substring string end))
|
|
101 (setq ew t)
|
|
102 )
|
|
103 (concat dest string)
|
|
104 ))
|
|
105
|
|
106
|
|
107 ;;; @ for region
|
|
108 ;;;
|
|
109
|
|
110 (defun mime-eword/decode-region (start end &optional unfolding must-unfold)
|
|
111 "Decode MIME encoded-words in region between START and END.
|
|
112
|
|
113 If UNFOLDING is not nil, it unfolds before decoding.
|
|
114
|
|
115 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
|
|
116 if there are in decoded encoded-words (generated by bad manner MUA
|
|
117 such as a version of Net$cape). [tm-ew-d.el]"
|
|
118 (interactive "*r")
|
|
119 (save-excursion
|
|
120 (save-restriction
|
|
121 (narrow-to-region start end)
|
|
122 (if unfolding
|
|
123 (mime/unfolding)
|
|
124 )
|
|
125 (goto-char (point-min))
|
74
|
126 (while (re-search-forward "\\?=\\(\n*\\s +\\)+=\\?" nil t)
|
70
|
127 (replace-match "?==?")
|
4
|
128 )
|
70
|
129 (goto-char (point-min))
|
4
|
130 (let (charset encoding text)
|
|
131 (while (re-search-forward mime/encoded-word-regexp nil t)
|
|
132 (insert (mime/decode-encoded-word
|
|
133 (prog1
|
|
134 (buffer-substring (match-beginning 0) (match-end 0))
|
|
135 (delete-region (match-beginning 0) (match-end 0))
|
|
136 ) must-unfold))
|
|
137 ))
|
|
138 )))
|
|
139
|
|
140
|
|
141 ;;; @ for message header
|
|
142 ;;;
|
|
143
|
|
144 (defun mime/decode-message-header ()
|
|
145 "Decode MIME encoded-words in message header. [tm-ew-d.el]"
|
|
146 (interactive "*")
|
|
147 (save-excursion
|
|
148 (save-restriction
|
|
149 (narrow-to-region (goto-char (point-min))
|
|
150 (progn (re-search-forward "^$" nil t) (point)))
|
|
151 (mime-eword/decode-region (point-min) (point-max) t)
|
|
152 )))
|
|
153
|
|
154 (defun mime/unfolding ()
|
|
155 (goto-char (point-min))
|
|
156 (let (field beg end)
|
|
157 (while (re-search-forward std11-field-head-regexp nil t)
|
|
158 (setq beg (match-beginning 0)
|
74
|
159 end (std11-field-end))
|
4
|
160 (setq field (buffer-substring beg end))
|
|
161 (if (string-match mime/encoded-word-regexp field)
|
74
|
162 (save-restriction
|
|
163 (narrow-to-region (goto-char beg) end)
|
|
164 (while (re-search-forward "\n\\([ \t]\\)" nil t)
|
|
165 (replace-match
|
|
166 (match-string 1))
|
|
167 )
|
4
|
168 (goto-char (point-max))
|
|
169 ))
|
|
170 )))
|
|
171
|
|
172
|
|
173 ;;; @ encoded-word decoder
|
|
174 ;;;
|
|
175
|
|
176 (defun mime/decode-encoded-word (word &optional must-unfold)
|
|
177 "Decode WORD if it is an encoded-word.
|
|
178
|
|
179 If your emacs implementation can not decode the charset of WORD, it
|
|
180 returns WORD. Similarly the encoded-word is broken, it returns WORD.
|
|
181
|
|
182 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
|
|
183 if there are in decoded encoded-word (generated by bad manner MUA such
|
|
184 as a version of Net$cape). [tm-ew-d.el]"
|
|
185 (or (if (string-match mime/encoded-word-regexp word)
|
|
186 (let ((charset
|
|
187 (substring word (match-beginning 1) (match-end 1))
|
|
188 )
|
|
189 (encoding
|
|
190 (upcase
|
|
191 (substring word (match-beginning 2) (match-end 2))
|
|
192 ))
|
|
193 (text
|
|
194 (substring word (match-beginning 3) (match-end 3))
|
|
195 ))
|
74
|
196 (condition-case err
|
|
197 (mime/decode-encoded-text charset encoding text must-unfold)
|
|
198 (error nil))
|
70
|
199 ))
|
4
|
200 word))
|
|
201
|
|
202
|
|
203 ;;; @ encoded-text decoder
|
|
204 ;;;
|
|
205
|
|
206 (defun mime/decode-encoded-text (charset encoding string &optional must-unfold)
|
|
207 "Decode STRING as an encoded-text.
|
|
208
|
|
209 If your emacs implementation can not decode CHARSET, it returns nil.
|
|
210
|
|
211 If ENCODING is not \"B\" or \"Q\", it occurs error.
|
|
212 So you should write error-handling code if you don't want break by errors.
|
|
213
|
|
214 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
|
|
215 if there are in decoded encoded-text (generated by bad manner MUA such
|
|
216 as a version of Net$cape). [tm-ew-d.el]"
|
|
217 (let ((cs (mime-charset-to-coding-system charset)))
|
|
218 (if cs
|
|
219 (let ((dest
|
74
|
220 (cond ((and (string-equal "B" encoding)
|
|
221 (string-match mime/B-encoded-text-regexp string))
|
|
222 (base64-decode-string string))
|
|
223 ((and (string-equal "Q" encoding)
|
|
224 (string-match mime/Q-encoded-text-regexp string))
|
|
225 (q-encoding-decode-string string))
|
|
226 (t (message "Invalid encoded-word %s" encoding)
|
70
|
227 nil))))
|
4
|
228 (if dest
|
|
229 (progn
|
|
230 (setq dest (decode-coding-string dest cs))
|
|
231 (if must-unfold
|
|
232 (mapconcat (function
|
|
233 (lambda (chr)
|
70
|
234 (if (eq chr ?\n)
|
|
235 ""
|
|
236 (char-to-string chr)
|
|
237 )
|
4
|
238 ))
|
|
239 (std11-unfold-string dest)
|
|
240 "")
|
|
241 dest)
|
|
242 ))))))
|
|
243
|
|
244
|
|
245 ;;; @ end
|
|
246 ;;;
|
|
247
|
|
248 (provide 'tm-ew-d)
|
|
249
|
|
250 ;;; tm-ew-d.el ends here
|