Don't alter message-text if decryption fails.
[riece] / lisp / riece-epg.el
index 4a93efb..542bcd2 100644 (file)
@@ -1,5 +1,5 @@
 (require 'riece-message)
-(require 'riece-coding)
+(require 'riece-identity)
 
 (autoload 'epg-make-context "epg")
 (autoload 'epg-decrypt-string "epg")
@@ -21,7 +21,7 @@
 
 (defun riece-epg-passphrase-callback-function (key-id identity)
   (if (eq key-id 'SYM)
-      (let ((entry (assoc identity riece-epg-passphrase-alist))
+      (let ((entry (riece-identity-assoc identity riece-epg-passphrase-alist))
            passphrase)
        (or (copy-sequence (cdr entry))
            (progn
   "Encrypt the current line send send it to the current channel."
   (interactive)
   (let ((context (epg-make-context))
-       (string (riece-encode-coding-string
-                (buffer-substring
-                 (riece-line-beginning-position)
-                 (riece-line-end-position))))
+       (string (buffer-substring
+                (riece-line-beginning-position)
+                (riece-line-end-position)))
        entry)
+    (riece-with-server-buffer (riece-identity-server riece-current-channel)
+      (setq string (riece-encode-coding-string-for-identity
+                   string
+                   riece-current-channel)))
     (epg-context-set-passphrase-callback
      context
      (cons #'riece-epg-passphrase-callback-function
@@ -50,8 +53,8 @@
     (condition-case error
        (setq string (epg-encrypt-string context string nil))
       (error
-       (if (setq entry (assoc riece-current-channel
-                             riece-epg-passphrase-alist))
+       (if (setq entry (riece-identity-assoc riece-current-channel
+                                            riece-epg-passphrase-alist))
           (setcdr entry nil))
        (signal (car error) (cdr error))))
     (riece-command-send-message
                          (riece-message-text message))
        (let ((context (epg-make-context))
              (string (match-string 1 (riece-message-text message)))
+             (coding-system (or (riece-coding-system-for-identity
+                                 (riece-message-target message))
+                                riece-default-coding-system))
              entry)
          (epg-context-set-passphrase-callback
           context
           (cons #'riece-epg-passphrase-callback-function
                 (riece-message-target message)))
          (condition-case error
-             (setq string (epg-decrypt-string
-                           context
-                           (base64-decode-string string)))
+             (riece-message-set-text
+              message
+              (concat
+               "[OpenPGP Decrypted:"
+               (decode-coding-string
+                (epg-decrypt-string context (base64-decode-string string))
+                (if (consp coding-system)
+                    (car coding-system)
+                  coding-system))
+               "]"))
            (error
-            (if (setq entry (assoc (riece-message-target message)
-                                   riece-epg-passphrase-alist))
+            (if (setq entry (riece-identity-assoc
+                             (riece-message-target message)
+                             riece-epg-passphrase-alist))
                 (setcdr entry nil))
-            (message "%s" (cdr error))))
-         (riece-message-set-text message
-                                 (riece-decode-coding-string string)))))
+            (message "%s" (cdr error)))))))
   message)
 
 (defun riece-epg-insinuate ()