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)

    if options is true, then an attempt will be made to use SSL (now TLS) to connect to the server.

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mailman/receiver/imap.rb', line 27

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
  @starttls   = options[:starttls] || false
  @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.



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mailman/receiver/imap.rb', line 45

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.



60
61
62
63
64
# File 'lib/mailman/receiver/imap.rb', line 60

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.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mailman/receiver/imap.rb', line 68

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)


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

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