From 33e0d8146e0e6ebd387cf9ce253414aeaa9f2414 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Wed, 31 Jan 2024 08:18:20 -1000 Subject: [PATCH] Add Maildir support While here, remove some cruft that passed through previously. --- exe/email-report-processor | 28 ++++++++++++++----- features/support/aruba.rb | 1 - lib/email_report_processor.rb | 1 + lib/email_report_processor/mailbox.rb | 2 +- lib/email_report_processor/maildir.rb | 14 ++++++++++ spec/fixtures/sample-reports/dmarc/report.elm | 2 -- .../fixtures/sample-reports/tlsrpt/report.elm | 1 - 7 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 lib/email_report_processor/maildir.rb diff --git a/exe/email-report-processor b/exe/email-report-processor index 93cc28d..8361bda 100755 --- a/exe/email-report-processor +++ b/exe/email-report-processor @@ -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 @@ -35,9 +36,12 @@ 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? @@ -45,6 +49,11 @@ if options[:class].nil? 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? @@ -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 diff --git a/features/support/aruba.rb b/features/support/aruba.rb index 38b18ae..921afda 100644 --- a/features/support/aruba.rb +++ b/features/support/aruba.rb @@ -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 diff --git a/lib/email_report_processor.rb b/lib/email_report_processor.rb index 31f1a77..41fb2d5 100644 --- a/lib/email_report_processor.rb +++ b/lib/email_report_processor.rb @@ -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' diff --git a/lib/email_report_processor/mailbox.rb b/lib/email_report_processor/mailbox.rb index 3128313..bb907ea 100644 --- a/lib/email_report_processor/mailbox.rb +++ b/lib/email_report_processor/mailbox.rb @@ -29,7 +29,7 @@ def next_message if message.empty? nil else - message + Mail.new(message) end end # rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity diff --git a/lib/email_report_processor/maildir.rb b/lib/email_report_processor/maildir.rb new file mode 100644 index 0000000..5f14d6d --- /dev/null +++ b/lib/email_report_processor/maildir.rb @@ -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 diff --git a/spec/fixtures/sample-reports/dmarc/report.elm b/spec/fixtures/sample-reports/dmarc/report.elm index 4b3db3d..e6e6f99 100644 --- a/spec/fixtures/sample-reports/dmarc/report.elm +++ b/spec/fixtures/sample-reports/dmarc/report.elm @@ -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: X-Original-To: romain@blogreen.org Delivered-To: romain@blogreen.org diff --git a/spec/fixtures/sample-reports/tlsrpt/report.elm b/spec/fixtures/sample-reports/tlsrpt/report.elm index cf7b138..30f2580 100644 --- a/spec/fixtures/sample-reports/tlsrpt/report.elm +++ b/spec/fixtures/sample-reports/tlsrpt/report.elm @@ -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: X-Original-To: romain@blogreen.org Delivered-To: romain@blogreen.org