diff src/browser/sat_browser/otrjs_wrapper.py @ 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 1fa173107fde
children cd56f2b0e85b
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;""")