3 # "Simple but Perfect" mbox to Maildir converter v0.2
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 my $hostname = `hostname`;
25 # check for valid arguments
27 my ($maildir) = @ARGV;
29 print STDERR "Usage: perfect_maildir ~/Maildir < mbox\n";
33 # check for writable maildir
34 unless (-w "$maildir/cur") {
35 print STDERR "Cannot write to $maildir/cur\n";
38 unless (-w "$maildir/new") {
39 print STDERR "Cannot write to $maildir/new\n";
52 while (my $line = <STDIN>) {
53 # detect end of headers
54 last if $line eq "\n";
56 # strip "From" line from header
57 $headers .= $line unless $line =~ /^From ./;
60 $flags .= $1 if $line =~ /^Status: ([A-Z]+)/;
61 $flags .= $1 if $line =~ /^X-Status: ([A-Z]+)/;
62 $subject = $1 if $line =~ /^Subject: (.*)$/;
70 $file = "$maildir/cur/$time.$num.$hostname";
72 $extra .= 'F' if $flags =~ /F/; # flagged
73 $extra .= 'R' if $flags =~ /A/; # replied
74 $extra .= 'S' if (($flags =~ /R/) || ($flags =~ /O/)); # seen
75 $extra .= 'T' if $flags =~ /D/; # trashed
76 $file .= ":2,$extra" if $extra;
78 $file = "$maildir/new/$time.$num.$hostname";
81 # filter out the "DON'T DELETE THIS MESSAGE -- FOLDER INTERNAL DATA" message or the message doesn't exists
82 if (($num == 1 and $subject eq "DON'T DELETE THIS MESSAGE -- FOLDER INTERNAL DATA") || (!$headers)) {
88 print FILE "$headers\n";
89 while (my $line = <STDIN>) {
90 # detect end of message
91 last if $line =~ /^From ./;
94 $line =~ s/^>From (.)/From $1/;
99 goto repeat unless eof(STDIN);
101 my $elapsed = time - $time;
102 print "Inserted $num messages into maildir $maildir in $elapsed seconds\n";