Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 56 additions & 3 deletions backend/whatsapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from utilities import *;
from whatsapp_binary_reader import whatsappReadBinary;

WHATSAPP_WEB_VERSION="2,2121,6"
WHATSAPP_WEB_VERSION="2,2136,10"

reload(sys);
sys.setdefaultencoding("utf-8");
Expand Down Expand Up @@ -181,6 +181,22 @@ def onMessage(self, ws, message):
try:
processedData = whatsappReadBinary(decryptedMessage, True);
messageType = "binary";

# sort contacts obj{jid : name}
try:
if processedData[1]['type'] is "contacts":
messageType = "jsonContacts";
processedData.append(self.sortedContacts(processedData))
except:
pass
# sort statuses
try:
if processedData[2][0][0] is "status":
processedData[2] = self.sortedStatuses(processedData)
messageType = "jsonStatuses";
except:
pass
Comment on lines +186 to +198
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these exceptions caught as a result of, say, processedData[1]['type'] throwing IndexError or something?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, some responses don’t have a 'type' key, like 'action' response.
so sometimes it throws an error but try/except will handle that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these exceptions caught as a result of, say, processedData[1]['type'] throwing IndexError or something?

@quad any problem ?


except:
processedData = { "traceback": traceback.format_exc().splitlines() };
messageType = "error";
Expand Down Expand Up @@ -209,7 +225,7 @@ def onMessage(self, ws, message):
self.loginInfo["key"]["encKey"] = keysDecrypted[:32];
self.loginInfo["key"]["macKey"] = keysDecrypted[32:64];

self.save_session();
self.saveSession();
# eprint("private key : ", base64.b64encode(self.loginInfo["privateKey"].serialize()));
# eprint("secret : ", base64.b64encode(self.connInfo["secret"]));
# eprint("shared secret : ", base64.b64encode(self.connInfo["sharedSecret"]));
Expand All @@ -229,6 +245,7 @@ def onMessage(self, ws, message):
eprint(json.dumps( [messageTag,["admin","challenge",challenge,self.connInfo["serverToken"],self.loginInfo["clientId"]]]))
self.activeWs.send(json.dumps( [messageTag,["admin","challenge",challenge,self.connInfo["serverToken"],self.loginInfo["clientId"]]]));
elif jsonObj[0] == "Stream":
self.getStatuses(); # request for contacts statuses
pass;
elif jsonObj[0] == "Props":
pass;
Expand Down Expand Up @@ -276,14 +293,50 @@ def restoreSession(self, callback=None):
"serverToken"] + '", "' + self.loginInfo["clientId"] + '", "takeover"]'

self.activeWs.send(message)
def save_session(self):
def saveSession(self):
session = {"clientToken":self.connInfo["clientToken"],"serverToken":self.connInfo["serverToken"],
"clientId":self.loginInfo["clientId"],"macKey": self.loginInfo["key"]["macKey"].decode("latin_1")
,"encKey": self.loginInfo["key"]["encKey"].decode("latin_1")};
f = open("./session.json","w")
f.write(json.dumps(session))
f.close()

def sortedContacts(self,processedData):
contacts = {}
for contact in range(len(processedData[2])):
if 'name' in processedData[2][contact][1].keys() :
contacts[processedData[2][contact][1]['jid']] = processedData[2][contact][1]['name']
return contacts

def getStatuses(self):
messageId = "3EB0"+binascii.hexlify(Random.get_random_bytes(8)).upper()
encryptedMessage = WhatsAppEncrypt(
self.loginInfo["key"]["encKey"],
self.loginInfo["key"]["macKey"],
whatsappWriteBinary(["query", {"type": "status","jid":""}, None])
)
payload = bytearray(messageId) + bytearray(",") + bytearray(
to_bytes(WAMetrics.QUERY_MEDIA, 1)
) + bytearray([0x80]) + encryptedMessage
self.activeWs.send(payload, websocket.ABNF.OPCODE_BINARY)

def sortedStatuses(self,processedData):
entries = {}
bad = []
for user in range(len(processedData[2])):
jid = processedData[2][user][1]['jid']
for story in range(len(processedData[2][user][2])):
if processedData[2][user][2][story][0] == "picture" :
bad.append(story)
continue
decoded_msgs = WAWebMessageInfo.decode(processedData[2][user][2][story][2])
processedData[2][user][2][story] = decoded_msgs['message']
entries[jid] = processedData[2][user][2]
for b in bad:
del entries[jid][b]
return entries


def getLoginInfo(self, callback):
callback["func"]({ "type": "login_info", "data": self.loginInfo }, callback);

Expand Down