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., 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)
119 (autoload 'pgg-encrypt-region "pgg"
120 "PGP encryption of current region." t)
121 (autoload 'pgg-sign-region "pgg"
122 "PGP signature of current region." t)
123 (autoload 'pgg-insert-key "pgg"
124 "Insert PGP public key at point." t)
125 (autoload 'smime-encrypt-buffer "smime"
126 "S/MIME encryption of current buffer.")
127 (autoload 'smime-sign-buffer "smime"
128 "S/MIME signature of current buffer.")
135 (defconst mime-edit-version
137 (mime-product-name mime-user-interface-product) " "
138 (mapconcat #'number-to-string
139 (mime-product-version mime-user-interface-product) ".")
140 " - \"" (mime-product-code-name mime-user-interface-product) "\"")))
146 (defgroup mime-edit nil
150 (defcustom mime-ignore-preceding-spaces nil
151 "*Ignore preceding white spaces if non-nil."
155 (defcustom mime-ignore-trailing-spaces nil
156 "*Ignore trailing white spaces if non-nil."
160 (defcustom mime-ignore-same-text-tag t
161 "*Ignore preceding text content-type tag that is same with new one.
162 If non-nil, the text tag is not inserted unless something different."
166 (defcustom mime-auto-hide-body t
167 "*Hide non-textual body encoded in base64 after insertion if non-nil."
171 (defcustom mime-edit-voice-recorder
172 (function mime-edit-voice-recorder-for-sun)
173 "*Function to record a voice message and encode it."
177 (defcustom mime-edit-mode-hook nil
178 "*Hook called when enter MIME mode."
182 (defcustom mime-edit-translate-hook nil
183 "*Hook called before translating into a MIME compliant message.
184 To insert a signature file automatically, call the function
185 `mime-edit-insert-signature' from this hook."
189 (defcustom mime-edit-exit-hook nil
190 "*Hook called when exit MIME mode."
194 (defvar mime-content-types
196 ;; Charset parameter need not to be specified, since it is
197 ;; defined automatically while translation.
199 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
212 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
213 ("directory" "/pub/GNU/elisp/mime")
215 ("mode" "image" "ascii" "local8"))
220 ("mode" "image" "ascii" "local8"))
221 ("tftp" ("site") ("name"))
222 ("afs" ("site") ("name"))
223 ("local-file" ("site") ("name"))
225 ("server" "ftpmail@nic.karrn.ad.jp")
231 ("octet-stream" ("type" "" "tar" "shar"))
233 ("vnd.ms-powerpoint")
234 ("x-kiss" ("x-cnf")))
246 "*Alist of content-type, subtype, parameters and its values.")
248 (defcustom mime-file-types
251 ;; Programming languages
254 "application" "octet-stream" (("type" . "C++"))
256 "attachment" (("filename" . file)))
259 "application" "octet-stream" (("type" . "emacs-lisp"))
261 "attachment" (("filename" . file)))
264 "application" "octet-stream" (("type" . "common-lisp"))
266 "attachment" (("filename" . file)))
269 "application" "octet-stream" (("type" . "perl"))
271 "attachment" (("filename" . file)))
273 ;; Text or translated text
278 "inline" (("filename" . file)))
280 ;; .rc : procmail modules pm-xxxx.rc
281 ;; *rc : other resource files
283 ("\\.\\(rc\\|lst\\|log\\|sql\\|mak\\)$\\|\\..*rc$"
286 "attachment" (("filename" . file)))
293 ("\\.diff$\\|\\.patch$"
294 "application" "octet-stream" (("type" . "patch"))
296 "attachment" (("filename" . file)))
299 "text" "plain" nil nil nil nil)
302 ;; Octect binary text
305 "application" "msword" nil
307 "attachment" (("filename" . file)))
308 ("\\.ppt$" ; MS Power Point
309 "application" "vnd.ms-powerpoint" nil
311 "attachment" (("filename" . file)))
316 "inline" (("filename" . file)))
318 "application" "postscript" nil
320 "attachment" (("filename" . file)))
324 ("\\.jpg$\\|\\.jpeg$"
327 "inline" (("filename" . file)))
331 "inline" (("filename" . file)))
335 "inline" (("filename" . file)))
339 "inline" (("filename" . file)))
343 "inline" (("filename" . file)))
347 "inline" (("filename" . file)))
351 "inline" (("filename" . file)))
355 "inline" (("filename" . file)))
359 "attachment" (("filename" . file)))
363 "attachment" (("filename" . file)))
365 "application" "octet-stream" (("type" . "tar+gzip"))
367 "attachment" (("filename" . file)))
369 "application" "octet-stream" (("type" . "tar+gzip"))
371 "attachment" (("filename" . file)))
373 "application" "octet-stream" (("type" . "tar+compress"))
375 "attachment" (("filename" . file)))
377 "application" "octet-stream" (("type" . "tar+compress"))
379 "attachment" (("filename" . file)))
381 "application" "octet-stream" (("type" . "gzip"))
383 "attachment" (("filename" . file)))
385 "application" "octet-stream" (("type" . "compress"))
387 "attachment" (("filename" . file)))
389 "application" "octet-stream" (("type" . "lha"))
391 "attachment" (("filename" . file)))
393 "application" "zip" nil
395 "attachment" (("filename" . file)))
400 "application" "octet-stream" nil
402 "attachment" (("filename" . file))))
403 "*Alist of file name, types, parameters, and default encoding.
404 If encoding is nil, it is determined from its contents."
408 (choice :tag "Primary-Type"
409 ,@(nconc (mapcar (lambda (cell)
410 (list 'item (car cell)))
414 (choice :tag "Sub-Type"
417 (mapcar (lambda (cell)
418 (mapcar (lambda (cell)
419 (list 'item (car cell)))
424 (repeat :tag "Parameters of Content-Type field"
425 (cons string (choice string symbol)))
426 ;; content-transfer-encoding
427 (choice :tag "Encoding"
430 (mapcar (lambda (cell)
432 (mime-encoding-list))))
434 (choice :tag "Disposition-Type"
440 (repeat :tag "Parameters of Content-Disposition field"
441 (cons string (choice string symbol)))))
445 ;;; @@ about charset, encoding and transfer-level
448 (defvar mime-charset-type-list
450 (iso-8859-1 8 "quoted-printable")
451 (iso-8859-2 8 "quoted-printable")
452 (iso-8859-3 8 "quoted-printable")
453 (iso-8859-4 8 "quoted-printable")
454 (iso-8859-5 8 "quoted-printable")
455 (koi8-r 8 "quoted-printable")
456 (iso-8859-7 8 "quoted-printable")
457 (iso-8859-8 8 "quoted-printable")
458 (iso-8859-9 8 "quoted-printable")
459 (iso-2022-jp 7 "base64")
460 (iso-2022-jp-3 7 "base64")
461 (iso-2022-kr 7 "base64")
467 (shift_jis 8 "base64")
469 (iso-2022-jp-2 7 "base64")
470 (iso-2022-int-1 7 "base64")))
472 (defvar mime-transfer-level 7
473 "*A number of network transfer level. It should be bigger than 7.")
474 (make-variable-buffer-local 'mime-transfer-level)
476 (defsubst mime-encoding-name (transfer-level &optional not-omit)
477 (cond ((> transfer-level 8) "binary")
478 ((= transfer-level 8) "8bit")
481 (defvar mime-transfer-level-string
482 (mime-encoding-name mime-transfer-level 'not-omit)
483 "A string formatted version of mime-transfer-level")
484 (make-variable-buffer-local 'mime-transfer-level-string)
486 ;;; @@ about content transfer encoding
488 (defvar mime-content-transfer-encoding-priority-list
489 '(nil "8bit" "binary"))
491 ;;; @@ about message inserting
494 (defvar mime-edit-yank-ignored-field-list
495 '("Received" "Approved" "Path" "Replied" "Status"
496 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
497 "Delete these fields from original message when it is inserted
498 as message/rfc822 part.
499 Each elements are regexp of field-name.")
501 (defvar mime-edit-yank-ignored-field-regexp
503 (apply (function regexp-or) mime-edit-yank-ignored-field-list)
506 (defvar mime-edit-message-inserter-alist nil)
507 (defvar mime-edit-mail-inserter-alist nil)
510 ;;; @@ about message splitting
513 (defcustom mime-edit-split-message t
514 "*Split large message if it is non-nil."
518 (defcustom mime-edit-message-default-max-lines 1000
519 "*Default maximum lines of a message."
523 (defcustom mime-edit-message-max-lines-alist
524 '((news-reply-mode . 500))
525 "Alist of major-mode vs maximum lines of a message.
526 If it is not specified for a major-mode,
527 `mime-edit-message-default-max-lines' is used."
531 (defconst mime-edit-split-ignored-field-regexp
532 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|^Message-Id:\\)")
534 (defcustom mime-edit-split-blind-field-regexp
535 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
536 "*Regular expression to match field-name to be ignored when split sending."
540 (defvar mime-edit-split-message-sender-alist nil)
542 (defvar mime-edit-news-reply-mode-server-running nil)
548 (defconst mime-edit-single-part-tag-regexp
549 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
550 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
552 (defconst mime-edit-quoted-single-part-tag-regexp
553 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
555 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
557 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
559 (defconst mime-edit-beginning-tag-regexp
560 (regexp-or mime-edit-single-part-tag-regexp
561 mime-edit-multipart-beginning-regexp))
563 (defconst mime-edit-end-tag-regexp
564 (regexp-or mime-edit-single-part-tag-regexp
565 mime-edit-multipart-end-regexp))
567 (defconst mime-edit-tag-regexp
568 (regexp-or mime-edit-single-part-tag-regexp
569 mime-edit-multipart-beginning-regexp
570 mime-edit-multipart-end-regexp))
572 (defvar mime-tag-format "--[[%s]]"
573 "*Control-string making a MIME tag.")
575 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
576 "*Control-string making a MIME tag with encoding.")
579 ;;; @@ multipart boundary
582 (defvar mime-multipart-boundary "Multipart"
583 "*Boundary of a multipart message.")
586 ;;; @@ optional header fields
589 (defvar mime-edit-insert-user-agent-field t
590 "*If non-nil, insert User-Agent header field.")
592 (defvar mime-edit-user-agent-value
593 (concat (mime-product-name mime-user-interface-product)
595 (mapconcat #'number-to-string
596 (mime-product-version mime-user-interface-product) ".")
598 (mime-product-code-name mime-user-interface-product)
600 (mime-product-name mime-library-product)
602 (mapconcat #'number-to-string
603 (mime-product-version mime-library-product) ".")
605 (mime-product-code-name mime-library-product)
607 (if (fboundp 'apel-version)
608 (concat (apel-version) " "))
609 (if (featurep 'xemacs)
610 (concat (cond ((featurep 'utf-2000)
611 (concat "UTF-2000-MULE/" utf-2000-version))
612 ((featurep 'mule) "MULE"))
614 (if (string-match "^[0-9]+\\(\\.[0-9]+\\)" emacs-version)
617 (substring emacs-version 0 (match-end 0))
618 (cond ((and (boundp 'xemacs-betaname)
620 ;; It does not exist in XEmacs
621 ;; versions prior to 20.3.
622 (concat " " xemacs-betaname))
623 ((and (boundp 'emacs-patch-level)
625 ;; It does not exist in FSF Emacs or in
626 ;; XEmacs versions earlier than 21.1.1.
627 (format " (patch %d)" emacs-patch-level))
629 " (" xemacs-codename ")"
630 ;; `xemacs-extra-name' has appeared in the
631 ;; development version of XEmacs 21.5-b8.
632 (if (and (boundp 'xemacs-extra-name)
633 (symbol-value 'xemacs-extra-name))
634 (concat " " (symbol-value 'xemacs-extra-name))
637 system-configuration ")")
638 " (" emacs-version ")"))
639 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
640 (substring emacs-version 0 (match-beginning 0))
643 (if (boundp 'enable-multibyte-characters)
645 " (" system-configuration ")"
646 (if enable-multibyte-characters
647 (concat " MULE/" mule-version)
648 " (with unibyte mode)")
649 (if (featurep 'meadow)
650 (let ((mver (Meadow-version)))
651 (if (string-match "^Meadow-" mver)
655 (concat "MULE/" mule-version
656 " (based on Emacs " ver ")"))
657 (concat "Emacs/" ver " (" system-configuration ")")))))
658 "Body of User-Agent field.
659 If variable `mime-edit-insert-user-agent-field' is not nil, it is
660 inserted into message header.")
666 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
667 "*Specify MIME tspecials.
668 Tspecials means any character that matches with it in header must be quoted.")
670 (defconst mime-edit-mime-version-value
671 (concat "1.0 (generated by " mime-edit-version ")")
672 "MIME version number.")
674 (defconst mime-edit-mime-version-field-for-message/partial
675 (concat "MIME-Version:"
676 (mime-encode-field-body
677 (concat " 1.0 (split by " mime-edit-version ")\n")
679 "MIME version field for message/partial.")
682 ;;; @ keymap and menu
685 (defvar mime-edit-mode-flag nil)
686 (make-variable-buffer-local 'mime-edit-mode-flag)
688 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
689 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
690 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
691 "Keymap for MIME-Edit mode commands to insert entity or set status.")
693 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
694 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
695 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
696 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
697 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
698 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
699 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
700 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
701 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
702 (define-key mime-edit-mode-entity-map "t" 'mime-edit-insert-tag)
704 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
705 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
706 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
707 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
708 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
709 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
710 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
711 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
712 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
713 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
715 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
716 "Keymap prefix for MIME-Edit mode commands about enclosure.")
717 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
718 "Keymap for MIME-Edit mode commands about enclosure.")
720 (define-key mime-edit-mode-enclosure-map
721 "\C-a" 'mime-edit-enclose-alternative-region)
722 (define-key mime-edit-mode-enclosure-map
723 "\C-p" 'mime-edit-enclose-parallel-region)
724 (define-key mime-edit-mode-enclosure-map
725 "\C-m" 'mime-edit-enclose-mixed-region)
726 (define-key mime-edit-mode-enclosure-map
727 "\C-d" 'mime-edit-enclose-digest-region)
728 (define-key mime-edit-mode-enclosure-map
729 "\C-s" 'mime-edit-enclose-pgp-signed-region)
730 (define-key mime-edit-mode-enclosure-map
731 "\C-e" 'mime-edit-enclose-pgp-encrypted-region)
732 (define-key mime-edit-mode-enclosure-map
733 "\C-q" 'mime-edit-enclose-quote-region)
735 (defvar mime-edit-mode-map (make-sparse-keymap)
736 "Keymap for MIME-Edit mode commands.")
737 (define-key mime-edit-mode-map
738 mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
739 (define-key mime-edit-mode-map
740 mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
742 (defconst mime-edit-menu-title "MIME-Edit")
744 (defconst mime-edit-menu-list
745 '((mime-help "Describe MIME editor mode" mime-edit-help)
746 (file "Insert File" mime-edit-insert-file)
747 (external "Insert External" mime-edit-insert-external)
748 (voice "Insert Voice" mime-edit-insert-voice)
749 (message "Insert Message" mime-edit-insert-message)
750 (mail "Insert Mail" mime-edit-insert-mail)
751 (signature "Insert Signature" mime-edit-insert-signature)
752 (text "Insert Text" mime-edit-insert-text)
753 (tag "Insert Tag" mime-edit-insert-tag)
754 (alternative "Enclose as alternative"
755 mime-edit-enclose-alternative-region)
756 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
757 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
758 (digest "Enclose as digest" mime-edit-enclose-digest-region)
759 (signed "Enclose as signed" mime-edit-enclose-pgp-signed-region)
760 (encrypted "Enclose as encrypted" mime-edit-enclose-pgp-encrypted-region)
761 (quote "Verbatim region" mime-edit-enclose-quote-region)
762 (key "Insert Public Key" mime-edit-insert-key)
763 (split "Set splitting" mime-edit-set-split)
764 (sign "PGP sign" mime-edit-set-sign)
765 (encrypt "PGP encrypt" mime-edit-set-encrypt)
766 (preview "Preview Message" mime-edit-preview-message)
767 (level "Toggle transfer-level" mime-edit-toggle-transfer-level))
768 "MIME-edit menubar entry.")
770 (cond ((featurep 'xemacs)
771 ;; modified by Pekka Marjola <pema@iki.fi>
772 ;; 1995/9/5 (c.f. [tm-en:69])
773 (defun mime-edit-define-menu-for-xemacs ()
774 "Define menu for XEmacs."
775 (cond ((featurep 'menubar)
776 (make-local-variable 'current-menubar)
777 (set-buffer-menubar current-menubar)
780 (cons mime-edit-menu-title
783 (vector (nth 1 item)(nth 2 item)
784 mime-edit-mode-flag)))
785 mime-edit-menu-list))))))
787 ;; modified by Steven L. Baur <steve@miranova.com>
788 ;; 1995/12/6 (c.f. [tm-en:209])
789 (or (boundp 'mime-edit-popup-menu-for-xemacs)
790 (setq mime-edit-popup-menu-for-xemacs
791 (append '("MIME Commands" "---")
792 (mapcar (function (lambda (item)
796 mime-edit-menu-list)))))
797 ((>= emacs-major-version 19)
798 (define-key mime-edit-mode-map [menu-bar mime-edit]
799 (cons mime-edit-menu-title
800 (make-sparse-keymap mime-edit-menu-title)))
803 (define-key mime-edit-mode-map
804 (vector 'menu-bar 'mime-edit (car item))
805 (cons (nth 1 item)(nth 2 item)))))
806 (reverse mime-edit-menu-list))))
812 (defvar mime-edit-touched-flag nil)
815 (defun mime-edit-mode ()
816 "MIME minor mode for editing the tagged MIME message.
818 In this mode, basically, the message is composed in the tagged MIME
819 format. The message tag looks like:
821 --[[text/plain; charset=ISO-2022-JP][7bit]]
823 The tag specifies the MIME content type, subtype, optional parameters
824 and transfer encoding of the message following the tag. Messages
825 without any tag are treated as `text/plain' by default. Charset and
826 transfer encoding are automatically defined unless explicitly
827 specified. Binary messages such as audio and image are usually
828 hidden. The messages in the tagged MIME format are automatically
829 translated into a MIME compliant message when exiting this mode.
831 Available charsets depend on Emacs version being used. The following
832 lists the available charsets of each emacs.
834 Without mule: US-ASCII and ISO-8859-1 (or other charset) are available.
835 With mule: US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
836 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
837 CN-BIG5 and ISO-2022-INT-1 are available.
839 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
840 be used to represent multilingual text in intermixed manner. Any
841 languages that has no registered charset are represented as either
842 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
844 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
845 without mule, please set variable `default-mime-charset'. This
846 variable must be symbol of which name is a MIME charset.
848 If you want to add more charsets in mule, please set variable
849 `charsets-mime-charset-alist'. This variable must be alist of which
850 key is list of charset and value is symbol of MIME charset. If name
851 of coding-system is different as MIME charset, please set variable
852 `mime-charset-coding-system-alist'. This variable must be alist of
853 which key is MIME charset and value is coding-system.
855 Following commands are available in addition to major mode commands:
858 \\[mime-edit-insert-text] insert a text message.
859 \\[mime-edit-insert-file] insert a (binary) file.
860 \\[mime-edit-insert-external] insert a reference to external body.
861 \\[mime-edit-insert-voice] insert a voice message.
862 \\[mime-edit-insert-message] insert a mail or news message.
863 \\[mime-edit-insert-mail] insert a mail message.
864 \\[mime-edit-insert-signature] insert a signature file at end.
865 \\[mime-edit-insert-key] insert PGP public key.
866 \\[mime-edit-insert-tag] insert a new MIME tag.
868 \[make enclosure (maybe multipart)\]
869 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
870 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
871 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
872 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
873 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
874 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
875 \\[mime-edit-enclose-quote-region] enclose as verbose mode
876 (to avoid to expand tags)
879 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
880 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
881 \\[mime-edit-set-split] set message splitting mode.
882 \\[mime-edit-set-sign] set PGP-sign mode.
883 \\[mime-edit-set-encrypt] set PGP-encryption mode.
884 \\[mime-edit-preview-message] preview editing MIME message.
885 \\[mime-edit-exit] exit and translate into a MIME
887 \\[mime-edit-help] show this help.
888 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
891 Additional commands are available in some major modes:
892 C-c C-c exit, translate and run the original command.
893 C-c C-s exit, translate and run the original command.
895 The following is a message example written in the tagged MIME format.
896 TABs at the beginning of the line are not a part of the message:
898 This is a conventional plain text. It should be translated
901 This is also a plain text. But, it is explicitly specified as
903 --[[text/plain; charset=ISO-8859-1]]
904 This is also a plain text. But charset is specified as
907 ¡Hola! Buenos días. ¿Cómo está usted?
909 This is a <bold>enriched text</bold>.
910 --[[image/gif][base64]]...image encoded in base64 here...
911 --[[audio/basic][base64]]...audio encoded in base64 here...
913 User customizable variables (not documented all of them):
915 Specifies a key prefix for MIME minor mode commands.
917 mime-ignore-preceding-spaces
918 Preceding white spaces in a message body are ignored if non-nil.
920 mime-ignore-trailing-spaces
921 Trailing white spaces in a message body are ignored if non-nil.
924 Hide a non-textual body message encoded in base64 after insertion
928 A number of network transfer level. It should be bigger than 7.
929 If you are in 8bit-through environment, please set 8.
931 mime-edit-voice-recorder
932 Specifies a function to record a voice message and encode it.
933 The function `mime-edit-voice-recorder-for-sun' is for Sun
937 Turning on MIME mode calls the value of mime-edit-mode-hook, if
940 mime-edit-translate-hook
941 The value of mime-edit-translate-hook is called just before translating
942 the tagged MIME format into a MIME compliant message if it is
943 non-nil. If the hook call the function mime-edit-insert-signature,
944 the signature file will be inserted automatically.
947 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
950 (if mime-edit-mode-flag
952 (if mime-edit-touched-flag
954 (make-local-variable 'mime-edit-touched-flag)
955 (setq mime-edit-touched-flag t)
956 (turn-on-mime-edit))))
959 (cond ((featurep 'xemacs)
960 (add-minor-mode 'mime-edit-mode-flag
961 '((" MIME-Edit " mime-transfer-level-string))
966 (set-alist 'minor-mode-alist
968 '((" MIME-Edit " mime-transfer-level-string)))
969 (set-alist 'minor-mode-map-alist
971 mime-edit-mode-map)))
975 (defun turn-on-mime-edit ()
976 "Unconditionally turn on MIME-Edit mode."
978 (if mime-edit-mode-flag
979 (error "You are already editing a MIME message.")
980 (setq mime-edit-mode-flag t)
982 ;; Set transfer level into mode line
984 (setq mime-transfer-level-string
985 (mime-encoding-name mime-transfer-level 'not-omit))
986 (force-mode-line-update)
988 ;; Define menu for XEmacs.
989 (if (featurep 'xemacs)
990 (mime-edit-define-menu-for-xemacs))
992 ;; I don't care about saving these.
993 (setq paragraph-start
994 (regexp-or mime-edit-single-part-tag-regexp
996 (setq paragraph-separate
997 (regexp-or mime-edit-single-part-tag-regexp
999 (run-hooks 'mime-edit-mode-hook)
1002 (substitute-command-keys
1003 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))))
1006 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1009 (defun mime-edit-exit (&optional nomime no-error)
1010 "Translate the tagged MIME message into a MIME compliant message.
1011 With no argument encode a message in the buffer into MIME, otherwise
1012 just return to previous mode."
1014 (if (not mime-edit-mode-flag)
1016 (error "You aren't editing a MIME message."))
1019 (run-hooks 'mime-edit-translate-hook)
1020 (mime-edit-translate-buffer)))
1021 ;; Restore previous state.
1022 (setq mime-edit-mode-flag nil)
1023 (if (and (featurep 'xemacs)
1024 (featurep 'menubar))
1025 (delete-menu-item (list mime-edit-menu-title)))
1026 (set-buffer-modified-p (buffer-modified-p))
1027 (run-hooks 'mime-edit-exit-hook)
1028 (message "Exit MIME editor mode.")))
1030 (defun mime-edit-maybe-translate ()
1032 (mime-edit-exit nil t)
1033 (call-interactively 'mime-edit-maybe-split-and-send))
1035 (defun mime-edit-help ()
1036 "Show help message about MIME mode."
1038 (with-output-to-temp-buffer "*Help*"
1039 (princ "MIME editor mode:\n")
1040 (princ (documentation 'mime-edit-mode))
1041 (print-help-return-message)))
1043 (defun mime-edit-insert-text (&optional subtype)
1044 "Insert a text message.
1045 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1046 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1048 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1050 (if (looking-at mime-edit-single-part-tag-regexp)
1052 ;; Make a space between the following message.
1055 (if (and (member (cadr ret) '("enriched"))
1056 (fboundp 'enriched-mode))
1058 (if (boundp 'enriched-mode)
1059 (enriched-mode -1))))))
1061 (defun mime-edit-insert-file (file &optional verbose)
1062 "Insert a message from a file."
1063 (interactive "fInsert file as MIME message: \nP")
1064 (let* ((guess (mime-find-file-type file))
1065 (type (nth 0 guess))
1066 (subtype (nth 1 guess))
1067 (parameters (nth 2 guess))
1068 (encoding (nth 3 guess))
1069 (disposition-type (nth 4 guess))
1070 (disposition-params (nth 5 guess)))
1072 (setq type (mime-prompt-for-type type)
1073 subtype (mime-prompt-for-subtype type subtype)))
1074 (if (or (interactive-p) verbose)
1075 (setq encoding (mime-prompt-for-encoding encoding)))
1076 (if (or (consp parameters) (stringp disposition-type))
1077 (let ((rest parameters) cell attribute value)
1078 (setq parameters "")
1080 (setq cell (car rest))
1081 (setq attribute (car cell))
1082 (setq value (cdr cell))
1083 (if (eq value 'file)
1084 (setq value (std11-wrap-as-quoted-string
1085 (file-name-nondirectory file))))
1086 (setq parameters (concat parameters "; " attribute "=" value))
1087 (setq rest (cdr rest)))
1088 (if disposition-type
1091 (concat parameters "\n"
1092 "Content-Disposition: " disposition-type))
1093 (setq rest disposition-params)
1095 (setq cell (car rest))
1096 (setq attribute (car cell))
1097 (setq value (cdr cell))
1098 (if (eq value 'file)
1099 (setq value (std11-wrap-as-quoted-string
1100 (file-name-nondirectory file))))
1102 (concat parameters "; " attribute "=" value))
1103 (setq rest (cdr rest)))))))
1104 (mime-edit-insert-tag type subtype parameters)
1105 (mime-edit-insert-binary-file file encoding)))
1107 (defun mime-edit-insert-external ()
1108 "Insert a reference to external body."
1110 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1112 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1114 (let* ((pritype (mime-prompt-for-type))
1115 (subtype (mime-prompt-for-subtype pritype))
1116 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1119 (insert "Content-Type: "
1120 pritype "/" subtype (or parameters "") "\n")))
1121 (if (and (not (eobp))
1122 (not (looking-at mime-edit-single-part-tag-regexp)))
1123 (insert (mime-make-text-tag) "\n")))
1125 (defun mime-edit-insert-voice ()
1126 "Insert a voice message."
1130 "What transfer encoding: "
1131 (mime-encoding-alist) nil t nil)))
1132 (mime-edit-insert-tag "audio" "basic" nil)
1133 (mime-edit-define-encoding encoding)
1135 (narrow-to-region (point)(point))
1137 (funcall mime-edit-voice-recorder encoding)
1140 (add-text-properties
1141 (point-min)(point-max) '(invisible t mime-edit-invisible t))
1142 (goto-char (point-max)))))))
1144 (defun mime-edit-insert-signature (&optional arg)
1145 "Insert a signature file."
1147 (let ((signature-insert-hook
1150 (let ((items (mime-find-file-type signature-file-name)))
1151 (apply (function mime-edit-insert-tag)
1152 (car items) (cadr items) (list (caddr items))))))))
1153 (insert-signature arg)))
1156 ;; Insert a new tag around a point.
1158 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1159 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1160 If nothing is inserted, return nil."
1163 (mime-edit-goto-tag)
1164 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1165 (< (match-beginning 0) p)
1166 (< p (match-end 0)))
1167 (goto-char (match-beginning 0))
1174 (mime-prompt-for-type)))
1177 (mime-prompt-for-subtype pritype)))
1180 (mime-prompt-for-parameters pritype subtype delimiter)))
1181 ;; Make a new MIME tag.
1182 (setq newtag (mime-make-tag pritype subtype parameters))
1183 ;; Find an current MIME tag.
1186 (if (mime-edit-goto-tag)
1187 (buffer-substring (match-beginning 0) (match-end 0))
1188 ;; Assume content type is 'text/plan'.
1189 (mime-make-tag "text" "plain"))))
1190 ;; We are only interested in TEXT.
1192 (not (mime-test-content-type
1193 (mime-edit-get-contype oldtag) "text")))
1196 (if (or (not oldtag) ;Not text
1197 (or mime-ignore-same-text-tag
1198 (not (string-equal oldtag newtag))))
1200 ;; Mark the beginning of the tag for convenience.
1201 (push-mark (point) 'nomsg)
1202 (insert newtag "\n")
1203 (list pritype subtype parameters) ;New tag is created.
1205 ;; Restore previous point.
1207 nil ;Nothing is created.
1210 (defun mime-edit-insert-binary-file (file &optional encoding)
1211 "Insert binary FILE at point.
1212 Optional argument ENCODING specifies an encoding method such as base64."
1213 (let* ((tagend (1- (point))) ;End of the tag
1214 (hide-p (and mime-auto-hide-body
1217 (let ((en (downcase encoding)))
1218 (or (string-equal en "7bit")
1219 (string-equal en "8bit")
1220 (string-equal en "binary")))))))
1222 (narrow-to-region (point)(point))
1223 (mime-insert-encoded-file file encoding)
1225 (add-text-properties
1226 (point-min)(point-max) '(invisible t mime-edit-invisible t)))
1227 (goto-char (point-max)))
1229 (looking-at mime-edit-tag-regexp)
1230 (= (point)(point-max))
1231 (mime-edit-insert-tag "text" "plain"))
1232 ;; Define encoding even if it is 7bit.
1233 (if (stringp encoding)
1235 (goto-char tagend) ; Make sure which line the tag is on.
1236 (mime-edit-define-encoding encoding)))))
1239 ;; Commands work on a current message flagment.
1241 (defun mime-edit-goto-tag ()
1242 "Search for the beginning of the tagged MIME message."
1243 (let ((current (point)))
1244 (if (looking-at mime-edit-tag-regexp)
1246 ;; At first, go to the end.
1247 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1248 (goto-char (1- (match-beginning 0))) ;For multiline tag
1251 (goto-char (point-max))))
1252 ;; Then search for the beginning.
1253 (re-search-backward mime-edit-end-tag-regexp nil t)
1254 (or (looking-at mime-edit-beginning-tag-regexp)
1255 ;; Restore previous point.
1260 (defun mime-edit-content-beginning ()
1261 "Return the point of the beginning of content."
1263 (let ((beg (save-excursion
1264 (beginning-of-line) (point))))
1265 (if (mime-edit-goto-tag)
1266 (let ((top (point)))
1267 (goto-char (match-end 0))
1268 (if (and (= beg top)
1269 (= (following-char) ?\^M))
1273 ;; Default text/plain tag.
1274 (goto-char (point-min))
1276 (concat "\n" (regexp-quote mail-header-separator)
1277 (if mime-ignore-preceding-spaces
1278 "[ \t\n]*\n" "\n")) nil 'move)
1281 (defun mime-edit-content-end ()
1282 "Return the point of the end of content."
1284 (if (mime-edit-goto-tag)
1286 (goto-char (1+ (match-end 0)))
1287 (if (get-text-property (point) 'mime-edit-invisible)
1288 (or (next-single-property-change (point) 'mime-edit-invisible)
1290 ;; Move to the end of this text.
1291 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1292 ;; Don't forget a multiline tag.
1293 (goto-char (match-beginning 0)))
1295 ;; Assume the message begins with text/plain.
1296 (goto-char (mime-edit-content-beginning))
1297 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1298 ;; Don't forget a multiline tag.
1299 (goto-char (match-beginning 0)))
1302 (defun mime-edit-define-charset (charset)
1303 "Set charset of current tag to CHARSET."
1305 (if (mime-edit-goto-tag)
1306 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1307 (delete-region (match-beginning 0) (match-end 0))
1310 (mime-edit-set-parameter
1311 (mime-edit-get-contype tag)
1313 (let ((comment (get charset 'mime-charset-comment)))
1315 (concat (upcase (symbol-name charset)) " (" comment ")")
1316 (upcase (symbol-name charset)))))
1317 (mime-edit-get-encoding tag)))))))
1319 (defun mime-edit-define-encoding (encoding)
1320 "Set encoding of current tag to ENCODING."
1322 (if (mime-edit-goto-tag)
1323 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1324 (delete-region (match-beginning 0) (match-end 0))
1325 (insert (mime-create-tag (mime-edit-get-contype tag) encoding))))))
1327 (defun mime-edit-choose-charset ()
1328 "Choose charset of a text following current point."
1329 (detect-mime-charset-region (point) (mime-edit-content-end)))
1331 (defun mime-make-text-tag (&optional subtype)
1332 "Make a tag for a text after current point.
1333 Subtype of text type can be specified by an optional argument SUBTYPE.
1334 Otherwise, it is obtained from mime-content-types."
1335 (let* ((pritype "text")
1336 (subtype (or subtype
1337 (car (car (cdr (assoc pritype mime-content-types)))))))
1338 ;; Charset should be defined later.
1339 (mime-make-tag pritype subtype)))
1342 ;; Tag handling functions
1344 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1345 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1346 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1350 (defun mime-create-tag (contype &optional encoding)
1351 "Make a tag with CONTENT-TYPE and optional ENCODING."
1352 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1355 (defun mime-edit-get-contype (tag)
1356 "Return Content-Type (including parameters) of TAG."
1358 (or (string-match mime-edit-single-part-tag-regexp tag)
1359 (string-match mime-edit-multipart-beginning-regexp tag)
1360 (string-match mime-edit-multipart-end-regexp tag))
1361 (substring tag (match-beginning 1) (match-end 1))))
1363 (defun mime-edit-get-encoding (tag)
1364 "Return encoding of TAG."
1366 (string-match mime-edit-single-part-tag-regexp tag)
1368 (not (= (match-beginning 3) (match-end 3)))
1369 (substring tag (match-beginning 3) (match-end 3))))
1371 (defun mime-get-parameter (contype parameter)
1372 "For given CONTYPE return value for PARAMETER.
1373 Nil if no such parameter."
1377 (regexp-quote parameter)
1378 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1380 (substring contype (match-beginning 1) (match-end 1))
1381 nil ;No such parameter
1384 (defun mime-edit-set-parameter (contype parameter value)
1385 "For given CONTYPE set PARAMETER to VALUE."
1386 (let (ctype opt-fields)
1387 (if (string-match "\n[^ \t\n\r]+:" contype)
1388 (setq ctype (substring contype 0 (match-beginning 0))
1389 opt-fields (substring contype (match-beginning 0)))
1390 (setq ctype contype))
1394 (regexp-quote parameter)
1395 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1398 (concat (substring ctype 0 (match-beginning 1))
1400 (substring ctype (match-end 1))
1402 (concat ctype "; " parameter "=" value opt-fields)
1405 (defun mime-strip-parameters (contype)
1406 "Return primary content-type and subtype without parameters for CONTYPE."
1407 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1408 (substring contype (match-beginning 1) (match-end 1)) nil))
1410 (defun mime-test-content-type (contype type &optional subtype)
1411 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1412 (and (stringp contype)
1415 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1416 (downcase contype))))
1421 (defun mime-find-file-type (file)
1422 "Guess Content-Type, subtype, and parameters from FILE."
1424 (guesses mime-file-types))
1425 (while (and (not guess) guesses)
1426 (if (string-match (car (car guesses)) file)
1427 (setq guess (cdr (car guesses))))
1428 (setq guesses (cdr guesses)))
1431 (defun mime-prompt-for-type (&optional default)
1432 "Ask for Content-type."
1434 ;; Repeat until primary content type is specified.
1435 (while (string-equal type "")
1437 (completing-read "What content type: "
1440 'require-match ;Type must be specified.
1442 (if (string-equal type "")
1444 (message "Content type is required.")
1449 (defun mime-prompt-for-subtype (type &optional default)
1450 "Ask for subtype of media-type TYPE."
1451 (let ((subtypes (cdr (assoc type mime-content-types))))
1453 (assoc default subtypes))
1454 (setq default (car (car subtypes)))))
1459 "What content subtype: (default " default ") ")
1460 "What content subtype: ")
1461 (cdr (assoc type mime-content-types))
1463 'require-match ;Subtype must be specified.
1465 (if (string-equal answer "") default answer)))
1467 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1468 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1469 Optional DELIMITER specifies parameter delimiter (';' by default)."
1470 (let* ((delimiter (or delimiter "; "))
1475 (mime-prompt-for-parameters-1
1477 (cdr (assoc pritype mime-content-types))))))
1479 (if (and (stringp parameters)
1480 (not (string-equal parameters "")))
1481 (concat delimiter parameters)
1482 "" ;"" if no parameters
1485 (defun mime-prompt-for-parameters-1 (optlist)
1486 (apply (function append)
1487 (mapcar (function mime-prompt-for-parameter) optlist)))
1489 (defun mime-prompt-for-parameter (parameter)
1491 Parameter must be '(PROMPT CHOICE1 (CHOICE2...))."
1492 (let* ((prompt (car parameter))
1493 (choices (mapcar (function
1495 (if (consp e) e (list e))))
1497 (default (car (car choices)))
1503 (concat "What " prompt
1505 (if (string-equal default "") "\"\"" default)
1507 choices nil nil ""))
1508 ;; If nothing is selected, use default.
1509 (if (string-equal answer "")
1510 (setq answer default)))
1512 (read-string (concat "What " prompt ": "))))
1513 (cons (if (and answer
1514 (not (string-equal answer "")))
1516 ;; Note: control characters ignored!
1517 (if (string-match mime-tspecials-regexp answer)
1518 (concat "\"" answer "\"") answer)))
1519 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))))
1521 (defun mime-prompt-for-encoding (default)
1522 "Ask for Content-Transfer-Encoding."
1527 "What transfer encoding: "
1528 (mime-encoding-alist) nil t default))
1533 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1536 (defvar mime-edit-translate-buffer-hook
1537 '(mime-edit-pgp-enclose-buffer
1538 mime-edit-translate-body
1539 mime-edit-translate-header))
1541 (defun mime-edit-translate-header ()
1542 "Encode the message header into network representation."
1543 (mime-encode-header-in-buffer 'code-conversion)
1544 (run-hooks 'mime-edit-translate-header-hook))
1546 (defun mime-edit-translate-buffer ()
1547 "Encode the tagged MIME message in current buffer in MIME compliant message."
1550 (if (catch 'mime-edit-error
1552 (run-hooks 'mime-edit-translate-buffer-hook)))
1555 (error "Translation error!"))))
1557 (defun mime-edit-find-inmost ()
1558 (goto-char (point-min))
1559 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1560 (let ((bb (match-beginning 0))
1562 (type (buffer-substring (match-beginning 1)(match-end 1)))
1564 (setq end-exp (format "--}-<<%s>>\n" type))
1566 (if (re-search-forward end-exp nil t)
1567 (setq eb (match-beginning 0))
1568 (setq eb (point-max)))
1569 (narrow-to-region be eb)
1571 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1573 (narrow-to-region (match-beginning 0)(point-max))
1574 (mime-edit-find-inmost))
1576 (list type bb be eb)))))
1578 (defun mime-edit-process-multipart-1 (boundary)
1579 (let ((ret (mime-edit-find-inmost)))
1581 (let ((type (car ret))
1582 (bb (nth 1 ret))(be (nth 2 ret))
1584 (narrow-to-region bb eb)
1585 (delete-region bb be)
1586 (setq bb (point-min))
1587 (setq eb (point-max))
1590 (if (looking-at mime-edit-multipart-end-regexp)
1591 (let ((beg (match-beginning 0))
1592 (end (match-end 0)))
1593 (delete-region beg end)
1594 (or (looking-at mime-edit-beginning-tag-regexp)
1596 (insert (concat (mime-make-text-tag) "\n")))))
1597 (cond ((string-equal type "quote")
1598 (mime-edit-enquote-region bb eb))
1599 ((string-equal type "pgp-signed")
1600 (mime-edit-sign-pgp-mime bb eb boundary))
1601 ((string-equal type "pgp-encrypted")
1602 (mime-edit-encrypt-pgp-mime bb eb boundary))
1603 ((string-equal type "kazu-signed")
1604 (mime-edit-sign-pgp-kazu bb eb boundary))
1605 ((string-equal type "kazu-encrypted")
1606 (mime-edit-encrypt-pgp-kazu bb eb boundary))
1607 ((string-equal type "smime-signed")
1608 (mime-edit-sign-smime bb eb boundary))
1609 ((string-equal type "smime-encrypted")
1610 (mime-edit-encrypt-smime bb eb boundary))
1613 (nth 2 (mime-edit-translate-region bb eb
1617 (format "--[[multipart/%s;
1618 boundary=\"%s\"][7bit]]\n"
1622 (defun mime-edit-enquote-region (beg end)
1625 (narrow-to-region beg end)
1627 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1628 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1629 (replace-match (concat "- " (substring tag 1))))))))
1631 (defun mime-edit-dequote-region (beg end)
1634 (narrow-to-region beg end)
1636 (while (re-search-forward
1637 mime-edit-quoted-single-part-tag-regexp nil t)
1638 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1639 (replace-match (concat "-" (substring tag 2))))))))
1641 (defvar mime-edit-pgp-user-id nil)
1643 (defun mime-edit-sign-pgp-mime (beg end boundary)
1646 (let* ((from (std11-field-body "From" mail-header-separator))
1648 (narrow-to-region beg end)
1649 (mime-edit-translate-region beg end boundary)))
1651 (encoding (nth 1 ret))
1652 (pgp-boundary (concat "pgp-sign-" boundary))
1655 (insert (format "Content-Type: %s\n" ctype))
1657 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1659 (or (let ((pgg-default-user-id
1660 (or mime-edit-pgp-user-id
1662 (nth 1 (std11-extract-address-components from))
1663 pgg-default-user-id))))
1664 (pgg-sign-region (point-min)(point-max)))
1665 (throw 'mime-edit-error 'pgp-error))
1667 (cdr (assq 'hash-algorithm
1668 (cdar (with-current-buffer pgg-output-buffer
1669 (pgg-parse-armor-region
1670 (point-min)(point-max))))))
1673 (concat "; micalg=pgp-" (downcase (symbol-name micalg)))
1676 (insert (format "--[[multipart/signed;
1678 protocol=\"application/pgp-signature\"][7bit]]
1680 " pgp-boundary micalg pgp-boundary))
1681 (goto-char (point-max))
1682 (insert (format "\n--%s
1683 Content-Type: application/pgp-signature
1684 Content-Transfer-Encoding: 7bit
1687 (insert-buffer-substring pgg-output-buffer)
1688 (goto-char (point-max))
1689 (insert (format "\n--%s--\n" pgp-boundary))))))
1691 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1693 (defun mime-edit-make-encrypt-recipient-header ()
1694 (let* ((names mime-edit-encrypt-recipient-fields-list)
1696 (std11-field-bodies (cons "From" names)
1697 nil mail-header-separator))
1700 (setq values (cdr values))))
1701 (header (and (stringp from)
1702 (if (string-equal from "")
1704 (format "From: %s\n" from))))
1706 (while (and names values)
1707 (let ((name (car names))
1708 (value (car values)))
1709 (and (stringp value)
1710 (or (string-equal value "")
1712 (setq header (concat header name ": " value "\n")
1713 recipients (if recipients
1714 (concat recipients " ," value)
1716 (setq names (cdr names)
1717 values (cdr values)))
1718 (vector from recipients header)))
1720 (defun mime-edit-encrypt-pgp-mime (beg end boundary)
1723 (let (from recipients header)
1724 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1725 (setq from (aref ret 0)
1726 recipients (aref ret 1)
1727 header (aref ret 2)))
1728 (narrow-to-region beg end)
1730 (mime-edit-translate-region beg end boundary))
1732 (encoding (nth 1 ret))
1733 (pgp-boundary (concat "pgp-" boundary)))
1736 (insert (format "Content-Type: %s\n" ctype))
1738 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1740 (mime-encode-header-in-buffer)
1741 (or (let ((pgg-default-user-id
1742 (or mime-edit-pgp-user-id
1744 (nth 1 (std11-extract-address-components from))
1745 pgg-default-user-id))))
1747 (point-min) (point-max)
1748 (mapcar (lambda (recipient)
1749 (nth 1 (std11-extract-address-components
1751 (split-string recipients
1752 "\\([ \t\n]*,[ \t\n]*\\)+"))))
1753 (throw 'mime-edit-error 'pgp-error))
1754 (delete-region (point-min)(point-max))
1756 (insert (format "--[[multipart/encrypted;
1758 protocol=\"application/pgp-encrypted\"][7bit]]
1760 Content-Type: application/pgp-encrypted
1763 Content-Type: application/octet-stream
1764 Content-Transfer-Encoding: 7bit
1766 " pgp-boundary pgp-boundary pgp-boundary))
1767 (insert-buffer-substring pgg-output-buffer)
1768 (goto-char (point-max))
1769 (insert (format "\n--%s--\n" pgp-boundary)))))))
1771 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1774 (narrow-to-region beg end)
1776 (mime-edit-translate-region beg end boundary))
1778 (encoding (nth 1 ret)))
1780 (insert (format "Content-Type: %s\n" ctype))
1782 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1784 (or (pgg-sign-region beg (point-max) 'clearsign)
1785 (throw 'mime-edit-error 'pgp-error))
1788 "--[[application/pgp; format=mime][7bit]]\n")
1791 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1793 (let (recipients header)
1794 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1795 (setq recipients (aref ret 1)
1796 header (aref ret 2)))
1798 (narrow-to-region beg end)
1800 (mime-edit-translate-region beg end boundary))
1802 (encoding (nth 1 ret)))
1805 (insert (format "Content-Type: %s\n" ctype))
1807 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1809 (or (pgg-encrypt-region beg (point-max) recipients)
1810 (throw 'mime-edit-error 'pgp-error))
1813 "--[[application/pgp; format=mime][7bit]]\n")
1816 (defun mime-edit-sign-smime (beg end boundary)
1820 (narrow-to-region beg end)
1821 (mime-edit-translate-region beg end boundary)))
1823 (encoding (nth 1 ret))
1824 (smime-boundary (concat "smime-sign-" boundary)))
1826 (insert (format "Content-Type: %s\n" ctype))
1828 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1830 (let (buffer-undo-list)
1831 (goto-char (point-min))
1832 (while (progn (end-of-line) (not (eobp)))
1835 (or (smime-sign-buffer)
1836 (throw 'mime-edit-error 'pgp-error)))
1838 (if (re-search-forward "^Content-Type:\\s-*" nil t)
1839 (let* ((start (match-beginning 0))
1840 (body (buffer-substring (match-end 0) (std11-field-end))))
1841 (delete-region start (line-beginning-position 2))
1843 (insert "--[[" body "][7bit]]\n")))))))
1845 (defun mime-edit-encrypt-smime (beg end boundary)
1849 (narrow-to-region beg end)
1850 (mime-edit-translate-region beg end boundary)))
1852 (encoding (nth 1 ret)))
1854 (insert (format "Content-Type: %s\n" ctype))
1856 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1858 (goto-char (point-min))
1859 (while (progn (end-of-line) (not (eobp)))
1862 (or (smime-encrypt-buffer)
1863 (throw 'mime-edit-error 'pgp-error))
1865 (if (re-search-forward "^Content-Type:\\s-*" nil t)
1866 (let* ((start (match-beginning 0))
1867 (body (buffer-substring (match-end 0) (std11-field-end))))
1868 (delete-region start (line-beginning-position 2))
1870 (insert "--[[" body "]]\n")))))))
1872 (defsubst replace-space-with-underline (str)
1873 (mapconcat (function
1880 (defun mime-edit-make-boundary ()
1881 (concat mime-multipart-boundary "_"
1882 (replace-space-with-underline (current-time-string))))
1884 (defun mime-edit-translate-body ()
1885 "Encode the tagged MIME body in current buffer in MIME compliant message."
1888 (let ((boundary (mime-edit-make-boundary))
1891 (while (mime-edit-process-multipart-1
1892 (format "%s-%d" boundary i))
1895 ;; We are interested in message body.
1898 (goto-char (point-min))
1900 (concat "\n" (regexp-quote mail-header-separator)
1901 (if mime-ignore-preceding-spaces
1902 "[ \t\n]*\n" "\n")) nil 'move)
1906 (goto-char (point-max))
1907 (and mime-ignore-trailing-spaces
1908 (re-search-backward "[^ \t\n]\n" beg t)
1911 (setq ret (mime-edit-translate-region
1913 (format "%s-%d" boundary i)))))
1914 (mime-edit-dequote-region (point-min)(point-max))
1915 (let ((contype (car ret)) ;Content-Type
1916 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
1918 ;; Insert User-Agent field
1919 (and mime-edit-insert-user-agent-field
1920 (or (mail-position-on-field "User-Agent")
1921 (insert mime-edit-user-agent-value)))
1922 ;; Make primary MIME headers.
1923 (or (mail-position-on-field "MIME-Version")
1924 (insert mime-edit-mime-version-value))
1925 ;; Remove old Content-Type and other fields.
1927 (goto-char (point-min))
1928 (search-forward (concat "\n" mail-header-separator "\n") nil t)
1929 (narrow-to-region (point-min) (point))
1930 (goto-char (point-min))
1931 (mime-delete-field "Content-Type")
1932 (mime-delete-field "Content-Transfer-Encoding"))
1933 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
1934 (mail-position-on-field "Content-Type")
1938 (mail-position-on-field "Content-Transfer-Encoding")
1939 (insert encoding)))))))
1941 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
1942 "Translate single-part-tag to MIME header."
1943 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
1944 (let* ((beg (match-beginning 0))
1946 (tag (buffer-substring beg end)))
1947 (delete-region beg end)
1948 (let ((contype (mime-edit-get-contype tag))
1949 (encoding (mime-edit-get-encoding tag)))
1950 (insert (concat prefix "--" boundary "\n"))
1952 (narrow-to-region (point)(point))
1953 (insert "Content-Type: " contype "\n")
1955 (insert "Content-Transfer-Encoding: " encoding "\n"))
1956 (mime-encode-header-in-buffer))
1960 (downcase encoding)))))))
1962 (defun mime-edit-translate-region (beg end &optional boundary multipart)
1964 (setq boundary (mime-edit-make-boundary)))
1967 (narrow-to-region beg end)
1968 (let ((tag nil) ;MIME tag
1969 (contype nil) ;Content-Type
1970 (encoding nil) ;Content-Transfer-Encoding
1971 (nparts 0)) ;Number of body parts
1972 ;; Normalize the body part by inserting appropriate message
1973 ;; tags for every message contents.
1974 (mime-edit-normalize-body)
1975 ;; Counting the number of Content-Type.
1976 (goto-char (point-min))
1977 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1978 (setq nparts (1+ nparts)))
1979 ;; Begin translation.
1981 ((and (<= nparts 1)(not multipart))
1982 ;; It's a singular message.
1983 (goto-char (point-min))
1984 (while (re-search-forward
1985 mime-edit-single-part-tag-regexp nil t)
1987 (buffer-substring (match-beginning 0) (match-end 0)))
1988 (delete-region (match-beginning 0) (1+ (match-end 0)))
1989 (setq contype (mime-edit-get-contype tag))
1990 (setq encoding (mime-edit-get-encoding tag))))
1992 ;; It's a multipart message.
1993 (goto-char (point-min))
1994 (let ((prio mime-content-transfer-encoding-priority-list)
1996 (when (setq part-info
1997 (mime-edit-translate-single-part-tag boundary))
1998 (and (setq nprio (member (cdr part-info) prio))
2000 (while (setq part-info
2001 (mime-edit-translate-single-part-tag boundary "\n"))
2002 (and (setq nprio (member (cdr part-info) prio))
2003 (setq prio nprio))))
2004 ;; Define Content-Type as "multipart/mixed".
2006 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
2007 (setq encoding (car prio))
2008 ;; Insert the trailer.
2009 (goto-char (point-max))
2010 (insert "\n--" boundary "--\n"))))
2011 (list contype encoding boundary nparts)))))
2013 (defun mime-edit-normalize-body ()
2014 "Normalize the body part by inserting appropriate message tags."
2015 ;; Insert the first MIME tags if necessary.
2016 (goto-char (point-min))
2017 (if (not (looking-at mime-edit-single-part-tag-regexp))
2018 (insert (mime-make-text-tag) "\n"))
2019 ;; Check each tag, and add new tag or correct it if necessary.
2020 (goto-char (point-min))
2021 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2022 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
2023 (contype (mime-edit-get-contype tag))
2024 (charset (mime-get-parameter contype "charset"))
2025 (encoding (mime-edit-get-encoding tag)))
2026 ;; Remove extra whitespaces after the tag.
2027 (if (looking-at "[ \t]+$")
2028 (delete-region (match-beginning 0) (match-end 0)))
2030 (end (mime-edit-content-end)))
2031 (if (= end (point-max))
2034 (or (looking-at mime-edit-beginning-tag-regexp)
2036 (insert (mime-make-text-tag) "\n")))
2037 (remove-text-properties beg end '(invisible mime-edit-invisible))
2040 ((mime-test-content-type contype "message")
2041 ;; Content-type "message" should be sent as is.
2043 ((mime-test-content-type contype "text")
2044 ;; Define charset for text if necessary.
2045 (setq charset (if charset
2046 (intern (downcase charset))
2047 (mime-edit-choose-charset)))
2048 (mime-edit-define-charset charset)
2049 (cond ((string-equal contype "text/x-rot13-47-48")
2052 (mule-caesar-region (point) (mime-edit-content-end))))
2053 ((string-equal contype "text/enriched")
2058 (end (mime-edit-content-end)))
2059 ;; Patch for hard newlines
2062 ;; (while (search-forward "\n" end t)
2063 ;; (put-text-property (match-beginning 0)
2066 ;; End patch for hard newlines
2067 (enriched-encode beg end nil)
2069 (if (search-forward "\n\n")
2070 (delete-region beg (match-end 0)))))))
2071 ;; Point is now on current tag.
2072 ;; Define encoding and encode text if necessary.
2073 (or encoding ;Encoding is not specified.
2076 (let ((ret (cdr (assq charset mime-charset-type-list))))
2078 (setq bits (car ret)
2081 conv "quoted-printable")))
2082 (if (<= bits mime-transfer-level)
2083 (mime-encoding-name bits)
2085 (beg (mime-edit-content-beginning)))
2086 (encode-mime-charset-region beg (mime-edit-content-end)
2088 ;; Protect "From " in beginning of line
2090 (narrow-to-region beg (mime-edit-content-end))
2092 (let (case-fold-search)
2093 (if (re-search-forward "^From " nil t)
2095 (if (memq charset '(iso-2022-jp
2100 (replace-match "\e(BFrom ")
2101 (re-search-forward "^From " nil t)))
2102 (setq encoding "quoted-printable"))))))
2103 ;; canonicalize line break code
2104 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2106 (narrow-to-region beg (mime-edit-content-end))
2108 (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2109 (replace-match "\\1\r\n"))))
2111 (mime-encode-region beg (mime-edit-content-end)
2112 (or encoding "7bit"))
2113 (mime-edit-define-encoding encoding)))
2114 (goto-char (mime-edit-content-end)))
2115 ((null encoding) ;Encoding is not specified.
2116 ;; Application, image, audio, video, and any other
2117 ;; unknown content-type without encoding should be
2119 (let* ((encoding "base64") ;Encode in BASE64 by default.
2120 (beg (mime-edit-content-beginning))
2121 (end (mime-edit-content-end)))
2122 (mime-encode-region beg end encoding)
2123 (mime-edit-define-encoding encoding))
2124 (forward-line 1))))))
2126 (defun mime-delete-field (field)
2127 "Delete header FIELD."
2128 (let ((regexp (format "^%s:[ \t]*" field)))
2129 (goto-char (point-min))
2130 (while (re-search-forward regexp nil t)
2131 (delete-region (match-beginning 0)
2132 (1+ (std11-field-end))))))
2136 ;;; Platform dependent functions
2139 ;; Sun implementations
2141 (defun mime-edit-voice-recorder-for-sun (encoding)
2142 "Record voice in a buffer using Sun audio device,
2143 and insert data encoded as ENCODING."
2144 (message "Start the recording on %s. Type C-g to finish the recording..."
2146 (mime-insert-encoded-file "/dev/audio" encoding))
2149 ;;; @ Other useful commands.
2152 ;; Message forwarding commands as content-type "message/rfc822".
2154 (defun mime-edit-insert-message (&optional message)
2156 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2157 (if (and inserter (fboundp inserter))
2159 (mime-edit-insert-tag "message" "rfc822")
2160 (funcall inserter message))
2161 (message "Sorry, I don't have message inserter for your MUA."))))
2163 (defun mime-edit-insert-mail (&optional message)
2165 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2166 (if (and inserter (fboundp inserter))
2168 (mime-edit-insert-tag "message" "rfc822")
2169 (funcall inserter message))
2170 (message "Sorry, I don't have mail inserter for your MUA."))))
2172 (defun mime-edit-inserted-message-filter ()
2175 (let ((header-start (point))
2176 (case-fold-search t)
2179 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2180 (if (re-search-forward "^$" (mark t))
2181 (narrow-to-region header-start (match-beginning 0)))
2182 (goto-char header-start)
2183 (while (and (re-search-forward
2184 mime-edit-yank-ignored-field-regexp nil t)
2185 (setq beg (match-beginning 0))
2186 (setq end (1+ (std11-field-end))))
2187 (delete-region beg end))))))
2190 ;;; @ multipart enclosure
2193 (defun mime-edit-enclose-region-internal (type beg end)
2197 (narrow-to-region beg end)
2198 (insert (format "--<<%s>>-{\n" type))
2199 (goto-char (point-max))
2200 (insert (format "--}-<<%s>>\n" type))
2201 (goto-char (point-max)))
2202 (or (looking-at mime-edit-beginning-tag-regexp)
2204 (insert (mime-make-text-tag) "\n"))))
2206 (defun mime-edit-enclose-quote-region (beg end)
2208 (mime-edit-enclose-region-internal 'quote beg end))
2210 (defun mime-edit-enclose-mixed-region (beg end)
2212 (mime-edit-enclose-region-internal 'mixed beg end))
2214 (defun mime-edit-enclose-parallel-region (beg end)
2216 (mime-edit-enclose-region-internal 'parallel beg end))
2218 (defun mime-edit-enclose-digest-region (beg end)
2220 (mime-edit-enclose-region-internal 'digest beg end))
2222 (defun mime-edit-enclose-alternative-region (beg end)
2224 (mime-edit-enclose-region-internal 'alternative beg end))
2226 (defun mime-edit-enclose-pgp-signed-region (beg end)
2228 (mime-edit-enclose-region-internal 'pgp-signed beg end))
2230 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2232 (mime-edit-enclose-region-internal 'pgp-encrypted beg end))
2234 (defun mime-edit-enclose-kazu-signed-region (beg end)
2236 (mime-edit-enclose-region-internal 'kazu-signed beg end))
2238 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2240 (mime-edit-enclose-region-internal 'kazu-encrypted beg end))
2242 (defun mime-edit-enclose-smime-signed-region (beg end)
2244 (mime-edit-enclose-region-internal 'smime-signed beg end))
2246 (defun mime-edit-enclose-smime-encrypted-region (beg end)
2248 (mime-edit-enclose-region-internal 'smime-encrypted beg end))
2250 (defun mime-edit-insert-key (&optional arg)
2251 "Insert a pgp public key."
2253 (mime-edit-insert-tag "application" "pgp-keys")
2254 (mime-edit-define-encoding "7bit")
2256 (if (and (not (eobp))
2257 (not (looking-at mime-edit-single-part-tag-regexp)))
2258 (insert (mime-make-text-tag) "\n")))
2264 (defun mime-edit-set-split (arg)
2267 (y-or-n-p "Do you want to enable split? ")))
2268 (setq mime-edit-split-message arg)
2270 (message "This message is enabled to split.")
2271 (message "This message is not enabled to split.")))
2273 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2274 "Toggle transfer-level is 7bit or 8bit through.
2276 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2278 (if (numberp transfer-level)
2279 (setq mime-transfer-level transfer-level)
2280 (if (< mime-transfer-level 8)
2281 (setq mime-transfer-level 8)
2282 (setq mime-transfer-level 7)))
2283 (message (format "Current transfer-level is %d bit"
2284 mime-transfer-level))
2285 (setq mime-transfer-level-string
2286 (mime-encoding-name mime-transfer-level 'not-omit))
2287 (force-mode-line-update))
2289 (defun mime-edit-set-transfer-level-7bit ()
2291 (mime-edit-toggle-transfer-level 7))
2293 (defun mime-edit-set-transfer-level-8bit ()
2295 (mime-edit-toggle-transfer-level 8))
2301 (defvar mime-edit-pgp-processing nil)
2302 (make-variable-buffer-local 'mime-edit-pgp-processing)
2304 (defun mime-edit-set-sign (arg)
2307 (y-or-n-p "Do you want to sign? ")))
2310 (or (memq 'sign mime-edit-pgp-processing)
2311 (setq mime-edit-pgp-processing
2312 (nconc mime-edit-pgp-processing
2313 (copy-sequence '(sign)))))
2314 (message "This message will be signed."))
2315 (setq mime-edit-pgp-processing
2316 (delq 'sign mime-edit-pgp-processing))
2317 (message "This message will not be signed.")))
2319 (defun mime-edit-set-encrypt (arg)
2322 (y-or-n-p "Do you want to encrypt? ")))
2325 (or (memq 'encrypt mime-edit-pgp-processing)
2326 (setq mime-edit-pgp-processing
2327 (nconc mime-edit-pgp-processing
2328 (copy-sequence '(encrypt)))))
2329 (message "This message will be encrypt."))
2330 (setq mime-edit-pgp-processing
2331 (delq 'encrypt mime-edit-pgp-processing))
2332 (message "This message will not be encrypt.")))
2334 (defun mime-edit-pgp-enclose-buffer ()
2335 (let ((beg (save-excursion
2336 (goto-char (point-min))
2337 (if (search-forward (concat "\n" mail-header-separator "\n"))
2340 (dolist (pgp-processing mime-edit-pgp-processing)
2341 (case pgp-processing
2343 (mime-edit-enclose-pgp-signed-region
2346 (mime-edit-enclose-pgp-encrypted-region
2347 beg (point-max))))))))
2353 (defun mime-edit-insert-partial-header (fields subject
2354 id number total separator)
2356 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2357 (insert mime-edit-mime-version-field-for-message/partial)
2359 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2360 id number total separator)))
2362 (defun mime-edit-split-and-send
2363 (&optional cmd lines mime-edit-message-max-length)
2367 (count-lines (point-min) (point-max))))
2368 (or mime-edit-message-max-length
2369 (setq mime-edit-message-max-length
2370 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2371 mime-edit-message-default-max-lines)))
2372 (let* ((mime-edit-draft-file-name
2373 (or (buffer-file-name)
2375 (expand-file-name "mime-draft" temporary-file-directory))))
2376 (separator mail-header-separator)
2378 (replace-space-with-underline (current-time-string))
2379 "@" (system-name) "\"")))
2380 (run-hooks 'mime-edit-before-split-hook)
2381 (let ((the-buf (current-buffer))
2382 (copy-buf (get-buffer-create " *Original Message*"))
2383 (header (std11-header-string-except
2384 mime-edit-split-ignored-field-regexp separator))
2385 (subject (mail-fetch-field "subject"))
2386 (total (+ (/ lines mime-edit-message-max-length)
2387 (if (> (mod lines mime-edit-message-max-length) 0)
2393 mime-edit-split-message-sender-alist))
2397 (error "Split sender is not specified for `%s'." major-mode)))))
2398 (mime-edit-partial-number 1)
2401 (set-buffer copy-buf)
2403 (insert-buffer the-buf)
2405 (if (re-search-forward
2406 (concat "^" (regexp-quote separator) "$") nil t)
2407 (let ((he (match-beginning 0)))
2409 (narrow-to-region (point-min) he)))
2410 (goto-char (point-min))
2411 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2412 (delete-region (match-beginning 0)
2413 (1+ (std11-field-end))))))
2414 (while (< mime-edit-partial-number total)
2417 (set-buffer copy-buf)
2418 (setq data (buffer-substring
2421 (goto-line mime-edit-message-max-length)
2423 (delete-region (point-min)(point)))
2424 (mime-edit-insert-partial-header
2425 header subject id mime-edit-partial-number total separator)
2428 (message (format "Sending %d/%d..."
2429 mime-edit-partial-number total))
2430 (call-interactively command)
2431 (message (format "Sending %d/%d...done"
2432 mime-edit-partial-number total)))
2433 (setq mime-edit-partial-number
2434 (1+ mime-edit-partial-number)))
2437 (set-buffer copy-buf)
2438 (setq data (buffer-string))
2440 (mime-edit-insert-partial-header
2441 header subject id mime-edit-partial-number total separator)
2444 (message (format "Sending %d/%d..."
2445 mime-edit-partial-number total))
2446 (message (format "Sending %d/%d...done"
2447 mime-edit-partial-number total))))))
2449 (defun mime-edit-maybe-split-and-send (&optional cmd)
2451 (run-hooks 'mime-edit-before-send-hook)
2452 (let ((mime-edit-message-max-length
2453 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2454 mime-edit-message-default-max-lines))
2455 (lines (count-lines (point-min) (point-max))))
2456 (if (and (> lines mime-edit-message-max-length)
2457 mime-edit-split-message)
2458 (mime-edit-split-and-send cmd lines mime-edit-message-max-length))))
2461 ;;; @ preview message
2464 (defvar mime-edit-buffer nil) ; buffer local variable
2466 (defun mime-edit-preview-message ()
2467 "preview editing MIME message."
2469 (let* ((str (buffer-string))
2470 (separator mail-header-separator)
2471 (the-buf (current-buffer))
2472 (buf-name (buffer-name))
2473 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2474 (buf (get-buffer temp-buf-name))
2475 (pgp-processing mime-edit-pgp-processing))
2478 (switch-to-buffer buf)
2480 (setq buf (get-buffer-create temp-buf-name))
2481 (switch-to-buffer buf))
2483 (setq major-mode 'mime-temp-message-mode)
2484 (make-local-variable 'mail-header-separator)
2485 (setq mail-header-separator separator)
2486 (make-local-variable 'mime-edit-buffer)
2487 (setq mime-edit-buffer the-buf)
2488 (setq mime-edit-pgp-processing pgp-processing)
2490 (run-hooks 'mime-edit-translate-hook)
2491 (mime-edit-translate-buffer)
2492 (goto-char (point-min))
2493 (if (re-search-forward
2494 (concat "^" (regexp-quote separator) "$"))
2497 (make-local-variable 'mime-edit-temp-message-buffer)
2498 (setq mime-edit-temp-message-buffer buf)))
2500 (defun mime-edit-quitting-method ()
2501 "Quitting method for mime-view."
2502 (let* ((temp mime-edit-temp-message-buffer)
2504 (mime-preview-kill-buffer)
2506 (setq buf mime-edit-buffer)
2508 (switch-to-buffer buf)))
2510 (set-alist 'mime-preview-quitting-method-alist
2511 'mime-temp-message-mode
2512 #'mime-edit-quitting-method)
2518 (defvar mime-edit-again-ignored-field-regexp
2519 (concat "^\\(" "Content-.*\\|Mime-Version"
2520 (if mime-edit-insert-user-agent-field "\\|User-Agent")
2522 "Regexp for deleted header fields when `mime-edit-again' is called.")
2524 (defsubst eliminate-top-spaces (string)
2525 "Eliminate top sequence of space or tab in STRING."
2526 (if (string-match "^[ \t]+" string)
2527 (substring string (match-end 0))
2530 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
2533 (cdr (assoc (mime-content-type-parameter content-type "protocol")
2534 '(("application/pgp-encrypted" . pgp-encrypted)
2535 ("application/pgp-signature" . pgp-signed))))
2536 (mime-content-type-subtype content-type)))
2537 (boundary (mime-content-type-parameter content-type "boundary"))
2538 (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
2539 (re-search-forward boundary-pat nil t)
2540 (let ((bb (match-beginning 0)) eb tag)
2541 (setq tag (format "\n--<<%s>>-{\n" subtype))
2544 (setq bb (+ bb (length tag)))
2546 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2548 (setq eb (match-beginning 0))
2549 (replace-match (format "--}-<<%s>>\n" subtype))
2551 (narrow-to-region bb eb)
2552 (goto-char (point-min))
2553 (while (re-search-forward boundary-pat nil t)
2554 (let ((beg (match-beginning 0))
2556 (delete-region beg (match-end 0))
2558 (if (re-search-forward boundary-pat nil t)
2559 (setq end (match-beginning 0))
2560 (setq end (point-max)))
2562 (narrow-to-region beg end)
2564 ((eq subtype 'pgp-encrypted)
2567 (goto-char (point-min))
2568 (re-search-forward "^-+BEGIN PGP MESSAGE-+$"
2571 (save-window-excursion
2572 (pgg-decrypt-region (match-beginning 0)
2574 (delete-region (point-min)(point-max))))
2575 (insert-buffer-substring pgg-output-buffer)
2576 (mime-edit-decode-message-in-buffer
2577 nil not-decode-text)
2578 (delete-region (goto-char (point-min))
2579 (if (search-forward "\n\n" nil t)
2582 (goto-char (point-max))))
2584 (mime-edit-decode-message-in-buffer
2585 (if (eq subtype 'digest)
2587 (make-mime-content-type 'message 'rfc822)))
2589 (goto-char (point-max))))))))))
2590 (goto-char (point-min))
2591 (or (= (point-min) 1)
2592 (delete-region (point-min)
2593 (if (search-forward "\n\n" nil t)
2597 (defun mime-edit-decode-single-part-in-buffer
2598 (content-type not-decode-text &optional content-disposition)
2599 (let* ((type (mime-content-type-primary-type content-type))
2600 (subtype (mime-content-type-subtype content-type))
2601 (ctype (format "%s/%s" type subtype))
2603 (pstr (let ((bytes (+ 14 (length ctype))))
2604 (mapconcat (function
2606 (if (string= (car attr) "charset")
2608 (setq charset (cdr attr))
2610 (let* ((str (concat (car attr)
2613 (setq bytes (+ bytes bs 2))
2616 (setq bytes (+ bs 1))
2619 (mime-content-type-parameters content-type) "")))
2622 (limit (save-excursion
2623 (if (search-forward "\n\n" nil t)
2626 (mime-content-disposition-type content-disposition))
2628 (if disposition-type
2629 (let ((bytes (+ 21 (length (format "%s" disposition-type)))))
2630 (mapconcat (function
2635 (if (string-equal "filename"
2637 (std11-wrap-as-quoted-string
2641 (setq bytes (+ bytes bs 2))
2644 (setq bytes (+ bs 1))
2647 (mime-content-disposition-parameters
2648 content-disposition)
2650 (if disposition-type
2651 (setq pstr (format "%s\nContent-Disposition: %s%s"
2652 pstr disposition-type disposition-str)))
2654 (if (re-search-forward
2655 "^Content-Transfer-Encoding:" limit t)
2656 (let ((beg (match-beginning 0))
2657 (hbeg (match-end 0))
2658 (end (std11-field-end limit)))
2661 (eliminate-top-spaces
2662 (std11-unfold-string
2663 (buffer-substring hbeg end)))))
2664 (if (or charset (eq type 'text))
2666 (delete-region beg (1+ end))
2667 (goto-char (point-min))
2668 (if (search-forward "\n\n" nil t)
2671 (match-end 0)(point-max) encoding)
2673 encoding nil))))))))
2674 (if (and (eq type 'text)
2675 (or encoded (not not-decode-text)))
2678 (goto-char (point-min))
2679 (while (re-search-forward "\r\n" nil t)
2680 (replace-match "\n")))
2681 (decode-mime-charset-region (point-min)(point-max)
2682 (or charset default-mime-charset))))
2683 (let ((he (if (re-search-forward "^$" nil t)
2686 (if (and (eq type 'text)
2687 (eq subtype 'x-rot13-47-48))
2688 (mule-caesar-region he (point-max)))
2689 (if (= (point-min) 1)
2695 (format "%s/%s%s" type subtype pstr)
2697 (delete-region (point-min) he)
2699 (mime-create-tag (format "%s/%s%s" type subtype pstr)
2703 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
2706 (goto-char (point-min))
2707 (let ((ctl (or (mime-read-Content-Type)
2708 default-content-type)))
2710 (let ((type (mime-content-type-primary-type ctl)))
2712 ((and (eq type 'application)
2713 (eq (mime-content-type-subtype ctl) 'pgp-signature))
2714 (delete-region (point-min)(point-max)))
2715 ((eq type 'multipart)
2716 (mime-edit-decode-multipart-in-buffer ctl not-decode-text))
2718 (mime-edit-decode-single-part-in-buffer
2719 ctl not-decode-text (mime-read-Content-Disposition)))))
2721 (decode-mime-charset-region (point-min) (point-max)
2722 default-mime-charset)))
2723 (if (= (point-min) 1)
2726 (std11-narrow-to-header)
2727 (goto-char (point-min))
2728 (while (re-search-forward
2729 mime-edit-again-ignored-field-regexp nil t)
2730 (delete-region (match-beginning 0) (1+ (std11-field-end)))))
2731 (mime-decode-header-in-buffer (not not-decode-text)))))))
2734 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
2735 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
2736 Content-Type and Content-Transfer-Encoding header fields will be
2737 converted to MIME-Edit tags."
2739 (goto-char (point-min))
2741 (concat "\n" (regexp-quote mail-header-separator) "\n")
2743 (replace-match "\n\n"))
2744 (mime-edit-decode-message-in-buffer nil not-decode-text)
2745 (goto-char (point-min))
2747 (and (re-search-forward "^$")
2748 (replace-match mail-header-separator)))
2750 (turn-on-mime-edit)))
2756 (provide 'mime-edit)
2758 (run-hooks 'mime-edit-load-hook)
2760 ;;; mime-edit.el ends here