#!/usr/bin/env ruby require 'rafini' using Rafini::Exception using Rafini::Array begin nogui, hlp, vrs = ARGV.any?('--no-gui'), ARGV.any?('-h', '--help'), ARGV.any?('-v', '--version') if nogui or hlp or vrs # Going to proceed on need only basis, which may break some conventions... # Get the version. require 'gtk2passwordapp/version' mod = Gtk2passwordapp version = mod::VERSION if vrs puts version exit end vbs = ARGV.any?('-V', '--verbose') # Get the config. require 'xdg' require 'gtk2passwordapp/config' config = mod::CONFIG if hlp and !vbs # Note that this will be the original help without any of the user's edit. puts config[:Help] exit end # Where are the user's data? require 'user_space' require 'yaml' UserSpace::OPTIONS[:parser] = YAML UserSpace::OPTIONS[:ext] = 'yml' appdir = mod::APPDIR appname = File.join 'gtk3app', mod.name.downcase user_space = UserSpace.new(appname: appname, appdir: appdir) user_space.install unless user_space.version == version user_space.configures(config) if hlp # and vbs # Presumably, the user may have edited the help and is requesting this version. puts config[:Help] exit end # It's possible that the password file it not there. pwd_file = config[:PwdFile] raise "Could not find passwords data file." unless File.exist? pwd_file # Going to get and verify the password. require 'io/console' # Standard library pwd0 = nil print 'Password: ' pwd = $stdin.noecho(&:gets).strip while pwd != pwd0 exit if pwd == '' pwd0 = pwd puts print 'Again: ' pwd = $stdin.noecho(&:gets).strip end puts # Ready to read the passwords file. require 'yaml_zlib_blowfish' require 'gtk2passwordapp/account' require 'gtk2passwordapp/accounts' accounts = Gtk2passwordapp::Accounts.new(pwd_file, pwd) accounts.load # Does the user want a data dump? if ARGV.include? '--dump' puts # Remember that ARGV is at least ['--no-gui']. pattern = (ARGV.last[0]=='-')? nil : Regexp.new(ARGV.last) now = Time.now.to_i accounts.names.each do |name| next if pattern and not pattern=~name account = accounts.get(name) puts [account.name, account.username, account.password].join("\n\t") if vbs puts "\t" + account.url puts "\t" + account.note using Rafini::Odometers puts "\tUpdated " + (now - account.updated).sec2time.to_s + ' ago.' end puts end exit end # Looks like the user just wants a particular password. # Get the account name. unless name = (ARGV.last[0]=='-')? nil : ARGV.last print 'Account Name: ' name = $stdin.gets.strip end # Does the given account exist? raise "Could not find account \"#{name}\"." unless accounts.include?(name) # Put the password! account = accounts.get(name) password = account.password puts password # And we're done! exit end rescue StandardError $!.puts exit 1 end # Run gui! require 'gtk3app' ARGV.unshift 'gtk2passwordapp' # going to pretend to be gtk3app Gtk3App.main