Skip to content

Commit

Permalink
Add a maximum for mails to keep
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Millien <marc.millien@gmail.com>
  • Loading branch information
marcmillien committed Apr 20, 2018
1 parent 30332a6 commit 74cba52
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/mail_catcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def log_exception(message, context, exception)
:http_ip => "127.0.0.1",
:http_port => "1080",
:http_path => "/",
:max_mail_to_keep => -1,
:verbose => false,
:daemon => !windows?,
:browse => false,
Expand Down Expand Up @@ -126,6 +127,10 @@ def parse! arguments=ARGV, defaults=@defaults
options[:http_port] = port
end

parser.on("--max_mail_to_keep NUM", Integer, "Set the maximum number of mails to keepr") do |num|
options[:max_mail_to_keep] = num
end

parser.on("--http-path PATH", String, "Add a prefix to all HTTP paths") do |path|
clean_path = Rack::Utils.clean_path_info("/#{path}")

Expand Down
9 changes: 9 additions & 0 deletions lib/mail_catcher/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,13 @@ def delete_message!(message_id)
@delete_messages_query.execute(message_id) and
@delete_message_parts_query.execute(message_id)
end

def delete_older_messages!(max_mail_to_keep = MailCatcher::options[:max_mail_to_keep])
return if max_mail_to_keep == -1
@delete_older_messages_query ||= db.prepare "DELETE FROM message WHERE id NOT IN (SELECT id FROM message ORDER BY created_at DESC LIMIT ?)"
@delete_older_message_parts_query ||= db.prepare "DELETE FROM message_part WHERE id NOT IN (SELECT id FROM message_part ORDER BY created_at DESC LIMIT ?)"

@delete_older_messages_query.execute(max_mail_to_keep) and
@delete_older_message_parts_query.execute(max_mail_to_keep)
end
end
1 change: 1 addition & 0 deletions lib/mail_catcher/smtp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def receive_data_chunk(lines)

def receive_message
MailCatcher::Mail.add_message current_message
MailCatcher::Mail.delete_older_messages!
puts "==> SMTP: Received message from '#{current_message[:sender]}' (#{current_message[:source].length} bytes)"
true
rescue => exception
Expand Down

0 comments on commit 74cba52

Please sign in to comment.