Skip to content

Instantly share code, notes, and snippets.

@skrat
Created October 22, 2013 09:43
Show Gist options
  • Save skrat/7097875 to your computer and use it in GitHub Desktop.
Save skrat/7097875 to your computer and use it in GitHub Desktop.
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