Sha256: f15a4e50a2969fea25f15b463be68c70b14307cc4de5e67b0b35e686c5813f9c

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

require 'eventmachine'
require 'ostruct'
require 'mail'

class SMTP < EventMachine::Protocols::SmtpServer
  def receive_sender(sender)
    current.sender = sender
    true
  end

  def receive_recipient(recipient)
    current.recipient = recipient
    true
  end

  def receive_message
    current.received = true
    current.completed_at = Time.now

    puts "==> SMTP: Received message from '#{current.sender}' (#{current.data.length} bytes)"

    if current.sender =~ /noreply@github.com/
      mail = Mail.new current.data
      plain_text = mail.body.parts.first.body.decoded
      if plain_text  =~ /https:\/\/github.com\/users\/(.*?)\/emails.*?(?=\n)/
        confirm_link = $&
        login = $1

        robot = ZombieFans::Robot.find login
        robot.sign_in
        robot.follow_confirm_link confirm_link
      end
    end

    true
  rescue => error
    puts "*** Error receiving message: #{current.inspect}"
    puts "    Exception: #{error}"
    puts "    Backtrace:"
    $!.backtrace.each do |line|
      puts "       #{line}"
    end
    false
  ensure
    @current = nil
  end

  def receive_data_command
    current.data = ""
    true
  end

  def receive_data_chunk(data)
    current.data << data.join("\n")
    true
  end

  def current
    @current ||= OpenStruct.new
  end

  def self.start(host = '0.0.0.0', port = 25)
    @server = EventMachine.start_server host, port, self
    puts "Starting MailCatcher on smtp://#{host}:#{port}"
  end

  def self.stop
    if @server
      EventMachine.stop_server @server
      @server = nil
    end
  end

  def self.running?
    !!@server
  end
 end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zombie_fans-0.2.0 lib/zombie_fans/smtp.rb