Class: SMTP

Inherits:
EventMachine::Protocols::SmtpServer
  • Object
show all
Defined in:
lib/zombie_fans/smtp.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.running?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/zombie_fans/smtp.rb', line 74

def self.running?
  !!@server
end

.start(host = '0.0.0.0', port = 25) ⇒ Object



62
63
64
65
# File 'lib/zombie_fans/smtp.rb', line 62

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

.stopObject



67
68
69
70
71
72
# File 'lib/zombie_fans/smtp.rb', line 67

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

Instance Method Details

#currentObject



58
59
60
# File 'lib/zombie_fans/smtp.rb', line 58

def current
  @current ||= OpenStruct.new
end

#receive_data_chunk(data) ⇒ Object



53
54
55
56
# File 'lib/zombie_fans/smtp.rb', line 53

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

#receive_data_commandObject



48
49
50
51
# File 'lib/zombie_fans/smtp.rb', line 48

def receive_data_command
  current.data = ""
  true
end

#receive_messageObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/zombie_fans/smtp.rb', line 16

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 =~ /[email protected]/
    mail = Mail.new current.data
    plain_text = mail.body.parts.first.body.decoded
    if plain_text  =~ /https:\/\/github.com\/users\/(.*?)\/emails.*?(?=\n)/
      confirm_link = $&
       = $1

      robot = ZombieFans::Robot.find 
      robot.
      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

#receive_recipient(recipient) ⇒ Object



11
12
13
14
# File 'lib/zombie_fans/smtp.rb', line 11

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

#receive_sender(sender) ⇒ Object



6
7
8
9
# File 'lib/zombie_fans/smtp.rb', line 6

def receive_sender(sender)
  current.sender = sender
  true
end