chiark / gitweb /
force ControlPath none
[bin.git] / perfect-maildir
1 #!/usr/bin/perl
2
3 # "Simple but Perfect" mbox to Maildir converter v0.1
4 # by Philip Mak <pmak@aaanime.net>
5
6 # Usage: perfect_maildir ~/Maildir < mbox
7
8 # Simple  - only converts one mbox (can use script in one-liners)
9 # Perfect - message Flags/X-Flags are converted; "^>From ." line is unescaped
10
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).
16
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!)
22
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
28 # ~/.muttrc file:
29 #
30 # set index_format="%4C %Z %{%b %d} %-15.15L (%4c) %s"
31
32 # check for valid arguments
33 my ($maildir) = @ARGV;
34 if (!$maildir) {
35   print STDERR "Usage: perfect_maildir ~/Maildir < mbox\n";
36   exit 1;
37 }
38
39 # check for writable maildir
40 unless (-w "$maildir/cur") {
41   print STDERR "Cannot write to $maildir/cur\n";
42   exit 1;
43 }
44 unless (-w "$maildir/new") {
45   print STDERR "Cannot write to $maildir/new\n";
46   exit 1;
47 }
48
49 my $num = 0;
50 my $time = time;
51
52 repeat:
53
54 # read header
55 my $headers = '';
56 my $flags = '';
57 my $subject = '';
58 while (my $line = <STDIN>) {
59   # detect end of headers
60   last if $line eq "\n";
61
62   # strip "From" line from header
63   $headers .= $line unless $line =~ /^From ./;
64
65   # detect flags
66   $flags .= $1 if $line =~ /^Status: ([A-Z]+)/;
67   $flags .= $1 if $line =~ /^X-Status: ([A-Z]+)/;
68   $subject = $1 if $line =~ /^Subject: (.*)$/;
69 }
70 $num++;
71
72 # open output file
73 my $file;
74 if ($flags =~ /O/) {
75   $file = "$maildir/cur/$time.$num.$ENV{HOSTNAME}";
76   my $extra = '';
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;
82 } else {
83   $file = "$maildir/new/$time.$num.$ENV{HOSTNAME}";
84 }
85
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");
88
89 open(FILE, ">$file");
90 print FILE "$headers\n";
91 while (my $line = <STDIN>) {
92   # detect end of message
93   last if $line =~ /^From ./;
94
95   # unescape "From"
96   $line =~ s/^>From (.)/From $1/;
97
98   print FILE $line;
99 }
100 close(FILE);
101
102 goto repeat unless eof(STDIN);
103
104 my $elapsed = time - $time;
105 print "Inserted $num messages into maildir $maildir in $elapsed seconds\n";