Skip to content

Commit

Permalink
Merge pull request #375 from marcmillien/offer-the_possibility_to_kee…
Browse files Browse the repository at this point in the history
…p_a_finite_number_of_mails

Offer the possibility to keep a finite number of mails
  • Loading branch information
sj26 committed Nov 28, 2019
2 parents 4446d58 + 47913ac commit b4b0649
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
5 changes: 5 additions & 0 deletions lib/mail_catcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def log_exception(message, context, exception)
:http_ip => "127.0.0.1",
:http_port => "1080",
:http_path => "/",
:messages_limit => nil,
:verbose => false,
:daemon => !windows?,
:browse => false,
Expand Down Expand Up @@ -128,6 +129,10 @@ def parse! arguments=ARGV, defaults=@defaults
options[:http_port] = port
end

parser.on("--messages-limit COUNT", Integer, "Only keep up to COUNT most recent messages") do |count|
options[:messages_limit] = count
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
18 changes: 10 additions & 8 deletions lib/mail_catcher/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def db
charset TEXT,
body BLOB,
size INTEGER,
created_at DATETIME DEFAULT CURRENT_DATETIME
created_at DATETIME DEFAULT CURRENT_DATETIME,
FOREIGN KEY (message_id) REFERENCES message (id) ON DELETE CASCADE
)
SQL
end
Expand Down Expand Up @@ -153,16 +154,17 @@ def message_part_cid(message_id, cid)

def delete!
@delete_all_messages_query ||= db.prepare "DELETE FROM message"
@delete_all_message_parts_query ||= db.prepare "DELETE FROM message_part"

@delete_all_messages_query.execute and
@delete_all_message_parts_query.execute
@delete_all_messages_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.execute(message_id) and
@delete_message_parts_query.execute(message_id)
@delete_messages_query.execute(message_id)
end

def delete_older_messages!(count = MailCatcher.options[:messages_limit])
return if count.nil?
@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_messages_query.execute(count)
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 @@ -46,6 +46,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 b4b0649

Please sign in to comment.