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

309 lines
8.7 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'
2013-07-18 19:56:45 +00:00
2013-09-04 17:48:06 +00:00
require "#{APP_ROOT}/lib/MPW.rb"
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
def initialize(lang, config_file=nil)
@m = MPW.new(config_file)
2013-07-17 20:31:28 +00:00
if not @m.checkconfig()
2013-12-25 17:51:41 +00:00
self.setup(lang)
2013-07-17 20:31:28 +00:00
end
if not self.decrypt()
2013-12-25 17:51:41 +00:00
puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}"
2013-07-17 20:31:28 +00:00
exit 2
2013-07-16 20:36:41 +00:00
end
end
# Create a new config file
2013-12-25 17:51:41 +00:00
# @args: lang -> the software language
def setup(lang)
puts I18n.t('cli.form.setup.title')
puts '--------------------'
language = ask(I18n.t('cli.form.setup.lang', :lang => lang)).to_s
key = ask(I18n.t('cli.form.setup.gpg_key')).to_s
file_gpg = ask(I18n.t('cli.form.setup.gpg_file', :home => Dir.home())).to_s
timeout_pwd = ask(I18n.t('cli.form.setup.timeout')).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
if @m.setup(key, language, file_gpg, timeout_pwd)
puts I18n.t('cli.form.setup.valid')
2013-07-17 20:31:28 +00:00
else
2013-12-25 17:51:41 +00:00
puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}"
2013-07-16 20:36:41 +00:00
end
if not @m.checkconfig()
puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}"
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()
2013-12-25 17:51:41 +00:00
@passwd = ask(I18n.t('cli.display.gpg_password')) {|q| q.echo = false}
2013-09-08 18:04:31 +00:00
return @m.decrypt(@passwd)
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)
result = @m.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
2013-09-08 18:04:31 +00:00
self.displayFormat(r)
else
2013-09-08 18:04:31 +00:00
self.displayFormatAlt(r)
end
2013-07-17 20:31:28 +00:00
end
else
2013-12-25 17:51:41 +00:00
puts I18n.t('cli.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 '--------------------'
puts "Id: #{item[MPW::ID]}"
puts "#{I18n.t('cli.display.name')}: #{item[MPW::NAME]}"
puts "#{I18n.t('cli.display.group')}: #{item[MPW::GROUP]}"
puts "#{I18n.t('cli.display.server')}: #{item[MPW::SERVER]}"
puts "#{I18n.t('cli.display.protocol')}: #{item[MPW::PROTOCOL]}"
puts "#{I18n.t('cli.display.login')}: #{item[MPW::LOGIN]}"
puts "#{I18n.t('cli.display.password')}: #{item[MPW::PASSWORD]}"
puts "#{I18n.t('cli.display.port')}: #{item[MPW::PORT]}"
puts "#{I18n.t('cli.display.comment')}: #{item[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)
item[MPW::PORT].nil? ? (port = '') : (port = ":#{item[MPW::PORT]}")
if item[MPW::PASSWORD].nil? || item[MPW::PASSWORD].empty?
2013-09-04 17:48:06 +00:00
if item[MPW::LOGIN].include('@')
puts "# #{item[MPW::ID]} #{item[MPW::PROTOCOL]}://#{item[MPW::LOGIN]}@#{item[MPW::SERVER]}#{port}"
else
puts "# #{item[MPW::ID]} #{item[MPW::PROTOCOL]}://{#{item[MPW::LOGIN]}}@#{item[MPW::SERVER]}#{port}"
end
else
puts "# #{item[MPW::ID]} #{item[MPW::PROTOCOL]}://{#{item[MPW::LOGIN]}:#{item[MPW::PASSWORD]}}@#{item[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()
puts I18n.t('cli.form.add.title')
puts '--------------------'
name = ask(I18n.t('cli.form.add.name')).to_s
group = ask(I18n.t('cli.form.add.group')).to_s
server = ask(I18n.t('cli.form.add.server')).to_s
protocol = ask(I18n.t('cli.form.add.protocol')).to_s
login = ask(I18n.t('cli.form.add.login')).to_s
passwd = ask(I18n.t('cli.form.add.password')).to_s
port = ask(I18n.t('cli.form.add.port')).to_s
comment = ask(I18n.t('cli.form.add.comment')).to_s
2013-07-17 20:31:28 +00:00
if @m.add(name, group, server, protocol, login, passwd, port, comment)
if @m.encrypt()
2013-12-25 17:51:41 +00:00
puts I18n.t('cli.form.add.valid')
else
2013-12-25 17:51:41 +00:00
puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}"
end
2013-07-17 20:31:28 +00:00
else
2013-12-25 17:51:41 +00:00
puts "#{I18n.t('cli.display.error')}: #{@m.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)
row = @m.searchById(id)
if not row.empty?
puts I18n.t('cli.form.update.title')
puts '--------------------'
name = ask(I18n.t('cli.form.update.name' , :name => row[MPW::NAME])).to_s
group = ask(I18n.t('cli.form.update.group' , :group => row[MPW::GROUP])).to_s
server = ask(I18n.t('cli.form.update.server' , :server => row[MPW::SERVER])).to_s
protocol = ask(I18n.t('cli.form.update.protocol', :protocol => row[MPW::PROTOCOL])).to_s
login = ask(I18n.t('cli.form.update.login' , :login => row[MPW::LOGIN])).to_s
passwd = ask(I18n.t('cli.form.update.password')).to_s
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
2013-07-17 20:31:28 +00:00
if @m.update(id, name, group, server, protocol, login, passwd, port, comment)
2013-07-17 20:31:28 +00:00
if @m.encrypt()
2013-12-25 17:51:41 +00:00
puts I18n.t('cli.form.update.valid')
2013-07-17 20:31:28 +00:00
else
2013-12-25 17:51:41 +00:00
puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}"
2013-07-17 20:31:28 +00:00
end
else
2013-12-25 17:51:41 +00:00
puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}"
2013-07-16 20:36:41 +00:00
end
2013-07-17 20:31:28 +00:00
else
2013-12-25 17:51:41 +00:00
puts I18n.t('cli.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
2013-08-26 19:23:35 +00:00
result = @m.searchById(id)
if result.length > 0
2013-09-08 18:04:31 +00:00
self.displayFormat(result)
2013-08-26 19:23:35 +00:00
confirm = ask("#{I18n.t('cli.form.delete.ask', :id => id)} (y/N) ").to_s
if confirm =~ /^(y|yes|YES|Yes|Y)$/
force = true
end
else
2013-12-25 17:51:41 +00:00
puts I18n.t('cli.display.nothing')
end
end
if force
if @m.remove(id)
if @m.encrypt()
2013-12-25 17:51:41 +00:00
puts I18n.t('cli.form.delete.valid', :id => id)
else
2013-12-25 17:51:41 +00:00
puts "#{I18n.t('cli.display.error')}: #{@m.error_msg}"
end
2013-07-17 20:31:28 +00:00
else
2013-12-25 17:51:41 +00:00
puts I18n.t('cli.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)
if @m.export(file)
puts "The export in #{file} is succesfull!"
else
2013-12-25 17:51:41 +00:00
puts "#{I18n.t('cli.display.error')}: #{@m.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)
result = @m.importPreview(file)
if not force
if result.is_a?(Array) && !result.empty?
result.each do |r|
2013-09-08 18:04:31 +00:00
self.displayFormat(r)
2013-08-26 20:19:37 +00:00
end
confirm = ask("#{I18n.t('cli.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
2013-12-25 17:51:41 +00:00
puts I18n.t('cli.form.import.not_valid')
2013-08-26 20:19:37 +00:00
end
end
if force
if @m.import(file) && @m.encrypt()
2013-12-25 17:51:41 +00:00
puts I18n.t('cli.form.import.valid')
2013-07-25 17:51:43 +00:00
else
2013-12-25 17:51:41 +00:00
puts "#{I18n.t('cli.display.error')}: #{@m.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
2013-09-08 18:04:31 +00:00
if @m.timeout_pwd < Time.now.to_i - last_access
2013-12-25 17:51:41 +00:00
passwd_confirm = ask(I18n.t('cli.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
2013-12-25 17:51:41 +00:00
puts I18n.t('cli.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?
2013-09-09 19:48:42 +00:00
self.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?
2013-09-08 18:04:31 +00:00
self.update(command[1])
2013-09-08 15:56:10 +00:00
end
when 'remove', 'delete', 'r', 'd'
if !command[1].nil? && !command[1].empty?
2013-09-08 18:04:31 +00:00
self.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', '?'
puts I18n.t('cli.interactive.option.title')
puts '--------------------'
puts "display, show, d, s SEARCH #{I18n.t('cli.interactive.option.show')}"
puts "group, g #{I18n.t('cli.interactive.option.group')}"
puts "add, a #{I18n.t('cli.interactive.option.add')}"
puts "update, u ID #{I18n.t('cli.interactive.option.update')}"
puts "remove, delete, r, d ID #{I18n.t('cli.interactive.option.remove')}"
puts "help, h, ? #{I18n.t('cli.interactive.option.help')}"
puts "quit, exit, q #{I18n.t('cli.interactive.option.quit')}"
2013-09-08 15:56:10 +00:00
when 'quit', 'exit', 'q'
2013-12-25 17:51:41 +00:00
puts I18n.t('cli.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?
2013-12-25 17:51:41 +00:00
puts I18n.t('cli.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