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

359 lines
10 KiB
Ruby
Raw Normal View History

2013-07-16 20:36:41 +00:00
#!/usr/bin/ruby
# author: nishiki
# mail: nishiki@yaegashi.fr
2013-07-17 20:31:28 +00:00
# info: a simple script who m your passwords
2013-07-16 20:36:41 +00:00
require 'rubygems'
require 'highline/import'
2013-07-18 19:56:45 +00:00
require 'pathname'
2013-12-20 16:38:58 +00:00
require 'readline'
2013-12-25 17:51:41 +00:00
require 'i18n'
2014-12-06 22:53:03 +00:00
require 'colorize'
2013-07-18 19:56:45 +00:00
2014-09-12 06:41:27 +00:00
require "#{APP_ROOT}/lib/MPW"
2013-07-16 20:36:41 +00:00
class Cli
2013-08-25 08:07:39 +00:00
# Constructor
2013-12-30 21:14:29 +00:00
# @args: lang -> the operating system language
# config_file -> a specify config file
2014-09-07 08:00:46 +00:00
def initialize(config)
2014-01-15 22:11:39 +00:00
@config = config
end
# Sync the data with the server
2014-01-26 11:13:32 +00:00
# @rtnr: true if the synchro is finish
2014-01-30 22:08:38 +00:00
def sync
2014-11-16 18:39:38 +00:00
if not defined?(@sync)
2014-01-30 22:08:38 +00:00
case @config.sync_type
when 'mpw'
2014-09-12 06:41:27 +00:00
require "#{APP_ROOT}/lib/Sync/MPWSync"
2014-01-30 22:08:38 +00:00
@sync = MPW::Sync::MPWSync.new
when 'sftp', 'scp', 'ssh'
2014-09-12 06:41:27 +00:00
require "#{APP_ROOT}/lib/Sync/SSH"
2014-01-30 22:08:38 +00:00
@sync = MPW::Sync::SSH.new
2014-02-02 10:24:28 +00:00
when 'ftp'
2014-09-12 06:41:27 +00:00
require "#{APP_ROOT}/lib/Sync/FTP"
2014-02-02 10:24:28 +00:00
@sync = MPW::Sync::FTP.new
2014-01-30 22:08:38 +00:00
else
return false
2014-01-25 15:53:48 +00:00
end
end
2015-01-16 23:05:40 +00:00
2014-11-16 18:39:38 +00:00
if not @config.sync_host.nil? and not @config.sync_port.nil?
if not @sync.connect(@config.sync_host, @config.sync_user, @config.sync_pwd, @config.sync_path, @config.sync_port)
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('display.error')} #1: #{@sync.error_msg}".red
2014-01-23 22:04:13 +00:00
end
2014-01-14 22:00:52 +00:00
end
2014-01-26 11:13:32 +00:00
2014-01-30 22:08:38 +00:00
if @sync.enable
2015-01-17 11:01:37 +00:00
if not @mpw.sync(@sync.get(@config.key, @passwd), @config.last_update)
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('display.error')} #2: #{@mpw.error_msg}".red if not @mpw.error_msg.nil?
puts "#{I18n.t('display.error')} #3: #{@sync.error_msg}".red if not @sync.error_msg.nil?
2015-01-17 11:01:37 +00:00
2014-11-16 18:39:38 +00:00
elsif not @sync.update(File.open(@config.file_gpg).read)
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('display.error')} #4: #{@sync.error_msg}".red
2015-01-17 11:01:37 +00:00
2014-11-16 18:39:38 +00:00
elsif not @config.set_last_update
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('display.error')} #5: #{@config.error_msg}".red
2015-01-17 11:01:37 +00:00
2014-11-16 18:39:38 +00:00
elsif not @mpw.encrypt
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('display.error')} #6: #{@mpw.error_msg}".red
2015-01-17 11:01:37 +00:00
2014-01-30 22:08:38 +00:00
else
return true
end
end
rescue Exception => e
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('display.error')} #7: #{e}".red
2014-12-06 20:37:18 +00:00
puts @sync.error_msg if not @sync.error_msg.nil?
puts @config.error_msg if not @config.error_msg.nil?
puts @mpw.error_msg if not @mpw.error_msg.nil?
2014-01-30 22:08:38 +00:00
else
2014-01-26 11:13:32 +00:00
return false
2013-07-16 20:36:41 +00:00
end
# Create a new config file
2013-12-25 17:51:41 +00:00
# @args: lang -> the software language
def setup(lang)
2014-01-26 10:39:53 +00:00
puts I18n.t('form.setup.title')
puts '--------------------'
2014-08-31 10:29:57 +00:00
language = ask(I18n.t('form.setup.lang', lang: lang)).to_s
2014-01-26 10:39:53 +00:00
key = ask(I18n.t('form.setup.gpg_key')).to_s
2014-02-02 13:20:24 +00:00
share_keys = ask(I18n.t('form.setup.share_gpg_keys')).to_s
2014-12-06 15:24:40 +00:00
file_gpg = ask(I18n.t('form.setup.gpg_file', home: @conf.dir_config)).to_s
2014-02-01 18:11:19 +00:00
sync_type = ask(I18n.t('form.setup.sync_type')).to_s
2014-02-02 13:20:24 +00:00
if ['ssh', 'ftp', 'mpw'].include?(sync_type)
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
end
2013-07-16 20:36:41 +00:00
2014-11-16 18:39:38 +00:00
if language.nil? or language.empty?
2014-03-16 20:28:23 +00:00
language = lang
2014-02-02 13:20:24 +00:00
end
2014-03-16 20:28:23 +00:00
I18n.locale = language.to_sym
2013-12-25 17:51:41 +00:00
2014-11-16 18:39:38 +00:00
sync_type = sync_type.nil? or sync_type.empty? ? nil : sync_type
sync_host = sync_host.nil? or sync_host.empty? ? nil : sync_host
sync_port = sync_port.nil? or sync_port.empty? ? nil : sync_port.to_i
sync_user = sync_user.nil? or sync_user.empty? ? nil : sync_user
sync_pwd = sync_pwd.nil? or sync_pwd.empty? ? nil : sync_pwd
sync_path = sync_path.nil? or sync_path.empty? ? nil : sync_path
2014-01-25 15:53:48 +00:00
2014-12-06 22:40:09 +00:00
if @config.setup(key, share_keys, language, file_gpg, sync_type, sync_host, sync_port, sync_user, sync_pwd, sync_path)
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('form.setup.valid')}".green
2013-07-17 20:31:28 +00:00
else
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('display.error')} #8: #{@config.error_msg}".red
2014-02-02 13:20:24 +00:00
exit 2
2013-07-16 20:36:41 +00:00
end
2014-02-02 09:16:10 +00:00
if not @config.checkconfig
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('display.error')} #9: #{@config.error_msg}".red
2014-01-25 15:53:48 +00:00
exit 2
end
2013-07-17 20:31:28 +00:00
end
2013-07-16 20:36:41 +00:00
2014-03-16 20:28:23 +00:00
# Setup a new GPG key
def setup_gpg_key
puts I18n.t('form.setup_gpg_key.title')
puts '--------------------'
2014-03-18 20:08:09 +00:00
ask = ask(I18n.t('form.setup_gpg_key.ask')).to_s
2014-11-16 18:39:38 +00:00
if not ['Y', 'y', 'O', 'o'].include?(ask)
2014-03-18 20:08:09 +00:00
puts I18n.t('form.setup_gpg_key.no_create')
exit 2
end
2014-03-16 20:28:23 +00:00
name = ask(I18n.t('form.setup_gpg_key.name')).to_s
password = ask(I18n.t('form.setup_gpg_key.password')) {|q| q.echo = false}
confirm = ask(I18n.t('form.setup_gpg_key.confirm_password')) {|q| q.echo = false}
if password != confirm
puts I18n.t('form.setup_gpg_key.error_password')
exit 2
end
2014-03-16 20:28:23 +00:00
length = ask(I18n.t('form.setup_gpg_key.length')).to_s
expire = ask(I18n.t('form.setup_gpg_key.expire')).to_s
password = password.to_s
2014-03-16 20:28:23 +00:00
2014-11-16 18:39:38 +00:00
length = length.nil? or length.empty? ? 2048 : length.to_i
expire = expire.nil? or expire.empty? ? 0 : expire.to_i
2014-03-16 20:28:23 +00:00
puts I18n.t('form.setup_gpg_key.wait')
if @config.setup_gpg_key(password, name, length, expire)
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('form.setup_gpg_key.valid')}".green
2014-03-16 20:28:23 +00:00
else
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('display.error')} #10: #{@config.error_msg}".red
2014-03-16 20:28:23 +00:00
exit 2
end
end
2013-08-25 08:07:39 +00:00
# Request the GPG password and decrypt the file
2014-01-30 22:08:38 +00:00
def decrypt
2014-11-16 18:39:38 +00:00
if not defined?(@mpw)
2014-02-02 16:34:05 +00:00
@mpw = MPW::MPW.new(@config.file_gpg, @config.key, @config.share_keys)
2014-01-25 15:53:48 +00:00
end
2014-01-26 10:39:53 +00:00
@passwd = ask(I18n.t('display.gpg_password')) {|q| q.echo = false}
2014-11-16 18:39:38 +00:00
if not @mpw.decrypt(@passwd)
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('display.error')} #11: #{@mpw.error_msg}".red
2014-01-25 15:53:48 +00:00
exit 2
end
2013-07-17 20:31:28 +00:00
end
2013-07-16 20:36:41 +00:00
2013-08-25 08:07:39 +00:00
# Display the query's result
# @args: search -> the string to search
# protocol -> search from a particular protocol
2015-01-16 23:05:40 +00:00
def display(search, protocol=nil, group=nil)
2014-01-11 23:37:46 +00:00
result = @mpw.search(search, group, protocol)
2013-07-16 20:36:41 +00:00
2015-01-16 23:05:40 +00:00
case result.length
when 0
puts I18n.t('display.nothing')
when 1
display_item(result.first)
else
i = 1
2013-07-17 20:31:28 +00:00
result.each do |r|
2015-01-16 23:05:40 +00:00
print "#{i}: ".cyan
print r['name']
print " -> #{r['comment']}".magenta if not r['comment'].to_s.empty?
print "\n"
i += 1
end
choice = ask(I18n.t('form.select')).to_i
if choice >= 1 and choice < i
display_item(result[choice-1])
else
puts "#{I18n.t('display.warning')}: #{I18n.t('warning.select')}".yellow
2013-07-17 20:31:28 +00:00
end
2013-07-16 20:36:41 +00:00
end
end
# Display an item in the default format
2013-09-02 17:14:33 +00:00
# @args: item -> an array with the item information
2015-01-16 23:05:40 +00:00
def display_item(item)
2015-01-16 23:12:01 +00:00
puts '--------------------'.cyan
print 'Id: '.cyan
puts item['id']
print "#{I18n.t('display.name')}: ".cyan
puts item['name']
print "#{I18n.t('display.group')}: ".cyan
puts item['group']
print "#{I18n.t('display.server')}: ".cyan
puts item['host']
print "#{I18n.t('display.protocol')}: ".cyan
puts item['protocol']
print "#{I18n.t('display.login')}: ".cyan
puts item['login']
print "#{I18n.t('display.password')}: ".cyan
puts item['password']
print "#{I18n.t('display.port')}: ".cyan
puts item['port']
print "#{I18n.t('display.comment')}: ".cyan
puts item['comment']
2013-09-02 17:14:33 +00:00
end
2013-08-25 08:07:39 +00:00
# Form to add a new item
2014-02-02 09:16:10 +00:00
def add
2014-01-31 23:09:22 +00:00
row = []
2014-01-26 10:39:53 +00:00
puts I18n.t('form.add.title')
puts '--------------------'
2014-01-26 10:39:53 +00:00
name = ask(I18n.t('form.add.name')).to_s
group = ask(I18n.t('form.add.group')).to_s
server = ask(I18n.t('form.add.server')).to_s
protocol = ask(I18n.t('form.add.protocol')).to_s
login = ask(I18n.t('form.add.login')).to_s
passwd = ask(I18n.t('form.add.password')).to_s
port = ask(I18n.t('form.add.port')).to_s
comment = ask(I18n.t('form.add.comment')).to_s
2013-07-17 20:31:28 +00:00
2014-01-15 19:01:23 +00:00
if @mpw.update(name, group, server, protocol, login, passwd, port, comment)
2014-02-02 09:16:10 +00:00
if @mpw.encrypt
sync
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('form.add.valid')}".green
else
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('display.error')} #12: #{@mpw.error_msg}".red
end
2013-07-17 20:31:28 +00:00
else
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('display.error')} #13: #{@mpw.error_msg}".red
2013-07-17 20:31:28 +00:00
end
end
2013-07-16 20:36:41 +00:00
2013-08-25 08:07:39 +00:00
# Update an item
# @args: id -> the item's id
2013-07-17 20:31:28 +00:00
def update(id)
2014-01-26 14:09:48 +00:00
row = @mpw.search_by_id(id)
2013-07-17 20:31:28 +00:00
if not row.empty?
2014-01-26 10:39:53 +00:00
puts I18n.t('form.update.title')
puts '--------------------'
2015-01-27 08:37:53 +00:00
name = ask(I18n.t('form.update.name' , name: row['name'])).to_s
group = ask(I18n.t('form.update.group' , group: row['group'])).to_s
server = ask(I18n.t('form.update.server' , server: row['host'])).to_s
protocol = ask(I18n.t('form.update.protocol', protocol: row['protocol'])).to_s
login = ask(I18n.t('form.update.login' , login: row['login'])).to_s
2014-01-26 10:39:53 +00:00
passwd = ask(I18n.t('form.update.password')).to_s
2015-01-27 08:37:53 +00:00
port = ask(I18n.t('form.update.port' , port: row['port'])).to_s
comment = ask(I18n.t('form.update.comment' , comment: row['comment'])).to_s
2013-07-17 20:31:28 +00:00
2014-01-15 19:01:23 +00:00
if @mpw.update(name, group, server, protocol, login, passwd, port, comment, id)
2014-02-02 09:16:10 +00:00
if @mpw.encrypt
sync
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('form.update.valid')}".green
2013-07-17 20:31:28 +00:00
else
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('display.error')} #14: #{@mpw.error_msg}".red
2013-07-17 20:31:28 +00:00
end
else
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('display.error')} #15: #{@mpw.error_msg}".red
2013-07-16 20:36:41 +00:00
end
2013-07-17 20:31:28 +00:00
else
2014-01-26 10:39:53 +00:00
puts I18n.t('display.nothing')
2013-07-17 20:31:28 +00:00
end
end
2013-07-16 20:36:41 +00:00
2013-08-25 08:07:39 +00:00
# Remove an item
# @args: id -> the item's id
# force -> no resquest a validation
def remove(id, force=false)
if not force
2014-01-26 14:09:48 +00:00
result = @mpw.search_by_id(id)
2013-08-26 19:23:35 +00:00
if result.length > 0
2015-01-16 22:11:10 +00:00
display_item(result)
2013-08-26 19:23:35 +00:00
2014-08-31 10:29:57 +00:00
confirm = ask("#{I18n.t('form.delete.ask', id: id)} (y/N) ").to_s
if confirm =~ /^(y|yes|YES|Yes|Y)$/
force = true
end
else
2014-01-26 10:39:53 +00:00
puts I18n.t('display.nothing')
end
end
if force
2014-01-11 23:37:46 +00:00
if @mpw.remove(id)
2014-02-02 09:16:10 +00:00
if @mpw.encrypt
sync
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('form.delete.valid', id: id)}".green
else
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('display.error')} #16: #{@mpw.error_msg}".red
end
2013-07-17 20:31:28 +00:00
else
2014-01-26 10:39:53 +00:00
puts I18n.t('form.delete.not_valid')
2013-07-17 20:31:28 +00:00
end
2013-07-16 20:36:41 +00:00
end
2013-07-17 20:31:28 +00:00
end
2013-07-16 20:36:41 +00:00
2013-08-25 08:07:39 +00:00
# Export the items in a CSV file
# @args: file -> the destination file
2014-12-06 22:32:09 +00:00
def export(file, type=:yaml)
2014-11-15 18:14:42 +00:00
if @mpw.export(file, type)
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('export.valid', file)}".green
2013-07-25 17:51:43 +00:00
else
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('display.error')} #17: #{@mpw.error_msg}".red
2013-07-25 17:51:43 +00:00
end
end
2013-08-25 08:07:39 +00:00
# Import items from a CSV file
# @args: file -> the import file
2013-08-26 20:19:37 +00:00
# force -> no resquest a validation
2014-12-06 22:32:09 +00:00
def import(file, type=:yaml, force=false)
2013-08-26 20:19:37 +00:00
if not force
2014-12-06 22:32:09 +00:00
result = @mpw.import_preview(file, type)
2014-12-06 20:37:18 +00:00
if result.is_a?(Array) and not result.empty?
2013-08-26 20:19:37 +00:00
result.each do |r|
2015-01-16 22:11:10 +00:00
display_item(r)
2013-08-26 20:19:37 +00:00
end
2014-08-31 10:29:57 +00:00
confirm = ask("#{I18n.t('form.import.ask', file: file)} (y/N) ").to_s
2013-08-26 20:19:37 +00:00
if confirm =~ /^(y|yes|YES|Yes|Y)$/
force = true
end
else
2014-01-26 10:39:53 +00:00
puts I18n.t('form.import.not_valid')
2013-08-26 20:19:37 +00:00
end
end
if force
2014-12-06 22:32:09 +00:00
if @mpw.import(file, type) and @mpw.encrypt
2014-02-02 09:16:10 +00:00
sync
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('form.import.valid')}".green
2013-07-25 17:51:43 +00:00
else
2014-12-06 22:53:03 +00:00
puts "#{I18n.t('display.error')} #18: #{@mpw.error_msg}".red
2013-07-25 17:51:43 +00:00
end
end
end
2013-07-16 20:36:41 +00:00
end