Class: Mailman::Application
- Inherits:
-
Object
- Object
- Mailman::Application
- 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
-
#config ⇒ Config
readonly
The apps’s configuration.
-
#processor ⇒ MessageProcessor
readonly
The app’s message processor.
-
#router ⇒ Router
readonly
The app’s router.
Class Method Summary collapse
Instance Method Summary collapse
-
#default(&block) ⇒ Object
Sets the block to run if no routes match a message.
-
#initialize(config = :default, &block) ⇒ Application
constructor
Creates a new router, and sets up any routes passed in the block.
- #polling? ⇒ Boolean
-
#run ⇒ Object
Runs the application.
Constructor Details
#initialize(config = :default, &block) ⇒ Application
Creates a new router, and sets up any routes passed in the block.
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
#config ⇒ Config (readonly)
Returns the apps’s configuration.
26 27 28 |
# File 'lib/mailman/application.rb', line 26 def config @config end |
#processor ⇒ MessageProcessor (readonly)
Returns the app’s message processor.
23 24 25 |
# File 'lib/mailman/application.rb', line 23 def processor @processor end |
#router ⇒ Router (readonly)
Returns 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
46 47 48 |
# File 'lib/mailman/application.rb', line 46 def polling? config.poll_interval > 0 && !@polling_interrupt end |
#run ⇒ Object
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 = {:processor => @processor}.merge(config.imap) Mailman.logger.info "IMAP receiver enabled (#{[:username]}@#{[:server]})." polling_loop Receiver::IMAP.new() # POP3 elsif config.pop3 = {:processor => @processor}.merge(config.pop3) Mailman.logger.info "POP3 receiver enabled (#{[:username]}@#{[:server]})." polling_loop Receiver::POP3.new() # Maildir elsif config.maildir Mailman.logger.info "Maildir receiver enabled (#{config.maildir})." Mailman.logger.debug "Processing new message queue..." @maildir.list(:new).each do || @processor.() 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| = Maildir::Message.new(@maildir, Pathname.new(new_file).relative_path_from(base).to_s) @processor.() end end @listener = Listen::Listener.new(File.join(@maildir.path, 'new'), &callback) @listener.start sleep end end end |