2 ;;; tm-ew-d.el --- RFC 1522 based multilingual MIME message header
3 ;;; decoder for GNU Emacs
5 ;;; Copyright (C) 1995 Free Software Foundation, Inc.
6 ;;; Copyright (C) 1992 ENAMI Tsugutomo
7 ;;; Copyright (C) 1993,1994,1995 MORIOKA Tomohiko
9 ;;; Author: ENAMI Tsugutomo <enami@sys.ptg.sony.co.jp>
10 ;;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
11 ;;; Keywords: mail, news, MIME, RFC 1522, multilingual, encoded-word
23 (defconst tm-ew-d/RCS-ID
24 "$Id: tm-ew-d.el,v 7.5 1995/10/18 11:18:03 morioka Exp $")
25 (defconst mime/eword-decoder-version (get-version-string tm-ew-d/RCS-ID))
28 ;;; @ MIME encoded-word definition
31 (defconst mime/encoded-text-regexp "[!->@-~]+")
32 (defconst mime/encoded-word-regexp (concat (regexp-quote "=?")
40 mime/encoded-text-regexp
48 (defun mime-eword/decode-string (str)
49 (setq str (rfc822/unfolding-string str))
50 (let ((dest "")(ew nil)
52 (while (setq beg (string-match mime/encoded-word-regexp str))
54 (if (not (and (eq ew t) (string= (substring str 0 beg) " ")))
55 (setq dest (concat dest (substring str 0 beg)
59 (setq end (match-end 0))
60 (setq dest (concat dest
61 (mime/decode-encoded-word (substring str beg end))
63 (setq str (substring str end))
73 (defun mime-eword/decode-region (beg end &optional unfolding)
77 (narrow-to-region beg end)
81 (goto-char (point-min))
82 (while (re-search-forward
83 (concat (regexp-quote "?=") "\\s +" (regexp-quote "=?"))
85 (replace-match "?==?")
87 (goto-char (point-min))
88 (let (charset encoding text)
89 (while (re-search-forward mime/encoded-word-regexp nil t)
90 (insert (mime/decode-encoded-word
92 (buffer-substring (match-beginning 0) (match-end 0))
93 (delete-region (match-beginning 0) (match-end 0))
100 ;;; @ for message header
103 (defun mime/decode-message-header ()
107 (narrow-to-region (goto-char (point-min))
108 (progn (re-search-forward "^$" nil t) (point)))
109 (mime-eword/decode-region (point-min) (point-max) t)
112 (defun mime/unfolding ()
113 (goto-char (point-min))
115 (while (re-search-forward rfc822/field-top-regexp nil t)
116 (setq beg (match-beginning 0))
117 (setq end (rfc822/field-end))
118 (setq field (buffer-substring beg end))
119 (if (string-match mime/encoded-word-regexp field)
121 (narrow-to-region (goto-char beg) end)
122 (while (re-search-forward "\n[ \t]+" nil t)
125 (goto-char (point-max))
130 ;;; @ encoded-word decoder
133 (defun mime/decode-encoded-word (word)
134 (or (if (string-match mime/encoded-word-regexp word)
137 (substring word (match-beginning 1) (match-end 1))
141 (substring word (match-beginning 2) (match-end 2))
144 (substring word (match-beginning 3) (match-end 3))
146 (mime/decode-encoded-text charset encoding text)
151 ;;; @ encoded-text decoder
154 (defun mime/decode-encoded-text (charset encoding str)
156 (cond ((string= "B" encoding)
157 (base64-decode-string str))
158 ((string= "Q" encoding)
159 (q-encoding-decode-string str))
160 (t (message "unknown encoding %s" encoding)
163 (mime/convert-string-to-emacs charset dest)