Class: Mailman::MessageProcessor

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

Overview

Turns a raw email into a Mail::Message and passes it to the router.

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ MessageProcessor

Returns a new instance of MessageProcessor.

Parameters:

  • options (Hash)

    the options to create the processor with

Options Hash (options):

  • :router (Router)

    the router to pass processed messages to



8
9
10
11
# File 'lib/mailman/message_processor.rb', line 8

def initialize(options)
  @router = options[:router]
  @config = options[:config]
end

Instance Method Details

#process(message) ⇒ Object

Converts a raw email into a Mail::Message instance, and passes it to the router.

Parameters:

  • message (String)

    the message to process



16
17
18
19
20
21
22
23
24
25
# File 'lib/mailman/message_processor.rb', line 16

def process(message)
  mail = Mail.new(message)
  from = mail.from.nil? ? "unknown" : mail.from.first
  Mailman.logger.info "Got new message from '#{from}' with subject '#{mail.subject}'."

  # Run any middlewares before routing the message
  @config.middleware.run(mail) do
    @router.route(mail)
  end
end

#process_maildir_message(message) ⇒ Object

Processes a Maildir::Message instance.



28
29
30
31
32
33
34
35
36
# File 'lib/mailman/message_processor.rb', line 28

def process_maildir_message(message)
  begin
    process(message.data)
    message.process # move message to cur
    message.seen!
  rescue StandardError => error
    Mailman.logger.error "Error encountered processing message: #{message.inspect}\n #{error.class.to_s}: #{error.message}\n #{error.backtrace.join("\n")}"
  end
end