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