rgrove-larch 0.0.1.1 → 0.0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/larch/imap.rb +33 -27
- metadata +1 -1
data/lib/larch/imap.rb
CHANGED
@@ -352,42 +352,48 @@ class IMAP
|
|
352
352
|
def unsafe_connect
|
353
353
|
info "connecting..."
|
354
354
|
|
355
|
-
|
356
|
-
@imap = Net::IMAP.new(host, port, ssl?)
|
357
|
-
rescue => e
|
358
|
-
error e.message
|
359
|
-
raise
|
360
|
-
end
|
355
|
+
exception = nil
|
361
356
|
|
362
|
-
|
357
|
+
Thread.new do
|
358
|
+
begin
|
359
|
+
@imap = Net::IMAP.new(host, port, ssl?)
|
363
360
|
|
364
|
-
|
365
|
-
tried = []
|
361
|
+
info "connected on port #{port}" << (ssl? ? ' using SSL' : '')
|
366
362
|
|
367
|
-
|
368
|
-
|
369
|
-
end
|
363
|
+
auth_methods = ['PLAIN']
|
364
|
+
tried = []
|
370
365
|
|
371
|
-
|
372
|
-
|
366
|
+
['LOGIN', 'CRAM-MD5'].each do |method|
|
367
|
+
auth_methods << method if @imap.capability.include?("AUTH=#{method}")
|
368
|
+
end
|
373
369
|
|
374
|
-
|
370
|
+
begin
|
371
|
+
tried << method = auth_methods.pop
|
375
372
|
|
376
|
-
|
377
|
-
@imap.login(@username, @password)
|
378
|
-
else
|
379
|
-
@imap.authenticate(method, @username, @password)
|
380
|
-
end
|
373
|
+
debug "authenticating using #{method}"
|
381
374
|
|
382
|
-
|
375
|
+
if method == 'PLAIN'
|
376
|
+
@imap.login(@username, @password)
|
377
|
+
else
|
378
|
+
@imap.authenticate(method, @username, @password)
|
379
|
+
end
|
383
380
|
|
384
|
-
|
385
|
-
debug "#{method} auth failed: #{e.message}"
|
386
|
-
retry unless auth_methods.empty?
|
381
|
+
info "authenticated using #{method}"
|
387
382
|
|
388
|
-
|
389
|
-
|
390
|
-
|
383
|
+
rescue Net::IMAP::BadResponseError, Net::IMAP::NoResponseError => e
|
384
|
+
debug "#{method} auth failed: #{e.message}"
|
385
|
+
retry unless auth_methods.empty?
|
386
|
+
|
387
|
+
raise e, "#{e.message} (tried #{tried.join(', ')})"
|
388
|
+
end
|
389
|
+
|
390
|
+
rescue => e
|
391
|
+
exception = e
|
392
|
+
error e.message
|
393
|
+
end
|
394
|
+
end.join
|
395
|
+
|
396
|
+
raise exception if exception
|
391
397
|
end
|
392
398
|
|
393
399
|
end
|