Xref: feenix.metronet.com alt.sources:1480
Path: feenix.metronet.com!news.utdallas.edu!wupost!darwin.sura.net!news-feed-1.peachnet.edu!concert!rutgers!rochester!cornell!uw-beaver!cthomas
From: cthomas@cs.washington.edu (Christopher Thomas)
Newsgroups: alt.sources
Subject: sortmail: sort Pine mail folders
Summary: perl script that sorts pine mail folder by date
Keywords: sort pine mail folders
Message-ID: <1993Jun26.002834.10494@beaver.cs.washington.edu>
Date: 26 Jun 93 00:28:34 GMT
Sender: news@beaver.cs.washington.edu (USENET News System)
Organization: University of Washington Computer Science
Lines: 123

I use the mail system Pine to handle my mail, because it is much friendlier
than the Unix mail system.  Anyway, I have a number of folders, each
of which holds my mail for different subjects.  In some of my folders,
I like to keep not only my incoming mail, but my outgoing mail on a subject,
so I can keep track of the e-mail dialogue.

But, I'm lazy, and tend to go through my inbox and sent-mail in one big sweep,
shuttling mail off to the proper folders.  The result is that the messages
back and forth don't end up in chronological order, and makes for confusing
reading.  I know that Pine has a sort facility, but you have to re-sort every
time you open a folder.

This Perl script goes through a Pine mail folder, and prints out the messages
sorted by date/time.  This way, the folder is permanently sorted.

Assuming you have a folder called foobar, you would use sortmail like this:

  mv foobar foobar.bak            (I don't trust software, not even my own)
  sortmail < foobar.bak > foobar

and voila, foobar is now sorted.

Notes:

- You'll probably have to change the path to perl, since it is installed
  in a weird place on our system.
- I made an educated guess as to what the message separator is for Pine
      ( /^From\s.*\sUkn\s(.*)/ )
  If someone knows better, then let me know too.
- I'm not familiar with the default unix mail system, but I'm sure that
  this could be adapted to work for that, too.

**********************************     __       __      ____  __
* Christopher Thomas             *    / / | |  / /   |	   / / /
* Univ. of WA Computer Science   *     /  | |   /    |\	 \/   / 
**********************************    /    /   / \   |    \  / \ 

---------------------------------------------------------------------
#!/uns/acm/bin/perl

$mos{'Jan'} = 1;
$mos{'Feb'} = 2;
$mos{'Mar'} = 3;
$mos{'Apr'} = 4;
$mos{'May'} = 5;
$mos{'Jun'} = 6;
$mos{'Jul'} = 7;
$mos{'Aug'} = 8;
$mos{'Sep'} = 9;
$mos{'Oct'} = 10;
$mos{'Nov'} = 11;
$mos{'Dec'} = 12;

# Tells if d1 is less than d2. dates in the form: MMM DD HH:MM:SS YYYY
sub datecmp {
  local ($mo1,$d1,$h1,$min1,$s1,$y1,$mo2,$d2,$h2,$min2,$s2,$y2, $c) ;

  ($mo1,$d1,$h1,$min1,$s1,$y1) =
     $_[0] =~
        /([a-zA-Z]{3})\s*([0-9]*)\s*([0-9]*):([0-9]*):([0-9]*)\s([0-9]*)/;
  ($mo2,$d2,$h2,$min2,$s2,$y2) =
     $_[1] =~ 
        /([a-zA-Z]{3})\s*([0-9]*)\s*([0-9]*):([0-9]*):([0-9]*)\s([0-9]*)/;

#  print "$_[0] $_[1]\n";
#  print "$y1 < $y2 = ", $y1<$y2, "\n";
  ($y1 < $y2) && return -1;
  ($y1 > $y2) && return 1;
  $mos{$mo1} < $mos{$mo2} && return -1;
  $mos{$mo1} > $mos{$mo2} && return 1;
  $d1 < $d2 && return -1;
  $d1 > $d2 && return 1;
  $h1 < $h2 && return -1;
  $h1 > $h2 && return 1;
  $min1 < $min2 && return -1;
  $min1 > $min2 && return 1;
  $s1 < $s2 && return -1;
  $s1 > $s2 && return 1;
  return 0;
}

sub mailsorter {
#  print "$a $b\n";
  &datecmp($dates[$a], $dates[$b]);
}

$lines = 0;
$mesgs = -1;
	
while ($text[$lines] = <>) {
  if ($text[$lines] =~ /^From\s.*\sUkn\s(.*)/) {
    if ($mesgs >= 0) {
      $ends[$mesgs] = $lines;
    }
    ++$mesgs;
    $begins[$mesgs] = $lines;
    $dates[$mesgs] = $1;
    $index[$mesgs] = $mesgs;
  }
  ++$lines;
}
$ends[$mesgs] = $lines;

@sortedi = sort mailsorter @index;

for ($i=0; $i <= $mesgs; ++$i) {
#  print "$dates[$i] ", &mailsorter($dates[$i],$dates[$i+1]), 
#        " $dates[$i+1]\n";
  for ($j=$begins[$sortedi[$i]]; $j<$ends[$sortedi[$i]]; ++$j) {
    print $text[$j];
  }
}


#  ($mo1,$d1,$h1,$min1,$s1,$y1) =
#     "Sep 24 11:34:36 1992" =~ 
#          /([a-zA-Z]{3})\s*([0-9]*)\s*([0-9]*):([0-9]*):([0-9]*)\s([0-9]*)/;
#  print "$mo1 $d1 $h1 $min1 $s1 $y1\n";
-- 
**********************************     __       __      ____  __
* Christopher Thomas             *    / / | |  / /   |	   / / /
* Univ. of WA Computer Science   *     /  | |   /    |\	 \/   / 
**********************************    /    /   / \   |    \  / \ 
