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: 1.3 $
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.
35 (require 'std11-parse)
39 (defgroup eword-decode nil
40 "Encoded-word decoding"
47 (defconst eword-decode-RCS-ID
48 "$Id: eword-decode.el,v 1.3 1998-02-16 20:40:19 morioka Exp $")
49 (defconst eword-decode-version (get-version-string eword-decode-RCS-ID))
52 ;;; @ MIME encoded-word definition
55 (defconst eword-encoded-text-regexp "[!->@-~]+")
56 (defconst eword-encoded-word-regexp
57 (concat (regexp-quote "=?")
65 eword-encoded-text-regexp
73 (defconst base64-token-regexp "[A-Za-z0-9+/]")
74 (defconst base64-token-padding-regexp "[A-Za-z0-9+/=]")
76 (defconst eword-B-encoded-text-regexp
85 base64-token-padding-regexp
86 base64-token-padding-regexp
89 ;; (defconst eword-B-encoding-and-encoded-text-regexp
90 ;; (concat "\\(B\\)\\?" eword-B-encoded-text-regexp))
93 ;;; @@ Quoted-Printable
96 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
97 (defconst quoted-printable-octet-regexp
98 (concat "=[" quoted-printable-hex-chars
99 "][" quoted-printable-hex-chars "]"))
101 (defconst eword-Q-encoded-text-regexp
102 (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
103 ;; (defconst eword-Q-encoding-and-encoded-text-regexp
104 ;; (concat "\\(Q\\)\\?" eword-Q-encoded-text-regexp))
110 (defun eword-decode-string (string &optional must-unfold)
111 "Decode MIME encoded-words in STRING.
113 STRING is unfolded before decoding.
115 If an encoded-word is broken or your emacs implementation can not
116 decode the charset included in it, it is not decoded.
118 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
119 if there are in decoded encoded-words (generated by bad manner MUA
120 such as a version of Net$cape)."
121 (setq string (std11-unfold-string string))
122 (let ((dest "")(ew nil)
124 (while (and (string-match eword-encoded-word-regexp string)
125 (setq beg (match-beginning 0)
131 (string-match "^[ \t]+$" (substring string 0 beg))
133 (setq dest (concat dest (substring string 0 beg)))
138 (eword-decode-encoded-word
139 (substring string beg end) must-unfold)
141 (setq string (substring string end))
151 (defun eword-decode-region (start end &optional unfolding must-unfold)
152 "Decode MIME encoded-words in region between START and END.
154 If UNFOLDING is not nil, it unfolds before decoding.
156 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
157 if there are in decoded encoded-words (generated by bad manner MUA
158 such as a version of Net$cape)."
162 (narrow-to-region start end)
164 (eword-decode-unfold)
166 (goto-char (point-min))
167 (while (re-search-forward (concat "\\(" eword-encoded-word-regexp "\\)"
169 "\\(" eword-encoded-word-regexp "\\)")
171 (replace-match "\\1\\6")
172 (goto-char (point-min))
174 (while (re-search-forward eword-encoded-word-regexp nil t)
175 (insert (eword-decode-encoded-word
177 (buffer-substring (match-beginning 0) (match-end 0))
178 (delete-region (match-beginning 0) (match-end 0))
184 ;;; @ for message header
187 (defcustom eword-decode-ignored-field-list
188 '(newsgroups message-id path)
189 "*List of field-names to be ignored when decoding.
190 Each field name must be symbol."
192 :type '(repeat symbol))
194 (defcustom eword-decode-structured-field-list
195 '(reply-to resent-reply-to from resent-from sender resent-sender
196 to resent-to cc resent-cc bcc resent-bcc dcc
197 mime-version content-type content-transfer-encoding
199 "*List of field-names to decode as structured field.
200 Each field name must be symbol."
202 :type '(repeat symbol))
204 (defun eword-decode-header (&optional code-conversion separator)
205 "Decode MIME encoded-words in header fields.
206 If CODE-CONVERSION is nil, it decodes only encoded-words. If it is
207 mime-charset, it decodes non-ASCII bit patterns as the mime-charset.
208 Otherwise it decodes non-ASCII bit patterns as the
209 default-mime-charset.
210 If SEPARATOR is not nil, it is used as header separator."
214 (std11-narrow-to-header separator)
215 (let ((default-charset
217 (if (mime-charset-to-coding-system code-conversion)
219 default-mime-charset))))
221 (let (beg end field-name)
222 (goto-char (point-min))
223 (while (re-search-forward std11-field-head-regexp nil t)
224 (setq beg (match-beginning 0)
227 (downcase (buffer-substring beg (1- p))))
228 end (std11-field-end))
229 (cond ((memq field-name eword-decode-ignored-field-list)
232 ((memq field-name eword-decode-structured-field-list)
233 ;; Decode as structured field
234 (let ((body (buffer-substring p end))
235 (default-mime-charset default-charset))
236 (delete-region p end)
237 (insert (eword-decode-structured-field-body body))
240 ;; Decode as unstructured field
242 (narrow-to-region p end)
243 (decode-mime-charset-region p end default-charset)
244 (eword-decode-region p (point-max))
246 (eword-decode-region (point-min) (point-max) t)
249 (defun eword-decode-unfold ()
250 (goto-char (point-min))
252 (while (re-search-forward std11-field-head-regexp nil t)
253 (setq beg (match-beginning 0)
254 end (std11-field-end))
255 (setq field (buffer-substring beg end))
256 (if (string-match eword-encoded-word-regexp field)
258 (narrow-to-region (goto-char beg) end)
259 (while (re-search-forward "\n\\([ \t]\\)" nil t)
260 (replace-match (match-string 1))
262 (goto-char (point-max))
267 ;;; @ encoded-word decoder
270 (defvar eword-warning-face nil "Face used for invalid encoded-word.")
272 (defun eword-decode-encoded-word (word &optional must-unfold)
273 "Decode WORD if it is an encoded-word.
275 If your emacs implementation can not decode the charset of WORD, it
276 returns WORD. Similarly the encoded-word is broken, it returns WORD.
278 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
279 if there are in decoded encoded-word (generated by bad manner MUA such
280 as a version of Net$cape)."
281 (or (if (string-match eword-encoded-word-regexp word)
283 (substring word (match-beginning 1) (match-end 1))
287 (substring word (match-beginning 2) (match-end 2))
290 (substring word (match-beginning 3) (match-end 3))
293 (eword-decode-encoded-text charset encoding text must-unfold)
296 (add-text-properties 0 (length word)
297 (and eword-warning-face
298 (list 'face eword-warning-face))
305 ;;; @ encoded-text decoder
308 (defun eword-decode-encoded-text (charset encoding string
309 &optional must-unfold)
310 "Decode STRING as an encoded-text.
312 If your emacs implementation can not decode CHARSET, it returns nil.
314 If ENCODING is not \"B\" or \"Q\", it occurs error.
315 So you should write error-handling code if you don't want break by errors.
317 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
318 if there are in decoded encoded-text (generated by bad manner MUA such
319 as a version of Net$cape)."
320 (let ((cs (mime-charset-to-coding-system charset)))
324 ((string-equal "B" encoding)
325 (if (and (string-match eword-B-encoded-text-regexp string)
326 (string-equal string (match-string 0 string)))
327 (base64-decode-string string)
328 (error "Invalid encoded-text %s" string)))
329 ((string-equal "Q" encoding)
330 (if (and (string-match eword-Q-encoded-text-regexp string)
331 (string-equal string (match-string 0 string)))
332 (q-encoding-decode-string string)
333 (error "Invalid encoded-text %s" string)))
335 (error "Invalid encoding %s" encoding)
340 (setq dest (decode-coding-string dest cs))
347 (t (char-to-string chr)))
349 (std11-unfold-string dest)
355 ;;; @ lexical analyze
358 (defvar eword-lexical-analyze-cache nil)
359 (defvar eword-lexical-analyze-cache-max 299
360 "*Max position of eword-lexical-analyze-cache.
361 It is max size of eword-lexical-analyze-cache - 1.")
363 (defun eword-analyze-quoted-string (string)
364 (let ((p (std11-check-enclosure string ?\" ?\")))
366 (cons (cons 'quoted-string
367 (decode-mime-charset-string
368 (std11-strip-quoted-pair (substring string 1 (1- p)))
369 default-mime-charset))
370 (substring string p))
373 (defun eword-analyze-comment (string &optional must-unfold)
374 (let ((p (std11-check-enclosure string ?\( ?\) t)))
378 (decode-mime-charset-string
379 (std11-strip-quoted-pair (substring string 1 (1- p)))
380 default-mime-charset)
382 (substring string p))
385 (defun eword-analyze-encoded-word (string &optional must-unfold)
386 (if (eq (string-match eword-encoded-word-regexp string) 0)
387 (let ((end (match-end 0))
388 (dest (eword-decode-encoded-word (match-string 0 string)
391 (setq string (substring string end))
392 (while (eq (string-match `,(concat "[ \t\n]*\\("
393 eword-encoded-word-regexp
397 (setq end (match-end 0))
400 (eword-decode-encoded-word (match-string 1 string)
402 string (substring string end))
404 (cons (cons 'atom dest) string)
407 (defun eword-analyze-atom (string)
408 (if (string-match std11-atom-regexp string)
409 (let ((end (match-end 0)))
410 (cons (cons 'atom (decode-mime-charset-string
411 (substring string 0 end)
412 default-mime-charset))
413 (substring string end)
416 (defun eword-lexical-analyze-internal (string must-unfold)
418 (while (not (string-equal string ""))
420 (or (eword-analyze-quoted-string string)
421 (std11-analyze-domain-literal string)
422 (eword-analyze-comment string must-unfold)
423 (std11-analyze-spaces string)
424 (std11-analyze-special string)
425 (eword-analyze-encoded-word string must-unfold)
426 (eword-analyze-atom string)
429 (setq dest (cons (car ret) dest))
430 (setq string (cdr ret))
435 (defun eword-lexical-analyze (string &optional must-unfold)
436 "Return lexical analyzed list corresponding STRING.
437 It is like std11-lexical-analyze, but it decodes non us-ascii
438 characters encoded as encoded-words or invalid \"raw\" format.
439 \"Raw\" non us-ascii characters are regarded as variable
440 `default-mime-charset'."
441 (let ((key (copy-sequence string))
443 (set-text-properties 0 (length key) nil key)
444 (if (setq ret (assoc key eword-lexical-analyze-cache))
446 (setq ret (eword-lexical-analyze-internal key must-unfold))
447 (setq eword-lexical-analyze-cache
449 (last eword-lexical-analyze-cache
450 eword-lexical-analyze-cache-max)))
453 (defun eword-decode-structured-field-body (string &optional must-unfold)
454 "Decode non us-ascii characters in STRING as structured field body.
455 STRING is unfolded before decoding.
457 It decodes non us-ascii characters in FULL-NAME encoded as
458 encoded-words or invalid \"raw\" string. \"Raw\" non us-ascii
459 characters are regarded as variable `default-mime-charset'.
461 If an encoded-word is broken or your emacs implementation can not
462 decode the charset included in it, it is not decoded.
464 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
465 if there are in decoded encoded-words (generated by bad manner MUA
466 such as a version of Net$cape)."
469 (let ((type (car token))
471 (cond ((eq type 'quoted-string)
472 (std11-wrap-as-quoted-string value)
476 (std11-wrap-as-quoted-pairs value '(?( ?)))
481 (eword-lexical-analyze string must-unfold)
484 (defun eword-decode-unstructured-field-body (string &optional must-unfold)
485 "Decode non us-ascii characters in STRING as unstructured field body.
486 STRING is unfolded before decoding.
488 It decodes non us-ascii characters in FULL-NAME encoded as
489 encoded-words or invalid \"raw\" string. \"Raw\" non us-ascii
490 characters are regarded as variable `default-mime-charset'.
492 If an encoded-word is broken or your emacs implementation can not
493 decode the charset included in it, it is not decoded.
495 If MUST-UNFOLD is non-nil, it unfolds and eliminates line-breaks even
496 if there are in decoded encoded-words (generated by bad manner MUA
497 such as a version of Net$cape)."
499 (decode-mime-charset-string string default-mime-charset)
502 (defun eword-extract-address-components (string)
503 "Extract full name and canonical address from STRING.
504 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS).
505 If no name can be extracted, FULL-NAME will be nil.
506 It decodes non us-ascii characters in FULL-NAME encoded as
507 encoded-words or invalid \"raw\" string. \"Raw\" non us-ascii
508 characters are regarded as variable `default-mime-charset'."
509 (let* ((structure (car (std11-parse-address
510 (eword-lexical-analyze
511 (std11-unfold-string string) 'must-unfold))))
512 (phrase (std11-full-name-string structure))
513 (address (std11-address-string structure))
515 (list phrase address)
522 (provide 'eword-decode)
524 ;;; eword-decode.el ends here