1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-12-18 07:23:30 +00:00
mpw/lib/MPWConfig.rb

131 lines
3.4 KiB
Ruby
Raw Normal View History

2014-01-11 23:37:46 +00:00
#!/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
2014-01-14 22:00:52 +00:00
attr_accessor :last_update
2014-01-11 23:37:46 +00:00
attr_accessor :sync_host
attr_accessor :sync_port
attr_accessor :sync_pwd
2014-01-14 22:00:52 +00:00
attr_accessor :sync_suffix
attr_accessor :last_update
2014-01-11 23:37:46 +00:00
# Constructor
# @args: file_config -> the specify config file
def initialize(file_config=nil)
2014-01-12 10:36:56 +00:00
@error_msg = nil
2014-01-11 23:37:46 +00:00
@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,
2014-01-12 10:36:56 +00:00
'sync_suffix' => suffix,
'last_update' => 0 }}
2014-01-11 23:37:46 +00:00
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)
2014-01-14 22:00:52 +00:00
@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']
2014-01-15 19:55:17 +00:00
@sync_suffix = config['config']['sync_suffix']
2014-01-14 22:00:52 +00:00
@last_update = config['config']['last_update'].to_i
2014-01-11 23:37:46 +00:00
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
end
2014-01-14 22:00:52 +00:00
def setLastUpdate()
config = {'config' => {'key' => @key,
'lang' => @lang,
'file_gpg' => @file_gpg,
'timeout_pwd' => @timeout_pwd,
'sync_host' => @sync_host,
'sync_port' => @sync_port,
'sync_pwd' => @sync_pwd,
2014-01-15 19:52:59 +00:00
'sync_suffix' => @sync_suffix,
2014-01-14 22:00:52 +00:00
'last_update' => Time.now.to_i }}
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
2014-01-11 23:37:46 +00:00
end