Class: Minbox::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/minbox/client.rb

Constant Summary collapse

COMMANDS =
Hashie::Rash.new(
  /^AUTH LOGIN/i => AuthLogin.new,
  /^AUTH PLAIN/i => AuthPlain.new,
  /^DATA/i => Data.new,
  /^EHLO/i => Ehlo.new,
  /^HELO/i => Helo.new,
  /^MAIL FROM/i => Noop.new,
  /^NOOP/i => Noop.new,
  /^QUIT/i => Quit.new,
  /^RCPT TO/i => Noop.new,
  /^RSET/i => Noop.new,
  /^STARTTLS/i => StartTls.new
)
UNSUPPORTED =
Unsupported.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, socket, logger) ⇒ Client

Returns a new instance of Client.



108
109
110
111
112
# File 'lib/minbox/client.rb', line 108

def initialize(server, socket, logger)
  @server = server
  @logger = logger
  @socket = socket
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



106
107
108
# File 'lib/minbox/client.rb', line 106

def logger
  @logger
end

#serverObject (readonly)

Returns the value of attribute server.



106
107
108
# File 'lib/minbox/client.rb', line 106

def server
  @server
end

#socketObject (readonly)

Returns the value of attribute socket.



106
107
108
# File 'lib/minbox/client.rb', line 106

def socket
  @socket
end

Instance Method Details

#authenticate(username, password) ⇒ Object



153
154
155
156
157
158
# File 'lib/minbox/client.rb', line 153

def authenticate(username, password)
  logger.debug("#{username}:#{password}")
  return write '535 Authenticated failed - protocol error' unless username && password

  write '235 2.7.0 Authentication successful'
end

#closeObject



144
145
146
147
# File 'lib/minbox/client.rb', line 144

def close
  socket&.close
  @socket = nil
end

#connected?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/minbox/client.rb', line 149

def connected?
  @socket
end

#handle(&block) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/minbox/client.rb', line 114

def handle(&block)
  write "220 #{server.host} ESMTP"
  while connected? && (line = read)
    command = COMMANDS.fetch(line, UNSUPPORTED)
    command.run(self, line, &block)
  end
  close
rescue Errno::ECONNRESET, Errno::EPIPE => error
  logger.error(error)
  close
end

#readObject



138
139
140
141
142
# File 'lib/minbox/client.rb', line 138

def read
  line = socket.gets
  logger.debug("C: #{line.inspect}")
  line
end

#secure_socket!Object



126
127
128
129
130
# File 'lib/minbox/client.rb', line 126

def secure_socket!
  socket = OpenSSL::SSL::SSLSocket.new(@socket, server.ssl_context)
  socket.sync_close = true
  @socket = socket.accept
end

#write(message) ⇒ Object



132
133
134
135
136
# File 'lib/minbox/client.rb', line 132

def write(message)
  message = "#{message}\r\n"
  logger.debug("S: #{message.inspect}")
  socket.puts message
end