Skip to content

Instantly share code, notes, and snippets.

@RomanHargrave
Created May 12, 2020 14:20
Show Gist options
  • Select an option

  • Save RomanHargrave/92d64ecdaad714cecdee1896ac400307 to your computer and use it in GitHub Desktop.

Select an option

Save RomanHargrave/92d64ecdaad714cecdee1896ac400307 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Mail::Mbox::MessageParser;
use MIME::Parser;
use Date::Parse;
my $input_file = undef;
my $out_dir = undef;
my $start_time = undef;
my $end_time = undef;
GetOptions(
"mbox=s" => \$input_file,
"out=s" => \$out_dir,
"start=s" => sub { $start_time = str2time($_[1]); },
"end=s" => sub { $end_time = str2time($_[1]); },
);
die "Input mailbox not specified" unless $input_file;
die "Output directory not specified" unless $out_dir;
mkdir $out_dir;
my $mbox_fh = new FileHandle($input_file);
my $mbox = new Mail::Mbox::MessageParser({
file_name => $input_file,
file_handle => $mbox_fh,
enable_cache => 0, # super buggy
enable_grep => 1,
});
my $parser = new MIME::Parser;
$parser->decode_headers(1);
while (!$mbox->end_of_file()) {
my $mime_raw = $mbox->read_next_email();
my $entity = $parser->parse_data($$mime_raw);
my $msg_id = $entity->head()->get('Message-ID', 0);
my $date = $entity->head()->get('Date', 0);
my $udate = str2time($date);
chomp $msg_id;
my $keep = 1;
if ($start_time && $end_time) {
$keep = 0;
$keep = ($udate >= $start_time) && ($udate <= $end_time);
} else {
$keep = 0;
$keep ||= $udate >= $start_time if $start_time;
$keep ||= $udate <= $end_time if $end_time;
}
if ($keep || !$date) {
print "Writing $msg_id to $out_dir/$msg_id.eml\n";
open my $out_file, ">$out_dir/$msg_id.eml";
print $out_file $$mime_raw;
close $out_file;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment