mailcatcher/lib/mail_catcher.rb

198 lines
4.8 KiB
Ruby
Raw Normal View History

2011-05-27 00:19:15 -04:00
require 'active_support/all'
2010-10-24 20:51:17 -04:00
require 'eventmachine'
2011-06-01 20:57:14 -04:00
require 'optparse'
2011-06-01 20:15:50 -04:00
require 'rbconfig'
2010-10-24 20:51:17 -04:00
require 'thin'
module MailCatcher
2011-05-27 00:19:15 -04:00
extend ActiveSupport::Autoload
2011-06-01 10:02:04 -04:00
2011-05-27 00:19:15 -04:00
autoload :Events
2011-06-09 23:24:08 -04:00
autoload :Growl
2011-05-27 00:19:15 -04:00
autoload :Mail
autoload :Smtp
autoload :Web
2011-06-01 10:02:04 -04:00
2011-06-09 23:24:08 -04:00
module_function
def which command
IO.popen(["which", command], "r", :err => :close).read.chomp.presence
end
2011-06-09 23:24:08 -04:00
def mac?
2011-10-10 07:28:49 -04:00
RbConfig::CONFIG['host_os'] =~ /darwin/
2011-06-09 23:24:08 -04:00
end
def windows?
2011-10-10 07:28:49 -04:00
RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
2011-06-09 23:24:08 -04:00
end
def macruby?
mac? and const_defined? :MACRUBY_VERSION
end
def growlnotify?
which "growlnotify"
2011-06-09 23:24:08 -04:00
end
def growl?
growlnotify?
2011-06-09 23:24:08 -04:00
end
def browse?
windows? or which "open"
end
def browse url
if windows?
system "start", "/b", url
elsif which "open"
system "open", url
end
2011-06-09 23:24:08 -04:00
end
@@defaults = {
:smtp_ip => '127.0.0.1',
:smtp_port => '1025',
:http_ip => '127.0.0.1',
:http_port => '1080',
:verbose => false,
2011-06-09 23:24:08 -04:00
:daemon => !windows?,
:growl => growlnotify?,
:browse => false,
}
2011-06-01 10:02:04 -04:00
2011-06-09 23:24:08 -04:00
def parse! arguments=ARGV, defaults=@@defaults
@@defaults.dup.tap do |options|
OptionParser.new do |parser|
2011-06-09 23:24:08 -04:00
parser.banner = "Usage: mailcatcher [options]"
2011-10-09 04:50:17 -04:00
parser.version = File.read(File.expand_path("../../VERSION", __FILE__))
2011-05-27 00:39:03 -04:00
2011-06-09 23:24:08 -04:00
parser.on("--ip IP", "Set the ip address of both servers") do |ip|
options[:smtp_ip] = options[:http_ip] = ip
end
2011-06-09 23:24:08 -04:00
parser.on("--smtp-ip IP", "Set the ip address of the smtp server") do |ip|
options[:smtp_ip] = ip
end
2011-06-09 23:24:08 -04:00
parser.on("--smtp-port PORT", Integer, "Set the port of the smtp server") do |port|
options[:smtp_port] = port
end
2011-06-09 23:24:08 -04:00
parser.on("--http-ip IP", "Set the ip address of the http server") do |ip|
options[:http_ip] = ip
end
2011-06-09 23:24:08 -04:00
parser.on("--http-port PORT", Integer, "Set the port address of the http server") do |port|
options[:http_port] = port
end
2011-06-09 23:24:08 -04:00
if mac?
parser.on("--[no-]growl", "Growl to the local machine when a message arrives") do |growl|
if growl and not growlnotify?
puts "You'll need to install growlnotify from the Growl installer."
puts
puts "See: http://growl.info/extras.php#growlnotify"
exit!
end
options[:growl] = growl
end
end
2011-06-01 10:02:04 -04:00
unless windows?
parser.on('-f', '--foreground', 'Run in the foreground') do
options[:daemon] = false
end
2011-05-27 00:39:03 -04:00
end
if browse?
parser.on('-b', '--browse', 'Open web browser') do
options[:browse] = true
end
end
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
2011-06-01 10:02:04 -04:00
2011-06-09 23:24:08 -04:00
def run! options=nil
# If we are passed options, fill in the blanks
options &&= @@defaults.merge options
# Otherwise, parse them from ARGV
options ||= parse!
2011-06-01 10:02:04 -04:00
puts "Starting MailCatcher"
2010-10-24 20:51:17 -04:00
Thin::Logging.silent = true
2011-06-01 10:02:04 -04:00
# One EventMachine loop...
EventMachine.run do
2011-06-09 23:24:08 -04:00
# Get our lion on if asked
MailCatcher::Growl.start if options[:growl]
smtp_url = "smtp://#{options[:smtp_ip]}:#{options[:smtp_port]}"
http_url = "http://#{options[:http_ip]}:#{options[:http_port]}"
2011-06-01 10:02:04 -04:00
# Set up an SMTP server to run within EventMachine
2011-05-31 13:35:57 -04:00
rescue_port options[:smtp_port] do
EventMachine.start_server options[:smtp_ip], options[:smtp_port], Smtp
puts "==> #{smtp_url}"
2011-05-31 13:35:57 -04:00
end
2011-06-01 10:02:04 -04:00
# Let Thin set itself up inside our EventMachine loop
# (Skinny/WebSockets just works on the inside)
2011-05-31 13:35:57 -04:00
rescue_port options[:http_port] do
Thin::Server.start options[:http_ip], options[:http_port], Web
puts "==> #{http_url}"
end
# Open the web browser before detatching console
if options[:browse]
EventMachine.next_tick do
browse http_url
end
2011-05-31 13:35:57 -04:00
end
2011-06-01 10:02:04 -04:00
# Daemonize, if we should, but only after the servers have started.
2011-05-31 13:35:57 -04:00
if options[:daemon]
EventMachine.next_tick do
2011-05-31 13:36:05 -04:00
puts "*** MailCatcher now runs as a daemon by default. Go to the web interface to quit."
2011-05-31 13:35:57 -04:00
Process.daemon
end
end
2010-10-24 20:51:17 -04:00
end
end
2011-06-01 10:02:04 -04:00
2011-06-09 23:24:08 -04:00
def quit!
2011-05-31 12:49:36 -04:00
EventMachine.next_tick { EventMachine.stop_event_loop }
end
2011-05-31 13:35:57 -04:00
2011-06-01 10:02:04 -04:00
protected
2011-06-09 23:24:08 -04:00
module_function
2011-05-31 13:35:57 -04:00
2011-06-09 23:24:08 -04:00
def rescue_port port
2011-05-31 13:35:57 -04:00
begin
yield
2011-06-01 10:02:04 -04:00
2011-05-31 13:35:57 -04:00
# XXX: EventMachine only spits out RuntimeError with a string description
rescue RuntimeError
if $!.to_s =~ /\bno acceptor\b/
puts "~~> ERROR: Something's using port #{port}. Are you already running MailCatcher?"
exit -1
else
raise
end
end
end
2010-10-24 20:51:17 -04:00
end