diff --git a/.gitignore b/.gitignore index e43b0f9..f31b3e2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .DS_Store +*.swp diff --git a/README.rdoc b/README.rdoc index e1878de..04d13cf 100644 --- a/README.rdoc +++ b/README.rdoc @@ -35,6 +35,12 @@ The receiver object is expected to have a receive method that takes a message as Call fetch to download messages and process them. @fetcher.fetch + +or + + @fetcher.fetch do |message| + ... + end == Configuration @@ -43,6 +49,7 @@ The following options can be passed to the Fetcher.create factory metho [type] POP or IMAP [server] The IP address or domain name of the server [port] The port to connect to (defaults to the standard port for the type of server) +[keep] Set to any value to dont delete messages after retriving [ssl] Set to any value to use SSL encryption [username] The username used to connect to the server [password] The password used to connect to the server @@ -98,4 +105,4 @@ Created by Dan Weinand and Luke Francl. Development supported by {Slantwise Desi Generators for Rails 3 compatibility added by Amol Hatwár, Exceed Consulting. -Licensed under the terms of the MIT License. Be excellent to each other. \ No newline at end of file +Licensed under the terms of the MIT License. Be excellent to each other. diff --git a/lib/fetcher/base.rb b/lib/fetcher/base.rb index 5b7ea3e..51d6019 100644 --- a/lib/fetcher/base.rb +++ b/lib/fetcher/base.rb @@ -5,6 +5,7 @@ class Base # * :username - Username to use when connecting to server. # * :password - Password to use when connecting to server. # * :receiver - Receiver object to pass messages to. Assumes the + # * :keep - Do not remove messages from server # receiver object has a receive method that takes a message as it's argument # # Additional protocol-specific options implimented by sub-classes @@ -16,7 +17,6 @@ class Base # :receiver => IncomingMailHandler) def initialize(options={}) %w(server username password receiver).each do |opt| - raise ArgumentError, "#{opt} is required" unless options[opt.to_sym] # convert receiver to a Class if it isn't already. if opt == "receiver" && options[:receiver].is_a?(String) options[:receiver] = Kernel.const_get(options[:receiver]) @@ -27,7 +27,8 @@ def initialize(options={}) end # Run the fetching process - def fetch + def fetch &block + @receiver = block if block_given? establish_connection get_messages close_connection @@ -52,7 +53,11 @@ def close_connection #:nodoc: # Send message to receiver object def process_message(message) - @receiver.receive(message) + if @receiver.is_a? Proc + @receiver.call message + else + @receiver.receive(message) + end end # Stub. Should be overridden by subclass. diff --git a/lib/fetcher/imap.rb b/lib/fetcher/imap.rb index c0d0c72..122fe6a 100644 --- a/lib/fetcher/imap.rb +++ b/lib/fetcher/imap.rb @@ -52,7 +52,7 @@ def get_messages handle_bogus_message(msg) end # Mark message as deleted - @connection.uid_store(uid, "+FLAGS", [:Seen, :Deleted]) + @connection.uid_store(uid, "+FLAGS", [:Seen, :Deleted]) unless @keep end end @@ -64,7 +64,7 @@ def handle_bogus_message(message) # Delete messages and log out def close_connection - @connection.expunge + @connection.expunge unless @keep @connection.logout begin @connection.disconnect unless @connection.disconnected? diff --git a/lib/fetcher/pop.rb b/lib/fetcher/pop.rb index e165799..307b22d 100644 --- a/lib/fetcher/pop.rb +++ b/lib/fetcher/pop.rb @@ -28,10 +28,10 @@ def get_messages begin process_message(msg.pop) rescue - handle_bogus_message(msg.pop) + handle_bogus_message(msg) end # Delete message from server - msg.delete + msg.delete unless @keep end end end diff --git a/tasks/fetcher_tasks.rake b/tasks/fetcher_tasks.rake deleted file mode 100644 index d952ae4..0000000 --- a/tasks/fetcher_tasks.rake +++ /dev/null @@ -1,4 +0,0 @@ -# desc "Explaining what the task does" -# task :fetcher do -# # Task goes here -# end \ No newline at end of file