* wl-mime.el (wl-message-verify-pgp-nonmime): Encode by the coding
[elisp/wanderlust.git] / wl / wl-mime.el
index 575c235..4627e48 100644 (file)
 (require 'elmo)
 
 (eval-when-compile
-  (defalias-maybe 'Meadow-version 'ignore))
-
-(defvar xemacs-betaname)
-(defvar xemacs-codename)
-(defvar enable-multibyte-characters)
-(defvar mule-version)
+  (defalias-maybe 'pgg-decrypt-region 'ignore)
+  (defalias-maybe 'pgg-display-output-buffer 'ignore)
+  (defalias-maybe 'pgg-verify-region 'ignore))
 
 ;;; Draft
 
 
 (defun wl-draft-yank-current-message-entity ()
   "Yank currently displayed message entity.
-By setting following-method as yank-content."
+By setting following-method as yank-content.
+
+If region is active, yank region contents instead. \(this feature is available
+if and only if `transient-mark-mode' \(GNU Emacs\) or `zmacs-regions' \(XEmacs\)
+has Non-nil value\)"
   (let ((wl-draft-buffer (current-buffer))
        (mime-view-following-method-alist
         (list (cons 'wl-original-message-mode
@@ -61,10 +62,82 @@ By setting following-method as yank-content."
                     (function wl-draft-yank-to-draft-buffer)))))
     (if (get-buffer (wl-current-message-buffer))
        (save-excursion
+         (set-buffer (wl-current-message-buffer))
          (save-restriction
-           (set-buffer (wl-current-message-buffer))
            (widen)
-           (mime-preview-follow-current-entity))))))
+           (if (wl-region-exists-p)
+               (wl-mime-preview-follow-current-region)
+             (mime-preview-follow-current-entity)))))))
+
+;; modified mime-preview-follow-current-entity from mime-view.el
+(defun wl-mime-preview-follow-current-region ()
+  "Write follow message to current region.
+It calls following-method selected from variable
+`mime-preview-following-method-alist'."
+  (interactive)
+  (let ((r-beg (region-beginning))
+       (r-end (region-end))
+       (entity (get-text-property (point-min)
+                                  'mime-view-entity)))
+    (let* ((mode (mime-preview-original-major-mode 'recursive))
+          (new-name
+           (format "%s-active-region" (buffer-name)))
+          new-buf
+          (the-buf (current-buffer))
+          fields)
+      (save-excursion
+       (set-buffer (setq new-buf (get-buffer-create new-name)))
+       (erase-buffer)
+       (insert ?\n)
+       (insert-buffer-substring the-buf r-beg r-end)
+       (goto-char (point-min))
+       (let ((current-entity
+              (if (and (eq (mime-entity-media-type entity) 'message)
+                       (eq (mime-entity-media-subtype entity) 'rfc822))
+                  (car (mime-entity-children entity))
+                entity)))
+         (while (and current-entity
+                     (if (and (eq (mime-entity-media-type
+                                   current-entity) 'message)
+                              (eq (mime-entity-media-subtype
+                                   current-entity) 'rfc822))
+                         nil
+                       (mime-insert-header current-entity fields)
+                       t))
+           (setq fields (std11-collect-field-names)
+                 current-entity (mime-entity-parent current-entity))
+           ))
+       (let ((rest mime-view-following-required-fields-list)
+             field-name ret)
+         (while rest
+           (setq field-name (car rest))
+           (or (std11-field-body field-name)
+               (progn
+                 (save-excursion
+                   (set-buffer the-buf)
+                   (let ((entity (when mime-mother-buffer
+                                   (set-buffer mime-mother-buffer)
+                                   (get-text-property (point)
+                                                      'mime-view-entity))))
+                     (while (and entity
+                                 (null (setq ret (mime-entity-fetch-field
+                                                  entity field-name))))
+                       (setq entity (mime-entity-parent entity)))))
+                 (if ret
+                     (insert (concat field-name ": " ret "\n"))
+                   )))
+           (setq rest (cdr rest))
+           ))
+       )
+      (let ((f (cdr (assq mode mime-preview-following-method-alist))))
+       (if (functionp f)
+           (funcall f new-buf)
+         (message
+          (format
+           "Sorry, following method for %s is not implemented yet."
+           mode))
+         ))
+      )))
 
 (defalias 'wl-draft-enclose-digest-region 'mime-edit-enclose-digest-region)
 
@@ -176,42 +249,155 @@ By setting following-method as yank-content."
 (defun wl-message-delete-current-part ()
   "Delete a part under the cursor from the multipart message."
   (interactive)
-  (let* ((entity (get-text-property (point) 'mime-view-entity))
-        (node-id (mime-entity-node-id entity))
-        (header-start (mime-buffer-entity-header-start-internal entity))
-        (body-end (mime-buffer-entity-body-end-internal entity))
-        (folder (wl-folder-get-elmo-folder wl-message-buffer-cur-folder))
-        (number wl-message-buffer-cur-number)
-        (msgid (elmo-message-field folder number 'message-id)))
-    (when (and (elmo-folder-writable-p folder)
-              node-id
-              (yes-or-no-p
-               (format "Do you really want to delete part %s? "
-                       (wl-mime-node-id-to-string node-id))))
+  (save-restriction
+    (widen)
+    (let* ((entity (get-text-property (point) 'mime-view-entity))
+          (node-id (mime-entity-node-id entity))
+          (header-start (mime-buffer-entity-header-start-internal entity))
+          (body-end (mime-buffer-entity-body-end-internal entity))
+          (folder (wl-folder-get-elmo-folder wl-message-buffer-cur-folder))
+          (number wl-message-buffer-cur-number)
+          (msgid (elmo-message-field folder number 'message-id))
+          (orig-buf wl-message-buffer-original-buffer))
+      (if (eq (luna-class-name entity) 'mime-elmo-imap-entity)
+         (error "Please fetch the entire message (by typing 'C-u .') and try again"))
+      (with-current-buffer orig-buf
+       (unless (string-equal
+                (buffer-string)
+                (elmo-message-fetch folder number
+                                    (elmo-make-fetch-strategy 'entire)))
+         (error "Buffer content differs from actual message")))
+      (when (and (elmo-folder-writable-p folder)
+                (buffer-live-p orig-buf)
+                node-id
+                (yes-or-no-p
+                 (format "Do you really want to delete part %s? "
+                         (wl-mime-node-id-to-string node-id))))
+       (with-temp-buffer
+         (insert-buffer orig-buf)
+         (delete-region header-start body-end)
+         (goto-char header-start)
+         (insert "Content-Type: text/plain; charset=US-ASCII\n\n")
+         (insert "** This part has been removed by Wanderlust **\n\n")
+         (elmo-folder-append-buffer folder t))
+
+       (elmo-folder-append-messages
+        (wl-folder-get-elmo-folder wl-trash-folder)
+        folder (list number) nil)
+       (elmo-folder-delete-messages folder (list number))
+
+       (when (file-exists-p (elmo-cache-get-path msgid))
+         (delete-file (elmo-cache-get-path msgid)))
+
+       (mime-preview-quit)
+       (wl-summary-toggle-disp-msg 'off)
+       (wl-summary-sync nil "update")))))
+
+(defun wl-message-decrypt-pgp-nonmime ()
+  "Decrypt PGP encrypted region"
+  (interactive)
+  (require 'pgg)
+  (save-excursion
+    (beginning-of-line)
+    (if (or (re-search-forward "^-+END PGP MESSAGE-+$" nil t)
+           (re-search-backward "^-+END PGP MESSAGE-+$" nil t))
+       (let (beg end status)
+         (setq end (match-end 0))
+         (if (setq beg (re-search-backward "^-+BEGIN PGP MESSAGE-+$" nil t))
+             (let ((inhibit-read-only t)
+                   (buffer-file-coding-system wl-cs-autoconv))
+               (setq status (pgg-decrypt-region beg end))
+               (pgg-display-output-buffer beg end status))
+           (message "Cannot find pgp encrypted region")))
+      (message "Cannot find pgp encrypted region"))))
+
+(defun wl-message-verify-pgp-nonmime ()
+  "Verify PGP signed region"
+  (interactive)
+  (require 'pgg)
+  (save-excursion
+    (beginning-of-line)
+    (if (and (or (re-search-forward "^-+END PGP SIGNATURE-+$" nil t)
+                (re-search-backward "^-+END PGP SIGNATURE-+$" nil t))
+            (re-search-backward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t))
+       (let (status m-beg)
+         (let* ((beg (point))
+                (situation (mime-preview-find-boundary-info))
+                (p-end (aref situation 1))
+                (entity (aref situation 2))
+                (count 0))
+           (goto-char p-end)
+           (while (< beg (point))
+             (if (re-search-backward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t)
+                 (setq count (+ count 1))
+               (debug)))
+           (with-temp-buffer
+             (set-buffer-multibyte nil)
+             (insert (mime-entity-body entity))
+             (goto-char (point-max))
+             (while (> count 0)
+               (if (re-search-backward "^-+BEGIN PGP SIGNED MESSAGE-+$" nil t)
+                   (setq count (- count 1))
+                 (debug)))
+             (let ((r-beg (point))
+                   (r-end (re-search-forward "^-+END PGP SIGNATURE-+$" nil t)))
+               (if r-end
+                   (setq status (pgg-verify-region r-beg r-end nil 'fetch))
+                 (debug)))))
+         (mime-show-echo-buffer)
+         (set-buffer mime-echo-buffer-name)
+         (set-window-start
+          (get-buffer-window mime-echo-buffer-name)
+          (point-max))
+         (setq m-beg (point))
+         (insert-buffer-substring
+          (if status pgg-output-buffer pgg-errors-buffer))
+         (encode-coding-region m-beg (point) buffer-file-coding-system)
+         (decode-coding-region m-beg (point) wl-cs-autoconv))
+      (message "Cannot find pgp signed region"))))
+
+;; XXX: encrypted multipart isn't represented as multipart
+(defun wl-mime-preview-application/pgp (parent-entity entity situation)
+  (require 'pgg)
+  (goto-char (point-max))
+  (let ((p (point))
+       raw-buf to-buf representation-type child-entity)
+    (goto-char p)
+    (save-restriction
+      (narrow-to-region p p)
+      (setq to-buf (current-buffer))
       (with-temp-buffer
-       (elmo-message-fetch folder
-                           number
-                           (elmo-make-fetch-strategy 'entire)
+       (setq raw-buf (current-buffer))
+       (mime-insert-entity entity)
+       (when (progn
+               (goto-char (point-min))
+               (re-search-forward "^-+BEGIN PGP MESSAGE-+$" nil t))
+         (pgg-decrypt-region (point-min)(point-max))
+         (delete-region (point-min) (point-max))
+         (insert-buffer pgg-output-buffer)
+         (setq representation-type 'elmo-buffer))
+       (setq child-entity (mime-parse-message
+                           (mm-expand-class-name representation-type)
                            nil
-                           (current-buffer)
-                           'unread)
-       (kill-region header-start body-end)
-       (goto-char header-start)
-       (insert "Content-Type: text/plain; charset=US-ASCII\n\n")
-       (insert "** This part has been removed by Wanderlust **\n\n")
-       (elmo-folder-append-buffer folder t))
-
-      (elmo-folder-append-messages
-       (wl-folder-get-elmo-folder wl-trash-folder)
-       folder (list number) nil)
-      (elmo-folder-delete-messages folder (list number))
-
-      (when (file-exists-p (elmo-cache-get-path msgid))
-       (delete-file (elmo-cache-get-path msgid)))
-
-      (mime-preview-quit)
-      (wl-summary-toggle-disp-msg 'off)
-      (wl-summary-sync nil "update"))))
+                           parent-entity
+                           (mime-entity-node-id-internal parent-entity)))
+       (mime-display-entity
+        child-entity
+        nil
+        `((header . visible)
+          (body . visible)
+          (entity-button . invisible))
+        to-buf)))))
+
+(defun wl-mime-preview-application/pgp-encrypted (entity situation)
+  (let* ((entity-node-id (mime-entity-node-id entity))
+        (mother (mime-entity-parent entity))
+        (knum (car entity-node-id))
+        (onum (if (> knum 0)
+                  (1- knum)
+                (1+ knum)))
+        (orig-entity (nth onum (mime-entity-children mother))))
+    (wl-mime-preview-application/pgp entity orig-entity situation)))
 
 ;;; Summary
 (defun wl-summary-burst-subr (message-entity target number)
@@ -388,6 +574,14 @@ With ARG, ask destination folder."
      (major-mode . wl-original-message-mode)))
 
   (ctree-set-calist-strictly
+   'mime-preview-condition
+   '((type . application)(subtype . pgp-encrypted)
+     (encoding . t)
+     (body . invisible)
+     (body-presentation-method . wl-mime-preview-application/pgp-encrypted)
+     (major-mode . wl-original-message-mode)))
+
+  (ctree-set-calist-strictly
    'mime-acting-condition
    '((type . message) (subtype . partial)
      (method .  wl-mime-combine-message/partial-pieces)