Permalink
Browse files

A tiny commit to make percolate more powerful

Now you can run it like this:

    percolate.go do |message|
        do_things_with(message)
    end

instead of subclassing the responder and providing your own methods
to do stuff.
  • Loading branch information...
1 parent a5d413c commit 453e6594bddbc269a373d90ac1d3f9dfbbb42b64 @dagbrown committed with Dave Brown Nov 11, 2008
Showing with 9 additions and 6 deletions.
  1. +6 −4 lib/percolate/listener.rb
  2. +3 −2 lib/percolate/smtp/responder.rb
View
@@ -45,7 +45,7 @@ def initialize(opts = {})
# Once the listener is running, let it start handling mail by
# invoking the poorly-named "go" method.
- def go
+ def go &block
trap 'CLD' do
debug "Got SIGCHLD"
reap_children
@@ -58,15 +58,17 @@ def go
@pids = []
while mailsocket=@socket.accept
debug "Got connection from #{mailsocket.peeraddr[3]}"
- pid = handle_connection mailsocket
+
+ pid = handle_connection mailsocket, &block
+
mailsocket.close
@pids << pid
end
end
private
- def handle_connection mailsocket
+ def handle_connection mailsocket, &block
fork do # I can't imagine the contortions required
# in Win32 to get "fork" to work, but hey,
# maybe someone did so anyway.
@@ -79,7 +81,7 @@ def handle_connection mailsocket
cmd = mailsocket.readline
cmd.chomp! CRLF
- responder.command cmd
+ responder.command cmd, &block
end
rescue TransactionFinishedException
mailsocket.print responder.response + CRLF
@@ -59,6 +59,7 @@ def initialize(mailhostname, opts={})
# the RFC says, be any old crap at all! Don't
# even expect an RFC2822-formatted message)
def process_message message_object
+ yield message_object if block_given?
return true
end
@@ -305,10 +306,10 @@ def quit
# The special-case code for message data, which unlike other
# data, is delivered as lots and lots of lines with a sentinel.
- def handle_message_data message_line
+ def handle_message_data message_line, &block
if message_line == "."
@current_state = :message_received
- result, text = process_message @mail_object
+ result, text = process_message @mail_object, &block
if result or
( String === result and text.nil? )
if String === result and text.nil?

0 comments on commit 453e659

Please sign in to comment.