diff --git a/MPW/Config.rb b/MPW/Config.rb index 92a58fd..2919fcf 100644 --- a/MPW/Config.rb +++ b/MPW/Config.rb @@ -30,7 +30,7 @@ module MPW # @args: file_config -> the specify config file def initialize(file_config=nil) @error_msg = nil - @file_config = "#{Dir.home()}/.mpw.cfg" + @file_config = "#{Dir.home}/.mpw.cfg" if !file_config.nil? && !file_config.empty? @file_config = file_config @@ -57,7 +57,7 @@ module MPW end if file_gpg.empty? - file_gpg = "#{Dir.home()}/.mpw.gpg" + file_gpg = "#{Dir.home}/.mpw.gpg" end timeout_pwd = timeout_pwd.empty? ? 60 : timeout_pwd.to_i @@ -86,7 +86,7 @@ module MPW # Check the config file # @rtrn: true if the config file is correct - def checkconfig() + def checkconfig config = YAML::load_file(@file_config) @key = config['config']['key'] @lang = config['config']['lang'] diff --git a/MPW/Sync/MPW.rb b/MPW/Sync/MPW.rb index d0462a8..69fdd83 100644 --- a/MPW/Sync/MPW.rb +++ b/MPW/Sync/MPW.rb @@ -35,8 +35,9 @@ module MPW @password = password @suffix = path - @socket = TCPSocket.new(host, port.to_i) - @enable = true + TCPSocket.new(host, port.to_i) do + @enable = true + end rescue Exception => e @error_msg = "#{I18n.t('error.sync.connection')}\n#{e}" @enable = false @@ -51,17 +52,19 @@ module MPW if !@enable return nil end - - send_msg = {:action => 'get', - :gpg_key => @gpg_key, - :password => @password, - :suffix => @suffix} - @socket.puts send_msg.to_json - msg = JSON.parse(@socket.gets) - + TCPSocket.new(host, port.to_i) do |socket| + send_msg = {:action => 'get', + :gpg_key => @gpg_key, + :password => @password, + :suffix => @suffix} + + socket.puts send_msg.to_json + msg = JSON.parse(socket.gets) + end + if !defined?(msg['error']) - @error_msg = I18n.t('error.sync.communication') + error_msg = I18n.t('error.sync.communication') return nil elsif msg['error'].nil? tmp_file = tmpfile @@ -81,6 +84,7 @@ module MPW @error_msg = I18n.t(msg['error']) return nil end + end # Update the remote data @@ -91,14 +95,16 @@ module MPW return true end - send_msg = {:action => 'update', - :gpg_key => @gpg_key, - :password => @password, - :suffix => @suffix, - :data => data} - - @socket.puts send_msg.to_json - msg = JSON.parse(@socket.gets) + TCPSocket.new(host, port.to_i) do |socket| + send_msg = {:action => 'update', + :gpg_key => @gpg_key, + :password => @password, + :suffix => @suffix, + :data => data} + + socket.puts send_msg.to_json + msg = JSON.parse(socket.gets) + end if !defined?(msg['error']) @error_msg = I18n.t('error.sync.communication') @@ -111,16 +117,6 @@ module MPW end end - # Close the connection - def close - if !@enable - return - end - - send_msg = {:action => 'close'} - @socket.puts send_msg.to_json - end - # Generate a random string # @rtrn: a random string def tmpfile diff --git a/MPW/Sync/SSH.rb b/MPW/Sync/SSH.rb index e69a6e2..21c9c03 100644 --- a/MPW/Sync/SSH.rb +++ b/MPW/Sync/SSH.rb @@ -97,10 +97,6 @@ module MPW @error_msg = "#{I18n.t('error.sync.upload')}\n#{e}" return false end - - # Close the connection - def close - end # Generate a random string # @rtrn: a random string diff --git a/MPW/UI/Cli.rb b/MPW/UI/Cli.rb index 1b5c017..ead9701 100644 --- a/MPW/UI/Cli.rb +++ b/MPW/UI/Cli.rb @@ -23,11 +23,6 @@ class Cli @config = config end - # Close sync - def sync_close - @sync.close - end - # Sync the data with the server # @rtnr: true if the synchro is finish def sync @@ -77,7 +72,7 @@ class Cli puts '--------------------' language = ask(I18n.t('form.setup.lang', :lang => lang)).to_s key = ask(I18n.t('form.setup.gpg_key')).to_s - file_gpg = ask(I18n.t('form.setup.gpg_file', :home => Dir.home())).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 @@ -101,7 +96,7 @@ class Cli puts "#{I18n.t('display.error')}: #{@config.error_msg}" end - if not @config.checkconfig() + if not @config.checkconfig puts "#{I18n.t('display.error')}: #{@config.error_msg}" exit 2 end @@ -171,7 +166,7 @@ class Cli end # Form to add a new item - def add() + def add row = [] puts I18n.t('form.add.title') puts '--------------------' @@ -185,8 +180,8 @@ class Cli comment = ask(I18n.t('form.add.comment')).to_s if @mpw.update(name, group, server, protocol, login, passwd, port, comment) - if @mpw.encrypt() - sync() + if @mpw.encrypt + sync puts I18n.t('form.add.valid') else puts "#{I18n.t('display.error')}: #{@mpw.error_msg}" @@ -214,8 +209,8 @@ class Cli comment = ask(I18n.t('form.update.comment' , :comment => row[MPW::MPW::COMMENT])).to_s if @mpw.update(name, group, server, protocol, login, passwd, port, comment, id) - if @mpw.encrypt() - sync() + if @mpw.encrypt + sync puts I18n.t('form.update.valid') else puts "#{I18n.t('display.error')}: #{@mpw.error_msg}" @@ -249,8 +244,8 @@ class Cli if force if @mpw.remove(id) - if @mpw.encrypt() - sync() + if @mpw.encrypt + sync puts I18n.t('form.delete.valid', :id => id) else puts "#{I18n.t('display.error')}: #{@mpw.error_msg}" @@ -294,8 +289,8 @@ class Cli end if force - if @mpw.import(file) && @mpw.encrypt() - sync() + if @mpw.import(file) && @mpw.encrypt + sync puts I18n.t('form.import.valid') else puts "#{I18n.t('display.error')}: #{@mpw.error_msg}" @@ -304,7 +299,7 @@ class Cli end # Interactive mode - def interactive() + def interactive group = nil last_access = Time.now.to_i @@ -331,7 +326,7 @@ class Cli display(command[1], group, command[2]) end when 'add', 'a' - add() + add when 'update', 'u' if !command[1].nil? && !command[1].empty? update(command[1]) diff --git a/lib/Server.rb b/lib/Server.rb index 1821f5e..85d8f98 100644 --- a/lib/Server.rb +++ b/lib/Server.rb @@ -11,12 +11,12 @@ class Server attr_accessor :error_msg # Constructor - def initialize() + def initialize YAML::ENGINE.yamler='syck' end # Start the server - def start() + def start server = TCPServer.open(@host, @port) @log.info("The server is started on #{@host}:#{@port}") @@ -134,7 +134,7 @@ class Server hash = gpg_data['gpg']['hash'] else - salt = salt() + salt = salt hash = Digest::SHA256.hexdigest(salt + msg['password']) end diff --git a/mpw b/mpw index ebe9f2a..c13baeb 100755 --- a/mpw +++ b/mpw @@ -110,7 +110,7 @@ end.parse! # --------------------------------------------------------- # config = MPW::Config.new(options[:config]) -check_error = config.checkconfig() +check_error = config.checkconfig cli = Cli.new(lang, config) @@ -119,8 +119,8 @@ if !check_error || !options[:setup].nil? cli.setup(lang) end -cli.decrypt() -cli.sync() +cli.decrypt +cli.sync # Display the item's informations if not options[:display].nil? @@ -136,7 +136,7 @@ elsif not options[:update].nil? # Add a new item elsif not options[:add].nil? - cli.add() + cli.add # Export elsif not options[:export].nil? @@ -149,15 +149,13 @@ elsif not options[:import].nil? # Interactive mode else begin - cli.interactive() + cli.interactive rescue SystemExit, Interrupt - cli.sync_close() cli = nil return 1 end end -cli.sync_close() cli = nil exit 0 diff --git a/mpw-ssh b/mpw-ssh index f90cc81..079771e 100755 --- a/mpw-ssh +++ b/mpw-ssh @@ -63,7 +63,7 @@ end.parse! # --------------------------------------------------------- # config = MPW::Config.new(options[:config]) -check_error = config.checkconfig() +check_error = config.checkconfig cli = CliSSH.new(lang, config) cli.login = options[:login] @@ -80,11 +80,11 @@ elsif ARGV.length < 1 puts "#{I18n.t('ssh.option.usage')}: mpw-ssh SEARCH [options]" exit 2 else - cli.decrypt() - cli.sync() + cli.decrypt + cli.sync cli.ssh(search) end -cli.sync_close() +cli = nil exit 0