mailcatcher-jruby 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'mail_catcher'
4
- MailCatcher.run!
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'mail_catcher'
4
+ MailCatcher.run!
@@ -1,8 +1,8 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'puma/cli'
4
-
5
- rack_path = File.expand_path('../../config.ru', __FILE__)
6
- default_params = ARGV.include?('-p') ? [rack_path] : [rack_path, '-p', '9090']
7
- cli = Puma::CLI.new (default_params + ARGV)
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'puma/cli'
4
+
5
+ rack_path = File.expand_path('../../config.ru', __FILE__)
6
+ default_params = ARGV.include?('-p') ? [rack_path] : [rack_path, '-p', '9090']
7
+ cli = Puma::CLI.new (default_params + ARGV)
8
8
  cli.run
@@ -0,0 +1,7 @@
1
+ require 'sinatra/base'
2
+
3
+ $LOAD_PATH.unshift '.', 'lib'
4
+
5
+ require_relative 'lib/mail_catcher'
6
+
7
+ run MailCatcher::Web
@@ -1,220 +1,220 @@
1
- require 'active_support/core_ext'
2
- require 'eventmachine'
3
- require 'open3'
4
- require 'optparse'
5
- require 'rbconfig'
6
- #require 'thin'
7
-
8
- require_relative 'mail_catcher/version'
9
-
10
- module MailCatcher extend self
11
- def which command
12
- not windows? and Open3.popen3 'which', 'command' do |stdin, stdout, stderr|
13
- return stdout.read.chomp.presence
14
- end
15
- end
16
-
17
- def mac?
18
- RbConfig::CONFIG['host_os'] =~ /darwin/
19
- end
20
-
21
- def windows?
22
- RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
23
- end
24
-
25
- def macruby?
26
- mac? and const_defined? :MACRUBY_VERSION
27
- end
28
-
29
- def growlnotify?
30
- which "growlnotify"
31
- end
32
-
33
- def growl?
34
- growlnotify?
35
- end
36
-
37
- def browse?
38
- windows? or which "open"
39
- end
40
-
41
- def browse url
42
- if windows?
43
- system "start", "/b", url
44
- elsif which "open"
45
- system "open", url
46
- end
47
- end
48
-
49
- @@defaults = {
50
- :smtp_ip => '0.0.0.0',
51
- :smtp_port => '9000',
52
- :http_ip => '127.0.0.1',
53
- :http_port => '1080',
54
- :verbose => false,
55
- :daemon => false,
56
- #:daemon => !windows?,
57
- :growl => growlnotify?,
58
- :browse => false,
59
- :quit => true,
60
- }
61
-
62
- def options
63
- @@options rescue @@defaults
64
- end
65
-
66
- def quittable?
67
- options[:quit]
68
- end
69
-
70
- def parse! arguments=ARGV, defaults=@defaults
71
- @@defaults.dup.tap do |options|
72
- OptionParser.new do |parser|
73
- parser.banner = "Usage: mailcatcher [options]"
74
- parser.version = VERSION
75
-
76
- parser.on("--ip IP", "Set the ip address of both servers") do |ip|
77
- options[:smtp_ip] = options[:http_ip] = ip
78
- end
79
-
80
- parser.on("--smtp-ip IP", "Set the ip address of the smtp server") do |ip|
81
- options[:smtp_ip] = ip
82
- end
83
-
84
- parser.on("--smtp-port PORT", Integer, "Set the port of the smtp server") do |port|
85
- options[:smtp_port] = port
86
- end
87
-
88
- parser.on("--http-ip IP", "Set the ip address of the http server") do |ip|
89
- options[:http_ip] = ip
90
- end
91
-
92
- parser.on("--http-port PORT", Integer, "Set the port address of the http server") do |port|
93
- options[:http_port] = port
94
- end
95
-
96
- parser.on("--no-quit", "Don't allow quitting the process") do
97
- options[:quit] = false
98
- end
99
-
100
- if mac?
101
- parser.on("--[no-]growl", "Growl to the local machine when a message arrives") do |growl|
102
- if growl and not growlnotify?
103
- puts "You'll need to install growlnotify from the Growl installer."
104
- puts
105
- puts "See: http://growl.info/extras.php#growlnotify"
106
- exit -2
107
- end
108
-
109
- options[:growl] = growl
110
- end
111
- end
112
-
113
- unless windows?
114
- parser.on('-f', '--foreground', 'Run in the foreground') do
115
- options[:daemon] = false
116
- end
117
- end
118
-
119
- if browse?
120
- parser.on('-b', '--browse', 'Open web browser') do
121
- options[:browse] = true
122
- end
123
- end
124
-
125
- parser.on('-v', '--verbose', 'Be more verbose') do
126
- options[:verbose] = true
127
- end
128
-
129
- parser.on('-h', '--help', 'Display this help information') do
130
- puts parser
131
- exit
132
- end
133
- end.parse!
134
- end
135
- end
136
-
137
- def run! options=nil
138
- # If we are passed options, fill in the blanks
139
- options &&= options.reverse_merge @@defaults
140
- # Otherwise, parse them from ARGV
141
- options ||= parse!
142
-
143
- # Stash them away for later
144
- @@options = options
145
-
146
- puts "Starting MailCatcher"
147
-
148
- #Thin::Logging.silent = true
149
-
150
- # One EventMachine loop...
151
- EventMachine.run do
152
- # Get our lion on if asked
153
- MailCatcher::Growl.start if options[:growl]
154
-
155
- smtp_url = "smtp://#{options[:smtp_ip]}:#{options[:smtp_port]}"
156
- http_url = "http://#{options[:http_ip]}:#{options[:http_port]}"
157
-
158
- # Set up an SMTP server to run within EventMachine
159
- rescue_port options[:smtp_port] do
160
- EventMachine.start_server options[:smtp_ip], options[:smtp_port], Smtp
161
- puts "==> #{smtp_url}"
162
- end
163
-
164
- # Let Thin set itself up inside our EventMachine loop
165
- # (Skinny/WebSockets just works on the inside)
166
- # rescue_port options[:http_port] do
167
- #Thin::Server.start options[:http_ip], options[:http_port], Web
168
- # puts "==> #{http_url}"
169
- # end
170
-
171
- # Open the web browser before detatching console
172
- if options[:browse]
173
- EventMachine.next_tick do
174
- browse http_url
175
- end
176
- end
177
-
178
- # Daemonize, if we should, but only after the servers have started.
179
- if options[:daemon]
180
- EventMachine.next_tick do
181
- if quittable?
182
- puts "*** MailCatcher runs as a daemon by default. Go to the web interface to quit."
183
- else
184
- puts "*** MailCatcher is now running as a daemon that cannot be quit."
185
- end
186
- #Process.daemon
187
- end
188
- end
189
- end
190
- end
191
-
192
- def quit!
193
- EventMachine.next_tick { EventMachine.stop_event_loop }
194
- end
195
-
196
- protected
197
-
198
- def rescue_port port
199
- begin
200
- yield
201
-
202
- # XXX: EventMachine only spits out RuntimeError with a string description
203
- rescue RuntimeError
204
- if $!.to_s =~ /\bno acceptor\b/
205
- puts "~~> ERROR: Something's using port #{port}. Are you already running MailCatcher?"
206
- exit -1
207
- else
208
- raise
209
- end
210
- end
211
- end
212
- end
213
-
214
- require_relative 'mail_catcher/events'
215
- require_relative 'mail_catcher/growl'
216
- require_relative 'mail_catcher/mail'
217
- require_relative 'mail_catcher/smtp'
218
- require_relative 'mail_catcher/web'
219
-
1
+ require 'active_support/core_ext'
2
+ require 'eventmachine'
3
+ require 'open3'
4
+ require 'optparse'
5
+ require 'rbconfig'
6
+ #require 'thin'
7
+
8
+ require_relative 'mail_catcher/version'
9
+
10
+ module MailCatcher extend self
11
+ def which command
12
+ not windows? and Open3.popen3 'which', 'command' do |stdin, stdout, stderr|
13
+ return stdout.read.chomp.presence
14
+ end
15
+ end
16
+
17
+ def mac?
18
+ RbConfig::CONFIG['host_os'] =~ /darwin/
19
+ end
20
+
21
+ def windows?
22
+ RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
23
+ end
24
+
25
+ def macruby?
26
+ mac? and const_defined? :MACRUBY_VERSION
27
+ end
28
+
29
+ def growlnotify?
30
+ which "growlnotify"
31
+ end
32
+
33
+ def growl?
34
+ growlnotify?
35
+ end
36
+
37
+ def browse?
38
+ windows? or which "open"
39
+ end
40
+
41
+ def browse url
42
+ if windows?
43
+ system "start", "/b", url
44
+ elsif which "open"
45
+ system "open", url
46
+ end
47
+ end
48
+
49
+ @@defaults = {
50
+ :smtp_ip => '0.0.0.0',
51
+ :smtp_port => '9000',
52
+ :http_ip => '127.0.0.1',
53
+ :http_port => '1080',
54
+ :verbose => false,
55
+ :daemon => false,
56
+ #:daemon => !windows?,
57
+ :growl => growlnotify?,
58
+ :browse => false,
59
+ :quit => true,
60
+ }
61
+
62
+ def options
63
+ @@options rescue @@defaults
64
+ end
65
+
66
+ def quittable?
67
+ options[:quit]
68
+ end
69
+
70
+ def parse! arguments=ARGV, defaults=@defaults
71
+ @@defaults.dup.tap do |options|
72
+ OptionParser.new do |parser|
73
+ parser.banner = "Usage: mailcatcher [options]"
74
+ parser.version = VERSION
75
+
76
+ parser.on("--ip IP", "Set the ip address of both servers") do |ip|
77
+ options[:smtp_ip] = options[:http_ip] = ip
78
+ end
79
+
80
+ parser.on("--smtp-ip IP", "Set the ip address of the smtp server") do |ip|
81
+ options[:smtp_ip] = ip
82
+ end
83
+
84
+ parser.on("--smtp-port PORT", Integer, "Set the port of the smtp server") do |port|
85
+ options[:smtp_port] = port
86
+ end
87
+
88
+ parser.on("--http-ip IP", "Set the ip address of the http server") do |ip|
89
+ options[:http_ip] = ip
90
+ end
91
+
92
+ parser.on("--http-port PORT", Integer, "Set the port address of the http server") do |port|
93
+ options[:http_port] = port
94
+ end
95
+
96
+ parser.on("--no-quit", "Don't allow quitting the process") do
97
+ options[:quit] = false
98
+ end
99
+
100
+ if mac?
101
+ parser.on("--[no-]growl", "Growl to the local machine when a message arrives") do |growl|
102
+ if growl and not growlnotify?
103
+ puts "You'll need to install growlnotify from the Growl installer."
104
+ puts
105
+ puts "See: http://growl.info/extras.php#growlnotify"
106
+ exit -2
107
+ end
108
+
109
+ options[:growl] = growl
110
+ end
111
+ end
112
+
113
+ unless windows?
114
+ parser.on('-f', '--foreground', 'Run in the foreground') do
115
+ options[:daemon] = false
116
+ end
117
+ end
118
+
119
+ if browse?
120
+ parser.on('-b', '--browse', 'Open web browser') do
121
+ options[:browse] = true
122
+ end
123
+ end
124
+
125
+ parser.on('-v', '--verbose', 'Be more verbose') do
126
+ options[:verbose] = true
127
+ end
128
+
129
+ parser.on('-h', '--help', 'Display this help information') do
130
+ puts parser
131
+ exit
132
+ end
133
+ end.parse!
134
+ end
135
+ end
136
+
137
+ def run! options=nil
138
+ # If we are passed options, fill in the blanks
139
+ options &&= options.reverse_merge @@defaults
140
+ # Otherwise, parse them from ARGV
141
+ options ||= parse!
142
+
143
+ # Stash them away for later
144
+ @@options = options
145
+
146
+ puts "Starting MailCatcher"
147
+
148
+ #Thin::Logging.silent = true
149
+
150
+ # One EventMachine loop...
151
+ EventMachine.run do
152
+ # Get our lion on if asked
153
+ MailCatcher::Growl.start if options[:growl]
154
+
155
+ smtp_url = "smtp://#{options[:smtp_ip]}:#{options[:smtp_port]}"
156
+ http_url = "http://#{options[:http_ip]}:#{options[:http_port]}"
157
+
158
+ # Set up an SMTP server to run within EventMachine
159
+ rescue_port options[:smtp_port] do
160
+ EventMachine.start_server options[:smtp_ip], options[:smtp_port], Smtp
161
+ puts "==> #{smtp_url}"
162
+ end
163
+
164
+ # Let Thin set itself up inside our EventMachine loop
165
+ # (Skinny/WebSockets just works on the inside)
166
+ # rescue_port options[:http_port] do
167
+ #Thin::Server.start options[:http_ip], options[:http_port], Web
168
+ # puts "==> #{http_url}"
169
+ # end
170
+
171
+ # Open the web browser before detatching console
172
+ if options[:browse]
173
+ EventMachine.next_tick do
174
+ browse http_url
175
+ end
176
+ end
177
+
178
+ # Daemonize, if we should, but only after the servers have started.
179
+ if options[:daemon]
180
+ EventMachine.next_tick do
181
+ if quittable?
182
+ puts "*** MailCatcher runs as a daemon by default. Go to the web interface to quit."
183
+ else
184
+ puts "*** MailCatcher is now running as a daemon that cannot be quit."
185
+ end
186
+ #Process.daemon
187
+ end
188
+ end
189
+ end
190
+ end
191
+
192
+ def quit!
193
+ EventMachine.next_tick { EventMachine.stop_event_loop }
194
+ end
195
+
196
+ protected
197
+
198
+ def rescue_port port
199
+ begin
200
+ yield
201
+
202
+ # XXX: EventMachine only spits out RuntimeError with a string description
203
+ rescue RuntimeError
204
+ if $!.to_s =~ /\bno acceptor\b/
205
+ puts "~~> ERROR: Something's using port #{port}. Are you already running MailCatcher?"
206
+ exit -1
207
+ else
208
+ raise
209
+ end
210
+ end
211
+ end
212
+ end
213
+
214
+ require_relative 'mail_catcher/events'
215
+ require_relative 'mail_catcher/growl'
216
+ require_relative 'mail_catcher/mail'
217
+ require_relative 'mail_catcher/smtp'
218
+ require_relative 'mail_catcher/web'
219
+
220
220
  # MailCatcher.run!