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@ueda.info.waseda.ac.jp>
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., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, 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)
120 (autoload 'pgg-encrypt-region "pgg"
121 "PGP encryption of current region." t)
122 (autoload 'pgg-sign-region "pgg"
123 "PGP signature of current region." t)
124 (autoload 'pgg-insert-key "pgg"
125 "Insert PGP public key at point." t)
126 (autoload 'smime-encrypt-region "smime"
127 "S/MIME encryption of current region.")
128 (autoload 'smime-sign-region "smime"
129 "S/MIME signature of current region.")
130 (defvar smime-output-buffer)
131 (defvar smime-errors-buffer)
138 (defconst mime-edit-version
140 (mime-product-name mime-user-interface-product) " "
141 (mapconcat #'number-to-string
142 (mime-product-version mime-user-interface-product) ".")
143 " - \"" (mime-product-code-name mime-user-interface-product) "\"")))
149 (defgroup mime-edit nil
153 (defcustom mime-ignore-preceding-spaces nil
154 "*Ignore preceding white spaces if non-nil."
158 (defcustom mime-ignore-trailing-spaces nil
159 "*Ignore trailing white spaces if non-nil."
163 (defcustom mime-ignore-same-text-tag t
164 "*Ignore preceding text content-type tag that is same with new one.
165 If non-nil, the text tag is not inserted unless something different."
169 (defcustom mime-auto-hide-body t
170 "*Hide non-textual body encoded in base64 after insertion if non-nil."
174 (defcustom mime-edit-voice-recorder
175 (function mime-edit-voice-recorder-for-sun)
176 "*Function to record a voice message and encode it."
180 (defcustom mime-edit-mode-hook nil
181 "*Hook called when enter MIME mode."
185 (defcustom mime-edit-translate-hook nil
186 "*Hook called before translating into a MIME compliant message.
187 To insert a signature file automatically, call the function
188 `mime-edit-insert-signature' from this hook."
192 (defcustom mime-edit-exit-hook nil
193 "*Hook called when exit MIME mode."
197 (defvar mime-content-types
199 ;; Charset parameter need not to be specified, since it is
200 ;; defined automatically while translation.
202 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
215 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
216 ("directory" "/pub/GNU/elisp/mime")
218 ("mode" "image" "ascii" "local8"))
223 ("mode" "image" "ascii" "local8"))
224 ("tftp" ("site") ("name"))
225 ("afs" ("site") ("name"))
226 ("local-file" ("site") ("name"))
228 ("server" "ftpmail@nic.karrn.ad.jp")
236 ("octet-stream" ("type" "" "tar" "shar"))
238 ("vnd.ms-powerpoint")
239 ("x-kiss" ("x-cnf")))
253 "*Alist of content-type, subtype, parameters and its values.")
255 (defcustom mime-file-types
258 ;; Programming languages
261 "application" "octet-stream" (("type" . "C++"))
263 "attachment" (("filename" . file))
267 "application" "octet-stream" (("type" . "emacs-lisp"))
269 "attachment" (("filename" . file))
273 "application" "octet-stream" (("type" . "common-lisp"))
275 "attachment" (("filename" . file))
279 "application" "octet-stream" (("type" . "perl"))
281 "attachment" (("filename" . file))
284 ;; Text or translated text
289 "inline" (("filename" . file))
292 ;; .rc : procmail modules pm-xxxx.rc
293 ;; *rc : other resource files
295 ("\\.\\(rc\\|lst\\|log\\|sql\\|mak\\)$\\|\\..*rc$"
298 "attachment" (("filename" . file))
306 ("\\.diff$\\|\\.patch$"
307 "application" "octet-stream" (("type" . "patch"))
309 "attachment" (("filename" . file))
313 "text" "plain" nil nil nil nil)
316 ;; Octect binary text
319 "application" "msword" nil
321 "attachment" (("filename" . file))
323 ("\\.ppt$" ; MS Power Point
324 "application" "vnd.ms-powerpoint" nil
326 "attachment" (("filename" . file))
332 "inline" (("filename" . file))
335 "application" "postscript" nil
337 "attachment" (("filename" . file))
345 "inline" (("filename" . file))
350 "inline" (("filename" . file))
355 "inline" (("filename" . file))
360 "inline" (("filename" . file))
365 "inline" (("filename" . file))
370 "inline" (("filename" . file))
375 "inline" (("filename" . file))
380 "inline" (("filename" . file))
385 "attachment" (("filename" . file))
390 "attachment" (("filename" . file))
393 "application" "octet-stream" (("type" . "tar+gzip"))
395 "attachment" (("filename" . file))
398 "application" "octet-stream" (("type" . "tar+gzip"))
400 "attachment" (("filename" . file))
403 "application" "octet-stream" (("type" . "tar+compress"))
405 "attachment" (("filename" . file))
408 "application" "octet-stream" (("type" . "tar+compress"))
410 "attachment" (("filename" . file))
413 "application" "octet-stream" (("type" . "gzip"))
415 "attachment" (("filename" . file))
418 "application" "octet-stream" (("type" . "compress"))
420 "attachment" (("filename" . file))
423 "application" "octet-stream" (("type" . "lha"))
425 "attachment" (("filename" . file))
428 "application" "zip" nil
430 "attachment" (("filename" . file))
436 "application" "octet-stream" nil
438 "attachment" (("filename" . file)))
440 "*Alist of file name, types, parameters, and default encoding.
441 If encoding is nil, it is determined from its contents."
445 (choice :tag "Primary-Type"
446 ,@(nconc (mapcar (lambda (cell)
447 (list 'item (car cell))
452 (choice :tag "Sub-Type"
455 (mapcar (lambda (cell)
456 (mapcar (lambda (cell)
457 (list 'item (car cell))
463 (repeat :tag "Parameters of Content-Type field"
464 (cons string (choice string symbol)))
465 ;; content-transfer-encoding
466 (choice :tag "Encoding"
469 (mapcar (lambda (cell)
472 (mime-encoding-list))))
474 (choice :tag "Disposition-Type"
480 (repeat :tag "Parameters of Content-Disposition field"
481 (cons string (choice string symbol)))
486 ;;; @@ about charset, encoding and transfer-level
489 (defvar mime-charset-type-list
491 (iso-8859-1 8 "quoted-printable")
492 (iso-8859-2 8 "quoted-printable")
493 (iso-8859-3 8 "quoted-printable")
494 (iso-8859-4 8 "quoted-printable")
495 (iso-8859-5 8 "quoted-printable")
496 (koi8-r 8 "quoted-printable")
497 (iso-8859-7 8 "quoted-printable")
498 (iso-8859-8 8 "quoted-printable")
499 (iso-8859-9 8 "quoted-printable")
500 (iso-2022-jp 7 "base64")
501 (iso-2022-jp-3 7 "base64")
502 (iso-2022-kr 7 "base64")
508 (shift_jis 8 "base64")
510 (iso-2022-jp-2 7 "base64")
511 (iso-2022-int-1 7 "base64")
514 (defvar mime-transfer-level 7
515 "*A number of network transfer level. It should be bigger than 7.")
516 (make-variable-buffer-local 'mime-transfer-level)
518 (defsubst mime-encoding-name (transfer-level &optional not-omit)
519 (cond ((> transfer-level 8) "binary")
520 ((= transfer-level 8) "8bit")
524 (defvar mime-transfer-level-string
525 (mime-encoding-name mime-transfer-level 'not-omit)
526 "A string formatted version of mime-transfer-level")
527 (make-variable-buffer-local 'mime-transfer-level-string)
529 ;;; @@ about content transfer encoding
531 (defvar mime-content-transfer-encoding-priority-list
532 '(nil "8bit" "binary"))
534 ;;; @@ about message inserting
537 (defvar mime-edit-yank-ignored-field-list
538 '("Received" "Approved" "Path" "Replied" "Status"
539 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
540 "Delete these fields from original message when it is inserted
541 as message/rfc822 part.
542 Each elements are regexp of field-name.")
544 (defvar mime-edit-yank-ignored-field-regexp
546 (apply (function regexp-or) mime-edit-yank-ignored-field-list)
549 (defvar mime-edit-message-inserter-alist nil)
550 (defvar mime-edit-mail-inserter-alist nil)
553 ;;; @@ about message splitting
556 (defcustom mime-edit-split-message t
557 "*Split large message if it is non-nil."
561 (defcustom mime-edit-message-default-max-lines 1000
562 "*Default maximum lines of a message."
566 (defcustom mime-edit-message-max-lines-alist
567 '((news-reply-mode . 500))
568 "Alist of major-mode vs maximum lines of a message.
569 If it is not specified for a major-mode,
570 `mime-edit-message-default-max-lines' is used."
574 (defconst mime-edit-split-ignored-field-regexp
575 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|^Message-Id:\\)")
577 (defcustom mime-edit-split-blind-field-regexp
578 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
579 "*Regular expression to match field-name to be ignored when split sending."
583 (defvar mime-edit-split-message-sender-alist nil)
585 (defvar mime-edit-news-reply-mode-server-running nil)
591 (defconst mime-edit-single-part-tag-regexp
592 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
593 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
595 (defconst mime-edit-quoted-single-part-tag-regexp
596 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
598 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
600 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
602 (defconst mime-edit-beginning-tag-regexp
603 (regexp-or mime-edit-single-part-tag-regexp
604 mime-edit-multipart-beginning-regexp))
606 (defconst mime-edit-end-tag-regexp
607 (regexp-or mime-edit-single-part-tag-regexp
608 mime-edit-multipart-end-regexp))
610 (defconst mime-edit-tag-regexp
611 (regexp-or mime-edit-single-part-tag-regexp
612 mime-edit-multipart-beginning-regexp
613 mime-edit-multipart-end-regexp))
615 (defvar mime-tag-format "--[[%s]]"
616 "*Control-string making a MIME tag.")
618 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
619 "*Control-string making a MIME tag with encoding.")
622 ;;; @@ multipart boundary
625 (defvar mime-multipart-boundary "Multipart"
626 "*Boundary of a multipart message.")
629 ;;; @@ optional header fields
632 (defvar mime-edit-insert-user-agent-field t
633 "*If non-nil, insert User-Agent header field.")
635 (defvar mime-edit-user-agent-value
636 (concat (mime-product-name mime-user-interface-product)
638 (mapconcat #'number-to-string
639 (mime-product-version mime-user-interface-product) ".")
641 (mime-product-code-name mime-user-interface-product)
643 (mime-product-name mime-library-product)
645 (mapconcat #'number-to-string
646 (mime-product-version mime-library-product) ".")
648 (mime-product-code-name mime-library-product)
650 (if (fboundp 'apel-version)
651 (concat (apel-version) " "))
652 (if (featurep 'xemacs)
653 (concat (cond ((featurep 'utf-2000)
654 (concat "UTF-2000-MULE/" utf-2000-version))
655 ((featurep 'mule) "MULE"))
657 (if (string-match "^[0-9]+\\(\\.[0-9]+\\)" emacs-version)
660 (substring emacs-version 0 (match-end 0))
661 (cond ((and (boundp 'xemacs-betaname)
663 ;; It does not exist in XEmacs
664 ;; versions prior to 20.3.
665 (concat " " xemacs-betaname))
666 ((and (boundp 'emacs-patch-level)
668 ;; It does not exist in FSF Emacs or in
669 ;; XEmacs versions earlier than 21.1.1.
670 (format " (patch %d)" emacs-patch-level))
672 " (" xemacs-codename ")"
673 ;; `xemacs-extra-name' has appeared in the
674 ;; development version of XEmacs 21.5-b8.
675 (if (and (boundp 'xemacs-extra-name)
676 (symbol-value 'xemacs-extra-name))
677 (concat " " (symbol-value 'xemacs-extra-name))
680 system-configuration ")")
681 " (" emacs-version ")"))
682 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
683 (substring emacs-version 0 (match-beginning 0))
686 (if (boundp 'enable-multibyte-characters)
688 " (" system-configuration ")"
689 (if enable-multibyte-characters
690 (concat " MULE/" mule-version)
691 " (with unibyte mode)")
692 (if (featurep 'meadow)
693 (let ((mver (Meadow-version)))
694 (if (string-match "^Meadow-" mver)
699 (concat "MULE/" mule-version
700 " (based on Emacs " ver ")"))
701 (concat "Emacs/" ver " (" system-configuration ")")))))
702 "Body of User-Agent field.
703 If variable `mime-edit-insert-user-agent-field' is not nil, it is
704 inserted into message header.")
710 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
711 "*Specify MIME tspecials.
712 Tspecials means any character that matches with it in header must be quoted.")
714 (defconst mime-edit-mime-version-value
715 (concat "1.0 (generated by " mime-edit-version ")")
716 "MIME version number.")
718 (defconst mime-edit-mime-version-field-for-message/partial
719 (concat "MIME-Version:"
720 (mime-encode-field-body
721 (concat " 1.0 (split by " mime-edit-version ")\n")
723 "MIME version field for message/partial.")
726 ;;; @ keymap and menu
729 (defvar mime-edit-mode-flag nil)
730 (make-variable-buffer-local 'mime-edit-mode-flag)
732 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
733 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
734 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
735 "Keymap for MIME-Edit mode commands to insert entity or set status.")
737 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
738 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
739 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
740 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
741 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
742 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
743 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
744 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
745 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
746 (define-key mime-edit-mode-entity-map "t" 'mime-edit-insert-tag)
748 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
749 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
750 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
751 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
752 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
753 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
754 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
755 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
756 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
757 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
759 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
760 "Keymap prefix for MIME-Edit mode commands about enclosure.")
761 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
762 "Keymap for MIME-Edit mode commands about enclosure.")
764 (define-key mime-edit-mode-enclosure-map
765 "\C-a" 'mime-edit-enclose-alternative-region)
766 (define-key mime-edit-mode-enclosure-map
767 "\C-p" 'mime-edit-enclose-parallel-region)
768 (define-key mime-edit-mode-enclosure-map
769 "\C-m" 'mime-edit-enclose-mixed-region)
770 (define-key mime-edit-mode-enclosure-map
771 "\C-d" 'mime-edit-enclose-digest-region)
772 (define-key mime-edit-mode-enclosure-map
773 "\C-s" 'mime-edit-enclose-pgp-signed-region)
774 (define-key mime-edit-mode-enclosure-map
775 "\C-e" 'mime-edit-enclose-pgp-encrypted-region)
776 (define-key mime-edit-mode-enclosure-map
777 "\C-q" 'mime-edit-enclose-quote-region)
779 (defvar mime-edit-mode-map (make-sparse-keymap)
780 "Keymap for MIME-Edit mode commands.")
781 (define-key mime-edit-mode-map
782 mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
783 (define-key mime-edit-mode-map
784 mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
786 (defconst mime-edit-menu-title "MIME-Edit")
788 (defconst mime-edit-menu-list
789 '((mime-help "Describe MIME editor mode" mime-edit-help)
790 (file "Insert File" mime-edit-insert-file)
791 (external "Insert External" mime-edit-insert-external)
792 (voice "Insert Voice" mime-edit-insert-voice)
793 (message "Insert Message" mime-edit-insert-message)
794 (mail "Insert Mail" mime-edit-insert-mail)
795 (signature "Insert Signature" mime-edit-insert-signature)
796 (text "Insert Text" mime-edit-insert-text)
797 (tag "Insert Tag" mime-edit-insert-tag)
798 (alternative "Enclose as alternative"
799 mime-edit-enclose-alternative-region)
800 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
801 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
802 (digest "Enclose as digest" mime-edit-enclose-digest-region)
803 (signed "Enclose as signed" mime-edit-enclose-pgp-signed-region)
804 (encrypted "Enclose as encrypted" mime-edit-enclose-pgp-encrypted-region)
805 (quote "Verbatim region" mime-edit-enclose-quote-region)
806 (key "Insert Public Key" mime-edit-insert-key)
807 (split "About split" mime-edit-set-split)
808 (sign "About sign" mime-edit-set-sign)
809 (encrypt "About encryption" mime-edit-set-encrypt)
810 (preview "Preview Message" mime-edit-preview-message)
811 (level "Toggle transfer-level" mime-edit-toggle-transfer-level)
813 "MIME-edit menubar entry.")
815 (cond ((featurep 'xemacs)
816 ;; modified by Pekka Marjola <pema@iki.fi>
817 ;; 1995/9/5 (c.f. [tm-en:69])
818 (defun mime-edit-define-menu-for-xemacs ()
819 "Define menu for XEmacs."
820 (cond ((featurep 'menubar)
821 (make-local-variable 'current-menubar)
822 (set-buffer-menubar current-menubar)
825 (cons mime-edit-menu-title
828 (vector (nth 1 item)(nth 2 item)
831 mime-edit-menu-list)))
834 ;; modified by Steven L. Baur <steve@miranova.com>
835 ;; 1995/12/6 (c.f. [tm-en:209])
836 (or (boundp 'mime-edit-popup-menu-for-xemacs)
837 (setq mime-edit-popup-menu-for-xemacs
838 (append '("MIME Commands" "---")
839 (mapcar (function (lambda (item)
843 mime-edit-menu-list)))
846 ((>= emacs-major-version 19)
847 (define-key mime-edit-mode-map [menu-bar mime-edit]
848 (cons mime-edit-menu-title
849 (make-sparse-keymap mime-edit-menu-title)))
852 (define-key mime-edit-mode-map
853 (vector 'menu-bar 'mime-edit (car item))
854 (cons (nth 1 item)(nth 2 item))
857 (reverse mime-edit-menu-list)
865 (defvar mime-edit-touched-flag nil)
868 (defun mime-edit-mode ()
869 "MIME minor mode for editing the tagged MIME message.
871 In this mode, basically, the message is composed in the tagged MIME
872 format. The message tag looks like:
874 --[[text/plain; charset=ISO-2022-JP][7bit]]
876 The tag specifies the MIME content type, subtype, optional parameters
877 and transfer encoding of the message following the tag. Messages
878 without any tag are treated as `text/plain' by default. Charset and
879 transfer encoding are automatically defined unless explicitly
880 specified. Binary messages such as audio and image are usually
881 hidden. The messages in the tagged MIME format are automatically
882 translated into a MIME compliant message when exiting this mode.
884 Available charsets depend on Emacs version being used. The following
885 lists the available charsets of each emacs.
887 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
888 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
889 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
890 CN-BIG5 and ISO-2022-INT-1 are available.
892 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
893 be used to represent multilingual text in intermixed manner. Any
894 languages that has no registered charset are represented as either
895 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
897 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
898 without mule, please set variable `default-mime-charset'. This
899 variable must be symbol of which name is a MIME charset.
901 If you want to add more charsets in mule, please set variable
902 `charsets-mime-charset-alist'. This variable must be alist of which
903 key is list of charset and value is symbol of MIME charset. If name
904 of coding-system is different as MIME charset, please set variable
905 `mime-charset-coding-system-alist'. This variable must be alist of
906 which key is MIME charset and value is coding-system.
908 Following commands are available in addition to major mode commands:
911 \\[mime-edit-insert-text] insert a text message.
912 \\[mime-edit-insert-file] insert a (binary) file.
913 \\[mime-edit-insert-external] insert a reference to external body.
914 \\[mime-edit-insert-voice] insert a voice message.
915 \\[mime-edit-insert-message] insert a mail or news message.
916 \\[mime-edit-insert-mail] insert a mail message.
917 \\[mime-edit-insert-signature] insert a signature file at end.
918 \\[mime-edit-insert-key] insert PGP public key.
919 \\[mime-edit-insert-tag] insert a new MIME tag.
921 \[make enclosure (maybe multipart)\]
922 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
923 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
924 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
925 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
926 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
927 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
928 \\[mime-edit-enclose-quote-region] enclose as verbose mode
929 (to avoid to expand tags)
932 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
933 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
934 \\[mime-edit-set-split] set message splitting mode.
935 \\[mime-edit-set-sign] set PGP-sign mode.
936 \\[mime-edit-set-encrypt] set PGP-encryption mode.
937 \\[mime-edit-preview-message] preview editing MIME message.
938 \\[mime-edit-exit] exit and translate into a MIME
940 \\[mime-edit-help] show this help.
941 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
944 Additional commands are available in some major modes:
945 C-c C-c exit, translate and run the original command.
946 C-c C-s exit, translate and run the original command.
948 The following is a message example written in the tagged MIME format.
949 TABs at the beginning of the line are not a part of the message:
951 This is a conventional plain text. It should be translated
954 This is also a plain text. But, it is explicitly specified as
956 --[[text/plain; charset=ISO-8859-1]]
957 This is also a plain text. But charset is specified as
960 ¡Hola! Buenos días. ¿Cómo está usted?
962 This is a <bold>enriched text</bold>.
963 --[[image/gif][base64]]...image encoded in base64 here...
964 --[[audio/basic][base64]]...audio encoded in base64 here...
966 User customizable variables (not documented all of them):
968 Specifies a key prefix for MIME minor mode commands.
970 mime-ignore-preceding-spaces
971 Preceding white spaces in a message body are ignored if non-nil.
973 mime-ignore-trailing-spaces
974 Trailing white spaces in a message body are ignored if non-nil.
977 Hide a non-textual body message encoded in base64 after insertion
981 A number of network transfer level. It should be bigger than 7.
982 If you are in 8bit-through environment, please set 8.
984 mime-edit-voice-recorder
985 Specifies a function to record a voice message and encode it.
986 The function `mime-edit-voice-recorder-for-sun' is for Sun
990 Turning on MIME mode calls the value of mime-edit-mode-hook, if
993 mime-edit-translate-hook
994 The value of mime-edit-translate-hook is called just before translating
995 the tagged MIME format into a MIME compliant message if it is
996 non-nil. If the hook call the function mime-edit-insert-signature,
997 the signature file will be inserted automatically.
1000 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
1003 (if mime-edit-mode-flag
1005 (if mime-edit-touched-flag
1007 (make-local-variable 'mime-edit-touched-flag)
1008 (setq mime-edit-touched-flag t)
1013 (cond ((featurep 'xemacs)
1014 (add-minor-mode 'mime-edit-mode-flag
1015 '((" MIME-Edit " mime-transfer-level-string))
1021 (set-alist 'minor-mode-alist
1022 'mime-edit-mode-flag
1023 '((" MIME-Edit " mime-transfer-level-string)))
1024 (set-alist 'minor-mode-map-alist
1025 'mime-edit-mode-flag
1031 (defun turn-on-mime-edit ()
1032 "Unconditionally turn on MIME-Edit mode."
1034 (if mime-edit-mode-flag
1035 (error "You are already editing a MIME message.")
1036 (setq mime-edit-mode-flag t)
1038 ;; Set transfer level into mode line
1040 (setq mime-transfer-level-string
1041 (mime-encoding-name mime-transfer-level 'not-omit))
1042 (force-mode-line-update)
1044 ;; Define menu for XEmacs.
1045 (if (featurep 'xemacs)
1046 (mime-edit-define-menu-for-xemacs)
1051 ;; I don't care about saving these.
1052 (setq paragraph-start
1053 (regexp-or mime-edit-single-part-tag-regexp
1055 (setq paragraph-separate
1056 (regexp-or mime-edit-single-part-tag-regexp
1057 paragraph-separate))
1058 (run-hooks 'mime-edit-mode-hook)
1060 (substitute-command-keys
1061 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))
1065 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1068 (defun mime-edit-exit (&optional nomime no-error)
1069 "Translate the tagged MIME message into a MIME compliant message.
1070 With no argument encode a message in the buffer into MIME, otherwise
1071 just return to previous mode."
1073 (if (not mime-edit-mode-flag)
1075 (error "You aren't editing a MIME message.")
1079 (run-hooks 'mime-edit-translate-hook)
1080 (mime-edit-translate-buffer)))
1081 ;; Restore previous state.
1082 (setq mime-edit-mode-flag nil)
1083 (if (and (featurep 'xemacs)
1084 (featurep 'menubar))
1085 (delete-menu-item (list mime-edit-menu-title))
1088 (set-buffer-modified-p (buffer-modified-p))
1089 (run-hooks 'mime-edit-exit-hook)
1090 (message "Exit MIME editor mode.")
1093 (defun mime-edit-maybe-translate ()
1095 (mime-edit-exit nil t)
1096 (call-interactively 'mime-edit-maybe-split-and-send)
1099 (defun mime-edit-help ()
1100 "Show help message about MIME mode."
1102 (with-output-to-temp-buffer "*Help*"
1103 (princ "MIME editor mode:\n")
1104 (princ (documentation 'mime-edit-mode))
1105 (print-help-return-message)))
1107 (defun mime-edit-insert-text (&optional subtype)
1108 "Insert a text message.
1109 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1110 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1112 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1114 (if (looking-at mime-edit-single-part-tag-regexp)
1116 ;; Make a space between the following message.
1120 (if (and (member (cadr ret) '("enriched"))
1121 (fboundp 'enriched-mode))
1123 (if (boundp 'enriched-mode)
1128 (defun mime-edit-insert-file (file &optional verbose)
1129 "Insert a message from a file."
1130 (interactive "fInsert file as MIME message: \nP")
1131 (let* ((guess (mime-find-file-type file))
1132 (type (nth 0 guess))
1133 (subtype (nth 1 guess))
1134 (parameters (nth 2 guess))
1135 (encoding (nth 3 guess))
1136 (disposition-type (nth 4 guess))
1137 (disposition-params (nth 5 guess))
1140 (setq type (mime-prompt-for-type type)
1141 subtype (mime-prompt-for-subtype type subtype)
1143 (if (or (interactive-p) verbose)
1144 (setq encoding (mime-prompt-for-encoding encoding))
1146 (if (or (consp parameters) (stringp disposition-type))
1147 (let ((rest parameters) cell attribute value)
1148 (setq parameters "")
1150 (setq cell (car rest))
1151 (setq attribute (car cell))
1152 (setq value (cdr cell))
1153 (if (eq value 'file)
1154 (setq value (std11-wrap-as-quoted-string
1155 (file-name-nondirectory file)))
1157 (setq parameters (concat parameters "; " attribute "=" value))
1158 (setq rest (cdr rest))
1160 (if disposition-type
1163 (concat parameters "\n"
1164 "Content-Disposition: " disposition-type))
1165 (setq rest disposition-params)
1167 (setq cell (car rest))
1168 (setq attribute (car cell))
1169 (setq value (cdr cell))
1170 (if (eq value 'file)
1171 (setq value (std11-wrap-as-quoted-string
1172 (file-name-nondirectory file)))
1175 (concat parameters "; " attribute "=" value))
1176 (setq rest (cdr rest))
1180 (mime-edit-insert-tag type subtype parameters)
1181 (mime-edit-insert-binary-file file encoding)
1184 (defun mime-edit-insert-external ()
1185 "Insert a reference to external body."
1187 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1189 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1191 (let* ((pritype (mime-prompt-for-type))
1192 (subtype (mime-prompt-for-subtype pritype))
1193 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1196 (insert "Content-Type: "
1197 pritype "/" subtype (or parameters "") "\n")))
1198 (if (and (not (eobp))
1199 (not (looking-at mime-edit-single-part-tag-regexp)))
1200 (insert (mime-make-text-tag) "\n")))
1202 (defun mime-edit-insert-voice ()
1203 "Insert a voice message."
1207 "What transfer encoding: "
1208 (mime-encoding-alist) nil t nil)))
1209 (mime-edit-insert-tag "audio" "basic" nil)
1210 (mime-edit-define-encoding encoding)
1212 (narrow-to-region (1- (point))(point))
1214 (funcall mime-edit-voice-recorder encoding)
1217 (invisible-region (point-min)(point-max))
1218 (goto-char (point-max))
1221 (defun mime-edit-insert-signature (&optional arg)
1222 "Insert a signature file."
1224 (let ((signature-insert-hook
1227 (let ((items (mime-find-file-type signature-file-name)))
1228 (apply (function mime-edit-insert-tag)
1229 (car items) (cadr items) (list (caddr items))))
1232 (insert-signature arg)
1236 ;; Insert a new tag around a point.
1238 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1239 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1240 If nothing is inserted, return nil."
1243 (mime-edit-goto-tag)
1244 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1245 (< (match-beginning 0) p)
1248 (goto-char (match-beginning 0))
1257 (mime-prompt-for-type)))
1260 (mime-prompt-for-subtype pritype)))
1263 (mime-prompt-for-parameters pritype subtype delimiter)))
1264 ;; Make a new MIME tag.
1265 (setq newtag (mime-make-tag pritype subtype parameters))
1266 ;; Find an current MIME tag.
1269 (if (mime-edit-goto-tag)
1270 (buffer-substring (match-beginning 0) (match-end 0))
1271 ;; Assume content type is 'text/plan'.
1272 (mime-make-tag "text" "plain")
1274 ;; We are only interested in TEXT.
1276 (not (mime-test-content-type
1277 (mime-edit-get-contype oldtag) "text")))
1280 (if (or (not oldtag) ;Not text
1281 (or mime-ignore-same-text-tag
1282 (not (string-equal oldtag newtag))))
1284 ;; Mark the beginning of the tag for convenience.
1285 (push-mark (point) 'nomsg)
1286 (insert newtag "\n")
1287 (list pritype subtype parameters) ;New tag is created.
1289 ;; Restore previous point.
1291 nil ;Nothing is created.
1295 (defun mime-edit-insert-binary-file (file &optional encoding)
1296 "Insert binary FILE at point.
1297 Optional argument ENCODING specifies an encoding method such as base64."
1298 (let* ((tagend (1- (point))) ;End of the tag
1299 (hide-p (and mime-auto-hide-body
1302 (let ((en (downcase encoding)))
1303 (or (string-equal en "7bit")
1304 (string-equal en "8bit")
1305 (string-equal en "binary")
1309 (narrow-to-region tagend (point))
1310 (mime-insert-encoded-file file encoding)
1313 (invisible-region (point-min) (point-max))
1314 (goto-char (point-max))
1316 (goto-char (point-max))
1319 (looking-at mime-edit-tag-regexp)
1320 (= (point)(point-max))
1321 (mime-edit-insert-tag "text" "plain")
1323 ;; Define encoding even if it is 7bit.
1324 (if (stringp encoding)
1326 (goto-char tagend) ; Make sure which line the tag is on.
1327 (mime-edit-define-encoding encoding)
1332 ;; Commands work on a current message flagment.
1334 (defun mime-edit-goto-tag ()
1335 "Search for the beginning of the tagged MIME message."
1336 (let ((current (point)))
1337 (if (looking-at mime-edit-tag-regexp)
1339 ;; At first, go to the end.
1340 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1341 (goto-char (1- (match-beginning 0))) ;For multiline tag
1344 (goto-char (point-max))
1346 ;; Then search for the beginning.
1347 (re-search-backward mime-edit-end-tag-regexp nil t)
1348 (or (looking-at mime-edit-beginning-tag-regexp)
1349 ;; Restore previous point.
1356 (defun mime-edit-content-beginning ()
1357 "Return the point of the beginning of content."
1359 (let ((beg (save-excursion
1360 (beginning-of-line) (point))))
1361 (if (mime-edit-goto-tag)
1362 (let ((top (point)))
1363 (goto-char (match-end 0))
1364 (if (and (= beg top)
1365 (= (following-char) ?\^M))
1369 ;; Default text/plain tag.
1370 (goto-char (point-min))
1372 (concat "\n" (regexp-quote mail-header-separator)
1373 (if mime-ignore-preceding-spaces
1374 "[ \t\n]*\n" "\n")) nil 'move)
1378 (defun mime-edit-content-end ()
1379 "Return the point of the end of content."
1381 (if (mime-edit-goto-tag)
1383 (goto-char (match-end 0))
1384 (if (invisible-p (point))
1385 (next-visible-point (point))
1386 ;; Move to the end of this text.
1387 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1388 ;; Don't forget a multiline tag.
1389 (goto-char (match-beginning 0))
1393 ;; Assume the message begins with text/plain.
1394 (goto-char (mime-edit-content-beginning))
1395 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1396 ;; Don't forget a multiline tag.
1397 (goto-char (match-beginning 0)))
1401 (defun mime-edit-define-charset (charset)
1402 "Set charset of current tag to CHARSET."
1404 (if (mime-edit-goto-tag)
1405 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1406 (delete-region (match-beginning 0) (match-end 0))
1409 (mime-edit-set-parameter
1410 (mime-edit-get-contype tag)
1412 (let ((comment (get charset 'mime-charset-comment)))
1414 (concat (upcase (symbol-name charset)) " (" comment ")")
1415 (upcase (symbol-name charset)))))
1416 (mime-edit-get-encoding tag)))
1419 (defun mime-edit-define-encoding (encoding)
1420 "Set encoding of current tag to ENCODING."
1422 (if (mime-edit-goto-tag)
1423 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1424 (delete-region (match-beginning 0) (match-end 0))
1425 (insert (mime-create-tag (mime-edit-get-contype tag) encoding)))
1428 (defun mime-edit-choose-charset ()
1429 "Choose charset of a text following current point."
1430 (detect-mime-charset-region (point) (mime-edit-content-end))
1433 (defun mime-make-text-tag (&optional subtype)
1434 "Make a tag for a text after current point.
1435 Subtype of text type can be specified by an optional argument SUBTYPE.
1436 Otherwise, it is obtained from mime-content-types."
1437 (let* ((pritype "text")
1438 (subtype (or subtype
1439 (car (car (cdr (assoc pritype mime-content-types)))))))
1440 ;; Charset should be defined later.
1441 (mime-make-tag pritype subtype)))
1444 ;; Tag handling functions
1446 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1447 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1448 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1452 (defun mime-create-tag (contype &optional encoding)
1453 "Make a tag with CONTENT-TYPE and optional ENCODING."
1454 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1457 (defun mime-edit-get-contype (tag)
1458 "Return Content-Type (including parameters) of TAG."
1460 (or (string-match mime-edit-single-part-tag-regexp tag)
1461 (string-match mime-edit-multipart-beginning-regexp tag)
1462 (string-match mime-edit-multipart-end-regexp tag)
1464 (substring tag (match-beginning 1) (match-end 1))
1467 (defun mime-edit-get-encoding (tag)
1468 "Return encoding of TAG."
1470 (string-match mime-edit-single-part-tag-regexp tag)
1472 (not (= (match-beginning 3) (match-end 3)))
1473 (substring tag (match-beginning 3) (match-end 3))))
1475 (defun mime-get-parameter (contype parameter)
1476 "For given CONTYPE return value for PARAMETER.
1477 Nil if no such parameter."
1481 (regexp-quote parameter)
1482 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1484 (substring contype (match-beginning 1) (match-end 1))
1485 nil ;No such parameter
1488 (defun mime-edit-set-parameter (contype parameter value)
1489 "For given CONTYPE set PARAMETER to VALUE."
1490 (let (ctype opt-fields)
1491 (if (string-match "\n[^ \t\n\r]+:" contype)
1492 (setq ctype (substring contype 0 (match-beginning 0))
1493 opt-fields (substring contype (match-beginning 0)))
1494 (setq ctype contype)
1499 (regexp-quote parameter)
1500 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1503 (concat (substring ctype 0 (match-beginning 1))
1505 (substring ctype (match-end 1))
1507 (concat ctype "; " parameter "=" value opt-fields)
1510 (defun mime-strip-parameters (contype)
1511 "Return primary content-type and subtype without parameters for CONTYPE."
1512 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1513 (substring contype (match-beginning 1) (match-end 1)) nil))
1515 (defun mime-test-content-type (contype type &optional subtype)
1516 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1517 (and (stringp contype)
1520 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1521 (downcase contype))))
1526 (defun mime-find-file-type (file)
1527 "Guess Content-Type, subtype, and parameters from FILE."
1529 (guesses mime-file-types))
1530 (while (and (not guess) guesses)
1531 (if (string-match (car (car guesses)) file)
1532 (setq guess (cdr (car guesses))))
1533 (setq guesses (cdr guesses)))
1537 (defun mime-prompt-for-type (&optional default)
1538 "Ask for Content-type."
1540 ;; Repeat until primary content type is specified.
1541 (while (string-equal type "")
1543 (completing-read "What content type: "
1546 'require-match ;Type must be specified.
1549 (if (string-equal type "")
1551 (message "Content type is required.")
1558 (defun mime-prompt-for-subtype (type &optional default)
1559 "Ask for subtype of media-type TYPE."
1560 (let ((subtypes (cdr (assoc type mime-content-types))))
1562 (assoc default subtypes))
1563 (setq default (car (car subtypes)))
1569 "What content subtype: (default " default ") ")
1570 "What content subtype: ")
1571 (cdr (assoc type mime-content-types))
1573 'require-match ;Subtype must be specified.
1576 (if (string-equal answer "") default answer)))
1578 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1579 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1580 Optional DELIMITER specifies parameter delimiter (';' by default)."
1581 (let* ((delimiter (or delimiter "; "))
1586 (mime-prompt-for-parameters-1
1588 (cdr (assoc pritype mime-content-types))))))
1591 (if (and (stringp parameters)
1592 (not (string-equal parameters "")))
1593 (concat delimiter parameters)
1594 "" ;"" if no parameters
1597 (defun mime-prompt-for-parameters-1 (optlist)
1598 (apply (function append)
1599 (mapcar (function mime-prompt-for-parameter) optlist)))
1601 (defun mime-prompt-for-parameter (parameter)
1603 Parameter must be '(PROMPT CHOICE1 (CHOISE2 ...))."
1604 (let* ((prompt (car parameter))
1605 (choices (mapcar (function
1607 (if (consp e) e (list e))))
1609 (default (car (car choices)))
1615 (concat "What " prompt
1617 (if (string-equal default "") "\"\"" default)
1619 choices nil nil ""))
1620 ;; If nothing is selected, use default.
1621 (if (string-equal answer "")
1622 (setq answer default)))
1624 (read-string (concat "What " prompt ": "))))
1625 (cons (if (and answer
1626 (not (string-equal answer "")))
1628 ;; Note: control characters ignored!
1629 (if (string-match mime-tspecials-regexp answer)
1630 (concat "\"" answer "\"") answer)))
1631 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))
1634 (defun mime-prompt-for-encoding (default)
1635 "Ask for Content-Transfer-Encoding."
1640 "What transfer encoding: "
1641 (mime-encoding-alist) nil t default)
1647 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1650 (defvar mime-edit-translate-buffer-hook
1651 '(mime-edit-pgp-enclose-buffer
1652 mime-edit-translate-body
1653 mime-edit-translate-header))
1655 (defun mime-edit-translate-header ()
1656 "Encode the message header into network representation."
1657 (mime-encode-header-in-buffer 'code-conversion)
1658 (run-hooks 'mime-edit-translate-header-hook))
1660 (defun mime-edit-translate-buffer ()
1661 "Encode the tagged MIME message in current buffer in MIME compliant message."
1664 (if (catch 'mime-edit-error
1666 (run-hooks 'mime-edit-translate-buffer-hook)
1670 (error "Translation error!")
1673 (defun mime-edit-find-inmost ()
1674 (goto-char (point-min))
1675 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1676 (let ((bb (match-beginning 0))
1678 (type (buffer-substring (match-beginning 1)(match-end 1)))
1680 (setq end-exp (format "--}-<<%s>>\n" type))
1682 (if (re-search-forward end-exp nil t)
1683 (setq eb (match-beginning 0))
1684 (setq eb (point-max))
1686 (narrow-to-region be eb)
1688 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1690 (narrow-to-region (match-beginning 0)(point-max))
1691 (mime-edit-find-inmost)
1694 (list type bb be eb)
1697 (defun mime-edit-process-multipart-1 (boundary)
1698 (let ((ret (mime-edit-find-inmost)))
1700 (let ((type (car ret))
1701 (bb (nth 1 ret))(be (nth 2 ret))
1704 (narrow-to-region bb eb)
1705 (delete-region bb be)
1706 (setq bb (point-min))
1707 (setq eb (point-max))
1710 (if (looking-at mime-edit-multipart-end-regexp)
1711 (let ((beg (match-beginning 0))
1714 (delete-region beg end)
1715 (or (looking-at mime-edit-beginning-tag-regexp)
1717 (insert (concat (mime-make-text-tag) "\n"))
1719 (cond ((string-equal type "quote")
1720 (mime-edit-enquote-region bb eb)
1722 ((string-equal type "pgp-signed")
1723 (mime-edit-sign-pgp-mime bb eb boundary)
1725 ((string-equal type "pgp-encrypted")
1726 (mime-edit-encrypt-pgp-mime bb eb boundary)
1728 ((string-equal type "kazu-signed")
1729 (mime-edit-sign-pgp-kazu bb eb boundary)
1731 ((string-equal type "kazu-encrypted")
1732 (mime-edit-encrypt-pgp-kazu bb eb boundary)
1734 ((string-equal type "smime-signed")
1735 (mime-edit-sign-smime bb eb boundary)
1737 ((string-equal type "smime-encrypted")
1738 (mime-edit-encrypt-smime bb eb boundary)
1742 (nth 2 (mime-edit-translate-region bb eb
1746 (format "--[[multipart/%s;
1747 boundary=\"%s\"][7bit]]\n"
1752 (defun mime-edit-enquote-region (beg end)
1755 (narrow-to-region beg end)
1757 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1758 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1759 (replace-match (concat "- " (substring tag 1)))
1762 (defun mime-edit-dequote-region (beg end)
1765 (narrow-to-region beg end)
1767 (while (re-search-forward
1768 mime-edit-quoted-single-part-tag-regexp nil t)
1769 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1770 (replace-match (concat "-" (substring tag 2)))
1773 (defvar mime-edit-pgp-user-id nil)
1775 (defun mime-edit-sign-pgp-mime (beg end boundary)
1778 (let* ((from (std11-field-body "From" mail-header-separator))
1780 (narrow-to-region beg end)
1781 (mime-edit-translate-region beg end boundary)))
1783 (encoding (nth 1 ret))
1784 (pgp-boundary (concat "pgp-sign-" boundary))
1787 (insert (format "Content-Type: %s\n" ctype))
1789 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1792 (or (let ((pgg-default-user-id
1793 (or mime-edit-pgp-user-id
1795 (nth 1 (std11-extract-address-components from))
1796 pgg-default-user-id))))
1797 (pgg-sign-region (point-min)(point-max)))
1798 (throw 'mime-edit-error 'pgp-error)
1801 (cdr (assq 'hash-algorithm
1802 (cdar (with-current-buffer pgg-output-buffer
1803 (pgg-parse-armor-region
1804 (point-min)(point-max))))))
1807 (concat "; micalg=pgp-" (downcase (symbol-name micalg)))
1810 (insert (format "--[[multipart/signed;
1812 protocol=\"application/pgp-signature\"][7bit]]
1814 " pgp-boundary micalg pgp-boundary))
1815 (goto-char (point-max))
1816 (insert (format "\n--%s
1817 Content-Type: application/pgp-signature
1818 Content-Transfer-Encoding: 7bit
1821 (insert-buffer-substring pgg-output-buffer)
1822 (goto-char (point-max))
1823 (insert (format "\n--%s--\n" pgp-boundary))
1826 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1828 (defun mime-edit-make-encrypt-recipient-header ()
1829 (let* ((names mime-edit-encrypt-recipient-fields-list)
1831 (std11-field-bodies (cons "From" names)
1832 nil mail-header-separator))
1835 (setq values (cdr values))))
1836 (header (and (stringp from)
1837 (if (string-equal from "")
1839 (format "From: %s\n" from)
1842 (while (and names values)
1843 (let ((name (car names))
1844 (value (car values))
1846 (and (stringp value)
1847 (or (string-equal value "")
1849 (setq header (concat header name ": " value "\n")
1850 recipients (if recipients
1851 (concat recipients " ," value)
1854 (setq names (cdr names)
1855 values (cdr values))
1857 (vector from recipients header)
1860 (defun mime-edit-encrypt-pgp-mime (beg end boundary)
1863 (let (from recipients header)
1864 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1865 (setq from (aref ret 0)
1866 recipients (aref ret 1)
1867 header (aref ret 2))
1869 (narrow-to-region beg end)
1871 (mime-edit-translate-region beg end boundary))
1873 (encoding (nth 1 ret))
1874 (pgp-boundary (concat "pgp-" boundary)))
1877 (insert (format "Content-Type: %s\n" ctype))
1879 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1882 (mime-encode-header-in-buffer)
1883 (or (let ((pgg-default-user-id
1884 (or mime-edit-pgp-user-id
1886 (nth 1 (std11-extract-address-components from))
1887 pgg-default-user-id))))
1889 (point-min) (point-max)
1890 (mapcar (lambda (recipient)
1891 (nth 1 (std11-extract-address-components
1893 (split-string recipients
1894 "\\([ \t\n]*,[ \t\n]*\\)+")))
1896 (throw 'mime-edit-error 'pgp-error)
1898 (delete-region (point-min)(point-max))
1900 (insert (format "--[[multipart/encrypted;
1902 protocol=\"application/pgp-encrypted\"][7bit]]
1904 Content-Type: application/pgp-encrypted
1907 Content-Type: application/octet-stream
1908 Content-Transfer-Encoding: 7bit
1910 " pgp-boundary pgp-boundary pgp-boundary))
1911 (insert-buffer-substring pgg-output-buffer)
1912 (goto-char (point-max))
1913 (insert (format "\n--%s--\n" pgp-boundary))
1916 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1919 (narrow-to-region beg end)
1921 (mime-edit-translate-region beg end boundary))
1923 (encoding (nth 1 ret)))
1925 (insert (format "Content-Type: %s\n" ctype))
1927 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1930 (or (pgg-sign-region beg (point-max) 'clearsign)
1931 (throw 'mime-edit-error 'pgp-error)
1935 "--[[application/pgp; format=mime][7bit]]\n")
1939 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1941 (let (recipients header)
1942 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1943 (setq recipients (aref ret 1)
1944 header (aref ret 2))
1947 (narrow-to-region beg end)
1949 (mime-edit-translate-region beg end boundary))
1951 (encoding (nth 1 ret)))
1954 (insert (format "Content-Type: %s\n" ctype))
1956 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1959 (or (pgg-encrypt-region beg (point-max) recipients)
1960 (throw 'mime-edit-error 'pgp-error)
1964 "--[[application/pgp; format=mime][7bit]]\n")
1968 (defun mime-edit-sign-smime (beg end boundary)
1972 (narrow-to-region beg end)
1973 (mime-edit-translate-region beg end boundary)))
1975 (encoding (nth 1 ret))
1976 (smime-boundary (concat "smime-sign-" boundary)))
1978 (insert (format "Content-Type: %s\n" ctype))
1980 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
1983 (let (buffer-undo-list)
1984 (goto-char (point-min))
1985 (while (progn (end-of-line) (not (eobp)))
1988 (or (prog1 (smime-sign-region (point-min)(point-max))
1989 (push nil buffer-undo-list)
1990 (ignore-errors (undo)))
1991 (throw 'mime-edit-error 'pgp-error)
1994 (insert (format "--[[multipart/signed;
1995 boundary=\"%s\"; micalg=sha1;
1996 protocol=\"application/pkcs7-signature\"][7bit]]
1998 " smime-boundary smime-boundary))
1999 (goto-char (point-max))
2000 (insert (format "\n--%s
2001 Content-Type: application/pkcs7-signature; name=\"smime.p7s\"
2002 Content-Transfer-Encoding: base64
2003 Content-Disposition: attachment; filename=\"smime.p7s\"
2004 Content-Description: S/MIME Cryptographic Signature
2007 (insert-buffer-substring smime-output-buffer)
2008 (goto-char (point-max))
2009 (insert (format "\n--%s--\n" smime-boundary))
2012 (defun mime-edit-encrypt-smime (beg end boundary)
2016 (narrow-to-region beg end)
2017 (mime-edit-translate-region beg end boundary)))
2019 (encoding (nth 1 ret)))
2021 (insert (format "Content-Type: %s\n" ctype))
2023 (insert (format "Content-Transfer-Encoding: %s\n" encoding))
2026 (goto-char (point-min))
2027 (while (progn (end-of-line) (not (eobp)))
2030 (or (smime-encrypt-region (point-min)(point-max))
2031 (throw 'mime-edit-error 'pgp-error)
2033 (delete-region (point-min)(point-max))
2034 (insert "--[[application/pkcs7-mime; name=\"smime.p7m\"
2035 Content-Disposition: attachment; filename=\"smime.p7m\"
2036 Content-Description: S/MIME Encrypted Message][base64]]\n")
2037 (insert-buffer-substring smime-output-buffer)
2040 (defsubst replace-space-with-underline (str)
2041 (mapconcat (function
2049 (defun mime-edit-make-boundary ()
2050 (concat mime-multipart-boundary "_"
2051 (replace-space-with-underline (current-time-string))
2054 (defun mime-edit-translate-body ()
2055 "Encode the tagged MIME body in current buffer in MIME compliant message."
2058 (let ((boundary (mime-edit-make-boundary))
2061 (while (mime-edit-process-multipart-1
2062 (format "%s-%d" boundary i))
2066 ;; We are interested in message body.
2069 (goto-char (point-min))
2071 (concat "\n" (regexp-quote mail-header-separator)
2072 (if mime-ignore-preceding-spaces
2073 "[ \t\n]*\n" "\n")) nil 'move)
2077 (goto-char (point-max))
2078 (and mime-ignore-trailing-spaces
2079 (re-search-backward "[^ \t\n]\n" beg t)
2082 (setq ret (mime-edit-translate-region
2084 (format "%s-%d" boundary i)))
2086 (mime-edit-dequote-region (point-min)(point-max))
2087 (let ((contype (car ret)) ;Content-Type
2088 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
2090 ;; Insert User-Agent field
2091 (and mime-edit-insert-user-agent-field
2092 (or (mail-position-on-field "User-Agent")
2093 (insert mime-edit-user-agent-value)
2095 ;; Make primary MIME headers.
2096 (or (mail-position-on-field "MIME-Version")
2097 (insert mime-edit-mime-version-value))
2098 ;; Remove old Content-Type and other fields.
2100 (goto-char (point-min))
2101 (search-forward (concat "\n" mail-header-separator "\n") nil t)
2102 (narrow-to-region (point-min) (point))
2103 (goto-char (point-min))
2104 (mime-delete-field "Content-Type")
2105 (mime-delete-field "Content-Transfer-Encoding"))
2106 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
2107 (mail-position-on-field "Content-Type")
2111 (mail-position-on-field "Content-Transfer-Encoding")
2115 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
2116 "Translate single-part-tag to MIME header."
2117 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
2118 (let* ((beg (match-beginning 0))
2120 (tag (buffer-substring beg end)))
2121 (delete-region beg end)
2122 (let ((contype (mime-edit-get-contype tag))
2123 (encoding (mime-edit-get-encoding tag)))
2124 (insert (concat prefix "--" boundary "\n"))
2126 (narrow-to-region (point)(point))
2127 (insert "Content-Type: " contype "\n")
2129 (insert "Content-Transfer-Encoding: " encoding "\n"))
2130 (mime-encode-header-in-buffer))
2134 (downcase encoding))))
2137 (defun mime-edit-translate-region (beg end &optional boundary multipart)
2139 (setq boundary (mime-edit-make-boundary))
2143 (narrow-to-region beg end)
2144 (let ((tag nil) ;MIME tag
2145 (contype nil) ;Content-Type
2146 (encoding nil) ;Content-Transfer-Encoding
2147 (nparts 0)) ;Number of body parts
2148 ;; Normalize the body part by inserting appropriate message
2149 ;; tags for every message contents.
2150 (mime-edit-normalize-body)
2151 ;; Counting the number of Content-Type.
2152 (goto-char (point-min))
2153 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2154 (setq nparts (1+ nparts)))
2155 ;; Begin translation.
2157 ((and (<= nparts 1)(not multipart))
2158 ;; It's a singular message.
2159 (goto-char (point-min))
2160 (while (re-search-forward
2161 mime-edit-single-part-tag-regexp nil t)
2163 (buffer-substring (match-beginning 0) (match-end 0)))
2164 (delete-region (match-beginning 0) (1+ (match-end 0)))
2165 (setq contype (mime-edit-get-contype tag))
2166 (setq encoding (mime-edit-get-encoding tag))
2169 ;; It's a multipart message.
2170 (goto-char (point-min))
2171 (let ((prio mime-content-transfer-encoding-priority-list)
2173 (when (setq part-info
2174 (mime-edit-translate-single-part-tag boundary))
2175 (and (setq nprio (member (cdr part-info) prio))
2177 (while (setq part-info
2178 (mime-edit-translate-single-part-tag boundary "\n"))
2179 (and (setq nprio (member (cdr part-info) prio))
2180 (setq prio nprio))))
2181 ;; Define Content-Type as "multipart/mixed".
2183 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
2184 (setq encoding (car prio))
2185 ;; Insert the trailer.
2186 (goto-char (point-max))
2187 (insert "\n--" boundary "--\n")
2189 (list contype encoding boundary nparts)
2192 (defun mime-edit-normalize-body ()
2193 "Normalize the body part by inserting appropriate message tags."
2194 ;; Insert the first MIME tags if necessary.
2195 (goto-char (point-min))
2196 (if (not (looking-at mime-edit-single-part-tag-regexp))
2197 (insert (mime-make-text-tag) "\n"))
2198 ;; Check each tag, and add new tag or correct it if necessary.
2199 (goto-char (point-min))
2200 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2201 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
2202 (contype (mime-edit-get-contype tag))
2203 (charset (mime-get-parameter contype "charset"))
2204 (encoding (mime-edit-get-encoding tag)))
2205 ;; Remove extra whitespaces after the tag.
2206 (if (looking-at "[ \t]+$")
2207 (delete-region (match-beginning 0) (match-end 0)))
2209 (end (mime-edit-content-end))
2211 (if (= end (point-max))
2214 (or (looking-at mime-edit-beginning-tag-regexp)
2216 (insert (mime-make-text-tag) "\n")
2218 (visible-region beg end)
2222 ((mime-test-content-type contype "message")
2223 ;; Content-type "message" should be sent as is.
2226 ((mime-test-content-type contype "text")
2227 ;; Define charset for text if necessary.
2228 (setq charset (if charset
2229 (intern (downcase charset))
2230 (mime-edit-choose-charset)))
2231 (mime-edit-define-charset charset)
2232 (cond ((string-equal contype "text/x-rot13-47-48")
2235 (mule-caesar-region (point) (mime-edit-content-end))
2237 ((string-equal contype "text/enriched")
2242 (end (mime-edit-content-end))
2244 ;; Patch for hard newlines
2247 ;; (while (search-forward "\n" end t)
2248 ;; (put-text-property (match-beginning 0)
2251 ;; End patch for hard newlines
2252 (enriched-encode beg end nil)
2254 (if (search-forward "\n\n")
2255 (delete-region beg (match-end 0))
2258 ;; Point is now on current tag.
2259 ;; Define encoding and encode text if necessary.
2260 (or encoding ;Encoding is not specified.
2263 (let ((ret (cdr (assq charset mime-charset-type-list))))
2265 (setq bits (car ret)
2268 conv "quoted-printable")))
2269 (if (<= bits mime-transfer-level)
2270 (mime-encoding-name bits)
2272 (beg (mime-edit-content-beginning)))
2273 (encode-mime-charset-region beg (mime-edit-content-end)
2275 ;; Protect "From " in beginning of line
2277 (narrow-to-region beg (mime-edit-content-end))
2279 (let (case-fold-search)
2280 (if (re-search-forward "^From " nil t)
2282 (if (memq charset '(iso-2022-jp
2287 (replace-match "\e(BFrom ")
2288 (re-search-forward "^From " nil t)
2290 (setq encoding "quoted-printable")
2292 ;; canonicalize line break code
2293 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2295 (narrow-to-region beg (mime-edit-content-end))
2297 (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2298 (replace-match "\\1\r\n"))))
2300 (mime-encode-region beg (mime-edit-content-end)
2301 (or encoding "7bit"))
2302 (mime-edit-define-encoding encoding)
2304 (goto-char (mime-edit-content-end))
2306 ((null encoding) ;Encoding is not specified.
2307 ;; Application, image, audio, video, and any other
2308 ;; unknown content-type without encoding should be
2310 (let* ((encoding "base64") ;Encode in BASE64 by default.
2311 (beg (mime-edit-content-beginning))
2312 (end (mime-edit-content-end)))
2313 (mime-encode-region beg end encoding)
2314 (mime-edit-define-encoding encoding))
2319 (defun mime-delete-field (field)
2320 "Delete header FIELD."
2321 (let ((regexp (format "^%s:[ \t]*" field)))
2322 (goto-char (point-min))
2323 (while (re-search-forward regexp nil t)
2324 (delete-region (match-beginning 0)
2325 (1+ (std11-field-end))))))
2329 ;;; Platform dependent functions
2332 ;; Sun implementations
2334 (defun mime-edit-voice-recorder-for-sun (encoding)
2335 "Record voice in a buffer using Sun audio device,
2336 and insert data encoded as ENCODING."
2337 (message "Start the recording on %s. Type C-g to finish the recording..."
2339 (mime-insert-encoded-file "/dev/audio" encoding)
2343 ;;; @ Other useful commands.
2346 ;; Message forwarding commands as content-type "message/rfc822".
2348 (defun mime-edit-insert-message (&optional message)
2350 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2351 (if (and inserter (fboundp inserter))
2353 (mime-edit-insert-tag "message" "rfc822")
2354 (funcall inserter message)
2356 (message "Sorry, I don't have message inserter for your MUA.")
2359 (defun mime-edit-insert-mail (&optional message)
2361 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2362 (if (and inserter (fboundp inserter))
2364 (mime-edit-insert-tag "message" "rfc822")
2365 (funcall inserter message)
2367 (message "Sorry, I don't have mail inserter for your MUA.")
2370 (defun mime-edit-inserted-message-filter ()
2373 (let ((header-start (point))
2374 (case-fold-search t)
2377 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2378 (if (re-search-forward "^$" (mark t))
2379 (narrow-to-region header-start (match-beginning 0))
2381 (goto-char header-start)
2382 (while (and (re-search-forward
2383 mime-edit-yank-ignored-field-regexp nil t)
2384 (setq beg (match-beginning 0))
2385 (setq end (1+ (std11-field-end)))
2387 (delete-region beg end)
2392 ;;; @ multipart enclosure
2395 (defun mime-edit-enclose-region-internal (type beg end)
2399 (narrow-to-region beg end)
2400 (insert (format "--<<%s>>-{\n" type))
2401 (goto-char (point-max))
2402 (insert (format "--}-<<%s>>\n" type))
2403 (goto-char (point-max))
2405 (or (looking-at mime-edit-beginning-tag-regexp)
2407 (insert (mime-make-text-tag) "\n")
2411 (defun mime-edit-enclose-quote-region (beg end)
2413 (mime-edit-enclose-region-internal 'quote beg end)
2416 (defun mime-edit-enclose-mixed-region (beg end)
2418 (mime-edit-enclose-region-internal 'mixed beg end)
2421 (defun mime-edit-enclose-parallel-region (beg end)
2423 (mime-edit-enclose-region-internal 'parallel beg end)
2426 (defun mime-edit-enclose-digest-region (beg end)
2428 (mime-edit-enclose-region-internal 'digest beg end)
2431 (defun mime-edit-enclose-alternative-region (beg end)
2433 (mime-edit-enclose-region-internal 'alternative beg end)
2436 (defun mime-edit-enclose-pgp-signed-region (beg end)
2438 (mime-edit-enclose-region-internal 'pgp-signed beg end)
2441 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2443 (mime-edit-enclose-region-internal 'pgp-encrypted beg end)
2446 (defun mime-edit-enclose-kazu-signed-region (beg end)
2448 (mime-edit-enclose-region-internal 'kazu-signed beg end)
2451 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2453 (mime-edit-enclose-region-internal 'kazu-encrypted beg end)
2456 (defun mime-edit-enclose-smime-signed-region (beg end)
2458 (mime-edit-enclose-region-internal 'smime-signed beg end)
2461 (defun mime-edit-enclose-smime-encrypted-region (beg end)
2463 (mime-edit-enclose-region-internal 'smime-encrypted beg end)
2466 (defun mime-edit-insert-key (&optional arg)
2467 "Insert a pgp public key."
2469 (mime-edit-insert-tag "application" "pgp-keys")
2470 (mime-edit-define-encoding "7bit")
2472 (if (and (not (eobp))
2473 (not (looking-at mime-edit-single-part-tag-regexp)))
2474 (insert (mime-make-text-tag) "\n")))
2480 (defun mime-edit-set-split (arg)
2483 (y-or-n-p "Do you want to enable split? ")
2485 (setq mime-edit-split-message arg)
2487 (message "This message is enabled to split.")
2488 (message "This message is not enabled to split.")
2491 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2492 "Toggle transfer-level is 7bit or 8bit through.
2494 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2496 (if (numberp transfer-level)
2497 (setq mime-transfer-level transfer-level)
2498 (if (< mime-transfer-level 8)
2499 (setq mime-transfer-level 8)
2500 (setq mime-transfer-level 7)
2502 (message (format "Current transfer-level is %d bit"
2503 mime-transfer-level))
2504 (setq mime-transfer-level-string
2505 (mime-encoding-name mime-transfer-level 'not-omit))
2506 (force-mode-line-update)
2509 (defun mime-edit-set-transfer-level-7bit ()
2511 (mime-edit-toggle-transfer-level 7)
2514 (defun mime-edit-set-transfer-level-8bit ()
2516 (mime-edit-toggle-transfer-level 8)
2523 (defvar mime-edit-pgp-processing nil)
2524 (make-variable-buffer-local 'mime-edit-pgp-processing)
2526 (defun mime-edit-set-sign (arg)
2529 (y-or-n-p "Do you want to sign? ")
2533 (or (memq 'sign mime-edit-pgp-processing)
2534 (setq mime-edit-pgp-processing
2535 (nconc mime-edit-pgp-processing
2536 (copy-sequence '(sign)))))
2537 (message "This message will be signed.")
2539 (setq mime-edit-pgp-processing
2540 (delq 'sign mime-edit-pgp-processing))
2541 (message "This message will not be signed.")
2544 (defun mime-edit-set-encrypt (arg)
2547 (y-or-n-p "Do you want to encrypt? ")
2551 (or (memq 'encrypt mime-edit-pgp-processing)
2552 (setq mime-edit-pgp-processing
2553 (nconc mime-edit-pgp-processing
2554 (copy-sequence '(encrypt)))))
2555 (message "This message will be encrypt.")
2557 (setq mime-edit-pgp-processing
2558 (delq 'encrypt mime-edit-pgp-processing))
2559 (message "This message will not be encrypt.")
2562 (defun mime-edit-pgp-enclose-buffer ()
2563 (let ((beg (save-excursion
2564 (goto-char (point-min))
2565 (if (search-forward (concat "\n" mail-header-separator "\n"))
2570 (dolist (pgp-processing mime-edit-pgp-processing)
2571 (case pgp-processing
2573 (mime-edit-enclose-pgp-signed-region
2577 (mime-edit-enclose-pgp-encrypted-region
2586 (defun mime-edit-insert-partial-header (fields subject
2587 id number total separator)
2589 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2590 (insert mime-edit-mime-version-field-for-message/partial)
2592 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2593 id number total separator))
2596 (defun mime-edit-split-and-send
2597 (&optional cmd lines mime-edit-message-max-length)
2601 (count-lines (point-min) (point-max)))
2603 (or mime-edit-message-max-length
2604 (setq mime-edit-message-max-length
2605 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2606 mime-edit-message-default-max-lines))
2608 (let* ((mime-edit-draft-file-name
2609 (or (buffer-file-name)
2611 (expand-file-name "mime-draft" temporary-file-directory))))
2612 (separator mail-header-separator)
2614 (replace-space-with-underline (current-time-string))
2615 "@" (system-name) "\"")))
2616 (run-hooks 'mime-edit-before-split-hook)
2617 (let ((the-buf (current-buffer))
2618 (copy-buf (get-buffer-create " *Original Message*"))
2619 (header (std11-header-string-except
2620 mime-edit-split-ignored-field-regexp separator))
2621 (subject (mail-fetch-field "subject"))
2622 (total (+ (/ lines mime-edit-message-max-length)
2623 (if (> (mod lines mime-edit-message-max-length) 0)
2629 mime-edit-split-message-sender-alist))
2633 (error "Split sender is not specified for `%s'." major-mode)
2636 (mime-edit-partial-number 1)
2639 (set-buffer copy-buf)
2641 (insert-buffer the-buf)
2643 (if (re-search-forward
2644 (concat "^" (regexp-quote separator) "$") nil t)
2645 (let ((he (match-beginning 0)))
2647 (narrow-to-region (point-min) he)
2649 (goto-char (point-min))
2650 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2651 (delete-region (match-beginning 0)
2652 (1+ (std11-field-end)))
2654 (while (< mime-edit-partial-number total)
2657 (set-buffer copy-buf)
2658 (setq data (buffer-substring
2661 (goto-line mime-edit-message-max-length)
2664 (delete-region (point-min)(point))
2666 (mime-edit-insert-partial-header
2667 header subject id mime-edit-partial-number total separator)
2670 (message (format "Sending %d/%d..."
2671 mime-edit-partial-number total))
2672 (call-interactively command)
2673 (message (format "Sending %d/%d... done"
2674 mime-edit-partial-number total))
2676 (setq mime-edit-partial-number
2677 (1+ mime-edit-partial-number))
2681 (set-buffer copy-buf)
2682 (setq data (buffer-string))
2685 (mime-edit-insert-partial-header
2686 header subject id mime-edit-partial-number total separator)
2689 (message (format "Sending %d/%d..."
2690 mime-edit-partial-number total))
2691 (message (format "Sending %d/%d... done"
2692 mime-edit-partial-number total))
2696 (defun mime-edit-maybe-split-and-send (&optional cmd)
2698 (run-hooks 'mime-edit-before-send-hook)
2699 (let ((mime-edit-message-max-length
2700 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2701 mime-edit-message-default-max-lines))
2702 (lines (count-lines (point-min) (point-max)))
2704 (if (and (> lines mime-edit-message-max-length)
2705 mime-edit-split-message)
2706 (mime-edit-split-and-send cmd lines mime-edit-message-max-length)
2710 ;;; @ preview message
2713 (defvar mime-edit-buffer nil) ; buffer local variable
2715 (defun mime-edit-preview-message ()
2716 "preview editing MIME message."
2718 (let* ((str (buffer-string))
2719 (separator mail-header-separator)
2720 (the-buf (current-buffer))
2721 (buf-name (buffer-name))
2722 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2723 (buf (get-buffer temp-buf-name))
2724 (pgp-processing mime-edit-pgp-processing)
2728 (switch-to-buffer buf)
2731 (setq buf (get-buffer-create temp-buf-name))
2732 (switch-to-buffer buf)
2735 (setq major-mode 'mime-temp-message-mode)
2736 (make-local-variable 'mail-header-separator)
2737 (setq mail-header-separator separator)
2738 (make-local-variable 'mime-edit-buffer)
2739 (setq mime-edit-buffer the-buf)
2740 (setq mime-edit-pgp-processing pgp-processing)
2742 (run-hooks 'mime-edit-translate-hook)
2743 (mime-edit-translate-buffer)
2744 (goto-char (point-min))
2745 (if (re-search-forward
2746 (concat "^" (regexp-quote separator) "$"))
2750 (make-local-variable 'mime-edit-temp-message-buffer)
2751 (setq mime-edit-temp-message-buffer buf)))
2753 (defun mime-edit-quitting-method ()
2754 "Quitting method for mime-view."
2755 (let* ((temp mime-edit-temp-message-buffer)
2757 (mime-preview-kill-buffer)
2759 (setq buf mime-edit-buffer)
2761 (switch-to-buffer buf)))
2763 (set-alist 'mime-preview-quitting-method-alist
2764 'mime-temp-message-mode
2765 #'mime-edit-quitting-method)
2771 (defvar mime-edit-again-ignored-field-regexp
2772 (concat "^\\(" "Content-.*\\|Mime-Version"
2773 (if mime-edit-insert-user-agent-field "\\|User-Agent")
2775 "Regexp for deleted header fields when `mime-edit-again' is called.")
2777 (defsubst eliminate-top-spaces (string)
2778 "Eliminate top sequence of space or tab in STRING."
2779 (if (string-match "^[ \t]+" string)
2780 (substring string (match-end 0))
2783 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
2786 (cdr (assoc (mime-content-type-parameter content-type "protocol")
2787 '(("application/pgp-encrypted" . pgp-encrypted)
2788 ("application/pgp-signature" . pgp-signed))))
2789 (mime-content-type-subtype content-type)))
2790 (boundary (mime-content-type-parameter content-type "boundary"))
2791 (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
2792 (re-search-forward boundary-pat nil t)
2793 (let ((bb (match-beginning 0)) eb tag)
2794 (setq tag (format "\n--<<%s>>-{\n" subtype))
2797 (setq bb (+ bb (length tag)))
2799 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2801 (setq eb (match-beginning 0))
2802 (replace-match (format "--}-<<%s>>\n" subtype))
2804 (narrow-to-region bb eb)
2805 (goto-char (point-min))
2806 (while (re-search-forward boundary-pat nil t)
2807 (let ((beg (match-beginning 0))
2809 (delete-region beg (match-end 0))
2811 (if (re-search-forward boundary-pat nil t)
2812 (setq end (match-beginning 0))
2813 (setq end (point-max))
2816 (narrow-to-region beg end)
2818 ((eq subtype 'pgp-encrypted)
2821 (goto-char (point-min))
2822 (re-search-forward "^-+BEGIN PGP MESSAGE-+$"
2825 (save-window-excursion
2826 (pgg-decrypt-region (match-beginning 0)
2828 (delete-region (point-min)(point-max))))
2829 (insert-buffer-substring pgg-output-buffer)
2830 (mime-edit-decode-message-in-buffer
2831 nil not-decode-text)
2832 (delete-region (goto-char (point-min))
2833 (if (search-forward "\n\n" nil t)
2836 (goto-char (point-max))
2839 (mime-edit-decode-message-in-buffer
2840 (if (eq subtype 'digest)
2842 (make-mime-content-type 'message 'rfc822))
2845 (goto-char (point-max))
2849 (goto-char (point-min))
2850 (or (= (point-min) 1)
2851 (delete-region (point-min)
2852 (if (search-forward "\n\n" nil t)
2858 (defun mime-edit-decode-single-part-in-buffer
2859 (content-type not-decode-text &optional content-disposition)
2860 (let* ((type (mime-content-type-primary-type content-type))
2861 (subtype (mime-content-type-subtype content-type))
2862 (ctype (format "%s/%s" type subtype))
2864 (pstr (let ((bytes (+ 14 (length ctype))))
2865 (mapconcat (function
2867 (if (string= (car attr) "charset")
2869 (setq charset (cdr attr))
2871 (let* ((str (concat (car attr)
2874 (setq bytes (+ bytes bs 2))
2877 (setq bytes (+ bs 1))
2881 (mime-content-type-parameters content-type) "")))
2884 (limit (save-excursion
2885 (if (search-forward "\n\n" nil t)
2888 (mime-content-disposition-type content-disposition))
2890 (if disposition-type
2891 (let ((bytes (+ 21 (length (format "%s" disposition-type)))))
2892 (mapconcat (function
2897 (if (string-equal "filename"
2899 (std11-wrap-as-quoted-string
2903 (setq bytes (+ bytes bs 2))
2906 (setq bytes (+ bs 1))
2910 (mime-content-disposition-parameters
2911 content-disposition)
2914 (if disposition-type
2915 (setq pstr (format "%s\nContent-Disposition: %s%s"
2916 pstr disposition-type disposition-str))
2919 (if (re-search-forward
2920 "^Content-Transfer-Encoding:" limit t)
2921 (let ((beg (match-beginning 0))
2922 (hbeg (match-end 0))
2923 (end (std11-field-end limit)))
2926 (eliminate-top-spaces
2927 (std11-unfold-string
2928 (buffer-substring hbeg end)))))
2929 (if (or charset (eq type 'text))
2931 (delete-region beg (1+ end))
2932 (goto-char (point-min))
2933 (if (search-forward "\n\n" nil t)
2936 (match-end 0)(point-max) encoding)
2940 (if (or encoded (not not-decode-text))
2943 (goto-char (point-min))
2944 (while (re-search-forward "\r\n" nil t)
2945 (replace-match "\n")
2947 (decode-mime-charset-region (point-min)(point-max)
2948 (or charset default-mime-charset))
2950 (let ((he (if (re-search-forward "^$" nil t)
2954 (if (and (eq type 'text)
2955 (eq subtype 'x-rot13-47-48))
2956 (mule-caesar-region he (point-max))
2958 (if (= (point-min) 1)
2964 (format "%s/%s%s" type subtype pstr)
2967 (delete-region (point-min) he)
2969 (mime-create-tag (format "%s/%s%s" type subtype pstr)
2975 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
2978 (goto-char (point-min))
2979 (let ((ctl (or (mime-read-Content-Type)
2980 default-content-type)))
2982 (let ((type (mime-content-type-primary-type ctl)))
2984 ((and (eq type 'application)
2985 (eq (mime-content-type-subtype ctl) 'pgp-signature))
2986 (delete-region (point-min)(point-max))
2988 ((eq type 'multipart)
2989 (mime-edit-decode-multipart-in-buffer ctl not-decode-text)
2992 (mime-edit-decode-single-part-in-buffer
2993 ctl not-decode-text (mime-read-Content-Disposition))
2996 (decode-mime-charset-region (point-min) (point-max)
2997 default-mime-charset))
2999 (if (= (point-min) 1)
3002 (std11-narrow-to-header)
3003 (goto-char (point-min))
3004 (while (re-search-forward
3005 mime-edit-again-ignored-field-regexp nil t)
3006 (delete-region (match-beginning 0) (1+ (std11-field-end)))
3008 (mime-decode-header-in-buffer (not not-decode-text))
3013 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
3014 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
3015 Content-Type and Content-Transfer-Encoding header fields will be
3016 converted to MIME-Edit tags."
3018 (goto-char (point-min))
3020 (concat "\n" (regexp-quote mail-header-separator) "\n")
3022 (replace-match "\n\n")
3024 (mime-edit-decode-message-in-buffer nil not-decode-text)
3025 (goto-char (point-min))
3027 (and (re-search-forward "^$")
3028 (replace-match mail-header-separator)
3038 (provide 'mime-edit)
3040 (run-hooks 'mime-edit-load-hook)
3042 ;;; mime-edit.el ends here