Class: SMTP::Session

Inherits:
Object
  • Object
show all
Includes:
Commands::Definitions
Defined in:
lib/drillmail/smtp.rb

Direct Known Subclasses

MinimalSMTPSession

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Commands::Definitions

#commands, #replies, #reply

Constructor Details

#initialize(connection, host) ⇒ Session

Returns a new instance of Session.



35
36
37
38
39
40
41
# File 'lib/drillmail/smtp.rb', line 35

def initialize(connection, host)
  @connection = connection
  @host = host
  @active = true
  @domain = nil
  setup_session
end

Instance Attribute Details

#activeObject

Returns the value of attribute active.



29
30
31
# File 'lib/drillmail/smtp.rb', line 29

def active
  @active
end

#connectionObject

Returns the value of attribute connection.



27
28
29
# File 'lib/drillmail/smtp.rb', line 27

def connection
  @connection
end

#domainObject

Returns the value of attribute domain.



30
31
32
# File 'lib/drillmail/smtp.rb', line 30

def domain
  @domain
end

#forward_pathObject

Returns the value of attribute forward_path.



32
33
34
# File 'lib/drillmail/smtp.rb', line 32

def forward_path
  @forward_path
end

#hostObject

Returns the value of attribute host.



28
29
30
# File 'lib/drillmail/smtp.rb', line 28

def host
  @host
end

#messageObject

Returns the value of attribute message.



33
34
35
# File 'lib/drillmail/smtp.rb', line 33

def message
  @message
end

#reverse_pathObject

Returns the value of attribute reverse_path.



31
32
33
# File 'lib/drillmail/smtp.rb', line 31

def reverse_path
  @reverse_path
end

Instance Method Details

#process(request_string) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/drillmail/smtp.rb', line 68

def process(request_string)
  if request_string
    request_array = request_string.split(' ')
    {
      command: request_array.shift.to_sym,
      body: request_array.join(' ')
    }
  else
    { command: :NOOP, body: '' }
  end
end

#route(request) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/drillmail/smtp.rb', line 80

def route(request)
  puts "Requesting: #{request[:command]} #{request[:body]}"
  if commands.key?(request[:command])
    response = commands[request[:command]].call(request[:body])
  else
    response = reply(500)
  end
  puts "Responding: #{response}"
  response
end

#runObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/drillmail/smtp.rb', line 49

def run
  begin
    @connection.puts reply(220, "#{@host} Simple Mail Transfer")

    while @active 
      @connection.sync = true
      request = @connection.gets
      response = route(process(request))
      @connection.puts "#{response}\r\n"
    end
  rescue => e
    @connection.puts reply(421, e)
    puts e, e.backtrace
  ensure
      @connection.shutdown(:RDWR)
      @connection.close
  end
end

#setup_sessionObject



43
44
45
46
47
# File 'lib/drillmail/smtp.rb', line 43

def setup_session
  @reverse_path = ''
  @forward_path = ''
  @message = []
end