class MBidle::Account

Attributes

cert[RW]
folder[RW]
host[RW]
imaps[RW]
imaps?[RW]
name[RW]
pass[RW]
path[RW]
port[RW]
user[RW]

Public Class Methods

new(name) click to toggle source
# File lib/mbidle/account.rb, line 6
def initialize(name)
  @name = name
  @imaps = false
  @port = 993
end

Public Instance Methods

handle_error(error) click to toggle source
# File lib/mbidle/account.rb, line 65
def handle_error(error)
  case "#{error}"
  when /unbound/
    unless SHUTDOWN
      Log.warn "#{host} connection unbound for #{name}. Waiting 5 minutes for reconnect ..."
      EM::Timer.new(300, &method(:idle!))
    end
  else
    Log.error "Some unidentified error: #{error.inspect}"
    Log.error error.backtrace
  end
end
idle!() click to toggle source
# File lib/mbidle/account.rb, line 31
def idle!
  Log.debug "Connect to #{host}:#{port}#{imaps? ? ' via IMAPS' : ''} for #{name}"
  Sync.schedule(self)

  @client = EM::IMAP.new(host, port, imaps?)
  @client
    .connect
    .bind!(&method(:login))
    .bind!(&method(:select_folder))
    .bind!(&method(:wait_for_new_emails))
    .errback(&method(:handle_error))
rescue StandardError => e
  unless SHUTDOWN
    Log.warn "#{e} for #{name}. Waiting 5 minutes for reconnect ..."
    EM::Timer.new(300, &method(:idle!))
  end
end
login(_results) click to toggle source
# File lib/mbidle/account.rb, line 49
def login(_results)
  Log.debug "Login as #{user}@#{host} for #{name}"
  @client.login(user, pass)
end
matches?(given_path) click to toggle source
# File lib/mbidle/account.rb, line 27
def matches?(given_path)
  !(/^#{path}/ !~ given_path)
end
read_config_line(line) click to toggle source
# File lib/mbidle/account.rb, line 12
def read_config_line(line)
  match = /^(?<key>[^ ]+) (?<value>.+)$/.match(line)
  case match[:key]
  when 'Host' then @host = match[:value]
  when 'User' then @user = match[:value]
  when 'Pass' then @pass = match[:value]
  when 'Path' then @path = match[:value]
  when 'Inbox' then @folder = File.basename(match[:value])
  when 'CertificateFile' then @cert = match[:value]
  when 'UseIMAPS' then @imaps = (match[:value] == 'yes')
  when 'SSLType'
    @imaps = %w(IMAPS STARTTLS).include?(match[:value])
  end
end
select_folder(_results) click to toggle source
# File lib/mbidle/account.rb, line 54
def select_folder(_results)
  Log.debug "Select #{folder} for #{name}"
  @client.select(folder)
end
wait_for_new_emails(_results) click to toggle source
# File lib/mbidle/account.rb, line 59
def wait_for_new_emails(_results)
  @client.wait_for_new_emails do
    Sync.schedule(self)
  end
end