On 12/20/06, Simon Strandgaard <neoneye / gmail.com> wrote:
> On 12/20/06, Simon Strandgaard <neoneye / gmail.com> wrote:
> [snip]
> > I want to listen for:
> > 'Enter passphrase: '
> > 'Repeat passphrase: '
> > eof?
> [snip]
>
>
> Ok I got a partial solution.. now it would be nice if I could access
> the string accumlated in readlines when this thing timeouts.
[snip]

Now its working.. thanks ruby-talk :-)


require 'expect'
require 'timeout'
require 'pty'

def gnupg_encrypt(filename, passphrase)
  msg = ""
  PTY.spawn("gpg -c #{filename}") do |r,w,pid|
    w.sync = true
    r.expect("Enter passphrase: ", 1) do |e|
      raise "Enter passphrase" unless e
      w.puts passphrase
    end
    r.expect("Repeat passphrase: ", 1) do |e|
      raise "Repeat passphrase" unless e
      w.puts passphrase
    end
    timeout(1) do
      msg += r.getc.chr until r.eof
    end
  end
  if msg =~ /\S/
    p msg
    return -1
  end
  puts "OK"
  return 0
rescue Timeout::Error => e
  p e, msg
  return -2
rescue => e
  p e
  return -3
end


if $0 == __FILE__
  File.open("test", "a+") do |f|
    500000.times { f.write("helloworld") }
  end

  gnupg_encrypt("test", "secretpassword")
  system('ls test*')
end





-- 
Simon Strandgaard
http://opcoders.com/