class MailDaemon::Handler

Public Class Methods

new(options) click to toggle source
# File lib/mail_daemon.rb, line 18
def initialize(options)
  setup_options(options)
  default_option :connections, []
  default_option :debug, false

  puts "Setting up Signal Traps" if @options[:debug]
  Signal.trap("INT") {
    Thread.new {self.stop}.join
  }
  # Trap `Kill `
  Signal.trap("TERM") {
    Thread.new {self.stop}.join
  }
end

Public Instance Methods

reload(options) click to toggle source
# File lib/mail_daemon.rb, line 52
def reload(options)
  reload_options(options)
  self.restart
end
restart() click to toggle source
# File lib/mail_daemon.rb, line 33
def restart
  puts "Restarting... " if @options[:debug]
  self.stop if self.running?
  Thread.new do
    puts "Starting in new thread " if @options[:debug]
    self.start(&@client_handler_block)
  end
end
running?() click to toggle source
# File lib/mail_daemon.rb, line 42
def running?
  @watchers && @watchers.detect{|watcher| watcher.running?}
end
start() { |message, type| ... } click to toggle source
# File lib/mail_daemon.rb, line 57
def start(&block)
  @watchers = []

  @client_handler_block = block

  @options[:connections].each do |mailbox|
    mailbox[:ssl_options] = {:verify_mode => OpenSSL::SSL::VERIFY_NONE} if mailbox[:ssl]

    puts "Setting up watcher for #{mailbox[:username]}" if @options[:debug]
    @watchers << MailDaemon::Imap::Watcher.new(@options.merge(mailbox))
  end
  @threads = []
  @watchers.each do |watcher|
    @threads << Thread.new do
      begin
        watcher.start do |message|
          type = message.delete(:type)
          yield message, type
        end
      rescue => e
        puts "#{watcher}: #{e.message}" 
      end
    end
  end

  @threads.map{|t| t.join}

end
stop() click to toggle source

Signal catching

# File lib/mail_daemon.rb, line 47
def stop
  @watchers.map{|watcher| watcher.stop}
  @threads.map{|t| t.kill }
end