[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
|
---|
[1448] | 28 | my $oldroot = first { -d } "$ENV{HOME}/.Mail", "$ENV{HOME}/Mail";
|
---|
[1141] | 29 | # CHANGE AS YOU WISH
|
---|
[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(
|
---|
[1448] | 40 | 'debug|d' => \$debug,
|
---|
| 41 | 'help|h' => \$help,
|
---|
| 42 | 'man|m' => \$man,
|
---|
| 43 | 'oldroot|old|o' => \$oldroot,
|
---|
[1445] | 44 | ) or pod2usage(2);
|
---|
| 45 | pod2usage(1) if ($help);
|
---|
| 46 | pod2usage(-exitstatus => 0, -verbose => 2) if ($man);
|
---|
| 47 |
|
---|
[1446] | 48 | # debug mode overview
|
---|
| 49 | if ($debug) {
|
---|
| 50 | print <<EOF;
|
---|
| 51 | DEBUG MODE, not doing anything, just printing
|
---|
| 52 | --- current state ---
|
---|
| 53 | cmd = $cmd
|
---|
| 54 | oldroot = $oldroot
|
---|
| 55 | newroot = $newroot
|
---|
| 56 | --- end ---
|
---|
| 57 | EOF
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[1448] | 60 | # some sanity checks before proceeding
|
---|
| 61 | -d $oldroot or die "cannot find mailbox root `$oldroot'";
|
---|
| 62 |
|
---|
[1446] | 63 | # create destination path
|
---|
[1141] | 64 | if ($debug) {
|
---|
[1389] | 65 | print "TARGET DIR: mkdir -p $newroot\n" if ((not -d "$newroot") && (not $nrisfile));
|
---|
| 66 | print "CMD: mkdir -p $newroot\n" if ((not -d "$newroot") && (not $nrisfile));
|
---|
[1141] | 67 | } else {
|
---|
| 68 | mkpath("$newroot",0, 0755) if ((not -d "$newroot") && (not $nrisfile));
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[1446] | 71 | # the main work is done here
|
---|
| 72 | if ($debug) {
|
---|
| 73 | print "DESCENDING INTO oldroot($oldroot)\n";
|
---|
| 74 | }
|
---|
[1141] | 75 | find(\&md2mb,($oldroot));
|
---|
| 76 |
|
---|
[1446] | 77 | # rename `inbox' to `Inbox'
|
---|
[1390] | 78 | if ((-z "$newroot/Inbox") || (! -e "$newroot/Inbox")) {
|
---|
[1446] | 79 | if ($debug) {
|
---|
| 80 | print "RENAMING inbox($newroot/inbox) INTO Inbox($newroot/Inbox)\n" if (-e "$newroot/inbox");
|
---|
| 81 | } else {
|
---|
| 82 | print "Renaming inbox into Inbox\n";
|
---|
| 83 | move("$newroot/inbox","$newroot/Inbox") if (-e "$newroot/inbox");
|
---|
| 84 | }
|
---|
[1390] | 85 | }
|
---|
| 86 |
|
---|
[1141] | 87 | sub md2mb {
|
---|
| 88 |
|
---|
| 89 | if (-f $File::Find::name) {
|
---|
[1389] | 90 | if (($File::Find::name =~ /\.ids$/) ||
|
---|
[1141] | 91 | ($File::Find::name =~ /\.sorted$/) ||
|
---|
[1389] | 92 | ($File::Find::name =~ /\.index$/)) {
|
---|
| 93 | print "SKIP FILE: $File::Find::name\n" if ($debug);
|
---|
| 94 | return;
|
---|
| 95 | }
|
---|
[1141] | 96 | }
|
---|
| 97 | if (-d $File::Find::name) {
|
---|
[1389] | 98 | if (($File::Find::name =~ /\/cur$/) ||
|
---|
[1141] | 99 | ($File::Find::name =~ /\/new$/) ||
|
---|
[1389] | 100 | ($File::Find::name =~ /\/tmp$/)) {
|
---|
| 101 | print "SKIP DIR: $File::Find::name\n" if ($debug);
|
---|
| 102 | return;
|
---|
| 103 | }
|
---|
[1141] | 104 | }
|
---|
| 105 | my $destname = $File::Find::name;
|
---|
[1389] | 106 | # Target name is under a different root dir
|
---|
[1141] | 107 | $destname =~ s|^$oldroot||;
|
---|
[1389] | 108 | # Target name is not under a .directory dir but under a .sdb one
|
---|
[1390] | 109 | $destname =~ s|\.([^/]+)\.directory/|$1.sbd/|g;
|
---|
[1389] | 110 | # Here we create the target dir and target name
|
---|
[1141] | 111 | my $outputfile="$newroot/$destname";
|
---|
[1412] | 112 | my $cdir = dirname("$outputfile");
|
---|
[1389] | 113 | # Handle case where target file name is empty
|
---|
[1141] | 114 | $outputfile="$newroot" if ($destname =~ /^\s*$/);
|
---|
[1389] | 115 | # When we treat a dir, we will have to handle what it has below
|
---|
[1141] | 116 | if (-d $File::Find::name) {
|
---|
[1328] | 117 | if ($debug) {
|
---|
[1389] | 118 | print "DIR SRC: $File::Find::name\n";
|
---|
[1328] | 119 | }
|
---|
[1389] | 120 | my @files = (bsd_glob("$File::Find::name/cur/*"),bsd_glob("$File::Find::name/new/*"));
|
---|
[1141] | 121 | if (@files) {
|
---|
| 122 | if ($debug) {
|
---|
[1389] | 123 | print "DIR ($File::Find::name) DIR TARGET: mkdir -p $cdir\n" if (not -d "$cdir");
|
---|
[1141] | 124 | } else {
|
---|
| 125 | mkpath("$cdir",0, 0755) if (not -d "$cdir");
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | foreach my $file (@files) {
|
---|
| 129 | next unless -f $file; # skip non-regular files
|
---|
| 130 | next unless -s $file; # skip empty files
|
---|
| 131 | next unless -r $file; # skip unreadable files
|
---|
[1389] | 132 | $file =~ s/'/'"'"'/g; # escape ' (single quote)
|
---|
[1141] | 133 | # NOTE! The output file must not contain single quotes (')!
|
---|
| 134 | my $run = "cat '$file' | $cmd >> '$outputfile'";
|
---|
| 135 | if ($debug) {
|
---|
[1389] | 136 | print "COPYING CONTENT maildir($file) to $outputfile\n";
|
---|
| 137 | print "CMD: $run\n";
|
---|
[1141] | 138 | } else {
|
---|
[1389] | 139 | print "Copying maildir content from $file to $outputfile\n";
|
---|
[1141] | 140 | system($run) == 0 or warn "cannot run \"$run\".";
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | if (-f $File::Find::name) {
|
---|
[1389] | 145 | if (($File::Find::name =~ /\/cur\//) ||
|
---|
| 146 | ($File::Find::name =~ /\/new\//) ||
|
---|
| 147 | ($File::Find::name =~ /\/tmp\//)) {
|
---|
| 148 | print "SKIP FILE: $File::Find::name\n" if ($debug);
|
---|
| 149 | return;
|
---|
| 150 | }
|
---|
[1141] | 151 | if ($debug) {
|
---|
[1389] | 152 | print "FILE ($File::Find::name) TARGET DIR: mkdir -p $cdir\n" if (not -d "$cdir");
|
---|
| 153 | print "CMD: cp $File::Find::name $cdir\n";
|
---|
[1141] | 154 | } else {
|
---|
[1389] | 155 | print "Copying mailbox content from $File::Find::name to $cdir\n";
|
---|
[1141] | 156 | mkpath("$cdir",0, 0755) if (not -d "$cdir");
|
---|
| 157 | copy($File::Find::name,$cdir);
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
[1445] | 161 | __END__
|
---|
| 162 |
|
---|
| 163 | =head1 SYNOPSIS
|
---|
| 164 |
|
---|
| 165 | md2mb.pl [options]
|
---|
| 166 |
|
---|
| 167 | Options:
|
---|
[1448] | 168 | -debug | -d debug mode
|
---|
| 169 | -help | -h brief help message
|
---|
| 170 | -man | -m full documentation
|
---|
| 171 | -oldroot | -old | -o location of the KMail mailboxes
|
---|
[1445] | 172 |
|
---|
| 173 | =head1 OPTIONS
|
---|
| 174 |
|
---|
| 175 | =over 4
|
---|
| 176 |
|
---|
[1446] | 177 | =item B<-debug>
|
---|
| 178 |
|
---|
| 179 | Enter debug mode. This will print what would be done. No commands are executed,
|
---|
| 180 | so this is safe to use when testing.
|
---|
| 181 |
|
---|
[1445] | 182 | =item B<-help>
|
---|
| 183 |
|
---|
| 184 | Print a brief help message and exits.
|
---|
| 185 |
|
---|
| 186 | =item B<-man>
|
---|
| 187 |
|
---|
| 188 | Prints the manual page and exits.
|
---|
| 189 |
|
---|
[1448] | 190 | =item B<-oldroot>
|
---|
| 191 |
|
---|
| 192 | Specify where your B<KMail> mailboxes are to be found. By default assumes a
|
---|
| 193 | folder called F<.Mail> or F<Mail> in your homedir, preferring F<.Mail> if it
|
---|
| 194 | exists.
|
---|
| 195 |
|
---|
[1445] | 196 | =back
|
---|
| 197 |
|
---|
| 198 | =head1 DESCRIPTION
|
---|
| 199 |
|
---|
| 200 | B<md2mb.pl> will import a B<kmail> maildir environment, and transform it into a
|
---|
[1447] | 201 | B<thunderbird> one. It relies on either B<formail> or B<procmail> being
|
---|
| 202 | available in your search path, and will preferentially use B<formail>.
|
---|
[1445] | 203 |
|
---|
[1448] | 204 | By default, B<md2mb.pl> assumes that your B<kmail> mailboxes are stored in a
|
---|
| 205 | folder called F<.Mail> or F<Mail> in your homedir. If this assumption is
|
---|
| 206 | incorrect, you need to specify the correct folder with B<-oldroot>.
|
---|
| 207 |
|
---|
[1445] | 208 | =head1 AUTHOR
|
---|
| 209 |
|
---|
[1448] | 210 | =over 4
|
---|
| 211 |
|
---|
| 212 | =item *
|
---|
| 213 |
|
---|
[1445] | 214 | Bruno Cornec, http://brunocornec.wordpress.com
|
---|
| 215 |
|
---|
[1448] | 216 | =item *
|
---|
| 217 |
|
---|
| 218 | Edward Baudrez, C<< ebaudrez@cpan.org >>
|
---|
| 219 |
|
---|
| 220 | =back
|
---|
| 221 |
|
---|
[1445] | 222 | =head1 LICENSE
|
---|
| 223 |
|
---|
| 224 | Released under the GPLv2 or the Artistic license at your will.
|
---|
| 225 |
|
---|
| 226 | =cut
|
---|