Commit 51a5427

mokha <mokha@cisco.com>
2019-02-27 00:20:22
extract server class
1 parent 7265cfc
Changed files (3)
lib/minbox/cli.rb
@@ -24,49 +24,7 @@ module Minbox
 
       desc 'server <HOST> <PORT>', 'SMTP server'
       def server(host = 'localhost', port = '25')
-        require 'socket'
-
-        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
-
-          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
-          puts mail.inspect
-        end
+        Server.new(host, port).listen!
       end
 
       desc 'version', 'Display the current version'
lib/minbox/server.rb
@@ -0,0 +1,54 @@
+module Minbox
+  class Server
+    attr_reader :host, :port
+
+    def initialize(host, port)
+      @host = host
+      @port = port
+    end
+
+    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
+
+        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
+        puts mail.inspect
+      end
+    end
+  end
+end
lib/minbox.rb
@@ -1,6 +1,8 @@
+require 'socket'
+
+require "minbox/server"
 require "minbox/version"
 
 module Minbox
   class Error < StandardError; end
-  # Your code goes here...
 end