changeset 525:307f84fee972

browser_side (plugin OTR): avoid unecessary waiting time on menu callbacks when the private key or the context doesn't exist
author souliane <souliane@mailoo.org>
date Wed, 03 Sep 2014 19:21:40 +0200
parents d41e850b31b9
children ee38b6a87692
files src/browser/sat_browser/plugin_sec_otr.py
diffstat 1 files changed, 40 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/browser/sat_browser/plugin_sec_otr.py	Wed Sep 03 11:22:37 2014 +0200
+++ b/src/browser/sat_browser/plugin_sec_otr.py	Wed Sep 03 19:21:40 2014 +0200
@@ -42,7 +42,7 @@
 AUTH_QUEST_DEF = D_("<i>To authenticate using a question, pick a question whose answer is known only to you and your correspondent. Enter this question and this answer, then wait for your correspondent to enter the answer too. If the answers don't match, then you may be talking to an imposter.</i>{eol}{eol}").format(eol=DIALOG_EOL)
 AUTH_QUEST_ASK = D_("<i>Your correspondent is attempting to determine if he or she is really talking to you, or if it's someone pretending to be you. Your correspondent has asked a question, indicated below. To authenticate to your correspondent, enter the answer and click OK.</i>{eol}{eol}").format(eol=DIALOG_EOL)
 AUTH_SECRET_TXT = D_("{eol}{eol}Enter secret answer here: (case sensitive){eol}").format(eol=DIALOG_EOL)
-
+FEEDBACK_NOT_PRIVATE = D_("You haven't started any private conversation with %s yet.")
 
 DROP_TXT = D_("You private key is used to encrypt messages for your correspondent, nobody except you must know it, if you are in doubt, you should drop it!{eol}{eol}Are you sure you want to drop your private key?").format(eol=DIALOG_EOL)
 
@@ -123,7 +123,7 @@
 
         elif status == otr.context.STATUS_END_OTR:
             if msg_state == otr.context.STATE_PLAINTEXT:
-                feedback = _("You haven't start any private conversation with %s yet.")
+                feedback = FEEDBACK_NOT_PRIVATE
             elif msg_state == otr.context.STATE_ENCRYPTED:
                 feedback = _("%s has ended his/her private conversation with you; you should do the same.")
             elif msg_state == otr.context.STATE_FINISHED:
@@ -253,19 +253,22 @@
         self.account = Account(host)
         self.contexts = {}
 
-    def startContext(self, other_jid):
+    def startContext(self, other_jid, create=True):
         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:
-            self.contexts[other_jid] = Context(self.host, self.account, other_jid)
+            if create:
+                self.contexts[other_jid] = Context(self.host, self.account, other_jid)
+            else:
+                return None
         return self.contexts[other_jid]
 
-    def getContextForUser(self, other):
+    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)
+        return self.startContext(other, create)
 
 
 class OTR(object):
@@ -359,7 +362,18 @@
         @param menu_data: %(menu_data)s
         """
         def cb(other_jid):
-            otrctx = self.context_manager.getContextForUser(other_jid)
+            def not_available():
+                self.host.newMessageCb(other_jid, FEEDBACK_NOT_PRIVATE % other_jid.full(), "headline", self.host.whoami, {})
+
+            priv_key = self.context_manager.account.privkey
+            if priv_key is None:
+                not_available()
+                return
+
+            otrctx = self.context_manager.getContextForUser(other_jid, create=False)
+            if otrctx is None:
+                not_available()
+                return
             otrctx.disconnect()
         try:
             other_jid = menu_data['jid']
@@ -374,15 +388,26 @@
         @param menu_data: %(menu_data)s
         @param profile: %(doc_profile)s
         """
+        def not_available():
+            dialog.InfoDialog(_("No running session"), _("You must start a private conversation before authenticating your correspondent."), AddStyleName="maxWidthLimit").show()
+
+        priv_key = self.context_manager.account.privkey
+        if priv_key is None:
+            not_available()
+            return
+
         def cb(to_jid):
-            otrctx = self.context_manager.getContextForUser(to_jid)
+            otrctx = self.context_manager.getContextForUser(to_jid, create=False)
+            if otrctx is None:
+                not_available()
+                return
             otr_version = otrctx.getUsedVersion()
             if otr_version == otr.context.OTR_VERSION_2:
                 otrctx.fingerprintAuthCb()
             elif otr_version == otr.context.OTR_VERSION_3:
                 otrctx.smpAuthCb('question', None, 'asked')
             else:
-                dialog.InfoDialog(_("No running session"), _("You must start a private conversation before authenticating your correspondent."), AddStyleName="maxWidthLimit").show()
+                not_available()
 
         try:
             to_jid = menu_data['jid']
@@ -397,14 +422,13 @@
         @param menu_data: %(menu_data)s
         @param profile: %(doc_profile)s
         """
-        def cb(to_jid):
-            priv_key = self.context_manager.account.privkey
+        priv_key = self.context_manager.account.privkey
+        if priv_key is None:
+            # we have no private key yet
+            dialog.InfoDialog(_("No private key"), _("You don't have any private key yet."), AddStyleName="maxWidthLimit").show()
+            return
 
-            if priv_key is None:
-                # we have no private key yet
-                dialog.InfoDialog(_("No private key"), _("You don't have any private key yet!"), AddStyleName="maxWidthLimit").show()
-                return
-
+        def cb(to_jid):
             def dropKey(confirm):
                 if confirm:
                     # we end all sessions