3 # "Simple but Perfect" mbox to Maildir converter v0.1
4 # by Philip Mak <pmak@aaanime.net>
6 # Usage: perfect_maildir ~/Maildir < mbox
8 # Simple - only converts one mbox (can use script in one-liners)
9 # Perfect - message Flags/X-Flags are converted; "^>From ." line is unescaped
11 # I wrote this script after being unsatisfied with existing mbox to
12 # maildir converters. By making it "Simple", code complexity is kept
13 # low thus making it easy to program and debug. At the same time,
14 # since it only converts one mbox at a time, it is perfect for use in
15 # a shell ``for'' loop (for example).
17 # As for being "Perfect", to the best of my knowledge this script does
18 # the conversion correctly in all cases; it will translate "Status"
19 # and "X-Status" fields into maildir info, and it correctly detects
20 # where messages begin and end. (This is only version 0.1 so I may
21 # have messed something up though. Please send me feedback!)
23 # NOTE: The MUA ``mutt'' has a bug/feature where in the message index,
24 # it claims that all maildir messages have 0 lines unless they have a
25 # "Lines:" header set. perfect_maildir does not attempt to add the
26 # "Lines:" header; you may want to reconfigure ``mutt' to display byte
27 # size instead of lines instead by adding the following line to your
30 # set index_format="%4C %Z %{%b %d} %-15.15L (%4c) %s"
32 # check for valid arguments
33 my ($maildir) = @ARGV;
35 print STDERR "Usage: perfect_maildir ~/Maildir < mbox\n";
39 # check for writable maildir
40 unless (-w "$maildir/cur") {
41 print STDERR "Cannot write to $maildir/cur\n";
44 unless (-w "$maildir/new") {
45 print STDERR "Cannot write to $maildir/new\n";
58 while (my $line = <STDIN>) {
59 # detect end of headers
60 last if $line eq "\n";
62 # strip "From" line from header
63 $headers .= $line unless $line =~ /^From ./;
66 $flags .= $1 if $line =~ /^Status: ([A-Z]+)/;
67 $flags .= $1 if $line =~ /^X-Status: ([A-Z]+)/;
68 $subject = $1 if $line =~ /^Subject: (.*)$/;
75 $file = "$maildir/cur/$time.$num.$ENV{HOSTNAME}";
77 $extra .= 'F' if $flags =~ /F/; # flagged
78 $extra .= 'R' if $flags =~ /A/; # replied
79 $extra .= 'S' if $flags =~ /R/; # seen
80 $extra .= 'T' if $flags =~ /D/; # trashed
81 $file .= ":2,$extra" if $extra;
83 $file = "$maildir/new/$time.$num.$ENV{HOSTNAME}";
86 # filter out the "DON'T DELETE THIS MESSAGE -- FOLDER INTERNAL DATA" message
87 $file = '/dev/null' if ($num == 1 and $subject eq "DON'T DELETE THIS MESSAGE -- FOLDER INTERNAL DATA");
90 print FILE "$headers\n";
91 while (my $line = <STDIN>) {
92 # detect end of message
93 last if $line =~ /^From ./;
96 $line =~ s/^>From (.)/From $1/;
102 goto repeat unless eof(STDIN);
104 my $elapsed = time - $time;
105 print "Inserted $num messages into maildir $maildir in $elapsed seconds\n";