Created
February 21, 2013 09:35
-
-
Save chrismytton/5003501 to your computer and use it in GitHub Desktop.
Dumb Ruby SMTP Server - Pre Pre Alpha
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
require 'logger' | |
class MailServer | |
def initialize(port) | |
@sockets = Socket.tcp_server_sockets(port) | |
end | |
def start | |
Socket.accept_loop(@sockets) do |connection| | |
handle(connection) | |
end | |
end | |
def handle(connection) | |
logger.debug "Got connection: #{connection.remote_address.inspect} -> #{connection.local_address.inspect}" | |
connection.puts("220 localhost ESMTP hecticjeff smtp") | |
while command = connection.gets | |
logger.info "CLIENT: #{command.chomp}" | |
case command.split(' ').first | |
when 'EHLO' | |
connection.puts("250 localhost") | |
when 'QUIT' | |
connection.puts("221 localhost Bye") | |
connection.close | |
break | |
when 'DATA' | |
connection.puts("354 End data with <CR><LF>.<CR><LF>") | |
message = "" | |
waiting = false | |
while data = connection.gets | |
waiting = (data[-2..-1] == "\r\n") | |
break if data == ".\r\n" && waiting | |
message += data | |
end | |
logger.debug "Got message: #{message}" | |
connection.puts("250 OK") | |
when 'MAIL' | |
connection.puts("250 OK") | |
when 'RCPT' | |
connection.puts("250 OK") | |
else | |
logger.debug "Unknown command: #{command.inspect}" | |
end | |
end | |
logger.debug "Connection closed" | |
end | |
def logger | |
@logger ||= Logger.new(STDOUT) | |
end | |
end | |
MailServer.new(4481).start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment