From e0a120d8f12855c01349398e84c2935ab2392435 Mon Sep 17 00:00:00 2001 From: nishiki Date: Sun, 12 Jan 2014 00:37:46 +0100 Subject: [PATCH] restructiring the code --- lib/Cli.rb | 58 +++++++++++++------------ lib/MPW.rb | 87 +++---------------------------------- lib/MPWConfig.rb | 109 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 146 insertions(+), 108 deletions(-) create mode 100644 lib/MPWConfig.rb diff --git a/lib/Cli.rb b/lib/Cli.rb index cc67cd1..4947c00 100644 --- a/lib/Cli.rb +++ b/lib/Cli.rb @@ -10,6 +10,7 @@ require 'readline' require 'i18n' require "#{APP_ROOT}/lib/MPW.rb" +require "#{APP_ROOT}/lib/MPWConfig.rb" class Cli @@ -17,14 +18,15 @@ class Cli # @args: lang -> the operating system language # config_file -> a specify config file def initialize(lang, config_file=nil) - @m = MPW.new(config_file) + @config = MPWConfig.new(config_file) - if not @m.checkconfig() + if not @config.checkconfig() self.setup(lang) end + @mpw = MPW.new(@config.file_gpg, @config.key) if not self.decrypt() - puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}" + puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}" exit 2 end end @@ -44,28 +46,28 @@ class Cli end I18n.locale = language.to_sym - if @m.setup(key, language, file_gpg, timeout_pwd) + if @config.setup(key, language, file_gpg, timeout_pwd) puts I18n.t('cli.form.setup.valid') else - puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}" + puts "#{I18n.t('cli.display.error')}: #{@config.error_msg}" end - if not @m.checkconfig() - puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}" + if not @config.checkconfig() + puts "#{I18n.t('cli.display.error')}: #{@config.error_msg}" end end # Request the GPG password and decrypt the file def decrypt() @passwd = ask(I18n.t('cli.display.gpg_password')) {|q| q.echo = false} - return @m.decrypt(@passwd) + return @mpw.decrypt(@passwd) end # Display the query's result # @args: search -> the string to search # protocol -> search from a particular protocol def display(search, protocol=nil, group=nil, format=nil) - result = @m.search(search, group, protocol) + result = @mpw.search(search, group, protocol) if not result.empty? result.each do |r| @@ -125,21 +127,21 @@ class Cli port = ask(I18n.t('cli.form.add.port')).to_s comment = ask(I18n.t('cli.form.add.comment')).to_s - if @m.add(name, group, server, protocol, login, passwd, port, comment) - if @m.encrypt() + if @mpw.add(name, group, server, protocol, login, passwd, port, comment) + if @mpw.encrypt() puts I18n.t('cli.form.add.valid') else - puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}" + puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}" end else - puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}" + puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}" end end # Update an item # @args: id -> the item's id def update(id) - row = @m.searchById(id) + row = @mpw.searchById(id) if not row.empty? puts I18n.t('cli.form.update.title') @@ -153,14 +155,14 @@ class Cli port = ask(I18n.t('cli.form.update.port' , :port => row[MPW::PORT])).to_s comment = ask(I18n.t('cli.form.update.comment' , :comment => row[MPW::COMMENT])).to_s - if @m.update(id, name, group, server, protocol, login, passwd, port, comment) - if @m.encrypt() + if @mpw.update(id, name, group, server, protocol, login, passwd, port, comment) + if @mpw.encrypt() puts I18n.t('cli.form.update.valid') else - puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}" + puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}" end else - puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}" + puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}" end else puts I18n.t('cli.display.nothing') @@ -172,7 +174,7 @@ class Cli # force -> no resquest a validation def remove(id, force=false) if not force - result = @m.searchById(id) + result = @mpw.searchById(id) if result.length > 0 self.displayFormat(result) @@ -187,11 +189,11 @@ class Cli end if force - if @m.remove(id) - if @m.encrypt() + if @mpw.remove(id) + if @mpw.encrypt() puts I18n.t('cli.form.delete.valid', :id => id) else - puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}" + puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}" end else puts I18n.t('cli.form.delete.not_valid') @@ -202,10 +204,10 @@ class Cli # Export the items in a CSV file # @args: file -> the destination file def export(file) - if @m.export(file) + if @mpw.export(file) puts "The export in #{file} is succesfull!" else - puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}" + puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}" end end @@ -214,7 +216,7 @@ class Cli # @args: file -> the import file # force -> no resquest a validation def import(file, force=false) - result = @m.importPreview(file) + result = @mpw.importPreview(file) if not force if result.is_a?(Array) && !result.empty? @@ -232,10 +234,10 @@ class Cli end if force - if @m.import(file) && @m.encrypt() + if @mpw.import(file) && @mpw.encrypt() puts I18n.t('cli.form.import.valid') else - puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}" + puts "#{I18n.t('cli.display.error')}: #{@mpw.error_msg}" end end end @@ -247,7 +249,7 @@ class Cli while buf = Readline.readline(' ', true) - if @m.timeout_pwd < Time.now.to_i - last_access + if @config.timeout_pwd < Time.now.to_i - last_access passwd_confirm = ask(I18n.t('cli.interactive.ask_password')) {|q| q.echo = false} if @passwd.eql?(passwd_confirm) diff --git a/lib/MPW.rb b/lib/MPW.rb index e38ec0a..937ecdf 100644 --- a/lib/MPW.rb +++ b/lib/MPW.rb @@ -6,7 +6,6 @@ require 'rubygems' require 'gpgme' require 'csv' -require 'yaml' require 'i18n' class MPW @@ -22,79 +21,12 @@ class MPW COMMENT = 8 attr_accessor :error_msg - attr_accessor :timeout_pwd - + # Constructor - # @args: file_config -> the specify config file - def initialize(file_config=nil) - @error_msg = nil - @file_config = "#{Dir.home()}/.mpw.cfg" - - if !file_config.nil? && !file_config.empty? - @file_config = file_config - end - end - - # Create a new config file - # @args: key -> the gpg key to encrypt - # lang -> the software language - # file_gpg -> the file who is encrypted - # file_pwd -> the file who stock the password - # timeout_pwd -> time to save the password - # @rtrn: true if le config file is create - def setup(key, lang, file_gpg, timeout_pwd) - - if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/ - @error_msg = I18n.t('error.config.key_bad_format') - return false - end - - if file_gpg.empty? - file_gpg = "#{Dir.home()}/.mpw.gpg" - end - - timeout_pwd.empty? ? (timeout_pwd = 60) : (timeout_pwd = timeout_pwd.to_i) - - config = {'config' => {'key' => key, - 'lang' => lang, - 'file_gpg' => file_gpg, - 'timeout_pwd' => timeout_pwd}} - - begin - File.open(@file_config, 'w') do |file| - file << config.to_yaml - end - rescue Exception => e - @error_msg = "#{I18n.t('error.config.write')}\n#{e}" - return false - end - - return true - end - - # Check the config file - # @rtrn: true if the config file is correct - def checkconfig() - begin - config = YAML::load_file(@file_config) - @key = config['config']['key'] - @lang = config['config']['lang'] - @file_gpg = config['config']['file_gpg'] - @timeout_pwd = config['config']['timeout_pwd'].to_i - - if @key.empty? || @file_gpg.empty? - @error_msg = I18n.t('error.config.check') - return false - end - - I18n.locale = @lang.to_sym - - rescue Exception => e - @error_msg = "#{I18n.t('error.config.check')}\n#{e}" - return false - end - - return true + def initialize(file_gpg, key=nil) + @error_msg = nil + @file_gpg = file_gpg + @key = key end # Decrypt a gpg file @@ -117,10 +49,6 @@ class MPW return true rescue Exception => e - if !@file_pwd.nil? && File.exist?(@file_pwd) - File.delete(@file_pwd) - end - @error_msg = "#{I18n.t('error.gpg_file.decrypt')}\n#{e}" return false end @@ -327,7 +255,7 @@ class MPW end end - # Return + # Return a preview import # @args: file -> path to file import # @rtrn: an array with the items to import, if there is an error return false def importPreview(file) @@ -372,7 +300,6 @@ class MPW result << ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]).sample(length).join return result - #return ([*('A'..'Z'),*('a'..'z'),*('0'..'9')]-%w(0 1 I O l i o)).sample(length).join end - + end diff --git a/lib/MPWConfig.rb b/lib/MPWConfig.rb new file mode 100644 index 0000000..25dac98 --- /dev/null +++ b/lib/MPWConfig.rb @@ -0,0 +1,109 @@ +#!/usr/bin/ruby +# author: nishiki +# mail: nishiki@yaegashi.fr +# info: a simple script who manage your passwords + +require 'rubygems' +require 'yaml' +require 'i18n' + +class MPWConfig + + attr_accessor :error_msg + + attr_accessor :key + attr_accessor :lang + attr_accessor :file_gpg + attr_accessor :timeout_pwd + attr_accessor :sync_host + attr_accessor :sync_port + attr_accessor :sync_pwd + attr_accessor :sync_sufix + + # Constructor + # @args: file_config -> the specify config file + def initialize(file_config=nil) + @error_msg = nil + @file_config = "#{Dir.home()}/.mpw.cfg" + + if !file_config.nil? && !file_config.empty? + @file_config = file_config + end + end + + # Create a new config file + # @args: key -> the gpg key to encrypt + # lang -> the software language + # file_gpg -> the file who is encrypted + # timeout_pwd -> time to save the password + # @rtrn: true if le config file is create + def setup(key, lang, file_gpg, timeout_pwd) + + if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/ + @error_msg = I18n.t('error.config.key_bad_format') + return false + end + + if file_gpg.empty? + file_gpg = "#{Dir.home()}/.mpw.gpg" + end + + timeout_pwd.empty? ? (timeout_pwd = 60) : (timeout_pwd = timeout_pwd.to_i) + + config = {'config' => {'key' => key, + 'lang' => lang, + 'file_gpg' => file_gpg, + 'timeout_pwd' => timeout_pwd, + 'sync_host' => host, + 'sync_port' => port, + 'sync_pwd' => password, + 'sync_suffix' => suffix}} + + begin + File.open(@file_config, 'w') do |file| + file << config.to_yaml + end + rescue Exception => e + @error_msg = "#{I18n.t('error.config.write')}\n#{e}" + return false + end + + return true + end + + # Check the config file + # @rtrn: true if the config file is correct + def checkconfig() + begin + config = YAML::load_file(@file_config) + @key = config['config']['key'] + @lang = config['config']['lang'] + @file_gpg = config['config']['file_gpg'] + @timeout_pwd = config['config']['timeout_pwd'].to_i + @sync_host = config['config']['sync_host'] + @sync_port = config['config']['sync_port'] + @sync_pwd = config['config']['sync_pwd'] + @sync_sufix = config['config']['sync_suffix'] + + if @key.empty? || @file_gpg.empty? + @error_msg = I18n.t('error.config.check') + return false + end + + I18n.locale = @lang.to_sym + + rescue Exception => e + @error_msg = "#{I18n.t('error.config.check')}\n#{e}" + return false + end + + begin + @sync = TCPSocket.new(@sync_host, @sync_port) + rescue + @sync = false + end + + return true + end + +end