1 ;;; mime-edit.el --- Simple MIME Composer for GNU Emacs
3 ;; Copyright (C) 1993,94,95,96,97,98,99 Free Software Foundation, Inc.
5 ;; Author: UMEDA Masanobu <umerin@mse.kyutech.ac.jp>
6 ;; MORIOKA Tomohiko <morioka@jaist.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...
110 (eval-when-compile (require 'static))
113 (require 'mail-utils)
115 (require 'eword-encode) ; eword-encode-field-body
123 (autoload 'pgg-encrypt-region "pgg"
124 "PGP encryption of current region." t)
125 (autoload 'pgg-sign-region "pgg"
126 "PGP signature of current region." t)
127 (autoload 'pgg-insert-key "pgg"
128 "Insert PGP public key at point." t)
129 (autoload 'smime-encrypt-region "smime"
130 "S/MIME encryption of current region.")
131 (autoload 'smime-sign-region "smime"
132 "S/MIME signature of current region.")
133 (defvar smime-output-buffer)
134 (defvar smime-errors-buffer)
141 (defconst mime-edit-version
143 (mime-product-name mime-user-interface-product) " "
144 (mapconcat #'number-to-string
145 (mime-product-version mime-user-interface-product) ".")
146 " - \"" (mime-product-code-name mime-user-interface-product) "\"")))
152 (defgroup mime-edit nil
156 (defcustom mime-ignore-preceding-spaces nil
157 "*Ignore preceding white spaces if non-nil."
161 (defcustom mime-ignore-trailing-spaces nil
162 "*Ignore trailing white spaces if non-nil."
166 (defcustom mime-ignore-same-text-tag t
167 "*Ignore preceding text content-type tag that is same with new one.
168 If non-nil, the text tag is not inserted unless something different."
172 (defcustom mime-auto-hide-body t
173 "*Hide non-textual body encoded in base64 after insertion if non-nil."
177 (defcustom mime-edit-voice-recorder
178 (function mime-edit-voice-recorder-for-sun)
179 "*Function to record a voice message and encode it."
183 (defcustom mime-edit-mode-hook nil
184 "*Hook called when enter MIME mode."
188 (defcustom mime-edit-translate-hook nil
189 "*Hook called before translating into a MIME compliant message.
190 To insert a signature file automatically, call the function
191 `mime-edit-insert-signature' from this hook."
195 (defcustom mime-edit-exit-hook nil
196 "*Hook called when exit MIME mode."
200 (defvar mime-content-types
202 ;; Charset parameter need not to be specified, since it is
203 ;; defined automatically while translation.
205 ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
218 ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
219 ("directory" "/pub/GNU/elisp/mime")
221 ("mode" "image" "ascii" "local8"))
226 ("mode" "image" "ascii" "local8"))
227 ("tftp" ("site") ("name"))
228 ("afs" ("site") ("name"))
229 ("local-file" ("site") ("name"))
231 ("server" "ftpmail@nic.karrn.ad.jp")
237 ("octet-stream" ("type" "" "tar" "shar"))
239 ("vnd.ms-powerpoint")
240 ("x-kiss" ("x-cnf")))
252 "*Alist of content-type, subtype, parameters and its values.")
254 (defcustom mime-file-types
257 ;; Programming languages
260 "application" "octet-stream" (("type" . "C++"))
262 "attachment" (("filename" . file)))
265 "application" "octet-stream" (("type" . "emacs-lisp"))
267 "attachment" (("filename" . file)))
270 "application" "octet-stream" (("type" . "common-lisp"))
272 "attachment" (("filename" . file)))
275 "application" "octet-stream" (("type" . "perl"))
277 "attachment" (("filename" . file)))
279 ;; Text or translated text
284 "inline" (("filename" . file)))
286 ;; .rc : procmail modules pm-xxxx.rc
287 ;; *rc : other resource files
289 ("\\.\\(rc\\|lst\\|log\\|sql\\|mak\\)$\\|\\..*rc$"
292 "attachment" (("filename" . file)))
299 ("\\.diff$\\|\\.patch$"
300 "text" "plain" (("type" . "patch"))
302 "inline" (("filename" . file)))
305 "text" "plain" nil nil nil nil)
308 ;; Octect binary text
311 "application" "msword" nil
313 "attachment" (("filename" . file)))
314 ("\\.ppt$" ; MS Power Point
315 "application" "vnd.ms-powerpoint" nil
317 "attachment" (("filename" . file)))
322 "inline" (("filename" . file)))
324 "application" "postscript" nil
326 "attachment" (("filename" . file)))
333 "inline" (("filename" . file)))
337 "inline" (("filename" . file)))
341 "inline" (("filename" . file)))
345 "inline" (("filename" . file)))
349 "inline" (("filename" . file)))
353 "inline" (("filename" . file)))
357 "inline" (("filename" . file)))
361 "inline" (("filename" . file)))
365 "attachment" (("filename" . file)))
369 "attachment" (("filename" . file)))
371 "application" "octet-stream" (("type" . "tar+gzip"))
373 "attachment" (("filename" . file)))
375 "application" "octet-stream" (("type" . "tar+gzip"))
377 "attachment" (("filename" . file)))
379 "application" "octet-stream" (("type" . "tar+compress"))
381 "attachment" (("filename" . file)))
383 "application" "octet-stream" (("type" . "tar+compress"))
385 "attachment" (("filename" . file)))
387 "application" "octet-stream" (("type" . "gzip"))
389 "attachment" (("filename" . file)))
391 "application" "octet-stream" (("type" . "compress"))
393 "attachment" (("filename" . file)))
395 "application" "octet-stream" (("type" . "lha"))
397 "attachment" (("filename" . file)))
399 "application" "zip" nil
401 "attachment" (("filename" . file)))
406 "application" "octet-stream" nil
408 "attachment" (("filename" . file))))
409 "*Alist of file name, types, parameters, and default encoding.
410 If encoding is nil, it is determined from its contents."
414 (choice :tag "Primary-Type"
415 ,@(nconc (mapcar (lambda (cell)
416 (list 'item (car cell)))
420 (choice :tag "Sub-Type"
423 (mapcar (lambda (cell)
424 (mapcar (lambda (cell)
425 (list 'item (car cell)))
430 (repeat :tag "Parameters of Content-Type field"
431 (cons string (choice string symbol)))
432 ;; content-transfer-encoding
433 (choice :tag "Encoding"
436 (mapcar (lambda (cell)
438 (mime-encoding-list))))
440 (choice :tag "Disposition-Type"
446 (repeat :tag "Parameters of Content-Disposition field"
447 (cons string (choice string symbol)))))
450 (defvar mime-content-disposition-types
451 '(("attachment") ("inline")))
453 (defcustom mime-edit-attach-at-end-type nil
454 "*List of MIME types to be attached at the end of a message.
455 Values must be strings indicates MIME types. You can specify
456 either type/subtype or type only."
458 :type '(choice (const :tag "Nothing" nil)
459 (list (repeat symbol))))
461 ;;; @@ about charset, encoding and transfer-level
464 (defvar mime-charset-type-list
466 (iso-8859-1 8 "quoted-printable")
467 (iso-8859-2 8 "quoted-printable")
468 (iso-8859-3 8 "quoted-printable")
469 (iso-8859-4 8 "quoted-printable")
470 (iso-8859-5 8 "quoted-printable")
471 (koi8-r 8 "quoted-printable")
472 (iso-8859-7 8 "quoted-printable")
473 (iso-8859-8 8 "quoted-printable")
474 (iso-8859-9 8 "quoted-printable")
475 (iso-2022-jp 7 "base64")
476 (iso-2022-kr 7 "base64")
482 (shift_jis 8 "base64")
484 (iso-2022-jp-2 7 "base64")
485 (iso-2022-int-1 7 "base64")))
487 (defcustom mime-transfer-level 7
488 "*A number of network transfer level. It should be bigger than 7.
489 Currently it has no effect except mode-line display."
491 :type '(choice (const 7)
493 (const :tag "Binary" 9)))
494 (make-variable-buffer-local 'mime-transfer-level)
496 (defsubst mime-encoding-name (transfer-level &optional not-omit)
497 (cond ((> transfer-level 8) "binary")
498 ((= transfer-level 8) "8bit")
501 (defvar mime-transfer-level-string
502 (mime-encoding-name mime-transfer-level 'not-omit)
503 "A string formatted version of `mime-transfer-level'.")
504 (make-variable-buffer-local 'mime-transfer-level-string)
506 ;;; @@ about content transfer encoding
508 (defvar mime-content-transfer-encoding-priority-list
509 '(nil "8bit" "binary"))
511 ;;; @@ about message inserting
514 (defcustom mime-edit-yank-ignored-field-list
515 '("Received" "Approved" "Path" "Replied" "Status"
516 "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
517 "List of ignored header fields when inserting message/rfc822.
518 Each elements are regexp of field-name."
520 :type '(repeat regexp))
522 (defvar mime-edit-message-inserter-alist nil)
523 (defvar mime-edit-mail-inserter-alist nil)
526 ;;; @@ about message splitting
529 (defcustom mime-edit-split-message t
530 "*Split large message if it is non-nil."
534 (defcustom mime-edit-message-default-max-lines 5000
535 "*Default maximum lines of a message."
539 (defcustom mime-edit-message-max-lines-alist
540 '((news-reply-mode . 500))
541 "Alist of `major-mode' vs maximum lines of a message.
542 If it is not specified for a `major-mode',
543 `mime-edit-message-default-max-lines' is used."
547 (defconst mime-edit-split-ignored-field-regexp
548 "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|^Message-Id:\\)")
550 (defcustom mime-edit-split-blind-field-regexp
551 "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
552 "*Regular expression to match field-name to be ignored when split sending."
556 (defvar mime-edit-split-message-sender-alist nil)
558 (defvar mime-edit-news-reply-mode-server-running nil)
564 (defconst mime-edit-single-part-tag-regexp
565 "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
566 "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
568 (defconst mime-edit-quoted-single-part-tag-regexp
569 (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
571 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
573 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
575 (defconst mime-edit-beginning-tag-regexp
576 (regexp-or mime-edit-single-part-tag-regexp
577 mime-edit-multipart-beginning-regexp))
579 (defconst mime-edit-end-tag-regexp
580 (regexp-or mime-edit-single-part-tag-regexp
581 mime-edit-multipart-end-regexp))
583 (defconst mime-edit-tag-regexp
584 (regexp-or mime-edit-single-part-tag-regexp
585 mime-edit-multipart-beginning-regexp
586 mime-edit-multipart-end-regexp))
588 (defconst mime-tag-format "--[[%s]]"
589 "Control-string making a MIME tag.")
591 (defconst mime-tag-format-with-encoding "--[[%s][%s]]"
592 "Control-string making a MIME tag with encoding.")
595 ;;; @@ multipart boundary
598 (defvar mime-multipart-boundary "Multipart"
599 "*Boundary of a multipart message.")
602 ;;; @@ optional header fields
605 (defcustom mime-edit-insert-user-agent-field t
606 "*If non-nil, insert User-Agent header field."
610 (defvar mime-edit-user-agent-value
611 (concat (mime-product-name mime-user-interface-product)
613 (mapconcat #'number-to-string
614 (mime-product-version mime-user-interface-product) ".")
616 (mime-product-code-name mime-user-interface-product)
618 (mime-product-name mime-library-product)
620 (mapconcat #'number-to-string
621 (mime-product-version mime-library-product) ".")
623 (mime-product-code-name mime-library-product)
625 (if (fboundp 'apel-version)
626 (concat (apel-version) " "))
627 (if (featurep 'xemacs)
628 (concat (cond ((featurep 'utf-2000)
629 (concat "UTF-2000-MULE/" utf-2000-version))
630 ((featurep 'mule) "MULE"))
632 (if (string-match "^[0-9]+\\(\\.[0-9]+\\)" emacs-version)
635 (substring emacs-version 0 (match-end 0))
636 (cond ((and (boundp 'xemacs-betaname)
638 ;; It does not exist in XEmacs
639 ;; versions prior to 20.3.
640 (concat " " xemacs-betaname))
641 ((and (boundp 'emacs-patch-level)
643 ;; It does not exist in FSF Emacs or in
644 ;; XEmacs versions earlier than 21.1.1.
645 (format " (patch %d)" emacs-patch-level))
647 " (" xemacs-codename ") ("
648 system-configuration ")")
649 " (" emacs-version ")"))
650 (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
651 (substring emacs-version 0 (match-beginning 0))
654 (if (boundp 'enable-multibyte-characters)
656 " (" system-configuration ")"
657 (if enable-multibyte-characters
658 (concat " MULE/" mule-version)
659 " (with unibyte mode)")
660 (if (featurep 'meadow)
661 (let ((mver (Meadow-version)))
662 (if (string-match "^Meadow-" mver)
666 (concat "MULE/" mule-version
667 " (based on Emacs " ver ")"))
668 (concat "Emacs/" ver " (" system-configuration ")")))))
669 "Body of User-Agent field.
670 If variable `mime-edit-insert-user-agent-field' is not nil, it is
671 inserted into message header.")
677 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
678 "Specify MIME tspecials.
679 Tspecials means any character that matches with it in header must be quoted.")
681 (defconst mime-edit-mime-version-value
682 ; (concat "1.0 (generated by " mime-edit-version ")")
684 "MIME version number.")
686 ;;; @ keymap and menu
689 (defvar mime-edit-mode-flag nil)
690 (make-variable-buffer-local 'mime-edit-mode-flag)
692 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
693 "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
694 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
695 "Keymap for MIME-Edit mode commands to insert entity or set status.")
697 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
698 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
699 (define-key mime-edit-mode-entity-map "i" 'mime-edit-insert-text-file)
700 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
701 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
702 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
703 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
704 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
705 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
706 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
707 (define-key mime-edit-mode-entity-map "t" 'mime-edit-insert-tag)
709 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
710 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
711 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
712 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
713 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
714 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
715 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
716 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
717 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
718 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
720 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
721 "Keymap prefix for MIME-Edit mode commands about enclosure.")
722 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
723 "Keymap for MIME-Edit mode commands about enclosure.")
725 (define-key mime-edit-mode-enclosure-map
726 "\C-a" 'mime-edit-enclose-alternative-region)
727 (define-key mime-edit-mode-enclosure-map
728 "\C-p" 'mime-edit-enclose-parallel-region)
729 (define-key mime-edit-mode-enclosure-map
730 "\C-m" 'mime-edit-enclose-mixed-region)
731 (define-key mime-edit-mode-enclosure-map
732 "\C-d" 'mime-edit-enclose-digest-region)
733 (define-key mime-edit-mode-enclosure-map
734 "\C-s" 'mime-edit-enclose-pgp-signed-region)
735 (define-key mime-edit-mode-enclosure-map
736 "\C-e" 'mime-edit-enclose-pgp-encrypted-region)
737 (define-key mime-edit-mode-enclosure-map
738 "\C-q" 'mime-edit-enclose-quote-region)
740 (defvar mime-edit-mode-map (make-sparse-keymap)
741 "Keymap for MIME-Edit mode commands.")
742 (define-key mime-edit-mode-map
743 mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
744 (define-key mime-edit-mode-map
745 mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
747 (defconst mime-edit-menu-title "MIME-Edit")
749 (defconst mime-edit-menu-list
750 '((mime-help "Describe MIME editor mode" mime-edit-help)
751 (file "Insert File" mime-edit-insert-file)
752 (external "Insert External" mime-edit-insert-external)
753 (voice "Insert Voice" mime-edit-insert-voice)
754 (message "Insert Message" mime-edit-insert-message)
755 (mail "Insert Mail" mime-edit-insert-mail)
756 (signature "Insert Signature" mime-edit-insert-signature)
757 (text "Insert Text" mime-edit-insert-text)
758 (tag "Insert Tag" mime-edit-insert-tag)
759 (alternative "Enclose as alternative"
760 mime-edit-enclose-alternative-region)
761 (parallel "Enclose as parallel" mime-edit-enclose-parallel-region)
762 (mixed "Enclose as serial" mime-edit-enclose-mixed-region)
763 (digest "Enclose as digest" mime-edit-enclose-digest-region)
764 (signed "Enclose as signed" mime-edit-enclose-pgp-signed-region)
765 (encrypted "Enclose as encrypted" mime-edit-enclose-pgp-encrypted-region)
766 (quote "Verbatim region" mime-edit-enclose-quote-region)
767 (key "Insert Public Key" mime-edit-insert-key)
768 (split "About split" mime-edit-set-split)
769 (sign "About sign" mime-edit-set-sign)
770 (encrypt "About encryption" mime-edit-set-encrypt)
771 (preview "Preview Message" mime-edit-preview-message)
772 (level "Toggle transfer-level" mime-edit-toggle-transfer-level))
773 "MIME-edit menubar entry.")
775 (static-if (featurep 'xemacs)
776 ;; modified by Pekka Marjola <pema@iki.fi>
777 ;; 1995/9/5 (c.f. [tm-en:69])
779 (defun mime-edit-define-menu-for-xemacs ()
780 "Define menu for XEmacs."
781 (cond ((featurep 'menubar)
782 (make-local-variable 'current-menubar)
783 (set-buffer-menubar current-menubar)
786 (cons mime-edit-menu-title
789 (vector (nth 1 item)(nth 2 item)
790 mime-edit-mode-flag)))
791 mime-edit-menu-list))))))
792 ;; modified by Steven L. Baur <steve@miranova.com>
793 ;; 1995/12/6 (c.f. [tm-en:209])
794 (or (boundp 'mime-edit-popup-menu-for-xemacs)
795 (setq mime-edit-popup-menu-for-xemacs
796 (append '("MIME Commands" "---")
797 (mapcar (function (lambda (item)
801 mime-edit-menu-list)))))
802 ;; Bogus check. Nemacs is not supported.
803 ;;(>= emacs-major-version 19)
804 (define-key mime-edit-mode-map [menu-bar mime-edit]
805 (cons mime-edit-menu-title
806 (make-sparse-keymap mime-edit-menu-title)))
809 (define-key mime-edit-mode-map
810 (vector 'menu-bar 'mime-edit (car item))
811 (cons (nth 1 item)(nth 2 item)))))
812 (reverse mime-edit-menu-list)))
817 (defmacro mime-edit-insert-place (type-list &rest body)
819 (if (get-text-property (point) 'invisible)
820 (error "Can't split invisible region"))
821 (if (or (member (intern (concat (car ,type-list) "/" (cadr ,type-list)))
822 mime-edit-attach-at-end-type)
823 (member (intern (car ,type-list))
824 mime-edit-attach-at-end-type))
825 (goto-char (point-max)))
828 (defmacro mime-edit-force-text-tag (regexp)
829 `(cond ((looking-at (concat "\n*\\(" ,regexp "\\)"))
830 (replace-match "\\1"))
832 (insert (mime-make-text-tag) "\n"))))
837 (defvar mime-edit-touched-flag nil)
840 (defun mime-edit-mode ()
841 "MIME minor mode for editing the tagged MIME message.
843 In this mode, basically, the message is composed in the tagged MIME
844 format. The message tag looks like:
846 --[[text/plain; charset=ISO-2022-JP][7bit]]
848 The tag specifies the MIME content type, subtype, optional parameters
849 and transfer encoding of the message following the tag. Messages
850 without any tag are treated as `text/plain' by default. Charset and
851 transfer encoding are automatically defined unless explicitly
852 specified. Binary messages such as audio and image are usually
853 hidden. The messages in the tagged MIME format are automatically
854 translated into a MIME compliant message when exiting this mode.
856 Available charsets depend on Emacs.
858 These charsets are available in all emacsen (with MULE):
859 US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R, ISO-2022-JP,
860 ISO-2022-JP-2, EUC-KR, CN-GB-2312, CN-BIG5 and ISO-2022-INT-1 are
863 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
864 be used to represent multilingual text in intermixed manner. Any
865 languages that has no registered charset are represented as either
866 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
868 If you want to add more charsets in mule, please set variable
869 `charsets-mime-charset-alist'. This variable must be alist of which
870 key is list of charset and value is symbol of MIME charset. If name
871 of coding-system is different as MIME charset, please set variable
872 `mime-charset-coding-system-alist'. This variable must be alist of
873 which key is MIME charset and value is coding-system.
875 Following commands are available in addition to major mode commands:
878 \\[mime-edit-insert-text] insert a text message.
879 \\[mime-edit-insert-file] insert a (binary) file.
880 \\[mime-eidt-insert-text-file] insert a text file.
881 \\[mime-edit-insert-external] insert a reference to external body.
882 \\[mime-edit-insert-voice] insert a voice message.
883 \\[mime-edit-insert-message] insert a mail or news message.
884 \\[mime-edit-insert-mail] insert a mail message.
885 \\[mime-edit-insert-signature] insert a signature file at end.
886 \\[mime-edit-insert-key] insert PGP public key.
887 \\[mime-edit-insert-tag] insert a new MIME tag.
889 \[make enclosure (maybe multipart)\]
890 \\[mime-edit-enclose-alternative-region] enclose as multipart/alternative.
891 \\[mime-edit-enclose-parallel-region] enclose as multipart/parallel.
892 \\[mime-edit-enclose-mixed-region] enclose as multipart/mixed.
893 \\[mime-edit-enclose-digest-region] enclose as multipart/digest.
894 \\[mime-edit-enclose-pgp-signed-region] enclose as PGP signed.
895 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
896 \\[mime-edit-enclose-quote-region] enclose as verbose mode
897 (to avoid to expand tags)
900 \\[mime-edit-set-transfer-level-7bit] set transfer-level as 7.
901 \\[mime-edit-set-transfer-level-8bit] set transfer-level as 8.
902 \\[mime-edit-set-split] set message splitting mode.
903 \\[mime-edit-set-sign] set PGP-sign mode.
904 \\[mime-edit-set-encrypt] set PGP-encryption mode.
905 \\[mime-edit-preview-message] preview editing MIME message.
906 \\[mime-edit-exit] exit and translate into a MIME
908 \\[mime-edit-help] show this help.
909 \\[mime-edit-maybe-translate] exit and translate if in MIME mode,
912 Additional commands are available in some major modes:
913 C-c C-c exit, translate and run the original command.
914 C-c C-s exit, translate and run the original command.
916 The following is a message example written in the tagged MIME format.
917 TABs at the beginning of the line are not a part of the message:
919 This is a conventional plain text. It should be translated
922 This is also a plain text. But, it is explicitly specified as
924 --[[text/plain; charset=ISO-8859-1]]
925 This is also a plain text. But charset is specified as
928 ¡Hola! Buenos días. ¿Cómo está usted?
930 This is a <bold>enriched text</bold>.
931 --[[image/gif][base64]]...image encoded in base64 here...
932 --[[audio/basic][base64]]...audio encoded in base64 here...
934 User customizable variables (not all of them are documented):
935 mime-edit-mode-entity-prefix
936 mime-edit-mode-enclosure-prefix
937 Specifies a key prefix for MIME minor mode commands.
939 mime-ignore-preceding-spaces
940 Preceding white spaces in a message body are ignored if non-nil.
942 mime-ignore-trailing-spaces
943 Trailing white spaces in a message body are ignored if non-nil.
946 Hide a non-textual body message encoded in base64 after insertion
950 A number of network transfer level. It should be bigger than 7.
951 If you are in 8bit-through environment, please set to 8.
953 mime-edit-voice-recorder
954 Specifies a function to record and encode a voice message.
955 The function `mime-edit-voice-recorder-for-sun' is for Sun
959 Turning on MIME mode calls the value of mime-edit-mode-hook, if
962 mime-edit-translate-hook
963 The value of mime-edit-translate-hook is called just before translating
964 the tagged MIME format into a MIME compliant message if it is
965 non-nil. If the hook call the function mime-edit-insert-signature,
966 the signature file will be inserted automatically.
969 Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
972 (if mime-edit-mode-flag
974 (if mime-edit-touched-flag
976 (make-local-variable 'mime-edit-touched-flag)
977 (setq mime-edit-touched-flag t)
978 (turn-on-mime-edit))))
981 (static-if (featurep 'xemacs)
982 (add-minor-mode 'mime-edit-mode-flag
983 '((" MIME-Edit " mime-transfer-level-string))
987 (set-alist 'minor-mode-alist
989 '((" MIME-Edit " mime-transfer-level-string)))
990 (set-alist 'minor-mode-map-alist
996 (defun turn-on-mime-edit ()
997 "Unconditionally turn on MIME-Edit mode."
999 (if mime-edit-mode-flag
1000 (error "You are already editing a MIME message")
1001 (setq mime-edit-mode-flag t)
1003 ;; Set transfer level into mode line
1005 (setq mime-transfer-level-string
1006 (mime-encoding-name mime-transfer-level 'not-omit))
1007 (force-mode-line-update)
1009 ;; Define menu for XEmacs.
1010 (if (featurep 'xemacs)
1011 (mime-edit-define-menu-for-xemacs))
1015 ;; I don't care about saving these.
1016 (setq paragraph-start
1017 (regexp-or mime-edit-single-part-tag-regexp
1019 (setq paragraph-separate
1020 (regexp-or mime-edit-single-part-tag-regexp
1021 paragraph-separate))
1022 (run-hooks 'mime-edit-mode-hook)
1024 (substitute-command-keys
1025 "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))))
1028 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1031 (defun mime-edit-exit (&optional nomime no-error)
1032 "Translate the tagged MIME message into a MIME compliant message.
1033 When NOMIME is nil, encode a message in the buffer into MIME.
1034 Otherwise, just returns to previous mode. If NO-ERROR is non-nil,
1035 no errors will be signaled even if it is not MIME-Edit mode."
1037 (if (not mime-edit-mode-flag)
1039 (error "You aren't editing a MIME message"))
1042 (run-hooks 'mime-edit-translate-hook)
1043 (mime-edit-translate-buffer)))
1044 ;; Restore previous state.
1045 (setq mime-edit-mode-flag nil)
1046 (if (and (featurep 'xemacs)
1047 (featurep 'menubar))
1048 (delete-menu-item (list mime-edit-menu-title)))
1050 (set-buffer-modified-p (buffer-modified-p))
1051 (run-hooks 'mime-edit-exit-hook)
1052 (message "Exit MIME editor mode.")))
1054 (defun mime-edit-maybe-translate ()
1056 (mime-edit-exit nil t)
1057 (call-interactively 'mime-edit-maybe-split-and-send))
1059 (defun mime-edit-help ()
1060 "Show help message about MIME mode."
1062 (with-output-to-temp-buffer "*Help*"
1063 (princ "MIME editor mode:\n")
1064 (princ (documentation 'mime-edit-mode))
1065 (print-help-return-message)))
1067 (defun mime-edit-insert-text (&optional subtype)
1068 "Insert a text message.
1069 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1070 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1072 (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1074 (if (looking-at mime-edit-single-part-tag-regexp)
1076 ;; Make a space between the following message.
1079 (if (and (member (cadr ret) '("enriched"))
1080 (fboundp 'enriched-mode))
1082 (if (boundp 'enriched-mode)
1083 (enriched-mode -1))))))
1085 (defun mime-edit-insert-text-file (file &optional verbose)
1086 "Insert a text message from a FILE.
1087 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1088 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1089 (interactive "fInsert file as a MIME text: \nP")
1090 (let* ((guess (mime-find-file-type file))
1093 (parameters (nth 2 guess))
1094 ;; (encoding (nth 3 guess))
1095 (disposition-type (nth 4 guess))
1096 (disposition-params (nth 5 guess))
1098 (setq subtype (mime-prompt-for-subtype type subtype))
1099 ;; (if (or (interactive-p) verbose)
1100 ;; (setq encoding (mime-prompt-for-encoding encoding)))
1102 (setq disposition-type (mime-prompt-for-disposition disposition-type)))
1103 (if (or (consp parameters) (stringp disposition-type))
1104 (let ((rest parameters) cell attribute value)
1105 (setq parameters "")
1107 (setq cell (car rest))
1108 (setq attribute (car cell))
1109 (setq value (cdr cell))
1110 (if (eq value 'file)
1111 (setq value (std11-wrap-as-quoted-string
1112 (file-name-nondirectory file))))
1113 (setq parameters (concat parameters "; " attribute "=" value))
1114 (setq rest (cdr rest)))
1115 (if disposition-type
1118 (concat parameters "\n"
1119 "Content-Disposition: " disposition-type))
1120 (setq rest disposition-params)
1122 (setq cell (car rest))
1123 (setq attribute (car cell))
1124 (setq value (cdr cell))
1125 (if (eq value 'file)
1126 (setq value (std11-wrap-as-quoted-string
1127 (file-name-nondirectory file))))
1129 (concat parameters "; " attribute "=" value))
1130 (setq rest (cdr rest)))))))
1131 (mime-edit-insert-place
1133 (mime-edit-insert-tag type subtype parameters)
1134 ;; (if (stringp encoding)
1135 ;; (mime-edit-define-encoding encoding))
1137 (let ((ret (insert-file-contents file)))
1138 (forward-char (cadr ret))
1139 (mime-edit-force-text-tag mime-edit-single-part-regexp))))))
1141 (defun mime-edit-guess-charset (file)
1143 (let (candidates candidate eol eol-string)
1144 (set-buffer-multibyte nil)
1145 (insert-file-contents-as-binary file)
1146 (setq candidates (detect-coding-region (point-min) (point-max)))
1147 (setq candidate (if (listp candidates)
1150 (setq eol (coding-system-eol-type candidate))
1152 (static-if (featurep 'xemacs)
1155 (setq eol-string "\n"))
1157 (static-if (featurep 'xemacs)
1160 (setq eol-string "\r")))
1161 (goto-char (point-min))
1163 (while (search-forward eol-string nil t)
1164 (replace-match "\r\n")))
1165 (static-if (featurep 'xemacs)
1166 (setq candidate (coding-system-name (coding-system-base candidate)))
1167 (setq candidate (coding-system-base candidate)))
1169 (cond ((eq candidate 'undecided)
1170 (setq candidate "us-ascii"))
1171 ((eq candidate 'iso-2022-7bit)
1172 (setq candidate "iso-2022-jp"))
1175 (symbol-name (coding-system-to-mime-charset candidate)))))
1176 (cons candidate (buffer-string)))))
1178 (defun mime-edit-insert-file (file &optional verbose)
1179 "Insert a message from a FILE.
1180 If VERBOSE is non-nil, it will prompt for Content-Type,
1181 Content-Transfer-Encoding and Content-Disposition headers."
1182 (interactive "fInsert file as MIME message: \nP")
1183 (let* ((guess (mime-find-file-type file))
1184 (type (nth 0 guess))
1185 (subtype (nth 1 guess))
1186 (parameters (nth 2 guess))
1187 (encoding (nth 3 guess))
1188 (disposition-type (nth 4 guess))
1189 (disposition-params (nth 5 guess))
1192 (setq type (mime-prompt-for-type type)
1193 subtype (mime-prompt-for-subtype type subtype)))
1194 (if (or (interactive-p) verbose)
1195 (setq encoding (mime-prompt-for-encoding encoding)))
1197 (setq disposition-type (mime-prompt-for-disposition disposition-type)))
1198 (if (or (consp parameters) (stringp disposition-type))
1199 (let ((rest parameters) cell attribute value)
1200 (setq parameters "")
1201 (when (string= type "text")
1202 (setq charset-and-string (mime-edit-guess-charset file))
1204 (concat parameters "; charset="
1205 (car charset-and-string))))
1207 (setq cell (car rest))
1208 (setq attribute (car cell))
1209 (setq value (cdr cell))
1210 (if (eq value 'file)
1211 (setq value (std11-wrap-as-quoted-string
1212 (file-name-nondirectory file))))
1213 (setq parameters (concat parameters "; " attribute "=" value))
1214 (setq rest (cdr rest)))
1215 (if disposition-type
1218 (concat parameters "\n"
1219 "Content-Disposition: " disposition-type))
1220 (setq rest disposition-params)
1222 (setq cell (car rest))
1223 (setq attribute (car cell))
1224 (setq value (cdr cell))
1225 (if (eq value 'file)
1226 (setq value (std11-wrap-as-quoted-string
1227 (file-name-nondirectory file))))
1229 (concat parameters "; " attribute "=" value))
1230 (setq rest (cdr rest)))))))
1231 (mime-edit-insert-place
1233 (mime-edit-insert-tag type subtype parameters)
1234 (if charset-and-string
1235 (mime-edit-insert-binary-string (cdr charset-and-string) encoding)
1236 (mime-edit-insert-binary-file file encoding)))))
1238 (defun mime-edit-insert-external ()
1239 "Insert a reference to external body."
1241 (let* ((pritype (mime-prompt-for-type))
1242 (subtype (mime-prompt-for-subtype pritype))
1243 (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1244 (mime-edit-insert-place
1245 '("message" "external-body")
1246 (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1248 ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1252 (insert "Content-Type: "
1253 pritype "/" subtype (or parameters "") "\n"))
1254 (mime-edit-force-text-tag mime-edit-single-part-tag-regexp))))
1256 (defun mime-edit-insert-voice ()
1257 "Insert a voice message."
1261 "What transfer encoding: "
1262 (mime-encoding-alist) nil t nil)))
1263 (mime-edit-insert-place
1265 (mime-edit-insert-tag "audio" "basic" nil)
1266 (mime-edit-define-encoding encoding)
1268 (narrow-to-region (1- (point))(point))
1270 (funcall mime-edit-voice-recorder encoding)
1273 (invisible-region (point-min)(point-max))
1274 (goto-char (point-max))))))))
1276 (defun mime-edit-insert-signature (&optional arg)
1277 "Insert a signature file."
1279 (let ((signature-insert-hook
1282 (let ((items (mime-find-file-type signature-file-name)))
1283 (apply (function mime-edit-insert-tag)
1284 (car items) (cadr items) (list (caddr items))))))))
1285 (insert-signature arg)))
1288 ;; Insert a new tag around a point.
1290 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1291 "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1292 If nothing is inserted, return nil."
1294 (if (get-text-property (point) 'invisible)
1295 (error "Can't split invisible region"))
1297 (mime-edit-goto-tag)
1298 (if (and (re-search-forward mime-edit-tag-regexp nil t)
1299 (< (match-beginning 0) p)
1300 (< p (match-end 0)))
1301 (goto-char (match-beginning 0))
1308 (mime-prompt-for-type)))
1311 (mime-prompt-for-subtype pritype)))
1314 (mime-prompt-for-parameters pritype subtype delimiter)))
1315 ;; Make a new MIME tag.
1316 (setq newtag (mime-make-tag pritype subtype parameters))
1317 ;; Find an current MIME tag.
1320 (if (mime-edit-goto-tag)
1321 (buffer-substring (match-beginning 0) (match-end 0))
1322 ;; Assume content type is 'text/plan'.
1323 (mime-make-tag "text" "plain"))))
1324 ;; We are only interested in TEXT.
1326 (not (mime-test-content-type
1327 (mime-edit-get-contype oldtag) "text")))
1330 (if (or (not oldtag) ;Not text
1331 (or mime-ignore-same-text-tag
1332 (not (string-equal oldtag newtag))))
1334 ;; Mark the beginning of the tag for convenience.
1335 (push-mark (point) 'nomsg)
1336 (insert newtag "\n")
1337 (list pritype subtype parameters) ;New tag is created.
1339 ;; Restore previous point.
1341 nil ;Nothing is created.
1344 ;; #### This should be merged into the function below but for now,
1345 ;; don't change APIs.
1346 (defun mime-edit-insert-binary-string (string &optional encoding)
1347 "Insert binary STRING at point.
1348 Optional argument ENCODING specifies an encoding method such as base64."
1349 (let* ((tagend (1- (point))) ;End of the tag
1350 (hide-p (and mime-auto-hide-body
1353 (let ((en (downcase encoding)))
1354 (or (string-equal en "7bit")
1355 (string-equal en "8bit")
1356 (string-equal en "binary")))))))
1358 (narrow-to-region tagend (point))
1361 ;; #### @!#$%@!${$@}
1362 (set-buffer-multibyte nil)
1364 ;; #### Why mime-encode-string doesn't exist?
1365 (mime-encode-region (point-min) (point-max)
1366 (or encoding "7bit"))
1370 (invisible-region (point-min) (point-max))
1371 (goto-char (point-max)))
1372 (goto-char (point-max))))
1373 (mime-edit-force-text-tag mime-edit-tag-regexp)
1374 ;; Define encoding even if it is 7bit.
1375 (if (stringp encoding)
1377 (goto-char tagend) ; Make sure which line the tag is on.
1378 (mime-edit-define-encoding encoding)))))
1380 (defun mime-edit-insert-binary-file (file &optional encoding)
1381 "Insert binary FILE at point.
1382 Optional argument ENCODING specifies an encoding method such as base64."
1383 (let* ((tagend (1- (point))) ;End of the tag
1384 (hide-p (and mime-auto-hide-body
1387 (let ((en (downcase encoding)))
1388 (or (string-equal en "7bit")
1389 (string-equal en "8bit")
1390 (string-equal en "binary")))))))
1392 (narrow-to-region tagend (point))
1393 (mime-insert-encoded-file file encoding)
1396 (invisible-region (point-min) (point-max))
1397 (goto-char (point-max)))
1398 (goto-char (point-max))))
1399 (mime-edit-force-text-tag mime-edit-tag-regexp)
1400 ;; Define encoding even if it is 7bit.
1401 (if (stringp encoding)
1403 (goto-char tagend) ; Make sure which line the tag is on.
1404 (mime-edit-define-encoding encoding)))))
1407 ;; Commands work on a current message flagment.
1409 (defun mime-edit-goto-tag ()
1410 "Search for the beginning of the tagged MIME message."
1411 (let ((current (point)))
1412 (if (looking-at mime-edit-tag-regexp)
1414 ;; At first, go to the end.
1415 (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1416 (goto-char (1- (match-beginning 0))) ;For multiline tag
1419 (goto-char (point-max))))
1420 ;; Then search for the beginning.
1421 (re-search-backward mime-edit-end-tag-regexp nil t)
1422 (or (looking-at mime-edit-beginning-tag-regexp)
1423 ;; Restore previous point.
1428 (defun mime-edit-content-beginning ()
1429 "Return the point of the beginning of content."
1431 (let ((beg (save-excursion
1432 (beginning-of-line) (point))))
1433 (if (mime-edit-goto-tag)
1434 (let ((top (point)))
1435 (goto-char (match-end 0))
1436 (if (and (= beg top)
1437 (= (following-char) ?\^M))
1441 ;; Default text/plain tag.
1442 (goto-char (point-min))
1444 (concat "\n" (regexp-quote mail-header-separator)
1445 (if mime-ignore-preceding-spaces
1446 "[ \t\n]*\n" "\n")) nil 'move)
1449 (defun mime-edit-content-end ()
1450 "Return the point of the end of content."
1452 (if (mime-edit-goto-tag)
1454 (goto-char (match-end 0))
1455 (if (invisible-p (point))
1456 (next-visible-point (point))
1457 ;; Move to the end of this text.
1458 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1459 ;; Don't forget a multiline tag.
1460 (goto-char (match-beginning 0)))
1462 ;; Assume the message begins with text/plain.
1463 (goto-char (mime-edit-content-beginning))
1464 (if (re-search-forward mime-edit-tag-regexp nil 'move)
1465 ;; Don't forget a multiline tag.
1466 (goto-char (match-beginning 0)))
1469 (defun mime-edit-define-charset (charset)
1470 "Set charset of current tag to CHARSET."
1472 (if (mime-edit-goto-tag)
1473 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1474 (delete-region (match-beginning 0) (match-end 0))
1477 (mime-edit-set-parameter
1478 (mime-edit-get-contype tag)
1480 (let ((comment (get charset 'mime-charset-comment)))
1482 (concat (upcase (symbol-name charset)) " (" comment ")")
1483 (upcase (symbol-name charset)))))
1484 (mime-edit-get-encoding tag)))))))
1486 (defun mime-edit-define-encoding (encoding)
1487 "Set encoding of current tag to ENCODING."
1489 (if (mime-edit-goto-tag)
1490 (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1491 (delete-region (match-beginning 0) (match-end 0))
1492 (insert (mime-create-tag (mime-edit-get-contype tag) encoding))))))
1494 (defun mime-edit-choose-charset ()
1495 "Choose charset of a text following current point."
1496 (detect-mime-charset-region (point) (mime-edit-content-end)))
1498 (defun mime-make-text-tag (&optional subtype)
1499 "Make a tag for a text after current point.
1500 Subtype of text type can be specified by an optional argument SUBTYPE.
1501 Otherwise, it is obtained from `mime-content-types'."
1502 (let* ((pritype "text")
1503 (subtype (or subtype
1504 (car (car (cdr (assoc pritype mime-content-types)))))))
1505 ;; Charset should be defined later.
1506 (mime-make-tag pritype subtype)))
1509 ;; Tag handling functions
1511 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1512 "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1513 (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1517 (defun mime-create-tag (contype &optional encoding)
1518 "Make a tag with CONTENT-TYPE and optional ENCODING."
1519 (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1522 (defun mime-edit-get-contype (tag)
1523 "Return Content-Type (including parameters) of TAG."
1525 (or (string-match mime-edit-single-part-tag-regexp tag)
1526 (string-match mime-edit-multipart-beginning-regexp tag)
1527 (string-match mime-edit-multipart-end-regexp tag))
1528 (substring tag (match-beginning 1) (match-end 1))))
1530 (defun mime-edit-get-encoding (tag)
1531 "Return encoding of TAG."
1533 (string-match mime-edit-single-part-tag-regexp tag)
1535 (not (= (match-beginning 3) (match-end 3)))
1536 (substring tag (match-beginning 3) (match-end 3))))
1538 (defun mime-get-parameter (contype parameter)
1539 "For given CONTYPE return value for PARAMETER.
1540 Nil if no such parameter."
1544 (regexp-quote parameter)
1545 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1547 (substring contype (match-beginning 1) (match-end 1))
1548 nil ;No such parameter
1551 (defun mime-edit-set-parameter (contype parameter value)
1552 "For given CONTYPE set PARAMETER to VALUE."
1553 (let (ctype opt-fields)
1554 (if (string-match "\n[^ \t\n\r]+:" contype)
1555 (setq ctype (substring contype 0 (match-beginning 0))
1556 opt-fields (substring contype (match-beginning 0)))
1557 (setq ctype contype))
1561 (regexp-quote parameter)
1562 "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1565 (concat (substring ctype 0 (match-beginning 1))
1567 (substring contype (match-end 1)))
1568 ;; This field makes two CDP header when charset parameter is present.
1570 (concat ctype "; " parameter "=" value opt-fields))))
1572 (defun mime-strip-parameters (contype)
1573 "Return primary content-type and subtype without parameters for CONTYPE."
1574 (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1575 (substring contype (match-beginning 1) (match-end 1)) nil))
1577 (defun mime-test-content-type (contype type &optional subtype)
1578 "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1579 (and (stringp contype)
1582 (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1583 (downcase contype))))
1588 (defun mime-find-file-type (file)
1589 "Guess Content-Type, subtype, and parameters from FILE."
1591 (guesses mime-file-types))
1592 (while (and (not guess) guesses)
1593 (if (string-match (car (car guesses)) file)
1594 (setq guess (cdr (car guesses))))
1595 (setq guesses (cdr guesses)))
1598 (defun mime-prompt-for-type (&optional default)
1599 "Ask for Content-type."
1601 ;; Repeat until primary content type is specified.
1602 (while (string-equal type "")
1604 (completing-read "What content type: "
1607 'require-match ;Type must be specified.
1609 (if (string-equal type "")
1611 (message "Content type is required.")
1616 (defun mime-prompt-for-subtype (type &optional default)
1617 "Ask for subtype of media-type TYPE."
1618 (let ((subtypes (cdr (assoc type mime-content-types))))
1620 (assoc default subtypes))
1621 (setq default (car (car subtypes)))))
1626 "What content subtype: (default " default ") ")
1627 "What content subtype: ")
1628 (cdr (assoc type mime-content-types))
1630 'require-match ;Subtype must be specified.
1632 (if (string-equal answer "") default answer)))
1634 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1635 "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1636 Optional DELIMITER specifies parameter delimiter (';' by default)."
1637 (let* ((delimiter (or delimiter "; "))
1642 (mime-prompt-for-parameters-1
1644 (cdr (assoc pritype mime-content-types))))))
1646 (if (and (stringp parameters)
1647 (not (string-equal parameters "")))
1648 (concat delimiter parameters)
1649 "" ;"" if no parameters
1652 (defun mime-prompt-for-parameters-1 (optlist)
1653 (apply (function append)
1654 (mapcar (function mime-prompt-for-parameter) optlist)))
1656 (defun mime-prompt-for-parameter (parameter)
1658 Parameter must be '(PROMPT CHOICE1 (CHOISE2 ...))."
1659 (let* ((prompt (car parameter))
1660 (choices (mapcar (function
1662 (if (consp e) e (list e))))
1664 (default (car (car choices)))
1670 (concat "What " prompt
1672 (if (string-equal default "") "\"\"" default)
1674 choices nil nil ""))
1675 ;; If nothing is selected, use default.
1676 (if (string-equal answer "")
1677 (setq answer default)))
1679 (read-string (concat "What " prompt ": "))))
1680 (cons (if (and answer
1681 (not (string-equal answer "")))
1683 ;; Note: control characters ignored!
1684 (if (string-match mime-tspecials-regexp answer)
1685 (concat "\"" answer "\"") answer)))
1686 (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))))
1688 (defun mime-prompt-for-encoding (default)
1689 "Ask for Content-Transfer-Encoding."
1694 "What transfer encoding: "
1695 (mime-encoding-alist) nil t default))
1699 (defun mime-prompt-for-disposition (default)
1700 "Prompt Content-Disposition"
1701 (completing-read (concat "What disposition type (default "
1703 mime-content-disposition-types
1708 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1711 (defvar mime-edit-translate-buffer-hook
1712 '(mime-edit-pgp-enclose-buffer
1713 mime-edit-translate-body
1714 mime-edit-translate-header))
1716 (defun mime-edit-translate-header ()
1717 "Encode the message header into network representation."
1718 (eword-encode-header 'code-conversion)
1719 (run-hooks 'mime-edit-translate-header-hook))
1721 (defun mime-edit-translate-buffer ()
1722 "Encode the tagged MIME message in current buffer in MIME compliant message."
1725 (if (catch 'mime-edit-error
1727 (run-hooks 'mime-edit-translate-buffer-hook)))
1730 (error "Translation error!"))))
1732 (defun mime-edit-find-inmost ()
1733 (goto-char (point-min))
1734 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1735 (let ((bb (match-beginning 0))
1737 (type (buffer-substring (match-beginning 1)(match-end 1)))
1739 (setq end-exp (format "--}-<<%s>>\n" type))
1741 (if (re-search-forward end-exp nil t)
1742 (setq eb (match-beginning 0))
1743 (setq eb (point-max)))
1744 (narrow-to-region be eb)
1746 (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1748 (narrow-to-region (match-beginning 0)(point-max))
1749 (mime-edit-find-inmost))
1751 (list type bb be eb)))))
1753 (defun mime-edit-process-multipart-1 (boundary)
1754 (let ((ret (mime-edit-find-inmost)))
1756 (let ((type (car ret))
1757 (bb (nth 1 ret))(be (nth 2 ret))
1759 (narrow-to-region bb eb)
1760 (delete-region bb be)
1761 (setq bb (point-min))
1762 (setq eb (point-max))
1765 (if (looking-at mime-edit-multipart-end-regexp)
1766 (let ((beg (match-beginning 0))
1767 (end (match-end 0)))
1768 (delete-region beg end)
1769 (mime-edit-force-text-tag mime-edit-beginning-tag-regexp)))
1770 (cond ((string-equal type "quote")
1771 (mime-edit-enquote-region bb eb))
1772 ((string-equal type "pgp-signed")
1773 (mime-edit-sign-pgp-mime bb eb boundary))
1774 ((string-equal type "pgp-encrypted")
1775 (mime-edit-encrypt-pgp-mime bb eb boundary))
1776 ((string-equal type "kazu-signed")
1777 (mime-edit-sign-pgp-kazu bb eb boundary))
1778 ((string-equal type "kazu-encrypted")
1779 (mime-edit-encrypt-pgp-kazu bb eb boundary))
1780 ((string-equal type "smime-signed")
1781 (mime-edit-sign-smime bb eb boundary))
1782 ((string-equal type "smime-encrypted")
1783 (mime-edit-encrypt-smime bb eb boundary))
1786 (nth 2 (mime-edit-translate-region bb eb
1790 (format "--[[multipart/%s;
1791 boundary=\"%s\"][7bit]]\n"
1795 (defun mime-edit-enquote-region (beg end)
1798 (narrow-to-region beg end)
1800 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1801 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1802 (replace-match (concat "- " (substring tag 1))))))))
1804 (defun mime-edit-dequote-region (beg end)
1807 (narrow-to-region beg end)
1809 (while (re-search-forward
1810 mime-edit-quoted-single-part-tag-regexp nil t)
1811 (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1812 (replace-match (concat "-" (substring tag 2))))))))
1814 (defvar mime-edit-pgp-user-id nil)
1816 (defun mime-edit-sign-pgp-mime (beg end boundary)
1819 (let* ((from (std11-field-body "From" mail-header-separator))
1821 (narrow-to-region beg end)
1822 (mime-edit-translate-region beg end boundary)))
1824 (encoding (nth 1 ret))
1825 (pgp-boundary (concat "pgp-sign-" boundary))
1828 (insert (format "Content-Type: %s\n" ctype))
1830 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1832 (or (let ((pgg-default-user-id
1833 (or mime-edit-pgp-user-id
1835 (nth 1 (std11-extract-address-components from))
1836 pgg-default-user-id))))
1837 (pgg-sign-region (point-min)(point-max)))
1838 (throw 'mime-edit-error 'pgp-error))
1840 (cdr (assq 'hash-algorithm
1841 (cdar (with-current-buffer pgg-output-buffer
1842 (pgg-parse-armor-region
1843 (point-min)(point-max))))))
1846 (concat "; micalg=pgp-" (downcase (symbol-name micalg)))
1849 (insert (format "--[[multipart/signed;
1851 protocol=\"application/pgp-signature\"][7bit]]
1853 " pgp-boundary micalg pgp-boundary))
1854 (goto-char (point-max))
1855 (insert (format "\n--%s
1856 Content-Type: application/pgp-signature
1857 Content-Transfer-Encoding: 7bit
1860 (insert-buffer-substring pgg-output-buffer)
1861 (goto-char (point-max))
1862 (insert (format "\n--%s--\n" pgp-boundary))))))
1864 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1866 (defun mime-edit-make-encrypt-recipient-header ()
1867 (let* ((names mime-edit-encrypt-recipient-fields-list)
1869 (std11-field-bodies (cons "From" names)
1870 nil mail-header-separator))
1873 (setq values (cdr values))))
1874 (header (and (stringp from)
1875 (if (string-equal from "")
1877 (format "From: %s\n" from))))
1879 (while (and names values)
1880 (let ((name (car names))
1881 (value (car values)))
1882 (and (stringp value)
1883 (or (string-equal value "")
1885 (setq header (concat header name ": " value "\n")
1886 recipients (if recipients
1887 (concat recipients " ," value)
1889 (setq names (cdr names)
1890 values (cdr values)))
1891 (vector from recipients header)))
1893 (defun mime-edit-encrypt-pgp-mime (beg end boundary)
1896 (let (from recipients header)
1897 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1898 (setq from (aref ret 0)
1899 recipients (aref ret 1)
1900 header (aref ret 2)))
1901 (narrow-to-region beg end)
1903 (mime-edit-translate-region beg end boundary))
1905 (encoding (nth 1 ret))
1906 (pgp-boundary (concat "pgp-" boundary)))
1909 (insert (format "Content-Type: %s\n" ctype))
1911 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1913 (eword-encode-header)
1914 (or (let ((pgg-default-user-id
1915 (or mime-edit-pgp-user-id
1917 (nth 1 (std11-extract-address-components from))
1918 pgg-default-user-id))))
1920 (point-min) (point-max)
1921 (mapcar (lambda (recipient)
1922 (nth 1 (std11-extract-address-components
1924 (split-string recipients
1925 "\\([ \t\n]*,[ \t\n]*\\)+"))))
1926 (throw 'mime-edit-error 'pgp-error))
1927 (delete-region (point-min)(point-max))
1929 (insert (format "--[[multipart/encrypted;
1931 protocol=\"application/pgp-encrypted\"][7bit]]
1933 Content-Type: application/pgp-encrypted
1936 Content-Type: application/octet-stream
1937 Content-Transfer-Encoding: 7bit
1939 " pgp-boundary pgp-boundary pgp-boundary))
1940 (insert-buffer-substring pgg-output-buffer)
1941 (goto-char (point-max))
1942 (insert (format "\n--%s--\n" pgp-boundary)))))))
1944 (defun mime-edit-sign-pgp-kazu (beg end boundary)
1947 (narrow-to-region beg end)
1949 (mime-edit-translate-region beg end boundary))
1951 (encoding (nth 1 ret)))
1953 (insert (format "Content-Type: %s\n" ctype))
1955 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1957 (or (pgg-sign-region beg (point-max) 'clearsign)
1958 (throw 'mime-edit-error 'pgp-error))
1961 "--[[application/pgp; format=mime][7bit]]\n")
1964 (defun mime-edit-encrypt-pgp-kazu (beg end boundary)
1966 (let (recipients header)
1967 (let ((ret (mime-edit-make-encrypt-recipient-header)))
1968 (setq recipients (aref ret 1)
1969 header (aref ret 2)))
1971 (narrow-to-region beg end)
1973 (mime-edit-translate-region beg end boundary))
1975 (encoding (nth 1 ret)))
1978 (insert (format "Content-Type: %s\n" ctype))
1980 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1982 (or (pgg-encrypt-region beg (point-max) recipients)
1983 (throw 'mime-edit-error 'pgp-error))
1986 "--[[application/pgp; format=mime][7bit]]\n")
1989 (defun mime-edit-sign-smime (beg end boundary)
1993 (narrow-to-region beg end)
1994 (mime-edit-translate-region beg end boundary)))
1996 (encoding (nth 1 ret))
1997 (smime-boundary (concat "smime-sign-" boundary)))
1999 (insert (format "Content-Type: %s\n" ctype))
2001 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
2003 (let (buffer-undo-list)
2004 (goto-char (point-min))
2005 (while (progn (end-of-line) (not (eobp)))
2008 (or (prog1 (smime-sign-region (point-min)(point-max))
2009 (push nil buffer-undo-list)
2010 (ignore-errors (undo)))
2011 (throw 'mime-edit-error 'pgp-error)))
2013 (insert (format "--[[multipart/signed;
2014 boundary=\"%s\"; micalg=sha1;
2015 protocol=\"application/pkcs7-signature\"][7bit]]
2017 " smime-boundary smime-boundary))
2018 (goto-char (point-max))
2019 (insert (format "\n--%s
2020 Content-Type: application/pkcs7-signature; name=\"smime.p7s\"
2021 Content-Transfer-Encoding: base64
2022 Content-Disposition: attachment; filename=\"smime.p7s\"
2023 Content-Description: S/MIME Cryptographic Signature
2026 (insert-buffer-substring smime-output-buffer)
2027 (goto-char (point-max))
2028 (insert (format "\n--%s--\n" smime-boundary))))))
2030 (defun mime-edit-encrypt-smime (beg end boundary)
2034 (narrow-to-region beg end)
2035 (mime-edit-translate-region beg end boundary)))
2037 (encoding (nth 1 ret)))
2039 (insert (format "Content-Type: %s\n" ctype))
2041 (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
2043 (goto-char (point-min))
2044 (while (progn (end-of-line) (not (eobp)))
2047 (or (smime-encrypt-region (point-min)(point-max))
2048 (throw 'mime-edit-error 'pgp-error))
2049 (delete-region (point-min)(point-max))
2050 (insert "--[[application/pkcs7-mime; name=\"smime.p7m\"
2051 Content-Disposition: attachment; filename=\"smime.p7m\"
2052 Content-Description: S/MIME Encrypted Message][base64]]\n")
2053 (insert-buffer-substring smime-output-buffer)))))
2055 (defsubst replace-space-with-underline (str)
2056 (mapconcat (function
2063 (defun mime-edit-make-boundary ()
2064 (concat mime-multipart-boundary "_"
2065 (replace-space-with-underline (current-time-string))))
2067 (defun mime-edit-translate-body ()
2068 "Encode the tagged MIME body in current buffer in MIME compliant message."
2071 (let ((boundary (mime-edit-make-boundary))
2074 (while (mime-edit-process-multipart-1
2075 (format "%s-%d" boundary i))
2078 ;; We are interested in message body.
2081 (goto-char (point-min))
2083 (concat "\n" (regexp-quote mail-header-separator)
2084 (if mime-ignore-preceding-spaces
2085 "[ \t\n]*\n" "\n")) nil 'move)
2089 (goto-char (point-max))
2090 (and mime-ignore-trailing-spaces
2091 (re-search-backward "[^ \t\n]\n" beg t)
2094 (setq ret (mime-edit-translate-region
2096 (format "%s-%d" boundary i)))))
2097 (mime-edit-dequote-region (point-min)(point-max))
2098 (let ((contype (car ret)) ;Content-Type
2099 (encoding (nth 1 ret)) ;Content-Transfer-Encoding
2101 ;; Insert User-Agent field
2102 (and mime-edit-insert-user-agent-field
2103 (or (mail-position-on-field "User-Agent")
2104 (insert mime-edit-user-agent-value)))
2105 ;; Make primary MIME headers.
2106 (or (mail-position-on-field "MIME-Version")
2107 (insert mime-edit-mime-version-value))
2108 ;; Remove old Content-Type and other fields.
2110 (goto-char (point-min))
2111 (search-forward (concat "\n" mail-header-separator "\n") nil t)
2112 (narrow-to-region (point-min) (point))
2113 (goto-char (point-min))
2114 (mime-delete-field "Content-Type")
2115 (mime-delete-field "Content-Transfer-Encoding"))
2116 ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
2117 (mail-position-on-field "Content-Type")
2121 (mail-position-on-field "Content-Transfer-Encoding")
2122 (insert encoding)))))))
2124 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
2125 "Translate single-part-tag to MIME header."
2126 (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
2127 (let* ((beg (match-beginning 0))
2129 (tag (buffer-substring beg end)))
2130 (delete-region beg end)
2131 (let ((contype (mime-edit-get-contype tag))
2132 (encoding (mime-edit-get-encoding tag)))
2133 (insert (concat prefix "--" boundary "\n"))
2135 (narrow-to-region (point)(point))
2136 (insert "Content-Type: " contype "\n")
2138 (insert "Content-Transfer-Encoding: " encoding "\n"))
2139 (eword-encode-header))
2143 (downcase encoding)))))))
2145 (defun mime-edit-translate-region (beg end &optional boundary multipart)
2147 (setq boundary (mime-edit-make-boundary)))
2150 (narrow-to-region beg end)
2151 (let ((tag nil) ;MIME tag
2152 (contype nil) ;Content-Type
2153 (encoding nil) ;Content-Transfer-Encoding
2154 (nparts 0)) ;Number of body parts
2155 ;; Normalize the body part by inserting appropriate message
2156 ;; tags for every message contents.
2157 (mime-edit-normalize-body)
2158 ;; Counting the number of Content-Type.
2159 (goto-char (point-min))
2160 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2161 (setq nparts (1+ nparts)))
2162 ;; Begin translation.
2164 ((and (<= nparts 1)(not multipart))
2165 ;; It's a singular message.
2166 (goto-char (point-min))
2167 (while (re-search-forward
2168 mime-edit-single-part-tag-regexp nil t)
2170 (buffer-substring (match-beginning 0) (match-end 0)))
2171 (delete-region (match-beginning 0) (1+ (match-end 0)))
2172 (setq contype (mime-edit-get-contype tag))
2173 (setq encoding (mime-edit-get-encoding tag))))
2175 ;; It's a multipart message.
2176 (goto-char (point-min))
2177 (let ((prio mime-content-transfer-encoding-priority-list)
2179 (when (setq part-info
2180 (mime-edit-translate-single-part-tag boundary))
2181 (and (setq nprio (member (cdr part-info) prio))
2183 (while (setq part-info
2184 (mime-edit-translate-single-part-tag boundary "\n"))
2185 (and (setq nprio (member (cdr part-info) prio))
2186 (setq prio nprio))))
2187 ;; Define Content-Type as "multipart/mixed".
2189 (concat "multipart/mixed;\n boundary=\"" boundary "\""))
2190 (setq encoding (car prio))
2191 ;; Insert the trailer.
2192 (goto-char (point-max))
2193 (insert "\n--" boundary "--\n"))))
2194 (list contype encoding boundary nparts)))))
2196 (defun mime-edit-normalize-body ()
2197 "Normalize the body part by inserting appropriate message tags."
2198 ;; Insert the first MIME tags if necessary.
2199 (goto-char (point-min))
2200 (if (not (looking-at mime-edit-single-part-tag-regexp))
2201 (insert (mime-make-text-tag) "\n"))
2202 ;; Check each tag, and add new tag or correct it if necessary.
2203 (goto-char (point-min))
2204 (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2205 (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
2206 (contype (mime-edit-get-contype tag))
2207 (charset (mime-get-parameter contype "charset"))
2208 (encoding (mime-edit-get-encoding tag)))
2209 ;; Remove extra whitespaces after the tag.
2210 (if (looking-at "[ \t]+$")
2211 (delete-region (match-beginning 0) (match-end 0)))
2213 (end (mime-edit-content-end)))
2214 (if (= end (point-max))
2217 (mime-edit-force-text-tag mime-edit-beginning-tag-regexp))
2218 (visible-region beg end)
2221 ((mime-test-content-type contype "message")
2222 ;; Content-type "message" should be sent as is.
2224 ((mime-test-content-type contype "text")
2225 ;; Define charset for text if necessary.
2226 (setq charset (if charset
2227 (intern (downcase charset))
2228 (mime-edit-choose-charset)))
2229 (mime-edit-define-charset charset)
2230 (cond ((string-equal contype "text/x-rot13-47-48")
2233 (mule-caesar-region (point) (mime-edit-content-end))))
2234 ((string-equal contype "text/enriched")
2239 (end (mime-edit-content-end)))
2240 ;; Patch for hard newlines
2243 ;; (while (search-forward "\n" end t)
2244 ;; (put-text-property (match-beginning 0)
2247 ;; End patch for hard newlines
2248 (enriched-encode beg end nil)
2250 (if (search-forward "\n\n")
2251 (delete-region beg (match-end 0)))))))
2252 ;; Point is now on current tag.
2253 ;; Define encoding and encode text if necessary.
2254 (or encoding ;Encoding is not specified.
2257 (let ((ret (cdr (assq charset mime-charset-type-list))))
2259 (setq bits (car ret)
2262 conv "quoted-printable")))
2263 (if (<= bits mime-transfer-level)
2264 (mime-encoding-name bits)
2266 (beg (mime-edit-content-beginning)))
2267 (encode-mime-charset-region beg (mime-edit-content-end)
2269 ;; Protect "From " in beginning of line
2271 (narrow-to-region beg (mime-edit-content-end))
2273 (let (case-fold-search)
2274 (if (re-search-forward "^From " nil t)
2276 (if (memq charset '(iso-2022-jp
2281 (replace-match "\e(BFrom ")
2282 (re-search-forward "^From " nil t)))
2283 (setq encoding "quoted-printable"))))))
2284 ;; canonicalize line break code
2285 (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2287 (narrow-to-region beg (mime-edit-content-end))
2289 (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2290 (replace-match "\\1\r\n"))))
2292 (mime-encode-region beg (mime-edit-content-end)
2293 (or encoding "7bit"))
2294 (mime-edit-define-encoding encoding)))
2295 (goto-char (mime-edit-content-end)))
2296 ((null encoding) ;Encoding is not specified.
2297 ;; Application, image, audio, video, and any other
2298 ;; unknown content-type without encoding should be
2300 (let* ((encoding "base64") ;Encode in BASE64 by default.
2301 (beg (mime-edit-content-beginning))
2302 (end (mime-edit-content-end)))
2303 (mime-encode-region beg end encoding)
2304 (mime-edit-define-encoding encoding))
2305 (forward-line 1))))))
2307 (defun mime-delete-field (field)
2308 "Delete header FIELD."
2309 (let ((regexp (format "^%s:[ \t]*" field)))
2310 (goto-char (point-min))
2311 (while (re-search-forward regexp nil t)
2312 (delete-region (match-beginning 0)
2313 (1+ (std11-field-end))))))
2317 ;;; Platform dependent functions
2320 ;; Sun implementations
2322 (defun mime-edit-voice-recorder-for-sun (encoding)
2323 "Record voice in a buffer using Sun audio device,
2324 and insert data encoded as ENCODING."
2325 (message "Start the recording on %s. Type C-g to finish the recording..."
2327 (mime-insert-encoded-file "/dev/audio" encoding))
2330 ;;; @ Other useful commands.
2333 ;; Message forwarding commands as content-type "message/rfc822".
2335 (defun mime-edit-insert-message (&optional message)
2337 (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2338 (if (and inserter (fboundp inserter))
2340 (mime-edit-insert-place
2341 '("message" "rfc822")
2342 (mime-edit-insert-tag "message" "rfc822")
2343 (funcall inserter message)))
2344 (message "Sorry, I don't have message inserter for your MUA."))))
2346 (defun mime-edit-insert-mail (&optional message)
2348 (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2349 (if (and inserter (fboundp inserter))
2351 (mime-edit-insert-place
2352 '("message" "rfc822")
2353 (mime-edit-insert-tag "message" "rfc822")
2354 (funcall inserter message)))
2355 (message "Sorry, I don't have mail inserter for your MUA."))))
2357 (defun mime-edit-inserted-message-filter ()
2360 (let ((header-start (point))
2361 (case-fold-search t)
2364 ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2365 (if (re-search-forward "^$" (mark t))
2366 (narrow-to-region header-start (match-beginning 0)))
2367 (goto-char header-start)
2368 (while (and (re-search-forward
2370 (apply (function regexp-or)
2371 mime-edit-yank-ignored-field-list)
2373 (setq beg (match-beginning 0))
2374 (setq end (1+ (std11-field-end))))
2375 (delete-region beg end))))))
2378 ;;; @ multipart enclosure
2381 (defun mime-edit-enclose-region-internal (type beg end)
2385 (narrow-to-region beg end)
2386 (insert (format "--<<%s>>-{\n" type))
2387 (goto-char (point-max))
2388 (insert (format "--}-<<%s>>\n" type))
2389 (goto-char (point-max)))
2390 (mime-edit-force-text-tag mime-edit-beginning-tag-regexp)))
2392 (defun mime-edit-enclose-quote-region (beg end)
2394 (mime-edit-enclose-region-internal 'quote beg end))
2396 (defun mime-edit-enclose-mixed-region (beg end)
2398 (mime-edit-enclose-region-internal 'mixed beg end))
2400 (defun mime-edit-enclose-parallel-region (beg end)
2402 (mime-edit-enclose-region-internal 'parallel beg end))
2404 (defun mime-edit-enclose-digest-region (beg end)
2406 (mime-edit-enclose-region-internal 'digest beg end))
2408 (defun mime-edit-enclose-alternative-region (beg end)
2410 (mime-edit-enclose-region-internal 'alternative beg end))
2412 (defun mime-edit-enclose-pgp-signed-region (beg end)
2414 (mime-edit-enclose-region-internal 'pgp-signed beg end))
2416 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2418 (mime-edit-enclose-region-internal 'pgp-encrypted beg end))
2420 (defun mime-edit-enclose-kazu-signed-region (beg end)
2422 (mime-edit-enclose-region-internal 'kazu-signed beg end))
2424 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2426 (mime-edit-enclose-region-internal 'kazu-encrypted beg end))
2428 (defun mime-edit-enclose-smime-signed-region (beg end)
2430 (mime-edit-enclose-region-internal 'smime-signed beg end))
2432 (defun mime-edit-enclose-smime-encrypted-region (beg end)
2434 (mime-edit-enclose-region-internal 'smime-encrypted beg end))
2436 (defun mime-edit-insert-key (&optional arg)
2437 "Insert a pgp public key."
2439 (mime-edit-insert-tag "application" "pgp-keys")
2440 (mime-edit-define-encoding "7bit")
2442 (mime-edit-force-text-tag mime-edit-single-part-tag-regexp))
2448 (defun mime-edit-set-split (arg)
2451 (y-or-n-p "Do you want to enable split? ")))
2452 (setq mime-edit-split-message arg)
2454 (message "This message is enabled to split.")
2455 (message "This message is not enabled to split.")))
2457 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2458 "Toggle transfer-level is 7bit or 8bit through.
2460 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2462 (if (numberp transfer-level)
2463 (setq mime-transfer-level transfer-level)
2464 (if (< mime-transfer-level 8)
2465 (setq mime-transfer-level 8)
2466 (setq mime-transfer-level 7)))
2467 (message (format "Current transfer-level is %d bit"
2468 mime-transfer-level))
2469 (setq mime-transfer-level-string
2470 (mime-encoding-name mime-transfer-level 'not-omit))
2471 (force-mode-line-update))
2473 (defun mime-edit-set-transfer-level-7bit ()
2475 (mime-edit-toggle-transfer-level 7))
2477 (defun mime-edit-set-transfer-level-8bit ()
2479 (mime-edit-toggle-transfer-level 8))
2485 (defvar mime-edit-pgp-processing nil)
2486 (make-variable-buffer-local 'mime-edit-pgp-processing)
2488 (defun mime-edit-set-sign (arg)
2491 (y-or-n-p "Do you want to sign? ")))
2494 (or (memq 'sign mime-edit-pgp-processing)
2495 (setq mime-edit-pgp-processing
2496 (nconc mime-edit-pgp-processing
2497 (copy-sequence '(sign)))))
2498 (message "This message will be signed."))
2499 (setq mime-edit-pgp-processing
2500 (delq 'sign mime-edit-pgp-processing))
2501 (message "This message will not be signed.")))
2503 (defun mime-edit-set-encrypt (arg)
2506 (y-or-n-p "Do you want to encrypt? ")))
2509 (or (memq 'encrypt mime-edit-pgp-processing)
2510 (setq mime-edit-pgp-processing
2511 (nconc mime-edit-pgp-processing
2512 (copy-sequence '(encrypt)))))
2513 (message "This message will be encrypt."))
2514 (setq mime-edit-pgp-processing
2515 (delq 'encrypt mime-edit-pgp-processing))
2516 (message "This message will not be encrypt.")))
2518 (defun mime-edit-pgp-enclose-buffer ()
2519 (let ((beg (save-excursion
2520 (goto-char (point-min))
2521 (if (search-forward (concat "\n" mail-header-separator "\n"))
2524 (dolist (pgp-processing mime-edit-pgp-processing)
2525 (case pgp-processing
2527 (mime-edit-enclose-pgp-signed-region
2530 (mime-edit-enclose-pgp-encrypted-region
2531 beg (point-max))))))))
2537 (defun mime-edit-insert-partial-header (fields subject
2538 id number total separator)
2540 (insert (format "Subject: %s (%d/%d)\n" subject number total))
2541 (insert (format "Mime-Version: %s\n" mime-edit-mime-version-value))
2543 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2544 id number total separator)))
2546 (defun mime-edit-split-and-send
2547 (&optional cmd lines mime-edit-message-max-length)
2551 (count-lines (point-min) (point-max))))
2552 (or mime-edit-message-max-length
2553 (setq mime-edit-message-max-length
2554 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2555 mime-edit-message-default-max-lines)))
2557 ;; (mime-edit-draft-file-name
2558 ;; (or (buffer-file-name)
2560 ;; (expand-file-name "mime-draft" temporary-file-directory))))
2561 (separator mail-header-separator)
2563 (replace-space-with-underline (current-time-string))
2564 "@" (system-name) "\"")))
2565 (run-hooks 'mime-edit-before-split-hook)
2566 (let ((the-buf (current-buffer))
2567 (copy-buf (get-buffer-create " *Original Message*"))
2568 (header (std11-header-string-except
2569 mime-edit-split-ignored-field-regexp separator))
2570 (subject (mail-fetch-field "subject"))
2571 (total (+ (/ lines mime-edit-message-max-length)
2572 (if (> (mod lines mime-edit-message-max-length) 0)
2578 mime-edit-split-message-sender-alist))
2582 (error "Split sender is not specified for `%s'." major-mode)))))
2583 (mime-edit-partial-number 1)
2586 (set-buffer copy-buf)
2588 (insert-buffer the-buf)
2590 (if (re-search-forward
2591 (concat "^" (regexp-quote separator) "$") nil t)
2592 (let ((he (match-beginning 0)))
2594 (narrow-to-region (point-min) he)))
2595 (goto-char (point-min))
2596 (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2597 (delete-region (match-beginning 0)
2598 (1+ (std11-field-end))))))
2599 (while (< mime-edit-partial-number total)
2602 (set-buffer copy-buf)
2603 (setq data (buffer-substring
2606 (goto-line mime-edit-message-max-length)
2608 (delete-region (point-min)(point)))
2609 (mime-edit-insert-partial-header
2610 header subject id mime-edit-partial-number total separator)
2613 (message (format "Sending %d/%d..."
2614 mime-edit-partial-number total))
2615 (call-interactively command)
2616 (message (format "Sending %d/%d... done"
2617 mime-edit-partial-number total)))
2618 (setq mime-edit-partial-number
2619 (1+ mime-edit-partial-number)))
2622 (set-buffer copy-buf)
2623 (setq data (buffer-string))
2625 (mime-edit-insert-partial-header
2626 header subject id mime-edit-partial-number total separator)
2629 (message (format "Sending %d/%d..."
2630 mime-edit-partial-number total))
2631 (message (format "Sending %d/%d... done"
2632 mime-edit-partial-number total))))))
2634 (defun mime-edit-maybe-split-and-send (&optional cmd)
2636 (run-hooks 'mime-edit-before-send-hook)
2637 (let ((mime-edit-message-max-length
2638 (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2639 mime-edit-message-default-max-lines))
2640 (lines (count-lines (point-min) (point-max))))
2641 (if (and (> lines mime-edit-message-max-length)
2642 mime-edit-split-message)
2643 (mime-edit-split-and-send cmd lines mime-edit-message-max-length))))
2646 ;;; @ preview message
2649 (defvar mime-edit-buffer nil) ; buffer local variable
2650 (defvar mime-edit-temp-message-buffer nil)
2652 (defun mime-edit-preview-message ()
2653 "preview editing MIME message."
2655 (let* ((str (buffer-string))
2656 (separator mail-header-separator)
2657 (the-buf (current-buffer))
2658 (buf-name (buffer-name))
2659 (temp-buf-name (concat "*temp-article:" buf-name "*"))
2660 (buf (get-buffer temp-buf-name))
2661 (pgp-processing mime-edit-pgp-processing))
2664 (switch-to-buffer buf)
2666 (setq buf (get-buffer-create temp-buf-name))
2667 (switch-to-buffer buf))
2669 (setq major-mode 'mime-temp-message-mode)
2670 (make-local-variable 'mail-header-separator)
2671 (setq mail-header-separator separator)
2672 (make-local-variable 'mime-edit-buffer)
2673 (setq mime-edit-buffer the-buf)
2674 (setq mime-edit-pgp-processing pgp-processing)
2676 (run-hooks 'mime-edit-translate-hook)
2677 (mime-edit-translate-buffer)
2678 (goto-char (point-min))
2679 (if (re-search-forward
2680 (concat "^" (regexp-quote separator) "$"))
2683 (make-local-variable 'mime-edit-temp-message-buffer)
2684 (setq mime-edit-temp-message-buffer buf)))
2686 (defun mime-edit-quitting-method ()
2687 "Quitting method for mime-view."
2688 (let* ((temp mime-edit-temp-message-buffer)
2690 (mime-preview-kill-buffer)
2692 (setq buf mime-edit-buffer)
2694 (switch-to-buffer buf)))
2696 (set-alist 'mime-preview-quitting-method-alist
2697 'mime-temp-message-mode
2698 #'mime-edit-quitting-method)
2704 (defvar mime-edit-again-ignored-field-regexp
2705 (concat "^\\(" "Content-.*\\|Mime-Version"
2706 (if mime-edit-insert-user-agent-field "\\|User-Agent")
2708 "Regexp for deleted header fields when `mime-edit-again' is called.")
2710 (defsubst eliminate-top-spaces (string)
2711 "Eliminate top sequence of space or tab in STRING."
2712 (if (string-match "^[ \t]+" string)
2713 (substring string (match-end 0))
2716 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
2719 (cdr (assoc (mime-content-type-parameter content-type "protocol")
2720 '(("application/pgp-encrypted" . pgp-encrypted)
2721 ("application/pgp-signature" . pgp-signed))))
2722 (mime-content-type-subtype content-type)))
2723 (boundary (mime-content-type-parameter content-type "boundary"))
2724 (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
2725 (re-search-forward boundary-pat nil t)
2726 (let ((bb (match-beginning 0)) eb tag)
2727 (setq tag (format "\n--<<%s>>-{\n" subtype))
2730 (setq bb (+ bb (length tag)))
2732 (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2734 (setq eb (match-beginning 0))
2735 (replace-match (format "--}-<<%s>>\n" subtype))
2737 (narrow-to-region bb eb)
2738 (goto-char (point-min))
2739 (while (re-search-forward boundary-pat nil t)
2740 (let ((beg (match-beginning 0))
2742 (delete-region beg (match-end 0))
2744 (if (re-search-forward boundary-pat nil t)
2745 (setq end (match-beginning 0))
2746 (setq end (point-max)))
2748 (narrow-to-region beg end)
2750 ((eq subtype 'pgp-encrypted)
2753 (goto-char (point-min))
2754 (re-search-forward "^-+BEGIN PGP MESSAGE-+$"
2757 (save-window-excursion
2758 (pgg-decrypt-region (match-beginning 0)
2760 (delete-region (point-min)(point-max))))
2761 (insert-buffer-substring pgg-output-buffer)
2762 (mime-edit-decode-message-in-buffer
2763 nil not-decode-text)
2764 (delete-region (goto-char (point-min))
2765 (if (search-forward "\n\n" nil t)
2768 (goto-char (point-max))))
2770 (mime-edit-decode-message-in-buffer
2771 (if (eq subtype 'digest)
2773 (make-mime-content-type 'message 'rfc822)))
2775 (goto-char (point-max))))))))))
2776 (goto-char (point-min))
2777 (or (= (point-min) 1)
2778 (delete-region (point-min)
2779 (if (search-forward "\n\n" nil t)
2783 (defun mime-edit-decode-single-part-in-buffer
2784 (content-type not-decode-text &optional content-disposition)
2785 (let* ((type (mime-content-type-primary-type content-type))
2786 (subtype (mime-content-type-subtype content-type))
2787 (ctype (format "%s/%s" type subtype))
2789 (pstr (let ((bytes (+ 14 (length ctype))))
2790 (mapconcat (function
2792 (if (string= (car attr) "charset")
2794 (setq charset (cdr attr))
2796 (let* ((str (concat (car attr)
2799 (setq bytes (+ bytes bs 2))
2802 (setq bytes (+ bs 1))
2805 (mime-content-type-parameters content-type) "")))
2808 (limit (save-excursion
2809 (if (search-forward "\n\n" nil t)
2812 (mime-content-disposition-type content-disposition))
2814 (if disposition-type
2815 (let ((bytes (+ 21 (length (format "%s" disposition-type)))))
2816 (mapconcat (function
2821 (if (string-equal "filename"
2823 (std11-wrap-as-quoted-string
2827 (setq bytes (+ bytes bs 2))
2830 (setq bytes (+ bs 1))
2833 (mime-content-disposition-parameters
2834 content-disposition)
2836 (if disposition-type
2837 (setq pstr (format "%s\nContent-Disposition: %s%s"
2838 pstr disposition-type disposition-str)))
2840 (if (re-search-forward
2841 "^Content-Transfer-Encoding:" limit t)
2842 (let ((beg (match-beginning 0))
2843 (hbeg (match-end 0))
2844 (end (std11-field-end limit)))
2847 (eliminate-top-spaces
2848 (std11-unfold-string
2849 (buffer-substring hbeg end)))))
2850 (if (or charset (eq type 'text))
2852 (delete-region beg (1+ end))
2853 (goto-char (point-min))
2854 (if (search-forward "\n\n" nil t)
2857 (match-end 0)(point-max) encoding)
2859 encoding nil))))))))
2860 (if (or encoded (not not-decode-text))
2863 (goto-char (point-min))
2864 (while (re-search-forward "\r\n" nil t)
2865 (replace-match "\n")))
2866 (decode-mime-charset-region (point-min)(point-max)
2867 (or charset default-mime-charset))))
2868 (let ((he (if (re-search-forward "^$" nil t)
2871 (if (and (eq type 'text)
2872 (eq subtype 'x-rot13-47-48))
2873 (mule-caesar-region he (point-max)))
2874 (if (= (point-min) 1)
2880 (format "%s/%s%s" type subtype pstr)
2882 (delete-region (point-min) he)
2884 (mime-create-tag (format "%s/%s%s" type subtype pstr)
2888 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
2891 (goto-char (point-min))
2892 (let ((ctl (or (mime-read-Content-Type)
2893 default-content-type)))
2895 (let ((type (mime-content-type-primary-type ctl)))
2897 ((and (eq type 'application)
2898 (eq (mime-content-type-subtype ctl) 'pgp-signature))
2899 (delete-region (point-min)(point-max)))
2900 ((eq type 'multipart)
2901 (mime-edit-decode-multipart-in-buffer ctl not-decode-text))
2903 (mime-edit-decode-single-part-in-buffer
2904 ctl not-decode-text (mime-read-Content-Disposition)))))
2906 (decode-mime-charset-region (point-min) (point-max)
2907 default-mime-charset)))
2908 (if (= (point-min) 1)
2911 (std11-narrow-to-header)
2912 (goto-char (point-min))
2913 (while (re-search-forward
2914 mime-edit-again-ignored-field-regexp nil t)
2915 (delete-region (match-beginning 0) (1+ (std11-field-end)))))
2916 (mime-decode-header-in-buffer (not not-decode-text)))))))
2919 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
2920 "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
2921 Content-Type and Content-Transfer-Encoding header fields will be
2922 converted to MIME-Edit tags."
2924 (goto-char (point-min))
2926 (concat "\n" (regexp-quote mail-header-separator) "\n")
2928 (replace-match "\n\n"))
2929 (mime-edit-decode-message-in-buffer nil not-decode-text)
2930 (goto-char (point-min))
2932 (and (re-search-forward "^$")
2933 (replace-match mail-header-separator)))
2935 (turn-on-mime-edit)))
2941 (provide 'mime-edit)
2943 (run-hooks 'mime-edit-load-hook)
2945 ;;; mime-edit.el ends here