Created
July 30, 2013 14:34
-
-
Save managementboy/6113465 to your computer and use it in GitHub Desktop.
mailsort.pl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/perl | |
use Mail::Util qw(read_mbox); | |
use Mail::Header; | |
%nmon = ( | |
'Jan' => 1, | |
'Feb' => 2, | |
'Mar' => 3, | |
'Apr' => 4, | |
'May' => 5, | |
'Jun' => 6, | |
'Jul' => 7, | |
'Aug' => 8, | |
'Sep' => 9, | |
'Oct' => 10, | |
'Nov' => 11, | |
'Dec' => 12, | |
); | |
die usage() unless @ARGV; | |
$filein = shift @ARGV; | |
@msgrefs = Mail::Util::read_mbox ($filein); | |
$i = 0; | |
foreach $msgref (@msgrefs) { | |
@tmphead = (); | |
$date = ''; | |
foreach $line (@{$msgref}) { | |
if (( $new_msg == 0 and $line =~ /^\s*$/ ) | |
or $body[$i] ) { | |
# start of body | |
$body[$i] .= $line; | |
} else { | |
push @tmphead, $line; | |
$new_msg = 0; | |
} | |
} | |
$head[$i] = new Mail::Header (\@tmphead, 'MailFrom' => 'KEEP'); | |
$new_msg = 1; | |
chomp($date = $head[$i]->get ('Date')); | |
#print "$i: no date from header obj\n" unless $date =~ /.+/; | |
$date[$i] = [ splitmaildate($date) ]; | |
#print "$i: from splitmaildate($date): @{$date[$i]}\n"; | |
$nums[$i] = $i; | |
$i++; | |
} | |
@nums = sort by_date @nums; | |
foreach $i (@nums) { | |
#print "\nSD $i: @{$date[$i]}\n\n"; | |
$head[$i]->print(); | |
print "\n", $body[$i]; | |
} | |
sub by_date { | |
my @adate = @{$date[$a]}; | |
my @bdate = @{$date[$b]}; | |
my ($afoo, $bfoo); | |
foreach $afoo (@adate) { | |
$bfoo = shift @bdate; | |
$o = ($afoo <=> $bfoo); | |
if ($o != 0) { return ($o); } | |
} | |
0; | |
} | |
sub splitmaildate { | |
my ($date) = @_; | |
if ($date =~ | |
/\s*(\w\w\w,? )?(\d\d?) (\w\w\w) (\d\d+) (\d\d?:\d\d?:\d\d?)\s*(.*)/) { | |
$day = $1; $ndate = $2; $mon = $3; | |
$year = $4; $time = $5; $foo = $6; | |
} elsif ($date =~ | |
/\s*(\w\w\w,? )?(\d\d?) (\w\w\w) (\d\d?:\d\d?:\d\d?) (\d\d+)\s*(.*)/) { | |
$day = $1; $ndate = $2; $mon = $3; | |
$year = $5; $time = $4; $foo = $6; | |
} | |
$mon = $nmon{$mon}; | |
if ($year < 100) { $year += 1900; } | |
$time =~ /(\d\d?):(\d\d?):(\d\d?)/; | |
$sec = $3; $min = $2; $hour = $1; | |
($year, $mon, $ndate, $hour, $min, $sec); | |
} | |
sub usage { "usage: $0 <mail-file-to-be-sorted>\n"; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment