Class: Mailman::Receiver::IMAP

Inherits:
Object
  • Object
show all
Defined in:
lib/mailman/receiver/imap.rb

Overview

Receives messages using IMAP, and passes them to a MessageProcessor.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ IMAP

Returns a new instance of IMAP

Parameters:

  • options (Hash)

    the receiver options

Options Hash (options):

  • :processor (MessageProcessor)

    the processor to pass new messages to

  • :server (String)

    the server to connect to

  • :port (Integer)

    the port to connect to

  • :ssl (Boolean)

    whether or not to use ssl

  • :username (String)

    the username to authenticate with

  • :password (String)

    the password to authenticate with

  • :folder (String)

    the mail folder to search

  • :done_flags (Array)

    the flags to add to messages that have been processed

  • :filter (String)

    the search filter to use to select messages to process



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mailman/receiver/imap.rb', line 24

def initialize(options)
  @processor  = options[:processor]
  @server     = options[:server]
  @username   = options[:username]
  @password   = options[:password]
  @filter     = options[:filter] || 'UNSEEN'
  @done_flags = options[:done_flags] || [Net::IMAP::SEEN]
  @port       = options[:port] || 143
  @ssl        = options[:ssl] || false
  @folder     = options[:folder] || "INBOX"
end

Instance Attribute Details

#connectionNet::IMAP (readonly)

Returns the IMAP connection

Returns:

  • (Net::IMAP)

    the IMAP connection



9
10
11
# File 'lib/mailman/receiver/imap.rb', line 9

def connection
  @connection
end

Instance Method Details

#connectObject

Connects to the IMAP server.



37
38
39
40
41
42
43
# File 'lib/mailman/receiver/imap.rb', line 37

def connect
  if @connection.nil? or @connection.disconnected?
    @connection = Net::IMAP.new(@server, port: @port, ssl: @ssl)
    @connection.(@username, @password)
  end
  @connection.select(@folder)
end

#disconnectObject

Disconnects from the IMAP server.



46
47
48
49
# File 'lib/mailman/receiver/imap.rb', line 46

def disconnect
  @connection.logout
  @connection.disconnected? ? true : @connection.disconnect rescue nil
end

#get_messagesObject

Iterates through new messages, passing them to the processor, and flagging them as done.



53
54
55
56
57
58
59
60
61
# File 'lib/mailman/receiver/imap.rb', line 53

def get_messages
  @connection.search(@filter).each do |message|
    body = @connection.fetch(message, "RFC822")[0].attr["RFC822"]
    @processor.process(body)
    @connection.store(message, "+FLAGS", @done_flags)
  end
  # Clears messages that have the Deleted flag set
  @connection.expunge
end