Class: Mailman::Application

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

Overview

The main application class. Pass a block to #new to create a new app.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = :default, &block) ⇒ Application

Creates a new router, and sets up any routes passed in the block.

Parameters:

  • options (Hash)

    the application options

  • block (Proc)

    a block with routes


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mailman/application.rb', line 33

def initialize(config=:default, &block)
  @router = Mailman::Router.new
  @config = select_config(config)
  @processor = MessageProcessor.new(:router => @router, :config => @config)

  if self.config.maildir
    require 'maildir'
    @maildir = Maildir.new(self.config.maildir)
  end

  instance_eval(&block) if block_given?
end

Instance Attribute Details

#configConfig (readonly)

Returns the apps's configuration.

Returns:

  • (Config)

    the apps's configuration


26
27
28
# File 'lib/mailman/application.rb', line 26

def config
  @config
end

#processorMessageProcessor (readonly)

Returns the app's message processor.

Returns:


23
24
25
# File 'lib/mailman/application.rb', line 23

def processor
  @processor
end

#routerRouter (readonly)

Returns the app's router.

Returns:

  • (Router)

    the app's router


20
21
22
# File 'lib/mailman/application.rb', line 20

def router
  @router
end

Class Method Details

.run(config = nil, &block) ⇒ Object


9
10
11
12
13
14
15
16
17
# File 'lib/mailman/application.rb', line 9

def self.run(config=nil, &block)
  if config
    app = new(config, &block)
  else
    app = new(&block)
  end
  app.run
  app
end

Instance Method Details

#default(&block) ⇒ Object

Sets the block to run if no routes match a message.


51
52
53
# File 'lib/mailman/application.rb', line 51

def default(&block)
  @router.default_block = block
end

#polling?Boolean

Returns:

  • (Boolean)

46
47
48
# File 'lib/mailman/application.rb', line 46

def polling?
  config.poll_interval > 0 && !@polling_interrupt
end

#runObject

Runs the application.


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/mailman/application.rb', line 56

def run
  Mailman.logger.info "Mailman v#{Mailman::VERSION} started"

  if config.rails_root
    rails_env = File.join(config.rails_root, 'config', 'environment.rb')
    if File.exist?(rails_env) && !(defined?(Rails) && Rails.env)
      Mailman.logger.info "Rails root found in #{config.rails_root}, requiring environment..."
      require rails_env
    end
  end

  if config.graceful_death
    # When user presses CTRL-C, finish processing current message before exiting
    Signal.trap("INT") { @polling_interrupt = true }
  end

  # STDIN
  if !IS_WINDOWS && !config.ignore_stdin && $stdin.fcntl(Fcntl::F_GETFL, 0) == 0
    Mailman.logger.debug "Processing message from STDIN."
    @processor.process($stdin.read)

  # IMAP
  elsif config.imap
    options = {:processor => @processor}.merge(config.imap)
    Mailman.logger.info "IMAP receiver enabled (#{options[:username]}@#{options[:server]})."
    polling_loop Receiver::IMAP.new(options)

  # POP3
  elsif config.pop3
    options = {:processor => @processor}.merge(config.pop3)
    Mailman.logger.info "POP3 receiver enabled (#{options[:username]}@#{options[:server]})."
    polling_loop Receiver::POP3.new(options)

  # Maildir
  elsif config.maildir

    Mailman.logger.info "Maildir receiver enabled (#{config.maildir})."

    Mailman.logger.debug "Processing new message queue..."
    @maildir.list(:new).each do |message|
      @processor.process_maildir_message(message)
    end

    if config.watch_maildir
      require 'listen'
      Mailman.logger.debug "Monitoring the Maildir for new messages..."
      base = Pathname.new(@maildir.path)

      callback = Proc.new do |modified, added, removed|
        added.each do |new_file|
          message = Maildir::Message.new(@maildir, Pathname.new(new_file).relative_path_from(base).to_s)
          @processor.process_maildir_message(message)
        end
      end

      @listener = Listen::Listener.new(File.join(@maildir.path, 'new'), &callback)
      @listener.start
      sleep
    end
  end
end