3 # "Simple but Perfect" mbox to Maildir converter v0.3
4 # Copyright (C) 2001-2003 Philip Mak <pmak@aaanime.net>
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 use Date::Parse qw( str2time );
25 # This will be used to set the file time (needed for courier-imap and some others)
26 # $datestyle = "date": extract date from the "Date: " header
27 # $datestyle = "from": extract date from the "From " mbox header
28 my $datestyle = "from";
30 # Use maildir++ format (append the message size to the filename)
36 my $hostname = `hostname`;
39 # check for valid arguments
40 my ($maildir) = @ARGV;
42 print STDERR "Usage: perfect_maildir ~/Maildir < mbox\n";
44 } elsif (! -d $maildir) {
45 print STDERR "Cannot open $maildir\n";
49 # check for writable maildir
50 unless (-w "$maildir/cur") {
51 print STDERR "Cannot write to $maildir/cur\n";
54 unless (-w "$maildir/new") {
55 print STDERR "Cannot write to $maildir/new\n";
70 while (my $line = <STDIN>) {
71 # detect end of headers
72 last if $line eq "\n";
74 if ($datestyle eq "from") {
75 # Get date from the "From " line (this should appears here for the first message only)
76 $date = $1 if $line =~ /^From [^ ^\t]+[ \t]+(.{24})/;
77 } elsif ($datestyle eq "date") {
78 # Get date from the "Date: " header
79 $date = $1 if $line =~ /^Date: (.*)$/;
81 # strip "From" line from header
82 $headers .= $line unless $line =~ /^From ./;
85 $flags .= $1 if $line =~ /^Status: ([A-Z]+)/;
86 $flags .= $1 if $line =~ /^X-Status: ([A-Z]+)/;
87 $subject = $1 if $line =~ /^Subject: (.*)$/;
92 if ($datestyle =~ /(from|date)/) {
93 $delivered_time = str2time("$date");
95 $delivered_time = $time;
101 $file = sprintf( "%s%05d%s", "$maildir/cur/$delivered_time.", $num, ".$hostname" );
103 $extra .= 'F' if $flags =~ /F/; # flagged
104 $extra .= 'R' if $flags =~ /A/; # replied
105 $extra .= 'S' if (($flags =~ /R/) || ($flags =~ /O/)); # seen
106 $extra .= 'T' if $flags =~ /D/; # trashed
107 $file .= ":2,$extra" if $extra;
109 $file = sprintf( "%s%05d%s", "$maildir/new/$delivered_time.", $num, ".$hostname" );
112 # filter out the "DON'T DELETE THIS MESSAGE -- FOLDER INTERNAL DATA" message or the message doesn't exists
113 if (($num == 1 and $subject eq "DON'T DELETE THIS MESSAGE -- FOLDER INTERNAL DATA") || (!$headers)) {
118 open(FILE, ">$file");
119 print FILE "$headers\n";
120 while (my $line = <STDIN>) {
121 if ($datestyle eq "from") {
122 # Get date from the "From " line (this should appears here for the first message only)
123 $date = $1 if $line =~ /^From [^ ^\t]+[ \t]+(.{24})/;
125 # End of current message
126 last if ($line =~ /^From ./);
129 $line =~ s/^>From (.)/From $1/;
135 utime( $time, $delivered_time, $file ) if ($datestyle =~ /(from|date)/);
139 my $mdplusfile = $file;
140 $mdplusfile =~ s/\.$hostname/.$hostname,S=$size/;
141 rename $file,$mdplusfile;
144 goto repeat unless eof(STDIN);
146 my $elapsed = time - $time;
147 print "Inserted $num messages into maildir $maildir in $elapsed seconds\n";