class MailDaemon::Imap::Connection

Constants

SECONDS_IN_MINUTES

Public Class Methods

new(options, &block) click to toggle source
# File lib/mail_daemon/imap/connection.rb, line 11
def initialize(options, &block)
  setup_options(options)


  required_option [:host, :username, :password]

  default_option :port, 143
  default_option :folder, "inbox"
  default_option :ssl, false
  default_option :start_tls, false
  default_option :ssl_options, false
  default_option :sleep_time, 30
  default_option :imap_timeout, 60
  default_option :idle_recycle_time, 29 * SECONDS_IN_MINUTES


  @notify_status_block = block

  puts "initializing new imap connection for #{@options[:username]}:#{@options[:password]}@#{@options[:host]}:#{@options[:port]} " if @options[:debug]

  set_status(INITIALIZING)

end

Public Instance Methods

disconnect() click to toggle source
# File lib/mail_daemon/imap/connection.rb, line 140
def disconnect
  puts "disconnecting" if @options[:debug]
  @idle_required = false
  logout unless @imap.disconnected?

  set_status(DISCONNECTING)
  @imap.disconnect
  set_status(DISCONNECTED)
rescue  
  set_status(DISCONNECTED)
end
login() click to toggle source
# File lib/mail_daemon/imap/connection.rb, line 35
def login
  set_status(CONNECTING)
  puts "Creating a new imap object" if @options[:debug]
  puts "SSL Options;" if @options[:debug]
  puts @options[:ssl_options] if @options[:debug]
  puts "" if @options[:debug]
  @imap = Net::IMAP.new(@options[:host], :port => @options[:port], :ssl => @options[:ssl_options])
  capabilities = @imap.capability
  puts "Imap server capabilities are as follows:" if @options[:debug]
  puts capabilities if @options[:debug]
  puts "" if @options[:debug]
  @idle_available = capabilities.include?("IDLE")

  if @idle_available
    puts "Idle is available" if @options[:debug]
  else
    puts "Idle is not available" if @options[:debug]
  end

  set_status(LOGGING_ON)

  puts "Logging in..." if @options[:debug]
  @imap.login(@options[:username], @options[:password])

  @options[:folder] = "INBOX"
  puts "Selecting folder '#{@options[:folder]}'" if @options[:debug]
  @imap.select(@options[:folder])

  @imap.starttls({}, verify=false) if @options[:start_tls]

  @idle_required = true
  puts "Logged on." if @options[:debug]
  set_status(LOGGED_ON)
end
logout() click to toggle source
# File lib/mail_daemon/imap/connection.rb, line 128
def logout
  puts "Logging out" if @options[:debug]
  begin
    @imap.idle_done if @idleing
  rescue => e
    puts e.message
  end
  set_status(LOGGING_OFF)
  @imap.logout unless @imap.disconnected?
  set_status(LOGGED_OFF)
end
running?() click to toggle source
# File lib/mail_daemon/imap/connection.rb, line 156
def running?
  @imap && !@imap.disconnected?
end
status() click to toggle source
# File lib/mail_daemon/imap/connection.rb, line 152
def status
  {:status => @status}
end
wait_for_messages() { |fetch_message(message_id)| ... } click to toggle source
# File lib/mail_daemon/imap/connection.rb, line 70
def wait_for_messages(&block)
  puts "Fetching awaiting messages" if @options[:debug]
  fetch_message_ids.each do |message_id|
    yield fetch_message(message_id)
  end

  @watcher_block = block

  if @idle_available
    while(@idle_required)
      puts "Starting Idle thread..." if @options[:debug]
      puts "Will recycle in #{@options[:idle_recycle_time]} seconds" if @options[:debug]
      recycle_thread = Thread.new(Time.now + @options[:idle_recycle_time]) do |end_time|
        begin
          while Time.now < end_time
            sleep 1
          end
          puts "Recycling idle thread" if @options[:debug]
          @imap.idle_done
        rescue => e
          puts e.message
          puts e.backtrace.join("\n")
        end
      end

      @idleing = true
      @imap.idle do |message|
        begin
          if message.members.include?(:data) && !message[:data].kind_of?(Fixnum) && message[:data].members.include?(:text) && message[:data][:text] == "idling"
            set_status(IDLEING)
          else
            @imap.idle_done
          end
        rescue => e
          puts e.message
          puts e.backtrace.join("\n")
        end
      end
      recycle_thread.kill
      @idleing = false
      puts "Fetching messages" if @options[:debug]
      fetch_message_ids.each do |message_id|
        yield fetch_message(message_id)
      end
    end
  else
    puts "We're in polling mode..." if @options[:debug]
    set_status(POLLING)
    while(@idle_required)
      puts "Checking for messages" if @options[:debug]
      fetch_message_ids.each do |message_id|
        yield fetch_message(message_id)
      end
      sleep @options[:sleep_time]
    end
  end
end

Private Instance Methods

fetch_message(id) click to toggle source
# File lib/mail_daemon/imap/connection.rb, line 180
def fetch_message(id)
  envelope = @imap.uid_fetch(id, ["RFC822"])
  # TODO: maybe move this to after sent to queue
  # @imap.store(message_id, "-FLAGS", [:Seen])
  envelope[0][:attr]["RFC822"]
end
fetch_message_ids() click to toggle source
# File lib/mail_daemon/imap/connection.rb, line 170
def fetch_message_ids
  messages = []
  @options[:search_command] = "UNSEEN"

  # ap @imap.status("inbox", [@options[:search_command]])
  @imap.uid_search(@options[:search_command])
  # begin

end
send_notification(message) click to toggle source
# File lib/mail_daemon/imap/connection.rb, line 161
def send_notification(message)
  @notify_status_block.call :mailbox => @options, :type => "status_update", :status => message
end
set_status(status) click to toggle source
# File lib/mail_daemon/imap/connection.rb, line 165
def set_status(status)
  @status = status
  send_notification status
end