def __init__(self,cc, p_in, idx, method="sms"): super(WACodeRequest,self).__init__(); self.p_in = p_in #number self.addParam("cc", cc); self.addParam("in", p_in); self.addParam("lc", "US"); self.addParam("lg", "en"); self.addParam("mcc", "000"); self.addParam("mnc", "000"); self.addParam("method", method); self.addParam("id", idx) self.currentToken = Utilities.readToken() if self.currentToken: print("Read token from %s " % os.path.expanduser(Constants.tokenStorage)) else: self.currentToken = Constants.tokenData self.addParam("token", self.getToken(p_in, self.currentToken["t"])) self.url = "v.whatsapp.net/v2/code" self.pvars = ["status","reason","length", "method", "retry_after", "code", "param"] +\ ["login", "pw", "type", "expiration", "kind", "price", "cost", "currency", "price_expiration"] self.setParser(JSONResponseParser())
def send(self, parser=None): res = super(WACodeRequest, self).send(parser) #attempt recovery by fetching new token if res: if res["status"] == "fail": if res["reason"] in ("old_version", "bad_token" ) and Utilities.tokenCacheEnabled: print("Failed, reason: %s. Checking for a new token.." % res["reason"]) res = WARequest.sendRequest(Constants.tokenSource[0], 80, Constants.tokenSource[1], {}, {}) if res: tokenData = res.read() pvars = ["v", "r", "u", "t", "d"] jParser = JSONResponseParser() parsed = jParser.parse(tokenData.decode(), pvars) if (parsed["v"] != self.currentToken["v"] or parsed["r"] != self.currentToken["r"] or parsed["u"] != self.currentToken["u"] or parsed["t"] != self.currentToken["t"] or parsed["d"] != self.currentToken["d"]): self.currentToken = parsed print("Fetched a new token, persisting !") self.removeParam("token") print("Now retrying the request..") self.addParam( "token", self.getToken(self.p_in, self.currentToken["t"])) else: print("No new tokens :(") res = super(WACodeRequest, self).send(parser) if res and res["status"] != "fail": Utilities.persistToken(tokenData) #good token return res
def send(self, parser = None): res = super(WACodeRequest, self).send(parser) #attempt recovery by fetching new token if res: if res["status"] == "fail": if res["reason"] in ("old_version", "bad_token") and Utilities.tokenCacheEnabled: print("Failed, reason: %s. Checking for a new token.." % res["reason"]) res = WARequest.sendRequest(Constants.tokenSource[0], 80, Constants.tokenSource[1], {}, {}) if res: tokenData = res.read() pvars = ["v", "r", "u", "t", "d"] jParser = JSONResponseParser() parsed = jParser.parse(tokenData.decode(), pvars) if( parsed["v"] != self.currentToken["v"] or parsed["r"] != self.currentToken["r"] or parsed["u"] != self.currentToken["u"] or parsed["t"] != self.currentToken["t"] or parsed["d"] != self.currentToken["d"] ): self.currentToken = parsed print("Fetched a new token, persisting !") self.removeParam("token") print("Now retrying the request..") self.addParam("token", self.getToken(self.p_in, self.currentToken["t"])) else: print("No new tokens :(") res = super(WACodeRequest, self).send(parser) if res and res["status"] != "fail": Utilities.persistToken(tokenData) #good token return res
def __init__(self,cc, p_in, idx): super(WAExistsRequest,self).__init__(); self.addParam("cc",cc); self.addParam("in",p_in); self.addParam("id", idx); self.url = "v.whatsapp.net/v2/exist" self.pvars = ["status", "reason", "sms_length", "voice_length", "result","param", "pw", "login", "type", "expiration", "kind", "price", "cost", "currency", "price_expiration" ] self.setParser(JSONResponseParser())
def __init__(self, cc, p_in, code, idx): super(WARegRequest, self).__init__() self.addParam("cc", cc) self.addParam("in", p_in) self.addParam("id", idx) self.addParam("code", code) self.url = "v.whatsapp.net/v2/register" self.pvars = [ "status", "login", "pw", "type", "expiration", "kind", "price", "cost", "currency", "price_expiration", "reason", "retry_after" ] self.setParser(JSONResponseParser())
def __init__(self, username, password, nonce="0"): super(WAContactsSyncAuth, self).__init__() self.url = "sro.whatsapp.net/v2/sync/a" self.type = "POST" cnonce = Utilities.str( random.randint(100000000000000, 1000000000000000), 36) credentials = bytearray((username + ":s.whatsapp.net:").encode()) credentials.extend(password) if sys.version_info >= (3, 0): buf = lambda x: bytes(x, 'iso-8859-1') if type( x) is str else bytes(x) else: buf = buffer response = self.encode( self.md5( self.encode( self.md5( self.md5(buf(credentials)) + (":" + nonce + ":" + cnonce).encode())) + (":" + nonce + ":" + WAContactsSyncAuth.nc + ":" + cnonce + ":auth:").encode() + self.encode( self.md5(("AUTHENTICATE:" + WAContactsSyncAuth.digestUri ).encode())))).decode() authField = WAContactsSyncAuth.authTemplate.format( auth_method=WAContactsSyncAuth.authMethod, username=username, realm=WAContactsSyncAuth.realm, nonce=nonce, cnonce=cnonce, nc=WAContactsSyncAuth.nc, digest_uri=WAContactsSyncAuth.digestUri, response=response) self.addHeaderField("Authorization", authField) self.pvars = ["message"] self.setParser(JSONResponseParser())
def __init__(self,cc, p_in, idx, method="sms"): super(WACodeRequest,self).__init__(); self.addParam("cc", cc); self.addParam("in", p_in); self.addParam("lc", "US"); self.addParam("lg", "en"); self.addParam("mcc", "000"); self.addParam("mnc", "000"); self.addParam("method", method); self.addParam("id", idx) self.addParam("token", self.getToken(p_in)) self.url = "v.whatsapp.net/v2/code" self.pvars = ["status","reason","length", "method", "retry_after", "code", "param"] +\ ["login", "pw", "type", "expiration", "kind", "price", "cost", "currency", "price_expiration"] self.setParser(JSONResponseParser())