[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;
|
---|
[1141] | 17 |
|
---|
[1445] | 18 | # settings
|
---|
[1141] | 19 | my $cmd="formail";
|
---|
| 20 | # CHANGE AS YOU WISH
|
---|
[1328] | 21 | my $oldroot = "/users/segolene/.Mail";
|
---|
[1390] | 22 | my $newroot = "/users/segolene/.thunderbird/qk2f4dl6.default/Mail/pop.home.musique-ancienne.org/";
|
---|
[1141] | 23 | # Is the newroot a file (1) or a dir (0)
|
---|
| 24 | my $nrisfile = 0;
|
---|
| 25 | my $debug = 0;
|
---|
| 26 | # END CHANGE
|
---|
[1445] | 27 | my $help = 0;
|
---|
| 28 | my $man = 0;
|
---|
[1141] | 29 |
|
---|
[1445] | 30 | # parse command-line options
|
---|
| 31 | GetOptions(
|
---|
| 32 | 'help|h' => \$help,
|
---|
| 33 | 'man|m' => \$man,
|
---|
| 34 | 'verbose|v' => sub { $debug++ },
|
---|
| 35 | ) or pod2usage(2);
|
---|
| 36 | pod2usage(1) if ($help);
|
---|
| 37 | pod2usage(-exitstatus => 0, -verbose => 2) if ($man);
|
---|
| 38 |
|
---|
[1141] | 39 | print "DEBUG MODE, not doing anything, just printing\n" if ($debug);
|
---|
| 40 | if ($debug) {
|
---|
[1389] | 41 | print "TARGET DIR: mkdir -p $newroot\n" if ((not -d "$newroot") && (not $nrisfile));
|
---|
| 42 | print "CMD: mkdir -p $newroot\n" if ((not -d "$newroot") && (not $nrisfile));
|
---|
[1141] | 43 | } else {
|
---|
| 44 | mkpath("$newroot",0, 0755) if ((not -d "$newroot") && (not $nrisfile));
|
---|
| 45 | }
|
---|
[1412] | 46 | system("$cmd </dev/null >/dev/null 2>/dev/null") == 0 or die "cannot find formail on your \$PATH!\nTry installing procmail\nAborting";
|
---|
[1141] | 47 |
|
---|
| 48 | find(\&md2mb,($oldroot));
|
---|
| 49 |
|
---|
[1390] | 50 | if ((-z "$newroot/Inbox") || (! -e "$newroot/Inbox")) {
|
---|
| 51 | print "Renaming inbox into Inbox\n";
|
---|
| 52 | move("$newroot/inbox","$newroot/Inbox") if (-e "$newroot/inbox");
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[1141] | 55 | sub md2mb {
|
---|
| 56 |
|
---|
| 57 | if (-f $File::Find::name) {
|
---|
[1389] | 58 | if (($File::Find::name =~ /\.ids$/) ||
|
---|
[1141] | 59 | ($File::Find::name =~ /\.sorted$/) ||
|
---|
[1389] | 60 | ($File::Find::name =~ /\.index$/)) {
|
---|
| 61 | print "SKIP FILE: $File::Find::name\n" if ($debug);
|
---|
| 62 | return;
|
---|
| 63 | }
|
---|
[1141] | 64 | }
|
---|
| 65 | if (-d $File::Find::name) {
|
---|
[1389] | 66 | if (($File::Find::name =~ /\/cur$/) ||
|
---|
[1141] | 67 | ($File::Find::name =~ /\/new$/) ||
|
---|
[1389] | 68 | ($File::Find::name =~ /\/tmp$/)) {
|
---|
| 69 | print "SKIP DIR: $File::Find::name\n" if ($debug);
|
---|
| 70 | return;
|
---|
| 71 | }
|
---|
[1141] | 72 | }
|
---|
| 73 | my $destname = $File::Find::name;
|
---|
[1389] | 74 | # Target name is under a different root dir
|
---|
[1141] | 75 | $destname =~ s|^$oldroot||;
|
---|
[1389] | 76 | # Target name is not under a .directory dir but under a .sdb one
|
---|
[1390] | 77 | $destname =~ s|\.([^/]+)\.directory/|$1.sbd/|g;
|
---|
[1389] | 78 | # Here we create the target dir and target name
|
---|
[1141] | 79 | my $outputfile="$newroot/$destname";
|
---|
[1412] | 80 | my $cdir = dirname("$outputfile");
|
---|
[1389] | 81 | # Handle case where target file name is empty
|
---|
[1141] | 82 | $outputfile="$newroot" if ($destname =~ /^\s*$/);
|
---|
[1389] | 83 | # When we treat a dir, we will have to handle what it has below
|
---|
[1141] | 84 | if (-d $File::Find::name) {
|
---|
[1328] | 85 | if ($debug) {
|
---|
[1389] | 86 | print "DIR SRC: $File::Find::name\n";
|
---|
[1328] | 87 | }
|
---|
[1389] | 88 | my @files = (bsd_glob("$File::Find::name/cur/*"),bsd_glob("$File::Find::name/new/*"));
|
---|
[1141] | 89 | if (@files) {
|
---|
| 90 | if ($debug) {
|
---|
[1389] | 91 | print "DIR ($File::Find::name) DIR TARGET: mkdir -p $cdir\n" if (not -d "$cdir");
|
---|
[1141] | 92 | } else {
|
---|
| 93 | mkpath("$cdir",0, 0755) if (not -d "$cdir");
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | foreach my $file (@files) {
|
---|
| 97 | next unless -f $file; # skip non-regular files
|
---|
| 98 | next unless -s $file; # skip empty files
|
---|
| 99 | next unless -r $file; # skip unreadable files
|
---|
[1389] | 100 | $file =~ s/'/'"'"'/g; # escape ' (single quote)
|
---|
[1141] | 101 | # NOTE! The output file must not contain single quotes (')!
|
---|
| 102 | my $run = "cat '$file' | $cmd >> '$outputfile'";
|
---|
| 103 | if ($debug) {
|
---|
[1389] | 104 | print "COPYING CONTENT maildir($file) to $outputfile\n";
|
---|
| 105 | print "CMD: $run\n";
|
---|
[1141] | 106 | } else {
|
---|
[1389] | 107 | print "Copying maildir content from $file to $outputfile\n";
|
---|
[1141] | 108 | system($run) == 0 or warn "cannot run \"$run\".";
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | if (-f $File::Find::name) {
|
---|
[1389] | 113 | if (($File::Find::name =~ /\/cur\//) ||
|
---|
| 114 | ($File::Find::name =~ /\/new\//) ||
|
---|
| 115 | ($File::Find::name =~ /\/tmp\//)) {
|
---|
| 116 | print "SKIP FILE: $File::Find::name\n" if ($debug);
|
---|
| 117 | return;
|
---|
| 118 | }
|
---|
[1141] | 119 | if ($debug) {
|
---|
[1389] | 120 | print "FILE ($File::Find::name) TARGET DIR: mkdir -p $cdir\n" if (not -d "$cdir");
|
---|
| 121 | print "CMD: cp $File::Find::name $cdir\n";
|
---|
[1141] | 122 | } else {
|
---|
[1389] | 123 | print "Copying mailbox content from $File::Find::name to $cdir\n";
|
---|
[1141] | 124 | mkpath("$cdir",0, 0755) if (not -d "$cdir");
|
---|
| 125 | copy($File::Find::name,$cdir);
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
[1445] | 129 | __END__
|
---|
| 130 |
|
---|
| 131 | =head1 SYNOPSIS
|
---|
| 132 |
|
---|
| 133 | md2mb.pl [options]
|
---|
| 134 |
|
---|
| 135 | Options:
|
---|
| 136 | -help brief help message
|
---|
| 137 | -man full documentation
|
---|
| 138 |
|
---|
| 139 | =head1 OPTIONS
|
---|
| 140 |
|
---|
| 141 | =over 4
|
---|
| 142 |
|
---|
| 143 | =item B<-help>
|
---|
| 144 |
|
---|
| 145 | Print a brief help message and exits.
|
---|
| 146 |
|
---|
| 147 | =item B<-man>
|
---|
| 148 |
|
---|
| 149 | Prints the manual page and exits.
|
---|
| 150 |
|
---|
| 151 | =back
|
---|
| 152 |
|
---|
| 153 | =head1 DESCRIPTION
|
---|
| 154 |
|
---|
| 155 | B<md2mb.pl> will import a B<kmail> maildir environment, and transform it into a
|
---|
| 156 | B<thunderbird> one.
|
---|
| 157 |
|
---|
| 158 | =head1 AUTHOR
|
---|
| 159 |
|
---|
| 160 | Bruno Cornec, http://brunocornec.wordpress.com
|
---|
| 161 |
|
---|
| 162 | =head1 LICENSE
|
---|
| 163 |
|
---|
| 164 | Released under the GPLv2 or the Artistic license at your will.
|
---|
| 165 |
|
---|
| 166 | =cut
|
---|