- change the use of $ENV{HOSTNAME} by `hostname`
[manu/perfect_maildir.git] / perfect_maildir.pl
1 #!/usr/bin/perl
2
3 # "Simple but Perfect" mbox to Maildir converter v0.2
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 # Get the hostname
21
22 my $hostname = `hostname`;
23 chomp ($hostname);
24
25 # check for valid arguments
26
27 my ($maildir) = @ARGV;
28 if (!$maildir) {
29   print STDERR "Usage: perfect_maildir ~/Maildir < mbox\n";
30   exit 1;
31 }
32
33 # check for writable maildir
34 unless (-w "$maildir/cur") {
35   print STDERR "Cannot write to $maildir/cur\n";
36   exit 1;
37 }
38 unless (-w "$maildir/new") {
39   print STDERR "Cannot write to $maildir/new\n";
40   exit 1;
41 }
42
43 my $num = 0;
44 my $time = time;
45
46 repeat:
47
48 # read header
49 my $headers = '';
50 my $flags = '';
51 my $subject = '';
52 while (my $line = <STDIN>) {
53   # detect end of headers
54   last if $line eq "\n";
55
56   # strip "From" line from header
57   $headers .= $line unless $line =~ /^From ./;
58
59   # detect flags
60   $flags .= $1 if $line =~ /^Status: ([A-Z]+)/;
61   $flags .= $1 if $line =~ /^X-Status: ([A-Z]+)/;
62   $subject = $1 if $line =~ /^Subject: (.*)$/;
63 }
64
65 $num++;
66
67 # open output file
68 my $file;
69 if ($flags =~ /O/) {
70   $file = "$maildir/cur/$time.$num.$hostname";
71   my $extra = '';
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;
77 } else {
78   $file = "$maildir/new/$time.$num.$hostname";
79 }
80
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)) {
83         $file = '/dev/null';
84         $num--;
85 }
86
87 open(FILE, ">$file");
88 print FILE "$headers\n";
89 while (my $line = <STDIN>) {
90   # detect end of message
91   last if $line =~ /^From ./;
92
93   # unescape "From"
94   $line =~ s/^>From (.)/From $1/;
95
96   print FILE $line;
97 }
98 close(FILE);
99 goto repeat unless eof(STDIN);
100
101 my $elapsed = time - $time;
102 print "Inserted $num messages into maildir $maildir in $elapsed seconds\n";