Hi Folks,
I’m writing a script to check passwords against email accounts,
so I have written a method:
def tryIMAP (host, usessl, user, pass)
proto = usessl ? "IMAPS" : "IMAP"
begin
imap = Net::IMAP.new(host, usessl ? 993 : 143, usessl)
rescue SystemCallError
raise “#{proto} failed”
rescue SocketError
raise “#{proto} failed”
end
begin
imap.login( user, pass)
imap.logout
‘success’
rescue Net::IMAP::NoResponseError
‘failure’
end
end
I use the method like this…
while proto do
begin
case proto
when 'IMAPS'
status = tryIMAP(server, true, user, iD )
proto = nil
when 'IMAP'
status = tryIMAP(server, false, user, iD )
proto = nil
when 'POP'
status = tryPOP(server, user, iD )
proto = nil
else
proto = nil
end
rescue 'IMAPS failed'
proto = 'IMAP'
rescue 'IMAP failed'
proto = 'POP'
rescue 'POP failed'
servers[host][0] = 'NO'
proto = nil
STDERR.print "failed to connect to #{server}\n"
end
end # while proto
print "logged in for #{target}\n" if status == 'success'
If I try a server which does not support IMAPS I get:
class or module required for rescue clause (TypeError)
I’m at a loss to figure out what is wrong. I’ve googled on the error
message but have not found anything useful.
BTW I’m new to ruby and I’d welcome criticism of the above code. If
there are better ways of doing this I’d love to know.
Cheers, Russell