Commit 2e75512
lib/minbox/client.rb
@@ -1,73 +1,68 @@
module Minbox
class Client
- attr_reader :host, :socket, :mail
+ attr_reader :host, :socket, :logger
- def initialize(host, socket)
+ def initialize(host, socket, logger)
@host = host
- @socket = socket
+ @logger = logger
@mail = { headers: [], body: [] }
+ @socket = socket
end
def mail_message
socket.puts "220"
while socket && (line = socket.gets)
- process(line, socket)
+ case line
+ when /^EHLO/i then ehlo(line)
+ when /^HELO/i then helo(line)
+ when /^MAIL FROM/i then mail_from(line)
+ when /^RCPT TO/i then rcpt_to(line)
+ when /^DATA/i then data(line)
+ when /^QUIT/i then quit
+ else
+ logger.error(line)
+ socket.puts('502 Invalid/unsupported command')
+ end
end
- Mail.new(mail[:body].join)
+ Mail.new(@mail[:body].join)
end
private
- def process(line, socket)
- case line
- when /^EHLO/i then ehlo(line, socket)
- when /^HELO/i then helo(line, socket)
- when /^MAIL FROM/i then mail_from(line, socket)
- when /^RCPT TO/i then rcpt_to(line, socket)
- when /^DATA/i then data(line, socket)
- when /^QUIT/i then quit(line, socket)
- else
- puts "***" * 10
- puts line.inspect
- puts "***" * 10
- socket.puts('502 Invalid/unsupported command')
- end
- end
-
- def quit(line, socket)
+ def quit
socket.puts "221 Bye"
socket.close
@socket = nil
end
- def data(line, socket)
+ def data(line)
socket.puts "354 End data with <CR><LF>.<CR><LF>"
line = socket.gets
until line.match(/^\.\r\n$/)
- mail[:body] << line
+ @mail[:body] << line
line = socket.gets
end
socket.puts "250 OK"
- quit(line, socket)
+ quit
end
- def rcpt_to(line, socket)
- mail[:headers] << line
+ def rcpt_to(line)
+ @mail[:headers] << line
socket.puts "250 OK"
end
- def mail_from(line, socket)
- mail[:headers] << line
+ def mail_from(line)
+ @mail[:headers] << line
socket.puts "250 OK"
end
- def ehlo(line, socket)
+ def ehlo(line)
_ehlo, _client_domain = line.split(" ")
socket.puts "250-#{host}"
socket.puts "250 OK"
end
- def helo(line, socket)
+ def helo(line)
_ehlo, _client_domain = line.split(" ")
socket.puts "250 #{host}"
end
lib/minbox/server.rb
@@ -22,7 +22,7 @@ module Minbox
def handle(socket)
logger.debug("client connected: #{socket.inspect}")
- Client.new(host, socket).mail_message
+ Client.new(host, socket, logger).mail_message
end
def shutdown!