2013-07-16 22:36:41 +02:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
# author: nishiki
|
|
|
|
# mail: nishiki@yaegashi.fr
|
|
|
|
|
2013-12-20 17:38:58 +01:00
|
|
|
require 'readline'
|
2013-12-25 18:51:41 +01:00
|
|
|
require 'i18n'
|
2014-12-06 23:53:03 +01:00
|
|
|
require 'colorize'
|
2016-05-05 20:19:25 +02:00
|
|
|
require 'highline/import'
|
2016-05-05 23:07:37 +02:00
|
|
|
|
|
|
|
#TODO
|
|
|
|
require "#{APP_ROOT}/../lib/mpw/item.rb"
|
|
|
|
require "#{APP_ROOT}/../lib/mpw/mpw.rb"
|
2013-07-16 22:36:41 +02:00
|
|
|
|
|
|
|
class Cli
|
|
|
|
|
2013-08-25 10:07:39 +02:00
|
|
|
# Constructor
|
2013-12-30 22:14:29 +01:00
|
|
|
# @args: lang -> the operating system language
|
|
|
|
# config_file -> a specify config file
|
2016-05-05 20:19:25 +02:00
|
|
|
# TODO
|
2016-05-05 23:43:34 +02:00
|
|
|
def initialize(config, wallet)
|
2014-01-15 23:11:39 +01:00
|
|
|
@config = config
|
2016-05-05 23:43:34 +02:00
|
|
|
@wallet_file = "#{@config.wallet_dir}/#{wallet}.mpw"
|
2013-07-16 22:36:41 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Create a new config file
|
2013-12-25 18:51:41 +01:00
|
|
|
# @args: lang -> the software language
|
|
|
|
def setup(lang)
|
2014-01-26 11:39:53 +01:00
|
|
|
puts I18n.t('form.setup.title')
|
2013-12-30 19:47:47 +01:00
|
|
|
puts '--------------------'
|
2016-05-05 20:19:25 +02:00
|
|
|
language = ask(I18n.t('form.setup.lang', lang: lang)).to_s
|
|
|
|
key = ask(I18n.t('form.setup.gpg_key')).to_s
|
|
|
|
wallet_dir = ask(I18n.t('form.setup.wallet_dir')).to_s
|
|
|
|
|
2014-11-16 19:39:38 +01:00
|
|
|
if language.nil? or language.empty?
|
2014-03-16 21:28:23 +01:00
|
|
|
language = lang
|
2014-02-02 14:20:24 +01:00
|
|
|
end
|
2014-03-16 21:28:23 +01:00
|
|
|
I18n.locale = language.to_sym
|
2013-12-25 18:51:41 +01:00
|
|
|
|
2016-05-05 20:19:25 +02:00
|
|
|
if @config.setup(key, lang, wallet_dir)
|
2014-12-06 23:53:03 +01:00
|
|
|
puts "#{I18n.t('form.setup.valid')}".green
|
2013-07-17 22:31:28 +02:00
|
|
|
else
|
2014-12-06 23:53:03 +01:00
|
|
|
puts "#{I18n.t('display.error')} #8: #{@config.error_msg}".red
|
2014-02-02 14:20:24 +01:00
|
|
|
exit 2
|
2013-07-16 22:36:41 +02:00
|
|
|
end
|
2014-01-03 14:31:43 +01:00
|
|
|
|
2014-02-02 10:16:10 +01:00
|
|
|
if not @config.checkconfig
|
2014-12-06 23:53:03 +01:00
|
|
|
puts "#{I18n.t('display.error')} #9: #{@config.error_msg}".red
|
2014-01-25 16:53:48 +01:00
|
|
|
exit 2
|
2014-01-03 14:31:43 +01:00
|
|
|
end
|
2013-07-17 22:31:28 +02:00
|
|
|
end
|
2016-05-05 20:19:25 +02:00
|
|
|
|
2014-03-16 21:28:23 +01:00
|
|
|
# Setup a new GPG key
|
|
|
|
def setup_gpg_key
|
|
|
|
puts I18n.t('form.setup_gpg_key.title')
|
|
|
|
puts '--------------------'
|
2014-03-18 21:08:09 +01:00
|
|
|
ask = ask(I18n.t('form.setup_gpg_key.ask')).to_s
|
|
|
|
|
2014-11-16 19:39:38 +01:00
|
|
|
if not ['Y', 'y', 'O', 'o'].include?(ask)
|
2014-03-18 21:08:09 +01:00
|
|
|
puts I18n.t('form.setup_gpg_key.no_create')
|
|
|
|
exit 2
|
|
|
|
end
|
|
|
|
|
2014-03-16 21:28:23 +01:00
|
|
|
name = ask(I18n.t('form.setup_gpg_key.name')).to_s
|
2014-04-04 17:03:46 +02:00
|
|
|
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 21:28:23 +01:00
|
|
|
length = ask(I18n.t('form.setup_gpg_key.length')).to_s
|
|
|
|
expire = ask(I18n.t('form.setup_gpg_key.expire')).to_s
|
2014-04-04 17:03:46 +02:00
|
|
|
password = password.to_s
|
2014-03-16 21:28:23 +01:00
|
|
|
|
2014-11-16 19:39:38 +01:00
|
|
|
length = length.nil? or length.empty? ? 2048 : length.to_i
|
|
|
|
expire = expire.nil? or expire.empty? ? 0 : expire.to_i
|
2014-03-16 21:28:23 +01:00
|
|
|
|
|
|
|
puts I18n.t('form.setup_gpg_key.wait')
|
|
|
|
|
|
|
|
if @config.setup_gpg_key(password, name, length, expire)
|
2014-12-06 23:53:03 +01:00
|
|
|
puts "#{I18n.t('form.setup_gpg_key.valid')}".green
|
2014-03-16 21:28:23 +01:00
|
|
|
else
|
2014-12-06 23:53:03 +01:00
|
|
|
puts "#{I18n.t('display.error')} #10: #{@config.error_msg}".red
|
2014-03-16 21:28:23 +01:00
|
|
|
exit 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-05 20:19:25 +02:00
|
|
|
|
2013-08-25 10:07:39 +02:00
|
|
|
# Request the GPG password and decrypt the file
|
2014-01-30 23:08:38 +01:00
|
|
|
def decrypt
|
2014-11-16 19:39:38 +01:00
|
|
|
if not defined?(@mpw)
|
2016-05-05 20:19:25 +02:00
|
|
|
@password = ask(I18n.t('display.gpg_password')) {|q| q.echo = false}
|
|
|
|
@mpw = MPW::MPW.new(@config.key, @wallet_file, @password)
|
2014-01-25 16:53:48 +01:00
|
|
|
end
|
|
|
|
|
2016-05-05 20:19:25 +02:00
|
|
|
@mpw.read_data
|
2013-07-17 22:31:28 +02:00
|
|
|
end
|
2013-07-16 22:36:41 +02:00
|
|
|
|
2013-08-25 10:07:39 +02:00
|
|
|
# Display the query's result
|
|
|
|
# @args: search -> the string to search
|
|
|
|
# protocol -> search from a particular protocol
|
2015-02-14 10:42:03 +01:00
|
|
|
def display(options={})
|
|
|
|
result = @mpw.list(options)
|
2013-07-16 22:36:41 +02:00
|
|
|
|
2015-01-17 00:05:40 +01:00
|
|
|
case result.length
|
|
|
|
when 0
|
|
|
|
puts I18n.t('display.nothing')
|
|
|
|
when 1
|
|
|
|
display_item(result.first)
|
|
|
|
else
|
|
|
|
i = 1
|
2015-01-28 23:08:25 +01:00
|
|
|
result.each do |item|
|
2015-01-17 00:05:40 +01:00
|
|
|
print "#{i}: ".cyan
|
2015-01-28 23:08:25 +01:00
|
|
|
print item.name
|
|
|
|
print " -> #{item.comment}".magenta if not item.comment.to_s.empty?
|
2015-01-17 00:05:40 +01:00
|
|
|
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 22:31:28 +02:00
|
|
|
end
|
2013-07-16 22:36:41 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-02 19:50:40 +02:00
|
|
|
# Display an item in the default format
|
2013-09-02 19:14:33 +02:00
|
|
|
# @args: item -> an array with the item information
|
2015-01-17 00:05:40 +01:00
|
|
|
def display_item(item)
|
2015-01-17 00:12:01 +01:00
|
|
|
puts '--------------------'.cyan
|
|
|
|
print 'Id: '.cyan
|
2015-01-28 23:08:25 +01:00
|
|
|
puts item.id
|
2015-01-17 00:12:01 +01:00
|
|
|
print "#{I18n.t('display.name')}: ".cyan
|
2015-01-28 23:08:25 +01:00
|
|
|
puts item.name
|
2015-01-17 00:12:01 +01:00
|
|
|
print "#{I18n.t('display.group')}: ".cyan
|
2015-01-28 23:08:25 +01:00
|
|
|
puts item.group
|
2015-01-17 00:12:01 +01:00
|
|
|
print "#{I18n.t('display.server')}: ".cyan
|
2015-01-28 23:08:25 +01:00
|
|
|
puts item.host
|
2015-01-17 00:12:01 +01:00
|
|
|
print "#{I18n.t('display.protocol')}: ".cyan
|
2015-01-28 23:08:25 +01:00
|
|
|
puts item.protocol
|
2015-01-17 00:12:01 +01:00
|
|
|
print "#{I18n.t('display.login')}: ".cyan
|
2015-01-28 23:08:25 +01:00
|
|
|
puts item.user
|
2015-01-17 00:12:01 +01:00
|
|
|
print "#{I18n.t('display.password')}: ".cyan
|
2016-05-05 20:19:25 +02:00
|
|
|
puts @mpw.get_password(item.id)
|
2015-01-17 00:12:01 +01:00
|
|
|
print "#{I18n.t('display.port')}: ".cyan
|
2015-01-28 23:08:25 +01:00
|
|
|
puts item.port
|
2015-01-17 00:12:01 +01:00
|
|
|
print "#{I18n.t('display.comment')}: ".cyan
|
2015-01-28 23:08:25 +01:00
|
|
|
puts item.comment
|
2013-09-02 19:14:33 +02:00
|
|
|
end
|
|
|
|
|
2013-08-25 10:07:39 +02:00
|
|
|
# Form to add a new item
|
2014-02-02 10:16:10 +01:00
|
|
|
def add
|
2015-02-01 11:48:02 +01:00
|
|
|
options = {}
|
|
|
|
|
2014-01-26 11:39:53 +01:00
|
|
|
puts I18n.t('form.add.title')
|
2013-12-30 19:47:47 +01:00
|
|
|
puts '--------------------'
|
2015-02-01 11:48:02 +01:00
|
|
|
options[:name] = ask(I18n.t('form.add.name')).to_s
|
|
|
|
options[:group] = ask(I18n.t('form.add.group')).to_s
|
|
|
|
options[:host] = ask(I18n.t('form.add.server')).to_s
|
|
|
|
options[:protocol] = ask(I18n.t('form.add.protocol')).to_s
|
|
|
|
options[:user] = ask(I18n.t('form.add.login')).to_s
|
2016-05-05 20:19:25 +02:00
|
|
|
password = ask(I18n.t('form.add.password')).to_s
|
2015-02-01 11:48:02 +01:00
|
|
|
options[:port] = ask(I18n.t('form.add.port')).to_s
|
|
|
|
options[:comment] = ask(I18n.t('form.add.comment')).to_s
|
|
|
|
|
|
|
|
item = MPW::Item.new(options)
|
2016-05-05 20:19:25 +02:00
|
|
|
|
|
|
|
@mpw.add(item)
|
|
|
|
@mpw.set_password(item.id, password)
|
|
|
|
@mpw.write_data
|
|
|
|
|
|
|
|
puts "#{I18n.t('form.add.valid')}".green
|
2013-07-17 22:31:28 +02:00
|
|
|
end
|
2013-07-16 22:36:41 +02:00
|
|
|
|
2013-08-25 10:07:39 +02:00
|
|
|
# Update an item
|
|
|
|
# @args: id -> the item's id
|
2013-07-17 22:31:28 +02:00
|
|
|
def update(id)
|
2015-01-28 23:08:25 +01:00
|
|
|
item = @mpw.search_by_id(id)
|
2013-07-17 22:31:28 +02:00
|
|
|
|
2015-02-01 12:23:50 +01:00
|
|
|
if not item.nil?
|
|
|
|
options = {}
|
|
|
|
|
2014-01-26 11:39:53 +01:00
|
|
|
puts I18n.t('form.update.title')
|
2013-12-30 19:47:47 +01:00
|
|
|
puts '--------------------'
|
2015-02-01 12:23:50 +01:00
|
|
|
options[:name] = ask(I18n.t('form.update.name' , name: item.name)).to_s
|
|
|
|
options[:group] = ask(I18n.t('form.update.group' , group: item.group)).to_s
|
|
|
|
options[:host] = ask(I18n.t('form.update.server' , server: item.host)).to_s
|
|
|
|
options[:protocol] = ask(I18n.t('form.update.protocol', protocol: item.protocol)).to_s
|
|
|
|
options[:user] = ask(I18n.t('form.update.login' , login: item.user)).to_s
|
2016-05-05 20:19:25 +02:00
|
|
|
password = ask(I18n.t('form.update.password')).to_s
|
2015-02-01 12:23:50 +01:00
|
|
|
options[:port] = ask(I18n.t('form.update.port' , port: item.port)).to_s
|
|
|
|
options[:comment] = ask(I18n.t('form.update.comment' , comment: item.comment)).to_s
|
|
|
|
|
|
|
|
options.delete_if { |k,v| v.empty? }
|
2013-07-17 22:31:28 +02:00
|
|
|
|
2016-05-05 20:19:25 +02:00
|
|
|
item.update(options)
|
2016-05-05 23:14:28 +02:00
|
|
|
@mpw.set_password(item.id, password) if not password.empty?
|
2016-05-05 20:19:25 +02:00
|
|
|
@mpw.write_data
|
|
|
|
|
|
|
|
puts "#{I18n.t('form.update.valid')}".green
|
2013-07-17 22:31:28 +02:00
|
|
|
else
|
2014-01-26 11:39:53 +01:00
|
|
|
puts I18n.t('display.nothing')
|
2013-07-17 22:31:28 +02:00
|
|
|
end
|
2016-05-05 23:14:28 +02:00
|
|
|
rescue Exception => e
|
|
|
|
puts "#{I18n.t('display.error')} #14: #{e}".red
|
2013-07-17 22:31:28 +02:00
|
|
|
end
|
2013-07-16 22:36:41 +02:00
|
|
|
|
2013-08-25 10:07:39 +02:00
|
|
|
# Remove an item
|
|
|
|
# @args: id -> the item's id
|
|
|
|
# force -> no resquest a validation
|
2015-02-01 12:35:42 +01:00
|
|
|
def delete(id, force=false)
|
2016-05-05 23:24:08 +02:00
|
|
|
item = @mpw.search_by_id(id)
|
2013-08-26 21:23:35 +02:00
|
|
|
|
2016-05-05 23:24:08 +02:00
|
|
|
if item.nil?
|
|
|
|
puts I18n.t('display.nothing')
|
|
|
|
return
|
2013-07-17 23:19:35 +02:00
|
|
|
end
|
|
|
|
|
2016-05-05 23:24:08 +02:00
|
|
|
if not force
|
|
|
|
display_item(item)
|
2015-02-01 12:35:42 +01:00
|
|
|
|
2016-05-05 23:24:08 +02:00
|
|
|
confirm = ask("#{I18n.t('form.delete.ask', id: id)} (y/N) ").to_s
|
|
|
|
if not confirm =~ /^(y|yes|YES|Yes|Y)$/
|
|
|
|
return
|
2013-07-17 22:31:28 +02:00
|
|
|
end
|
2013-07-16 22:36:41 +02:00
|
|
|
end
|
2016-05-05 23:24:08 +02:00
|
|
|
|
|
|
|
item.delete
|
|
|
|
@mpw.write_data
|
|
|
|
|
|
|
|
puts "#{I18n.t('form.delete.valid', id: id)}".green
|
|
|
|
rescue
|
|
|
|
puts "#{I18n.t('display.error')} #16: #{@mpw.error_msg}".red
|
2013-07-17 22:31:28 +02:00
|
|
|
end
|
2013-07-16 22:36:41 +02:00
|
|
|
|
2013-08-25 10:07:39 +02:00
|
|
|
# Export the items in a CSV file
|
|
|
|
# @args: file -> the destination file
|
2016-05-05 20:19:25 +02:00
|
|
|
def export(file)
|
|
|
|
@mpw.export(file)
|
|
|
|
|
|
|
|
puts "#{I18n.t('export.valid', file)}".green
|
|
|
|
rescue Exception => e
|
|
|
|
puts "#{I18n.t('display.error')} #17: #{e}".red
|
2013-07-25 19:51:43 +02:00
|
|
|
end
|
|
|
|
|
2016-05-05 20:19:25 +02:00
|
|
|
# Import items from a YAML file
|
2013-08-25 10:07:39 +02:00
|
|
|
# @args: file -> the import file
|
2016-05-05 20:19:25 +02:00
|
|
|
def import(file)
|
|
|
|
@mpw.import(file)
|
|
|
|
@mpw.write_data
|
2013-08-26 22:19:37 +02:00
|
|
|
|
2016-05-05 20:19:25 +02:00
|
|
|
puts "#{I18n.t('form.import.valid')}".green
|
|
|
|
rescue Exception => e
|
|
|
|
puts "#{I18n.t('display.error')} #18: #{e}".red
|
2013-07-25 19:51:43 +02:00
|
|
|
end
|
2013-07-16 22:36:41 +02:00
|
|
|
end
|