class MailDaemon::ImapWatcher

Public Class Methods

new(options) click to toggle source
# File lib/mail_daemon/imap_watcher.rb, line 5
def initialize(options)
  @options = options
  @watchers = []

  raise "REDIS_URL environment variable is required (eg redis://localhost:6739)" unless ENV["REDIS_URL"]
  raise "MYSQL_HOST environment variable is required" unless ENV["MYSQL_HOST"]
  raise "MYSQL_DATABASE environment variable is required" unless ENV["MYSQL_DATABASE"]
  raise "MYSQL_USERNAME environment variable is required" unless ENV["MYSQL_USERNAME"]
  raise "MYSQL_PASSWORD environment variable is required" unless ENV["MYSQL_PASSWORD"]

  ENV["MYSQL_PASSWORD"] = "" unless ENV["MYSQL_PASSWORD"]
  ENV["MYSQL_PORT"] = "3306"

  redis_url = URI.parse(ENV["REDIS_URL"])

  $redis = Redis.new(:host => redis_url.host, :port => redis_url.port)

  Signal.trap("INT") {
    Thread.new {self.stop}.join
  }

  # Trap `Kill `
  Signal.trap("TERM") {
    Thread.new {self.stop}.join
  }

  restart
end

Public Instance Methods

restart() click to toggle source
# File lib/mail_daemon/imap_watcher.rb, line 67
def restart
  stop
  setup_watchers
  start
end
running?() click to toggle source
# File lib/mail_daemon/imap_watcher.rb, line 73
def running?
  !!@watchers.detect{|watcher| watcher.running? }
end
setup_watchers() click to toggle source
# File lib/mail_daemon/imap_watcher.rb, line 34
def setup_watchers
  # load uptodate config

  watchers = []
  mysql_client do |mysql|
    statement = mysql.prepare("select ea.*, a.nickname from case_blocks_email_accounts ea join case_blocks_accounts a on a.id = ea.account_id where ea.imap_enabled=1")
    result = statement.execute()
    result.each do |row|
      ssl = row["imap_ssl"]==1 ? {:verify_mode => "none"} : false
      decrypted_password = Encryption.new.decrypt(row["imap_encrypted_password"])
      begin
        watcher = Imap::Connection.new(@options.merge({:email => row["imap_username"], :host => row["imap_host"], :port => row["imap_port"], :password => decrypted_password, :ssl => ssl, :start_tls => row["imap_start_tls"]==1, :name => row["imap_folder_name"], :search_command => row["imap_search_command"], :message_count => row["imap_messages_processed"], :last_delivered_at => row["imap_last_delivered_at"], :delivery_method=>"sidekiq", :delivery_options=>{:redis_url => ENV["REDIS_URL"], :queue => "email_handler", :worker=>"EmailHandlerWorker"}}))
        watchers << watcher
      rescue => e
        puts "Unable to create IMAP4 connection for #{row['nickname']}/#{row['name']}. #{e.message}"
      end
    end
  end

end
start() click to toggle source
# File lib/mail_daemon/imap_watcher.rb, line 55
def start
  watchers.each do |watcher|
    watcher.watch do |message|
      ap message
    end
  end
end
stop() click to toggle source
# File lib/mail_daemon/imap_watcher.rb, line 63
def stop
  watchers.map{|watcher| watcher.stop! }
end

Private Instance Methods

mysql_client() { |client| ... } click to toggle source
# File lib/mail_daemon/imap_watcher.rb, line 78
def mysql_client(&block)
  client = Mysql2::Client.new(:host => ENV["MYSQL_HOST"], :port => ENV["MYSQL_PORT"], :username => ENV["MYSQL_USERNAME"], :password => ENV["MYSQL_PASSWORD"], :database => ENV["MYSQL_DATABASE"])
  yield client
  client.close
end