Created
October 22, 2013 09:43
-
-
Save skrat/7097875 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/imap' | |
def openNewConnection() | |
imap = Net::IMAP.new('imap.gmail.com', 993, :ssl => true) | |
imap.login('[email protected]', 'heymama') | |
imap.select('INBOX') | |
return imap | |
end | |
def checkForUnread(imap) | |
status = imap.status('INBOX', ['UNSEEN']) | |
numUnread = status['UNSEEN'] | |
puts "#{numUnread} new messages" | |
end | |
imap = openNewConnection() | |
checkForUnread(imap) | |
puts 'IMAP IDLE start...' | |
imap.idle do |resp| | |
begin | |
if resp.kind_of?(Net::IMAP::UntaggedResponse) and ['EXISTS', 'EXPUNGE', 'FETCH'].include? resp.name | |
imap2 = openNewConnection() | |
checkForUnread(imap2) | |
imap2.logout | |
end | |
rescue Exception => e | |
puts 'Ending IDLE due to an unhandled exception:' | |
puts e | |
imap.idle_done | |
end | |
end | |
puts 'IMAP IDLE done.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment