def __init__(self, parent): threading.Thread.__init__(self) self.parent = parent self.somethingInInbox = False self.start() # Register myself as listener to the message notifier DbMessageNotifier.getInstance().addListener(self)
def addMessageToOutbox(message): '''Add the given message to the outbox for sending later''' if message.recipients: # If the message is allowed to be relayed, we need to make a list of all our contacts relays = [] for r in message.recipients: # TODO: Check that len(r) == 16 ? print("Add outgoing message for", r) encryptKey = None prof = DbClient.getProfile(userid=r) if prof: encryptKey = prof.get("keyid", None) else: print("No profile for ", r) try: # message.output is a bytes() object, so we need to convert to Binary for storage messageToSend = Binary(message.createOutput(encryptKey)) DbClient._getOutboxTable().insert({"recipient":r, "relays":relays, "message":messageToSend, "queue":message.shouldBeQueued, "msgType":message.getMessageTypeKey()}) # Inform all interested listeners that there's been a change in the messages DbMessageNotifier.getInstance().notify() except CryptoError as e: print("Something has thrown a CryptoError :( can't add message to Outbox!", e)
def addMessageToInbox(message): '''A message has been received, need to add this to our inbox so it can be read''' # Calculate hash of message's body + timestamp + sender thisHash = DbClient.calculateHash({"body":message['messageBody'], "timestamp":message['timestamp'], "senderId":message['fromId']}) # Check id or hash of message to make sure we haven't got it already! inbox = DbClient._getInboxTable() if not inbox.find_one({"messageHash" : thisHash}): message['messageHash'] = thisHash # Either take its parent's conversation id, or generate a new one message['conversationid'] = DbClient.getConversationId(message.get("parentHash", None)) # print("Storing message to inbox:", message) inbox.insert(message) # Inform all interested listeners that there's been a change in the messages DbMessageNotifier.getInstance().notify() else: print("Received message with hash '", thisHash, "but I've got that one already")
def __init__(self, parent): '''Constructor''' QtCore.QObject.__init__(self) self.parent = parent self._broadcasting = False self._flushing = False self.flushSignal.connect(self.flushOutbox) # Set up timers self.flushTimer = QtCore.QTimer() self.connect(self.flushTimer, QtCore.SIGNAL("timeout()"), self.flushOutbox) self.flushTimer.start(300000) # flush every 5 minutes self.broadcastTimer = QtCore.QTimer() self.connect(self.broadcastTimer, QtCore.SIGNAL("timeout()"), self.broadcastOnlineStatus) self.broadcastTimer.start(350000) # broadcast about every 5 minutes # Register myself as listener to the message notifier DbMessageNotifier.getInstance().addListener(self) # Trigger a broadcast after 30 seconds QtCore.QTimer.singleShot(30000, self.broadcastOnlineStatus)
def __init__(self, parent): '''Constructor''' QObject.__init__(self) self.parent = parent self._broadcasting = False self._flushing = False self.flushSignal.connect(self.flushOutbox) # Set up timers self.flushTimer = QTimer() self.flushTimer.timeout.connect(self.flushOutbox) self.flushTimer.start(300000) # flush every 5 minutes self.broadcastTimer = QTimer() self.broadcastTimer.timeout.connect(self.broadcastOnlineStatus) self.broadcastTimer.start(350000) # broadcast about every 5 minutes # Register myself as listener to the message notifier DbMessageNotifier.getInstance().addListener(self) # Trigger a broadcast after 30 seconds QTimer.singleShot(30000, self.broadcastOnlineStatus)
def addToInbox(message): '''A message has been received, need to add this to our inbox so it can be read''' # Calculate hash of message's body + timestamp + sender thisHash = dbutils.calculateHash({ "body": message['messageBody'], "timestamp": message['timestamp'], "senderId": message['fromId'] }) # If hash is already in inbox, do nothing for m in DbI.getInboxMessages(): if m and m.get("messageHash") == thisHash: return # This is a new message message['messageHash'] = thisHash # Either take its parent's conversation id, or generate a new one message['conversationid'] = DbI.getConversationId( message.get("parentHash", None)) DbI.db_instance.addMessageToInbox(message) # Inform all interested listeners that there's been a change in the messages DbMessageNotifier.getInstance().notify()
def addToOutbox(message): '''Note: this method takes a message object (with recipients and a createOutput method), not just a dictionary of values.''' if message and message.recipients: relays = [] if message.shouldBeRelayed: relays = [p['torid'] for p in DbI.getTrustedProfiles()] # TODO: Save (unencrypted) copy in inbox too as a sent message for r in message.recipients: prof = DbI.getProfile(torid=r) if prof: encryptKey = prof["keyid"] try: # message.output is a bytes() object, so we need to convert to string for storage messageToSend = imageutils.bytesToString( message.createOutput(encryptKey)) if not messageToSend: print( "How can the message to send be empty for type", message.getMessageTypeKey()) DbI.db_instance.addMessageToOutbox({ "recipient": r, "relays": relays, "message": messageToSend, "queue": message.shouldBeQueued, "msgType": message.getMessageTypeKey() }) # Inform all interested listeners that there's been a change in the messages DbMessageNotifier.getInstance().notify() except CryptoError as e: print( "Something has thrown a CryptoError :( can't add message to Outbox!", e)
def addRelayMessageToOutbox(messageBytes, dontSendTo): '''Note: this method takes a set of bytes resulting from the encryption.''' messageRecipients = [p['torid'] for p in DbI.getTrustedProfiles()] if dontSendTo: messageRecipients = [ i for i in messageRecipients if i != dontSendTo ] if messageRecipients: # message output is a bytes() object, so we need to convert to Binary for storage messageToSend = imageutils.bytesToString(messageBytes) DbI.db_instance.addMessageToOutbox({ "recipientList": messageRecipients, "relays": None, "message": messageToSend, "queue": True, "msgType": "unknown" }) # Inform all interested listeners that there's been a change in the messages DbMessageNotifier.getInstance().notify() else: print( "After removing sender, there's noone left! - Throwing away message" )