chiark / gitweb /
pull-mail: New script to encapsulate what my crontab needs to do to pull
[bin.git] / mailsort
1 #! /usr/bin/perl -w
2 use strict;
3 use Date::Parse;
4
5 my $message;
6 my ($fromdate, $date);
7 my @messages;
8 my %dates;
9
10 while (<>)
11 {
12         chomp;
13         if (/^From /)
14         {
15                 if (defined $message)
16                 {
17                         push @messages, ($message);
18                         $dates{$message} = defined $date ? $date : $fromdate;
19                 }
20                 $message = "$_\n";
21                 /^From [^ ]* (.*)/;
22                 $fromdate = $1;
23                 $fromdate =~ s/ +/ /g;
24                 $fromdate = str2time $fromdate;
25                 $date = undef;
26         }
27         elsif (!(defined $date) && /^Date: (.*)/)
28         {
29                 $message .= "$_\n";
30                 $date = str2time $1;
31         }
32         else
33         {
34                 $message .= "$_\n";
35         }
36 }
37
38 if (defined $message)
39 {
40         push @messages, ($message);
41         $dates{$message} = defined $date ? $date : $fromdate;
42 }
43
44 print (sort { $dates{$a} <=> $dates{$b} } @messages);
45