From 1043b5c21c09af96801d7ca35f2895d60c250506 Mon Sep 17 00:00:00 2001
From: nishiki <nishiki@yaegashi.fr>
Date: Sat, 1 Feb 2014 19:11:19 +0100
Subject: [PATCH] fix sync via ssh

---
 MPW/Sync/SSH.rb | 25 ++++++++++++++-----------
 MPW/UI/Cli.rb   | 14 ++++++++++++--
 2 files changed, 26 insertions(+), 13 deletions(-)

diff --git a/MPW/Sync/SSH.rb b/MPW/Sync/SSH.rb
index bd92fc6..e69a6e2 100644
--- a/MPW/Sync/SSH.rb
+++ b/MPW/Sync/SSH.rb
@@ -9,8 +9,10 @@ module MPW
 
 		require 'rubygems'
 		require 'i18n'
+		require 'net/ssh'
+		require 'net/scp'
 		
-		class MPW
+		class SSH
 		
 			attr_accessor :error_msg
 			attr_accessor :enable
@@ -33,11 +35,12 @@ module MPW
 				@user     = user
 				@password = password
 				@path     = path
-				@port     = port.nil? || port.empty? ? 22 : port.to_i
+				@port     = port.instance_of?(Integer) ? 22 : port
 					
 				Net::SSH.start(@host, @user, :password => @password, :port => @port) do
+					@enable = true
 				end
-			rescue
+			rescue Exception => e
 				@error_msg = "#{I18n.t('error.sync.connection')}\n#{e}"
 				@enable    = false
 			else
@@ -53,14 +56,10 @@ module MPW
 				end
 				
 				tmp_file = tmpfile
-				Net::SCP.start(@host, @user, :password => @password, :port => @port) do |ssh|
-					ssh.scp.download(@path, tmp_file)
+				Net::SCP.start(@host, @user, :password => @password, :port => @port) do |scp|
+					scp.download!(@path, tmp_file)
 				end
 			
-				File.open(tmp_file, 'w') do |file|
-					file << msg['data']
-				end
-					
 				mpw = MPW.new(tmp_file)
 				if !mpw.decrypt(gpg_password)
 					@error_msg = mpw.error_msg
@@ -83,8 +82,12 @@ module MPW
 				end
 		
 				tmp_file = tmpfile
-				Net::SCP.start(@host, @user, :password => @password, :port => @port) do |ssh|
-					ssh.scp.upload(tmp_file, @path)
+				File.open(tmp_file, "w") do |file|
+					file << data
+				end
+
+				Net::SCP.start(@host, @user, :password => @password, :port => @port) do |scp|
+					scp.upload!(tmp_file, @path)
 				end
 
 				File.unlink(tmp_file)
diff --git a/MPW/UI/Cli.rb b/MPW/UI/Cli.rb
index 20ba037..1b5c017 100644
--- a/MPW/UI/Cli.rb
+++ b/MPW/UI/Cli.rb
@@ -12,6 +12,7 @@ require 'yaml'
 
 require "#{APP_ROOT}/MPW/MPW"
 require "#{APP_ROOT}/MPW/Sync/MPW"
+require "#{APP_ROOT}/MPW/Sync/SSH"
 
 class Cli
 
@@ -50,7 +51,9 @@ class Cli
 		if @sync.enable
 			if !@mpw.sync(@sync.get(@passwd), @config.last_update)
 				puts "#{I18n.t('display.error')}: #{@sync.error_msg}"
-			elsif !@sync.update(File.open(@config.file_gpg).read)
+			end
+
+			if !@sync.update(File.open(@config.file_gpg).read)
 				puts "#{I18n.t('display.error')}: #{@sync.error_msg}"
 			elsif !@config.set_last_update
 				puts "#{I18n.t('display.error')}: #{@config.error_msg}"
@@ -60,6 +63,9 @@ class Cli
 		end
 	rescue Exception => e
 		puts "#{I18n.t('display.error')}: #{e}"
+		puts @sync.error_msg
+		puts @config.error_msg
+		puts @mpw.error_msg
 	else
 		return false
 	end
@@ -73,19 +79,23 @@ class Cli
 		key         = ask(I18n.t('form.setup.gpg_key')).to_s
 		file_gpg    = ask(I18n.t('form.setup.gpg_file', :home => Dir.home())).to_s
 		timeout_pwd = ask(I18n.t('form.setup.timeout')).to_s
+		sync_type   = ask(I18n.t('form.setup.sync_type')).to_s
 		sync_host   = ask(I18n.t('form.setup.sync_host')).to_s
 		sync_port   = ask(I18n.t('form.setup.sync_port')).to_s
+		sync_user   = ask(I18n.t('form.setup.sync_user')).to_s
 		sync_pwd    = ask(I18n.t('form.setup.sync_pwd')).to_s
 		sync_path   = ask(I18n.t('form.setup.sync_path')).to_s
 		
 		I18n.locale = language.to_sym
 
+		sync_type = sync_type.empty? ? nil : sync_type
 		sync_host = sync_host.empty? ? nil : sync_host
 		sync_port = sync_port.empty? ? nil : sync_port.to_i
+		sync_user = sync_user.empty? ? nil : sync_user
 		sync_pwd  = sync_pwd.empty?  ? nil : sync_pwd
 		sync_path = sync_path.empty? ? nil : sync_path
 
-		if @config.setup(key, language, file_gpg, timeout_pwd, sync_host, sync_port, sync_pwd, sync_path)
+		if @config.setup(key, language, file_gpg, timeout_pwd, sync_type, sync_host, sync_port, sync_user, sync_pwd, sync_path)
 			puts I18n.t('form.setup.valid')
 		else
 			puts "#{I18n.t('display.error')}: #{@config.error_msg}"