Module: MailCatcher
- Extended by:
- MailCatcher
- Included in:
- MailCatcher
- Defined in:
- lib/mail_catcher.rb,
lib/mail_catcher/bus.rb,
lib/mail_catcher/web.rb,
lib/mail_catcher/version.rb,
lib/mail_catcher/integrations.rb,
lib/mail_catcher/integrations/mcp_tools.rb,
lib/mail_catcher/integrations/mcp_server.rb,
lib/mail_catcher/web/application.rb
Defined Under Namespace
Modules: Integrations, Mail, Web Classes: Smtp, SmtpTls
Constant Summary collapse
- Bus =
EventMachine::Channel.new
- VERSION =
'1.5.7'
Instance Method Summary collapse
- #browsable? ⇒ Boolean
- #browse(url) ⇒ Object
- #development? ⇒ Boolean
- #env ⇒ Object
- #http_server ⇒ Object
- #log_exception(message, context, exception) ⇒ Object
- #options ⇒ Object
- #parse!(arguments = ARGV, defaults = @defaults) ⇒ Object
- #quit! ⇒ Object
- #quittable? ⇒ Boolean
- #run!(options = nil) ⇒ Object
- #which?(command) ⇒ Boolean
- #windows? ⇒ Boolean
Instance Method Details
#browsable? ⇒ Boolean
51 52 53 |
# File 'lib/mail_catcher.rb', line 51 def browsable? windows? or which? 'open' end |
#browse(url) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/mail_catcher.rb', line 55 def browse(url) if windows? system 'start', '/b', url elsif which? 'open' system 'open', url end end |
#development? ⇒ Boolean
37 38 39 |
# File 'lib/mail_catcher.rb', line 37 def development? env == 'development' end |
#env ⇒ Object
33 34 35 |
# File 'lib/mail_catcher.rb', line 33 def env ENV.fetch('MAILCATCHER_ENV', 'production') end |
#http_server ⇒ Object
107 108 109 |
# File 'lib/mail_catcher.rb', line 107 def http_server @http_server end |
#log_exception(message, context, exception) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/mail_catcher.rb', line 63 def log_exception(, context, exception) gems_paths = (Gem.path | [Gem.default_dir]).map { |path| Regexp.escape(path) } gems_regexp = %r{(?:#{gems_paths.join('|')})/gems/([^/]+)-([\w.]+)/(.*)} gems_replace = '\1 (\2) \3' @logger.error("#{}: #{context.inspect}") @logger.error("Exception: #{exception}") backtrace = exception.backtrace&.map { |line| line.sub(gems_regexp, gems_replace) } backtrace&.each { |line| @logger.error(" #{line}") } @logger.error('Please submit this as an issue at https://github.com/spaquet/mailcatcher') end |
#options ⇒ Object
103 104 105 |
# File 'lib/mail_catcher.rb', line 103 def @options end |
#parse!(arguments = ARGV, defaults = @defaults) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/mail_catcher.rb', line 115 def parse!(arguments = ARGV, defaults = @defaults) @defaults.dup.tap do || OptionParser.new do |parser| parser. = 'Usage: mailcatcher [options]' parser.version = VERSION parser.separator '' parser.separator "MailCatcher NG v#{VERSION}" parser.separator '' parser.on('--ip IP', 'Set the ip address of both servers') do |ip| [:smtp_ip] = [:http_ip] = ip end parser.on('--smtp-ip IP', 'Set the ip address of the smtp server') do |ip| [:smtp_ip] = ip end parser.on('--smtp-port PORT', Integer, 'Set the port of the smtp server') do |port| [:smtp_port] = port end parser.on('--smtp-ssl', 'Enable SSL/TLS support for SMTP') do [:smtp_ssl] = true end parser.on('--smtp-ssl-cert PATH', 'Path to SSL certificate file (required with --smtp-ssl)') do |path| [:smtp_ssl_cert] = path end parser.on('--smtp-ssl-key PATH', 'Path to SSL private key file (required with --smtp-ssl)') do |path| [:smtp_ssl_key] = path end parser.on('--smtp-ssl-verify-peer', 'Verify client SSL certificates') do [:smtp_ssl_verify_peer] = true end parser.on('--smtps-port PORT', Integer, 'Set the port for direct TLS SMTP server (default: 1465)') do |port| [:smtps_port] = port end parser.on('--http-ip IP', 'Set the ip address of the http server') do |ip| [:http_ip] = ip end parser.on('--http-port PORT', Integer, 'Set the port address of the http server') do |port| [:http_port] = port end parser.on('--messages-limit COUNT', Integer, 'Only keep up to COUNT most recent messages') do |count| [:messages_limit] = count end parser.on('--persistence', 'Store messages in a persistent SQLite database file') do [:persistence] = true end parser.on('--http-path PATH', String, 'Add a prefix to all HTTP paths') do |path| clean_path = Rack::Utils.clean_path_info("/#{path}") [:http_path] = clean_path end parser.on('--forward-smtp-host HOST', 'SMTP server for forwarding messages') do |host| [:forward_smtp_host] = host end parser.on('--forward-smtp-port PORT', Integer, 'SMTP port for forwarding messages') do |port| [:forward_smtp_port] = port end parser.on('--forward-smtp-user USER', 'SMTP username for forwarding messages') do |user| [:forward_smtp_user] = user end parser.on('--forward-smtp-password PASSWORD', 'SMTP password for forwarding messages') do |password| [:forward_smtp_password] = password end parser.on('--[no-]forward-smtp-tls', 'Enable/disable TLS for forwarding SMTP (default: enabled)') do |tls| [:forward_smtp_tls] = tls end parser.on('--no-quit', "Don't allow quitting the process") do [:quit] = false end parser.on('--mcp', 'Enable MCP server for Claude integration') do [:mcp_enabled] = true end parser.on('--plugin', 'Enable Claude Plugin endpoints') do [:plugin_enabled] = true end unless windows? parser.on('-f', '--foreground', 'Run in the foreground') do [:daemon] = false end end if browsable? parser.on('-b', '--browse', 'Open web browser') do [:browse] = true end end parser.on('-v', '--verbose', 'Be more verbose') do [:verbose] = true end parser.on_tail('-h', '--help', 'Display this help information') do puts parser exit end parser.on_tail('--version', 'Display the current version') do puts "MailCatcher v#{VERSION}" exit end end.parse! end end |
#quit! ⇒ Object
320 321 322 323 324 |
# File 'lib/mail_catcher.rb', line 320 def quit! MailCatcher::Bus.push(type: 'quit') EventMachine.next_tick { EventMachine.stop_event_loop } end |
#quittable? ⇒ Boolean
111 112 113 |
# File 'lib/mail_catcher.rb', line 111 def quittable? [:quit] end |
#run!(options = nil) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/mail_catcher.rb', line 240 def run!( = nil) # If we are passed options, fill in the blanks &&= @defaults.merge # Otherwise, parse them from ARGV ||= parse! # Stash them away for later @options = # Validate SSL configuration if enabled validate_ssl_config! # If we're running in the foreground sync the output. $stdout.sync = $stderr.sync = true unless [:daemon] @logger.info("Starting MailCatcher NG v#{VERSION}") Thin::Logging.debug = development? Thin::Logging.silent = !development? @logger.level = development? ? Logger::DEBUG : Logger::INFO # Configure SSL/TLS if enabled configure_smtp_ssl! # One EventMachine loop... EventMachine.run do # Set up an SMTP server to run within EventMachine rescue_port [:smtp_port] do EventMachine.start_server [:smtp_ip], [:smtp_port], Smtp @logger.info("==> #{smtp_url}") end # Set up direct TLS (SMTPS) server if SSL is enabled if [:smtp_ssl] rescue_port [:smtps_port] do EventMachine.start_server [:smtp_ip], [:smtps_port], SmtpTls @logger.info("==> #{smtps_url}") end end # Let Thin set itself up inside our EventMachine loop # Faye connections are hijacked but continue to be supervised by thin rescue_port [:http_port] do @http_server = Thin::Server.new([:http_ip], [:http_port], Web, signals: false) @http_server.start @logger.info("==> #{http_url}") end # Start integrations (MCP, Plugins, etc.) if [:mcp_enabled] || [:plugin_enabled] Integrations.start() end # Make sure we quit nicely when asked # We need to handle outside the trap context, hence the timer trap('INT') { EM.add_timer(0) { quit! } } trap('TERM') { EM.add_timer(0) { quit! } } trap('QUIT') { EM.add_timer(0) { quit! } } unless windows? # Open the web browser before detaching console if [:browse] EventMachine.next_tick do browse http_url end end # Daemonize, if we should, but only after the servers have started. if [:daemon] EventMachine.next_tick do if quittable? @logger.info('MailCatcher runs as a daemon by default. Go to the web interface to quit.') else @logger.info('MailCatcher is now running as a daemon that cannot be quit.') end Process.daemon end end end end |
#which?(command) ⇒ Boolean
41 42 43 44 45 |
# File 'lib/mail_catcher.rb', line 41 def which?(command) ENV['PATH'].split(File::PATH_SEPARATOR).any? do |directory| File.executable?(File.join(directory, command.to_s)) end end |
#windows? ⇒ Boolean
47 48 49 |
# File 'lib/mail_catcher.rb', line 47 def windows? RbConfig::CONFIG['host_os'].match?(/mswin|mingw/) end |