Class: Mailman::Receiver::IMAP
- Inherits:
-
Object
- Object
- Mailman::Receiver::IMAP
- Defined in:
- lib/mailman/receiver/imap.rb
Overview
Receives messages using IMAP, and passes them to a MessageProcessor.
Instance Attribute Summary collapse
-
#connection ⇒ Net::IMAP
readonly
The IMAP connection.
Instance Method Summary collapse
-
#connect ⇒ Object
Connects to the IMAP server.
-
#disconnect ⇒ Object
Disconnects from the IMAP server.
-
#get_messages ⇒ Object
Iterates through new messages, passing them to the processor, and flagging them as done.
-
#initialize(options) ⇒ IMAP
constructor
A new instance of IMAP.
- #started? ⇒ Boolean
Constructor Details
#initialize(options) ⇒ IMAP
Returns a new instance of IMAP.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/mailman/receiver/imap.rb', line 28 def initialize() @processor = [:processor] @server = [:server] @username = [:username] @password = [:password] @filter = [:filter] || 'UNSEEN' @done_flags = [:done_flags] || [Net::IMAP::SEEN] @ssl = [:ssl] || false @starttls = [:starttls] || false @port = [:port] || (@ssl ? 993 : 143) @folder = [:folder] || "INBOX" if @starttls && @ssl raise StandardError.new("either specify ssl or starttls, not both") end end |
Instance Attribute Details
#connection ⇒ Net::IMAP (readonly)
Returns the IMAP connection.
9 10 11 |
# File 'lib/mailman/receiver/imap.rb', line 9 def connection @connection end |
Instance Method Details
#connect ⇒ Object
Connects to the IMAP server.
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mailman/receiver/imap.rb', line 46 def connect tries ||= 5 if @connection.nil? or @connection.disconnected? @connection = Net::IMAP.new(@server, port: @port, ssl: @ssl) if @starttls @connection.starttls end @connection.login(@username, @password) end @connection.select(@folder) rescue Net::IMAP::ByeResponseError, Net::IMAP::NoResponseError => e retry unless (tries -= 1).zero? end |
#disconnect ⇒ Object
Disconnects from the IMAP server.
61 62 63 64 65 |
# File 'lib/mailman/receiver/imap.rb', line 61 def disconnect return false if @connection.nil? @connection.logout @connection.disconnected? ? true : @connection.disconnect rescue nil end |
#get_messages ⇒ Object
Iterates through new messages, passing them to the processor, and flagging them as done.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/mailman/receiver/imap.rb', line 69 def @connection.search(@filter).each do || body = @connection.fetch(, "RFC822")[0].attr["RFC822"] begin @processor.process(body) rescue StandardError => error Mailman.logger.error "Error encountered processing message: #{.inspect}\n #{error.class.to_s}: #{error.}\n #{error.backtrace.join("\n")}" next end @connection.store(, "+FLAGS", @done_flags) end # Clears messages that have the Deleted flag set @connection.expunge end |
#started? ⇒ Boolean
84 85 86 |
# File 'lib/mailman/receiver/imap.rb', line 84 def started? not (!@connection.nil? && @connection.disconnected?) end |