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