<commit_before>#!/usr/bin/ruby
# author: nishiki
# mail: <EMAIL>
# info: a simple script who manage your passwords

require 'rubygems'
require 'i18n'
require 'yaml'
require 'tempfile'

require_relative './MPW'
require_relative './Item'
	
module MPW
	class Sync

		attr_accessor :error_msg

		def initialize(config, local, password=<PASSWORD>)
			@error_msg = nil
			@config    = config
			@local     = local
			@password  = password

			raise I18n.t('error.class') if not @local.instance_of?(MPW)
		end

		def get_remote
			case @config.sync_type
			when 'mpw'
				require "#{APP_ROOT}/lib/Sync/SyncMPW"
				@sync = SyncMPW.new(@config.sync_host, @config.sync_user, @config.sync_pwd, @config.sync_path, @config.sync_port)
			when 'sftp', 'scp', 'ssh'
				require "#{APP_ROOT}/lib/Sync/SSH"
				@sync = SyncSSH.new(@config.sync_host, @config.sync_user, @config.sync_pwd, @config.sync_path, @config.sync_port)
			when 'ftp'
				require "#{APP_ROOT}/lib/Sync/FTP"
				@sync = SyncFTP.new(@config.sync_host, @config.sync_user, @config.sync_pwd, @config.sync_path, @config.sync_port)
			else
				@error_msg =  I18n.t('error.unknown_type')
				return false
			end

			if not @sync.connect
				@error_msg = @sync.error_msg
				return false
			end

			
			file_tmp = Tempfile.new('mpw-')
			raise @sync.error_msg if not @sync.get(file_tmp.path)	

			@remote = MPW.new(file_tmp.path, @config.key)
			raise @remote.error_msg if not @remote.decrypt(@password)

			file_tmp.close(true)
			return true
		rescue Exception => e
			@error_msg = "#{I18n.t('error.sync.download')} #{e}"
			file_tmp.close(true)
			return false
		end

		# Sync remote data and local data
		# @args: data_remote -> array with the data remote
		#        last_update -> last update
		# @rtrn: false if data_remote is nil
		def sync
			if not @remote.to_s.empty?
				@local.list.each do |item|
					j      = 0
					update = false
		
					# Update item
					@remote.list.each do |r|
						if item.id == r.id
							if item.last_edit < r.last_edit
								raise item.error_msg if not item.update(name:     r.name,
								                                        group:    r.group,
								                                        host:     r.host,
								                                        protocol: r.protocol,
								                                        user:     r.user,
								                                        password: <PASSWORD>,
								                                        port:     r.port,
								                                        comment:  r.comment
								                                       )
							end

							update = true
							item.delete<commit_msg>add comment<commit_after>#!/usr/bin/ruby
# author: nishiki
# mail: <EMAIL>
# info: a simple script who manage your passwords

require 'rubygems'
require 'i18n'
require 'yaml'
require 'tempfile'

require_relative './MPW'
require_relative './Item'
	
module MPW
	class Sync

		attr_accessor :error_msg

		# Constructor
		# raise an exception if there is a bad parameter
		def initialize(config, local, password=<PASSWORD>)
			@error_msg = nil
			@config    = config
			@local     = local
			@password  = password

			raise I18n.t('error.class') if not @local.instance_of?(MPW)
		end

		# Get the data on remote host
		# @rtrn: true if get the date, else false
		def get_remote
			case @config.sync_type
			when 'mpw'
				require "#{APP_ROOT}/lib/Sync/SyncMPW"
				@sync = SyncMPW.new(@config.sync_host, @config.sync_user, @config.sync_pwd, @config.sync_path, @config.sync_port)
			when 'sftp', 'scp', 'ssh'
				require "#{APP_ROOT}/lib/Sync/SSH"
				@sync = SyncSSH.new(@config.sync_host, @config.sync_user, @config.sync_pwd, @config.sync_path, @config.sync_port)
			when 'ftp'
				require "#{APP_ROOT}/lib/Sync/FTP"
				@sync = SyncFTP.new(@config.sync_host, @config.sync_user, @config.sync_pwd, @config.sync_path, @config.sync_port)
			else
				@error_msg =  I18n.t('error.unknown_type')
				return false
			end

			if not @sync.connect
				@error_msg = @sync.error_msg
				return false
			end

			
			file_tmp = Tempfile.new('mpw-')
			raise @sync.error_msg if not @sync.get(file_tmp.path)	

			@remote = MPW.new(file_tmp.path, @config.key)
			raise @remote.error_msg if not @remote.decrypt(@password)

			file_tmp.close(true)
			return true
		rescue Exception => e
			@error_msg = "#{I18n.t('error.sync.download')} #{e}"
			file_tmp.close(true)
			return false
		end

		# Sync remote data and local data
		# raise an exception if there is a problem
		def sync
			if not @remote.to_s.empty?
				@local.list.each do |item|
					j      = 0
					update = false
		
					# Update item
					@remote.list.each do |r|
						if item.id == r.id
							if item.last_edit < r.last_edit
								raise item.error_msg if not item.update(name:     r.name,
								                                        group:    r.group,
								                                        host:     r.host,
								                                        protocol: r.protocol,
								                                        user:     r.user,
								                                        password: <PASSWORD>,
								                                        port:     r.port,
								                                        comment:  r.comment
								                                       )
							end

							update = true
							item.delete
{'fraction_non_alphanumeric': 0.10707635009310987, 'fraction_numerical': 0.0033519553072625698, 'mean_word_length': 1.816465652857892, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1911731', 'n_tokens_mistral': 2145, 'n_tokens_neox': 1678, 'n_words': 408}