Skip to content

Commit

Permalink
Basic SMTP conversation.
Browse files Browse the repository at this point in the history
  • Loading branch information
adscott committed Feb 12, 2012
1 parent be8a969 commit 2f202e9
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .rvmrc
@@ -1 +1 @@
rvm --create use 1.9.3-p0@mailbox
rvm --create use 1.8.7-p352@mailbox
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -2,3 +2,4 @@ source :rubygems

gem 'rake'
gem 'rspec'
gem 'ruby-debug'
10 changes: 10 additions & 0 deletions Gemfile.lock
@@ -1,8 +1,12 @@
GEM
remote: http://rubygems.org/
specs:
columnize (0.3.6)
diff-lcs (1.1.3)
linecache (0.46)
rbx-require-relative (> 0.0.4)
rake (0.9.2.2)
rbx-require-relative (0.0.5)
rspec (2.8.0)
rspec-core (~> 2.8.0)
rspec-expectations (~> 2.8.0)
Expand All @@ -11,10 +15,16 @@ GEM
rspec-expectations (2.8.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.8.0)
ruby-debug (0.10.4)
columnize (>= 0.1)
ruby-debug-base (~> 0.10.4.0)
ruby-debug-base (0.10.4)
linecache (>= 0.3)

PLATFORMS
ruby

DEPENDENCIES
rake
rspec
ruby-debug
71 changes: 71 additions & 0 deletions lib/mailbox.rb
@@ -1,17 +1,88 @@
require 'socket'

class Mailbox

attr_reader :mails

def initialize(port)
@port = port
@mails = []
end


def start
@service = TCPServer.new('localhost', @port )
Thread.new { accept(@service) }
end

def stop
@service.close unless @service.nil? || @service.closed?
end

def accept( service )
while session = service.accept

class << session
def get_line
line = gets
line.chomp! unless line.nil?
line
end
end

begin
serve( session )
rescue Exception => e
puts e.message
end
end
end

def serve( connection )
connection.puts( "220 localhost mailbox ready ESTMP" )
helo = connection.get_line

if helo =~ /^EHLO\s+/
connection.puts "250-localhost mailbox here"
connection.puts "250 HELP"
end

from = connection.get_line
connection.puts( "250 ok" )


to_list = []

loop do
to = connection.get_line
break if to.nil?

if to =~ /^DATA/
connection.puts( "354 start" )
break
else
to_list << to
connection.puts( "250 ok" )
end
end

lines = []
loop do
line = connection.get_line
break if line.nil? || line == "."
lines << line
end

connection.puts "250 ok"
connection.gets
connection.puts "221 bye"
connection.close

@mails << {
:from => from.gsub(/MAIL FROM:\s*/, ''),
# :to_list => to_list.map { |to| to.gsub( /RCPT TO:\s*/, "" ) }
# :body => lines.join( "\n" )
}
end

end
50 changes: 39 additions & 11 deletions spec/mailbox_spec.rb
Expand Up @@ -11,21 +11,49 @@
@mailbox.stop
end

it 'should open a port' do
@mailbox.start
describe 'ports' do

port(2525).should be_open
end
it 'should open a port' do
@mailbox.start

it 'should close a port' do
@mailbox.start
@mailbox.stop
port(2525).should be_open
end

it 'should close a port' do
@mailbox.start
@mailbox.stop

port(2525).should_not be_open
port(2525).should_not be_open
end

it 'should not allow a second server to be started on the same port'

def port(port)
Port.new(port)
end
end

def port(port)
Port.new(port)
end
describe 'should accept SMTP traffic' do

it 'should recieve email' do
@mailbox.start

socket = TCPSocket.open('localhost', 2525)
socket.puts('helo localhost.localdomain')
socket.puts('MAIL FROM:myaddress@mydomain.com')
socket.puts('RCPT TO:somone@somedomain.com')
socket.puts('DATA')
socket.puts('Hello Fred, can you call me?')
socket.puts('.')
socket.puts('QUIT')

while(@mailbox.mails.empty?) do
sleep(0.1)
end

@mailbox.mails[0][:from].should == 'myaddress@mydomain.com'
end

end

end

0 comments on commit 2f202e9

Please sign in to comment.