Class: IMAPProcessor::IDLE

Inherits:
IMAPProcessor show all
Defined in:
lib/imap_processor/idle.rb

Overview

Example class that supports IDLE on a mailbox and lists messages added or expunged.

Constant Summary

Constants inherited from IMAPProcessor

VERSION

Instance Attribute Summary

Attributes inherited from IMAPProcessor

#imap, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from IMAPProcessor

add_move, #capability, #connect, #create_mailbox, #delete_messages, #each_message, #each_part, #log, #mime_parts, #move_messages, run, #show_messages, #verbose?

Constructor Details

#initialize(options) ⇒ IDLE

Returns a new instance of IDLE.



18
19
20
21
22
23
# File 'lib/imap_processor/idle.rb', line 18

def initialize(options)
  super

  raise IMAPProcessor::Error, 'only one mailbox is supported' if
    @boxes.length > 1
end

Class Method Details

.process_args(args) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/imap_processor/idle.rb', line 10

def self.process_args(args)
  super __FILE__, args do |opts, options|
    opts.banner << <<-EOF
imap_idle lists messages added or expunged from a mailbox
    EOF
  end
end

Instance Method Details

#runObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/imap_processor/idle.rb', line 25

def run
  mailbox = @boxes.first

  connect do |connection|
    raise IMAPProcessor::Error, 'IDLE not supported on this server' unless
      connection.idle?

    imap = connection.imap

    imap.select mailbox
    exists = imap.responses['EXISTS'].first

    log "Starting IDLE"

    imap.idle do |response|
      next unless Net::IMAP::UntaggedResponse === response

      case response.name
      when 'EXPUNGE' then
        puts "Expunged message #{response.data}"
      when 'EXISTS' then
        latest_uid = response.data
        new = latest_uid - exists
        puts "#{new} messages added"

        show_messages_in mailbox, ((exists + 1)..latest_uid)

        exists = response.data
      when 'RECENT' then
        puts "#{response.data} recent messages"
      when 'OK' then # ending IDLE
      else
        log "Unhandled untagged response: #{response.name}"
      end
    end
  end
end

#show_messages_in(mailbox, uids) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/imap_processor/idle.rb', line 63

def show_messages_in(mailbox, uids)
  connect do |connection|
    imap = connection.imap

    imap.select mailbox

    show_messages uids
  end
end