| 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; |
|---|
| 10 | use File::Glob ':glob'; |
|---|
| 11 | |
|---|
| 12 | my $cmd="formail"; |
|---|
| 13 | # CHANGE AS YOU WISH |
|---|
| 14 | my $oldroot = "/users/segolene/.Mail"; |
|---|
| 15 | my $newroot = "/users/segolene/.thunderbird/qk2f4dl6.default/Mail/pop.home.musique-ancienne.org/"; |
|---|
| 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) { |
|---|
| 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)); |
|---|
| 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 | 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 | |
|---|
| 38 | sub md2mb { |
|---|
| 39 | |
|---|
| 40 | if (-f $File::Find::name) { |
|---|
| 41 | if (($File::Find::name =~ /\.ids$/) || |
|---|
| 42 | ($File::Find::name =~ /\.sorted$/) || |
|---|
| 43 | ($File::Find::name =~ /\.index$/)) { |
|---|
| 44 | print "SKIP FILE: $File::Find::name\n" if ($debug); |
|---|
| 45 | return; |
|---|
| 46 | } |
|---|
| 47 | } |
|---|
| 48 | if (-d $File::Find::name) { |
|---|
| 49 | if (($File::Find::name =~ /\/cur$/) || |
|---|
| 50 | ($File::Find::name =~ /\/new$/) || |
|---|
| 51 | ($File::Find::name =~ /\/tmp$/)) { |
|---|
| 52 | print "SKIP DIR: $File::Find::name\n" if ($debug); |
|---|
| 53 | return; |
|---|
| 54 | } |
|---|
| 55 | } |
|---|
| 56 | my $destname = $File::Find::name; |
|---|
| 57 | # Target name is under a different root dir |
|---|
| 58 | $destname =~ s|^$oldroot||; |
|---|
| 59 | # Target name is not under a .directory dir but under a .sdb one |
|---|
| 60 | $destname =~ s|\.([^/]+)\.directory/|$1.sbd/|g; |
|---|
| 61 | # Here we create the target dir and target name |
|---|
| 62 | my $cdir = dirname("$newroot/$destname"); |
|---|
| 63 | my $outputfile="$newroot/$destname"; |
|---|
| 64 | # Handle case where target file name is empty |
|---|
| 65 | $outputfile="$newroot" if ($destname =~ /^\s*$/); |
|---|
| 66 | # When we treat a dir, we will have to handle what it has below |
|---|
| 67 | if (-d $File::Find::name) { |
|---|
| 68 | if ($debug) { |
|---|
| 69 | print "DIR SRC: $File::Find::name\n"; |
|---|
| 70 | } |
|---|
| 71 | my @files = (bsd_glob("$File::Find::name/cur/*"),bsd_glob("$File::Find::name/new/*")); |
|---|
| 72 | if (@files) { |
|---|
| 73 | if ($debug) { |
|---|
| 74 | print "DIR ($File::Find::name) DIR TARGET: mkdir -p $cdir\n" if (not -d "$cdir"); |
|---|
| 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 |
|---|
| 83 | $file =~ s/'/'"'"'/g; # escape ' (single quote) |
|---|
| 84 | # NOTE! The output file must not contain single quotes (')! |
|---|
| 85 | my $run = "cat '$file' | $cmd >> '$outputfile'"; |
|---|
| 86 | if ($debug) { |
|---|
| 87 | print "COPYING CONTENT maildir($file) to $outputfile\n"; |
|---|
| 88 | print "CMD: $run\n"; |
|---|
| 89 | } else { |
|---|
| 90 | print "Copying maildir content from $file to $outputfile\n"; |
|---|
| 91 | system($run) == 0 or warn "cannot run \"$run\"."; |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | if (-f $File::Find::name) { |
|---|
| 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 | } |
|---|
| 102 | if ($debug) { |
|---|
| 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"; |
|---|
| 105 | } else { |
|---|
| 106 | print "Copying mailbox content from $File::Find::name to $cdir\n"; |
|---|
| 107 | mkpath("$cdir",0, 0755) if (not -d "$cdir"); |
|---|
| 108 | copy($File::Find::name,$cdir); |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | } |
|---|