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