source: ProjectBuilder/projects/md2mb/devel/md2mb/bin/md2mb.pl@ 1328

Last change on this file since 1328 was 1328, checked in by Bruno Cornec, 13 years ago
  • Fix a problem with spaces in folder names
File size: 2.8 KB
Line 
1#!/usr/bin/perl -w
2#
3# Program to import a maildir kmail environement into a thunderbird one.
4
5use strict;
6use File::Find;
7use File::Copy;
8use File::Basename;
9use File::Path;
10
11my $cmd="formail";
12# CHANGE AS YOU WISH
13my $oldroot = "/users/segolene/.Mail";
14my $newroot = "/users/segolene/.thunderbird/60q6hqwp.default/Mail/Local Folders/";
15# Is the newroot a file (1) or a dir (0)
16my $nrisfile = 0;
17my $debug = 0;
18# END CHANGE
19
20$debug++ if ((defined $ARGV[0]) && ($ARGV[0] eq "-v"));
21print "DEBUG MODE, not doing anything, just printing\n" if ($debug);
22if ($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}
27system("$cmd </dev/null >/dev/null 2>/dev/null") == 0 or die "cannot find formail on your \$PATH!\nAborting";
28
29find(\&md2mb,($oldroot));
30
31sub md2mb {
32
33if (-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}
42if (-d $File::Find::name) {
43 return if (
44 ($File::Find::name =~ /\/cur$/) ||
45 ($File::Find::name =~ /\/new$/) ||
46 ($File::Find::name =~ /\/tmp$/));
47}
48if ($debug) {
49 print "CURR: $File::Find::name\n";
50}
51my $destname = $File::Find::name;
52$destname =~ s|^$oldroot||;
53$destname =~ s|\.([[:alnum:]éèçàù\s]*)\.directory|$1.sbd|g;
54if ($debug) {
55 print "DEST: $destname\n";
56}
57my $cdir = dirname("$newroot/$destname");
58my $outputfile="$newroot/$destname";
59$outputfile="$newroot" if ($destname =~ /^\s*$/);
60if (-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}
87if (-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}
Note: See TracBrowser for help on using the repository browser.