Commit f747bc8

mokha <mokha@cisco.com>
2019-02-27 17:50:53
yield each mail message to the caller.
1 parent 51a5427
Changed files (2)
lib/minbox/cli.rb
@@ -19,12 +19,14 @@ module Minbox
         require 'net/smtp'
         Net::SMTP.start(host, port) do |smtp|
           smtp.send_message(mail.to_s, 'me@example.org', 'them@example.com')
-        end       
+        end
       end
 
       desc 'server <HOST> <PORT>', 'SMTP server'
       def server(host = 'localhost', port = '25')
-        Server.new(host, port).listen!
+        Server.new(host, port).listen! do |mail|
+          puts mail.to_s
+        end
       end
 
       desc 'version', 'Display the current version'
lib/minbox/server.rb
@@ -10,45 +10,44 @@ module Minbox
     def listen!
       server = TCPServer.new(port.to_i)
       loop do
-        client = server.accept
-        mail = { headers: [], body: [] }
-
-        client.puts "220"
-        ehlo, client_domain = client.gets.split(" ")
-        puts [ehlo, client_domain].inspect
-
-        if ["HELO", "EHLO"].include?(ehlo)
-          client.puts "250-#{host}"
-          client.puts "250-8BITMIME"
-          client.puts "250-SIZE 10485760"
-          client.puts "250-AUTH PLAIN LOGIN"
-          client.puts "250 OK"
-        else
-          puts 'Ooops...'
-          client.close
-          next
-        end
+        yield handle(server.accept)
+      end
+    end
 
-        data = client.gets
-        until data.start_with?("DATA")
-          mail[:headers] << data
-          client.puts "250 OK"
-          data = client.gets
-        end
-        client.puts "354 End data with <CR><LF>.<CR><LF>"
+    def handle(client)
+      mail = { headers: [], body: [] }
 
-        data = client.gets
-        until data.match(/^\.\r\n$/)
-          mail[:body] << data
-          data = client.gets
-        end
+      client.puts "220"
+      ehlo, _client_domain = client.gets.split(" ")
 
+      if ["HELO", "EHLO"].include?(ehlo)
+        client.puts "250-#{host}"
         client.puts "250 OK"
-        client.puts "221 Bye"
-
+      else
+        puts 'Ooops...'
         client.close
-        puts mail.inspect
+        return
+      end
+
+      data = client.gets
+      until data.start_with?("DATA")
+        mail[:headers] << data
+        client.puts "250 OK"
+        data = client.gets
       end
+      client.puts "354 End data with <CR><LF>.<CR><LF>"
+
+      data = client.gets
+      until data.match(/^\.\r\n$/)
+        mail[:body] << data
+        data = client.gets
+      end
+
+      client.puts "250 OK"
+      client.puts "221 Bye"
+      client.close
+
+      Mail.new(mail[:body].join)
     end
   end
 end