1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-30 08:53:05 +00:00
mpw/MPW/UI/Cli.rb

360 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-01-14 22:00:52 +00:00
require 'yaml'
2013-07-18 19:56:45 +00:00
2014-01-29 19:49:39 +00:00
require "#{APP_ROOT}/MPW/MPW"
require "#{APP_ROOT}/MPW/Sync/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-01-15 22:11:39 +00:00
def initialize(lang, config)
@config = config
end
2014-01-26 14:09:48 +00:00
# Close sync
def sync_close()
2014-01-15 22:11:39 +00:00
@sync.close()
end
# Sync the data with the server
2014-01-26 11:13:32 +00:00
# @rtnr: true if the synchro is finish
2014-01-15 22:11:39 +00:00
def sync()
2014-01-25 15:53:48 +00:00
if !defined?(@sync)
2014-01-29 19:49:39 +00:00
@sync = MPW::Sync::MPW.new
2014-01-25 15:53:48 +00:00
2014-01-26 11:13:32 +00:00
if !@config.sync_host.nil? && !@config.sync_port.nil?
if !@sync.connect(@config.sync_host, @config.sync_port, @config.key, @config.sync_pwd, @config.sync_suffix)
puts "#{I18n.t('display.error')}: #{@sync.error_msg}"
end
2014-01-25 15:53:48 +00:00
end
end
2014-01-15 22:11:39 +00:00
begin
2014-01-26 11:13:32 +00:00
if @sync.enable
if !@mpw.sync(@sync.get(@passwd), @config.last_update)
puts "#{I18n.t('display.error')}: #{@mpw.error_msg}"
elsif !@sync.update(File.open(@config.file_gpg).read)
puts "#{I18n.t('display.error')}: #{@sync.error_msg}"
2014-01-26 14:09:48 +00:00
elsif !@config.set_last_update()
2014-01-26 11:13:32 +00:00
puts "#{I18n.t('display.error')}: #{@config.error_msg}"
else
return true
end
2014-01-23 22:04:13 +00:00
end
2014-01-15 22:11:39 +00:00
rescue Exception => e
2014-01-27 19:19:38 +00:00
puts "#{I18n.t('display.error')}: #{e}"
2014-01-14 22:00:52 +00:00
end
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-01-26 10:39:53 +00:00
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
timeout_pwd = ask(I18n.t('form.setup.timeout')).to_s
sync_host = ask(I18n.t('form.setup.sync_host')).to_s
sync_port = ask(I18n.t('form.setup.sync_port')).to_s
sync_pwd = ask(I18n.t('form.setup.sync_pwd')).to_s
sync_suffix = ask(I18n.t('form.setup.sync_suffix')).to_s
2013-07-16 20:36:41 +00:00
2013-12-25 17:51:41 +00:00
if !File.exist?("#{APP_ROOT}/i18n/#{language}.yml")
2013-12-26 09:47:54 +00:00
language= 'en'
2013-12-25 17:51:41 +00:00
end
I18n.locale = language.to_sym
2014-01-29 18:18:36 +00:00
sync_host = sync_host.empty? ? nil : sync_host
sync_port = sync_port.empty? ? nil : sync_port.to_i
sync_pwd = sync_pwd.empty? ? nil : sync_pwd
sync_suffix = sync_suffix.empty? ? nil : sync_suffix
2014-01-25 15:53:48 +00:00
if @config.setup(key, language, file_gpg, timeout_pwd, sync_host, sync_port, sync_pwd, sync_suffix)
2014-01-26 10:39:53 +00:00
puts I18n.t('form.setup.valid')
2013-07-17 20:31:28 +00:00
else
2014-01-26 10:39:53 +00:00
puts "#{I18n.t('display.error')}: #{@config.error_msg}"
2013-07-16 20:36:41 +00:00
end
2014-01-11 23:37:46 +00:00
if not @config.checkconfig()
2014-01-26 10:39:53 +00:00
puts "#{I18n.t('display.error')}: #{@config.error_msg}"
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
# Request the GPG password and decrypt the file
2013-07-17 20:31:28 +00:00
def decrypt()
2014-01-25 15:53:48 +00:00
if !defined?(@mpw)
2014-01-29 19:49:39 +00:00
@mpw = MPW::MPW.new(@config.file_gpg, @config.key)
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-01-25 15:53:48 +00:00
if !@mpw.decrypt(@passwd)
2014-01-26 10:39:53 +00:00
puts "#{I18n.t('display.error')}: #{@mpw.error_msg}"
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
2013-09-11 18:47:51 +00:00
def display(search, protocol=nil, group=nil, format=nil)
2014-01-11 23:37:46 +00:00
result = @mpw.search(search, group, protocol)
2013-07-16 20:36:41 +00:00
2013-07-17 20:31:28 +00:00
if not result.empty?
result.each do |r|
if format.nil? || !format
2014-01-15 19:01:23 +00:00
displayFormat(r)
else
2014-01-15 19:01:23 +00:00
displayFormatAlt(r)
end
2013-07-17 20:31:28 +00:00
end
else
2014-01-26 10:39:53 +00:00
puts I18n.t('display.nothing')
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
def displayFormat(item)
puts '--------------------'
2014-01-29 19:49:39 +00:00
puts "Id: #{item[MPW::MPW::ID]}"
puts "#{I18n.t('display.name')}: #{item[MPW::MPW::NAME]}"
puts "#{I18n.t('display.group')}: #{item[MPW::MPW::GROUP]}"
puts "#{I18n.t('display.server')}: #{item[MPW::MPW::SERVER]}"
puts "#{I18n.t('display.protocol')}: #{item[MPW::MPW::PROTOCOL]}"
puts "#{I18n.t('display.login')}: #{item[MPW::MPW::LOGIN]}"
puts "#{I18n.t('display.password')}: #{item[MPW::MPW::PASSWORD]}"
puts "#{I18n.t('display.port')}: #{item[MPW::MPW::PORT]}"
puts "#{I18n.t('display.comment')}: #{item[MPW::MPW::COMMENT]}"
2013-09-02 17:14:33 +00:00
end
# Display an item in the alternative format
# @args: item -> an array with the item information
def displayFormatAlt(item)
2014-01-29 19:49:39 +00:00
port = item[MPW::MPW::PORT].nil? ? '' : ":#{item[MPW::MPW::PORT]}"
2014-01-29 19:49:39 +00:00
if item[MPW::MPW::PASSWORD].nil? || item[MPW::MPW::PASSWORD].empty?
if item[MPW::MPW::LOGIN].include('@')
puts "# #{item[MPW::MPW::ID]} #{item[MPW::MPW::PROTOCOL]}://#{item[MPW::MPW::LOGIN]}@#{item[MPW::MPW::SERVER]}#{port}"
else
2014-01-29 19:49:39 +00:00
puts "# #{item[MPW::MPW::ID]} #{item[MPW::MPW::PROTOCOL]}://{#{item[MPW::MPW::LOGIN]}}@#{item[MPW::MPW::SERVER]}#{port}"
end
else
2014-01-29 19:49:39 +00:00
puts "# #{item[MPW::MPW::ID]} #{item[MPW::MPW::PROTOCOL]}://{#{item[MPW::MPW::LOGIN]}:#{item[MPW::MPW::PASSWORD]}}@#{item[MPW::MPW::SERVER]}#{port}"
end
end
2013-08-25 08:07:39 +00:00
# Form to add a new item
2013-07-17 20:31:28 +00:00
def add()
row = Array.new()
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-01-11 23:37:46 +00:00
if @mpw.encrypt()
2014-01-23 22:04:13 +00:00
sync()
2014-01-26 10:39:53 +00:00
puts I18n.t('form.add.valid')
else
2014-01-26 10:39:53 +00:00
puts "#{I18n.t('display.error')}: #{@mpw.error_msg}"
end
2013-07-17 20:31:28 +00:00
else
2014-01-26 10:39:53 +00:00
puts "#{I18n.t('display.error')}: #{@mpw.error_msg}"
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 '--------------------'
2014-01-29 19:49:39 +00:00
name = ask(I18n.t('form.update.name' , :name => row[MPW::MPW::NAME])).to_s
group = ask(I18n.t('form.update.group' , :group => row[MPW::MPW::GROUP])).to_s
server = ask(I18n.t('form.update.server' , :server => row[MPW::MPW::SERVER])).to_s
protocol = ask(I18n.t('form.update.protocol', :protocol => row[MPW::MPW::PROTOCOL])).to_s
login = ask(I18n.t('form.update.login' , :login => row[MPW::MPW::LOGIN])).to_s
2014-01-26 10:39:53 +00:00
passwd = ask(I18n.t('form.update.password')).to_s
2014-01-29 19:49:39 +00:00
port = ask(I18n.t('form.update.port' , :port => row[MPW::MPW::PORT])).to_s
comment = ask(I18n.t('form.update.comment' , :comment => row[MPW::MPW::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-01-11 23:37:46 +00:00
if @mpw.encrypt()
2014-01-23 22:04:13 +00:00
sync()
2014-01-26 10:39:53 +00:00
puts I18n.t('form.update.valid')
2013-07-17 20:31:28 +00:00
else
2014-01-26 10:39:53 +00:00
puts "#{I18n.t('display.error')}: #{@mpw.error_msg}"
2013-07-17 20:31:28 +00:00
end
else
2014-01-26 10:39:53 +00:00
puts "#{I18n.t('display.error')}: #{@mpw.error_msg}"
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
2014-01-15 19:01:23 +00:00
displayFormat(result)
2013-08-26 19:23:35 +00:00
2014-01-26 10:39:53 +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)
if @mpw.encrypt()
2014-01-23 22:04:13 +00:00
sync()
2014-01-26 10:39:53 +00:00
puts I18n.t('form.delete.valid', :id => id)
else
2014-01-26 10:39:53 +00:00
puts "#{I18n.t('display.error')}: #{@mpw.error_msg}"
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
2013-07-25 17:51:43 +00:00
def export(file)
2014-01-11 23:37:46 +00:00
if @mpw.export(file)
2013-07-25 17:51:43 +00:00
puts "The export in #{file} is succesfull!"
else
2014-01-26 10:39:53 +00:00
puts "#{I18n.t('display.error')}: #{@mpw.error_msg}"
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
def import(file, force=false)
2014-01-26 14:09:48 +00:00
result = @mpw.import_preview(file)
2013-08-26 20:19:37 +00:00
if not force
if result.is_a?(Array) && !result.empty?
result.each do |r|
2014-01-15 19:01:23 +00:00
displayFormat(r)
2013-08-26 20:19:37 +00:00
end
2014-01-26 10:39:53 +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-01-11 23:37:46 +00:00
if @mpw.import(file) && @mpw.encrypt()
2014-01-23 22:04:13 +00:00
sync()
2014-01-26 10:39:53 +00:00
puts I18n.t('form.import.valid')
2013-07-25 17:51:43 +00:00
else
2014-01-26 10:39:53 +00:00
puts "#{I18n.t('display.error')}: #{@mpw.error_msg}"
2013-07-25 17:51:43 +00:00
end
end
end
2013-09-08 15:56:10 +00:00
# Interactive mode
def interactive()
2013-09-09 19:48:42 +00:00
group = nil
2013-09-08 18:04:31 +00:00
last_access = Time.now.to_i
2013-12-25 17:51:41 +00:00
while buf = Readline.readline('<mpw> ', true)
2013-12-20 16:38:58 +00:00
2014-01-11 23:37:46 +00:00
if @config.timeout_pwd < Time.now.to_i - last_access
2014-01-26 10:39:53 +00:00
passwd_confirm = ask(I18n.t('interactive.ask_password')) {|q| q.echo = false}
2013-09-08 18:04:31 +00:00
if @passwd.eql?(passwd_confirm)
last_access = Time.now.to_i
else
2014-01-26 10:39:53 +00:00
puts I18n.t('interactive.bad_password')
2013-09-08 18:04:31 +00:00
next
end
else
last_access = Time.now.to_i
end
2013-12-20 16:38:58 +00:00
command = buf.split(' ')
2013-09-08 15:56:10 +00:00
case command[0]
when 'display', 'show', 'd', 's'
if !command[1].nil? && !command[1].empty?
2014-01-15 19:01:23 +00:00
display(command[1], group, command[2])
2013-09-08 15:56:10 +00:00
end
when 'add', 'a'
2013-09-08 18:04:31 +00:00
add()
2013-09-08 15:56:10 +00:00
when 'update', 'u'
if !command[1].nil? && !command[1].empty?
2014-01-15 19:01:23 +00:00
update(command[1])
2013-09-08 15:56:10 +00:00
end
when 'remove', 'delete', 'r', 'd'
if !command[1].nil? && !command[1].empty?
2014-01-15 19:01:23 +00:00
remove(command[1])
2013-09-08 15:56:10 +00:00
end
2013-09-09 19:48:42 +00:00
when 'group', 'g'
if !command[1].nil? && !command[1].empty?
group = command[1]
else
group = nil
end
2013-09-08 15:56:10 +00:00
when 'help', 'h', '?'
2014-01-26 10:39:53 +00:00
puts I18n.t('interactive.option.title')
puts '--------------------'
2014-01-26 10:39:53 +00:00
puts "display, show, d, s SEARCH #{I18n.t('interactive.option.show')}"
puts "group, g #{I18n.t('interactive.option.group')}"
puts "add, a #{I18n.t('interactive.option.add')}"
puts "update, u ID #{I18n.t('interactive.option.update')}"
puts "remove, delete, r, d ID #{I18n.t('interactive.option.remove')}"
puts "help, h, ? #{I18n.t('interactive.option.help')}"
puts "quit, exit, q #{I18n.t('interactive.option.quit')}"
2013-09-08 15:56:10 +00:00
when 'quit', 'exit', 'q'
2014-01-26 10:39:53 +00:00
puts I18n.t('interactive.goodbye')
2013-09-08 15:56:10 +00:00
break
2013-09-09 19:48:42 +00:00
else
2013-09-09 20:45:14 +00:00
if !command[0].nil? && !command[0].empty?
2014-01-26 10:39:53 +00:00
puts I18n.t('interactive.unknown_command')
2013-09-09 19:48:42 +00:00
end
2013-09-08 15:56:10 +00:00
end
end
end
2013-07-16 20:36:41 +00:00
end