示例#1
0
 def get_files(self):
     username = self.username
     password = self.factory.get_password()
     timestamps = get_timestamps(self.factory.get_directory())
     object = {"command": "get", "username": username, "password": password, "timestamps": timestamps}
     if (self.connected == True and self.factory.command_out == False and len(q) == 0):
         self.q.append(object)
示例#2
0
    def lineReceived(self, line):
        data = json.loads(line)
        command = data["command"]
        retVal = {"done": True, 'success': True}

        print line
        # client interface messages come in here
        if command in ['register', 'authenticate', 'change password']:
            if command == 'register':
                success = self.factory.register(data['username'], data['password'])
                self.sendLine(json.dumps({'type': 'register', 'success': success}))
            elif command == 'authenticate':
                print 'AUTHENTICATION RECEIVED'
                success = self.factory.auth(data['username'], data['password'])
                reason = ''
                if not success:
                    user_exists = self.factory.userExists(data['username'])
                    reason = 'incorrect password'
                    if not user_exists:
                        reason = 'user does not exist'
                self.sendLine(json.dumps({'type': 'authenticate', 'success': success, 'reason': reason}))
            elif command == 'change password':
                success = self.factory.auth(data['username'], data['old_password'])
                reason = ''
                if not success:
                    user_exists = self.factory.userExists(data['username'])
                    reason = 'incorrect password'
                    if not user_exists:
                        reason = 'user does not exist'
                if success:
                    print 'here'
                    success = self.factory.updatePassword(data['username'], data['new_password'])
                print {'type': 'change password', 'success': success, 'reason': reason}
                self.sendLine(json.dumps({'type': 'change password', 'success': success, 'reason': reason}))
            return

        # client daemon messages come in here
        if (not self.factory.auth(data['username'], data['password'])) and not command in self.c_interface_commands:
            retVal['success'] = False
            self.sendLine(json.dumps(retVal))
            return
        if not os.path.exists(os.path.join(self.factory.files_path, data['username'])):
                os.mkdir(os.path.join(self.factory.files_path, data['username']))
        if not command in COMMANDS:
            self.sendLine(json.dumps(retVal))
            return
        if command == 'move':
            print "Receiving move from " + data['src'] + " to " + data['dest']
            self.factory.db.recordTrans(data['username'], "move", 0, data['src'] + " to " + data['dest'])
            src_path = os.path.join(self.factory.files_path, data["username"], self.clean_file_string(data['src']))
            dest_path = os.path.join(self.factory.files_path, data["username"], self.clean_file_string(data['dest']))
            if os.path.exists(src_path):
                os.renames(src_path, dest_path)
            self.sendLine(json.dumps(retVal))
        elif command == 'register':
            print 'Receiving register for username ' + data['username']
            success = self.factory.db.createUser(data['username'], data['password'])
            self.sendLine(json.dumps({'success': success}))
            return
        elif command == 'authenticate':
            retVal['success'] = self.factory.auth(data['username'], data['password'])
            self.sendLine(json.dumps(retVal))
            return
        elif command == "delete":
            print "Receiving delete of " + data["what"] + " " + data['file']
            self.factory.db.recordTrans(data['username'], "delete", 0, data["what"] + " " + data['file'])
            path = os.path.join(self.factory.files_path, self.clean_file_string(data['username']), self.clean_file_string(data["file"]))
            if (os.path.exists(path)):
                if (data["what"] == "file"):
                    os.unlink(path)
                elif data["what"] == "directory":
                    shutil.rmtree(path)
            self.sendLine(json.dumps(retVal))
        elif command == 'create':
            print "Receiving create for " + data['file']
            if data['what'] == 'directory':
                path = os.path.join(self.factory.files_path, data["username"], self.clean_file_string(data['file']))
                if not os.path.exists(path):
                    os.mkdir(path)
            else:
                path = os.path.join(self.factory.files_path, data["username"], self.clean_file_string(data['file']))
                if not os.path.exists(path):
                    os.mknod(path)
            self.sendLine(json.dumps(retVal))
        elif command == 'get':
            #TODO GET DOESNT WORK FOR DIRECTORIES
            self.factory.db.recordTrans(data['username'], "get", 0, "/")
            if not os.path.exists(os.path.join(self.factory.files_path, data['username'])):
                os.mkdir(os.path.join(self.factory.files_path, data['username']))
            timestamps = get_timestamps(os.path.join(self.factory.files_path, data['username']))
            retVal = dict(retVal.items() + {'files': [], 'directories': [], "remove": []}.items())
            for file, stamp in timestamps.iteritems():
                if (not file in data['timestamps'] or stamp > data['timestamps'][file]):
                    path = os.path.join(self.factory.files_path, data['username'], self.clean_file_string(file))
                    if os.path.isdir(path):
                        retVal['directories'].append(file)
                    elif os.path.isfile(path):
                        retVal['files'].append(file)
            for file, stamp in data['timestamps'].iteritems():
                if not file in timestamps:
                    retVal['remove'].append(file)
            #print retVal
            self.sendLine(json.dumps(retVal))
        elif command == 'get_file':
            file_path = os.path.join(self.factory.files_path, data['username'], self.clean_file_string(data['rel_path']))
            if os.path.exists(file_path):
                self.setRawMode()
                for bytes in read_bytes_from_file(file_path):
                    self.transport.write(bytes)

                self.transport.write('\r\n')

                # When the transfer is finished, we go back to the line mode
                self.setLineMode()
            else:
                self.sendLine("FILEDOESNOTEXISTFAIL2389")
        elif command == 'put':
            try:
                filename = self.clean_file_string(data["relative_path"])
            except IndexError:
                self.transport.write('Missing filename or file MD5 hash\n')
                self.transport.write('ENDMSG\n')
                return

            file_path = os.path.join(self.factory.files_path, data['username'], filename)
            # Switch to the raw mode (for receiving binary data)
            self.factory.db.recordTrans(data['username'], "put", 0, file_path)
            print 'Receiving file: %s' % (filename)
            retVal['command'] = 'put'
            retVal['local_file_path'] = data['local_file_path']
            self.sendLine(json.dumps(retVal))
            self.file_to_update = file_path
            self.setRawMode()

        elif command == 'help':
            self.sendLine(json.dumps(retVal))
        elif command == 'quit':
            self.transport.loseConnection()
        else:
            self.sendLine(json.dumps(retVal))