def handleEncNode(self, node): recipient_id = node["to"].split('@')[0] v2 = node["to"] if node.getChild("enc"): # media enc is only for v2 messsages if '-' in recipient_id: ## Handle Groups def getResultNodes(resultNode, requestEntity): groupInfo = InfoGroupsResultIqProtocolEntity.fromProtocolTreeNode(resultNode) jids = list(groupInfo.getParticipants().keys()) #keys in py3 returns dict_keys jids.remove(self.getLayerInterface(YowAuthenticationProtocolLayer).getUsername(True)) jidsNoSession = [] for jid in jids: if not self.store.containsSession(jid.split('@')[0], 1): jidsNoSession.append(jid) else: pass if len(jidsNoSession): self.getKeysFor(jidsNoSession, lambda successJids, b: self.sendToGroupWithSessions(node, successJids)) else: self.sendToGroupWithSessions(node, jids) groupInfoIq = InfoGroupsIqProtocolEntity(Jid.normalize(node["to"])) self._sendIq(groupInfoIq, getResultNodes) else: messageData = self.serializeToProtobuf(node) if messageData: if not self.store.containsSession(recipient_id, 1): def on_get_keys(successJids,b): print(successJids) if len(successJids) == 1: self.sendToContact(node) else: self.toLower(node) self.getKeysFor([node["to"]],on_get_keys, lambda: self.toLower(node)) else : sessionCipher = self.getSessionCipher(recipient_id) messageData = messageData.SerializeToString() + self.getPadding() ciphertext = sessionCipher.encrypt(messageData) mediaType = node.getChild("enc")["type"] if node.getChild("enc") else None encEntity = EncryptedMessageProtocolEntity( [ EncProtocolEntity( EncProtocolEntity.TYPE_MSG if ciphertext.__class__ == WhisperMessage else EncProtocolEntity.TYPE_PKMSG, 2 if v2 else 1, ciphertext.serialize(), mediaType)], "text" if not mediaType else "media", _id=node["id"], to=node["to"], notify=node["notify"], timestamp=node["timestamp"], participant=node["participant"], offline=node["offline"], retry=node["retry"] ) print(encEntity.toProtocolTreeNode()) self.toLower(encEntity.toProtocolTreeNode()) else: # case of unserializable messages (audio, video) ? self.toLower(node) else: self.toLower(node)
def sendToGroup(self, node, retryReceiptEntity=None): groupJid = node["to"] ownNumber = self.getLayerInterface( YowAuthenticationProtocolLayer).getUsername(False) ownJid = self.getLayerInterface( YowAuthenticationProtocolLayer).getUsername(True) senderKeyName = SenderKeyName(node["to"], AxolotlAddress(ownNumber, 0)) senderKeyRecord = self.store.loadSenderKey(senderKeyName) def sendToGroup(resultNode, requestEntity): groupInfo = InfoGroupsResultIqProtocolEntity.fromProtocolTreeNode( resultNode) jids = list(groupInfo.getParticipants().keys() ) #keys in py3 returns dict_keys if ownJid in jids: jids.remove(ownJid) return self.ensureSessionsAndSendToGroup(node, jids) if senderKeyRecord.isEmpty(): groupInfoIq = InfoGroupsIqProtocolEntity(groupJid) self._sendIq(groupInfoIq, sendToGroup) else: retryCount = 0 jidsNeedSenderKey = [] if retryReceiptEntity is not None: retryCount = retryReceiptEntity.getRetryCount() jidsNeedSenderKey.append(retryReceiptEntity.getRetryJid()) self.sendToGroupWithSessions(node, jidsNeedSenderKey, retryCount)
def sendToGroup(self, node, retryReceiptEntity = None): logger.debug("sendToGroup(node=[omitted], retryReceiptEntity=[%s])" % ("[retry_count=%s, retry_jid=%s]" % ( retryReceiptEntity.getRetryCount(), retryReceiptEntity.getRetryJid()) ) if retryReceiptEntity is not None else None) groupJid = node["to"] ownJid = self.getLayerInterface(YowAuthenticationProtocolLayer).getUsername(True) senderKeyRecord = self.manager.load_senderkey(node["to"]) def sendToGroup(resultNode, requestEntity): groupInfo = InfoGroupsResultIqProtocolEntity.fromProtocolTreeNode(resultNode) jids = list(groupInfo.getParticipants().keys()) #keys in py3 returns dict_keys if ownJid in jids: jids.remove(ownJid) return self.ensureSessionsAndSendToGroup(node, jids) if senderKeyRecord.isEmpty(): logger.debug("senderKeyRecord is empty, requesting group info") groupInfoIq = InfoGroupsIqProtocolEntity(groupJid) self._sendIq(groupInfoIq, sendToGroup) else: logger.debug("We have a senderKeyRecord") retryCount = 0 jidsNeedSenderKey = [] if retryReceiptEntity is not None: retryCount = retryReceiptEntity.getRetryCount() jidsNeedSenderKey.append(retryReceiptEntity.getRetryJid()) self.sendToGroupWithSessions(node, jidsNeedSenderKey, retryCount)
def sendToGroup(self, node, retryReceiptEntity=None): """ Group send sequence: check if senderkeyrecord exists no: - create, - get group jids from info request - for each jid without a session, get keys to create the session - send message with dist key for all participants yes: - send skmsg without any dist key received retry for a participant - request participants keys - send message with dist key only + conversation, only for this participat """ logger.debug("sendToGroup(node=[omitted], retryReceiptEntity=[%s])" % ("[retry_count=%s, retry_jid=%s]" % (retryReceiptEntity.getRetryCount(), retryReceiptEntity.getRetryJid())) if retryReceiptEntity is not None else None) groupJid = node["to"] ownJid = self.getLayerInterface( YowAuthenticationProtocolLayer).getUsername(True) senderKeyRecord = self.manager.load_senderkey(node["to"]) def sendToGroup(resultNode, requestEntity): groupInfo = InfoGroupsResultIqProtocolEntity.fromProtocolTreeNode( resultNode) jids = list(groupInfo.getParticipants().keys() ) #keys in py3 returns dict_keys if ownJid in jids: jids.remove(ownJid) return self.ensureSessionsAndSendToGroup(node, jids) if senderKeyRecord.isEmpty(): logger.debug("senderKeyRecord is empty, requesting group info") groupInfoIq = InfoGroupsIqProtocolEntity(groupJid) self._sendIq(groupInfoIq, sendToGroup) else: logger.debug("We have a senderKeyRecord") retryCount = 0 jidsNeedSenderKey = [] if retryReceiptEntity is not None: retryCount = retryReceiptEntity.getRetryCount() jidsNeedSenderKey.append(retryReceiptEntity.getRetryJid()) self.sendToGroupWithSessions(node, jidsNeedSenderKey, retryCount)
def handleEncNode(self, node): recipient_id = node["to"].split('@')[0] v2 = node["to"] if node.getChild("enc"): # media enc is only for v2 messsages if '-' in recipient_id: ## Handle Groups def getResultNodes(resultNode, requestEntity): groupInfo = InfoGroupsResultIqProtocolEntity.fromProtocolTreeNode(resultNode) jids = list(groupInfo.getParticipants().keys()) #keys in py3 returns dict_keys jids.remove(self.getLayerInterface(YowAuthenticationProtocolLayer).getUsername(True)) jidsNoSession = [] for jid in jids: if not self.manager.session_exists(jid.split('@')[0]): jidsNoSession.append(jid) if len(jidsNoSession): self.getKeysFor(jidsNoSession, lambda successJids, b: self.sendToGroupWithSessions(node, successJids)) else: self.sendToGroupWithSessions(node, jids) groupInfoIq = InfoGroupsIqProtocolEntity(Jid.normalize(node["to"])) self._sendIq(groupInfoIq, getResultNodes) else: messageData = self.serializeToProtobuf(node) # print (messageData) if messageData: ciphertext = self.manager.encrypt(recipient_id, messageData) mediaType = node.getChild("enc")["type"] if node.getChild("enc") else None encEntity = EncryptedMessageProtocolEntity( [ EncProtocolEntity( EncProtocolEntity.TYPE_MSG if ciphertext.__class__ == WhisperMessage else EncProtocolEntity.TYPE_PKMSG, 2 if v2 else 1, ciphertext.serialize(), mediaType)], "text" if not mediaType else "media", MessageAttributes.from_message_protocoltreenode(node) ) self.toLower(encEntity.toProtocolTreeNode()) else: # case of unserializable messages (audio, video) ? self.toLower(node) else: self.toLower(node)
def group_info(self, payload): jid = payload["phone"] callback = payload["callback"] entity = InfoGroupsIqProtocolEntity(self.normalizeJid(jid)) self.callbackQueue[entity.getId()] = callback self.toLower(entity)
def getGroupInfo(self, groupId): entity = InfoGroupsIqProtocolEntity(groupId) self.toLower(entity)