Module: Imp::UI

Defined in:
lib/imp/ui.rb

Overview

Module handling user I/O.

Constant Summary collapse

DEFAULT_FILE =

The default file to save encrypted passwords in.

'~/.imp/default.enc'
HISTFILE =

The file of the history of the prompt.

'~/.imp/hist'
PROMPT =

The string precending user input in the prompt.

'imp> '
TIMEOUT =

The time in seconds, after which the program exits if it recieves no input from the user.

300

Class Method Summary collapse

Class Method Details

.close_fileObject

Closes the tree. This generally only happens right before ruby is about to exit so it isn’t that important but hey.



47
48
49
50
# File 'lib/imp/ui.rb', line 47

def self.close_file
  $tree.close
  $tree = nil
end

.load_fileObject

Loads and decrypts a file. The password is asked for interactively.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/imp/ui.rb', line 28

def self.load_file
  until $tree
    begin
      passwd = ask("Password for file #{$file} (leave blank to cancel): ")\
          do |q|
        q.echo = false
      end
      if passwd == ''
        break
      end
      $tree = EncryptedTree.new(passwd, $file)
    rescue OpenSSL::Cipher::CipherError
      $stderr.puts "Decryption failed. Corrupt file or wrong password."
    end
  end
end

.mainObject

Runs the program.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/imp/ui.rb', line 53

def self.main
  load_options
  if $opts[:file]
    $file = $opts[:file]
  else
    $file = DEFAULT_FILE
  end
  load_file
  # If no password was entered, quit.
  exit unless $tree
  welcome
  init_readline
  begin
    prompt
  ensure
    close_file
  end
end

.timeout(&block) ⇒ Object

Times out execution of a block and exits printing an appropriate message if the block doesn’t time out in time.

The name is due to a conflict with Timeout’s own.



76
77
78
79
80
81
82
83
# File 'lib/imp/ui.rb', line 76

def self.timeout(&block)
  begin
    Timeout::timeout(TIMEOUT, &block)
  rescue Timeout::Error
    $stderr.puts "\nUser input timeout. Closing..."
    exit
  end
end