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

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

use either formail' or procmail'

Use either of those programs to process your mailboxes, whichever is
available, in that order of preference. Also, abort the program as early
as possible if neither program can be found.

File size: 5.0 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
28# CHANGE AS YOU WISH
29my $oldroot = "/users/segolene/.Mail";
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) or pod2usage(2);
44pod2usage(1) if ($help);
45pod2usage(-exitstatus => 0, -verbose => 2) if ($man);
46
47# debug mode overview
48if ($debug) {
49 print <<EOF;
50DEBUG MODE, not doing anything, just printing
51--- current state ---
52cmd = $cmd
53oldroot = $oldroot
54newroot = $newroot
55--- end ---
56EOF
57}
58
59# create destination path
60if ($debug) {
61 print "TARGET DIR: mkdir -p $newroot\n" if ((not -d "$newroot") && (not $nrisfile));
62 print "CMD: mkdir -p $newroot\n" if ((not -d "$newroot") && (not $nrisfile));
63} else {
64 mkpath("$newroot",0, 0755) if ((not -d "$newroot") && (not $nrisfile));
65}
66
67# the main work is done here
68if ($debug) {
69 print "DESCENDING INTO oldroot($oldroot)\n";
70}
71find(\&md2mb,($oldroot));
72
73# rename `inbox' to `Inbox'
74if ((-z "$newroot/Inbox") || (! -e "$newroot/Inbox")) {
75 if ($debug) {
76 print "RENAMING inbox($newroot/inbox) INTO Inbox($newroot/Inbox)\n" if (-e "$newroot/inbox");
77 } else {
78 print "Renaming inbox into Inbox\n";
79 move("$newroot/inbox","$newroot/Inbox") if (-e "$newroot/inbox");
80 }
81}
82
83sub md2mb {
84
85if (-f $File::Find::name) {
86 if (($File::Find::name =~ /\.ids$/) ||
87 ($File::Find::name =~ /\.sorted$/) ||
88 ($File::Find::name =~ /\.index$/)) {
89 print "SKIP FILE: $File::Find::name\n" if ($debug);
90 return;
91 }
92}
93if (-d $File::Find::name) {
94 if (($File::Find::name =~ /\/cur$/) ||
95 ($File::Find::name =~ /\/new$/) ||
96 ($File::Find::name =~ /\/tmp$/)) {
97 print "SKIP DIR: $File::Find::name\n" if ($debug);
98 return;
99 }
100}
101my $destname = $File::Find::name;
102# Target name is under a different root dir
103$destname =~ s|^$oldroot||;
104# Target name is not under a .directory dir but under a .sdb one
105$destname =~ s|\.([^/]+)\.directory/|$1.sbd/|g;
106# Here we create the target dir and target name
107my $outputfile="$newroot/$destname";
108my $cdir = dirname("$outputfile");
109# Handle case where target file name is empty
110$outputfile="$newroot" if ($destname =~ /^\s*$/);
111# When we treat a dir, we will have to handle what it has below
112if (-d $File::Find::name) {
113 if ($debug) {
114 print "DIR SRC: $File::Find::name\n";
115 }
116 my @files = (bsd_glob("$File::Find::name/cur/*"),bsd_glob("$File::Find::name/new/*"));
117 if (@files) {
118 if ($debug) {
119 print "DIR ($File::Find::name) DIR TARGET: mkdir -p $cdir\n" if (not -d "$cdir");
120 } else {
121 mkpath("$cdir",0, 0755) if (not -d "$cdir");
122 }
123 }
124 foreach my $file (@files) {
125 next unless -f $file; # skip non-regular files
126 next unless -s $file; # skip empty files
127 next unless -r $file; # skip unreadable files
128 $file =~ s/'/'"'"'/g; # escape ' (single quote)
129 # NOTE! The output file must not contain single quotes (')!
130 my $run = "cat '$file' | $cmd >> '$outputfile'";
131 if ($debug) {
132 print "COPYING CONTENT maildir($file) to $outputfile\n";
133 print "CMD: $run\n";
134 } else {
135 print "Copying maildir content from $file to $outputfile\n";
136 system($run) == 0 or warn "cannot run \"$run\".";
137 }
138 }
139}
140if (-f $File::Find::name) {
141 if (($File::Find::name =~ /\/cur\//) ||
142 ($File::Find::name =~ /\/new\//) ||
143 ($File::Find::name =~ /\/tmp\//)) {
144 print "SKIP FILE: $File::Find::name\n" if ($debug);
145 return;
146 }
147 if ($debug) {
148 print "FILE ($File::Find::name) TARGET DIR: mkdir -p $cdir\n" if (not -d "$cdir");
149 print "CMD: cp $File::Find::name $cdir\n";
150 } else {
151 print "Copying mailbox content from $File::Find::name to $cdir\n";
152 mkpath("$cdir",0, 0755) if (not -d "$cdir");
153 copy($File::Find::name,$cdir);
154 }
155}
156}
157__END__
158
159=head1 SYNOPSIS
160
161md2mb.pl [options]
162
163 Options:
164 -debug | -d debug mode
165 -help | -h brief help message
166 -man | -m full documentation
167
168=head1 OPTIONS
169
170=over 4
171
172=item B<-debug>
173
174Enter debug mode. This will print what would be done. No commands are executed,
175so this is safe to use when testing.
176
177=item B<-help>
178
179Print a brief help message and exits.
180
181=item B<-man>
182
183Prints the manual page and exits.
184
185=back
186
187=head1 DESCRIPTION
188
189B<md2mb.pl> will import a B<kmail> maildir environment, and transform it into a
190B<thunderbird> one. It relies on either B<formail> or B<procmail> being
191available in your search path, and will preferentially use B<formail>.
192
193=head1 AUTHOR
194
195Bruno Cornec, http://brunocornec.wordpress.com
196
197=head1 LICENSE
198
199Released under the GPLv2 or the Artistic license at your will.
200
201=cut
Note: See TracBrowser for help on using the repository browser.