[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";
|
---|
[1389] | 15 | my $newroot = "/users/segolene/.thunderbird/60q6hqwp.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 |
|
---|
| 33 | sub md2mb {
|
---|
| 34 |
|
---|
| 35 | if (-f $File::Find::name) {
|
---|
[1389] | 36 | if (($File::Find::name =~ /\.ids$/) ||
|
---|
[1141] | 37 | ($File::Find::name =~ /\.sorted$/) ||
|
---|
[1389] | 38 | ($File::Find::name =~ /\.index$/)) {
|
---|
| 39 | print "SKIP FILE: $File::Find::name\n" if ($debug);
|
---|
| 40 | return;
|
---|
| 41 | }
|
---|
[1141] | 42 | }
|
---|
| 43 | if (-d $File::Find::name) {
|
---|
[1389] | 44 | if (($File::Find::name =~ /\/cur$/) ||
|
---|
[1141] | 45 | ($File::Find::name =~ /\/new$/) ||
|
---|
[1389] | 46 | ($File::Find::name =~ /\/tmp$/)) {
|
---|
| 47 | print "SKIP DIR: $File::Find::name\n" if ($debug);
|
---|
| 48 | return;
|
---|
| 49 | }
|
---|
[1141] | 50 | }
|
---|
| 51 | my $destname = $File::Find::name;
|
---|
[1389] | 52 | # Target name is under a different root dir
|
---|
[1141] | 53 | $destname =~ s|^$oldroot||;
|
---|
[1389] | 54 | # Target name is not under a .directory dir but under a .sdb one
|
---|
[1141] | 55 | $destname =~ s|\.([[:alnum:]éèçàù\s]*)\.directory|$1.sbd|g;
|
---|
[1389] | 56 | # Here we create the target dir and target name
|
---|
[1141] | 57 | my $cdir = dirname("$newroot/$destname");
|
---|
| 58 | my $outputfile="$newroot/$destname";
|
---|
[1389] | 59 | # Handle case where target file name is empty
|
---|
[1141] | 60 | $outputfile="$newroot" if ($destname =~ /^\s*$/);
|
---|
[1389] | 61 | # When we treat a dir, we will have to handle what it has below
|
---|
[1141] | 62 | if (-d $File::Find::name) {
|
---|
[1328] | 63 | if ($debug) {
|
---|
[1389] | 64 | print "DIR SRC: $File::Find::name\n";
|
---|
[1328] | 65 | }
|
---|
[1389] | 66 | my @files = (bsd_glob("$File::Find::name/cur/*"),bsd_glob("$File::Find::name/new/*"));
|
---|
[1141] | 67 | if (@files) {
|
---|
| 68 | if ($debug) {
|
---|
[1389] | 69 | print "DIR ($File::Find::name) DIR TARGET: mkdir -p $cdir\n" if (not -d "$cdir");
|
---|
[1141] | 70 | } else {
|
---|
| 71 | mkpath("$cdir",0, 0755) if (not -d "$cdir");
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 | foreach my $file (@files) {
|
---|
| 75 | next unless -f $file; # skip non-regular files
|
---|
| 76 | next unless -s $file; # skip empty files
|
---|
| 77 | next unless -r $file; # skip unreadable files
|
---|
[1389] | 78 | $file =~ s/'/'"'"'/g; # escape ' (single quote)
|
---|
[1141] | 79 | # NOTE! The output file must not contain single quotes (')!
|
---|
| 80 | my $run = "cat '$file' | $cmd >> '$outputfile'";
|
---|
| 81 | if ($debug) {
|
---|
[1389] | 82 | print "COPYING CONTENT maildir($file) to $outputfile\n";
|
---|
| 83 | print "CMD: $run\n";
|
---|
[1141] | 84 | } else {
|
---|
[1389] | 85 | print "Copying maildir content from $file to $outputfile\n";
|
---|
[1141] | 86 | system($run) == 0 or warn "cannot run \"$run\".";
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | if (-f $File::Find::name) {
|
---|
[1389] | 91 | if (($File::Find::name =~ /\/cur\//) ||
|
---|
| 92 | ($File::Find::name =~ /\/new\//) ||
|
---|
| 93 | ($File::Find::name =~ /\/tmp\//)) {
|
---|
| 94 | print "SKIP FILE: $File::Find::name\n" if ($debug);
|
---|
| 95 | return;
|
---|
| 96 | }
|
---|
[1141] | 97 | if ($debug) {
|
---|
[1389] | 98 | print "FILE ($File::Find::name) TARGET DIR: mkdir -p $cdir\n" if (not -d "$cdir");
|
---|
| 99 | print "CMD: cp $File::Find::name $cdir\n";
|
---|
[1141] | 100 | } else {
|
---|
[1389] | 101 | print "Copying mailbox content from $File::Find::name to $cdir\n";
|
---|
[1141] | 102 | mkpath("$cdir",0, 0755) if (not -d "$cdir");
|
---|
| 103 | copy($File::Find::name,$cdir);
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | }
|
---|