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 | # CHANGE AS YOU WISH
|
---|
29 | my $oldroot = "/users/segolene/.Mail";
|
---|
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 | ) or pod2usage(2);
|
---|
44 | pod2usage(1) if ($help);
|
---|
45 | pod2usage(-exitstatus => 0, -verbose => 2) if ($man);
|
---|
46 |
|
---|
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
|
---|
60 | if ($debug) {
|
---|
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));
|
---|
63 | } else {
|
---|
64 | mkpath("$newroot",0, 0755) if ((not -d "$newroot") && (not $nrisfile));
|
---|
65 | }
|
---|
66 |
|
---|
67 | # the main work is done here
|
---|
68 | if ($debug) {
|
---|
69 | print "DESCENDING INTO oldroot($oldroot)\n";
|
---|
70 | }
|
---|
71 | find(\&md2mb,($oldroot));
|
---|
72 |
|
---|
73 | # rename `inbox' to `Inbox'
|
---|
74 | if ((-z "$newroot/Inbox") || (! -e "$newroot/Inbox")) {
|
---|
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 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | sub md2mb {
|
---|
84 |
|
---|
85 | if (-f $File::Find::name) {
|
---|
86 | if (($File::Find::name =~ /\.ids$/) ||
|
---|
87 | ($File::Find::name =~ /\.sorted$/) ||
|
---|
88 | ($File::Find::name =~ /\.index$/)) {
|
---|
89 | print "SKIP FILE: $File::Find::name\n" if ($debug);
|
---|
90 | return;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | if (-d $File::Find::name) {
|
---|
94 | if (($File::Find::name =~ /\/cur$/) ||
|
---|
95 | ($File::Find::name =~ /\/new$/) ||
|
---|
96 | ($File::Find::name =~ /\/tmp$/)) {
|
---|
97 | print "SKIP DIR: $File::Find::name\n" if ($debug);
|
---|
98 | return;
|
---|
99 | }
|
---|
100 | }
|
---|
101 | my $destname = $File::Find::name;
|
---|
102 | # Target name is under a different root dir
|
---|
103 | $destname =~ s|^$oldroot||;
|
---|
104 | # Target name is not under a .directory dir but under a .sdb one
|
---|
105 | $destname =~ s|\.([^/]+)\.directory/|$1.sbd/|g;
|
---|
106 | # Here we create the target dir and target name
|
---|
107 | my $outputfile="$newroot/$destname";
|
---|
108 | my $cdir = dirname("$outputfile");
|
---|
109 | # Handle case where target file name is empty
|
---|
110 | $outputfile="$newroot" if ($destname =~ /^\s*$/);
|
---|
111 | # When we treat a dir, we will have to handle what it has below
|
---|
112 | if (-d $File::Find::name) {
|
---|
113 | if ($debug) {
|
---|
114 | print "DIR SRC: $File::Find::name\n";
|
---|
115 | }
|
---|
116 | my @files = (bsd_glob("$File::Find::name/cur/*"),bsd_glob("$File::Find::name/new/*"));
|
---|
117 | if (@files) {
|
---|
118 | if ($debug) {
|
---|
119 | print "DIR ($File::Find::name) DIR TARGET: mkdir -p $cdir\n" if (not -d "$cdir");
|
---|
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
|
---|
128 | $file =~ s/'/'"'"'/g; # escape ' (single quote)
|
---|
129 | # NOTE! The output file must not contain single quotes (')!
|
---|
130 | my $run = "cat '$file' | $cmd >> '$outputfile'";
|
---|
131 | if ($debug) {
|
---|
132 | print "COPYING CONTENT maildir($file) to $outputfile\n";
|
---|
133 | print "CMD: $run\n";
|
---|
134 | } else {
|
---|
135 | print "Copying maildir content from $file to $outputfile\n";
|
---|
136 | system($run) == 0 or warn "cannot run \"$run\".";
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 | if (-f $File::Find::name) {
|
---|
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 | }
|
---|
147 | if ($debug) {
|
---|
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";
|
---|
150 | } else {
|
---|
151 | print "Copying mailbox content from $File::Find::name to $cdir\n";
|
---|
152 | mkpath("$cdir",0, 0755) if (not -d "$cdir");
|
---|
153 | copy($File::Find::name,$cdir);
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|
157 | __END__
|
---|
158 |
|
---|
159 | =head1 SYNOPSIS
|
---|
160 |
|
---|
161 | md2mb.pl [options]
|
---|
162 |
|
---|
163 | Options:
|
---|
164 | -debug | -d debug mode
|
---|
165 | -help | -h brief help message
|
---|
166 | -man | -m full documentation
|
---|
167 |
|
---|
168 | =head1 OPTIONS
|
---|
169 |
|
---|
170 | =over 4
|
---|
171 |
|
---|
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 |
|
---|
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
|
---|
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>.
|
---|
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
|
---|