source: ProjectBuilder/projects/md2mb/devel/md2mb/bin/md2mb.pl@ 1448

Last change on this file since 1448 was 1448, checked in by ebaudrez, 12 years ago

improvements to handling of $oldroot

Try to determine $oldroot automatically, by looking for a folder called
.Mail or Mail in your homedir. Failing that, the root can be specified
with a command-line option.

File size: 5.7 KB
Line 
1#!/usr/bin/perl -w
2
3=head1 NAME
4
5md2mb.pl - Import a maildir kmail environment into a thunderbird one
6
7=cut
8
9use strict;
10use File::Find;
11use File::Copy;
12use File::Basename;
13use File::Path;
14use File::Glob ':glob';
15use Getopt::Long;
16use Pod::Usage;
17use List::Util qw(first);
18
19# settings
20my $cmd = first {
21 system("$_ </dev/null >/dev/null 2>/dev/null") == 0
22 } qw(formail procmail);
23$cmd or die <<EOF;
24cannot find a usable program to manipulate your mailboxes in your \$PATH!
25Try installing `formail' or `procmail'
26Aborting
27EOF
28my $oldroot = first { -d } "$ENV{HOME}/.Mail", "$ENV{HOME}/Mail";
29# CHANGE AS YOU WISH
30my $newroot = "/users/segolene/.thunderbird/qk2f4dl6.default/Mail/pop.home.musique-ancienne.org/";
31# Is the newroot a file (1) or a dir (0)
32my $nrisfile = 0;
33# END CHANGE
34my $debug = 0;
35my $help = 0;
36my $man = 0;
37
38# parse command-line options
39GetOptions(
40 'debug|d' => \$debug,
41 'help|h' => \$help,
42 'man|m' => \$man,
43 'oldroot|old|o' => \$oldroot,
44) or pod2usage(2);
45pod2usage(1) if ($help);
46pod2usage(-exitstatus => 0, -verbose => 2) if ($man);
47
48# debug mode overview
49if ($debug) {
50 print <<EOF;
51DEBUG MODE, not doing anything, just printing
52--- current state ---
53cmd = $cmd
54oldroot = $oldroot
55newroot = $newroot
56--- end ---
57EOF
58}
59
60# some sanity checks before proceeding
61-d $oldroot or die "cannot find mailbox root `$oldroot'";
62
63# create destination path
64if ($debug) {
65 print "TARGET DIR: mkdir -p $newroot\n" if ((not -d "$newroot") && (not $nrisfile));
66 print "CMD: mkdir -p $newroot\n" if ((not -d "$newroot") && (not $nrisfile));
67} else {
68 mkpath("$newroot",0, 0755) if ((not -d "$newroot") && (not $nrisfile));
69}
70
71# the main work is done here
72if ($debug) {
73 print "DESCENDING INTO oldroot($oldroot)\n";
74}
75find(\&md2mb,($oldroot));
76
77# rename `inbox' to `Inbox'
78if ((-z "$newroot/Inbox") || (! -e "$newroot/Inbox")) {
79 if ($debug) {
80 print "RENAMING inbox($newroot/inbox) INTO Inbox($newroot/Inbox)\n" if (-e "$newroot/inbox");
81 } else {
82 print "Renaming inbox into Inbox\n";
83 move("$newroot/inbox","$newroot/Inbox") if (-e "$newroot/inbox");
84 }
85}
86
87sub md2mb {
88
89if (-f $File::Find::name) {
90 if (($File::Find::name =~ /\.ids$/) ||
91 ($File::Find::name =~ /\.sorted$/) ||
92 ($File::Find::name =~ /\.index$/)) {
93 print "SKIP FILE: $File::Find::name\n" if ($debug);
94 return;
95 }
96}
97if (-d $File::Find::name) {
98 if (($File::Find::name =~ /\/cur$/) ||
99 ($File::Find::name =~ /\/new$/) ||
100 ($File::Find::name =~ /\/tmp$/)) {
101 print "SKIP DIR: $File::Find::name\n" if ($debug);
102 return;
103 }
104}
105my $destname = $File::Find::name;
106# Target name is under a different root dir
107$destname =~ s|^$oldroot||;
108# Target name is not under a .directory dir but under a .sdb one
109$destname =~ s|\.([^/]+)\.directory/|$1.sbd/|g;
110# Here we create the target dir and target name
111my $outputfile="$newroot/$destname";
112my $cdir = dirname("$outputfile");
113# Handle case where target file name is empty
114$outputfile="$newroot" if ($destname =~ /^\s*$/);
115# When we treat a dir, we will have to handle what it has below
116if (-d $File::Find::name) {
117 if ($debug) {
118 print "DIR SRC: $File::Find::name\n";
119 }
120 my @files = (bsd_glob("$File::Find::name/cur/*"),bsd_glob("$File::Find::name/new/*"));
121 if (@files) {
122 if ($debug) {
123 print "DIR ($File::Find::name) DIR TARGET: mkdir -p $cdir\n" if (not -d "$cdir");
124 } else {
125 mkpath("$cdir",0, 0755) if (not -d "$cdir");
126 }
127 }
128 foreach my $file (@files) {
129 next unless -f $file; # skip non-regular files
130 next unless -s $file; # skip empty files
131 next unless -r $file; # skip unreadable files
132 $file =~ s/'/'"'"'/g; # escape ' (single quote)
133 # NOTE! The output file must not contain single quotes (')!
134 my $run = "cat '$file' | $cmd >> '$outputfile'";
135 if ($debug) {
136 print "COPYING CONTENT maildir($file) to $outputfile\n";
137 print "CMD: $run\n";
138 } else {
139 print "Copying maildir content from $file to $outputfile\n";
140 system($run) == 0 or warn "cannot run \"$run\".";
141 }
142 }
143}
144if (-f $File::Find::name) {
145 if (($File::Find::name =~ /\/cur\//) ||
146 ($File::Find::name =~ /\/new\//) ||
147 ($File::Find::name =~ /\/tmp\//)) {
148 print "SKIP FILE: $File::Find::name\n" if ($debug);
149 return;
150 }
151 if ($debug) {
152 print "FILE ($File::Find::name) TARGET DIR: mkdir -p $cdir\n" if (not -d "$cdir");
153 print "CMD: cp $File::Find::name $cdir\n";
154 } else {
155 print "Copying mailbox content from $File::Find::name to $cdir\n";
156 mkpath("$cdir",0, 0755) if (not -d "$cdir");
157 copy($File::Find::name,$cdir);
158 }
159}
160}
161__END__
162
163=head1 SYNOPSIS
164
165md2mb.pl [options]
166
167 Options:
168 -debug | -d debug mode
169 -help | -h brief help message
170 -man | -m full documentation
171 -oldroot | -old | -o location of the KMail mailboxes
172
173=head1 OPTIONS
174
175=over 4
176
177=item B<-debug>
178
179Enter debug mode. This will print what would be done. No commands are executed,
180so this is safe to use when testing.
181
182=item B<-help>
183
184Print a brief help message and exits.
185
186=item B<-man>
187
188Prints the manual page and exits.
189
190=item B<-oldroot>
191
192Specify where your B<KMail> mailboxes are to be found. By default assumes a
193folder called F<.Mail> or F<Mail> in your homedir, preferring F<.Mail> if it
194exists.
195
196=back
197
198=head1 DESCRIPTION
199
200B<md2mb.pl> will import a B<kmail> maildir environment, and transform it into a
201B<thunderbird> one. It relies on either B<formail> or B<procmail> being
202available in your search path, and will preferentially use B<formail>.
203
204By default, B<md2mb.pl> assumes that your B<kmail> mailboxes are stored in a
205folder called F<.Mail> or F<Mail> in your homedir. If this assumption is
206incorrect, you need to specify the correct folder with B<-oldroot>.
207
208=head1 AUTHOR
209
210=over 4
211
212=item *
213
214Bruno Cornec, http://brunocornec.wordpress.com
215
216=item *
217
218Edward Baudrez, C<< ebaudrez@cpan.org >>
219
220=back
221
222=head1 LICENSE
223
224Released under the GPLv2 or the Artistic license at your will.
225
226=cut
Note: See TracBrowser for help on using the repository browser.