| 1 | #!/usr/bin/perl -w |
|---|
| 2 | |
|---|
| 3 | =head1 NAME |
|---|
| 4 | |
|---|
| 5 | md2mb.pl - Import a maildir kmail environment into a thunderbird one |
|---|
| 6 | |
|---|
| 7 | =cut |
|---|
| 8 | |
|---|
| 9 | use strict; |
|---|
| 10 | use File::Find; |
|---|
| 11 | use File::Copy; |
|---|
| 12 | use File::Basename; |
|---|
| 13 | use File::Path; |
|---|
| 14 | use File::Glob ':glob'; |
|---|
| 15 | use Getopt::Long; |
|---|
| 16 | use Pod::Usage; |
|---|
| 17 | use List::Util qw(first); |
|---|
| 18 | |
|---|
| 19 | # settings |
|---|
| 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 |
|---|
| 28 | my $oldroot = first { -d } "$ENV{HOME}/.Mail", "$ENV{HOME}/Mail"; |
|---|
| 29 | # CHANGE AS YOU WISH |
|---|
| 30 | my $newroot = "/users/segolene/.thunderbird/qk2f4dl6.default/Mail/pop.home.musique-ancienne.org/"; |
|---|
| 31 | # Is the newroot a file (1) or a dir (0) |
|---|
| 32 | my $nrisfile = 0; |
|---|
| 33 | # END CHANGE |
|---|
| 34 | my $debug = 0; |
|---|
| 35 | my $help = 0; |
|---|
| 36 | my $man = 0; |
|---|
| 37 | |
|---|
| 38 | # parse command-line options |
|---|
| 39 | GetOptions( |
|---|
| 40 | 'debug|d' => \$debug, |
|---|
| 41 | 'help|h' => \$help, |
|---|
| 42 | 'man|m' => \$man, |
|---|
| 43 | 'oldroot|old|o' => \$oldroot, |
|---|
| 44 | ) or pod2usage(2); |
|---|
| 45 | pod2usage(1) if ($help); |
|---|
| 46 | pod2usage(-exitstatus => 0, -verbose => 2) if ($man); |
|---|
| 47 | |
|---|
| 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 | |
|---|
| 60 | # some sanity checks before proceeding |
|---|
| 61 | -d $oldroot or die "cannot find mailbox root `$oldroot'"; |
|---|
| 62 | |
|---|
| 63 | # create destination path |
|---|
| 64 | if ($debug) { |
|---|
| 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)); |
|---|
| 67 | } else { |
|---|
| 68 | mkpath("$newroot",0, 0755) if ((not -d "$newroot") && (not $nrisfile)); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | # the main work is done here |
|---|
| 72 | if ($debug) { |
|---|
| 73 | print "DESCENDING INTO oldroot($oldroot)\n"; |
|---|
| 74 | } |
|---|
| 75 | find(\&md2mb,($oldroot)); |
|---|
| 76 | |
|---|
| 77 | # rename `inbox' to `Inbox' |
|---|
| 78 | if ((-z "$newroot/Inbox") || (! -e "$newroot/Inbox")) { |
|---|
| 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 | } |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | sub md2mb { |
|---|
| 88 | |
|---|
| 89 | if (-f $File::Find::name) { |
|---|
| 90 | if (($File::Find::name =~ /\.ids$/) || |
|---|
| 91 | ($File::Find::name =~ /\.sorted$/) || |
|---|
| 92 | ($File::Find::name =~ /\.index$/)) { |
|---|
| 93 | print "SKIP FILE: $File::Find::name\n" if ($debug); |
|---|
| 94 | return; |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | if (-d $File::Find::name) { |
|---|
| 98 | if (($File::Find::name =~ /\/cur$/) || |
|---|
| 99 | ($File::Find::name =~ /\/new$/) || |
|---|
| 100 | ($File::Find::name =~ /\/tmp$/)) { |
|---|
| 101 | print "SKIP DIR: $File::Find::name\n" if ($debug); |
|---|
| 102 | return; |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | my $destname = $File::Find::name; |
|---|
| 106 | # Target name is under a different root dir |
|---|
| 107 | $destname =~ s|^$oldroot||; |
|---|
| 108 | # Target name is not under a .directory dir but under a .sdb one |
|---|
| 109 | $destname =~ s|\.([^/]+)\.directory/|$1.sbd/|g; |
|---|
| 110 | # Here we create the target dir and target name |
|---|
| 111 | my $outputfile="$newroot/$destname"; |
|---|
| 112 | my $cdir = dirname("$outputfile"); |
|---|
| 113 | # Handle case where target file name is empty |
|---|
| 114 | $outputfile="$newroot" if ($destname =~ /^\s*$/); |
|---|
| 115 | # When we treat a dir, we will have to handle what it has below |
|---|
| 116 | if (-d $File::Find::name) { |
|---|
| 117 | if ($debug) { |
|---|
| 118 | print "DIR SRC: $File::Find::name\n"; |
|---|
| 119 | } |
|---|
| 120 | my @files = (bsd_glob("$File::Find::name/cur/*"),bsd_glob("$File::Find::name/new/*")); |
|---|
| 121 | if (@files) { |
|---|
| 122 | if ($debug) { |
|---|
| 123 | print "DIR ($File::Find::name) DIR TARGET: mkdir -p $cdir\n" if (not -d "$cdir"); |
|---|
| 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 |
|---|
| 132 | $file =~ s/'/'"'"'/g; # escape ' (single quote) |
|---|
| 133 | # NOTE! The output file must not contain single quotes (')! |
|---|
| 134 | my $run = "cat '$file' | $cmd >> '$outputfile'"; |
|---|
| 135 | if ($debug) { |
|---|
| 136 | print "COPYING CONTENT maildir($file) to $outputfile\n"; |
|---|
| 137 | print "CMD: $run\n"; |
|---|
| 138 | } else { |
|---|
| 139 | print "Copying maildir content from $file to $outputfile\n"; |
|---|
| 140 | system($run) == 0 or warn "cannot run \"$run\"."; |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | if (-f $File::Find::name) { |
|---|
| 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 | } |
|---|
| 151 | if ($debug) { |
|---|
| 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"; |
|---|
| 154 | } else { |
|---|
| 155 | print "Copying mailbox content from $File::Find::name to $cdir\n"; |
|---|
| 156 | mkpath("$cdir",0, 0755) if (not -d "$cdir"); |
|---|
| 157 | copy($File::Find::name,$cdir); |
|---|
| 158 | } |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | __END__ |
|---|
| 162 | |
|---|
| 163 | =head1 SYNOPSIS |
|---|
| 164 | |
|---|
| 165 | md2mb.pl [options] |
|---|
| 166 | |
|---|
| 167 | Options: |
|---|
| 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 |
|---|
| 172 | |
|---|
| 173 | =head1 OPTIONS |
|---|
| 174 | |
|---|
| 175 | =over 4 |
|---|
| 176 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 196 | =back |
|---|
| 197 | |
|---|
| 198 | =head1 DESCRIPTION |
|---|
| 199 | |
|---|
| 200 | B<md2mb.pl> will import a B<kmail> maildir environment, and transform it into a |
|---|
| 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>. |
|---|
| 203 | |
|---|
| 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 | |
|---|
| 208 | =head1 AUTHOR |
|---|
| 209 | |
|---|
| 210 | =over 4 |
|---|
| 211 | |
|---|
| 212 | =item * |
|---|
| 213 | |
|---|
| 214 | Bruno Cornec, http://brunocornec.wordpress.com |
|---|
| 215 | |
|---|
| 216 | =item * |
|---|
| 217 | |
|---|
| 218 | Edward Baudrez, C<< ebaudrez@cpan.org >> |
|---|
| 219 | |
|---|
| 220 | =back |
|---|
| 221 | |
|---|
| 222 | =head1 LICENSE |
|---|
| 223 | |
|---|
| 224 | Released under the GPLv2 or the Artistic license at your will. |
|---|
| 225 | |
|---|
| 226 | =cut |
|---|