Add --show-from-server option to parse the server from the Recieved header and display in a column, off by default

This commit is contained in:
Travis Burtrum 2016-08-11 08:23:33 -04:00
parent 0aa927ab50
commit 017e704791
4 changed files with 26 additions and 5 deletions

View File

@ -188,12 +188,14 @@ class MailCatcher
$("#messages tbody tr").show()
addMessage: (message) ->
$("<tr />").attr("data-message-id", message.id.toString())
row = $("<tr />").attr("data-message-id", message.id.toString())
.append($("<td/>").text(message.sender or "No sender").toggleClass("blank", !message.sender))
.append($("<td/>").text((message.recipients || []).join(", ") or "No receipients").toggleClass("blank", !message.recipients.length))
.append($("<td/>").text(message.subject or "No subject").toggleClass("blank", !message.subject))
.append($("<td/>").text(@formatDate(message.created_at)))
.prependTo($("#messages tbody"))
if 'from_server' of message
row = row.append($("<td/>").text(message.from_server or "Unknown").toggleClass("blank", !message.from_server))
row.prependTo($("#messages tbody"))
@updateMessagesCount()
scrollToRow: (row) ->

View File

@ -83,6 +83,7 @@ module MailCatcher extend self
:sqlite_db => ':memory:',
:delete_older_than => nil,
:keep_num_emails => nil,
:show_from_server => false,
}
def options
@ -93,6 +94,10 @@ module MailCatcher extend self
options[:quit]
end
def show_from_server?
options[:show_from_server]
end
def parse! arguments=ARGV, defaults=@defaults
@@defaults.dup.tap do |options|
OptionParser.new do |parser|
@ -123,6 +128,10 @@ module MailCatcher extend self
options[:quit] = false
end
parser.on("--show-from-server", "Show From Server column") do
options[:show_from_server] = true
end
parser.on("--sqlite-db PATH", "Set the path to the sqlite database, default in-memory only") do |db|
options[:sqlite_db] = db
end

View File

@ -13,6 +13,7 @@ module MailCatcher::Mail extend self
sender TEXT,
recipients TEXT,
subject TEXT,
from_server TEXT,
source BLOB,
size TEXT,
type TEXT,
@ -38,12 +39,13 @@ module MailCatcher::Mail extend self
end
def add_message(message)
@add_message_query ||= db.prepare("INSERT INTO message (sender, recipients, subject, source, type, size, created_at) VALUES (?, ?, ?, ?, ?, ?, datetime('now'))")
@add_message_query ||= db.prepare("INSERT INTO message (sender, recipients, subject, from_server, source, type, size, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, datetime('now'))")
mail = Mail.new(message[:source])
from_server = mail.received ? mail.received.value.sub(/^from\s+/, '').sub(/\s+.*$/, '') : nil
sender = (mail.from && !mail.from.empty?) ? mail.from : message[:sender]
recipients = (mail.to && !mail.to.empty?) ? mail.to : message[:recipients]
@add_message_query.execute(sender, JSON.generate(recipients), mail.subject, message[:source], mail.mime_type || "text/plain", message[:source].length)
@add_message_query.execute(sender, JSON.generate(recipients), mail.subject, from_server, 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?
@ -77,7 +79,9 @@ module MailCatcher::Mail extend self
end
def messages
@messages_query ||= db.prepare "SELECT id, sender, recipients, subject, size, created_at FROM message ORDER BY created_at, id ASC"
@messages_query ||= db.prepare MailCatcher.show_from_server? ?
"SELECT id, sender, recipients, subject, from_server, size, created_at FROM message ORDER BY created_at, id ASC" :
"SELECT id, sender, recipients, subject, size, created_at FROM message ORDER BY created_at, id ASC"
@messages_query.execute.map do |row|
Hash[row.fields.zip(row)].tap do |message|
message["recipients"] &&= JSON.parse(message["recipients"])
@ -89,6 +93,9 @@ module MailCatcher::Mail extend self
@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|
unless MailCatcher.show_from_server?
message.delete('from_server')
end
message["recipients"] &&= JSON.parse(message["recipients"])
end
end

View File

@ -27,6 +27,9 @@
<th>To</th>
<th>Subject</th>
<th>Received</th>
<% if MailCatcher.show_from_server? %>
<th>From Server</th>
<% end %>
</tr>
</thead>
<tbody></tbody>