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