Update README with new settings.
[manu/perfect_maildir.git] / perfect_maildir.pl
1 #!/usr/bin/perl -w
2
3 # "Simple but Perfect" mbox to Maildir converter v0.3
4 # Copyright (C) 2001-2003  Philip Mak <pmak@aaanime.net>
5
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.
10
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.
15
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.
19
20
21 use strict;
22 use Date::Parse qw( str2time );
23
24 #### Settings
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";
29
30 # Use maildir++ format (append the message size to the filename)
31 my $maildirplus = 0;
32 ####
33
34
35 # Get the hostname
36 my $hostname = `hostname`;
37 chomp ($hostname);
38
39 # check for valid arguments
40 my ($maildir) = @ARGV;
41 if (!$maildir) {
42   print STDERR "Usage: perfect_maildir ~/Maildir < mbox\n";
43   exit 1;
44 } elsif (! -d $maildir) {
45   print STDERR "Cannot open $maildir\n";
46   exit 1;
47 }
48
49 # check for writable maildir
50 unless (-w "$maildir/cur") {
51   print STDERR "Cannot write to $maildir/cur\n";
52   exit 1;
53 }
54 unless (-w "$maildir/new") {
55   print STDERR "Cannot write to $maildir/new\n";
56   exit 1;
57 }
58
59 my $num = 0;
60 my $time = time;
61 my $date;
62 my $delivered_time;
63
64 repeat:
65
66 # read header
67 my $headers = '';
68 my $flags = '';
69 my $subject = '';
70 while (my $line = <STDIN>) {
71   # detect end of headers
72   last if $line eq "\n";
73
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: (.*)$/;
80   }
81   # strip "From" line from header
82   $headers .= $line unless $line =~ /^From ./;
83
84   # detect flags
85   $flags .= $1 if $line =~ /^Status: ([A-Z]+)/;
86   $flags .= $1 if $line =~ /^X-Status: ([A-Z]+)/;
87   $subject = $1 if $line =~ /^Subject: (.*)$/;
88 }
89
90 $num++;
91
92 if ($datestyle =~ /(from|date)/) {
93   $delivered_time = str2time("$date");
94 } else {
95   $delivered_time = $time;
96 }
97
98 # open output file
99 my $file;
100 if ($flags =~ /O/) {
101   $file = sprintf( "%s%05d%s", "$maildir/cur/$delivered_time.", $num, ".$hostname" );
102   my $extra = '';
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;
108 } else {
109   $file = sprintf( "%s%05d%s", "$maildir/new/$delivered_time.", $num, ".$hostname" );
110 }
111
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)) {
114         $file = '/dev/null';
115         $num--;
116 }
117
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})/;
124   }
125     # End of current message 
126   last if ($line =~ /^From ./);
127
128   # unescape "From"
129   $line =~ s/^>From (.)/From $1/;
130
131   print FILE $line;
132 }
133 close(FILE);
134
135 utime( $time, $delivered_time, $file ) if ($datestyle =~ /(from|date)/);
136
137 if ($maildirplus) {
138         my $size = -s $file;
139         my $mdplusfile = $file;
140         $mdplusfile =~ s/\.$hostname/.$hostname,S=$size/;
141         rename $file,$mdplusfile;
142 }
143
144 goto repeat unless eof(STDIN);
145
146 my $elapsed = time - $time;
147 print "Inserted $num messages into maildir $maildir in $elapsed seconds\n";