Commit 718853e

mokha <mokha@cisco.com>
2019-03-03 21:01:48
add support for processing multiple emails in a single session.
1 parent c5e6488
Changed files (3)
lib/minbox/cli.rb
@@ -19,7 +19,7 @@ module Minbox
         require 'net/smtp'
         Net::SMTP.start(host, port) do |smtp|
           smtp.send_message(mail.to_s, 'me+1@example.org', 'them+1@example.com')
-          #smtp.send_message(mail.to_s, 'me+2@example.org', 'them+2@example.com')
+          smtp.send_message(mail.to_s, 'me+2@example.org', 'them+2@example.com')
         end
       end
 
lib/minbox/client.rb
@@ -5,7 +5,6 @@ module Minbox
     def initialize(host, socket, logger)
       @host = host
       @logger = logger
-      @body = []
       @socket = socket
     end
 
@@ -17,7 +16,7 @@ module Minbox
         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 /^DATA/i then data(line, &block)
         when /^QUIT/i then quit
         when /^STARTTLS/i then start_tls
         when /^RSET/i then reset
@@ -27,7 +26,6 @@ module Minbox
           write '502 Invalid/unsupported command'
         end
       end
-      block.call(Mail.new(@body.join))
     end
 
     private
@@ -37,14 +35,16 @@ module Minbox
       close
     end
 
-    def data(line)
+    def data(line, &block)
       write "354 End data with <CR><LF>.<CR><LF>"
+      body = []
       line = read
       until line.nil? || line.match(/^\.\r\n$/)
-        @body << line
+        body << line
         line = read
       end
       write "250 OK"
+      block.call(Mail.new(body.join))
     end
 
     def rcpt_to(line)
@@ -71,7 +71,6 @@ module Minbox
     end
 
     def reset
-      @body = []
       write '250 OK'
     end
 
@@ -80,13 +79,13 @@ module Minbox
     end
 
     def write(message)
-      logger.debug message
+      logger.debug message.inspect
       socket.puts message
     end
 
     def read
       line = socket.gets
-      logger.debug line
+      logger.debug line.inspect
       line
     end
 
lib/minbox/publisher.rb
@@ -13,7 +13,10 @@ module Minbox
     end
 
     def publish(mail)
-      publishers.each { |x| x.publish(mail) }
+      Thread.new do
+        Minbox.logger.debug("Publishing: #{mail.message_id}")
+        publishers.each { |x| x.publish(mail) }
+      end
     end
 
     def self.from(outputs)