+(defun mime-editor/encrypt-pgp-kazu (beg end boundary)
+ (save-excursion
+ (let ((from (rfc822/get-field-body "From"))
+ (to (rfc822/get-field-body "To"))
+ (cc (rfc822/get-field-body "cc"))
+ recipients)
+ (save-restriction
+ (narrow-to-region beg end)
+ (let* ((ret
+ (mime-editor/translate-region beg end boundary))
+ (ctype (car ret))
+ (encoding (nth 1 ret))
+ (parts (nth 3 ret))
+ )
+ (goto-char beg)
+ (if (and (stringp from)
+ (not (string-equal from "")))
+ (insert (format "From: %s\n" from))
+ )
+ (if (and (stringp to)
+ (not (string-equal to "")))
+ (progn
+ (insert (format "To: %s\n" to))
+ (setq recipients to)
+ ))
+ (if (and (stringp cc)
+ (not (string-equal cc "")))
+ (progn
+ (insert (format "cc: %s\n" cc))
+ (if recipients
+ (setq recipients (concat recipients "," cc))
+ (setq recipients cc)
+ )))
+ (insert (format "Content-Type: %s\n" ctype))
+ (if encoding
+ (insert (format "Content-Transfer-Encoding: %s\n" encoding))
+ )
+ (insert "\n")
+ (or (as-binary-process
+ (mc-pgp-encrypt-region
+ (mc-split "\\([ \t\n]*,[ \t\n]*\\)+" recipients)
+ beg (point-max))
+ )
+ (throw 'mime-editor/error 'pgp-error)
+ )
+ (goto-char beg)
+ (insert
+ "--[[application/pgp; format=mime][7bit]]\n")
+ ))
+ )))
+
+(defun mime-editor/translate-body ()
+ "Encode the tagged MIME body in current buffer in MIME compliant message."
+ (interactive)
+ (save-excursion
+ (let ((boundary
+ (concat mime-multipart-boundary "_"
+ (replace-space-with-underline (current-time-string))
+ ))
+ (i 1)
+ ret)
+ (while (mime-editor/process-multipart-1
+ (format "%s-%d" boundary i))
+ (setq i (1+ i))
+ )
+ (save-restriction
+ ;; We are interested in message body.
+ (let* ((beg
+ (progn
+ (goto-char (point-min))
+ (re-search-forward
+ (concat "\n" (regexp-quote mail-header-separator)
+ (if mime-ignore-preceding-spaces
+ "[ \t\n]*\n" "\n")) nil 'move)
+ (point)))
+ (end
+ (progn
+ (goto-char (point-max))
+ (and mime-ignore-trailing-spaces
+ (re-search-backward "[^ \t\n]\n" beg t)
+ (forward-char 1))
+ (point))))
+ (setq ret (mime-editor/translate-region
+ beg end
+ (format "%s-%d" boundary i)))
+ ))
+ (mime-editor/dequote-region (point-min)(point-max))
+ (let ((contype (car ret)) ;Content-Type
+ (encoding (nth 1 ret)) ;Content-Transfer-Encoding
+ )
+ ;; Make primary MIME headers.
+ (or (mail-position-on-field "Mime-Version")
+ (insert mime-editor/mime-version-value))
+ ;; Remove old Content-Type and other fields.
+ (save-restriction
+ (goto-char (point-min))
+ (search-forward (concat "\n" mail-header-separator "\n") nil t)
+ (narrow-to-region (point-min) (point))
+ (goto-char (point-min))
+ (mime-delete-field "Content-Type")
+ (mime-delete-field "Content-Transfer-Encoding"))
+ ;; Then, insert Content-Type and Content-Transfer-Encoding fields.
+ (mail-position-on-field "Content-Type")
+ (insert contype)
+ (if encoding
+ (progn
+ (mail-position-on-field "Content-Transfer-Encoding")
+ (insert encoding)))
+ ))))
+
+(defun mime-editor/translate-single-part-tag (&optional prefix)
+ (if (re-search-forward mime-editor/single-part-tag-regexp nil t)
+ (let* ((beg (match-beginning 0))
+ (end (match-end 0))
+ (tag (buffer-substring beg end))
+ )
+ (delete-region beg end)
+ (setq contype (mime-editor/get-contype tag))
+ (setq encoding (mime-editor/get-encoding tag))
+ (insert (concat prefix "--" boundary "\n"))
+ (save-restriction
+ (narrow-to-region (point)(point))
+ (insert "Content-Type: " contype "\n")
+ (if encoding
+ (insert "Content-Transfer-Encoding: " encoding "\n"))
+ (mime/encode-message-header)
+ )
+ t)))
+
+(defun mime-editor/translate-region (beg end &optional boundary multipart)
+ (if (null boundary)
+ (setq boundary
+ (concat mime-multipart-boundary "_"
+ (replace-space-with-underline (current-time-string))))
+ )
+ (save-excursion
+ (save-restriction
+ (narrow-to-region beg end)
+ (let ((tag nil) ;MIME tag
+ (contype nil) ;Content-Type
+ (encoding nil) ;Content-Transfer-Encoding
+ (nparts 0)) ;Number of body parts
+ ;; Normalize the body part by inserting appropriate message
+ ;; tags for every message contents.
+ (mime-editor/normalize-body)
+ ;; Counting the number of Content-Type.
+ (goto-char (point-min))
+ (while (re-search-forward mime-editor/single-part-tag-regexp nil t)
+ (setq nparts (1+ nparts)))
+ ;; Begin translation.
+ (cond
+ ((and (<= nparts 1)(not multipart))
+ ;; It's a singular message.
+ (goto-char (point-min))
+ (while (re-search-forward
+ mime-editor/single-part-tag-regexp nil t)
+ (setq tag
+ (buffer-substring (match-beginning 0) (match-end 0)))
+ (delete-region (match-beginning 0) (1+ (match-end 0)))
+ (setq contype (mime-editor/get-contype tag))
+ (setq encoding (mime-editor/get-encoding tag))
+ ))
+ (t
+ ;; It's a multipart message.
+ (goto-char (point-min))
+ (and (mime-editor/translate-single-part-tag)
+ (while (mime-editor/translate-single-part-tag "\n"))
+ )
+ ;; Define Content-Type as "multipart/mixed".
+ (setq contype
+ (concat "multipart/mixed;\n boundary=\"" boundary "\""))
+ ;; Content-Transfer-Encoding must be "7bit".
+ ;; The following encoding can be `nil', but is
+ ;; specified as is since there is no way that a user
+ ;; specifies it.
+ (setq encoding "7bit")
+ ;; Insert the trailer.
+ (goto-char (point-max))
+ (insert "\n--" boundary "--\n")
+ ))
+ (list contype encoding boundary nparts)
+ ))))
+
+(defun mime-editor/normalize-body ()
+ "Normalize the body part by inserting appropriate message tags."
+ ;; Insert the first MIME tags if necessary.
+ (goto-char (point-min))
+ (if (not (looking-at mime-editor/single-part-tag-regexp))
+ (insert (mime-make-text-tag) "\n"))
+ ;; Check each tag, and add new tag or correct it if necessary.
+ (goto-char (point-min))
+ (while (re-search-forward mime-editor/single-part-tag-regexp nil t)
+ (let* ((tag (buffer-substring (match-beginning 0) (match-end 0)))
+ (contype (mime-editor/get-contype tag))
+ (charset (mime-get-parameter contype "charset"))
+ (encoding (mime-editor/get-encoding tag)))
+ ;; Remove extra whitespaces after the tag.
+ (if (looking-at "[ \t]+$")
+ (delete-region (match-beginning 0) (match-end 0)))
+ (let ((beg (point))
+ (end (mime-editor/content-end))
+ )
+ (goto-char end)
+ (or (looking-at mime-editor/beginning-tag-regexp)
+ (eobp)
+ (insert (mime-make-text-tag) "\n")
+ )
+ (visible-region beg end)
+ (goto-char beg)
+ )
+ (cond
+ ((mime-test-content-type contype "message")
+ ;; Content-type "message" should be sent as is.
+ (forward-line 1)
+ )
+ ((mime-test-content-type contype "text")
+ ;; Define charset for text if necessary.
+ (setq charset (if charset
+ (intern (downcase charset))
+ (mime-editor/choose-charset)))
+ (mime-editor/define-charset charset)
+ (cond ((string-equal contype "text/x-rot13-47")
+ (save-excursion
+ (forward-line)
+ (set-mark (point))
+ (goto-char (mime-editor/content-end))
+ (tm:caesar-region)
+ ))
+ ((string-equal contype "text/enriched")
+ (save-excursion
+ (let ((beg (progn
+ (forward-line)
+ (point)))
+ (end (mime-editor/content-end))
+ )
+ (enriched-encode beg end)
+ (goto-char beg)
+ (if (search-forward "\n\n")
+ (delete-region beg (match-end 0))
+ )
+ ))))
+ ;; Point is now on current tag.
+ ;; Define encoding and encode text if necessary.
+ (or encoding ;Encoding is not specified.
+ (let* ((encoding
+ (cdr
+ (assq charset
+ mime-editor/charset-default-encoding-alist)
+ ))
+ (beg (mime-editor/content-beginning))
+ )
+ (encode-mime-charset-region beg (mime-editor/content-end)
+ charset)
+ (mime-encode-region beg (mime-editor/content-end) encoding)
+ (mime-editor/define-encoding encoding)
+ ))
+ (goto-char (mime-editor/content-end))
+ )
+ ((null encoding) ;Encoding is not specified.
+ ;; Application, image, audio, video, and any other
+ ;; unknown content-type without encoding should be
+ ;; encoded.
+ (let* ((encoding "base64") ;Encode in BASE64 by default.
+ (beg (mime-editor/content-beginning))
+ (end (mime-editor/content-end))
+ (body (buffer-substring beg end))
+ )
+ (mime-encode-region beg end encoding)
+ (mime-editor/define-encoding encoding))
+ (forward-line 1)
+ ))
+ )))
+
+(defun mime-delete-field (field)
+ "Delete header FIELD."
+ (let ((regexp (format "^%s:[ \t]*" field)))
+ (goto-char (point-min))
+ (while (re-search-forward regexp nil t)
+ (delete-region (match-beginning 0)
+ (progn (forward-line 1) (point)))
+ )))
+