[1141] | 1 | #!/usr/bin/perl -w
|
---|
| 2 | #
|
---|
| 3 | # Program to import a maildir kmail environement into a thunderbird one.
|
---|
[1391] | 4 | #
|
---|
| 5 | # Released under the GPLv2 or the Artistic license at your will.
|
---|
[1141] | 6 |
|
---|
| 7 | use strict;
|
---|
| 8 | use File::Find;
|
---|
| 9 | use File::Copy;
|
---|
| 10 | use File::Basename;
|
---|
| 11 | use File::Path;
|
---|
[1389] | 12 | use File::Glob ':glob';
|
---|
[1141] | 13 |
|
---|
| 14 | my $cmd="formail";
|
---|
| 15 | # CHANGE AS YOU WISH
|
---|
[1328] | 16 | my $oldroot = "/users/segolene/.Mail";
|
---|
[1390] | 17 | my $newroot = "/users/segolene/.thunderbird/qk2f4dl6.default/Mail/pop.home.musique-ancienne.org/";
|
---|
[1141] | 18 | # Is the newroot a file (1) or a dir (0)
|
---|
| 19 | my $nrisfile = 0;
|
---|
| 20 | my $debug = 0;
|
---|
| 21 | # END CHANGE
|
---|
| 22 |
|
---|
| 23 | $debug++ if ((defined $ARGV[0]) && ($ARGV[0] eq "-v"));
|
---|
| 24 | print "DEBUG MODE, not doing anything, just printing\n" if ($debug);
|
---|
| 25 | if ($debug) {
|
---|
[1389] | 26 | print "TARGET DIR: mkdir -p $newroot\n" if ((not -d "$newroot") && (not $nrisfile));
|
---|
| 27 | print "CMD: mkdir -p $newroot\n" if ((not -d "$newroot") && (not $nrisfile));
|
---|
[1141] | 28 | } else {
|
---|
| 29 | mkpath("$newroot",0, 0755) if ((not -d "$newroot") && (not $nrisfile));
|
---|
| 30 | }
|
---|
| 31 | system("$cmd </dev/null >/dev/null 2>/dev/null") == 0 or die "cannot find formail on your \$PATH!\nAborting";
|
---|
| 32 |
|
---|
| 33 | find(\&md2mb,($oldroot));
|
---|
| 34 |
|
---|
[1390] | 35 | if ((-z "$newroot/Inbox") || (! -e "$newroot/Inbox")) {
|
---|
| 36 | print "Renaming inbox into Inbox\n";
|
---|
| 37 | move("$newroot/inbox","$newroot/Inbox") if (-e "$newroot/inbox");
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[1141] | 40 | sub md2mb {
|
---|
| 41 |
|
---|
| 42 | if (-f $File::Find::name) {
|
---|
[1389] | 43 | if (($File::Find::name =~ /\.ids$/) ||
|
---|
[1141] | 44 | ($File::Find::name =~ /\.sorted$/) ||
|
---|
[1389] | 45 | ($File::Find::name =~ /\.index$/)) {
|
---|
| 46 | print "SKIP FILE: $File::Find::name\n" if ($debug);
|
---|
| 47 | return;
|
---|
| 48 | }
|
---|
[1141] | 49 | }
|
---|
| 50 | if (-d $File::Find::name) {
|
---|
[1389] | 51 | if (($File::Find::name =~ /\/cur$/) ||
|
---|
[1141] | 52 | ($File::Find::name =~ /\/new$/) ||
|
---|
[1389] | 53 | ($File::Find::name =~ /\/tmp$/)) {
|
---|
| 54 | print "SKIP DIR: $File::Find::name\n" if ($debug);
|
---|
| 55 | return;
|
---|
| 56 | }
|
---|
[1141] | 57 | }
|
---|
| 58 | my $destname = $File::Find::name;
|
---|
[1389] | 59 | # Target name is under a different root dir
|
---|
[1141] | 60 | $destname =~ s|^$oldroot||;
|
---|
[1389] | 61 | # Target name is not under a .directory dir but under a .sdb one
|
---|
[1390] | 62 | $destname =~ s|\.([^/]+)\.directory/|$1.sbd/|g;
|
---|
[1389] | 63 | # Here we create the target dir and target name
|
---|
[1141] | 64 | my $cdir = dirname("$newroot/$destname");
|
---|
| 65 | my $outputfile="$newroot/$destname";
|
---|
[1389] | 66 | # Handle case where target file name is empty
|
---|
[1141] | 67 | $outputfile="$newroot" if ($destname =~ /^\s*$/);
|
---|
[1389] | 68 | # When we treat a dir, we will have to handle what it has below
|
---|
[1141] | 69 | if (-d $File::Find::name) {
|
---|
[1328] | 70 | if ($debug) {
|
---|
[1389] | 71 | print "DIR SRC: $File::Find::name\n";
|
---|
[1328] | 72 | }
|
---|
[1389] | 73 | my @files = (bsd_glob("$File::Find::name/cur/*"),bsd_glob("$File::Find::name/new/*"));
|
---|
[1141] | 74 | if (@files) {
|
---|
| 75 | if ($debug) {
|
---|
[1389] | 76 | print "DIR ($File::Find::name) DIR TARGET: mkdir -p $cdir\n" if (not -d "$cdir");
|
---|
[1141] | 77 | } else {
|
---|
| 78 | mkpath("$cdir",0, 0755) if (not -d "$cdir");
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | foreach my $file (@files) {
|
---|
| 82 | next unless -f $file; # skip non-regular files
|
---|
| 83 | next unless -s $file; # skip empty files
|
---|
| 84 | next unless -r $file; # skip unreadable files
|
---|
[1389] | 85 | $file =~ s/'/'"'"'/g; # escape ' (single quote)
|
---|
[1141] | 86 | # NOTE! The output file must not contain single quotes (')!
|
---|
| 87 | my $run = "cat '$file' | $cmd >> '$outputfile'";
|
---|
| 88 | if ($debug) {
|
---|
[1389] | 89 | print "COPYING CONTENT maildir($file) to $outputfile\n";
|
---|
| 90 | print "CMD: $run\n";
|
---|
[1141] | 91 | } else {
|
---|
[1389] | 92 | print "Copying maildir content from $file to $outputfile\n";
|
---|
[1141] | 93 | system($run) == 0 or warn "cannot run \"$run\".";
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | if (-f $File::Find::name) {
|
---|
[1389] | 98 | if (($File::Find::name =~ /\/cur\//) ||
|
---|
| 99 | ($File::Find::name =~ /\/new\//) ||
|
---|
| 100 | ($File::Find::name =~ /\/tmp\//)) {
|
---|
| 101 | print "SKIP FILE: $File::Find::name\n" if ($debug);
|
---|
| 102 | return;
|
---|
| 103 | }
|
---|
[1141] | 104 | if ($debug) {
|
---|
[1389] | 105 | print "FILE ($File::Find::name) TARGET DIR: mkdir -p $cdir\n" if (not -d "$cdir");
|
---|
| 106 | print "CMD: cp $File::Find::name $cdir\n";
|
---|
[1141] | 107 | } else {
|
---|
[1389] | 108 | print "Copying mailbox content from $File::Find::name to $cdir\n";
|
---|
[1141] | 109 | mkpath("$cdir",0, 0755) if (not -d "$cdir");
|
---|
| 110 | copy($File::Find::name,$cdir);
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|