Class: PollterGeist::IMAPCommands

Inherits:
Object
  • Object
show all
Defined in:
lib/pollter_geist/imap_commands.rb

Instance Method Summary collapse

Constructor Details

#initialize(imap, logger) ⇒ IMAPCommands

Returns a new instance of IMAPCommands.



3
4
5
6
7
8
# File 'lib/pollter_geist/imap_commands.rb', line 3

def initialize imap, logger
  @imap   = imap
  @logger = logger

  @idler = ImapIdler.new(self)
end

Instance Method Details

#expungeObject



41
42
43
44
# File 'lib/pollter_geist/imap_commands.rb', line 41

def expunge
  @logger.debug("expunge, remove all messages with Delete flag")
  @imap.expunge
end

#fetch(uids) ⇒ Object



46
47
48
49
# File 'lib/pollter_geist/imap_commands.rb', line 46

def fetch uids
  logger.debug "fetching: #{uids}"
  @imap.uid_fetch(uids, ['RFC822']).map { |r| r.attr['RFC822'] }
end

#imapObject



70
71
72
# File 'lib/pollter_geist/imap_commands.rb', line 70

def imap
  @imap
end

#loggerObject



66
67
68
# File 'lib/pollter_geist/imap_commands.rb', line 66

def logger
  @logger
end

#message_uidsArray

fetches a list of message UIDs

Returns:

  • (Array)

    of message UIDs



27
28
29
30
31
32
33
34
# File 'lib/pollter_geist/imap_commands.rb', line 27

def message_uids
  @logger.debug('fetching uids')
  # Searching for all messages where TO include @ is a workaround, since the imap#status method
  # does not work for some reason.
  message_ids = @imap.search(['TO', '@'])
  result      = @imap.fetch(message_ids, ['UID'])
  result.map { |r| r.attr['UID'].to_i }
end

#remove(uid) ⇒ Object



36
37
38
39
# File 'lib/pollter_geist/imap_commands.rb', line 36

def remove uid
  @logger.debug("removing #{uid}")
  @imap.uid_store uid, "+FLAGS", [:Deleted]
end

#select_mailbox(mailbox) ⇒ Object



10
11
12
13
# File 'lib/pollter_geist/imap_commands.rb', line 10

def select_mailbox mailbox
  logger.debug("selecting mailbox #{mailbox}")
  @imap.select(mailbox)
end

#wait_for(events, timeout = 29*60) ⇒ Object

Issues IDLE command for the imap server, it takes a list of events to listen for and returns once one of events occured or if there was a timeout.

Parameters:

  • events (events)

    a list of events to listen for, EXPUNGE, RECENT, EXISTS

  • timeout (Numeric) (defaults to: 29*60)

    a timeout, if no event has ocoured for this time

Returns:

  • will return an object which responds to #success?, #timeout? and #event



21
22
23
# File 'lib/pollter_geist/imap_commands.rb', line 21

def wait_for events, timeout = 29*60
  @idler.idle(events, timeout)
end

#wait_for_changes(timeout = 20*60) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pollter_geist/imap_commands.rb', line 51

def wait_for_changes timeout = 20*60
  logger.debug('waiting for changes in inbox')
  change_set = ChangeListener.new
  change_set.tick(message_uids)
  result = wait_for(['RECENT', 'EXPUNGE'], timeout)
  logger.debug('updating current view')
  change_set.tick(message_uids)
  if change_set.changed?
    logger.debug('changes detected!')
    yield(change_set)
  else
    logger.debug('no changes detected')
  end
end