Skip to content

Commit

Permalink
Whitespace pedantry.
Browse files Browse the repository at this point in the history
  • Loading branch information
sj26 committed Jun 10, 2011
1 parent 49d42da commit 4b68915
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 40 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ MailCatcher runs a super simple SMTP server which catches any message sent to it
* Download original email to view in your native mail client(s).
* Command line options to override the default SMTP/HTTP IP and port settings.
* Mail appears instantly if your browser supports [WebSockets][websockets], otherwise updates every thirty seconds.
* Growl notifications when you receive a new message.
* Runs as a daemon run in the background.
* Sendmail-analogue command, `catchmail`, makes [using mailcatcher from PHP][withphp] a lot easier.
* Written super-simply in EventMachine, easy to dig in and change.
Expand Down
38 changes: 19 additions & 19 deletions lib/mail_catcher/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ def db
end
end
end

def add_message(message)
@@add_message_query ||= db.prepare("INSERT INTO message (sender, recipients, subject, source, type, size, created_at) VALUES (?, ?, ?, ?, ?, ?, datetime('now'))")

mail = Mail.new(message[:source])
result = @@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
Expand All @@ -51,7 +51,7 @@ def add_message(message)
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)
end

EventMachine.next_tick do
message = MailCatcher::Mail.message message_id
MailCatcher::Events::MessageAdded.push message
Expand All @@ -62,12 +62,12 @@ def add_message_part(*args)
@@add_message_part_query ||= db.prepare "INSERT INTO message_part (message_id, cid, type, is_attachment, filename, charset, body, size, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, datetime('now'))"
@@add_message_part_query.execute(*args)
end

def latest_created_at
@@latest_created_at_query ||= db.prepare "SELECT created_at FROM message ORDER BY created_at DESC LIMIT 1"
@@latest_created_at_query.execute.next
end

def messages
@@messages_query ||= db.prepare "SELECT id, sender, recipients, subject, size, created_at FROM message ORDER BY created_at ASC"
@@messages_query.execute.map do |row|
Expand All @@ -76,64 +76,64 @@ def messages
end
end
end

def message(id)
@@message_query ||= db.prepare "SELECT * FROM message WHERE id = ? LIMIT 1"
row = @@message_query.execute(id).next
row && Hash[row.fields.zip(row)].tap do |message|
message["recipients"] &&= ActiveSupport::JSON.decode message["recipients"]
end
end

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"])
end

def message_has_plain?(id)
@@message_has_plain_query ||= db.prepare "SELECT 1 FROM message_part WHERE message_id = ? AND is_attachment = 0 AND type = 'text/plain' LIMIT 1"
(!!@@message_has_plain_query.execute(id).next) || message(id)["type"] == "text/plain"
end

def message_parts(id)
@@message_parts_query ||= db.prepare "SELECT cid, type, filename, size FROM message_part WHERE message_id = ? ORDER BY filename ASC"
@@message_parts_query.execute(id).map do |row|
Hash[row.fields.zip(row)]
end
end

def message_attachments(id)
@@message_parts_query ||= db.prepare "SELECT cid, type, filename, size FROM message_part WHERE message_id = ? AND is_attachment = 1 ORDER BY filename ASC"
@@message_parts_query.execute(id).map do |row|
Hash[row.fields.zip(row)]
end
end

def message_part(message_id, part_id)
@@message_part_query ||= db.prepare "SELECT * FROM message_part WHERE message_id = ? AND id = ? LIMIT 1"
row = @@message_part_query.execute(message_id, part_id).next
row && Hash[row.fields.zip(row)]
end

def message_part_type(message_id, part_type)
@@message_part_type_query ||= db.prepare "SELECT * FROM message_part WHERE message_id = ? AND type = ? AND is_attachment = 0 LIMIT 1"
row = @@message_part_type_query.execute(message_id, part_type).next
row && Hash[row.fields.zip(row)]
end

def message_part_html(message_id)
part = message_part_type(message_id, "text/html")
part ||= message_part_type(message_id, "application/xhtml+xml")
part ||= begin
message = message(message_id)
message if ['text/html', 'application/xhtml+xml'].include? message["type"]
message if message.present? and ['text/html', 'application/xhtml+xml'].include? message["type"]
end
end

def message_part_plain(message_id)
message_part_type message_id, "text/plain"
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.execute(message_id).map do |row|
Expand All @@ -142,13 +142,13 @@ def message_part_cid(message_id, cid)
part["cid"] == cid
end
end

def delete!
@@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
end
end
end
10 changes: 5 additions & 5 deletions lib/mail_catcher/smtp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ class Smtp < EventMachine::Protocols::SmtpServer
def current_message
@current_message ||= {}
end

def receive_reset
@current_message = nil
true
end

def receive_sender(sender)
current_message[:sender] = sender
true
end

def receive_recipient(recipient)
current_message[:recipients] ||= []
current_message[:recipients] << recipient
true
end

def receive_data_chunk(lines)
current_message[:source] ||= ""
current_message[:source] += lines.join("\n")
Expand All @@ -45,4 +45,4 @@ def receive_message
@current_message = nil
end
end
end
end
32 changes: 16 additions & 16 deletions lib/mail_catcher/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ module MailCatcher
class Web < Sinatra::Base
set :root, Pathname.new(__FILE__).dirname.parent.parent
set :haml, :format => :html5

get '/' do
haml :index
end

delete '/' do
MailCatcher.quit!
status 204
end

get '/messages' do
if request.websocket?
request.websocket!(
Expand All @@ -36,12 +36,12 @@ class Web < Sinatra::Base
MailCatcher::Mail.messages.to_json
end
end

delete '/messages' do
MailCatcher::Mail.delete!
status 204
end

get '/messages/:id.json' do
id = params[:id].to_i
if message = MailCatcher::Mail.message(id)
Expand All @@ -59,26 +59,26 @@ class Web < Sinatra::Base
not_found
end
end

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

body = part["body"]

# Rewrite body to link to embedded attachments served by cid
body.gsub! /cid:([^'"> ]+)/, "#{id}/\\1"

# Rewrite body to open links in a new window
body.gsub! /<a\s+/, '<a target="_blank" '

body
else
not_found
end
end

get "/messages/:id.plain" do
id = params[:id].to_i
if part = MailCatcher::Mail.message_part_plain(id)
Expand All @@ -88,7 +88,7 @@ class Web < Sinatra::Base
not_found
end
end

get "/messages/:id.source" do
id = params[:id].to_i
if message = MailCatcher::Mail.message(id)
Expand All @@ -98,7 +98,7 @@ class Web < Sinatra::Base
not_found
end
end

get "/messages/:id.eml" do
id = params[:id].to_i
if message = MailCatcher::Mail.message(id)
Expand All @@ -108,7 +108,7 @@ class Web < Sinatra::Base
not_found
end
end

get "/messages/:id/:cid" do
id = params[:id].to_i
if part = MailCatcher::Mail.message_part_cid(id, params[:cid])
Expand All @@ -119,9 +119,9 @@ class Web < Sinatra::Base
not_found
end
end

not_found do
"<html><body><h1>No Dice</h1><p>The message you were looking for does not exist, or doesn't have content of this type.</p></body></html>"
end
end
end
end

0 comments on commit 4b68915

Please sign in to comment.