Skip to content
This repository has been archived by the owner on Jan 6, 2018. It is now read-only.

Commit

Permalink
IMAP sort of works now
Browse files Browse the repository at this point in the history
  • Loading branch information
Burke Libbey committed Jan 4, 2011
1 parent 75fb46f commit 698ef7f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Gemfile
@@ -1,4 +1,6 @@
source :gemcutter

gem 'ruby-debug'

# Specify your gem's dependencies in enso.gemspec
gemspec
8 changes: 8 additions & 0 deletions Gemfile.lock
Expand Up @@ -8,9 +8,16 @@ PATH
GEM
remote: http://rubygems.org/
specs:
columnize (0.3.2)
eventmachine (0.12.10)
json (1.4.6)
linecache (0.43)
roauth (0.0.3)
ruby-debug (0.10.4)
columnize (>= 0.1)
ruby-debug-base (~> 0.10.4.0)
ruby-debug-base (0.10.4)
linecache (>= 0.3)
twitter-stream (0.1.10)
eventmachine (>= 0.12.8)
roauth (>= 0.0.2)
Expand All @@ -22,4 +29,5 @@ DEPENDENCIES
bundler (>= 1.0.0)
enso!
json
ruby-debug
twitter-stream (~> 0.1.10)
36 changes: 23 additions & 13 deletions lib/enso/stream/imap.rb
Expand Up @@ -4,27 +4,37 @@ module Stream
class IMAP < Base

def initialize(domain, port, ssl, username, password)
@imap = Net::IMAP.new(domain, port, ssl)
@imap.login(username, password)
@imap.select("INBOX")
@domain = domain
@port = port
@ssl = ssl
@username = username
@password = password

@imap.add_response_handler proc { |resp|
if resp.name == "EXISTS"
fetch_message(resp.data)
end
}
@imap = Net::IMAP.new(@domain, @port, @ssl)
@imap.login(@username, @password)
@imap.select("INBOX")

@top_mid = @imap.search('ALL').last
@top_uid = @imap.fetch(@top_mid, 'UID')[0].attr['UID']
end

def tick_quantum
15
end

def fetch_message(data)
@imap.fetch([data], "(ENVELOPE)")
end

def tick
@imap.noop
mids = []
query = "UID #{@top_uid}:*"
@imap.check
@imap.search(query).each do |message_id|
next if message_id == @top_mid
@top_mid = [@top_mid, message_id].max
mids << message_id.to_i
end
messages = mids.any? ? @imap.fetch(mids, "(UID ENVELOPE)") : []
@top_uid = [@top_uid, messages.map{|m|m.attr['UID']}.max].compact.max

messages.map{|m|format_message(m.attr['ENVELOPE'])}
end

def format_message(msg)
Expand Down

0 comments on commit 698ef7f

Please sign in to comment.