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
20 "$Id: tiny-mime.el,v 5.18 1995/08/26 18:38:37 morioka Exp $")
22 (defconst mime/tiny-mime-version (get-version-string mime/RCS-ID))
25 ;;; @ MIME encoded-word definition
28 (defconst mime/charset-regexp "[A-Za-z0-9!#$%&'*+---^_`{}|~]")
29 (defconst mime/encoded-text-regexp "[!->@-~]+")
31 (defconst mime/Base64-token-regexp "[A-Za-z0-9+/=]")
32 (defconst mime/Base64-encoded-text-regexp
34 mime/Base64-token-regexp
35 mime/Base64-token-regexp
36 mime/Base64-token-regexp
37 mime/Base64-token-regexp
39 (defconst mime/Base64-encoding-and-encoded-text-regexp
40 (concat "\\(B\\)\\?" mime/Base64-encoded-text-regexp))
42 (defconst mime/Quoted-Printable-hex-char-regexp "[0123456789ABCDEF]")
43 (defconst mime/Quoted-Printable-octet-regexp
45 mime/Quoted-Printable-hex-char-regexp
46 mime/Quoted-Printable-hex-char-regexp))
47 (defconst mime/Quoted-Printable-encoded-text-regexp
48 (concat "\\([^=?]\\|" mime/Quoted-Printable-octet-regexp "\\)+"))
49 (defconst mime/Quoted-Printable-encoding-and-encoded-text-regexp
50 (concat "\\(Q\\)\\?" mime/Quoted-Printable-encoded-text-regexp))
52 (defconst mime/encoded-word-regexp (concat (regexp-quote "=?")
60 mime/encoded-text-regexp
64 (defun mime/nth-string (s n)
66 (substring s (match-beginning n) (match-end n))
67 (buffer-substring (match-beginning n) (match-end n))))
69 (defun mime/encoded-word-charset (str)
70 (mime/nth-string str 1))
72 (defun mime/encoded-word-encoding (str)
73 (mime/nth-string str 2))
75 (defun mime/encoded-word-encoded-text (str)
76 (mime/nth-string str 3))
78 (defun mime/rest-of-string (str)
80 (substring str (match-end 0))
81 (buffer-substring (match-end 0)(point-max))
88 (defvar mime/no-encoding-header-fields '("X-Nsubject" "Newsgroups"))
90 (defvar mime/use-X-Nsubject nil)
93 ;;; @ compatible module among Mule, NEmacs and NEpoch
95 (cond ((boundp 'MULE) (require 'tm-mule))
96 ((boundp 'NEMACS)(require 'tm-nemacs))
97 (t (require 'tm-orig))
101 ;;; @ Application Interface
104 ;;; @@ MIME header decoders
108 (defun mime/decode-encoded-word (word)
109 (if (string-match mime/encoded-word-regexp word)
110 (let ((charset (upcase (mime/encoded-word-charset word)))
111 (encoding (mime/encoded-word-encoding word))
112 (text (mime/encoded-word-encoded-text word)))
113 (mime/decode-encoded-text charset encoding text))
116 (defun mime/decode-region (beg end)
120 (narrow-to-region beg end)
121 (goto-char (point-min))
122 (let (charset encoding text)
123 (while (re-search-forward mime/encoded-word-regexp nil t)
124 (insert (mime/decode-encoded-word
126 (buffer-substring (match-beginning 0) (match-end 0))
127 (delete-region (match-beginning 0) (match-end 0))
133 (defun mime/decode-message-header ()
137 (narrow-to-region (goto-char (point-min))
138 (progn (re-search-forward "^$" nil t) (point)))
139 (mime/prepare-decode-message-header)
140 (mime/decode-region (point-min) (point-max))
143 (defun mime/decode-string (str)
144 (let ((dest "")(ew nil)
146 (while (setq beg (string-match mime/encoded-word-regexp str))
148 (if (not (and (eq ew t) (string= (substring str 0 beg) " ")))
149 (setq dest (concat dest (substring str 0 beg)
153 (setq end (match-end 0))
154 (setq dest (concat dest (mime/decode-encoded-word (substring str beg end))
156 (setq str (substring str end))
162 ;;; @@ MIME header encoders
165 (defun mime/encode-string (string encoding &optional mode)
166 (cond ((equal encoding "B") (mime/base64-encode-string string))
167 ((equal encoding "Q") (mime/Quoted-Printable-encode-string string mode))
171 (defun mime/encode-field (str)
172 (setq str (message/unfolding-string str))
173 (let ((ret (message/divide-field str))
174 field-name field-body)
175 (setq field-name (car ret))
176 (setq field-body (nth 1 ret))
177 (concat field-name " "
178 (cond ((string= field-body "") "")
179 ((or (string-match "^Reply-To:$" field-name)
180 (string-match "^From:$" field-name)
181 (string-match "^Sender:$" field-name)
182 (string-match "^Resent-Reply-To:$" field-name)
183 (string-match "^Resent-From:$" field-name)
184 (string-match "^Resent-Sender:$" field-name)
185 (string-match "^To:$" field-name)
186 (string-match "^Resent-To:$" field-name)
187 (string-match "^cc:$" field-name)
188 (string-match "^Resent-cc:$" field-name)
189 (string-match "^bcc:$" field-name)
190 (string-match "^Resent-bcc:$" field-name)
192 (mime/encode-address-list
193 (+ (length field-name) 1) field-body)
197 (let ((r mime/no-encoding-header-fields) fn)
200 (if (string-match (concat "^" fn ":$") field-name)
201 (throw 'tag field-body)
205 (nth 1 (mime/encode-header-string
206 (+ (length field-name) 1) field-body))
211 (defun mime/encode-message-header ()
215 (narrow-to-region (goto-char (point-min))
218 (concat "^" (regexp-quote mail-header-separator) "$")
222 (goto-char (point-min))
224 (while (re-search-forward "^.+:.*\\(\n\\s +.*\\)*" nil t)
225 (setq beg (match-beginning 0))
226 (setq end (match-end 0))
227 (setq field (buffer-substring beg end))
228 (insert (mime/encode-field
230 (buffer-substring beg end)
231 (delete-region beg end)
234 (if mime/use-X-Nsubject
236 (goto-char (point-min))
237 (if (re-search-forward "^Subject:.*\\(\n\\s +.*\\)*" nil t)
238 (let ((str (buffer-substring (match-beginning 0)(match-end 0))))
239 (if (string-match mime/encoded-word-regexp str)
242 (nth 1 (message/divide-field
244 (message/unfolding-string str))
250 ;;; @ Base64 (B-encode) decoder/encoder
251 ;;; by Enami Tsugutomo
254 (defun mime/base64-decode-string (string)
255 (mime/base64-mapconcat (function mime/base64-decode-chars) 4 string))
257 ;; (mime/base64-encode-string (mime/base64-decode-string "GyRAOjRGI0stGyhK"))
258 (defun mime/base64-encode-string (string &optional mode)
259 (let ((es (mime/base64-mapconcat (function mime/base64-encode-chars) 3 string))
261 (setq m (mod (length es) 4))
268 ;; (char-to-string (mime/base64-bit-to-char 26))
269 (defun mime/base64-bit-to-char (n)
270 (cond ((eq n nil) ?=)
272 ((< n 52) (+ ?a (- n 26)))
273 ((< n 62) (+ ?0 (- n 52)))
276 (t (error "not a base64 integer %d" n))))
278 (defun mime/base64-char-to-bit (c)
279 (cond ((and (<= ?A c) (<= c ?Z)) (- c ?A))
280 ((and (<= ?a c) (<= c ?z)) (+ (- c ?a) 26))
281 ((and (<= ?0 c) (<= c ?9)) (+ (- c ?0) 52))
285 (t (error "not a base64 character %c" c))))
287 (defun mime/mask (i n) (logand i (1- (ash 1 n))))
289 (defun mime/base64-encode-1 (a &optional b &optional c)
291 (cons (logior (ash (mime/mask a 2) (- 6 2))
294 (cons (logior (ash (mime/mask b 4) (- 6 4))
297 (cons (mime/mask c (- 6 0))
300 (defun mime/base64-decode-1 (a b &optional c &optional d)
301 (cons (logior (ash a 2) (ash b (- 2 6)))
302 (if c (cons (logior (ash (mime/mask b 4) 4)
303 (mime/mask (ash c (- 4 6)) 4))
304 (if d (cons (logior (ash (mime/mask c 2) 6) d)
307 ;; (mime/base64-decode-chars ?G ?y ?R ?A)
308 (defun mime/base64-decode-chars (a b c d)
309 (apply (function mime/base64-decode-1)
310 (mapcar (function mime/base64-char-to-bit)
313 ;; (mapcar (function char-to-string) (mime/base64-encode-chars 27 36 64))
314 (defun mime/base64-encode-chars (a b c)
315 (mapcar (function mime/base64-bit-to-char) (mime/base64-encode-1 a b c)))
317 (defun mime/base64-fecth-from (func from pos len)
321 ret (cons (funcall func from (+ pos len)) ret)))
324 (defun mime/base64-fecth-from-buffer (from pos len)
325 (mime/base64-fecth-from (function (lambda (f p) (char-after p)))
328 (defun mime/base64-fecth-from-string (from pos len)
329 (mime/base64-fecth-from (function (lambda (f p)
330 (if (< p (length f)) (aref f p))))
333 (defun mime/base64-fecth (source pos len)
334 (cond ((stringp source) (mime/base64-fecth-from-string source pos len))
335 (t (mime/base64-fecth-from-buffer source pos len))))
337 (defun mime/base64-mapconcat (func unit string)
339 (while (< i (length string))
341 (apply (function concat)
343 (mapcar (function char-to-string)
344 (apply func (mime/base64-fecth string i unit)))))
348 ;;; @ Quoted-Printable (Q-encode) encoder/decoder
351 (defun mime/Quoted-Printable-decode-string (str)
356 (setq chr (elt str i))
360 (setq h (hex-char-to-number (elt str (+ i 1))))
361 (setq l (hex-char-to-number (elt str (+ i 2))))
362 (setq num (+ (* h 16) l))
363 (setq dest (concat dest (char-to-string num)))
367 (setq dest (concat dest (char-to-string chr)))
371 (setq dest (concat dest (char-to-string 32)))
375 (setq dest (concat dest (char-to-string chr)))
381 (defun mime/Quoted-Printable-encode-string (str &optional mode)
388 (setq chr (elt str i))
390 (setq dest (concat dest "_"))
395 (and (eq mode 'comment)
400 (and (eq mode 'phrase)
401 (not (string-match "[A-Za-z0-9!*+/=_---]"
402 (char-to-string chr)))
406 (setq dest (concat dest
408 (char-to-string (number-to-hex-char (/ chr 16)))
409 (char-to-string (number-to-hex-char (% chr 16)))
412 (t (setq dest (concat dest (char-to-string chr)))
418 ;;; @ functions for message header encoding
421 (defun mime/encode-and-split-string (n string charset encoding)
423 (len (length string))
424 (js (mime/convert-string-from-emacs string charset))
425 (cesl (+ (length charset) (length encoding) 6 ))
427 (setq ewl (mime/encoded-word-length js encoding))
430 (setq m (+ n ewl cesl))
433 (while (and (< i len)
434 (setq js (mime/convert-string-from-emacs
435 (substring string 0 i) charset))
436 (setq m (+ n (mime/encoded-word-length js encoding) cesl))
439 (setq i (+ i (char-bytes (elt string i))))
441 (setq js (mime/convert-string-from-emacs
442 (substring string 0 j) charset))
443 (setq m (+ n (mime/encoded-word-length js encoding) cesl))
444 (setq rest (substring string j))
449 (list m (concat "=?" charset "?" encoding "?"
450 (mime/encode-string js encoding)
455 (defun mime/encode-header-word (n string charset encoding)
456 (let (dest str ret m)
457 (if (null (setq ret (mime/encode-and-split-string n string charset encoding)))
460 (setq dest (nth 1 ret))
462 (setq str (nth 2 ret))
463 (while (and (stringp str)
464 (setq ret (mime/encode-and-split-string 1 str charset encoding))
466 (setq dest (concat dest "\n " (nth 1 ret)))
468 (setq str (nth 2 ret))
474 (defun mime/encode-header-string (n string &optional mode)
475 (if (string= string "")
477 (let ((ssl (mime/separate-string-for-encoder string))
478 i len cell et w ew (dest "") b l)
479 (setq len (length ssl))
480 (setq cell (nth 0 ssl))
482 ;; string-width crashes when the argument is nil,
483 ;; so replace the argument
484 ;; (original modification by Kenji Rikitake 9-JAN-1995)
485 (setq w (or (cdr cell) ""))
488 (if (> (+ n (string-width w)) 76)
490 (setq dest (concat dest "\n "))
494 (setq dest (concat dest w))
495 (setq b (+ b (string-width w)))
498 (setq ew (mime/encode-header-word n (cdr cell) (car et) (cdr et)))
499 (setq dest (nth 1 ew))
504 (setq cell (nth i ssl))
507 (cond ((string-match "^[ \t]*$" w)
508 (setq b (+ b (string-width (cdr cell))))
509 (setq dest (concat dest (cdr cell)))
512 (if (> (+ b (string-width w)) 76)
514 (if (eq (elt dest (- (length dest) 1)) 32)
515 (setq dest (substring dest 0 (- (length dest) 1)))
517 (setq dest (concat dest "\n " w))
518 (setq b (+ (length w) 1))
520 (setq l (length dest))
522 (eq (elt dest (- l 2)) ?\?)
523 (eq (elt dest (- l 1)) ?=)
526 (setq dest (concat dest " "))
529 (setq dest (concat dest w))
530 (setq b (+ b (string-width w)))
533 (if (not (eq (elt dest (- (length dest) 1)) 32))
535 (setq dest (concat dest " "))
539 (mime/encode-header-word b (cdr cell) (car et) (cdr et)))
541 (if (string-match "^\n" (nth 1 ew))
542 (setq dest (concat (substring dest 0 (- (length dest) 1))
544 (setq dest (concat dest (nth 1 ew)))
552 (defun mime/encode-address-list (n str)
553 (let* ((ret (message/parse-addresses str))
554 (r ret) cell en-ret j cl (dest "") s)
557 (cond ((string= (nth 1 cell) "<")
558 (setq en-ret (mime/encode-header-string n (nth 0 cell) 'phrase))
559 (setq dest (concat dest (nth 1 en-ret)))
560 (setq n (car en-ret))
563 (mime/encode-header-string
564 n (concat (nth 1 cell)(nth 2 cell)(nth 3 cell) ", ")))
565 (setq en-ret (mime/encode-header-string
566 n (concat (nth 1 cell)(nth 2 cell)(nth 3 cell))))
568 (if (and (eq (elt (nth 1 en-ret) 0) ?\n)
569 (eq (elt dest (- (length dest) 1)) 32))
570 (setq dest (substring dest 0 (- (length dest) 1)))
572 (setq dest (concat dest (nth 1 en-ret)))
573 (setq n (car en-ret))
576 (setq en-ret (mime/encode-header-string n (nth 0 cell)))
577 (setq dest (concat dest (nth 1 en-ret)))
578 (setq n (car en-ret))
580 (setq en-ret (mime/encode-header-string (+ n 2) (nth 2 cell)
582 (if (eq (elt (nth 1 en-ret) 0) ?\n)
584 (setq dest (concat dest "\n ("))
585 (setq en-ret (mime/encode-header-string 2 (nth 2 cell)
589 (setq dest (concat dest " ("))
591 (setq dest (concat dest (nth 1 en-ret)))
592 (setq n (car en-ret))
595 (mime/encode-header-string n (concat (nth 3 cell) ", "))
597 (setq en-ret (mime/encode-header-string n (nth 3 cell)))
599 (setq dest (concat dest (nth 1 en-ret)))
600 (setq n (car en-ret))
605 (mime/encode-header-string n (concat (nth 0 cell) ", "))
607 (setq en-ret (mime/encode-header-string n (nth 0 cell)))
609 (setq dest (concat dest (nth 1 en-ret)))
610 (setq n (car en-ret))
617 ;;; @ utility for encoder
620 ;;; @@ encoded-word length
623 (defun mime/encoded-word-length (string encoding)
624 (cond ((equal encoding "B") (mime/base64-length string))
625 ((equal encoding "Q") (mime/Quoted-Printable-length string))
629 (defun mime/base64-length (string)
630 (let ((l (length string))
633 (if (= (mod l 3) 0) 0 1)
637 (defun mime/Quoted-Printable-length (string &optional mode)
638 (let ((l 0)(i 0)(len (length string)) chr)
640 (setq chr (elt string i))
641 (if (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))
648 ;;; @@ separate by character set
652 (defconst LC-space 2)
654 ;; by mol. 1993/10/16
655 (defun mime/char-type (chr)
656 (if (or (= chr 32)(= chr ?\t))
661 (defun mime/separate-string-by-chartype (string)
662 (let ((len (length string))
667 (setq chr (elt string 0))
668 (setq pcs (mime/char-type chr))
669 (setq i (char-bytes chr))
670 (setq ds (substring string 0 i))
672 (setq chr (elt string i))
673 (setq cs (mime/char-type chr))
674 (setq j (+ i (char-bytes chr)))
675 (setq s (substring string i j))
678 (setq ds (concat ds s))
679 (progn (setq dest (append dest (list (cons pcs ds))))
684 (if (not (string= ds ""))
685 (setq dest (append dest (list (cons pcs ds)))))
689 (defun mime/separate-string-by-charset (str)
690 (let ((rl (mime/separate-string-by-chartype str))
691 (i 1) len (pcell nil) cell ncell dpcell (dest nil) LC)
692 (setq len (length rl))
693 (setq dpcell (list (nth 0 rl)))
694 (setq cell (nth 1 rl))
695 (setq ncell (nth 2 rl))
697 (setq LC (car (car dpcell)))
698 (cond ((and (not (eq LC lc-ascii))
699 (eq (car cell) LC-space)
700 (not (eq (car ncell) lc-ascii)))
701 (setq dpcell (list (cons LC
702 (concat (cdr (car dpcell)) (cdr cell))
705 ((and (not (eq LC lc-ascii))
707 (setq dpcell (list (cons LC
708 (concat (cdr (car dpcell)) (cdr cell))
711 ((and (eq LC lc-ascii)
712 (member (car cell) mime/latin-lc-list))
713 (setq dpcell (list (cons (car cell)
714 (concat (cdr (car dpcell)) (cdr cell))
717 ((and (member LC mime/latin-lc-list)
718 (eq (car cell) lc-ascii))
719 (setq dpcell (list (cons LC
720 (concat (cdr (car dpcell)) (cdr cell))
724 (setq dest (append dest dpcell))
725 (setq dpcell (list cell))
729 (setq ncell (nth (+ i 1) rl))
731 (setq dest (append dest dpcell))
734 (defun mime/separate-string-for-encoder (string)
736 (if (string-match "[ \t]+$" string)
738 (setq lastspace (substring string
741 (setq string (substring string 0 (match-beginning 0)))
743 (let ((rl (mime/separate-string-by-charset string))
744 (i 0) len cell0 cell1 cell2 (dest nil))
745 (setq len (length rl))
746 (setq cell0 (nth 0 rl))
747 (setq cell1 (nth 1 rl))
748 (setq cell2 (nth 2 rl))
750 (cond ((and (not (eq (car cell0) lc-ascii))
751 (eq (car cell1) LC-space)
752 (not (eq (car cell2) lc-ascii))
757 (cdr (assoc (car cell0)
758 mime/lc-charset-and-encoding-alist))
759 (concat (cdr cell0) (cdr cell1))
762 (setq cell0 (nth i rl))
763 (setq cell1 (nth (+ i 1) rl))
764 (setq cell2 (nth (+ i 2) rl))
770 (cdr (assoc (car cell0)
771 mime/lc-charset-and-encoding-alist))
776 (setq cell2 (nth (+ i 2) rl))
781 (list (cons nil lastspace))))
787 ;;; basic functions for MIME header decoder
790 ;;; @ utility for decoder
793 (defun mime/unfolding ()
794 (goto-char (point-min))
796 (while (re-search-forward message/field-name-regexp nil t)
797 (setq beg (match-beginning 0))
799 (if (re-search-forward "\n[!-9;-~]+:" nil t)
800 (goto-char (match-beginning 0))
801 (if (re-search-forward "^$" nil t)
802 (goto-char (1- (match-beginning 0)))
807 (setq field (buffer-substring beg end))
808 (if (string-match mime/encoded-word-regexp field)
810 (narrow-to-region (goto-char beg) end)
811 (while (re-search-forward "\n[ \t]+" nil t)
814 (goto-char (point-max))
818 (defun mime/prepare-decode-message-header ()
820 (goto-char (point-min))
821 (while (re-search-forward
822 (concat (regexp-quote "?=")
826 (replace-match "?==?")
830 (run-hooks 'mime/tiny-mime-load-hook)
837 ;;; mode: outline-minor
838 ;;; outline-regexp: ";;; @+\\|(......"