[1141] | 1 | #!/usr/bin/perl -w
|
---|
| 2 |
|
---|
[1445] | 3 | =head1 NAME
|
---|
| 4 |
|
---|
| 5 | md2mb.pl - Import a maildir kmail environment into a thunderbird one
|
---|
| 6 |
|
---|
| 7 | =cut
|
---|
| 8 |
|
---|
[1141] | 9 | use strict;
|
---|
| 10 | use File::Find;
|
---|
| 11 | use File::Copy;
|
---|
| 12 | use File::Basename;
|
---|
| 13 | use File::Path;
|
---|
[1389] | 14 | use File::Glob ':glob';
|
---|
[1445] | 15 | use Getopt::Long;
|
---|
| 16 | use Pod::Usage;
|
---|
[1447] | 17 | use List::Util qw(first);
|
---|
[1141] | 18 |
|
---|
[1445] | 19 | # settings
|
---|
[1453] | 20 | my $cmd = 'formail';
|
---|
[1141] | 21 | my $debug = 0;
|
---|
[1450] | 22 | my $file = 0; # is the newroot a file (1) or a dir (0) ?
|
---|
[1445] | 23 | my $help = 0;
|
---|
| 24 | my $man = 0;
|
---|
[1449] | 25 | my $oldroot = first { -d } "$ENV{HOME}/.Mail", "$ENV{HOME}/Mail";
|
---|
| 26 | my @default_profiles = bsd_glob("$ENV{HOME}/.thunderbird/*.default");
|
---|
| 27 | my $profile = shift @default_profiles;
|
---|
| 28 | # if there is more than one default profile (quite unlikely, but you never
|
---|
| 29 | # know), unset $profile again and let the user resolve this
|
---|
| 30 | if (@default_profiles) { undef $profile }
|
---|
| 31 | my $subdir;
|
---|
[1451] | 32 | my $touch = 0;
|
---|
[1141] | 33 |
|
---|
[1445] | 34 | # parse command-line options
|
---|
| 35 | GetOptions(
|
---|
[1456] | 36 | 'cmd|c=s' => \$cmd,
|
---|
[1454] | 37 | 'debug|d+' => \$debug,
|
---|
| 38 | 'file|f' => \$file,
|
---|
| 39 | 'help|h' => \$help,
|
---|
| 40 | 'man|m' => \$man,
|
---|
| 41 | 'oldroot|o=s' => \$oldroot,
|
---|
| 42 | 'profile|p=s' => \$profile,
|
---|
| 43 | 'subdir|s=s' => \$subdir,
|
---|
| 44 | 'touch|t' => \$touch,
|
---|
[1445] | 45 | ) or pod2usage(2);
|
---|
| 46 | pod2usage(1) if ($help);
|
---|
| 47 | pod2usage(-exitstatus => 0, -verbose => 2) if ($man);
|
---|
[1449] | 48 | defined $subdir or pod2usage("Error: -subdir is a mandatory argument\n");
|
---|
[1445] | 49 |
|
---|
[1449] | 50 | # the new root is constructed from the profile and the chosen subdirectory name
|
---|
[1455] | 51 | my $newroot = "$profile/$subdir" if defined $profile;
|
---|
[1449] | 52 |
|
---|
[1446] | 53 | # debug mode overview
|
---|
| 54 | if ($debug) {
|
---|
| 55 | print <<EOF;
|
---|
| 56 | DEBUG MODE, not doing anything, just printing
|
---|
| 57 | --- current state ---
|
---|
[1454] | 58 | debug = $debug
|
---|
[1446] | 59 | cmd = $cmd
|
---|
| 60 | oldroot = $oldroot
|
---|
[1449] | 61 | profile = $profile
|
---|
| 62 | subdir = $subdir
|
---|
| 63 | newroot = $newroot (constructed)
|
---|
[1446] | 64 | --- end ---
|
---|
| 65 | EOF
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[1448] | 68 | # some sanity checks before proceeding
|
---|
[1453] | 69 | system("$cmd </dev/null >/dev/null 2>/dev/null") == 0 or die <<EOF;
|
---|
| 70 | Cannot run `$cmd' or locate it in your \$PATH!
|
---|
| 71 |
|
---|
| 72 | I need the program `formail' to process your mailboxes. You can provide the
|
---|
| 73 | full path to the formail utility using the `-cmd' command-line switch.
|
---|
| 74 |
|
---|
| 75 | Perhaps `formail' is not installed on your system. Try installing the `formail'
|
---|
| 76 | package using your distribution's package manager. The formail utility might be
|
---|
| 77 | contained in the `procmail' package (e.g., Ubuntu 11.10), so you may need to
|
---|
| 78 | look for the latter instead.
|
---|
| 79 |
|
---|
| 80 | Aborting ...
|
---|
| 81 | EOF
|
---|
[1448] | 82 | -d $oldroot or die "cannot find mailbox root `$oldroot'";
|
---|
[1449] | 83 | -d $profile or die "cannot find Thunderbird profile directory `$profile'";
|
---|
[1448] | 84 |
|
---|
[1446] | 85 | # create destination path
|
---|
[1141] | 86 | if ($debug) {
|
---|
[1450] | 87 | print "TARGET DIR: mkdir -p $newroot\n" if ((not -d "$newroot") && (not $file));
|
---|
| 88 | print "CMD: mkdir -p $newroot\n" if ((not -d "$newroot") && (not $file));
|
---|
[1141] | 89 | } else {
|
---|
[1450] | 90 | mkpath("$newroot",0, 0755) if ((not -d "$newroot") && (not $file));
|
---|
[1141] | 91 | }
|
---|
| 92 |
|
---|
[1446] | 93 | # the main work is done here
|
---|
| 94 | if ($debug) {
|
---|
| 95 | print "DESCENDING INTO oldroot($oldroot)\n";
|
---|
| 96 | }
|
---|
[1141] | 97 | find(\&md2mb,($oldroot));
|
---|
| 98 |
|
---|
[1451] | 99 | # now go back again and create empty files corresponding to the `.sbd'
|
---|
| 100 | # directories
|
---|
| 101 | if ($touch) {
|
---|
| 102 | print "DESCENDING AGAIN INTO oldroot($oldroot)\n" if ($debug);
|
---|
| 103 | find(sub {
|
---|
| 104 | return if (! -d); # consider only directories ...
|
---|
| 105 | return if (! /\.sbd$/); # ... with the right name
|
---|
| 106 | s/\.sbd//; # strip .sbd suffix
|
---|
| 107 | if ($debug) {
|
---|
| 108 | print "WOULD CREATE file($_) IF NEEDED\n";
|
---|
| 109 | } else {
|
---|
| 110 | open my $fh, '>>', $_ or die "cannot open $_: $!";
|
---|
| 111 | }
|
---|
| 112 | }, $newroot);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[1446] | 115 | # rename `inbox' to `Inbox'
|
---|
[1390] | 116 | if ((-z "$newroot/Inbox") || (! -e "$newroot/Inbox")) {
|
---|
[1446] | 117 | if ($debug) {
|
---|
| 118 | print "RENAMING inbox($newroot/inbox) INTO Inbox($newroot/Inbox)\n" if (-e "$newroot/inbox");
|
---|
| 119 | } else {
|
---|
| 120 | print "Renaming inbox into Inbox\n";
|
---|
| 121 | move("$newroot/inbox","$newroot/Inbox") if (-e "$newroot/inbox");
|
---|
| 122 | }
|
---|
[1390] | 123 | }
|
---|
| 124 |
|
---|
[1141] | 125 | sub md2mb {
|
---|
| 126 |
|
---|
| 127 | if (-f $File::Find::name) {
|
---|
[1389] | 128 | if (($File::Find::name =~ /\.ids$/) ||
|
---|
[1141] | 129 | ($File::Find::name =~ /\.sorted$/) ||
|
---|
[1389] | 130 | ($File::Find::name =~ /\.index$/)) {
|
---|
| 131 | print "SKIP FILE: $File::Find::name\n" if ($debug);
|
---|
| 132 | return;
|
---|
| 133 | }
|
---|
[1141] | 134 | }
|
---|
| 135 | if (-d $File::Find::name) {
|
---|
[1389] | 136 | if (($File::Find::name =~ /\/cur$/) ||
|
---|
[1141] | 137 | ($File::Find::name =~ /\/new$/) ||
|
---|
[1389] | 138 | ($File::Find::name =~ /\/tmp$/)) {
|
---|
| 139 | print "SKIP DIR: $File::Find::name\n" if ($debug);
|
---|
| 140 | return;
|
---|
| 141 | }
|
---|
[1141] | 142 | }
|
---|
| 143 | my $destname = $File::Find::name;
|
---|
[1389] | 144 | # Target name is under a different root dir
|
---|
[1141] | 145 | $destname =~ s|^$oldroot||;
|
---|
[1389] | 146 | # Target name is not under a .directory dir but under a .sdb one
|
---|
[1390] | 147 | $destname =~ s|\.([^/]+)\.directory/|$1.sbd/|g;
|
---|
[1389] | 148 | # Here we create the target dir and target name
|
---|
[1141] | 149 | my $outputfile="$newroot/$destname";
|
---|
[1412] | 150 | my $cdir = dirname("$outputfile");
|
---|
[1389] | 151 | # Handle case where target file name is empty
|
---|
[1141] | 152 | $outputfile="$newroot" if ($destname =~ /^\s*$/);
|
---|
[1389] | 153 | # When we treat a dir, we will have to handle what it has below
|
---|
[1141] | 154 | if (-d $File::Find::name) {
|
---|
[1328] | 155 | if ($debug) {
|
---|
[1389] | 156 | print "DIR SRC: $File::Find::name\n";
|
---|
[1328] | 157 | }
|
---|
[1389] | 158 | my @files = (bsd_glob("$File::Find::name/cur/*"),bsd_glob("$File::Find::name/new/*"));
|
---|
[1141] | 159 | if (@files) {
|
---|
| 160 | if ($debug) {
|
---|
[1389] | 161 | print "DIR ($File::Find::name) DIR TARGET: mkdir -p $cdir\n" if (not -d "$cdir");
|
---|
[1141] | 162 | } else {
|
---|
| 163 | mkpath("$cdir",0, 0755) if (not -d "$cdir");
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | foreach my $file (@files) {
|
---|
| 167 | next unless -f $file; # skip non-regular files
|
---|
| 168 | next unless -s $file; # skip empty files
|
---|
| 169 | next unless -r $file; # skip unreadable files
|
---|
[1389] | 170 | $file =~ s/'/'"'"'/g; # escape ' (single quote)
|
---|
[1141] | 171 | # NOTE! The output file must not contain single quotes (')!
|
---|
| 172 | my $run = "cat '$file' | $cmd >> '$outputfile'";
|
---|
| 173 | if ($debug) {
|
---|
[1389] | 174 | print "COPYING CONTENT maildir($file) to $outputfile\n";
|
---|
| 175 | print "CMD: $run\n";
|
---|
[1141] | 176 | } else {
|
---|
[1389] | 177 | print "Copying maildir content from $file to $outputfile\n";
|
---|
[1141] | 178 | system($run) == 0 or warn "cannot run \"$run\".";
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 | if (-f $File::Find::name) {
|
---|
[1389] | 183 | if (($File::Find::name =~ /\/cur\//) ||
|
---|
| 184 | ($File::Find::name =~ /\/new\//) ||
|
---|
| 185 | ($File::Find::name =~ /\/tmp\//)) {
|
---|
| 186 | print "SKIP FILE: $File::Find::name\n" if ($debug);
|
---|
| 187 | return;
|
---|
| 188 | }
|
---|
[1141] | 189 | if ($debug) {
|
---|
[1389] | 190 | print "FILE ($File::Find::name) TARGET DIR: mkdir -p $cdir\n" if (not -d "$cdir");
|
---|
| 191 | print "CMD: cp $File::Find::name $cdir\n";
|
---|
[1141] | 192 | } else {
|
---|
[1389] | 193 | print "Copying mailbox content from $File::Find::name to $cdir\n";
|
---|
[1141] | 194 | mkpath("$cdir",0, 0755) if (not -d "$cdir");
|
---|
| 195 | copy($File::Find::name,$cdir);
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
[1445] | 199 | __END__
|
---|
| 200 |
|
---|
| 201 | =head1 SYNOPSIS
|
---|
| 202 |
|
---|
[1449] | 203 | md2mb.pl [options] -s name
|
---|
[1445] | 204 |
|
---|
| 205 | Options:
|
---|
[1454] | 206 | -cmd | -c command to process mail
|
---|
| 207 | -debug | -d debug mode
|
---|
| 208 | -file | -f whether $newroot is a file or a directory
|
---|
| 209 | -help | -h brief help message
|
---|
| 210 | -man | -m full documentation
|
---|
| 211 | -oldroot | -o location of the KMail mailboxes
|
---|
| 212 | -profile | -p path to the Thunderbird profile to install to
|
---|
| 213 | -subdir | -s subdir that will be created to hold the
|
---|
| 214 | imported mails
|
---|
| 215 | -touch | -t create empty files corresponding to .sdb dirs
|
---|
[1445] | 216 |
|
---|
| 217 | =head1 OPTIONS
|
---|
| 218 |
|
---|
| 219 | =over 4
|
---|
| 220 |
|
---|
[1453] | 221 | =item B<-cmd>
|
---|
| 222 |
|
---|
| 223 | Specify the command to process your maildirs. By default uses B<formail> in
|
---|
| 224 | your C<$PATH>, but you can use this option to specify the full path to the
|
---|
| 225 | executable to use instead.
|
---|
| 226 |
|
---|
[1446] | 227 | =item B<-debug>
|
---|
| 228 |
|
---|
| 229 | Enter debug mode. This will print what would be done. No commands are executed,
|
---|
| 230 | so this is safe to use when testing.
|
---|
| 231 |
|
---|
[1450] | 232 | =item B<-file>
|
---|
| 233 |
|
---|
| 234 | Specifies that $newroot is a file. If this option is omitted, it is assumed
|
---|
| 235 | that $newroot is a directory instead.
|
---|
| 236 |
|
---|
[1445] | 237 | =item B<-help>
|
---|
| 238 |
|
---|
| 239 | Print a brief help message and exits.
|
---|
| 240 |
|
---|
| 241 | =item B<-man>
|
---|
| 242 |
|
---|
| 243 | Prints the manual page and exits.
|
---|
| 244 |
|
---|
[1449] | 245 | =item B<-oldroot> I<path>
|
---|
[1448] | 246 |
|
---|
| 247 | Specify where your B<KMail> mailboxes are to be found. By default assumes a
|
---|
| 248 | folder called F<.Mail> or F<Mail> in your homedir, preferring F<.Mail> if it
|
---|
| 249 | exists.
|
---|
| 250 |
|
---|
[1449] | 251 | =item B<-profile> I<path>
|
---|
| 252 |
|
---|
| 253 | Specify the path to the Thunderbird profile. Your imported mails will go inside
|
---|
| 254 | a directory in this profile. By default uses the default profile in the
|
---|
| 255 | F<.thunderbird> directory in your home directory, if it is unique.
|
---|
| 256 |
|
---|
| 257 | =item B<-subdir> I<name>
|
---|
| 258 |
|
---|
| 259 | Specify the subdirectory I<name> that will be created inside the Thunderbird
|
---|
| 260 | profile to hold the imported mails. This option is mandatory; there is no
|
---|
| 261 | default.
|
---|
| 262 |
|
---|
[1451] | 263 | =item B<-touch>
|
---|
| 264 |
|
---|
| 265 | Create empty files corresponding to the F<.sbd> dirs created by this script.
|
---|
| 266 | Use this hack if your mails don't show up in Thunderbird.
|
---|
| 267 |
|
---|
[1445] | 268 | =back
|
---|
| 269 |
|
---|
[1449] | 270 | =head1 EXAMPLES
|
---|
| 271 |
|
---|
| 272 | # this will fetch all mails from the folder .Mail or Mail in your home
|
---|
| 273 | # directory, and import them into your default Thunderbird profile, in
|
---|
| 274 | # a directory called Mail/pop.home.musique-ancienne.org/
|
---|
| 275 | #
|
---|
| 276 | # usually, this is all you need to specify:
|
---|
| 277 |
|
---|
| 278 | perl md2mb.pl -s Mail/pop.home.musique-ancienne.org/
|
---|
| 279 |
|
---|
| 280 | # on your computer, the mails may end up in
|
---|
| 281 | # /users/segolene/.thunderbird/qk2f4dl6.default/Mail/pop.home.musique-ancienne.org/
|
---|
| 282 |
|
---|
| 283 | # if md2mb.pl cannot figure out where your default Thunderbird profile
|
---|
| 284 | # is, use the following:
|
---|
| 285 |
|
---|
| 286 | perl md2mb.pl -p ~/.thunderbird/qk2f4dl6.default -s Mail/pop.home.musique-ancienne.org/
|
---|
| 287 |
|
---|
[1445] | 288 | =head1 DESCRIPTION
|
---|
| 289 |
|
---|
| 290 | B<md2mb.pl> will import a B<kmail> maildir environment, and transform it into a
|
---|
[1453] | 291 | B<thunderbird> one. It relies on B<formail> (or an equivalent utility) being
|
---|
| 292 | available on your system.
|
---|
[1445] | 293 |
|
---|
[1448] | 294 | By default, B<md2mb.pl> assumes that your B<kmail> mailboxes are stored in a
|
---|
| 295 | folder called F<.Mail> or F<Mail> in your homedir. If this assumption is
|
---|
| 296 | incorrect, you need to specify the correct folder with B<-oldroot>.
|
---|
| 297 |
|
---|
[1449] | 298 | The mails will be imported into the Thunderbird profile that is either
|
---|
| 299 | specified with B<-profile> or determined automatically. The script will try to
|
---|
| 300 | determine the default Thunderbird profile automatically. If there is a folder
|
---|
| 301 | that ends in F<.default> in the F<.thunderbird> directory in your home dir, and
|
---|
| 302 | if it is unique, that one will be used.
|
---|
| 303 |
|
---|
| 304 | The mails finally end up in a subdirectory below the profile. You must specify
|
---|
| 305 | the subdirectory on the command line with B<-subdir>, as shown in the examples
|
---|
| 306 | above.
|
---|
| 307 |
|
---|
[1451] | 308 | If you have used a structure with subfolders in KMail, mails in subfolders
|
---|
| 309 | might not show up in Thunderbird. Remove the import, and run again with
|
---|
| 310 | B<-touch>.
|
---|
| 311 |
|
---|
[1445] | 312 | =head1 AUTHOR
|
---|
| 313 |
|
---|
[1448] | 314 | =over 4
|
---|
| 315 |
|
---|
| 316 | =item *
|
---|
| 317 |
|
---|
[1445] | 318 | Bruno Cornec, http://brunocornec.wordpress.com
|
---|
| 319 |
|
---|
[1448] | 320 | =item *
|
---|
| 321 |
|
---|
| 322 | Edward Baudrez, C<< ebaudrez@cpan.org >>
|
---|
| 323 |
|
---|
| 324 | =back
|
---|
| 325 |
|
---|
[1445] | 326 | =head1 LICENSE
|
---|
| 327 |
|
---|
| 328 | Released under the GPLv2 or the Artistic license at your will.
|
---|
| 329 |
|
---|
| 330 | =cut
|
---|