Class: Bosh::Agent::SmtpServer

Inherits:
EM::Protocols::SmtpServer
  • Object
show all
Defined in:
lib/bosh_agent/smtp_server.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

MAX_MESSAGE_SIZE =

1M

1024 * 1024
INACTIVITY_TIMEOUT =

seconds

10

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ SmtpServer

Returns a new instance of SmtpServer.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bosh_agent/smtp_server.rb', line 12

def initialize(*args)
  super

  options = args.last

  @logger        = Config.logger
  @user          = options[:user]
  @password      = options[:password]
  @processor     = options[:processor]
  @authenticated = false
  @chunks        = [ ]
  @msg_size      = 0

  # @parms come from EM:Protocols::SmtpServer
  @parms[:auth] = true if @user && @password

  self.comm_inactivity_timeout = INACTIVITY_TIMEOUT
end

Instance Method Details

#authenticated?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/bosh_agent/smtp_server.rb', line 39

def authenticated?
  @authenticated
end

#get_server_domainObject



35
36
37
# File 'lib/bosh_agent/smtp_server.rb', line 35

def get_server_domain
  get_server_greeting
end

#get_server_greetingObject



31
32
33
# File 'lib/bosh_agent/smtp_server.rb', line 31

def get_server_greeting
  "BOSH Agent SMTP Server"
end

#receive_data_chunk(c) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/bosh_agent/smtp_server.rb', line 63

def receive_data_chunk(c)
  @msg_size += c.join.length

  if @msg_size > MAX_MESSAGE_SIZE
    send_data "552 Message too large\r\n"
    close_connection_after_writing
  else
    @chunks += c
  end
end

#receive_messageObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/bosh_agent/smtp_server.rb', line 74

def receive_message
  unless @processor
    @logger.error "Failed to process alert: no alert processor provided"
    return false
  end

  unless @processor.respond_to?(:process_email_alert)
    @logger.error "Failed to process alert: alert processor should respond to :process_email_alert method"
    return false
  end

  message = @chunks.join("\n")
  @chunks = [ ] # Support multiple data blocks in a single SMTP session
  @processor.process_email_alert(message)

  # WARNING: this MUST return true, otherwise Monit will try to send alerts over and over again
  true
end

#receive_plain_auth(user, password) ⇒ Object

We don’t really care about senders and recipients as we only use SMTP for data transport between Monit and Agent. However it’s handy to use the SMTP sequence constraints to enforce that only authenticated user can actually send data.



49
50
51
# File 'lib/bosh_agent/smtp_server.rb', line 49

def receive_plain_auth(user, password)
  @authenticated = (user == @user && @password == password)
end

#receive_recipient(rcpt) ⇒ Object

Only accept RCPT TO if already authenticated



59
60
61
# File 'lib/bosh_agent/smtp_server.rb', line 59

def receive_recipient(rcpt)
  authenticated?
end

#receive_sender(sender) ⇒ Object

Only accept MAIL FROM if already authenticated



54
55
56
# File 'lib/bosh_agent/smtp_server.rb', line 54

def receive_sender(sender)
  authenticated?
end