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
16 (if (not (fboundp 'member))
24 "$Id: tiny-mime.el,v 5.3 1994/10/25 11:51:05 morioka Exp $")
26 (defconst mime/tiny-mime-version (get-version-string mime/RCS-ID))
29 ;;; @ MIME encoded-word definition
32 (defconst mime/charset-regexp "[A-Za-z0-9!#$%&'*+---^_`{}|~]")
33 (defconst mime/encoded-text-regexp "[!->@-~]+")
35 (defconst mime/Base64-token-regexp "[A-Za-z0-9+/=]")
36 (defconst mime/Base64-encoded-text-regexp
38 mime/Base64-token-regexp
39 mime/Base64-token-regexp
40 mime/Base64-token-regexp
41 mime/Base64-token-regexp
43 (defconst mime/Base64-encoding-and-encoded-text-regexp
44 (concat "\\(B\\)\\?" mime/Base64-encoded-text-regexp))
46 (defconst mime/Quoted-Printable-hex-char "[0123456789ABCDEF]")
47 (defconst mime/Quoted-Printable-encoded-text-regexp
48 (concat "\\([^=?_]\\|="
49 mime/Quoted-Printable-hex-char
50 mime/Quoted-Printable-hex-char
52 (defconst mime/Quoted-Printable-encoding-and-encoded-text-regexp
53 (concat "\\(Q\\)\\?" mime/Quoted-Printable-encoded-text-regexp))
55 (defconst mime/encoded-word-regexp (concat (regexp-quote "=?")
63 mime/encoded-text-regexp
67 (defun mime/nth-string (s n)
69 (substring s (match-beginning n) (match-end n))
70 (buffer-substring (match-beginning n) (match-end n))))
72 (defun mime/encoded-word-charset (str)
73 (mime/nth-string str 1))
75 (defun mime/encoded-word-encoding (str)
76 (mime/nth-string str 2))
78 (defun mime/encoded-word-encoded-text (str)
79 (mime/nth-string str 3))
81 (defun mime/rest-of-string (str)
83 (substring str (match-end 0))
84 (buffer-substring (match-end 0))))
89 (defvar mime/no-encoding-header-fields '("X-Nsubject"))
91 (defvar mime/use-X-Nsubject nil)
94 ;;; @ compatible module among Mule, NEmacs and NEpoch
96 (cond ((boundp 'MULE) (require 'tm-mule))
97 ((boundp 'NEMACS)(require 'tm-nemacs))
98 (t (require 'tm-orig))
102 ;;; @ Application Interface
105 ;;; @@ MIME header decoders
109 (defun mime/decode-encoded-word (word)
110 (if (string-match mime/encoded-word-regexp word)
111 (let ((charset (upcase (mime/encoded-word-charset word)))
112 (encoding (mime/encoded-word-encoding word))
113 (text (mime/encoded-word-encoded-text word)))
114 (mime/decode-encoded-text charset encoding text))
117 (defun mime/decode-region (beg end)
121 (narrow-to-region beg end)
122 (goto-char (point-min))
123 (let (charset encoding text)
124 (while (re-search-forward mime/encoded-word-regexp nil t)
125 (insert (mime/decode-encoded-word
127 (buffer-substring (match-beginning 0) (match-end 0))
128 (delete-region (match-beginning 0) (match-end 0))
134 (defun mime/decode-message-header ()
138 (narrow-to-region (goto-char (point-min))
139 (progn (re-search-forward "^$" nil t) (point)))
140 (mime/prepare-decode-message-header)
141 (mime/decode-region (point-min) (point-max))
144 (defun mime/decode-string (str)
145 (let ((dest "")(ew nil)
147 (while (setq beg (string-match mime/encoded-word-regexp str))
149 (if (not (and (eq ew t) (string= (substring str 0 beg) " ")))
150 (setq dest (concat dest (substring str 0 beg)
154 (setq end (match-end 0))
155 (setq dest (concat dest (mime/decode-encoded-word (substring str beg end))
157 (setq str (substring str end))
163 ;;; @@ MIME header encoders
166 (defun mime/encode-string (string encoding &optional mode)
167 (cond ((equal encoding "B") (mime/base64-encode-string string))
168 ((equal encoding "Q") (mime/Quoted-Printable-encode-string string mode))
172 (defun mime/encode-field (str)
173 (setq str (message/unfolding-string str))
174 (let ((ret (message/divide-field str))
175 field-name field-body)
176 (setq field-name (car ret))
177 (setq field-body (nth 1 ret))
178 (concat field-name " "
179 (cond ((string= field-body "") "")
180 ((or (string-match "^Reply-To:$" field-name)
181 (string-match "^From:$" field-name)
182 (string-match "^Sender:$" field-name)
183 (string-match "^Resent-Reply-To:$" field-name)
184 (string-match "^Resent-From:$" field-name)
185 (string-match "^Resent-Sender:$" field-name)
186 (string-match "^To:$" field-name)
187 (string-match "^Resent-To:$" field-name)
188 (string-match "^cc:$" field-name)
189 (string-match "^Resent-cc:$" field-name)
190 (string-match "^bcc:$" field-name)
191 (string-match "^Resent-bcc:$" field-name)
193 (mime/encode-address-list
194 (+ (length field-name) 1) field-body)
198 (let ((r mime/no-encoding-header-fields) fn)
201 (if (string-match (concat "^" fn ":$") field-name)
202 (throw 'tag field-body)
206 (nth 1 (mime/encode-header-string
207 (+ (length field-name) 1) field-body))
212 (defun mime/encode-message-header ()
216 (narrow-to-region (goto-char (point-min))
219 (concat "^" (regexp-quote mail-header-separator) "$")
223 (goto-char (point-min))
225 (while (re-search-forward "^.+:.*\\(\n\\s +.*\\)*" nil t)
226 (setq beg (match-beginning 0))
227 (setq end (match-end 0))
228 (setq field (buffer-substring beg end))
229 (insert (mime/encode-field
231 (buffer-substring beg end)
232 (delete-region beg end)
235 (if mime/use-X-Nsubject
237 (goto-char (point-min))
238 (if (re-search-forward "^Subject:.*\\(\n\\s +.*\\)*" nil t)
239 (let ((str (buffer-substring (match-beginning 0)(match-end 0))))
240 (if (string-match mime/encoded-word-regexp str)
243 (nth 1 (message/divide-field
245 (message/unfolding-string str))
251 ;;; @ Base64 (B-encode) decoder/encoder
252 ;;; by Enami Tsugutomo
255 (defun mime/base64-decode-string (string)
256 (mime/base64-mapconcat (function mime/base64-decode-chars) 4 string))
258 ;; (mime/base64-encode-string (mime/base64-decode-string "GyRAOjRGI0stGyhK"))
259 (defun mime/base64-encode-string (string &optional mode)
260 (let ((es (mime/base64-mapconcat (function mime/base64-encode-chars) 3 string))
262 (setq m (mod (length es) 4))
269 ;; (char-to-string (mime/base64-bit-to-char 26))
270 (defun mime/base64-bit-to-char (n)
271 (cond ((eq n nil) ?=)
273 ((< n 52) (+ ?a (- n 26)))
274 ((< n 62) (+ ?0 (- n 52)))
277 (t (error "not a base64 integer %d" n))))
279 (defun mime/base64-char-to-bit (c)
280 (cond ((and (<= ?A c) (<= c ?Z)) (- c ?A))
281 ((and (<= ?a c) (<= c ?z)) (+ (- c ?a) 26))
282 ((and (<= ?0 c) (<= c ?9)) (+ (- c ?0) 52))
286 (t (error "not a base64 character %c" c))))
288 (defun mime/mask (i n) (logand i (1- (ash 1 n))))
290 (defun mime/base64-encode-1 (a &optional b &optional c)
292 (cons (logior (ash (mime/mask a 2) (- 6 2))
295 (cons (logior (ash (mime/mask b 4) (- 6 4))
298 (cons (mime/mask c (- 6 0))
301 (defun mime/base64-decode-1 (a b &optional c &optional d)
302 (cons (logior (ash a 2) (ash b (- 2 6)))
303 (if c (cons (logior (ash (mime/mask b 4) 4)
304 (mime/mask (ash c (- 4 6)) 4))
305 (if d (cons (logior (ash (mime/mask c 2) 6) d)
308 ;; (mime/base64-decode-chars ?G ?y ?R ?A)
309 (defun mime/base64-decode-chars (a b c d)
310 (apply (function mime/base64-decode-1)
311 (mapcar (function mime/base64-char-to-bit)
314 ;; (mapcar (function char-to-string) (mime/base64-encode-chars 27 36 64))
315 (defun mime/base64-encode-chars (a b c)
316 (mapcar (function mime/base64-bit-to-char) (mime/base64-encode-1 a b c)))
318 (defun mime/base64-fecth-from (func from pos len)
322 ret (cons (funcall func from (+ pos len)) ret)))
325 (defun mime/base64-fecth-from-buffer (from pos len)
326 (mime/base64-fecth-from (function (lambda (f p) (char-after p)))
329 (defun mime/base64-fecth-from-string (from pos len)
330 (mime/base64-fecth-from (function (lambda (f p)
331 (if (< p (length f)) (aref f p))))
334 (defun mime/base64-fecth (source pos len)
335 (cond ((stringp source) (mime/base64-fecth-from-string source pos len))
336 (t (mime/base64-fecth-from-buffer source pos len))))
338 (defun mime/base64-mapconcat (func unit string)
340 (while (< i (length string))
342 (apply (function concat)
344 (mapcar (function char-to-string)
345 (apply func (mime/base64-fecth string i unit)))))
349 ;;; @ Quoted-Printable (Q-encode) encoder/decoder
352 (defun mime/Quoted-Printable-decode-string (str)
357 (setq chr (elt str i))
361 (setq h (hex-char-to-number (elt str (+ i 1))))
362 (setq l (hex-char-to-number (elt str (+ i 2))))
363 (setq num (+ (* h 16) l))
364 (setq dest (concat dest (char-to-string num)))
368 (setq dest (concat dest (char-to-string chr)))
372 (setq dest (concat dest (char-to-string 32)))
376 (setq dest (concat dest (char-to-string chr)))
382 (defun mime/Quoted-Printable-encode-string (str &optional mode)
389 (setq chr (elt str i))
391 (setq dest (concat dest "_"))
396 (and (eq mode 'comment)
401 (and (eq mode 'phrase)
402 (not (string-match "[A-Za-z0-9!*+/=_---]"
403 (char-to-string chr)))
407 (setq dest (concat dest
409 (char-to-string (number-to-hex-char (/ chr 16)))
410 (char-to-string (number-to-hex-char (% chr 16)))
413 (t (setq dest (concat dest (char-to-string chr)))
419 ;;; @ functions for message header encoding
422 (defun mime/encode-and-split-string (n string charset encoding)
424 (len (length string))
425 (js (mime/convert-string-from-emacs string charset))
426 (cesl (+ (length charset) (length encoding) 6 ))
428 (setq ewl (mime/encoded-word-length js encoding))
431 (setq m (+ n ewl cesl))
434 (while (and (< i len)
435 (setq js (mime/convert-string-from-emacs
436 (substring string 0 i) charset))
437 (setq m (+ n (mime/encoded-word-length js encoding) cesl))
440 (setq i (+ i (char-bytes (elt string i))))
442 (setq js (mime/convert-string-from-emacs
443 (substring string 0 j) charset))
444 (setq m (+ n (mime/encoded-word-length js encoding) cesl))
445 (setq rest (substring string j))
450 (list m (concat "=?" charset "?" encoding "?"
451 (mime/encode-string js encoding)
456 (defun mime/encode-header-word (n string charset encoding)
457 (let (dest str ret m)
458 (if (null (setq ret (mime/encode-and-split-string n string charset encoding)))
461 (setq dest (nth 1 ret))
463 (setq str (nth 2 ret))
464 (while (and (stringp str)
465 (setq ret (mime/encode-and-split-string 1 str charset encoding))
467 (setq dest (concat dest "\n " (nth 1 ret)))
469 (setq str (nth 2 ret))
475 (defun mime/encode-header-string (n string &optional mode)
476 (let ((ssl (mime/separate-string-for-encoder string))
477 i len cell et w ew (dest "") b l)
478 (setq len (length ssl))
479 (setq cell (nth 0 ssl))
484 (if (> (+ n (string-width w)) 76)
486 (setq dest (concat dest "\n "))
490 (setq dest (concat dest w))
491 (setq b (+ b (string-width w)))
494 (setq ew (mime/encode-header-word n (cdr cell) (car et) (cdr et)))
495 (setq dest (nth 1 ew))
500 (setq cell (nth i ssl))
503 (cond ((string-match "^[ \t]*$" w)
504 (setq b (+ b (string-width (cdr cell))))
505 (setq dest (concat dest (cdr cell)))
508 (if (> (+ b (string-width w)) 76)
510 (if (eq (elt dest (- (length dest) 1)) 32)
511 (setq dest (substring dest 0 (- (length dest) 1)))
513 (setq dest (concat dest "\n " w))
514 (setq b (+ (length w) 1))
516 (setq l (length dest))
518 (eq (elt dest (- l 2)) ?\?)
519 (eq (elt dest (- l 1)) ?=)
522 (setq dest (concat dest " "))
525 (setq dest (concat dest w))
526 (setq b (+ b (string-width w)))
529 (if (not (eq (elt dest (- (length dest) 1)) 32))
531 (setq dest (concat dest " "))
534 (setq ew (mime/encode-header-word b (cdr cell) (car et) (cdr et)))
536 (if (string-match "^\n" (nth 1 ew))
537 (setq dest (concat (substring dest 0 (- (length dest) 1))
539 (setq dest (concat dest (nth 1 ew)))
546 (defun mime/encode-address-list (n str)
547 (let ((ret (message/parse-addresses str))
548 len (i 0) cell en-ret j cl (dest "") s)
549 (setq len (length ret))
551 (setq cell (nth i ret))
552 (cond ((string= (nth 1 cell) "<")
553 (setq en-ret (mime/encode-header-string n (nth 0 cell) 'phrase))
554 (setq dest (concat dest (nth 1 en-ret)))
555 (setq n (car en-ret))
558 (mime/encode-header-string
559 n (concat (nth 1 cell)(nth 2 cell)(nth 3 cell) ", ")))
560 (setq en-ret (mime/encode-header-string
561 n (concat (nth 1 cell)(nth 2 cell)(nth 3 cell))))
563 (if (and (eq (elt (nth 1 en-ret) 0) ?\n)
564 (eq (elt dest (- (length dest) 1)) 32))
565 (setq dest (substring dest 0 (- (length dest) 1)))
567 (setq dest (concat dest (nth 1 en-ret)))
568 (setq n (car en-ret))
571 (setq en-ret (mime/encode-header-string n (nth 0 cell)))
572 (setq dest (concat dest (nth 1 en-ret)))
573 (setq n (car en-ret))
575 (setq en-ret (mime/encode-header-string (+ n 2) (nth 2 cell) 'comment))
576 (if (eq (elt (nth 1 en-ret) 0) ?\n)
578 (setq dest (concat dest "\n ("))
579 (setq en-ret (mime/encode-header-string 2 (nth 2 cell) 'comment))
582 (setq dest (concat dest " ("))
584 (setq dest (concat dest (nth 1 en-ret)))
585 (setq n (car en-ret))
588 (mime/encode-header-string n (concat (nth 3 cell) ", ")))
589 (setq en-ret (mime/encode-header-string n (nth 3 cell)))
591 (setq dest (concat dest (nth 1 en-ret)))
592 (setq n (car en-ret))
597 (mime/encode-header-string n (concat (nth 0 cell) ", ")))
598 (setq en-ret (mime/encode-header-string n (nth 0 cell)))
600 (setq dest (concat dest (nth 1 en-ret)))
601 (setq n (car en-ret))
606 ;;; @ utility functions
610 (defun hex-char-to-number (chr)
611 (cond ((and (<= ?0 chr)(<= chr ?9)) (- chr ?0))
612 ((and (<= ?A chr)(<= chr ?F)) (+ (- chr ?A) 10))
613 ((and (<= ?a chr)(<= chr ?f)) (+ (- chr ?a) 10))
616 (defun number-to-hex-char (n)
622 ;;; @ utility for encoder
625 ;;; @@ encoded-word length
628 (defun mime/encoded-word-length (string encoding)
629 (cond ((equal encoding "B") (mime/base64-length string))
630 ((equal encoding "Q") (mime/Quoted-Printable-length string))
634 (defun mime/base64-length (string)
635 (let ((l (length string))
638 (if (= (mod l 3) 0) 0 1)
642 (defun mime/Quoted-Printable-length (string &optional mode)
643 (let ((l 0)(i 0)(len (length string)) chr)
645 (setq chr (elt string i))
646 (if (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))
653 ;;; @@ separate by character set
657 (defconst LC-space 2)
659 ;; by mol. 1993/10/16
660 (defun mime/char-type (chr)
661 (if (or (= chr 32)(= chr ?\t))
666 (defun mime/separate-string-by-chartype (string)
667 (let ((len (length string))
671 (progn (setq chr (elt string 0))
672 (setq pcs (mime/char-type chr))
673 (setq i (char-bytes chr))
674 (setq ds (substring string 0 i))
676 (setq chr (elt string i))
677 (setq cs (mime/char-type chr))
678 (setq j (+ i (char-bytes chr)))
679 (setq s (substring string i j))
682 (setq ds (concat ds s))
683 (progn (setq dest (append dest (list (cons pcs ds))))
688 (if (not (string= ds ""))
689 (setq dest (append dest (list (cons pcs ds)))))
693 (defun mime/separate-string-by-charset (str)
694 (let ((rl (mime/separate-string-by-chartype str))
695 (i 1) len (pcell nil) cell ncell dpcell (dest nil) LC)
696 (setq len (length rl))
697 (setq dpcell (list (nth 0 rl)))
698 (setq cell (nth 1 rl))
699 (setq ncell (nth 2 rl))
701 (setq LC (car (car dpcell)))
702 (cond ((and (not (eq LC lc-ascii))
703 (eq (car cell) LC-space)
704 (not (eq (car ncell) lc-ascii)))
705 (setq dpcell (list (cons LC
706 (concat (cdr (car dpcell)) (cdr cell))
709 ((and (not (eq LC lc-ascii))
711 (setq dpcell (list (cons LC
712 (concat (cdr (car dpcell)) (cdr cell))
715 ((and (eq LC lc-ascii)
716 (member (car cell) mime/latin-lc-list))
717 (setq dpcell (list (cons (car cell)
718 (concat (cdr (car dpcell)) (cdr cell))
721 ((and (member LC mime/latin-lc-list)
722 (eq (car cell) lc-ascii))
723 (setq dpcell (list (cons LC
724 (concat (cdr (car dpcell)) (cdr cell))
728 (setq dest (append dest dpcell))
729 (setq dpcell (list cell))
733 (setq ncell (nth (+ i 1) rl))
735 (setq dest (append dest dpcell))
738 (defun mime/separate-string-for-encoder (string)
740 (if (string-match "[ \t]+$" string)
742 (setq lastspace (substring string
745 (setq string (substring string 0 (match-beginning 0)))
747 (let ((rl (mime/separate-string-by-charset string))
748 (i 0) len cell0 cell1 cell2 (dest nil))
749 (setq len (length rl))
750 (setq cell0 (nth 0 rl))
751 (setq cell1 (nth 1 rl))
752 (setq cell2 (nth 2 rl))
754 (cond ((and (not (eq (car cell0) lc-ascii))
755 (eq (car cell1) LC-space)
756 (not (eq (car cell2) lc-ascii))
761 (cdr (assoc (car cell0)
762 mime/lc-charset-and-encoding-alist))
763 (concat (cdr cell0) (cdr cell1))
766 (setq cell0 (nth i rl))
767 (setq cell1 (nth (+ i 1) rl))
768 (setq cell2 (nth (+ i 2) rl))
774 (cdr (assoc (car cell0)
775 mime/lc-charset-and-encoding-alist))
780 (setq cell2 (nth (+ i 2) rl))
785 (list (cons nil lastspace))))
791 ;;; basic functions for MIME header decoder
794 ;;; @ utility for decoder
797 (defun mime/unfolding ()
798 (goto-char (point-min))
800 (while (re-search-forward message/field-regexp nil t)
801 (setq beg (match-beginning 0))
802 (setq end (match-end 0))
803 (setq field (buffer-substring beg end))
804 (if (string-match mime/encoded-word-regexp field)
808 (narrow-to-region (goto-char beg) end)
809 (while (re-search-forward "\n[ \t]+" nil t)
817 (defun mime/prepare-decode-message-header ()
819 (goto-char (point-min))
820 (while (re-search-forward
821 (concat (regexp-quote "?=")
825 (replace-match "?==?")
829 (run-hooks 'mime/tiny-mime-load-hook)
834 ;;; mode: outline-minor
835 ;;; outline-regexp: ";;; @+\\|(......"