My style has evolved

This commit is contained in:
Samuel Cochran 2014-03-17 16:31:05 +11:00
parent 2056339bdb
commit 272b4fa855
8 changed files with 61 additions and 53 deletions

View File

@ -1,4 +1,5 @@
#!/usr/bin/env ruby
require 'mail_catcher'
MailCatcher.run!

View File

@ -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'

View File

@ -1,5 +1,7 @@
require 'eventmachine'
require "eventmachine"
module MailCatcher::Events
module MailCatcher
module Events
MessageAdded = EventMachine::Channel.new
end
end

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)