* mime-pgp.el (mime-pgp-use): New variable.
[elisp/semi.git] / mime-edit.el
1 ;;; mime-edit.el --- Simple MIME Composer for GNU Emacs
2
3 ;; Copyright (C) 1993,94,95,96,97,98,99,2000 Free Software Foundation, Inc.
4
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
11
12 ;; This file is part of SEMI (Sophisticated Emacs MIME Interfaces).
13
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.
18
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.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
28
29 ;;; Commentary:
30
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.
37
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.
46
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:
50 ;;
51 ;; (autoload 'turn-on-mime-edit "mime-edit"
52 ;;           "Minor mode for editing MIME message." t)
53 ;;
54 ;; In case of Mail mode (includes VM mode), you need the following
55 ;; hook definition:
56 ;;
57 ;; (add-hook 'mail-mode-hook 'turn-on-mime-edit)
58 ;; (add-hook 'mail-send-hook 'mime-edit-maybe-translate)
59 ;;
60 ;; In case of MH-E, you need the following hook definition:
61 ;;
62 ;; (add-hook 'mh-letter-mode-hook
63 ;;           (function
64 ;;            (lambda ()
65 ;;              (turn-on-mime-edit)
66 ;;              (make-local-variable 'mail-header-separator)
67 ;;              (setq mail-header-separator "--------")
68 ;;              ))))
69 ;; (add-hook 'mh-before-send-letter-hook 'mime-edit-maybe-translate)
70 ;;
71 ;; In case of News mode, you need the following hook definition:
72 ;;
73 ;; (add-hook 'news-reply-mode-hook 'turn-on-mime-edit)
74 ;; (add-hook 'news-inews-hook 'mime-edit-maybe-translate)
75 ;;
76 ;; In case of Emacs 19, it is possible to emphasize the message tags
77 ;; using font-lock mode as follows:
78 ;;
79 ;; (add-hook 'mime-edit-mode-hook
80 ;;           (function
81 ;;            (lambda ()
82 ;;              (font-lock-mode 1)
83 ;;              (setq font-lock-keywords (list mime-edit-tag-regexp))
84 ;;              ))))
85
86 ;; The message tag looks like:
87 ;;
88 ;;      --[[TYPE/SUBTYPE;PARAMETERS][ENCODING]]
89 ;;
90 ;; The tagged MIME message examples:
91 ;;
92 ;; This is a conventional plain text.  It should be translated into
93 ;; text/plain.
94 ;;
95 ;;--[[text/plain]]
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.
99 ;;
100 ;; ¡Hola!  Buenos días.  ¿Cómo está usted?
101 ;;--[[text/enriched]]
102 ;; <center>This is a richtext.</center>
103 ;;
104 ;;--[[image/gif][base64]]^M...image encoded in base64 comes here...
105 ;;
106 ;;--[[audio/basic][base64]]^M...audio encoded in base64 comes here...
107
108 ;;; Code:
109
110 (require 'sendmail)
111 (require 'mail-utils)
112 (require 'mel)
113 (require 'mime-view)
114 (require 'signature)
115 (require 'alist)
116
117 (defvar mime-edit-pgp-use
118   (condition-case nil
119       (progn
120         (require 'epg-config)
121         (epg-check-configuration (epg-configuration))
122         (autoload 'epg-make-context "epg")
123         'epg)
124     (error
125      (require 'pgg-def)
126      (require 'pgg-parse)
127      (autoload 'pgg-encrypt-region "pgg"
128        "PGP encryption of current region." t)
129      (autoload 'pgg-sign-region "pgg"
130        "PGP signature of current region." t)
131      (autoload 'pgg-insert-key "pgg"
132        "Insert PGP public key at point." t)
133      'pgg))
134   "Which PGG library to be used in MIME-Edit.")
135
136 ;;; @ version
137 ;;;
138
139 (eval-and-compile
140   (defconst mime-edit-version
141     (concat
142      (mime-product-name mime-user-interface-product) " "
143      (mapconcat #'number-to-string
144                 (mime-product-version mime-user-interface-product) ".")
145      " - \"" (mime-product-code-name mime-user-interface-product) "\"")))
146
147
148 ;;; @ variables
149 ;;;
150
151 (defgroup mime-edit nil
152   "MIME edit mode"
153   :group 'mime)
154
155 (defcustom mime-ignore-preceding-spaces nil
156   "*Ignore preceding white spaces if non-nil."
157   :group 'mime-edit
158   :type 'boolean)
159
160 (defcustom mime-ignore-trailing-spaces nil
161   "*Ignore trailing white spaces if non-nil."
162   :group 'mime-edit
163   :type 'boolean)
164
165 (defcustom mime-ignore-same-text-tag t
166   "*Ignore preceding text content-type tag that is same with new one.
167 If non-nil, the text tag is not inserted unless something different."
168   :group 'mime-edit
169   :type 'boolean)
170
171 (defcustom mime-auto-hide-body t
172   "*Hide non-textual body encoded in base64 after insertion if non-nil."
173   :group 'mime-edit
174   :type 'boolean)
175
176 (defcustom mime-edit-voice-recorder
177   (function mime-edit-voice-recorder-for-sun)
178   "*Function to record a voice message and encode it."
179   :group 'mime-edit
180   :type 'function)
181
182 (defcustom mime-edit-mode-hook nil
183   "*Hook called when enter MIME mode."
184   :group 'mime-edit
185   :type 'hook)
186
187 (defcustom mime-edit-translate-hook nil
188   "*Hook called before translating into a MIME compliant message.
189 To insert a signature file automatically, call the function
190 `mime-edit-insert-signature' from this hook."
191   :group 'mime-edit
192   :type 'hook)
193
194 (defcustom mime-edit-exit-hook nil
195   "*Hook called when exit MIME mode."
196   :group 'mime-edit
197   :type 'hook)
198
199 (defvar mime-content-types
200   '(("text"
201      ;; Charset parameter need not to be specified, since it is
202      ;; defined automatically while translation.
203      ("plain"
204       ;;("charset" "" "ISO-2022-JP" "US-ASCII" "ISO-8859-1" "ISO-8859-8")
205       )
206      ("enriched")
207      ("html")
208      ("css") ; rfc2318
209      ("xml") ; rfc2376
210      ("x-latex")
211      ;; ("x-rot13-47-48")
212      )
213     ("message"
214      ("external-body"
215       ("access-type"
216        ("anon-ftp"
217         ("site" "ftp.jaist.ac.jp" "wnoc-fuk.wide.ad.jp" "nic.karrn.ad.jp")
218         ("directory" "/pub/GNU/elisp/mime")
219         ("name")
220         ("mode" "image" "ascii" "local8"))
221        ("ftp"
222         ("site")
223         ("directory")
224         ("name")
225         ("mode" "image" "ascii" "local8"))
226        ("tftp"        ("site") ("name"))
227        ("afs"         ("site") ("name"))
228        ("local-file"  ("site") ("name"))
229        ("mail-server"
230         ("server" "ftpmail@nic.karrn.ad.jp")
231         ("subject"))
232        ("url"         ("url"))))
233      ("rfc822")
234      ("news"))
235     ("application"
236      ("octet-stream" ("type" "" "tar" "shar"))
237      ("postscript")
238      ("pdf")
239      ("msword")
240      ("vnd.ms-excel")
241      ("vnd.ms-powerpoint")
242      ("x-kiss" ("x-cnf")))
243     ("image"
244      ("gif")
245      ("jpeg")
246      ("png")
247      ("tiff")
248      ("x-pic")
249      ("x-mag")
250      ("x-xwd")
251      ("x-xbm"))
252     ("audio" ("basic"))
253     ("video" ("mpeg")))
254   "*Alist of content-type, subtype, parameters and its values.")
255
256 (defcustom mime-file-types
257   '(
258
259     ;; Programming languages
260
261     ("\\.cc$"
262      "application" "octet-stream" (("type" . "C++"))
263      "7bit"
264      "attachment"       (("filename" . file)))
265
266     ("\\.el$"
267      "application" "octet-stream" (("type" . "emacs-lisp"))
268      "7bit"
269      "attachment"       (("filename" . file)))
270
271     ("\\.lsp$"
272      "application" "octet-stream" (("type" . "common-lisp"))
273      "7bit"
274      "attachment"       (("filename" . file)))
275
276     ("\\.pl$"
277      "application" "octet-stream" (("type" . "perl"))
278      "7bit"
279      "attachment"       (("filename" . file)))
280
281     ;; Text or translated text
282
283     ("\\.txt$"
284      "text"     "plain"         nil
285      nil
286      "inline"           (("filename" . file)))
287
288      ;; .rc : procmail modules pm-xxxx.rc
289      ;; *rc : other resource files
290
291     ("\\.\\(rc\\|lst\\|log\\|sql\\|mak\\)$\\|\\..*rc$"
292      "text"     "plain"         nil
293      nil
294      "attachment"       (("filename" . file)))
295
296     ("\\.html$"
297      "text"     "html"          nil
298      nil
299      nil                nil)
300
301     ("\\.diff$\\|\\.patch$"
302      "application" "octet-stream" (("type" . "patch"))
303      nil
304      "attachment"       (("filename" . file)))
305
306     ("\\.signature"
307      "text"     "plain"         nil     nil     nil     nil)
308
309
310     ;;  Octect binary text
311
312     ("\\.doc$"                          ;MS Word
313      "application" "msword" nil
314      "base64"
315      "attachment" (("filename" . file)))
316     ("\\.xls$"                          ; MS Excel
317      "application" "vnd.ms-excel" nil
318      "base64"
319      "attachment" (("filename" . file)))
320     ("\\.ppt$"                          ; MS Power Point
321      "application" "vnd.ms-powerpoint" nil
322      "base64"
323      "attachment" (("filename" . file)))
324
325     ("\\.pln$"
326      "text"     "plain"         nil
327      nil
328      "inline"           (("filename" . file)))
329     ("\\.ps$"
330      "application" "postscript" nil
331      "base64"
332      "attachment"       (("filename" . file)))
333     ("\\.pdf$"
334      "application" "pdf"        nil
335      "base64"
336      "attachment"       (("filename" . file)))
337
338     ;;  Pure binary
339
340     ("\\.jpg$\\|\\.jpeg$"
341      "image"    "jpeg"          nil
342      "base64"
343      "inline"           (("filename" . file)))
344     ("\\.gif$"
345      "image"    "gif"           nil
346      "base64"
347      "inline"           (("filename" . file)))
348     ("\\.png$"
349      "image"    "png"           nil
350      "base64"
351      "inline"           (("filename" . file)))
352     ("\\.tiff$"
353      "image"    "tiff"          nil
354      "base64"
355      "inline"           (("filename" . file)))
356     ("\\.pic$"
357      "image"    "x-pic"         nil
358      "base64"
359      "inline"           (("filename" . file)))
360     ("\\.mag$"
361      "image"    "x-mag"         nil
362      "base64"
363      "inline"           (("filename" . file)))
364     ("\\.xbm$"
365      "image"    "x-xbm"         nil
366      "base64"
367      "inline"           (("filename" . file)))
368     ("\\.xwd$"
369      "image"    "x-xwd"         nil
370      "base64"
371      "inline"           (("filename" . file)))
372     ("\\.au$"
373      "audio"    "basic"         nil
374      "base64"
375      "attachment"               (("filename" . file)))
376     ("\\.mpg$"
377      "video"    "mpeg"          nil
378      "base64"
379      "attachment"       (("filename" . file)))
380     ("\\.tar\\.gz$"
381      "application" "octet-stream" (("type" . "tar+gzip"))
382      "base64"
383      "attachment"       (("filename" . file)))
384     ("\\.tgz$"
385      "application" "octet-stream" (("type" . "tar+gzip"))
386      "base64"
387      "attachment"       (("filename" . file)))
388     ("\\.tar\\.Z$"
389      "application" "octet-stream" (("type" . "tar+compress"))
390      "base64"
391      "attachment"       (("filename" . file)))
392     ("\\.taz$"
393      "application" "octet-stream" (("type" . "tar+compress"))
394      "base64"
395      "attachment"       (("filename" . file)))
396     ("\\.gz$"
397      "application" "octet-stream" (("type" . "gzip"))
398      "base64"
399      "attachment"       (("filename" . file)))
400     ("\\.Z$"
401      "application" "octet-stream" (("type" . "compress"))
402      "base64"
403      "attachment"       (("filename" . file)))
404     ("\\.lzh$"
405      "application" "octet-stream" (("type" . "lha"))
406      "base64"
407      "attachment"       (("filename" . file)))
408     ("\\.zip$"
409      "application" "zip" nil
410      "base64"
411      "attachment"       (("filename" . file)))
412
413     ;; Rest
414
415     (".*"
416      "application" "octet-stream" nil
417      nil
418      "attachment"       (("filename" . file))))
419   "*Alist of file name, types, parameters, and default encoding.
420 If encoding is nil, it is determined from its contents."
421   :type `(repeat
422           (list regexp
423                 ;; primary-type
424                 (choice :tag "Primary-Type"
425                         ,@(nconc (mapcar (lambda (cell)
426                                            (list 'item (car cell)))
427                                          mime-content-types)
428                                  '(string)))
429                 ;; subtype
430                 (choice :tag "Sub-Type"
431                         ,@(nconc
432                            (apply #'nconc
433                                   (mapcar (lambda (cell)
434                                             (mapcar (lambda (cell)
435                                                       (list 'item (car cell)))
436                                                     (cdr cell)))
437                                           mime-content-types))
438                            '(string)))
439                 ;; parameters
440                 (repeat :tag "Parameters of Content-Type field"
441                         (cons string (choice string symbol)))
442                 ;; content-transfer-encoding
443                 (choice :tag "Encoding"
444                         ,@(cons
445                            '(const nil)
446                            (mapcar (lambda (cell)
447                                      (list 'item cell))
448                                    (mime-encoding-list))))
449                 ;; disposition-type
450                 (choice :tag "Disposition-Type"
451                         (item nil)
452                         (item "inline")
453                         (item "attachment")
454                         string)
455                 ;; parameters
456                 (repeat :tag "Parameters of Content-Disposition field"
457                         (cons string (choice string symbol)))))
458   :group 'mime-edit)
459
460
461 ;;; @@ about charset, encoding and transfer-level
462 ;;;
463
464 (defvar mime-charset-type-list
465   '((us-ascii           7 nil)
466     (iso-8859-1         8 "quoted-printable")
467     (iso-8859-2         8 "quoted-printable")
468     (iso-8859-3         8 "quoted-printable")
469     (iso-8859-4         8 "quoted-printable")
470     (iso-8859-5         8 "quoted-printable")
471     (koi8-r             8 "quoted-printable")
472     (iso-8859-7         8 "quoted-printable")
473     (iso-8859-8         8 "quoted-printable")
474     (iso-8859-9         8 "quoted-printable")
475     (iso-8859-14        8 "quoted-printable")
476     (iso-8859-15        8 "quoted-printable")
477     (iso-2022-jp        7 "base64")
478     (iso-2022-jp-3      7 "base64")
479     (iso-2022-kr        7 "base64")
480     (euc-kr             8 "base64")
481     (cn-gb              8 "base64")
482     (gb2312             8 "base64")
483     (cn-big5            8 "base64")
484     (big5               8 "base64")
485     (shift_jis          8 "base64")
486     (tis-620            8 "base64")
487     (iso-2022-jp-2      7 "base64")
488     (iso-2022-int-1     7 "base64")))
489
490 (defvar mime-transfer-level 7
491   "*A number of network transfer level.  It should be bigger than 7.")
492 (make-variable-buffer-local 'mime-transfer-level)
493
494 (defsubst mime-encoding-name (transfer-level &optional not-omit)
495   (cond ((> transfer-level 8) "binary")
496         ((= transfer-level 8) "8bit")
497         (not-omit "7bit")))
498
499 (defvar mime-transfer-level-string
500   (mime-encoding-name mime-transfer-level 'not-omit)
501   "A string formatted version of mime-transfer-level")
502 (make-variable-buffer-local 'mime-transfer-level-string)
503
504 ;;; @@ about content transfer encoding
505
506 (defvar mime-content-transfer-encoding-priority-list
507   '(nil "8bit" "binary"))
508
509 ;;; @@ about message inserting
510 ;;;
511
512 (defvar mime-edit-yank-ignored-field-list
513   '("Received" "Approved" "Path" "Replied" "Status"
514     "Xref" "X-UIDL" "X-Filter" "X-Gnus-.*" "X-VM-.*")
515   "Delete these fields from original message when it is inserted
516 as message/rfc822 part.
517 Each elements are regexp of field-name.")
518
519 (defvar mime-edit-yank-ignored-field-regexp
520   (concat "^"
521           (apply (function regexp-or) mime-edit-yank-ignored-field-list)
522           ":"))
523
524 (defvar mime-edit-message-inserter-alist nil)
525 (defvar mime-edit-mail-inserter-alist nil)
526
527
528 ;;; @@ about message splitting
529 ;;;
530
531 (defcustom mime-edit-split-message t
532   "*Split large message if it is non-nil."
533   :group 'mime-edit
534   :type 'boolean)
535
536 (defcustom mime-edit-message-default-max-lines 1000
537   "*Default maximum lines of a message."
538   :group 'mime-edit
539   :type 'integer)
540
541 (defcustom mime-edit-message-max-lines-alist
542   '((news-reply-mode . 500))
543   "Alist of major-mode vs maximum lines of a message.
544 If it is not specified for a major-mode,
545 `mime-edit-message-default-max-lines' is used."
546   :group 'mime-edit
547   :type 'list)
548
549 (defconst mime-edit-split-ignored-field-regexp
550   "\\(^Content-\\|^Subject:\\|^Mime-Version:\\|^Message-Id:\\)")
551
552 (defcustom mime-edit-split-blind-field-regexp
553   "\\(^[BDFbdf]cc:\\|^cc:[ \t]*$\\)"
554   "*Regular expression to match field-name to be ignored when split sending."
555   :group 'mime-edit
556   :type 'regexp)
557
558 (defvar mime-edit-split-message-sender-alist nil)
559
560 (defvar mime-edit-news-reply-mode-server-running nil)
561
562
563 ;;; @@ about tag
564 ;;;
565
566 (defconst mime-edit-single-part-tag-regexp
567   "--[[][[]\\([^]]*\\)]\\([[]\\([^]]*\\)]\\|\\)]"
568   "*Regexp of MIME tag in the form of [[CONTENT-TYPE][ENCODING]].")
569
570 (defconst mime-edit-quoted-single-part-tag-regexp
571   (concat "- " (substring mime-edit-single-part-tag-regexp 1)))
572
573 (defconst mime-edit-multipart-beginning-regexp "--<<\\([^<>]+\\)>>-{\n")
574
575 (defconst mime-edit-multipart-end-regexp "--}-<<\\([^<>]+\\)>>\n")
576
577 (defconst mime-edit-beginning-tag-regexp
578   (regexp-or mime-edit-single-part-tag-regexp
579              mime-edit-multipart-beginning-regexp))
580
581 (defconst mime-edit-end-tag-regexp
582   (regexp-or mime-edit-single-part-tag-regexp
583              mime-edit-multipart-end-regexp))
584
585 (defconst mime-edit-tag-regexp
586   (regexp-or mime-edit-single-part-tag-regexp
587              mime-edit-multipart-beginning-regexp
588              mime-edit-multipart-end-regexp))
589
590 (defvar mime-tag-format "--[[%s]]"
591   "*Control-string making a MIME tag.")
592
593 (defvar mime-tag-format-with-encoding "--[[%s][%s]]"
594   "*Control-string making a MIME tag with encoding.")
595
596
597 ;;; @@ multipart boundary
598 ;;;
599
600 (defvar mime-multipart-boundary "Multipart"
601   "*Boundary of a multipart message.")
602
603
604 ;;; @@ optional header fields
605 ;;;
606
607 (defvar mime-edit-insert-user-agent-field t
608   "*If non-nil, insert User-Agent header field.")
609
610 (defvar mime-edit-user-agent-value
611   (concat (mime-product-name mime-user-interface-product)
612           "/"
613           (mapconcat #'number-to-string
614                      (mime-product-version mime-user-interface-product) ".")
615           " ("
616           (mime-product-code-name mime-user-interface-product)
617           ") "
618           (mime-product-name mime-library-product)
619           "/"
620           (mapconcat #'number-to-string
621                      (mime-product-version mime-library-product) ".")
622           " ("
623           (mime-product-code-name mime-library-product)
624           ") "
625           (if (fboundp 'apel-version)
626               (concat (apel-version) " "))
627           (if (eq mime-edit-pgp-use 'epg)
628               (concat "EasyPG/" epg-version-number " "))
629           (if (featurep 'xemacs)
630               (concat (cond ((and (featurep 'chise)
631                                   (boundp 'xemacs-chise-version))
632                              (concat "CHISE-MULE/" xemacs-chise-version))
633                             ((featurep 'utf-2000)
634                              (concat "UTF-2000-MULE/" utf-2000-version))
635                             ((featurep 'mule) "MULE"))
636                       " XEmacs"
637                       (if (string-match "^[0-9]+\\(\\.[0-9]+\\)" emacs-version)
638                           (concat
639                            "/"
640                            (substring emacs-version 0 (match-end 0))
641                            (cond ((and (boundp 'xemacs-betaname)
642                                        xemacs-betaname)
643                                   ;; It does not exist in XEmacs
644                                   ;; versions prior to 20.3.
645                                   (concat " " xemacs-betaname))
646                                  ((and (boundp 'emacs-patch-level)
647                                        emacs-patch-level)
648                                   ;; It does not exist in FSF Emacs or in
649                                   ;; XEmacs versions earlier than 21.1.1.
650                                   (format " (patch %d)" emacs-patch-level))
651                                  (t ""))
652                            " (" xemacs-codename ")"
653                            ;; `xemacs-extra-name' has appeared in the
654                            ;; development version of XEmacs 21.5-b8.
655                            (if (and (boundp 'xemacs-extra-name)
656                                     (symbol-value 'xemacs-extra-name))
657                                (concat " " (symbol-value 'xemacs-extra-name))
658                              "")
659                            " ("
660                            system-configuration ")")
661                         " (" emacs-version ")"))
662             (let ((ver (if (string-match "\\.[0-9]+$" emacs-version)
663                            (substring emacs-version 0 (match-beginning 0))
664                          emacs-version)))
665               (if (featurep 'mule)
666                   (if (boundp 'enable-multibyte-characters)
667                       (concat "Emacs/" ver
668                               " (" system-configuration ")"
669                               (if enable-multibyte-characters
670                                   (concat " MULE/" mule-version)
671                                 " (with unibyte mode)")
672                               (if (featurep 'meadow)
673                                   (let ((mver (Meadow-version)))
674                                     (if (string-match "^Meadow-" mver)
675                                         (concat " Meadow/"
676                                                 (substring mver
677                                                            (match-end 0)))))))
678                     (concat "MULE/" mule-version
679                             " (based on Emacs " ver ")"))
680                 (concat "Emacs/" ver " (" system-configuration ")")))))
681   "Body of User-Agent field.
682 If variable `mime-edit-insert-user-agent-field' is not nil, it is
683 inserted into message header.")
684
685 \f
686 ;;; @ constants
687 ;;;
688
689 (defconst mime-tspecials-regexp "[][()<>@,;:\\\"/?.= \t]"
690   "*Specify MIME tspecials.
691 Tspecials means any character that matches with it in header must be quoted.")
692
693 (defconst mime-edit-mime-version-value
694   (concat "1.0 (generated by " mime-edit-version ")")
695   "MIME version number.")
696
697 (defconst mime-edit-mime-version-field-for-message/partial
698   (concat "MIME-Version:"
699           (mime-encode-field-body
700            (concat " 1.0 (split by " mime-edit-version ")\n")
701            "MIME-Version"))
702   "MIME version field for message/partial.")
703
704
705 ;;; @ keymap and menu
706 ;;;
707
708 (defvar mime-edit-mode-flag nil)
709 (make-variable-buffer-local 'mime-edit-mode-flag)
710
711 (defvar mime-edit-mode-entity-prefix "\C-c\C-x"
712   "Keymap prefix for MIME-Edit mode commands to insert entity or set status.")
713 (defvar mime-edit-mode-entity-map (make-sparse-keymap)
714   "Keymap for MIME-Edit mode commands to insert entity or set status.")
715
716 (define-key mime-edit-mode-entity-map "\C-t" 'mime-edit-insert-text)
717 (define-key mime-edit-mode-entity-map "\C-i" 'mime-edit-insert-file)
718 (define-key mime-edit-mode-entity-map "\C-e" 'mime-edit-insert-external)
719 (define-key mime-edit-mode-entity-map "\C-v" 'mime-edit-insert-voice)
720 (define-key mime-edit-mode-entity-map "\C-y" 'mime-edit-insert-message)
721 (define-key mime-edit-mode-entity-map "\C-m" 'mime-edit-insert-mail)
722 (define-key mime-edit-mode-entity-map "\C-w" 'mime-edit-insert-signature)
723 (define-key mime-edit-mode-entity-map "\C-s" 'mime-edit-insert-signature)
724 (define-key mime-edit-mode-entity-map "\C-k" 'mime-edit-insert-key)
725 (define-key mime-edit-mode-entity-map "t"    'mime-edit-insert-tag)
726
727 (define-key mime-edit-mode-entity-map "7" 'mime-edit-set-transfer-level-7bit)
728 (define-key mime-edit-mode-entity-map "8" 'mime-edit-set-transfer-level-8bit)
729 (define-key mime-edit-mode-entity-map "/" 'mime-edit-set-split)
730 (define-key mime-edit-mode-entity-map "s" 'mime-edit-set-sign)
731 (define-key mime-edit-mode-entity-map "v" 'mime-edit-set-sign)
732 (define-key mime-edit-mode-entity-map "e" 'mime-edit-set-encrypt)
733 (define-key mime-edit-mode-entity-map "h" 'mime-edit-set-encrypt)
734 (define-key mime-edit-mode-entity-map "p" 'mime-edit-preview-message)
735 (define-key mime-edit-mode-entity-map "\C-z" 'mime-edit-exit)
736 (define-key mime-edit-mode-entity-map "?" 'mime-edit-help)
737
738 (defvar mime-edit-mode-enclosure-prefix "\C-c\C-m"
739   "Keymap prefix for MIME-Edit mode commands about enclosure.")
740 (defvar mime-edit-mode-enclosure-map (make-sparse-keymap)
741   "Keymap for MIME-Edit mode commands about enclosure.")
742
743 (define-key mime-edit-mode-enclosure-map
744   "\C-a" 'mime-edit-enclose-alternative-region)
745 (define-key mime-edit-mode-enclosure-map
746   "\C-p" 'mime-edit-enclose-parallel-region)
747 (define-key mime-edit-mode-enclosure-map
748   "\C-m" 'mime-edit-enclose-mixed-region)
749 (define-key mime-edit-mode-enclosure-map
750   "\C-d" 'mime-edit-enclose-digest-region)
751 (define-key mime-edit-mode-enclosure-map
752   "\C-s" 'mime-edit-enclose-pgp-signed-region)
753 (define-key mime-edit-mode-enclosure-map
754   "\C-e" 'mime-edit-enclose-pgp-encrypted-region)
755 (define-key mime-edit-mode-enclosure-map
756   "s" 'mime-edit-enclose-smime-signed-region)
757 (define-key mime-edit-mode-enclosure-map
758   "e" 'mime-edit-enclose-smime-encrypted-region)
759 (define-key mime-edit-mode-enclosure-map
760   "\C-q" 'mime-edit-enclose-quote-region)
761
762 (defvar mime-edit-mode-map (make-sparse-keymap)
763   "Keymap for MIME-Edit mode commands.")
764 (define-key mime-edit-mode-map
765   mime-edit-mode-entity-prefix mime-edit-mode-entity-map)
766 (define-key mime-edit-mode-map
767   mime-edit-mode-enclosure-prefix mime-edit-mode-enclosure-map)
768
769 (defconst mime-edit-menu-title "MIME-Edit")
770
771 (defconst mime-edit-menu-list
772   '((mime-help  "Describe MIME editor mode" mime-edit-help)
773     (file       "Insert File"           mime-edit-insert-file)
774     (external   "Insert External"       mime-edit-insert-external)
775     (voice      "Insert Voice"          mime-edit-insert-voice)
776     (message    "Insert Message"        mime-edit-insert-message)
777     (mail       "Insert Mail"           mime-edit-insert-mail)
778     (signature  "Insert Signature"      mime-edit-insert-signature)
779     (text       "Insert Text"           mime-edit-insert-text)
780     (tag        "Insert Tag"            mime-edit-insert-tag)
781     (alternative "Enclose as alternative"
782                  mime-edit-enclose-alternative-region)
783     (parallel   "Enclose as parallel"   mime-edit-enclose-parallel-region)
784     (mixed      "Enclose as serial"     mime-edit-enclose-mixed-region)
785     (digest     "Enclose as digest"     mime-edit-enclose-digest-region)
786     (signed     "Enclose as signed"     mime-edit-enclose-pgp-signed-region)
787     (encrypted  "Enclose as encrypted"  mime-edit-enclose-pgp-encrypted-region)
788     (quote      "Verbatim region"       mime-edit-enclose-quote-region)
789     (key        "Insert Public Key"     mime-edit-insert-key)
790     (split      "Set splitting"         mime-edit-set-split)
791     (sign       "PGP sign"              mime-edit-set-sign)
792     (encrypt    "PGP encrypt"           mime-edit-set-encrypt)
793     (preview    "Preview Message"       mime-edit-preview-message)
794     (level      "Toggle transfer-level" mime-edit-toggle-transfer-level))
795   "MIME-edit menubar entry.")
796
797 (cond ((featurep 'xemacs)
798        ;; modified by Pekka Marjola <pema@iki.fi>
799        ;;       1995/9/5 (c.f. [tm-en:69])
800        (defun mime-edit-define-menu-for-xemacs ()
801          "Define menu for XEmacs."
802          (cond ((featurep 'menubar)
803                 (make-local-variable 'current-menubar)
804                 (set-buffer-menubar current-menubar)
805                 (add-submenu
806                  nil
807                  (cons mime-edit-menu-title
808                        (mapcar (function
809                                 (lambda (item)
810                                   (vector (nth 1 item)(nth 2 item)
811                                           mime-edit-mode-flag)))
812                                mime-edit-menu-list))))))
813
814        ;; modified by Steven L. Baur <steve@miranova.com>
815        ;;       1995/12/6 (c.f. [tm-en:209])
816        (or (boundp 'mime-edit-popup-menu-for-xemacs)
817            (setq mime-edit-popup-menu-for-xemacs
818                  (append '("MIME Commands" "---")
819                          (mapcar (function (lambda (item)
820                                              (vector (nth 1 item)
821                                                      (nth 2 item)
822                                                      t)))
823                                  mime-edit-menu-list)))))
824       ((>= emacs-major-version 19)
825        (define-key mime-edit-mode-map [menu-bar mime-edit]
826          (cons mime-edit-menu-title
827                (make-sparse-keymap mime-edit-menu-title)))
828        (mapcar (function
829                 (lambda (item)
830                   (define-key mime-edit-mode-map
831                     (vector 'menu-bar 'mime-edit (car item))
832                     (cons (nth 1 item)(nth 2 item)))))
833                (reverse mime-edit-menu-list))))
834
835
836 ;;; @ functions
837 ;;;
838
839 (defvar mime-edit-touched-flag nil)
840
841 ;;;###autoload
842 (defun mime-edit-mode ()
843   "MIME minor mode for editing the tagged MIME message.
844
845 In this mode, basically, the message is composed in the tagged MIME
846 format. The message tag looks like:
847
848         --[[text/plain; charset=ISO-2022-JP][7bit]]
849
850 The tag specifies the MIME content type, subtype, optional parameters
851 and transfer encoding of the message following the tag.  Messages
852 without any tag are treated as `text/plain' by default.  Charset and
853 transfer encoding are automatically defined unless explicitly
854 specified.  Binary messages such as audio and image are usually
855 hidden.  The messages in the tagged MIME format are automatically
856 translated into a MIME compliant message when exiting this mode.
857
858 Available charsets depend on Emacs version being used.  The following
859 lists the available charsets of each emacs.
860
861 Without mule:   US-ASCII and ISO-8859-1 (or other charset) are available.
862 With mule:      US-ASCII, ISO-8859-* (except for ISO-8859-5), KOI8-R,
863                 ISO-2022-JP, ISO-2022-JP-2, EUC-KR, CN-GB-2312,
864                 CN-BIG5 and ISO-2022-INT-1 are available.
865
866 ISO-2022-JP-2 and ISO-2022-INT-1 charsets used in mule is expected to
867 be used to represent multilingual text in intermixed manner.  Any
868 languages that has no registered charset are represented as either
869 ISO-2022-JP-2 or ISO-2022-INT-1 in mule.
870
871 If you want to use non-ISO-8859-1 charset in Emacs 19 or XEmacs
872 without mule, please set variable `default-mime-charset'.  This
873 variable must be symbol of which name is a MIME charset.
874
875 If you want to add more charsets in mule, please set variable
876 `charsets-mime-charset-alist'.  This variable must be alist of which
877 key is list of charset and value is symbol of MIME charset.  If name
878 of coding-system is different as MIME charset, please set variable
879 `mime-charset-coding-system-alist'.  This variable must be alist of
880 which key is MIME charset and value is coding-system.
881
882 Following commands are available in addition to major mode commands:
883
884 \[make single part\]
885 \\[mime-edit-insert-text]       insert a text message.
886 \\[mime-edit-insert-file]       insert a (binary) file.
887 \\[mime-edit-insert-external]   insert a reference to external body.
888 \\[mime-edit-insert-voice]      insert a voice message.
889 \\[mime-edit-insert-message]    insert a mail or news message.
890 \\[mime-edit-insert-mail]       insert a mail message.
891 \\[mime-edit-insert-signature]  insert a signature file at end.
892 \\[mime-edit-insert-key]        insert PGP public key.
893 \\[mime-edit-insert-tag]        insert a new MIME tag.
894
895 \[make enclosure (maybe multipart)\]
896 \\[mime-edit-enclose-alternative-region]   enclose as multipart/alternative.
897 \\[mime-edit-enclose-parallel-region]      enclose as multipart/parallel.
898 \\[mime-edit-enclose-mixed-region]         enclose as multipart/mixed.
899 \\[mime-edit-enclose-digest-region]        enclose as multipart/digest.
900 \\[mime-edit-enclose-pgp-signed-region]    enclose as PGP signed.
901 \\[mime-edit-enclose-pgp-encrypted-region] enclose as PGP encrypted.
902 \\[mime-edit-enclose-quote-region]         enclose as verbose mode
903                                            (to avoid to expand tags)
904
905 \[other commands\]
906 \\[mime-edit-set-transfer-level-7bit]   set transfer-level as 7.
907 \\[mime-edit-set-transfer-level-8bit]   set transfer-level as 8.
908 \\[mime-edit-set-split]                 set message splitting mode.
909 \\[mime-edit-set-sign]                  set PGP-sign mode.
910 \\[mime-edit-set-encrypt]               set PGP-encryption mode.
911 \\[mime-edit-preview-message]           preview editing MIME message.
912 \\[mime-edit-exit]                      exit and translate into a MIME
913                                         compliant message.
914 \\[mime-edit-help]                      show this help.
915 \\[mime-edit-maybe-translate]           exit and translate if in MIME mode,
916                                         then split.
917
918 Additional commands are available in some major modes:
919 C-c C-c         exit, translate and run the original command.
920 C-c C-s         exit, translate and run the original command.
921
922 The following is a message example written in the tagged MIME format.
923 TABs at the beginning of the line are not a part of the message:
924
925         This is a conventional plain text.  It should be translated
926         into text/plain.
927         --[[text/plain]]
928         This is also a plain text.  But, it is explicitly specified as
929         is.
930         --[[text/plain; charset=ISO-8859-1]]
931         This is also a plain text.  But charset is specified as
932         iso-8859-1.
933
934         ¡Hola!  Buenos días.  ¿Cómo está usted?
935         --[[text/enriched]]
936         This is a <bold>enriched text</bold>.
937         --[[image/gif][base64]]...image encoded in base64 here...
938         --[[audio/basic][base64]]...audio encoded in base64 here...
939
940 User customizable variables (not documented all of them):
941  mime-edit-prefix
942     Specifies a key prefix for MIME minor mode commands.
943
944  mime-ignore-preceding-spaces
945     Preceding white spaces in a message body are ignored if non-nil.
946
947  mime-ignore-trailing-spaces
948     Trailing white spaces in a message body are ignored if non-nil.
949
950  mime-auto-hide-body
951     Hide a non-textual body message encoded in base64 after insertion
952     if non-nil.
953
954  mime-transfer-level
955     A number of network transfer level.  It should be bigger than 7.
956     If you are in 8bit-through environment, please set 8.
957
958  mime-edit-voice-recorder
959     Specifies a function to record a voice message and encode it.
960     The function `mime-edit-voice-recorder-for-sun' is for Sun
961     SparcStations.
962
963  mime-edit-mode-hook
964     Turning on MIME mode calls the value of mime-edit-mode-hook, if
965     it is non-nil.
966
967  mime-edit-translate-hook
968     The value of mime-edit-translate-hook is called just before translating
969     the tagged MIME format into a MIME compliant message if it is
970     non-nil.  If the hook call the function mime-edit-insert-signature,
971     the signature file will be inserted automatically.
972
973  mime-edit-exit-hook
974     Turning off MIME mode calls the value of mime-edit-exit-hook, if it is
975     non-nil."
976   (interactive)
977   (if mime-edit-mode-flag
978       (mime-edit-exit)
979     (if mime-edit-touched-flag
980         (mime-edit-again)
981       (make-local-variable 'mime-edit-touched-flag)
982       (setq mime-edit-touched-flag t)
983       (turn-on-mime-edit))))
984
985
986 (cond ((featurep 'xemacs)
987        (add-minor-mode 'mime-edit-mode-flag
988                        '((" MIME-Edit "  mime-transfer-level-string))
989                        mime-edit-mode-map
990                        nil
991                        'mime-edit-mode))
992       (t
993        (set-alist 'minor-mode-alist
994                   'mime-edit-mode-flag
995                   '((" MIME-Edit "  mime-transfer-level-string)))
996        (set-alist 'minor-mode-map-alist
997                   'mime-edit-mode-flag
998                   mime-edit-mode-map)))
999
1000
1001 ;;;###autoload
1002 (defun turn-on-mime-edit ()
1003   "Unconditionally turn on MIME-Edit mode."
1004   (interactive)
1005   (if mime-edit-mode-flag
1006       (error "You are already editing a MIME message.")
1007     (setq mime-edit-mode-flag t)
1008
1009     ;; Set transfer level into mode line
1010     ;;
1011     (setq mime-transfer-level-string
1012           (mime-encoding-name mime-transfer-level 'not-omit))
1013     (force-mode-line-update)
1014
1015     ;; Define menu for XEmacs.
1016     (if (featurep 'xemacs)
1017         (mime-edit-define-menu-for-xemacs))
1018
1019     ;; I don't care about saving these.
1020     (setq paragraph-start
1021           (regexp-or mime-edit-single-part-tag-regexp
1022                      paragraph-start))
1023     (setq paragraph-separate
1024           (regexp-or mime-edit-single-part-tag-regexp
1025                      paragraph-separate))
1026     (run-hooks 'mime-edit-mode-hook)
1027     (message
1028      "%s"
1029      (substitute-command-keys
1030       "Type \\[mime-edit-exit] to exit MIME mode, and type \\[mime-edit-help] to get help."))))
1031
1032 ;;;###autoload
1033 (defalias 'edit-mime 'turn-on-mime-edit) ; for convenience
1034
1035
1036 (defun mime-edit-exit (&optional nomime no-error)
1037   "Translate the tagged MIME message into a MIME compliant message.
1038 With no argument encode a message in the buffer into MIME, otherwise
1039 just return to previous mode."
1040   (interactive "P")
1041   (if (not mime-edit-mode-flag)
1042       (if (null no-error)
1043           (error "You aren't editing a MIME message."))
1044     (if (not nomime)
1045         (progn
1046           (run-hooks 'mime-edit-translate-hook)
1047           (mime-edit-translate-buffer)))
1048     ;; Restore previous state.
1049     (setq mime-edit-mode-flag nil)
1050     (if (and (featurep 'xemacs)
1051              (featurep 'menubar))
1052         (delete-menu-item (list mime-edit-menu-title)))
1053     (set-buffer-modified-p (buffer-modified-p))
1054     (run-hooks 'mime-edit-exit-hook)
1055     (message "Exit MIME editor mode.")))
1056
1057 (defun mime-edit-maybe-translate ()
1058   (interactive)
1059   (mime-edit-exit nil t)
1060   (call-interactively 'mime-edit-maybe-split-and-send))
1061
1062 (defun mime-edit-help ()
1063   "Show help message about MIME mode."
1064   (interactive)
1065   (with-output-to-temp-buffer "*Help*"
1066     (princ "MIME editor mode:\n")
1067     (princ (documentation 'mime-edit-mode))
1068     (print-help-return-message)))
1069
1070 (defun mime-edit-insert-text (&optional subtype)
1071   "Insert a text message.
1072 Charset is automatically obtained from the `charsets-mime-charset-alist'.
1073 If optional argument SUBTYPE is not nil, text/SUBTYPE tag is inserted."
1074   (interactive)
1075   (let ((ret (mime-edit-insert-tag "text" subtype nil)))
1076     (when ret
1077       (if (looking-at mime-edit-single-part-tag-regexp)
1078           (progn
1079             ;; Make a space between the following message.
1080             (insert "\n")
1081             (forward-char -1)))
1082       (if (and (member (cadr ret) '("enriched"))
1083                (fboundp 'enriched-mode))
1084           (enriched-mode t)
1085         (if (boundp 'enriched-mode)
1086             (enriched-mode -1))))))
1087
1088 (defun mime-edit-insert-file (file &optional verbose)
1089   "Insert a message from a file."
1090   (interactive "fInsert file as MIME message: \nP")
1091   (let*  ((guess (mime-find-file-type file))
1092           (type (nth 0 guess))
1093           (subtype (nth 1 guess))
1094           (parameters (nth 2 guess))
1095           (encoding (nth 3 guess))
1096           (disposition-type (nth 4 guess))
1097           (disposition-params (nth 5 guess)))
1098     (if verbose
1099         (setq type    (mime-prompt-for-type type)
1100               subtype (mime-prompt-for-subtype type subtype)))
1101     (if (or (interactive-p) verbose)
1102         (setq encoding (mime-prompt-for-encoding encoding)))
1103     (if (or (consp parameters) (stringp disposition-type))
1104         (let ((rest parameters) cell attribute value)
1105           (setq parameters "")
1106           (while rest
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
1116               (progn
1117                 (setq parameters
1118                       (concat parameters "\n"
1119                               "Content-Disposition: " disposition-type))
1120                 (setq rest disposition-params)
1121                 (while rest
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))))
1128                   (setq parameters
1129                         (concat parameters "; " attribute "=" value))
1130                   (setq rest (cdr rest)))))))
1131     (mime-edit-insert-tag type subtype parameters)
1132     (mime-edit-insert-binary-file file encoding)))
1133
1134 (defun mime-edit-insert-external ()
1135   "Insert a reference to external body."
1136   (interactive)
1137   (mime-edit-insert-tag "message" "external-body" nil ";\n\t")
1138   ;;(forward-char -1)
1139   ;;(insert "Content-Description: " (read-string "Content-Description: ") "\n")
1140   ;;(forward-line 1)
1141   (let* ((pritype (mime-prompt-for-type))
1142          (subtype (mime-prompt-for-subtype pritype))
1143          (parameters (mime-prompt-for-parameters pritype subtype ";\n\t")))
1144     (and pritype
1145          subtype
1146          (insert "Content-Type: "
1147                  pritype "/" subtype (or parameters "") "\n")))
1148   (if (and (not (eobp))
1149            (not (looking-at mime-edit-single-part-tag-regexp)))
1150       (insert (mime-make-text-tag) "\n")))
1151
1152 (defun mime-edit-insert-voice ()
1153   "Insert a voice message."
1154   (interactive)
1155   (let ((encoding
1156          (completing-read
1157           "What transfer encoding: "
1158           (mime-encoding-alist) nil t nil)))
1159     (mime-edit-insert-tag "audio" "basic" nil)
1160     (mime-edit-define-encoding encoding)
1161     (save-restriction
1162       (narrow-to-region (point)(point))
1163       (unwind-protect
1164           (funcall mime-edit-voice-recorder encoding)
1165         (progn
1166           (insert "\n")
1167           (add-text-properties
1168            (point-min)(point-max) '(invisible t mime-edit-invisible t))
1169           (goto-char (point-max)))))))
1170
1171 (defun mime-edit-insert-signature (&optional arg)
1172   "Insert a signature file."
1173   (interactive "P")
1174   (let ((signature-insert-hook
1175          (function
1176           (lambda ()
1177             (let ((items (mime-find-file-type signature-file-name)))
1178               (apply (function mime-edit-insert-tag)
1179                      (car items) (cadr items) (list (caddr items))))))))
1180     (insert-signature arg)))
1181
1182 \f
1183 ;; Insert a new tag around a point.
1184
1185 (defun mime-edit-insert-tag (&optional pritype subtype parameters delimiter)
1186   "Insert new MIME tag and return a list of PRITYPE, SUBTYPE, and PARAMETERS.
1187 If nothing is inserted, return nil."
1188   (interactive)
1189   (let ((p (point)))
1190     (mime-edit-goto-tag)
1191     (if (and (re-search-forward mime-edit-tag-regexp nil t)
1192              (< (match-beginning 0) p)
1193              (< p (match-end 0)))
1194         (goto-char (match-beginning 0))
1195       (goto-char p)))
1196   (let ((oldtag nil)
1197         (newtag nil)
1198         (current (point)))
1199     (setq pritype
1200           (or pritype
1201               (mime-prompt-for-type)))
1202     (setq subtype
1203           (or subtype
1204               (mime-prompt-for-subtype pritype)))
1205     (setq parameters
1206           (or parameters
1207               (mime-prompt-for-parameters pritype subtype delimiter)))
1208     ;; Make a new MIME tag.
1209     (setq newtag (mime-make-tag pritype subtype parameters))
1210     ;; Find an current MIME tag.
1211     (setq oldtag
1212           (save-excursion
1213             (if (mime-edit-goto-tag)
1214                 (buffer-substring (match-beginning 0) (match-end 0))
1215               ;; Assume content type is 'text/plan'.
1216               (mime-make-tag "text" "plain"))))
1217     ;; We are only interested in TEXT.
1218     (if (and oldtag
1219              (not (mime-test-content-type
1220                    (mime-edit-get-contype oldtag) "text")))
1221         (setq oldtag nil))
1222     ;; Make a new tag.
1223     (if (or (not oldtag)                ;Not text
1224             (or mime-ignore-same-text-tag
1225                 (not (string-equal oldtag newtag))))
1226         (progn
1227           ;; Mark the beginning of the tag for convenience.
1228           (push-mark (point) 'nomsg)
1229           (insert newtag "\n")
1230           (list pritype subtype parameters) ;New tag is created.
1231           )
1232       ;; Restore previous point.
1233       (goto-char current)
1234       nil                               ;Nothing is created.
1235       )))
1236
1237 (defun mime-edit-insert-binary-file (file &optional encoding)
1238   "Insert binary FILE at point.
1239 Optional argument ENCODING specifies an encoding method such as base64."
1240   (let* ((tagend (1- (point)))          ;End of the tag
1241          (hide-p (and mime-auto-hide-body
1242                       (stringp encoding)
1243                       (not
1244                        (let ((en (downcase encoding)))
1245                          (or (string-equal en "7bit")
1246                              (string-equal en "8bit")
1247                              (string-equal en "binary")))))))
1248     (save-restriction
1249       (narrow-to-region (point)(point))
1250       (mime-insert-encoded-file file encoding)
1251       (if hide-p
1252           (add-text-properties
1253            (point-min)(point-max) '(invisible t mime-edit-invisible t)))
1254       (goto-char (point-max)))
1255     (or hide-p
1256         (looking-at mime-edit-tag-regexp)
1257         (= (point)(point-max))
1258         (mime-edit-insert-tag "text" "plain"))
1259     ;; Define encoding even if it is 7bit.
1260     (if (stringp encoding)
1261         (save-excursion
1262           (goto-char tagend) ; Make sure which line the tag is on.
1263           (mime-edit-define-encoding encoding)))))
1264
1265 \f
1266 ;; Commands work on a current message flagment.
1267
1268 (defun mime-edit-goto-tag ()
1269   "Search for the beginning of the tagged MIME message."
1270   (let ((current (point)))
1271     (if (looking-at mime-edit-tag-regexp)
1272         t
1273       ;; At first, go to the end.
1274       (cond ((re-search-forward mime-edit-beginning-tag-regexp nil t)
1275              (goto-char (1- (match-beginning 0))) ;For multiline tag
1276              )
1277             (t
1278              (goto-char (point-max))))
1279       ;; Then search for the beginning.
1280       (re-search-backward mime-edit-end-tag-regexp nil t)
1281       (or (looking-at mime-edit-beginning-tag-regexp)
1282           ;; Restore previous point.
1283           (progn
1284             (goto-char current)
1285             nil)))))
1286
1287 (defun mime-edit-content-beginning ()
1288   "Return the point of the beginning of content."
1289   (save-excursion
1290     (let ((beg (save-excursion
1291                  (beginning-of-line) (point))))
1292       (if (mime-edit-goto-tag)
1293           (let ((top (point)))
1294             (goto-char (match-end 0))
1295             (if (and (= beg top)
1296                      (= (following-char) ?\^M))
1297                 (point)
1298               (forward-line 1)
1299               (point)))
1300         ;; Default text/plain tag.
1301         (goto-char (point-min))
1302         (re-search-forward
1303          (concat "\n" (regexp-quote mail-header-separator)
1304                  (if mime-ignore-preceding-spaces
1305                      "[ \t\n]*\n" "\n")) nil 'move)
1306         (point)))))
1307
1308 (defun mime-edit-content-end ()
1309   "Return the point of the end of content."
1310   (save-excursion
1311     (if (mime-edit-goto-tag)
1312         (progn
1313           (goto-char (1+ (match-end 0)))
1314           (if (get-text-property (point) 'mime-edit-invisible)
1315               (or (next-single-property-change (point) 'mime-edit-invisible)
1316                   (point-max))
1317             ;; Move to the end of this text.
1318             (if (re-search-forward mime-edit-tag-regexp nil 'move)
1319                 ;; Don't forget a multiline tag.
1320                 (goto-char (match-beginning 0)))
1321             (point)))
1322       ;; Assume the message begins with text/plain.
1323       (goto-char (mime-edit-content-beginning))
1324       (if (re-search-forward mime-edit-tag-regexp nil 'move)
1325           ;; Don't forget a multiline tag.
1326           (goto-char (match-beginning 0)))
1327       (point))))
1328
1329 (defun mime-edit-define-charset (charset)
1330   "Set charset of current tag to CHARSET."
1331   (save-excursion
1332     (if (mime-edit-goto-tag)
1333         (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1334           (delete-region (match-beginning 0) (match-end 0))
1335           (insert
1336            (mime-create-tag
1337             (mime-edit-set-parameter
1338              (mime-edit-get-contype tag)
1339              "charset"
1340              (let ((comment (get charset 'mime-charset-comment)))
1341                (if comment
1342                    (concat (upcase (symbol-name charset)) " (" comment ")")
1343                  (upcase (symbol-name charset)))))
1344             (mime-edit-get-encoding tag)))))))
1345
1346 (defun mime-edit-define-encoding (encoding)
1347   "Set encoding of current tag to ENCODING."
1348   (save-excursion
1349     (if (mime-edit-goto-tag)
1350         (let ((tag (buffer-substring (match-beginning 0) (match-end 0))))
1351           (delete-region (match-beginning 0) (match-end 0))
1352           (insert (mime-create-tag (mime-edit-get-contype tag) encoding))))))
1353
1354 (defun mime-edit-choose-charset ()
1355   "Choose charset of a text following current point."
1356   (detect-mime-charset-region (point) (mime-edit-content-end)))
1357
1358 (defun mime-make-text-tag (&optional subtype)
1359   "Make a tag for a text after current point.
1360 Subtype of text type can be specified by an optional argument SUBTYPE.
1361 Otherwise, it is obtained from mime-content-types."
1362   (let* ((pritype "text")
1363          (subtype (or subtype
1364                       (car (car (cdr (assoc pritype mime-content-types)))))))
1365     ;; Charset should be defined later.
1366     (mime-make-tag pritype subtype)))
1367
1368 \f
1369 ;; Tag handling functions
1370
1371 (defun mime-make-tag (pritype subtype &optional parameters encoding)
1372   "Make a tag of MIME message of PRITYPE, SUBTYPE and optional PARAMETERS."
1373   (mime-create-tag (concat (or pritype "") "/" (or subtype "")
1374                            (or parameters ""))
1375                    encoding))
1376
1377 (defun mime-create-tag (contype &optional encoding)
1378   "Make a tag with CONTENT-TYPE and optional ENCODING."
1379   (format (if encoding mime-tag-format-with-encoding mime-tag-format)
1380           contype encoding))
1381
1382 (defun mime-edit-get-contype (tag)
1383   "Return Content-Type (including parameters) of TAG."
1384   (and (stringp tag)
1385        (or (string-match mime-edit-single-part-tag-regexp tag)
1386            (string-match mime-edit-multipart-beginning-regexp tag)
1387            (string-match mime-edit-multipart-end-regexp tag))
1388        (substring tag (match-beginning 1) (match-end 1))))
1389
1390 (defun mime-edit-get-encoding (tag)
1391   "Return encoding of TAG."
1392   (and (stringp tag)
1393        (string-match mime-edit-single-part-tag-regexp tag)
1394        (match-beginning 3)
1395        (not (= (match-beginning 3) (match-end 3)))
1396        (substring tag (match-beginning 3) (match-end 3))))
1397
1398 (defun mime-get-parameter (contype parameter)
1399   "For given CONTYPE return value for PARAMETER.
1400 Nil if no such parameter."
1401   (if (string-match
1402        (concat
1403         ";[ \t\n]*"
1404         (regexp-quote parameter)
1405         "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\([ \t\n]*;\\|$\\)")
1406        contype)
1407       (substring contype (match-beginning 1) (match-end 1))
1408     nil                                 ;No such parameter
1409     ))
1410
1411 (defun mime-edit-set-parameter (contype parameter value)
1412   "For given CONTYPE set PARAMETER to VALUE."
1413   (let (ctype opt-fields)
1414     (if (string-match "\n[^ \t\n\r]+:" contype)
1415         (setq ctype (substring contype 0 (match-beginning 0))
1416               opt-fields (substring contype (match-beginning 0)))
1417       (setq ctype contype))
1418     (if (string-match
1419          (concat
1420           ";[ \t\n]*\\("
1421           (regexp-quote parameter)
1422           "[ \t\n]*=[ \t\n]*\\([^\" \t\n;]*\\|\"[^\"]*\"\\)\\)[ \t\n]*\\(;\\|$\\)")
1423          ctype)
1424         ;; Change value
1425         (concat (substring ctype 0 (match-beginning 1))
1426                 parameter "=" value
1427                 (substring ctype (match-end 1))
1428                 opt-fields)
1429       (concat ctype "; " parameter "=" value opt-fields)
1430       )))
1431
1432 (defun mime-strip-parameters (contype)
1433   "Return primary content-type and subtype without parameters for CONTYPE."
1434   (if (string-match "^[ \t]*\\([^; \t\n]*\\)" contype)
1435       (substring contype (match-beginning 1) (match-end 1)) nil))
1436
1437 (defun mime-test-content-type (contype type &optional subtype)
1438   "Test if CONTYPE is a TYPE and an optional SUBTYPE."
1439   (and (stringp contype)
1440        (stringp type)
1441        (string-match
1442         (concat "^[ \t]*" (downcase type) "/" (downcase (or subtype "")))
1443         (downcase contype))))
1444
1445 \f
1446 ;; Basic functions
1447
1448 (defun mime-find-file-type (file)
1449   "Guess Content-Type, subtype, and parameters from FILE."
1450   (let ((guess nil)
1451         (guesses mime-file-types))
1452     (while (and (not guess) guesses)
1453       (if (string-match (car (car guesses)) file)
1454           (setq guess (cdr (car guesses))))
1455       (setq guesses (cdr guesses)))
1456     guess))
1457
1458 (defun mime-prompt-for-type (&optional default)
1459   "Ask for Content-type."
1460   (let ((type ""))
1461     ;; Repeat until primary content type is specified.
1462     (while (string-equal type "")
1463       (setq type
1464             (completing-read "What content type: "
1465                              mime-content-types
1466                              nil
1467                              'require-match ;Type must be specified.
1468                              default))
1469       (if (string-equal type "")
1470           (progn
1471             (message "Content type is required.")
1472             (beep)
1473             (sit-for 1))))
1474     type))
1475
1476 (defun mime-prompt-for-subtype (type &optional default)
1477   "Ask for subtype of media-type TYPE."
1478   (let ((subtypes (cdr (assoc type mime-content-types))))
1479     (or (and default
1480              (assoc default subtypes))
1481         (setq default (car (car subtypes)))))
1482   (let* ((answer
1483           (completing-read
1484            (if default
1485                (concat
1486                 "What content subtype: (default " default ") ")
1487              "What content subtype: ")
1488            (cdr (assoc type mime-content-types))
1489            nil
1490            'require-match               ;Subtype must be specified.
1491            nil)))
1492     (if (string-equal answer "") default answer)))
1493
1494 (defun mime-prompt-for-parameters (pritype subtype &optional delimiter)
1495   "Ask for Content-type parameters of Content-Type PRITYPE and SUBTYPE.
1496 Optional DELIMITER specifies parameter delimiter (';' by default)."
1497   (let* ((delimiter (or delimiter "; "))
1498          (parameters
1499           (mapconcat
1500            (function identity)
1501            (delq nil
1502                  (mime-prompt-for-parameters-1
1503                   (cdr (assoc subtype
1504                               (cdr (assoc pritype mime-content-types))))))
1505            delimiter)))
1506     (if (and (stringp parameters)
1507              (not (string-equal parameters "")))
1508         (concat delimiter parameters)
1509       ""                                ;"" if no parameters
1510       )))
1511
1512 (defun mime-prompt-for-parameters-1 (optlist)
1513   (apply (function append)
1514          (mapcar (function mime-prompt-for-parameter) optlist)))
1515
1516 (defun mime-prompt-for-parameter (parameter)
1517   "Ask for PARAMETER.
1518 Parameter must be '(PROMPT CHOICE1 (CHOICE2...))."
1519   (let* ((prompt (car parameter))
1520          (choices (mapcar (function
1521                            (lambda (e)
1522                              (if (consp e) e (list e))))
1523                           (cdr parameter)))
1524          (default (car (car choices)))
1525          (answer nil))
1526     (if choices
1527         (progn
1528           (setq answer
1529                 (completing-read
1530                  (concat "What " prompt
1531                          ": (default "
1532                          (if (string-equal default "") "\"\"" default)
1533                          ") ")
1534                  choices nil nil ""))
1535           ;; If nothing is selected, use default.
1536           (if (string-equal answer "")
1537               (setq answer default)))
1538       (setq answer
1539             (read-string (concat "What " prompt ": "))))
1540     (cons (if (and answer
1541                    (not (string-equal answer "")))
1542               (concat prompt "="
1543                       ;; Note: control characters ignored!
1544                       (if (string-match mime-tspecials-regexp answer)
1545                           (concat "\"" answer "\"") answer)))
1546           (mime-prompt-for-parameters-1 (cdr (assoc answer (cdr parameter)))))))
1547
1548 (defun mime-prompt-for-encoding (default)
1549   "Ask for Content-Transfer-Encoding."
1550   (let (encoding)
1551     (while (string=
1552             (setq encoding
1553                   (completing-read
1554                    "What transfer encoding: "
1555                    (mime-encoding-alist) nil t default))
1556             ""))
1557     encoding))
1558
1559 \f
1560 ;;; @ Translate the tagged MIME messages into a MIME compliant message.
1561 ;;;
1562
1563 (defvar mime-edit-translate-buffer-hook
1564   '(mime-edit-pgp-enclose-buffer
1565     mime-edit-translate-body
1566     mime-edit-translate-header))
1567
1568 (defun mime-edit-translate-header ()
1569   "Encode the message header into network representation."
1570   (mime-encode-header-in-buffer 'code-conversion)
1571   (run-hooks 'mime-edit-translate-header-hook))
1572
1573 (defun mime-edit-translate-buffer ()
1574   "Encode the tagged MIME message in current buffer in MIME compliant message."
1575   (interactive)
1576   (undo-boundary)
1577   (if (catch 'mime-edit-error
1578         (save-excursion
1579           (run-hooks 'mime-edit-translate-buffer-hook)))
1580       (progn
1581         (undo)
1582         (error "Translation error!"))))
1583
1584 (defun mime-edit-find-inmost ()
1585   (goto-char (point-min))
1586   (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1587       (let ((bb (match-beginning 0))
1588             (be (match-end 0))
1589             (type (buffer-substring (match-beginning 1)(match-end 1)))
1590             end-exp eb)
1591         (setq end-exp (format "--}-<<%s>>\n" type))
1592         (widen)
1593         (if (re-search-forward end-exp nil t)
1594             (setq eb (match-beginning 0))
1595           (setq eb (point-max)))
1596         (narrow-to-region be eb)
1597         (goto-char be)
1598         (if (re-search-forward mime-edit-multipart-beginning-regexp nil t)
1599             (progn
1600               (narrow-to-region (match-beginning 0)(point-max))
1601               (mime-edit-find-inmost))
1602           (widen)
1603           (list type bb be eb)))))
1604
1605 (defun mime-edit-process-multipart-1 (boundary)
1606   (let ((ret (mime-edit-find-inmost)))
1607     (if ret
1608         (let ((type (car ret))
1609               (bb (nth 1 ret))(be (nth 2 ret))
1610               (eb (nth 3 ret)))
1611           (narrow-to-region bb eb)
1612           (delete-region bb be)
1613           (setq bb (point-min))
1614           (setq eb (point-max))
1615           (widen)
1616           (goto-char eb)
1617           (if (looking-at mime-edit-multipart-end-regexp)
1618               (let ((beg (match-beginning 0))
1619                     (end (match-end 0)))
1620                 (delete-region beg end)
1621                 (or (looking-at mime-edit-beginning-tag-regexp)
1622                     (eobp)
1623                     (insert (concat (mime-make-text-tag) "\n")))))
1624           (cond ((string-equal type "quote")
1625                  (mime-edit-enquote-region bb eb))
1626                 ((string-equal type "pgp-signed")
1627                  (if (eq mime-edit-pgp-use 'epg)
1628                      (mime-edit-sign-pgp-mime-with-epg bb eb boundary)
1629                    (mime-edit-sign-pgp-mime-with-pgg bb eb boundary)))
1630                 ((string-equal type "pgp-encrypted")
1631                  (if (eq mime-edit-pgp-use 'epg)
1632                      (mime-edit-encrypt-pgp-mime-with-epg bb eb boundary)
1633                    (mime-edit-encrypt-pgp-mime-with-pgg bb eb boundary)))
1634                 ((string-equal type "kazu-signed")
1635                  (if (eq mime-edit-pgp-use 'epg)
1636                      (mime-edit-sign-pgp-kazu-with-epg bb eb boundary)
1637                    (mime-edit-sign-pgp-kazu-with-pgg bb eb boundary)))
1638                 ((string-equal type "kazu-encrypted")
1639                  (if (eq mime-edit-pgp-use 'epg)
1640                      (mime-edit-encrypt-pgp-kazu-with-epg bb eb boundary)
1641                    (mime-edit-encrypt-pgp-kazu-with-pgg bb eb boundary)))
1642                 ((string-equal type "smime-signed")
1643                  (mime-edit-sign-smime bb eb boundary))
1644                 ((string-equal type "smime-encrypted")
1645                  (mime-edit-encrypt-smime bb eb boundary))
1646                 (t
1647                  (setq boundary
1648                        (nth 2 (mime-edit-translate-region bb eb
1649                                                             boundary t)))
1650                  (goto-char bb)
1651                  (insert
1652                   (format "--[[multipart/%s;
1653  boundary=\"%s\"][7bit]]\n"
1654                           type boundary))))
1655           boundary))))
1656
1657 (defun mime-edit-enquote-region (beg end)
1658   (save-excursion
1659     (save-restriction
1660       (narrow-to-region beg end)
1661       (goto-char beg)
1662       (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
1663         (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1664           (replace-match (concat "- " (substring tag 1))))))))
1665
1666 (defun mime-edit-dequote-region (beg end)
1667   (save-excursion
1668     (save-restriction
1669       (narrow-to-region beg end)
1670       (goto-char beg)
1671       (while (re-search-forward
1672               mime-edit-quoted-single-part-tag-regexp nil t)
1673         (let ((tag (buffer-substring (match-beginning 0)(match-end 0))))
1674           (replace-match (concat "-" (substring tag 2))))))))
1675
1676 (defvar mime-edit-pgp-user-id nil)
1677
1678 (defun mime-edit-delete-trailing-whitespace ()
1679   (save-match-data
1680     (save-excursion
1681       (goto-char (point-min))
1682       (while (re-search-forward "[ \t]+$" nil t)
1683         (delete-region (match-beginning 0) (match-end 0))))))
1684
1685 (defun mime-edit-sign-pgp-mime-with-epg (beg end boundary)
1686   (save-excursion
1687     (save-restriction
1688       (let* ((from (std11-field-body "From" mail-header-separator))
1689              (ret (progn 
1690                     (narrow-to-region beg end)
1691                     (mime-edit-translate-region beg end boundary)))
1692              (ctype    (car ret))
1693              (encoding (nth 1 ret))
1694              (pgp-boundary (concat "pgp-sign-" boundary))
1695              (context (epg-make-context))
1696              signature micalg)
1697         (mime-edit-delete-trailing-whitespace) ; RFC3156
1698         (goto-char beg)
1699         (insert (format "Content-Type: %s\n" ctype))
1700         (if encoding
1701             (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1702         (insert "\n")
1703         (epg-context-set-armor context t)
1704         (epg-context-set-textmode context t)
1705         (epg-context-set-signers
1706          context
1707          (epa-select-keys
1708           context
1709           "\
1710 Select keys for signing.
1711 If no one is selected, default secret key is used.  "
1712           (if from 
1713               (list (nth 1 (std11-extract-address-components from))))
1714           t))
1715         (condition-case error
1716             (setq signature
1717                   (epg-sign-string context
1718                                    (buffer-substring (point-min) (point-max))
1719                                    'detached))
1720           (error (signal 'mime-edit-error (cdr error))))
1721         (setq micalg (epg-new-signature-digest-algorithm
1722                       (car (epg-context-result-for context 'sign))))
1723         (goto-char beg)
1724         (insert (format "--[[multipart/signed;
1725  boundary=\"%s\"%s;
1726  protocol=\"application/pgp-signature\"][7bit]]
1727 --%s
1728 "
1729                         pgp-boundary
1730                         (if micalg
1731                             (concat "; micalg=pgp-"
1732                                     (downcase
1733                                      (cdr (assq micalg
1734                                                 epg-digest-algorithm-alist))))
1735                           "")
1736                         pgp-boundary))
1737         (goto-char (point-max))
1738         (insert (format "\n--%s
1739 Content-Type: application/pgp-signature
1740 Content-Transfer-Encoding: 7bit
1741 Content-Description: OpenPGP Digital Signature
1742
1743 " pgp-boundary))
1744         (insert signature)
1745         (goto-char (point-max))
1746         (insert (format "\n--%s--\n" pgp-boundary))))))
1747
1748 (defun mime-edit-sign-pgp-mime-with-pgg (beg end boundary)
1749   (save-excursion
1750     (save-restriction
1751       (let* ((from (std11-field-body "From" mail-header-separator))
1752              (ret (progn 
1753                     (narrow-to-region beg end)
1754                     (mime-edit-translate-region beg end boundary)))
1755              (ctype    (car ret))
1756              (encoding (nth 1 ret))
1757              (pgp-boundary (concat "pgp-sign-" boundary))
1758              micalg)
1759         (mime-edit-delete-trailing-whitespace) ; RFC3156
1760         (goto-char beg)
1761         (insert (format "Content-Type: %s\n" ctype))
1762         (if encoding
1763             (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1764         (insert "\n")
1765         (or (let ((pgg-default-user-id 
1766                    (or mime-edit-pgp-user-id
1767                        (if from 
1768                            (nth 1 (std11-extract-address-components from))
1769                          pgg-default-user-id))))
1770               (pgg-sign-region (point-min)(point-max)))
1771             (throw 'mime-edit-error 'pgp-error))
1772         (setq micalg
1773               (cdr (assq 'hash-algorithm
1774                          (cdar (with-current-buffer pgg-output-buffer
1775                                  (pgg-parse-armor-region 
1776                                   (point-min)(point-max))))))
1777               micalg 
1778               (if micalg
1779                   (concat "; micalg=pgp-" (downcase (symbol-name micalg)))
1780                 ""))
1781         (goto-char beg)
1782         (insert (format "--[[multipart/signed;
1783  boundary=\"%s\"%s;
1784  protocol=\"application/pgp-signature\"][7bit]]
1785 --%s
1786 " pgp-boundary micalg pgp-boundary))
1787         (goto-char (point-max))
1788         (insert (format "\n--%s
1789 Content-Type: application/pgp-signature
1790 Content-Transfer-Encoding: 7bit
1791
1792 " pgp-boundary))
1793         (insert-buffer-substring pgg-output-buffer)
1794         (goto-char (point-max))
1795         (insert (format "\n--%s--\n" pgp-boundary))))))
1796
1797 (defvar mime-edit-encrypt-recipient-fields-list '("To" "cc"))
1798
1799 (defun mime-edit-make-encrypt-recipient-header ()
1800   (let* ((names mime-edit-encrypt-recipient-fields-list)
1801          (values
1802           (std11-field-bodies (cons "From" names)
1803                               nil mail-header-separator))
1804          (from (prog1
1805                    (car values)
1806                  (setq values (cdr values))))
1807          (header (and (stringp from)
1808                       (if (string-equal from "")
1809                           ""
1810                         (format "From: %s\n" from))))
1811          recipients)
1812     (while (and names values)
1813       (let ((name (car names))
1814             (value (car values)))
1815         (and (stringp value)
1816              (or (string-equal value "")
1817                  (progn
1818                    (setq header (concat header name ": " value "\n")
1819                          recipients (if recipients
1820                                         (concat recipients " ," value)
1821                                       value))))))
1822       (setq names (cdr names)
1823             values (cdr values)))
1824     (vector from recipients header)))
1825
1826 (defun mime-edit-encrypt-pgp-mime-with-epg (beg end boundary)
1827   (save-excursion
1828     (save-restriction
1829       (let (recipients header)
1830         (let ((ret (mime-edit-make-encrypt-recipient-header)))
1831           (setq recipients (aref ret 1)
1832                 header (aref ret 2)))
1833         (narrow-to-region beg end)
1834         (let* ((ret
1835                 (mime-edit-translate-region beg end boundary))
1836                (ctype    (car ret))
1837                (encoding (nth 1 ret))
1838                (pgp-boundary (concat "pgp-" boundary))
1839                (context (epg-make-context))
1840                cipher)
1841           (goto-char beg)
1842           (insert header)
1843           (insert (format "Content-Type: %s\n" ctype))
1844           (if encoding
1845               (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1846           (insert "\n")
1847           (mime-encode-header-in-buffer)
1848           (epg-context-set-armor context t)
1849           (condition-case error
1850               (setq cipher
1851                     (epg-encrypt-string
1852                      context
1853                      (buffer-substring (point-min) (point-max))
1854                      (epa-select-keys
1855                       context
1856                       "\
1857 Select recipents for encryption.
1858 If no one is selected, symmetric encryption will be performed.  "
1859                       (mapcar (lambda (recipient)
1860                                 (nth 1 (std11-extract-address-components
1861                                         recipient)))
1862                               (split-string recipients 
1863                                             "\\([ \t\n]*,[ \t\n]*\\)+")))))
1864             (error (signal 'mime-edit-error (cdr error))))
1865           (delete-region (point-min)(point-max))
1866           (goto-char beg)
1867           (insert (format "--[[multipart/encrypted;
1868  boundary=\"%s\";
1869  protocol=\"application/pgp-encrypted\"][7bit]]
1870 --%s
1871 Content-Type: application/pgp-encrypted
1872
1873 --%s
1874 Content-Type: application/octet-stream
1875 Content-Transfer-Encoding: 7bit
1876
1877 " pgp-boundary pgp-boundary pgp-boundary))
1878           (insert cipher)
1879           (goto-char (point-max))
1880           (insert (format "\n--%s--\n" pgp-boundary)))))))
1881
1882 (defun mime-edit-encrypt-pgp-mime-with-pgg (beg end boundary)
1883   (save-excursion
1884     (save-restriction
1885       (let (from recipients header)
1886         (let ((ret (mime-edit-make-encrypt-recipient-header)))
1887           (setq from (aref ret 0)
1888                 recipients (aref ret 1)
1889                 header (aref ret 2)))
1890         (narrow-to-region beg end)
1891         (let* ((ret
1892                 (mime-edit-translate-region beg end boundary))
1893                (ctype    (car ret))
1894                (encoding (nth 1 ret))
1895                (pgp-boundary (concat "pgp-" boundary)))
1896           (goto-char beg)
1897           (insert header)
1898           (insert (format "Content-Type: %s\n" ctype))
1899           (if encoding
1900               (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1901           (insert "\n")
1902           (mime-encode-header-in-buffer)
1903           (or (let ((pgg-default-user-id 
1904                      (or mime-edit-pgp-user-id
1905                          (if from 
1906                              (nth 1 (std11-extract-address-components from))
1907                            pgg-default-user-id))))                   
1908                 (pgg-encrypt-region 
1909                  (point-min) (point-max) 
1910                  (mapcar (lambda (recipient)
1911                            (nth 1 (std11-extract-address-components
1912                                    recipient)))
1913                          (split-string recipients 
1914                                        "\\([ \t\n]*,[ \t\n]*\\)+"))))
1915               (throw 'mime-edit-error 'pgp-error))
1916           (delete-region (point-min)(point-max))
1917           (goto-char beg)
1918           (insert (format "--[[multipart/encrypted;
1919  boundary=\"%s\";
1920  protocol=\"application/pgp-encrypted\"][7bit]]
1921 --%s
1922 Content-Type: application/pgp-encrypted
1923
1924 --%s
1925 Content-Type: application/octet-stream
1926 Content-Transfer-Encoding: 7bit
1927
1928 " pgp-boundary pgp-boundary pgp-boundary))
1929           (insert-buffer-substring pgg-output-buffer)
1930           (goto-char (point-max))
1931           (insert (format "\n--%s--\n" pgp-boundary)))))))
1932
1933 (defun mime-edit-sign-pgp-kazu-with-epg (beg end boundary)
1934   (save-excursion
1935     (save-restriction
1936       (narrow-to-region beg end)
1937       (let* ((ret
1938               (mime-edit-translate-region beg end boundary))
1939              (ctype    (car ret))
1940              (encoding (nth 1 ret))
1941              (context (epg-make-context))
1942              signature)
1943         (goto-char beg)
1944         (insert (format "Content-Type: %s\n" ctype))
1945         (if encoding
1946             (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1947         (insert "\n")
1948         (condition-case error
1949             (setq signature
1950                   (epg-sign-string context
1951                                    (buffer-substring beg (point-max))
1952                                    'clearsign))
1953           (error (signal 'mime-edit-error (cdr error))))
1954         (goto-char beg)
1955         (insert
1956          "--[[application/pgp; format=mime][7bit]]\n" signature)
1957         ))))
1958
1959 (defun mime-edit-sign-pgp-kazu-with-pgg (beg end boundary)
1960   (save-excursion
1961     (save-restriction
1962       (narrow-to-region beg end)
1963       (let* ((ret
1964               (mime-edit-translate-region beg end boundary))
1965              (ctype    (car ret))
1966              (encoding (nth 1 ret)))
1967         (goto-char beg)
1968         (insert (format "Content-Type: %s\n" ctype))
1969         (if encoding
1970             (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1971         (insert "\n")
1972         (or (pgg-sign-region beg (point-max) 'clearsign)
1973             (throw 'mime-edit-error 'pgp-error))
1974         (goto-char beg)
1975         (insert
1976          "--[[application/pgp; format=mime][7bit]]\n")
1977         ))))
1978
1979 (defun mime-edit-encrypt-pgp-kazu-with-epg (beg end boundary)
1980   (save-excursion
1981     (let (recipients header)
1982       (let ((ret (mime-edit-make-encrypt-recipient-header)))
1983         (setq recipients (aref ret 1)
1984               header (aref ret 2)))
1985       (save-restriction
1986         (narrow-to-region beg end)
1987         (let* ((ret
1988                 (mime-edit-translate-region beg end boundary))
1989                (ctype    (car ret))
1990                (encoding (nth 1 ret))
1991                (context (epg-make-context))
1992                cipher)
1993           (goto-char beg)
1994           (insert header)
1995           (insert (format "Content-Type: %s\n" ctype))
1996           (if encoding
1997               (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
1998           (insert "\n")
1999           (epg-context-set-armor context t)
2000           (condition-case error
2001               (setq cipher
2002                     (epg-encrypt-string
2003                      context
2004                      (buffer-substring beg (point-max))
2005                      (epa-select-keys
2006                       context
2007                       "\
2008 Select recipents for encryption.
2009 If no one is selected, symmetric encryption will be performed.  "
2010                       (mapcar (lambda (recipient)
2011                                 (nth 1 (std11-extract-address-components
2012                                         recipient)))
2013                               (split-string recipients 
2014                                             "\\([ \t\n]*,[ \t\n]*\\)+")))))
2015             (error (signal 'mime-edit-error (cdr error))))
2016           (goto-char beg)
2017           (insert
2018            "--[[application/pgp; format=mime][7bit]]\n" cipher)
2019           )))))
2020
2021 (defun mime-edit-encrypt-pgp-kazu-with-pgg (beg end boundary)
2022   (save-excursion
2023     (let (recipients header)
2024       (let ((ret (mime-edit-make-encrypt-recipient-header)))
2025         (setq recipients (aref ret 1)
2026               header (aref ret 2)))
2027       (save-restriction
2028         (narrow-to-region beg end)
2029         (let* ((ret
2030                 (mime-edit-translate-region beg end boundary))
2031                (ctype    (car ret))
2032                (encoding (nth 1 ret)))
2033           (goto-char beg)
2034           (insert header)
2035           (insert (format "Content-Type: %s\n" ctype))
2036           (if encoding
2037               (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
2038           (insert "\n")
2039           (or (pgg-encrypt-region beg (point-max) recipients)
2040               (throw 'mime-edit-error 'pgp-error))
2041           (goto-char beg)
2042           (insert
2043            "--[[application/pgp; format=mime][7bit]]\n")
2044           )))))
2045
2046 (defun mime-edit-convert-lbt-string (string)
2047   (let ((index 0))
2048     (while (setq index (string-match "\n" string index))
2049       (setq string (replace-match "\r\n" nil nil string)
2050             index (+ index 2)))         ;(length "\r\n")
2051     string))
2052       
2053 (defun mime-edit-sign-smime (beg end boundary)
2054   (save-excursion
2055     (save-restriction
2056       (let* ((from (std11-field-body "From" mail-header-separator))
2057              (ret (progn 
2058                     (narrow-to-region beg end)
2059                     (mime-edit-translate-region beg end boundary)))
2060              (ctype    (car ret))
2061              (encoding (nth 1 ret))
2062              (smime-boundary (concat "smime-sign-" boundary))
2063              (context (epg-make-context 'CMS))
2064              signature micalg)
2065         (goto-char beg)
2066         (insert (format "Content-Type: %s\n" ctype))
2067         (if encoding
2068             (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
2069         (insert "\n")
2070         (epg-context-set-signers
2071          context
2072          (epa-select-keys
2073           context
2074           "\
2075 Select keys for signing.
2076 If no one is selected, default secret key is used.  "
2077           (if from 
2078               (list (nth 1 (std11-extract-address-components from))))
2079           t))
2080         (condition-case error
2081             (setq signature
2082                   (epg-sign-string context
2083                                    (mime-edit-convert-lbt-string
2084                                     (buffer-substring (point-min) (point-max)))
2085                                    'detached))
2086           (error (signal 'mime-edit-error (cdr error))))
2087         (setq micalg (epg-new-signature-digest-algorithm
2088                       (car (epg-context-result-for context 'sign))))
2089         (goto-char beg)
2090         (insert (format "--[[multipart/signed;
2091  boundary=\"%s\"%s;
2092  protocol=\"application/pkcs7-signature\"][7bit]]
2093 --%s
2094 "
2095                         smime-boundary
2096                         (if micalg
2097                             (concat "; micalg="
2098                                     (downcase
2099                                      (cdr (assq micalg
2100                                                 epg-digest-algorithm-alist))))
2101                           "")
2102                         smime-boundary))
2103         (goto-char (point-max))
2104         (insert (format "\n--%s
2105 Content-Type: application/pkcs7-signature; name=smime.p7s
2106 Content-Transfer-Encoding: base64
2107 Content-Disposition: attachment; filename=smime.p7s
2108 Content-Description: S/MIME Digital Signature
2109
2110 " smime-boundary)
2111                 (base64-encode-string signature))))))
2112
2113 (defun mime-edit-encrypt-smime (beg end boundary)
2114   (save-excursion
2115     (save-restriction
2116       (let (recipients header)
2117         (let ((ret (mime-edit-make-encrypt-recipient-header)))
2118           (setq recipients (aref ret 1)
2119                 header (aref ret 2)))
2120         (narrow-to-region beg end)
2121         (let* ((ret
2122                 (mime-edit-translate-region beg end boundary))
2123                (ctype    (car ret))
2124                (encoding (nth 1 ret))
2125                (context (epg-make-context 'CMS))
2126                cipher)
2127           (goto-char beg)
2128           (insert header)
2129           (insert (format "Content-Type: %s\n" ctype))
2130           (if encoding
2131               (insert (format "Content-Transfer-Encoding: %s\n" encoding)))
2132           (insert "\n")
2133           (mime-encode-header-in-buffer)
2134           (condition-case error
2135               (setq cipher
2136                     (epg-encrypt-string
2137                      context
2138                      (buffer-substring (point-min) (point-max))
2139                      (epa-select-keys
2140                       context
2141                       "\
2142 Select recipents for encryption.
2143 If no one is selected, symmetric encryption will be performed.  "
2144                       (mapcar (lambda (recipient)
2145                                 (nth 1 (std11-extract-address-components
2146                                         recipient)))
2147                               (split-string recipients 
2148                                             "\\([ \t\n]*,[ \t\n]*\\)+")))))
2149             (error (signal 'mime-edit-error (cdr error))))
2150           (delete-region (point-min)(point-max))
2151           (goto-char beg)
2152           (insert (format "--[[application/pkcs7-mime;
2153  smime-type=enveloped-data;
2154  name=smime.p7m
2155 Content-Disposition: attachment; filename=smime.p7m][base64]]
2156
2157 ")
2158                   (base64-encode-string cipher)))))))
2159
2160 (defsubst replace-space-with-underline (str)
2161   (mapconcat (function
2162               (lambda (arg)
2163                 (char-to-string
2164                  (if (eq arg ?\ )
2165                      ?_
2166                    arg)))) str ""))
2167
2168 (defun mime-edit-make-boundary ()
2169   (concat mime-multipart-boundary "_"
2170           (replace-space-with-underline (current-time-string))))
2171
2172 (defun mime-edit-translate-body ()
2173   "Encode the tagged MIME body in current buffer in MIME compliant message."
2174   (interactive)
2175   (save-excursion
2176     (let ((boundary (mime-edit-make-boundary))
2177           (i 1)
2178           ret)
2179       (while (mime-edit-process-multipart-1
2180               (format "%s-%d" boundary i))
2181         (setq i (1+ i)))
2182       (save-restriction
2183         ;; We are interested in message body.
2184         (let* ((beg
2185                 (progn
2186                   (goto-char (point-min))
2187                   (re-search-forward
2188                    (concat "\n" (regexp-quote mail-header-separator)
2189                            (if mime-ignore-preceding-spaces
2190                                "[ \t\n]*\n" "\n")) nil 'move)
2191                   (point)))
2192                (end
2193                 (progn
2194                   (goto-char (point-max))
2195                   (and mime-ignore-trailing-spaces
2196                        (re-search-backward "[^ \t\n]\n" beg t)
2197                        (forward-char 1))
2198                   (point))))
2199           (setq ret (mime-edit-translate-region
2200                      beg end
2201                      (format "%s-%d" boundary i)))))
2202       (mime-edit-dequote-region (point-min)(point-max))
2203       (let ((contype (car ret))         ;Content-Type
2204             (encoding (nth 1 ret))      ;Content-Transfer-Encoding
2205             )
2206         ;; Insert User-Agent field
2207         (and mime-edit-insert-user-agent-field
2208              (or (mail-position-on-field "User-Agent")
2209                  (insert mime-edit-user-agent-value)))
2210         ;; Make primary MIME headers.
2211         (or (mail-position-on-field "MIME-Version")
2212             (insert mime-edit-mime-version-value))
2213         ;; Remove old Content-Type and other fields.
2214         (save-restriction
2215           (goto-char (point-min))
2216           (search-forward (concat "\n" mail-header-separator "\n") nil t)
2217           (narrow-to-region (point-min) (point))
2218           (goto-char (point-min))
2219           (mime-delete-field "Content-Type")
2220           (mime-delete-field "Content-Transfer-Encoding"))
2221         ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
2222         (mail-position-on-field "Content-Type")
2223         (insert contype)
2224         (if encoding
2225             (progn
2226               (mail-position-on-field "Content-Transfer-Encoding")
2227               (insert encoding)))))))
2228
2229 (defun mime-edit-translate-single-part-tag (boundary &optional prefix)
2230   "Translate single-part-tag to MIME header."
2231   (if (re-search-forward mime-edit-single-part-tag-regexp nil t)
2232       (let* ((beg (match-beginning 0))
2233              (end (match-end 0))
2234              (tag (buffer-substring beg end)))
2235         (delete-region beg end)
2236         (let ((contype (mime-edit-get-contype tag))
2237               (encoding (mime-edit-get-encoding tag)))
2238           (insert (concat prefix "--" boundary "\n"))
2239           (save-restriction
2240             (narrow-to-region (point)(point))
2241             (insert "Content-Type: " contype "\n")
2242             (if encoding
2243                 (insert "Content-Transfer-Encoding: " encoding "\n"))
2244             (mime-encode-header-in-buffer))
2245           (cons (and contype
2246                      (downcase contype))
2247                 (and encoding
2248                      (downcase encoding)))))))
2249
2250 (defun mime-edit-translate-region (beg end &optional boundary multipart)
2251   (or boundary
2252       (setq boundary (mime-edit-make-boundary)))
2253   (save-excursion
2254     (save-restriction
2255       (narrow-to-region beg end)
2256       (let ((tag nil)                   ;MIME tag
2257             (contype nil)               ;Content-Type
2258             (encoding nil)              ;Content-Transfer-Encoding
2259             (nparts 0))                 ;Number of body parts
2260         ;; Normalize the body part by inserting appropriate message
2261         ;; tags for every message contents.
2262         (mime-edit-normalize-body)
2263         ;; Counting the number of Content-Type.
2264         (goto-char (point-min))
2265         (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2266           (setq nparts (1+ nparts)))
2267         ;; Begin translation.
2268         (cond
2269          ((and (<= nparts 1)(not multipart))
2270           ;; It's a singular message.
2271           (goto-char (point-min))
2272           (while (re-search-forward
2273                   mime-edit-single-part-tag-regexp nil t)
2274             (setq tag
2275                   (buffer-substring (match-beginning 0) (match-end 0)))
2276             (delete-region (match-beginning 0) (1+ (match-end 0)))
2277             (setq contype (mime-edit-get-contype tag))
2278             (setq encoding (mime-edit-get-encoding tag))))
2279          (t
2280           ;; It's a multipart message.
2281           (goto-char (point-min))
2282           (let ((prio mime-content-transfer-encoding-priority-list)
2283                 part-info nprio)
2284             (when (setq part-info
2285                         (mime-edit-translate-single-part-tag boundary))
2286               (and (setq nprio (member (cdr part-info) prio))
2287                    (setq prio nprio))
2288               (while (setq part-info
2289                            (mime-edit-translate-single-part-tag boundary "\n"))
2290                 (and (setq nprio (member (cdr part-info) prio))
2291                      (setq prio nprio))))
2292             ;; Define Content-Type as "multipart/mixed".
2293             (setq contype
2294                   (concat "multipart/mixed;\n boundary=\"" boundary "\""))
2295             (setq encoding (car prio))
2296             ;; Insert the trailer.
2297             (goto-char (point-max))
2298             (insert "\n--" boundary "--\n"))))
2299          (list contype encoding boundary nparts)))))
2300
2301 (defun mime-edit-normalize-body ()
2302   "Normalize the body part by inserting appropriate message tags."
2303   ;; Insert the first MIME tags if necessary.
2304   (goto-char (point-min))
2305   (if (not (looking-at mime-edit-single-part-tag-regexp))
2306       (insert (mime-make-text-tag) "\n"))
2307   ;; Check each tag, and add new tag or correct it if necessary.
2308   (goto-char (point-min))
2309   (while (re-search-forward mime-edit-single-part-tag-regexp nil t)
2310     (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
2311            (contype (mime-edit-get-contype tag))
2312            (charset (mime-get-parameter contype "charset"))
2313            (encoding (mime-edit-get-encoding tag)))
2314       ;; Remove extra whitespaces after the tag.
2315       (if (looking-at "[ \t]+$")
2316           (delete-region (match-beginning 0) (match-end 0)))
2317       (let ((beg (point))
2318             (end (mime-edit-content-end)))
2319         (if (= end (point-max))
2320             nil
2321           (goto-char end)
2322           (or (looking-at mime-edit-beginning-tag-regexp)
2323               (eobp)
2324               (insert (mime-make-text-tag) "\n")))
2325         (remove-text-properties beg end '(invisible mime-edit-invisible))
2326         (goto-char beg))
2327       (cond
2328        ((mime-test-content-type contype "message")
2329         ;; Content-type "message" should be sent as is.
2330         (forward-line 1))
2331        ((mime-test-content-type contype "text")
2332         ;; Define charset for text if necessary.
2333         (setq charset (if charset
2334                           (intern (downcase charset))
2335                         (mime-edit-choose-charset)))
2336         (mime-edit-define-charset charset)
2337         (cond ((string-equal contype "text/x-rot13-47-48")
2338                (save-excursion
2339                  (forward-line)
2340                  (mule-caesar-region (point) (mime-edit-content-end))))
2341               ((string-equal contype "text/enriched")
2342                (save-excursion
2343                  (let ((beg (progn
2344                               (forward-line)
2345                               (point)))
2346                        (end (mime-edit-content-end)))
2347                    ;; Patch for hard newlines
2348                    ;; (save-excursion
2349                    ;;   (goto-char beg)
2350                    ;;   (while (search-forward "\n" end t)
2351                    ;;     (put-text-property (match-beginning 0)
2352                    ;;                        (point)
2353                    ;;                        'hard t)))
2354                    ;; End patch for hard newlines
2355                    (enriched-encode beg end nil)
2356                    (goto-char beg)
2357                    (if (search-forward "\n\n")
2358                        (delete-region beg (match-end 0)))))))
2359         ;; Point is now on current tag.
2360         ;; Define encoding and encode text if necessary.
2361         (or encoding    ;Encoding is not specified.
2362             (let* ((encoding
2363                     (let (bits conv)
2364                       (let ((ret (cdr (assq charset mime-charset-type-list))))
2365                         (if ret
2366                             (setq bits (car ret)
2367                                   conv (nth 1 ret))
2368                           (setq bits 8
2369                                 conv "quoted-printable")))
2370                       (if (<= bits mime-transfer-level)
2371                           (mime-encoding-name bits)
2372                         conv)))
2373                    (beg (mime-edit-content-beginning)))
2374               (encode-mime-charset-region beg (mime-edit-content-end)
2375                                           charset)
2376               ;; Protect "From " in beginning of line
2377               (save-restriction
2378                 (narrow-to-region beg (mime-edit-content-end))
2379                 (goto-char beg)
2380                 (let (case-fold-search)
2381                   (if (re-search-forward "^From " nil t)
2382                       (unless encoding
2383                         (if (memq charset '(iso-2022-jp
2384                                             iso-2022-jp-2
2385                                             iso-2022-int-1
2386                                             x-ctext))
2387                             (while (progn
2388                                      (replace-match "\e(BFrom ")
2389                                      (re-search-forward "^From " nil t)))
2390                           (setq encoding "quoted-printable"))))))
2391               ;; canonicalize line break code
2392               (or (member encoding '(nil "7bit" "8bit" "quoted-printable"))
2393                   (save-restriction
2394                     (narrow-to-region beg (mime-edit-content-end))
2395                     (goto-char beg)
2396                     (while (re-search-forward "\\(\\=\\|[^\r]\\)\n" nil t)
2397                       ;; In a certain period, `replace-match' with "\\N"
2398                       ;; converted 8-bit characters into multibyte string,
2399                       ;; but it has been fixed at 2004-01-15.
2400                       ;;(replace-match "\\1\r\n"))))
2401                       (backward-char 1)
2402                       (insert "\r")
2403                       (forward-char 1))))
2404               (goto-char beg)
2405               (mime-encode-region beg (mime-edit-content-end)
2406                                   (or encoding "7bit"))
2407               (mime-edit-define-encoding encoding)))
2408         (goto-char (mime-edit-content-end)))
2409        ((null encoding)         ;Encoding is not specified.
2410         ;; Application, image, audio, video, and any other
2411         ;; unknown content-type without encoding should be
2412         ;; encoded.
2413         (let* ((encoding "base64")      ;Encode in BASE64 by default.
2414                (beg (mime-edit-content-beginning))
2415                (end (mime-edit-content-end)))
2416           (mime-encode-region beg end encoding)
2417           (mime-edit-define-encoding encoding))
2418         (forward-line 1))))))
2419
2420 (defun mime-delete-field (field)
2421   "Delete header FIELD."
2422   (let ((regexp (format "^%s:[ \t]*" field)))
2423     (goto-char (point-min))
2424     (while (re-search-forward regexp nil t)
2425       (delete-region (match-beginning 0)
2426                      (1+ (std11-field-end))))))
2427
2428 \f
2429 ;;;
2430 ;;; Platform dependent functions
2431 ;;;
2432
2433 ;; Sun implementations
2434
2435 (defun mime-edit-voice-recorder-for-sun (encoding)
2436   "Record voice in a buffer using Sun audio device,
2437 and insert data encoded as ENCODING."
2438   (message "Start the recording on %s.  Type C-g to finish the recording..."
2439            (system-name))
2440   (mime-insert-encoded-file "/dev/audio" encoding))
2441
2442 \f
2443 ;;; @ Other useful commands.
2444 ;;;
2445
2446 ;; Message forwarding commands as content-type "message/rfc822".
2447
2448 (defun mime-edit-insert-message (&optional message)
2449   (interactive)
2450   (let ((inserter (cdr (assq major-mode mime-edit-message-inserter-alist))))
2451     (if (and inserter (fboundp inserter))
2452         (progn
2453           (mime-edit-insert-tag "message" "rfc822")
2454           (funcall inserter message))
2455       (message "Sorry, I don't have message inserter for your MUA."))))
2456
2457 (defun mime-edit-insert-mail (&optional message)
2458   (interactive)
2459   (let ((inserter (cdr (assq major-mode mime-edit-mail-inserter-alist))))
2460     (if (and inserter (fboundp inserter))
2461         (progn
2462           (mime-edit-insert-tag "message" "rfc822")
2463           (funcall inserter message))
2464       (message "Sorry, I don't have mail inserter for your MUA."))))
2465
2466 (defun mime-edit-inserted-message-filter ()
2467   (save-excursion
2468     (save-restriction
2469       (let ((header-start (point))
2470             (case-fold-search t)
2471             beg end)
2472         ;; for Emacs 18
2473         ;; (if (re-search-forward "^$" (marker-position (mark-marker)))
2474         (if (re-search-forward "^$" (mark t))
2475             (narrow-to-region header-start (match-beginning 0)))
2476         (goto-char header-start)
2477         (while (and (re-search-forward
2478                      mime-edit-yank-ignored-field-regexp nil t)
2479                     (setq beg (match-beginning 0))
2480                     (setq end (1+ (std11-field-end))))
2481           (delete-region beg end))))))
2482
2483
2484 ;;; @ multipart enclosure
2485 ;;;
2486
2487 (defun mime-edit-enclose-region-internal (type beg end)
2488   (save-excursion
2489     (goto-char beg)
2490     (save-restriction
2491       (narrow-to-region beg end)
2492       (insert (format "--<<%s>>-{\n" type))
2493       (goto-char (point-max))
2494       (insert (format "--}-<<%s>>\n" type))
2495       (goto-char (point-max)))
2496     (or (looking-at mime-edit-beginning-tag-regexp)
2497         (eobp)
2498         (insert (mime-make-text-tag) "\n"))))
2499
2500 (defun mime-edit-enclose-quote-region (beg end)
2501   (interactive "*r")
2502   (mime-edit-enclose-region-internal 'quote beg end))
2503
2504 (defun mime-edit-enclose-mixed-region (beg end)
2505   (interactive "*r")
2506   (mime-edit-enclose-region-internal 'mixed beg end))
2507
2508 (defun mime-edit-enclose-parallel-region (beg end)
2509   (interactive "*r")
2510   (mime-edit-enclose-region-internal 'parallel beg end))
2511
2512 (defun mime-edit-enclose-digest-region (beg end)
2513   (interactive "*r")
2514   (mime-edit-enclose-region-internal 'digest beg end))
2515
2516 (defun mime-edit-enclose-alternative-region (beg end)
2517   (interactive "*r")
2518   (mime-edit-enclose-region-internal 'alternative beg end))
2519
2520 (defun mime-edit-enclose-pgp-signed-region (beg end)
2521   (interactive "*r")
2522   (mime-edit-enclose-region-internal 'pgp-signed beg end))
2523
2524 (defun mime-edit-enclose-pgp-encrypted-region (beg end)
2525   (interactive "*r")
2526   (mime-edit-enclose-region-internal 'pgp-encrypted beg end))
2527
2528 (defun mime-edit-enclose-kazu-signed-region (beg end)
2529   (interactive "*r")
2530   (mime-edit-enclose-region-internal 'kazu-signed beg end))
2531
2532 (defun mime-edit-enclose-kazu-encrypted-region (beg end)
2533   (interactive "*r")
2534   (mime-edit-enclose-region-internal 'kazu-encrypted beg end))
2535
2536 (defun mime-edit-enclose-smime-signed-region (beg end)
2537   (interactive "*r")
2538   (mime-edit-enclose-region-internal 'smime-signed beg end))
2539
2540 (defun mime-edit-enclose-smime-encrypted-region (beg end)
2541   (interactive "*r")
2542   (mime-edit-enclose-region-internal 'smime-encrypted beg end))
2543
2544 (defun mime-edit-insert-key (&optional arg)
2545   "Insert a pgp public key."
2546   (interactive "P")
2547   (mime-edit-insert-tag "application" "pgp-keys")
2548   (mime-edit-define-encoding "7bit")
2549   (let ((context (epg-make-context)))
2550     (epg-context-set-armor context t)
2551     (epg-export-keys-to-string context
2552                                (epa-select-keys context
2553                                                 "Select keys for export.  ")))
2554   (if (and (not (eobp))
2555            (not (looking-at mime-edit-single-part-tag-regexp)))
2556       (insert (mime-make-text-tag) "\n")))
2557
2558
2559 ;;; @ flag setting
2560 ;;;
2561
2562 (defun mime-edit-set-split (arg)
2563   (interactive
2564    (list
2565     (y-or-n-p "Do you want to enable split? ")))
2566   (setq mime-edit-split-message arg)
2567   (if arg
2568       (message "This message is enabled to split.")
2569     (message "This message is not enabled to split.")))
2570
2571 (defun mime-edit-toggle-transfer-level (&optional transfer-level)
2572   "Toggle transfer-level is 7bit or 8bit through.
2573
2574 Optional TRANSFER-LEVEL is a number of transfer-level, 7 or 8."
2575   (interactive)
2576   (if (numberp transfer-level)
2577       (setq mime-transfer-level transfer-level)
2578     (if (< mime-transfer-level 8)
2579         (setq mime-transfer-level 8)
2580       (setq mime-transfer-level 7)))
2581   (message (format "Current transfer-level is %d bit"
2582                    mime-transfer-level))
2583   (setq mime-transfer-level-string
2584         (mime-encoding-name mime-transfer-level 'not-omit))
2585   (force-mode-line-update))
2586
2587 (defun mime-edit-set-transfer-level-7bit ()
2588   (interactive)
2589   (mime-edit-toggle-transfer-level 7))
2590
2591 (defun mime-edit-set-transfer-level-8bit ()
2592   (interactive)
2593   (mime-edit-toggle-transfer-level 8))
2594
2595
2596 ;;; @ pgp
2597 ;;;
2598
2599 (defvar mime-edit-pgp-processing nil)
2600 (make-variable-buffer-local 'mime-edit-pgp-processing)
2601
2602 (defun mime-edit-set-sign (arg)
2603   (interactive
2604    (list
2605     (y-or-n-p "Do you want to sign? ")))
2606   (if arg
2607       (progn
2608         (or (memq 'sign mime-edit-pgp-processing)
2609             (setq mime-edit-pgp-processing 
2610                   (nconc mime-edit-pgp-processing 
2611                          (copy-sequence '(sign)))))
2612         (message "This message will be signed."))
2613     (setq mime-edit-pgp-processing 
2614           (delq 'sign mime-edit-pgp-processing))
2615     (message "This message will not be signed.")))
2616
2617 (defun mime-edit-set-encrypt (arg)
2618   (interactive
2619    (list
2620     (y-or-n-p "Do you want to encrypt? ")))
2621   (if arg
2622       (progn
2623         (or (memq 'encrypt mime-edit-pgp-processing)
2624             (setq mime-edit-pgp-processing 
2625                   (nconc mime-edit-pgp-processing 
2626                          (copy-sequence '(encrypt)))))
2627         (message "This message will be encrypt."))
2628     (setq mime-edit-pgp-processing
2629           (delq 'encrypt mime-edit-pgp-processing))
2630     (message "This message will not be encrypt.")))
2631
2632 (defun mime-edit-pgp-enclose-buffer ()
2633   (let ((beg (save-excursion
2634                (goto-char (point-min))
2635                (if (search-forward (concat "\n" mail-header-separator "\n"))
2636                    (match-end 0)))))
2637     (if beg
2638         (dolist (pgp-processing mime-edit-pgp-processing)
2639           (case pgp-processing
2640             (sign
2641              (mime-edit-enclose-pgp-signed-region 
2642               beg (point-max)))
2643             (encrypt
2644              (mime-edit-enclose-pgp-encrypted-region 
2645               beg (point-max))))))))
2646
2647
2648 ;;; @ split
2649 ;;;
2650
2651 (defun mime-edit-insert-partial-header (fields subject
2652                                                id number total separator)
2653   (insert fields)
2654   (insert (format "Subject: %s (%d/%d)\n" subject number total))
2655   (insert mime-edit-mime-version-field-for-message/partial)
2656   (insert (format "\
2657 Content-Type: message/partial; id=%s; number=%d; total=%d\n%s\n"
2658                   id number total separator)))
2659
2660 (defun mime-edit-split-and-send
2661   (&optional cmd lines mime-edit-message-max-length)
2662   (interactive)
2663   (or lines
2664       (setq lines
2665             (count-lines (point-min) (point-max))))
2666   (or mime-edit-message-max-length
2667       (setq mime-edit-message-max-length
2668             (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2669                 mime-edit-message-default-max-lines)))
2670   (let ((separator mail-header-separator)
2671         (id (concat "\""
2672                     (replace-space-with-underline (current-time-string))
2673                     "@" (system-name) "\"")))
2674     (run-hooks 'mime-edit-before-split-hook)
2675     (let ((the-buf (current-buffer))
2676           (copy-buf (get-buffer-create " *Original Message*"))
2677           (header (std11-header-string-except
2678                    mime-edit-split-ignored-field-regexp separator))
2679           (subject (mail-fetch-field "subject"))
2680           (total (+ (/ lines mime-edit-message-max-length)
2681                     (if (> (mod lines mime-edit-message-max-length) 0)
2682                         1)))
2683           (command
2684            (or cmd
2685                (cdr
2686                 (assq major-mode
2687                       mime-edit-split-message-sender-alist))
2688                (function
2689                 (lambda ()
2690                   (interactive)
2691                   (error "Split sender is not specified for `%s'." major-mode)))))
2692           (mime-edit-partial-number 1)
2693           data)
2694       (save-excursion
2695         (set-buffer copy-buf)
2696         (erase-buffer)
2697         (insert-buffer the-buf)
2698         (save-restriction
2699           (if (re-search-forward
2700                (concat "^" (regexp-quote separator) "$") nil t)
2701               (let ((he (match-beginning 0)))
2702                 (replace-match "")
2703                 (narrow-to-region (point-min) he)))
2704           (goto-char (point-min))
2705           (while (re-search-forward mime-edit-split-blind-field-regexp nil t)
2706             (delete-region (match-beginning 0)
2707                            (1+ (std11-field-end))))))
2708       (while (< mime-edit-partial-number total)
2709         (erase-buffer)
2710         (save-excursion
2711           (set-buffer copy-buf)
2712           (setq data (buffer-substring
2713                       (point-min)
2714                       (progn
2715                         (goto-line mime-edit-message-max-length)
2716                         (point))))
2717           (delete-region (point-min)(point)))
2718         (mime-edit-insert-partial-header
2719          header subject id mime-edit-partial-number total separator)
2720         (insert data)
2721         (save-excursion
2722           (message (format "Sending %d/%d..."
2723                            mime-edit-partial-number total))
2724           (call-interactively command)
2725           (message (format "Sending %d/%d...done"
2726                            mime-edit-partial-number total)))
2727         (setq mime-edit-partial-number
2728               (1+ mime-edit-partial-number)))
2729       (erase-buffer)
2730       (save-excursion
2731         (set-buffer copy-buf)
2732         (setq data (buffer-string))
2733         (erase-buffer))
2734       (mime-edit-insert-partial-header
2735        header subject id mime-edit-partial-number total separator)
2736       (insert data)
2737       (save-excursion
2738         (message (format "Sending %d/%d..."
2739                          mime-edit-partial-number total))
2740         (message (format "Sending %d/%d...done"
2741                          mime-edit-partial-number total))))))
2742
2743 (defun mime-edit-maybe-split-and-send (&optional cmd)
2744   (interactive)
2745   (run-hooks 'mime-edit-before-send-hook)
2746   (let ((mime-edit-message-max-length
2747          (or (cdr (assq major-mode mime-edit-message-max-lines-alist))
2748              mime-edit-message-default-max-lines))
2749         (lines (count-lines (point-min) (point-max))))
2750     (if (and (> lines mime-edit-message-max-length)
2751              mime-edit-split-message)
2752         (mime-edit-split-and-send cmd lines mime-edit-message-max-length))))
2753
2754
2755 ;;; @ preview message
2756 ;;;
2757
2758 (defvar mime-edit-buffer nil) ; buffer local variable
2759 (defvar mime-edit-temp-message-buffer nil) ; buffer local variable
2760
2761 (defun mime-edit-preview-message ()
2762   "preview editing MIME message."
2763   (interactive)
2764   (let* ((str (buffer-string))
2765          (separator mail-header-separator)
2766          (the-buf (current-buffer))
2767          (buf-name (buffer-name))
2768          (temp-buf-name (concat "*temp-article:" buf-name "*"))
2769          (buf (get-buffer temp-buf-name))
2770          (pgp-processing mime-edit-pgp-processing))
2771     (if buf
2772         (progn
2773           (switch-to-buffer buf)
2774           (erase-buffer))
2775       (setq buf (get-buffer-create temp-buf-name))
2776       (switch-to-buffer buf))
2777     (insert str)
2778     (setq major-mode 'mime-temp-message-mode)
2779     (make-local-variable 'mail-header-separator)
2780     (setq mail-header-separator separator)
2781     (make-local-variable 'mime-edit-buffer)
2782     (setq mime-edit-buffer the-buf)
2783     (setq mime-edit-pgp-processing pgp-processing)
2784
2785     (run-hooks 'mime-edit-translate-hook)
2786     (mime-edit-translate-buffer)
2787     (goto-char (point-min))
2788     (if (re-search-forward
2789          (concat "^" (regexp-quote separator) "$"))
2790         (replace-match ""))
2791     (mime-view-buffer)
2792     (make-local-variable 'mime-edit-temp-message-buffer)
2793     (setq mime-edit-temp-message-buffer buf)))
2794
2795 (defun mime-edit-quitting-method ()
2796   "Quitting method for mime-view."
2797   (let* ((temp mime-edit-temp-message-buffer)
2798          buf)
2799     (mime-preview-kill-buffer)
2800     (set-buffer temp)
2801     (setq buf mime-edit-buffer)
2802     (kill-buffer temp)
2803     (switch-to-buffer buf)))
2804
2805 (set-alist 'mime-preview-quitting-method-alist
2806            'mime-temp-message-mode
2807            #'mime-edit-quitting-method)
2808
2809
2810 ;;; @ edit again
2811 ;;;
2812
2813 (defvar mime-edit-again-ignored-field-regexp
2814   (concat "^\\(" "Content-.*\\|Mime-Version"
2815           (if mime-edit-insert-user-agent-field "\\|User-Agent")
2816           "\\):")
2817   "Regexp for deleted header fields when `mime-edit-again' is called.")
2818
2819 (defsubst eliminate-top-spaces (string)
2820   "Eliminate top sequence of space or tab in STRING."
2821   (if (string-match "^[ \t]+" string)
2822       (substring string (match-end 0))
2823     string))
2824
2825 (defun mime-edit-decode-multipart-in-buffer (content-type not-decode-text)
2826   (let* ((subtype
2827           (or
2828            (cdr (assoc (mime-content-type-parameter content-type "protocol")
2829                        '(("application/pgp-encrypted" . pgp-encrypted)
2830                          ("application/pgp-signature" . pgp-signed))))
2831            (mime-content-type-subtype content-type)))
2832          (boundary (mime-content-type-parameter content-type "boundary"))
2833          (boundary-pat (concat "\n--" (regexp-quote boundary) "[ \t]*\n")))
2834     (re-search-forward boundary-pat nil t)
2835     (let ((bb (match-beginning 0)) eb tag)
2836       (setq tag (format "\n--<<%s>>-{\n" subtype))
2837       (goto-char bb)
2838       (insert tag)
2839       (setq bb (+ bb (length tag)))
2840       (re-search-forward
2841        (concat "\n--" (regexp-quote boundary) "--[ \t]*\n")
2842        nil t)
2843       (setq eb (match-beginning 0))
2844       (replace-match (format "--}-<<%s>>\n" subtype))
2845       (save-restriction
2846         (narrow-to-region bb eb)
2847         (goto-char (point-min))
2848         (while (re-search-forward boundary-pat nil t)
2849           (let ((beg (match-beginning 0))
2850                 end)
2851             (delete-region beg (match-end 0))
2852             (save-excursion
2853               (if (re-search-forward boundary-pat nil t)
2854                   (setq end (match-beginning 0))
2855                 (setq end (point-max)))
2856               (save-restriction
2857                 (narrow-to-region beg end)
2858                 (cond
2859                  ((eq subtype 'pgp-encrypted)
2860                   (when (progn
2861                            (goto-char (point-min))
2862                            (re-search-forward "^-+BEGIN PGP MESSAGE-+$"
2863                                               nil t))
2864                     (insert (epg-decrypt-string
2865                              (epg-make-context)
2866                              (buffer-substring (match-beginning 0)
2867                                                (point-max))))
2868                     (delete-region (point)(point-max))
2869                     (mime-edit-decode-message-in-buffer 
2870                      nil not-decode-text)
2871                     (delete-region (goto-char (point-min))
2872                                    (if (search-forward "\n\n" nil t)
2873                                        (match-end 0)
2874                                      (point-min)))
2875                     (goto-char (point-max))))
2876                  (t 
2877                   (mime-edit-decode-message-in-buffer
2878                    (if (eq subtype 'digest)
2879                        (eval-when-compile
2880                          (make-mime-content-type 'message 'rfc822)))
2881                    not-decode-text)
2882                   (goto-char (point-max))))))))))
2883     (goto-char (point-min))
2884     (or (= (point-min) 1)
2885         (delete-region (point-min)
2886                        (if (search-forward "\n\n" nil t)
2887                            (match-end 0)
2888                          (point-min))))))
2889
2890 (defun mime-edit-decode-single-part-in-buffer
2891   (content-type not-decode-text &optional content-disposition)
2892   (let* ((type (mime-content-type-primary-type content-type))
2893          (subtype (mime-content-type-subtype content-type))
2894          (ctype (format "%s/%s" type subtype))
2895          charset
2896          (pstr (let ((bytes (+ 14 (length ctype))))
2897                  (mapconcat (function
2898                              (lambda (attr)
2899                                (if (string= (car attr) "charset")
2900                                    (progn
2901                                      (setq charset (cdr attr))
2902                                      "")
2903                                  (let* ((str (concat (car attr)
2904                                                      "=" (cdr attr)))
2905                                         (bs (length str)))
2906                                    (setq bytes (+ bytes bs 2))
2907                                    (if (< bytes 76)
2908                                        (concat "; " str)
2909                                      (setq bytes (+ bs 1))
2910                                      (concat ";\n " str)
2911                                      )))))
2912                             (mime-content-type-parameters content-type) "")))
2913          encoding
2914          encoded
2915          (limit (save-excursion
2916                   (if (search-forward "\n\n" nil t)
2917                       (1- (point)))))
2918          (disposition-type
2919           (mime-content-disposition-type content-disposition))
2920          (disposition-str
2921           (if disposition-type
2922               (let ((bytes (+ 21 (length (format "%s" disposition-type)))))
2923                 (mapconcat (function
2924                             (lambda (attr)
2925                               (let* ((str (concat
2926                                            (car attr)
2927                                            "="
2928                                            (if (string-equal "filename"
2929                                                              (car attr))
2930                                                (std11-wrap-as-quoted-string
2931                                                 (cdr attr))
2932                                              (cdr attr))))
2933                                      (bs (length str)))
2934                                 (setq bytes (+ bytes bs 2))
2935                                 (if (< bytes 76)
2936                                     (concat "; " str)
2937                                   (setq bytes (+ bs 1))
2938                                   (concat ";\n " str)
2939                                   ))))
2940                            (mime-content-disposition-parameters
2941                             content-disposition)
2942                            "")))))
2943     (if disposition-type
2944         (setq pstr (format "%s\nContent-Disposition: %s%s"
2945                            pstr disposition-type disposition-str)))
2946     (save-excursion
2947       (if (re-search-forward
2948            "^Content-Transfer-Encoding:" limit t)
2949           (let ((beg (match-beginning 0))
2950                 (hbeg (match-end 0))
2951                 (end (std11-field-end limit)))
2952             (setq encoding
2953                   (downcase
2954                    (eliminate-top-spaces
2955                     (std11-unfold-string
2956                      (buffer-substring hbeg end)))))
2957             (if (or charset (eq type 'text))
2958                 (progn
2959                   (delete-region beg (1+ end))
2960                   (goto-char (point-min))
2961                   (if (search-forward "\n\n" nil t)
2962                       (progn
2963                         (mime-decode-region
2964                          (match-end 0)(point-max) encoding)
2965                         (setq encoded t
2966                               encoding nil))))))))
2967     (if (and (eq type 'text)
2968              (or encoded (not not-decode-text)))
2969         (progn
2970           (save-excursion
2971             (goto-char (point-min))
2972             (while (re-search-forward "\r\n" nil t)
2973               (replace-match "\n")))
2974           (decode-mime-charset-region (point-min)(point-max)
2975                                       (or charset default-mime-charset))))
2976     (let ((he (if (re-search-forward "^$" nil t)
2977                   (match-end 0)
2978                 (point-min))))
2979       (if (and (eq type 'text)
2980                (eq subtype 'x-rot13-47-48))
2981           (mule-caesar-region he (point-max)))
2982       (if (= (point-min) 1)
2983           (progn
2984             (goto-char he)
2985             (insert
2986              (concat "\n"
2987                      (mime-create-tag
2988                       (format "%s/%s%s" type subtype pstr)
2989                       encoding))))
2990         (delete-region (point-min) he)
2991         (insert
2992          (mime-create-tag (format "%s/%s%s" type subtype pstr)
2993                           encoding))))))
2994
2995 ;;;###autoload
2996 (defun mime-edit-decode-message-in-buffer (&optional default-content-type
2997                                                      not-decode-text)
2998   (save-excursion
2999     (goto-char (point-min))
3000     (let ((ctl (or (mime-read-Content-Type)
3001                    default-content-type)))
3002       (if ctl
3003           (let ((type (mime-content-type-primary-type ctl)))
3004             (cond
3005              ((and (eq type 'application)
3006                    (eq (mime-content-type-subtype ctl) 'pgp-signature))
3007               (delete-region (point-min)(point-max)))
3008              ((eq type 'multipart)
3009               (mime-edit-decode-multipart-in-buffer ctl not-decode-text))
3010              (t
3011               (mime-edit-decode-single-part-in-buffer
3012                ctl not-decode-text (mime-read-Content-Disposition)))))
3013         (or not-decode-text
3014             (decode-mime-charset-region (point-min) (point-max)
3015                                         default-mime-charset)))
3016       (if (= (point-min) 1)
3017           (progn
3018             (save-restriction
3019               (std11-narrow-to-header)
3020               (goto-char (point-min))
3021               (while (re-search-forward
3022                       mime-edit-again-ignored-field-regexp nil t)
3023                 (delete-region (match-beginning 0) (1+ (std11-field-end)))))
3024             (mime-decode-header-in-buffer (not not-decode-text)))))))
3025
3026 ;;;###autoload
3027 (defun mime-edit-again (&optional not-decode-text no-separator not-turn-on)
3028   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode.
3029 Content-Type and Content-Transfer-Encoding header fields will be
3030 converted to MIME-Edit tags."
3031   (interactive)
3032   (goto-char (point-min))
3033   (if (search-forward
3034        (concat "\n" (regexp-quote mail-header-separator) "\n")
3035        nil t)
3036       (replace-match "\n\n"))
3037   (mime-edit-decode-message-in-buffer nil not-decode-text)
3038   (goto-char (point-min))
3039   (or no-separator
3040       (and (re-search-forward "^$")
3041            (replace-match mail-header-separator)))
3042   (or not-turn-on
3043       (turn-on-mime-edit)))
3044
3045
3046 ;;; @ end
3047 ;;;
3048
3049 (provide 'mime-edit)
3050
3051 (run-hooks 'mime-edit-load-hook)
3052
3053 ;;; mime-edit.el ends here