1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-30 17:03:03 +00:00
mpw/lib/Sync.rb

137 lines
3.7 KiB
Ruby
Raw Normal View History

2015-02-08 20:15:06 +00:00
#!/usr/bin/ruby
# author: nishiki
# mail: nishiki@yaegashi.fr
# info: a simple script who manage your passwords
require 'rubygems'
require 'i18n'
require 'yaml'
2015-02-08 20:57:15 +00:00
require 'tempfile'
2015-02-08 21:54:04 +00:00
require_relative './MPW'
require_relative './Item'
2015-02-08 20:15:06 +00:00
module MPW
class Sync
attr_accessor :error_msg
2015-02-13 18:28:59 +00:00
# Constructor
# raise an exception if there is a bad parameter
2015-02-08 20:15:06 +00:00
def initialize(config, local, password=nil)
@error_msg = nil
@config = config
@local = local
@password = password
raise I18n.t('error.class') if not @local.instance_of?(MPW)
end
2015-02-13 18:28:59 +00:00
# Get the data on remote host
# @rtrn: true if get the date, else false
2015-02-08 20:15:06 +00:00
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"
2015-02-08 21:43:35 +00:00
@sync = SyncFTP.new(@config.sync_host, @config.sync_user, @config.sync_pwd, @config.sync_path, @config.sync_port)
2015-02-08 20:15:06 +00:00
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
2015-02-09 20:39:13 +00:00
@error_msg = "#{I18n.t('error.sync.download')} #{e}"
2015-02-08 20:15:06 +00:00
file_tmp.close(true)
return false
end
# Sync remote data and local data
2015-02-13 18:28:59 +00:00
# raise an exception if there is a problem
2015-02-08 20:15:06 +00:00
def sync
2015-02-08 20:15:06 +00:00
if not @remote.to_s.empty?
@local.list.each do |item|
@remote.list.each do |r|
# Update item
2015-02-08 20:15:06 +00:00
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: r.password,
port: r.port,
comment: r.comment
2015-02-08 21:43:35 +00:00
)
2015-02-08 20:15:06 +00:00
end
2015-03-22 21:43:18 +00:00
r.delete
2015-02-08 20:15:06 +00:00
break
end
end
# Remove an old item
2015-05-11 17:23:59 +00:00
if item.last_sync.to_i < @config.last_sync
2015-02-08 20:15:06 +00:00
item.delete
end
end
end
# Add item
@remote.list.each do |r|
if r.last_edit > @config.last_update
2015-03-22 21:43:18 +00:00
item = Item.new(id: r.id,
name: r.name,
group: r.group,
host: r.host,
protocol: r.protocol,
user: r.user,
password: r.password,
port: r.port,
comment: r.comment,
2015-03-22 21:45:43 +00:00
created: r.created,
2015-03-22 21:43:18 +00:00
last_edit: r.last_edit
2015-02-08 21:43:35 +00:00
)
2015-02-08 20:15:06 +00:00
raise @local.error_msg if not @local.add(item)
end
end
2015-03-19 22:17:01 +00:00
@local.list.each do |item|
item.set_last_sync
end
2015-02-08 20:15:06 +00:00
raise @mpw.error_msg if not @local.encrypt
2015-03-19 22:17:01 +00:00
raise @sync.error_msg if not @sync.update(@config.file_gpg)
2015-03-22 21:43:18 +00:00
@config.set_last_sync
2015-02-08 20:15:06 +00:00
return true
2015-05-11 17:23:59 +00:00
# rescue Exception => e
2015-02-09 20:39:13 +00:00
@error_msg = "#{I18n.t('error.sync.unknown')} #{e}"
2015-02-08 20:15:06 +00:00
return false
end
end
end