version 0.9.4claws
[claws.git] / tools / maildir2sylpheed.pl
1 #!/usr/bin/perl
2
3 #  * Copyright © 2003 Paul Mangan <claws@thewildbeast.co.uk>
4 #  *
5 #  * This file is free software; you can redistribute it and/or modify it
6 #  * under the terms of the GNU General Public License as published by
7 #  * the Free Software Foundation; either version 2 of the License, or
8 #  * (at your option) any later version.
9 #  *
10 #  * This program is distributed in the hope that it will be useful, but
11 #  * WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 #  * General Public License for more details.
14 #  *
15 #  * You should have received a copy of the GNU General Public License
16 #  * along with this program; if not, write to the Free Software
17 #  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 ## script name : maildir2sylpheed.pl
20
21 ## script purpose : convert a Kmail mailbox into a Sylpheed mailbox
22
23 ## USAGE: maildir2sylpheed.pl --kmaildir=Mail
24
25 ## tested with Kmail 1.5.2
26
27 use Getopt::Long;
28 use File::Recurse;
29
30 $kmaildir  = '';
31 $iNeedHelp = '';
32
33 $sylpheed_tmpdir = "sylpheed_tmp";
34 $kmail_olddir    = "kmail_junk";
35
36 GetOptions("kmaildir=s" => \$kmaildir,
37            "help"        => \$iNeedHelp);
38
39 if ($kmaildir eq "" || $iNeedHelp) {
40         if (!$iNeedHelp) {
41                 print "No directory name given\n";
42         }
43         print "Use the following format:\n";
44         print "\tmaildir2sylpheed.pl --kmaildir=mail_folder_name\n\n";
45         print "For example: 'Mail'\n";
46         exit;
47 }
48
49 chdir($ENV{HOME});
50
51 $MAIL_dir = "$ENV{HOME}/$kmaildir";
52
53 mkdir("$sylpheed_tmpdir", 0755);
54
55 my %files = Recurse(["$MAIL_dir"], {});
56
57 foreach (keys %files) { 
58         $dir = $_;
59         push(@dirs, "$_");
60         foreach (@{ $files{$_} }) { 
61                 push(@files, "$dir/$_");
62         }
63 }
64
65 foreach $direc (@dirs) {
66         if ($direc !~ m/^drafts$/
67             && $direc !~ m/^outbox$/
68             && $direc !~ m/^trash$/
69             && $direc !~ m/^inbox$/) {
70                 $tmpdir = $direc;
71                 $tmpdir =~ s/^$MAIL_dir//;
72                 $tmpdir =~ s/sent-mail/sent/;
73                 $tmpdir =~ s/\/cur$//;
74                 $tmpdir =~ s/\/new$//;
75                 $tmpdir =~ s/^\///;
76                 $tmpdir =~ s/\.directory//g;
77                 $tmpdir =~ s/\.//g;
78                 mkdir("$sylpheed_tmpdir/$tmpdir");
79                 opendir(DIR, "$direc")
80                         || die("Can't open directory");
81                 push(@subdirs,(readdir(DIR)));
82                 closedir DIR;
83         }
84
85         foreach $subdir (@subdirs) {
86                 if ($subdir !~ m/\.directory$/
87                     && $subdir!~ m/^\.*$/
88                     && $subdir !~ m/cur\/$/
89                     && $subdir !~ m/new\/$/
90                     && $subdir !~ m/tmp\/$/) {
91                         $sub_dir =~ s/\.directory//;
92                         unless (-e "$sylpheed_tmpdir/$tmpdir/$sub_dir") {
93                                 mkdir("$sylpheed_tmpdir/$tmpdir/$sub_dir");
94                         }
95                 }               
96         }
97 }
98
99 $count = 1;
100 foreach $file (@files) {
101         $tmpfile = $file;
102         if ($tmpfile =~ m/\/cur\//
103             || $tmpfile =~ m/\/new\//) {
104                 $tmpfile =~ s/\/new//;
105                 $tmpfile =~ s/\/cur//;
106                 @spl_str = split("/", $tmpfile);
107                 pop(@spl_str);
108                 push(@spl_str, "$count");
109                 foreach $spl_str (@spl_str) {
110                         $spl_str =~ s/^\.//;
111                         $spl_str =~ s/\.directory$//;
112                         $spl_str =~ s/sent-mail/sent/;
113                 }
114                 $nfile = join("/", @spl_str);
115                 $nfile =~ s/\/$kmaildir\//\/$sylpheed_tmpdir\//;
116         }
117
118         if (-e "$file" && $nfile ne "") {
119                 system("cp \"$file\" \"$nfile\"");
120                 $count++;
121         }       
122 }
123
124 system("mv $kmaildir $kmail_olddir");
125 system("mv $sylpheed_tmpdir $ENV{HOME}/Mail");
126
127 print "Sucessfully converted mailbox \"$MAIL_dir\"\n";
128 print "Start Sylpheed and right-click \"Mailbox (MH)\" and ";
129 print "select \"Rebuild folder tree\"\n";
130 print "You may also need to run \"/File/Folder/Check for ";
131 print "new messages in all folders\"\n\n";
132 print "Your kmail directories have been backed-up to\n";
133 print "$ENV{HOME}/$kmail_olddir\n\n";
134
135 exit;