Skip to content

Commit

Permalink
Add Maildir support
Browse files Browse the repository at this point in the history
While here, remove some cruft that passed through previously.
  • Loading branch information
smortex committed Jan 31, 2024
1 parent a1d94d0 commit 33e0d81
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 12 deletions.
28 changes: 21 additions & 7 deletions exe/email-report-processor
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ require 'email_report_processor'
require 'optparse'

options = {
class: nil,
mbox: false,
os: {},
class: nil,
mbox: false,
maildir: false,
os: {},
}

OptionParser.new do |opts|
OptionParser.new do |opts| # rubocop:disable Metrics/BlockLength
opts.banner = "usage: #{$PROGRAM_NAME} [options] [file...]"
opts.separator("\nReport type selection:")
opts.on('--dmarc', 'Process DMARC Reports') do
Expand All @@ -35,16 +36,24 @@ OptionParser.new do |opts|
end

opts.separator("\nMiscellaneous options:")
opts.on('--mbox', 'Treat the provided file as a mailbox') do
opts.on('--mbox', 'Treat the provided files as Mbox') do
options[:mbox] = true
end
opts.on('--maildir', 'Treat the provided files as Maildir') do
options[:maildir] = true
end
end.parse!

if options[:class].nil?
warn('A processor type must be passed')
exit 1
end

if options[:mbox] && options[:maildir]
warn('The --mbox and --maildir options are mutualy exclusive')
exit 1
end

processor = options[:class].new(**options[:os])

if ARGV.empty?
Expand All @@ -55,8 +64,13 @@ else
if options[:mbox]
m = EmailReportProcessor::MailBox.new(filename)

while (message = m.next_message)
mail = Mail.new(message)
while (mail = m.next_message)
processor.process_message(mail)
end
elsif options[:maildir]
m = EmailReportProcessor::MailDir.new(filename)

while (mail = m.next_message)
processor.process_message(mail)
end
else
Expand Down
1 change: 0 additions & 1 deletion features/support/aruba.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
$server = WEBrick::HTTPServer.new(server_options)
Thread.new { $server.start }
Timeout.timeout(1) { sleep(0.1) until started }
puts 'go'
end

AfterAll do
Expand Down
1 change: 1 addition & 0 deletions lib/email_report_processor.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'email_report_processor/mailbox'
require 'email_report_processor/maildir'
require 'email_report_processor/dmarc_rua'
require 'email_report_processor/tlsrpt_rua'
2 changes: 1 addition & 1 deletion lib/email_report_processor/mailbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def next_message
if message.empty?
nil
else
message
Mail.new(message)
end
end
# rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
Expand Down
14 changes: 14 additions & 0 deletions lib/email_report_processor/maildir.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module EmailReportProcessor
class MailDir
def initialize(filename)
@messages = Dir["#{filename}/cur/*"]
end

def next_message
mail = @messages.pop
Mail.new(File.read(mail)) if mail
end
end
end
2 changes: 0 additions & 2 deletions spec/fixtures/sample-reports/dmarc/report.elm
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
From SRS0=XBdr=6A=google.com=noreply-dmarc-support@example.com Sat Feb 4 13:46:24 2023
>From SRS0=XBdr=6A=google.com=noreply-dmarc-support@example.com Sat Feb 4 13:46:24 2023
Return-Path: <SRS0=XBdr=6A=google.com=noreply-dmarc-support@example.com>
X-Original-To: romain@blogreen.org
Delivered-To: romain@blogreen.org
Expand Down
1 change: 0 additions & 1 deletion spec/fixtures/sample-reports/tlsrpt/report.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
From SRS0=pMZs=57=smtp-tls-reporting.bounces.google.com=3Nd3cYxoKAIgz03q1xA-4y51-5x4-3q1035uzss00sxq.o0y@blogreen.org Fri Feb 3 12:09:04 2023
Return-Path: <SRS0=pMZs=57=smtp-tls-reporting.bounces.google.com=3Nd3cYxoKAIgz03q1xA-4y51-5x4-3q1035uzss00sxq.o0y@blogreen.org>
X-Original-To: romain@blogreen.org
Delivered-To: romain@blogreen.org
Expand Down

0 comments on commit 33e0d81

Please sign in to comment.