changeset 537:cd492c18b366

browser_side (plugin OTR): manage the plain text messages ourselves rather than leaving it to otr.js
author souliane <souliane@mailoo.org>
date Sun, 07 Sep 2014 19:28:27 +0200
parents 048ae7314156
children 3317e5d0ac1d
files src/browser/sat_browser/otrjs_wrapper.py src/browser/sat_browser/plugin_sec_otr.py
diffstat 2 files changed, 39 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/browser/sat_browser/otrjs_wrapper.py	Sun Sep 07 16:40:33 2014 +0200
+++ b/src/browser/sat_browser/otrjs_wrapper.py	Sun Sep 07 19:28:27 2014 +0200
@@ -53,8 +53,12 @@
     STATE_PLAINTEXT = None
     STATE_ENCRYPTED = None
     STATE_FINISHED = None
+    OTR_TAG = None
     OTR_VERSION_2 = None
     OTR_VERSION_3 = None
+    WHITESPACE_TAG = None
+    WHITESPACE_TAG_V2 = None
+    WHITESPACE_TAG_V3 = None
 
     JS("""
     $cls_definition['STATUS_SEND_QUERY'] = OTR.CONST.STATUS_SEND_QUERY;
@@ -64,8 +68,12 @@
     $cls_definition['STATE_PLAINTEXT'] = OTR.CONST.MSGSTATE_PLAINTEXT;
     $cls_definition['STATE_ENCRYPTED'] = OTR.CONST.MSGSTATE_ENCRYPTED;
     $cls_definition['STATE_FINISHED'] = OTR.CONST.MSGSTATE_FINISHED;
+    $cls_definition['OTR_TAG'] = OTR.CONST.OTR_TAG;
     $cls_definition['OTR_VERSION_2'] = OTR.CONST.OTR_VERSION_2;
     $cls_definition['OTR_VERSION_3'] = OTR.CONST.OTR_VERSION_3;
+    $cls_definition['WHITESPACE_TAG'] = OTR.CONST.WHITESPACE_TAG;
+    $cls_definition['WHITESPACE_TAG_V2'] = OTR.CONST.WHITESPACE_TAG_V2;
+    $cls_definition['WHITESPACE_TAG_V3'] = OTR.CONST.WHITESPACE_TAG_V3;
     """)
 
     class UnencryptedMessage(Exception):
@@ -229,5 +237,27 @@
             JS("""return DSA.parsePrivate(key);""")
 
 
+class proto(object):
+
+    @classmethod
+    def checkForOTR(cls, body):
+        """Helper method to check if the message contains OTR starting tag or whitespace
+
+        @return:
+            - context.OTR_TAG if the message starts with it
+            - context.WHITESPACE_TAG if the message contains OTR whitespaces
+            - None otherwise
+        """
+        if body.startswith(context.OTR_TAG):
+            return context.OTR_TAG
+        index = body.find(context.WHITESPACE_TAG)
+        if index < 0:
+            return False
+        tags = [body[i:i + 8] for i in range(index, len(body), 8)]
+        if [True for tag in tags if tag in (context.WHITESPACE_TAG_V2, context.WHITESPACE_TAG_V3)]:
+            return context.WHITESPACE_TAG
+        return None
+
+
 # serialazePrivateKey is the method name in potr
 JS("""DSA.serializePrivateKey = DSA.packPrivate;""")
--- a/src/browser/sat_browser/plugin_sec_otr.py	Sun Sep 07 16:40:33 2014 +0200
+++ b/src/browser/sat_browser/plugin_sec_otr.py	Sun Sep 07 19:28:27 2014 +0200
@@ -50,6 +50,8 @@
     'ALLOW_V2': True,
     'ALLOW_V3': True,
     'REQUIRE_ENCRYPTION': False,
+    'SEND_WHITESPACE_TAG': False,
+    'WHITESPACE_START_AKE': False
 }
 
 # list a couple of texts (untrusted, trusted) for each state
@@ -92,6 +94,8 @@
 
     def receiveMessageCb(self, msg, encrypted):
         assert isinstance(self.peer, jid.JID)
+        if not encrypted:
+            log.warning("A plain-text message has been handled by otr.js")
         log.debug("message received (was %s): %s" % ('encrypted' if encrypted else 'plain', msg))
         if not encrypted:
             if self.state == otr.context.STATE_ENCRYPTED:
@@ -348,13 +352,17 @@
     def messageReceivedTrigger(self, from_jid, msg, msg_type, to_jid, extra):
         if msg_type == C.MESS_TYPE_INFO:
             return True
-        other_jid = to_jid if from_jid.bare == self.host.whoami.bare else from_jid
+
+        tag = otr.proto.checkForOTR(msg)
+        if tag is None or (tag == otr.context.WHITESPACE_TAG and not DEFAULT_POLICY_FLAGS['WHITESPACE_START_AKE']):
+            return True  # TODO: signal the user that the contact wants to speak OTR
 
         def cb(jid):
             otrctx = self.context_manager.getContextForUser(jid)
             otrctx.receiveMessage(msg)
             return False  # interrupt the main process
 
+        other_jid = to_jid if from_jid.bare == self.host.whoami.bare else from_jid
         self.fixResource(other_jid, cb)
 
     def sendMessageTrigger(self, to_jid, msg, msg_type, extra):