1 ;;; mime-edit.el --- Simple MIME Composer for GNU Emacs
3 ;; Copyright (C) 1993,94,95,96,97,98,99,2000 Free Software Foundation, Inc.
5 ;; Author: UMEDA Masanobu <umerin@mse.kyutech.ac.jp>
6 ;; MORIOKA Tomohiko <tomo@kanji.zinbun.kyoto-u.ac.jp>
7 ;; Daiki Ueno <ueno@unixuser.org>
8 ;; Created: 1994/08/21 renamed from mime.el
9 ;; Renamed: 1997/2/21 from tm-edit.el
10 ;; Keywords: MIME, multimedia, multilingual, mail, news
12 ;; This file is part of SEMI (Sophisticated Emacs MIME Interfaces).
14 ;; This program is free software; you can redistribute it and/or
15 ;; modify it under the terms of the GNU General Public License as
16 ;; published by the Free Software Foundation; either version 2, or (at
17 ;; your option) any later version.
19 ;; This program is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ;; General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
31 ;; This is an Emacs minor mode for editing Internet multimedia
32 ;; messages formatted in MIME (RFC 2045, 2046, 2047, 2048 and 2049).
33 ;; All messages in this mode are composed in the tagged MIME format,
34 ;; that are described in the following examples. The messages
35 ;; composed in the tagged MIME format are automatically translated
36 ;; into a MIME compliant message when exiting the mode.
38 ;; Mule (multilingual feature of Emacs 20 and multilingual extension
39 ;; for XEmacs 20) has a capability of handling multilingual text in
40 ;; limited ISO-2022 manner that is based on early experiences in
41 ;; Japanese Internet community and resulted in RFC 1468 (ISO-2022-JP
42 ;; charset for MIME). In order to enable multilingual capability in
43 ;; single text message in MIME, charset of multilingual text written
44 ;; in Mule is declared as either `ISO-2022-JP-2' [RFC 1554]. Mule is
45 ;; required for reading the such messages.
47 ;; This MIME composer can work with Mail mode, mh-e letter Mode, and
48 ;; News mode. First of all, you need the following autoload
49 ;; definition to load mime-edit-mode automatically:
51 ;; (autoload 'turn-on-mime-edit "mime-edit"
52 ;; "Minor mode for editing MIME message." t)
54 ;; In case of Mail mode (includes VM mode), you need the following
57 ;; (add-hook 'mail-mode-hook 'turn-on-mime-edit)
58 ;; (add-hook 'mail-send-hook 'mime-edit-maybe-translate)
60 ;; In case of MH-E, you need the following hook definition:
62 ;; (add-hook 'mh-letter-mode-hook
65 ;; (turn-on-mime-edit)
66 ;; (make-local-variable 'mail-header-separator)
67 ;; (setq mail-header-separator "--------")
69 ;; (add-hook 'mh-before-send-letter-hook 'mime-edit-maybe-translate)
71 ;; In case of News mode, you need the following hook definition:
73 ;; (add-hook 'news-reply-mode-hook 'turn-on-mime-edit)
74 ;; (add-hook 'news-inews-hook 'mime-edit-maybe-translate)
76 ;; In case of Emacs 19, it is possible to emphasize the message tags
77 ;; using font-lock mode as follows:
79 ;; (add-hook 'mime-edit-mode-hook
83 ;; (setq font-lock-keywords (list mime-edit-tag-regexp))
86 ;; The message tag looks like:
88 ;; --[[TYPE/SUBTYPE;PARAMETERS][ENCODING]]
90 ;; The tagged MIME message examples:
92 ;; This is a conventional plain text. It should be translated into
96 ;; This is also a plain text. But, it is explicitly specified as is.
97 ;;--[[text/plain; charset=ISO-8859-1]]
98 ;; This is also a plain text. But charset is specified as iso-8859-1.
100 ;; ¡Hola! Buenos días. ¿Cómo está usted?
101 ;;--[[text/enriched]]
102 ;; <center>This is a richtext.</center>
104 ;;--[[image/gif][base64]]^M...image encoded in base64 comes here...
106 ;;--[[audio/basic][base64]]^M...audio encoded in base64 comes here...
111 (require 'mail-utils)
117 (defvar mime-edit-pgp-use
120 (require 'epg-config)
121 (epg-check-configuration (epg-configuration))
122 (autoload 'epg-make-context "epg")
127 (autoload 'pgg-encrypt-region "pgg"
128 "PGP encryption of current region." t)
129 (autoload 'pgg-sign-region "pgg"
130 "PGP signature of current region." t)
131 (autoload 'pgg-insert-key "pgg"
132 "Insert PGP public key at point." t)
134 "Which PGG library to be used in MIME-Edit.")
140 (defconst mime-edit-version
142 (mime-product-name mime-user-interface-product) " "
143 (mapconcat #'number-to-string
144 (mime-product-version mime-user-interface-product) ".")
145 " - \"" (mime-product-code-name mime-user-interface-product) "\"")))
151 (defgroup mime-edit nil
155 (defcustom mime-ignore-preceding-spaces nil
156 "*Ignore preceding white spaces if non-nil."
160 (defcustom mime-ignore-trailing-spaces nil
161 "*Ignore trailing white spaces if non-nil."
165 (defcustom mime-ignore-same-text-tag t
166 "*Ignore preceding text content-type tag that is same with new one.
167 If non-nil, the text tag is not inserted unless something different."
171 (defcustom mime-auto-hide-body t
172 "*Hide non-textual body encoded in base64 after insertion if non-nil."
176 (defcustom mime-edit-voice-recorder
177 (function mime-edit-voice-recorder-for-sun)
178 "*Function to record a voice message and encode it."
182 (defcustom mime-edit-mode-hook nil
183 "*Hook called when enter MIME mode."
187 (defcustom mime-edit-translate-hook nil
188 "*Hook called before translating into a MIME compliant message.
189 To insert a signature file automatically, call the function
190 `mime-edit-insert-signature' from this hook."
194 (defcustom mime-edit-exit-hook nil
195 "*Hook called when exit MIME mode."
199 (defvar mime-content-types
201 ;; Charset parameter need not to be specified, since it is
202 ;; defined automatically while translation.
204 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
217 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
218 ("directory" "/pub/GNU/elisp/mime")
220 ("mode" "image" "ascii" "local8"))
225 ("mode" "image" "ascii" "local8"))
226 ("tftp" ("site") ("name"))
227 ("afs" ("site") ("name"))
228 ("local-file" ("site") ("name"))
230 ("server" "ftpmail@nic.karrn.ad.jp")
236 ("octet-stream" ("type" "" "tar" "shar"))
241 ("vnd.ms-powerpoint")
242 ("x-kiss" ("x-cnf")))
254 "*Alist of content-type, subtype, parameters and its values.")
256 (defcustom mime-file-types
259 ;; Programming languages
262 "application" "octet-stream" (("type" . "C++"))
264 "attachment" (("filename" . file)))
267 "application" "octet-stream" (("type" . "emacs-lisp"))
269 "attachment" (("filename" . file)))
272 "application" "octet-stream" (("type" . "common-lisp"))
274 "attachment" (("filename" . file)))
277 "application" "octet-stream" (("type" . "perl"))
279 "attachment" (("filename" . file)))
281 ;; Text or translated text
286 "inline" (("filename" . file)))
288 ;; .rc : procmail modules pm-xxxx.rc
289 ;; *rc : other resource files
291 ("\\.\\(rc\\|lst\\|log\\|sql\\|mak\\)$\\|\\..*rc$"
294 "attachment" (("filename" . file)))
301 ("\\.diff$\\|\\.patch$"
302 "application" "octet-stream" (("type" . "patch"))
304 "attachment" (("filename" . file)))
307 "text" "plain" nil nil nil nil)
310 ;; Octect binary text
313 "application" "msword" nil
315 "attachment" (("filename" . file)))
316 ("\\.xls$" ; MS Excel
317 "application" "vnd.ms-excel" nil
319 "attachment" (("filename" . file)))
320 ("\\.ppt$" ; MS Power Point
321 "application" "vnd.ms-powerpoint" nil
323 "attachment" (("filename" . file)))
328 "inline" (("filename" . file)))
330 "application" "postscript" nil
332 "attachment" (("filename" . file)))
334 "application" "pdf" nil
336 "attachment" (("filename" . file)))
340 ("\\.jpg$\\|\\.jpeg$"
343 "inline" (("filename" . file)))
347 "inline" (("filename" . file)))
351 "inline" (("filename" . file)))
355 "inline" (("filename" . file)))
359 "inline" (("filename" . file)))
363 "inline" (("filename" . file)))
367 "inline" (("filename" . file)))
371 "inline" (("filename" . file)))
375 "attachment" (("filename" . file)))
379 "attachment" (("filename" . file)))
381 "application" "octet-stream" (("type" . "tar+gzip"))
383 "attachment" (("filename" . file)))
385 "application" "octet-stream" (("type" . "tar+gzip"))
387 "attachment" (("filename" . file)))
389 "application" "octet-stream" (("type" . "tar+compress"))
391 "attachment" (("filename" . file)))
393 "application" "octet-stream" (("type" . "tar+compress"))
395 "attachment" (("filename" . file)))
397 "application" "octet-stream" (("type" . "gzip"))
399 "attachment" (("filename" . file)))
401 "application" "octet-stream" (("type" . "compress"))
403 "attachment" (("filename" . file)))
405 "application" "octet-stream" (("type" . "lha"))
407 "attachment" (("filename" . file)))
409 "application" "zip" nil
411 "attachment" (("filename" . file)))
416 "application" "octet-stream" nil
418 "attachment" (("filename" . file))))
419 "*Alist of file name, types, parameters, and default encoding.
420 If encoding is nil, it is determined from its contents."
424 (choice :tag "Primary-Type"
425 ,@(nconc (mapcar (lambda (cell)
426 (list 'item (car cell)))
430 (choice :tag "Sub-Type"
433 (mapcar (lambda (cell)
434 (mapcar (lambda (cell)
435 (list 'item (car cell)))
440 (repeat :tag "Parameters of Content-Type field"
441 (cons string (choice string symbol)))
442 ;; content-transfer-encoding
443 (choice :tag "Encoding"
446 (mapcar (lambda (cell)
448 (mime-encoding-list))))
450 (choice :tag "Disposition-Type"
456 (repeat :tag "Parameters of Content-Disposition field"
457 (cons string (choice string symbol)))))
461 ;;; @@ about charset, encoding and transfer-level
464 (defvar mime-charset-type-list
466 (iso-8859-1 8 "quoted-printable")
467 (iso-8859-2 8 "quoted-printable")
468 (iso-8859-3 8 "quoted-printable")
469 (iso-8859-4 8 "quoted-printable")
470 (iso-8859-5 8 "quoted-printable")
471 (koi8-r 8 "quoted-printable")
472 (iso-8859-7 8 "quoted-printable")
473 (iso-8859-8 8 "quoted-printable")
474 (iso-8859-9 8 "quoted-printable")
475 (iso-8859-14 8 "quoted-printable")
476 (iso-8859-15 8 "quoted-printable")
477 (iso-2022-jp 7 "base64")
478 (iso-2022-jp-3 7 "base64")
479 (iso-2022-kr 7 "base64")
485 (shift_jis 8 "base64")
487 (iso-2022-jp-2 7 "base64")
488 (iso-2022-int-1 7 "base64")))
490 (defvar mime-transfer-level 7
491 "*A number of network transfer level. It should be bigger than 7.")
492 (make-variable-buffer-local 'mime-transfer-level)
494 (defsubst mime-encoding-name (transfer-level &optional not-omit)
495 (cond ((> transfer-level 8) "binary")
496 ((= transfer-level 8) "8bit")
499 (defvar mime-transfer-level-string
500 (mime-encoding-name mime-transfer-level 'not-omit)
501 "A string formatted version of mime-transfer-level")
502 (make-variable-buffer-local 'mime-transfer-level-string)
504 ;;; @@ about content transfer encoding
506 (defvar mime-content-transfer-encoding-priority-list
507 '(nil "8bit" "binary"))
509 ;;; @@ about message inserting
512 (defvar mime-edit-yank-ignored-field-list
513 '("Received" "Approved" "Path" "Replied" "Status"
514 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
515 "Delete these fields from original message when it is inserted
516 as message/rfc822 part.
517 Each elements are regexp of field-name.")
519 (defvar mime-edit-yank-ignored-field-regexp
521 (apply (function regexp-or) mime-edit-yank-ignored-field-list)
524 (defvar mime-edit-message-inserter-alist nil)
525 (defvar mime-edit-mail-inserter-alist nil)
528 ;;; @@ about message splitting
531 (defcustom mime-edit-split-message t
532 "*Split large message if it is non-nil."
536 (defcustom mime-edit-message-default-max-lines 1000
537 "*Default maximum lines of a message."
541 (defcustom mime-edit-message-max-lines-alist
542 '((news-reply-mode . 500))
543 "Alist of major-mode vs maximum lines of a message.
544 If it is not specified for a major-mode,
545 `mime-edit-message-default-max-lines' is used."
549 (defconst mime-edit-split-ignored-field-regexp
550 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|^Message-Id:\\)")
552 (defcustom mime-edit-split-blind-field-regexp
553 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
554 "*Regular expression to match field-name to be ignored when split sending."
558 (defvar mime-edit-split-message-sender-alist nil)
560 (defvar mime-edit-news-reply-mode-server-running nil)
566 (defconst mime-edit-single-part-tag-regexp
567 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
568 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
570 (defconst mime-edit-quoted-single-part-tag-regexp
571 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
573 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
575 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
577 (defconst mime-edit-beginning-tag-regexp
578 (regexp-or mime-edit-single-part-tag-regexp
579 mime-edit-multipart-beginning-regexp))
581 (defconst mime-edit-end-tag-regexp
582 (regexp-or mime-edit-single-part-tag-regexp
583 mime-edit-multipart-end-regexp))
585 (defconst mime-edit-tag-regexp
586 (regexp-or mime-edit-single-part-tag-regexp
587 mime-edit-multipart-beginning-regexp
588 mime-edit-multipart-end-regexp))
590 (defvar mime-tag-format "--[[%s]]"
591 "*Control-string making a MIME tag.")
593 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
594 "*Control-string making a MIME tag with encoding.")
597 ;;; @@ multipart boundary
600 (defvar mime-multipart-boundary "Multipart"
601 "*Boundary of a multipart message.")
604 ;;; @@ optional header fields
607 (defvar mime-edit-insert-user-agent-field t
608 "*If non-nil, insert User-Agent header field.")
610 (defvar mime-edit-user-agent-value
611 (concat (mime-product-name mime-user-interface-product)
613 (mapconcat #'number-to-string
614 (mime-product-version mime-user-interface-product) ".")
616 (mime-product-code-name mime-user-interface-product)
618 (mime-product-name mime-library-product)
620 (mapconcat #'number-to-string
621 (mime-product-version mime-library-product) ".")
623 (mime-product-code-name mime-library-product)
625 (if (fboundp 'apel-version)
626 (concat (apel-version) " "))
627 (if (eq mime-edit-pgp-use 'epg)
628 (concat "EasyPG/" epg-version-number " "))
629 (if (featurep 'xemacs)
630 (concat (cond ((and (featurep 'chise)
631 (boundp 'xemacs-chise-version))
632 (concat "CHISE-MULE/" xemacs-chise-version))
633 ((featurep 'utf-2000)
634 (concat "UTF-2000-MULE/" utf-2000-version))
635 ((featurep 'mule) "MULE"))
637 (if (string-match "^[0-9]+\\(\\.[0-9]+\\)" emacs-version)
640 (substring emacs-version 0 (match-end 0))
641 (cond ((and (boundp 'xemacs-betaname)
643 ;; It does not exist in XEmacs
644 ;; versions prior to 20.3.
645 (concat " " xemacs-betaname))
646 ((and (boundp 'emacs-patch-level)
648 ;; It does not exist in FSF Emacs or in
649 ;; XEmacs versions earlier than 21.1.1.
650 (format " (patch %d)" emacs-patch-level))
652 " (" xemacs-codename ")"
653 ;; `xemacs-extra-name' has appeared in the
654 ;; development version of XEmacs 21.5-b8.
655 (if (and (boundp 'xemacs-extra-name)
656 (symbol-value 'xemacs-extra-name))
657 (concat " " (symbol-value 'xemacs-extra-name))
660 system-configuration ")")
661 " (" emacs-version ")"))
662 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
663 (substring emacs-version 0 (match-beginning 0))
666 (if (boundp 'enable-multibyte-characters)
668 " (" system-configuration ")"
669 (if enable-multibyte-characters
670 (concat " MULE/" mule-version)
671 " (with unibyte mode)")
672 (if (featurep 'meadow)
673 (let ((mver (Meadow-version)))
674 (if (string-match "^Meadow-" mver)
678 (concat "MULE/" mule-version
679 " (based on Emacs " ver ")"))
680 (concat "Emacs/" ver " (" system-configuration ")")))))
681 "Body of User-Agent field.
682 If variable `mime-edit-insert-user-agent-field' is not nil, it is
683 inserted into message header.")
689 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
690 "*Specify MIME tspecials.
691 Tspecials means any character that matches with it in header must be quoted.")
693 (defconst mime-edit-mime-version-value
694 (concat "1.0 (generated by " mime-edit-version ")")
695 "MIME version number.")
697 (defconst mime-edit-mime-version-field-for-message/partial
698 (concat "MIME-Version:"
699 (mime-encode-field-body
700 (concat " 1.0 (split by " mime-edit-version ")\n")
702 "MIME version field for message/partial.")
705 ;;; @ keymap and menu
708 (defvar mime-edit-mode-flag nil)
709 (make-variable-buffer-local 'mime-edit-mode-flag)
711 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
712 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
713 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
714 "Keymap for MIME-Edit mode commands to insert entity or set status.")
716 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
717 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
718 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
719 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
720 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
721 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
722 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
723 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
724 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
725 (define-key mime-edit-mode-entity-map "t" 'mime-edit-insert-tag)
727 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
728 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
729 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
730 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
731 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
732 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
733 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
734 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
735 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
736 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
738 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
739 "Keymap prefix for MIME-Edit mode commands about enclosure.")
740 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
741 "Keymap for MIME-Edit mode commands about enclosure.")
743 (define-key mime-edit-mode-enclosure-map
744 "\C-a" 'mime-edit-enclose-alternative-region)
745 (define-key mime-edit-mode-enclosure-map
746 "\C-p" 'mime-edit-enclose-parallel-region)
747 (define-key mime-edit-mode-enclosure-map
748 "\C-m" 'mime-edit-enclose-mixed-region)
749 (define-key mime-edit-mode-enclosure-map
750 "\C-d" 'mime-edit-enclose-digest-region)
751 (define-key mime-edit-mode-enclosure-map
752 "\C-s" 'mime-edit-enclose-pgp-signed-region)
753 (define-key mime-edit-mode-enclosure-map
754 "\C-e" 'mime-edit-enclose-pgp-encrypted-region)
755 (define-key mime-edit-mode-enclosure-map
756 "s" 'mime-edit-enclose-smime-signed-region)
757 (define-key mime-edit-mode-enclosure-map
758 "e" 'mime-edit-enclose-smime-encrypted-region)
759 (define-key mime-edit-mode-enclosure-map
760 "\C-q" 'mime-edit-enclose-quote-region)
762 (defvar mime-edit-mode-map (make-sparse-keymap)
763 "Keymap for MIME-Edit mode commands.")
764 (define-key mime-edit-mode-map
765 mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
766 (define-key mime-edit-mode-map
767 mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
769 (defconst mime-edit-menu-title "MIME-Edit")
771 (defconst mime-edit-menu-list
772 '((mime-help "Describe MIME editor mode" mime-edit-help)
773 (file "Insert File" mime-edit-insert-file)
774 (external "Insert External" mime-edit-insert-external)
775 (voice "Insert Voice" mime-edit-insert-voice)
776 (message "Insert Message" mime-edit-insert-message)
777 (mail "Insert Mail" mime-edit-insert-mail)
778 (signature "Insert Signature" mime-edit-insert-signature)
779 (text "Insert Text" mime-edit-insert-text)
780 (tag "Insert Tag" mime-edit-insert-tag)
781 (alternative "Enclose as alternative"
782 mime-edit-enclose-alternative-region)
783 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
784 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
785 (digest "Enclose as digest" mime-edit-enclose-digest-region)
786 (signed "Enclose as signed" mime-edit-enclose-pgp-signed-region)
787 (encrypted "Enclose as encrypted" mime-edit-enclose-pgp-encrypted-region)
788 (quote "Verbatim region" mime-edit-enclose-quote-region)
789 (key "Insert Public Key" mime-edit-insert-key)
790 (split "Set splitting" mime-edit-set-split)
791 (sign "PGP sign" mime-edit-set-sign)
792 (encrypt "PGP encrypt" mime-edit-set-encrypt)
793 (preview "Preview Message" mime-edit-preview-message)
794 (level "Toggle transfer-level" mime-edit-toggle-transfer-level))
795 "MIME-edit menubar entry.")
797 (cond ((featurep 'xemacs)
798 ;; modified by Pekka Marjola <pema@iki.fi>
799 ;; 1995/9/5 (c.f. [tm-en:69])
800 (defun mime-edit-define-menu-for-xemacs ()
801 "Define menu for XEmacs."
802 (cond ((featurep 'menubar)
803 (make-local-variable 'current-menubar)
804 (set-buffer-menubar current-menubar)
807 (cons mime-edit-menu-title
810 (vector (nth 1 item)(nth 2 item)
811 mime-edit-mode-flag)))
812 mime-edit-menu-list))))))
814 ;; modified by Steven L. Baur <steve@miranova.com>
815 ;; 1995/12/6 (c.f. [tm-en:209])
816 (or (boundp 'mime-edit-popup-menu-for-xemacs)
817 (setq mime-edit-popup-menu-for-xemacs
818 (append '("MIME Commands" "---")
819 (mapcar (function (lambda (item)
823 mime-edit-menu-list)))))
824 ((>= emacs-major-version 19)
825 (define-key mime-edit-mode-map [menu-bar mime-edit]
826 (cons mime-edit-menu-title
827 (make-sparse-keymap mime-edit-menu-title)))
830 (define-key mime-edit-mode-map
831 (vector 'menu-bar 'mime-edit (car item))
832 (cons (nth 1 item)(nth 2 item)))))
833 (reverse mime-edit-menu-list))))
839 (defvar mime-edit-touched-flag nil)
842 (defun mime-edit-mode ()
843 "MIME minor mode for editing the tagged MIME message.
845 In this mode, basically, the message is composed in the tagged MIME
846 format. The message tag looks like:
848 --[[text/plain; charset=ISO-2022-JP][7bit]]
850 The tag specifies the MIME content type, subtype, optional parameters
851 and transfer encoding of the message following the tag. Messages
852 without any tag are treated as `text/plain' by default. Charset and
853 transfer encoding are automatically defined unless explicitly
854 specified. Binary messages such as audio and image are usually
855 hidden. The messages in the tagged MIME format are automatically
856 translated into a MIME compliant message when exiting this mode.
858 Available charsets depend on Emacs version being used. The following
859 lists the available charsets of each emacs.
861 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
862 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
863 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
864 CN-BIG5 and ISO-2022-INT-1 are available.
866 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
867 be used to represent multilingual text in intermixed manner. Any
868 languages that has no registered charset are represented as either
869 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
871 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
872 without mule, please set variable `default-mime-charset'. This
873 variable must be symbol of which name is a MIME charset.
875 If you want to add more charsets in mule, please set variable
876 `charsets-mime-charset-alist'. This variable must be alist of which
877 key is list of charset and value is symbol of MIME charset. If name
878 of coding-system is different as MIME charset, please set variable
879 `mime-charset-coding-system-alist'. This variable must be alist of
880 which key is MIME charset and value is coding-system.
882 Following commands are available in addition to major mode commands:
885 \\[mime-edit-insert-text] insert a text message.
886 \\[mime-edit-insert-file] insert a (binary) file.
887 \\[mime-edit-insert-external] insert a reference to external body.
888 \\[mime-edit-insert-voice] insert a voice message.
889 \\[mime-edit-insert-message] insert a mail or news message.
890 \\[mime-edit-insert-mail] insert a mail message.
891 \\[mime-edit-insert-signature] insert a signature file at end.
892 \\[mime-edit-insert-key] insert PGP public key.
893 \\[mime-edit-insert-tag] insert a new MIME tag.
895 \[make enclosure (maybe multipart)\]
896 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
897 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
898 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
899 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
900 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
901 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
902 \\[mime-edit-enclose-quote-region] enclose as verbose mode
903 (to avoid to expand tags)
906 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
907 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
908 \\[mime-edit-set-split] set message splitting mode.
909 \\[mime-edit-set-sign] set PGP-sign mode.
910 \\[mime-edit-set-encrypt] set PGP-encryption mode.
911 \\[mime-edit-preview-message] preview editing MIME message.
912 \\[mime-edit-exit] exit and translate into a MIME
914 \\[mime-edit-help] show this help.
915 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
918 Additional commands are available in some major modes:
919 C-c C-c exit, translate and run the original command.
920 C-c C-s exit, translate and run the original command.
922 The following is a message example written in the tagged MIME format.
923 TABs at the beginning of the line are not a part of the message:
925 This is a conventional plain text. It should be translated
928 This is also a plain text. But, it is explicitly specified as
930 --[[text/plain; charset=ISO-8859-1]]
931 This is also a plain text. But charset is specified as
934 ¡Hola! Buenos días. ¿Cómo está usted?
936 This is a <bold>enriched text</bold>.
937 --[[image/gif][base64]]...image encoded in base64 here...
938 --[[audio/basic][base64]]...audio encoded in base64 here...
940 User customizable variables (not documented all of them):
942 Specifies a key prefix for MIME minor mode commands.
944 mime-ignore-preceding-spaces
945 Preceding white spaces in a message body are ignored if non-nil.
947 mime-ignore-trailing-spaces
948 Trailing white spaces in a message body are ignored if non-nil.
951 Hide a non-textual body message encoded in base64 after insertion
955 A number of network transfer level. It should be bigger than 7.
956 If you are in 8bit-through environment, please set 8.
958 mime-edit-voice-recorder
959 Specifies a function to record a voice message and encode it.
960 The function `mime-edit-voice-recorder-for-sun' is for Sun
964 Turning on MIME mode calls the value of mime-edit-mode-hook, if
967 mime-edit-translate-hook
968 The value of mime-edit-translate-hook is called just before translating
969 the tagged MIME format into a MIME compliant message if it is
970 non-nil. If the hook call the function mime-edit-insert-signature,
971 the signature file will be inserted automatically.
974 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
977 (if mime-edit-mode-flag
979 (if mime-edit-touched-flag
981 (make-local-variable 'mime-edit-touched-flag)
982 (setq mime-edit-touched-flag t)
983 (turn-on-mime-edit))))
986 (cond ((featurep 'xemacs)
987 (add-minor-mode 'mime-edit-mode-flag
988 '((" MIME-Edit " mime-transfer-level-string))
993 (set-alist 'minor-mode-alist
995 '((" MIME-Edit " mime-transfer-level-string)))
996 (set-alist 'minor-mode-map-alist
998 mime-edit-mode-map)))
1002 (defun turn-on-mime-edit ()
1003 "Unconditionally turn on MIME-Edit mode."
1005 (if mime-edit-mode-flag
1006 (error "You are already editing a MIME message.")
1007 (setq mime-edit-mode-flag t)
1009 ;; Set transfer level into mode line
1011 (setq mime-transfer-level-string
1012 (mime-encoding-name mime-transfer-level 'not-omit))
1013 (force-mode-line-update)
1015 ;; Define menu for XEmacs.
1016 (if (featurep 'xemacs)
1017 (mime-edit-define-menu-for-xemacs))
1019 ;; I don't care about saving these.
1020 (setq paragraph-start
1021 (regexp-or mime-edit-single-part-tag-regexp
1023 (setq paragraph-separate
1024 (regexp-or mime-edit-single-part-tag-regexp
1025 paragraph-separate))
1026 (run-hooks 'mime-edit-mode-hook)
1029 (substitute-command-keys
1030 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))))
1033 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1036 (defun mime-edit-exit (&optional nomime no-error)
1037 "Translate the tagged MIME message into a MIME compliant message.
1038 With no argument encode a message in the buffer into MIME, otherwise
1039 just return to previous mode."
1041 (if (not mime-edit-mode-flag)
1043 (error "You aren't editing a MIME message."))
1046 (run-hooks 'mime-edit-translate-hook)
1047 (mime-edit-translate-buffer)))
1048 ;; Restore previous state.
1049 (setq mime-edit-mode-flag nil)
1050 (if (and (featurep 'xemacs)
1051 (featurep 'menubar))
1052 (delete-menu-item (list mime-edit-menu-title)))
1053 (set-buffer-modified-p (buffer-modified-p))
1054 (run-hooks 'mime-edit-exit-hook)
1055 (message "Exit MIME editor mode.")))
1057 (defun mime-edit-maybe-translate ()
1059 (mime-edit-exit nil t)
1060 (call-interactively 'mime-edit-maybe-split-and-send))
1062 (defun mime-edit-help ()
1063 "Show help message about MIME mode."
1065 (with-output-to-temp-buffer "*Help*"
1066 (princ "MIME editor mode:\n")
1067 (princ (documentation 'mime-edit-mode))
1068 (print-help-return-message)))
1070 (defun mime-edit-insert-text (&optional subtype)
1071 "Insert a text message.
1072 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1073 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1075 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1077 (if (looking-at mime-edit-single-part-tag-regexp)
1079 ;; Make a space between the following message.
1082 (if (and (member (cadr ret) '("enriched"))
1083 (fboundp 'enriched-mode))
1085 (if (boundp 'enriched-mode)
1086 (enriched-mode -1))))))
1088 (defun mime-edit-insert-file (file &optional verbose)
1089 "Insert a message from a file."
1090 (interactive "fInsert file as MIME message: \nP")
1091 (let* ((guess (mime-find-file-type file))
1092 (type (nth 0 guess))
1093 (subtype (nth 1 guess))
1094 (parameters (nth 2 guess))
1095 (encoding (nth 3 guess))
1096 (disposition-type (nth 4 guess))
1097 (disposition-params (nth 5 guess)))
1099 (setq type (mime-prompt-for-type type)
1100 subtype (mime-prompt-for-subtype type subtype)))
1101 (if (or (interactive-p) verbose)
1102 (setq encoding (mime-prompt-for-encoding encoding)))
1103 (if (or (consp parameters) (stringp disposition-type))
1104 (let ((rest parameters) cell attribute value)
1105 (setq parameters "")
1107 (setq cell (car rest))
1108 (setq attribute (car cell))
1109 (setq value (cdr cell))
1110 (if (eq value 'file)
1111 (setq value (std11-wrap-as-quoted-string
1112 (file-name-nondirectory file))))
1113 (setq parameters (concat parameters "; " attribute "=" value))
1114 (setq rest (cdr rest)))
1115 (if disposition-type
1118 (concat parameters "\n"
1119 "Content-Disposition: " disposition-type))
1120 (setq rest disposition-params)
1122 (setq cell (car rest))
1123 (setq attribute (car cell))
1124 (setq value (cdr cell))
1125 (if (eq value 'file)
1126 (setq value (std11-wrap-as-quoted-string
1127 (file-name-nondirectory file))))
1129 (concat parameters "; " attribute "=" value))
1130 (setq rest (cdr rest)))))))
1131 (mime-edit-insert-tag type subtype parameters)
1132 (mime-edit-insert-binary-file file encoding)))
1134 (defun mime-edit-insert-external ()
1135 "Insert a reference to external body."
1137 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1139 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1141 (let* ((pritype (mime-prompt-for-type))
1142 (subtype (mime-prompt-for-subtype pritype))
1143 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1146 (insert "Content-Type: "
1147 pritype "/" subtype (or parameters "") "\n")))
1148 (if (and (not (eobp))
1149 (not (looking-at mime-edit-single-part-tag-regexp)))
1150 (insert (mime-make-text-tag) "\n")))
1152 (defun mime-edit-insert-voice ()
1153 "Insert a voice message."
1157 "What transfer encoding: "
1158 (mime-encoding-alist) nil t nil)))
1159 (mime-edit-insert-tag "audio" "basic" nil)
1160 (mime-edit-define-encoding encoding)
1162 (narrow-to-region (point)(point))
1164 (funcall mime-edit-voice-recorder encoding)
1167 (add-text-properties
1168 (point-min)(point-max) '(invisible t mime-edit-invisible t))
1169 (goto-char (point-max)))))))
1171 (defun mime-edit-insert-signature (&optional arg)
1172 "Insert a signature file."
1174 (let ((signature-insert-hook
1177 (let ((items (mime-find-file-type signature-file-name)))
1178 (apply (function mime-edit-insert-tag)
1179 (car items) (cadr items) (list (caddr items))))))))
1180 (insert-signature arg)))
1183 ;; Insert a new tag around a point.
1185 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1186 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1187 If nothing is inserted, return nil."
1190 (mime-edit-goto-tag)
1191 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1192 (< (match-beginning 0) p)
1193 (< p (match-end 0)))
1194 (goto-char (match-beginning 0))
1201 (mime-prompt-for-type)))
1204 (mime-prompt-for-subtype pritype)))
1207 (mime-prompt-for-parameters pritype subtype delimiter)))
1208 ;; Make a new MIME tag.
1209 (setq newtag (mime-make-tag pritype subtype parameters))
1210 ;; Find an current MIME tag.
1213 (if (mime-edit-goto-tag)
1214 (buffer-substring (match-beginning 0) (match-end 0))
1215 ;; Assume content type is 'text/plan'.
1216 (mime-make-tag "text" "plain"))))
1217 ;; We are only interested in TEXT.
1219 (not (mime-test-content-type
1220 (mime-edit-get-contype oldtag) "text")))
1223 (if (or (not oldtag) ;Not text
1224 (or mime-ignore-same-text-tag
1225 (not (string-equal oldtag newtag))))
1227 ;; Mark the beginning of the tag for convenience.
1228 (push-mark (point) 'nomsg)
1229 (insert newtag "\n")
1230 (list pritype subtype parameters) ;New tag is created.
1232 ;; Restore previous point.
1234 nil ;Nothing is created.
1237 (defun mime-edit-insert-binary-file (file &optional encoding)
1238 "Insert binary FILE at point.
1239 Optional argument ENCODING specifies an encoding method such as base64."
1240 (let* ((tagend (1- (point))) ;End of the tag
1241 (hide-p (and mime-auto-hide-body
1244 (let ((en (downcase encoding)))
1245 (or (string-equal en "7bit")
1246 (string-equal en "8bit")
1247 (string-equal en "binary")))))))
1249 (narrow-to-region (point)(point))
1250 (mime-insert-encoded-file file encoding)
1252 (add-text-properties
1253 (point-min)(point-max) '(invisible t mime-edit-invisible t)))
1254 (goto-char (point-max)))
1256 (looking-at mime-edit-tag-regexp)
1257 (= (point)(point-max))
1258 (mime-edit-insert-tag "text" "plain"))
1259 ;; Define encoding even if it is 7bit.
1260 (if (stringp encoding)
1262 (goto-char tagend) ; Make sure which line the tag is on.
1263 (mime-edit-define-encoding encoding)))))
1266 ;; Commands work on a current message flagment.
1268 (defun mime-edit-goto-tag ()
1269 "Search for the beginning of the tagged MIME message."
1270 (let ((current (point)))
1271 (if (looking-at mime-edit-tag-regexp)
1273 ;; At first, go to the end.
1274 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1275 (goto-char (1- (match-beginning 0))) ;For multiline tag
1278 (goto-char (point-max))))
1279 ;; Then search for the beginning.
1280 (re-search-backward mime-edit-end-tag-regexp nil t)
1281 (or (looking-at mime-edit-beginning-tag-regexp)
1282 ;; Restore previous point.
1287 (defun mime-edit-content-beginning ()
1288 "Return the point of the beginning of content."
1290 (let ((beg (save-excursion
1291 (beginning-of-line) (point))))
1292 (if (mime-edit-goto-tag)
1293 (let ((top (point)))
1294 (goto-char (match-end 0))
1295 (if (and (= beg top)
1296 (= (following-char) ?\^M))
1300 ;; Default text/plain tag.
1301 (goto-char (point-min))
1303 (concat "\n" (regexp-quote mail-header-separator)
1304 (if mime-ignore-preceding-spaces
1305 "[ \t\n]*\n" "\n")) nil 'move)
1308 (defun mime-edit-content-end ()
1309 "Return the point of the end of content."
1311 (if (mime-edit-goto-tag)
1313 (goto-char (1+ (match-end 0)))
1314 (if (get-text-property (point) 'mime-edit-invisible)
1315 (or (next-single-property-change (point) 'mime-edit-invisible)
1317 ;; Move to the end of this text.
1318 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1319 ;; Don't forget a multiline tag.
1320 (goto-char (match-beginning 0)))
1322 ;; Assume the message begins with text/plain.
1323 (goto-char (mime-edit-content-beginning))
1324 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1325 ;; Don't forget a multiline tag.
1326 (goto-char (match-beginning 0)))
1329 (defun mime-edit-define-charset (charset)
1330 "Set charset of current tag to CHARSET."
1332 (if (mime-edit-goto-tag)
1333 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1334 (delete-region (match-beginning 0) (match-end 0))
1337 (mime-edit-set-parameter
1338 (mime-edit-get-contype tag)
1340 (let ((comment (get charset 'mime-charset-comment)))
1342 (concat (upcase (symbol-name charset)) " (" comment ")")
1343 (upcase (symbol-name charset)))))
1344 (mime-edit-get-encoding tag)))))))
1346 (defun mime-edit-define-encoding (encoding)
1347 "Set encoding of current tag to ENCODING."
1349 (if (mime-edit-goto-tag)
1350 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1351 (delete-region (match-beginning 0) (match-end 0))
1352 (insert (mime-create-tag (mime-edit-get-contype tag) encoding))))))
1354 (defun mime-edit-choose-charset ()
1355 "Choose charset of a text following current point."
1356 (detect-mime-charset-region (point) (mime-edit-content-end)))
1358 (defun mime-make-text-tag (&optional subtype)
1359 "Make a tag for a text after current point.
1360 Subtype of text type can be specified by an optional argument SUBTYPE.
1361 Otherwise, it is obtained from mime-content-types."
1362 (let* ((pritype "text")
1363 (subtype (or subtype
1364 (car (car (cdr (assoc pritype mime-content-types)))))))
1365 ;; Charset should be defined later.
1366 (mime-make-tag pritype subtype)))
1369 ;; Tag handling functions
1371 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1372 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1373 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1377 (defun mime-create-tag (contype &optional encoding)
1378 "Make a tag with CONTENT-TYPE and optional ENCODING."
1379 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1382 (defun mime-edit-get-contype (tag)
1383 "Return Content-Type (including parameters) of TAG."
1385 (or (string-match mime-edit-single-part-tag-regexp tag)
1386 (string-match mime-edit-multipart-beginning-regexp tag)
1387 (string-match mime-edit-multipart-end-regexp tag))
1388 (substring tag (match-beginning 1) (match-end 1))))
1390 (defun mime-edit-get-encoding (tag)
1391 "Return encoding of TAG."
1393 (string-match mime-edit-single-part-tag-regexp tag)
1395 (not (= (match-beginning 3) (match-end 3)))
1396 (substring tag (match-beginning 3) (match-end 3))))
1398 (defun mime-get-parameter (contype parameter)
1399 "For given CONTYPE return value for PARAMETER.
1400 Nil if no such parameter."
1404 (regexp-quote parameter)
1405 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1407 (substring contype (match-beginning 1) (match-end 1))
1408 nil ;No such parameter
1411 (defun mime-edit-set-parameter (contype parameter value)
1412 "For given CONTYPE set PARAMETER to VALUE."
1413 (let (ctype opt-fields)
1414 (if (string-match "\n[^ \t\n\r]+:" contype)
1415 (setq ctype (substring contype 0 (match-beginning 0))
1416 opt-fields (substring contype (match-beginning 0)))
1417 (setq ctype contype))
1421 (regexp-quote parameter)
1422 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1425 (concat (substring ctype 0 (match-beginning 1))
1427 (substring ctype (match-end 1))
1429 (concat ctype "; " parameter "=" value opt-fields)
1432 (defun mime-strip-parameters (contype)
1433 "Return primary content-type and subtype without parameters for CONTYPE."
1434 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1435 (substring contype (match-beginning 1) (match-end 1)) nil))
1437 (defun mime-test-content-type (contype type &optional subtype)
1438 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1439 (and (stringp contype)
1442 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1443 (downcase contype))))
1448 (defun mime-find-file-type (file)
1449 "Guess Content-Type, subtype, and parameters from FILE."
1451 (guesses mime-file-types))
1452 (while (and (not guess) guesses)
1453 (if (string-match (car (car guesses)) file)
1454 (setq guess (cdr (car guesses))))
1455 (setq guesses (cdr guesses)))
1458 (defun mime-prompt-for-type (&optional default)
1459 "Ask for Content-type."
1461 ;; Repeat until primary content type is specified.
1462 (while (string-equal type "")
1464 (completing-read "What content type: "
1467 'require-match ;Type must be specified.
1469 (if (string-equal type "")
1471 (message "Content type is required.")
1476 (defun mime-prompt-for-subtype (type &optional default)
1477 "Ask for subtype of media-type TYPE."
1478 (let ((subtypes (cdr (assoc type mime-content-types))))
1480 (assoc default subtypes))
1481 (setq default (car (car subtypes)))))
1486 "What content subtype: (default " default ") ")
1487 "What content subtype: ")
1488 (cdr (assoc type mime-content-types))
1490 'require-match ;Subtype must be specified.
1492 (if (string-equal answer "") default answer)))
1494 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1495 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1496 Optional DELIMITER specifies parameter delimiter (';' by default)."
1497 (let* ((delimiter (or delimiter "; "))
1502 (mime-prompt-for-parameters-1
1504 (cdr (assoc pritype mime-content-types))))))
1506 (if (and (stringp parameters)
1507 (not (string-equal parameters "")))
1508 (concat delimiter parameters)
1509 "" ;"" if no parameters
1512 (defun mime-prompt-for-parameters-1 (optlist)
1513 (apply (function append)
1514 (mapcar (function mime-prompt-for-parameter) optlist)))
1516 (defun mime-prompt-for-parameter (parameter)
1518 Parameter must be '(PROMPT CHOICE1 (CHOICE2...))."
1519 (let* ((prompt (car parameter))
1520 (choices (mapcar (function
1522 (if (consp e) e (list e))))
1524 (default (car (car choices)))
1530 (concat "What " prompt
1532 (if (string-equal default "") "\"\"" default)
1534 choices nil nil ""))
1535 ;; If nothing is selected, use default.
1536 (if (string-equal answer "")
1537 (setq answer default)))
1539 (read-string (concat "What " prompt ": "))))
1540 (cons (if (and answer
1541 (not (string-equal answer "")))
1543 ;; Note: control characters ignored!
1544 (if (string-match mime-tspecials-regexp answer)
1545 (concat "\"" answer "\"") answer)))
1546 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))))
1548 (defun mime-prompt-for-encoding (default)
1549 "Ask for Content-Transfer-Encoding."
1554 "What transfer encoding: "
1555 (mime-encoding-alist) nil t default))
1560 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1563 (defvar mime-edit-translate-buffer-hook
1564 '(mime-edit-pgp-enclose-buffer
1565 mime-edit-translate-body
1566 mime-edit-translate-header))
1568 (defun mime-edit-translate-header ()
1569 "Encode the message header into network representation."
1570 (mime-encode-header-in-buffer 'code-conversion)
1571 (run-hooks 'mime-edit-translate-header-hook))
1573 (defun mime-edit-translate-buffer ()
1574 "Encode the tagged MIME message in current buffer in MIME compliant message."
1577 (if (catch 'mime-edit-error
1579 (run-hooks 'mime-edit-translate-buffer-hook)))
1582 (error "Translation error!"))))
1584 (defun mime-edit-find-inmost ()
1585 (goto-char (point-min))
1586 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1587 (let ((bb (match-beginning 0))
1589 (type (buffer-substring (match-beginning 1)(match-end 1)))
1591 (setq end-exp (format "--}-<<%s>>\n" type))
1593 (if (re-search-forward end-exp nil t)
1594 (setq eb (match-beginning 0))
1595 (setq eb (point-max)))
1596 (narrow-to-region be eb)
1598 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1600 (narrow-to-region (match-beginning 0)(point-max))
1601 (mime-edit-find-inmost))
1603 (list type bb be eb)))))
1605 (defun mime-edit-process-multipart-1 (boundary)
1606 (let ((ret (mime-edit-find-inmost)))
1608 (let ((type (car ret))
1609 (bb (nth 1 ret))(be (nth 2 ret))
1611 (narrow-to-region bb eb)
1612 (delete-region bb be)
1613 (setq bb (point-min))
1614 (setq eb (point-max))
1617 (if (looking-at mime-edit-multipart-end-regexp)
1618 (let ((beg (match-beginning 0))
1619 (end (match-end 0)))
1620 (delete-region beg end)
1621 (or (looking-at mime-edit-beginning-tag-regexp)
1623 (insert (concat (mime-make-text-tag) "\n")))))
1624 (cond ((string-equal type "quote")
1625 (mime-edit-enquote-region bb eb))
1626 ((string-equal type "pgp-signed")
1627 (if (eq mime-edit-pgp-use 'epg)
1628 (mime-edit-sign-pgp-mime-with-epg bb eb boundary)
1629 (mime-edit-sign-pgp-mime-with-pgg bb eb boundary)))
1630 ((string-equal type "pgp-encrypted")
1631 (if (eq mime-edit-pgp-use 'epg)
1632 (mime-edit-encrypt-pgp-mime-with-epg bb eb boundary)
1633 (mime-edit-encrypt-pgp-mime-with-pgg bb eb boundary)))
1634 ((string-equal type "kazu-signed")
1635 (if (eq mime-edit-pgp-use 'epg)
1636 (mime-edit-sign-pgp-kazu-with-epg bb eb boundary)
1637 (mime-edit-sign-pgp-kazu-with-pgg bb eb boundary)))
1638 ((string-equal type "kazu-encrypted")
1639 (if (eq mime-edit-pgp-use 'epg)
1640 (mime-edit-encrypt-pgp-kazu-with-epg bb eb boundary)
1641 (mime-edit-encrypt-pgp-kazu-with-pgg bb eb boundary)))
1642 ((string-equal type "smime-signed")
1643 (mime-edit-sign-smime bb eb boundary))
1644 ((string-equal type "smime-encrypted")
1645 (mime-edit-encrypt-smime bb eb boundary))
1648 (nth 2 (mime-edit-translate-region bb eb
1652 (format "--[[multipart/%s;
1653 boundary=\"%s\"][7bit]]\n"
1657 (defun mime-edit-enquote-region (beg end)
1660 (narrow-to-region beg end)
1662 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1663 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1664 (replace-match (concat "- " (substring tag 1))))))))
1666 (defun mime-edit-dequote-region (beg end)
1669 (narrow-to-region beg end)
1671 (while (re-search-forward
1672 mime-edit-quoted-single-part-tag-regexp nil t)
1673 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1674 (replace-match (concat "-" (substring tag 2))))))))
1676 (defvar mime-edit-pgp-user-id nil)
1678 (defun mime-edit-delete-trailing-whitespace ()
1681 (goto-char (point-min))
1682 (while (re-search-forward "[ \t]+$" nil t)
1683 (delete-region (match-beginning 0) (match-end 0))))))
1685 (defun mime-edit-sign-pgp-mime-with-epg (beg end boundary)
1688 (let* ((from (std11-field-body "From" mail-header-separator))
1690 (narrow-to-region beg end)
1691 (mime-edit-translate-region beg end boundary)))
1693 (encoding (nth 1 ret))
1694 (pgp-boundary (concat "pgp-sign-" boundary))
1695 (context (epg-make-context))
1697 (mime-edit-delete-trailing-whitespace) ; RFC3156
1699 (insert (format "Content-Type: %s\n" ctype))
1701 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1703 (epg-context-set-armor context t)
1704 (epg-context-set-textmode context t)
1705 (epg-context-set-signers
1710 Select keys for signing.
1711 If no one is selected, default secret key is used. "
1713 (list (nth 1 (std11-extract-address-components from))))
1715 (condition-case error
1717 (epg-sign-string context
1718 (buffer-substring (point-min) (point-max))
1720 (error (signal 'mime-edit-error (cdr error))))
1721 (setq micalg (epg-new-signature-digest-algorithm
1722 (car (epg-context-result-for context 'sign))))
1724 (insert (format "--[[multipart/signed;
1726 protocol=\"application/pgp-signature\"][7bit]]
1731 (concat "; micalg=pgp-"
1734 epg-digest-algorithm-alist))))
1737 (goto-char (point-max))
1738 (insert (format "\n--%s
1739 Content-Type: application/pgp-signature
1740 Content-Transfer-Encoding: 7bit
1741 Content-Description: OpenPGP Digital Signature
1745 (goto-char (point-max))
1746 (insert (format "\n--%s--\n" pgp-boundary))))))
1748 (defun mime-edit-sign-pgp-mime-with-pgg (beg end boundary)
1751 (let* ((from (std11-field-body "From" mail-header-separator))
1753 (narrow-to-region beg end)
1754 (mime-edit-translate-region beg end boundary)))
1756 (encoding (nth 1 ret))
1757 (pgp-boundary (concat "pgp-sign-" boundary))
1759 (mime-edit-delete-trailing-whitespace) ; RFC3156
1761 (insert (format "Content-Type: %s\n" ctype))
1763 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1765 (or (let ((pgg-default-user-id
1766 (or mime-edit-pgp-user-id
1768 (nth 1 (std11-extract-address-components from))
1769 pgg-default-user-id))))
1770 (pgg-sign-region (point-min)(point-max)))
1771 (throw 'mime-edit-error 'pgp-error))
1773 (cdr (assq 'hash-algorithm
1774 (cdar (with-current-buffer pgg-output-buffer
1775 (pgg-parse-armor-region
1776 (point-min)(point-max))))))
1779 (concat "; micalg=pgp-" (downcase (symbol-name micalg)))
1782 (insert (format "--[[multipart/signed;
1784 protocol=\"application/pgp-signature\"][7bit]]
1786 " pgp-boundary micalg pgp-boundary))
1787 (goto-char (point-max))
1788 (insert (format "\n--%s
1789 Content-Type: application/pgp-signature
1790 Content-Transfer-Encoding: 7bit
1793 (insert-buffer-substring pgg-output-buffer)
1794 (goto-char (point-max))
1795 (insert (format "\n--%s--\n" pgp-boundary))))))
1797 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1799 (defun mime-edit-make-encrypt-recipient-header ()
1800 (let* ((names mime-edit-encrypt-recipient-fields-list)
1802 (std11-field-bodies (cons "From" names)
1803 nil mail-header-separator))
1806 (setq values (cdr values))))
1807 (header (and (stringp from)
1808 (if (string-equal from "")
1810 (format "From: %s\n" from))))
1812 (while (and names values)
1813 (let ((name (car names))
1814 (value (car values)))
1815 (and (stringp value)
1816 (or (string-equal value "")
1818 (setq header (concat header name ": " value "\n")
1819 recipients (if recipients
1820 (concat recipients " ," value)
1822 (setq names (cdr names)
1823 values (cdr values)))
1824 (vector from recipients header)))
1826 (defun mime-edit-encrypt-pgp-mime-with-epg (beg end boundary)
1829 (let (recipients header)
1830 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1831 (setq recipients (aref ret 1)
1832 header (aref ret 2)))
1833 (narrow-to-region beg end)
1835 (mime-edit-translate-region beg end boundary))
1837 (encoding (nth 1 ret))
1838 (pgp-boundary (concat "pgp-" boundary))
1839 (context (epg-make-context))
1843 (insert (format "Content-Type: %s\n" ctype))
1845 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1847 (mime-encode-header-in-buffer)
1848 (epg-context-set-armor context t)
1849 (condition-case error
1853 (buffer-substring (point-min) (point-max))
1857 Select recipents for encryption.
1858 If no one is selected, symmetric encryption will be performed. "
1859 (mapcar (lambda (recipient)
1860 (nth 1 (std11-extract-address-components
1862 (split-string recipients
1863 "\\([ \t\n]*,[ \t\n]*\\)+")))))
1864 (error (signal 'mime-edit-error (cdr error))))
1865 (delete-region (point-min)(point-max))
1867 (insert (format "--[[multipart/encrypted;
1869 protocol=\"application/pgp-encrypted\"][7bit]]
1871 Content-Type: application/pgp-encrypted
1874 Content-Type: application/octet-stream
1875 Content-Transfer-Encoding: 7bit
1877 " pgp-boundary pgp-boundary pgp-boundary))
1879 (goto-char (point-max))
1880 (insert (format "\n--%s--\n" pgp-boundary)))))))
1882 (defun mime-edit-encrypt-pgp-mime-with-pgg (beg end boundary)
1885 (let (from recipients header)
1886 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1887 (setq from (aref ret 0)
1888 recipients (aref ret 1)
1889 header (aref ret 2)))
1890 (narrow-to-region beg end)
1892 (mime-edit-translate-region beg end boundary))
1894 (encoding (nth 1 ret))
1895 (pgp-boundary (concat "pgp-" boundary)))
1898 (insert (format "Content-Type: %s\n" ctype))
1900 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1902 (mime-encode-header-in-buffer)
1903 (or (let ((pgg-default-user-id
1904 (or mime-edit-pgp-user-id
1906 (nth 1 (std11-extract-address-components from))
1907 pgg-default-user-id))))
1909 (point-min) (point-max)
1910 (mapcar (lambda (recipient)
1911 (nth 1 (std11-extract-address-components
1913 (split-string recipients
1914 "\\([ \t\n]*,[ \t\n]*\\)+"))))
1915 (throw 'mime-edit-error 'pgp-error))
1916 (delete-region (point-min)(point-max))
1918 (insert (format "--[[multipart/encrypted;
1920 protocol=\"application/pgp-encrypted\"][7bit]]
1922 Content-Type: application/pgp-encrypted
1925 Content-Type: application/octet-stream
1926 Content-Transfer-Encoding: 7bit
1928 " pgp-boundary pgp-boundary pgp-boundary))
1929 (insert-buffer-substring pgg-output-buffer)
1930 (goto-char (point-max))
1931 (insert (format "\n--%s--\n" pgp-boundary)))))))
1933 (defun mime-edit-sign-pgp-kazu-with-epg (beg end boundary)
1936 (narrow-to-region beg end)
1938 (mime-edit-translate-region beg end boundary))
1940 (encoding (nth 1 ret))
1941 (context (epg-make-context))
1944 (insert (format "Content-Type: %s\n" ctype))
1946 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1948 (condition-case error
1950 (epg-sign-string context
1951 (buffer-substring beg (point-max))
1953 (error (signal 'mime-edit-error (cdr error))))
1956 "--[[application/pgp; format=mime][7bit]]\n" signature)
1959 (defun mime-edit-sign-pgp-kazu-with-pgg (beg end boundary)
1962 (narrow-to-region beg end)
1964 (mime-edit-translate-region beg end boundary))
1966 (encoding (nth 1 ret)))
1968 (insert (format "Content-Type: %s\n" ctype))
1970 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1972 (or (pgg-sign-region beg (point-max) 'clearsign)
1973 (throw 'mime-edit-error 'pgp-error))
1976 "--[[application/pgp; format=mime][7bit]]\n")
1979 (defun mime-edit-encrypt-pgp-kazu-with-epg (beg end boundary)
1981 (let (recipients header)
1982 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1983 (setq recipients (aref ret 1)
1984 header (aref ret 2)))
1986 (narrow-to-region beg end)
1988 (mime-edit-translate-region beg end boundary))
1990 (encoding (nth 1 ret))
1991 (context (epg-make-context))
1995 (insert (format "Content-Type: %s\n" ctype))
1997 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1999 (epg-context-set-armor context t)
2000 (condition-case error
2004 (buffer-substring beg (point-max))
2008 Select recipents for encryption.
2009 If no one is selected, symmetric encryption will be performed. "
2010 (mapcar (lambda (recipient)
2011 (nth 1 (std11-extract-address-components
2013 (split-string recipients
2014 "\\([ \t\n]*,[ \t\n]*\\)+")))))
2015 (error (signal 'mime-edit-error (cdr error))))
2018 "--[[application/pgp; format=mime][7bit]]\n" cipher)
2021 (defun mime-edit-encrypt-pgp-kazu-with-pgg (beg end boundary)
2023 (let (recipients header)
2024 (let ((ret (mime-edit-make-encrypt-recipient-header)))
2025 (setq recipients (aref ret 1)
2026 header (aref ret 2)))
2028 (narrow-to-region beg end)
2030 (mime-edit-translate-region beg end boundary))
2032 (encoding (nth 1 ret)))
2035 (insert (format "Content-Type: %s\n" ctype))
2037 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
2039 (or (pgg-encrypt-region beg (point-max) recipients)
2040 (throw 'mime-edit-error 'pgp-error))
2043 "--[[application/pgp; format=mime][7bit]]\n")
2046 (defun mime-edit-convert-lbt-string (string)
2048 (while (setq index (string-match "\n" string index))
2049 (setq string (replace-match "\r\n" nil nil string)
2050 index (+ index 2))) ;(length "\r\n")
2053 (defun mime-edit-sign-smime (beg end boundary)
2056 (let* ((from (std11-field-body "From" mail-header-separator))
2058 (narrow-to-region beg end)
2059 (mime-edit-translate-region beg end boundary)))
2061 (encoding (nth 1 ret))
2062 (smime-boundary (concat "smime-sign-" boundary))
2063 (context (epg-make-context 'CMS))
2066 (insert (format "Content-Type: %s\n" ctype))
2068 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
2070 (epg-context-set-signers
2075 Select keys for signing.
2076 If no one is selected, default secret key is used. "
2078 (list (nth 1 (std11-extract-address-components from))))
2080 (condition-case error
2082 (epg-sign-string context
2083 (mime-edit-convert-lbt-string
2084 (buffer-substring (point-min) (point-max)))
2086 (error (signal 'mime-edit-error (cdr error))))
2087 (setq micalg (epg-new-signature-digest-algorithm
2088 (car (epg-context-result-for context 'sign))))
2090 (insert (format "--[[multipart/signed;
2092 protocol=\"application/pkcs7-signature\"][7bit]]
2100 epg-digest-algorithm-alist))))
2103 (goto-char (point-max))
2104 (insert (format "\n--%s
2105 Content-Type: application/pkcs7-signature; name=smime.p7s
2106 Content-Transfer-Encoding: base64
2107 Content-Disposition: attachment; filename=smime.p7s
2108 Content-Description: S/MIME Digital Signature
2111 (base64-encode-string signature))))))
2113 (defun mime-edit-encrypt-smime (beg end boundary)
2116 (let (recipients header)
2117 (let ((ret (mime-edit-make-encrypt-recipient-header)))
2118 (setq recipients (aref ret 1)
2119 header (aref ret 2)))
2120 (narrow-to-region beg end)
2122 (mime-edit-translate-region beg end boundary))
2124 (encoding (nth 1 ret))
2125 (context (epg-make-context 'CMS))
2129 (insert (format "Content-Type: %s\n" ctype))
2131 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
2133 (mime-encode-header-in-buffer)
2134 (condition-case error
2138 (buffer-substring (point-min) (point-max))
2142 Select recipents for encryption.
2143 If no one is selected, symmetric encryption will be performed. "
2144 (mapcar (lambda (recipient)
2145 (nth 1 (std11-extract-address-components
2147 (split-string recipients
2148 "\\([ \t\n]*,[ \t\n]*\\)+")))))
2149 (error (signal 'mime-edit-error (cdr error))))
2150 (delete-region (point-min)(point-max))
2152 (insert (format "--[[application/pkcs7-mime;
2153 smime-type=enveloped-data;
2155 Content-Disposition: attachment; filename=smime.p7m][base64]]
2158 (base64-encode-string cipher)))))))
2160 (defsubst replace-space-with-underline (str)
2161 (mapconcat (function
2168 (defun mime-edit-make-boundary ()
2169 (concat mime-multipart-boundary "_"
2170 (replace-space-with-underline (current-time-string))))
2172 (defun mime-edit-translate-body ()
2173 "Encode the tagged MIME body in current buffer in MIME compliant message."
2176 (let ((boundary (mime-edit-make-boundary))
2179 (while (mime-edit-process-multipart-1
2180 (format "%s-%d" boundary i))
2183 ;; We are interested in message body.
2186 (goto-char (point-min))
2188 (concat "\n" (regexp-quote mail-header-separator)
2189 (if mime-ignore-preceding-spaces
2190 "[ \t\n]*\n" "\n")) nil 'move)
2194 (goto-char (point-max))
2195 (and mime-ignore-trailing-spaces
2196 (re-search-backward "[^ \t\n]\n" beg t)
2199 (setq ret (mime-edit-translate-region
2201 (format "%s-%d" boundary i)))))
2202 (mime-edit-dequote-region (point-min)(point-max))
2203 (let ((contype (car ret)) ;Content-Type
2204 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
2206 ;; Insert User-Agent field
2207 (and mime-edit-insert-user-agent-field
2208 (or (mail-position-on-field "User-Agent")
2209 (insert mime-edit-user-agent-value)))
2210 ;; Make primary MIME headers.
2211 (or (mail-position-on-field "MIME-Version")
2212 (insert mime-edit-mime-version-value))
2213 ;; Remove old Content-Type and other fields.
2215 (goto-char (point-min))
2216 (search-forward (concat "\n" mail-header-separator "\n") nil t)
2217 (narrow-to-region (point-min) (point))
2218 (goto-char (point-min))
2219 (mime-delete-field "Content-Type")
2220 (mime-delete-field "Content-Transfer-Encoding"))
2221 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
2222 (mail-position-on-field "Content-Type")
2226 (mail-position-on-field "Content-Transfer-Encoding")
2227 (insert encoding)))))))
2229 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
2230 "Translate single-part-tag to MIME header."
2231 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
2232 (let* ((beg (match-beginning 0))
2234 (tag (buffer-substring beg end)))
2235 (delete-region beg end)
2236 (let ((contype (mime-edit-get-contype tag))
2237 (encoding (mime-edit-get-encoding tag)))
2238 (insert (concat prefix "--" boundary "\n"))
2240 (narrow-to-region (point)(point))
2241 (insert "Content-Type: " contype "\n")
2243 (insert "Content-Transfer-Encoding: " encoding "\n"))
2244 (mime-encode-header-in-buffer))
2248 (downcase encoding)))))))
2250 (defun mime-edit-translate-region (beg end &optional boundary multipart)
2252 (setq boundary (mime-edit-make-boundary)))
2255 (narrow-to-region beg end)
2256 (let ((tag nil) ;MIME tag
2257 (contype nil) ;Content-Type
2258 (encoding nil) ;Content-Transfer-Encoding
2259 (nparts 0)) ;Number of body parts
2260 ;; Normalize the body part by inserting appropriate message
2261 ;; tags for every message contents.
2262 (mime-edit-normalize-body)
2263 ;; Counting the number of Content-Type.
2264 (goto-char (point-min))
2265 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2266 (setq nparts (1+ nparts)))
2267 ;; Begin translation.
2269 ((and (<= nparts 1)(not multipart))
2270 ;; It's a singular message.
2271 (goto-char (point-min))
2272 (while (re-search-forward
2273 mime-edit-single-part-tag-regexp nil t)
2275 (buffer-substring (match-beginning 0) (match-end 0)))
2276 (delete-region (match-beginning 0) (1+ (match-end 0)))
2277 (setq contype (mime-edit-get-contype tag))
2278 (setq encoding (mime-edit-get-encoding tag))))
2280 ;; It's a multipart message.
2281 (goto-char (point-min))
2282 (let ((prio mime-content-transfer-encoding-priority-list)
2284 (when (setq part-info
2285 (mime-edit-translate-single-part-tag boundary))
2286 (and (setq nprio (member (cdr part-info) prio))
2288 (while (setq part-info
2289 (mime-edit-translate-single-part-tag boundary "\n"))
2290 (and (setq nprio (member (cdr part-info) prio))
2291 (setq prio nprio))))
2292 ;; Define Content-Type as "multipart/mixed".
2294 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
2295 (setq encoding (car prio))
2296 ;; Insert the trailer.
2297 (goto-char (point-max))
2298 (insert "\n--" boundary "--\n"))))
2299 (list contype encoding boundary nparts)))))
2301 (defun mime-edit-normalize-body ()
2302 "Normalize the body part by inserting appropriate message tags."
2303 ;; Insert the first MIME tags if necessary.
2304 (goto-char (point-min))
2305 (if (not (looking-at mime-edit-single-part-tag-regexp))
2306 (insert (mime-make-text-tag) "\n"))
2307 ;; Check each tag, and add new tag or correct it if necessary.
2308 (goto-char (point-min))
2309 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2310 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
2311 (contype (mime-edit-get-contype tag))
2312 (charset (mime-get-parameter contype "charset"))
2313 (encoding (mime-edit-get-encoding tag)))
2314 ;; Remove extra whitespaces after the tag.
2315 (if (looking-at "[ \t]+$")
2316 (delete-region (match-beginning 0) (match-end 0)))
2318 (end (mime-edit-content-end)))
2319 (if (= end (point-max))
2322 (or (looking-at mime-edit-beginning-tag-regexp)
2324 (insert (mime-make-text-tag) "\n")))
2325 (remove-text-properties beg end '(invisible mime-edit-invisible))
2328 ((mime-test-content-type contype "message")
2329 ;; Content-type "message" should be sent as is.
2331 ((mime-test-content-type contype "text")
2332 ;; Define charset for text if necessary.
2333 (setq charset (if charset
2334 (intern (downcase charset))
2335 (mime-edit-choose-charset)))
2336 (mime-edit-define-charset charset)
2337 (cond ((string-equal contype "text/x-rot13-47-48")
2340 (mule-caesar-region (point) (mime-edit-content-end))))
2341 ((string-equal contype "text/enriched")
2346 (end (mime-edit-content-end)))
2347 ;; Patch for hard newlines
2350 ;; (while (search-forward "\n" end t)
2351 ;; (put-text-property (match-beginning 0)
2354 ;; End patch for hard newlines
2355 (enriched-encode beg end nil)
2357 (if (search-forward "\n\n")
2358 (delete-region beg (match-end 0)))))))
2359 ;; Point is now on current tag.
2360 ;; Define encoding and encode text if necessary.
2361 (or encoding ;Encoding is not specified.
2364 (let ((ret (cdr (assq charset mime-charset-type-list))))
2366 (setq bits (car ret)
2369 conv "quoted-printable")))
2370 (if (<= bits mime-transfer-level)
2371 (mime-encoding-name bits)
2373 (beg (mime-edit-content-beginning)))
2374 (encode-mime-charset-region beg (mime-edit-content-end)
2376 ;; Protect "From " in beginning of line
2378 (narrow-to-region beg (mime-edit-content-end))
2380 (let (case-fold-search)
2381 (if (re-search-forward "^From " nil t)
2383 (if (memq charset '(iso-2022-jp
2388 (replace-match "\e(BFrom ")
2389 (re-search-forward "^From " nil t)))
2390 (setq encoding "quoted-printable"))))))
2391 ;; canonicalize line break code
2392 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2394 (narrow-to-region beg (mime-edit-content-end))
2396 (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2397 ;; In a certain period, `replace-match' with "\\N"
2398 ;; converted 8-bit characters into multibyte string,
2399 ;; but it has been fixed at 2004-01-15.
2400 ;;(replace-match "\\1\r\n"))))
2405 (mime-encode-region beg (mime-edit-content-end)
2406 (or encoding "7bit"))
2407 (mime-edit-define-encoding encoding)))
2408 (goto-char (mime-edit-content-end)))
2409 ((null encoding) ;Encoding is not specified.
2410 ;; Application, image, audio, video, and any other
2411 ;; unknown content-type without encoding should be
2413 (let* ((encoding "base64") ;Encode in BASE64 by default.
2414 (beg (mime-edit-content-beginning))
2415 (end (mime-edit-content-end)))
2416 (mime-encode-region beg end encoding)
2417 (mime-edit-define-encoding encoding))
2418 (forward-line 1))))))
2420 (defun mime-delete-field (field)
2421 "Delete header FIELD."
2422 (let ((regexp (format "^%s:[ \t]*" field)))
2423 (goto-char (point-min))
2424 (while (re-search-forward regexp nil t)
2425 (delete-region (match-beginning 0)
2426 (1+ (std11-field-end))))))
2430 ;;; Platform dependent functions
2433 ;; Sun implementations
2435 (defun mime-edit-voice-recorder-for-sun (encoding)
2436 "Record voice in a buffer using Sun audio device,
2437 and insert data encoded as ENCODING."
2438 (message "Start the recording on %s. Type C-g to finish the recording..."
2440 (mime-insert-encoded-file "/dev/audio" encoding))
2443 ;;; @ Other useful commands.
2446 ;; Message forwarding commands as content-type "message/rfc822".
2448 (defun mime-edit-insert-message (&optional message)
2450 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2451 (if (and inserter (fboundp inserter))
2453 (mime-edit-insert-tag "message" "rfc822")
2454 (funcall inserter message))
2455 (message "Sorry, I don't have message inserter for your MUA."))))
2457 (defun mime-edit-insert-mail (&optional message)
2459 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2460 (if (and inserter (fboundp inserter))
2462 (mime-edit-insert-tag "message" "rfc822")
2463 (funcall inserter message))
2464 (message "Sorry, I don't have mail inserter for your MUA."))))
2466 (defun mime-edit-inserted-message-filter ()
2469 (let ((header-start (point))
2470 (case-fold-search t)
2473 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2474 (if (re-search-forward "^$" (mark t))
2475 (narrow-to-region header-start (match-beginning 0)))
2476 (goto-char header-start)
2477 (while (and (re-search-forward
2478 mime-edit-yank-ignored-field-regexp nil t)
2479 (setq beg (match-beginning 0))
2480 (setq end (1+ (std11-field-end))))
2481 (delete-region beg end))))))
2484 ;;; @ multipart enclosure
2487 (defun mime-edit-enclose-region-internal (type beg end)
2491 (narrow-to-region beg end)
2492 (insert (format "--<<%s>>-{\n" type))
2493 (goto-char (point-max))
2494 (insert (format "--}-<<%s>>\n" type))
2495 (goto-char (point-max)))
2496 (or (looking-at mime-edit-beginning-tag-regexp)
2498 (insert (mime-make-text-tag) "\n"))))
2500 (defun mime-edit-enclose-quote-region (beg end)
2502 (mime-edit-enclose-region-internal 'quote beg end))
2504 (defun mime-edit-enclose-mixed-region (beg end)
2506 (mime-edit-enclose-region-internal 'mixed beg end))
2508 (defun mime-edit-enclose-parallel-region (beg end)
2510 (mime-edit-enclose-region-internal 'parallel beg end))
2512 (defun mime-edit-enclose-digest-region (beg end)
2514 (mime-edit-enclose-region-internal 'digest beg end))
2516 (defun mime-edit-enclose-alternative-region (beg end)
2518 (mime-edit-enclose-region-internal 'alternative beg end))
2520 (defun mime-edit-enclose-pgp-signed-region (beg end)
2522 (mime-edit-enclose-region-internal 'pgp-signed beg end))
2524 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2526 (mime-edit-enclose-region-internal 'pgp-encrypted beg end))
2528 (defun mime-edit-enclose-kazu-signed-region (beg end)
2530 (mime-edit-enclose-region-internal 'kazu-signed beg end))
2532 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2534 (mime-edit-enclose-region-internal 'kazu-encrypted beg end))
2536 (defun mime-edit-enclose-smime-signed-region (beg end)
2538 (mime-edit-enclose-region-internal 'smime-signed beg end))
2540 (defun mime-edit-enclose-smime-encrypted-region (beg end)
2542 (mime-edit-enclose-region-internal 'smime-encrypted beg end))
2544 (defun mime-edit-insert-key (&optional arg)
2545 "Insert a pgp public key."
2547 (mime-edit-insert-tag "application" "pgp-keys")
2548 (mime-edit-define-encoding "7bit")
2549 (let ((context (epg-make-context)))
2550 (epg-context-set-armor context t)
2551 (epg-export-keys-to-string context
2552 (epa-select-keys context
2553 "Select keys for export. ")))
2554 (if (and (not (eobp))
2555 (not (looking-at mime-edit-single-part-tag-regexp)))
2556 (insert (mime-make-text-tag) "\n")))
2562 (defun mime-edit-set-split (arg)
2565 (y-or-n-p "Do you want to enable split? ")))
2566 (setq mime-edit-split-message arg)
2568 (message "This message is enabled to split.")
2569 (message "This message is not enabled to split.")))
2571 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2572 "Toggle transfer-level is 7bit or 8bit through.
2574 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2576 (if (numberp transfer-level)
2577 (setq mime-transfer-level transfer-level)
2578 (if (< mime-transfer-level 8)
2579 (setq mime-transfer-level 8)
2580 (setq mime-transfer-level 7)))
2581 (message (format "Current transfer-level is %d bit"
2582 mime-transfer-level))
2583 (setq mime-transfer-level-string
2584 (mime-encoding-name mime-transfer-level 'not-omit))
2585 (force-mode-line-update))
2587 (defun mime-edit-set-transfer-level-7bit ()
2589 (mime-edit-toggle-transfer-level 7))
2591 (defun mime-edit-set-transfer-level-8bit ()
2593 (mime-edit-toggle-transfer-level 8))
2599 (defvar mime-edit-pgp-processing nil)
2600 (make-variable-buffer-local 'mime-edit-pgp-processing)
2602 (defun mime-edit-set-sign (arg)
2605 (y-or-n-p "Do you want to sign? ")))
2608 (or (memq 'sign mime-edit-pgp-processing)
2609 (setq mime-edit-pgp-processing
2610 (nconc mime-edit-pgp-processing
2611 (copy-sequence '(sign)))))
2612 (message "This message will be signed."))
2613 (setq mime-edit-pgp-processing
2614 (delq 'sign mime-edit-pgp-processing))
2615 (message "This message will not be signed.")))
2617 (defun mime-edit-set-encrypt (arg)
2620 (y-or-n-p "Do you want to encrypt? ")))
2623 (or (memq 'encrypt mime-edit-pgp-processing)
2624 (setq mime-edit-pgp-processing
2625 (nconc mime-edit-pgp-processing
2626 (copy-sequence '(encrypt)))))
2627 (message "This message will be encrypt."))
2628 (setq mime-edit-pgp-processing
2629 (delq 'encrypt mime-edit-pgp-processing))
2630 (message "This message will not be encrypt.")))
2632 (defun mime-edit-pgp-enclose-buffer ()
2633 (let ((beg (save-excursion
2634 (goto-char (point-min))
2635 (if (search-forward (concat "\n" mail-header-separator "\n"))
2638 (dolist (pgp-processing mime-edit-pgp-processing)
2639 (case pgp-processing
2641 (mime-edit-enclose-pgp-signed-region
2644 (mime-edit-enclose-pgp-encrypted-region
2645 beg (point-max))))))))
2651 (defun mime-edit-insert-partial-header (fields subject
2652 id number total separator)
2654 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2655 (insert mime-edit-mime-version-field-for-message/partial)
2657 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2658 id number total separator)))
2660 (defun mime-edit-split-and-send
2661 (&optional cmd lines mime-edit-message-max-length)
2665 (count-lines (point-min) (point-max))))
2666 (or mime-edit-message-max-length
2667 (setq mime-edit-message-max-length
2668 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2669 mime-edit-message-default-max-lines)))
2670 (let ((separator mail-header-separator)
2672 (replace-space-with-underline (current-time-string))
2673 "@" (system-name) "\"")))
2674 (run-hooks 'mime-edit-before-split-hook)
2675 (let ((the-buf (current-buffer))
2676 (copy-buf (get-buffer-create " *Original Message*"))
2677 (header (std11-header-string-except
2678 mime-edit-split-ignored-field-regexp separator))
2679 (subject (mail-fetch-field "subject"))
2680 (total (+ (/ lines mime-edit-message-max-length)
2681 (if (> (mod lines mime-edit-message-max-length) 0)
2687 mime-edit-split-message-sender-alist))
2691 (error "Split sender is not specified for `%s'." major-mode)))))
2692 (mime-edit-partial-number 1)
2695 (set-buffer copy-buf)
2697 (insert-buffer the-buf)
2699 (if (re-search-forward
2700 (concat "^" (regexp-quote separator) "$") nil t)
2701 (let ((he (match-beginning 0)))
2703 (narrow-to-region (point-min) he)))
2704 (goto-char (point-min))
2705 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2706 (delete-region (match-beginning 0)
2707 (1+ (std11-field-end))))))
2708 (while (< mime-edit-partial-number total)
2711 (set-buffer copy-buf)
2712 (setq data (buffer-substring
2715 (goto-line mime-edit-message-max-length)
2717 (delete-region (point-min)(point)))
2718 (mime-edit-insert-partial-header
2719 header subject id mime-edit-partial-number total separator)
2722 (message (format "Sending %d/%d..."
2723 mime-edit-partial-number total))
2724 (call-interactively command)
2725 (message (format "Sending %d/%d...done"
2726 mime-edit-partial-number total)))
2727 (setq mime-edit-partial-number
2728 (1+ mime-edit-partial-number)))
2731 (set-buffer copy-buf)
2732 (setq data (buffer-string))
2734 (mime-edit-insert-partial-header
2735 header subject id mime-edit-partial-number total separator)
2738 (message (format "Sending %d/%d..."
2739 mime-edit-partial-number total))
2740 (message (format "Sending %d/%d...done"
2741 mime-edit-partial-number total))))))
2743 (defun mime-edit-maybe-split-and-send (&optional cmd)
2745 (run-hooks 'mime-edit-before-send-hook)
2746 (let ((mime-edit-message-max-length
2747 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2748 mime-edit-message-default-max-lines))
2749 (lines (count-lines (point-min) (point-max))))
2750 (if (and (> lines mime-edit-message-max-length)
2751 mime-edit-split-message)
2752 (mime-edit-split-and-send cmd lines mime-edit-message-max-length))))
2755 ;;; @ preview message
2758 (defvar mime-edit-buffer nil) ; buffer local variable
2759 (defvar mime-edit-temp-message-buffer nil) ; buffer local variable
2761 (defun mime-edit-preview-message ()
2762 "preview editing MIME message."
2764 (let* ((str (buffer-string))
2765 (separator mail-header-separator)
2766 (the-buf (current-buffer))
2767 (buf-name (buffer-name))
2768 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2769 (buf (get-buffer temp-buf-name))
2770 (pgp-processing mime-edit-pgp-processing))
2773 (switch-to-buffer buf)
2775 (setq buf (get-buffer-create temp-buf-name))
2776 (switch-to-buffer buf))
2778 (setq major-mode 'mime-temp-message-mode)
2779 (make-local-variable 'mail-header-separator)
2780 (setq mail-header-separator separator)
2781 (make-local-variable 'mime-edit-buffer)
2782 (setq mime-edit-buffer the-buf)
2783 (setq mime-edit-pgp-processing pgp-processing)
2785 (run-hooks 'mime-edit-translate-hook)
2786 (mime-edit-translate-buffer)
2787 (goto-char (point-min))
2788 (if (re-search-forward
2789 (concat "^" (regexp-quote separator) "$"))
2792 (make-local-variable 'mime-edit-temp-message-buffer)
2793 (setq mime-edit-temp-message-buffer buf)))
2795 (defun mime-edit-quitting-method ()
2796 "Quitting method for mime-view."
2797 (let* ((temp mime-edit-temp-message-buffer)
2799 (mime-preview-kill-buffer)
2801 (setq buf mime-edit-buffer)
2803 (switch-to-buffer buf)))
2805 (set-alist 'mime-preview-quitting-method-alist
2806 'mime-temp-message-mode
2807 #'mime-edit-quitting-method)
2813 (defvar mime-edit-again-ignored-field-regexp
2814 (concat "^\\(" "Content-.*\\|Mime-Version"
2815 (if mime-edit-insert-user-agent-field "\\|User-Agent")
2817 "Regexp for deleted header fields when `mime-edit-again' is called.")
2819 (defsubst eliminate-top-spaces (string)
2820 "Eliminate top sequence of space or tab in STRING."
2821 (if (string-match "^[ \t]+" string)
2822 (substring string (match-end 0))
2825 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
2828 (cdr (assoc (mime-content-type-parameter content-type "protocol")
2829 '(("application/pgp-encrypted" . pgp-encrypted)
2830 ("application/pgp-signature" . pgp-signed))))
2831 (mime-content-type-subtype content-type)))
2832 (boundary (mime-content-type-parameter content-type "boundary"))
2833 (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
2834 (re-search-forward boundary-pat nil t)
2835 (let ((bb (match-beginning 0)) eb tag)
2836 (setq tag (format "\n--<<%s>>-{\n" subtype))
2839 (setq bb (+ bb (length tag)))
2841 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2843 (setq eb (match-beginning 0))
2844 (replace-match (format "--}-<<%s>>\n" subtype))
2846 (narrow-to-region bb eb)
2847 (goto-char (point-min))
2848 (while (re-search-forward boundary-pat nil t)
2849 (let ((beg (match-beginning 0))
2851 (delete-region beg (match-end 0))
2853 (if (re-search-forward boundary-pat nil t)
2854 (setq end (match-beginning 0))
2855 (setq end (point-max)))
2857 (narrow-to-region beg end)
2859 ((eq subtype 'pgp-encrypted)
2861 (goto-char (point-min))
2862 (re-search-forward "^-+BEGIN PGP MESSAGE-+$"
2864 (insert (epg-decrypt-string
2866 (buffer-substring (match-beginning 0)
2868 (delete-region (point)(point-max))
2869 (mime-edit-decode-message-in-buffer
2870 nil not-decode-text)
2871 (delete-region (goto-char (point-min))
2872 (if (search-forward "\n\n" nil t)
2875 (goto-char (point-max))))
2877 (mime-edit-decode-message-in-buffer
2878 (if (eq subtype 'digest)
2880 (make-mime-content-type 'message 'rfc822)))
2882 (goto-char (point-max))))))))))
2883 (goto-char (point-min))
2884 (or (= (point-min) 1)
2885 (delete-region (point-min)
2886 (if (search-forward "\n\n" nil t)
2890 (defun mime-edit-decode-single-part-in-buffer
2891 (content-type not-decode-text &optional content-disposition)
2892 (let* ((type (mime-content-type-primary-type content-type))
2893 (subtype (mime-content-type-subtype content-type))
2894 (ctype (format "%s/%s" type subtype))
2896 (pstr (let ((bytes (+ 14 (length ctype))))
2897 (mapconcat (function
2899 (if (string= (car attr) "charset")
2901 (setq charset (cdr attr))
2903 (let* ((str (concat (car attr)
2906 (setq bytes (+ bytes bs 2))
2909 (setq bytes (+ bs 1))
2912 (mime-content-type-parameters content-type) "")))
2915 (limit (save-excursion
2916 (if (search-forward "\n\n" nil t)
2919 (mime-content-disposition-type content-disposition))
2921 (if disposition-type
2922 (let ((bytes (+ 21 (length (format "%s" disposition-type)))))
2923 (mapconcat (function
2928 (if (string-equal "filename"
2930 (std11-wrap-as-quoted-string
2934 (setq bytes (+ bytes bs 2))
2937 (setq bytes (+ bs 1))
2940 (mime-content-disposition-parameters
2941 content-disposition)
2943 (if disposition-type
2944 (setq pstr (format "%s\nContent-Disposition: %s%s"
2945 pstr disposition-type disposition-str)))
2947 (if (re-search-forward
2948 "^Content-Transfer-Encoding:" limit t)
2949 (let ((beg (match-beginning 0))
2950 (hbeg (match-end 0))
2951 (end (std11-field-end limit)))
2954 (eliminate-top-spaces
2955 (std11-unfold-string
2956 (buffer-substring hbeg end)))))
2957 (if (or charset (eq type 'text))
2959 (delete-region beg (1+ end))
2960 (goto-char (point-min))
2961 (if (search-forward "\n\n" nil t)
2964 (match-end 0)(point-max) encoding)
2966 encoding nil))))))))
2967 (if (and (eq type 'text)
2968 (or encoded (not not-decode-text)))
2971 (goto-char (point-min))
2972 (while (re-search-forward "\r\n" nil t)
2973 (replace-match "\n")))
2974 (decode-mime-charset-region (point-min)(point-max)
2975 (or charset default-mime-charset))))
2976 (let ((he (if (re-search-forward "^$" nil t)
2979 (if (and (eq type 'text)
2980 (eq subtype 'x-rot13-47-48))
2981 (mule-caesar-region he (point-max)))
2982 (if (= (point-min) 1)
2988 (format "%s/%s%s" type subtype pstr)
2990 (delete-region (point-min) he)
2992 (mime-create-tag (format "%s/%s%s" type subtype pstr)
2996 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
2999 (goto-char (point-min))
3000 (let ((ctl (or (mime-read-Content-Type)
3001 default-content-type)))
3003 (let ((type (mime-content-type-primary-type ctl)))
3005 ((and (eq type 'application)
3006 (eq (mime-content-type-subtype ctl) 'pgp-signature))
3007 (delete-region (point-min)(point-max)))
3008 ((eq type 'multipart)
3009 (mime-edit-decode-multipart-in-buffer ctl not-decode-text))
3011 (mime-edit-decode-single-part-in-buffer
3012 ctl not-decode-text (mime-read-Content-Disposition)))))
3014 (decode-mime-charset-region (point-min) (point-max)
3015 default-mime-charset)))
3016 (if (= (point-min) 1)
3019 (std11-narrow-to-header)
3020 (goto-char (point-min))
3021 (while (re-search-forward
3022 mime-edit-again-ignored-field-regexp nil t)
3023 (delete-region (match-beginning 0) (1+ (std11-field-end)))))
3024 (mime-decode-header-in-buffer (not not-decode-text)))))))
3027 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
3028 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
3029 Content-Type and Content-Transfer-Encoding header fields will be
3030 converted to MIME-Edit tags."
3032 (goto-char (point-min))
3034 (concat "\n" (regexp-quote mail-header-separator) "\n")
3036 (replace-match "\n\n"))
3037 (mime-edit-decode-message-in-buffer nil not-decode-text)
3038 (goto-char (point-min))
3040 (and (re-search-forward "^$")
3041 (replace-match mail-header-separator)))
3043 (turn-on-mime-edit)))
3049 (provide 'mime-edit)
3051 (run-hooks 'mime-edit-load-hook)
3053 ;;; mime-edit.el ends here