From: Emmanuel Lacour Date: Sun, 31 Oct 2004 18:47:00 +0000 (+0100) Subject: - added support for setting the file mtime and the timestamp filename to the X-Git-Tag: 0.3~1 X-Git-Url: http://git.home-dn.net/?p=manu%2Fperfect_maildir.git;a=commitdiff_plain;h=039baed471b351cb8137017397b6ba7c5dd91ef2 - added support for setting the file mtime and the timestamp filename to the date in the "Date: " header or in the "From " mbox line. Thanks to Lukas van Schagen, Gudleik Rasch and Bill McGonigle for giving me some different patches doing this, I mixed it to get the better way I hope;) (default: "From ") - added support for the maildir++ format (append the message size to the filename) (default: deactivated) --- diff --git a/CHANGES b/CHANGES index 5cd5ada..995f3d6 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,17 @@ +v0.3 Sun Oct 31 19:47:33 UTC 2004 + +Emmanuel Lacour + +- added support for setting the file mtime and the timestamp filename to the + date in the "Date: " header or in the "From " mbox line. Thanks to Lukas van + Schagen, Gudleik Rasch and Bill McGonigle for giving me some different + patches doing this, I mixed it to get the better way I hope;) + (default: "From ") +- added support for the maildir++ format (append the message size to the filename) + (default: deactivated) + +Those two settings can be changed at the top of the file. + v0.2 Sun Mar 9 11:05:56 CET 2003 Emmanuel Lacour diff --git a/perfect_maildir.pl b/perfect_maildir.pl index eed9be0..4667d65 100755 --- a/perfect_maildir.pl +++ b/perfect_maildir.pl @@ -1,6 +1,6 @@ -#!/usr/bin/perl +#!/usr/bin/perl -w -# "Simple but Perfect" mbox to Maildir converter v0.2 +# "Simple but Perfect" mbox to Maildir converter v0.3 # Copyright (C) 2001-2003 Philip Mak # # This program is free software; you can redistribute it and/or @@ -17,17 +17,33 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -# Get the hostname +use strict; +use Date::Parse qw( str2time ); + +#### Settings +# This will be used to set the file time (needed for courier-imap and some others) +# $datestyle = "date": extract date from the "Date: " header +# $datestyle = "from": extract date from the "From " mbox header +my $datestyle = "from"; + +# Use maildir++ format (append the message size to the filename) +my $maildirplus = 0; +#### + + +# Get the hostname my $hostname = `hostname`; chomp ($hostname); # check for valid arguments - my ($maildir) = @ARGV; if (!$maildir) { print STDERR "Usage: perfect_maildir ~/Maildir < mbox\n"; exit 1; +} elsif (! -d $maildir) { + print STDERR "Cannot open $maildir\n"; + exit 1; } # check for writable maildir @@ -42,6 +58,8 @@ unless (-w "$maildir/new") { my $num = 0; my $time = time; +my $date; +my $delivered_time; repeat: @@ -53,6 +71,13 @@ while (my $line = ) { # detect end of headers last if $line eq "\n"; + if ($datestyle eq "from") { + # Get date from the "From " line (this should appears here for the first message only) + $date = $1 if $line =~ /^From [^ ^\t]+[ \t]+(.{24})/; + } elsif ($datestyle eq "date") { + # Get date from the "Date: " header + $date = $1 if $line =~ /^Date: (.*)$/; + } # strip "From" line from header $headers .= $line unless $line =~ /^From ./; @@ -64,10 +89,16 @@ while (my $line = ) { $num++; +if ($datestyle =~ /(from|date)/) { + $delivered_time = str2time("$date"); +} else { + $delivered_time = $time; +} + # open output file my $file; if ($flags =~ /O/) { - $file = "$maildir/cur/$time.$num.$hostname"; + $file = sprintf( "%s%05d%s", "$maildir/cur/$delivered_time.", $num, ".$hostname" ); my $extra = ''; $extra .= 'F' if $flags =~ /F/; # flagged $extra .= 'R' if $flags =~ /A/; # replied @@ -75,7 +106,7 @@ if ($flags =~ /O/) { $extra .= 'T' if $flags =~ /D/; # trashed $file .= ":2,$extra" if $extra; } else { - $file = "$maildir/new/$time.$num.$hostname"; + $file = sprintf( "%s%05d%s", "$maildir/new/$delivered_time.", $num, ".$hostname" ); } # filter out the "DON'T DELETE THIS MESSAGE -- FOLDER INTERNAL DATA" message or the message doesn't exists @@ -87,8 +118,12 @@ if (($num == 1 and $subject eq "DON'T DELETE THIS MESSAGE -- FOLDER INTERNAL DAT open(FILE, ">$file"); print FILE "$headers\n"; while (my $line = ) { - # detect end of message - last if $line =~ /^From ./; + if ($datestyle eq "from") { + # Get date from the "From " line (this should appears here for the first message only) + $date = $1 if $line =~ /^From [^ ^\t]+[ \t]+(.{24})/; + } + # End of current message + last if ($line =~ /^From ./); # unescape "From" $line =~ s/^>From (.)/From $1/; @@ -96,6 +131,16 @@ while (my $line = ) { print FILE $line; } close(FILE); + +utime( $time, $delivered_time, $file ) if ($datestyle =~ /(from|date)/); + +if ($maildirplus) { + my $size = -s $file; + my $mdplusfile = $file; + $mdplusfile =~ s/\.$hostname/.$hostname,S=$size/; + rename $file,$mdplusfile; +} + goto repeat unless eof(STDIN); my $elapsed = time - $time;