diff src/browser/sat_browser/plugin_sec_otr.py @ 528:ac66b8b11ab8

plugin OTR: fixes handling of the FINISHED state
author souliane <souliane@mailoo.org>
date Fri, 05 Sep 2014 10:47:17 +0200
parents 307f84fee972
children 9bfd71e2b35c
line wrap: on
line diff
--- a/src/browser/sat_browser/plugin_sec_otr.py	Fri Sep 05 10:40:52 2014 +0200
+++ b/src/browser/sat_browser/plugin_sec_otr.py	Fri Sep 05 10:47:17 2014 +0200
@@ -253,22 +253,22 @@
         self.account = Account(host)
         self.contexts = {}
 
-    def startContext(self, other_jid, create=True):
+    def startContext(self, other_jid):
         assert isinstance(other_jid, jid.JID)
         # FIXME upstream: apparently pyjamas doesn't implement setdefault well, it ignores JID.__hash__ redefinition
         #context = self.contexts.setdefault(other_jid, Context(self.host, self.account, other_jid))
         if other_jid not in self.contexts:
-            if create:
-                self.contexts[other_jid] = Context(self.host, self.account, other_jid)
-            else:
-                return None
+            self.contexts[other_jid] = Context(self.host, self.account, other_jid)
         return self.contexts[other_jid]
 
     def getContextForUser(self, other, create=True):
         log.debug(u"getContextForUser [%s]" % other)
         if not other.resource:
             log.error("getContextForUser called with a bare jid")
-        return self.startContext(other, create)
+        if start:
+            return self.startContext(other_jid)
+        else:
+            return self.contexts.get(other_jid, None)
 
 
 class OTR(object):
@@ -323,11 +323,15 @@
 
     def sendMessageTrigger(self, to_jid, msg, msg_type, extra):
         def cb(jid):
-            otrctx = self.context_manager.getContextForUser(jid)
-            if msg_type != 'groupchat' and otrctx.state == otr.context.STATE_ENCRYPTED:
-                log.debug(u"encrypting message")
-                otrctx.sendMessage(msg)
-                self.host.newMessageCb(self.host.whoami, msg, msg_type, jid, extra)
+            otrctx = self.context_manager.getContextForUser(jid, start=False)
+            if otrctx is not None and msg_type != 'groupchat' and otrctx.state != otr.context.STATE_PLAINTEXT:
+                if otrctx.state == otr.context.STATE_ENCRYPTED:
+                    log.debug(u"encrypting message")
+                    otrctx.sendMessage(msg)
+                    self.host.newMessageCb(self.host.whoami, msg, msg_type, jid, extra)
+                else:
+                    feedback = D_("Your message was not sent because your correspondent closed the encrypted conversation on his/her side. Either close your own side, or refresh the session.")
+                    dialog.InfoDialog(_('Finished encrypted session'), feedback, AddStyleName="maxWidthLimit").show()
             else:
                 log.debug(u"sending message unencrypted")
                 self.host.bridge.call('sendMessage', (None, self.host.sendError), to_jid.full(), msg, '', msg_type, extra)
@@ -397,7 +401,7 @@
             return
 
         def cb(to_jid):
-            otrctx = self.context_manager.getContextForUser(to_jid, create=False)
+            otrctx = self.context_manager.getContextForUser(to_jid, start=False)
             if otrctx is None:
                 not_available()
                 return