From 7701354e047b5263d5f7cf9d051b4f06c053a73a 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 | 26 ++++++++++++++----- features/support/aruba.rb | 1 - lib/email_report_processor.rb | 1 + lib/email_report_processor/base.rb | 4 +-- lib/email_report_processor/mailbox.rb | 2 +- spec/fixtures/sample-reports/dmarc/report.elm | 2 -- .../fixtures/sample-reports/tlsrpt/report.elm | 1 - 7 files changed, 24 insertions(+), 13 deletions(-) diff --git a/exe/email-report-processor b/exe/email-report-processor index 93cc28d..283f361 100755 --- a/exe/email-report-processor +++ b/exe/email-report-processor @@ -6,9 +6,10 @@ require 'email_report_processor' require 'optparse' options = { - class: nil, - mbox: false, - os: {}, + class: nil, + mbox: false, + maildir: false, + os: {}, } OptionParser.new do |opts| @@ -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/base.rb b/lib/email_report_processor/base.rb index 316491e..ace7ae0 100644 --- a/lib/email_report_processor/base.rb +++ b/lib/email_report_processor/base.rb @@ -27,8 +27,8 @@ def send_report(report) req.basic_auth(uri.user, uri.password) res = http.request(req) - puts res.inspect - puts res.body.inspect + #puts res.inspect + #puts res.body.inspect end def process_message(mail) 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/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