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