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.1 1995/10/03 04:34:26 morioka Exp $
13 ;;; Keywords: mail, news, MIME, RFC 1522, multilingual, encoded-word
21 ;;; @ MIME encoded-word definition
24 (defconst mime/encoded-text-regexp "[!->@-~]+")
25 (defconst mime/encoded-word-regexp (concat (regexp-quote "=?")
33 mime/encoded-text-regexp
41 (defun mime/decode-encoded-words-string (str)
42 (let ((dest "")(ew nil)
44 (while (setq beg (string-match mime/encoded-word-regexp str))
46 (if (not (and (eq ew t) (string= (substring str 0 beg) " ")))
47 (setq dest (concat dest (substring str 0 beg)
51 (setq end (match-end 0))
52 (setq dest (concat dest
53 (mime/decode-encoded-word (substring str beg end))
55 (setq str (substring str end))
65 (defun mime/decode-encoded-words-region (beg end &optional unfolding)
69 (narrow-to-region beg end)
73 (goto-char (point-min))
74 (while (re-search-forward
75 (concat (regexp-quote "?=") "\\s +" (regexp-quote "=?"))
77 (replace-match "?==?")
79 (goto-char (point-min))
80 (let (charset encoding text)
81 (while (re-search-forward mime/encoded-word-regexp nil t)
82 (insert (mime/decode-encoded-word
84 (buffer-substring (match-beginning 0) (match-end 0))
85 (delete-region (match-beginning 0) (match-end 0))
92 ;;; @ for message header
95 (defun mime/decode-message-header ()
99 (narrow-to-region (goto-char (point-min))
100 (progn (re-search-forward "^$" nil t) (point)))
101 (mime/decode-encoded-words-region (point-min) (point-max) t)
104 (defun mime/unfolding ()
105 (goto-char (point-min))
107 (while (re-search-forward message/field-name-regexp nil t)
108 (setq beg (match-beginning 0))
109 (setq end (message/field-end))
110 (setq field (buffer-substring beg end))
111 (if (string-match mime/encoded-word-regexp field)
113 (narrow-to-region (goto-char beg) end)
114 (while (re-search-forward "\n[ \t]+" nil t)
117 (goto-char (point-max))
122 ;;; @ encoded-word decoder
125 (defun mime/decode-encoded-word (word)
126 (or (if (string-match mime/encoded-word-regexp word)
129 (substring word (match-beginning 1) (match-end 1))
133 (substring word (match-beginning 2) (match-end 2))
136 (substring word (match-beginning 3) (match-end 3))
138 (mime/decode-encoded-text charset encoding text)
143 ;;; @ encoded-text decoder
146 (defun mime/decode-encoded-text (charset encoding str)
148 (cond ((string= "B" encoding)
149 (base64-decode-string str))
150 ((string= "Q" encoding)
151 (q-encoding-decode-string str))
152 (t (message "unknown encoding %s" encoding)
155 (mime/convert-string-to-emacs charset dest)