Ruby实现邮件主动推送触发程序

2019-09-25 09:47:21于丽

require 'net/imap'

# Extend support for idle command. See online.
# http://www.ruby-forum.com/topic/50828
# https://gist.github.com/jem/2783772
# but that was wrong. see /opt/ruby-1.9.1-p243/lib/net/imap.rb.
class Net::IMAP
  def idle
    cmd = "IDLE"
    synchronize do
      @idle_tag = generate_tag
      put_string(@idle_tag + " " + cmd)
      put_string(CRLF)
    end
  end

  def say_done
    cmd = "DONE"
    synchronize do
      put_string(cmd)
      put_string(CRLF)
    end
  end

  def await_done_confirmation
    synchronize do
      get_tagged_response(@idle_tag, nil)
      puts 'just got confirmation'
    end
  end
end

class Remailer
  attr_reader :imap

  public
  def initialize
    @imap = nil
    @mailer = nil
    start_imap
  end

  def tidy
    stop_imap
  end

  def print_pust
       envelope = @imap.fetch(-1, "ENVELOPE")[0].attr["ENVELOPE"]
       puts "From:#{envelope.from[0].name}t Subject: #{envelope.subject}"
  end

  def bounce_idle
    # Bounces the idle command.
    @imap.say_done
    @imap.await_done_confirmation
    # Do a manual check, just in case things aren't working properly.
    @imap.idle
  end

  private
  def start_imap
    @imap = Net::IMAP.new('pop.i-click.com')
    @imap.login USERNAME, PW
    @imap.select 'INBOX'

    # Add handler.
    @imap.add_response_handler do |resp|
      if resp.kind_of?(Net::IMAP::UntaggedResponse) and resp.name == "EXISTS"
        @imap.say_done
        Thread.new do
          @imap.await_done_confirmation
          print_pust
          @imap.idle
        end
      end
    end
    @imap.idle
  end

  def stop_imap
    @imap.done
  end

end