[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
|
---|
| 29 | # Is the newroot a file (1) or a dir (0)
|
---|
| 30 | my $nrisfile = 0;
|
---|
[1446] | 31 | # END CHANGE
|
---|
[1141] | 32 | my $debug = 0;
|
---|
[1445] | 33 | my $help = 0;
|
---|
| 34 | my $man = 0;
|
---|
[1449] | 35 | my $oldroot = first { -d } "$ENV{HOME}/.Mail", "$ENV{HOME}/Mail";
|
---|
| 36 | my @default_profiles = bsd_glob("$ENV{HOME}/.thunderbird/*.default");
|
---|
| 37 | my $profile = shift @default_profiles;
|
---|
| 38 | # if there is more than one default profile (quite unlikely, but you never
|
---|
| 39 | # know), unset $profile again and let the user resolve this
|
---|
| 40 | if (@default_profiles) { undef $profile }
|
---|
| 41 | my $subdir;
|
---|
[1141] | 42 |
|
---|
[1445] | 43 | # parse command-line options
|
---|
| 44 | GetOptions(
|
---|
[1449] | 45 | 'debug|d' => \$debug,
|
---|
| 46 | 'help|h' => \$help,
|
---|
| 47 | 'man|m' => \$man,
|
---|
| 48 | 'oldroot|old|o=s' => \$oldroot,
|
---|
| 49 | 'profile|p=s' => \$profile,
|
---|
| 50 | 'subdir|s=s' => \$subdir,
|
---|
[1445] | 51 | ) or pod2usage(2);
|
---|
| 52 | pod2usage(1) if ($help);
|
---|
| 53 | pod2usage(-exitstatus => 0, -verbose => 2) if ($man);
|
---|
[1449] | 54 | defined $subdir or pod2usage("Error: -subdir is a mandatory argument\n");
|
---|
[1445] | 55 |
|
---|
[1449] | 56 | # the new root is constructed from the profile and the chosen subdirectory name
|
---|
| 57 | my $newroot = "$profile/$subdir";
|
---|
| 58 |
|
---|
[1446] | 59 | # debug mode overview
|
---|
| 60 | if ($debug) {
|
---|
| 61 | print <<EOF;
|
---|
| 62 | DEBUG MODE, not doing anything, just printing
|
---|
| 63 | --- current state ---
|
---|
| 64 | cmd = $cmd
|
---|
| 65 | oldroot = $oldroot
|
---|
[1449] | 66 | profile = $profile
|
---|
| 67 | subdir = $subdir
|
---|
| 68 | newroot = $newroot (constructed)
|
---|
[1446] | 69 | --- end ---
|
---|
| 70 | EOF
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[1448] | 73 | # some sanity checks before proceeding
|
---|
| 74 | -d $oldroot or die "cannot find mailbox root `$oldroot'";
|
---|
[1449] | 75 | -d $profile or die "cannot find Thunderbird profile directory `$profile'";
|
---|
[1448] | 76 |
|
---|
[1446] | 77 | # create destination path
|
---|
[1141] | 78 | if ($debug) {
|
---|
[1389] | 79 | print "TARGET DIR: mkdir -p $newroot\n" if ((not -d "$newroot") && (not $nrisfile));
|
---|
| 80 | print "CMD: mkdir -p $newroot\n" if ((not -d "$newroot") && (not $nrisfile));
|
---|
[1141] | 81 | } else {
|
---|
| 82 | mkpath("$newroot",0, 0755) if ((not -d "$newroot") && (not $nrisfile));
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[1446] | 85 | # the main work is done here
|
---|
| 86 | if ($debug) {
|
---|
| 87 | print "DESCENDING INTO oldroot($oldroot)\n";
|
---|
| 88 | }
|
---|
[1141] | 89 | find(\&md2mb,($oldroot));
|
---|
| 90 |
|
---|
[1446] | 91 | # rename `inbox' to `Inbox'
|
---|
[1390] | 92 | if ((-z "$newroot/Inbox") || (! -e "$newroot/Inbox")) {
|
---|
[1446] | 93 | if ($debug) {
|
---|
| 94 | print "RENAMING inbox($newroot/inbox) INTO Inbox($newroot/Inbox)\n" if (-e "$newroot/inbox");
|
---|
| 95 | } else {
|
---|
| 96 | print "Renaming inbox into Inbox\n";
|
---|
| 97 | move("$newroot/inbox","$newroot/Inbox") if (-e "$newroot/inbox");
|
---|
| 98 | }
|
---|
[1390] | 99 | }
|
---|
| 100 |
|
---|
[1141] | 101 | sub md2mb {
|
---|
| 102 |
|
---|
| 103 | if (-f $File::Find::name) {
|
---|
[1389] | 104 | if (($File::Find::name =~ /\.ids$/) ||
|
---|
[1141] | 105 | ($File::Find::name =~ /\.sorted$/) ||
|
---|
[1389] | 106 | ($File::Find::name =~ /\.index$/)) {
|
---|
| 107 | print "SKIP FILE: $File::Find::name\n" if ($debug);
|
---|
| 108 | return;
|
---|
| 109 | }
|
---|
[1141] | 110 | }
|
---|
| 111 | if (-d $File::Find::name) {
|
---|
[1389] | 112 | if (($File::Find::name =~ /\/cur$/) ||
|
---|
[1141] | 113 | ($File::Find::name =~ /\/new$/) ||
|
---|
[1389] | 114 | ($File::Find::name =~ /\/tmp$/)) {
|
---|
| 115 | print "SKIP DIR: $File::Find::name\n" if ($debug);
|
---|
| 116 | return;
|
---|
| 117 | }
|
---|
[1141] | 118 | }
|
---|
| 119 | my $destname = $File::Find::name;
|
---|
[1389] | 120 | # Target name is under a different root dir
|
---|
[1141] | 121 | $destname =~ s|^$oldroot||;
|
---|
[1389] | 122 | # Target name is not under a .directory dir but under a .sdb one
|
---|
[1390] | 123 | $destname =~ s|\.([^/]+)\.directory/|$1.sbd/|g;
|
---|
[1389] | 124 | # Here we create the target dir and target name
|
---|
[1141] | 125 | my $outputfile="$newroot/$destname";
|
---|
[1412] | 126 | my $cdir = dirname("$outputfile");
|
---|
[1389] | 127 | # Handle case where target file name is empty
|
---|
[1141] | 128 | $outputfile="$newroot" if ($destname =~ /^\s*$/);
|
---|
[1389] | 129 | # When we treat a dir, we will have to handle what it has below
|
---|
[1141] | 130 | if (-d $File::Find::name) {
|
---|
[1328] | 131 | if ($debug) {
|
---|
[1389] | 132 | print "DIR SRC: $File::Find::name\n";
|
---|
[1328] | 133 | }
|
---|
[1389] | 134 | my @files = (bsd_glob("$File::Find::name/cur/*"),bsd_glob("$File::Find::name/new/*"));
|
---|
[1141] | 135 | if (@files) {
|
---|
| 136 | if ($debug) {
|
---|
[1389] | 137 | print "DIR ($File::Find::name) DIR TARGET: mkdir -p $cdir\n" if (not -d "$cdir");
|
---|
[1141] | 138 | } else {
|
---|
| 139 | mkpath("$cdir",0, 0755) if (not -d "$cdir");
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | foreach my $file (@files) {
|
---|
| 143 | next unless -f $file; # skip non-regular files
|
---|
| 144 | next unless -s $file; # skip empty files
|
---|
| 145 | next unless -r $file; # skip unreadable files
|
---|
[1389] | 146 | $file =~ s/'/'"'"'/g; # escape ' (single quote)
|
---|
[1141] | 147 | # NOTE! The output file must not contain single quotes (')!
|
---|
| 148 | my $run = "cat '$file' | $cmd >> '$outputfile'";
|
---|
| 149 | if ($debug) {
|
---|
[1389] | 150 | print "COPYING CONTENT maildir($file) to $outputfile\n";
|
---|
| 151 | print "CMD: $run\n";
|
---|
[1141] | 152 | } else {
|
---|
[1389] | 153 | print "Copying maildir content from $file to $outputfile\n";
|
---|
[1141] | 154 | system($run) == 0 or warn "cannot run \"$run\".";
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 | if (-f $File::Find::name) {
|
---|
[1389] | 159 | if (($File::Find::name =~ /\/cur\//) ||
|
---|
| 160 | ($File::Find::name =~ /\/new\//) ||
|
---|
| 161 | ($File::Find::name =~ /\/tmp\//)) {
|
---|
| 162 | print "SKIP FILE: $File::Find::name\n" if ($debug);
|
---|
| 163 | return;
|
---|
| 164 | }
|
---|
[1141] | 165 | if ($debug) {
|
---|
[1389] | 166 | print "FILE ($File::Find::name) TARGET DIR: mkdir -p $cdir\n" if (not -d "$cdir");
|
---|
| 167 | print "CMD: cp $File::Find::name $cdir\n";
|
---|
[1141] | 168 | } else {
|
---|
[1389] | 169 | print "Copying mailbox content from $File::Find::name to $cdir\n";
|
---|
[1141] | 170 | mkpath("$cdir",0, 0755) if (not -d "$cdir");
|
---|
| 171 | copy($File::Find::name,$cdir);
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
[1445] | 175 | __END__
|
---|
| 176 |
|
---|
| 177 | =head1 SYNOPSIS
|
---|
| 178 |
|
---|
[1449] | 179 | md2mb.pl [options] -s name
|
---|
[1445] | 180 |
|
---|
| 181 | Options:
|
---|
[1448] | 182 | -debug | -d debug mode
|
---|
| 183 | -help | -h brief help message
|
---|
| 184 | -man | -m full documentation
|
---|
| 185 | -oldroot | -old | -o location of the KMail mailboxes
|
---|
[1449] | 186 | -profile | -p path to the Thunderbird profile to install to
|
---|
| 187 | -subdir | -s subdir that will be created to hold the
|
---|
| 188 | imported mails
|
---|
[1445] | 189 |
|
---|
| 190 | =head1 OPTIONS
|
---|
| 191 |
|
---|
| 192 | =over 4
|
---|
| 193 |
|
---|
[1446] | 194 | =item B<-debug>
|
---|
| 195 |
|
---|
| 196 | Enter debug mode. This will print what would be done. No commands are executed,
|
---|
| 197 | so this is safe to use when testing.
|
---|
| 198 |
|
---|
[1445] | 199 | =item B<-help>
|
---|
| 200 |
|
---|
| 201 | Print a brief help message and exits.
|
---|
| 202 |
|
---|
| 203 | =item B<-man>
|
---|
| 204 |
|
---|
| 205 | Prints the manual page and exits.
|
---|
| 206 |
|
---|
[1449] | 207 | =item B<-oldroot> I<path>
|
---|
[1448] | 208 |
|
---|
| 209 | Specify where your B<KMail> mailboxes are to be found. By default assumes a
|
---|
| 210 | folder called F<.Mail> or F<Mail> in your homedir, preferring F<.Mail> if it
|
---|
| 211 | exists.
|
---|
| 212 |
|
---|
[1449] | 213 | =item B<-profile> I<path>
|
---|
| 214 |
|
---|
| 215 | Specify the path to the Thunderbird profile. Your imported mails will go inside
|
---|
| 216 | a directory in this profile. By default uses the default profile in the
|
---|
| 217 | F<.thunderbird> directory in your home directory, if it is unique.
|
---|
| 218 |
|
---|
| 219 | =item B<-subdir> I<name>
|
---|
| 220 |
|
---|
| 221 | Specify the subdirectory I<name> that will be created inside the Thunderbird
|
---|
| 222 | profile to hold the imported mails. This option is mandatory; there is no
|
---|
| 223 | default.
|
---|
| 224 |
|
---|
[1445] | 225 | =back
|
---|
| 226 |
|
---|
[1449] | 227 | =head1 EXAMPLES
|
---|
| 228 |
|
---|
| 229 | # this will fetch all mails from the folder .Mail or Mail in your home
|
---|
| 230 | # directory, and import them into your default Thunderbird profile, in
|
---|
| 231 | # a directory called Mail/pop.home.musique-ancienne.org/
|
---|
| 232 | #
|
---|
| 233 | # usually, this is all you need to specify:
|
---|
| 234 |
|
---|
| 235 | perl md2mb.pl -s Mail/pop.home.musique-ancienne.org/
|
---|
| 236 |
|
---|
| 237 | # on your computer, the mails may end up in
|
---|
| 238 | # /users/segolene/.thunderbird/qk2f4dl6.default/Mail/pop.home.musique-ancienne.org/
|
---|
| 239 |
|
---|
| 240 | # if md2mb.pl cannot figure out where your default Thunderbird profile
|
---|
| 241 | # is, use the following:
|
---|
| 242 |
|
---|
| 243 | perl md2mb.pl -p ~/.thunderbird/qk2f4dl6.default -s Mail/pop.home.musique-ancienne.org/
|
---|
| 244 |
|
---|
[1445] | 245 | =head1 DESCRIPTION
|
---|
| 246 |
|
---|
| 247 | B<md2mb.pl> will import a B<kmail> maildir environment, and transform it into a
|
---|
[1447] | 248 | B<thunderbird> one. It relies on either B<formail> or B<procmail> being
|
---|
| 249 | available in your search path, and will preferentially use B<formail>.
|
---|
[1445] | 250 |
|
---|
[1448] | 251 | By default, B<md2mb.pl> assumes that your B<kmail> mailboxes are stored in a
|
---|
| 252 | folder called F<.Mail> or F<Mail> in your homedir. If this assumption is
|
---|
| 253 | incorrect, you need to specify the correct folder with B<-oldroot>.
|
---|
| 254 |
|
---|
[1449] | 255 | The mails will be imported into the Thunderbird profile that is either
|
---|
| 256 | specified with B<-profile> or determined automatically. The script will try to
|
---|
| 257 | determine the default Thunderbird profile automatically. If there is a folder
|
---|
| 258 | that ends in F<.default> in the F<.thunderbird> directory in your home dir, and
|
---|
| 259 | if it is unique, that one will be used.
|
---|
| 260 |
|
---|
| 261 | The mails finally end up in a subdirectory below the profile. You must specify
|
---|
| 262 | the subdirectory on the command line with B<-subdir>, as shown in the examples
|
---|
| 263 | above.
|
---|
| 264 |
|
---|
[1445] | 265 | =head1 AUTHOR
|
---|
| 266 |
|
---|
[1448] | 267 | =over 4
|
---|
| 268 |
|
---|
| 269 | =item *
|
---|
| 270 |
|
---|
[1445] | 271 | Bruno Cornec, http://brunocornec.wordpress.com
|
---|
| 272 |
|
---|
[1448] | 273 | =item *
|
---|
| 274 |
|
---|
| 275 | Edward Baudrez, C<< ebaudrez@cpan.org >>
|
---|
| 276 |
|
---|
| 277 | =back
|
---|
| 278 |
|
---|
[1445] | 279 | =head1 LICENSE
|
---|
| 280 |
|
---|
| 281 | Released under the GPLv2 or the Artistic license at your will.
|
---|
| 282 |
|
---|
| 283 | =cut
|
---|