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
14 (if (not (fboundp 'member))
22 "$Id: tiny-mime.el,v 5.16 1995/07/26 05:54:16 morioka Exp $")
24 (defconst mime/tiny-mime-version (get-version-string mime/RCS-ID))
27 ;;; @ MIME encoded-word definition
30 (defconst mime/charset-regexp "[A-Za-z0-9!#$%&'*+---^_`{}|~]")
31 (defconst mime/encoded-text-regexp "[!->@-~]+")
33 (defconst mime/Base64-token-regexp "[A-Za-z0-9+/=]")
34 (defconst mime/Base64-encoded-text-regexp
36 mime/Base64-token-regexp
37 mime/Base64-token-regexp
38 mime/Base64-token-regexp
39 mime/Base64-token-regexp
41 (defconst mime/Base64-encoding-and-encoded-text-regexp
42 (concat "\\(B\\)\\?" mime/Base64-encoded-text-regexp))
44 (defconst mime/Quoted-Printable-hex-char-regexp "[0123456789ABCDEF]")
45 (defconst mime/Quoted-Printable-octet-regexp
47 mime/Quoted-Printable-hex-char-regexp
48 mime/Quoted-Printable-hex-char-regexp))
49 (defconst mime/Quoted-Printable-encoded-text-regexp
50 (concat "\\([^=?]\\|" mime/Quoted-Printable-octet-regexp "\\)+"))
51 (defconst mime/Quoted-Printable-encoding-and-encoded-text-regexp
52 (concat "\\(Q\\)\\?" mime/Quoted-Printable-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"))
92 (defvar mime/use-X-Nsubject nil)
95 ;;; @ compatible module among Mule, NEmacs and NEpoch
97 (cond ((boundp 'MULE) (require 'tm-mule))
98 ((boundp 'NEMACS)(require 'tm-nemacs))
99 (t (require 'tm-orig))
103 ;;; @ Application Interface
106 ;;; @@ MIME header decoders
110 (defun mime/decode-encoded-word (word)
111 (if (string-match mime/encoded-word-regexp word)
112 (let ((charset (upcase (mime/encoded-word-charset word)))
113 (encoding (mime/encoded-word-encoding word))
114 (text (mime/encoded-word-encoded-text word)))
115 (mime/decode-encoded-text charset encoding text))
118 (defun mime/decode-region (beg end)
122 (narrow-to-region beg end)
123 (goto-char (point-min))
124 (let (charset encoding text)
125 (while (re-search-forward mime/encoded-word-regexp nil t)
126 (insert (mime/decode-encoded-word
128 (buffer-substring (match-beginning 0) (match-end 0))
129 (delete-region (match-beginning 0) (match-end 0))
135 (defun mime/decode-message-header ()
139 (narrow-to-region (goto-char (point-min))
140 (progn (re-search-forward "^$" nil t) (point)))
141 (mime/prepare-decode-message-header)
142 (mime/decode-region (point-min) (point-max))
145 (defun mime/decode-string (str)
146 (let ((dest "")(ew nil)
148 (while (setq beg (string-match mime/encoded-word-regexp str))
150 (if (not (and (eq ew t) (string= (substring str 0 beg) " ")))
151 (setq dest (concat dest (substring str 0 beg)
155 (setq end (match-end 0))
156 (setq dest (concat dest (mime/decode-encoded-word (substring str beg end))
158 (setq str (substring str end))
164 ;;; @@ MIME header encoders
167 (defun mime/encode-string (string encoding &optional mode)
168 (cond ((equal encoding "B") (mime/base64-encode-string string))
169 ((equal encoding "Q") (mime/Quoted-Printable-encode-string string mode))
173 (defun mime/encode-field (str)
174 (setq str (message/unfolding-string str))
175 (let ((ret (message/divide-field str))
176 field-name field-body)
177 (setq field-name (car ret))
178 (setq field-body (nth 1 ret))
179 (concat field-name " "
180 (cond ((string= field-body "") "")
181 ((or (string-match "^Reply-To:$" field-name)
182 (string-match "^From:$" field-name)
183 (string-match "^Sender:$" field-name)
184 (string-match "^Resent-Reply-To:$" field-name)
185 (string-match "^Resent-From:$" field-name)
186 (string-match "^Resent-Sender:$" field-name)
187 (string-match "^To:$" field-name)
188 (string-match "^Resent-To:$" field-name)
189 (string-match "^cc:$" field-name)
190 (string-match "^Resent-cc:$" field-name)
191 (string-match "^bcc:$" field-name)
192 (string-match "^Resent-bcc:$" field-name)
194 (mime/encode-address-list
195 (+ (length field-name) 1) field-body)
199 (let ((r mime/no-encoding-header-fields) fn)
202 (if (string-match (concat "^" fn ":$") field-name)
203 (throw 'tag field-body)
207 (nth 1 (mime/encode-header-string
208 (+ (length field-name) 1) field-body))
213 (defun mime/encode-message-header ()
217 (narrow-to-region (goto-char (point-min))
220 (concat "^" (regexp-quote mail-header-separator) "$")
224 (goto-char (point-min))
226 (while (re-search-forward "^.+:.*\\(\n\\s +.*\\)*" nil t)
227 (setq beg (match-beginning 0))
228 (setq end (match-end 0))
229 (setq field (buffer-substring beg end))
230 (insert (mime/encode-field
232 (buffer-substring beg end)
233 (delete-region beg end)
236 (if mime/use-X-Nsubject
238 (goto-char (point-min))
239 (if (re-search-forward "^Subject:.*\\(\n\\s +.*\\)*" nil t)
240 (let ((str (buffer-substring (match-beginning 0)(match-end 0))))
241 (if (string-match mime/encoded-word-regexp str)
244 (nth 1 (message/divide-field
246 (message/unfolding-string str))
252 ;;; @ Base64 (B-encode) decoder/encoder
253 ;;; by Enami Tsugutomo
256 (defun mime/base64-decode-string (string)
257 (mime/base64-mapconcat (function mime/base64-decode-chars) 4 string))
259 ;; (mime/base64-encode-string (mime/base64-decode-string "GyRAOjRGI0stGyhK"))
260 (defun mime/base64-encode-string (string &optional mode)
261 (let ((es (mime/base64-mapconcat (function mime/base64-encode-chars) 3 string))
263 (setq m (mod (length es) 4))
270 ;; (char-to-string (mime/base64-bit-to-char 26))
271 (defun mime/base64-bit-to-char (n)
272 (cond ((eq n nil) ?=)
274 ((< n 52) (+ ?a (- n 26)))
275 ((< n 62) (+ ?0 (- n 52)))
278 (t (error "not a base64 integer %d" n))))
280 (defun mime/base64-char-to-bit (c)
281 (cond ((and (<= ?A c) (<= c ?Z)) (- c ?A))
282 ((and (<= ?a c) (<= c ?z)) (+ (- c ?a) 26))
283 ((and (<= ?0 c) (<= c ?9)) (+ (- c ?0) 52))
287 (t (error "not a base64 character %c" c))))
289 (defun mime/mask (i n) (logand i (1- (ash 1 n))))
291 (defun mime/base64-encode-1 (a &optional b &optional c)
293 (cons (logior (ash (mime/mask a 2) (- 6 2))
296 (cons (logior (ash (mime/mask b 4) (- 6 4))
299 (cons (mime/mask c (- 6 0))
302 (defun mime/base64-decode-1 (a b &optional c &optional d)
303 (cons (logior (ash a 2) (ash b (- 2 6)))
304 (if c (cons (logior (ash (mime/mask b 4) 4)
305 (mime/mask (ash c (- 4 6)) 4))
306 (if d (cons (logior (ash (mime/mask c 2) 6) d)
309 ;; (mime/base64-decode-chars ?G ?y ?R ?A)
310 (defun mime/base64-decode-chars (a b c d)
311 (apply (function mime/base64-decode-1)
312 (mapcar (function mime/base64-char-to-bit)
315 ;; (mapcar (function char-to-string) (mime/base64-encode-chars 27 36 64))
316 (defun mime/base64-encode-chars (a b c)
317 (mapcar (function mime/base64-bit-to-char) (mime/base64-encode-1 a b c)))
319 (defun mime/base64-fecth-from (func from pos len)
323 ret (cons (funcall func from (+ pos len)) ret)))
326 (defun mime/base64-fecth-from-buffer (from pos len)
327 (mime/base64-fecth-from (function (lambda (f p) (char-after p)))
330 (defun mime/base64-fecth-from-string (from pos len)
331 (mime/base64-fecth-from (function (lambda (f p)
332 (if (< p (length f)) (aref f p))))
335 (defun mime/base64-fecth (source pos len)
336 (cond ((stringp source) (mime/base64-fecth-from-string source pos len))
337 (t (mime/base64-fecth-from-buffer source pos len))))
339 (defun mime/base64-mapconcat (func unit string)
341 (while (< i (length string))
343 (apply (function concat)
345 (mapcar (function char-to-string)
346 (apply func (mime/base64-fecth string i unit)))))
350 ;;; @ Quoted-Printable (Q-encode) encoder/decoder
353 (defun mime/Quoted-Printable-decode-string (str)
358 (setq chr (elt str i))
362 (setq h (hex-char-to-number (elt str (+ i 1))))
363 (setq l (hex-char-to-number (elt str (+ i 2))))
364 (setq num (+ (* h 16) l))
365 (setq dest (concat dest (char-to-string num)))
369 (setq dest (concat dest (char-to-string chr)))
373 (setq dest (concat dest (char-to-string 32)))
377 (setq dest (concat dest (char-to-string chr)))
383 (defun mime/Quoted-Printable-encode-string (str &optional mode)
390 (setq chr (elt str i))
392 (setq dest (concat dest "_"))
397 (and (eq mode 'comment)
402 (and (eq mode 'phrase)
403 (not (string-match "[A-Za-z0-9!*+/=_---]"
404 (char-to-string chr)))
408 (setq dest (concat dest
410 (char-to-string (number-to-hex-char (/ chr 16)))
411 (char-to-string (number-to-hex-char (% chr 16)))
414 (t (setq dest (concat dest (char-to-string chr)))
420 ;;; @ functions for message header encoding
423 (defun mime/encode-and-split-string (n string charset encoding)
425 (len (length string))
426 (js (mime/convert-string-from-emacs string charset))
427 (cesl (+ (length charset) (length encoding) 6 ))
429 (setq ewl (mime/encoded-word-length js encoding))
432 (setq m (+ n ewl cesl))
435 (while (and (< i len)
436 (setq js (mime/convert-string-from-emacs
437 (substring string 0 i) charset))
438 (setq m (+ n (mime/encoded-word-length js encoding) cesl))
441 (setq i (+ i (char-bytes (elt string i))))
443 (setq js (mime/convert-string-from-emacs
444 (substring string 0 j) charset))
445 (setq m (+ n (mime/encoded-word-length js encoding) cesl))
446 (setq rest (substring string j))
451 (list m (concat "=?" charset "?" encoding "?"
452 (mime/encode-string js encoding)
457 (defun mime/encode-header-word (n string charset encoding)
458 (let (dest str ret m)
459 (if (null (setq ret (mime/encode-and-split-string n string charset encoding)))
462 (setq dest (nth 1 ret))
464 (setq str (nth 2 ret))
465 (while (and (stringp str)
466 (setq ret (mime/encode-and-split-string 1 str charset encoding))
468 (setq dest (concat dest "\n " (nth 1 ret)))
470 (setq str (nth 2 ret))
476 (defun mime/encode-header-string (n string &optional mode)
477 (if (string= string "")
479 (let ((ssl (mime/separate-string-for-encoder string))
480 i len cell et w ew (dest "") b l)
481 (setq len (length ssl))
482 (setq cell (nth 0 ssl))
484 ;; string-width crashes when the argument is nil,
485 ;; so replace the argument
486 ;; (original modification by Kenji Rikitake 9-JAN-1995)
487 (setq w (or (cdr cell) ""))
490 (if (> (+ n (string-width w)) 76)
492 (setq dest (concat dest "\n "))
496 (setq dest (concat dest w))
497 (setq b (+ b (string-width w)))
500 (setq ew (mime/encode-header-word n (cdr cell) (car et) (cdr et)))
501 (setq dest (nth 1 ew))
506 (setq cell (nth i ssl))
509 (cond ((string-match "^[ \t]*$" w)
510 (setq b (+ b (string-width (cdr cell))))
511 (setq dest (concat dest (cdr cell)))
514 (if (> (+ b (string-width w)) 76)
516 (if (eq (elt dest (- (length dest) 1)) 32)
517 (setq dest (substring dest 0 (- (length dest) 1)))
519 (setq dest (concat dest "\n " w))
520 (setq b (+ (length w) 1))
522 (setq l (length dest))
524 (eq (elt dest (- l 2)) ?\?)
525 (eq (elt dest (- l 1)) ?=)
528 (setq dest (concat dest " "))
531 (setq dest (concat dest w))
532 (setq b (+ b (string-width w)))
535 (if (not (eq (elt dest (- (length dest) 1)) 32))
537 (setq dest (concat dest " "))
541 (mime/encode-header-word b (cdr cell) (car et) (cdr et)))
543 (if (string-match "^\n" (nth 1 ew))
544 (setq dest (concat (substring dest 0 (- (length dest) 1))
546 (setq dest (concat dest (nth 1 ew)))
554 (defun mime/encode-address-list (n str)
555 (let* ((ret (message/parse-addresses str))
556 (r ret) cell en-ret j cl (dest "") s)
559 (cond ((string= (nth 1 cell) "<")
560 (setq en-ret (mime/encode-header-string n (nth 0 cell) 'phrase))
561 (setq dest (concat dest (nth 1 en-ret)))
562 (setq n (car en-ret))
565 (mime/encode-header-string
566 n (concat (nth 1 cell)(nth 2 cell)(nth 3 cell) ", ")))
567 (setq en-ret (mime/encode-header-string
568 n (concat (nth 1 cell)(nth 2 cell)(nth 3 cell))))
570 (if (and (eq (elt (nth 1 en-ret) 0) ?\n)
571 (eq (elt dest (- (length dest) 1)) 32))
572 (setq dest (substring dest 0 (- (length dest) 1)))
574 (setq dest (concat dest (nth 1 en-ret)))
575 (setq n (car en-ret))
578 (setq en-ret (mime/encode-header-string n (nth 0 cell)))
579 (setq dest (concat dest (nth 1 en-ret)))
580 (setq n (car en-ret))
582 (setq en-ret (mime/encode-header-string (+ n 2) (nth 2 cell)
584 (if (eq (elt (nth 1 en-ret) 0) ?\n)
586 (setq dest (concat dest "\n ("))
587 (setq en-ret (mime/encode-header-string 2 (nth 2 cell)
591 (setq dest (concat dest " ("))
593 (setq dest (concat dest (nth 1 en-ret)))
594 (setq n (car en-ret))
597 (mime/encode-header-string n (concat (nth 3 cell) ", "))
599 (setq en-ret (mime/encode-header-string n (nth 3 cell)))
601 (setq dest (concat dest (nth 1 en-ret)))
602 (setq n (car en-ret))
607 (mime/encode-header-string n (concat (nth 0 cell) ", "))
609 (setq en-ret (mime/encode-header-string n (nth 0 cell)))
611 (setq dest (concat dest (nth 1 en-ret)))
612 (setq n (car en-ret))
619 ;;; @ utility for encoder
622 ;;; @@ encoded-word length
625 (defun mime/encoded-word-length (string encoding)
626 (cond ((equal encoding "B") (mime/base64-length string))
627 ((equal encoding "Q") (mime/Quoted-Printable-length string))
631 (defun mime/base64-length (string)
632 (let ((l (length string))
635 (if (= (mod l 3) 0) 0 1)
639 (defun mime/Quoted-Printable-length (string &optional mode)
640 (let ((l 0)(i 0)(len (length string)) chr)
642 (setq chr (elt string i))
643 (if (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))
650 ;;; @@ separate by character set
654 (defconst LC-space 2)
656 ;; by mol. 1993/10/16
657 (defun mime/char-type (chr)
658 (if (or (= chr 32)(= chr ?\t))
663 (defun mime/separate-string-by-chartype (string)
664 (let ((len (length string))
669 (setq chr (elt string 0))
670 (setq pcs (mime/char-type chr))
671 (setq i (char-bytes chr))
672 (setq ds (substring string 0 i))
674 (setq chr (elt string i))
675 (setq cs (mime/char-type chr))
676 (setq j (+ i (char-bytes chr)))
677 (setq s (substring string i j))
680 (setq ds (concat ds s))
681 (progn (setq dest (append dest (list (cons pcs ds))))
686 (if (not (string= ds ""))
687 (setq dest (append dest (list (cons pcs ds)))))
691 (defun mime/separate-string-by-charset (str)
692 (let ((rl (mime/separate-string-by-chartype str))
693 (i 1) len (pcell nil) cell ncell dpcell (dest nil) LC)
694 (setq len (length rl))
695 (setq dpcell (list (nth 0 rl)))
696 (setq cell (nth 1 rl))
697 (setq ncell (nth 2 rl))
699 (setq LC (car (car dpcell)))
700 (cond ((and (not (eq LC lc-ascii))
701 (eq (car cell) LC-space)
702 (not (eq (car ncell) lc-ascii)))
703 (setq dpcell (list (cons LC
704 (concat (cdr (car dpcell)) (cdr cell))
707 ((and (not (eq LC lc-ascii))
709 (setq dpcell (list (cons LC
710 (concat (cdr (car dpcell)) (cdr cell))
713 ((and (eq LC lc-ascii)
714 (member (car cell) mime/latin-lc-list))
715 (setq dpcell (list (cons (car cell)
716 (concat (cdr (car dpcell)) (cdr cell))
719 ((and (member LC mime/latin-lc-list)
720 (eq (car cell) lc-ascii))
721 (setq dpcell (list (cons LC
722 (concat (cdr (car dpcell)) (cdr cell))
726 (setq dest (append dest dpcell))
727 (setq dpcell (list cell))
731 (setq ncell (nth (+ i 1) rl))
733 (setq dest (append dest dpcell))
736 (defun mime/separate-string-for-encoder (string)
738 (if (string-match "[ \t]+$" string)
740 (setq lastspace (substring string
743 (setq string (substring string 0 (match-beginning 0)))
745 (let ((rl (mime/separate-string-by-charset string))
746 (i 0) len cell0 cell1 cell2 (dest nil))
747 (setq len (length rl))
748 (setq cell0 (nth 0 rl))
749 (setq cell1 (nth 1 rl))
750 (setq cell2 (nth 2 rl))
752 (cond ((and (not (eq (car cell0) lc-ascii))
753 (eq (car cell1) LC-space)
754 (not (eq (car cell2) lc-ascii))
759 (cdr (assoc (car cell0)
760 mime/lc-charset-and-encoding-alist))
761 (concat (cdr cell0) (cdr cell1))
764 (setq cell0 (nth i rl))
765 (setq cell1 (nth (+ i 1) rl))
766 (setq cell2 (nth (+ i 2) rl))
772 (cdr (assoc (car cell0)
773 mime/lc-charset-and-encoding-alist))
778 (setq cell2 (nth (+ i 2) rl))
783 (list (cons nil lastspace))))
789 ;;; basic functions for MIME header decoder
792 ;;; @ utility for decoder
795 (defun mime/unfolding ()
796 (goto-char (point-min))
798 (while (re-search-forward message/field-name-regexp nil t)
799 (setq beg (match-beginning 0))
801 (if (re-search-forward "\n[!-9;-~]+:" nil t)
802 (goto-char (match-beginning 0))
803 (if (re-search-forward "^$" nil t)
804 (goto-char (1- (match-beginning 0)))
809 (setq field (buffer-substring beg end))
810 (if (string-match mime/encoded-word-regexp field)
812 (narrow-to-region (goto-char beg) end)
813 (while (re-search-forward "\n[ \t]+" nil t)
816 (goto-char (point-max))
820 (defun mime/prepare-decode-message-header ()
822 (goto-char (point-min))
823 (while (re-search-forward
824 (concat (regexp-quote "?=")
828 (replace-match "?==?")
832 (run-hooks 'mime/tiny-mime-load-hook)
839 ;;; mode: outline-minor
840 ;;; outline-regexp: ";;; @+\\|(......"