1 | #!/usr/bin/perl -w
|
---|
2 | #
|
---|
3 | # Program to import a maildir kmail environement into a thunderbird one.
|
---|
4 |
|
---|
5 | use strict;
|
---|
6 | use File::Find;
|
---|
7 | use File::Copy;
|
---|
8 | use File::Basename;
|
---|
9 | use File::Path;
|
---|
10 |
|
---|
11 | my $cmd="formail";
|
---|
12 | # CHANGE AS YOU WISH
|
---|
13 | my $oldroot = "/users/segolene/.Mail";
|
---|
14 | my $newroot = "/users/segolene/.thunderbird/60q6hqwp.default/Mail/Local Folders/";
|
---|
15 | # Is the newroot a file (1) or a dir (0)
|
---|
16 | my $nrisfile = 0;
|
---|
17 | my $debug = 0;
|
---|
18 | # END CHANGE
|
---|
19 |
|
---|
20 | $debug++ if ((defined $ARGV[0]) && ($ARGV[0] eq "-v"));
|
---|
21 | print "DEBUG MODE, not doing anything, just printing\n" if ($debug);
|
---|
22 | if ($debug) {
|
---|
23 | print "CMD1: mkdir -p $newroot\n" if ((not -d "$newroot") && (not $nrisfile));
|
---|
24 | } else {
|
---|
25 | mkpath("$newroot",0, 0755) if ((not -d "$newroot") && (not $nrisfile));
|
---|
26 | }
|
---|
27 | system("$cmd </dev/null >/dev/null 2>/dev/null") == 0 or die "cannot find formail on your \$PATH!\nAborting";
|
---|
28 |
|
---|
29 | find(\&md2mb,($oldroot));
|
---|
30 |
|
---|
31 | sub md2mb {
|
---|
32 |
|
---|
33 | if (-f $File::Find::name) {
|
---|
34 | return if (
|
---|
35 | ($File::Find::name =~ /\.ids$/) ||
|
---|
36 | ($File::Find::name =~ /\.sorted$/) ||
|
---|
37 | ($File::Find::name =~ /\.index$/) ||
|
---|
38 | ($File::Find::name =~ /\/cur\//) ||
|
---|
39 | ($File::Find::name =~ /\/new\//) ||
|
---|
40 | ($File::Find::name =~ /\/tmp\//));
|
---|
41 | }
|
---|
42 | if (-d $File::Find::name) {
|
---|
43 | return if (
|
---|
44 | ($File::Find::name =~ /\/cur$/) ||
|
---|
45 | ($File::Find::name =~ /\/new$/) ||
|
---|
46 | ($File::Find::name =~ /\/tmp$/));
|
---|
47 | }
|
---|
48 | if ($debug) {
|
---|
49 | print "CURR: $File::Find::name\n";
|
---|
50 | }
|
---|
51 | my $destname = $File::Find::name;
|
---|
52 | $destname =~ s|^$oldroot||;
|
---|
53 | $destname =~ s|\.([[:alnum:]éèçàù\s]*)\.directory|$1.sbd|g;
|
---|
54 | if ($debug) {
|
---|
55 | print "DEST: $destname\n";
|
---|
56 | }
|
---|
57 | my $cdir = dirname("$newroot/$destname");
|
---|
58 | my $outputfile="$newroot/$destname";
|
---|
59 | $outputfile="$newroot" if ($destname =~ /^\s*$/);
|
---|
60 | if (-d $File::Find::name) {
|
---|
61 | if ($debug) {
|
---|
62 | print "HANDLING: $File::Find::name content\n";
|
---|
63 | }
|
---|
64 | my @files = (<"$File::Find::name"/cur/*>,<"$File::Find::name"/new/*>);
|
---|
65 | if (@files) {
|
---|
66 | if ($debug) {
|
---|
67 | print "CMD2: mkdir -p $cdir\n" if (not -d "$cdir");
|
---|
68 | } else {
|
---|
69 | mkpath("$cdir",0, 0755) if (not -d "$cdir");
|
---|
70 | }
|
---|
71 | }
|
---|
72 | foreach my $file (@files) {
|
---|
73 | next unless -f $file; # skip non-regular files
|
---|
74 | next unless -s $file; # skip empty files
|
---|
75 | next unless -r $file; # skip unreadable files
|
---|
76 | $file =~ s/'/'"'"'/; # escape ' (single quote)
|
---|
77 | # NOTE! The output file must not contain single quotes (')!
|
---|
78 | my $run = "cat '$file' | $cmd >> '$outputfile'";
|
---|
79 | if ($debug) {
|
---|
80 | print "CMD3: $run\n";
|
---|
81 | } else {
|
---|
82 | print "Copying maildir content from $File::Find::name to $outputfile\n";
|
---|
83 | system($run) == 0 or warn "cannot run \"$run\".";
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 | if (-f $File::Find::name) {
|
---|
88 | if ($debug) {
|
---|
89 | print "CMD2: mkdir -p $cdir\n" if (not -d "$cdir");
|
---|
90 | print "CMD3: cp $File::Find::name $cdir\n";
|
---|
91 | } else {
|
---|
92 | mkpath("$cdir",0, 0755) if (not -d "$cdir");
|
---|
93 | copy($File::Find::name,$cdir);
|
---|
94 | print "Copying mailbox content from $File::Find::name to $outputfile\n";
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|