Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Maildir support #7

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 [email protected] Sat Feb 4 13:46:24 2023
>From [email protected] Sat Feb 4 13:46:24 2023
Return-Path: <[email protected]>
X-Original-To: [email protected]
Delivered-To: [email protected]
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: [email protected]
Delivered-To: [email protected]
Expand Down
Loading