def parseGroupCreated(self,node): jid = node.getAttributeValue("from"); groupNode = node.getChild(0) if ProtocolTreeNode.tagEquals(groupNode,"error"): errorCode = groupNode.getAttributeValue("code") self.signalInterface.send("group_createFail", (errorCode,)) return ProtocolTreeNode.require(groupNode,"group") group_id = groupNode.getAttributeValue("id") self.signalInterface.send("group_createSuccess", (group_id + "@g.us",))
def parseGroupCreated(self,node): jid = node.getAttributeValue("from"); groupNode = node.getChild(0) if ProtocolTreeNode.tagEquals(groupNode,"error"): errorCode = groupNode.getAttributeValue("code") self.signalInterface.send("group_createFail", (errorCode,)) return ProtocolTreeNode.require(groupNode,"group") group_id = groupNode.getAttributeValue("id") self.signalInterface.send("group_createSuccess", (group_id + "@g.us",))
def readSuccess(self): node = self.conn.reader.nextTree() self._d("Login Status: %s" % (node.tag)) if ProtocolTreeNode.tagEquals(node, "failure"): self.authObject.authenticationFailed() return 0 #raise Exception("Login Failure"); ProtocolTreeNode.require(node, "success") expiration = node.getAttributeValue("expiration") if expiration is not None: self._d("Expires: " + str(expiration)) self.authObject.expireDate = expiration kind = node.getAttributeValue("kind") self._d("Account type: %s" % (kind)) if kind == "paid": self.authObject.accountKind = 1 elif kind == "free": self.authObject.accountKind = 0 else: self.authObject.accountKind = -1 status = node.getAttributeValue("status") self._d("Account status: %s" % (status)) if status == "expired": self.loginFailed.emit() raise Exception("Account expired on " + str(self.authObject.expireDate)) if status == "active": if expiration is None: #raise Exception ("active account with no expiration"); '''@@TODO expiration changed to creation''' else: self.authObject.accountKind = 1 self.conn.reader.inn.buf = [] nextChallenge = node.data f = open("/home/user/.wazapp/challenge", "wb") f.write(nextChallenge) f.close() self.conn.writer.outputKey = self.outputKey self.authObject.authenticationComplete() return 1
def readSuccess(self): node = self.conn.reader.nextTree(); self._d("Login Status: %s"%(node.tag)); if ProtocolTreeNode.tagEquals(node,"failure"): self.authObject.authenticationFailed() return 0 #raise Exception("Login Failure"); ProtocolTreeNode.require(node,"success"); expiration = node.getAttributeValue("expiration"); if expiration is not None: self._d("Expires: "+str(expiration)); self.authObject.expireDate = expiration; kind = node.getAttributeValue("kind"); self._d("Account type: %s"%(kind)) if kind == "paid": self.authObject.accountKind = 1; elif kind == "free": self.authObject.accountKind = 0; else: self.authObject.accountKind = -1; status = node.getAttributeValue("status"); self._d("Account status: %s"%(status)); if status == "expired": self.loginFailed.emit() raise Exception("Account expired on "+str(self.authObject.expireDate)); if status == "active": if expiration is None: #raise Exception ("active account with no expiration"); '''@@TODO expiration changed to creation''' else: self.authObject.accountKind = 1; self.conn.reader.inn.buf = []; nextChallenge = node.data; f = open("/home/user/.wazapp/challenge", "wb") f.write(nextChallenge) f.close() self.conn.writer.outputKey = self.outputKey self.authObject.authenticationComplete() return 1
def parseGroupInfo(self,node): jid = node.getAttributeValue("from"); groupNode = node.getChild(0) if "error code" in groupNode.toString(): self.signalInterface.send("group_infoError",(0,)) #@@TODO replace with real error code else: ProtocolTreeNode.require(groupNode,"group") #gid = groupNode.getAttributeValue("id") owner = groupNode.getAttributeValue("owner") subject = groupNode.getAttributeValue("subject") if sys.version_info < (3, 0) else groupNode.getAttributeValue("subject").encode('latin-1').decode(); subjectT = groupNode.getAttributeValue("s_t") subjectOwner = groupNode.getAttributeValue("s_o") creation = groupNode.getAttributeValue("creation") self.signalInterface.send("group_gotInfo",(jid, owner, subject, subjectOwner, int(subjectT),int(creation)))
def parseGroupInfo(self,node): jid = node.getAttributeValue("from"); groupNode = node.getChild(0) if "error code" in groupNode.toString(): self.signalInterface.send("group_infoError",(0,)) #@@TODO replace with real error code else: ProtocolTreeNode.require(groupNode,"group") #gid = groupNode.getAttributeValue("id") owner = groupNode.getAttributeValue("owner") subject = groupNode.getAttributeValue("subject") if sys.version_info < (3, 0) else groupNode.getAttributeValue("subject").encode('latin-1').decode(); subjectT = groupNode.getAttributeValue("s_t") subjectOwner = groupNode.getAttributeValue("s_o") creation = groupNode.getAttributeValue("creation") self.signalInterface.send("group_gotInfo",(jid, owner, subject, subjectOwner, int(subjectT),int(creation)))
def parseLastOnline(self,node): jid = node.getAttributeValue("from"); firstChild = node.getChild(0); if "error" in firstChild.toString(): return ProtocolTreeNode.require(firstChild,"query"); seconds = firstChild.getAttributeValue("seconds"); status = None status = firstChild.data #@@TODO discarded? try: if seconds is not None and jid is not None: self.signalInterface.send("presence_updated", (jid, int(seconds))) except: self._d("Ignored exception in handleLastOnline "+ sys.exc_info()[1])
def parseLastOnline(self,node): jid = node.getAttributeValue("from"); firstChild = node.getChild(0); if "error" in firstChild.toString(): return ProtocolTreeNode.require(firstChild,"query"); seconds = firstChild.getAttributeValue("seconds"); status = None status = firstChild.data #@@TODO discarded? try: if seconds is not None and jid is not None: self.signalInterface.send("presence_updated", (jid, int(seconds))) except: self._d("Ignored exception in handleLastOnline "+ sys.exc_info()[1])
def run(self): self._d("Read thread startedX"); while True: countdown = self.timeout - ((int(time.time()) - self.lastPongTime)) remainder = countdown % self.selectTimeout countdown = countdown - remainder if countdown <= 0: self._d("No hope, dying!") self.sendDisconnected("closed") return else: if countdown % (self.selectTimeout*10) == 0 or countdown < 11: self._d("Waiting, time to die: T-%i seconds" % countdown ) if self.timeout-countdown == 150 and self.ping and self.autoPong: self.ping() self.selectTimeout = 1 if countdown < 11 else 3 try: ready = select.select([self.socket.reader.rawIn], [], [], self.selectTimeout) except: self._d("Error in ready") raise return if self.terminateRequested: return if ready[0]: try: node = self.socket.reader.nextTree() except ConnectionClosedException: #print traceback.format_exc() self._d("Socket closed, got 0 bytes!") #self.signalInterface.send("disconnected", ("closed",)) self.sendDisconnected("closed") return self.lastPongTime = int(time.time()); if node is not None: if ProtocolTreeNode.tagEquals(node,"iq"): iqType = node.getAttributeValue("type") idx = node.getAttributeValue("id") if iqType is None: raise Exception("iq doesn't have type") if iqType == "result": if idx in self.requests: self.requests[idx](node) del self.requests[idx] elif idx.startswith(self.connection.user): accountNode = node.getChild(0) ProtocolTreeNode.require(accountNode,"account") kind = accountNode.getAttributeValue("kind") if kind == "paid": self.connection.account_kind = 1 elif kind == "free": self.connection.account_kind = 0 else: self.connection.account_kind = -1 expiration = accountNode.getAttributeValue("expiration") if expiration is None: raise Exception("no expiration") try: self.connection.expire_date = long(expiration) except ValueError: raise IOError("invalid expire date %s"%(expiration)) self.eventHandler.onAccountChanged(self.connection.account_kind,self.connection.expire_date) elif iqType == "error": if idx in self.requests: self.requests[idx](node) del self.requests[idx] elif iqType == "get": childNode = node.getChild(0) if ProtocolTreeNode.tagEquals(childNode,"ping"): if self.autoPong: self.onPing(idx) self.signalInterface.send("ping", (idx,)) elif ProtocolTreeNode.tagEquals(childNode,"query") and node.getAttributeValue("from") is not None and "http://jabber.org/protocol/disco#info" == childNode.getAttributeValue("xmlns"): pin = childNode.getAttributeValue("pin"); timeoutString = childNode.getAttributeValue("timeout"); try: timeoutSeconds = int(timeoutString) if timeoutString is not None else None except ValueError: raise Exception("relay-iq exception parsing timeout %s "%(timeoutString)) if pin is not None: self.eventHandler.onRelayRequest(pin,timeoutSeconds,idx) elif iqType == "set": childNode = node.getChild(0) if ProtocolTreeNode.tagEquals(childNode,"query"): xmlns = childNode.getAttributeValue("xmlns") if xmlns == "jabber:iq:roster": itemNodes = childNode.getAllChildren("item"); ask = "" for itemNode in itemNodes: jid = itemNode.getAttributeValue("jid") subscription = itemNode.getAttributeValue("subscription") ask = itemNode.getAttributeValue("ask") else: raise Exception("Unkown iq type %s"%(iqType)) elif ProtocolTreeNode.tagEquals(node,"presence"): xmlns = node.getAttributeValue("xmlns") jid = node.getAttributeValue("from") if (xmlns is None or xmlns == "urn:xmpp") and jid is not None: presenceType = node.getAttributeValue("type") if presenceType == "unavailable": self.signalInterface.send("presence_unavailable", (jid,)) elif presenceType is None or presenceType == "available": self.signalInterface.send("presence_available", (jid,)) elif xmlns == "w" and jid is not None: status = node.getAttributeValue("status") if status == "dirty": #categories = self.parseCategories(node); #@@TODO, send along with signal self._d("WILL SEND DIRTY") self.signalInterface.send("status_dirty") self._d("SENT DIRTY") elif ProtocolTreeNode.tagEquals(node,"message"): self.parseMessage(node) self._d("Reader thread terminating now!")
def run(self): self._d("Read thread startedX"); while True: countdown = self.timeout - ((int(time.time()) - self.lastPongTime)) remainder = countdown % self.selectTimeout countdown = countdown - remainder if countdown <= 0: self._d("No hope, dying!") self.sendDisconnected("closed") return else: if countdown % (self.selectTimeout*10) == 0 or countdown < 11: self._d("Waiting, time to die: T-%i seconds" % countdown ) if self.timeout-countdown == 150 and self.ping and self.autoPong: self.ping() self.selectTimeout = 1 if countdown < 11 else 3 try: ready = select.select([self.socket.reader.rawIn], [], [], self.selectTimeout) except: self._d("Error in ready") raise return if self.terminateRequested: return if ready[0]: try: node = self.socket.reader.nextTree() except ConnectionClosedException: #print traceback.format_exc() self._d("Socket closed, got 0 bytes!") #self.signalInterface.send("disconnected", ("closed",)) self.sendDisconnected("closed") return self.lastPongTime = int(time.time()); if node is not None: if ProtocolTreeNode.tagEquals(node,"iq"): iqType = node.getAttributeValue("type") idx = node.getAttributeValue("id") if iqType is None: raise Exception("iq doesn't have type") if iqType == "result": if idx in self.requests: self.requests[idx](node) del self.requests[idx] elif idx.startswith(self.connection.user): accountNode = node.getChild(0) ProtocolTreeNode.require(accountNode,"account") kind = accountNode.getAttributeValue("kind") if kind == "paid": self.connection.account_kind = 1 elif kind == "free": self.connection.account_kind = 0 else: self.connection.account_kind = -1 expiration = accountNode.getAttributeValue("expiration") if expiration is None: raise Exception("no expiration") try: self.connection.expire_date = long(expiration) except ValueError: raise IOError("invalid expire date %s"%(expiration)) self.eventHandler.onAccountChanged(self.connection.account_kind,self.connection.expire_date) elif iqType == "error": if idx in self.requests: self.requests[idx](node) del self.requests[idx] elif iqType == "get": childNode = node.getChild(0) if ProtocolTreeNode.tagEquals(childNode,"ping"): if self.autoPong: self.onPing(idx) self.signalInterface.send("ping", (idx,)) elif ProtocolTreeNode.tagEquals(childNode,"query") and node.getAttributeValue("from") is not None and "http://jabber.org/protocol/disco#info" == childNode.getAttributeValue("xmlns"): pin = childNode.getAttributeValue("pin"); timeoutString = childNode.getAttributeValue("timeout"); try: timeoutSeconds = int(timeoutString) if timeoutString is not None else None except ValueError: raise Exception("relay-iq exception parsing timeout %s "%(timeoutString)) if pin is not None: self.eventHandler.onRelayRequest(pin,timeoutSeconds,idx) elif iqType == "set": childNode = node.getChild(0) if ProtocolTreeNode.tagEquals(childNode,"query"): xmlns = childNode.getAttributeValue("xmlns") if xmlns == "jabber:iq:roster": itemNodes = childNode.getAllChildren("item"); ask = "" for itemNode in itemNodes: jid = itemNode.getAttributeValue("jid") subscription = itemNode.getAttributeValue("subscription") ask = itemNode.getAttributeValue("ask") else: raise Exception("Unkown iq type %s"%(iqType)) elif ProtocolTreeNode.tagEquals(node,"presence"): xmlns = node.getAttributeValue("xmlns") jid = node.getAttributeValue("from") if (xmlns is None or xmlns == "urn:xmpp") and jid is not None: presenceType = node.getAttributeValue("type") if presenceType == "unavailable": self.signalInterface.send("presence_unavailable", (jid,)) elif presenceType is None or presenceType == "available": self.signalInterface.send("presence_available", (jid,)) elif xmlns == "w" and jid is not None: status = node.getAttributeValue("status") if status == "dirty": #categories = self.parseCategories(node); #@@TODO, send along with signal self._d("WILL SEND DIRTY") self.signalInterface.send("status_dirty") self._d("SENT DIRTY") elif ProtocolTreeNode.tagEquals(node,"message"): self.parseMessage(node) self._d("Reader thread terminating now!")