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, Hash)

    if options is true, then an attempt will be made to use SSL (now TLS) to connect to the server. A Hash can be used to enable ssl and supply SSL context options.

  • :starttls (Boolean)

    use STARTTLS command to start TLS session.

  • :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



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(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]
  @ssl        = options[:ssl] || false
  @starttls   = options[:starttls] || false
  @port       = options[:port] || (@ssl ? 993 : 143)
  @folder     = options[:folder] || "INBOX"

  if @starttls && @ssl
    raise StandardError.new("either specify ssl or starttls, not both")
  end
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.



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.(@username, @password)
  end
  @connection.select(@folder)
rescue Net::IMAP::ByeResponseError, Net::IMAP::NoResponseError => e
  retry unless (tries -= 1).zero?
end

#disconnectObject

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_messagesObject

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 get_messages
  @connection.search(@filter).each do |message|
    body = @connection.fetch(message, "RFC822")[0].attr["RFC822"]
    begin
      @processor.process(body)
    rescue StandardError => error
      Mailman.logger.error "Error encountered processing message: #{message.inspect}\n #{error.class.to_s}: #{error.message}\n #{error.backtrace.join("\n")}"
      next
    end
    @connection.store(message, "+FLAGS", @done_flags)
  end
  # Clears messages that have the Deleted flag set
  @connection.expunge
end

#started?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/mailman/receiver/imap.rb', line 84

def started?
  not (!@connection.nil? && @connection.disconnected?)
end