From 272b4fa855e5ef5140a02c715ff1f1182955ab5d Mon Sep 17 00:00:00 2001 From: Samuel Cochran Date: Mon, 17 Mar 2014 16:31:05 +1100 Subject: [PATCH] My style has evolved --- bin/catchmail | 2 +- bin/mailcatcher | 1 + lib/mail_catcher.rb | 28 ++++++++++++++-------------- lib/mail_catcher/events.rb | 8 +++++--- lib/mail_catcher/growl.rb | 14 +++++++------- lib/mail_catcher/mail.rb | 28 ++++++++++++++-------------- lib/mail_catcher/smtp.rb | 4 +++- lib/mail_catcher/web.rb | 29 ++++++++++++++++------------- 8 files changed, 61 insertions(+), 53 deletions(-) diff --git a/bin/catchmail b/bin/catchmail index 39f5f7c..40ef230 100755 --- a/bin/catchmail +++ b/bin/catchmail @@ -32,7 +32,7 @@ OptionParser.new do |parser| parser.on('-f FROM', 'Set the sending address') do |from| options[:from] = from end - + parser.on('-oi', 'Ignored option -oi') do |ignored| end parser.on('-t', 'Ignored option -t') do |ignored| diff --git a/bin/mailcatcher b/bin/mailcatcher index 46ea7c5..bffc501 100755 --- a/bin/mailcatcher +++ b/bin/mailcatcher @@ -1,4 +1,5 @@ #!/usr/bin/env ruby require 'mail_catcher' + MailCatcher.run! diff --git a/lib/mail_catcher.rb b/lib/mail_catcher.rb index 69e8c9f..a3cba42 100644 --- a/lib/mail_catcher.rb +++ b/lib/mail_catcher.rb @@ -1,14 +1,20 @@ -require 'active_support/core_ext' -require 'eventmachine' -require 'open3' -require 'optparse' -require 'rbconfig' -require 'thin' +require "open3" +require "optparse" +require "rbconfig" -require 'mail_catcher/version' +require "active_support/core_ext" +require "eventmachine" +require "thin" + +require "mail_catcher/events" +require "mail_catcher/growl" +require "mail_catcher/mail" +require "mail_catcher/smtp" +require "mail_catcher/web" +require "mail_catcher/version" module MailCatcher extend self - def which command + def which(command) not windows? and Open3.popen3 'which', 'command' do |stdin, stdout, stderr| return stdout.read.chomp.presence end @@ -214,9 +220,3 @@ protected end end end - -require 'mail_catcher/events' -require 'mail_catcher/growl' -require 'mail_catcher/mail' -require 'mail_catcher/smtp' -require 'mail_catcher/web' diff --git a/lib/mail_catcher/events.rb b/lib/mail_catcher/events.rb index 5812685..3449f0d 100644 --- a/lib/mail_catcher/events.rb +++ b/lib/mail_catcher/events.rb @@ -1,5 +1,7 @@ -require 'eventmachine' +require "eventmachine" -module MailCatcher::Events - MessageAdded = EventMachine::Channel.new +module MailCatcher + module Events + MessageAdded = EventMachine::Channel.new + end end diff --git a/lib/mail_catcher/growl.rb b/lib/mail_catcher/growl.rb index 35ab418..e65139d 100644 --- a/lib/mail_catcher/growl.rb +++ b/lib/mail_catcher/growl.rb @@ -1,16 +1,16 @@ +require "mail_catcher/events" + module MailCatcher module Growl extend self def start - MailCatcher::Events::MessageAdded.subscribe MailCatcher::Growl.method :notify + MailCatcher::Events::MessageAdded.subscribe(method(:notify)) end def notify message - image_path = File.expand_path(File.join(__FILE__, '..', '..', '..', 'public', 'images', 'logo_large.png')) - system "growlnotify", "--image", image_path, "--name", "MailCatcher", "--message", "Message received:\n#{message["subject"]}" + system "growlnotify", + "--image", File.expand_path(File.join(__FILE__, "../public/images/logo_large.png")), + "--name", "MailCatcher", + "--message", "Message received:\n#{message["subject"]}" end - - # TODO: Native support on MacRuby with click backs - #def click - #end end end diff --git a/lib/mail_catcher/mail.rb b/lib/mail_catcher/mail.rb index ee4ccd3..609d557 100644 --- a/lib/mail_catcher/mail.rb +++ b/lib/mail_catcher/mail.rb @@ -1,12 +1,12 @@ -require 'active_support/json' -require 'mail' -require 'sqlite3' -require 'eventmachine' +require "active_support/json" +require "eventmachine" +require "mail" +require "sqlite3" module MailCatcher::Mail extend self def db @__db ||= begin - SQLite3::Database.new(':memory:', :type_translation => true).tap do |db| + SQLite3::Database.new(":memory:", :type_translation => true).tap do |db| db.execute(<<-SQL) CREATE TABLE message ( id INTEGER PRIMARY KEY ASC, @@ -41,7 +41,7 @@ module MailCatcher::Mail extend self @add_message_query ||= db.prepare("INSERT INTO message (sender, recipients, subject, source, type, size, created_at) VALUES (?, ?, ?, ?, ?, ?, datetime('now'))") mail = Mail.new(message[:source]) - @add_message_query.execute(message[:sender], message[:recipients].to_json, mail.subject, message[:source], mail.mime_type || 'text/plain', message[:source].length) + @add_message_query.execute(message[:sender], message[:recipients].to_json, mail.subject, message[:source], mail.mime_type || "text/plain", message[:source].length) message_id = db.last_insert_row_id parts = mail.all_parts parts = [mail] if parts.empty? @@ -49,7 +49,7 @@ module MailCatcher::Mail extend self body = part.body.to_s # Only parts have CIDs, not mail cid = part.cid if part.respond_to? :cid - add_message_part(message_id, cid, part.mime_type || 'text/plain', part.attachment? ? 1 : 0, part.filename, part.charset, body, body.length) + add_message_part(message_id, cid, part.mime_type || "text/plain", part.attachment? ? 1 : 0, part.filename, part.charset, body, body.length) end EventMachine.next_tick do @@ -87,7 +87,7 @@ module MailCatcher::Mail extend self def message_has_html?(id) @message_has_html_query ||= db.prepare "SELECT 1 FROM message_part WHERE message_id = ? AND is_attachment = 0 AND type IN ('application/xhtml+xml', 'text/html') LIMIT 1" - (!!@message_has_html_query.execute(id).next) || ['text/html', 'application/xhtml+xml'].include?(message(id)["type"]) + (!!@message_has_html_query.execute(id).next) || ["text/html", "application/xhtml+xml"].include?(message(id)["type"]) end def message_has_plain?(id) @@ -126,7 +126,7 @@ module MailCatcher::Mail extend self part ||= message_part_type(message_id, "application/xhtml+xml") part ||= begin message = message(message_id) - message if message.present? and ['text/html', 'application/xhtml+xml'].include? message["type"] + message if message.present? and ["text/html", "application/xhtml+xml"].include? message["type"] end end @@ -135,7 +135,7 @@ module MailCatcher::Mail extend self end def message_part_cid(message_id, cid) - @message_part_cid_query ||= db.prepare 'SELECT * FROM message_part WHERE message_id = ?' + @message_part_cid_query ||= db.prepare "SELECT * FROM message_part WHERE message_id = ?" @message_part_cid_query.execute(message_id).map do |row| Hash[row.fields.zip(row)] end.find do |part| @@ -144,16 +144,16 @@ module MailCatcher::Mail extend self end def delete! - @delete_messages_query ||= db.prepare 'DELETE FROM message' - @delete_message_parts_query ||= db.prepare 'DELETE FROM message_part' + @delete_messages_query ||= db.prepare "DELETE FROM message" + @delete_message_parts_query ||= db.prepare "DELETE FROM message_part" @delete_messages_query.execute and @delete_message_parts_query.execute end def delete_message!(message_id) - @delete_messages_query ||= db.prepare 'DELETE FROM message WHERE id = ?' - @delete_message_parts_query ||= db.prepare 'DELETE FROM message_part WHERE message_id = ?' + @delete_messages_query ||= db.prepare "DELETE FROM message WHERE id = ?" + @delete_message_parts_query ||= db.prepare "DELETE FROM message_part WHERE message_id = ?" @delete_messages_query.execute(message_id) and @delete_message_parts_query.execute(message_id) end diff --git a/lib/mail_catcher/smtp.rb b/lib/mail_catcher/smtp.rb index 9148365..04d01f2 100644 --- a/lib/mail_catcher/smtp.rb +++ b/lib/mail_catcher/smtp.rb @@ -1,4 +1,6 @@ -require 'eventmachine' +require "eventmachine" + +require "mail_catcher/mail" class MailCatcher::Smtp < EventMachine::Protocols::SmtpServer # We override EM's mail from processing to allow multiple mail-from commands diff --git a/lib/mail_catcher/web.rb b/lib/mail_catcher/web.rb index 9b2b4b9..ac231b8 100644 --- a/lib/mail_catcher/web.rb +++ b/lib/mail_catcher/web.rb @@ -1,9 +1,12 @@ -require 'sinatra' -require 'pathname' -require 'net/http' -require 'uri' +require "pathname" +require "net/http" +require "uri" -require 'skinny' +require "sinatra" +require "skinny" + +require "mail_catcher/events" +require "mail_catcher/mail" class Sinatra::Request include Skinny::Helpers @@ -13,11 +16,11 @@ class MailCatcher::Web < Sinatra::Base set :root, File.expand_path("#{__FILE__}/../../..") set :haml, :format => :html5 - get '/' do + get "/" do haml :index end - delete '/' do + delete "/" do if MailCatcher.quittable? MailCatcher.quit! status 204 @@ -26,7 +29,7 @@ class MailCatcher::Web < Sinatra::Base end end - get '/messages' do + get "/messages" do if request.websocket? request.websocket!( :on_start => proc do |websocket| @@ -40,12 +43,12 @@ class MailCatcher::Web < Sinatra::Base end end - delete '/messages' do + delete "/messages" do MailCatcher::Mail.delete! status 204 end - get '/messages/:id.json' do + get "/messages/:id.json" do id = params[:id].to_i if message = MailCatcher::Mail.message(id) message.merge({ @@ -55,7 +58,7 @@ class MailCatcher::Web < Sinatra::Base ("plain" if MailCatcher::Mail.message_has_plain? id) ].compact, "attachments" => MailCatcher::Mail.message_attachments(id).map do |attachment| - attachment.merge({"href" => "/messages/#{escape(id)}/parts/#{escape(attachment['cid'])}"}) + attachment.merge({"href" => "/messages/#{escape(id)}/parts/#{escape(attachment["cid"])}"}) end, }).to_json else @@ -63,7 +66,7 @@ class MailCatcher::Web < Sinatra::Base end end - get '/messages/:id.html' do + get "/messages/:id.html" do id = params[:id].to_i if part = MailCatcher::Mail.message_part_html(id) content_type part["type"], :charset => (part["charset"] || "utf8") @@ -133,7 +136,7 @@ class MailCatcher::Web < Sinatra::Base end end - delete '/messages/:id' do + delete "/messages/:id" do id = params[:id].to_i if message = MailCatcher::Mail.message(id) MailCatcher::Mail.delete_message!(id)