1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-10-27 18:53:19 +00:00
mpw/lib/Cli.rb

317 lines
8.4 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-25 17:51:41 +00:00
def initialize(lang)
2013-07-17 20:31:28 +00:00
@m = MPW.new()
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')}"
2013-07-18 20:16:53 +00:00
puts "# --------------------"
2013-12-25 17:51:41 +00:00
language = ask(I18n.t('cli.form.setup.lang', :lang => lang))
key = ask(I18n.t('cli.form.setup.gpg_key'))
file_gpg = ask(I18n.t('cli.form.setup.gpg_file', :home => Dir.home()))
timeout_pwd = ask(I18n.t('cli.form.setup.timeout'))
2013-07-16 20:36:41 +00:00
2013-12-25 17:51:41 +00:00
if !File.exist?("#{APP_ROOT}/i18n/#{language}.yml")
language= 'en_US'
end
I18n.load_path = Dir["#{APP_ROOT}/i18n/#{language}.yml"]
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
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]}"
2013-12-25 17:51:41 +00:00
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()
2013-12-25 17:51:41 +00:00
puts "# #{I18n.t('cli.form.add.title')}"
2013-07-17 20:31:28 +00:00
puts "# --------------------"
2013-12-25 17:51:41 +00:00
name = ask(I18n.t('cli.form.add.name'))
group = ask(I18n.t('cli.form.add.group'))
server = ask(I18n.t('cli.form.add.server'))
protocol = ask(I18n.t('cli.form.add.protocol'))
login = ask(I18n.t('cli.form.add.login'))
passwd = ask(I18n.t('cli.form.add.password'))
port = ask(I18n.t('cli.form.add.port'))
comment = ask(I18n.t('cli.form.add.comment'))
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?
2013-12-25 17:51:41 +00:00
puts "# #{I18n.t('cli.form.update.title')}"
2013-07-17 20:31:28 +00:00
puts "# --------------------"
2013-12-25 17:51:41 +00:00
name = ask(I18n.t('cli.form.update.name' , :name => row[MPW::NAME]))
group = ask(I18n.t('cli.form.update.group' , :group => row[MPW::GROUP]))
server = ask(I18n.t('cli.form.update.server' , :server => row[MPW::SERVER]))
protocol = ask(I18n.t('cli.form.update.protocol', :protocol => row[MPW::PROTOCOL]))
login = ask(I18n.t('cli.form.update.login' , :login => row[MPW::LOGIN]))
passwd = ask(I18n.t('cli.form.update.password'))
port = ask(I18n.t('cli.form.update.port' , :port => row[MPW::PORT]))
comment = ask(I18n.t('cli.form.update.comment' , :comment => row[MPW::COMMENT]))
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
2013-12-25 17:51:41 +00:00
confirm = ask("#{I18n.t('cli.form.delete.ask', :id => id)} (y/N) ")
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
2013-12-25 17:51:41 +00:00
confirm = ask("#{I18n.t('cli.form.import.ask', :file => file)} (y/N) ")
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 '# Help'
puts '# --------------------'
puts '# Display an item:'
puts '# display SEARCH'
puts '# show SEARCH'
puts '# s SEARCH'
puts '# d SEARCH'
puts '# Add an new item:'
puts '# add'
puts '# a'
puts '# Update an item:'
puts '# update ID'
puts '# u ID'
puts '# Remove an item:'
puts '# remove ID'
puts '# delete ID'
puts '# r ID'
puts '# d ID'
puts '# Quit the program:'
puts '# quit'
puts '# exit'
puts '# q'
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