Exemplo n.º 1
0
	def testBytesToString(self):
		'''Testing the conversion from bytes to strings'''
		b1 = bytearray("abc1 88ABC_ä", "utf8")
		s1 = imageutils.bytesToString(b1)
		self.assertEqual(s1, "616263312038384142435fc3a4", "String 1 should match")

		b2 = bytearray()
		b2.append(0)
		b2.append(1)
		b2.append(2)
		b2.append(13)
		s2 = imageutils.bytesToString(b2)
		self.assertEqual(s2, "0001020d", "String 2 should match")
Exemplo n.º 2
0
	def testBytesToStringToBytes(self):
		'''Testing the conversion from strings back to bytes'''
		b1 = bytearray([81, 119, 200, 3, 0, 9, 0, 55])
		s1 = imageutils.bytesToString(b1)
		self.assertEqual(s1, "5177c80300090037", "string should match the expected one")
		b2 = imageutils.stringToBytes(s1)
		self.assertEqual(b1, b2, "byte array should be the same after conversion to string and back")
Exemplo n.º 3
0
    def updateProfile(torid, inProfile, picOutputPath=None):
        '''Updates the profile with the given torid,
		   or adds a new profile if it's not found.
		   Also exports the avatar to the picOutputPath
		   if the profile picture has changed'''
        # If the profile pic path has changed, then we need to load the file
        givenprofilepicpath = inProfile.get('profilepicpath', None)
        pic_changed = False
        if givenprofilepicpath and os.path.exists(givenprofilepicpath):
            pic_changed = True
            # check if it's the same path as already stored
            storedProfile = DbI.getProfile(torid)
            if not storedProfile or storedProfile[
                    'profilepicpath'] != givenprofilepicpath:
                # file path has been given, so need to make a string from the bytes
                picBytes = imageutils.makeThumbnailBinary(givenprofilepicpath)
                inProfile['profilepic'] = imageutils.bytesToString(picBytes)
        elif inProfile.get('profilepic', None):
            pic_changed = True

        inProfile['torid'] = torid
        if not DbI.db_instance.addOrUpdateProfile(inProfile):
            print("FAILED to update profile!")
        if pic_changed and picOutputPath:
            DbI._updateAvatar(torid, picOutputPath)
        if inProfile.get("status", None) in ["blocked", "deleted", "trusted"]:
            DbI.updateContactList(
                Config.getProperty(Config.KEY_ALLOW_FRIENDS_TO_SEE_FRIENDS))
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
 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"
         )