Class: Cybertrain::Test::ReceivedMessage

Inherits:
Object
  • Object
show all
Defined in:
cybertrain/test/smtp_receiver.rb

Overview

What one SMTP connection carried.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#envelope_from ⇒ String (readonly)

Returns the MAIL FROM address, without the angle brackets.

Returns:

  • (String) —

    the MAIL FROM address, without the angle brackets



16
17
18
# File 'cybertrain/test/smtp_receiver.rb', line 16

def envelope_from
  @envelope_from
end

#envelope_recipients ⇒ Array<String> (readonly)

Returns the RCPT TO addresses the receiver accepted.

Returns:

  • (Array<String>) —

    the RCPT TO addresses the receiver accepted



20
21
22
# File 'cybertrain/test/smtp_receiver.rb', line 20

def envelope_recipients
  @envelope_recipients
end

#message_data ⇒ String (readonly)

Returns the message as it was sent after DATA: headers, a blank line, the body, CRLF line endings, dot-stuffing undone, the final “.” line removed; “” when no message was accepted.

Returns:

  • (String) —

    the message as it was sent after DATA: headers, a blank line, the body, CRLF line endings, dot-stuffing undone, the final “.” line removed; “” when no message was accepted



26
27
28
# File 'cybertrain/test/smtp_receiver.rb', line 26

def message_data
  @message_data
end

#transcript ⇒ Array<String> (readonly)

Returns every command line the client sent, in order (“EHLO localhost”, “MAIL FROM:a@example.com”, …, “QUIT”); the message itself is in #message_data, not here.

Returns:

  • (Array<String>) —

    every command line the client sent, in order (“EHLO localhost”, “MAIL FROM:a@example.com”, …, “QUIT”); the message itself is in #message_data, not here



32
33
34
# File 'cybertrain/test/smtp_receiver.rb', line 32

def transcript
  @transcript
end

Instance Method Details

#delivered? ⇒ Boolean

Whether a message was accepted (the 250 after the data was sent).

Returns:

  • (Boolean)


44
45
46
# File 'cybertrain/test/smtp_receiver.rb', line 44

def delivered?
  !@message_data.empty?
end

#message_body ⇒ String

The message body: what follows the blank line after the headers.

Returns:

  • (String)


51
52
53
54
55
56
# File 'cybertrain/test/smtp_receiver.rb', line 51

def message_body
  at = @message_data.byteindex("\r\n\r\n")
  return "" if at.nil?

  @message_data.byteslice(at + 4, @message_data.bytesize - at - 4)
end

#message_header(name) ⇒ String

The value of the first message header named name (any case), unfolded; “” when there is none.

Parameters:

  • name (String)

Returns:

  • (String)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'cybertrain/test/smtp_receiver.rb', line 63

def message_header(name)
  wanted = name.to_s.downcase
  data = @message_data
  stop = data.byteindex("\r\n\r\n")
  stop = data.bytesize if stop.nil?
  pos = 0
  found = false
  value = String.new
  while pos < stop
    eol = data.byteindex("\r\n", pos)
    eol = stop if eol.nil? || eol > stop
    line = data.byteslice(pos, eol - pos)
    pos = eol + 2
    continued = line.start_with?(" ") || line.start_with?("\t")
    if found
      break unless continued

      value << " " << line.strip
    elsif !continued
      colon = line.byteindex(":")
      if !colon.nil? && line.byteslice(0, colon).downcase == wanted
        found = true
        value << line.byteslice(colon + 1, line.bytesize - colon - 1).strip
      end
    end
  end
  value
end