2007-01-17 [paul] 2.7.1cvs13
[claws.git] / tools / maildir2claws-mail.pl
1 #!/usr/bin/perl
2
3 #  * This file is free software; you can redistribute it and/or modify it
4 #  * under the terms of the GNU General Public License as published by
5 #  * the Free Software Foundation; either version 2 of the License, or
6 #  * (at your option) any later version.
7 #  *
8 #  * This program is distributed in the hope that it will be useful, but
9 #  * WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 #  * General Public License for more details.
12 #  *
13 #  * You should have received a copy of the GNU General Public License
14 #  * along with this program; if not, write to the Free Software
15 #  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #  *
17 #  * Copyright 2003 Paul Mangan <paul@claws-mail.org>
18 #  *
19 #  * 2003-10-01: add --debug and --dry-run options
20 #  * 2003-09-30: updated/improved by Matthias Förste <itsjustme@users.sourceforge.net>
21 #  * 2003-05-27: version one
22
23 ## script name : maildir2sylpheed.pl
24
25 ## script purpose : convert a Kmail mailbox into a Claws Mail mailbox
26
27 ## USAGE: maildir2claws-mail.pl --kmaildir=Mail
28
29 ## tested with Kmail version 1.5.2
30
31 use strict;
32
33 use Getopt::Long;
34 use File::Find;
35
36 my $kmaildir  = '';
37 my $iNeedHelp = '';
38 # dont actually change anything if set(useful in conjunction with debug)
39 my $PRETEND = '';
40 # print debug info if set
41 my $DEBUG = '';
42
43 my $claws_tmpdir = "$ENV{HOME}/claws_tmp";
44 my $kmail_olddir = "$ENV{HOME}/kmail_junk";
45
46 GetOptions("kmaildir=s" => \$kmaildir,
47            "help"       => \$iNeedHelp,
48            "dry-run"    => \$PRETEND,
49            "debug"      => \$DEBUG);
50
51 if ($kmaildir eq "" || $iNeedHelp) {
52         if (!$iNeedHelp) {
53                 print "No directory name given\n";
54         }
55         print "Use the following format:\n";
56         print "\tmaildir2claws-mail.pl --kmaildir=mail_folder_name\n\n";
57         print "For example: 'Mail'\n";
58         exit;
59 }
60
61 $kmaildir = "$ENV{PWD}/$kmaildir" unless '/' eq substr($kmaildir,0,1);
62
63 my $count = 1;
64 my $MAIL_dir = "$kmaildir";
65
66 my $find_opts = { wanted => \&process };
67
68 if (-d $MAIL_dir) {
69         find($find_opts , ($MAIL_dir));
70 } else {
71         print "\n$MAIL_dir is not a directory !\n";
72         exit;
73 }
74
75 unless ($PRETEND) {
76         mkdir("$claws_tmpdir", 0755);
77         system("mv $kmaildir $kmail_olddir");
78         system("mv $claws_tmpdir $ENV{HOME}/Mail");
79
80         print "\n\nSucessfully converted mailbox \"$MAIL_dir\"\n";
81         print "Start claws-mail and right-click \"Mailbox (MH)\" and ";
82         print "select \"Rebuild folder tree\"\n";
83         print "You may also need to run \"/File/Folder/Check for ";
84         print "new messages in all folders\"\n\n";
85         print "Your kmail directories have been backed-up to\n";
86         print "$kmail_olddir\n\n";
87 }
88
89 print "\n";
90 exit;
91
92 sub process() {
93         if (-d) {
94                 process_dir($File::Find::dir);
95         } else {
96                 process_file($File::Find::name);
97         }
98 }
99
100 sub process_dir() {
101         my $direc = shift();
102         $DEBUG && print "\nDIR $direc";
103
104         if ($direc !~ m/^drafts$/ &&
105             $direc !~ m/^outbox$/ &&
106             $direc !~ m/^trash$/  && 
107             $direc !~ m/^inbox$/) {
108                 my $tmpdir = $direc;
109                 $tmpdir =~ s/^$MAIL_dir//;
110                 $tmpdir =~ s/^sent-mail$/sent/;
111                 $tmpdir =~ s/\/cur$//;
112                 $tmpdir =~ s/\/new$//;
113                 $tmpdir =~ s/^\///;
114                 $tmpdir =~ s/\.directory//g;
115                 $tmpdir =~ s/\.//g;
116                 
117                 my $newdir = "$claws_tmpdir/$tmpdir";
118                 $DEBUG && print qq{\n>>> -e "$newdir" || mkdir("$newdir")};
119                 $PRETEND || -e "$newdir" || mkdir("$newdir");
120         }
121
122 }
123
124 sub process_file {
125         my $file = shift;
126         $DEBUG && print "\nFILE $file";
127
128         my $nfile;
129         my $tmpfile = $file;
130
131         if ($tmpfile =~ m/\/cur\// || 
132             $tmpfile =~ m/\/new\//) {
133
134                 $tmpfile =~ s/\/new//;
135                 $tmpfile =~ s/\/cur//;
136
137                 my @spl_str = split("/", $tmpfile);
138                 pop(@spl_str);
139                 push(@spl_str, "$count");
140
141                 foreach my $spl_str (@spl_str) {
142                         $spl_str =~ s/^\.//;
143                         $spl_str =~ s/\.directory$//;
144                         $spl_str =~ s/^sent-mail$/sent/;
145                 }
146
147                 $nfile = join("/", @spl_str);
148                 $nfile =~ s|$kmaildir|$claws_tmpdir/|;
149         }
150
151         if (-e "$file" && $nfile ne "") {
152                 $DEBUG && print qq{\n+++ cp "$file" "$nfile"};
153                 $PRETEND || system("cp \"$file\" \"$nfile\"");
154                 $count++;
155         }
156
157 }