2 ;;; A multilingual MIME message header encoder/decoder.
3 ;;; by Morioka Tomohiko (morioka@jaist.ac.jp)
5 ;;; original MIME decoder is
6 ;;; mime.el,v 1.5 1992/07/18 07:52:08 by Enami Tsugutomo
24 "$Id: tiny-mime.el,v 6.4 1995/09/04 01:16:45 morioka Exp $")
26 (defconst mime/tiny-mime-version (get-version-string mime/RCS-ID))
29 ;;; @ MIME encoded-word definition
32 (defconst mime/Base64-token-regexp "[A-Za-z0-9+/=]")
33 (defconst mime/Base64-encoded-text-regexp
35 mime/Base64-token-regexp
36 mime/Base64-token-regexp
37 mime/Base64-token-regexp
38 mime/Base64-token-regexp
40 (defconst mime/Base64-encoding-and-encoded-text-regexp
41 (concat "\\(B\\)\\?" mime/Base64-encoded-text-regexp))
43 (defconst mime/Quoted-Printable-hex-char-regexp "[0123456789ABCDEF]")
44 (defconst mime/Quoted-Printable-octet-regexp
46 mime/Quoted-Printable-hex-char-regexp
47 mime/Quoted-Printable-hex-char-regexp))
48 (defconst mime/Quoted-Printable-encoded-text-regexp
49 (concat "\\([^=?]\\|" mime/Quoted-Printable-octet-regexp "\\)+"))
50 (defconst mime/Quoted-Printable-encoding-and-encoded-text-regexp
51 (concat "\\(Q\\)\\?" mime/Quoted-Printable-encoded-text-regexp))
53 (defconst mime/encoded-text-regexp "[!->@-~]+")
54 (defconst mime/encoded-word-regexp (concat (regexp-quote "=?")
62 mime/encoded-text-regexp
66 (defun mime/nth-string (s n)
68 (substring s (match-beginning n) (match-end n))
69 (buffer-substring (match-beginning n) (match-end n))))
71 (defun mime/encoded-word-charset (str)
72 (mime/nth-string str 1))
74 (defun mime/encoded-word-encoding (str)
75 (mime/nth-string str 2))
77 (defun mime/encoded-word-encoded-text (str)
78 (mime/nth-string str 3))
80 (defun mime/rest-of-string (str)
82 (substring str (match-end 0))
83 (buffer-substring (match-end 0)(point-max))
90 (defvar mime/no-encoding-header-fields '("X-Nsubject" "Newsgroups"))
92 (defvar mime/use-X-Nsubject nil)
95 ;;; @ Application Interface
98 ;;; @@ MIME header decoders
101 (defun mime/decode-encoded-text (charset encoding str)
103 (cond ((string= "B" encoding)
104 (base64-decode-string str))
105 ((string= "Q" encoding)
106 (q-encoding-decode-string str))
107 (t (message "unknown encoding %s" encoding)
110 (mime/convert-string-to-emacs charset dest)
113 (defun mime/decode-encoded-word (word)
114 (or (if (string-match mime/encoded-word-regexp word)
115 (let ((charset (upcase (mime/encoded-word-charset word)))
116 (encoding (upcase (mime/encoded-word-encoding word)))
117 (text (mime/encoded-word-encoded-text word)))
118 (mime/decode-encoded-text charset encoding text)
122 (defun mime/decode-region (beg end)
126 (narrow-to-region beg end)
127 (goto-char (point-min))
128 (let (charset encoding text)
129 (while (re-search-forward mime/encoded-word-regexp nil t)
130 (insert (mime/decode-encoded-word
132 (buffer-substring (match-beginning 0) (match-end 0))
133 (delete-region (match-beginning 0) (match-end 0))
139 (defun mime/decode-message-header ()
143 (narrow-to-region (goto-char (point-min))
144 (progn (re-search-forward "^$" nil t) (point)))
145 (mime/prepare-decode-message-header)
146 (mime/decode-region (point-min) (point-max))
149 (defun mime/decode-string (str)
150 (let ((dest "")(ew nil)
152 (while (setq beg (string-match mime/encoded-word-regexp str))
154 (if (not (and (eq ew t) (string= (substring str 0 beg) " ")))
155 (setq dest (concat dest (substring str 0 beg)
159 (setq end (match-end 0))
160 (setq dest (concat dest
161 (mime/decode-encoded-word (substring str beg end))
163 (setq str (substring str end))
169 ;;; @@ MIME header encoders
172 (defun mime/encode-string (string encoding &optional mode)
173 (cond ((string= encoding "B") (base64-encode-string string))
174 ((string= encoding "Q") (q-encoding-encode-string string mode))
177 (defun mime/encode-field (str)
178 (setq str (message/unfolding-string str))
179 (let ((ret (message/divide-field str))
180 field-name field-body)
181 (setq field-name (car ret))
182 (setq field-body (nth 1 ret))
183 (concat field-name " "
184 (cond ((string= field-body "") "")
185 ((or (string-match "^Reply-To:$" field-name)
186 (string-match "^From:$" field-name)
187 (string-match "^Sender:$" field-name)
188 (string-match "^Resent-Reply-To:$" field-name)
189 (string-match "^Resent-From:$" field-name)
190 (string-match "^Resent-Sender:$" field-name)
191 (string-match "^To:$" field-name)
192 (string-match "^Resent-To:$" field-name)
193 (string-match "^cc:$" field-name)
194 (string-match "^Resent-cc:$" field-name)
195 (string-match "^bcc:$" field-name)
196 (string-match "^Resent-bcc:$" field-name)
198 (mime/encode-address-list
199 (+ (length field-name) 1) field-body)
203 (let ((r mime/no-encoding-header-fields) fn)
206 (if (string-match (concat "^" fn ":$") field-name)
207 (throw 'tag field-body)
211 (nth 1 (mime/encode-header-string
212 (+ (length field-name) 1) field-body))
217 (defun mime/encode-message-header ()
221 (narrow-to-region (goto-char (point-min))
225 "^" (regexp-quote mail-header-separator) "$")
229 (goto-char (point-min))
231 (while (re-search-forward "^.+:.*\\(\n\\s +.*\\)*" nil t)
232 (setq beg (match-beginning 0))
233 (setq end (match-end 0))
234 (setq field (buffer-substring beg end))
235 (insert (mime/encode-field
237 (buffer-substring beg end)
238 (delete-region beg end)
241 (if mime/use-X-Nsubject
243 (goto-char (point-min))
244 (if (re-search-forward "^Subject:.*\\(\n\\s +.*\\)*" nil t)
245 (let ((str (buffer-substring (match-beginning 0)
247 (if (string-match mime/encoded-word-regexp str)
250 (nth 1 (message/divide-field
252 (message/unfolding-string str))
259 ;;; @ functions for message header encoding
262 (defun mime/encode-and-split-string (n string charset encoding)
264 (len (length string))
265 (js (mime/convert-string-from-emacs string charset))
266 (cesl (+ (length charset) (length encoding) 6 ))
268 (setq ewl (mime/encoded-word-length js encoding))
271 (setq m (+ n ewl cesl))
274 (while (and (< i len)
275 (setq js (mime/convert-string-from-emacs
276 (substring string 0 i) charset))
278 (mime/encoded-word-length js encoding)
282 (setq i (+ i (char-bytes (elt string i))))
284 (setq js (mime/convert-string-from-emacs
285 (substring string 0 j) charset))
286 (setq m (+ n (mime/encoded-word-length js encoding) cesl))
287 (setq rest (substring string j))
292 (list m (concat "=?" charset "?" encoding "?"
293 (mime/encode-string js encoding)
298 (defun mime/encode-header-word (n string charset encoding)
299 (let (dest str ret m)
301 (mime/encode-and-split-string n string charset encoding)))
304 (setq dest (nth 1 ret))
306 (setq str (nth 2 ret))
307 (while (and (stringp str)
309 (mime/encode-and-split-string
310 1 str charset encoding))
312 (setq dest (concat dest "\n " (nth 1 ret)))
314 (setq str (nth 2 ret))
320 (defun mime/encode-header-string (n string &optional mode)
321 (if (string= string "")
323 (let ((ssl (mime/separate-string-for-encoder string))
324 i len cell et w ew (dest "") b l)
325 (setq len (length ssl))
326 (setq cell (nth 0 ssl))
328 ;; string-width crashes when the argument is nil,
329 ;; so replace the argument
330 ;; (original modification by Kenji Rikitake 9-JAN-1995)
331 (setq w (or (cdr cell) ""))
334 (if (> (+ n (string-width w)) 76)
336 (setq dest (concat dest "\n "))
340 (setq dest (concat dest w))
341 (setq b (+ b (string-width w)))
344 (setq ew (mime/encode-header-word n (cdr cell) (car et) (cdr et)))
345 (setq dest (nth 1 ew))
350 (setq cell (nth i ssl))
353 (cond ((string-match "^[ \t]*$" w)
354 (setq b (+ b (string-width (cdr cell))))
355 (setq dest (concat dest (cdr cell)))
358 (if (> (+ b (string-width w)) 76)
360 (if (eq (elt dest (- (length dest) 1)) 32)
361 (setq dest (substring dest 0 (- (length dest) 1)))
363 (setq dest (concat dest "\n " w))
364 (setq b (+ (length w) 1))
366 (setq l (length dest))
368 (eq (elt dest (- l 2)) ?\?)
369 (eq (elt dest (- l 1)) ?=)
372 (setq dest (concat dest " "))
375 (setq dest (concat dest w))
376 (setq b (+ b (string-width w)))
379 (if (not (eq (elt dest (- (length dest) 1)) 32))
381 (setq dest (concat dest " "))
385 (mime/encode-header-word b (cdr cell) (car et) (cdr et)))
387 (if (string-match "^\n" (nth 1 ew))
388 (setq dest (concat (substring dest 0 (- (length dest) 1))
390 (setq dest (concat dest (nth 1 ew)))
398 (defun mime/encode-address-list (n str)
399 (let* ((ret (message/parse-addresses str))
400 (r ret) cell en-ret j cl (dest "") s)
403 (cond ((string= (nth 1 cell) "<")
404 (setq en-ret (mime/encode-header-string n (nth 0 cell) 'phrase))
405 (setq dest (concat dest (nth 1 en-ret)))
406 (setq n (car en-ret))
409 (mime/encode-header-string
410 n (concat (nth 1 cell)(nth 2 cell)(nth 3 cell) ", ")))
411 (setq en-ret (mime/encode-header-string
412 n (concat (nth 1 cell)(nth 2 cell)(nth 3 cell))))
414 (if (and (eq (elt (nth 1 en-ret) 0) ?\n)
415 (eq (elt dest (- (length dest) 1)) 32))
416 (setq dest (substring dest 0 (- (length dest) 1)))
418 (setq dest (concat dest (nth 1 en-ret)))
419 (setq n (car en-ret))
422 (setq en-ret (mime/encode-header-string n (nth 0 cell)))
423 (setq dest (concat dest (nth 1 en-ret)))
424 (setq n (car en-ret))
426 (setq en-ret (mime/encode-header-string (+ n 2) (nth 2 cell)
428 (if (eq (elt (nth 1 en-ret) 0) ?\n)
430 (setq dest (concat dest "\n ("))
431 (setq en-ret (mime/encode-header-string 2 (nth 2 cell)
435 (setq dest (concat dest " ("))
437 (setq dest (concat dest (nth 1 en-ret)))
438 (setq n (car en-ret))
441 (mime/encode-header-string n (concat (nth 3 cell) ", "))
443 (setq en-ret (mime/encode-header-string n (nth 3 cell)))
445 (setq dest (concat dest (nth 1 en-ret)))
446 (setq n (car en-ret))
451 (mime/encode-header-string n (concat (nth 0 cell) ", "))
453 (setq en-ret (mime/encode-header-string n (nth 0 cell)))
455 (setq dest (concat dest (nth 1 en-ret)))
456 (setq n (car en-ret))
463 ;;; @ utility for encoder
466 ;;; @@ encoded-word length
469 (defun mime/encoded-word-length (string encoding)
470 (cond ((equal encoding "B") (mime/base64-length string))
471 ((equal encoding "Q") (mime/Quoted-Printable-length string))
475 (defun mime/base64-length (string)
476 (let ((l (length string))
479 (if (= (mod l 3) 0) 0 1)
483 (defun mime/Quoted-Printable-length (string &optional mode)
484 (let ((l 0)(i 0)(len (length string)) chr)
486 (setq chr (elt string i))
487 (if (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))
494 ;;; @@ separate by character set
498 (defconst LC-space 2)
500 ;; by mol. 1993/10/16
501 (defun mime/char-type (chr)
502 (if (or (= chr 32)(= chr ?\t))
507 (defun mime/separate-string-by-chartype (string)
508 (let ((len (length string))
513 (setq chr (elt string 0))
514 (setq pcs (mime/char-type chr))
515 (setq i (char-bytes chr))
516 (setq ds (substring string 0 i))
518 (setq chr (elt string i))
519 (setq cs (mime/char-type chr))
520 (setq j (+ i (char-bytes chr)))
521 (setq s (substring string i j))
524 (setq ds (concat ds s))
525 (progn (setq dest (append dest (list (cons pcs ds))))
530 (if (not (string= ds ""))
531 (setq dest (append dest (list (cons pcs ds)))))
535 (defun mime/separate-string-by-charset (str)
536 (let ((rl (mime/separate-string-by-chartype str))
537 (i 1) len (pcell nil) cell ncell dpcell (dest nil) LC)
538 (setq len (length rl))
539 (setq dpcell (list (nth 0 rl)))
540 (setq cell (nth 1 rl))
541 (setq ncell (nth 2 rl))
543 (setq LC (car (car dpcell)))
544 (cond ((and (not (eq LC lc-ascii))
545 (eq (car cell) LC-space)
546 (not (eq (car ncell) lc-ascii)))
547 (setq dpcell (list (cons LC
548 (concat (cdr (car dpcell)) (cdr cell))
551 ((and (not (eq LC lc-ascii))
553 (setq dpcell (list (cons LC
554 (concat (cdr (car dpcell)) (cdr cell))
557 ((and (eq LC lc-ascii)
558 (member (car cell) mime/latin-lc-list))
559 (setq dpcell (list (cons (car cell)
560 (concat (cdr (car dpcell)) (cdr cell))
563 ((and (member LC mime/latin-lc-list)
564 (eq (car cell) lc-ascii))
565 (setq dpcell (list (cons LC
566 (concat (cdr (car dpcell)) (cdr cell))
570 (setq dest (append dest dpcell))
571 (setq dpcell (list cell))
575 (setq ncell (nth (+ i 1) rl))
577 (setq dest (append dest dpcell))
580 (defun mime/separate-string-for-encoder (string)
582 (if (string-match "[ \t]+$" string)
584 (setq lastspace (substring string
587 (setq string (substring string 0 (match-beginning 0)))
589 (let ((rl (mime/separate-string-by-charset string))
590 (i 0) len cell0 cell1 cell2 (dest nil))
591 (setq len (length rl))
592 (setq cell0 (nth 0 rl))
593 (setq cell1 (nth 1 rl))
594 (setq cell2 (nth 2 rl))
596 (cond ((and (not (eq (car cell0) lc-ascii))
597 (eq (car cell1) LC-space)
598 (not (eq (car cell2) lc-ascii))
603 (cdr (assoc (car cell0)
604 mime/lc-charset-and-encoding-alist))
605 (concat (cdr cell0) (cdr cell1))
608 (setq cell0 (nth i rl))
609 (setq cell1 (nth (+ i 1) rl))
610 (setq cell2 (nth (+ i 2) rl))
616 (cdr (assoc (car cell0)
617 mime/lc-charset-and-encoding-alist))
622 (setq cell2 (nth (+ i 2) rl))
627 (list (cons nil lastspace))))
633 ;;; basic functions for MIME header decoder
636 ;;; @ utility for decoder
639 (defun mime/unfolding ()
640 (goto-char (point-min))
642 (while (re-search-forward message/field-name-regexp nil t)
643 (setq beg (match-beginning 0))
645 (if (re-search-forward "\n[!-9;-~]+:" nil t)
646 (goto-char (match-beginning 0))
647 (if (re-search-forward "^$" nil t)
648 (goto-char (1- (match-beginning 0)))
653 (setq field (buffer-substring beg end))
654 (if (string-match mime/encoded-word-regexp field)
656 (narrow-to-region (goto-char beg) end)
657 (while (re-search-forward "\n[ \t]+" nil t)
660 (goto-char (point-max))
664 (defun mime/prepare-decode-message-header ()
666 (goto-char (point-min))
667 (while (re-search-forward
668 (concat (regexp-quote "?=")
672 (replace-match "?==?")
676 (run-hooks 'mime/tiny-mime-load-hook)
683 ;;; mode: outline-minor
684 ;;; outline-regexp: ";;; @+\\|(......"