Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

IMAP sort of works now

  • Loading branch information...
commit 698ef7f3302ac3e0c6f5b71c4ab9acf3c7b8974b 1 parent 75fb46f
Burke Libbey authored
Showing with 33 additions and 13 deletions.
  1. +2 −0  Gemfile
  2. +8 −0 Gemfile.lock
  3. +23 −13 lib/enso/stream/imap.rb
View
2  Gemfile
@@ -1,4 +1,6 @@
source :gemcutter
+gem 'ruby-debug'
+
# Specify your gem's dependencies in enso.gemspec
gemspec
View
8 Gemfile.lock
@@ -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)
@@ -22,4 +29,5 @@ DEPENDENCIES
bundler (>= 1.0.0)
enso!
json
+ ruby-debug
twitter-stream (~> 0.1.10)
View
36 lib/enso/stream/imap.rb
@@ -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)
Please sign in to comment.
Something went wrong with that request. Please try again.