module Ssbx::Cli

Public Class Methods

add_users(config) click to toggle source
# File lib/ssbx/cli/user_task.rb, line 29
def self.add_users(config)
    f = Ssbx::File.new
    bx = Ssbx::Box.new(f)
    p = get_password(config)
    
    data = Ssbx::Util.read(config['file']) do |io|
        bx.read(io, config['user'], p)
    end

    config['add_users'].each do |u|
        u, p = u[0], u[1] || u[0]
        
        Ssbx::Util.write(config['file']) do |io|
            bx.write(io, u, p, data)
        end 
    end
end
change_password(config) click to toggle source
# File lib/ssbx/cli/change_password_task.rb, line 9
def self.change_password(config)
    STDIN.echo = false
    begin
        print("Old password: ")
        old_pw = STDIN.readline.chomp
        print("\nNew password: ")
        new_pw1 = STDIN.readline.chomp
        print("\nRetype new password: ")
        new_pw2 = STDIN.readline.chomp
        puts ''

        if new_pw1 != new_pw2
            puts "Old and new passwords do not match. Taking no action."
        else
            f = Ssbx::File.new
            bx = Ssbx::Box.new(f)

            # Decrypt with the old key.
            data = Ssbx::Util.read(config['file']) { |io| bx.read(io, config['user'], old_pw) }

            Ssbx::Util.write(config['file']) { |io| bx.write(io, config['user'], new_pw1, data) }

        end
    ensure
        STDIN.echo = true
    end
end
config(config) click to toggle source
# File lib/ssbx/cli/config_task.rb, line 16
def self.config(config)
    print "User name? [#{ENV['USER']}]: "
    config['user'] = STDIN.readline.chomp
    config['user'] = ENV['USER'] if config['user'] == ''

    require 'io/console'
    STDIN.echo = false
    print "Password: "
    p = begin
        STDIN.readline.chomp
    ensure
        STDIN.echo = true
        puts ''
    end
    config['pass'] = p unless p == ''

    print "Editor? [#{ENV['EDITOR']}]: "
    e = STDIN.readline.chomp
    config['editor'] = e unless e == ''

    require 'yaml'
    ::File.open(@@default_config_file, 'wb') do |io|
        io.write(YAML::dump(
            config.select do |k,v|
                k !~ /^delete_users|add_users|config|verbose$/
            end
        ))
    end
end
delete_users(config) click to toggle source
# File lib/ssbx/cli/user_task.rb, line 20
def self.delete_users(config)
    config['delete_users'].each do |u|
        f = Ssbx::Util.read(config['file']) { |io| Ssbx::File.new(io) }
        bx = Ssbx::Box.new(f)
        bx.remove_user(u)
        Ssbx::Util.write(config['file']) { |io| f.write(io) }
    end
end
edit(config) click to toggle source
# File lib/ssbx/cli/edit_task.rb, line 29
def self.edit(config)
    f = Ssbx::File.new
    bx = Ssbx::Box.new(f)
    p = get_password(config)

    data = if ::File.file? config['file']
        Ssbx::Util.read(config['file']) do |io|
            bx.read(io, config['user'], p)
        end
    else
        ''
    end

    tmpfile = Tempfile.new("ssbx")
    tmpfile.write(data)
    tmpfile.flush

    begin
        editor = config['editor'] || ENV['EDITOR']

        system("#{editor} #{tmpfile.path}")

        tmpfile.rewind
        data = ::File.read(tmpfile)
        ::File.open(config['file'], 'wb') do |io|
            bx.write(io, config['user'], p, data)
        end
    ensure
        tmpfile.unlink
    end        
end
get(config) click to toggle source
# File lib/ssbx/cli/edit_task.rb, line 19
def self.get(config)
    f = Ssbx::File.new
    bx = Ssbx::Box.new(f)
    data = Ssbx::Util.read(config['file']) do |io|
        bx.read(io, config['user'], get_password(config))
    end

    Ssbx::Util.write(config['out']) { |io| io.write(data) }
end
get_password(config) click to toggle source
# File lib/ssbx/cli/get_password_task.rb, line 5
def self.get_password(config)
    if config['pass'] == 'prompt'
        print "Enter your password: "
        STDIN.echo = false
        begin
            config['pass'] = STDIN.readline.chomp
            puts ''
        ensure
            STDIN.echo = true
        end
    end

    config['pass']
end
list_users(config) click to toggle source
# File lib/ssbx/cli/user_task.rb, line 7
def self.list_users(config)
    f = Ssbx::File.new
    bx = Ssbx::Box.new(f)

    Ssbx::Util.read(config['file']) do |io|
        f.read(io)
    end

    bx.list.each do |u|
        puts u
    end           
end
load_default(config) click to toggle source
# File lib/ssbx/cli/config_task.rb, line 9
def self.load_default(config)
    if ::File.file? @@default_config_file
        c = YAML::load(::File.read(@@default_config_file))
        config.merge!(c)
    end
end
set(config) click to toggle source
# File lib/ssbx/cli/edit_task.rb, line 8
def self.set(config)
    f = Ssbx::File.new
    bx = Ssbx::Box.new(f)
    data = Ssbx::Util.read(config['set']) do |io|
        io.read
    end
    Ssbx::Util.write(config['file']) do |io|
        bx.write(io, config['user'], get_password(config), data)
    end 
end