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>
12 ;;; $Id: tm-ew-d.el,v 7.2 1995/10/05 12:20:36 morioka Exp $
13 ;;; Keywords: mail, news, MIME, RFC 1522, multilingual, encoded-word
22 ;;; @ MIME encoded-word definition
25 (defconst mime/encoded-text-regexp "[!->@-~]+")
26 (defconst mime/encoded-word-regexp (concat (regexp-quote "=?")
34 mime/encoded-text-regexp
42 (defun mime/decode-encoded-words-string (str)
43 (let ((dest "")(ew nil)
45 (while (setq beg (string-match mime/encoded-word-regexp str))
47 (if (not (and (eq ew t) (string= (substring str 0 beg) " ")))
48 (setq dest (concat dest (substring str 0 beg)
52 (setq end (match-end 0))
53 (setq dest (concat dest
54 (mime/decode-encoded-word (substring str beg end))
56 (setq str (substring str end))
66 (defun mime/decode-encoded-words-region (beg end &optional unfolding)
70 (narrow-to-region beg end)
74 (goto-char (point-min))
75 (while (re-search-forward
76 (concat (regexp-quote "?=") "\\s +" (regexp-quote "=?"))
78 (replace-match "?==?")
80 (goto-char (point-min))
81 (let (charset encoding text)
82 (while (re-search-forward mime/encoded-word-regexp nil t)
83 (insert (mime/decode-encoded-word
85 (buffer-substring (match-beginning 0) (match-end 0))
86 (delete-region (match-beginning 0) (match-end 0))
93 ;;; @ for message header
96 (defun mime/decode-message-header ()
100 (narrow-to-region (goto-char (point-min))
101 (progn (re-search-forward "^$" nil t) (point)))
102 (mime/decode-encoded-words-region (point-min) (point-max) t)
105 (defun mime/unfolding ()
106 (goto-char (point-min))
108 (while (re-search-forward rfc822/field-top-regexp nil t)
109 (setq beg (match-beginning 0))
110 (setq end (rfc822/field-end))
111 (setq field (buffer-substring beg end))
112 (if (string-match mime/encoded-word-regexp field)
114 (narrow-to-region (goto-char beg) end)
115 (while (re-search-forward "\n[ \t]+" nil t)
118 (goto-char (point-max))
123 ;;; @ encoded-word decoder
126 (defun mime/decode-encoded-word (word)
127 (or (if (string-match mime/encoded-word-regexp word)
130 (substring word (match-beginning 1) (match-end 1))
134 (substring word (match-beginning 2) (match-end 2))
137 (substring word (match-beginning 3) (match-end 3))
139 (mime/decode-encoded-text charset encoding text)
144 ;;; @ encoded-text decoder
147 (defun mime/decode-encoded-text (charset encoding str)
149 (cond ((string= "B" encoding)
150 (base64-decode-string str))
151 ((string= "Q" encoding)
152 (q-encoding-decode-string str))
153 (t (message "unknown encoding %s" encoding)
156 (mime/convert-string-to-emacs charset dest)