class PollterGeist::IMAPCommands

Public Class Methods

new(imap, logger) click to toggle source
# File lib/pollter_geist/imap_commands.rb, line 3
def initialize imap, logger
  @imap   = imap
  @logger = logger

  @idler = ImapIdler.new(self)
end

Public Instance Methods

expunge() click to toggle source
# 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) click to toggle source
# 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
imap() click to toggle source
# File lib/pollter_geist/imap_commands.rb, line 70
def imap
  @imap
end
logger() click to toggle source
# File lib/pollter_geist/imap_commands.rb, line 66
def logger
  @logger
end
message_uids() click to toggle source

fetches a list of message UIDs @return [Array] of message UIDs

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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.

@param events [events] a list of events to listen for, EXPUNGE, RECENT, EXISTS @param timeout [Numeric] a timeout, if no event has ocoured for this time @return will return an object which responds to success?, timeout? and event

# 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) { |change_set| ... } click to toggle source
# 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