* wl-summary.el (wl-summary-rescan): Use numbers in the msgdb.
[elisp/wanderlust.git] / wl / wl-mime.el
index 19e2fc0..d6f467b 100644 (file)
 (require 'mime-play)
 (require 'elmo)
 
+(eval-when-compile
+  (defalias-maybe 'pgg-decrypt-region 'ignore)
+  (defalias-maybe 'pgg-display-output-buffer 'ignore)
+  (defalias-maybe 'pgg-verify-region 'ignore))
+
 ;;; Draft
 
 (defalias 'wl-draft-editor-mode 'mime-edit-mode)
@@ -128,9 +133,8 @@ It calls following-method selected from variable
        (if (functionp f)
            (funcall f new-buf)
          (message
-          (format
-           "Sorry, following method for %s is not implemented yet."
-           mode))
+          "Sorry, following method for %s is not implemented yet."
+          mode)
          ))
       )))
 
@@ -147,7 +151,7 @@ It calls following-method selected from variable
         (mime-header-encode-method-alist
          (append
           '((wl-draft-eword-encode-address-list
-             .  (To Cc Bcc Resent-To Resent-Cc Bcc Resent-Bcc)))
+             .  (To Cc Bcc Resent-To Resent-Cc Resent-Bcc From)))
           (if (boundp 'mime-header-encode-method-alist)
               (symbol-value 'mime-header-encode-method-alist))))
         mime-view-ignored-field-list   ; all header.
@@ -187,7 +191,7 @@ It calls following-method selected from variable
       (when wl-highlight-body-too
        (wl-highlight-body))
       (run-hooks 'wl-draft-preview-message-hook))
-    (message recipients-message)))
+    (message "%s" recipients-message)))
 
 (defalias 'wl-draft-caesar-region  'mule-caesar-region)
 
@@ -268,25 +272,131 @@ It calls following-method selected from variable
                 (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")))))
+       (when (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))
+
+         (elmo-folder-move-messages
+          folder (list number)
+          (wl-folder-get-elmo-folder wl-trash-folder))
+         (when (and (elmo-cache-get-path msgid)
+                    (file-exists-p (elmo-cache-get-path msgid)))
+           (delete-file (elmo-cache-get-path msgid)))
+
+         (mime-preview-quit)
+         (wl-summary-delete-messages-on-buffer (list number))
+         (wl-summary-toggle-disp-msg 'off)
+         (setq wl-message-buffer nil)
+         (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))
+         (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
+       (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
+                           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)
@@ -306,9 +416,7 @@ It calls following-method selected from variable
                 (car (mime-entity-children message-entity)))
           (with-temp-buffer
             (insert (mime-entity-body message-entity))
-            (elmo-folder-append-buffer
-             target
-             (mime-entity-fetch-field entity "Message-ID")))))
+            (elmo-folder-append-buffer target))))
     number))
 
 (defun wl-summary-burst (&optional arg)
@@ -317,12 +425,12 @@ With ARG, ask destination folder."
   (interactive "P")
   (let ((raw-buf (wl-summary-get-original-buffer))
        (view-buf wl-message-buffer)
-       children message-entity content-type target)
+       children message-entity content-type target-name target)
     (save-excursion
       (setq target wl-summary-buffer-elmo-folder)
       (when (or arg (not (elmo-folder-writable-p target)))
-       (let ((fld (wl-summary-read-folder wl-default-folder "to extract to")))
-         (setq target (wl-folder-get-elmo-folder fld))))
+       (setq target-name (wl-summary-read-folder wl-default-folder "to extract to"))
+       (setq target (wl-folder-get-elmo-folder target-name)))
       (wl-summary-set-message-buffer-or-redisplay)
       (with-current-buffer view-buf
        (setq message-entity (get-text-property (point-min) 'mime-view-entity)))
@@ -333,7 +441,9 @@ With ARG, ask destination folder."
        (message "Bursting...done"))
       (if (elmo-folder-plugged-p target)
          (elmo-folder-check target)))
-    (wl-summary-sync-update)))
+    (when (or (not target-name)
+             (string= wl-summary-buffer-folder-name target-name))
+      (save-excursion (wl-summary-sync-update)))))
 
 ;; internal variable.
 (defvar wl-mime-save-directory nil "Last saved directory.")
@@ -359,9 +469,9 @@ With ARG, ask destination folder."
 (defun wl-mime-combine-message/partial-pieces (entity situation)
   "Internal method for wl to combine message/partial messages automatically."
   (interactive)
-  (let* ((msgdb (save-excursion
-                 (set-buffer wl-message-buffer-cur-summary-buffer)
-                 (wl-summary-buffer-msgdb)))
+  (let* ((folder (save-excursion
+                  (set-buffer wl-message-buffer-cur-summary-buffer)
+                  wl-summary-buffer-elmo-folder))
         (mime-display-header-hook 'wl-highlight-headers)
         (folder wl-message-buffer-cur-folder)
         (id (or (cdr (assoc "id" situation)) ""))
@@ -394,18 +504,16 @@ With ARG, ask destination folder."
              wl-summary-buffer-mime-charset)))
       (if (string-match "[0-9\n]+" subject-id)
          (setq subject-id (substring subject-id 0 (match-beginning 0))))
-      (setq overviews (elmo-msgdb-get-overview msgdb))
       (catch 'tag
-       (while overviews
+       (elmo-folder-do-each-message-entity (entity folder)
          (when (string-match
                 (regexp-quote subject-id)
-                (elmo-msgdb-overview-entity-get-subject (car overviews)))
+                (elmo-message-entity-field entity 'subject))
            (let* ((message
                    ;; request message at the cursor in Subject buffer.
                    (wl-message-request-partial
                     folder
-                    (elmo-msgdb-overview-entity-get-number
-                     (car overviews))))
+                    (elmo-message-entity-number entity)))
                   (situation (mime-entity-situation message))
                   (the-id (or (cdr (assoc "id" situation)) "")))
              (when (string= (downcase the-id)
@@ -413,8 +521,7 @@ With ARG, ask destination folder."
                (with-current-buffer mother
                  (mime-store-message/partial-piece message situation))
                (if (file-exists-p full-file)
-                   (throw 'tag nil)))))
-         (setq overviews (cdr overviews)))
+                   (throw 'tag nil))))))
        (message "Not all partials found.")))))
 
 (defun wl-mime-display-text/plain (entity situation)
@@ -463,6 +570,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)