2011-05-27 00:19:15 -04:00
|
|
|
require 'active_support/all'
|
2011-05-27 00:39:03 -04:00
|
|
|
require 'daemons'
|
2010-10-24 20:51:17 -04:00
|
|
|
require 'eventmachine'
|
|
|
|
require 'thin'
|
|
|
|
|
|
|
|
module MailCatcher
|
2011-05-27 00:19:15 -04:00
|
|
|
extend ActiveSupport::Autoload
|
|
|
|
|
|
|
|
autoload :Events
|
|
|
|
autoload :Mail
|
|
|
|
autoload :Smtp
|
|
|
|
autoload :Web
|
2010-10-24 20:51:17 -04:00
|
|
|
|
2011-05-27 00:19:04 -04:00
|
|
|
@@defaults = {
|
|
|
|
:smtp_ip => '127.0.0.1',
|
|
|
|
:smtp_port => '1025',
|
|
|
|
:http_ip => '127.0.0.1',
|
|
|
|
:http_port => '1080',
|
|
|
|
:verbose => false,
|
2011-05-27 00:39:03 -04:00
|
|
|
:daemon => false,
|
2011-05-27 00:19:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
def self.parse! arguments=ARGV, defaults=@@defaults
|
|
|
|
@@defaults.dup.tap do |options|
|
|
|
|
OptionParser.new do |parser|
|
|
|
|
parser.banner = 'Usage: mailcatcher [options]'
|
2011-05-27 00:39:03 -04:00
|
|
|
|
2011-05-27 00:23:46 -04:00
|
|
|
parser.on('--ip IP', 'Set the ip address of both servers') do |ip|
|
|
|
|
options[:smtp_ip] = options[:http_ip] = ip
|
|
|
|
end
|
2011-05-27 00:19:04 -04:00
|
|
|
|
|
|
|
parser.on('--smtp-ip IP', 'Set the ip address of the smtp server') do |ip|
|
|
|
|
options[:smtp_ip] = ip
|
|
|
|
end
|
|
|
|
|
|
|
|
parser.on('--smtp-port PORT', Integer, 'Set the port of the smtp server') do |port|
|
|
|
|
options[:smtp_port] = port
|
|
|
|
end
|
|
|
|
|
|
|
|
parser.on('--http-ip IP', 'Set the ip address of the http server') do |ip|
|
|
|
|
options[:http_ip] = ip
|
|
|
|
end
|
|
|
|
|
|
|
|
parser.on('--http-port PORT', Integer, 'Set the port address of the http server') do |port|
|
|
|
|
options[:http_port] = port
|
|
|
|
end
|
|
|
|
|
2011-05-27 00:39:03 -04:00
|
|
|
parser.on('-d', '--daemon', 'Run as a daemon') do
|
|
|
|
options[:daemon] = true
|
|
|
|
end
|
|
|
|
|
2011-05-27 00:19:04 -04:00
|
|
|
parser.on('-v', '--verbose', 'Be more verbose') do
|
|
|
|
options[:verbose] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
parser.on('-h', '--help', 'Display this help information') do
|
|
|
|
puts parser
|
|
|
|
exit!
|
|
|
|
end
|
|
|
|
end.parse!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.run! options=nil
|
|
|
|
# If we are passed options, fill in the blanks
|
|
|
|
options &&= @@defaults.merge options
|
|
|
|
# Otherwise, parse them from ARGV
|
|
|
|
options ||= parse!
|
2010-10-24 21:23:19 -04:00
|
|
|
|
2010-10-25 11:32:56 -04:00
|
|
|
puts "Starting MailCatcher"
|
|
|
|
puts "==> smtp://#{options[:smtp_ip]}:#{options[:smtp_port]}"
|
|
|
|
puts "==> http://#{options[:http_ip]}:#{options[:http_port]}"
|
|
|
|
|
2010-10-24 20:51:17 -04:00
|
|
|
Thin::Logging.silent = true
|
2011-05-27 00:39:03 -04:00
|
|
|
|
2011-05-30 03:37:13 -04:00
|
|
|
# One EventMachine loop...
|
2010-10-27 15:12:26 -04:00
|
|
|
EventMachine.run do
|
2011-05-30 03:37:13 -04:00
|
|
|
# Set up an SMTP server to run within EventMachine
|
2010-10-27 15:12:26 -04:00
|
|
|
EventMachine.start_server options[:smtp_ip], options[:smtp_port], Smtp
|
2011-05-27 00:39:03 -04:00
|
|
|
|
2011-05-30 03:37:13 -04:00
|
|
|
# Let Thin set itself up inside our EventMachine loop
|
|
|
|
# (Skinny/WebSockets just works on the inside)
|
2010-10-27 15:12:26 -04:00
|
|
|
Thin::Server.start options[:http_ip], options[:http_port], Web
|
2011-05-27 00:39:03 -04:00
|
|
|
|
2011-05-30 03:37:13 -04:00
|
|
|
# Daemonize, if we should, but only after the servers have started.
|
|
|
|
EventMachine.next_tick { Process.daemon } if options[:daemon]
|
2010-10-24 20:51:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|