Skip to content
Browse files

Refactored IMAP and POP3 code to use shared 'polling_loop' method

  • Loading branch information...
1 parent 6ea375e commit 9f2095faadd30cd2d5854137c18555bc6f646f51 @ndbroadbent ndbroadbent committed Mar 17, 2012
Showing with 45 additions and 48 deletions.
  1. +45 −48 lib/mailman/application.rb
View
93 lib/mailman/application.rb
@@ -24,6 +24,10 @@ def initialize(&block)
instance_eval(&block)
end
+ def polling?
+ Mailman.config.poll_interval > 0 && !@polling_interrupt
+ end
+
# Sets the block to run if no routes match a message.
def default(&block)
@router.default_block = block
@@ -39,62 +43,33 @@ def run
require rails_env
end
- if !Mailman.config.ignore_stdin && $stdin.fcntl(Fcntl::F_GETFL, 0) == 0 # we have stdin
+ if Mailman.config.graceful_death
+ # When user presses CTRL-C, finish processing current message before exiting
+ Signal.trap("INT") { @polling_interrupt = true }
+ end
+
+ # STDIN
+ # ---------------------------------------------------------------------
+ if !Mailman.config.ignore_stdin && $stdin.fcntl(Fcntl::F_GETFL, 0) == 0
Mailman.logger.debug "Processing message from STDIN."
@processor.process($stdin.read)
+ # IMAP
+ # ---------------------------------------------------------------------
elsif Mailman.config.imap
options = {:processor => @processor}.merge(Mailman.config.imap)
Mailman.logger.info "IMAP receiver enabled (#{options[:username]}@#{options[:server]})."
- if Mailman.config.poll_interval > 0
- polling = true
- Mailman.logger.info "Polling enabled. Checking every #{Mailman.config.poll_interval} seconds."
- else
- polling = false
- Mailman.logger.info 'Polling disabled. Checking for messages once.'
- end
-
- connection = Receiver::IMAP.new(options)
-
- if Mailman.config.graceful_death
- Signal.trap("INT") {polling = false}
- end
-
- loop do
- begin
- Mailman.logger.debug "Checking IMAP server #{options[:server]} for messages"
- connection.connect
- connection.get_messages
- connection.disconnect
- rescue SystemCallError => e
- Mailman.logger.error e.message
- end
-
- break if !polling
- sleep Mailman.config.poll_interval
- end
+ polling_loop Receiver::IMAP.new(options)
+ # POP3
+ # ---------------------------------------------------------------------
elsif Mailman.config.pop3
options = {:processor => @processor}.merge(Mailman.config.pop3)
Mailman.logger.info "POP3 receiver enabled (#{options[:username]}@#{options[:server]})."
- if Mailman.config.poll_interval > 0 # we should poll
- polling = true
- Mailman.logger.info "Polling enabled. Checking every #{Mailman.config.poll_interval} seconds."
- else
- polling = false
- Mailman.logger.info 'Polling disabled. Checking for messages once.'
- end
-
- connection = Receiver::POP3.new(options)
- loop do
- Mailman.logger.debug "Checking POP3 server for messages..."
- connection.connect
- connection.get_messages
- connection.disconnect
- break if !polling
- sleep Mailman.config.poll_interval
- end
+ polling_loop Receiver::POP3.new(options)
+ # Maildir
+ # ---------------------------------------------------------------------
elsif Mailman.config.maildir
require 'maildir'
require 'fssm'
@@ -108,9 +83,6 @@ def run
process_maildir
}
end
- elsif $stdin.fcntl(Fcntl::F_GETFL, 0) == 0 # we have stdin
- Mailman.logger.debug "Processing message from STDIN."
- @processor.process($stdin.read)
end
end
@@ -125,5 +97,30 @@ def process_maildir
end
end
+ private
+
+ # Run the polling loop for the email inbox connection
+ def polling_loop(connection)
+ if polling?
+ polling_msg = "Polling enabled. Checking every #{Mailman.config.poll_interval} seconds."
+ else
+ polling_msg = "Polling disabled. Checking for messages once."
+ end
+ Mailman.logger.info(polling_msg)
+
+ loop do
+ begin
+ connection.connect
+ connection.get_messages
+ connection.disconnect
+ rescue SystemCallError => e
+ Mailman.logger.error e.message
+ end
+
+ break unless polling?
+ sleep Mailman.config.poll_interval
+ end
+ end
+
end
end

0 comments on commit 9f2095f

Please sign in to comment.
Something went wrong with that request. Please try again.